Lots of refactorings and a few minor improvements.
[docutils.git] / docutils / tools / editors / emacs / rst.el
blobd303299f4b4a3cc7ebb3d731246136db1a50458d
1 ;;; rst.el --- Mode for viewing and editing reStructuredText-documents -*- lexical-binding: t -*-
3 ;; Copyright (C) 2003-2017 Free Software Foundation, Inc.
5 ;; Maintainer: Stefan Merten <stefan at merten-home dot de>
6 ;; Author: Stefan Merten <stefan at merten-home dot de>,
7 ;; Martin Blais <blais@furius.ca>,
8 ;; David Goodger <goodger@python.org>,
9 ;; Wei-Wei Guo <wwguocn@gmail.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package provides major mode rst-mode, which supports documents marked
29 ;; up using the reStructuredText format. Support includes font locking as well
30 ;; as a lot of convenience functions for editing. It does this by defining a
31 ;; Emacs major mode: rst-mode (ReST). This mode is derived from text-mode.
32 ;; This package also contains:
34 ;; - Functions to automatically adjust and cycle the section underline
35 ;; adornments;
36 ;; - A mode that displays the table of contents and allows you to jump anywhere
37 ;; from it;
38 ;; - Functions to insert and automatically update a TOC in your source
39 ;; document;
40 ;; - Function to insert list, processing item bullets and enumerations
41 ;; automatically;
42 ;; - Font-lock highlighting of most reStructuredText structures;
43 ;; - Indentation and filling according to reStructuredText syntax;
44 ;; - Cursor movement according to reStructuredText syntax;
45 ;; - Some other convenience functions.
47 ;; See the accompanying document in the docutils documentation about
48 ;; the contents of this package and how to use it.
50 ;; For more information about reStructuredText, see
51 ;; http://docutils.sourceforge.net/rst.html
53 ;; For full details on how to use the contents of this file, see
54 ;; http://docutils.sourceforge.net/docs/user/emacs.html
56 ;; There are a number of convenient key bindings provided by rst-mode. For the
57 ;; bindings, try C-c C-h when in rst-mode. There are also many variables that
58 ;; can be customized, look for defcustom in this file or look for the "rst"
59 ;; customization group contained in the "wp" group.
61 ;; If you use the table-of-contents feature, you may want to add a hook to
62 ;; update the TOC automatically every time you adjust a section title::
64 ;; (add-hook 'rst-adjust-hook 'rst-toc-update)
66 ;; Syntax highlighting: font-lock is enabled by default. If you want to turn
67 ;; off syntax highlighting to rst-mode, you can use the following::
69 ;; (setq font-lock-global-modes '(not rst-mode ...))
72 ;;; DOWNLOAD
74 ;; The latest release of this file lies in the docutils source code repository:
75 ;; http://docutils.svn.sourceforge.net/svnroot/docutils/trunk/docutils/tools/editors/emacs/rst.el
77 ;;; INSTALLATION
79 ;; Add the following lines to your init file:
81 ;; (require 'rst)
83 ;; If you are using `.txt' as a standard extension for reST files as
84 ;; http://docutils.sourceforge.net/FAQ.html#what-s-the-standard-filename-extension-for-a-restructuredtext-file
85 ;; suggests you may use one of the `Local Variables in Files' mechanism Emacs
86 ;; provides to set the major mode automatically. For instance you may use::
88 ;; .. -*- mode: rst -*-
90 ;; in the very first line of your file. The following code is useful if you
91 ;; want automatically enter rst-mode from any file with compatible extensions:
93 ;; (setq auto-mode-alist
94 ;; (append '(("\\.txt\\'" . rst-mode)
95 ;; ("\\.rst\\'" . rst-mode)
96 ;; ("\\.rest\\'" . rst-mode)) auto-mode-alist))
99 ;;; Code:
101 ;; FIXME: Check through major mode conventions again.
103 ;; FIXME: Embed complicated `defconst's in `eval-when-compile'.
105 ;; Common Lisp stuff
106 (require 'cl-lib)
108 ;; Correct wrong declaration.
109 (def-edebug-spec push
110 (&or [form symbolp] [form gv-place]))
112 ;; Correct wrong declaration. This still doesn't support dotted desctructuring
113 ;; though.
114 (def-edebug-spec cl-lambda-list
115 (([&rest cl-macro-arg]
116 [&optional ["&optional" cl-&optional-arg &rest cl-&optional-arg]]
117 [&optional ["&rest" arg]]
118 [&optional ["&key" [cl-&key-arg &rest cl-&key-arg]
119 &optional "&allow-other-keys"]]
120 [&optional ["&aux" &rest
121 &or (symbolp &optional def-form) symbolp]]
124 ;; Add missing declaration.
125 (def-edebug-spec cl-type-spec sexp) ;; This is not exactly correct but good
126 ;; enough.
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;; Support for `testcover'
131 (when (and (boundp 'testcover-1value-functions)
132 (boundp 'testcover-compose-functions))
133 ;; Below `lambda' is used in a loop with varying parameters and is thus not
134 ;; 1valued.
135 (setq testcover-1value-functions
136 (delq 'lambda testcover-1value-functions))
137 (add-to-list 'testcover-compose-functions 'lambda))
139 (defun rst-testcover-defcustom ()
140 "Remove all customized variables from `testcover-module-constants'.
141 This seems to be a bug in `testcover': `defcustom' variables are
142 considered constants. Revert it with this function after each `defcustom'."
143 (when (boundp 'testcover-module-constants)
144 (setq testcover-module-constants
145 (delq nil
146 (mapcar
147 #'(lambda (sym)
148 (if (not (plist-member (symbol-plist sym) 'standard-value))
149 sym))
150 testcover-module-constants)))))
152 (defun rst-testcover-add-compose (fun)
153 "Add FUN to `testcover-compose-functions'."
154 (when (boundp 'testcover-compose-functions)
155 (add-to-list 'testcover-compose-functions fun)))
157 (defun rst-testcover-add-1value (fun)
158 "Add FUN to `testcover-1value-functions'."
159 (when (boundp 'testcover-1value-functions)
160 (add-to-list 'testcover-1value-functions fun)))
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 ;; Helpers.
166 (cl-defmacro rst-destructuring-dolist
167 ((arglist list &optional result) &rest body)
168 "`cl-dolist' with destructuring of the list elements.
169 ARGLIST is a Common List argument list which may include
170 destructuring. LIST, RESULT and BODY are as for `cl-dolist'.
171 Note that definitions in ARGLIST are visible only in the BODY and
172 neither in RESULT nor in LIST."
173 ;; FIXME: It would be very useful if the definitions in ARGLIST would be
174 ;; visible in RESULT. But may be this is rather a
175 ;; `rst-destructuring-do' then.
176 (declare (debug
177 (&define ([&or symbolp cl-macro-list] def-form &optional def-form)
178 cl-declarations def-body))
179 (indent 1))
180 (let ((var (make-symbol "--rst-destructuring-dolist-var--")))
181 `(cl-dolist (,var ,list ,result)
182 (cl-destructuring-bind ,arglist ,var
183 ,@body))))
185 (defun rst-forward-line-strict (n &optional limit)
186 ;; testcover: ok.
187 "Try to move point to beginning of line I + N where I is the current line.
188 Return t if movement is successful. Otherwise don't move point
189 and return nil. If a position is given by LIMIT, movement
190 happened but the following line is missing and thus its beginning
191 can not be reached but the movement reached at least LIMIT
192 consider this a successful movement. LIMIT is ignored in other
193 cases."
194 (let ((start (point)))
195 (if (and (zerop (forward-line n))
196 (or (bolp)
197 (and limit
198 (>= (point) limit))))
200 (goto-char start)
201 nil)))
203 (defun rst-forward-line-looking-at (n rst-re-args &optional fun)
204 ;; testcover: ok.
205 "Move forward N lines and if successful check whether RST-RE-ARGS is matched.
206 Moving forward is done by `rst-forward-line-strict'. RST-RE-ARGS
207 is a single or a list of arguments for `rst-re'. FUN is a
208 function defaulting to `identity' which is called after the call
209 to `looking-at' receiving its return value as the first argument.
210 When FUN is called match data is just set by `looking-at' and
211 point is at the beginning of the line. Return nil if moving
212 forward failed or otherwise the return value of FUN. Preserve
213 global match data, point, mark and current buffer."
214 (unless (listp rst-re-args)
215 (setq rst-re-args (list rst-re-args)))
216 (unless fun
217 (setq fun #'identity))
218 (save-match-data
219 (save-excursion
220 (when (rst-forward-line-strict n)
221 (funcall fun (looking-at (apply #'rst-re rst-re-args)))))))
223 (rst-testcover-add-1value 'rst-delete-entire-line)
224 (defun rst-delete-entire-line (n)
225 "Move N lines and delete the entire line."
226 (delete-region (line-beginning-position (+ n 1))
227 (line-beginning-position (+ n 2))))
230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ;; Versions
233 (defun rst-extract-version (delim-re head-re re tail-re var &optional default)
234 ;; testcover: ok.
235 "Extract the version from a variable according to the given regexes.
236 Return the version after regex DELIM-RE and HEAD-RE matching RE
237 and before TAIL-RE and DELIM-RE in VAR or DEFAULT for no match."
238 (if (string-match
239 (concat delim-re head-re "\\(" re "\\)" tail-re delim-re)
240 var)
241 (match-string 1 var)
242 default))
244 ;; Use CVSHeader to really get information from CVS and not other version
245 ;; control systems.
246 (defconst rst-cvs-header
247 "$CVSHeader: sm/rst_el/rst.el,v 1.1058.2.2 2017/01/03 21:56:09 stefan Exp $")
248 (defconst rst-cvs-rev
249 (rst-extract-version "\\$" "CVSHeader: \\S + " "[0-9]+\\(?:\\.[0-9]+\\)+"
250 " .*" rst-cvs-header "0.0")
251 "The CVS revision of this file. CVS revision is the development revision.")
252 (defconst rst-cvs-timestamp
253 (rst-extract-version "\\$" "CVSHeader: \\S + \\S + "
254 "[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+" " .*"
255 rst-cvs-header "1970-01-01 00:00:00")
256 "The CVS time stamp of this file.")
258 ;; Use LastChanged... to really get information from SVN.
259 (defconst rst-svn-rev
260 (rst-extract-version "\\$" "LastChangedRevision: " "[0-9]+" " "
261 "$LastChangedRevision$")
262 "The SVN revision of this file.
263 SVN revision is the upstream (docutils) revision.")
264 (defconst rst-svn-timestamp
265 (rst-extract-version "\\$" "LastChangedDate: " ".+?+" " "
266 "$LastChangedDate$")
267 "The SVN time stamp of this file.")
269 ;; Maintained by the release process.
270 (defconst rst-official-version
271 (rst-extract-version "%" "OfficialVersion: " "[0-9]+\\(?:\\.[0-9]+\\)+" " "
272 "%OfficialVersion: 1.5.1 %")
273 "Official version of the package.")
274 (defconst rst-official-cvs-rev
275 (rst-extract-version "[%$]" "Revision: " "[0-9]+\\(?:\\.[0-9]+\\)+" " "
276 "$Revision$")
277 "CVS revision of this file in the official version.")
279 (defconst rst-version
280 (if (equal rst-official-cvs-rev rst-cvs-rev)
281 rst-official-version
282 (format "%s (development %s [%s])" rst-official-version
283 rst-cvs-rev rst-cvs-timestamp))
284 "The version string.
285 Starts with the current official version. For developer versions
286 in parentheses follows the development revision and the time stamp.")
288 (defconst rst-package-emacs-version-alist
289 '(("1.0.0" . "24.3")
290 ("1.1.0" . "24.3")
291 ("1.2.0" . "24.3")
292 ("1.2.1" . "24.3")
293 ("1.3.0" . "24.3")
294 ("1.3.1" . "24.3")
295 ("1.4.0" . "24.3")
296 ("1.4.1" . "24.5")
297 ("1.4.2" . "24.5")
298 ("1.5.0" . "26.1")
299 ("1.5.1" . "26.2")
302 (unless (assoc rst-official-version rst-package-emacs-version-alist)
303 (error "Version %s not listed in `rst-package-emacs-version-alist'"
304 rst-version))
306 (add-to-list 'customize-package-emacs-version-alist
307 (cons 'ReST rst-package-emacs-version-alist))
310 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 ;; Initialize customization
313 (defgroup rst nil "Support for reStructuredText documents."
314 :group 'text
315 :version "23.1"
316 :link '(url-link "http://docutils.sourceforge.net/rst.html"))
319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320 ;; Facilities for regular expressions used everywhere
322 ;; The trailing numbers in the names give the number of referenceable regex
323 ;; groups contained in the regex.
325 ;; Used to be customizable but really is not customizable but fixed by the reST
326 ;; syntax.
327 (defconst rst-bullets
328 ;; Sorted so they can form a character class when concatenated.
329 '(?- ?* ?+ ?• ?‣ ?⁃)
330 "List of all possible bullet characters for bulleted lists.")
332 (defconst rst-uri-schemes
333 '("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https" "imap"
334 "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero" "rtsp"
335 "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais")
336 "Supported URI schemes.")
338 (defconst rst-adornment-chars
339 ;; Sorted so they can form a character class when concatenated.
340 '(?\]
341 ?! ?\" ?# ?$ ?% ?& ?' ?\( ?\) ?* ?+ ?, ?. ?/ ?: ?\; ?< ?= ?> ?? ?@ ?\[ ?\\
342 ?^ ?_ ?` ?{ ?| ?} ?~
344 "Characters which may be used in adornments for sections and transitions.")
346 (defconst rst-max-inline-length
347 1000
348 "Maximum length of inline markup to recognize.")
350 (defconst rst-re-alist-def
351 ;; `*-beg' matches * at the beginning of a line.
352 ;; `*-end' matches * at the end of a line.
353 ;; `*-prt' matches a part of *.
354 ;; `*-tag' matches *.
355 ;; `*-sta' matches the start of * which may be followed by respective content.
356 ;; `*-pfx' matches the delimiter left of *.
357 ;; `*-sfx' matches the delimiter right of *.
358 ;; `*-hlp' helper for *.
360 ;; A trailing number says how many referenceable groups are contained.
363 ;; Horizontal white space (`hws')
364 (hws-prt "[\t ]")
365 (hws-tag hws-prt "*") ; Optional sequence of horizontal white space.
366 (hws-sta hws-prt "+") ; Mandatory sequence of horizontal white space.
368 ;; Lines (`lin')
369 (lin-beg "^" hws-tag) ; Beginning of a possibly indented line.
370 (lin-end hws-tag "$") ; End of a line with optional trailing white space.
371 (linemp-tag "^" hws-tag "$") ; Empty line with optional white space.
373 ;; Various tags and parts
374 (ell-tag "\\.\\.\\.") ; Ellipsis
375 (bul-tag ,(concat "[" rst-bullets "]")) ; A bullet.
376 (ltr-tag "[a-zA-Z]") ; A letter enumerator tag.
377 (num-prt "[0-9]") ; A number enumerator part.
378 (num-tag num-prt "+") ; A number enumerator tag.
379 (rom-prt "[IVXLCDMivxlcdm]") ; A roman enumerator part.
380 (rom-tag rom-prt "+") ; A roman enumerator tag.
381 (aut-tag "#") ; An automatic enumerator tag.
382 (dcl-tag "::") ; Double colon.
384 ;; Block lead in (`bli')
385 (bli-sfx (:alt hws-sta "$")) ; Suffix of a block lead-in with *optional*
386 ; immediate content.
388 ;; Various starts
389 (bul-sta bul-tag bli-sfx) ; Start of a bulleted item.
390 (bul-beg lin-beg bul-sta) ; A bullet item at the beginning of a line.
392 ;; Explicit markup tag (`exm')
393 (exm-tag "\\.\\.")
394 (exm-sta exm-tag hws-sta)
395 (exm-beg lin-beg exm-sta)
397 ;; Counters in enumerations (`cnt')
398 (cntany-tag (:alt ltr-tag num-tag rom-tag aut-tag)) ; An arbitrary counter.
399 (cntexp-tag (:alt ltr-tag num-tag rom-tag)) ; An arbitrary explicit counter.
401 ;; Enumerator (`enm')
402 (enmany-tag (:alt
403 (:seq cntany-tag "\\.")
404 (:seq "(?" cntany-tag ")"))) ; An arbitrary enumerator.
405 (enmexp-tag (:alt
406 (:seq cntexp-tag "\\.")
407 (:seq "(?" cntexp-tag ")"))) ; An arbitrary explicit
408 ; enumerator.
409 (enmaut-tag (:alt
410 (:seq aut-tag "\\.")
411 (:seq "(?" aut-tag ")"))) ; An automatic enumerator.
412 (enmany-sta enmany-tag bli-sfx) ; An arbitrary enumerator start.
413 (enmexp-sta enmexp-tag bli-sfx) ; An arbitrary explicit enumerator start.
414 (enmexp-beg lin-beg enmexp-sta) ; An arbitrary explicit enumerator start
415 ; at the beginning of a line.
417 ;; Items may be enumerated or bulleted (`itm')
418 (itmany-tag (:alt enmany-tag bul-tag)) ; An arbitrary item tag.
419 (itmany-sta-1 (:grp itmany-tag) bli-sfx) ; An arbitrary item start, group
420 ; is the item tag.
421 (itmany-beg-1 lin-beg itmany-sta-1) ; An arbitrary item start at the
422 ; beginning of a line, group is the
423 ; item tag.
425 ;; Inline markup (`ilm')
426 (ilm-pfx (:alt "^" hws-prt "[-'\"([{<‘“«’/:]"))
427 (ilm-sfx (:alt "$" hws-prt "[]-'\")}>’”»/:.,;!?\\]"))
429 ;; Inline markup content (`ilc')
430 (ilcsgl-tag "\\S ") ; A single non-white character.
431 (ilcast-prt (:alt "[^*\\]" "\\\\.")) ; Part of non-asterisk content.
432 (ilcbkq-prt (:alt "[^`\\]" "\\\\.")) ; Part of non-backquote content.
433 (ilcbkqdef-prt (:alt "[^`\\\n]" "\\\\.")) ; Part of non-backquote
434 ; definition.
435 (ilcbar-prt (:alt "[^|\\]" "\\\\.")) ; Part of non-vertical-bar content.
436 (ilcbardef-prt (:alt "[^|\\\n]" "\\\\.")) ; Part of non-vertical-bar
437 ; definition.
438 (ilcast-sfx "[^\t *\\]") ; Suffix of non-asterisk content.
439 (ilcbkq-sfx "[^\t `\\]") ; Suffix of non-backquote content.
440 (ilcbar-sfx "[^\t |\\]") ; Suffix of non-vertical-bar content.
441 (ilcrep-hlp ,(format "\\{0,%d\\}" rst-max-inline-length)) ; Repeat count.
442 (ilcast-tag (:alt ilcsgl-tag
443 (:seq ilcsgl-tag
444 ilcast-prt ilcrep-hlp
445 ilcast-sfx))) ; Non-asterisk content.
446 (ilcbkq-tag (:alt ilcsgl-tag
447 (:seq ilcsgl-tag
448 ilcbkq-prt ilcrep-hlp
449 ilcbkq-sfx))) ; Non-backquote content.
450 (ilcbkqdef-tag (:alt ilcsgl-tag
451 (:seq ilcsgl-tag
452 ilcbkqdef-prt ilcrep-hlp
453 ilcbkq-sfx))) ; Non-backquote definition.
454 (ilcbar-tag (:alt ilcsgl-tag
455 (:seq ilcsgl-tag
456 ilcbar-prt ilcrep-hlp
457 ilcbar-sfx))) ; Non-vertical-bar content.
458 (ilcbardef-tag (:alt ilcsgl-tag
459 (:seq ilcsgl-tag
460 ilcbardef-prt ilcrep-hlp
461 ilcbar-sfx))) ; Non-vertical-bar definition.
463 ;; Fields (`fld')
464 (fldnam-prt (:alt "[^:\n]" "\\\\:")) ; Part of a field name.
465 (fldnam-tag fldnam-prt "+") ; A field name.
466 (fld-tag ":" fldnam-tag ":") ; A field marker.
468 ;; Options (`opt')
469 (optsta-tag (:alt "[-+/]" "--")) ; Start of an option.
470 (optnam-tag "\\sw" (:alt "-" "\\sw") "*") ; Name of an option.
471 (optarg-tag (:shy "[ =]\\S +")) ; Option argument.
472 (optsep-tag (:shy "," hws-prt)) ; Separator between options.
473 (opt-tag (:shy optsta-tag optnam-tag optarg-tag "?")) ; A complete option.
475 ;; Footnotes and citations (`fnc')
476 (fncnam-prt "[^]\n]") ; Part of a footnote or citation name.
477 (fncnam-tag fncnam-prt "+") ; A footnote or citation name.
478 (fnc-tag "\\[" fncnam-tag "]") ; A complete footnote or citation tag.
479 (fncdef-tag-2 (:grp exm-sta)
480 (:grp fnc-tag)) ; A complete footnote or citation definition
481 ; tag. First group is the explicit markup
482 ; start, second group is the footnote /
483 ; citation tag.
484 (fnc-sta-2 fncdef-tag-2 bli-sfx) ; Start of a footnote or citation
485 ; definition. First group is the explicit
486 ; markup start, second group is the
487 ; footnote / citation tag.
489 ;; Substitutions (`sub')
490 (sub-tag "|" ilcbar-tag "|") ; A complete substitution tag.
491 (subdef-tag "|" ilcbardef-tag "|") ; A complete substitution definition
492 ; tag.
494 ;; Symbol (`sym')
495 (sym-prt "[-+.:_]") ; Non-word part of a symbol.
496 (sym-tag (:shy "\\sw+" (:shy sym-prt "\\sw+") "*"))
498 ;; URIs (`uri')
499 (uri-tag (:alt ,@rst-uri-schemes))
501 ;; Adornment (`ado')
502 (ado-prt "[" ,(concat rst-adornment-chars) "]")
503 (adorep3-hlp "\\{3,\\}") ; There must be at least 3 characters because
504 ; otherwise explicit markup start would be
505 ; recognized.
506 (adorep2-hlp "\\{2,\\}") ; As `adorep3-hlp' but when the first of three
507 ; characters is matched differently.
508 (ado-tag-1-1 (:grp ado-prt)
509 "\\1" adorep2-hlp) ; A complete adornment, group is the first
510 ; adornment character and MUST be the FIRST
511 ; group in the whole expression.
512 (ado-tag-1-2 (:grp ado-prt)
513 "\\2" adorep2-hlp) ; A complete adornment, group is the first
514 ; adornment character and MUST be the
515 ; SECOND group in the whole expression.
516 (ado-beg-2-1 "^" (:grp ado-tag-1-2)
517 lin-end) ; A complete adornment line; first group is the whole
518 ; adornment and MUST be the FIRST group in the whole
519 ; expression; second group is the first adornment
520 ; character.
522 ;; Titles (`ttl')
523 (ttl-tag "\\S *\\w.*\\S ") ; A title text.
524 (ttl-beg-1 lin-beg (:grp ttl-tag)) ; A title text at the beginning of a
525 ; line. First group is the complete,
526 ; trimmed title text.
528 ;; Directives and substitution definitions (`dir')
529 (dir-tag-3 (:grp exm-sta)
530 (:grp (:shy subdef-tag hws-sta) "?")
531 (:grp sym-tag dcl-tag)) ; A directive or substitution definition
532 ; tag. First group is explicit markup
533 ; start, second group is a possibly
534 ; empty substitution tag, third group is
535 ; the directive tag including the double
536 ; colon.
537 (dir-sta-3 dir-tag-3 bli-sfx) ; Start of a directive or substitution
538 ; definition. Groups are as in dir-tag-3.
540 ;; Literal block (`lit')
541 (lit-sta-2 (:grp (:alt "[^.\n]" "\\.[^.\n]") ".*") "?"
542 (:grp dcl-tag) "$") ; Start of a literal block. First group is
543 ; any text before the double colon tag which
544 ; may not exist, second group is the double
545 ; colon tag.
547 ;; Comments (`cmt')
548 (cmt-sta-1 (:grp exm-sta) "[^[|_\n]"
549 (:alt "[^:\n]" (:seq ":" (:alt "[^:\n]" "$")))
550 "*$") ; Start of a comment block; first group is explicit markup
551 ; start.
553 ;; Paragraphs (`par')
554 (par-tag- (:alt itmany-tag fld-tag opt-tag fncdef-tag-2 dir-tag-3 exm-tag)
555 ) ; Tag at the beginning of a paragraph; there may be groups in
556 ; certain cases.
558 "Definition alist of relevant regexes.
559 Each entry consists of the symbol naming the regex and an
560 argument list for `rst-re'.")
562 (defvar rst-re-alist) ; Forward declare to use it in `rst-re'.
564 ;; FIXME: Use `sregex' or `rx' instead of re-inventing the wheel.
565 (rst-testcover-add-compose 'rst-re)
566 (defun rst-re (&rest args)
567 ;; testcover: ok.
568 "Interpret ARGS as regular expressions and return a regex string.
569 Each element of ARGS may be one of the following:
571 A string which is inserted unchanged.
573 A character which is resolved to a quoted regex.
575 A symbol which is resolved to a string using `rst-re-alist-def'.
577 A list with a keyword in the car. Each element of the cdr of such
578 a list is recursively interpreted as ARGS. The results of this
579 interpretation are concatenated according to the keyword.
581 For the keyword `:seq' the results are simply concatenated.
583 For the keyword `:shy' the results are concatenated and
584 surrounded by a shy-group (\"\\(?:...\\)\").
586 For the keyword `:alt' the results form an alternative (\"\\|\")
587 which is shy-grouped (\"\\(?:...\\)\").
589 For the keyword `:grp' the results are concatenated and form a
590 referenceable group (\"\\(...\\)\").
592 After interpretation of ARGS the results are concatenated as for
593 `:seq'."
594 (apply #'concat
595 (mapcar
596 #'(lambda (re)
597 (cond
598 ((stringp re)
600 ((symbolp re)
601 (cadr (assoc re rst-re-alist)))
602 ((characterp re)
603 (regexp-quote (char-to-string re)))
604 ((listp re)
605 (let ((nested
606 (mapcar (lambda (elt)
607 (rst-re elt))
608 (cdr re))))
609 (cond
610 ((eq (car re) :seq)
611 (mapconcat #'identity nested ""))
612 ((eq (car re) :shy)
613 (concat "\\(?:" (mapconcat #'identity nested "") "\\)"))
614 ((eq (car re) :grp)
615 (concat "\\(" (mapconcat #'identity nested "") "\\)"))
616 ((eq (car re) :alt)
617 (concat "\\(?:" (mapconcat #'identity nested "\\|") "\\)"))
619 (error "Unknown list car: %s" (car re))))))
621 (error "Unknown object type for building regex: %s" re))))
622 args)))
624 ;; FIXME: Remove circular dependency between `rst-re' and `rst-re-alist'.
625 (with-no-warnings ; Silence byte-compiler about this construction.
626 (defconst rst-re-alist
627 ;; Shadow global value we are just defining so we can construct it step by
628 ;; step.
629 (let (rst-re-alist)
630 (dolist (re rst-re-alist-def rst-re-alist)
631 (setq rst-re-alist
632 (nconc rst-re-alist
633 (list (list (car re) (apply #'rst-re (cdr re))))))))
634 "Alist mapping symbols from `rst-re-alist-def' to regex strings."))
637 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
638 ;; Concepts
640 ;; Each of the following classes represents an own concept. The suffix of the
641 ;; class name is used in the code to represent entities of the respective
642 ;; class.
644 ;; In addition a reStructuredText section header in the buffer is called
645 ;; "section".
647 ;; For lists a "s" is added to the name of the concepts.
650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
651 ;; Class rst-Ado
653 (cl-defstruct
654 (rst-Ado
655 (:constructor nil) ; Prevent creating unchecked values.
656 ;; Construct a transition.
657 (:constructor
658 rst-Ado-new-transition
659 (&aux
660 (char nil)
661 (-style 'transition)))
662 ;; Construct a simple section header.
663 (:constructor
664 rst-Ado-new-simple
665 (char-arg
666 &aux
667 (char (rst-Ado--validate-char char-arg))
668 (-style 'simple)))
669 ;; Construct a over-and-under section header.
670 (:constructor
671 rst-Ado-new-over-and-under
672 (char-arg
673 &aux
674 (char (rst-Ado--validate-char char-arg))
675 (-style 'over-and-under)))
676 ;; Construct from adornment with inverted style.
677 (:constructor
678 rst-Ado-new-invert
679 (ado-arg
680 &aux
681 (char (rst-Ado-char ado-arg))
682 (-style (let ((sty (rst-Ado--style ado-arg)))
683 (cond
684 ((eq sty 'simple)
685 'over-and-under)
686 ((eq sty 'over-and-under)
687 'simple)
688 (sty)))))))
689 "Representation of a reStructuredText adornment.
690 Adornments are either section markers where they markup the
691 section header or transitions.
693 This type is immutable."
694 ;; The character used for the adornment.
695 (char nil :read-only t)
696 ;; The style of the adornment. This is a private attribute.
697 (-style nil :read-only t))
699 ;; Private class methods
701 (defun rst-Ado--validate-char (char)
702 ;; testcover: ok.
703 "Validate CHAR to be a valid adornment character.
704 Return CHAR if so or signal an error otherwise."
705 (cl-check-type char character)
706 (cl-check-type char (satisfies
707 (lambda (c)
708 (memq c rst-adornment-chars)))
709 "Character must be a valid adornment character")
710 char)
712 ;; Public methods
714 (defun rst-Ado-is-transition (self)
715 ;; testcover: ok.
716 "Return non-nil if SELF is a transition adornment."
717 (cl-check-type self rst-Ado)
718 (eq (rst-Ado--style self) 'transition))
720 (defun rst-Ado-is-section (self)
721 ;; testcover: ok.
722 "Return non-nil if SELF is a section adornment."
723 (cl-check-type self rst-Ado)
724 (not (rst-Ado-is-transition self)))
726 (defun rst-Ado-is-simple (self)
727 ;; testcover: ok.
728 "Return non-nil if SELF is a simple section adornment."
729 (cl-check-type self rst-Ado)
730 (eq (rst-Ado--style self) 'simple))
732 (defun rst-Ado-is-over-and-under (self)
733 ;; testcover: ok.
734 "Return non-nil if SELF is a over-and-under section adornment."
735 (cl-check-type self rst-Ado)
736 (eq (rst-Ado--style self) 'over-and-under))
738 (defun rst-Ado-equal (self other)
739 ;; testcover: ok.
740 "Return non-nil when SELF and OTHER are equal."
741 (cl-check-type self rst-Ado)
742 (cl-check-type other rst-Ado)
743 (cond
744 ((not (eq (rst-Ado--style self) (rst-Ado--style other)))
745 nil)
746 ((rst-Ado-is-transition self))
747 ((equal (rst-Ado-char self) (rst-Ado-char other)))))
749 (defun rst-Ado-position (self ados)
750 ;; testcover: ok.
751 "Return position of SELF in ADOS or nil."
752 (cl-check-type self rst-Ado)
753 (cl-position-if #'(lambda (e)
754 (rst-Ado-equal self e))
755 ados))
758 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
759 ;; Class rst-Hdr
761 (cl-defstruct
762 (rst-Hdr
763 (:constructor nil) ; Prevent creating unchecked values.
764 ;; Construct while all parameters must be valid.
765 (:constructor
766 rst-Hdr-new
767 (ado-arg
768 indent-arg
769 &aux
770 (ado (rst-Hdr--validate-ado ado-arg))
771 (indent (rst-Hdr--validate-indent indent-arg ado nil))))
772 ;; Construct while all parameters but `indent' must be valid.
773 (:constructor
774 rst-Hdr-new-lax
775 (ado-arg
776 indent-arg
777 &aux
778 (ado (rst-Hdr--validate-ado ado-arg))
779 (indent (rst-Hdr--validate-indent indent-arg ado t))))
780 ;; Construct a header with same characteristics but opposite style as `ado'.
781 (:constructor
782 rst-Hdr-new-invert
783 (ado-arg
784 indent-arg
785 &aux
786 (ado (rst-Hdr--validate-ado (rst-Ado-new-invert ado-arg)))
787 (indent (rst-Hdr--validate-indent indent-arg ado t))))
788 (:copier nil)) ; Not really needed for an immutable type.
789 "Representation of reStructuredText section header characteristics.
791 This type is immutable."
792 ;; The adornment of the header.
793 (ado nil :read-only t)
794 ;; The indentation of a title text or nil if not given.
795 (indent nil :read-only t))
797 ;; Private class methods
799 (defun rst-Hdr--validate-indent (indent ado lax)
800 ;; testcover: ok.
801 "Validate INDENT to be a valid indentation for ADO.
802 Return INDENT if so or signal an error otherwise. If LAX don't
803 signal an error and return a valid indent."
804 (cl-check-type indent integer)
805 (cond
806 ((zerop indent)
807 indent)
808 ((rst-Ado-is-simple ado)
809 (if lax
811 (signal 'args-out-of-range
812 '("Indentation must be 0 for style simple"))))
813 ((< indent 0)
814 (if lax
816 (signal 'args-out-of-range
817 '("Indentation must not be negative"))))
818 ;; Implicitly over-and-under.
819 (indent)))
821 (defun rst-Hdr--validate-ado (ado)
822 ;; testcover: ok.
823 "Validate ADO to be a valid adornment.
824 Return ADO if so or signal an error otherwise."
825 (cl-check-type ado rst-Ado)
826 (cond
827 ((rst-Ado-is-transition ado)
828 (signal 'args-out-of-range
829 '("Adornment for header must not be transition.")))
830 (ado)))
832 ;; Public class methods
834 (defvar rst-preferred-adornments) ; Forward declaration.
836 (defun rst-Hdr-preferred-adornments ()
837 ;; testcover: ok.
838 "Return preferred adornments as list of `rst-Hdr'."
839 (mapcar (cl-function
840 (lambda ((character style indent))
841 (rst-Hdr-new-lax
842 (if (eq style 'over-and-under)
843 (rst-Ado-new-over-and-under character)
844 (rst-Ado-new-simple character))
845 indent)))
846 rst-preferred-adornments))
848 ;; Public methods
850 (defun rst-Hdr-member-ado (self hdrs)
851 ;; testcover: ok.
852 "Return sublist of HDRS whose car's adornment equals that of SELF or nil."
853 (cl-check-type self rst-Hdr)
854 (let ((ado (rst-Hdr-ado self)))
855 (cl-member-if #'(lambda (hdr)
856 (rst-Ado-equal ado (rst-Hdr-ado hdr)))
857 hdrs)))
859 (defun rst-Hdr-ado-map (selves)
860 ;; testcover: ok.
861 "Return `rst-Ado' list extracted from elements of SELVES."
862 (mapcar #'rst-Hdr-ado selves))
864 (defun rst-Hdr-get-char (self)
865 ;; testcover: ok.
866 "Return character of the adornment of SELF."
867 (cl-check-type self rst-Hdr)
868 (rst-Ado-char (rst-Hdr-ado self)))
870 (defun rst-Hdr-is-over-and-under (self)
871 ;; testcover: ok.
872 "Return non-nil if SELF is a over-and-under section header."
873 (cl-check-type self rst-Hdr)
874 (rst-Ado-is-over-and-under (rst-Hdr-ado self)))
877 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
878 ;; Class rst-Ttl
880 (cl-defstruct
881 (rst-Ttl
882 (:constructor nil) ; Prevent creating unchecked values.
883 ;; Construct with valid parameters for all attributes.
884 (:constructor ; Private constructor
885 rst-Ttl--new
886 (ado-arg
887 match-arg
888 indent-arg
889 text-arg
890 &aux
891 (ado (rst-Ttl--validate-ado ado-arg))
892 (match (rst-Ttl--validate-match match-arg ado))
893 (indent (rst-Ttl--validate-indent indent-arg ado))
894 (text (rst-Ttl--validate-text text-arg ado))
895 (hdr (condition-case nil
896 (rst-Hdr-new ado indent)
897 (error nil)))))
898 (:copier nil)) ; Not really needed for an immutable type.
899 "Representation of a reStructuredText section header as found in a buffer.
900 This type gathers information about an adorned part in the buffer.
902 This type is immutable."
903 ;; The adornment characteristics or nil for a title candidate.
904 (ado nil :read-only t)
905 ;; The match-data for `ado' in a form similarly returned by `match-data' (but
906 ;; not necessarily with markers in buffers). Match group 0 matches the whole
907 ;; construct. Match group 1 matches the overline adornment if present.
908 ;; Match group 2 matches the section title text or the transition. Match
909 ;; group 3 matches the underline adornment.
910 (match nil :read-only t)
911 ;; An indentation found for the title line or nil for a transition.
912 (indent nil :read-only t)
913 ;; The text of the title or nil for a transition.
914 (text nil :read-only t)
915 ;; The header characteristics if it is a valid section header.
916 (hdr nil :read-only t)
917 ;; FIXME refactoring: Should have an attribute `buffer' for the buffer this
918 ;; title is found in. This breaks lots and lots of tests.
919 ;; However, with private constructor they may not be
920 ;; necessary any more. In case it is really a buffer then
921 ;; also `match' could be real data from `match-data' which
922 ;; contains markers instead of integers.
925 ;; Private class methods
927 (defun rst-Ttl--validate-ado (ado)
928 ;; testcover: ok.
929 "Return valid ADO or signal error."
930 (cl-check-type ado (or null rst-Ado))
931 ado)
933 (defun rst-Ttl--validate-match (match ado)
934 ;; testcover: ok.
935 "Return valid MATCH matching ADO or signal error."
936 (cl-check-type ado (or null rst-Ado))
937 (cl-check-type match list)
938 (cl-check-type match (satisfies (lambda (m)
939 (equal (length m) 8)))
940 "Match data must consist of exactly 8 buffer positions.")
941 (dolist (pos match)
942 (cl-check-type pos (or null integer-or-marker)))
943 (cl-destructuring-bind (all-beg all-end
944 ovr-beg ovr-end
945 txt-beg txt-end
946 und-beg und-end) match
947 (unless (and (integer-or-marker-p all-beg) (integer-or-marker-p all-end))
948 (signal 'args-out-of-range
949 '("First two elements of match data must be buffer positions.")))
950 (cond
951 ((null ado)
952 (unless (and (null ovr-beg) (null ovr-end)
953 (integer-or-marker-p txt-beg) (integer-or-marker-p txt-end)
954 (null und-beg) (null und-end))
955 (signal 'args-out-of-range
956 '("For a title candidate exactly the third match pair must be set."))))
957 ((rst-Ado-is-transition ado)
958 (unless (and (null ovr-beg) (null ovr-end)
959 (integer-or-marker-p txt-beg) (integer-or-marker-p txt-end)
960 (null und-beg) (null und-end))
961 (signal 'args-out-of-range
962 '("For a transition exactly the third match pair must be set."))))
963 ((rst-Ado-is-simple ado)
964 (unless (and (null ovr-beg) (null ovr-end)
965 (integer-or-marker-p txt-beg) (integer-or-marker-p txt-end)
966 (integer-or-marker-p und-beg) (integer-or-marker-p und-end))
967 (signal 'args-out-of-range
968 '("For a simple section adornment exactly the third and fourth match pair must be set."))))
969 (t ; over-and-under
970 (unless (and (integer-or-marker-p ovr-beg) (integer-or-marker-p ovr-end)
971 (integer-or-marker-p txt-beg) (integer-or-marker-p txt-end)
972 (or (null und-beg) (integer-or-marker-p und-beg))
973 (or (null und-end) (integer-or-marker-p und-end)))
974 (signal 'args-out-of-range
975 '("For a over-and-under section adornment all match pairs must be set."))))))
976 match)
978 (defun rst-Ttl--validate-indent (indent ado)
979 ;; testcover: ok.
980 "Return valid INDENT for ADO or signal error."
981 (if (and ado (rst-Ado-is-transition ado))
982 (cl-check-type indent null
983 "Indent for a transition must be nil.")
984 (cl-check-type indent (integer 0 *)
985 "Indent for a section header must be non-negative."))
986 indent)
988 (defun rst-Ttl--validate-text (text ado)
989 ;; testcover: ok.
990 "Return valid TEXT for ADO or signal error."
991 (if (and ado (rst-Ado-is-transition ado))
992 (cl-check-type text null
993 "Transitions may not have title text.")
994 (cl-check-type text string))
995 text)
997 ;; Public class methods
999 (defun rst-Ttl-from-buffer (ado beg-ovr beg-txt beg-und txt)
1000 ;; testcover: ok.
1001 "Return a `rst-Ttl' constructed from information in the current buffer.
1002 ADO is the adornment or nil for a title candidate. BEG-OVR and
1003 BEG-UND are the starting points of the overline or underline,
1004 respectively. They may be nil if the respective thing is missing.
1005 BEG-TXT is the beginning of the title line or the transition and
1006 must be given. The end of the line is used as the end point. TXT
1007 is the title text or nil. If TXT is given the indendation of the
1008 line containing BEG-TXT is used as indentation. Match group 0 is
1009 derived from the remaining information."
1010 (cl-check-type beg-txt integer-or-marker)
1011 (save-excursion
1012 (let ((end-ovr (when beg-ovr
1013 (goto-char beg-ovr)
1014 (line-end-position)))
1015 (end-txt (progn
1016 (goto-char beg-txt)
1017 (line-end-position)))
1018 (end-und (when beg-und
1019 (goto-char beg-und)
1020 (line-end-position)))
1021 (ind (when txt
1022 (goto-char beg-txt)
1023 (current-indentation))))
1024 (rst-Ttl--new ado
1025 (list
1026 (or beg-ovr beg-txt) (or end-und end-txt)
1027 beg-ovr end-ovr
1028 beg-txt end-txt
1029 beg-und end-und)
1030 ind txt))))
1032 ;; Public methods
1034 (defun rst-Ttl-get-title-beginning (self)
1035 ;; testcover: ok.
1036 "Return position of beginning of title text of SELF.
1037 This position should always be at the start of a line."
1038 (cl-check-type self rst-Ttl)
1039 (nth 4 (rst-Ttl-match self)))
1041 (defun rst-Ttl-get-beginning (self)
1042 ;; testcover: ok.
1043 "Return position of beginning of whole SELF."
1044 (cl-check-type self rst-Ttl)
1045 (nth 0 (rst-Ttl-match self)))
1047 (defun rst-Ttl-get-end (self)
1048 ;; testcover: ok.
1049 "Return position of end of whole SELF."
1050 (cl-check-type self rst-Ttl)
1051 (nth 1 (rst-Ttl-match self)))
1053 (defun rst-Ttl-is-section (self)
1054 ;; testcover: ok.
1055 "Return non-nil if SELF is a section header or candidate."
1056 (cl-check-type self rst-Ttl)
1057 (rst-Ttl-text self))
1059 (defun rst-Ttl-is-candidate (self)
1060 ;; testcover: ok.
1061 "Return non-nil if SELF is a candidate for a section header."
1062 (cl-check-type self rst-Ttl)
1063 (not (rst-Ttl-ado self)))
1065 (defun rst-Ttl-contains (self position)
1066 "Return whether SELF contain POSITION.
1067 Return 0 if SELF contains POSITION, < 0 if SELF ends before
1068 POSITION and > 0 if SELF starts after position."
1069 (cl-check-type self rst-Ttl)
1070 (cl-check-type position integer-or-marker)
1071 (cond
1072 ((< (nth 1 (rst-Ttl-match self)) position)
1074 ((> (nth 0 (rst-Ttl-match self)) position)
1076 (0)))
1079 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1080 ;; Class rst-Stn
1082 (cl-defstruct
1083 (rst-Stn
1084 (:constructor nil) ; Prevent creating unchecked values.
1085 ;; Construct while all parameters must be valid.
1086 (:constructor
1087 rst-Stn-new
1088 (ttl-arg
1089 level-arg
1090 children-arg
1091 &aux
1092 (ttl (rst-Stn--validate-ttl ttl-arg))
1093 (level (rst-Stn--validate-level level-arg ttl))
1094 (children (rst-Stn--validate-children children-arg ttl)))))
1095 "Representation of a section tree node.
1097 This type is immutable."
1098 ;; The title of the node or nil for a missing node.
1099 (ttl nil :read-only t)
1100 ;; The level of the node in the tree. Negative for the (virtual) top level
1101 ;; node.
1102 (level nil :read-only t)
1103 ;; The list of children of the node.
1104 (children nil :read-only t))
1105 ;; FIXME refactoring: Should have an attribute `buffer' for the buffer this
1106 ;; title is found in. Or use `rst-Ttl-buffer'.
1108 ;; Private class methods
1110 (defun rst-Stn--validate-ttl (ttl)
1111 ;; testcover: ok.
1112 "Return valid TTL or signal error."
1113 (cl-check-type ttl (or null rst-Ttl))
1114 ttl)
1116 (defun rst-Stn--validate-level (level ttl)
1117 ;; testcover: ok.
1118 "Return valid LEVEL for TTL or signal error."
1119 (cl-check-type level integer)
1120 (when (and ttl (< level 0))
1121 ;; testcover: Never reached because a title may not have a negative level
1122 (signal 'args-out-of-range
1123 '("Top level node must not have a title.")))
1124 level)
1126 (defun rst-Stn--validate-children (children ttl)
1127 ;; testcover: ok.
1128 "Return valid CHILDREN for TTL or signal error."
1129 (cl-check-type children list)
1130 (dolist (child children)
1131 (cl-check-type child rst-Stn))
1132 (unless (or ttl children)
1133 (signal 'args-out-of-range
1134 '("A missing node must have children.")))
1135 children)
1137 ;; Public methods
1139 (defun rst-Stn-get-title-beginning (self)
1140 ;; testcover: ok.
1141 "Return the beginning of the title of SELF.
1142 Handles missing node properly."
1143 (cl-check-type self rst-Stn)
1144 (let ((ttl (rst-Stn-ttl self)))
1145 (if ttl
1146 (rst-Ttl-get-title-beginning ttl)
1147 (rst-Stn-get-title-beginning (car (rst-Stn-children self))))))
1149 (defun rst-Stn-get-text (self &optional default)
1150 ;; testcover: ok.
1151 "Return title text of SELF or DEFAULT if SELF is a missing node.
1152 For a missing node and no DEFAULT given return a standard title text."
1153 (cl-check-type self rst-Stn)
1154 (let ((ttl (rst-Stn-ttl self)))
1155 (cond
1156 (ttl
1157 (rst-Ttl-text ttl))
1158 (default)
1159 ("[missing node]"))))
1161 (defun rst-Stn-is-top (self)
1162 ;; testcover: ok.
1163 "Return non-nil if SELF is a top level node."
1164 (cl-check-type self rst-Stn)
1165 (< (rst-Stn-level self) 0))
1168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1169 ;; Mode definition
1171 (defun rst-define-key (keymap key def &rest deprecated)
1172 ;; testcover: ok.
1173 "Bind like `define-key' but add deprecated key definitions.
1174 KEYMAP, KEY, and DEF are as in `define-key'. DEPRECATED key
1175 definitions should be in vector notation. These are defined
1176 as well but give an additional message."
1177 (define-key keymap key def)
1178 (when deprecated
1179 (let* ((command-name (symbol-name def))
1180 (forwarder-function-name
1181 (if (string-match "^rst-\\(.*\\)$" command-name)
1182 (concat "rst-deprecated-"
1183 (match-string 1 command-name))
1184 (error "Not an RST command: %s" command-name)))
1185 (forwarder-function (intern forwarder-function-name)))
1186 (unless (fboundp forwarder-function)
1187 (defalias forwarder-function
1188 (lambda ()
1189 (interactive)
1190 (call-interactively def)
1191 (message "[Deprecated use of key %s; use key %s instead]"
1192 (key-description (this-command-keys))
1193 (key-description key)))
1194 ;; FIXME: In Emacs-25 we could use (:documentation ...) instead.
1195 (format "Deprecated binding for %s, use \\[%s] instead."
1196 def def)))
1197 (dolist (dep-key deprecated)
1198 (define-key keymap dep-key forwarder-function)))))
1200 ;; Key bindings.
1201 (defvar rst-mode-map
1202 (let ((map (make-sparse-keymap)))
1204 ;; \C-c is the general keymap.
1205 (rst-define-key map [?\C-c ?\C-h] #'describe-prefix-bindings)
1208 ;; Section Adornments
1210 ;; The adjustment function that adorns or rotates a section title.
1211 (rst-define-key map [?\C-c ?\C-=] #'rst-adjust [?\C-c ?\C-a t])
1212 (rst-define-key map [?\C-=] #'rst-adjust) ; Does not work on macOS and
1213 ; on consoles.
1215 ;; \C-c \C-a is the keymap for adornments.
1216 (rst-define-key map [?\C-c ?\C-a ?\C-h] #'describe-prefix-bindings)
1217 ;; Another binding which works with all types of input.
1218 (rst-define-key map [?\C-c ?\C-a ?\C-a] #'rst-adjust)
1219 ;; Display the hierarchy of adornments implied by the current document
1220 ;; contents.
1221 (rst-define-key map [?\C-c ?\C-a ?\C-d] #'rst-display-hdr-hierarchy)
1222 ;; Homogenize the adornments in the document.
1223 (rst-define-key map [?\C-c ?\C-a ?\C-s] #'rst-straighten-sections
1224 [?\C-c ?\C-s])
1227 ;; Section Movement and Selection
1229 ;; Mark the subsection where the cursor is.
1230 (rst-define-key map [?\C-\M-h] #'rst-mark-section
1231 ;; Same as mark-defun sgml-mark-current-element.
1232 [?\C-c ?\C-m])
1233 ;; Move backward/forward between section titles.
1234 ;; FIXME: Also bind similar to outline mode.
1235 (rst-define-key map [?\C-\M-a] #'rst-backward-section
1236 ;; Same as beginning-of-defun.
1237 [?\C-c ?\C-n])
1238 (rst-define-key map [?\C-\M-e] #'rst-forward-section
1239 ;; Same as end-of-defun.
1240 [?\C-c ?\C-p])
1243 ;; Operating on regions
1245 ;; \C-c \C-r is the keymap for regions.
1246 (rst-define-key map [?\C-c ?\C-r ?\C-h] #'describe-prefix-bindings)
1247 ;; Makes region a line-block.
1248 (rst-define-key map [?\C-c ?\C-r ?\C-l] #'rst-line-block-region
1249 [?\C-c ?\C-d])
1250 ;; Shift region left or right according to tabs.
1251 (rst-define-key map [?\C-c ?\C-r tab] #'rst-shift-region
1252 [?\C-c ?\C-r t] [?\C-c ?\C-l t])
1255 ;; Operating on lists
1257 ;; \C-c \C-l is the keymap for lists.
1258 (rst-define-key map [?\C-c ?\C-l ?\C-h] #'describe-prefix-bindings)
1259 ;; Makes paragraphs in region as a bullet list.
1260 (rst-define-key map [?\C-c ?\C-l ?\C-b] #'rst-bullet-list-region
1261 [?\C-c ?\C-b])
1262 ;; Makes paragraphs in region as a enumeration.
1263 (rst-define-key map [?\C-c ?\C-l ?\C-e] #'rst-enumerate-region
1264 [?\C-c ?\C-e])
1265 ;; Converts bullets to an enumeration.
1266 (rst-define-key map [?\C-c ?\C-l ?\C-c] #'rst-convert-bullets-to-enumeration
1267 [?\C-c ?\C-v])
1268 ;; Make sure that all the bullets in the region are consistent.
1269 (rst-define-key map [?\C-c ?\C-l ?\C-s] #'rst-straighten-bullets-region
1270 [?\C-c ?\C-w])
1271 ;; Insert a list item.
1272 (rst-define-key map [?\C-c ?\C-l ?\C-i] #'rst-insert-list)
1275 ;; Table-of-Contents Features
1277 ;; \C-c \C-t is the keymap for table of contents.
1278 (rst-define-key map [?\C-c ?\C-t ?\C-h] #'describe-prefix-bindings)
1279 ;; Enter a TOC buffer to view and move to a specific section.
1280 (rst-define-key map [?\C-c ?\C-t ?\C-t] #'rst-toc)
1281 ;; Insert a TOC here.
1282 (rst-define-key map [?\C-c ?\C-t ?\C-i] #'rst-toc-insert
1283 [?\C-c ?\C-i])
1284 ;; Update the document's TOC (without changing the cursor position).
1285 (rst-define-key map [?\C-c ?\C-t ?\C-u] #'rst-toc-update
1286 [?\C-c ?\C-u])
1287 ;; Go to the section under the cursor (cursor must be in internal TOC).
1288 (rst-define-key map [?\C-c ?\C-t ?\C-j] #'rst-toc-follow-link
1289 [?\C-c ?\C-f])
1292 ;; Converting Documents from Emacs
1294 ;; \C-c \C-c is the keymap for compilation.
1295 (rst-define-key map [?\C-c ?\C-c ?\C-h] #'describe-prefix-bindings)
1296 ;; Run one of two pre-configured toolset commands on the document.
1297 (rst-define-key map [?\C-c ?\C-c ?\C-c] #'rst-compile
1298 [?\C-c ?1])
1299 (rst-define-key map [?\C-c ?\C-c ?\C-a] #'rst-compile-alt-toolset
1300 [?\C-c ?2])
1301 ;; Convert the active region to pseudo-xml using the docutils tools.
1302 (rst-define-key map [?\C-c ?\C-c ?\C-x] #'rst-compile-pseudo-region
1303 [?\C-c ?3])
1304 ;; Convert the current document to PDF and launch a viewer on the results.
1305 (rst-define-key map [?\C-c ?\C-c ?\C-p] #'rst-compile-pdf-preview
1306 [?\C-c ?4])
1307 ;; Convert the current document to S5 slides and view in a web browser.
1308 (rst-define-key map [?\C-c ?\C-c ?\C-s] #'rst-compile-slides-preview
1309 [?\C-c ?5])
1311 map)
1312 "Keymap for reStructuredText mode commands.
1313 This inherits from Text mode.")
1316 ;; Abbrevs.
1317 (define-abbrev-table 'rst-mode-abbrev-table
1318 (mapcar #'(lambda (x)
1319 (append x '(nil 0 system)))
1320 '(("contents" ".. contents::\n..\n ")
1321 ("con" ".. contents::\n..\n ")
1322 ("cont" "[...]")
1323 ("skip" "\n\n[...]\n\n ")
1324 ("seq" "\n\n[...]\n\n ")
1325 ;; FIXME: Add footnotes, links, and more.
1327 "Abbrev table used while in `rst-mode'.")
1330 ;; Syntax table.
1331 (defvar rst-mode-syntax-table
1332 (let ((st (copy-syntax-table text-mode-syntax-table)))
1333 (modify-syntax-entry ?$ "." st)
1334 (modify-syntax-entry ?% "." st)
1335 (modify-syntax-entry ?& "." st)
1336 (modify-syntax-entry ?' "." st)
1337 (modify-syntax-entry ?* "." st)
1338 (modify-syntax-entry ?+ "." st)
1339 (modify-syntax-entry ?- "." st)
1340 (modify-syntax-entry ?/ "." st)
1341 (modify-syntax-entry ?< "." st)
1342 (modify-syntax-entry ?= "." st)
1343 (modify-syntax-entry ?> "." st)
1344 (modify-syntax-entry ?\\ "\\" st)
1345 (modify-syntax-entry ?_ "." st)
1346 (modify-syntax-entry ?| "." st)
1347 (modify-syntax-entry"." st)
1348 (modify-syntax-entry"." st)
1349 (modify-syntax-entry ?‘ "." st)
1350 (modify-syntax-entry ?’ "." st)
1351 (modify-syntax-entry ?“ "." st)
1352 (modify-syntax-entry ?” "." st)
1354 "Syntax table used while in `rst-mode'.")
1356 (defcustom rst-mode-hook nil
1357 "Hook run when `rst-mode' is turned on.
1358 The hook for `text-mode' is run before this one."
1359 :group 'rst
1360 :type '(hook))
1361 (rst-testcover-defcustom)
1363 ;; Pull in variable definitions silencing byte-compiler.
1364 (require 'newcomment)
1366 (defvar electric-pair-pairs)
1367 (defvar electric-indent-inhibit)
1369 ;; Use rst-mode for *.rst and *.rest files. Many ReStructured-Text files
1370 ;; use *.txt, but this is too generic to be set as a default.
1371 ;;;###autoload (add-to-list 'auto-mode-alist (purecopy '("\\.re?st\\'" . rst-mode)))
1372 ;;;###autoload
1373 (define-derived-mode rst-mode text-mode "ReST"
1374 "Major mode for editing reStructuredText documents.
1375 \\<rst-mode-map>
1377 Turning on `rst-mode' calls the normal hooks `text-mode-hook'
1378 and `rst-mode-hook'. This mode also supports font-lock
1379 highlighting.
1381 \\{rst-mode-map}"
1382 :abbrev-table rst-mode-abbrev-table
1383 :syntax-table rst-mode-syntax-table
1384 :group 'rst
1386 ;; Paragraph recognition.
1387 (setq-local paragraph-separate
1388 (rst-re '(:alt
1389 "\f"
1390 lin-end)))
1391 (setq-local paragraph-start
1392 (rst-re '(:alt
1393 "\f"
1394 lin-end
1395 (:seq hws-tag par-tag- bli-sfx))))
1397 ;; Indenting and filling.
1398 (setq-local indent-line-function #'rst-indent-line)
1399 (setq-local adaptive-fill-mode t)
1400 (setq-local adaptive-fill-regexp (rst-re 'hws-tag 'par-tag- "?" 'hws-tag))
1401 (setq-local adaptive-fill-function #'rst-adaptive-fill)
1402 (setq-local fill-paragraph-handle-comment nil)
1404 ;; Comments.
1405 (setq-local comment-start ".. ")
1406 (setq-local comment-start-skip (rst-re 'lin-beg 'exm-tag 'bli-sfx))
1407 (setq-local comment-continue " ")
1408 (setq-local comment-multi-line t)
1409 (setq-local comment-use-syntax nil)
1410 ;; reStructuredText has not really a comment ender but nil is not really a
1411 ;; permissible value.
1412 (setq-local comment-end "")
1413 (setq-local comment-end-skip nil)
1415 ;; Commenting in reStructuredText is very special so use our own set of
1416 ;; functions.
1417 (setq-local comment-line-break-function #'rst-comment-line-break)
1418 (setq-local comment-indent-function #'rst-comment-indent)
1419 (setq-local comment-insert-comment-function #'rst-comment-insert-comment)
1420 (setq-local comment-region-function #'rst-comment-region)
1421 (setq-local uncomment-region-function #'rst-uncomment-region)
1423 (setq-local electric-pair-pairs '((?\" . ?\") (?\* . ?\*) (?\` . ?\`)))
1425 ;; Imenu and which function.
1426 ;; FIXME: Check documentation of `which-function' for alternative ways to
1427 ;; determine the current function name.
1428 (setq-local imenu-create-index-function #'rst-imenu-create-index)
1430 ;; Font lock.
1431 (setq-local font-lock-defaults
1432 '(rst-font-lock-keywords
1433 t nil nil nil
1434 (font-lock-multiline . t)
1435 (font-lock-mark-block-function . mark-paragraph)))
1436 (add-hook 'font-lock-extend-region-functions #'rst-font-lock-extend-region t)
1438 ;; Text after a changed line may need new fontification.
1439 (setq-local jit-lock-contextually t)
1441 ;; Indentation is not deterministic.
1442 (setq-local electric-indent-inhibit t))
1444 ;;;###autoload
1445 (define-minor-mode rst-minor-mode
1446 "Toggle ReST minor mode.
1447 With a prefix argument ARG, enable ReST minor mode if ARG is
1448 positive, and disable it otherwise. If called from Lisp, enable
1449 the mode if ARG is omitted or nil.
1451 When ReST minor mode is enabled, the ReST mode keybindings
1452 are installed on top of the major mode bindings. Use this
1453 for modes derived from Text mode, like Mail mode."
1454 ;; The initial value.
1456 ;; The indicator for the mode line.
1457 " ReST"
1458 ;; The minor mode bindings.
1459 rst-mode-map
1460 :group 'rst)
1462 ;; FIXME: can I somehow install these too?
1463 ;; :abbrev-table rst-mode-abbrev-table
1464 ;; :syntax-table rst-mode-syntax-table
1467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1468 ;; Section adornment adjustment
1470 ;; The following functions implement a smart automatic title sectioning feature.
1471 ;; The idea is that with the cursor sitting on a section title, we try to get as
1472 ;; much information from context and try to do the best thing automatically.
1473 ;; This function can be invoked many times and/or with prefix argument to rotate
1474 ;; between the various sectioning adornments.
1476 ;; Some notes:
1478 ;; - The underlining character that is used depends on context. The file is
1479 ;; scanned to find other sections and an appropriate character is selected.
1480 ;; If the function is invoked on a section that is complete, the character is
1481 ;; rotated among the existing section adornments.
1483 ;; Note that when rotating the characters, if we come to the end of the
1484 ;; hierarchy of adornments, the variable `rst-preferred-adornments' is
1485 ;; consulted to propose a new underline adornment, and if continued, we cycle
1486 ;; the adornments all over again. Set this variable to nil if you want to
1487 ;; limit the underlining character propositions to the existing adornments in
1488 ;; the file.
1490 ;; - An underline/overline that is not extended to the column at which it should
1491 ;; be hanging is dubbed INCOMPLETE. For example::
1493 ;; |Some Title
1494 ;; |-------
1496 ;; Examples of default invocation:
1498 ;; |Some Title ---> |Some Title
1499 ;; | |----------
1501 ;; |Some Title ---> |Some Title
1502 ;; |----- |----------
1504 ;; | |------------
1505 ;; | Some Title ---> | Some Title
1506 ;; | |------------
1508 ;; In over-and-under style, when alternating the style, a variable is
1509 ;; available to select how much default indent to use (it can be zero). Note
1510 ;; that if the current section adornment already has an indent, we don't
1511 ;; adjust it to the default, we rather use the current indent that is already
1512 ;; there for adjustment (unless we cycle, in which case we use the indent
1513 ;; that has been found previously).
1515 (defgroup rst-adjust nil
1516 "Settings for adjustment and cycling of section title adornments."
1517 :group 'rst
1518 :version "21.1")
1520 (define-obsolete-variable-alias
1521 'rst-preferred-decorations 'rst-preferred-adornments "rst 1.0.0")
1522 ;; FIXME: Default must match suggestion in
1523 ;; http://sphinx-doc.org/rest.html#sections for Python documentation.
1524 (defcustom rst-preferred-adornments '((?= over-and-under 1)
1525 (?= simple 0)
1526 (?- simple 0)
1527 (?~ simple 0)
1528 (?+ simple 0)
1529 (?` simple 0)
1530 (?# simple 0)
1531 (?@ simple 0))
1532 "Preferred hierarchy of section title adornments.
1533 A list consisting of lists of the form (CHARACTER STYLE INDENT).
1534 CHARACTER is the character used. STYLE is one of the symbols
1535 `over-and-under' or `simple'. INDENT is an integer giving the
1536 wanted indentation for STYLE `over-and-under'.
1538 This sequence is consulted to offer a new adornment suggestion
1539 when we rotate the underlines at the end of the existing
1540 hierarchy of characters, or when there is no existing section
1541 title in the file.
1543 Set this to an empty list to use only the adornment found in the
1544 file."
1545 :group 'rst-adjust
1546 :type `(repeat
1547 (group :tag "Adornment specification"
1548 (choice :tag "Adornment character"
1549 ,@(mapcar #'(lambda (char)
1550 (list 'const
1551 :tag (char-to-string char) char))
1552 rst-adornment-chars))
1553 (radio :tag "Adornment type"
1554 (const :tag "Overline and underline" over-and-under)
1555 (const :tag "Underline only" simple))
1556 (integer :tag "Indentation for overline and underline type"
1557 :value 0))))
1558 (rst-testcover-defcustom)
1560 ;; FIXME: Rename this to `rst-over-and-under-default-indent' and set default to
1561 ;; 0 because the effect of 1 is probably surprising in the few cases
1562 ;; where this is used.
1563 ;; FIXME: A matching adornment style can be looked for in
1564 ;; `rst-preferred-adornments' and its indentation used before using this
1565 ;; variable.
1566 (defcustom rst-default-indent 1
1567 "Number of characters to indent the section title.
1568 This is only used while toggling adornment styles when switching
1569 from a simple adornment style to a over-and-under adornment
1570 style. In addition this is used in cases where the adornments
1571 found in the buffer are to be used but the indentation for
1572 over-and-under adornments is inconsistent across the buffer."
1573 :group 'rst-adjust
1574 :type '(integer))
1575 (rst-testcover-defcustom)
1577 (defun rst-new-preferred-hdr (seen prev)
1578 ;; testcover: ok.
1579 "Return a new, preferred `rst-Hdr' different from all in SEEN.
1580 PREV is the previous `rst-Hdr' in the buffer. If given the
1581 search starts after this entry. Return nil if no new preferred
1582 `rst-Hdr' can be found."
1583 ;; All preferred adornments are candidates.
1584 (let ((candidates
1585 (append
1586 (if prev
1587 ;; Start searching after the level of the previous adornment.
1588 (cdr (rst-Hdr-member-ado prev (rst-Hdr-preferred-adornments))))
1589 (rst-Hdr-preferred-adornments))))
1590 (cl-find-if #'(lambda (cand)
1591 (not (rst-Hdr-member-ado cand seen)))
1592 candidates)))
1594 (defun rst-update-section (hdr)
1595 ;; testcover: ok.
1596 "Unconditionally update the style of the section header at point to HDR.
1597 If there are existing overline and/or underline from the
1598 existing adornment, they are removed before adding the
1599 requested adornment."
1600 (end-of-line)
1601 (let ((indent (or (rst-Hdr-indent hdr) 0))
1602 (marker (point-marker))
1603 new)
1605 ;; Fixup whitespace at the beginning and end of the line.
1606 (1value
1607 (rst-forward-line-strict 0))
1608 (delete-horizontal-space)
1609 (insert (make-string indent ? ))
1610 (end-of-line)
1611 (delete-horizontal-space)
1612 (setq new (make-string (+ (current-column) indent) (rst-Hdr-get-char hdr)))
1614 ;; Remove previous line if it is an adornment.
1615 ;; FIXME refactoring: Check whether this deletes `hdr' which *has* all the
1616 ;; data necessary.
1617 (when (and (rst-forward-line-looking-at -1 'ado-beg-2-1)
1618 ;; Avoid removing the underline of a title right above us.
1619 (not (rst-forward-line-looking-at -2 'ttl-beg-1)))
1620 (rst-delete-entire-line -1))
1622 ;; Remove following line if it is an adornment.
1623 (when (rst-forward-line-looking-at +1 'ado-beg-2-1)
1624 (rst-delete-entire-line +1))
1626 ;; Insert underline.
1627 (unless (rst-forward-line-strict +1)
1628 ;; Normalize buffer by adding final newline.
1629 (newline 1))
1630 (open-line 1)
1631 (insert new)
1633 ;; Insert overline.
1634 (when (rst-Hdr-is-over-and-under hdr)
1635 (1value ; Underline inserted above.
1636 (rst-forward-line-strict -1))
1637 (open-line 1)
1638 (insert new))
1640 (goto-char marker)))
1642 (defun rst-classify-adornment (adornment end &optional accept-over-only)
1643 ;; testcover: ok.
1644 "Classify adornment string for section titles and transitions.
1645 ADORNMENT is the complete adornment string as found in the buffer
1646 with optional trailing whitespace. END is the point after the
1647 last character of ADORNMENT. Return a `rst-Ttl' or nil if no
1648 syntactically valid adornment is found. If ACCEPT-OVER-ONLY an
1649 overline with a missing underline is accepted as valid and
1650 returned."
1651 (save-excursion
1652 (save-match-data
1653 (when (string-match (rst-re 'ado-beg-2-1) adornment)
1654 (goto-char end)
1655 (let* ((ado-ch (string-to-char (match-string 2 adornment)))
1656 (ado-re (rst-re ado-ch 'adorep3-hlp)) ; RE matching the
1657 ; adornment.
1658 (beg-pnt (progn
1659 (1value
1660 (rst-forward-line-strict 0))
1661 (point)))
1662 (nxt-emp ; Next line nonexistent or empty
1663 (not (rst-forward-line-looking-at +1 'lin-end #'not)))
1664 (prv-emp ; Previous line nonexistent or empty
1665 (not (rst-forward-line-looking-at -1 'lin-end #'not)))
1666 txt-blw
1667 (ttl-blw ; Title found below starting here.
1668 (rst-forward-line-looking-at
1669 +1 'ttl-beg-1
1670 #'(lambda (mtcd)
1671 (when mtcd
1672 (setq txt-blw (match-string-no-properties 1))
1673 (point)))))
1674 txt-abv
1675 (ttl-abv ; Title found above starting here.
1676 (rst-forward-line-looking-at
1677 -1 'ttl-beg-1
1678 #'(lambda (mtcd)
1679 (when mtcd
1680 (setq txt-abv (match-string-no-properties 1))
1681 (point)))))
1682 (und-fnd ; Matching underline found starting here.
1683 (and ttl-blw
1684 (rst-forward-line-looking-at
1685 +2 (list ado-re 'lin-end)
1686 #'(lambda (mtcd)
1687 (when mtcd
1688 (point))))))
1689 (ovr-fnd ; Matching overline found starting here.
1690 (and ttl-abv
1691 (rst-forward-line-looking-at
1692 -2 (list ado-re 'lin-end)
1693 #'(lambda (mtcd)
1694 (when mtcd
1695 (point))))))
1696 (und-wng ; Wrong underline found starting here.
1697 (and ttl-blw
1698 (not und-fnd)
1699 (rst-forward-line-looking-at
1700 +2 'ado-beg-2-1
1701 #'(lambda (mtcd)
1702 (when mtcd
1703 (point))))))
1704 (ovr-wng ; Wrong overline found starting here.
1705 (and ttl-abv (not ovr-fnd)
1706 (rst-forward-line-looking-at
1707 -2 'ado-beg-2-1
1708 #'(lambda (mtcd)
1709 (when (and
1710 mtcd
1711 ;; An adornment above may be a legal
1712 ;; adornment for the line above - consider it
1713 ;; a wrong overline only when it is equally
1714 ;; long.
1715 (equal
1716 (length (match-string-no-properties 1))
1717 (length adornment)))
1718 (point)))))))
1719 (cond
1720 ((and nxt-emp prv-emp)
1721 ;; A transition.
1722 (rst-Ttl-from-buffer (rst-Ado-new-transition)
1723 nil beg-pnt nil nil))
1724 (ovr-fnd ; Prefer overline match over underline match.
1725 ;; An overline with an underline.
1726 (rst-Ttl-from-buffer (rst-Ado-new-over-and-under ado-ch)
1727 ovr-fnd ttl-abv beg-pnt txt-abv))
1728 (und-fnd
1729 ;; An overline with an underline.
1730 (rst-Ttl-from-buffer (rst-Ado-new-over-and-under ado-ch)
1731 beg-pnt ttl-blw und-fnd txt-blw))
1732 ((and ttl-abv (not ovr-wng))
1733 ;; An underline.
1734 (rst-Ttl-from-buffer (rst-Ado-new-simple ado-ch)
1735 nil ttl-abv beg-pnt txt-abv))
1736 ((and accept-over-only ttl-blw (not und-wng))
1737 ;; An overline with a missing underline.
1738 (rst-Ttl-from-buffer (rst-Ado-new-over-and-under ado-ch)
1739 beg-pnt ttl-blw nil txt-blw))
1741 ;; Invalid adornment.
1742 nil)))))))
1744 (defun rst-ttl-at-point ()
1745 ;; testcover: ok.
1746 "Find a section title line around point and return its characteristics.
1747 If the point is on an adornment line find the respective title
1748 line. If the point is on an empty line check previous or next
1749 line whether it is a suitable title line and use it if so. If
1750 point is on a suitable title line use it. Return a `rst-Ttl' for
1751 a section header or nil if no title line is found."
1752 (save-excursion
1753 (save-match-data
1754 (1value
1755 (rst-forward-line-strict 0))
1756 (let* (cnd-beg ; Beginning of a title candidate.
1757 cnd-txt ; Text of a title candidate.
1758 (cnd-fun #'(lambda (mtcd) ; Function setting title candidate data.
1759 (when mtcd
1760 (setq cnd-beg (match-beginning 0))
1761 (setq cnd-txt (match-string-no-properties 1))
1762 t)))
1763 ttl)
1764 (cond
1765 ((looking-at (rst-re 'ado-beg-2-1))
1766 ;; Adornment found - consider it.
1767 (setq ttl (rst-classify-adornment (match-string-no-properties 0)
1768 (match-end 0) t)))
1769 ((looking-at (rst-re 'lin-end))
1770 ;; Empty line found - check surrounding lines for a title.
1772 (rst-forward-line-looking-at -1 'ttl-beg-1 cnd-fun)
1773 (rst-forward-line-looking-at +1 'ttl-beg-1 cnd-fun)))
1774 ((looking-at (rst-re 'ttl-beg-1))
1775 ;; Title line found - check for a following underline.
1776 (setq ttl (rst-forward-line-looking-at
1777 1 'ado-beg-2-1
1778 #'(lambda (mtcd)
1779 (when mtcd
1780 (rst-classify-adornment
1781 (match-string-no-properties 0) (match-end 0))))))
1782 ;; Title candidate found if no valid adornment found.
1783 (funcall cnd-fun (not ttl))))
1784 (cond
1785 ((and ttl (rst-Ttl-is-section ttl))
1786 ttl)
1787 (cnd-beg
1788 (rst-Ttl-from-buffer nil nil cnd-beg nil cnd-txt)))))))
1790 ;; The following function and variables are used to maintain information about
1791 ;; current section adornment in a buffer local cache. Thus they can be used for
1792 ;; font-locking and manipulation commands.
1794 (defvar-local rst-all-ttls-cache nil
1795 "All section adornments in the buffer as found by `rst-all-ttls'.
1796 Set to t when no section adornments were found.")
1798 ;; FIXME: If this variable is set to a different value font-locking of section
1799 ;; headers is wrong.
1800 (defvar-local rst-hdr-hierarchy-cache nil
1801 "Section hierarchy in the buffer as determined by `rst-hdr-hierarchy'.
1802 Set to t when no section adornments were found.
1803 Value depends on `rst-all-ttls-cache'.")
1805 (rst-testcover-add-1value 'rst-reset-section-caches)
1806 (defun rst-reset-section-caches ()
1807 "Reset all section cache variables.
1808 Should be called by interactive functions which deal with sections."
1809 (setq rst-all-ttls-cache nil
1810 rst-hdr-hierarchy-cache nil))
1812 (defun rst-all-ttls-compute ()
1813 ;; testcover: ok.
1814 "Return a list of `rst-Ttl' for current buffer with ascending line number."
1815 (save-excursion
1816 (save-match-data
1817 (let (ttls)
1818 (goto-char (point-min))
1819 ;; Iterate over all the section titles/adornments in the file.
1820 (while (re-search-forward (rst-re 'ado-beg-2-1) nil t)
1821 (let ((ttl (rst-classify-adornment
1822 (match-string-no-properties 0) (point))))
1823 (when (and ttl (rst-Ttl-is-section ttl))
1824 (when (rst-Ttl-hdr ttl)
1825 (push ttl ttls))
1826 (goto-char (rst-Ttl-get-end ttl)))))
1827 (nreverse ttls)))))
1829 (defun rst-all-ttls ()
1830 "Return all the section adornments in the current buffer.
1831 Return a list of `rst-Ttl' with ascending line number.
1833 Uses and sets `rst-all-ttls-cache'."
1834 (unless rst-all-ttls-cache
1835 (setq rst-all-ttls-cache (or (rst-all-ttls-compute) t)))
1836 (if (eq rst-all-ttls-cache t)
1838 (copy-sequence rst-all-ttls-cache)))
1840 (defun rst-infer-hdr-hierarchy (hdrs)
1841 ;; testcover: ok.
1842 "Build a hierarchy from HDRS.
1843 HDRS reflects the order in which the headers appear in the
1844 buffer. Return a `rst-Hdr' list representing the hierarchy of
1845 headers in the buffer. Indentation is unified."
1846 (let (ado2indents) ; Asscociates `rst-Ado' with the set of indents seen for
1847 ; it.
1848 (dolist (hdr hdrs)
1849 (let* ((ado (rst-Hdr-ado hdr))
1850 (indent (rst-Hdr-indent hdr))
1851 (found (assoc ado ado2indents)))
1852 (if found
1853 (setcdr found (cl-adjoin indent (cdr found)))
1854 (push (list ado indent) ado2indents))))
1855 (mapcar (cl-function
1856 (lambda ((ado consistent &rest inconsistent))
1857 (rst-Hdr-new ado (if inconsistent
1858 rst-default-indent
1859 consistent))))
1860 (nreverse ado2indents))))
1862 (defun rst-hdr-hierarchy (&optional ignore-position)
1863 ;; testcover: ok.
1864 "Return the hierarchy of section titles in the file as a `rst-Hdr' list.
1865 Each returned element may be used directly to create a section
1866 adornment on that level. If IGNORE-POSITION a title containing
1867 this position is not taken into account when building the
1868 hierarchy unless it appears again elsewhere. This catches cases
1869 where the current title is edited and may not be final regarding
1870 its level.
1872 Uses and sets `rst-hdr-hierarchy-cache' unless IGNORE-POSITION is
1873 given."
1874 (let* ((all-ttls (rst-all-ttls))
1875 (ignore-ttl
1876 (if ignore-position
1877 (cl-find-if
1878 #'(lambda (ttl)
1879 (equal (rst-Ttl-contains ttl ignore-position) 0))
1880 all-ttls)))
1881 (really-ignore
1882 (if ignore-ttl
1883 (<= (cl-count-if
1884 #'(lambda (ttl)
1885 (rst-Ado-equal (rst-Ttl-ado ignore-ttl)
1886 (rst-Ttl-ado ttl)))
1887 all-ttls)
1888 1)))
1889 (real-ttls (delq (if really-ignore ignore-ttl) all-ttls)))
1890 (copy-sequence ; Protect cache.
1891 (if (and (not ignore-position) rst-hdr-hierarchy-cache)
1892 (if (eq rst-hdr-hierarchy-cache t)
1894 rst-hdr-hierarchy-cache)
1895 (let ((r (rst-infer-hdr-hierarchy (mapcar #'rst-Ttl-hdr real-ttls))))
1896 (setq rst-hdr-hierarchy-cache
1897 (if ignore-position
1898 ;; Clear cache reflecting that a possible update is not
1899 ;; reflected.
1901 (or r t)))
1902 r)))))
1904 (defun rst-all-ttls-with-level ()
1905 ;; testcover: ok.
1906 "Return the section adornments with levels set according to hierarchy.
1907 Return a list of (`rst-Ttl' . LEVEL) with ascending line number."
1908 (let ((hier (rst-Hdr-ado-map (rst-hdr-hierarchy))))
1909 (mapcar
1910 #'(lambda (ttl)
1911 (cons ttl (rst-Ado-position (rst-Ttl-ado ttl) hier)))
1912 (rst-all-ttls))))
1914 (defun rst-get-previous-hdr ()
1915 "Return the `rst-Hdr' before point or nil if none."
1916 (let ((prev (cl-find-if #'(lambda (ttl)
1917 (< (rst-Ttl-contains ttl (point)) 0))
1918 (rst-all-ttls)
1919 :from-end t)))
1920 (and prev (rst-Ttl-hdr prev))))
1922 (defun rst-adornment-complete-p (ado indent)
1923 ;; testcover: ok.
1924 "Return t if the adornment ADO around point is complete using INDENT.
1925 The adornment is complete if it is a completely correct
1926 reStructuredText adornment for the title line at point. This
1927 includes indentation and correct length of adornment lines."
1928 ;; Note: we assume that the detection of the overline as being the underline
1929 ;; of a preceding title has already been detected, and has been eliminated
1930 ;; from the adornment that is given to us.
1931 (let ((exps (list "^" (rst-Ado-char ado)
1932 (format "\\{%d\\}"
1933 (+ (save-excursion
1934 ;; Determine last column of title.
1935 (end-of-line)
1936 (current-column))
1937 indent)) "$")))
1938 (and (rst-forward-line-looking-at +1 exps)
1939 (or (rst-Ado-is-simple ado)
1940 (rst-forward-line-looking-at -1 exps))
1941 t))) ; Normalize return value.
1943 (defun rst-next-hdr (hdr hier prev down)
1944 ;; testcover: ok.
1945 "Return the next best `rst-Hdr' upward from HDR.
1946 Consider existing hierarchy HIER and preferred headers. PREV may
1947 be a previous `rst-Hdr' which may be taken into account. If DOWN
1948 return the next best `rst-Hdr' downward instead. Return nil in
1949 HIER is nil."
1950 (let* ((normalized-hier (if down
1951 hier
1952 (reverse hier)))
1953 (fnd (rst-Hdr-member-ado hdr normalized-hier))
1954 (prev-fnd (and prev (rst-Hdr-member-ado prev normalized-hier))))
1956 ;; Next entry in existing hierarchy if it exists.
1957 (cadr fnd)
1958 (if fnd
1959 ;; If current header is found try introducing a new one from preferred
1960 ;; hierarchy.
1961 (rst-new-preferred-hdr hier prev)
1962 ;; If not found try using previous header.
1963 (if down
1964 (cadr prev-fnd)
1965 (car prev-fnd)))
1966 ;; All failed - rotate by using first from normalized existing hierarchy.
1967 (car normalized-hier))))
1969 ;; FIXME: A line "``/`` full" is not accepted as a section title.
1970 (defun rst-adjust (pfxarg)
1971 ;; testcover: ok.
1972 "Auto-adjust the adornment around point.
1973 Adjust/rotate the section adornment for the section title around
1974 point or promote/demote the adornments inside the region,
1975 depending on whether the region is active. This function is meant
1976 to be invoked possibly multiple times, and can vary its behavior
1977 with a positive PFXARG (toggle style), or with a negative
1978 PFXARG (alternate behavior).
1980 This function is a bit of a swiss knife. It is meant to adjust
1981 the adornments of a section title in reStructuredText. It tries
1982 to deal with all the possible cases gracefully and to do \"the
1983 right thing\" in all cases.
1985 See the documentations of `rst-adjust-section' and
1986 `rst-adjust-region' for full details.
1988 The method can take either (but not both) of
1990 a. a (non-negative) prefix argument, which means to toggle the
1991 adornment style. Invoke with a prefix argument for example;
1993 b. a negative numerical argument, which generally inverts the
1994 direction of search in the file or hierarchy. Invoke with C--
1995 prefix for example."
1996 (interactive "P")
1997 (let* ((origpt (point-marker))
1998 (reverse-direction (and pfxarg (< (prefix-numeric-value pfxarg) 0)))
1999 (toggle-style (and pfxarg (not reverse-direction))))
2000 (if (use-region-p)
2001 (rst-adjust-region (and pfxarg t))
2002 (let ((msg (rst-adjust-section toggle-style reverse-direction)))
2003 (when msg
2004 (apply #'message msg))))
2005 (run-hooks 'rst-adjust-hook)
2006 (rst-reset-section-caches)
2007 (set-marker
2008 (goto-char origpt) nil)))
2010 (defcustom rst-adjust-hook nil
2011 "Hooks to be run after running `rst-adjust'."
2012 :group 'rst-adjust
2013 :type '(hook)
2014 :package-version '(rst . "1.1.0"))
2015 (rst-testcover-defcustom)
2017 (defcustom rst-new-adornment-down nil
2018 "Controls level of new adornment for section headers."
2019 :group 'rst-adjust
2020 :type '(choice
2021 (const :tag "Same level as previous one" nil)
2022 (const :tag "One level down relative to the previous one" t))
2023 :package-version '(rst . "1.1.0"))
2024 (rst-testcover-defcustom)
2026 (defun rst-adjust-adornment (pfxarg)
2027 "Call `rst-adjust-section' interactively.
2028 Keep this for compatibility for older bindings (are there any?).
2029 Argument PFXARG has the same meaning as for `rst-adjust'."
2030 (interactive "P")
2032 (let* ((reverse-direction (and pfxarg (< (prefix-numeric-value pfxarg) 0)))
2033 (toggle-style (and pfxarg (not reverse-direction))))
2034 (rst-adjust-section toggle-style reverse-direction)))
2036 (defun rst-adjust-new-hdr (toggle-style reverse ttl)
2037 ;; testcover: ok.
2038 "Return a new `rst-Hdr' for `rst-adjust-section' related to TTL.
2039 TOGGLE-STYLE and REVERSE are from
2040 `rst-adjust-section'. TOGGLE-STYLE may be consumed and thus is
2041 returned.
2043 Return a list (HDR TOGGLE-STYLE MSG...). HDR is the result or
2044 nil. TOGGLE-STYLE is the new TOGGLE-STYLE to use in the
2045 caller. MSG is a list which is non-empty in case HDR is nil
2046 giving an argument list for `message'."
2047 (save-excursion
2048 (goto-char (rst-Ttl-get-title-beginning ttl))
2049 (let ((indent (rst-Ttl-indent ttl))
2050 (ado (rst-Ttl-ado ttl))
2051 (prev (rst-get-previous-hdr))
2052 hdr-msg)
2053 (setq
2054 hdr-msg
2055 (cond
2056 ((rst-Ttl-is-candidate ttl)
2057 ;; Case 1: No adornment at all.
2058 (let ((hier (rst-hdr-hierarchy)))
2059 (if prev
2060 ;; Previous header exists - use it.
2061 (cond
2062 ;; Customization and parameters require that the previous level
2063 ;; is used - use it as is.
2064 ((or (and rst-new-adornment-down reverse)
2065 (and (not rst-new-adornment-down) (not reverse)))
2066 prev)
2067 ;; Advance one level down.
2068 ((rst-next-hdr prev hier prev t))
2069 ("Neither hierarchy nor preferences can suggest a deeper header"))
2070 ;; First header in the buffer - use the first adornment from
2071 ;; preferences or hierarchy.
2072 (let ((p (car (rst-Hdr-preferred-adornments)))
2073 (h (car hier)))
2074 (cond
2075 ((if reverse
2076 ;; Prefer hierarchy for downwards
2077 (or h p)
2078 ;; Prefer preferences for upwards
2079 (or p h)))
2080 ("No preferences to suggest a top level from"))))))
2081 ((not (rst-adornment-complete-p ado indent))
2082 ;; Case 2: Incomplete adornment.
2083 ;; Use lax since indentation might not match suggestion.
2084 (rst-Hdr-new-lax ado indent))
2085 ;; Case 3: Complete adornment exists from here on.
2086 (toggle-style
2087 ;; Simply switch the style of the current adornment.
2088 (setq toggle-style nil) ; Remember toggling has been done.
2089 (rst-Hdr-new-invert ado rst-default-indent))
2091 ;; Rotate, ignoring a sole adornment around the current line.
2092 (let ((hier (rst-hdr-hierarchy (point))))
2093 (cond
2094 ;; Next header can be determined from hierarchy or preferences.
2095 ((rst-next-hdr
2096 ;; Use lax since indentation might not match suggestion.
2097 (rst-Hdr-new-lax ado indent) hier prev reverse))
2098 ;; No next header found.
2099 ("No preferences or hierarchy to suggest another level from"))))))
2100 (if (stringp hdr-msg)
2101 (list nil toggle-style hdr-msg)
2102 (list hdr-msg toggle-style)))))
2104 (defun rst-adjust-section (toggle-style reverse)
2105 ;; testcover: ok.
2106 "Adjust/rotate the section adornment for the section title around point.
2107 The action this function takes depends on context around the
2108 point, and it is meant to be invoked possibly more than once to
2109 rotate among the various possibilities. Basically, this function
2110 deals with:
2112 - adding an adornment if the title does not have one;
2114 - adjusting the length of the underline characters to fit a
2115 modified title;
2117 - rotating the adornment in the set of already existing
2118 sectioning adornments used in the file;
2120 - switching between simple and over-and-under styles by giving
2121 TOGGLE-STYLE.
2123 Return nil if the function did something. If the function were
2124 not able to do something return an argument list for `message' to
2125 inform the user about what failed.
2127 The following is a detailed description but you should normally
2128 not have to read it.
2130 Before applying the adornment change, the cursor is placed on the
2131 closest line that could contain a section title if such is found
2132 around the cursor. Then the following cases are distinguished.
2134 * Case 1: No Adornment
2136 If the current line has no adornment around it,
2138 - search for a previous adornment, and apply this adornment (unless
2139 `rst-new-adornment-down') or one level lower (otherwise) to the current
2140 line. If there is no defined level below this previous adornment, we
2141 suggest the most appropriate of the `rst-preferred-adornments'.
2143 If REVERSE is true, we simply use the previous adornment found
2144 directly.
2146 - if there is no adornment found in the given direction, we use the first of
2147 `rst-preferred-adornments'.
2149 TOGGLE-STYLE forces a toggle of the prescribed adornment style.
2151 * Case 2: Incomplete Adornment
2153 If the current line does have an existing adornment, but the adornment is
2154 incomplete, that is, the underline/overline does not extend to exactly the
2155 end of the title line (it is either too short or too long), we simply extend
2156 the length of the underlines/overlines to fit exactly the section title.
2158 If TOGGLE-STYLE we toggle the style of the adornment as well.
2160 REVERSE has no effect in this case.
2162 * Case 3: Complete Existing Adornment
2164 If the adornment is complete (i.e. the underline (overline) length is already
2165 adjusted to the end of the title line), we rotate the current title's
2166 adornment according to the adornment hierarchy found in the buffer. This is
2167 meant to be used potentially multiple times, until the desired adornment is
2168 found around the title.
2170 If we hit the boundary of the hierarchy, exactly one choice from the list of
2171 preferred adornments is suggested/chosen, the first of those adornment that
2172 has not been seen in the buffer yet, and the next invocation rolls over to
2173 the other end of the hierarchy (i.e. it cycles).
2175 If REVERSE is we go up in the hierarchy. Otherwise we go down.
2177 However, if TOGGLE-STYLE, we do not rotate the adornment, but instead simply
2178 toggle the style of the current adornment."
2179 (rst-reset-section-caches)
2180 (let ((ttl (rst-ttl-at-point)))
2181 (if (not ttl)
2182 '("No section header or candidate at point")
2183 (cl-destructuring-bind
2184 (hdr toggle-style &rest msg
2185 &aux
2186 (indent (rst-Ttl-indent ttl))
2187 (moved (- (line-number-at-pos (rst-Ttl-get-title-beginning ttl))
2188 (line-number-at-pos))))
2189 (rst-adjust-new-hdr toggle-style reverse ttl)
2190 (if msg
2192 (when toggle-style
2193 (setq hdr (rst-Hdr-new-invert (rst-Hdr-ado hdr) indent)))
2194 ;; Override indent with present indent if there is some.
2195 (when (> indent 0)
2196 ;; Use lax since existing indent may not be valid for new style.
2197 (setq hdr (rst-Hdr-new-lax (rst-Hdr-ado hdr) indent)))
2198 (goto-char (rst-Ttl-get-title-beginning ttl))
2199 (rst-update-section hdr)
2200 ;; Correct the position of the cursor to more accurately reflect
2201 ;; where it was located when the function was invoked.
2202 (unless (zerop moved)
2203 (1value ; No lines may be left to move.
2204 (rst-forward-line-strict (- moved)))
2205 (end-of-line))
2206 nil)))))
2208 ;; Maintain an alias for compatibility.
2209 (defalias 'rst-adjust-section-title 'rst-adjust)
2211 (defun rst-adjust-region (demote)
2212 ;; testcover: ok.
2213 "Promote the section titles within the region.
2214 With argument DEMOTE or a prefix argument, demote the section
2215 titles instead. The algorithm used at the boundaries of the
2216 hierarchy is similar to that used by `rst-adjust-section'."
2217 (interactive "P")
2218 (rst-reset-section-caches)
2219 (let* ((beg (region-beginning))
2220 (end (region-end))
2221 (ttls-reg (cl-remove-if-not
2222 #'(lambda (ttl)
2223 (and
2224 (>= (rst-Ttl-contains ttl beg) 0)
2225 (< (rst-Ttl-contains ttl end) 0)))
2226 (rst-all-ttls))))
2227 (save-excursion
2228 ;; Apply modifications.
2229 (rst-destructuring-dolist
2230 ((marker &rest hdr
2231 &aux (hier (rst-hdr-hierarchy)))
2232 (mapcar #'(lambda (ttl)
2233 (cons (copy-marker (rst-Ttl-get-title-beginning ttl))
2234 (rst-Ttl-hdr ttl)))
2235 ttls-reg))
2236 (set-marker
2237 (goto-char marker) nil)
2238 ;; `rst-next-hdr' cannot return nil because we apply to a section
2239 ;; header so there is some hierarchy.
2240 (rst-update-section (rst-next-hdr hdr hier nil demote)))
2241 (setq deactivate-mark nil))))
2243 (defun rst-display-hdr-hierarchy ()
2244 ;; testcover: ok.
2245 "Display the current file's section title adornments hierarchy.
2246 Hierarchy is displayed in a temporary buffer."
2247 (interactive)
2248 (rst-reset-section-caches)
2249 (let ((hdrs (rst-hdr-hierarchy))
2250 (level 1))
2251 (with-output-to-temp-buffer "*rest section hierarchy*"
2252 (with-current-buffer standard-output
2253 (dolist (hdr hdrs)
2254 (insert (format "\nSection Level %d" level))
2255 (rst-update-section hdr)
2256 (goto-char (point-max))
2257 (insert "\n")
2258 (cl-incf level))))))
2260 ;; Maintain an alias for backward compatibility.
2261 (defalias 'rst-display-adornments-hierarchy 'rst-display-hdr-hierarchy)
2263 ;; FIXME: Should accept an argument giving the hierarchy level to start with
2264 ;; instead of the top of the hierarchy.
2265 (defun rst-straighten-sections ()
2266 ;; testcover: ok.
2267 "Redo the adornments of all section titles in the current buffer.
2268 This is done using the preferred set of adornments. This can be
2269 used, for example, when using somebody else's copy of a document,
2270 in order to adapt it to our preferred style."
2271 (interactive)
2272 (rst-reset-section-caches)
2273 (save-excursion
2274 (rst-destructuring-dolist
2275 ((marker &rest level)
2276 (mapcar
2277 (cl-function
2278 (lambda ((ttl &rest level))
2279 ;; Use markers so edits don't disturb the position.
2280 (cons (copy-marker (rst-Ttl-get-title-beginning ttl)) level)))
2281 (rst-all-ttls-with-level)))
2282 (set-marker
2283 (goto-char marker) nil)
2284 (rst-update-section (nth level (rst-Hdr-preferred-adornments))))))
2286 ;; Maintain an alias for compatibility.
2287 (defalias 'rst-straighten-adornments 'rst-straighten-sections)
2290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2291 ;; Insert list items
2293 ;; Borrowed from a2r.el (version 1.3), by Lawrence Mitchell <wence@gmx.li>. I
2294 ;; needed to make some tiny changes to the functions, so I put it here.
2295 ;; -- Wei-Wei Guo
2297 (defconst rst-arabic-to-roman
2298 '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2299 (100 . "C") (90 . "XC") (50 . "L") (40 . "XL")
2300 (10 . "X") (9 . "IX") (5 . "V") (4 . "IV")
2301 (1 . "I"))
2302 "List of maps between Arabic numbers and their Roman numeral equivalents.")
2304 (defun rst-arabic-to-roman (num)
2305 ;; testcover: ok.
2306 "Convert Arabic number NUM to its Roman numeral representation.
2308 Obviously, NUM must be greater than zero. Don't blame me, blame the
2309 Romans, I mean \"what have the Romans ever _done_ for /us/?\" (with
2310 apologies to Monty Python)."
2311 (cl-check-type num (integer 1 *))
2312 (let ((map rst-arabic-to-roman)
2313 (r ""))
2314 (while (and map (> num 0))
2315 (cl-destructuring-bind ((val &rest sym) &rest next) map
2316 (if (>= num val)
2317 (setq r (concat r sym)
2318 num (- num val))
2319 (setq map next))))
2322 (defun rst-roman-to-arabic (string)
2323 ;; testcover: ok.
2324 "Convert STRING of Roman numerals to an Arabic number.
2325 If STRING contains a letter which isn't a valid Roman numeral,
2326 the rest of the string from that point onwards is ignored.
2327 Hence:
2328 MMD == 2500
2330 MMDFLXXVI == 2500."
2331 (cl-check-type string string)
2332 (cl-check-type string (satisfies (lambda (s)
2333 (not (equal s ""))))
2334 "Roman number may not be an empty string.")
2335 (let ((res 0)
2336 (map rst-arabic-to-roman))
2337 (save-match-data
2338 (while map
2339 (cl-destructuring-bind ((val &rest sym) &rest next) map
2340 (if (string-match (concat "^" sym) string)
2341 (setq res (+ res val)
2342 string (replace-match "" nil t string))
2343 (setq map next))))
2344 (cl-check-type string (satisfies (lambda (s)
2345 (equal s "")))
2346 "Invalid characters in roman number")
2347 res)))
2349 ;; End of borrow.
2351 ;; FIXME: All the following code should not consider single lines as items but
2352 ;; paragraphs as reST does.
2354 (defun rst-insert-list-new-tag (tag)
2355 ;; testcover: ok.
2356 "Insert first item of a new list tagged with TAG.
2358 Adding a new list might consider three situations:
2360 (a) Current line is a blank line.
2361 (b) Previous line is a blank line.
2362 (c) Following line is a blank line.
2364 When (a) and (b), just add the new list at current line.
2366 when (a) and not (b), a blank line is added before adding the new list.
2368 When not (a), first forward point to the end of the line, and add two
2369 blank lines, then add the new list.
2371 Other situations are just ignored and left to users themselves."
2372 ;; FIXME: Following line is not considered at all.
2373 (let ((pfx-nls
2374 ;; FIXME: Doesn't work properly for white-space line. See
2375 ;; `rst-insert-list-new-BUGS'.
2376 (if (rst-forward-line-looking-at 0 'lin-end)
2377 (if (not (rst-forward-line-looking-at -1 'lin-end #'not))
2380 2)))
2381 (end-of-line)
2382 ;; FIXME: The indentation is not fixed to a single space by the syntax. May
2383 ;; be this should be configurable or rather taken from the context.
2384 (insert (make-string pfx-nls ?\n) tag " ")))
2386 (defconst rst-initial-items
2387 (append (mapcar #'char-to-string rst-bullets)
2388 (let (vals)
2389 (dolist (fmt '("%s." "(%s)" "%s)"))
2390 (dolist (c '("#" "1" "a" "A" "I" "i"))
2391 (push (format fmt c) vals)))
2392 (nreverse vals)))
2393 "List of initial items. It's a collection of bullets and enumerations.")
2395 (defun rst-insert-list-new-item ()
2396 ;; testcover: ok.
2397 "Insert a new list item.
2399 User is asked to select the item style first, for example (a), i), +.
2400 Use TAB for completion and choices.
2402 If user selects bullets or #, it's just added with position arranged by
2403 `rst-insert-list-new-tag'.
2405 If user selects enumerations, a further prompt is given. User need to
2406 input a starting item, for example 'e' for 'A)' style. The position is
2407 also arranged by `rst-insert-list-new-tag'."
2408 (let* ((itemstyle (completing-read
2409 "Select preferred item style [#.]: "
2410 rst-initial-items nil t nil nil "#."))
2411 (cnt (if (string-match (rst-re 'cntexp-tag) itemstyle)
2412 (match-string 0 itemstyle)))
2414 (save-match-data
2415 (cond
2416 ((equal cnt "a")
2417 (let ((itemno (read-string "Give starting value [a]: "
2418 nil nil "a")))
2419 (downcase (substring itemno 0 1))))
2420 ((equal cnt "A")
2421 (let ((itemno (read-string "Give starting value [A]: "
2422 nil nil "A")))
2423 (upcase (substring itemno 0 1))))
2424 ((equal cnt "I")
2425 (let ((itemno (read-number "Give starting value [1]: " 1)))
2426 (rst-arabic-to-roman itemno)))
2427 ((equal cnt "i")
2428 (let ((itemno (read-number "Give starting value [1]: " 1)))
2429 (downcase (rst-arabic-to-roman itemno))))
2430 ((equal cnt "1")
2431 (let ((itemno (read-number "Give starting value [1]: " 1)))
2432 (number-to-string itemno)))))))
2433 (if no
2434 (setq itemstyle (replace-match no t t itemstyle)))
2435 (rst-insert-list-new-tag itemstyle)))
2437 (defcustom rst-preferred-bullets
2438 '(?* ?- ?+)
2439 "List of favorite bullets."
2440 :group 'rst
2441 :type `(repeat
2442 (choice ,@(mapcar #'(lambda (char)
2443 (list 'const
2444 :tag (char-to-string char) char))
2445 rst-bullets)))
2446 :package-version '(rst . "1.1.0"))
2447 (rst-testcover-defcustom)
2449 (defun rst-insert-list-continue (ind tag tab prefer-roman)
2450 ;; testcover: ok.
2451 "Insert a new list tag after the current line according to style.
2452 Style is defined by indentaton IND, TAG and suffix TAB. If
2453 PREFER-ROMAN roman numbering is preferred over using letters."
2454 (end-of-line)
2455 (insert
2456 ;; FIXME: Separating lines must be possible.
2457 "\n"
2459 (save-match-data
2460 (if (not (string-match (rst-re 'cntexp-tag) tag))
2462 (let ((pfx (substring tag 0 (match-beginning 0)))
2463 (cnt (match-string 0 tag))
2464 (sfx (substring tag (match-end 0))))
2465 (concat
2467 (cond
2468 ((string-match (rst-re 'num-tag) cnt)
2469 (number-to-string (1+ (string-to-number (match-string 0 cnt)))))
2470 ((and
2471 (string-match (rst-re 'rom-tag) cnt)
2472 (save-match-data
2473 (if (string-match (rst-re 'ltr-tag) cnt) ; Also a letter tag.
2474 (save-excursion
2475 ;; FIXME: Assumes one line list items without separating
2476 ;; empty lines.
2477 ;; Use of `rst-forward-line-looking-at' is very difficult
2478 ;; here so don't do it.
2479 (if (and (rst-forward-line-strict -1)
2480 (looking-at (rst-re 'enmexp-beg)))
2481 (string-match
2482 (rst-re 'rom-tag)
2483 (match-string 0)) ; Previous was a roman tag.
2484 prefer-roman)) ; Don't know - use flag.
2485 t))) ; Not a letter tag.
2486 (let* ((old (match-string 0 cnt))
2487 (new (rst-arabic-to-roman
2488 (1+ (rst-roman-to-arabic (upcase old))))))
2489 (if (equal old (upcase old))
2490 (upcase new)
2491 (downcase new))))
2492 ((string-match (rst-re 'ltr-tag) cnt)
2493 (char-to-string (1+ (string-to-char (match-string 0 cnt))))))
2494 sfx))))
2495 tab))
2497 ;; FIXME: At least the continuation may be folded into
2498 ;; `newline-and-indent`. However, this may not be wanted by everyone so
2499 ;; it should be possible to switch this off.
2500 (defun rst-insert-list (&optional prefer-roman)
2501 ;; testcover: ok.
2502 "Insert a list item at the current point.
2504 The command can insert a new list or a continuing list. When it is called at a
2505 non-list line, it will promote to insert new list. When it is called at a list
2506 line, it will insert a list with the same list style.
2508 1. When inserting a new list:
2510 User is asked to select the item style first, for example (a), i), +. Use TAB
2511 for completion and choices.
2513 (a) If user selects bullets or #, it's just added.
2514 (b) If user selects enumerations, a further prompt is given. User needs to
2515 input a starting item, for example `e' for `A)' style.
2517 The position of the new list is arranged according to whether or not the
2518 current line and the previous line are blank lines.
2520 2. When continuing a list, one thing needs to be noticed:
2522 List style alphabetical list, such as `a.', and roman numerical list, such as
2523 `i.', have some overlapping items, for example `v.' The function can deal with
2524 the problem elegantly in most situations. But when those overlapped list are
2525 preceded by a blank line, it is hard to determine which type to use
2526 automatically. The function uses alphabetical list by default. If you want
2527 roman numerical list, just use a prefix to set PREFER-ROMAN."
2528 (interactive "P")
2529 (save-match-data
2530 (1value
2531 (rst-forward-line-strict 0))
2532 ;; FIXME: Finds only tags in single line items. Multi-line items should be
2533 ;; considered as well.
2534 ;; Using `rst-forward-line-looking-at' is more complicated so don't do it.
2535 (if (looking-at (rst-re 'itmany-beg-1))
2536 (rst-insert-list-continue
2537 (buffer-substring-no-properties
2538 (match-beginning 0) (match-beginning 1))
2539 (match-string 1)
2540 (buffer-substring-no-properties (match-end 1) (match-end 0))
2541 prefer-roman)
2542 (rst-insert-list-new-item))))
2544 ;; FIXME: This is wrong because it misses prefixed lines without intervening
2545 ;; new line. See `rst-straighten-bullets-region-BUGS' and
2546 ;; `rst-find-begs-BUGS'.
2547 (defun rst-find-begs (beg end rst-re-beg)
2548 ;; testcover: ok.
2549 "Return the positions of begs in region BEG to END.
2550 RST-RE-BEG is a `rst-re' argument and matched at the beginning of
2551 a line. Return a list of (POINT . COLUMN) where POINT gives the
2552 point after indentaton and COLUMN gives its column. The list is
2553 ordererd by POINT."
2554 (let (r)
2555 (save-match-data
2556 (save-excursion
2557 ;; FIXME refactoring: Consider making this construct a macro looping
2558 ;; over the lines.
2559 (goto-char beg)
2560 (1value
2561 (rst-forward-line-strict 0))
2562 (while (< (point) end)
2563 (let ((clm (current-indentation)))
2564 ;; FIXME refactoring: Consider using `rst-forward-line-looking-at'.
2565 (when (and
2566 (looking-at (rst-re rst-re-beg)) ; Start found
2567 (not (rst-forward-line-looking-at
2568 -1 'lin-end
2569 #'(lambda (mtcd) ; Previous line exists and is...
2570 (and
2571 (not mtcd) ; non-empty,
2572 (<= (current-indentation) clm) ; less indented
2573 (not (and (= (current-indentation) clm)
2574 ; not a beg at same level.
2575 (looking-at (rst-re rst-re-beg)))))))))
2576 (back-to-indentation)
2577 (push (cons (point) clm) r)))
2578 (1value ; At least one line is moved in this loop.
2579 (rst-forward-line-strict 1 end)))))
2580 (nreverse r)))
2582 (defun rst-straighten-bullets-region (beg end)
2583 ;; testcover: ok.
2584 "Make all the bulleted list items in the region from BEG to END consistent.
2585 Use this after you have merged multiple bulleted lists to make
2586 them use the preferred bullet characters given by
2587 `rst-preferred-bullets' for each level. If bullets are found on
2588 levels beyond the `rst-preferred-bullets' list, they are not
2589 modified."
2590 (interactive "r")
2591 (save-excursion
2592 (let (clm2pnts) ; Map a column to a list of points at this column.
2593 (rst-destructuring-dolist
2594 ((point &rest column
2595 &aux (found (assoc column clm2pnts)))
2596 (rst-find-begs beg end 'bul-beg))
2597 (if found
2598 ;;; (push point (cdr found)) ; FIXME: Doesn't work with `testcover'.
2599 (setcdr found (cons point (cdr found))) ; Synonym.
2600 (push (list column point) clm2pnts)))
2601 (rst-destructuring-dolist
2602 ((bullet _clm &rest pnts)
2603 ;; Zip preferred bullets and sorted columns associating a bullet
2604 ;; with a column and all the points this column is found.
2605 (cl-mapcar #'(lambda (bullet clm2pnt)
2606 (cons bullet clm2pnt))
2607 rst-preferred-bullets
2608 (sort clm2pnts #'car-less-than-car)))
2609 ;; Replace the bullets by the preferred ones.
2610 (dolist (pnt pnts)
2611 (goto-char pnt)
2612 ;; FIXME: Assumes bullet to replace is a single char.
2613 (delete-char 1)
2614 (insert bullet))))))
2617 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2618 ;; Table of contents
2620 (defun rst-all-stn ()
2621 ;; testcover: ok.
2622 "Return the hierarchical tree of sections as a top level `rst-Stn'.
2623 Return value satisfies `rst-Stn-is-top' or is nil for no
2624 sections."
2625 (cdr (rst-remaining-stn (rst-all-ttls-with-level) -1)))
2627 (defun rst-remaining-stn (unprocessed expected)
2628 ;; testcover: ok.
2629 "Process the first entry of UNPROCESSED expected to be on level EXPECTED.
2630 UNPROCESSED is the remaining list of (`rst-Ttl' . LEVEL) entries.
2631 Return (REMAINING . STN) for the first entry of UNPROCESSED.
2632 REMAINING is the list of still unprocessed entries. STN is a
2633 `rst-Stn' or nil if UNPROCESSED is empty."
2634 (if (not unprocessed)
2635 (1value
2636 (cons nil nil))
2637 (cl-destructuring-bind
2638 ((ttl &rest level) &rest next
2639 &aux fnd children)
2640 unprocessed
2641 (when (= level expected)
2642 ;; Consume the current entry and create the current node with it.
2643 (setq fnd ttl)
2644 (setq unprocessed next))
2645 ;; Build the child nodes as long as they have deeper level.
2646 (while (and unprocessed (> (cdar unprocessed) expected))
2647 (cl-destructuring-bind (remaining &rest stn)
2648 (rst-remaining-stn unprocessed (1+ expected))
2649 (when stn
2650 (push stn children))
2651 (setq unprocessed remaining)))
2652 (cons unprocessed
2653 (when (or fnd children)
2654 (rst-Stn-new fnd expected (nreverse children)))))))
2656 (defun rst-stn-containing-point (stn &optional point)
2657 ;; testcover: ok.
2658 "Return `rst-Stn' in STN before POINT or nil if in no section.
2659 POINT defaults to the current point. STN may be nil for no
2660 section headers at all."
2661 (when stn
2662 (setq point (or point (point)))
2663 (when (>= point (rst-Stn-get-title-beginning stn))
2664 ;; Point may be in this section or a child.
2665 (let ((in-child (cl-find-if
2666 #'(lambda (child)
2667 (>= point (rst-Stn-get-title-beginning child)))
2668 (rst-Stn-children stn)
2669 :from-end t)))
2670 (if in-child
2671 (rst-stn-containing-point in-child point)
2672 stn)))))
2674 (defgroup rst-toc nil
2675 "Settings for reStructuredText table of contents."
2676 :group 'rst
2677 :version "21.1")
2679 (defcustom rst-toc-indent 2
2680 "Indentation for table-of-contents display.
2681 Also used for formatting insertion, when numbering is disabled."
2682 :type 'integer
2683 :group 'rst-toc)
2684 (rst-testcover-defcustom)
2686 (defcustom rst-toc-insert-style 'fixed
2687 "Insertion style for table-of-contents.
2688 Set this to one of the following values to determine numbering and
2689 indentation style:
2690 - `plain': no numbering (fixed indentation)
2691 - `fixed': numbering, but fixed indentation
2692 - `aligned': numbering, titles aligned under each other
2693 - `listed': titles as list items"
2694 :type '(choice (const plain)
2695 (const fixed)
2696 (const aligned)
2697 (const listed))
2698 :group 'rst-toc)
2699 (rst-testcover-defcustom)
2701 (defcustom rst-toc-insert-number-separator " "
2702 "Separator that goes between the TOC number and the title."
2703 :type 'string
2704 :group 'rst-toc)
2705 (rst-testcover-defcustom)
2707 (defcustom rst-toc-insert-max-level nil
2708 "If non-nil, maximum depth of the inserted TOC."
2709 :type '(choice (const nil) integer)
2710 :group 'rst-toc)
2711 (rst-testcover-defcustom)
2713 (defun rst-toc-insert (&optional max-level)
2714 ;; testcover: ok.
2715 "Insert the table of contents of the current section at the current column.
2716 By default the top level is ignored if there is only one, because
2717 we assume that the document will have a single title. A numeric
2718 prefix argument MAX-LEVEL overrides `rst-toc-insert-max-level'.
2719 Text in the line beyond column is deleted."
2720 (interactive "P")
2721 (rst-reset-section-caches)
2722 (let ((pt-stn (rst-stn-containing-point (rst-all-stn))))
2723 (when pt-stn
2724 (let ((max
2725 (if (and (integerp max-level)
2726 (> (prefix-numeric-value max-level) 0))
2727 (prefix-numeric-value max-level)
2728 rst-toc-insert-max-level))
2729 (ind (current-column))
2730 (buf (current-buffer))
2731 (tabs indent-tabs-mode) ; Copy buffer local value.
2732 txt)
2733 (setq txt
2734 ;; Render to temporary buffer so markers are created correctly.
2735 (with-temp-buffer
2736 (rst-toc-insert-tree pt-stn buf rst-toc-insert-style max
2737 rst-toc-link-keymap nil)
2738 (goto-char (point-min))
2739 (when (rst-forward-line-strict 1)
2740 ;; There are lines to indent.
2741 (let ((indent-tabs-mode tabs))
2742 (indent-rigidly (point) (point-max) ind)))
2743 (buffer-string)))
2744 (unless (zerop (length txt))
2745 ;; Delete possible trailing text.
2746 (delete-region (point) (line-beginning-position 2))
2747 (insert txt)
2748 (backward-char 1))))))
2750 (defun rst-toc-insert-link (pfx stn buf keymap)
2751 ;; testcover: ok.
2752 "Insert text of STN in BUF as a linked section reference at point.
2753 If KEYMAP use this as keymap property. PFX is inserted before text."
2754 (let ((beg (point)))
2755 (insert pfx)
2756 (insert (rst-Stn-get-text stn))
2757 (put-text-property beg (point) 'mouse-face 'highlight)
2758 (insert "\n")
2759 (put-text-property
2760 beg (point) 'rst-toc-target
2761 (set-marker (make-marker) (rst-Stn-get-title-beginning stn) buf))
2762 (when keymap
2763 (put-text-property beg (point) 'keymap keymap))))
2765 (defun rst-toc-get-link (link-buf link-pnt)
2766 ;; testcover: ok.
2767 "Return the link from text property at LINK-PNT in LINK-BUF."
2768 (let ((mrkr (get-text-property link-pnt 'rst-toc-target link-buf)))
2769 (unless mrkr
2770 (error "No section on this line"))
2771 (unless (buffer-live-p (marker-buffer mrkr))
2772 (error "Buffer for this section was killed"))
2773 mrkr))
2775 (defconst rst-toc-link-keymap
2776 (let ((map (make-sparse-keymap)))
2777 (define-key map [mouse-1] 'rst-toc-mouse-follow-link)
2778 map)
2779 "Keymap used for links in TOC.")
2781 (defun rst-toc-insert-tree (stn buf style depth keymap tgt-stn)
2782 ;; testcover: ok.
2783 "Insert table of contents of tree below top node STN in buffer BUF.
2784 STYLE is the style to use and must be one of the symbols allowed
2785 for `rst-toc-insert-style'. DEPTH is the maximum relative depth
2786 from STN to insert or nil for no maximum depth. See
2787 `rst-toc-insert-link' for KEYMAP. Return beginning of title line
2788 if TGT-STN is rendered or nil if not rendered or TGT-STN is nil.
2789 Just return nil if STN is nil."
2790 (when stn
2791 (rst-toc-insert-children (rst-Stn-children stn) buf style depth 0 "" keymap
2792 tgt-stn)))
2794 (defun rst-toc-insert-children (children buf style depth indent numbering
2795 keymap tgt-stn)
2796 ;; testcover: ok.
2797 "In the current buffer at point insert CHILDREN in BUF to table of contents.
2798 See `rst-toc-insert-tree' for STYLE, DEPTH and TGT-STN. See
2799 `rst-toc-insert-stn' for INDENT and NUMBERING. See
2800 `rst-toc-insert-link' for KEYMAP."
2801 (let ((count 1)
2802 ;; Child numbering is done from the parent.
2803 (num-fmt (format "%%%dd"
2804 (1+ (floor (log (1+ (length children)) 10)))))
2805 fnd)
2806 (when (not (equal numbering ""))
2807 ;; Add separating dot to existing numbering.
2808 (setq numbering (concat numbering ".")))
2809 (dolist (child children fnd)
2810 (setq fnd
2811 (or (rst-toc-insert-stn child buf style depth indent
2812 (concat numbering (format num-fmt count))
2813 keymap tgt-stn) fnd))
2814 (cl-incf count))))
2816 ;; FIXME refactoring: Use `rst-Stn-buffer' instead of `buf'.
2817 (defun rst-toc-insert-stn (stn buf style depth indent numbering keymap tgt-stn)
2818 ;; testcover: ok.
2819 "In the current buffer at point insert STN in BUF into table of contents.
2820 See `rst-toc-insert-tree' for STYLE, DEPTH and TGT-STN. INDENT
2821 is the indentation depth to use for STN. NUMBERING is the prefix
2822 numbering for STN. See `rst-toc-insert-link' for KEYMAP."
2823 (when (or (not depth) (> depth 0))
2824 (cl-destructuring-bind
2825 (pfx add
2826 &aux (fnd (when (and tgt-stn
2827 (equal (rst-Stn-get-title-beginning stn)
2828 (rst-Stn-get-title-beginning tgt-stn)))
2829 (point))))
2830 (cond
2831 ((eq style 'plain)
2832 (list "" rst-toc-indent))
2833 ((eq style 'fixed)
2834 (list (concat numbering rst-toc-insert-number-separator)
2835 rst-toc-indent))
2836 ((eq style 'aligned)
2837 (list (concat numbering rst-toc-insert-number-separator)
2838 (+ (length numbering)
2839 (length rst-toc-insert-number-separator))))
2840 ((eq style 'listed)
2841 (list (format "%c " (car rst-preferred-bullets)) 2)))
2842 ;; Indent using spaces so buffer characteristics like `indent-tabs-mode'
2843 ;; do not matter.
2844 (rst-toc-insert-link (concat (make-string indent ? ) pfx) stn buf keymap)
2845 (or (rst-toc-insert-children (rst-Stn-children stn) buf style
2846 (when depth
2847 (1- depth))
2848 (+ indent add) numbering keymap tgt-stn)
2849 fnd))))
2851 (defun rst-toc-update ()
2852 ;; testcover: ok.
2853 "Automatically find the contents section of a document and update.
2854 Updates the inserted TOC if present. You can use this in your
2855 file-write hook to always make it up-to-date automatically."
2856 (interactive)
2857 (save-match-data
2858 (save-excursion
2859 ;; Find and delete an existing comment after the first contents
2860 ;; directive. Delete that region.
2861 (goto-char (point-min))
2862 ;; FIXME: Should accept indentation of the whole block.
2863 ;; We look for the following and the following only (in other words, if
2864 ;; your syntax differs, this won't work.).
2866 ;; .. contents:: [...anything here...]
2867 ;; [:field: value]...
2868 ;; ..
2869 ;; XXXXXXXX
2870 ;; XXXXXXXX
2871 ;; [more lines]
2872 ;; FIXME: Works only for the first of these tocs. There should be a
2873 ;; fixed text after the comment such as "RST-MODE ELECTRIC TOC".
2874 ;; May be parameters such as `max-level' should be appended.
2875 (let ((beg (re-search-forward
2876 (1value
2877 (rst-re "^" 'exm-sta "contents" 'dcl-tag ".*\n"
2878 "\\(?:" 'hws-sta 'fld-tag ".*\n\\)*" 'exm-tag))
2879 nil t))
2880 fnd)
2881 (when
2882 (and beg
2883 (rst-forward-line-looking-at
2884 1 'lin-end
2885 #'(lambda (mtcd)
2886 (unless mtcd
2887 (rst-apply-indented-blocks
2888 (point) (point-max) (current-indentation)
2889 #'(lambda (count _in-first _in-sub in-super in-empty
2890 _relind)
2891 (cond
2892 ((or (> count 1) in-super))
2893 ((not in-empty)
2894 (setq fnd (line-end-position))
2895 nil)))))
2896 t)))
2897 (when fnd
2898 (delete-region beg fnd))
2899 (goto-char beg)
2900 (insert "\n ")
2901 ;; FIXME: Ignores an `max-level' given to the original
2902 ;; `rst-toc-insert'. `max-level' could be rendered to the first
2903 ;; line.
2904 (rst-toc-insert)))))
2905 ;; Note: always return nil, because this may be used as a hook.
2906 nil)
2908 ;; FIXME: Updating the toc on saving would be nice. However, this doesn't work
2909 ;; correctly:
2911 ;; (add-hook 'write-contents-hooks 'rst-toc-update-fun)
2912 ;; (defun rst-toc-update-fun ()
2913 ;; ;; Disable undo for the write file hook.
2914 ;; (let ((buffer-undo-list t)) (rst-toc-update) ))
2916 ;; Maintain an alias for compatibility.
2917 (defalias 'rst-toc-insert-update 'rst-toc-update)
2919 (defconst rst-toc-buffer-name "*Table of Contents*"
2920 "Name of the Table of Contents buffer.")
2922 (defvar-local rst-toc-mode-return-wincfg nil
2923 "Window configuration to which to return when leaving the TOC.")
2925 (defun rst-toc ()
2926 ;; testcover: ok.
2927 "Display a table of contents for current buffer.
2928 Displays all section titles found in the current buffer in a
2929 hierarchical list. The resulting buffer can be navigated, and
2930 selecting a section title moves the cursor to that section."
2931 (interactive)
2932 (rst-reset-section-caches)
2933 (let* ((wincfg (list (current-window-configuration) (point-marker)))
2934 (sectree (rst-all-stn))
2935 (target-stn (rst-stn-containing-point sectree))
2936 (target-buf (current-buffer))
2937 (buf (get-buffer-create rst-toc-buffer-name))
2938 target-pos)
2939 (with-current-buffer buf
2940 (let ((inhibit-read-only t))
2941 (rst-toc-mode)
2942 (delete-region (point-min) (point-max))
2943 ;; FIXME: Could use a customizable style.
2944 (setq target-pos (rst-toc-insert-tree
2945 sectree target-buf 'plain nil nil target-stn))))
2946 (display-buffer buf)
2947 (pop-to-buffer buf)
2948 (setq rst-toc-mode-return-wincfg wincfg)
2949 (goto-char (or target-pos (point-min)))))
2951 ;; Maintain an alias for compatibility.
2952 (defalias 'rst-goto-section 'rst-toc-follow-link)
2954 (defun rst-toc-follow-link (link-buf link-pnt kill)
2955 ;; testcover: ok.
2956 "Follow the link to the section at LINK-PNT in LINK-BUF.
2957 LINK-PNT and LINK-BUF default to the point in the current buffer.
2958 With prefix argument KILL a TOC buffer is destroyed. Throw an
2959 error if there is no working link at the given position."
2960 (interactive "i\nd\nP")
2961 (unless link-buf
2962 (setq link-buf (current-buffer)))
2963 ;; Do not catch errors from `rst-toc-get-link' because otherwise the error is
2964 ;; suppressed and invisible in interactve use.
2965 (let ((mrkr (rst-toc-get-link link-buf link-pnt)))
2966 (condition-case nil
2967 (rst-toc-mode-return kill)
2968 ;; Catch errors when not in `toc-mode'.
2969 (error nil))
2970 (pop-to-buffer (marker-buffer mrkr))
2971 (goto-char mrkr)
2972 ;; FIXME: Should be a customizable number of lines from beginning or end of
2973 ;; window just like the argument to `recenter`. It would be ideal if
2974 ;; the adornment is always completely visible.
2975 (recenter 5)))
2977 ;; Maintain an alias for compatibility.
2978 (defalias 'rst-toc-mode-goto-section 'rst-toc-mode-follow-link-kill)
2980 ;; FIXME: Cursor before or behind the list must be handled properly; before the
2981 ;; list should jump to the top and behind the list to the last normal
2982 ;; paragraph.
2983 (defun rst-toc-mode-follow-link-kill ()
2984 ;; testcover: ok.
2985 "Follow the link to the section at point and kill the TOC buffer."
2986 (interactive)
2987 (rst-toc-follow-link (current-buffer) (point) t))
2989 ;; Maintain an alias for compatibility.
2990 (defalias 'rst-toc-mode-mouse-goto 'rst-toc-mouse-follow-link)
2992 (defun rst-toc-mouse-follow-link (event kill)
2993 ;; testcover: uncovered.
2994 "In `rst-toc' mode, go to the occurrence whose line you click on.
2995 EVENT is the input event. Kill TOC buffer if KILL."
2996 (interactive "e\ni")
2997 (rst-toc-follow-link (window-buffer (posn-window (event-end event)))
2998 (posn-point (event-end event)) kill))
3000 ;; Maintain an alias for compatibility.
3001 (defalias 'rst-toc-mode-mouse-goto-kill 'rst-toc-mode-mouse-follow-link-kill)
3003 (defun rst-toc-mode-mouse-follow-link-kill (event)
3004 ;; testcover: uncovered.
3005 "Same as `rst-toc-mouse-follow-link', but kill TOC buffer as well.
3006 EVENT is the input event."
3007 (interactive "e")
3008 (rst-toc-mouse-follow-link event t))
3010 ;; Maintain an alias for compatibility.
3011 (defalias 'rst-toc-quit-window 'rst-toc-mode-return)
3013 (defun rst-toc-mode-return (kill)
3014 ;; testcover: ok.
3015 "Leave the current TOC buffer and return to the previous environment.
3016 With prefix argument KILL non-nil, kill the buffer instead of
3017 burying it."
3018 (interactive "P")
3019 (unless rst-toc-mode-return-wincfg
3020 (error "Not in a `toc-mode' buffer"))
3021 (cl-destructuring-bind
3022 (wincfg pos
3023 &aux (toc-buf (current-buffer)))
3024 rst-toc-mode-return-wincfg
3025 (set-window-configuration wincfg)
3026 (goto-char pos)
3027 (if kill
3028 (kill-buffer toc-buf)
3029 (bury-buffer toc-buf))))
3031 (defun rst-toc-mode-return-kill ()
3032 ;; testcover: uncovered.
3033 "Like `rst-toc-mode-return' but kill TOC buffer."
3034 (interactive)
3035 (rst-toc-mode-return t))
3037 (defvar rst-toc-mode-map
3038 (let ((map (make-sparse-keymap)))
3039 (define-key map [mouse-1] #'rst-toc-mode-mouse-follow-link-kill)
3040 (define-key map [mouse-2] #'rst-toc-mouse-follow-link)
3041 (define-key map "\C-m" #'rst-toc-mode-follow-link-kill)
3042 (define-key map "f" #'rst-toc-mode-follow-link-kill)
3043 (define-key map "n" #'next-line)
3044 (define-key map "p" #'previous-line)
3045 (define-key map "q" #'rst-toc-mode-return)
3046 (define-key map "z" #'rst-toc-mode-return-kill)
3047 map)
3048 "Keymap for `rst-toc-mode'.")
3050 (define-derived-mode rst-toc-mode special-mode "ReST-TOC"
3051 "Major mode for output from \\[rst-toc], the table-of-contents for the document.
3052 \\{rst-toc-mode-map}"
3053 ;; FIXME: `revert-buffer-function` must be defined so `revert-buffer` works
3054 ;; as expected for a special mode. In particular the referred buffer
3055 ;; needs to be rescanned and the TOC must be updated accordingly.
3056 ;; FIXME: Should contain the name of the buffer this is the toc of.
3057 (setq header-line-format "Table of Contents"))
3059 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3060 ;; Section movement
3062 ;; FIXME testcover: Use `testcover'. Mark up a function with sufficient test
3063 ;; coverage by a comment tagged with `testcover' after the
3064 ;; `defun'. Then move this comment.
3066 (defun rst-forward-section (offset)
3067 "Jump forward OFFSET section titles ending up at the start of the title line.
3068 OFFSET defaults to 1 and may be negative to move backward. An
3069 OFFSET of 0 does not move unless point is inside a title. Go to
3070 end or beginning of buffer if no more section titles in the desired
3071 direction."
3072 (interactive "p")
3073 (rst-reset-section-caches)
3074 (let* ((ttls (rst-all-ttls))
3075 (count (length ttls))
3076 (pnt (point))
3077 (contained nil) ; Title contains point (or is after point otherwise).
3078 (found (or (cl-position-if
3079 ;; Find a title containing or after point.
3080 #'(lambda (ttl)
3081 (let ((cmp (rst-Ttl-contains ttl pnt)))
3082 (cond
3083 ((= cmp 0) ; Title contains point.
3084 (setq contained t)
3086 ((> cmp 0) ; Title after point.
3087 t))))
3088 ttls)
3089 ;; Point after all titles.
3090 count))
3091 (target (+ found offset
3092 ;; If point is in plain text found title is already one
3093 ;; step forward.
3094 (if (and (not contained) (>= offset 0)) -1 0))))
3095 (goto-char (cond
3096 ((< target 0)
3097 (point-min))
3098 ((>= target count)
3099 (point-max))
3100 ((and (not contained) (= offset 0))
3101 ;; Point not in title and should not move - do not move.
3102 pnt)
3103 ((rst-Ttl-get-title-beginning (nth target ttls)))))))
3105 (defun rst-backward-section (offset)
3106 "Like `rst-forward-section', except move backward by OFFSET."
3107 (interactive "p")
3108 (rst-forward-section (- offset)))
3110 ;; FIXME: What is `allow-extend' for? See `mark-paragraph' for an explanation.
3111 (defun rst-mark-section (&optional count allow-extend)
3112 "Select COUNT sections around point.
3113 Mark following sections for positive COUNT or preceding sections
3114 for negative COUNT."
3115 ;; Cloned from mark-paragraph.
3116 (interactive "p\np")
3117 (unless count (setq count 1))
3118 (when (zerop count)
3119 (error "Cannot mark zero sections"))
3120 (cond ((and allow-extend
3121 (or (and (eq last-command this-command) (mark t))
3122 (use-region-p)))
3123 (set-mark
3124 (save-excursion
3125 (goto-char (mark))
3126 (rst-forward-section count)
3127 (point))))
3129 (rst-forward-section count)
3130 (push-mark nil t t)
3131 (rst-forward-section (- count)))))
3134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3135 ;; Indentation
3137 (defun rst-find-leftmost-column (beg end)
3138 "Return the leftmost column spanned by region BEG to END.
3139 The line containing the start of the region is always considered
3140 spanned. If the region ends at the beginning of a line this line
3141 is not considered spanned, otherwise it is spanned."
3142 (let (mincol)
3143 (save-match-data
3144 (save-excursion
3145 (goto-char beg)
3146 (1value
3147 (rst-forward-line-strict 0))
3148 (while (< (point) end)
3149 (unless (looking-at (rst-re 'lin-end))
3150 (setq mincol (if mincol
3151 (min mincol (current-indentation))
3152 (current-indentation))))
3153 (rst-forward-line-strict 1 end)))
3154 mincol)))
3156 ;; FIXME: At the moment only block comments with leading empty comment line are
3157 ;; supported. Comment lines with leading comment markup should be also
3158 ;; supported. May be a customizable option could control which style to
3159 ;; prefer.
3161 (defgroup rst-indent nil "Settings for indentation in reStructuredText.
3163 In reStructuredText indentation points are usually determined by
3164 preceding lines. Sometimes the syntax allows arbitrary indentation
3165 points such as where to start the first line following a directive.
3166 These indentation widths can be customized here."
3167 :group 'rst
3168 :package-version '(rst . "1.1.0"))
3170 (define-obsolete-variable-alias
3171 'rst-shift-basic-offset 'rst-indent-width "rst 1.0.0")
3172 (defcustom rst-indent-width 2
3173 "Indentation when there is no more indentation point given."
3174 :group 'rst-indent
3175 :type '(integer))
3176 (rst-testcover-defcustom)
3178 (defcustom rst-indent-field 3
3179 "Indentation for first line after a field or 0 to always indent for content."
3180 :group 'rst-indent
3181 :package-version '(rst . "1.1.0")
3182 :type '(integer))
3183 (rst-testcover-defcustom)
3185 (defcustom rst-indent-literal-normal 3
3186 "Default indentation for literal block after a markup on an own line."
3187 :group 'rst-indent
3188 :package-version '(rst . "1.1.0")
3189 :type '(integer))
3190 (rst-testcover-defcustom)
3192 (defcustom rst-indent-literal-minimized 2
3193 "Default indentation for literal block after a minimized markup."
3194 :group 'rst-indent
3195 :package-version '(rst . "1.1.0")
3196 :type '(integer))
3197 (rst-testcover-defcustom)
3199 (defcustom rst-indent-comment 3
3200 "Default indentation for first line of a comment."
3201 :group 'rst-indent
3202 :package-version '(rst . "1.1.0")
3203 :type '(integer))
3204 (rst-testcover-defcustom)
3206 ;; FIXME: Must consider other tabs:
3207 ;; * Line blocks
3208 ;; * Definition lists
3209 ;; * Option lists
3210 (defun rst-line-tabs ()
3211 "Return tabs of the current line or nil for no tab.
3212 The list is sorted so the tab where writing continues most likely
3213 is the first one. Each tab is of the form (COLUMN . INNER).
3214 COLUMN is the column of the tab. INNER is non-nil if this is an
3215 inner tab. I.e. a tab which does come from the basic indentation
3216 and not from inner alignment points."
3217 (save-excursion
3218 (rst-forward-line-strict 0)
3219 (save-match-data
3220 (unless (looking-at (rst-re 'lin-end))
3221 (back-to-indentation)
3222 ;; Current indentation is always the least likely tab.
3223 (let ((tabs (list (list (point) 0 nil)))) ; (POINT OFFSET INNER)
3224 ;; Push inner tabs more likely to continue writing.
3225 (cond
3226 ;; Item.
3227 ((looking-at (rst-re '(:grp itmany-tag hws-sta) '(:grp "\\S ") "?"))
3228 (when (match-string 2)
3229 (push (list (match-beginning 2) 0 t) tabs)))
3230 ;; Field.
3231 ((looking-at (rst-re '(:grp fld-tag) '(:grp hws-tag)
3232 '(:grp "\\S ") "?"))
3233 (unless (zerop rst-indent-field)
3234 (push (list (match-beginning 1) rst-indent-field t) tabs))
3235 (if (match-string 3)
3236 (push (list (match-beginning 3) 0 t) tabs)
3237 (if (zerop rst-indent-field)
3238 (push (list (match-end 2)
3239 (if (string= (match-string 2) "") 1 0)
3241 tabs))))
3242 ;; Directive.
3243 ((looking-at (rst-re 'dir-sta-3 '(:grp "\\S ") "?"))
3244 (push (list (match-end 1) 0 t) tabs)
3245 (unless (string= (match-string 2) "")
3246 (push (list (match-end 2) 0 t) tabs))
3247 (when (match-string 4)
3248 (push (list (match-beginning 4) 0 t) tabs)))
3249 ;; Footnote or citation definition.
3250 ((looking-at (rst-re 'fnc-sta-2 '(:grp "\\S ") "?"))
3251 (push (list (match-end 1) 0 t) tabs)
3252 (when (match-string 3)
3253 (push (list (match-beginning 3) 0 t) tabs)))
3254 ;; Comment.
3255 ((looking-at (rst-re 'cmt-sta-1))
3256 (push (list (point) rst-indent-comment t) tabs)))
3257 ;; Start of literal block.
3258 (when (looking-at (rst-re 'lit-sta-2))
3259 (cl-destructuring-bind (point offset _inner) (car tabs)
3260 (push (list point
3261 (+ offset
3262 (if (match-string 1)
3263 rst-indent-literal-minimized
3264 rst-indent-literal-normal))
3266 tabs)))
3267 (mapcar (cl-function
3268 (lambda ((point offset inner))
3269 (goto-char point)
3270 (cons (+ (current-column) offset) inner)))
3271 tabs))))))
3273 (defun rst-compute-tabs (pt)
3274 "Build the list of possible tabs for all lines above.
3275 Search backwards from point PT to build the list of possible tabs.
3276 Return a list of tabs sorted by likeliness to continue writing
3277 like `rst-line-tabs'. Nearer lines have generally a higher
3278 likeliness than farther lines. Return nil if no tab is found in
3279 the text above."
3280 ;; FIXME: See test `indent-for-tab-command-BUGS`.
3281 (save-excursion
3282 (goto-char pt)
3283 (let (leftmost ; Leftmost column found so far.
3284 innermost ; Leftmost column for inner tab.
3285 tablist)
3286 (while (and (rst-forward-line-strict -1)
3287 (or (not leftmost)
3288 (> leftmost 0)))
3289 (let ((tabs (rst-line-tabs)))
3290 (when tabs
3291 (let ((leftcol (apply #'min (mapcar #'car tabs))))
3292 ;; Consider only lines indented less or same if not INNERMOST.
3293 (when (or (not leftmost)
3294 (< leftcol leftmost)
3295 (and (not innermost) (= leftcol leftmost)))
3296 (rst-destructuring-dolist ((column &rest inner) tabs)
3297 (when (or
3298 (and (not inner)
3299 (or (not leftmost)
3300 (< column leftmost)))
3301 (and inner
3302 (or (not innermost)
3303 (< column innermost))))
3304 (setq tablist (cl-adjoin column tablist))))
3305 (setq innermost (if (cl-some #'cdr tabs) ; Has inner.
3306 leftcol
3307 innermost))
3308 (setq leftmost leftcol))))))
3309 (nreverse tablist))))
3311 (defun rst-indent-line (&optional dflt)
3312 "Indent current line to next best reStructuredText tab.
3313 The next best tab is taken from the tab list returned by
3314 `rst-compute-tabs' which is used in a cyclic manner. If the
3315 current indentation does not end on a tab use the first one. If
3316 the current indentation is on a tab use the next tab. This allows
3317 a repeated use of \\[indent-for-tab-command] to cycle through all
3318 possible tabs. If no indentation is possible return `noindent' or
3319 use DFLT. Return the indentation indented to. When point is in
3320 indentation it ends up at its end. Otherwise the point is kept
3321 relative to the content."
3322 (let* ((pt (point-marker))
3323 (cur (current-indentation))
3324 (clm (current-column))
3325 (tabs (rst-compute-tabs (point)))
3326 (fnd (cl-position cur tabs :test #'equal))
3327 ind)
3328 (if (and (not tabs) (not dflt))
3329 'noindent
3330 (if (not tabs)
3331 (setq ind dflt)
3332 (if (not fnd)
3333 (setq fnd 0)
3334 (setq fnd (1+ fnd))
3335 (if (>= fnd (length tabs))
3336 (setq fnd 0)))
3337 (setq ind (nth fnd tabs)))
3338 (indent-line-to ind)
3339 (if (> clm cur)
3340 (goto-char pt))
3341 (set-marker pt nil)
3342 ind)))
3344 (defun rst-shift-region (beg end cnt)
3345 "Shift region BEG to END by CNT tabs.
3346 Shift by one tab to the right (CNT > 0) or left (CNT < 0) or
3347 remove all indentation (CNT = 0). A tab is taken from the text
3348 above. If no suitable tab is found `rst-indent-width' is used."
3349 (interactive "r\np")
3350 (let ((tabs (sort (rst-compute-tabs beg)
3351 #'(lambda (x y)
3352 (<= x y))))
3353 (leftmostcol (rst-find-leftmost-column beg end)))
3354 (when (or (> leftmostcol 0) (> cnt 0))
3355 ;; Apply the indent.
3356 (indent-rigidly
3357 beg end
3358 (if (zerop cnt)
3359 (- leftmostcol)
3360 ;; Find the next tab after the leftmost column.
3361 (let* ((cmp (if (> cnt 0) #'> #'<))
3362 (tabs (if (> cnt 0) tabs (reverse tabs)))
3363 (len (length tabs))
3364 (dir (cl-signum cnt)) ; Direction to take.
3365 (abs (abs cnt)) ; Absolute number of steps to take.
3366 ;; Get the position of the first tab beyond leftmostcol.
3367 (fnd (cl-position-if #'(lambda (elt)
3368 (funcall cmp elt leftmostcol))
3369 tabs))
3370 ;; Virtual position of tab.
3371 (pos (+ (or fnd len) (1- abs)))
3372 (tab (if (< pos len)
3373 ;; Tab exists - use it.
3374 (nth pos tabs)
3375 ;; Column needs to be computed.
3376 (let ((col (+ (or (car (last tabs)) leftmostcol)
3377 ;; Base on last known column.
3378 (* (- pos (1- len)) ; Distance left.
3379 dir ; Direction to take.
3380 rst-indent-width))))
3381 (if (< col 0) 0 col)))))
3382 (- tab leftmostcol)))))))
3384 ;; FIXME: A paragraph with an (incorrectly) indented second line is not filled
3385 ;; correctly::
3387 ;; Some start
3388 ;; continued wrong
3389 (defun rst-adaptive-fill ()
3390 "Return fill prefix found at point.
3391 Value for `adaptive-fill-function'."
3392 (save-match-data
3393 (let ((fnd (if (looking-at adaptive-fill-regexp)
3394 (match-string-no-properties 0))))
3395 (if (save-match-data
3396 (not (string-match comment-start-skip fnd)))
3397 ;; An non-comment prefix is fine.
3399 ;; Matches a comment - return whitespace instead.
3400 (make-string (-
3401 (save-excursion
3402 (goto-char (match-end 0))
3403 (current-column))
3404 (save-excursion
3405 (goto-char (match-beginning 0))
3406 (current-column))) ? )))))
3408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3409 ;; Comments
3411 (defun rst-comment-line-break (&optional soft)
3412 "Break line and indent, continuing reStructuredText comment if within one.
3413 Value for `comment-line-break-function'. If SOFT use soft
3414 newlines as mandated by `comment-line-break-function'."
3415 (if soft
3416 (insert-and-inherit ?\n)
3417 (newline 1))
3418 (save-excursion
3419 (forward-char -1)
3420 (delete-horizontal-space))
3421 (delete-horizontal-space)
3422 (let ((tabs (rst-compute-tabs (point))))
3423 (when tabs
3424 (indent-line-to (car tabs)))))
3426 (defun rst-comment-indent ()
3427 "Return indentation for current comment line."
3428 (car (rst-compute-tabs (point))))
3430 (defun rst-comment-insert-comment ()
3431 "Insert a comment in the current line."
3432 (rst-indent-line 0)
3433 (insert comment-start))
3435 (defun rst-comment-region (beg end &optional arg)
3436 "Comment or uncomment the current region.
3437 Region is from BEG to END. Uncomment if ARG."
3438 (save-excursion
3439 (if (consp arg)
3440 (rst-uncomment-region beg end arg)
3441 (goto-char beg)
3442 (rst-forward-line-strict 0)
3443 (let ((ind (current-indentation))
3444 (bol (point)))
3445 (indent-rigidly bol end rst-indent-comment)
3446 (goto-char bol)
3447 (open-line 1)
3448 (indent-line-to ind)
3449 (insert (comment-string-strip comment-start t t))))))
3451 (defun rst-uncomment-region (beg end &optional _arg)
3452 "Uncomment the current region.
3453 Region is from BEG to END. _ARG is ignored"
3454 (save-excursion
3455 (goto-char beg)
3456 (rst-forward-line-strict 0)
3457 (let ((bol (point)))
3458 (rst-forward-line-strict 1 end)
3459 (indent-rigidly (point) end (- rst-indent-comment))
3460 (goto-char bol)
3461 (rst-delete-entire-line 0))))
3463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3464 ;; Apply to indented block
3466 ;; FIXME: These next functions should become part of a larger effort to redo
3467 ;; the bullets in bulleted lists. The enumerate would just be one of
3468 ;; the possible outputs.
3470 ;; FIXME: We need to do the enumeration removal as well.
3472 (defun rst-apply-indented-blocks (beg end ind fun)
3473 "Apply FUN to all lines from BEG to END in blocks indented to IND.
3474 The first indented block starts with the first non-empty line
3475 containing or after BEG and indented to IND. After the first
3476 line the indented block may contain more lines with same
3477 indentation (the paragraph) followed by empty lines and lines
3478 more indented (the sub-blocks). A following line indented to IND
3479 starts the next paragraph. A non-empty line with less
3480 indentation than IND terminates the current paragraph. FUN is
3481 applied to each line like this
3483 (FUN COUNT IN-FIRST IN-SUB IN-SUPER IN-EMPTY RELIND)
3485 COUNT is 0 before the first paragraph and increments for every
3486 paragraph found on level IND. IN-FIRST is non-nil if this is the
3487 first line of such a paragraph. IN-SUB is non-nil if this line
3488 is part of a sub-block while IN-SUPER is non-nil of this line is
3489 part of a less indented block (super-block). IN-EMPTY is non-nil
3490 if this line is empty where an empty line is considered being
3491 part of the previous block. RELIND is nil for an empty line, 0
3492 for a line indented to IND, and the positive or negative number
3493 of columns more or less indented otherwise. When FUN is called
3494 point is immediately behind indentation of that line. FUN may
3495 change everything as long as a marker at END and at the beginning
3496 of the following line is handled correctly by the change. A
3497 non-nil return value from FUN breaks the loop and is returned.
3498 Otherwise return nil."
3499 (let ((endm (copy-marker end t))
3500 (count 0) ; Before first indented block.
3501 (nxt (when (< beg end)
3502 (copy-marker beg t)))
3503 (broken t)
3504 in-sub in-super stop)
3505 (save-match-data
3506 (save-excursion
3507 (while (and (not stop) nxt)
3508 (set-marker
3509 (goto-char nxt) nil)
3510 (setq nxt (save-excursion
3511 ;; FIXME refactoring: Replace `(forward-line)
3512 ;; (back-to-indentation)` by
3513 ;; `(forward-to-indentation)`
3514 (when (and (rst-forward-line-strict 1 endm)
3515 (< (point) endm))
3516 (copy-marker (point) t))))
3517 (back-to-indentation)
3518 (let ((relind (- (current-indentation) ind))
3519 (in-empty (looking-at (rst-re 'lin-end)))
3520 in-first)
3521 (cond
3522 (in-empty
3523 (setq relind nil))
3524 ((< relind 0)
3525 (setq in-sub nil)
3526 (setq in-super t))
3527 ((> relind 0)
3528 (setq in-sub t)
3529 (setq in-super nil))
3530 (t ; Non-empty line in indented block.
3531 (when (or broken in-sub in-super)
3532 (setq in-first t)
3533 (cl-incf count))
3534 (setq in-sub nil)
3535 (setq in-super nil)))
3536 (save-excursion
3537 (setq
3538 stop
3539 (funcall fun count in-first in-sub in-super in-empty relind)))
3540 (setq broken in-empty)))
3541 (set-marker endm nil)
3542 stop))))
3544 (defun rst-enumerate-region (beg end all)
3545 "Add enumeration to all the leftmost paragraphs in the given region.
3546 The region is specified between BEG and END. With ALL,
3547 do all lines instead of just paragraphs."
3548 (interactive "r\nP")
3549 (let ((enum 0)
3550 (indent ""))
3551 (rst-apply-indented-blocks
3552 beg end (rst-find-leftmost-column beg end)
3553 #'(lambda (count in-first in-sub in-super in-empty _relind)
3554 (cond
3555 (in-empty)
3556 (in-super)
3557 ((zerop count))
3558 (in-sub
3559 (insert indent))
3560 ((or in-first all)
3561 (let ((tag (format "%d. " (cl-incf enum))))
3562 (setq indent (make-string (length tag) ? ))
3563 (insert tag)))
3565 (insert indent)))
3566 nil))))
3568 ;; FIXME: Does not deal with deeper indentation - although
3569 ;; `rst-apply-indented-blocks' could.
3570 (defun rst-bullet-list-region (beg end all)
3571 "Add bullets to all the leftmost paragraphs in the given region.
3572 The region is specified between BEG and END. With ALL,
3573 do all lines instead of just paragraphs."
3574 (interactive "r\nP")
3575 (unless rst-preferred-bullets
3576 (error "No preferred bullets defined"))
3577 (let* ((bul (format "%c " (car rst-preferred-bullets)))
3578 (indent (make-string (length bul) ? )))
3579 (rst-apply-indented-blocks
3580 beg end (rst-find-leftmost-column beg end)
3581 #'(lambda (count in-first in-sub in-super in-empty _relind)
3582 (cond
3583 (in-empty)
3584 (in-super)
3585 ((zerop count))
3586 (in-sub
3587 (insert indent))
3588 ((or in-first all)
3589 (insert bul))
3591 (insert indent)))
3592 nil))))
3594 ;; FIXME: Does not deal with a varying number of digits appropriately.
3595 ;; FIXME: Does not deal with multiple levels independently.
3596 ;; FIXME: Does not indent a multiline item correctly.
3597 (defun rst-convert-bullets-to-enumeration (beg end)
3598 "Convert the bulleted and enumerated items in the region to enumerated lists.
3599 Renumber as necessary. Region is from BEG to END."
3600 (interactive "r")
3601 (let ((count 1))
3602 (save-match-data
3603 (save-excursion
3604 (dolist (marker (mapcar
3605 (cl-function
3606 (lambda ((pnt &rest clm))
3607 (copy-marker pnt)))
3608 (rst-find-begs beg end 'itmany-beg-1)))
3609 (set-marker
3610 (goto-char marker) nil)
3611 (looking-at (rst-re 'itmany-beg-1))
3612 (replace-match (format "%d." count) nil nil nil 1)
3613 (cl-incf count))))))
3615 (defun rst-line-block-region (beg end &optional with-empty)
3616 "Add line block prefixes for a region.
3617 Region is from BEG to END. With WITH-EMPTY prefix empty lines too."
3618 (interactive "r\nP")
3619 (let ((ind (rst-find-leftmost-column beg end)))
3620 (rst-apply-indented-blocks
3621 beg end ind
3622 #'(lambda (_count _in-first _in-sub in-super in-empty _relind)
3623 (when (and (not in-super) (or with-empty (not in-empty)))
3624 (move-to-column ind t)
3625 (insert "| "))
3626 nil))))
3629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3630 ;; Font lock
3632 (require 'font-lock)
3634 ;; FIXME: The obsolete variables need to disappear.
3636 ;; The following versions have been done inside Emacs and should not be
3637 ;; replaced by `:package-version' attributes until a change.
3639 (defgroup rst-faces nil "Faces used in Rst Mode."
3640 :group 'rst
3641 :group 'faces
3642 :version "21.1")
3644 (defface rst-block '((t :inherit font-lock-keyword-face))
3645 "Face used for all syntax marking up a special block."
3646 :version "24.1"
3647 :group 'rst-faces)
3649 (defcustom rst-block-face 'rst-block
3650 "All syntax marking up a special block."
3651 :version "24.1"
3652 :group 'rst-faces
3653 :type '(face))
3654 (rst-testcover-defcustom)
3655 (make-obsolete-variable 'rst-block-face
3656 "customize the face `rst-block' instead."
3657 "24.1")
3659 (defface rst-external '((t :inherit font-lock-type-face))
3660 "Face used for field names and interpreted text."
3661 :version "24.1"
3662 :group 'rst-faces)
3664 (defcustom rst-external-face 'rst-external
3665 "Field names and interpreted text."
3666 :version "24.1"
3667 :group 'rst-faces
3668 :type '(face))
3669 (rst-testcover-defcustom)
3670 (make-obsolete-variable 'rst-external-face
3671 "customize the face `rst-external' instead."
3672 "24.1")
3674 (defface rst-definition '((t :inherit font-lock-function-name-face))
3675 "Face used for all other defining constructs."
3676 :version "24.1"
3677 :group 'rst-faces)
3679 (defcustom rst-definition-face 'rst-definition
3680 "All other defining constructs."
3681 :version "24.1"
3682 :group 'rst-faces
3683 :type '(face))
3684 (rst-testcover-defcustom)
3685 (make-obsolete-variable 'rst-definition-face
3686 "customize the face `rst-definition' instead."
3687 "24.1")
3689 ;; XEmacs compatibility (?).
3690 (defface rst-directive (if (boundp 'font-lock-builtin-face)
3691 '((t :inherit font-lock-builtin-face))
3692 '((t :inherit font-lock-preprocessor-face)))
3693 "Face used for directives and roles."
3694 :version "24.1"
3695 :group 'rst-faces)
3697 (defcustom rst-directive-face 'rst-directive
3698 "Directives and roles."
3699 :group 'rst-faces
3700 :type '(face))
3701 (rst-testcover-defcustom)
3702 (make-obsolete-variable 'rst-directive-face
3703 "customize the face `rst-directive' instead."
3704 "24.1")
3706 (defface rst-comment '((t :inherit font-lock-comment-face))
3707 "Face used for comments."
3708 :version "24.1"
3709 :group 'rst-faces)
3711 (defcustom rst-comment-face 'rst-comment
3712 "Comments."
3713 :version "24.1"
3714 :group 'rst-faces
3715 :type '(face))
3716 (rst-testcover-defcustom)
3717 (make-obsolete-variable 'rst-comment-face
3718 "customize the face `rst-comment' instead."
3719 "24.1")
3721 (defface rst-emphasis1 '((t :inherit italic))
3722 "Face used for simple emphasis."
3723 :version "24.1"
3724 :group 'rst-faces)
3726 (defcustom rst-emphasis1-face 'rst-emphasis1
3727 "Simple emphasis."
3728 :version "24.1"
3729 :group 'rst-faces
3730 :type '(face))
3731 (rst-testcover-defcustom)
3732 (make-obsolete-variable 'rst-emphasis1-face
3733 "customize the face `rst-emphasis1' instead."
3734 "24.1")
3736 (defface rst-emphasis2 '((t :inherit bold))
3737 "Face used for double emphasis."
3738 :version "24.1"
3739 :group 'rst-faces)
3741 (defcustom rst-emphasis2-face 'rst-emphasis2
3742 "Double emphasis."
3743 :group 'rst-faces
3744 :type '(face))
3745 (rst-testcover-defcustom)
3746 (make-obsolete-variable 'rst-emphasis2-face
3747 "customize the face `rst-emphasis2' instead."
3748 "24.1")
3750 (defface rst-literal '((t :inherit font-lock-string-face))
3751 "Face used for literal text."
3752 :version "24.1"
3753 :group 'rst-faces)
3755 (defcustom rst-literal-face 'rst-literal
3756 "Literal text."
3757 :version "24.1"
3758 :group 'rst-faces
3759 :type '(face))
3760 (rst-testcover-defcustom)
3761 (make-obsolete-variable 'rst-literal-face
3762 "customize the face `rst-literal' instead."
3763 "24.1")
3765 (defface rst-reference '((t :inherit font-lock-variable-name-face))
3766 "Face used for references to a definition."
3767 :version "24.1"
3768 :group 'rst-faces)
3770 (defcustom rst-reference-face 'rst-reference
3771 "References to a definition."
3772 :version "24.1"
3773 :group 'rst-faces
3774 :type '(face))
3775 (rst-testcover-defcustom)
3776 (make-obsolete-variable 'rst-reference-face
3777 "customize the face `rst-reference' instead."
3778 "24.1")
3780 (defface rst-transition '((t :inherit font-lock-keyword-face))
3781 "Face used for a transition."
3782 :package-version '(rst . "1.3.0")
3783 :group 'rst-faces)
3785 (defface rst-adornment '((t :inherit font-lock-keyword-face))
3786 "Face used for the adornment of a section header."
3787 :package-version '(rst . "1.3.0")
3788 :group 'rst-faces)
3790 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3792 (dolist (var '(rst-level-face-max rst-level-face-base-color
3793 rst-level-face-base-light
3794 rst-level-face-format-light
3795 rst-level-face-step-light
3796 rst-level-1-face
3797 rst-level-2-face
3798 rst-level-3-face
3799 rst-level-4-face
3800 rst-level-5-face
3801 rst-level-6-face))
3802 (make-obsolete-variable var "customize the faces `rst-level-*' instead."
3803 "24.3"))
3805 ;; Define faces for the first 6 levels. More levels are possible, however.
3806 (defface rst-level-1 '((((background light)) (:background "grey85"))
3807 (((background dark)) (:background "grey15")))
3808 "Default face for section title text at level 1."
3809 :package-version '(rst . "1.4.0"))
3811 (defface rst-level-2 '((((background light)) (:background "grey78"))
3812 (((background dark)) (:background "grey22")))
3813 "Default face for section title text at level 2."
3814 :package-version '(rst . "1.4.0"))
3816 (defface rst-level-3 '((((background light)) (:background "grey71"))
3817 (((background dark)) (:background "grey29")))
3818 "Default face for section title text at level 3."
3819 :package-version '(rst . "1.4.0"))
3821 (defface rst-level-4 '((((background light)) (:background "grey64"))
3822 (((background dark)) (:background "grey36")))
3823 "Default face for section title text at level 4."
3824 :package-version '(rst . "1.4.0"))
3826 (defface rst-level-5 '((((background light)) (:background "grey57"))
3827 (((background dark)) (:background "grey43")))
3828 "Default face for section title text at level 5."
3829 :package-version '(rst . "1.4.0"))
3831 (defface rst-level-6 '((((background light)) (:background "grey50"))
3832 (((background dark)) (:background "grey50")))
3833 "Default face for section title text at level 6."
3834 :package-version '(rst . "1.4.0"))
3836 (defcustom rst-adornment-faces-alist
3837 '((t . rst-transition)
3838 (nil . rst-adornment)
3839 (1 . rst-level-1)
3840 (2 . rst-level-2)
3841 (3 . rst-level-3)
3842 (4 . rst-level-4)
3843 (5 . rst-level-5)
3844 (6 . rst-level-6))
3845 "Faces for the various adornment types.
3846 Key is a number (for the section title text of that level
3847 starting with 1), t (for transitions) or nil (for section title
3848 adornment). If you need levels beyond 6 you have to define faces
3849 of your own."
3850 :group 'rst-faces
3851 :type '(alist
3852 :key-type
3853 (choice
3854 (integer :tag "Section level")
3855 (const :tag "transitions" t)
3856 (const :tag "section title adornment" nil))
3857 :value-type (face)))
3858 (rst-testcover-defcustom)
3860 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3862 (defvar rst-font-lock-keywords
3863 ;; The reST-links in the comments below all relate to sections in
3864 ;; http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html.
3865 `(;; FIXME: Block markup is not recognized in blocks after explicit markup
3866 ;; start.
3868 ;; Simple `Body Elements`_
3869 ;; `Bullet Lists`_
3870 ;; FIXME: A bullet directly after a field name is not recognized.
3871 (,(rst-re 'lin-beg '(:grp bul-sta))
3872 1 rst-block-face)
3873 ;; `Enumerated Lists`_
3874 (,(rst-re 'lin-beg '(:grp enmany-sta))
3875 1 rst-block-face)
3876 ;; `Definition Lists`_
3877 ;; FIXME: missing.
3878 ;; `Field Lists`_
3879 (,(rst-re 'lin-beg '(:grp fld-tag) 'bli-sfx)
3880 1 rst-external-face)
3881 ;; `Option Lists`_
3882 (,(rst-re 'lin-beg '(:grp opt-tag (:shy optsep-tag opt-tag) "*")
3883 '(:alt "$" (:seq hws-prt "\\{2\\}")))
3884 1 rst-block-face)
3885 ;; `Line Blocks`_
3886 ;; Only for lines containing no more bar - to distinguish from tables.
3887 (,(rst-re 'lin-beg '(:grp "|" bli-sfx) "[^|\n]*$")
3888 1 rst-block-face)
3890 ;; `Tables`_
3891 ;; FIXME: missing
3893 ;; All the `Explicit Markup Blocks`_
3894 ;; `Footnotes`_ / `Citations`_
3895 (,(rst-re 'lin-beg 'fnc-sta-2)
3896 (1 rst-definition-face)
3897 (2 rst-definition-face))
3898 ;; `Directives`_ / `Substitution Definitions`_
3899 (,(rst-re 'lin-beg 'dir-sta-3)
3900 (1 rst-directive-face)
3901 (2 rst-definition-face)
3902 (3 rst-directive-face))
3903 ;; `Hyperlink Targets`_
3904 (,(rst-re 'lin-beg
3905 '(:grp exm-sta "_" (:alt
3906 (:seq "`" ilcbkqdef-tag "`")
3907 (:seq (:alt "[^:\\\n]" "\\\\.") "+")) ":")
3908 'bli-sfx)
3909 1 rst-definition-face)
3910 (,(rst-re 'lin-beg '(:grp "__") 'bli-sfx)
3911 1 rst-definition-face)
3913 ;; All `Inline Markup`_
3914 ;; Most of them may be multiline though this is uninteresting.
3916 ;; FIXME: Condition 5 preventing fontification of e.g. "*" not implemented
3917 ;; `Strong Emphasis`_.
3918 (,(rst-re 'ilm-pfx '(:grp "\\*\\*" ilcast-tag "\\*\\*") 'ilm-sfx)
3919 1 rst-emphasis2-face)
3920 ;; `Emphasis`_
3921 (,(rst-re 'ilm-pfx '(:grp "\\*" ilcast-tag "\\*") 'ilm-sfx)
3922 1 rst-emphasis1-face)
3923 ;; `Inline Literals`_
3924 (,(rst-re 'ilm-pfx '(:grp "``" ilcbkq-tag "``") 'ilm-sfx)
3925 1 rst-literal-face)
3926 ;; `Inline Internal Targets`_
3927 (,(rst-re 'ilm-pfx '(:grp "_`" ilcbkq-tag "`") 'ilm-sfx)
3928 1 rst-definition-face)
3929 ;; `Hyperlink References`_
3930 ;; FIXME: `Embedded URIs and Aliases`_ not considered.
3931 ;; FIXME: Directly adjacent marked up words are not fontified correctly
3932 ;; unless they are not separated by two spaces: foo_ bar_.
3933 (,(rst-re 'ilm-pfx '(:grp (:alt (:seq "`" ilcbkq-tag "`")
3934 (:seq "\\sw" (:alt "\\sw" "-") "+\\sw"))
3935 "__?") 'ilm-sfx)
3936 1 rst-reference-face)
3937 ;; `Interpreted Text`_
3938 (,(rst-re 'ilm-pfx '(:grp (:shy ":" sym-tag ":") "?")
3939 '(:grp "`" ilcbkq-tag "`")
3940 '(:grp (:shy ":" sym-tag ":") "?") 'ilm-sfx)
3941 (1 rst-directive-face)
3942 (2 rst-external-face)
3943 (3 rst-directive-face))
3944 ;; `Footnote References`_ / `Citation References`_
3945 (,(rst-re 'ilm-pfx '(:grp fnc-tag "_") 'ilm-sfx)
3946 1 rst-reference-face)
3947 ;; `Substitution References`_
3948 ;; FIXME: References substitutions like |this|_ or |this|__ are not
3949 ;; fontified correctly.
3950 (,(rst-re 'ilm-pfx '(:grp sub-tag) 'ilm-sfx)
3951 1 rst-reference-face)
3952 ;; `Standalone Hyperlinks`_
3953 ;; FIXME: This takes it easy by using a whitespace as delimiter.
3954 (,(rst-re 'ilm-pfx '(:grp uri-tag ":\\S +") 'ilm-sfx)
3955 1 rst-definition-face)
3956 (,(rst-re 'ilm-pfx '(:grp sym-tag "@" sym-tag ) 'ilm-sfx)
3957 1 rst-definition-face)
3959 ;; Do all block fontification as late as possible so 'append works.
3961 ;; Sections_ / Transitions_
3962 ;; For sections this is multiline.
3963 (,(rst-re 'ado-beg-2-1)
3964 (rst-font-lock-handle-adornment-matcher
3965 (rst-font-lock-handle-adornment-pre-match-form
3966 (match-string-no-properties 1) (match-end 1))
3968 (1 (cdr (assoc nil rst-adornment-faces-alist)) append t)
3969 (2 (cdr (assoc rst-font-lock-adornment-level
3970 rst-adornment-faces-alist)) append t)
3971 (3 (cdr (assoc nil rst-adornment-faces-alist)) append t)))
3973 ;; FIXME: FACESPEC could be used instead of ordinary faces to set
3974 ;; properties on comments and literal blocks so they are *not*
3975 ;; inline fontified. See (elisp)Search-based Fontification.
3977 ;; FIXME: And / or use `syntax-propertize' functions as in `octave-mod.el'
3978 ;; and other V24 modes. May make `font-lock-extend-region'
3979 ;; superfluous.
3981 ;; `Comments`_
3982 ;; This is multiline.
3983 (,(rst-re 'lin-beg 'cmt-sta-1)
3984 (1 rst-comment-face)
3985 (rst-font-lock-find-unindented-line-match
3986 (rst-font-lock-find-unindented-line-limit (match-end 1))
3988 (0 rst-comment-face append)))
3989 (,(rst-re 'lin-beg '(:grp exm-tag) '(:grp hws-tag) "$")
3990 (1 rst-comment-face)
3991 (2 rst-comment-face)
3992 (rst-font-lock-find-unindented-line-match
3993 (rst-font-lock-find-unindented-line-limit 'next)
3995 (0 rst-comment-face append)))
3997 ;; FIXME: This is not rendered as comment::
3998 ;; .. .. list-table::
3999 ;; :stub-columns: 1
4000 ;; :header-rows: 1
4002 ;; FIXME: This is rendered wrong::
4004 ;; xxx yyy::
4006 ;; ----|> KKKKK <|----
4007 ;; / \
4008 ;; -|> AAAAAAAAAAPPPPPP <|- -|> AAAAAAAAAABBBBBBB <|-
4009 ;; | | | |
4010 ;; | | | |
4011 ;; PPPPPP PPPPPPDDDDDDD BBBBBBB PPPPPPBBBBBBB
4013 ;; Indentation needs to be taken from the line with the ``::`` and not from
4014 ;; the first content line.
4016 ;; `Indented Literal Blocks`_
4017 ;; This is multiline.
4018 (,(rst-re 'lin-beg 'lit-sta-2)
4019 (2 rst-block-face)
4020 (rst-font-lock-find-unindented-line-match
4021 (rst-font-lock-find-unindented-line-limit t)
4023 (0 rst-literal-face append)))
4025 ;; FIXME: `Quoted Literal Blocks`_ missing.
4026 ;; This is multiline.
4028 ;; `Doctest Blocks`_
4029 ;; FIXME: This is wrong according to the specification:
4031 ;; Doctest blocks are text blocks which begin with ">>> ", the Python
4032 ;; interactive interpreter main prompt, and end with a blank line.
4033 ;; Doctest blocks are treated as a special case of literal blocks,
4034 ;; without requiring the literal block syntax. If both are present, the
4035 ;; literal block syntax takes priority over Doctest block syntax:
4037 ;; This is an ordinary paragraph.
4039 ;; >>> print 'this is a Doctest block'
4040 ;; this is a Doctest block
4042 ;; The following is a literal block::
4044 ;; >>> This is not recognized as a doctest block by
4045 ;; reStructuredText. It *will* be recognized by the doctest
4046 ;; module, though!
4048 ;; Indentation is not required for doctest blocks.
4049 (,(rst-re 'lin-beg '(:grp (:alt ">>>" ell-tag)) '(:grp ".+"))
4050 (1 rst-block-face)
4051 (2 rst-literal-face)))
4052 "Keywords to highlight in rst mode.")
4054 (defvar font-lock-beg)
4055 (defvar font-lock-end)
4057 (defun rst-font-lock-extend-region ()
4058 "Extend the font-lock region if it might be in a multi-line construct.
4059 Return non-nil if so. Font-lock region is from `font-lock-beg'
4060 to `font-lock-end'."
4061 (let ((r (rst-font-lock-extend-region-internal font-lock-beg font-lock-end)))
4062 (when r
4063 (setq font-lock-beg (car r))
4064 (setq font-lock-end (cdr r))
4065 t)))
4067 (defun rst-font-lock-extend-region-internal (beg end)
4068 "Check the region BEG / END for being in the middle of a multi-line construct.
4069 Return nil if not or a cons with new values for BEG / END"
4070 (let ((nbeg (rst-font-lock-extend-region-extend beg -1))
4071 (nend (rst-font-lock-extend-region-extend end 1)))
4072 (if (or nbeg nend)
4073 (cons (or nbeg beg) (or nend end)))))
4075 ;; FIXME refactoring: Use `rst-forward-line-strict' instead.
4076 (defun rst-forward-line (&optional n)
4077 "Like `forward-line' but always end up in column 0 and return accordingly.
4078 Move N lines forward just as `forward-line'."
4079 (let ((left (forward-line n)))
4080 (if (bolp)
4081 left
4082 ;; FIXME: This may move back for positive n - is this desired?
4083 (forward-line 0)
4084 (- left (cl-signum n)))))
4086 ;; FIXME: If a single line is made a section header by `rst-adjust' the header
4087 ;; is not always fontified immediately.
4088 (defun rst-font-lock-extend-region-extend (pt dir)
4089 "Extend the region starting at point PT and extending in direction DIR.
4090 Return extended point or nil if not moved."
4091 ;; There are many potential multiline constructs but there are two groups
4092 ;; which are really relevant. The first group consists of
4094 ;; * comment lines without leading explicit markup tag and
4096 ;; * literal blocks following "::"
4098 ;; which are both indented. Thus indentation is the first thing recognized
4099 ;; here. The second criteria is an explicit markup tag which may be a comment
4100 ;; or a double colon at the end of a line.
4102 ;; The second group consists of the adornment cases.
4103 (if (not (get-text-property pt 'font-lock-multiline))
4104 ;; Move only if we don't start inside a multiline construct already.
4105 (save-match-data
4106 (save-excursion
4107 (let ( ; Non-empty non-indented line, explicit markup tag or literal
4108 ; block tag.
4109 (stop-re (rst-re '(:alt "[^ \t\n]"
4110 (:seq hws-tag exm-tag)
4111 (:seq ".*" dcl-tag lin-end)))))
4112 ;; The comments below are for dir == -1 / dir == 1.
4113 (goto-char pt)
4114 (rst-forward-line-strict 0)
4115 (setq pt (point))
4116 (while (and (not (looking-at stop-re))
4117 (zerop (rst-forward-line dir)))) ; try previous / next
4118 ; line if it exists.
4119 (if (looking-at (rst-re 'ado-beg-2-1)) ; may be an underline /
4120 ; overline.
4121 (if (zerop (rst-forward-line dir))
4122 (if (looking-at (rst-re 'ttl-beg-1)) ; title found, i.e.
4123 ; underline / overline
4124 ; found.
4125 (if (zerop (rst-forward-line dir))
4126 (if (not
4127 (looking-at (rst-re 'ado-beg-2-1))) ; no
4128 ; overline
4130 ; underline.
4131 (rst-forward-line (- dir)))) ; step back to
4132 ; title /
4133 ; adornment.
4134 (if (< dir 0) ; keep downward adornment.
4135 (rst-forward-line (- dir))))) ; step back to adornment.
4136 (if (looking-at (rst-re 'ttl-beg-1)) ; may be a title.
4137 (if (zerop (rst-forward-line dir))
4138 (if (not
4139 (looking-at (rst-re 'ado-beg-2-1))) ; no overline /
4140 ; underline.
4141 (rst-forward-line (- dir)))))) ; step back to line.
4142 (if (not (= (point) pt))
4143 (point)))))))
4145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4146 ;; Indented blocks
4148 (defun rst-forward-indented-block (&optional column limit)
4149 ;; testcover: ok.
4150 "Move forward across one indented block.
4151 Find the next non-empty line which is not indented at least to
4152 COLUMN (defaults to the column of the point). Moves point to
4153 first character of this line or the first of the empty lines
4154 immediately before it and returns that position. If there is no
4155 such line before LIMIT (defaults to the end of the buffer)
4156 returns nil and point is not moved."
4157 (let (fnd candidate)
4158 (setq fnd (rst-apply-indented-blocks
4159 (point) (or limit (point-max)) (or column (current-column))
4160 #'(lambda (_count _in-first _in-sub in-super in-empty _relind)
4161 (cond
4162 (in-empty
4163 (setq candidate (or candidate (line-beginning-position)))
4164 nil)
4165 (in-super
4166 (or candidate (line-beginning-position)))
4167 (t ; Non-empty, same or more indented line.
4168 (setq candidate nil)
4169 nil)))))
4170 (when fnd
4171 (goto-char fnd))))
4173 (defvar rst-font-lock-find-unindented-line-begin nil
4174 "Beginning of the match if `rst-font-lock-find-unindented-line-end'.")
4176 (defvar rst-font-lock-find-unindented-line-end nil
4177 "End of the match as determined by `rst-font-lock-find-unindented-line-limit'.
4178 Also used as a trigger for `rst-font-lock-find-unindented-line-match'.")
4180 (defun rst-font-lock-find-unindented-line-limit (ind-pnt)
4181 "Find the next unindented line relative to indentation at IND-PNT.
4182 Return this point, the end of the buffer or nil if nothing found.
4183 If IND-PNT is `next' take the indentation from the next line if
4184 this is not empty and indented more than the current one. If
4185 IND-PNT is non-nil but not a number take the indentation from the
4186 next non-empty line if this is indented more than the current one."
4187 (setq rst-font-lock-find-unindented-line-begin ind-pnt)
4188 (setq rst-font-lock-find-unindented-line-end
4189 (save-match-data
4190 (save-excursion
4191 (when (not (numberp ind-pnt))
4192 ;; Find indentation point in next line if any.
4193 (setq ind-pnt
4194 ;; FIXME: Should be refactored to two different functions
4195 ;; giving their result to this function, may be
4196 ;; integrated in caller.
4197 (save-match-data
4198 (let ((cur-ind (current-indentation)))
4199 (if (eq ind-pnt 'next)
4200 (when (and (rst-forward-line-strict 1 (point-max))
4201 (< (point) (point-max)))
4202 ;; Not at EOF.
4203 (setq rst-font-lock-find-unindented-line-begin
4204 (point))
4205 (when (and (not (looking-at (rst-re 'lin-end)))
4206 (> (current-indentation) cur-ind))
4207 ;; Use end of indentation if non-empty line.
4208 (looking-at (rst-re 'hws-tag))
4209 (match-end 0)))
4210 ;; Skip until non-empty line or EOF.
4211 (while (and (rst-forward-line-strict 1 (point-max))
4212 (< (point) (point-max))
4213 (looking-at (rst-re 'lin-end))))
4214 (when (< (point) (point-max))
4215 ;; Not at EOF.
4216 (setq rst-font-lock-find-unindented-line-begin
4217 (point))
4218 (when (> (current-indentation) cur-ind)
4219 ;; Indentation bigger than line of departure.
4220 (looking-at (rst-re 'hws-tag))
4221 (match-end 0))))))))
4222 (when ind-pnt
4223 (goto-char ind-pnt)
4224 (or (rst-forward-indented-block nil (point-max))
4225 (point-max)))))))
4227 (defun rst-font-lock-find-unindented-line-match (_limit)
4228 "Set the match found earlier if match were found.
4229 Match has been found by `rst-font-lock-find-unindented-line-limit'
4230 the first time called or no match is found. Return non-nil if
4231 match was found. _LIMIT is not used but mandated by the caller."
4232 (when rst-font-lock-find-unindented-line-end
4233 (set-match-data
4234 (list rst-font-lock-find-unindented-line-begin
4235 rst-font-lock-find-unindented-line-end))
4236 (put-text-property rst-font-lock-find-unindented-line-begin
4237 rst-font-lock-find-unindented-line-end
4238 'font-lock-multiline t)
4239 ;; Make sure this is called only once.
4240 (setq rst-font-lock-find-unindented-line-end nil)
4243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4244 ;; Adornments
4246 (defvar rst-font-lock-adornment-level nil
4247 "Storage for `rst-font-lock-handle-adornment-matcher'.
4248 Either section level of the current adornment or t for a transition.")
4250 (defun rst-adornment-level (ado)
4251 "Return section level for ADO or t for a transition.
4252 If ADO is found in the hierarchy return its level. Otherwise
4253 return a level one beyond the existing hierarchy."
4254 (if (rst-Ado-is-transition ado)
4256 (let ((hier (rst-Hdr-ado-map (rst-hdr-hierarchy))))
4257 (1+ (or (rst-Ado-position ado hier)
4258 (length hier))))))
4260 (defvar rst-font-lock-adornment-match nil
4261 "Storage for match for current adornment.
4262 Set by `rst-font-lock-handle-adornment-pre-match-form'. Also used
4263 as a trigger for `rst-font-lock-handle-adornment-matcher'.")
4265 (defun rst-font-lock-handle-adornment-pre-match-form (ado ado-end)
4266 "Determine limit for adornments.
4267 Determine all things necessary for font-locking section titles
4268 and transitions and put the result to `rst-font-lock-adornment-match'
4269 and `rst-font-lock-adornment-level'. ADO is the complete adornment
4270 matched. ADO-END is the point where ADO ends. Return the point
4271 where the whole adorned construct ends.
4273 Called as a PRE-MATCH-FORM in the sense of `font-lock-keywords'."
4274 (let ((ttl (rst-classify-adornment ado ado-end)))
4275 (if (not ttl)
4276 (setq rst-font-lock-adornment-level nil
4277 rst-font-lock-adornment-match nil)
4278 (setq rst-font-lock-adornment-level
4279 (rst-adornment-level (rst-Ttl-ado ttl)))
4280 (setq rst-font-lock-adornment-match (rst-Ttl-match ttl))
4281 (goto-char (rst-Ttl-get-beginning ttl))
4282 (rst-Ttl-get-end ttl))))
4284 (defun rst-font-lock-handle-adornment-matcher (_limit)
4285 "Set the match found earlier if match were found.
4286 Match has been found by
4287 `rst-font-lock-handle-adornment-pre-match-form' the first time
4288 called or no match is found. Return non-nil if match was found.
4290 Called as a MATCHER in the sense of `font-lock-keywords'.
4291 _LIMIT is not used but mandated by the caller."
4292 (let ((match rst-font-lock-adornment-match))
4293 ;; May run only once - enforce this.
4294 (setq rst-font-lock-adornment-match nil)
4295 (when match
4296 (set-match-data match)
4297 (goto-char (match-end 0))
4298 (put-text-property (match-beginning 0) (match-end 0)
4299 'font-lock-multiline t)
4300 t)))
4303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4304 ;; Compilation
4306 (defgroup rst-compile nil
4307 "Settings for support of conversion of reStructuredText
4308 document with \\[rst-compile]."
4309 :group 'rst
4310 :version "21.1")
4312 (defcustom rst-compile-toolsets
4313 `((html ,(if (executable-find "rst2html.py") "rst2html.py" "rst2html")
4314 ".html" nil)
4315 (latex ,(if (executable-find "rst2latex.py") "rst2latex.py" "rst2latex")
4316 ".tex" nil)
4317 (newlatex ,(if (executable-find "rst2newlatex.py") "rst2newlatex.py"
4318 "rst2newlatex")
4319 ".tex" nil)
4320 (pseudoxml ,(if (executable-find "rst2pseudoxml.py") "rst2pseudoxml.py"
4321 "rst2pseudoxml")
4322 ".xml" nil)
4323 (xml ,(if (executable-find "rst2xml.py") "rst2xml.py" "rst2xml")
4324 ".xml" nil)
4325 (pdf ,(if (executable-find "rst2pdf.py") "rst2pdf.py" "rst2pdf")
4326 ".pdf" nil)
4327 (s5 ,(if (executable-find "rst2s5.py") "rst2s5.py" "rst2s5")
4328 ".html" nil))
4329 ;; FIXME: Add at least those converters officially supported like `rst2odt'
4330 ;; and `rst2man'.
4331 ;; FIXME: To make this really useful there should be a generic command the
4332 ;; user gives one of the symbols and this way select the conversion to
4333 ;; run. This should replace the toolset stuff somehow.
4334 ;; FIXME: Allow a template for the conversion command so `rst2pdf ... -o ...'
4335 ;; can be supported.
4336 "Table describing the command to use for each tool-set.
4337 An association list of the tool-set to a list of the (command to use,
4338 extension of produced filename, options to the tool (nil or a
4339 string)) to be used for converting the document."
4340 ;; FIXME: These are not options but symbols which may be referenced by
4341 ;; `rst-compile-*-toolset` below. The `:validate' keyword of
4342 ;; `defcustom' may help to define this properly in newer Emacs
4343 ;; versions (> 23.1).
4344 :type '(alist :options (html latex newlatex pseudoxml xml pdf s5)
4345 :key-type symbol
4346 :value-type (list :tag "Specification"
4347 (file :tag "Command")
4348 (string :tag "File extension")
4349 (choice :tag "Command options"
4350 (const :tag "No options" nil)
4351 (string :tag "Options"))))
4352 :group 'rst-compile
4353 :package-version "1.2.0")
4354 (rst-testcover-defcustom)
4356 ;; FIXME: Must be defcustom.
4357 (defvar rst-compile-primary-toolset 'html
4358 "The default tool-set for `rst-compile'.")
4360 ;; FIXME: Must be defcustom.
4361 (defvar rst-compile-secondary-toolset 'latex
4362 "The default tool-set for `rst-compile' with a prefix argument.")
4364 (defun rst-compile-find-conf ()
4365 "Look for the configuration file in the parents of the current path."
4366 (interactive)
4367 (let ((file-name "docutils.conf")
4368 (buffer-file (buffer-file-name)))
4369 ;; Move up in the dir hierarchy till we find a change log file.
4370 (let* ((dir (file-name-directory buffer-file))
4371 (prevdir nil))
4372 (while (and (or (not (string= dir prevdir))
4373 (setq dir nil)
4374 nil)
4375 (not (file-exists-p (concat dir file-name))))
4376 ;; Move up to the parent dir and try again.
4377 (setq prevdir dir)
4378 (setq dir (expand-file-name (file-name-directory
4379 (directory-file-name
4380 (file-name-directory dir))))))
4381 (or (and dir (concat dir file-name)) nil))))
4383 (require 'compile)
4385 (defun rst-compile (&optional use-alt)
4386 "Compile command to convert reST document into some output file.
4387 Attempts to find configuration file, if it can, overrides the
4388 options. There are two commands to choose from; with USE-ALT,
4389 select the alternative tool-set."
4390 (interactive "P")
4391 ;; Note: maybe we want to check if there is a Makefile too and not do anything
4392 ;; if that is the case. I dunno.
4393 (cl-destructuring-bind
4394 (command extension options
4395 &aux (conffile (rst-compile-find-conf))
4396 (bufname (file-name-nondirectory buffer-file-name)))
4397 (cdr (assq (if use-alt
4398 rst-compile-secondary-toolset
4399 rst-compile-primary-toolset)
4400 rst-compile-toolsets))
4401 ;; Set compile-command before invocation of compile.
4402 (setq-local
4403 compile-command
4404 (mapconcat
4405 #'identity
4406 (list command
4407 (or options "")
4408 (if conffile
4409 (concat "--config=" (shell-quote-argument conffile))
4411 (shell-quote-argument bufname)
4412 (shell-quote-argument (concat (file-name-sans-extension bufname)
4413 extension)))
4414 " "))
4415 ;; Invoke the compile command.
4416 (if (or compilation-read-command use-alt)
4417 (call-interactively #'compile)
4418 (compile compile-command))))
4420 (defun rst-compile-alt-toolset ()
4421 "Compile command with the alternative tool-set."
4422 (interactive)
4423 (rst-compile t))
4425 (defun rst-compile-pseudo-region ()
4426 "Show pseudo-XML rendering.
4427 Rendering is done of the current active region, or of the entire
4428 buffer, if the region is not selected."
4429 ;; FIXME: The region should be given interactively.
4430 (interactive)
4431 (with-output-to-temp-buffer "*pseudoxml*"
4432 (shell-command-on-region
4433 (if mark-active (region-beginning) (point-min))
4434 (if mark-active (region-end) (point-max))
4435 (cadr (assq 'pseudoxml rst-compile-toolsets))
4436 standard-output)))
4438 ;; FIXME: Should be integrated in `rst-compile-toolsets'.
4439 (defvar rst-pdf-program "xpdf"
4440 "Program used to preview PDF files.")
4442 (defun rst-compile-pdf-preview ()
4443 "Convert the document to a PDF file and launch a preview program."
4444 (interactive)
4445 (let* ((tmp-filename (make-temp-file "rst_el" nil ".pdf"))
4446 (command (format "%s %s %s && %s %s ; rm %s"
4447 (cadr (assq 'pdf rst-compile-toolsets))
4448 buffer-file-name tmp-filename
4449 rst-pdf-program tmp-filename tmp-filename)))
4450 (start-process-shell-command "rst-pdf-preview" nil command)
4451 ;; Note: you could also use (compile command) to view the compilation
4452 ;; output.
4455 ;; FIXME: Should be integrated in `rst-compile-toolsets' defaulting to
4456 ;; something like `browse-url'.
4457 (defvar rst-slides-program "firefox"
4458 "Program used to preview S5 slides.")
4460 (defun rst-compile-slides-preview ()
4461 "Convert the document to an S5 slide presentation and launch a preview program."
4462 (interactive)
4463 (let* ((tmp-filename (make-temp-file "rst_el" nil ".html"))
4464 (command (format "%s %s %s && %s %s ; rm %s"
4465 (cadr (assq 's5 rst-compile-toolsets))
4466 buffer-file-name tmp-filename
4467 rst-slides-program tmp-filename tmp-filename)))
4468 (start-process-shell-command "rst-slides-preview" nil command)
4469 ;; Note: you could also use (compile command) to view the compilation
4470 ;; output.
4473 ;; FIXME: Add `rst-compile-html-preview'.
4475 ;; FIXME: Add support for `restview` (http://mg.pov.lt/restview/). May be a
4476 ;; more general facility for calling commands on a reST file would make
4477 ;; sense.
4480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4481 ;; Imenu support
4483 ;; FIXME: Consider a key binding. A key binding needs to definitely switch on
4484 ;; `which-func-mode' - i.e. `which-func-modes' must be set properly.
4486 ;; Based on ideas from Masatake YAMATO <yamato@redhat.com>.
4488 (defun rst-imenu-convert-cell (stn)
4489 "Convert a STN to an Imenu index node and return it."
4490 (let ((ttl (rst-Stn-ttl stn))
4491 (children (rst-Stn-children stn))
4492 (pos (rst-Stn-get-title-beginning stn))
4493 (txt (rst-Stn-get-text stn ""))
4494 (pfx " ")
4495 (sfx "")
4496 name)
4497 (when ttl
4498 (let ((hdr (rst-Ttl-hdr ttl)))
4499 (setq pfx (char-to-string (rst-Hdr-get-char hdr)))
4500 (when (rst-Hdr-is-over-and-under hdr)
4501 (setq sfx pfx))))
4502 ;; FIXME: Overline adornment characters need to be in front so they
4503 ;; become visible even for long title lines. May be an additional
4504 ;; level number is also useful.
4505 (setq name (format "%s%s%s" pfx txt sfx))
4506 (cons name ; The name of the entry.
4507 (if children
4508 (cons ; The entry has a submenu.
4509 (cons name pos) ; The entry itself.
4510 (mapcar #'rst-imenu-convert-cell children)) ; The children.
4511 pos)))) ; The position of a plain entry.
4513 ;; FIXME: Document title and subtitle need to be handled properly. They should
4514 ;; get an own "Document" top level entry.
4515 (defun rst-imenu-create-index ()
4516 "Create index for Imenu.
4517 Return as described for `imenu--index-alist'."
4518 (rst-reset-section-caches)
4519 (let ((root (rst-all-stn)))
4520 (when root
4521 (mapcar #'rst-imenu-convert-cell (rst-Stn-children root)))))
4524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4525 ;; Convenience functions
4527 ;; FIXME: Unbound command - should be bound or removed.
4528 (defun rst-replace-lines (fromchar tochar)
4529 "Replace flush-left lines of FROMCHAR with equal-length lines of TOCHAR."
4530 (interactive "\
4531 cSearch for flush-left lines of char:
4532 cand replace with char: ")
4533 (save-excursion
4534 (let ((searchre (rst-re "^" fromchar "+\\( *\\)$"))
4535 (found 0))
4536 (while (search-forward-regexp searchre nil t)
4537 (setq found (1+ found))
4538 (goto-char (match-beginning 1))
4539 (let ((width (current-column)))
4540 (rst-delete-entire-line 0)
4541 (insert-char tochar width)))
4542 (message "%d lines replaced." found))))
4544 ;; FIXME: Unbound command - should be bound or removed.
4545 (defun rst-join-paragraph ()
4546 "Join lines in current paragraph into one line, removing end-of-lines."
4547 (interactive)
4548 (let ((fill-column 65000)) ; Some big number.
4549 (call-interactively #'fill-paragraph)))
4551 ;; FIXME: Unbound command - should be bound or removed.
4552 (defun rst-force-fill-paragraph ()
4553 "Fill paragraph at point, first joining the paragraph's lines into one.
4554 This is useful for filling list item paragraphs."
4555 (interactive)
4556 (rst-join-paragraph)
4557 (fill-paragraph nil))
4560 ;; FIXME: Unbound command - should be bound or removed.
4561 ;; Generic character repeater function.
4562 ;; For sections, better to use the specialized function above, but this can
4563 ;; be useful for creating separators.
4564 (defun rst-repeat-last-character (use-next)
4565 "Fill the current line using the last character on the current line.
4566 Fill up to the length of the preceding line or up to `fill-column' if preceding
4567 line is empty.
4569 If USE-NEXT, use the next line rather than the preceding line.
4571 If the current line is longer than the desired length, shave the characters off
4572 the current line to fit the desired length.
4574 As an added convenience, if the command is repeated immediately, the alternative
4575 column is used (fill-column vs. end of previous/next line)."
4576 (interactive "P")
4577 (let* ((curcol (current-column))
4578 (curline (+ (count-lines (point-min) (point))
4579 (if (zerop curcol) 1 0)))
4580 (lbp (line-beginning-position 0))
4581 (prevcol (if (and (= curline 1) (not use-next))
4582 fill-column
4583 (save-excursion
4584 (forward-line (if use-next 1 -1))
4585 (end-of-line)
4586 (skip-chars-backward " \t" lbp)
4587 (let ((cc (current-column)))
4588 (if (zerop cc) fill-column cc)))))
4589 (rightmost-column
4590 (cond ((equal last-command 'rst-repeat-last-character)
4591 (if (= curcol fill-column) prevcol fill-column))
4592 (t (save-excursion
4593 (if (zerop prevcol) fill-column prevcol))))))
4594 (end-of-line)
4595 (if (> (current-column) rightmost-column)
4596 ;; Shave characters off the end.
4597 (delete-region (- (point)
4598 (- (current-column) rightmost-column))
4599 (point))
4600 ;; Fill with last characters.
4601 (insert-char (preceding-char)
4602 (- rightmost-column (current-column))))))
4606 ;; LocalWords: docutils http sourceforge rst html wp svn svnroot txt reST regex
4607 ;; LocalWords: regexes alist seq alt grp keymap abbrev overline overlines toc
4608 ;; LocalWords: XML PNT propertized init referenceable
4610 (provide 'rst)
4612 ;; Local Variables:
4613 ;; sentence-end-double-space: t
4614 ;; End:
4616 ;;; rst.el ends here