I'm doing quite a bit of manual XML editing (the source definition of some code generation I'm doing is a custom XML format) and of course prefer to use Emacs over any special purpose (usually ugly) XML editors. nXml mode has stood me well in the past, but I cannot get my head around its "outline" support. Various internet and SO posts effectively say nothing - I'm wondering if anyone has any practical experience with outlining/folding XML in Emacs (any mode) whether or not that requires altering the XML structure itself.
4 Answers
I found this SO post: https://stackoverflow.com/questions/944614/emacs-does-hideshow-work-with-xml-mode-sgml-mode
(require 'hideshow)
(require 'sgml-mode)
(require 'nxml-mode)
(add-to-list 'hs-special-modes-alist
'(nxml-mode
"<!--\\|<[^/>]*[^/]>"
"-->\\|</[^/>]*[^/]>"
"<!--"
sgml-skip-tag-forward
nil))
(add-hook 'nxml-mode-hook 'hs-minor-mode)
;; optional key bindings, easier than hs defaults
(define-key nxml-mode-map (kbd "C-c h") 'hs-toggle-hiding)
You can use the code from there, slightly modified, for nxml-mode easily.
This will allow you to toggle hiding/unhiding of xml elements with C-ch and will support underscores in the names.
-
2
-
4@SeanAllred: I fixed the typo. The cut-and-pasters will thank you for the catch!– Dan ♦Nov 5, 2014 at 19:20
-
Brilliant! It also looks like it will be easy to write custom functions for
hideshow
like scanning a buffer to toggle all instances of a given tag etc. Would also be nice if clicking on an ellipsis with a mouse would un-hide, but I'll try not to shave the yak too much :)– MarkNov 6, 2014 at 5:21 -
@MarkAufflick hideshow has mouse support, by default, shift + mouse 2 is bound to hs-mouse-toggle-hiding, which should work exactly like you want it to. Nov 7, 2014 at 14:11
-
2+1000. This answer saved my sanity. I love nxml-mode but was switching constantly between it and a dedicated XML editor just for the latter's tag-folding support. I wonder, though, if it'd be possible to implement this functionality using the same library as dirtree.el, which supports fold/unfold with the mouse and line-drawing of the tree structure in GUI Emacsen? Nov 3, 2015 at 22:55
web-mode has element folding built in and bound to C-c C-f. But you will lose some of the features of using nxml-mode obviously.
-
I did not know about web-mode (don't do much web dev these days). I'll try it out and report back, thanks.– MarkNov 2, 2014 at 10:18
-
1So close! Unfortunately web-mode doesn't allow for underscores in xml tag names (which we use). Littered throughout the web-mode code are hundreds of near-same hard coded regex strings. I had a stab at working out which should be modified but it because exhausting! Otherwise, the folding in web-mode does indeed work :)– MarkNov 5, 2014 at 1:13
There is a quite wonderful gem called noxml-fold-mode
that exactly helps with that.
https://github.com/paddymcall/noxml-fold
It can do folding and it also has visibility cycling.
(add-to-list
'hs-special-modes-alist
'(nxml-mode
"<!--\\|<[^/>][^>]*>" "-->\\|</[^/>]+>" "<!--" #'nxml-forward-element nil))
(add-hook 'nxml-mode-hook #'hs-minor-mode)
;; (setcdr (assoc 'nxml-mode hs-special-modes-alist) (list "<!--\\|<[^/>][^>]*>" "-->\\|</[^/>]+>" "<!--" #'nxml-forward-element nil))
xsl
-- I incorporated a counter to deal with nested tags with the same name.