1 ;;; nxml-outln.el --- outline support for nXML mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 2004, 2007-2017 Free Software Foundation, Inc.
6 ;; Keywords: wp, hypermedia, languages, XML
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; A section can be in one of three states
26 ;; 1. display normally; this displays each child section
27 ;; according to its state; anything not part of child sections is also
29 ;; 2. display just the title specially; child sections are not displayed
30 ;; regardless of their state; anything not part of child sections is
32 ;; 3. display the title specially and display child sections
33 ;; according to their state; anything not part of the child section is
35 ;; The state of a section is determined by the value of the
36 ;; nxml-outline-state text property of the < character that starts
38 ;; For state 1 the value is nil or absent.
39 ;; For state 2 it is the symbol hide-children.
40 ;; For state 3 it is t.
41 ;; The special display is achieved by using overlays. The overlays
42 ;; are computed from the nxml-outline-state property by
43 ;; `nxml-refresh-outline'. There overlays all have a category property
44 ;; with an nxml-outline-display property with value t.
46 ;; For a section to be recognized as such, the following conditions must
48 ;; - its start-tag must occur at the start of a line (possibly indented)
49 ;; - its local name must match `nxml-section-element-name-regexp'
50 ;; - it must have a heading element; a heading element is an
51 ;; element whose name matches `nxml-heading-element-name-regexp',
52 ;; and that occurs as, or as a descendant of, the first child element
55 ;; XXX What happens if an nxml-outline-state property is attached to a
56 ;; character that doesn't start a section element?
58 ;; An outlined section (an section with a non-nil nxml-outline-state
59 ;; property) can be displayed in either single-line or multi-line
60 ;; form. Single-line form is used when the outline state is hide-children
61 ;; or there are no child sections; multi-line form is used otherwise.
62 ;; There are two flavors of single-line form: with children and without.
63 ;; The with-children flavor is used when there are child sections.
64 ;; Single line with children looks like
65 ;; <+section>A section title...</>
66 ;; Single line without children looks like
67 ;; <-section>A section title...</>
68 ;; Multi line looks likes
69 ;; <-section>A section title...
70 ;; [child sections displayed here]
72 ;; The indent of an outlined section is computed relative to the
73 ;; outermost containing outlined element. The indent of the
74 ;; outermost containing element comes from the non-outlined
75 ;; indent of the section start-tag.
83 (defcustom nxml-section-element-name-regexp
84 "article\\|\\(sub\\)*section\\|chapter\\|div\\|appendix\\|part\\|preface\\|reference\\|simplesect\\|bibliography\\|bibliodiv\\|glossary\\|glossdiv"
85 "Regular expression matching the name of elements used as sections.
86 An XML element is treated as a section if:
88 - its local name (that is, the name without the prefix) matches
91 - either its first child element or a descendant of that first child
92 element has a local name matching the variable
93 `nxml-heading-element-name-regexp'; and
95 - its start-tag occurs at the beginning of a line (possibly indented)."
99 (defcustom nxml-heading-element-name-regexp
"title\\|head"
100 "Regular expression matching the name of elements used as headings.
101 An XML element is only recognized as a heading if it occurs as or
102 within the first child of an element that is recognized as a section.
103 See the variable `nxml-section-element-name-regexp' for more details."
107 (defcustom nxml-outline-child-indent
2
108 "Indentation in an outline for child element relative to parent element."
112 (defface nxml-heading
'((t :weight bold
))
113 "Face for the contents of abbreviated heading elements."
116 (defface nxml-outline-indicator
'((t))
117 "Face for `+' or `-' before element names in outlines."
120 (defface nxml-outline-active-indicator
121 '((t :box t
:inherit nxml-outline-indicator
))
122 "Face for clickable `+' or `-' before element names in outlines."
125 (defface nxml-outline-ellipsis
'((t :weight bold
))
126 "Face used for `...' in outlines."
129 (defvar nxml-heading-scan-distance
1000
130 "Maximum distance from section to scan for heading.")
132 (defvar nxml-outline-prefix-map
133 (let ((map (make-sparse-keymap)))
134 (define-key map
"\C-a" 'nxml-show-all
)
135 (define-key map
"\C-t" 'nxml-hide-all-text-content
)
136 (define-key map
"\C-r" 'nxml-refresh-outline
)
137 (define-key map
"\C-c" 'nxml-hide-direct-text-content
)
138 (define-key map
"\C-e" 'nxml-show-direct-text-content
)
139 (define-key map
"\C-d" 'nxml-hide-subheadings
)
140 (define-key map
"\C-s" 'nxml-show
)
141 (define-key map
"\C-k" 'nxml-show-subheadings
)
142 (define-key map
"\C-l" 'nxml-hide-text-content
)
143 (define-key map
"\C-i" 'nxml-show-direct-subheadings
)
144 (define-key map
"\C-o" 'nxml-hide-other
)
147 ;;; Commands for changing visibility
149 (defun nxml-show-all ()
150 "Show all elements in the buffer normally."
152 (with-silent-modifications
153 (remove-text-properties (point-min)
155 '(nxml-outline-state nil
)))
156 (nxml-outline-set-overlay nil
(point-min) (point-max)))
158 (defun nxml-hide-all-text-content ()
159 "Hide all text content in the buffer.
160 Anything that is in a section but is not a heading will be hidden.
161 The visibility of headings at any level will not be changed. See the
162 variable `nxml-section-element-name-regexp' for more details on how to
163 customize which elements are recognized as sections and headings."
165 (nxml-transform-buffer-outline '((nil . t
))))
167 (defun nxml-show-direct-text-content ()
168 "Show the text content that is directly part of the section containing point.
169 Each subsection will be shown according to its individual state, which
170 will not be changed. The section containing point is the innermost
171 section that contains the character following point. See the variable
172 `nxml-section-element-name-regexp' for more details on how to
173 customize which elements are recognized as sections and headings."
175 (nxml-outline-pre-adjust-point)
176 (nxml-set-outline-state (nxml-section-start-position) nil
)
177 (nxml-refresh-outline)
178 (nxml-outline-adjust-point))
180 (defun nxml-show-direct-subheadings ()
181 "Show the immediate subheadings of the section containing point.
182 The section containing point is the innermost section that contains
183 the character following point. See the variable
184 `nxml-section-element-name-regexp' for more details on how to
185 customize which elements are recognized as sections and headings."
187 (let ((pos (nxml-section-start-position)))
188 (when (eq (nxml-get-outline-state pos
) 'hide-children
)
189 (nxml-set-outline-state pos t
)))
190 (nxml-refresh-outline)
191 (nxml-outline-adjust-point))
193 (defun nxml-hide-direct-text-content ()
194 "Hide the text content that is directly part of the section containing point.
195 The heading of the section will remain visible. The state of
196 subsections will not be changed. The section containing point is the
197 innermost section that contains the character following point. See the
198 variable `nxml-section-element-name-regexp' for more details on how to
199 customize which elements are recognized as sections and headings."
201 (let ((pos (nxml-section-start-position)))
202 (when (null (nxml-get-outline-state pos
))
203 (nxml-set-outline-state pos t
)))
204 (nxml-refresh-outline)
205 (nxml-outline-adjust-point))
207 (defun nxml-hide-subheadings ()
208 "Hide the subheadings that are part of the section containing point.
209 The text content will also be hidden, leaving only the heading of the
210 section itself visible. The state of the subsections will also be
211 changed to hide their headings, so that \\[nxml-show-direct-text-content]
212 would show only the heading of the subsections. The section containing
213 point is the innermost section that contains the character following
214 point. See the variable `nxml-section-element-name-regexp' for more
215 details on how to customize which elements are recognized as sections
218 (nxml-transform-subtree-outline '((nil . hide-children
)
219 (t . hide-children
))))
222 "Show the section containing point normally, without hiding anything.
223 This includes everything in the section at any level. The section
224 containing point is the innermost section that contains the character
225 following point. See the variable `nxml-section-element-name-regexp'
226 for more details on how to customize which elements are recognized as
227 sections and headings."
229 (nxml-transform-subtree-outline '((hide-children . nil
)
232 (defun nxml-hide-text-content ()
233 "Hide text content at all levels in the section containing point.
234 The section containing point is the innermost section that contains
235 the character following point. See the variable
236 `nxml-section-element-name-regexp' for more details on how to
237 customize which elements are recognized as sections and headings."
239 (nxml-transform-subtree-outline '((nil . t
))))
241 (defun nxml-show-subheadings ()
242 "Show the subheadings at all levels of the section containing point.
243 The visibility of the text content at all levels in the section is not
244 changed. The section containing point is the innermost section that
245 contains the character following point. See the variable
246 `nxml-section-element-name-regexp' for more details on how to
247 customize which elements are recognized as sections and headings."
249 (nxml-transform-subtree-outline '((hide-children . t
))))
251 ;; These variables are dynamically bound. They are use to pass information to
252 ;; nxml-section-tag-transform-outline-state.
254 (defvar nxml-outline-state-transform-exceptions nil
)
255 (defvar nxml-target-section-pos nil
)
256 (defvar nxml-depth-in-target-section nil
)
257 (defvar nxml-outline-state-transform-alist nil
)
259 (defvar nxml-outline-display-section-tag-function nil
)
261 (defun nxml-hide-other ()
262 "Hide text content other than that directly in the section containing point.
263 Hide headings other than those of ancestors of that section and their
264 immediate subheadings. The section containing point is the innermost
265 section that contains the character following point. See the variable
266 `nxml-section-element-name-regexp' for more details on how to
267 customize which elements are recognized as sections and headings."
269 (let ((nxml-outline-state-transform-exceptions nil
))
271 (while (and (condition-case err
272 (nxml-back-to-section-start)
273 (nxml-outline-error (nxml-report-outline-error
274 "Couldn't find containing section: %s"
277 (when (and nxml-outline-state-transform-exceptions
278 (null (nxml-get-outline-state (point))))
279 (nxml-set-outline-state (point) t
))
280 (setq nxml-outline-state-transform-exceptions
282 nxml-outline-state-transform-exceptions
))
283 (< nxml-prolog-end
(point))))
284 (goto-char (1- (point)))))
285 (nxml-transform-buffer-outline '((nil . hide-children
)
286 (t . hide-children
)))))
288 (defun nxml-transform-buffer-outline (alist)
289 (let ((nxml-target-section-pos nil
)
290 (nxml-depth-in-target-section 0)
291 (nxml-outline-state-transform-alist alist
)
292 (nxml-outline-display-section-tag-function
293 'nxml-section-tag-transform-outline-state
))
294 (nxml-refresh-outline))
295 (nxml-outline-adjust-point))
297 (defun nxml-transform-subtree-outline (alist)
298 (let ((nxml-target-section-pos (nxml-section-start-position))
299 (nxml-depth-in-target-section nil
)
300 (nxml-outline-state-transform-alist alist
)
301 (nxml-outline-display-section-tag-function
302 'nxml-section-tag-transform-outline-state
))
303 (nxml-refresh-outline))
304 (nxml-outline-adjust-point))
306 (defun nxml-outline-pre-adjust-point ()
307 (cond ((and (< (point-min) (point))
308 (get-char-property (1- (point)) 'invisible
)
309 (not (get-char-property (point) 'invisible
))
310 (let ((str (or (get-char-property (point) 'before-string
)
311 (get-char-property (point) 'display
))))
314 (string= (substring str
0 3) "..."))))
315 ;; The ellipsis is a display property on a visible character
316 ;; following an invisible region. The position of the event
317 ;; will be the position before that character. We want to
318 ;; move point to the other side of the invisible region, i.e.
319 ;; following the last visible character before that invisible
321 (goto-char (previous-single-char-property-change (1- (point))
323 ((and (< (point) (point-max))
324 (get-char-property (point) 'display
)
325 (get-char-property (1+ (point)) 'invisible
))
326 (goto-char (next-single-char-property-change (1+ (point))
328 ((and (< (point) (point-max))
329 (get-char-property (point) 'invisible
))
330 (goto-char (next-single-char-property-change (point)
333 (defun nxml-outline-adjust-point ()
334 "Adjust point after showing or hiding elements."
335 (when (and (get-char-property (point) 'invisible
)
336 (< (point-min) (point))
337 (get-char-property (1- (point)) 'invisible
))
338 (goto-char (previous-single-char-property-change (point)
343 (defun nxml-transform-outline-state (section-start-pos)
345 (nxml-get-outline-state section-start-pos
))
346 (change (assq old-state
347 nxml-outline-state-transform-alist
)))
349 (nxml-set-outline-state section-start-pos
352 (defun nxml-section-tag-transform-outline-state (startp
357 (setq nxml-depth-in-target-section
358 (and nxml-depth-in-target-section
359 (> nxml-depth-in-target-section
0)
360 (1- nxml-depth-in-target-section
)))
361 (cond (nxml-depth-in-target-section
362 (setq nxml-depth-in-target-section
363 (1+ nxml-depth-in-target-section
)))
364 ((= section-start-pos nxml-target-section-pos
)
365 (setq nxml-depth-in-target-section
0)))
366 (when (and nxml-depth-in-target-section
367 (not (member section-start-pos
368 nxml-outline-state-transform-exceptions
)))
369 (nxml-transform-outline-state section-start-pos
))))
371 (defun nxml-get-outline-state (pos)
372 (get-text-property pos
'nxml-outline-state
))
374 (defun nxml-set-outline-state (pos state
)
375 (with-silent-modifications
377 (put-text-property pos
(1+ pos
) 'nxml-outline-state state
)
378 (remove-text-properties pos
(1+ pos
) '(nxml-outline-state nil
)))))
382 (defun nxml-mouse-show-direct-text-content (event)
383 "Do the same as \\[nxml-show-direct-text-content] from a mouse click."
385 (and (nxml-mouse-set-point event
)
386 (nxml-show-direct-text-content)))
388 (defun nxml-mouse-hide-direct-text-content (event)
389 "Do the same as \\[nxml-hide-direct-text-content] from a mouse click."
391 (and (nxml-mouse-set-point event
)
392 (nxml-hide-direct-text-content)))
394 (defun nxml-mouse-hide-subheadings (event)
395 "Do the same as \\[nxml-hide-subheadings] from a mouse click."
397 (and (nxml-mouse-set-point event
)
398 (nxml-hide-subheadings)))
400 (defun nxml-mouse-show-direct-subheadings (event)
401 "Do the same as \\[nxml-show-direct-subheadings] from a mouse click."
403 (and (nxml-mouse-set-point event
)
404 (nxml-show-direct-subheadings)))
406 (defun nxml-mouse-set-point (event)
407 (mouse-set-point event
)
408 (and nxml-prolog-end t
))
412 (defsubst nxml-token-start-tag-p
()
413 (or (eq xmltok-type
'start-tag
)
414 (eq xmltok-type
'partial-start-tag
)))
416 (defsubst nxml-token-end-tag-p
()
417 (or (eq xmltok-type
'end-tag
)
418 (eq xmltok-type
'partial-end-tag
)))
420 (defun nxml-refresh-outline ()
421 "Refresh the outline to correspond to the current XML element structure."
424 (goto-char (point-min))
425 (kill-local-variable 'line-move-ignore-invisible
)
426 (make-local-variable 'line-move-ignore-invisible
)
428 (nxml-outline-display-rest nil nil nil
)
430 (nxml-report-outline-error "Cannot display outline: %s" err
)))))
432 (defun nxml-outline-display-rest (outline-state start-tag-indent tag-qnames
)
433 "Display up to and including the end of the current element.
434 OUTLINE-STATE can be nil, t, hide-children. START-TAG-INDENT is the
435 indent of the start-tag of the current element, or nil if no
436 containing element has a non-nil OUTLINE-STATE. TAG-QNAMES is a list
437 of the qnames of the open elements. Point is after the title content.
438 Leave point after the closing end-tag. Return t if we had a
439 non-transparent child section."
440 (let ((last-pos (point))
441 (transparent-depth 0)
442 ;; don't want ellipsis before root element
443 (had-children (not tag-qnames
)))
445 (cond ((not (nxml-section-tag-forward))
446 (if (null tag-qnames
)
448 (nxml-outline-error "missing end-tag %s"
451 ((nxml-token-end-tag-p)
452 (when nxml-outline-display-section-tag-function
453 (funcall nxml-outline-display-section-tag-function
456 (let ((qname (xmltok-end-tag-qname)))
458 (nxml-outline-error "extra end-tag %s" qname
))
459 (unless (string= (car tag-qnames
) qname
)
460 (nxml-outline-error "mismatched end-tag; expected %s, got %s"
463 (cond ((> transparent-depth
0)
464 (setq transparent-depth
(1- transparent-depth
))
465 (setq tag-qnames
(cdr tag-qnames
))
468 (nxml-outline-set-overlay nil last-pos
(point))
470 ((or (not had-children
)
471 (eq outline-state
'hide-children
))
472 (nxml-outline-display-single-line-end-tag last-pos
)
475 (nxml-outline-display-multi-line-end-tag last-pos
480 (let* ((qname (xmltok-start-tag-qname))
481 (section-start-pos xmltok-start
)
483 (and (or nxml-outline-display-section-tag-function
484 (not (eq outline-state
'had-children
))
486 (nxml-token-starts-line-p)
487 (nxml-heading-start-position))))
488 (when nxml-outline-display-section-tag-function
489 (funcall nxml-outline-display-section-tag-function
493 (setq tag-qnames
(cons qname tag-qnames
))
494 (if (or (not heading-start-pos
)
495 (and (eq outline-state
'hide-children
)
496 (setq had-children t
)))
497 (setq transparent-depth
(1+ transparent-depth
))
498 (nxml-display-section last-pos
505 (setq had-children t
)
506 (setq tag-qnames
(cdr tag-qnames
))
507 (setq last-pos
(point))))
511 (defconst nxml-highlighted-less-than
512 (propertize "<" 'face
'nxml-tag-delimiter
))
514 (defconst nxml-highlighted-greater-than
515 (propertize ">" 'face
'nxml-tag-delimiter
))
517 (defconst nxml-highlighted-colon
518 (propertize ":" 'face
'nxml-element-colon
))
520 (defconst nxml-highlighted-slash
521 (propertize "/" 'face
'nxml-tag-slash
))
523 (defconst nxml-highlighted-ellipsis
524 (propertize "..." 'face
'nxml-outline-ellipsis
))
526 (defconst nxml-highlighted-empty-end-tag
527 (concat nxml-highlighted-ellipsis
528 nxml-highlighted-less-than
529 nxml-highlighted-slash
530 nxml-highlighted-greater-than
))
532 (defconst nxml-highlighted-inactive-minus
533 (propertize "-" 'face
'nxml-outline-indicator
))
535 (defconst nxml-highlighted-active-minus
536 (propertize "-" 'face
'nxml-outline-active-indicator
))
538 (defconst nxml-highlighted-active-plus
539 (propertize "+" 'face
'nxml-outline-active-indicator
))
541 (defun nxml-display-section (last-pos
548 (let* ((section-start-pos-bol
550 (goto-char section-start-pos
)
551 (skip-chars-backward " \t")
553 (outline-state (nxml-get-outline-state section-start-pos
))
554 (newline-before-section-start-category
555 (cond ((and (not had-children
) parent-outline-state
)
556 'nxml-outline-display-ellipsis
)
557 (outline-state 'nxml-outline-display-show
)
559 (nxml-outline-set-overlay (and parent-outline-state
560 'nxml-outline-display-hide
)
562 (1- section-start-pos-bol
)
566 (let* ((indent (if parent-indent
567 (+ parent-indent nxml-outline-child-indent
)
569 (goto-char section-start-pos
)
572 (nxml-outline-set-overlay newline-before-section-start-category
573 (1- section-start-pos-bol
)
574 section-start-pos-bol
576 (nxml-outline-set-overlay 'nxml-outline-display-hide
577 section-start-pos-bol
579 (setq start-tag-overlay
580 (nxml-outline-set-overlay 'nxml-outline-display-show
582 (1+ section-start-pos
)
584 ;; line motion commands don't work right if start-tag-overlay
585 ;; covers multiple lines
586 (nxml-outline-set-overlay 'nxml-outline-display-hide
587 (1+ section-start-pos
)
589 (goto-char heading-start-pos
)
590 (nxml-end-of-heading)
591 (nxml-outline-set-overlay 'nxml-outline-display-heading
595 (nxml-outline-display-rest outline-state
598 (overlay-put start-tag-overlay
602 (make-string indent ?\
)
604 nxml-highlighted-less-than
606 (cond ((not had-children
)
607 nxml-highlighted-inactive-minus
)
608 ((eq outline-state
'hide-children
)
609 (overlay-put start-tag-overlay
611 'nxml-outline-display-hiding-tag
)
612 nxml-highlighted-active-plus
)
614 (overlay-put start-tag-overlay
616 'nxml-outline-display-showing-tag
)
617 nxml-highlighted-active-minus
))
619 (nxml-highlighted-qname (car tag-qnames
))
621 nxml-highlighted-greater-than
))))
623 (goto-char heading-start-pos
)
624 (nxml-end-of-heading)
625 (nxml-outline-set-overlay newline-before-section-start-category
626 (1- section-start-pos-bol
)
629 (nxml-outline-display-rest outline-state
632 nxml-outline-child-indent
))
635 (defun nxml-highlighted-qname (qname)
636 (let ((colon (string-match ":" qname
)))
638 (concat (propertize (substring qname
0 colon
)
640 'nxml-element-prefix
)
641 nxml-highlighted-colon
642 (propertize (substring qname
(1+ colon
))
644 'nxml-element-local-name
))
647 'nxml-element-local-name
))))
649 (defun nxml-outline-display-single-line-end-tag (last-pos)
650 (nxml-outline-set-overlay 'nxml-outline-display-hide
655 (overlay-put (nxml-outline-set-overlay 'nxml-outline-display-show
660 nxml-highlighted-empty-end-tag
))
662 (defun nxml-outline-display-multi-line-end-tag (last-pos start-tag-indent
)
663 (let ((indentp (save-excursion
665 (skip-chars-forward " \t")
666 (and (eq (char-after) ?
\n)
668 (goto-char (1+ (point)))
669 (nxml-outline-set-overlay nil last-pos
(point))
670 (setq last-pos
(point))
671 (goto-char xmltok-start
)
675 (nxml-outline-set-overlay 'nxml-outline-display-hide
680 (setq end-tag-overlay
681 (nxml-outline-set-overlay 'nxml-outline-display-showing-tag
685 (overlay-put end-tag-overlay
688 (make-string start-tag-indent ?\
)
690 nxml-highlighted-less-than
691 nxml-highlighted-slash
692 nxml-highlighted-active-minus
693 (nxml-highlighted-qname (xmltok-end-tag-qname))
694 nxml-highlighted-greater-than
))))
696 (defvar nxml-outline-show-map
697 (let ((map (make-sparse-keymap)))
698 (define-key map
"\C-m" 'nxml-show-direct-text-content
)
699 (define-key map
[mouse-2
] 'nxml-mouse-show-direct-text-content
)
702 (defvar nxml-outline-show-help
"mouse-2: show")
704 (put 'nxml-outline-display-show
'nxml-outline-display t
)
705 (put 'nxml-outline-display-show
'evaporate t
)
706 (put 'nxml-outline-display-show
'keymap nxml-outline-show-map
)
707 (put 'nxml-outline-display-show
'help-echo nxml-outline-show-help
)
709 (put 'nxml-outline-display-hide
'nxml-outline-display t
)
710 (put 'nxml-outline-display-hide
'evaporate t
)
711 (put 'nxml-outline-display-hide
'invisible t
)
712 (put 'nxml-outline-display-hide
'keymap nxml-outline-show-map
)
713 (put 'nxml-outline-display-hide
'help-echo nxml-outline-show-help
)
715 (put 'nxml-outline-display-ellipsis
'nxml-outline-display t
)
716 (put 'nxml-outline-display-ellipsis
'evaporate t
)
717 (put 'nxml-outline-display-ellipsis
'keymap nxml-outline-show-map
)
718 (put 'nxml-outline-display-ellipsis
'help-echo nxml-outline-show-help
)
719 (put 'nxml-outline-display-ellipsis
'before-string nxml-highlighted-ellipsis
)
721 (put 'nxml-outline-display-heading
'keymap nxml-outline-show-map
)
722 (put 'nxml-outline-display-heading
'help-echo nxml-outline-show-help
)
723 (put 'nxml-outline-display-heading
'nxml-outline-display t
)
724 (put 'nxml-outline-display-heading
'evaporate t
)
725 (put 'nxml-outline-display-heading
'face
'nxml-heading
)
727 (defvar nxml-outline-hiding-tag-map
728 (let ((map (make-sparse-keymap)))
729 (define-key map
[mouse-1
] 'nxml-mouse-show-direct-subheadings
)
730 (define-key map
[mouse-2
] 'nxml-mouse-show-direct-text-content
)
731 (define-key map
"\C-m" 'nxml-show-direct-text-content
)
734 (defvar nxml-outline-hiding-tag-help
735 "mouse-1: show subheadings, mouse-2: show text content")
737 (put 'nxml-outline-display-hiding-tag
'nxml-outline-display t
)
738 (put 'nxml-outline-display-hiding-tag
'evaporate t
)
739 (put 'nxml-outline-display-hiding-tag
'keymap nxml-outline-hiding-tag-map
)
740 (put 'nxml-outline-display-hiding-tag
'help-echo nxml-outline-hiding-tag-help
)
742 (defvar nxml-outline-showing-tag-map
743 (let ((map (make-sparse-keymap)))
744 (define-key map
[mouse-1
] 'nxml-mouse-hide-subheadings
)
745 (define-key map
[mouse-2
] 'nxml-mouse-show-direct-text-content
)
746 (define-key map
"\C-m" 'nxml-show-direct-text-content
)
749 (defvar nxml-outline-showing-tag-help
750 "mouse-1: hide subheadings, mouse-2: show text content")
752 (put 'nxml-outline-display-showing-tag
'nxml-outline-display t
)
753 (put 'nxml-outline-display-showing-tag
'evaporate t
)
754 (put 'nxml-outline-display-showing-tag
'keymap nxml-outline-showing-tag-map
)
755 (put 'nxml-outline-display-showing-tag
757 nxml-outline-showing-tag-help
)
759 (defun nxml-outline-set-overlay (category
765 "Replace any `nxml-outline-display' overlays between START and END.
766 Overlays are removed if they overlay the region between START and END,
767 and have a non-nil `nxml-outline-display' property (typically via their
768 category). If CATEGORY is non-nil, they will be replaced with a new
769 overlay with that category from START to END. If CATEGORY is nil,
770 no new overlay will be created."
772 (let ((overlays (overlays-in start end
))
775 (setq overlay
(car overlays
))
776 (setq overlays
(cdr overlays
))
777 (when (overlay-get overlay
'nxml-outline-display
)
778 (delete-overlay overlay
))))
780 (let ((overlay (make-overlay start
785 (overlay-put overlay
'category category
)
786 (setq line-move-ignore-invisible t
)
789 (defun nxml-end-of-heading ()
790 "Move from the start of the content of the heading to the end.
791 Do not move past the end of the line."
792 (let ((pos (condition-case nil
793 (and (nxml-scan-element-forward (point) t
)
795 (nxml-scan-error nil
))))
797 (skip-chars-backward " \t")
799 (setq pos
(nxml-token-before))
800 (when (eq xmltok-type
'end-tag
)
804 (skip-chars-backward " \t")
807 ;;; Navigating section structure
809 (defun nxml-token-starts-line-p ()
811 (goto-char xmltok-start
)
812 (skip-chars-backward " \t")
815 (defvar nxml-cached-section-tag-regexp nil
)
816 (defvar nxml-cached-section-element-name-regexp nil
)
818 (defsubst nxml-make-section-tag-regexp
()
819 (if (eq nxml-cached-section-element-name-regexp
820 nxml-section-element-name-regexp
)
821 nxml-cached-section-tag-regexp
822 (nxml-make-section-tag-regexp-1)))
824 (defun nxml-make-section-tag-regexp-1 ()
825 (setq nxml-cached-section-element-name-regexp nil
)
826 (setq nxml-cached-section-tag-regexp
828 "\\(" xmltok-ncname-regexp
":\\)?"
829 nxml-section-element-name-regexp
831 (setq nxml-cached-section-element-name-regexp
832 nxml-section-element-name-regexp
)
833 nxml-cached-section-tag-regexp
)
835 (defun nxml-section-tag-forward ()
836 "Move forward past the first tag that is a section start- or end-tag.
837 Return `xmltok-type' for tag.
838 If no tag found, return nil and move to the end of the buffer."
839 (let ((case-fold-search nil
)
840 (tag-regexp (nxml-make-section-tag-regexp))
842 (when (< (point) nxml-prolog-end
)
843 (goto-char nxml-prolog-end
))
844 (while (cond ((not (re-search-forward tag-regexp nil
'move
))
845 (setq xmltok-type nil
)
848 (goto-char (match-beginning 0))
849 (setq match-end
(match-end 0))
850 (nxml-ensure-scan-up-to-date)
851 (let ((end (nxml-inside-end (point))))
857 (and (memq xmltok-type
'(start-tag
861 ;; just in case wildcard matched non-name chars
862 (= xmltok-name-end
(1- match-end
))))
867 (defun nxml-section-tag-backward ()
868 "Move backward to the end of a tag that is a section start- or end-tag.
869 The position of the end of the tag must be <= point.
870 Point is at the end of the tag. `xmltok-start' is the start."
871 (let ((case-fold-search nil
)
873 (tag-regexp (nxml-make-section-tag-regexp))
875 (if (< (point) nxml-prolog-end
)
877 (goto-char (point-min))
879 (while (cond ((not (re-search-backward tag-regexp
882 (setq xmltok-type nil
)
883 (goto-char (point-min))
886 (goto-char (match-beginning 0))
887 (setq match-end
(match-end 0))
888 (nxml-ensure-scan-up-to-date)
889 (let ((pos (nxml-inside-start (point))))
895 (and (<= (point) start
)
896 (memq xmltok-type
'(start-tag
900 ;; just in case wildcard matched non-name chars
901 (= xmltok-name-end
(1- match-end
))))
903 (t (goto-char xmltok-start
)
907 (defun nxml-section-start-position ()
908 "Return the position of the start of the section containing point.
909 Signal an error on failure."
911 (save-excursion (if (nxml-back-to-section-start)
913 (error "Not in section")))
915 (nxml-report-outline-error "Couldn't determine containing section: %s"
918 (defun nxml-back-to-section-start (&optional invisible-ok
)
919 "Try to move back to the start of the section containing point.
920 The start of the section must be <= point.
921 Only visible sections are included unless INVISIBLE-OK is non-nil.
922 If found, return t. Otherwise move to `point-min' and return nil.
923 If unbalanced section tags are found, signal an `nxml-outline-error'."
924 (when (or (nxml-after-section-start-tag)
925 (nxml-section-tag-backward))
926 (let (open-tags found
)
927 (while (let (section-start-pos)
928 (setq section-start-pos xmltok-start
)
929 (if (nxml-token-end-tag-p)
930 (setq open-tags
(cons (xmltok-end-tag-qname)
933 (when (and (nxml-token-starts-line-p)
935 (not (get-char-property section-start-pos
937 (nxml-heading-start-position))
939 (let ((qname (xmltok-start-tag-qname)))
940 (unless (string= (car open-tags
) qname
)
941 (nxml-outline-error "mismatched end-tag"))
942 (setq open-tags
(cdr open-tags
)))))
943 (goto-char section-start-pos
)
945 (nxml-section-tag-backward))))
948 (defun nxml-after-section-start-tag ()
949 "If the character after point is in a section start-tag, move after it.
950 Return the token type. Otherwise return nil.
951 Set up variables like `xmltok-forward'."
952 (let ((pos (nxml-token-after))
953 (case-fold-search nil
))
954 (when (and (memq xmltok-type
'(start-tag partial-start-tag
))
956 (goto-char xmltok-start
)
957 (looking-at (nxml-make-section-tag-regexp))))
961 (defun nxml-heading-start-position ()
962 "Return the position of the start of the content of a heading element.
963 Adjust the position to be after initial leading whitespace.
964 Return nil if no heading element is found. Requires point to be
965 immediately after the section's start-tag."
967 (heading-regexp (concat "\\`\\("
968 nxml-heading-element-name-regexp
971 (section-regexp (concat "\\`\\("
972 nxml-section-element-name-regexp
977 (while (and (xmltok-forward)
978 (cond ((memq xmltok-type
'(end-tag partial-end-tag
))
979 (and (not (string-match section-regexp
980 (xmltok-end-tag-local-name)))
982 (setq depth
(1- depth
))))
983 ;; XXX Not sure whether this is a good idea
984 ;;((eq xmltok-type 'empty-element)
986 ((not (memq xmltok-type
987 '(start-tag partial-start-tag
)))
989 ((string-match section-regexp
990 (xmltok-start-tag-local-name))
992 ((string-match heading-regexp
993 (xmltok-start-tag-local-name))
994 (skip-chars-forward " \t\r\n")
998 (setq depth
(1+ depth
))
1000 (<= (- (point) start
) nxml-heading-scan-distance
))))
1005 (defun nxml-report-outline-error (msg err
)
1006 (error msg
(apply #'format-message
(cdr err
))))
1008 (defun nxml-outline-error (&rest args
)
1009 (signal 'nxml-outline-error args
))
1011 (define-error 'nxml-outline-error
1012 "Cannot create outline of buffer that is not well-formed" 'nxml-error
)
1016 (defun nxml-debug-overlays ()
1018 (let ((overlays (nreverse (overlays-in (point-min) (point-max))))
1021 (setq overlay
(car overlays
))
1022 (setq overlays
(cdr overlays
))
1023 (when (overlay-get overlay
'nxml-outline-display
)
1024 (message "overlay %s: %s...%s (%s)"
1025 (overlay-get overlay
'category
)
1026 (overlay-start overlay
)
1027 (overlay-end overlay
)
1028 (overlay-get overlay
'display
))))))
1030 (provide 'nxml-outln
)
1032 ;;; nxml-outln.el ends here