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/>.
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
36 ;; - A mode that displays the table of contents and allows you to jump anywhere
38 ;; - Functions to insert and automatically update a TOC in your source
40 ;; - Function to insert list, processing item bullets and enumerations
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 ...))
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
79 ;; Add the following lines to your init file:
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))
101 ;; FIXME: Check through major mode conventions again.
103 ;; FIXME: Embed complicated `defconst's in `eval-when-compile'.
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 destructuring
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
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
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
148 (if (not (plist-member (symbol-plist sym
) 'standard-value
))
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
177 (&define
([&or symbolp cl-macro-list
] def-form
&optional def-form
)
178 cl-declarations def-body
))
180 (let ((var (make-symbol "--rst-destructuring-dolist-var--")))
181 `(cl-dolist (,var
,list
,result
)
182 (cl-destructuring-bind ,arglist
,var
185 (defun rst-forward-line-strict (n &optional limit
)
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
194 (let ((start (point)))
195 (if (and (zerop (forward-line n
))
198 (>= (point) limit
))))
203 (defun rst-forward-line-looking-at (n rst-re-args
&optional fun
)
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
)))
217 (setq fun
#'identity
))
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 (defun rst-extract-version (delim-re head-re re tail-re var
&optional default
)
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."
239 (concat delim-re head-re
"\\(" re
"\\)" tail-re delim-re
)
244 ;; Use CVSHeader to really get information from CVS and not other version
246 (defconst rst-cvs-header
247 "$CVSHeader: sm/rst_el/rst.el,v 1.1058.2.9 2017/01/08 09:54:50 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: 8015 $")
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: 2017-01-08 10:54:35 +0100 (Sun, 08 Jan 2017) $")
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.2 %")
273 "Official version of the package.")
274 (defconst rst-official-cvs-rev
275 (rst-extract-version "[%$]" "Revision: " "[0-9]+\\(?:\\.[0-9]+\\)+" " "
276 "$Revision: 1.1058.2.9 $")
277 "CVS revision of this file in the official version.")
279 (defconst rst-version
280 (if (equal rst-official-cvs-rev rst-cvs-rev
)
282 (format "%s (development %s [%s])" rst-official-version
283 rst-cvs-rev rst-cvs-timestamp
))
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
301 ;; Whatever the Emacs version is this rst.el version ends up in.
304 (unless (assoc rst-official-version rst-package-emacs-version-alist
)
305 (error "Version %s not listed in `rst-package-emacs-version-alist'"
308 (add-to-list 'customize-package-emacs-version-alist
309 (cons 'ReST rst-package-emacs-version-alist
))
312 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 ;; Initialize customization
315 (defgroup rst nil
"Support for reStructuredText documents."
318 :link
'(url-link "http://docutils.sourceforge.net/rst.html"))
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ;; Facilities for regular expressions used everywhere
324 ;; The trailing numbers in the names give the number of referenceable regex
325 ;; groups contained in the regex.
327 ;; Used to be customizable but really is not customizable but fixed by the reST
329 (defconst rst-bullets
330 ;; Sorted so they can form a character class when concatenated.
332 "List of all possible bullet characters for bulleted lists.")
334 (defconst rst-uri-schemes
335 '("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https" "imap"
336 "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero" "rtsp"
337 "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais")
338 "Supported URI schemes.")
340 (defconst rst-adornment-chars
341 ;; Sorted so they can form a character class when concatenated.
343 ?
! ?
\" ?
# ?$ ?% ?
& ?
' ?\
( ?\
) ?
* ?
+ ?
, ?. ?
/ ?
: ?\
; ?< ?= ?> ?? ?@ ?\[ ?\\
346 "Characters which may be used in adornments for sections and transitions.")
348 (defconst rst-max-inline-length
350 "Maximum length of inline markup to recognize.")
352 (defconst rst-re-alist-def
353 ;; `*-beg' matches * at the beginning of a line.
354 ;; `*-end' matches * at the end of a line.
355 ;; `*-prt' matches a part of *.
356 ;; `*-tag' matches *.
357 ;; `*-sta' matches the start of * which may be followed by respective content.
358 ;; `*-pfx' matches the delimiter left of *.
359 ;; `*-sfx' matches the delimiter right of *.
360 ;; `*-hlp' helper for *.
362 ;; A trailing number says how many referenceable groups are contained.
365 ;; Horizontal white space (`hws')
367 (hws-tag hws-prt
"*") ; Optional sequence of horizontal white space.
368 (hws-sta hws-prt
"+") ; Mandatory sequence of horizontal white space.
371 (lin-beg "^" hws-tag
) ; Beginning of a possibly indented line.
372 (lin-end hws-tag
"$") ; End of a line with optional trailing white space.
373 (linemp-tag "^" hws-tag
"$") ; Empty line with optional white space.
375 ;; Various tags and parts
376 (ell-tag "\\.\\.\\.") ; Ellipsis
377 (bul-tag ,(concat "[" rst-bullets
"]")) ; A bullet.
378 (ltr-tag "[a-zA-Z]") ; A letter enumerator tag.
379 (num-prt "[0-9]") ; A number enumerator part.
380 (num-tag num-prt
"+") ; A number enumerator tag.
381 (rom-prt "[IVXLCDMivxlcdm]") ; A roman enumerator part.
382 (rom-tag rom-prt
"+") ; A roman enumerator tag.
383 (aut-tag "#") ; An automatic enumerator tag.
384 (dcl-tag "::") ; Double colon.
386 ;; Block lead in (`bli')
387 (bli-sfx (:alt hws-sta
"$")) ; Suffix of a block lead-in with *optional*
391 (bul-sta bul-tag bli-sfx
) ; Start of a bulleted item.
392 (bul-beg lin-beg bul-sta
) ; A bullet item at the beginning of a line.
394 ;; Explicit markup tag (`exm')
396 (exm-sta exm-tag hws-sta
)
397 (exm-beg lin-beg exm-sta
)
399 ;; Counters in enumerations (`cnt')
400 (cntany-tag (:alt ltr-tag num-tag rom-tag aut-tag
)) ; An arbitrary counter.
401 (cntexp-tag (:alt ltr-tag num-tag rom-tag
)) ; An arbitrary explicit counter.
403 ;; Enumerator (`enm')
405 (:seq cntany-tag
"\\.")
406 (:seq
"(?" cntany-tag
")"))) ; An arbitrary enumerator.
408 (:seq cntexp-tag
"\\.")
409 (:seq
"(?" cntexp-tag
")"))) ; An arbitrary explicit
413 (:seq
"(?" aut-tag
")"))) ; An automatic enumerator.
414 (enmany-sta enmany-tag bli-sfx
) ; An arbitrary enumerator start.
415 (enmexp-sta enmexp-tag bli-sfx
) ; An arbitrary explicit enumerator start.
416 (enmexp-beg lin-beg enmexp-sta
) ; An arbitrary explicit enumerator start
417 ; at the beginning of a line.
419 ;; Items may be enumerated or bulleted (`itm')
420 (itmany-tag (:alt enmany-tag bul-tag
)) ; An arbitrary item tag.
421 (itmany-sta-1 (:grp itmany-tag
) bli-sfx
) ; An arbitrary item start, group
423 (itmany-beg-1 lin-beg itmany-sta-1
) ; An arbitrary item start at the
424 ; beginning of a line, group is the
427 ;; Inline markup (`ilm')
428 (ilm-pfx (:alt
"^" hws-prt
"[-'\"([{<‘“«’/:]"))
429 (ilm-sfx (:alt
"$" hws-prt
"[]-'\")}>’”»/:.,;!?\\]"))
431 ;; Inline markup content (`ilc')
432 (ilcsgl-tag "\\S ") ; A single non-white character.
433 (ilcast-prt (:alt
"[^*\\]" "\\\\.")) ; Part of non-asterisk content.
434 (ilcbkq-prt (:alt
"[^`\\]" "\\\\.")) ; Part of non-backquote content.
435 (ilcbkqdef-prt (:alt
"[^`\\\n]" "\\\\.")) ; Part of non-backquote
437 (ilcbar-prt (:alt
"[^|\\]" "\\\\.")) ; Part of non-vertical-bar content.
438 (ilcbardef-prt (:alt
"[^|\\\n]" "\\\\.")) ; Part of non-vertical-bar
440 (ilcast-sfx "[^\t *\\]") ; Suffix of non-asterisk content.
441 (ilcbkq-sfx "[^\t `\\]") ; Suffix of non-backquote content.
442 (ilcbar-sfx "[^\t |\\]") ; Suffix of non-vertical-bar content.
443 (ilcrep-hlp ,(format "\\{0,%d\\}" rst-max-inline-length
)) ; Repeat count.
444 (ilcast-tag (:alt ilcsgl-tag
446 ilcast-prt ilcrep-hlp
447 ilcast-sfx
))) ; Non-asterisk content.
448 (ilcbkq-tag (:alt ilcsgl-tag
450 ilcbkq-prt ilcrep-hlp
451 ilcbkq-sfx
))) ; Non-backquote content.
452 (ilcbkqdef-tag (:alt ilcsgl-tag
454 ilcbkqdef-prt ilcrep-hlp
455 ilcbkq-sfx
))) ; Non-backquote definition.
456 (ilcbar-tag (:alt ilcsgl-tag
458 ilcbar-prt ilcrep-hlp
459 ilcbar-sfx
))) ; Non-vertical-bar content.
460 (ilcbardef-tag (:alt ilcsgl-tag
462 ilcbardef-prt ilcrep-hlp
463 ilcbar-sfx
))) ; Non-vertical-bar definition.
466 (fldnam-prt (:alt
"[^:\n]" "\\\\:")) ; Part of a field name.
467 (fldnam-tag fldnam-prt
"+") ; A field name.
468 (fld-tag ":" fldnam-tag
":") ; A field marker.
471 (optsta-tag (:alt
"[-+/]" "--")) ; Start of an option.
472 (optnam-tag "\\sw" (:alt
"-" "\\sw") "*") ; Name of an option.
473 (optarg-tag (:shy
"[ =]\\S +")) ; Option argument.
474 (optsep-tag (:shy
"," hws-prt
)) ; Separator between options.
475 (opt-tag (:shy optsta-tag optnam-tag optarg-tag
"?")) ; A complete option.
477 ;; Footnotes and citations (`fnc')
478 (fncnam-prt "[^]\n]") ; Part of a footnote or citation name.
479 (fncnam-tag fncnam-prt
"+") ; A footnote or citation name.
480 (fnc-tag "\\[" fncnam-tag
"]") ; A complete footnote or citation tag.
481 (fncdef-tag-2 (:grp exm-sta
)
482 (:grp fnc-tag
)) ; A complete footnote or citation definition
483 ; tag. First group is the explicit markup
484 ; start, second group is the footnote /
486 (fnc-sta-2 fncdef-tag-2 bli-sfx
) ; Start of a footnote or citation
487 ; definition. First group is the explicit
488 ; markup start, second group is the
489 ; footnote / citation tag.
491 ;; Substitutions (`sub')
492 (sub-tag "|" ilcbar-tag
"|") ; A complete substitution tag.
493 (subdef-tag "|" ilcbardef-tag
"|") ; A complete substitution definition
497 (sym-prt "[-+.:_]") ; Non-word part of a symbol.
498 (sym-tag (:shy
"\\sw+" (:shy sym-prt
"\\sw+") "*"))
501 (uri-tag (:alt
,@rst-uri-schemes
))
504 (ado-prt "[" ,(concat rst-adornment-chars
) "]")
505 (adorep3-hlp "\\{3,\\}") ; There must be at least 3 characters because
506 ; otherwise explicit markup start would be
508 (adorep2-hlp "\\{2,\\}") ; As `adorep3-hlp' but when the first of three
509 ; characters is matched differently.
510 (ado-tag-1-1 (:grp ado-prt
)
511 "\\1" adorep2-hlp
) ; A complete adornment, group is the first
512 ; adornment character and MUST be the FIRST
513 ; group in the whole expression.
514 (ado-tag-1-2 (:grp ado-prt
)
515 "\\2" adorep2-hlp
) ; A complete adornment, group is the first
516 ; adornment character and MUST be the
517 ; SECOND group in the whole expression.
518 (ado-beg-2-1 "^" (:grp ado-tag-1-2
)
519 lin-end
) ; A complete adornment line; first group is the whole
520 ; adornment and MUST be the FIRST group in the whole
521 ; expression; second group is the first adornment
525 (ttl-tag "\\S *\\w.*\\S ") ; A title text.
526 (ttl-beg-1 lin-beg
(:grp ttl-tag
)) ; A title text at the beginning of a
527 ; line. First group is the complete,
528 ; trimmed title text.
530 ;; Directives and substitution definitions (`dir')
531 (dir-tag-3 (:grp exm-sta
)
532 (:grp
(:shy subdef-tag hws-sta
) "?")
533 (:grp sym-tag dcl-tag
)) ; A directive or substitution definition
534 ; tag. First group is explicit markup
535 ; start, second group is a possibly
536 ; empty substitution tag, third group is
537 ; the directive tag including the double
539 (dir-sta-3 dir-tag-3 bli-sfx
) ; Start of a directive or substitution
540 ; definition. Groups are as in dir-tag-3.
542 ;; Literal block (`lit')
543 (lit-sta-2 (:grp
(:alt
"[^.\n]" "\\.[^.\n]") ".*") "?"
544 (:grp dcl-tag
) "$") ; Start of a literal block. First group is
545 ; any text before the double colon tag which
546 ; may not exist, second group is the double
550 (cmt-sta-1 (:grp exm-sta
) "[^[|_\n]"
551 (:alt
"[^:\n]" (:seq
":" (:alt
"[^:\n]" "$")))
552 "*$") ; Start of a comment block; first group is explicit markup
555 ;; Paragraphs (`par')
556 (par-tag- (:alt itmany-tag fld-tag opt-tag fncdef-tag-2 dir-tag-3 exm-tag
)
557 ) ; Tag at the beginning of a paragraph; there may be groups in
560 "Definition alist of relevant regexes.
561 Each entry consists of the symbol naming the regex and an
562 argument list for `rst-re'.")
564 (defvar rst-re-alist
) ; Forward declare to use it in `rst-re'.
566 ;; FIXME: Use `sregex' or `rx' instead of re-inventing the wheel.
567 (rst-testcover-add-compose 'rst-re
)
568 (defun rst-re (&rest args
)
570 "Interpret ARGS as regular expressions and return a regex string.
571 Each element of ARGS may be one of the following:
573 A string which is inserted unchanged.
575 A character which is resolved to a quoted regex.
577 A symbol which is resolved to a string using `rst-re-alist-def'.
579 A list with a keyword in the car. Each element of the cdr of such
580 a list is recursively interpreted as ARGS. The results of this
581 interpretation are concatenated according to the keyword.
583 For the keyword `:seq' the results are simply concatenated.
585 For the keyword `:shy' the results are concatenated and
586 surrounded by a shy-group (\"\\(?:...\\)\").
588 For the keyword `:alt' the results form an alternative (\"\\|\")
589 which is shy-grouped (\"\\(?:...\\)\").
591 For the keyword `:grp' the results are concatenated and form a
592 referenceable group (\"\\(...\\)\").
594 After interpretation of ARGS the results are concatenated as for
603 (cadr (assoc re rst-re-alist
)))
605 (regexp-quote (char-to-string re
)))
608 (mapcar (lambda (elt)
613 (mapconcat #'identity nested
""))
615 (concat "\\(?:" (mapconcat #'identity nested
"") "\\)"))
617 (concat "\\(" (mapconcat #'identity nested
"") "\\)"))
619 (concat "\\(?:" (mapconcat #'identity nested
"\\|") "\\)"))
621 (error "Unknown list car: %s" (car re
))))))
623 (error "Unknown object type for building regex: %s" re
))))
626 ;; FIXME: Remove circular dependency between `rst-re' and `rst-re-alist'.
627 (with-no-warnings ; Silence byte-compiler about this construction.
628 (defconst rst-re-alist
629 ;; Shadow global value we are just defining so we can construct it step by
632 (dolist (re rst-re-alist-def rst-re-alist
)
635 (list (list (car re
) (apply #'rst-re
(cdr re
))))))))
636 "Alist mapping symbols from `rst-re-alist-def' to regex strings."))
639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
642 ;; Each of the following classes represents an own concept. The suffix of the
643 ;; class name is used in the code to represent entities of the respective
646 ;; In addition a reStructuredText section header in the buffer is called
649 ;; For lists a "s" is added to the name of the concepts.
652 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
657 (:constructor nil
) ; Prevent creating unchecked values.
658 ;; Construct a transition.
660 rst-Ado-new-transition
663 (-style 'transition
)))
664 ;; Construct a simple section header.
669 (char (rst-Ado--validate-char char-arg
))
671 ;; Construct a over-and-under section header.
673 rst-Ado-new-over-and-under
676 (char (rst-Ado--validate-char char-arg
))
677 (-style 'over-and-under
)))
678 ;; Construct from adornment with inverted style.
683 (char (rst-Ado-char ado-arg
))
684 (-style (let ((sty (rst-Ado--style ado-arg
)))
688 ((eq sty
'over-and-under
)
691 "Representation of a reStructuredText adornment.
692 Adornments are either section markers where they markup the
693 section header or transitions.
695 This type is immutable."
696 ;; The character used for the adornment.
697 (char nil
:read-only t
)
698 ;; The style of the adornment. This is a private attribute.
699 (-style nil
:read-only t
))
701 ;; Private class methods
703 (defun rst-Ado--validate-char (char)
705 "Validate CHAR to be a valid adornment character.
706 Return CHAR if so or signal an error otherwise."
707 (cl-check-type char character
)
708 (cl-check-type char
(satisfies
710 (memq c rst-adornment-chars
)))
711 "Character must be a valid adornment character")
716 (defun rst-Ado-is-transition (self)
718 "Return non-nil if SELF is a transition adornment."
719 (cl-check-type self rst-Ado
)
720 (eq (rst-Ado--style self
) 'transition
))
722 (defun rst-Ado-is-section (self)
724 "Return non-nil if SELF is a section adornment."
725 (cl-check-type self rst-Ado
)
726 (not (rst-Ado-is-transition self
)))
728 (defun rst-Ado-is-simple (self)
730 "Return non-nil if SELF is a simple section adornment."
731 (cl-check-type self rst-Ado
)
732 (eq (rst-Ado--style self
) 'simple
))
734 (defun rst-Ado-is-over-and-under (self)
736 "Return non-nil if SELF is a over-and-under section adornment."
737 (cl-check-type self rst-Ado
)
738 (eq (rst-Ado--style self
) 'over-and-under
))
740 (defun rst-Ado-equal (self other
)
742 "Return non-nil when SELF and OTHER are equal."
743 (cl-check-type self rst-Ado
)
744 (cl-check-type other rst-Ado
)
746 ((not (eq (rst-Ado--style self
) (rst-Ado--style other
)))
748 ((rst-Ado-is-transition self
))
749 ((equal (rst-Ado-char self
) (rst-Ado-char other
)))))
751 (defun rst-Ado-position (self ados
)
753 "Return position of SELF in ADOS or nil."
754 (cl-check-type self rst-Ado
)
755 (cl-position-if #'(lambda (e)
756 (rst-Ado-equal self e
))
760 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
765 (:constructor nil
) ; Prevent creating unchecked values.
766 ;; Construct while all parameters must be valid.
772 (ado (rst-Hdr--validate-ado ado-arg
))
773 (indent (rst-Hdr--validate-indent indent-arg ado nil
))))
774 ;; Construct while all parameters but `indent' must be valid.
780 (ado (rst-Hdr--validate-ado ado-arg
))
781 (indent (rst-Hdr--validate-indent indent-arg ado t
))))
782 ;; Construct a header with same characteristics but opposite style as `ado'.
788 (ado (rst-Hdr--validate-ado (rst-Ado-new-invert ado-arg
)))
789 (indent (rst-Hdr--validate-indent indent-arg ado t
))))
790 (:copier nil
)) ; Not really needed for an immutable type.
791 "Representation of reStructuredText section header characteristics.
793 This type is immutable."
794 ;; The adornment of the header.
795 (ado nil
:read-only t
)
796 ;; The indentation of a title text or nil if not given.
797 (indent nil
:read-only t
))
799 ;; Private class methods
801 (defun rst-Hdr--validate-indent (indent ado lax
)
803 "Validate INDENT to be a valid indentation for ADO.
804 Return INDENT if so or signal an error otherwise. If LAX don't
805 signal an error and return a valid indent."
806 (cl-check-type indent integer
)
810 ((rst-Ado-is-simple ado
)
813 (signal 'args-out-of-range
814 '("Indentation must be 0 for style simple"))))
818 (signal 'args-out-of-range
819 '("Indentation must not be negative"))))
820 ;; Implicitly over-and-under.
823 (defun rst-Hdr--validate-ado (ado)
825 "Validate ADO to be a valid adornment.
826 Return ADO if so or signal an error otherwise."
827 (cl-check-type ado rst-Ado
)
829 ((rst-Ado-is-transition ado
)
830 (signal 'args-out-of-range
831 '("Adornment for header must not be transition.")))
834 ;; Public class methods
836 (defvar rst-preferred-adornments
) ; Forward declaration.
838 (defun rst-Hdr-preferred-adornments ()
840 "Return preferred adornments as list of `rst-Hdr'."
842 (lambda ((character style indent
))
844 (if (eq style
'over-and-under
)
845 (rst-Ado-new-over-and-under character
)
846 (rst-Ado-new-simple character
))
848 rst-preferred-adornments
))
852 (defun rst-Hdr-member-ado (self hdrs
)
854 "Return sublist of HDRS whose car's adornment equals that of SELF or nil."
855 (cl-check-type self rst-Hdr
)
856 (let ((ado (rst-Hdr-ado self
)))
857 (cl-member-if #'(lambda (hdr)
858 (rst-Ado-equal ado
(rst-Hdr-ado hdr
)))
861 (defun rst-Hdr-ado-map (selves)
863 "Return `rst-Ado' list extracted from elements of SELVES."
864 (mapcar #'rst-Hdr-ado selves
))
866 (defun rst-Hdr-get-char (self)
868 "Return character of the adornment of SELF."
869 (cl-check-type self rst-Hdr
)
870 (rst-Ado-char (rst-Hdr-ado self
)))
872 (defun rst-Hdr-is-over-and-under (self)
874 "Return non-nil if SELF is a over-and-under section header."
875 (cl-check-type self rst-Hdr
)
876 (rst-Ado-is-over-and-under (rst-Hdr-ado self
)))
879 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
884 (:constructor nil
) ; Prevent creating unchecked values.
885 ;; Construct with valid parameters for all attributes.
886 (:constructor
; Private constructor
893 (ado (rst-Ttl--validate-ado ado-arg
))
894 (match (rst-Ttl--validate-match match-arg ado
))
895 (indent (rst-Ttl--validate-indent indent-arg ado
))
896 (text (rst-Ttl--validate-text text-arg ado
))
897 (hdr (condition-case nil
898 (rst-Hdr-new ado indent
)
900 (:copier nil
)) ; Not really needed for an immutable type.
901 "Representation of a reStructuredText section header as found in a buffer.
902 This type gathers information about an adorned part in the buffer.
904 This type is immutable."
905 ;; The adornment characteristics or nil for a title candidate.
906 (ado nil
:read-only t
)
907 ;; The match-data for `ado' in a form similarly returned by `match-data' (but
908 ;; not necessarily with markers in buffers). Match group 0 matches the whole
909 ;; construct. Match group 1 matches the overline adornment if present.
910 ;; Match group 2 matches the section title text or the transition. Match
911 ;; group 3 matches the underline adornment.
912 (match nil
:read-only t
)
913 ;; An indentation found for the title line or nil for a transition.
914 (indent nil
:read-only t
)
915 ;; The text of the title or nil for a transition.
916 (text nil
:read-only t
)
917 ;; The header characteristics if it is a valid section header.
918 (hdr nil
:read-only t
)
919 ;; FIXME refactoring: Should have an attribute `buffer' for the buffer this
920 ;; title is found in. This breaks lots and lots of tests.
921 ;; However, with private constructor they may not be
922 ;; necessary any more. In case it is really a buffer then
923 ;; also `match' could be real data from `match-data' which
924 ;; contains markers instead of integers.
927 ;; Private class methods
929 (defun rst-Ttl--validate-ado (ado)
931 "Return valid ADO or signal error."
932 (cl-check-type ado
(or null rst-Ado
))
935 (defun rst-Ttl--validate-match (match ado
)
937 "Return valid MATCH matching ADO or signal error."
938 (cl-check-type ado
(or null rst-Ado
))
939 (cl-check-type match list
)
940 (cl-check-type match
(satisfies (lambda (m)
941 (equal (length m
) 8)))
942 "Match data must consist of exactly 8 buffer positions.")
944 (cl-check-type pos
(or null integer-or-marker
)))
945 (cl-destructuring-bind (all-beg all-end
948 und-beg und-end
) match
949 (unless (and (integer-or-marker-p all-beg
) (integer-or-marker-p all-end
))
950 (signal 'args-out-of-range
951 '("First two elements of match data must be buffer positions.")))
954 (unless (and (null ovr-beg
) (null ovr-end
)
955 (integer-or-marker-p txt-beg
) (integer-or-marker-p txt-end
)
956 (null und-beg
) (null und-end
))
957 (signal 'args-out-of-range
958 '("For a title candidate exactly the third match pair must be set."))))
959 ((rst-Ado-is-transition ado
)
960 (unless (and (null ovr-beg
) (null ovr-end
)
961 (integer-or-marker-p txt-beg
) (integer-or-marker-p txt-end
)
962 (null und-beg
) (null und-end
))
963 (signal 'args-out-of-range
964 '("For a transition exactly the third match pair must be set."))))
965 ((rst-Ado-is-simple ado
)
966 (unless (and (null ovr-beg
) (null ovr-end
)
967 (integer-or-marker-p txt-beg
) (integer-or-marker-p txt-end
)
968 (integer-or-marker-p und-beg
) (integer-or-marker-p und-end
))
969 (signal 'args-out-of-range
970 '("For a simple section adornment exactly the third and fourth match pair must be set."))))
972 (unless (and (integer-or-marker-p ovr-beg
) (integer-or-marker-p ovr-end
)
973 (integer-or-marker-p txt-beg
) (integer-or-marker-p txt-end
)
974 (or (null und-beg
) (integer-or-marker-p und-beg
))
975 (or (null und-end
) (integer-or-marker-p und-end
)))
976 (signal 'args-out-of-range
977 '("For a over-and-under section adornment all match pairs must be set."))))))
980 (defun rst-Ttl--validate-indent (indent ado
)
982 "Return valid INDENT for ADO or signal error."
983 (if (and ado
(rst-Ado-is-transition ado
))
984 (cl-check-type indent null
985 "Indent for a transition must be nil.")
986 (cl-check-type indent
(integer 0 *)
987 "Indent for a section header must be non-negative."))
990 (defun rst-Ttl--validate-text (text ado
)
992 "Return valid TEXT for ADO or signal error."
993 (if (and ado
(rst-Ado-is-transition ado
))
994 (cl-check-type text null
995 "Transitions may not have title text.")
996 (cl-check-type text string
))
999 ;; Public class methods
1001 (defun rst-Ttl-from-buffer (ado beg-ovr beg-txt beg-und txt
)
1003 "Return a `rst-Ttl' constructed from information in the current buffer.
1004 ADO is the adornment or nil for a title candidate. BEG-OVR and
1005 BEG-UND are the starting points of the overline or underline,
1006 respectively. They may be nil if the respective thing is missing.
1007 BEG-TXT is the beginning of the title line or the transition and
1008 must be given. The end of the line is used as the end point. TXT
1009 is the title text or nil. If TXT is given the indentation of the
1010 line containing BEG-TXT is used as indentation. Match group 0 is
1011 derived from the remaining information."
1012 (cl-check-type beg-txt integer-or-marker
)
1014 (let ((end-ovr (when beg-ovr
1016 (line-end-position)))
1019 (line-end-position)))
1020 (end-und (when beg-und
1022 (line-end-position)))
1025 (current-indentation))))
1028 (or beg-ovr beg-txt
) (or end-und end-txt
)
1036 (defun rst-Ttl-get-title-beginning (self)
1038 "Return position of beginning of title text of SELF.
1039 This position should always be at the start of a line."
1040 (cl-check-type self rst-Ttl
)
1041 (nth 4 (rst-Ttl-match self
)))
1043 (defun rst-Ttl-get-beginning (self)
1045 "Return position of beginning of whole SELF."
1046 (cl-check-type self rst-Ttl
)
1047 (nth 0 (rst-Ttl-match self
)))
1049 (defun rst-Ttl-get-end (self)
1051 "Return position of end of whole SELF."
1052 (cl-check-type self rst-Ttl
)
1053 (nth 1 (rst-Ttl-match self
)))
1055 (defun rst-Ttl-is-section (self)
1057 "Return non-nil if SELF is a section header or candidate."
1058 (cl-check-type self rst-Ttl
)
1059 (rst-Ttl-text self
))
1061 (defun rst-Ttl-is-candidate (self)
1063 "Return non-nil if SELF is a candidate for a section header."
1064 (cl-check-type self rst-Ttl
)
1065 (not (rst-Ttl-ado self
)))
1067 (defun rst-Ttl-contains (self position
)
1068 "Return whether SELF contain POSITION.
1069 Return 0 if SELF contains POSITION, < 0 if SELF ends before
1070 POSITION and > 0 if SELF starts after position."
1071 (cl-check-type self rst-Ttl
)
1072 (cl-check-type position integer-or-marker
)
1074 ((< (nth 1 (rst-Ttl-match self
)) position
)
1076 ((> (nth 0 (rst-Ttl-match self
)) position
)
1081 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1086 (:constructor nil
) ; Prevent creating unchecked values.
1087 ;; Construct while all parameters must be valid.
1094 (ttl (rst-Stn--validate-ttl ttl-arg
))
1095 (level (rst-Stn--validate-level level-arg ttl
))
1096 (children (rst-Stn--validate-children children-arg ttl
)))))
1097 "Representation of a section tree node.
1099 This type is immutable."
1100 ;; The title of the node or nil for a missing node.
1101 (ttl nil
:read-only t
)
1102 ;; The level of the node in the tree. Negative for the (virtual) top level
1104 (level nil
:read-only t
)
1105 ;; The list of children of the node.
1106 (children nil
:read-only t
))
1107 ;; FIXME refactoring: Should have an attribute `buffer' for the buffer this
1108 ;; title is found in. Or use `rst-Ttl-buffer'.
1110 ;; Private class methods
1112 (defun rst-Stn--validate-ttl (ttl)
1114 "Return valid TTL or signal error."
1115 (cl-check-type ttl
(or null rst-Ttl
))
1118 (defun rst-Stn--validate-level (level ttl
)
1120 "Return valid LEVEL for TTL or signal error."
1121 (cl-check-type level integer
)
1122 (when (and ttl
(< level
0))
1123 ;; testcover: Never reached because a title may not have a negative level
1124 (signal 'args-out-of-range
1125 '("Top level node must not have a title.")))
1128 (defun rst-Stn--validate-children (children ttl
)
1130 "Return valid CHILDREN for TTL or signal error."
1131 (cl-check-type children list
)
1132 (dolist (child children
)
1133 (cl-check-type child rst-Stn
))
1134 (unless (or ttl children
)
1135 (signal 'args-out-of-range
1136 '("A missing node must have children.")))
1141 (defun rst-Stn-get-title-beginning (self)
1143 "Return the beginning of the title of SELF.
1144 Handles missing node properly."
1145 (cl-check-type self rst-Stn
)
1146 (let ((ttl (rst-Stn-ttl self
)))
1148 (rst-Ttl-get-title-beginning ttl
)
1149 (rst-Stn-get-title-beginning (car (rst-Stn-children self
))))))
1151 (defun rst-Stn-get-text (self &optional default
)
1153 "Return title text of SELF or DEFAULT if SELF is a missing node.
1154 For a missing node and no DEFAULT given return a standard title text."
1155 (cl-check-type self rst-Stn
)
1156 (let ((ttl (rst-Stn-ttl self
)))
1161 ("[missing node]"))))
1163 (defun rst-Stn-is-top (self)
1165 "Return non-nil if SELF is a top level node."
1166 (cl-check-type self rst-Stn
)
1167 (< (rst-Stn-level self
) 0))
1170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1173 (defun rst-define-key (keymap key def
&rest deprecated
)
1175 "Bind like `define-key' but add deprecated key definitions.
1176 KEYMAP, KEY, and DEF are as in `define-key'. DEPRECATED key
1177 definitions should be in vector notation. These are defined
1178 as well but give an additional message."
1179 (define-key keymap key def
)
1181 (let* ((command-name (symbol-name def
))
1182 (forwarder-function-name
1183 (if (string-match "^rst-\\(.*\\)$" command-name
)
1184 (concat "rst-deprecated-"
1185 (match-string 1 command-name
))
1186 (error "Not an RST command: %s" command-name
)))
1187 (forwarder-function (intern forwarder-function-name
)))
1188 (unless (fboundp forwarder-function
)
1189 (defalias forwarder-function
1192 (call-interactively def
)
1193 (message "[Deprecated use of key %s; use key %s instead]"
1194 (key-description (this-command-keys))
1195 (key-description key
)))
1196 ;; FIXME: In Emacs-25 we could use (:documentation ...) instead.
1197 (format "Deprecated binding for %s, use \\[%s] instead."
1199 (dolist (dep-key deprecated
)
1200 (define-key keymap dep-key forwarder-function
)))))
1203 (defvar rst-mode-map
1204 (let ((map (make-sparse-keymap)))
1206 ;; \C-c is the general keymap.
1207 (rst-define-key map
[?\C-c ?\C-h
] #'describe-prefix-bindings
)
1210 ;; Section Adornments
1212 ;; The adjustment function that adorns or rotates a section title.
1213 (rst-define-key map
[?\C-c ?\C-
=] #'rst-adjust
[?\C-c ?\C-a t
])
1214 (rst-define-key map
[?\C-
=] #'rst-adjust
) ; Does not work on macOS and
1217 ;; \C-c \C-a is the keymap for adornments.
1218 (rst-define-key map
[?\C-c ?\C-a ?\C-h
] #'describe-prefix-bindings
)
1219 ;; Another binding which works with all types of input.
1220 (rst-define-key map
[?\C-c ?\C-a ?\C-a
] #'rst-adjust
)
1221 ;; Display the hierarchy of adornments implied by the current document
1223 (rst-define-key map
[?\C-c ?\C-a ?\C-d
] #'rst-display-hdr-hierarchy
)
1224 ;; Homogenize the adornments in the document.
1225 (rst-define-key map
[?\C-c ?\C-a ?\C-s
] #'rst-straighten-sections
1229 ;; Section Movement and Selection
1231 ;; Mark the subsection where the cursor is.
1232 (rst-define-key map
[?\C-\M-h
] #'rst-mark-section
1233 ;; Same as mark-defun sgml-mark-current-element.
1235 ;; Move backward/forward between section titles.
1236 ;; FIXME: Also bind similar to outline mode.
1237 (rst-define-key map
[?\C-\M-a
] #'rst-backward-section
1238 ;; Same as beginning-of-defun.
1240 (rst-define-key map
[?\C-\M-e
] #'rst-forward-section
1241 ;; Same as end-of-defun.
1245 ;; Operating on regions
1247 ;; \C-c \C-r is the keymap for regions.
1248 (rst-define-key map
[?\C-c ?\C-r ?\C-h
] #'describe-prefix-bindings
)
1249 ;; Makes region a line-block.
1250 (rst-define-key map
[?\C-c ?\C-r ?\C-l
] #'rst-line-block-region
1252 ;; Shift region left or right according to tabs.
1253 (rst-define-key map
[?\C-c ?\C-r tab
] #'rst-shift-region
1254 [?\C-c ?\C-r t
] [?\C-c ?\C-l t
])
1257 ;; Operating on lists
1259 ;; \C-c \C-l is the keymap for lists.
1260 (rst-define-key map
[?\C-c ?\C-l ?\C-h
] #'describe-prefix-bindings
)
1261 ;; Makes paragraphs in region as a bullet list.
1262 (rst-define-key map
[?\C-c ?\C-l ?\C-b
] #'rst-bullet-list-region
1264 ;; Makes paragraphs in region as a enumeration.
1265 (rst-define-key map
[?\C-c ?\C-l ?\C-e
] #'rst-enumerate-region
1267 ;; Converts bullets to an enumeration.
1268 (rst-define-key map
[?\C-c ?\C-l ?\C-c
] #'rst-convert-bullets-to-enumeration
1270 ;; Make sure that all the bullets in the region are consistent.
1271 (rst-define-key map
[?\C-c ?\C-l ?\C-s
] #'rst-straighten-bullets-region
1273 ;; Insert a list item.
1274 (rst-define-key map
[?\C-c ?\C-l ?\C-i
] #'rst-insert-list
)
1277 ;; Table-of-Contents Features
1279 ;; \C-c \C-t is the keymap for table of contents.
1280 (rst-define-key map
[?\C-c ?\C-t ?\C-h
] #'describe-prefix-bindings
)
1281 ;; Enter a TOC buffer to view and move to a specific section.
1282 (rst-define-key map
[?\C-c ?\C-t ?\C-t
] #'rst-toc
)
1283 ;; Insert a TOC here.
1284 (rst-define-key map
[?\C-c ?\C-t ?\C-i
] #'rst-toc-insert
1286 ;; Update the document's TOC (without changing the cursor position).
1287 (rst-define-key map
[?\C-c ?\C-t ?\C-u
] #'rst-toc-update
1289 ;; Go to the section under the cursor (cursor must be in internal TOC).
1290 (rst-define-key map
[?\C-c ?\C-t ?\C-j
] #'rst-toc-follow-link
1294 ;; Converting Documents from Emacs
1296 ;; \C-c \C-c is the keymap for compilation.
1297 (rst-define-key map
[?\C-c ?\C-c ?\C-h
] #'describe-prefix-bindings
)
1298 ;; Run one of two pre-configured toolset commands on the document.
1299 (rst-define-key map
[?\C-c ?\C-c ?\C-c
] #'rst-compile
1301 (rst-define-key map
[?\C-c ?\C-c ?\C-a
] #'rst-compile-alt-toolset
1303 ;; Convert the active region to pseudo-xml using the docutils tools.
1304 (rst-define-key map
[?\C-c ?\C-c ?\C-x
] #'rst-compile-pseudo-region
1306 ;; Convert the current document to PDF and launch a viewer on the results.
1307 (rst-define-key map
[?\C-c ?\C-c ?\C-p
] #'rst-compile-pdf-preview
1309 ;; Convert the current document to S5 slides and view in a web browser.
1310 (rst-define-key map
[?\C-c ?\C-c ?\C-s
] #'rst-compile-slides-preview
1314 "Keymap for reStructuredText mode commands.
1315 This inherits from Text mode.")
1319 (define-abbrev-table 'rst-mode-abbrev-table
1320 (mapcar #'(lambda (x)
1321 (append x
'(nil 0 system
)))
1322 '(("contents" ".. contents::\n..\n ")
1323 ("con" ".. contents::\n..\n ")
1325 ("skip" "\n\n[...]\n\n ")
1326 ("seq" "\n\n[...]\n\n ")
1327 ;; FIXME: Add footnotes, links, and more.
1329 "Abbrev table used while in `rst-mode'.")
1333 (defvar rst-mode-syntax-table
1334 (let ((st (copy-syntax-table text-mode-syntax-table
)))
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
)
1353 (modify-syntax-entry ?“
"." st
)
1354 (modify-syntax-entry ?”
"." st
)
1356 "Syntax table used while in `rst-mode'.")
1358 (defcustom rst-mode-hook nil
1359 "Hook run when `rst-mode' is turned on.
1360 The hook for `text-mode' is run before this one."
1363 (rst-testcover-defcustom)
1365 ;; Pull in variable definitions silencing byte-compiler.
1366 (require 'newcomment
)
1368 (defvar electric-pair-pairs
)
1369 (defvar electric-indent-inhibit
)
1371 ;; Use rst-mode for *.rst and *.rest files. Many ReStructured-Text files
1372 ;; use *.txt, but this is too generic to be set as a default.
1373 ;;;###autoload (add-to-list 'auto-mode-alist (purecopy '("\\.re?st\\'" . rst-mode)))
1375 (define-derived-mode rst-mode text-mode
"ReST"
1376 "Major mode for editing reStructuredText documents.
1379 Turning on `rst-mode' calls the normal hooks `text-mode-hook'
1380 and `rst-mode-hook'. This mode also supports font-lock
1384 :abbrev-table rst-mode-abbrev-table
1385 :syntax-table rst-mode-syntax-table
1388 ;; Paragraph recognition.
1389 (setq-local paragraph-separate
1393 (setq-local paragraph-start
1397 (:seq hws-tag par-tag- bli-sfx
))))
1399 ;; Indenting and filling.
1400 (setq-local indent-line-function
#'rst-indent-line
)
1401 (setq-local adaptive-fill-mode t
)
1402 (setq-local adaptive-fill-regexp
(rst-re 'hws-tag
'par-tag-
"?" 'hws-tag
))
1403 (setq-local adaptive-fill-function
#'rst-adaptive-fill
)
1404 (setq-local fill-paragraph-handle-comment nil
)
1407 (setq-local comment-start
".. ")
1408 (setq-local comment-start-skip
(rst-re 'lin-beg
'exm-tag
'bli-sfx
))
1409 (setq-local comment-continue
" ")
1410 (setq-local comment-multi-line t
)
1411 (setq-local comment-use-syntax nil
)
1412 ;; reStructuredText has not really a comment ender but nil is not really a
1413 ;; permissible value.
1414 (setq-local comment-end
"")
1415 (setq-local comment-end-skip nil
)
1417 ;; Commenting in reStructuredText is very special so use our own set of
1419 (setq-local comment-line-break-function
#'rst-comment-line-break
)
1420 (setq-local comment-indent-function
#'rst-comment-indent
)
1421 (setq-local comment-insert-comment-function
#'rst-comment-insert-comment
)
1422 (setq-local comment-region-function
#'rst-comment-region
)
1423 (setq-local uncomment-region-function
#'rst-uncomment-region
)
1425 (setq-local electric-pair-pairs
'((?
\" . ?
\") (?\
* . ?\
*) (?\
` . ?\
`)))
1427 ;; Imenu and which function.
1428 ;; FIXME: Check documentation of `which-function' for alternative ways to
1429 ;; determine the current function name.
1430 (setq-local imenu-create-index-function
#'rst-imenu-create-index
)
1433 (setq-local font-lock-defaults
1434 '(rst-font-lock-keywords
1436 (font-lock-multiline . t
)
1437 (font-lock-mark-block-function . mark-paragraph
)))
1438 (add-hook 'font-lock-extend-region-functions
#'rst-font-lock-extend-region t
)
1440 ;; Text after a changed line may need new fontification.
1441 (setq-local jit-lock-contextually t
)
1443 ;; Indentation is not deterministic.
1444 (setq-local electric-indent-inhibit t
))
1447 (define-minor-mode rst-minor-mode
1448 "Toggle ReST minor mode.
1449 With a prefix argument ARG, enable ReST minor mode if ARG is
1450 positive, and disable it otherwise. If called from Lisp, enable
1451 the mode if ARG is omitted or nil.
1453 When ReST minor mode is enabled, the ReST mode keybindings
1454 are installed on top of the major mode bindings. Use this
1455 for modes derived from Text mode, like Mail mode."
1456 ;; The initial value.
1458 ;; The indicator for the mode line.
1460 ;; The minor mode bindings.
1464 ;; FIXME: can I somehow install these too?
1465 ;; :abbrev-table rst-mode-abbrev-table
1466 ;; :syntax-table rst-mode-syntax-table
1469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1470 ;; Section adornment adjustment
1472 ;; The following functions implement a smart automatic title sectioning feature.
1473 ;; The idea is that with the cursor sitting on a section title, we try to get as
1474 ;; much information from context and try to do the best thing automatically.
1475 ;; This function can be invoked many times and/or with prefix argument to rotate
1476 ;; between the various sectioning adornments.
1480 ;; - The underlining character that is used depends on context. The file is
1481 ;; scanned to find other sections and an appropriate character is selected.
1482 ;; If the function is invoked on a section that is complete, the character is
1483 ;; rotated among the existing section adornments.
1485 ;; Note that when rotating the characters, if we come to the end of the
1486 ;; hierarchy of adornments, the variable `rst-preferred-adornments' is
1487 ;; consulted to propose a new underline adornment, and if continued, we cycle
1488 ;; the adornments all over again. Set this variable to nil if you want to
1489 ;; limit the underlining character propositions to the existing adornments in
1492 ;; - An underline/overline that is not extended to the column at which it should
1493 ;; be hanging is dubbed INCOMPLETE. For example::
1498 ;; Examples of default invocation:
1500 ;; |Some Title ---> |Some Title
1503 ;; |Some Title ---> |Some Title
1504 ;; |----- |----------
1507 ;; | Some Title ---> | Some Title
1510 ;; In over-and-under style, when alternating the style, a variable is
1511 ;; available to select how much default indent to use (it can be zero). Note
1512 ;; that if the current section adornment already has an indent, we don't
1513 ;; adjust it to the default, we rather use the current indent that is already
1514 ;; there for adjustment (unless we cycle, in which case we use the indent
1515 ;; that has been found previously).
1517 (defgroup rst-adjust nil
1518 "Settings for adjustment and cycling of section title adornments."
1522 (define-obsolete-variable-alias
1523 'rst-preferred-decorations
'rst-preferred-adornments
"rst 1.0.0")
1524 ;; FIXME: Default must match suggestion in
1525 ;; http://sphinx-doc.org/rest.html#sections for Python documentation.
1526 (defcustom rst-preferred-adornments
'((?
= over-and-under
1)
1534 "Preferred hierarchy of section title adornments.
1535 A list consisting of lists of the form (CHARACTER STYLE INDENT).
1536 CHARACTER is the character used. STYLE is one of the symbols
1537 `over-and-under' or `simple'. INDENT is an integer giving the
1538 wanted indentation for STYLE `over-and-under'.
1540 This sequence is consulted to offer a new adornment suggestion
1541 when we rotate the underlines at the end of the existing
1542 hierarchy of characters, or when there is no existing section
1545 Set this to an empty list to use only the adornment found in the
1549 (group :tag
"Adornment specification"
1550 (choice :tag
"Adornment character"
1551 ,@(mapcar #'(lambda (char)
1553 :tag
(char-to-string char
) char
))
1554 rst-adornment-chars
))
1555 (radio :tag
"Adornment type"
1556 (const :tag
"Overline and underline" over-and-under
)
1557 (const :tag
"Underline only" simple
))
1558 (integer :tag
"Indentation for overline and underline type"
1560 (rst-testcover-defcustom)
1562 ;; FIXME: Rename this to `rst-over-and-under-default-indent' and set default to
1563 ;; 0 because the effect of 1 is probably surprising in the few cases
1564 ;; where this is used.
1565 ;; FIXME: A matching adornment style can be looked for in
1566 ;; `rst-preferred-adornments' and its indentation used before using this
1568 (defcustom rst-default-indent
1
1569 "Number of characters to indent the section title.
1570 This is only used while toggling adornment styles when switching
1571 from a simple adornment style to a over-and-under adornment
1572 style. In addition this is used in cases where the adornments
1573 found in the buffer are to be used but the indentation for
1574 over-and-under adornments is inconsistent across the buffer."
1577 (rst-testcover-defcustom)
1579 (defun rst-new-preferred-hdr (seen prev
)
1581 "Return a new, preferred `rst-Hdr' different from all in SEEN.
1582 PREV is the previous `rst-Hdr' in the buffer. If given the
1583 search starts after this entry. Return nil if no new preferred
1584 `rst-Hdr' can be found."
1585 ;; All preferred adornments are candidates.
1589 ;; Start searching after the level of the previous adornment.
1590 (cdr (rst-Hdr-member-ado prev
(rst-Hdr-preferred-adornments))))
1591 (rst-Hdr-preferred-adornments))))
1592 (cl-find-if #'(lambda (cand)
1593 (not (rst-Hdr-member-ado cand seen
)))
1596 (defun rst-update-section (hdr)
1598 "Unconditionally update the style of the section header at point to HDR.
1599 If there are existing overline and/or underline from the
1600 existing adornment, they are removed before adding the
1601 requested adornment."
1603 (let ((indent (or (rst-Hdr-indent hdr
) 0))
1604 (marker (point-marker))
1607 ;; Fixup whitespace at the beginning and end of the line.
1609 (rst-forward-line-strict 0))
1610 (delete-horizontal-space)
1611 (insert (make-string indent ?
))
1613 (delete-horizontal-space)
1614 (setq new
(make-string (+ (current-column) indent
) (rst-Hdr-get-char hdr
)))
1616 ;; Remove previous line if it is an adornment.
1617 ;; FIXME refactoring: Check whether this deletes `hdr' which *has* all the
1619 (when (and (rst-forward-line-looking-at -
1 'ado-beg-2-1
)
1620 ;; Avoid removing the underline of a title right above us.
1621 (not (rst-forward-line-looking-at -
2 'ttl-beg-1
)))
1622 (rst-delete-entire-line -
1))
1624 ;; Remove following line if it is an adornment.
1625 (when (rst-forward-line-looking-at +1 'ado-beg-2-1
)
1626 (rst-delete-entire-line +1))
1628 ;; Insert underline.
1629 (unless (rst-forward-line-strict +1)
1630 ;; Normalize buffer by adding final newline.
1636 (when (rst-Hdr-is-over-and-under hdr
)
1637 (1value ; Underline inserted above.
1638 (rst-forward-line-strict -
1))
1642 (goto-char marker
)))
1644 (defun rst-classify-adornment (adornment end
&optional accept-over-only
)
1646 "Classify adornment string for section titles and transitions.
1647 ADORNMENT is the complete adornment string as found in the buffer
1648 with optional trailing whitespace. END is the point after the
1649 last character of ADORNMENT. Return a `rst-Ttl' or nil if no
1650 syntactically valid adornment is found. If ACCEPT-OVER-ONLY an
1651 overline with a missing underline is accepted as valid and
1655 (when (string-match (rst-re 'ado-beg-2-1
) adornment
)
1657 (let* ((ado-ch (string-to-char (match-string 2 adornment
)))
1658 (ado-re (rst-re ado-ch
'adorep3-hlp
)) ; RE matching the
1662 (rst-forward-line-strict 0))
1664 (nxt-emp ; Next line nonexistent or empty
1665 (not (rst-forward-line-looking-at +1 'lin-end
#'not
)))
1666 (prv-emp ; Previous line nonexistent or empty
1667 (not (rst-forward-line-looking-at -
1 'lin-end
#'not
)))
1669 (ttl-blw ; Title found below starting here.
1670 (rst-forward-line-looking-at
1674 (setq txt-blw
(match-string-no-properties 1))
1677 (ttl-abv ; Title found above starting here.
1678 (rst-forward-line-looking-at
1682 (setq txt-abv
(match-string-no-properties 1))
1684 (und-fnd ; Matching underline found starting here.
1686 (rst-forward-line-looking-at
1687 +2 (list ado-re
'lin-end
)
1691 (ovr-fnd ; Matching overline found starting here.
1693 (rst-forward-line-looking-at
1694 -
2 (list ado-re
'lin-end
)
1698 (und-wng ; Wrong underline found starting here.
1701 (rst-forward-line-looking-at
1706 (ovr-wng ; Wrong overline found starting here.
1707 (and ttl-abv
(not ovr-fnd
)
1708 (rst-forward-line-looking-at
1713 ;; An adornment above may be a legal
1714 ;; adornment for the line above - consider it
1715 ;; a wrong overline only when it is equally
1718 (length (match-string-no-properties 1))
1719 (length adornment
)))
1722 ((and nxt-emp prv-emp
)
1724 (rst-Ttl-from-buffer (rst-Ado-new-transition)
1725 nil beg-pnt nil nil
))
1726 (ovr-fnd ; Prefer overline match over underline match.
1727 ;; An overline with an underline.
1728 (rst-Ttl-from-buffer (rst-Ado-new-over-and-under ado-ch
)
1729 ovr-fnd ttl-abv beg-pnt txt-abv
))
1731 ;; An overline with an underline.
1732 (rst-Ttl-from-buffer (rst-Ado-new-over-and-under ado-ch
)
1733 beg-pnt ttl-blw und-fnd txt-blw
))
1734 ((and ttl-abv
(not ovr-wng
))
1736 (rst-Ttl-from-buffer (rst-Ado-new-simple ado-ch
)
1737 nil ttl-abv beg-pnt txt-abv
))
1738 ((and accept-over-only ttl-blw
(not und-wng
))
1739 ;; An overline with a missing underline.
1740 (rst-Ttl-from-buffer (rst-Ado-new-over-and-under ado-ch
)
1741 beg-pnt ttl-blw nil txt-blw
))
1743 ;; Invalid adornment.
1746 (defun rst-ttl-at-point ()
1748 "Find a section title line around point and return its characteristics.
1749 If the point is on an adornment line find the respective title
1750 line. If the point is on an empty line check previous or next
1751 line whether it is a suitable title line and use it if so. If
1752 point is on a suitable title line use it. Return a `rst-Ttl' for
1753 a section header or nil if no title line is found."
1757 (rst-forward-line-strict 0))
1758 (let* (cnd-beg ; Beginning of a title candidate.
1759 cnd-txt
; Text of a title candidate.
1760 (cnd-fun #'(lambda (mtcd) ; Function setting title candidate data.
1762 (setq cnd-beg
(match-beginning 0))
1763 (setq cnd-txt
(match-string-no-properties 1))
1767 ((looking-at (rst-re 'ado-beg-2-1
))
1768 ;; Adornment found - consider it.
1769 (setq ttl
(rst-classify-adornment (match-string-no-properties 0)
1771 ((looking-at (rst-re 'lin-end
))
1772 ;; Empty line found - check surrounding lines for a title.
1774 (rst-forward-line-looking-at -
1 'ttl-beg-1 cnd-fun
)
1775 (rst-forward-line-looking-at +1 'ttl-beg-1 cnd-fun
)))
1776 ((looking-at (rst-re 'ttl-beg-1
))
1777 ;; Title line found - check for a following underline.
1778 (setq ttl
(rst-forward-line-looking-at
1782 (rst-classify-adornment
1783 (match-string-no-properties 0) (match-end 0))))))
1784 ;; Title candidate found if no valid adornment found.
1785 (funcall cnd-fun
(not ttl
))))
1787 ((and ttl
(rst-Ttl-is-section ttl
))
1790 (rst-Ttl-from-buffer nil nil cnd-beg nil cnd-txt
)))))))
1792 ;; The following function and variables are used to maintain information about
1793 ;; current section adornment in a buffer local cache. Thus they can be used for
1794 ;; font-locking and manipulation commands.
1796 (defvar-local rst-all-ttls-cache nil
1797 "All section adornments in the buffer as found by `rst-all-ttls'.
1798 Set to t when no section adornments were found.")
1800 ;; FIXME: If this variable is set to a different value font-locking of section
1801 ;; headers is wrong.
1802 (defvar-local rst-hdr-hierarchy-cache nil
1803 "Section hierarchy in the buffer as determined by `rst-hdr-hierarchy'.
1804 Set to t when no section adornments were found.
1805 Value depends on `rst-all-ttls-cache'.")
1807 (rst-testcover-add-1value 'rst-reset-section-caches
)
1808 (defun rst-reset-section-caches ()
1809 "Reset all section cache variables.
1810 Should be called by interactive functions which deal with sections."
1811 (setq rst-all-ttls-cache nil
1812 rst-hdr-hierarchy-cache nil
))
1814 (defun rst-all-ttls-compute ()
1816 "Return a list of `rst-Ttl' for current buffer with ascending line number."
1820 (goto-char (point-min))
1821 ;; Iterate over all the section titles/adornments in the file.
1822 (while (re-search-forward (rst-re 'ado-beg-2-1
) nil t
)
1823 (let ((ttl (rst-classify-adornment
1824 (match-string-no-properties 0) (point))))
1825 (when (and ttl
(rst-Ttl-is-section ttl
))
1826 (when (rst-Ttl-hdr ttl
)
1828 (goto-char (rst-Ttl-get-end ttl
)))))
1831 (defun rst-all-ttls ()
1832 "Return all the section adornments in the current buffer.
1833 Return a list of `rst-Ttl' with ascending line number.
1835 Uses and sets `rst-all-ttls-cache'."
1836 (unless rst-all-ttls-cache
1837 (setq rst-all-ttls-cache
(or (rst-all-ttls-compute) t
)))
1838 (if (eq rst-all-ttls-cache t
)
1840 (copy-sequence rst-all-ttls-cache
)))
1842 (defun rst-infer-hdr-hierarchy (hdrs)
1844 "Build a hierarchy from HDRS.
1845 HDRS reflects the order in which the headers appear in the
1846 buffer. Return a `rst-Hdr' list representing the hierarchy of
1847 headers in the buffer. Indentation is unified."
1848 (let (ado2indents) ; Associates `rst-Ado' with the set of indents seen for it.
1850 (let* ((ado (rst-Hdr-ado hdr
))
1851 (indent (rst-Hdr-indent hdr
))
1852 (found (assoc ado ado2indents
)))
1854 (setcdr found
(cl-adjoin indent
(cdr found
)))
1855 (push (list ado indent
) ado2indents
))))
1856 (mapcar (cl-function
1857 (lambda ((ado consistent
&rest inconsistent
))
1858 (rst-Hdr-new ado
(if inconsistent
1861 (nreverse ado2indents
))))
1863 (defun rst-hdr-hierarchy (&optional ignore-position
)
1865 "Return the hierarchy of section titles in the file as a `rst-Hdr' list.
1866 Each returned element may be used directly to create a section
1867 adornment on that level. If IGNORE-POSITION a title containing
1868 this position is not taken into account when building the
1869 hierarchy unless it appears again elsewhere. This catches cases
1870 where the current title is edited and may not be final regarding
1873 Uses and sets `rst-hdr-hierarchy-cache' unless IGNORE-POSITION is
1875 (let* ((all-ttls (rst-all-ttls))
1880 (equal (rst-Ttl-contains ttl ignore-position
) 0))
1886 (rst-Ado-equal (rst-Ttl-ado ignore-ttl
)
1890 (real-ttls (delq (if really-ignore ignore-ttl
) all-ttls
)))
1891 (copy-sequence ; Protect cache.
1892 (if (and (not ignore-position
) rst-hdr-hierarchy-cache
)
1893 (if (eq rst-hdr-hierarchy-cache t
)
1895 rst-hdr-hierarchy-cache
)
1896 (let ((r (rst-infer-hdr-hierarchy (mapcar #'rst-Ttl-hdr real-ttls
))))
1897 (setq rst-hdr-hierarchy-cache
1899 ;; Clear cache reflecting that a possible update is not
1905 (defun rst-all-ttls-with-level ()
1907 "Return the section adornments with levels set according to hierarchy.
1908 Return a list of (`rst-Ttl' . LEVEL) with ascending line number."
1909 (let ((hier (rst-Hdr-ado-map (rst-hdr-hierarchy))))
1912 (cons ttl
(rst-Ado-position (rst-Ttl-ado ttl
) hier
)))
1915 (defun rst-get-previous-hdr ()
1916 "Return the `rst-Hdr' before point or nil if none."
1917 (let ((prev (cl-find-if #'(lambda (ttl)
1918 (< (rst-Ttl-contains ttl
(point)) 0))
1921 (and prev
(rst-Ttl-hdr prev
))))
1923 (defun rst-adornment-complete-p (ado indent
)
1925 "Return t if the adornment ADO around point is complete using INDENT.
1926 The adornment is complete if it is a completely correct
1927 reStructuredText adornment for the title line at point. This
1928 includes indentation and correct length of adornment lines."
1929 ;; Note: we assume that the detection of the overline as being the underline
1930 ;; of a preceding title has already been detected, and has been eliminated
1931 ;; from the adornment that is given to us.
1932 (let ((exps (list "^" (rst-Ado-char ado
)
1935 ;; Determine last column of title.
1939 (and (rst-forward-line-looking-at +1 exps
)
1940 (or (rst-Ado-is-simple ado
)
1941 (rst-forward-line-looking-at -
1 exps
))
1942 t
))) ; Normalize return value.
1944 (defun rst-next-hdr (hdr hier prev down
)
1946 "Return the next best `rst-Hdr' upward from HDR.
1947 Consider existing hierarchy HIER and preferred headers. PREV may
1948 be a previous `rst-Hdr' which may be taken into account. If DOWN
1949 return the next best `rst-Hdr' downward instead. Return nil in
1951 (let* ((normalized-hier (if down
1954 (fnd (rst-Hdr-member-ado hdr normalized-hier
))
1955 (prev-fnd (and prev
(rst-Hdr-member-ado prev normalized-hier
))))
1957 ;; Next entry in existing hierarchy if it exists.
1960 ;; If current header is found try introducing a new one from preferred
1962 (rst-new-preferred-hdr hier prev
)
1963 ;; If not found try using previous header.
1967 ;; All failed - rotate by using first from normalized existing hierarchy.
1968 (car normalized-hier
))))
1970 ;; FIXME: A line "``/`` full" is not accepted as a section title.
1971 (defun rst-adjust (pfxarg)
1973 "Auto-adjust the adornment around point.
1974 Adjust/rotate the section adornment for the section title around
1975 point or promote/demote the adornments inside the region,
1976 depending on whether the region is active. This function is meant
1977 to be invoked possibly multiple times, and can vary its behavior
1978 with a positive PFXARG (toggle style), or with a negative
1979 PFXARG (alternate behavior).
1981 This function is a bit of a swiss knife. It is meant to adjust
1982 the adornments of a section title in reStructuredText. It tries
1983 to deal with all the possible cases gracefully and to do \"the
1984 right thing\" in all cases.
1986 See the documentations of `rst-adjust-section' and
1987 `rst-adjust-region' for full details.
1989 The method can take either (but not both) of
1991 a. a (non-negative) prefix argument, which means to toggle the
1992 adornment style. Invoke with a prefix argument for example;
1994 b. a negative numerical argument, which generally inverts the
1995 direction of search in the file or hierarchy. Invoke with C--
1996 prefix for example."
1998 (let* ((origpt (point-marker))
1999 (reverse-direction (and pfxarg
(< (prefix-numeric-value pfxarg
) 0)))
2000 (toggle-style (and pfxarg
(not reverse-direction
))))
2002 (rst-adjust-region (and pfxarg t
))
2003 (let ((msg (rst-adjust-section toggle-style reverse-direction
)))
2005 (apply #'message msg
))))
2006 (run-hooks 'rst-adjust-hook
)
2007 (rst-reset-section-caches)
2009 (goto-char origpt
) nil
)))
2011 (defcustom rst-adjust-hook nil
2012 "Hooks to be run after running `rst-adjust'."
2015 :package-version
'(rst .
"1.1.0"))
2016 (rst-testcover-defcustom)
2018 (defcustom rst-new-adornment-down nil
2019 "Controls level of new adornment for section headers."
2022 (const :tag
"Same level as previous one" nil
)
2023 (const :tag
"One level down relative to the previous one" t
))
2024 :package-version
'(rst .
"1.1.0"))
2025 (rst-testcover-defcustom)
2027 (defun rst-adjust-adornment (pfxarg)
2028 "Call `rst-adjust-section' interactively.
2029 Keep this for compatibility for older bindings (are there any?).
2030 Argument PFXARG has the same meaning as for `rst-adjust'."
2033 (let* ((reverse-direction (and pfxarg
(< (prefix-numeric-value pfxarg
) 0)))
2034 (toggle-style (and pfxarg
(not reverse-direction
))))
2035 (rst-adjust-section toggle-style reverse-direction
)))
2037 (defun rst-adjust-new-hdr (toggle-style reverse ttl
)
2039 "Return a new `rst-Hdr' for `rst-adjust-section' related to TTL.
2040 TOGGLE-STYLE and REVERSE are from
2041 `rst-adjust-section'. TOGGLE-STYLE may be consumed and thus is
2044 Return a list (HDR TOGGLE-STYLE MSG...). HDR is the result or
2045 nil. TOGGLE-STYLE is the new TOGGLE-STYLE to use in the
2046 caller. MSG is a list which is non-empty in case HDR is nil
2047 giving an argument list for `message'."
2049 (goto-char (rst-Ttl-get-title-beginning ttl
))
2050 (let ((indent (rst-Ttl-indent ttl
))
2051 (ado (rst-Ttl-ado ttl
))
2052 (prev (rst-get-previous-hdr))
2057 ((rst-Ttl-is-candidate ttl
)
2058 ;; Case 1: No adornment at all.
2059 (let ((hier (rst-hdr-hierarchy)))
2061 ;; Previous header exists - use it.
2063 ;; Customization and parameters require that the previous level
2064 ;; is used - use it as is.
2065 ((or (and rst-new-adornment-down reverse
)
2066 (and (not rst-new-adornment-down
) (not reverse
)))
2068 ;; Advance one level down.
2069 ((rst-next-hdr prev hier prev t
))
2070 ("Neither hierarchy nor preferences can suggest a deeper header"))
2071 ;; First header in the buffer - use the first adornment from
2072 ;; preferences or hierarchy.
2073 (let ((p (car (rst-Hdr-preferred-adornments)))
2077 ;; Prefer hierarchy for downwards
2079 ;; Prefer preferences for upwards
2081 ("No preferences to suggest a top level from"))))))
2082 ((not (rst-adornment-complete-p ado indent
))
2083 ;; Case 2: Incomplete adornment.
2084 ;; Use lax since indentation might not match suggestion.
2085 (rst-Hdr-new-lax ado indent
))
2086 ;; Case 3: Complete adornment exists from here on.
2088 ;; Simply switch the style of the current adornment.
2089 (setq toggle-style nil
) ; Remember toggling has been done.
2090 (rst-Hdr-new-invert ado rst-default-indent
))
2092 ;; Rotate, ignoring a sole adornment around the current line.
2093 (let ((hier (rst-hdr-hierarchy (point))))
2095 ;; Next header can be determined from hierarchy or preferences.
2097 ;; Use lax since indentation might not match suggestion.
2098 (rst-Hdr-new-lax ado indent
) hier prev reverse
))
2099 ;; No next header found.
2100 ("No preferences or hierarchy to suggest another level from"))))))
2101 (if (stringp hdr-msg
)
2102 (list nil toggle-style hdr-msg
)
2103 (list hdr-msg toggle-style
)))))
2105 (defun rst-adjust-section (toggle-style reverse
)
2107 "Adjust/rotate the section adornment for the section title around point.
2108 The action this function takes depends on context around the
2109 point, and it is meant to be invoked possibly more than once to
2110 rotate among the various possibilities. Basically, this function
2113 - adding an adornment if the title does not have one;
2115 - adjusting the length of the underline characters to fit a
2118 - rotating the adornment in the set of already existing
2119 sectioning adornments used in the file;
2121 - switching between simple and over-and-under styles by giving
2124 Return nil if the function did something. If the function were
2125 not able to do something return an argument list for `message' to
2126 inform the user about what failed.
2128 The following is a detailed description but you should normally
2129 not have to read it.
2131 Before applying the adornment change, the cursor is placed on the
2132 closest line that could contain a section title if such is found
2133 around the cursor. Then the following cases are distinguished.
2135 * Case 1: No Adornment
2137 If the current line has no adornment around it,
2139 - search for a previous adornment, and apply this adornment (unless
2140 `rst-new-adornment-down') or one level lower (otherwise) to the current
2141 line. If there is no defined level below this previous adornment, we
2142 suggest the most appropriate of the `rst-preferred-adornments'.
2144 If REVERSE is true, we simply use the previous adornment found
2147 - if there is no adornment found in the given direction, we use the first of
2148 `rst-preferred-adornments'.
2150 TOGGLE-STYLE forces a toggle of the prescribed adornment style.
2152 * Case 2: Incomplete Adornment
2154 If the current line does have an existing adornment, but the adornment is
2155 incomplete, that is, the underline/overline does not extend to exactly the
2156 end of the title line (it is either too short or too long), we simply extend
2157 the length of the underlines/overlines to fit exactly the section title.
2159 If TOGGLE-STYLE we toggle the style of the adornment as well.
2161 REVERSE has no effect in this case.
2163 * Case 3: Complete Existing Adornment
2165 If the adornment is complete (i.e. the underline (overline) length is already
2166 adjusted to the end of the title line), we rotate the current title's
2167 adornment according to the adornment hierarchy found in the buffer. This is
2168 meant to be used potentially multiple times, until the desired adornment is
2169 found around the title.
2171 If we hit the boundary of the hierarchy, exactly one choice from the list of
2172 preferred adornments is suggested/chosen, the first of those adornment that
2173 has not been seen in the buffer yet, and the next invocation rolls over to
2174 the other end of the hierarchy (i.e. it cycles).
2176 If REVERSE is we go up in the hierarchy. Otherwise we go down.
2178 However, if TOGGLE-STYLE, we do not rotate the adornment, but instead simply
2179 toggle the style of the current adornment."
2180 (rst-reset-section-caches)
2181 (let ((ttl (rst-ttl-at-point)))
2183 '("No section header or candidate at point")
2184 (cl-destructuring-bind
2185 (hdr toggle-style
&rest msg
2187 (indent (rst-Ttl-indent ttl
))
2188 (moved (- (line-number-at-pos (rst-Ttl-get-title-beginning ttl
))
2189 (line-number-at-pos))))
2190 (rst-adjust-new-hdr toggle-style reverse ttl
)
2194 (setq hdr
(rst-Hdr-new-invert (rst-Hdr-ado hdr
) indent
)))
2195 ;; Override indent with present indent if there is some.
2197 ;; Use lax since existing indent may not be valid for new style.
2198 (setq hdr
(rst-Hdr-new-lax (rst-Hdr-ado hdr
) indent
)))
2199 (goto-char (rst-Ttl-get-title-beginning ttl
))
2200 (rst-update-section hdr
)
2201 ;; Correct the position of the cursor to more accurately reflect
2202 ;; where it was located when the function was invoked.
2203 (unless (zerop moved
)
2204 (1value ; No lines may be left to move.
2205 (rst-forward-line-strict (- moved
)))
2209 ;; Maintain an alias for compatibility.
2210 (defalias 'rst-adjust-section-title
'rst-adjust
)
2212 (defun rst-adjust-region (demote)
2214 "Promote the section titles within the region.
2215 With argument DEMOTE or a prefix argument, demote the section
2216 titles instead. The algorithm used at the boundaries of the
2217 hierarchy is similar to that used by `rst-adjust-section'."
2219 (rst-reset-section-caches)
2220 (let* ((beg (region-beginning))
2222 (ttls-reg (cl-remove-if-not
2225 (>= (rst-Ttl-contains ttl beg
) 0)
2226 (< (rst-Ttl-contains ttl end
) 0)))
2229 ;; Apply modifications.
2230 (rst-destructuring-dolist
2232 &aux
(hier (rst-hdr-hierarchy)))
2233 (mapcar #'(lambda (ttl)
2234 (cons (copy-marker (rst-Ttl-get-title-beginning ttl
))
2238 (goto-char marker
) nil
)
2239 ;; `rst-next-hdr' cannot return nil because we apply to a section
2240 ;; header so there is some hierarchy.
2241 (rst-update-section (rst-next-hdr hdr hier nil demote
)))
2242 (setq deactivate-mark nil
))))
2244 (defun rst-display-hdr-hierarchy ()
2246 "Display the current file's section title adornments hierarchy.
2247 Hierarchy is displayed in a temporary buffer."
2249 (rst-reset-section-caches)
2250 (let ((hdrs (rst-hdr-hierarchy))
2252 (with-output-to-temp-buffer "*rest section hierarchy*"
2253 (with-current-buffer standard-output
2255 (insert (format "\nSection Level %d" level
))
2256 (rst-update-section hdr
)
2257 (goto-char (point-max))
2259 (cl-incf level
))))))
2261 ;; Maintain an alias for backward compatibility.
2262 (defalias 'rst-display-adornments-hierarchy
'rst-display-hdr-hierarchy
)
2264 ;; FIXME: Should accept an argument giving the hierarchy level to start with
2265 ;; instead of the top of the hierarchy.
2266 (defun rst-straighten-sections ()
2268 "Redo the adornments of all section titles in the current buffer.
2269 This is done using the preferred set of adornments. This can be
2270 used, for example, when using somebody else's copy of a document,
2271 in order to adapt it to our preferred style."
2273 (rst-reset-section-caches)
2275 (rst-destructuring-dolist
2276 ((marker &rest level
)
2279 (lambda ((ttl &rest level
))
2280 ;; Use markers so edits don't disturb the position.
2281 (cons (copy-marker (rst-Ttl-get-title-beginning ttl
)) level
)))
2282 (rst-all-ttls-with-level)))
2284 (goto-char marker
) nil
)
2285 (rst-update-section (nth level
(rst-Hdr-preferred-adornments))))))
2287 ;; Maintain an alias for compatibility.
2288 (defalias 'rst-straighten-adornments
'rst-straighten-sections
)
2291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2292 ;; Insert list items
2294 ;; Borrowed from a2r.el (version 1.3), by Lawrence Mitchell <wence@gmx.li>. I
2295 ;; needed to make some tiny changes to the functions, so I put it here.
2298 (defconst rst-arabic-to-roman
2299 '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
2300 (100 .
"C") (90 .
"XC") (50 .
"L") (40 .
"XL")
2301 (10 .
"X") (9 .
"IX") (5 .
"V") (4 .
"IV")
2303 "List of maps between Arabic numbers and their Roman numeral equivalents.")
2305 (defun rst-arabic-to-roman (num)
2307 "Convert Arabic number NUM to its Roman numeral representation.
2309 Obviously, NUM must be greater than zero. Don't blame me, blame the
2310 Romans, I mean \"what have the Romans ever _done_ for /us/?\" (with
2311 apologies to Monty Python)."
2312 (cl-check-type num
(integer 1 *))
2313 (let ((map rst-arabic-to-roman
)
2315 (while (and map
(> num
0))
2316 (cl-destructuring-bind ((val &rest sym
) &rest next
) map
2318 (setq r
(concat r sym
)
2323 (defun rst-roman-to-arabic (string)
2325 "Convert STRING of Roman numerals to an Arabic number.
2326 If STRING contains a letter which isn't a valid Roman numeral,
2327 the rest of the string from that point onwards is ignored.
2332 (cl-check-type string string
)
2333 (cl-check-type string
(satisfies (lambda (s)
2334 (not (equal s
""))))
2335 "Roman number may not be an empty string.")
2337 (map rst-arabic-to-roman
))
2340 (cl-destructuring-bind ((val &rest sym
) &rest next
) map
2341 (if (string-match (concat "^" sym
) string
)
2342 (setq res
(+ res val
)
2343 string
(replace-match "" nil t string
))
2345 (cl-check-type string
(satisfies (lambda (s)
2347 "Invalid characters in roman number")
2352 ;; FIXME: All the following code should not consider single lines as items but
2353 ;; paragraphs as reST does.
2355 (defun rst-insert-list-new-tag (tag)
2357 "Insert first item of a new list tagged with TAG.
2359 Adding a new list might consider three situations:
2361 (a) Current line is a blank line.
2362 (b) Previous line is a blank line.
2363 (c) Following line is a blank line.
2365 When (a) and (b), just add the new list at current line.
2367 when (a) and not (b), a blank line is added before adding the new list.
2369 When not (a), first forward point to the end of the line, and add two
2370 blank lines, then add the new list.
2372 Other situations are just ignored and left to users themselves."
2373 ;; FIXME: Following line is not considered at all.
2375 ;; FIXME: Doesn't work properly for white-space line. See
2376 ;; `rst-insert-list-new-BUGS'.
2377 (if (rst-forward-line-looking-at 0 'lin-end
)
2378 (if (not (rst-forward-line-looking-at -
1 'lin-end
#'not
))
2383 ;; FIXME: The indentation is not fixed to a single space by the syntax. May
2384 ;; be this should be configurable or rather taken from the context.
2385 (insert (make-string pfx-nls ?
\n) tag
" ")))
2387 (defconst rst-initial-items
2388 (append (mapcar #'char-to-string rst-bullets
)
2390 (dolist (fmt '("%s." "(%s)" "%s)"))
2391 (dolist (c '("#" "1" "a" "A" "I" "i"))
2392 (push (format fmt c
) vals
)))
2394 "List of initial items. It's a collection of bullets and enumerations.")
2396 (defun rst-insert-list-new-item ()
2398 "Insert a new list item.
2400 User is asked to select the item style first, for example (a), i), +.
2401 Use TAB for completion and choices.
2403 If user selects bullets or #, it's just added with position arranged by
2404 `rst-insert-list-new-tag'.
2406 If user selects enumerations, a further prompt is given. User need to
2407 input a starting item, for example 'e' for 'A)' style. The position is
2408 also arranged by `rst-insert-list-new-tag'."
2409 (let* ((itemstyle (completing-read
2410 "Select preferred item style [#.]: "
2411 rst-initial-items nil t nil nil
"#."))
2412 (cnt (if (string-match (rst-re 'cntexp-tag
) itemstyle
)
2413 (match-string 0 itemstyle
)))
2418 (let ((itemno (read-string "Give starting value [a]: "
2420 (downcase (substring itemno
0 1))))
2422 (let ((itemno (read-string "Give starting value [A]: "
2424 (upcase (substring itemno
0 1))))
2426 (let ((itemno (read-number "Give starting value [1]: " 1)))
2427 (rst-arabic-to-roman itemno
)))
2429 (let ((itemno (read-number "Give starting value [1]: " 1)))
2430 (downcase (rst-arabic-to-roman itemno
))))
2432 (let ((itemno (read-number "Give starting value [1]: " 1)))
2433 (number-to-string itemno
)))))))
2435 (setq itemstyle
(replace-match no t t itemstyle
)))
2436 (rst-insert-list-new-tag itemstyle
)))
2438 (defcustom rst-preferred-bullets
2440 "List of favorite bullets."
2443 (choice ,@(mapcar #'(lambda (char)
2445 :tag
(char-to-string char
) char
))
2447 :package-version
'(rst .
"1.1.0"))
2448 (rst-testcover-defcustom)
2450 (defun rst-insert-list-continue (ind tag tab prefer-roman
)
2452 "Insert a new list tag after the current line according to style.
2453 Style is defined by indentation IND, TAG and suffix TAB. If
2454 PREFER-ROMAN roman numbering is preferred over using letters."
2457 ;; FIXME: Separating lines must be possible.
2461 (if (not (string-match (rst-re 'cntexp-tag
) tag
))
2463 (let ((pfx (substring tag
0 (match-beginning 0)))
2464 (cnt (match-string 0 tag
))
2465 (sfx (substring tag
(match-end 0))))
2469 ((string-match (rst-re 'num-tag
) cnt
)
2470 (number-to-string (1+ (string-to-number (match-string 0 cnt
)))))
2472 (string-match (rst-re 'rom-tag
) cnt
)
2474 (if (string-match (rst-re 'ltr-tag
) cnt
) ; Also a letter tag.
2476 ;; FIXME: Assumes one line list items without separating
2478 ;; Use of `rst-forward-line-looking-at' is very difficult
2479 ;; here so don't do it.
2480 (if (and (rst-forward-line-strict -
1)
2481 (looking-at (rst-re 'enmexp-beg
)))
2484 (match-string 0)) ; Previous was a roman tag.
2485 prefer-roman
)) ; Don't know - use flag.
2486 t
))) ; Not a letter tag.
2487 (let* ((old (match-string 0 cnt
))
2488 (new (rst-arabic-to-roman
2489 (1+ (rst-roman-to-arabic (upcase old
))))))
2490 (if (equal old
(upcase old
))
2493 ((string-match (rst-re 'ltr-tag
) cnt
)
2494 (char-to-string (1+ (string-to-char (match-string 0 cnt
))))))
2498 ;; FIXME: At least the continuation may be folded into
2499 ;; `newline-and-indent`. However, this may not be wanted by everyone so
2500 ;; it should be possible to switch this off.
2501 (defun rst-insert-list (&optional prefer-roman
)
2503 "Insert a list item at the current point.
2505 The command can insert a new list or a continuing list. When it is called at a
2506 non-list line, it will promote to insert new list. When it is called at a list
2507 line, it will insert a list with the same list style.
2509 1. When inserting a new list:
2511 User is asked to select the item style first, for example (a), i), +. Use TAB
2512 for completion and choices.
2514 (a) If user selects bullets or #, it's just added.
2515 (b) If user selects enumerations, a further prompt is given. User needs to
2516 input a starting item, for example `e' for `A)' style.
2518 The position of the new list is arranged according to whether or not the
2519 current line and the previous line are blank lines.
2521 2. When continuing a list, one thing needs to be noticed:
2523 List style alphabetical list, such as `a.', and roman numerical list, such as
2524 `i.', have some overlapping items, for example `v.' The function can deal with
2525 the problem elegantly in most situations. But when those overlapped list are
2526 preceded by a blank line, it is hard to determine which type to use
2527 automatically. The function uses alphabetical list by default. If you want
2528 roman numerical list, just use a prefix to set PREFER-ROMAN."
2532 (rst-forward-line-strict 0))
2533 ;; FIXME: Finds only tags in single line items. Multi-line items should be
2534 ;; considered as well.
2535 ;; Using `rst-forward-line-looking-at' is more complicated so don't do it.
2536 (if (looking-at (rst-re 'itmany-beg-1
))
2537 (rst-insert-list-continue
2538 (buffer-substring-no-properties
2539 (match-beginning 0) (match-beginning 1))
2541 (buffer-substring-no-properties (match-end 1) (match-end 0))
2543 (rst-insert-list-new-item))))
2545 ;; FIXME: This is wrong because it misses prefixed lines without intervening
2546 ;; new line. See `rst-straighten-bullets-region-BUGS' and
2547 ;; `rst-find-begs-BUGS'.
2548 (defun rst-find-begs (beg end rst-re-beg
)
2550 "Return the positions of begs in region BEG to END.
2551 RST-RE-BEG is a `rst-re' argument and matched at the beginning of
2552 a line. Return a list of (POINT . COLUMN) where POINT gives the
2553 point after indentation and COLUMN gives its column. The list is
2558 ;; FIXME refactoring: Consider making this construct a macro looping
2562 (rst-forward-line-strict 0))
2563 (while (< (point) end
)
2564 (let ((clm (current-indentation)))
2565 ;; FIXME refactoring: Consider using `rst-forward-line-looking-at'.
2567 (looking-at (rst-re rst-re-beg
)) ; Start found
2568 (not (rst-forward-line-looking-at
2570 #'(lambda (mtcd) ; Previous line exists and is...
2572 (not mtcd
) ; non-empty,
2573 (<= (current-indentation) clm
) ; less indented
2574 (not (and (= (current-indentation) clm
)
2575 ; not a beg at same level.
2576 (looking-at (rst-re rst-re-beg
)))))))))
2577 (back-to-indentation)
2578 (push (cons (point) clm
) r
)))
2579 (1value ; At least one line is moved in this loop.
2580 (rst-forward-line-strict 1 end
)))))
2583 (defun rst-straighten-bullets-region (beg end
)
2585 "Make all the bulleted list items in the region from BEG to END consistent.
2586 Use this after you have merged multiple bulleted lists to make
2587 them use the preferred bullet characters given by
2588 `rst-preferred-bullets' for each level. If bullets are found on
2589 levels beyond the `rst-preferred-bullets' list, they are not
2593 (let (clm2pnts) ; Map a column to a list of points at this column.
2594 (rst-destructuring-dolist
2595 ((point &rest column
2596 &aux
(found (assoc column clm2pnts
)))
2597 (rst-find-begs beg end
'bul-beg
))
2599 ;;; (push point (cdr found)) ; FIXME: Doesn't work with `testcover'.
2600 (setcdr found
(cons point
(cdr found
))) ; Synonym.
2601 (push (list column point
) clm2pnts
)))
2602 (rst-destructuring-dolist
2603 ((bullet _clm
&rest pnts
)
2604 ;; Zip preferred bullets and sorted columns associating a bullet
2605 ;; with a column and all the points this column is found.
2606 (cl-mapcar #'(lambda (bullet clm2pnt
)
2607 (cons bullet clm2pnt
))
2608 rst-preferred-bullets
2609 (sort clm2pnts
#'car-less-than-car
)))
2610 ;; Replace the bullets by the preferred ones.
2613 ;; FIXME: Assumes bullet to replace is a single char.
2615 (insert bullet
))))))
2618 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2619 ;; Table of contents
2621 (defun rst-all-stn ()
2623 "Return the hierarchical tree of sections as a top level `rst-Stn'.
2624 Return value satisfies `rst-Stn-is-top' or is nil for no
2626 (cdr (rst-remaining-stn (rst-all-ttls-with-level) -
1)))
2628 (defun rst-remaining-stn (unprocessed expected
)
2630 "Process the first entry of UNPROCESSED expected to be on level EXPECTED.
2631 UNPROCESSED is the remaining list of (`rst-Ttl' . LEVEL) entries.
2632 Return (REMAINING . STN) for the first entry of UNPROCESSED.
2633 REMAINING is the list of still unprocessed entries. STN is a
2634 `rst-Stn' or nil if UNPROCESSED is empty."
2635 (if (not unprocessed
)
2638 (cl-destructuring-bind
2639 ((ttl &rest level
) &rest next
2642 (when (= level expected
)
2643 ;; Consume the current entry and create the current node with it.
2645 (setq unprocessed next
))
2646 ;; Build the child nodes as long as they have deeper level.
2647 (while (and unprocessed
(> (cdar unprocessed
) expected
))
2648 (cl-destructuring-bind (remaining &rest stn
)
2649 (rst-remaining-stn unprocessed
(1+ expected
))
2651 (push stn children
))
2652 (setq unprocessed remaining
)))
2654 (when (or fnd children
)
2655 (rst-Stn-new fnd expected
(nreverse children
)))))))
2657 (defun rst-stn-containing-point (stn &optional point
)
2659 "Return `rst-Stn' in STN before POINT or nil if in no section.
2660 POINT defaults to the current point. STN may be nil for no
2661 section headers at all."
2663 (setq point
(or point
(point)))
2664 (when (>= point
(rst-Stn-get-title-beginning stn
))
2665 ;; Point may be in this section or a child.
2666 (let ((in-child (cl-find-if
2668 (>= point
(rst-Stn-get-title-beginning child
)))
2669 (rst-Stn-children stn
)
2672 (rst-stn-containing-point in-child point
)
2675 (defgroup rst-toc nil
2676 "Settings for reStructuredText table of contents."
2680 (defcustom rst-toc-indent
2
2681 "Indentation for table-of-contents display.
2682 Also used for formatting insertion, when numbering is disabled."
2685 (rst-testcover-defcustom)
2687 (defcustom rst-toc-insert-style
'fixed
2688 "Insertion style for table-of-contents.
2689 Set this to one of the following values to determine numbering and
2691 - `plain': no numbering (fixed indentation)
2692 - `fixed': numbering, but fixed indentation
2693 - `aligned': numbering, titles aligned under each other
2694 - `listed': titles as list items"
2695 :type
'(choice (const plain
)
2700 (rst-testcover-defcustom)
2702 (defcustom rst-toc-insert-number-separator
" "
2703 "Separator that goes between the TOC number and the title."
2706 (rst-testcover-defcustom)
2708 (defcustom rst-toc-insert-max-level nil
2709 "If non-nil, maximum depth of the inserted TOC."
2710 :type
'(choice (const nil
) integer
)
2712 (rst-testcover-defcustom)
2714 (defun rst-toc-insert (&optional max-level
)
2716 "Insert the table of contents of the current section at the current column.
2717 By default the top level is ignored if there is only one, because
2718 we assume that the document will have a single title. A numeric
2719 prefix argument MAX-LEVEL overrides `rst-toc-insert-max-level'.
2720 Text in the line beyond column is deleted."
2722 (rst-reset-section-caches)
2723 (let ((pt-stn (rst-stn-containing-point (rst-all-stn))))
2726 (if (and (integerp max-level
)
2727 (> (prefix-numeric-value max-level
) 0))
2728 (prefix-numeric-value max-level
)
2729 rst-toc-insert-max-level
))
2730 (ind (current-column))
2731 (buf (current-buffer))
2732 (tabs indent-tabs-mode
) ; Copy buffer local value.
2735 ;; Render to temporary buffer so markers are created correctly.
2737 (rst-toc-insert-tree pt-stn buf rst-toc-insert-style max
2738 rst-toc-link-keymap nil
)
2739 (goto-char (point-min))
2740 (when (rst-forward-line-strict 1)
2741 ;; There are lines to indent.
2742 (let ((indent-tabs-mode tabs
))
2743 (indent-rigidly (point) (point-max) ind
)))
2745 (unless (zerop (length txt
))
2746 ;; Delete possible trailing text.
2747 (delete-region (point) (line-beginning-position 2))
2749 (backward-char 1))))))
2751 (defun rst-toc-insert-link (pfx stn buf keymap
)
2753 "Insert text of STN in BUF as a linked section reference at point.
2754 If KEYMAP use this as keymap property. PFX is inserted before text."
2755 (let ((beg (point)))
2757 (insert (rst-Stn-get-text stn
))
2758 (put-text-property beg
(point) 'mouse-face
'highlight
)
2761 beg
(point) 'rst-toc-target
2762 (set-marker (make-marker) (rst-Stn-get-title-beginning stn
) buf
))
2764 (put-text-property beg
(point) 'keymap keymap
))))
2766 (defun rst-toc-get-link (link-buf link-pnt
)
2768 "Return the link from text property at LINK-PNT in LINK-BUF."
2769 (let ((mrkr (get-text-property link-pnt
'rst-toc-target link-buf
)))
2771 (error "No section on this line"))
2772 (unless (buffer-live-p (marker-buffer mrkr
))
2773 (error "Buffer for this section was killed"))
2776 (defconst rst-toc-link-keymap
2777 (let ((map (make-sparse-keymap)))
2778 (define-key map
[mouse-1
] 'rst-toc-mouse-follow-link
)
2780 "Keymap used for links in TOC.")
2782 (defun rst-toc-insert-tree (stn buf style depth keymap tgt-stn
)
2784 "Insert table of contents of tree below top node STN in buffer BUF.
2785 STYLE is the style to use and must be one of the symbols allowed
2786 for `rst-toc-insert-style'. DEPTH is the maximum relative depth
2787 from STN to insert or nil for no maximum depth. See
2788 `rst-toc-insert-link' for KEYMAP. Return beginning of title line
2789 if TGT-STN is rendered or nil if not rendered or TGT-STN is nil.
2790 Just return nil if STN is nil."
2792 (rst-toc-insert-children (rst-Stn-children stn
) buf style depth
0 "" keymap
2795 (defun rst-toc-insert-children (children buf style depth indent numbering
2798 "In the current buffer at point insert CHILDREN in BUF to table of contents.
2799 See `rst-toc-insert-tree' for STYLE, DEPTH and TGT-STN. See
2800 `rst-toc-insert-stn' for INDENT and NUMBERING. See
2801 `rst-toc-insert-link' for KEYMAP."
2803 ;; Child numbering is done from the parent.
2804 (num-fmt (format "%%%dd"
2805 (1+ (floor (log (1+ (length children
)) 10)))))
2807 (when (not (equal numbering
""))
2808 ;; Add separating dot to existing numbering.
2809 (setq numbering
(concat numbering
".")))
2810 (dolist (child children fnd
)
2812 (or (rst-toc-insert-stn child buf style depth indent
2813 (concat numbering
(format num-fmt count
))
2814 keymap tgt-stn
) fnd
))
2817 ;; FIXME refactoring: Use `rst-Stn-buffer' instead of `buf'.
2818 (defun rst-toc-insert-stn (stn buf style depth indent numbering keymap tgt-stn
)
2820 "In the current buffer at point insert STN in BUF into table of contents.
2821 See `rst-toc-insert-tree' for STYLE, DEPTH and TGT-STN. INDENT
2822 is the indentation depth to use for STN. NUMBERING is the prefix
2823 numbering for STN. See `rst-toc-insert-link' for KEYMAP."
2824 (when (or (not depth
) (> depth
0))
2825 (cl-destructuring-bind
2827 &aux
(fnd (when (and tgt-stn
2828 (equal (rst-Stn-get-title-beginning stn
)
2829 (rst-Stn-get-title-beginning tgt-stn
)))
2833 (list "" rst-toc-indent
))
2835 (list (concat numbering rst-toc-insert-number-separator
)
2837 ((eq style
'aligned
)
2838 (list (concat numbering rst-toc-insert-number-separator
)
2839 (+ (length numbering
)
2840 (length rst-toc-insert-number-separator
))))
2842 (list (format "%c " (car rst-preferred-bullets
)) 2)))
2843 ;; Indent using spaces so buffer characteristics like `indent-tabs-mode'
2845 (rst-toc-insert-link (concat (make-string indent ?
) pfx
) stn buf keymap
)
2846 (or (rst-toc-insert-children (rst-Stn-children stn
) buf style
2849 (+ indent add
) numbering keymap tgt-stn
)
2852 (defun rst-toc-update ()
2854 "Automatically find the contents section of a document and update.
2855 Updates the inserted TOC if present. You can use this in your
2856 file-write hook to always make it up-to-date automatically."
2860 ;; Find and delete an existing comment after the first contents
2861 ;; directive. Delete that region.
2862 (goto-char (point-min))
2863 ;; FIXME: Should accept indentation of the whole block.
2864 ;; We look for the following and the following only (in other words, if
2865 ;; your syntax differs, this won't work.).
2867 ;; .. contents:: [...anything here...]
2868 ;; [:field: value]...
2873 ;; FIXME: Works only for the first of these tocs. There should be a
2874 ;; fixed text after the comment such as "RST-MODE ELECTRIC TOC".
2875 ;; May be parameters such as `max-level' should be appended.
2876 (let ((beg (re-search-forward
2878 (rst-re "^" 'exm-sta
"contents" 'dcl-tag
".*\n"
2879 "\\(?:" 'hws-sta
'fld-tag
".*\n\\)*" 'exm-tag
))
2884 (rst-forward-line-looking-at
2888 (rst-apply-indented-blocks
2889 (point) (point-max) (current-indentation)
2890 #'(lambda (count _in-first _in-sub in-super in-empty
2893 ((or (> count
1) in-super
))
2895 (setq fnd
(line-end-position))
2899 (delete-region beg fnd
))
2902 ;; FIXME: Ignores an `max-level' given to the original
2903 ;; `rst-toc-insert'. `max-level' could be rendered to the first
2905 (rst-toc-insert)))))
2906 ;; Note: always return nil, because this may be used as a hook.
2909 ;; FIXME: Updating the toc on saving would be nice. However, this doesn't work
2912 ;; (add-hook 'write-contents-hooks 'rst-toc-update-fun)
2913 ;; (defun rst-toc-update-fun ()
2914 ;; ;; Disable undo for the write file hook.
2915 ;; (let ((buffer-undo-list t)) (rst-toc-update) ))
2917 ;; Maintain an alias for compatibility.
2918 (defalias 'rst-toc-insert-update
'rst-toc-update
)
2920 (defconst rst-toc-buffer-name
"*Table of Contents*"
2921 "Name of the Table of Contents buffer.")
2923 (defvar-local rst-toc-mode-return-wincfg nil
2924 "Window configuration to which to return when leaving the TOC.")
2928 "Display a table of contents for current buffer.
2929 Displays all section titles found in the current buffer in a
2930 hierarchical list. The resulting buffer can be navigated, and
2931 selecting a section title moves the cursor to that section."
2933 (rst-reset-section-caches)
2934 (let* ((wincfg (list (current-window-configuration) (point-marker)))
2935 (sectree (rst-all-stn))
2936 (target-stn (rst-stn-containing-point sectree
))
2937 (target-buf (current-buffer))
2938 (buf (get-buffer-create rst-toc-buffer-name
))
2940 (with-current-buffer buf
2941 (let ((inhibit-read-only t
))
2943 (delete-region (point-min) (point-max))
2944 ;; FIXME: Could use a customizable style.
2945 (setq target-pos
(rst-toc-insert-tree
2946 sectree target-buf
'plain nil nil target-stn
))))
2947 (display-buffer buf
)
2949 (setq rst-toc-mode-return-wincfg wincfg
)
2950 (goto-char (or target-pos
(point-min)))))
2952 ;; Maintain an alias for compatibility.
2953 (defalias 'rst-goto-section
'rst-toc-follow-link
)
2955 (defun rst-toc-follow-link (link-buf link-pnt kill
)
2957 "Follow the link to the section at LINK-PNT in LINK-BUF.
2958 LINK-PNT and LINK-BUF default to the point in the current buffer.
2959 With prefix argument KILL a TOC buffer is destroyed. Throw an
2960 error if there is no working link at the given position."
2961 (interactive "i\nd\nP")
2963 (setq link-buf
(current-buffer)))
2964 ;; Do not catch errors from `rst-toc-get-link' because otherwise the error is
2965 ;; suppressed and invisible in interactive use.
2966 (let ((mrkr (rst-toc-get-link link-buf link-pnt
)))
2968 (rst-toc-mode-return kill
)
2969 ;; Catch errors when not in `toc-mode'.
2971 (pop-to-buffer (marker-buffer mrkr
))
2973 ;; FIXME: Should be a customizable number of lines from beginning or end of
2974 ;; window just like the argument to `recenter`. It would be ideal if
2975 ;; the adornment is always completely visible.
2978 ;; Maintain an alias for compatibility.
2979 (defalias 'rst-toc-mode-goto-section
'rst-toc-mode-follow-link-kill
)
2981 ;; FIXME: Cursor before or behind the list must be handled properly; before the
2982 ;; list should jump to the top and behind the list to the last normal
2984 (defun rst-toc-mode-follow-link-kill ()
2986 "Follow the link to the section at point and kill the TOC buffer."
2988 (rst-toc-follow-link (current-buffer) (point) t
))
2990 ;; Maintain an alias for compatibility.
2991 (defalias 'rst-toc-mode-mouse-goto
'rst-toc-mouse-follow-link
)
2993 (defun rst-toc-mouse-follow-link (event kill
)
2994 ;; testcover: uncovered.
2995 "In `rst-toc' mode, go to the occurrence whose line you click on.
2996 EVENT is the input event. Kill TOC buffer if KILL."
2997 (interactive "e\ni")
2998 (rst-toc-follow-link (window-buffer (posn-window (event-end event
)))
2999 (posn-point (event-end event
)) kill
))
3001 ;; Maintain an alias for compatibility.
3002 (defalias 'rst-toc-mode-mouse-goto-kill
'rst-toc-mode-mouse-follow-link-kill
)
3004 (defun rst-toc-mode-mouse-follow-link-kill (event)
3005 ;; testcover: uncovered.
3006 "Same as `rst-toc-mouse-follow-link', but kill TOC buffer as well.
3007 EVENT is the input event."
3009 (rst-toc-mouse-follow-link event t
))
3011 ;; Maintain an alias for compatibility.
3012 (defalias 'rst-toc-quit-window
'rst-toc-mode-return
)
3014 (defun rst-toc-mode-return (kill)
3016 "Leave the current TOC buffer and return to the previous environment.
3017 With prefix argument KILL non-nil, kill the buffer instead of
3020 (unless rst-toc-mode-return-wincfg
3021 (error "Not in a `toc-mode' buffer"))
3022 (cl-destructuring-bind
3024 &aux
(toc-buf (current-buffer)))
3025 rst-toc-mode-return-wincfg
3026 (set-window-configuration wincfg
)
3029 (kill-buffer toc-buf
)
3030 (bury-buffer toc-buf
))))
3032 (defun rst-toc-mode-return-kill ()
3033 ;; testcover: uncovered.
3034 "Like `rst-toc-mode-return' but kill TOC buffer."
3036 (rst-toc-mode-return t
))
3038 (defvar rst-toc-mode-map
3039 (let ((map (make-sparse-keymap)))
3040 (define-key map
[mouse-1
] #'rst-toc-mode-mouse-follow-link-kill
)
3041 (define-key map
[mouse-2
] #'rst-toc-mouse-follow-link
)
3042 (define-key map
"\C-m" #'rst-toc-mode-follow-link-kill
)
3043 (define-key map
"f" #'rst-toc-mode-follow-link-kill
)
3044 (define-key map
"n" #'next-line
)
3045 (define-key map
"p" #'previous-line
)
3046 (define-key map
"q" #'rst-toc-mode-return
)
3047 (define-key map
"z" #'rst-toc-mode-return-kill
)
3049 "Keymap for `rst-toc-mode'.")
3051 (define-derived-mode rst-toc-mode special-mode
"ReST-TOC"
3052 "Major mode for output from \\[rst-toc], the table-of-contents for the document.
3053 \\{rst-toc-mode-map}"
3054 ;; FIXME: `revert-buffer-function` must be defined so `revert-buffer` works
3055 ;; as expected for a special mode. In particular the referred buffer
3056 ;; needs to be rescanned and the TOC must be updated accordingly.
3057 ;; FIXME: Should contain the name of the buffer this is the toc of.
3058 (setq header-line-format
"Table of Contents"))
3060 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3063 ;; FIXME testcover: Use `testcover'. Mark up a function with sufficient test
3064 ;; coverage by a comment tagged with `testcover' after the
3065 ;; `defun'. Then move this comment.
3067 (defun rst-forward-section (offset)
3068 "Jump forward OFFSET section titles ending up at the start of the title line.
3069 OFFSET defaults to 1 and may be negative to move backward. An
3070 OFFSET of 0 does not move unless point is inside a title. Go to
3071 end or beginning of buffer if no more section titles in the desired
3074 (rst-reset-section-caches)
3075 (let* ((ttls (rst-all-ttls))
3076 (count (length ttls
))
3078 (contained nil
) ; Title contains point (or is after point otherwise).
3079 (found (or (cl-position-if
3080 ;; Find a title containing or after point.
3082 (let ((cmp (rst-Ttl-contains ttl pnt
)))
3084 ((= cmp
0) ; Title contains point.
3087 ((> cmp
0) ; Title after point.
3090 ;; Point after all titles.
3092 (target (+ found offset
3093 ;; If point is in plain text found title is already one
3095 (if (and (not contained
) (>= offset
0)) -
1 0))))
3101 ((and (not contained
) (= offset
0))
3102 ;; Point not in title and should not move - do not move.
3104 ((rst-Ttl-get-title-beginning (nth target ttls
)))))))
3106 (defun rst-backward-section (offset)
3107 "Like `rst-forward-section', except move backward by OFFSET."
3109 (rst-forward-section (- offset
)))
3111 ;; FIXME: What is `allow-extend' for? See `mark-paragraph' for an explanation.
3112 (defun rst-mark-section (&optional count allow-extend
)
3113 "Select COUNT sections around point.
3114 Mark following sections for positive COUNT or preceding sections
3115 for negative COUNT."
3116 ;; Cloned from mark-paragraph.
3117 (interactive "p\np")
3118 (unless count
(setq count
1))
3120 (error "Cannot mark zero sections"))
3121 (cond ((and allow-extend
3122 (or (and (eq last-command this-command
) (mark t
))
3127 (rst-forward-section count
)
3130 (rst-forward-section count
)
3132 (rst-forward-section (- count
)))))
3135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3138 (defun rst-find-leftmost-column (beg end
)
3139 "Return the leftmost column spanned by region BEG to END.
3140 The line containing the start of the region is always considered
3141 spanned. If the region ends at the beginning of a line this line
3142 is not considered spanned, otherwise it is spanned."
3148 (rst-forward-line-strict 0))
3149 (while (< (point) end
)
3150 (unless (looking-at (rst-re 'lin-end
))
3151 (setq mincol
(if mincol
3152 (min mincol
(current-indentation))
3153 (current-indentation))))
3154 (rst-forward-line-strict 1 end
)))
3157 ;; FIXME: At the moment only block comments with leading empty comment line are
3158 ;; supported. Comment lines with leading comment markup should be also
3159 ;; supported. May be a customizable option could control which style to
3162 (defgroup rst-indent nil
"Settings for indentation in reStructuredText.
3164 In reStructuredText indentation points are usually determined by
3165 preceding lines. Sometimes the syntax allows arbitrary indentation
3166 points such as where to start the first line following a directive.
3167 These indentation widths can be customized here."
3169 :package-version
'(rst .
"1.1.0"))
3171 (define-obsolete-variable-alias
3172 'rst-shift-basic-offset
'rst-indent-width
"rst 1.0.0")
3173 (defcustom rst-indent-width
2
3174 "Indentation when there is no more indentation point given."
3177 (rst-testcover-defcustom)
3179 (defcustom rst-indent-field
3
3180 "Indentation for first line after a field or 0 to always indent for content."
3182 :package-version
'(rst .
"1.1.0")
3184 (rst-testcover-defcustom)
3186 (defcustom rst-indent-literal-normal
3
3187 "Default indentation for literal block after a markup on an own line."
3189 :package-version
'(rst .
"1.1.0")
3191 (rst-testcover-defcustom)
3193 (defcustom rst-indent-literal-minimized
2
3194 "Default indentation for literal block after a minimized markup."
3196 :package-version
'(rst .
"1.1.0")
3198 (rst-testcover-defcustom)
3200 (defcustom rst-indent-comment
3
3201 "Default indentation for first line of a comment."
3203 :package-version
'(rst .
"1.1.0")
3205 (rst-testcover-defcustom)
3207 ;; FIXME: Must consider other tabs:
3209 ;; * Definition lists
3211 (defun rst-line-tabs ()
3212 "Return tabs of the current line or nil for no tab.
3213 The list is sorted so the tab where writing continues most likely
3214 is the first one. Each tab is of the form (COLUMN . INNER).
3215 COLUMN is the column of the tab. INNER is non-nil if this is an
3216 inner tab. I.e. a tab which does come from the basic indentation
3217 and not from inner alignment points."
3219 (rst-forward-line-strict 0)
3221 (unless (looking-at (rst-re 'lin-end
))
3222 (back-to-indentation)
3223 ;; Current indentation is always the least likely tab.
3224 (let ((tabs (list (list (point) 0 nil
)))) ; (POINT OFFSET INNER)
3225 ;; Push inner tabs more likely to continue writing.
3228 ((looking-at (rst-re '(:grp itmany-tag hws-sta
) '(:grp
"\\S ") "?"))
3229 (when (match-string 2)
3230 (push (list (match-beginning 2) 0 t
) tabs
)))
3232 ((looking-at (rst-re '(:grp fld-tag
) '(:grp hws-tag
)
3233 '(:grp
"\\S ") "?"))
3234 (unless (zerop rst-indent-field
)
3235 (push (list (match-beginning 1) rst-indent-field t
) tabs
))
3236 (if (match-string 3)
3237 (push (list (match-beginning 3) 0 t
) tabs
)
3238 (if (zerop rst-indent-field
)
3239 (push (list (match-end 2)
3240 (if (string= (match-string 2) "") 1 0)
3244 ((looking-at (rst-re 'dir-sta-3
'(:grp
"\\S ") "?"))
3245 (push (list (match-end 1) 0 t
) tabs
)
3246 (unless (string= (match-string 2) "")
3247 (push (list (match-end 2) 0 t
) tabs
))
3248 (when (match-string 4)
3249 (push (list (match-beginning 4) 0 t
) tabs
)))
3250 ;; Footnote or citation definition.
3251 ((looking-at (rst-re 'fnc-sta-2
'(:grp
"\\S ") "?"))
3252 (push (list (match-end 1) 0 t
) tabs
)
3253 (when (match-string 3)
3254 (push (list (match-beginning 3) 0 t
) tabs
)))
3256 ((looking-at (rst-re 'cmt-sta-1
))
3257 (push (list (point) rst-indent-comment t
) tabs
)))
3258 ;; Start of literal block.
3259 (when (looking-at (rst-re 'lit-sta-2
))
3260 (cl-destructuring-bind (point offset _inner
) (car tabs
)
3263 (if (match-string 1)
3264 rst-indent-literal-minimized
3265 rst-indent-literal-normal
))
3268 (mapcar (cl-function
3269 (lambda ((point offset inner
))
3271 (cons (+ (current-column) offset
) inner
)))
3274 (defun rst-compute-tabs (pt)
3275 "Build the list of possible tabs for all lines above.
3276 Search backwards from point PT to build the list of possible tabs.
3277 Return a list of tabs sorted by likeliness to continue writing
3278 like `rst-line-tabs'. Nearer lines have generally a higher
3279 likeliness than farther lines. Return nil if no tab is found in
3281 ;; FIXME: See test `indent-for-tab-command-BUGS`.
3284 (let (leftmost ; Leftmost column found so far.
3285 innermost
; Leftmost column for inner tab.
3287 (while (and (rst-forward-line-strict -
1)
3290 (let ((tabs (rst-line-tabs)))
3292 (let ((leftcol (apply #'min
(mapcar #'car tabs
))))
3293 ;; Consider only lines indented less or same if not INNERMOST.
3294 (when (or (not leftmost
)
3295 (< leftcol leftmost
)
3296 (and (not innermost
) (= leftcol leftmost
)))
3297 (rst-destructuring-dolist ((column &rest inner
) tabs
)
3301 (< column leftmost
)))
3304 (< column innermost
))))
3305 (setq tablist
(cl-adjoin column tablist
))))
3306 (setq innermost
(if (cl-some #'cdr tabs
) ; Has inner.
3309 (setq leftmost leftcol
))))))
3310 (nreverse tablist
))))
3312 (defun rst-indent-line (&optional dflt
)
3313 "Indent current line to next best reStructuredText tab.
3314 The next best tab is taken from the tab list returned by
3315 `rst-compute-tabs' which is used in a cyclic manner. If the
3316 current indentation does not end on a tab use the first one. If
3317 the current indentation is on a tab use the next tab. This allows
3318 a repeated use of \\[indent-for-tab-command] to cycle through all
3319 possible tabs. If no indentation is possible return `noindent' or
3320 use DFLT. Return the indentation indented to. When point is in
3321 indentation it ends up at its end. Otherwise the point is kept
3322 relative to the content."
3323 (let* ((pt (point-marker))
3324 (cur (current-indentation))
3325 (clm (current-column))
3326 (tabs (rst-compute-tabs (point)))
3327 (fnd (cl-position cur tabs
:test
#'equal
))
3329 (if (and (not tabs
) (not dflt
))
3336 (if (>= fnd
(length tabs
))
3338 (setq ind
(nth fnd tabs
)))
3339 (indent-line-to ind
)
3345 (defun rst-shift-region (beg end cnt
)
3346 "Shift region BEG to END by CNT tabs.
3347 Shift by one tab to the right (CNT > 0) or left (CNT < 0) or
3348 remove all indentation (CNT = 0). A tab is taken from the text
3349 above. If no suitable tab is found `rst-indent-width' is used."
3350 (interactive "r\np")
3351 (let ((tabs (sort (rst-compute-tabs beg
)
3354 (leftmostcol (rst-find-leftmost-column beg end
)))
3355 (when (or (> leftmostcol
0) (> cnt
0))
3356 ;; Apply the indent.
3361 ;; Find the next tab after the leftmost column.
3362 (let* ((cmp (if (> cnt
0) #'> #'<))
3363 (tabs (if (> cnt
0) tabs
(reverse tabs
)))
3365 (dir (cl-signum cnt
)) ; Direction to take.
3366 (abs (abs cnt
)) ; Absolute number of steps to take.
3367 ;; Get the position of the first tab beyond leftmostcol.
3368 (fnd (cl-position-if #'(lambda (elt)
3369 (funcall cmp elt leftmostcol
))
3371 ;; Virtual position of tab.
3372 (pos (+ (or fnd len
) (1- abs
)))
3373 (tab (if (< pos len
)
3374 ;; Tab exists - use it.
3376 ;; Column needs to be computed.
3377 (let ((col (+ (or (car (last tabs
)) leftmostcol
)
3378 ;; Base on last known column.
3379 (* (- pos
(1- len
)) ; Distance left.
3380 dir
; Direction to take.
3381 rst-indent-width
))))
3382 (if (< col
0) 0 col
)))))
3383 (- tab leftmostcol
)))))))
3385 ;; FIXME: A paragraph with an (incorrectly) indented second line is not filled
3390 (defun rst-adaptive-fill ()
3391 "Return fill prefix found at point.
3392 Value for `adaptive-fill-function'."
3394 (let ((fnd (if (looking-at adaptive-fill-regexp
)
3395 (match-string-no-properties 0))))
3396 (if (save-match-data
3397 (not (string-match comment-start-skip fnd
)))
3398 ;; An non-comment prefix is fine.
3400 ;; Matches a comment - return whitespace instead.
3403 (goto-char (match-end 0))
3406 (goto-char (match-beginning 0))
3407 (current-column))) ?
)))))
3409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3412 (defun rst-comment-line-break (&optional soft
)
3413 "Break line and indent, continuing reStructuredText comment if within one.
3414 Value for `comment-line-break-function'. If SOFT use soft
3415 newlines as mandated by `comment-line-break-function'."
3417 (insert-and-inherit ?
\n)
3421 (delete-horizontal-space))
3422 (delete-horizontal-space)
3423 (let ((tabs (rst-compute-tabs (point))))
3425 (indent-line-to (car tabs
)))))
3427 (defun rst-comment-indent ()
3428 "Return indentation for current comment line."
3429 (car (rst-compute-tabs (point))))
3431 (defun rst-comment-insert-comment ()
3432 "Insert a comment in the current line."
3434 (insert comment-start
))
3436 (defun rst-comment-region (beg end
&optional arg
)
3437 "Comment or uncomment the current region.
3438 Region is from BEG to END. Uncomment if ARG."
3441 (rst-uncomment-region beg end arg
)
3443 (rst-forward-line-strict 0)
3444 (let ((ind (current-indentation))
3446 (indent-rigidly bol end rst-indent-comment
)
3449 (indent-line-to ind
)
3450 (insert (comment-string-strip comment-start t t
))))))
3452 (defun rst-uncomment-region (beg end
&optional _arg
)
3453 "Uncomment the current region.
3454 Region is from BEG to END. _ARG is ignored"
3457 (rst-forward-line-strict 0)
3458 (let ((bol (point)))
3459 (rst-forward-line-strict 1 end
)
3460 (indent-rigidly (point) end
(- rst-indent-comment
))
3462 (rst-delete-entire-line 0))))
3464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3465 ;; Apply to indented block
3467 ;; FIXME: These next functions should become part of a larger effort to redo
3468 ;; the bullets in bulleted lists. The enumerate would just be one of
3469 ;; the possible outputs.
3471 ;; FIXME: We need to do the enumeration removal as well.
3473 (defun rst-apply-indented-blocks (beg end ind fun
)
3474 "Apply FUN to all lines from BEG to END in blocks indented to IND.
3475 The first indented block starts with the first non-empty line
3476 containing or after BEG and indented to IND. After the first
3477 line the indented block may contain more lines with same
3478 indentation (the paragraph) followed by empty lines and lines
3479 more indented (the sub-blocks). A following line indented to IND
3480 starts the next paragraph. A non-empty line with less
3481 indentation than IND terminates the current paragraph. FUN is
3482 applied to each line like this
3484 (FUN COUNT IN-FIRST IN-SUB IN-SUPER IN-EMPTY RELIND)
3486 COUNT is 0 before the first paragraph and increments for every
3487 paragraph found on level IND. IN-FIRST is non-nil if this is the
3488 first line of such a paragraph. IN-SUB is non-nil if this line
3489 is part of a sub-block while IN-SUPER is non-nil of this line is
3490 part of a less indented block (super-block). IN-EMPTY is non-nil
3491 if this line is empty where an empty line is considered being
3492 part of the previous block. RELIND is nil for an empty line, 0
3493 for a line indented to IND, and the positive or negative number
3494 of columns more or less indented otherwise. When FUN is called
3495 point is immediately behind indentation of that line. FUN may
3496 change everything as long as a marker at END and at the beginning
3497 of the following line is handled correctly by the change. A
3498 non-nil return value from FUN breaks the loop and is returned.
3499 Otherwise return nil."
3500 (let ((endm (copy-marker end t
))
3501 (count 0) ; Before first indented block.
3502 (nxt (when (< beg end
)
3503 (copy-marker beg t
)))
3505 in-sub in-super stop
)
3508 (while (and (not stop
) nxt
)
3510 (goto-char nxt
) nil
)
3511 (setq nxt
(save-excursion
3512 ;; FIXME refactoring: Replace `(forward-line)
3513 ;; (back-to-indentation)` by
3514 ;; `(forward-to-indentation)`
3515 (when (and (rst-forward-line-strict 1 endm
)
3517 (copy-marker (point) t
))))
3518 (back-to-indentation)
3519 (let ((relind (- (current-indentation) ind
))
3520 (in-empty (looking-at (rst-re 'lin-end
)))
3530 (setq in-super nil
))
3531 (t ; Non-empty line in indented block.
3532 (when (or broken in-sub in-super
)
3536 (setq in-super nil
)))
3540 (funcall fun count in-first in-sub in-super in-empty relind
)))
3541 (setq broken in-empty
)))
3542 (set-marker endm nil
)
3545 (defun rst-enumerate-region (beg end all
)
3546 "Add enumeration to all the leftmost paragraphs in the given region.
3547 The region is specified between BEG and END. With ALL,
3548 do all lines instead of just paragraphs."
3549 (interactive "r\nP")
3552 (rst-apply-indented-blocks
3553 beg end
(rst-find-leftmost-column beg end
)
3554 #'(lambda (count in-first in-sub in-super in-empty _relind
)
3562 (let ((tag (format "%d. " (cl-incf enum
))))
3563 (setq indent
(make-string (length tag
) ?
))
3569 ;; FIXME: Does not deal with deeper indentation - although
3570 ;; `rst-apply-indented-blocks' could.
3571 (defun rst-bullet-list-region (beg end all
)
3572 "Add bullets to all the leftmost paragraphs in the given region.
3573 The region is specified between BEG and END. With ALL,
3574 do all lines instead of just paragraphs."
3575 (interactive "r\nP")
3576 (unless rst-preferred-bullets
3577 (error "No preferred bullets defined"))
3578 (let* ((bul (format "%c " (car rst-preferred-bullets
)))
3579 (indent (make-string (length bul
) ?
)))
3580 (rst-apply-indented-blocks
3581 beg end
(rst-find-leftmost-column beg end
)
3582 #'(lambda (count in-first in-sub in-super in-empty _relind
)
3595 ;; FIXME: Does not deal with a varying number of digits appropriately.
3596 ;; FIXME: Does not deal with multiple levels independently.
3597 ;; FIXME: Does not indent a multiline item correctly.
3598 (defun rst-convert-bullets-to-enumeration (beg end
)
3599 "Convert the bulleted and enumerated items in the region to enumerated lists.
3600 Renumber as necessary. Region is from BEG to END."
3605 (dolist (marker (mapcar
3607 (lambda ((pnt &rest clm
))
3609 (rst-find-begs beg end
'itmany-beg-1
)))
3611 (goto-char marker
) nil
)
3612 (looking-at (rst-re 'itmany-beg-1
))
3613 (replace-match (format "%d." count
) nil nil nil
1)
3614 (cl-incf count
))))))
3616 (defun rst-line-block-region (beg end
&optional with-empty
)
3617 "Add line block prefixes for a region.
3618 Region is from BEG to END. With WITH-EMPTY prefix empty lines too."
3619 (interactive "r\nP")
3620 (let ((ind (rst-find-leftmost-column beg end
)))
3621 (rst-apply-indented-blocks
3623 #'(lambda (_count _in-first _in-sub in-super in-empty _relind
)
3624 (when (and (not in-super
) (or with-empty
(not in-empty
)))
3625 (move-to-column ind t
)
3630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3633 (require 'font-lock
)
3635 ;; FIXME: The obsolete variables need to disappear.
3637 ;; The following versions have been done inside Emacs and should not be
3638 ;; replaced by `:package-version' attributes until a change.
3640 (defgroup rst-faces nil
"Faces used in Rst Mode."
3645 (defface rst-block
'((t :inherit font-lock-keyword-face
))
3646 "Face used for all syntax marking up a special block."
3650 (defcustom rst-block-face
'rst-block
3651 "All syntax marking up a special block."
3655 (rst-testcover-defcustom)
3656 (make-obsolete-variable 'rst-block-face
3657 "customize the face `rst-block' instead."
3660 (defface rst-external
'((t :inherit font-lock-type-face
))
3661 "Face used for field names and interpreted text."
3665 (defcustom rst-external-face
'rst-external
3666 "Field names and interpreted text."
3670 (rst-testcover-defcustom)
3671 (make-obsolete-variable 'rst-external-face
3672 "customize the face `rst-external' instead."
3675 (defface rst-definition
'((t :inherit font-lock-function-name-face
))
3676 "Face used for all other defining constructs."
3680 (defcustom rst-definition-face
'rst-definition
3681 "All other defining constructs."
3685 (rst-testcover-defcustom)
3686 (make-obsolete-variable 'rst-definition-face
3687 "customize the face `rst-definition' instead."
3690 ;; XEmacs compatibility (?).
3691 (defface rst-directive
(if (boundp 'font-lock-builtin-face
)
3692 '((t :inherit font-lock-builtin-face
))
3693 '((t :inherit font-lock-preprocessor-face
)))
3694 "Face used for directives and roles."
3698 (defcustom rst-directive-face
'rst-directive
3699 "Directives and roles."
3702 (rst-testcover-defcustom)
3703 (make-obsolete-variable 'rst-directive-face
3704 "customize the face `rst-directive' instead."
3707 (defface rst-comment
'((t :inherit font-lock-comment-face
))
3708 "Face used for comments."
3712 (defcustom rst-comment-face
'rst-comment
3717 (rst-testcover-defcustom)
3718 (make-obsolete-variable 'rst-comment-face
3719 "customize the face `rst-comment' instead."
3722 (defface rst-emphasis1
'((t :inherit italic
))
3723 "Face used for simple emphasis."
3727 (defcustom rst-emphasis1-face
'rst-emphasis1
3732 (rst-testcover-defcustom)
3733 (make-obsolete-variable 'rst-emphasis1-face
3734 "customize the face `rst-emphasis1' instead."
3737 (defface rst-emphasis2
'((t :inherit bold
))
3738 "Face used for double emphasis."
3742 (defcustom rst-emphasis2-face
'rst-emphasis2
3746 (rst-testcover-defcustom)
3747 (make-obsolete-variable 'rst-emphasis2-face
3748 "customize the face `rst-emphasis2' instead."
3751 (defface rst-literal
'((t :inherit font-lock-string-face
))
3752 "Face used for literal text."
3756 (defcustom rst-literal-face
'rst-literal
3761 (rst-testcover-defcustom)
3762 (make-obsolete-variable 'rst-literal-face
3763 "customize the face `rst-literal' instead."
3766 (defface rst-reference
'((t :inherit font-lock-variable-name-face
))
3767 "Face used for references to a definition."
3771 (defcustom rst-reference-face
'rst-reference
3772 "References to a definition."
3776 (rst-testcover-defcustom)
3777 (make-obsolete-variable 'rst-reference-face
3778 "customize the face `rst-reference' instead."
3781 (defface rst-transition
'((t :inherit font-lock-keyword-face
))
3782 "Face used for a transition."
3783 :package-version
'(rst .
"1.3.0")
3786 (defface rst-adornment
'((t :inherit font-lock-keyword-face
))
3787 "Face used for the adornment of a section header."
3788 :package-version
'(rst .
"1.3.0")
3791 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3793 (dolist (var '(rst-level-face-max rst-level-face-base-color
3794 rst-level-face-base-light
3795 rst-level-face-format-light
3796 rst-level-face-step-light
3803 (make-obsolete-variable var
"customize the faces `rst-level-*' instead."
3806 ;; Define faces for the first 6 levels. More levels are possible, however.
3807 (defface rst-level-1
'((((background light
)) (:background
"grey85"))
3808 (((background dark
)) (:background
"grey15")))
3809 "Default face for section title text at level 1."
3810 :package-version
'(rst .
"1.4.0"))
3812 (defface rst-level-2
'((((background light
)) (:background
"grey78"))
3813 (((background dark
)) (:background
"grey22")))
3814 "Default face for section title text at level 2."
3815 :package-version
'(rst .
"1.4.0"))
3817 (defface rst-level-3
'((((background light
)) (:background
"grey71"))
3818 (((background dark
)) (:background
"grey29")))
3819 "Default face for section title text at level 3."
3820 :package-version
'(rst .
"1.4.0"))
3822 (defface rst-level-4
'((((background light
)) (:background
"grey64"))
3823 (((background dark
)) (:background
"grey36")))
3824 "Default face for section title text at level 4."
3825 :package-version
'(rst .
"1.4.0"))
3827 (defface rst-level-5
'((((background light
)) (:background
"grey57"))
3828 (((background dark
)) (:background
"grey43")))
3829 "Default face for section title text at level 5."
3830 :package-version
'(rst .
"1.4.0"))
3832 (defface rst-level-6
'((((background light
)) (:background
"grey50"))
3833 (((background dark
)) (:background
"grey50")))
3834 "Default face for section title text at level 6."
3835 :package-version
'(rst .
"1.4.0"))
3837 (defcustom rst-adornment-faces-alist
3838 '((t . rst-transition
)
3839 (nil . rst-adornment
)
3846 "Faces for the various adornment types.
3847 Key is a number (for the section title text of that level
3848 starting with 1), t (for transitions) or nil (for section title
3849 adornment). If you need levels beyond 6 you have to define faces
3855 (integer :tag
"Section level")
3856 (const :tag
"transitions" t
)
3857 (const :tag
"section title adornment" nil
))
3858 :value-type
(face)))
3859 (rst-testcover-defcustom)
3861 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3863 (defvar rst-font-lock-keywords
3864 ;; The reST-links in the comments below all relate to sections in
3865 ;; http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html.
3866 `(;; FIXME: Block markup is not recognized in blocks after explicit markup
3869 ;; Simple `Body Elements`_
3871 ;; FIXME: A bullet directly after a field name is not recognized.
3872 (,(rst-re 'lin-beg
'(:grp bul-sta
))
3874 ;; `Enumerated Lists`_
3875 (,(rst-re 'lin-beg
'(:grp enmany-sta
))
3877 ;; `Definition Lists`_
3880 (,(rst-re 'lin-beg
'(:grp fld-tag
) 'bli-sfx
)
3881 1 rst-external-face
)
3883 (,(rst-re 'lin-beg
'(:grp opt-tag
(:shy optsep-tag opt-tag
) "*")
3884 '(:alt
"$" (:seq hws-prt
"\\{2\\}")))
3887 ;; Only for lines containing no more bar - to distinguish from tables.
3888 (,(rst-re 'lin-beg
'(:grp
"|" bli-sfx
) "[^|\n]*$")
3894 ;; All the `Explicit Markup Blocks`_
3895 ;; `Footnotes`_ / `Citations`_
3896 (,(rst-re 'lin-beg
'fnc-sta-2
)
3897 (1 rst-definition-face
)
3898 (2 rst-definition-face
))
3899 ;; `Directives`_ / `Substitution Definitions`_
3900 (,(rst-re 'lin-beg
'dir-sta-3
)
3901 (1 rst-directive-face
)
3902 (2 rst-definition-face
)
3903 (3 rst-directive-face
))
3904 ;; `Hyperlink Targets`_
3906 '(:grp exm-sta
"_" (:alt
3907 (:seq
"`" ilcbkqdef-tag
"`")
3908 (:seq
(:alt
"[^:\\\n]" "\\\\.") "+")) ":")
3910 1 rst-definition-face
)
3911 (,(rst-re 'lin-beg
'(:grp
"__") 'bli-sfx
)
3912 1 rst-definition-face
)
3914 ;; All `Inline Markup`_
3915 ;; Most of them may be multiline though this is uninteresting.
3917 ;; FIXME: Condition 5 preventing fontification of e.g. "*" not implemented
3918 ;; `Strong Emphasis`_.
3919 (,(rst-re 'ilm-pfx
'(:grp
"\\*\\*" ilcast-tag
"\\*\\*") 'ilm-sfx
)
3920 1 rst-emphasis2-face
)
3922 (,(rst-re 'ilm-pfx
'(:grp
"\\*" ilcast-tag
"\\*") 'ilm-sfx
)
3923 1 rst-emphasis1-face
)
3924 ;; `Inline Literals`_
3925 (,(rst-re 'ilm-pfx
'(:grp
"``" ilcbkq-tag
"``") 'ilm-sfx
)
3927 ;; `Inline Internal Targets`_
3928 (,(rst-re 'ilm-pfx
'(:grp
"_`" ilcbkq-tag
"`") 'ilm-sfx
)
3929 1 rst-definition-face
)
3930 ;; `Hyperlink References`_
3931 ;; FIXME: `Embedded URIs and Aliases`_ not considered.
3932 ;; FIXME: Directly adjacent marked up words are not fontified correctly
3933 ;; unless they are not separated by two spaces: foo_ bar_.
3934 (,(rst-re 'ilm-pfx
'(:grp
(:alt
(:seq
"`" ilcbkq-tag
"`")
3935 (:seq
"\\sw" (:alt
"\\sw" "-") "+\\sw"))
3937 1 rst-reference-face
)
3938 ;; `Interpreted Text`_
3939 (,(rst-re 'ilm-pfx
'(:grp
(:shy
":" sym-tag
":") "?")
3940 '(:grp
"`" ilcbkq-tag
"`")
3941 '(:grp
(:shy
":" sym-tag
":") "?") 'ilm-sfx
)
3942 (1 rst-directive-face
)
3943 (2 rst-external-face
)
3944 (3 rst-directive-face
))
3945 ;; `Footnote References`_ / `Citation References`_
3946 (,(rst-re 'ilm-pfx
'(:grp fnc-tag
"_") 'ilm-sfx
)
3947 1 rst-reference-face
)
3948 ;; `Substitution References`_
3949 ;; FIXME: References substitutions like |this|_ or |this|__ are not
3950 ;; fontified correctly.
3951 (,(rst-re 'ilm-pfx
'(:grp sub-tag
) 'ilm-sfx
)
3952 1 rst-reference-face
)
3953 ;; `Standalone Hyperlinks`_
3954 ;; FIXME: This takes it easy by using a whitespace as delimiter.
3955 (,(rst-re 'ilm-pfx
'(:grp uri-tag
":\\S +") 'ilm-sfx
)
3956 1 rst-definition-face
)
3957 (,(rst-re 'ilm-pfx
'(:grp sym-tag
"@" sym-tag
) 'ilm-sfx
)
3958 1 rst-definition-face
)
3960 ;; Do all block fontification as late as possible so 'append works.
3962 ;; Sections_ / Transitions_
3963 ;; For sections this is multiline.
3964 (,(rst-re 'ado-beg-2-1
)
3965 (rst-font-lock-handle-adornment-matcher
3966 (rst-font-lock-handle-adornment-pre-match-form
3967 (match-string-no-properties 1) (match-end 1))
3969 (1 (cdr (assoc nil rst-adornment-faces-alist
)) append t
)
3970 (2 (cdr (assoc rst-font-lock-adornment-level
3971 rst-adornment-faces-alist
)) append t
)
3972 (3 (cdr (assoc nil rst-adornment-faces-alist
)) append t
)))
3974 ;; FIXME: FACESPEC could be used instead of ordinary faces to set
3975 ;; properties on comments and literal blocks so they are *not*
3976 ;; inline fontified. See (elisp)Search-based Fontification.
3978 ;; FIXME: And / or use `syntax-propertize' functions as in `octave-mod.el'
3979 ;; and other V24 modes. May make `font-lock-extend-region'
3983 ;; This is multiline.
3984 (,(rst-re 'lin-beg
'cmt-sta-1
)
3985 (1 rst-comment-face
)
3986 (rst-font-lock-find-unindented-line-match
3987 (rst-font-lock-find-unindented-line-limit (match-end 1))
3989 (0 rst-comment-face append
)))
3990 (,(rst-re 'lin-beg
'(:grp exm-tag
) '(:grp hws-tag
) "$")
3991 (1 rst-comment-face
)
3992 (2 rst-comment-face
)
3993 (rst-font-lock-find-unindented-line-match
3994 (rst-font-lock-find-unindented-line-limit 'next
)
3996 (0 rst-comment-face append
)))
3998 ;; FIXME: This is not rendered as comment::
3999 ;; .. .. list-table::
4003 ;; FIXME: This is rendered wrong::
4007 ;; ----|> KKKKK <|----
4009 ;; -|> AAAAAAAAAAPPPPPP <|- -|> AAAAAAAAAABBBBBBB <|-
4012 ;; PPPPPP PPPPPPDDDDDDD BBBBBBB PPPPPPBBBBBBB
4014 ;; Indentation needs to be taken from the line with the ``::`` and not from
4015 ;; the first content line.
4017 ;; `Indented Literal Blocks`_
4018 ;; This is multiline.
4019 (,(rst-re 'lin-beg
'lit-sta-2
)
4021 (rst-font-lock-find-unindented-line-match
4022 (rst-font-lock-find-unindented-line-limit t
)
4024 (0 rst-literal-face append
)))
4026 ;; FIXME: `Quoted Literal Blocks`_ missing.
4027 ;; This is multiline.
4029 ;; `Doctest Blocks`_
4030 ;; FIXME: This is wrong according to the specification:
4032 ;; Doctest blocks are text blocks which begin with ">>> ", the Python
4033 ;; interactive interpreter main prompt, and end with a blank line.
4034 ;; Doctest blocks are treated as a special case of literal blocks,
4035 ;; without requiring the literal block syntax. If both are present, the
4036 ;; literal block syntax takes priority over Doctest block syntax:
4038 ;; This is an ordinary paragraph.
4040 ;; >>> print 'this is a Doctest block'
4041 ;; this is a Doctest block
4043 ;; The following is a literal block::
4045 ;; >>> This is not recognized as a doctest block by
4046 ;; reStructuredText. It *will* be recognized by the doctest
4049 ;; Indentation is not required for doctest blocks.
4050 (,(rst-re 'lin-beg
'(:grp
(:alt
">>>" ell-tag
)) '(:grp
".+"))
4052 (2 rst-literal-face
)))
4053 "Keywords to highlight in rst mode.")
4055 (defvar font-lock-beg
)
4056 (defvar font-lock-end
)
4058 (defun rst-font-lock-extend-region ()
4059 "Extend the font-lock region if it might be in a multi-line construct.
4060 Return non-nil if so. Font-lock region is from `font-lock-beg'
4061 to `font-lock-end'."
4062 (let ((r (rst-font-lock-extend-region-internal font-lock-beg font-lock-end
)))
4064 (setq font-lock-beg
(car r
))
4065 (setq font-lock-end
(cdr r
))
4068 (defun rst-font-lock-extend-region-internal (beg end
)
4069 "Check the region BEG / END for being in the middle of a multi-line construct.
4070 Return nil if not or a cons with new values for BEG / END"
4071 (let ((nbeg (rst-font-lock-extend-region-extend beg -
1))
4072 (nend (rst-font-lock-extend-region-extend end
1)))
4074 (cons (or nbeg beg
) (or nend end
)))))
4076 ;; FIXME refactoring: Use `rst-forward-line-strict' instead.
4077 (defun rst-forward-line (&optional n
)
4078 "Like `forward-line' but always end up in column 0 and return accordingly.
4079 Move N lines forward just as `forward-line'."
4080 (let ((left (forward-line n
)))
4083 ;; FIXME: This may move back for positive n - is this desired?
4085 (- left
(cl-signum n
)))))
4087 ;; FIXME: If a single line is made a section header by `rst-adjust' the header
4088 ;; is not always fontified immediately.
4089 (defun rst-font-lock-extend-region-extend (pt dir
)
4090 "Extend the region starting at point PT and extending in direction DIR.
4091 Return extended point or nil if not moved."
4092 ;; There are many potential multiline constructs but there are two groups
4093 ;; which are really relevant. The first group consists of
4095 ;; * comment lines without leading explicit markup tag and
4097 ;; * literal blocks following "::"
4099 ;; which are both indented. Thus indentation is the first thing recognized
4100 ;; here. The second criteria is an explicit markup tag which may be a comment
4101 ;; or a double colon at the end of a line.
4103 ;; The second group consists of the adornment cases.
4104 (if (not (get-text-property pt
'font-lock-multiline
))
4105 ;; Move only if we don't start inside a multiline construct already.
4108 (let ( ; Non-empty non-indented line, explicit markup tag or literal
4110 (stop-re (rst-re '(:alt
"[^ \t\n]"
4111 (:seq hws-tag exm-tag
)
4112 (:seq
".*" dcl-tag lin-end
)))))
4113 ;; The comments below are for dir == -1 / dir == 1.
4115 (rst-forward-line-strict 0)
4117 (while (and (not (looking-at stop-re
))
4118 (zerop (rst-forward-line dir
)))) ; try previous / next
4119 ; line if it exists.
4120 (if (looking-at (rst-re 'ado-beg-2-1
)) ; may be an underline /
4122 (if (zerop (rst-forward-line dir
))
4123 (if (looking-at (rst-re 'ttl-beg-1
)) ; title found, i.e.
4124 ; underline / overline
4126 (if (zerop (rst-forward-line dir
))
4128 (looking-at (rst-re 'ado-beg-2-1
))) ; no
4132 (rst-forward-line (- dir
)))) ; step back to
4135 (if (< dir
0) ; keep downward adornment.
4136 (rst-forward-line (- dir
))))) ; step back to adornment.
4137 (if (looking-at (rst-re 'ttl-beg-1
)) ; may be a title.
4138 (if (zerop (rst-forward-line dir
))
4140 (looking-at (rst-re 'ado-beg-2-1
))) ; no overline /
4142 (rst-forward-line (- dir
)))))) ; step back to line.
4143 (if (not (= (point) pt
))
4146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4149 (defun rst-forward-indented-block (&optional column limit
)
4151 "Move forward across one indented block.
4152 Find the next (i.e. excluding the current line) non-empty line
4153 which is not indented at least to COLUMN (defaults to the column
4154 of the point). Move point to first character of this line or the
4155 first of the empty lines immediately before it and return that
4156 position. If there is no such line before LIMIT (defaults to the
4157 end of the buffer) return nil and do not move point."
4158 (let (fnd candidate
)
4159 (setq fnd
(rst-apply-indented-blocks
4160 (line-beginning-position 2) ; Skip the current line
4161 (or limit
(point-max)) (or column
(current-column))
4162 #'(lambda (_count _in-first _in-sub in-super in-empty _relind
)
4165 (setq candidate
(or candidate
(line-beginning-position)))
4168 (or candidate
(line-beginning-position)))
4169 (t ; Non-empty, same or more indented line.
4170 (setq candidate nil
)
4175 (defvar rst-font-lock-find-unindented-line-begin nil
4176 "Beginning of the match if `rst-font-lock-find-unindented-line-end'.")
4178 (defvar rst-font-lock-find-unindented-line-end nil
4179 "End of the match as determined by `rst-font-lock-find-unindented-line-limit'.
4180 Also used as a trigger for `rst-font-lock-find-unindented-line-match'.")
4182 (defun rst-font-lock-find-unindented-line-limit (ind-pnt)
4183 "Find the next unindented line relative to indentation at IND-PNT.
4184 Return this point, the end of the buffer or nil if nothing found.
4185 If IND-PNT is `next' take the indentation from the next line if
4186 this is not empty and indented more than the current one. If
4187 IND-PNT is non-nil but not a number take the indentation from the
4188 next non-empty line if this is indented more than the current one."
4189 (setq rst-font-lock-find-unindented-line-begin ind-pnt
)
4190 (setq rst-font-lock-find-unindented-line-end
4193 (when (not (numberp ind-pnt
))
4194 ;; Find indentation point in next line if any.
4196 ;; FIXME: Should be refactored to two different functions
4197 ;; giving their result to this function, may be
4198 ;; integrated in caller.
4200 (let ((cur-ind (current-indentation)))
4201 (if (eq ind-pnt
'next
)
4202 (when (and (rst-forward-line-strict 1 (point-max))
4203 (< (point) (point-max)))
4205 (setq rst-font-lock-find-unindented-line-begin
4207 (when (and (not (looking-at (rst-re 'lin-end
)))
4208 (> (current-indentation) cur-ind
))
4209 ;; Use end of indentation if non-empty line.
4210 (looking-at (rst-re 'hws-tag
))
4212 ;; Skip until non-empty line or EOF.
4213 (while (and (rst-forward-line-strict 1 (point-max))
4214 (< (point) (point-max))
4215 (looking-at (rst-re 'lin-end
))))
4216 (when (< (point) (point-max))
4218 (setq rst-font-lock-find-unindented-line-begin
4220 (when (> (current-indentation) cur-ind
)
4221 ;; Indentation bigger than line of departure.
4222 (looking-at (rst-re 'hws-tag
))
4223 (match-end 0))))))))
4226 (or (rst-forward-indented-block nil
(point-max))
4229 (defun rst-font-lock-find-unindented-line-match (_limit)
4230 "Set the match found earlier if match were found.
4231 Match has been found by `rst-font-lock-find-unindented-line-limit'
4232 the first time called or no match is found. Return non-nil if
4233 match was found. _LIMIT is not used but mandated by the caller."
4234 (when rst-font-lock-find-unindented-line-end
4236 (list rst-font-lock-find-unindented-line-begin
4237 rst-font-lock-find-unindented-line-end
))
4238 (put-text-property rst-font-lock-find-unindented-line-begin
4239 rst-font-lock-find-unindented-line-end
4240 'font-lock-multiline t
)
4241 ;; Make sure this is called only once.
4242 (setq rst-font-lock-find-unindented-line-end nil
)
4245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4248 (defvar rst-font-lock-adornment-level nil
4249 "Storage for `rst-font-lock-handle-adornment-matcher'.
4250 Either section level of the current adornment or t for a transition.")
4252 (defun rst-adornment-level (ado)
4253 "Return section level for ADO or t for a transition.
4254 If ADO is found in the hierarchy return its level. Otherwise
4255 return a level one beyond the existing hierarchy."
4256 (if (rst-Ado-is-transition ado
)
4258 (let ((hier (rst-Hdr-ado-map (rst-hdr-hierarchy))))
4259 (1+ (or (rst-Ado-position ado hier
)
4262 (defvar rst-font-lock-adornment-match nil
4263 "Storage for match for current adornment.
4264 Set by `rst-font-lock-handle-adornment-pre-match-form'. Also used
4265 as a trigger for `rst-font-lock-handle-adornment-matcher'.")
4267 (defun rst-font-lock-handle-adornment-pre-match-form (ado ado-end
)
4268 "Determine limit for adornments.
4269 Determine all things necessary for font-locking section titles
4270 and transitions and put the result to `rst-font-lock-adornment-match'
4271 and `rst-font-lock-adornment-level'. ADO is the complete adornment
4272 matched. ADO-END is the point where ADO ends. Return the point
4273 where the whole adorned construct ends.
4275 Called as a PRE-MATCH-FORM in the sense of `font-lock-keywords'."
4276 (let ((ttl (rst-classify-adornment ado ado-end
)))
4278 (setq rst-font-lock-adornment-level nil
4279 rst-font-lock-adornment-match nil
)
4280 (setq rst-font-lock-adornment-level
4281 (rst-adornment-level (rst-Ttl-ado ttl
)))
4282 (setq rst-font-lock-adornment-match
(rst-Ttl-match ttl
))
4283 (goto-char (rst-Ttl-get-beginning ttl
))
4284 (rst-Ttl-get-end ttl
))))
4286 (defun rst-font-lock-handle-adornment-matcher (_limit)
4287 "Set the match found earlier if match were found.
4288 Match has been found by
4289 `rst-font-lock-handle-adornment-pre-match-form' the first time
4290 called or no match is found. Return non-nil if match was found.
4292 Called as a MATCHER in the sense of `font-lock-keywords'.
4293 _LIMIT is not used but mandated by the caller."
4294 (let ((match rst-font-lock-adornment-match
))
4295 ;; May run only once - enforce this.
4296 (setq rst-font-lock-adornment-match nil
)
4298 (set-match-data match
)
4299 (goto-char (match-end 0))
4300 (put-text-property (match-beginning 0) (match-end 0)
4301 'font-lock-multiline t
)
4305 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4308 (defgroup rst-compile nil
4309 "Settings for support of conversion of reStructuredText
4310 document with \\[rst-compile]."
4314 (defcustom rst-compile-toolsets
4315 `((html ,(if (executable-find "rst2html.py") "rst2html.py" "rst2html")
4317 (latex ,(if (executable-find "rst2latex.py") "rst2latex.py" "rst2latex")
4319 (newlatex ,(if (executable-find "rst2newlatex.py") "rst2newlatex.py"
4322 (pseudoxml ,(if (executable-find "rst2pseudoxml.py") "rst2pseudoxml.py"
4325 (xml ,(if (executable-find "rst2xml.py") "rst2xml.py" "rst2xml")
4327 (pdf ,(if (executable-find "rst2pdf.py") "rst2pdf.py" "rst2pdf")
4329 (s5 ,(if (executable-find "rst2s5.py") "rst2s5.py" "rst2s5")
4331 ;; FIXME: Add at least those converters officially supported like `rst2odt'
4333 ;; FIXME: To make this really useful there should be a generic command the
4334 ;; user gives one of the symbols and this way select the conversion to
4335 ;; run. This should replace the toolset stuff somehow.
4336 ;; FIXME: Allow a template for the conversion command so `rst2pdf ... -o ...'
4337 ;; can be supported.
4338 "Table describing the command to use for each tool-set.
4339 An association list of the tool-set to a list of the (command to use,
4340 extension of produced filename, options to the tool (nil or a
4341 string)) to be used for converting the document."
4342 ;; FIXME: These are not options but symbols which may be referenced by
4343 ;; `rst-compile-*-toolset` below. The `:validate' keyword of
4344 ;; `defcustom' may help to define this properly in newer Emacs
4345 ;; versions (> 23.1).
4346 :type
'(alist :options
(html latex newlatex pseudoxml xml pdf s5
)
4348 :value-type
(list :tag
"Specification"
4349 (file :tag
"Command")
4350 (string :tag
"File extension")
4351 (choice :tag
"Command options"
4352 (const :tag
"No options" nil
)
4353 (string :tag
"Options"))))
4355 :package-version
"1.2.0")
4356 (rst-testcover-defcustom)
4358 ;; FIXME: Must be defcustom.
4359 (defvar rst-compile-primary-toolset
'html
4360 "The default tool-set for `rst-compile'.")
4362 ;; FIXME: Must be defcustom.
4363 (defvar rst-compile-secondary-toolset
'latex
4364 "The default tool-set for `rst-compile' with a prefix argument.")
4366 (defun rst-compile-find-conf ()
4367 "Look for the configuration file in the parents of the current path."
4369 (let ((file-name "docutils.conf")
4370 (buffer-file (buffer-file-name)))
4371 ;; Move up in the dir hierarchy till we find a change log file.
4372 (let* ((dir (file-name-directory buffer-file
))
4374 (while (and (or (not (string= dir prevdir
))
4377 (not (file-exists-p (concat dir file-name
))))
4378 ;; Move up to the parent dir and try again.
4380 (setq dir
(expand-file-name (file-name-directory
4381 (directory-file-name
4382 (file-name-directory dir
))))))
4383 (or (and dir
(concat dir file-name
)) nil
))))
4387 (defun rst-compile (&optional use-alt
)
4388 "Compile command to convert reST document into some output file.
4389 Attempts to find configuration file, if it can, overrides the
4390 options. There are two commands to choose from; with USE-ALT,
4391 select the alternative tool-set."
4393 ;; Note: maybe we want to check if there is a Makefile too and not do anything
4394 ;; if that is the case. I dunno.
4395 (cl-destructuring-bind
4396 (command extension options
4397 &aux
(conffile (rst-compile-find-conf))
4398 (bufname (file-name-nondirectory buffer-file-name
)))
4399 (cdr (assq (if use-alt
4400 rst-compile-secondary-toolset
4401 rst-compile-primary-toolset
)
4402 rst-compile-toolsets
))
4403 ;; Set compile-command before invocation of compile.
4411 (concat "--config=" (shell-quote-argument conffile
))
4413 (shell-quote-argument bufname
)
4414 (shell-quote-argument (concat (file-name-sans-extension bufname
)
4417 ;; Invoke the compile command.
4418 (if (or compilation-read-command use-alt
)
4419 (call-interactively #'compile
)
4420 (compile compile-command
))))
4422 (defun rst-compile-alt-toolset ()
4423 "Compile command with the alternative tool-set."
4427 (defun rst-compile-pseudo-region ()
4428 "Show pseudo-XML rendering.
4429 Rendering is done of the current active region, or of the entire
4430 buffer, if the region is not selected."
4431 ;; FIXME: The region should be given interactively.
4433 (with-output-to-temp-buffer "*pseudoxml*"
4434 (shell-command-on-region
4435 (if mark-active
(region-beginning) (point-min))
4436 (if mark-active
(region-end) (point-max))
4437 (cadr (assq 'pseudoxml rst-compile-toolsets
))
4440 ;; FIXME: Should be integrated in `rst-compile-toolsets'.
4441 (defvar rst-pdf-program
"xpdf"
4442 "Program used to preview PDF files.")
4444 (defun rst-compile-pdf-preview ()
4445 "Convert the document to a PDF file and launch a preview program."
4447 (let* ((tmp-filename (make-temp-file "rst_el" nil
".pdf"))
4448 (command (format "%s %s %s && %s %s ; rm %s"
4449 (cadr (assq 'pdf rst-compile-toolsets
))
4450 buffer-file-name tmp-filename
4451 rst-pdf-program tmp-filename tmp-filename
)))
4452 (start-process-shell-command "rst-pdf-preview" nil command
)
4453 ;; Note: you could also use (compile command) to view the compilation
4457 ;; FIXME: Should be integrated in `rst-compile-toolsets' defaulting to
4458 ;; something like `browse-url'.
4459 (defvar rst-slides-program
"firefox"
4460 "Program used to preview S5 slides.")
4462 (defun rst-compile-slides-preview ()
4463 "Convert the document to an S5 slide presentation and launch a preview program."
4465 (let* ((tmp-filename (make-temp-file "rst_el" nil
".html"))
4466 (command (format "%s %s %s && %s %s ; rm %s"
4467 (cadr (assq 's5 rst-compile-toolsets
))
4468 buffer-file-name tmp-filename
4469 rst-slides-program tmp-filename tmp-filename
)))
4470 (start-process-shell-command "rst-slides-preview" nil command
)
4471 ;; Note: you could also use (compile command) to view the compilation
4475 ;; FIXME: Add `rst-compile-html-preview'.
4477 ;; FIXME: Add support for `restview` (http://mg.pov.lt/restview/). May be a
4478 ;; more general facility for calling commands on a reST file would make
4482 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4485 ;; FIXME: Consider a key binding. A key binding needs to definitely switch on
4486 ;; `which-func-mode' - i.e. `which-func-modes' must be set properly.
4488 ;; Based on ideas from Masatake YAMATO <yamato@redhat.com>.
4490 (defun rst-imenu-convert-cell (stn)
4491 "Convert a STN to an Imenu index node and return it."
4492 (let ((ttl (rst-Stn-ttl stn
))
4493 (children (rst-Stn-children stn
))
4494 (pos (rst-Stn-get-title-beginning stn
))
4495 (txt (rst-Stn-get-text stn
""))
4500 (let ((hdr (rst-Ttl-hdr ttl
)))
4501 (setq pfx
(char-to-string (rst-Hdr-get-char hdr
)))
4502 (when (rst-Hdr-is-over-and-under hdr
)
4504 ;; FIXME: Overline adornment characters need to be in front so they
4505 ;; become visible even for long title lines. May be an additional
4506 ;; level number is also useful.
4507 (setq name
(format "%s%s%s" pfx txt sfx
))
4508 (cons name
; The name of the entry.
4510 (cons ; The entry has a submenu.
4511 (cons name pos
) ; The entry itself.
4512 (mapcar #'rst-imenu-convert-cell children
)) ; The children.
4513 pos
)))) ; The position of a plain entry.
4515 ;; FIXME: Document title and subtitle need to be handled properly. They should
4516 ;; get an own "Document" top level entry.
4517 (defun rst-imenu-create-index ()
4518 "Create index for Imenu.
4519 Return as described for `imenu--index-alist'."
4520 (rst-reset-section-caches)
4521 (let ((root (rst-all-stn)))
4523 (mapcar #'rst-imenu-convert-cell
(rst-Stn-children root
)))))
4526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4527 ;; Convenience functions
4529 ;; FIXME: Unbound command - should be bound or removed.
4530 (defun rst-replace-lines (fromchar tochar
)
4531 "Replace flush-left lines of FROMCHAR with equal-length lines of TOCHAR."
4533 cSearch for flush-left lines of char:
4534 cand replace with char: ")
4536 (let ((searchre (rst-re "^" fromchar
"+\\( *\\)$"))
4538 (while (search-forward-regexp searchre nil t
)
4539 (setq found
(1+ found
))
4540 (goto-char (match-beginning 1))
4541 (let ((width (current-column)))
4542 (rst-delete-entire-line 0)
4543 (insert-char tochar width
)))
4544 (message "%d lines replaced." found
))))
4546 ;; FIXME: Unbound command - should be bound or removed.
4547 (defun rst-join-paragraph ()
4548 "Join lines in current paragraph into one line, removing end-of-lines."
4550 (let ((fill-column 65000)) ; Some big number.
4551 (call-interactively #'fill-paragraph
)))
4553 ;; FIXME: Unbound command - should be bound or removed.
4554 (defun rst-force-fill-paragraph ()
4555 "Fill paragraph at point, first joining the paragraph's lines into one.
4556 This is useful for filling list item paragraphs."
4558 (rst-join-paragraph)
4559 (fill-paragraph nil
))
4562 ;; FIXME: Unbound command - should be bound or removed.
4563 ;; Generic character repeater function.
4564 ;; For sections, better to use the specialized function above, but this can
4565 ;; be useful for creating separators.
4566 (defun rst-repeat-last-character (use-next)
4567 "Fill the current line using the last character on the current line.
4568 Fill up to the length of the preceding line or up to `fill-column' if preceding
4571 If USE-NEXT, use the next line rather than the preceding line.
4573 If the current line is longer than the desired length, shave the characters off
4574 the current line to fit the desired length.
4576 As an added convenience, if the command is repeated immediately, the alternative
4577 column is used (fill-column vs. end of previous/next line)."
4579 (let* ((curcol (current-column))
4580 (curline (+ (count-lines (point-min) (point))
4581 (if (zerop curcol
) 1 0)))
4582 (lbp (line-beginning-position 0))
4583 (prevcol (if (and (= curline
1) (not use-next
))
4586 (forward-line (if use-next
1 -
1))
4588 (skip-chars-backward " \t" lbp
)
4589 (let ((cc (current-column)))
4590 (if (zerop cc
) fill-column cc
)))))
4592 (cond ((equal last-command
'rst-repeat-last-character
)
4593 (if (= curcol fill-column
) prevcol fill-column
))
4595 (if (zerop prevcol
) fill-column prevcol
))))))
4597 (if (> (current-column) rightmost-column
)
4598 ;; Shave characters off the end.
4599 (delete-region (- (point)
4600 (- (current-column) rightmost-column
))
4602 ;; Fill with last characters.
4603 (insert-char (preceding-char)
4604 (- rightmost-column
(current-column))))))
4608 ;; LocalWords: docutils http sourceforge rst html wp svn svnroot txt reST regex
4609 ;; LocalWords: regexes alist seq alt grp keymap abbrev overline overlines toc
4610 ;; LocalWords: XML PNT propertized init referenceable
4615 ;; sentence-end-double-space: t
4618 ;;; rst.el ends here