nxml-mode extensions
[emacs-init.git] / setup / nxml.el
1 (defvar nxml-where-elements-to-id 3)
2 (defvar nxml-where-max-elements 6)
3
4 (require 'cl)
5
6 (defun nxml-where ()
7   "Display the hierarchy of XML elements the point is on as a path."
8   (and (eq major-mode 'nxml-mode)
9            (let (path-to-id path-rest)
10                  (save-excursion
11                    (save-restriction
12                          (widen)
13                          (while (and (not (bobp))
14                                                  (condition-case nil (progn (nxml-backward-up-element) t) (error nil)))
15                            (multiple-value-bind 
16                                    (has-id step)
17                                    (loop with has-id = nil
18                          with step = (xmltok-start-tag-local-name)
19                                              for att in xmltok-attributes
20                                                  if (string= (xmltok-attribute-local-name att) "id")
21                                                  return (values t (concat "\"" (xmltok-attribute-value att) "\""))
22                                                  else if (string= (xmltok-attribute-local-name att) "name")
23                                                  do (setq has-id t step (concat "\"" (xmltok-attribute-value att) "\""))
24                                                  finally return (values has-id step ))
25                                  (if (or path-to-id has-id)
26                                          (setq path-to-id (cons step path-to-id))
27                                    (setq path-rest (cons step path-rest)))))))
28                  (let ((path-to-id-len (length path-to-id))
29                            (path-rest-len (length path-rest)))
30                    (if (> path-to-id-len nxml-where-elements-to-id)
31                            (progn
32                                  (setq path-to-id (nthcdr (- path-to-id-len nxml-where-elements-to-id -1) path-to-id))
33                                  (setq path-to-id (cons "..." path-to-id))
34                                  (setq path-to-id-len nxml-where-elements-to-id))
35                          (setq path-to-id (cons "" path-to-id)))
36                    (when (> (+ path-to-id-len path-rest-len) nxml-where-max-elements)
37                          (setq path-rest (nbutlast path-rest (- path-rest-len (- nxml-where-max-elements path-to-id-len) -1)))
38                          (setq path-rest (nconc path-rest (list "...")))))
39                  (mapconcat 'identity (nconc path-to-id path-rest) "/"))))
40   
41 (require 'which-func)
42
43 (add-to-list 'which-func-functions 'nxml-where)
44 (add-to-list 'which-func-modes 'nxml-mode)
45 (add-to-list 'which-func-non-auto-modes 'nxml-mode)
46
47 (add-to-list 'auto-mode-alist '("\\.xslt?\\'" . nxml-mode))
48 (add-to-list 'auto-mode-alist '("\\.xsd\\'" . nxml-mode))
49
50 (require 'hideshow)
51
52 (add-to-list 'hs-special-modes-alist '(nxml-mode ("\\(<[^/>]*>\\)$" 1)
53                                                                                                  "</[^/>]*>$"))
54 (defun nxml-enable-hs ()
55   (setq nxml-sexp-element-flag t)
56   (hs-minor-mode 1))
57
58 (add-hook 'nxml-mode-hook 'nxml-enable-hs)
59
60 (defun hs-nxml-enter ()
61   (interactive)
62   (when (hs-already-hidden-p)
63         (hs-show-block)
64         (hs-hide-level 1)
65         (nxml-forward-element)
66         (nxml-backward-element)))
67
68 (defun hs-nxml-leave ()
69   (interactive)
70   (nxml-backward-up-element)
71   (hs-hide-block)
72   (nxml-backward-up-element))
73
74 (defun hs-nxml-hide-other ()
75   (interactive)
76   (let ((p (point)))
77     (hs-hide-all)
78     (while (progn (goto-char p) (hs-already-hidden-p))
79       (hs-nxml-enter))
80     (hs-show-block)
81     (goto-char p)
82     (recenter-top-bottom)))
83
84 (define-key nxml-mode-map (kbd "\C-c <left>") 'hs-nxml-leave)
85 (define-key nxml-mode-map (kbd "\C-c <right>") 'hs-nxml-enter)
86 (define-key nxml-mode-map (kbd "\C-c @ o") 'hs-nxml-hide-other)
87
88 (defun nxml-complete-and-autoclose-element (use-region)
89   (interactive "P")
90   (let* ((start (if use-region (setq start (set-marker (make-marker) (region-beginning)))))
91          (end (setq use-region (set-marker (make-marker) (region-end))))
92          (beg (and start (= (point) start))))
93   (save-excursion
94     (insert " "))
95   (nxml-complete)
96   (let ((name (xmltok-start-tag-local-name)))
97     (delete-char 1)
98     (if (and start end)
99         (progn
100           (if (not beg)
101               (progn
102                 (delete-char (- (1+ (length name))))
103                 (goto-char start)
104                 (insert "<" name ">"))
105             (insert ">"))
106           (goto-char end))
107       (insert ">"))
108     (save-excursion
109       (insert "</" name ">")))))
110
111 (define-key nxml-mode-map (kbd "\C-c ." ) 'nxml-complete-and-autoclose-element)
112 (define-key nxml-mode-map (kbd "\C-c\C-c") 'recompile)
113
114 (defconst nxml-docbook-common-elements
115   '(("section" . ("para" "itemizedlist" "variablelist" "section" "bridgehead" "task" "procedure"))
116     ("para" . ("emphasis" "code" "replaceable"))
117     ("emphasis" . ("code"))
118     ("itemizedlist" . ("listitem"))
119     ("orderedlist" . ("listitem"))
120     ("variablelist" . ("varlistentry"))
121     ("varlistentry" . ("term" "listitem"))
122     ("term" . ("emphasis" "code"))
123     ("listitem" . ("para" "itemizedlist"))
124     ("task" . ("tasksummary" "procedure"))
125     ("tasksummary" . ("para"))
126     ("procedure" . ("step"))
127     ("step" . ("para" "procedure"))
128     ("mathphrase" . ("replaceable" "superscript" "subscript"))
129     ("title" . ("code" "replaceable"))
130     ("literallayout" . ("replaceable" "emphasis" "code"))))
131
132 (defvar nxml-docbook-last-common-element nil)
133
134 (defun nxml-docbook-make-common-element ()
135   (interactive)
136   (let ((start (set-marker (make-marker) (point)))
137         (end (set-marker (make-marker) (point)))
138         do-region)
139     (when (or (region-active-p)
140               (and (eq real-last-command 'nxml-docbook-make-common-element)
141                    (car nxml-docbook-last-common-element)))
142       (set-marker start (region-beginning))
143       (set-marker end (region-end))
144       (message "do-mark %s %s" start end)
145       (setq do-region t))
146     (message "cycle? %s %s" real-last-command nxml-docbook-last-common-element)
147     (when (and (eq real-last-command 'nxml-docbook-make-common-element)
148                (cdr nxml-docbook-last-common-element))
149       (delete-region (save-excursion (skip-chars-backward "^<") (1- (point))) start)
150       (delete-region end (save-excursion (skip-chars-forward "^>") (1+ (point)))))
151     (let* ((token-end (nxml-token-before)) 
152            (start-tag-end
153             (save-excursion
154               (when (and (< (point) token-end)
155                          (memq xmltok-type
156                                '(cdata-section
157                                  processing-instruction
158                                  comment
159                                  start-tag
160                                  end-tag
161                                  empty-element)))
162                 (setq nxml-docbook-last-common-element nil)
163                 (error "Point is inside a %s"
164                        (nxml-token-type-friendly-name xmltok-type)))
165               (nxml-scan-element-backward token-end t)))
166            (context (xmltok-start-tag-qname))
167            (elements (cdr (assoc context nxml-docbook-common-elements)))
168            (index (if (and elements
169                            (eq real-last-command 'nxml-docbook-make-common-element)
170                            (cdr nxml-docbook-last-common-element))
171                       (1+ (cdr nxml-docbook-last-common-element))
172                     0))
173            (element (and elements (nth index elements))))
174       (when (not elements)
175         (setq nxml-docbook-last-common-element nil)
176         (error "No common elements for %s" context))
177       (if element
178           (progn
179             (goto-char start)
180             (insert-before-markers "<" element ">")
181             (goto-char end)
182             (insert "</" element ">")
183             (goto-char end)
184             (setq nxml-docbook-last-common-element (cons do-region index)))
185         (setq nxml-docbook-last-common-element (cons do-region nil)))
186       (when do-region
187         (set-mark start)
188         (message  "Fiddlesticks: %s %s %s" (mark t) mark-active (region-active-p))))))
189
190 (defun nxml-just-one-space-or-skip-end ()
191   (interactive)
192   (if (looking-at "</")
193       (progn
194         (skip-chars-forward "^>")
195         (forward-char 1))
196     (just-one-space)))
197
198 (define-key nxml-mode-map (kbd "M-RET") 'nxml-docbook-make-common-element)
199 (define-key nxml-mode-map (kbd "M-SPC") 'nxml-just-one-space-or-skip-end)
200
201 (defun nxml-open-line ()
202   (interactive)
203   (open-line 1)
204   (save-excursion
205     (forward-line 1)
206     (indent-according-to-mode))
207   (newline-and-indent))
208
209 (define-key nxml-mode-map (kbd "M-o") 'nxml-open-line)