nxml-outln.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. ;;; nxml-outln.el --- outline support for nXML mode
  2. ;; Copyright (C) 2004, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: James Clark
  4. ;; Keywords: XML
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; A section can be in one of three states
  18. ;; 1. display normally; this displays each child section
  19. ;; according to its state; anything not part of child sections is also
  20. ;; displayed normally
  21. ;; 2. display just the title specially; child sections are not displayed
  22. ;; regardless of their state; anything not part of child sections is
  23. ;; not displayed
  24. ;; 3. display the title specially and display child sections
  25. ;; according to their state; anything not part of the child section is
  26. ;; not displayed
  27. ;; The state of a section is determined by the value of the
  28. ;; nxml-outline-state text property of the < character that starts
  29. ;; the section.
  30. ;; For state 1 the value is nil or absent.
  31. ;; For state 2 it is the symbol hide-children.
  32. ;; For state 3 it is t.
  33. ;; The special display is achieved by using overlays. The overlays
  34. ;; are computed from the nxml-outline-state property by
  35. ;; `nxml-refresh-outline'. There overlays all have a category property
  36. ;; with an nxml-outline-display property with value t.
  37. ;;
  38. ;; For a section to be recognized as such, the following conditions must
  39. ;; be satisfied:
  40. ;; - its start-tag must occur at the start of a line (possibly indented)
  41. ;; - its local name must match `nxml-section-element-name-regexp'
  42. ;; - it must have a heading element; a heading element is an
  43. ;; element whose name matches `nxml-heading-element-name-regexp',
  44. ;; and that occurs as, or as a descendant of, the first child element
  45. ;; of the section
  46. ;;
  47. ;; XXX What happens if an nxml-outline-state property is attached to a
  48. ;; character that doesn't start a section element?
  49. ;;
  50. ;; An outlined section (an section with a non-nil nxml-outline-state
  51. ;; property) can be displayed in either single-line or multi-line
  52. ;; form. Single-line form is used when the outline state is hide-children
  53. ;; or there are no child sections; multi-line form is used otherwise.
  54. ;; There are two flavors of single-line form: with children and without.
  55. ;; The with-children flavor is used when there are child sections.
  56. ;; Single line with children looks like
  57. ;; <+section>A section title...</>
  58. ;; Single line without children looks like
  59. ;; <-section>A section title...</>
  60. ;; Multi line looks likes
  61. ;; <-section>A section title...
  62. ;; [child sections displayed here]
  63. ;; </-section>
  64. ;; The indent of an outlined section is computed relative to the
  65. ;; outermost containing outlined element. The indent of the
  66. ;; outermost containing element comes from the non-outlined
  67. ;; indent of the section start-tag.
  68. ;;; Code:
  69. (require 'xmltok)
  70. (require 'nxml-util)
  71. (require 'nxml-rap)
  72. (defcustom nxml-section-element-name-regexp
  73. "article\\|\\(sub\\)*section\\|chapter\\|div\\|appendix\\|part\\|preface\\|reference\\|simplesect\\|bibliography\\|bibliodiv\\|glossary\\|glossdiv"
  74. "Regular expression matching the name of elements used as sections.
  75. An XML element is treated as a section if:
  76. - its local name (that is, the name without the prefix) matches
  77. this regexp;
  78. - either its first child element or a descendant of that first child
  79. element has a local name matching the variable
  80. `nxml-heading-element-name-regexp'; and
  81. - its start-tag occurs at the beginning of a line (possibly indented)."
  82. :group 'nxml
  83. :type 'regexp)
  84. (defcustom nxml-heading-element-name-regexp "title\\|head"
  85. "Regular expression matching the name of elements used as headings.
  86. An XML element is only recognized as a heading if it occurs as or
  87. within the first child of an element that is recognized as a section.
  88. See the variable `nxml-section-element-name-regexp' for more details."
  89. :group 'nxml
  90. :type 'regexp)
  91. (defcustom nxml-outline-child-indent 2
  92. "Indentation in an outline for child element relative to parent element."
  93. :group 'nxml
  94. :type 'integer)
  95. (defface nxml-heading '((t :weight bold))
  96. "Face for the contents of abbreviated heading elements."
  97. :group 'nxml-faces)
  98. (defface nxml-outline-indicator '((t))
  99. "Face for `+' or `-' before element names in outlines."
  100. :group 'nxml-faces)
  101. (defface nxml-outline-active-indicator
  102. '((t :box t :inherit nxml-outline-indicator))
  103. "Face for clickable `+' or `-' before element names in outlines."
  104. :group 'nxml-faces)
  105. (defface nxml-outline-ellipsis '((t :weight bold))
  106. "Face used for `...' in outlines."
  107. :group 'nxml-faces)
  108. (defvar nxml-heading-scan-distance 1000
  109. "Maximum distance from section to scan for heading.")
  110. (defvar nxml-outline-prefix-map
  111. (let ((map (make-sparse-keymap)))
  112. (define-key map "\C-a" 'nxml-show-all)
  113. (define-key map "\C-t" 'nxml-hide-all-text-content)
  114. (define-key map "\C-r" 'nxml-refresh-outline)
  115. (define-key map "\C-c" 'nxml-hide-direct-text-content)
  116. (define-key map "\C-e" 'nxml-show-direct-text-content)
  117. (define-key map "\C-d" 'nxml-hide-subheadings)
  118. (define-key map "\C-s" 'nxml-show)
  119. (define-key map "\C-k" 'nxml-show-subheadings)
  120. (define-key map "\C-l" 'nxml-hide-text-content)
  121. (define-key map "\C-i" 'nxml-show-direct-subheadings)
  122. (define-key map "\C-o" 'nxml-hide-other)
  123. map))
  124. ;;; Commands for changing visibility
  125. (defun nxml-show-all ()
  126. "Show all elements in the buffer normally."
  127. (interactive)
  128. (nxml-with-unmodifying-text-property-changes
  129. (remove-text-properties (point-min)
  130. (point-max)
  131. '(nxml-outline-state nil)))
  132. (nxml-outline-set-overlay nil (point-min) (point-max)))
  133. (defun nxml-hide-all-text-content ()
  134. "Hide all text content in the buffer.
  135. Anything that is in a section but is not a heading will be hidden.
  136. The visibility of headings at any level will not be changed. See the
  137. variable `nxml-section-element-name-regexp' for more details on how to
  138. customize which elements are recognized as sections and headings."
  139. (interactive)
  140. (nxml-transform-buffer-outline '((nil . t))))
  141. (defun nxml-show-direct-text-content ()
  142. "Show the text content that is directly part of the section containing point.
  143. Each subsection will be shown according to its individual state, which
  144. will not be changed. The section containing point is the innermost
  145. section that contains the character following point. See the variable
  146. `nxml-section-element-name-regexp' for more details on how to
  147. customize which elements are recognized as sections and headings."
  148. (interactive)
  149. (nxml-outline-pre-adjust-point)
  150. (nxml-set-outline-state (nxml-section-start-position) nil)
  151. (nxml-refresh-outline)
  152. (nxml-outline-adjust-point))
  153. (defun nxml-show-direct-subheadings ()
  154. "Show the immediate subheadings of the section containing point.
  155. The section containing point is the innermost section that contains
  156. the character following point. See the variable
  157. `nxml-section-element-name-regexp' for more details on how to
  158. customize which elements are recognized as sections and headings."
  159. (interactive)
  160. (let ((pos (nxml-section-start-position)))
  161. (when (eq (nxml-get-outline-state pos) 'hide-children)
  162. (nxml-set-outline-state pos t)))
  163. (nxml-refresh-outline)
  164. (nxml-outline-adjust-point))
  165. (defun nxml-hide-direct-text-content ()
  166. "Hide the text content that is directly part of the section containing point.
  167. The heading of the section will remain visible. The state of
  168. subsections will not be changed. The section containing point is the
  169. innermost section that contains the character following point. See the
  170. variable `nxml-section-element-name-regexp' for more details on how to
  171. customize which elements are recognized as sections and headings."
  172. (interactive)
  173. (let ((pos (nxml-section-start-position)))
  174. (when (null (nxml-get-outline-state pos))
  175. (nxml-set-outline-state pos t)))
  176. (nxml-refresh-outline)
  177. (nxml-outline-adjust-point))
  178. (defun nxml-hide-subheadings ()
  179. "Hide the subheadings that are part of the section containing point.
  180. The text content will also be hidden, leaving only the heading of the
  181. section itself visible. The state of the subsections will also be
  182. changed to hide their headings, so that \\[nxml-show-direct-text-content]
  183. would show only the heading of the subsections. The section containing
  184. point is the innermost section that contains the character following
  185. point. See the variable `nxml-section-element-name-regexp' for more
  186. details on how to customize which elements are recognized as sections
  187. and headings."
  188. (interactive)
  189. (nxml-transform-subtree-outline '((nil . hide-children)
  190. (t . hide-children))))
  191. (defun nxml-show ()
  192. "Show the section containing point normally, without hiding anything.
  193. This includes everything in the section at any level. The section
  194. containing point is the innermost section that contains the character
  195. following point. See the variable `nxml-section-element-name-regexp'
  196. for more details on how to customize which elements are recognized as
  197. sections and headings."
  198. (interactive)
  199. (nxml-transform-subtree-outline '((hide-children . nil)
  200. (t . nil))))
  201. (defun nxml-hide-text-content ()
  202. "Hide text content at all levels in the section containing point.
  203. The section containing point is the innermost section that contains
  204. the character following point. See the variable
  205. `nxml-section-element-name-regexp' for more details on how to
  206. customize which elements are recognized as sections and headings."
  207. (interactive)
  208. (nxml-transform-subtree-outline '((nil . t))))
  209. (defun nxml-show-subheadings ()
  210. "Show the subheadings at all levels of the section containing point.
  211. The visibility of the text content at all levels in the section is not
  212. changed. The section containing point is the innermost section that
  213. contains the character following point. See the variable
  214. `nxml-section-element-name-regexp' for more details on how to
  215. customize which elements are recognized as sections and headings."
  216. (interactive)
  217. (nxml-transform-subtree-outline '((hide-children . t))))
  218. (defun nxml-hide-other ()
  219. "Hide text content other than that directly in the section containing point.
  220. Hide headings other than those of ancestors of that section and their
  221. immediate subheadings. The section containing point is the innermost
  222. section that contains the character following point. See the variable
  223. `nxml-section-element-name-regexp' for more details on how to
  224. customize which elements are recognized as sections and headings."
  225. (interactive)
  226. (let ((nxml-outline-state-transform-exceptions nil))
  227. (save-excursion
  228. (while (and (condition-case err
  229. (nxml-back-to-section-start)
  230. (nxml-outline-error (nxml-report-outline-error
  231. "Couldn't find containing section: %s"
  232. err)))
  233. (progn
  234. (when (and nxml-outline-state-transform-exceptions
  235. (null (nxml-get-outline-state (point))))
  236. (nxml-set-outline-state (point) t))
  237. (setq nxml-outline-state-transform-exceptions
  238. (cons (point)
  239. nxml-outline-state-transform-exceptions))
  240. (< nxml-prolog-end (point))))
  241. (goto-char (1- (point)))))
  242. (nxml-transform-buffer-outline '((nil . hide-children)
  243. (t . hide-children)))))
  244. ;; These variables are dynamically bound. They are use to pass information to
  245. ;; nxml-section-tag-transform-outline-state.
  246. (defvar nxml-outline-state-transform-exceptions nil)
  247. (defvar nxml-target-section-pos nil)
  248. (defvar nxml-depth-in-target-section nil)
  249. (defvar nxml-outline-state-transform-alist nil)
  250. (defun nxml-transform-buffer-outline (alist)
  251. (let ((nxml-target-section-pos nil)
  252. (nxml-depth-in-target-section 0)
  253. (nxml-outline-state-transform-alist alist)
  254. (nxml-outline-display-section-tag-function
  255. 'nxml-section-tag-transform-outline-state))
  256. (nxml-refresh-outline))
  257. (nxml-outline-adjust-point))
  258. (defun nxml-transform-subtree-outline (alist)
  259. (let ((nxml-target-section-pos (nxml-section-start-position))
  260. (nxml-depth-in-target-section nil)
  261. (nxml-outline-state-transform-alist alist)
  262. (nxml-outline-display-section-tag-function
  263. 'nxml-section-tag-transform-outline-state))
  264. (nxml-refresh-outline))
  265. (nxml-outline-adjust-point))
  266. (defun nxml-outline-pre-adjust-point ()
  267. (cond ((and (< (point-min) (point))
  268. (get-char-property (1- (point)) 'invisible)
  269. (not (get-char-property (point) 'invisible))
  270. (let ((str (or (get-char-property (point) 'before-string)
  271. (get-char-property (point) 'display))))
  272. (and (stringp str)
  273. (>= (length str) 3)
  274. (string= (substring str 0 3) "..."))))
  275. ;; The ellipsis is a display property on a visible character
  276. ;; following an invisible region. The position of the event
  277. ;; will be the position before that character. We want to
  278. ;; move point to the other side of the invisible region, i.e.
  279. ;; following the last visible character before that invisible
  280. ;; region.
  281. (goto-char (previous-single-char-property-change (1- (point))
  282. 'invisible)))
  283. ((and (< (point) (point-max))
  284. (get-char-property (point) 'display)
  285. (get-char-property (1+ (point)) 'invisible))
  286. (goto-char (next-single-char-property-change (1+ (point))
  287. 'invisible)))
  288. ((and (< (point) (point-max))
  289. (get-char-property (point) 'invisible))
  290. (goto-char (next-single-char-property-change (point)
  291. 'invisible)))))
  292. (defun nxml-outline-adjust-point ()
  293. "Adjust point after showing or hiding elements."
  294. (when (and (get-char-property (point) 'invisible)
  295. (< (point-min) (point))
  296. (get-char-property (1- (point)) 'invisible))
  297. (goto-char (previous-single-char-property-change (point)
  298. 'invisible
  299. nil
  300. nxml-prolog-end))))
  301. (defun nxml-transform-outline-state (section-start-pos)
  302. (let* ((old-state
  303. (nxml-get-outline-state section-start-pos))
  304. (change (assq old-state
  305. nxml-outline-state-transform-alist)))
  306. (when change
  307. (nxml-set-outline-state section-start-pos
  308. (cdr change)))))
  309. (defun nxml-section-tag-transform-outline-state (startp
  310. section-start-pos
  311. &optional
  312. heading-start-pos)
  313. (if (not startp)
  314. (setq nxml-depth-in-target-section
  315. (and nxml-depth-in-target-section
  316. (> nxml-depth-in-target-section 0)
  317. (1- nxml-depth-in-target-section)))
  318. (cond (nxml-depth-in-target-section
  319. (setq nxml-depth-in-target-section
  320. (1+ nxml-depth-in-target-section)))
  321. ((= section-start-pos nxml-target-section-pos)
  322. (setq nxml-depth-in-target-section 0)))
  323. (when (and nxml-depth-in-target-section
  324. (not (member section-start-pos
  325. nxml-outline-state-transform-exceptions)))
  326. (nxml-transform-outline-state section-start-pos))))
  327. (defun nxml-get-outline-state (pos)
  328. (get-text-property pos 'nxml-outline-state))
  329. (defun nxml-set-outline-state (pos state)
  330. (nxml-with-unmodifying-text-property-changes
  331. (if state
  332. (put-text-property pos (1+ pos) 'nxml-outline-state state)
  333. (remove-text-properties pos (1+ pos) '(nxml-outline-state nil)))))
  334. ;;; Mouse interface
  335. (defun nxml-mouse-show-direct-text-content (event)
  336. "Do the same as \\[nxml-show-direct-text-content] from a mouse click."
  337. (interactive "e")
  338. (and (nxml-mouse-set-point event)
  339. (nxml-show-direct-text-content)))
  340. (defun nxml-mouse-hide-direct-text-content (event)
  341. "Do the same as \\[nxml-hide-direct-text-content] from a mouse click."
  342. (interactive "e")
  343. (and (nxml-mouse-set-point event)
  344. (nxml-hide-direct-text-content)))
  345. (defun nxml-mouse-hide-subheadings (event)
  346. "Do the same as \\[nxml-hide-subheadings] from a mouse click."
  347. (interactive "e")
  348. (and (nxml-mouse-set-point event)
  349. (nxml-hide-subheadings)))
  350. (defun nxml-mouse-show-direct-subheadings (event)
  351. "Do the same as \\[nxml-show-direct-subheadings] from a mouse click."
  352. (interactive "e")
  353. (and (nxml-mouse-set-point event)
  354. (nxml-show-direct-subheadings)))
  355. (defun nxml-mouse-set-point (event)
  356. (mouse-set-point event)
  357. (and nxml-prolog-end t))
  358. ;; Display
  359. (defsubst nxml-token-start-tag-p ()
  360. (or (eq xmltok-type 'start-tag)
  361. (eq xmltok-type 'partial-start-tag)))
  362. (defsubst nxml-token-end-tag-p ()
  363. (or (eq xmltok-type 'end-tag)
  364. (eq xmltok-type 'partial-end-tag)))
  365. (defun nxml-refresh-outline ()
  366. "Refresh the outline to correspond to the current XML element structure."
  367. (interactive)
  368. (save-excursion
  369. (goto-char (point-min))
  370. (kill-local-variable 'line-move-ignore-invisible)
  371. (make-local-variable 'line-move-ignore-invisible)
  372. (condition-case err
  373. (nxml-outline-display-rest nil nil nil)
  374. (nxml-outline-error
  375. (nxml-report-outline-error "Cannot display outline: %s" err)))))
  376. (defvar nxml-outline-display-section-tag-function nil)
  377. (defun nxml-outline-display-rest (outline-state start-tag-indent tag-qnames)
  378. "Display up to and including the end of the current element.
  379. OUTLINE-STATE can be nil, t, hide-children. START-TAG-INDENT is the
  380. indent of the start-tag of the current element, or nil if no
  381. containing element has a non-nil OUTLINE-STATE. TAG-QNAMES is a list
  382. of the qnames of the open elements. Point is after the title content.
  383. Leave point after the closing end-tag. Return t if we had a
  384. non-transparent child section."
  385. (let ((last-pos (point))
  386. (transparent-depth 0)
  387. ;; don't want ellipsis before root element
  388. (had-children (not tag-qnames)))
  389. (while
  390. (cond ((not (nxml-section-tag-forward))
  391. (if (null tag-qnames)
  392. nil
  393. (nxml-outline-error "missing end-tag %s"
  394. (car tag-qnames))))
  395. ;; section end-tag
  396. ((nxml-token-end-tag-p)
  397. (when nxml-outline-display-section-tag-function
  398. (funcall nxml-outline-display-section-tag-function
  399. nil
  400. xmltok-start))
  401. (let ((qname (xmltok-end-tag-qname)))
  402. (unless tag-qnames
  403. (nxml-outline-error "extra end-tag %s" qname))
  404. (unless (string= (car tag-qnames) qname)
  405. (nxml-outline-error "mismatched end-tag; expected %s, got %s"
  406. (car tag-qnames)
  407. qname)))
  408. (cond ((> transparent-depth 0)
  409. (setq transparent-depth (1- transparent-depth))
  410. (setq tag-qnames (cdr tag-qnames))
  411. t)
  412. ((not outline-state)
  413. (nxml-outline-set-overlay nil last-pos (point))
  414. nil)
  415. ((or (not had-children)
  416. (eq outline-state 'hide-children))
  417. (nxml-outline-display-single-line-end-tag last-pos)
  418. nil)
  419. (t
  420. (nxml-outline-display-multi-line-end-tag last-pos
  421. start-tag-indent)
  422. nil)))
  423. ;; section start-tag
  424. (t
  425. (let* ((qname (xmltok-start-tag-qname))
  426. (section-start-pos xmltok-start)
  427. (heading-start-pos
  428. (and (or nxml-outline-display-section-tag-function
  429. (not (eq outline-state 'had-children))
  430. (not had-children))
  431. (nxml-token-starts-line-p)
  432. (nxml-heading-start-position))))
  433. (when nxml-outline-display-section-tag-function
  434. (funcall nxml-outline-display-section-tag-function
  435. t
  436. section-start-pos
  437. heading-start-pos))
  438. (setq tag-qnames (cons qname tag-qnames))
  439. (if (or (not heading-start-pos)
  440. (and (eq outline-state 'hide-children)
  441. (setq had-children t)))
  442. (setq transparent-depth (1+ transparent-depth))
  443. (nxml-display-section last-pos
  444. section-start-pos
  445. heading-start-pos
  446. start-tag-indent
  447. outline-state
  448. had-children
  449. tag-qnames)
  450. (setq had-children t)
  451. (setq tag-qnames (cdr tag-qnames))
  452. (setq last-pos (point))))
  453. t)))
  454. had-children))
  455. (defconst nxml-highlighted-less-than
  456. (propertize "<" 'face 'nxml-tag-delimiter))
  457. (defconst nxml-highlighted-greater-than
  458. (propertize ">" 'face 'nxml-tag-delimiter))
  459. (defconst nxml-highlighted-colon
  460. (propertize ":" 'face 'nxml-element-colon))
  461. (defconst nxml-highlighted-slash
  462. (propertize "/" 'face 'nxml-tag-slash))
  463. (defconst nxml-highlighted-ellipsis
  464. (propertize "..." 'face 'nxml-outline-ellipsis))
  465. (defconst nxml-highlighted-empty-end-tag
  466. (concat nxml-highlighted-ellipsis
  467. nxml-highlighted-less-than
  468. nxml-highlighted-slash
  469. nxml-highlighted-greater-than))
  470. (defconst nxml-highlighted-inactive-minus
  471. (propertize "-" 'face 'nxml-outline-indicator))
  472. (defconst nxml-highlighted-active-minus
  473. (propertize "-" 'face 'nxml-outline-active-indicator))
  474. (defconst nxml-highlighted-active-plus
  475. (propertize "+" 'face 'nxml-outline-active-indicator))
  476. (defun nxml-display-section (last-pos
  477. section-start-pos
  478. heading-start-pos
  479. parent-indent
  480. parent-outline-state
  481. had-children
  482. tag-qnames)
  483. (let* ((section-start-pos-bol
  484. (save-excursion
  485. (goto-char section-start-pos)
  486. (skip-chars-backward " \t")
  487. (point)))
  488. (outline-state (nxml-get-outline-state section-start-pos))
  489. (newline-before-section-start-category
  490. (cond ((and (not had-children) parent-outline-state)
  491. 'nxml-outline-display-ellipsis)
  492. (outline-state 'nxml-outline-display-show)
  493. (t nil))))
  494. (nxml-outline-set-overlay (and parent-outline-state
  495. 'nxml-outline-display-hide)
  496. last-pos
  497. (1- section-start-pos-bol)
  498. nil
  499. t)
  500. (if outline-state
  501. (let* ((indent (if parent-indent
  502. (+ parent-indent nxml-outline-child-indent)
  503. (save-excursion
  504. (goto-char section-start-pos)
  505. (current-column))))
  506. start-tag-overlay)
  507. (nxml-outline-set-overlay newline-before-section-start-category
  508. (1- section-start-pos-bol)
  509. section-start-pos-bol
  510. t)
  511. (nxml-outline-set-overlay 'nxml-outline-display-hide
  512. section-start-pos-bol
  513. section-start-pos)
  514. (setq start-tag-overlay
  515. (nxml-outline-set-overlay 'nxml-outline-display-show
  516. section-start-pos
  517. (1+ section-start-pos)
  518. t))
  519. ;; line motion commands don't work right if start-tag-overlay
  520. ;; covers multiple lines
  521. (nxml-outline-set-overlay 'nxml-outline-display-hide
  522. (1+ section-start-pos)
  523. heading-start-pos)
  524. (goto-char heading-start-pos)
  525. (nxml-end-of-heading)
  526. (nxml-outline-set-overlay 'nxml-outline-display-heading
  527. heading-start-pos
  528. (point))
  529. (let* ((had-children
  530. (nxml-outline-display-rest outline-state
  531. indent
  532. tag-qnames)))
  533. (overlay-put start-tag-overlay
  534. 'display
  535. (concat
  536. ;; indent
  537. (make-string indent ?\ )
  538. ;; <
  539. nxml-highlighted-less-than
  540. ;; + or - indicator
  541. (cond ((not had-children)
  542. nxml-highlighted-inactive-minus)
  543. ((eq outline-state 'hide-children)
  544. (overlay-put start-tag-overlay
  545. 'category
  546. 'nxml-outline-display-hiding-tag)
  547. nxml-highlighted-active-plus)
  548. (t
  549. (overlay-put start-tag-overlay
  550. 'category
  551. 'nxml-outline-display-showing-tag)
  552. nxml-highlighted-active-minus))
  553. ;; qname
  554. (nxml-highlighted-qname (car tag-qnames))
  555. ;; >
  556. nxml-highlighted-greater-than))))
  557. ;; outline-state nil
  558. (goto-char heading-start-pos)
  559. (nxml-end-of-heading)
  560. (nxml-outline-set-overlay newline-before-section-start-category
  561. (1- section-start-pos-bol)
  562. (point)
  563. t)
  564. (nxml-outline-display-rest outline-state
  565. (and parent-indent
  566. (+ parent-indent
  567. nxml-outline-child-indent))
  568. tag-qnames))))
  569. (defun nxml-highlighted-qname (qname)
  570. (let ((colon (string-match ":" qname)))
  571. (if colon
  572. (concat (propertize (substring qname 0 colon)
  573. 'face
  574. 'nxml-element-prefix)
  575. nxml-highlighted-colon
  576. (propertize (substring qname (1+ colon))
  577. 'face
  578. 'nxml-element-local-name))
  579. (propertize qname
  580. 'face
  581. 'nxml-element-local-name))))
  582. (defun nxml-outline-display-single-line-end-tag (last-pos)
  583. (nxml-outline-set-overlay 'nxml-outline-display-hide
  584. last-pos
  585. xmltok-start
  586. nil
  587. t)
  588. (overlay-put (nxml-outline-set-overlay 'nxml-outline-display-show
  589. xmltok-start
  590. (point)
  591. t)
  592. 'display
  593. nxml-highlighted-empty-end-tag))
  594. (defun nxml-outline-display-multi-line-end-tag (last-pos start-tag-indent)
  595. (let ((indentp (save-excursion
  596. (goto-char last-pos)
  597. (skip-chars-forward " \t")
  598. (and (eq (char-after) ?\n)
  599. (progn
  600. (goto-char (1+ (point)))
  601. (nxml-outline-set-overlay nil last-pos (point))
  602. (setq last-pos (point))
  603. (goto-char xmltok-start)
  604. (beginning-of-line)
  605. t))))
  606. end-tag-overlay)
  607. (nxml-outline-set-overlay 'nxml-outline-display-hide
  608. last-pos
  609. xmltok-start
  610. nil
  611. t)
  612. (setq end-tag-overlay
  613. (nxml-outline-set-overlay 'nxml-outline-display-showing-tag
  614. xmltok-start
  615. (point)
  616. t))
  617. (overlay-put end-tag-overlay
  618. 'display
  619. (concat (if indentp
  620. (make-string start-tag-indent ?\ )
  621. "")
  622. nxml-highlighted-less-than
  623. nxml-highlighted-slash
  624. nxml-highlighted-active-minus
  625. (nxml-highlighted-qname (xmltok-end-tag-qname))
  626. nxml-highlighted-greater-than))))
  627. (defvar nxml-outline-show-map
  628. (let ((map (make-sparse-keymap)))
  629. (define-key map "\C-m" 'nxml-show-direct-text-content)
  630. (define-key map [mouse-2] 'nxml-mouse-show-direct-text-content)
  631. map))
  632. (defvar nxml-outline-show-help "mouse-2: show")
  633. (put 'nxml-outline-display-show 'nxml-outline-display t)
  634. (put 'nxml-outline-display-show 'evaporate t)
  635. (put 'nxml-outline-display-show 'keymap nxml-outline-show-map)
  636. (put 'nxml-outline-display-show 'help-echo nxml-outline-show-help)
  637. (put 'nxml-outline-display-hide 'nxml-outline-display t)
  638. (put 'nxml-outline-display-hide 'evaporate t)
  639. (put 'nxml-outline-display-hide 'invisible t)
  640. (put 'nxml-outline-display-hide 'keymap nxml-outline-show-map)
  641. (put 'nxml-outline-display-hide 'help-echo nxml-outline-show-help)
  642. (put 'nxml-outline-display-ellipsis 'nxml-outline-display t)
  643. (put 'nxml-outline-display-ellipsis 'evaporate t)
  644. (put 'nxml-outline-display-ellipsis 'keymap nxml-outline-show-map)
  645. (put 'nxml-outline-display-ellipsis 'help-echo nxml-outline-show-help)
  646. (put 'nxml-outline-display-ellipsis 'before-string nxml-highlighted-ellipsis)
  647. (put 'nxml-outline-display-heading 'keymap nxml-outline-show-map)
  648. (put 'nxml-outline-display-heading 'help-echo nxml-outline-show-help)
  649. (put 'nxml-outline-display-heading 'nxml-outline-display t)
  650. (put 'nxml-outline-display-heading 'evaporate t)
  651. (put 'nxml-outline-display-heading 'face 'nxml-heading)
  652. (defvar nxml-outline-hiding-tag-map
  653. (let ((map (make-sparse-keymap)))
  654. (define-key map [mouse-1] 'nxml-mouse-show-direct-subheadings)
  655. (define-key map [mouse-2] 'nxml-mouse-show-direct-text-content)
  656. (define-key map "\C-m" 'nxml-show-direct-text-content)
  657. map))
  658. (defvar nxml-outline-hiding-tag-help
  659. "mouse-1: show subheadings, mouse-2: show text content")
  660. (put 'nxml-outline-display-hiding-tag 'nxml-outline-display t)
  661. (put 'nxml-outline-display-hiding-tag 'evaporate t)
  662. (put 'nxml-outline-display-hiding-tag 'keymap nxml-outline-hiding-tag-map)
  663. (put 'nxml-outline-display-hiding-tag 'help-echo nxml-outline-hiding-tag-help)
  664. (defvar nxml-outline-showing-tag-map
  665. (let ((map (make-sparse-keymap)))
  666. (define-key map [mouse-1] 'nxml-mouse-hide-subheadings)
  667. (define-key map [mouse-2] 'nxml-mouse-show-direct-text-content)
  668. (define-key map "\C-m" 'nxml-show-direct-text-content)
  669. map))
  670. (defvar nxml-outline-showing-tag-help
  671. "mouse-1: hide subheadings, mouse-2: show text content")
  672. (put 'nxml-outline-display-showing-tag 'nxml-outline-display t)
  673. (put 'nxml-outline-display-showing-tag 'evaporate t)
  674. (put 'nxml-outline-display-showing-tag 'keymap nxml-outline-showing-tag-map)
  675. (put 'nxml-outline-display-showing-tag
  676. 'help-echo
  677. nxml-outline-showing-tag-help)
  678. (defun nxml-outline-set-overlay (category
  679. start
  680. end
  681. &optional
  682. front-advance
  683. rear-advance)
  684. "Replace any `nxml-outline-display' overlays between START and END.
  685. Overlays are removed if they overlay the region between START and END,
  686. and have a non-nil `nxml-outline-display' property (typically via their
  687. category). If CATEGORY is non-nil, they will be replaced with a new
  688. overlay with that category from START to END. If CATEGORY is nil,
  689. no new overlay will be created."
  690. (when (< start end)
  691. (let ((overlays (overlays-in start end))
  692. overlay)
  693. (while overlays
  694. (setq overlay (car overlays))
  695. (setq overlays (cdr overlays))
  696. (when (overlay-get overlay 'nxml-outline-display)
  697. (delete-overlay overlay))))
  698. (and category
  699. (let ((overlay (make-overlay start
  700. end
  701. nil
  702. front-advance
  703. rear-advance)))
  704. (overlay-put overlay 'category category)
  705. (setq line-move-ignore-invisible t)
  706. overlay))))
  707. (defun nxml-end-of-heading ()
  708. "Move from the start of the content of the heading to the end.
  709. Do not move past the end of the line."
  710. (let ((pos (condition-case err
  711. (and (nxml-scan-element-forward (point) t)
  712. xmltok-start)
  713. (nxml-scan-error nil))))
  714. (end-of-line)
  715. (skip-chars-backward " \t")
  716. (cond ((not pos)
  717. (setq pos (nxml-token-before))
  718. (when (eq xmltok-type 'end-tag)
  719. (goto-char pos)))
  720. ((< pos (point))
  721. (goto-char pos)))
  722. (skip-chars-backward " \t")
  723. (point)))
  724. ;;; Navigating section structure
  725. (defun nxml-token-starts-line-p ()
  726. (save-excursion
  727. (goto-char xmltok-start)
  728. (skip-chars-backward " \t")
  729. (bolp)))
  730. (defvar nxml-cached-section-tag-regexp nil)
  731. (defvar nxml-cached-section-element-name-regexp nil)
  732. (defsubst nxml-make-section-tag-regexp ()
  733. (if (eq nxml-cached-section-element-name-regexp
  734. nxml-section-element-name-regexp)
  735. nxml-cached-section-tag-regexp
  736. (nxml-make-section-tag-regexp-1)))
  737. (defun nxml-make-section-tag-regexp-1 ()
  738. (setq nxml-cached-section-element-name-regexp nil)
  739. (setq nxml-cached-section-tag-regexp
  740. (concat "</?\\("
  741. "\\(" xmltok-ncname-regexp ":\\)?"
  742. nxml-section-element-name-regexp
  743. "\\)[ \t\r\n>]"))
  744. (setq nxml-cached-section-element-name-regexp
  745. nxml-section-element-name-regexp)
  746. nxml-cached-section-tag-regexp)
  747. (defun nxml-section-tag-forward ()
  748. "Move forward past the first tag that is a section start- or end-tag.
  749. Return `xmltok-type' for tag.
  750. If no tag found, return nil and move to the end of the buffer."
  751. (let ((case-fold-search nil)
  752. (tag-regexp (nxml-make-section-tag-regexp))
  753. match-end)
  754. (when (< (point) nxml-prolog-end)
  755. (goto-char nxml-prolog-end))
  756. (while (cond ((not (re-search-forward tag-regexp nil 'move))
  757. (setq xmltok-type nil)
  758. nil)
  759. ((progn
  760. (goto-char (match-beginning 0))
  761. (setq match-end (match-end 0))
  762. (nxml-ensure-scan-up-to-date)
  763. (let ((end (nxml-inside-end (point))))
  764. (when end
  765. (goto-char end)
  766. t))))
  767. ((progn
  768. (xmltok-forward)
  769. (and (memq xmltok-type '(start-tag
  770. partial-start-tag
  771. end-tag
  772. partial-end-tag))
  773. ;; just in case wildcard matched non-name chars
  774. (= xmltok-name-end (1- match-end))))
  775. nil)
  776. (t))))
  777. xmltok-type)
  778. (defun nxml-section-tag-backward ()
  779. "Move backward to the end of a tag that is a section start- or end-tag.
  780. The position of the end of the tag must be <= point.
  781. Point is at the end of the tag. `xmltok-start' is the start."
  782. (let ((case-fold-search nil)
  783. (start (point))
  784. (tag-regexp (nxml-make-section-tag-regexp))
  785. match-end)
  786. (if (< (point) nxml-prolog-end)
  787. (progn
  788. (goto-char (point-min))
  789. nil)
  790. (while (cond ((not (re-search-backward tag-regexp
  791. nxml-prolog-end
  792. 'move))
  793. (setq xmltok-type nil)
  794. (goto-char (point-min))
  795. nil)
  796. ((progn
  797. (goto-char (match-beginning 0))
  798. (setq match-end (match-end 0))
  799. (nxml-ensure-scan-up-to-date)
  800. (let ((pos (nxml-inside-start (point))))
  801. (when pos
  802. (goto-char (1- pos))
  803. t))))
  804. ((progn
  805. (xmltok-forward)
  806. (and (<= (point) start)
  807. (memq xmltok-type '(start-tag
  808. partial-start-tag
  809. end-tag
  810. partial-end-tag))
  811. ;; just in case wildcard matched non-name chars
  812. (= xmltok-name-end (1- match-end))))
  813. nil)
  814. (t (goto-char xmltok-start)
  815. t)))
  816. xmltok-type)))
  817. (defun nxml-section-start-position ()
  818. "Return the position of the start of the section containing point.
  819. Signal an error on failure."
  820. (condition-case err
  821. (save-excursion (if (nxml-back-to-section-start)
  822. (point)
  823. (error "Not in section")))
  824. (nxml-outline-error
  825. (nxml-report-outline-error "Couldn't determine containing section: %s"
  826. err))))
  827. (defun nxml-back-to-section-start (&optional invisible-ok)
  828. "Try to move back to the start of the section containing point.
  829. The start of the section must be <= point.
  830. Only visible sections are included unless INVISIBLE-OK is non-nil.
  831. If found, return t. Otherwise move to `point-min' and return nil.
  832. If unbalanced section tags are found, signal an `nxml-outline-error'."
  833. (when (or (nxml-after-section-start-tag)
  834. (nxml-section-tag-backward))
  835. (let (open-tags found)
  836. (while (let (section-start-pos)
  837. (setq section-start-pos xmltok-start)
  838. (if (nxml-token-end-tag-p)
  839. (setq open-tags (cons (xmltok-end-tag-qname)
  840. open-tags))
  841. (if (not open-tags)
  842. (when (and (nxml-token-starts-line-p)
  843. (or invisible-ok
  844. (not (get-char-property section-start-pos
  845. 'invisible)))
  846. (nxml-heading-start-position))
  847. (setq found t))
  848. (let ((qname (xmltok-start-tag-qname)))
  849. (unless (string= (car open-tags) qname)
  850. (nxml-outline-error "mismatched end-tag"))
  851. (setq open-tags (cdr open-tags)))))
  852. (goto-char section-start-pos)
  853. (and (not found)
  854. (nxml-section-tag-backward))))
  855. found)))
  856. (defun nxml-after-section-start-tag ()
  857. "If the character after point is in a section start-tag, move after it.
  858. Return the token type. Otherwise return nil.
  859. Set up variables like `xmltok-forward'."
  860. (let ((pos (nxml-token-after))
  861. (case-fold-search nil))
  862. (when (and (memq xmltok-type '(start-tag partial-start-tag))
  863. (save-excursion
  864. (goto-char xmltok-start)
  865. (looking-at (nxml-make-section-tag-regexp))))
  866. (goto-char pos)
  867. xmltok-type)))
  868. (defun nxml-heading-start-position ()
  869. "Return the position of the start of the content of a heading element.
  870. Adjust the position to be after initial leading whitespace.
  871. Return nil if no heading element is found. Requires point to be
  872. immediately after the section's start-tag."
  873. (let ((depth 0)
  874. (heading-regexp (concat "\\`\\("
  875. nxml-heading-element-name-regexp
  876. "\\)\\'"))
  877. (section-regexp (concat "\\`\\("
  878. nxml-section-element-name-regexp
  879. "\\)\\'"))
  880. (start (point))
  881. found)
  882. (save-excursion
  883. (while (and (xmltok-forward)
  884. (cond ((memq xmltok-type '(end-tag partial-end-tag))
  885. (and (not (string-match section-regexp
  886. (xmltok-end-tag-local-name)))
  887. (> depth 0)
  888. (setq depth (1- depth))))
  889. ;; XXX Not sure whether this is a good idea
  890. ;;((eq xmltok-type 'empty-element)
  891. ;; nil)
  892. ((not (memq xmltok-type
  893. '(start-tag partial-start-tag)))
  894. t)
  895. ((string-match section-regexp
  896. (xmltok-start-tag-local-name))
  897. nil)
  898. ((string-match heading-regexp
  899. (xmltok-start-tag-local-name))
  900. (skip-chars-forward " \t\r\n")
  901. (setq found (point))
  902. nil)
  903. (t
  904. (setq depth (1+ depth))
  905. t))
  906. (<= (- (point) start) nxml-heading-scan-distance))))
  907. found))
  908. ;;; Error handling
  909. (defun nxml-report-outline-error (msg err)
  910. (error msg (apply 'format (cdr err))))
  911. (defun nxml-outline-error (&rest args)
  912. (signal 'nxml-outline-error args))
  913. (put 'nxml-outline-error
  914. 'error-conditions
  915. '(error nxml-error nxml-outline-error))
  916. (put 'nxml-outline-error
  917. 'error-message
  918. "Cannot create outline of buffer that is not well-formed")
  919. ;;; Debugging
  920. (defun nxml-debug-overlays ()
  921. (interactive)
  922. (let ((overlays (nreverse (overlays-in (point-min) (point-max))))
  923. overlay)
  924. (while overlays
  925. (setq overlay (car overlays))
  926. (setq overlays (cdr overlays))
  927. (when (overlay-get overlay 'nxml-outline-display)
  928. (message "overlay %s: %s...%s (%s)"
  929. (overlay-get overlay 'category)
  930. (overlay-start overlay)
  931. (overlay-end overlay)
  932. (overlay-get overlay 'display))))))
  933. (provide 'nxml-outln)
  934. ;;; nxml-outln.el ends here