Commit | Line | Data |
---|---|---|
94e9c286 SM |
1 | ;;; rst.el --- Mode for viewing and editing reStructuredText-documents. |
2 | ||
acaf905b | 3 | ;; Copyright (C) 2003-2012 Free Software Foundation, Inc. |
94e9c286 | 4 | |
d13c8be6 | 5 | ;; Maintainer: Stefan Merten <smerten@oekonux.de> |
6d3f7c2f SM |
6 | ;; Author: Stefan Merten <smerten@oekonux.de>, |
7 | ;; Martin Blais <blais@furius.ca>, | |
d13c8be6 SM |
8 | ;; David Goodger <goodger@python.org>, |
9 | ;; Wei-Wei Guo <wwguocn@gmail.com> | |
94e9c286 SM |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
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/>. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
d13c8be6 | 28 | ;; This package provides major mode rst-mode, which supports documents marked |
6d3f7c2f SM |
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: | |
94e9c286 SM |
33 | ;; |
34 | ;; - Functions to automatically adjust and cycle the section underline | |
d13c8be6 | 35 | ;; adornments; |
94e9c286 SM |
36 | ;; - A mode that displays the table of contents and allows you to jump anywhere |
37 | ;; from it; | |
38 | ;; - Functions to insert and automatically update a TOC in your source | |
39 | ;; document; | |
d13c8be6 SM |
40 | ;; - Function to insert list, processing item bullets and enumerations |
41 | ;; automatically; | |
42 | ;; - Font-lock highlighting of most reStructuredText structures; | |
43 | ;; - Indentation and filling according to reStructuredText syntax; | |
44 | ;; - Cursor movement according to reStructuredText syntax; | |
94e9c286 SM |
45 | ;; - Some other convenience functions. |
46 | ;; | |
47 | ;; See the accompanying document in the docutils documentation about | |
48 | ;; the contents of this package and how to use it. | |
49 | ;; | |
50 | ;; For more information about reStructuredText, see | |
51 | ;; http://docutils.sourceforge.net/rst.html | |
52 | ;; | |
53 | ;; For full details on how to use the contents of this file, see | |
54 | ;; http://docutils.sourceforge.net/docs/user/emacs.html | |
55 | ;; | |
56 | ;; | |
6d3f7c2f | 57 | ;; There are a number of convenient key bindings provided by rst-mode. |
b4747519 | 58 | ;; For more on bindings, see rst-mode-map below. There are also many variables |
d13c8be6 | 59 | ;; that can be customized, look for defcustom in this file. |
94e9c286 SM |
60 | ;; |
61 | ;; If you use the table-of-contents feature, you may want to add a hook to | |
6d3f7c2f | 62 | ;; update the TOC automatically every time you adjust a section title:: |
94e9c286 SM |
63 | ;; |
64 | ;; (add-hook 'rst-adjust-hook 'rst-toc-update) | |
65 | ;; | |
b4747519 SM |
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:: | |
94e9c286 SM |
68 | ;; |
69 | ;; (setq font-lock-global-modes '(not rst-mode ...)) | |
70 | ;; | |
94e9c286 | 71 | ;; |
94e9c286 | 72 | ;; |
d13c8be6 | 73 | ;; Customization is done by customizable variables contained in customization |
6d3f7c2f | 74 | ;; group "rst" and subgroups. Group "rst" is contained in the "wp" group. |
94e9c286 | 75 | ;; |
94e9c286 SM |
76 | |
77 | ;;; DOWNLOAD | |
78 | ||
d13c8be6 SM |
79 | ;; The latest release of this file lies in the docutils source code repository: |
80 | ;; http://docutils.svn.sourceforge.net/svnroot/docutils/trunk/docutils/tools/editors/emacs/rst.el | |
94e9c286 SM |
81 | |
82 | ;;; INSTALLATION | |
83 | ||
84 | ;; Add the following lines to your `.emacs' file: | |
85 | ;; | |
86 | ;; (require 'rst) | |
87 | ;; | |
88 | ;; If you are using `.txt' as a standard extension for reST files as | |
89 | ;; http://docutils.sourceforge.net/FAQ.html#what-s-the-standard-filename-extension-for-a-restructuredtext-file | |
90 | ;; suggests you may use one of the `Local Variables in Files' mechanism Emacs | |
b4747519 | 91 | ;; provides to set the major mode automatically. For instance you may use:: |
94e9c286 SM |
92 | ;; |
93 | ;; .. -*- mode: rst -*- | |
94 | ;; | |
b4747519 SM |
95 | ;; in the very first line of your file. The following code is useful if you |
96 | ;; want automatically enter rst-mode from any file with compatible extensions: | |
94e9c286 SM |
97 | ;; |
98 | ;; (setq auto-mode-alist | |
6d3f7c2f SM |
99 | ;; (append '(("\\.txt\\'" . rst-mode) |
100 | ;; ("\\.rst\\'" . rst-mode) | |
101 | ;; ("\\.rest\\'" . rst-mode)) auto-mode-alist)) | |
94e9c286 SM |
102 | ;; |
103 | ||
d13c8be6 | 104 | ;;; Code: |
94e9c286 | 105 | |
42152ee4 SM |
106 | ;; FIXME: Use `eval-when-compile' when calls to `some', `position', `signum' |
107 | ;; and `position-if' are replaced. | |
108 | (require 'cl) | |
94e9c286 | 109 | |
d13c8be6 SM |
110 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
111 | ;; Versions | |
112 | ||
113 | (defun rst-extract-version (delim-re head-re re tail-re var &optional default) | |
6d3f7c2f SM |
114 | "Extract the version from a variable according to the given regexes. |
115 | Return the version after regex DELIM-RE and HEAD-RE matching RE | |
116 | and before TAIL-RE and DELIM-RE in VAR or DEFAULT for no match." | |
d13c8be6 SM |
117 | (if (string-match |
118 | (concat delim-re head-re "\\(" re "\\)" tail-re delim-re) | |
119 | var) | |
120 | (match-string 1 var) | |
121 | default)) | |
122 | ||
123 | ;; Use CVSHeader to really get information from CVS and not other version | |
6d3f7c2f | 124 | ;; control systems. |
d13c8be6 | 125 | (defconst rst-cvs-header |
42152ee4 | 126 | "$CVSHeader: sm/rst_el/rst.el,v 1.257.2.10 2012-06-02 09:38:40 stefan Exp $") |
d13c8be6 SM |
127 | (defconst rst-cvs-rev |
128 | (rst-extract-version "\\$" "CVSHeader: \\S + " "[0-9]+\\(?:\\.[0-9]+\\)+" | |
129 | " .*" rst-cvs-header "0.0") | |
6d3f7c2f | 130 | "The CVS revision of this file. CVS revision is the development revision.") |
d13c8be6 SM |
131 | (defconst rst-cvs-timestamp |
132 | (rst-extract-version "\\$" "CVSHeader: \\S + \\S + " | |
133 | "[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+" " .*" | |
134 | rst-cvs-header "1970-01-01 00:00:00") | |
6d3f7c2f | 135 | "The CVS time stamp of this file.") |
d13c8be6 | 136 | |
6d3f7c2f | 137 | ;; Use LastChanged... to really get information from SVN. |
d13c8be6 SM |
138 | (defconst rst-svn-rev |
139 | (rst-extract-version "\\$" "LastChangedRevision: " "[0-9]+" " " | |
140 | "$LastChangedRevision: 7399 $") | |
141 | "The SVN revision of this file. | |
142 | SVN revision is the upstream (docutils) revision.") | |
143 | (defconst rst-svn-timestamp | |
144 | (rst-extract-version "\\$" "LastChangedDate: " ".+?+" " " | |
145 | "$LastChangedDate: 2012-04-29 17:01:05 +0200 (Sun, 29 Apr 2012) $") | |
6d3f7c2f | 146 | "The SVN time stamp of this file.") |
d13c8be6 | 147 | |
6d3f7c2f | 148 | ;; Maintained by the release process. |
d13c8be6 SM |
149 | (defconst rst-official-version |
150 | (rst-extract-version "%" "OfficialVersion: " "[0-9]+\\(?:\\.[0-9]+\\)+" " " | |
151 | "%OfficialVersion: 1.2.1 %") | |
152 | "Official version of the package.") | |
153 | (defconst rst-official-cvs-rev | |
154 | (rst-extract-version "[%$]" "Revision: " "[0-9]+\\(?:\\.[0-9]+\\)+" " " | |
155 | "%Revision: 1.256 %") | |
156 | "CVS revision of this file in the official version.") | |
157 | ||
158 | (defconst rst-version | |
159 | (if (equal rst-official-cvs-rev rst-cvs-rev) | |
160 | rst-official-version | |
161 | (format "%s (development %s [%s])" rst-official-version | |
162 | rst-cvs-rev rst-cvs-timestamp)) | |
163 | "The version string. | |
6d3f7c2f SM |
164 | Starts with the current official version. For developer versions |
165 | in parentheses follows the development revision and the time stamp.") | |
d13c8be6 SM |
166 | |
167 | (defconst rst-package-emacs-version-alist | |
168 | '(("1.0.0" . "24.0") | |
169 | ("1.1.0" . "24.0") | |
170 | ("1.2.0" . "24.0") | |
171 | ("1.2.1" . "24.0"))) | |
172 | ||
173 | (unless (assoc rst-official-version rst-package-emacs-version-alist) | |
174 | (error "Version %s not listed in `rst-package-emacs-version-alist'" | |
175 | rst-version)) | |
176 | ||
177 | (add-to-list 'customize-package-emacs-version-alist | |
178 | (cons 'ReST rst-package-emacs-version-alist)) | |
94e9c286 | 179 | |
d13c8be6 SM |
180 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
181 | ;; Initialize customization | |
94e9c286 SM |
182 | |
183 | \f | |
92439579 | 184 | (defgroup rst nil "Support for reStructuredText documents." |
94e9c286 SM |
185 | :group 'wp |
186 | :version "23.1" | |
187 | :link '(url-link "http://docutils.sourceforge.net/rst.html")) | |
188 | ||
94e9c286 SM |
189 | \f |
190 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
d13c8be6 SM |
191 | ;; Facilities for regular expressions used everywhere |
192 | ||
193 | ;; The trailing numbers in the names give the number of referenceable regex | |
6d3f7c2f | 194 | ;; groups contained in the regex. |
d13c8be6 SM |
195 | |
196 | ;; Used to be customizable but really is not customizable but fixed by the reST | |
6d3f7c2f | 197 | ;; syntax. |
d13c8be6 | 198 | (defconst rst-bullets |
6d3f7c2f | 199 | ;; Sorted so they can form a character class when concatenated. |
d13c8be6 SM |
200 | '(?- ?* ?+ ?\u2022 ?\u2023 ?\u2043) |
201 | "List of all possible bullet characters for bulleted lists.") | |
202 | ||
203 | (defconst rst-uri-schemes | |
204 | '("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https" "imap" | |
205 | "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero" "rtsp" | |
206 | "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais") | |
207 | "Supported URI schemes.") | |
208 | ||
209 | (defconst rst-adornment-chars | |
6d3f7c2f | 210 | ;; Sorted so they can form a character class when concatenated. |
d13c8be6 SM |
211 | '(?\] |
212 | ?! ?\" ?# ?$ ?% ?& ?' ?\( ?\) ?* ?+ ?, ?. ?/ ?: ?\; ?< ?= ?> ?? ?@ ?\[ ?\\ | |
213 | ?^ ?_ ?` ?{ ?| ?} ?~ | |
214 | ?-) | |
215 | "Characters which may be used in adornments for sections and transitions.") | |
216 | ||
217 | (defconst rst-max-inline-length | |
218 | 1000 | |
219 | "Maximum length of inline markup to recognize.") | |
220 | ||
221 | (defconst rst-re-alist-def | |
6d3f7c2f SM |
222 | ;; `*-beg' matches * at the beginning of a line. |
223 | ;; `*-end' matches * at the end of a line. | |
224 | ;; `*-prt' matches a part of *. | |
225 | ;; `*-tag' matches *. | |
226 | ;; `*-sta' matches the start of * which may be followed by respective content. | |
227 | ;; `*-pfx' matches the delimiter left of *. | |
228 | ;; `*-sfx' matches the delimiter right of *. | |
229 | ;; `*-hlp' helper for *. | |
d13c8be6 SM |
230 | ;; |
231 | ;; A trailing number says how many referenceable groups are contained. | |
232 | `( | |
233 | ||
234 | ;; Horizontal white space (`hws') | |
235 | (hws-prt "[\t ]") | |
6d3f7c2f SM |
236 | (hws-tag hws-prt "*") ; Optional sequence of horizontal white space. |
237 | (hws-sta hws-prt "+") ; Mandatory sequence of horizontal white space. | |
d13c8be6 SM |
238 | |
239 | ;; Lines (`lin') | |
6d3f7c2f SM |
240 | (lin-beg "^" hws-tag) ; Beginning of a possibly indented line. |
241 | (lin-end hws-tag "$") ; End of a line with optional trailing white space. | |
242 | (linemp-tag "^" hws-tag "$") ; Empty line with optional white space. | |
d13c8be6 SM |
243 | |
244 | ;; Various tags and parts | |
245 | (ell-tag "\\.\\.\\.") ; Ellipsis | |
6d3f7c2f SM |
246 | (bul-tag ,(concat "[" rst-bullets "]")) ; A bullet. |
247 | (ltr-tag "[a-zA-Z]") ; A letter enumerator tag. | |
248 | (num-prt "[0-9]") ; A number enumerator part. | |
249 | (num-tag num-prt "+") ; A number enumerator tag. | |
250 | (rom-prt "[IVXLCDMivxlcdm]") ; A roman enumerator part. | |
251 | (rom-tag rom-prt "+") ; A roman enumerator tag. | |
252 | (aut-tag "#") ; An automatic enumerator tag. | |
253 | (dcl-tag "::") ; Double colon. | |
d13c8be6 SM |
254 | |
255 | ;; Block lead in (`bli') | |
256 | (bli-sfx (:alt hws-sta "$")) ; Suffix of a block lead-in with *optional* | |
6d3f7c2f | 257 | ; immediate content. |
d13c8be6 SM |
258 | |
259 | ;; Various starts | |
6d3f7c2f | 260 | (bul-sta bul-tag bli-sfx) ; Start of a bulleted item. |
d13c8be6 SM |
261 | |
262 | ;; Explicit markup tag (`exm') | |
263 | (exm-tag "\\.\\.") | |
264 | (exm-sta exm-tag hws-sta) | |
265 | (exm-beg lin-beg exm-sta) | |
266 | ||
267 | ;; Counters in enumerations (`cnt') | |
6d3f7c2f SM |
268 | (cntany-tag (:alt ltr-tag num-tag rom-tag aut-tag)) ; An arbitrary counter. |
269 | (cntexp-tag (:alt ltr-tag num-tag rom-tag)) ; An arbitrary explicit counter. | |
d13c8be6 SM |
270 | |
271 | ;; Enumerator (`enm') | |
272 | (enmany-tag (:alt | |
273 | (:seq cntany-tag "\\.") | |
6d3f7c2f | 274 | (:seq "(?" cntany-tag ")"))) ; An arbitrary enumerator. |
d13c8be6 SM |
275 | (enmexp-tag (:alt |
276 | (:seq cntexp-tag "\\.") | |
277 | (:seq "(?" cntexp-tag ")"))) ; An arbitrary explicit | |
6d3f7c2f | 278 | ; enumerator. |
d13c8be6 SM |
279 | (enmaut-tag (:alt |
280 | (:seq aut-tag "\\.") | |
6d3f7c2f SM |
281 | (:seq "(?" aut-tag ")"))) ; An automatic enumerator. |
282 | (enmany-sta enmany-tag bli-sfx) ; An arbitrary enumerator start. | |
283 | (enmexp-sta enmexp-tag bli-sfx) ; An arbitrary explicit enumerator start. | |
d13c8be6 | 284 | (enmexp-beg lin-beg enmexp-sta) ; An arbitrary explicit enumerator start |
6d3f7c2f | 285 | ; at the beginning of a line. |
d13c8be6 SM |
286 | |
287 | ;; Items may be enumerated or bulleted (`itm') | |
6d3f7c2f | 288 | (itmany-tag (:alt enmany-tag bul-tag)) ; An arbitrary item tag. |
d13c8be6 | 289 | (itmany-sta-1 (:grp itmany-tag) bli-sfx) ; An arbitrary item start, group |
6d3f7c2f | 290 | ; is the item tag. |
d13c8be6 SM |
291 | (itmany-beg-1 lin-beg itmany-sta-1) ; An arbitrary item start at the |
292 | ; beginning of a line, group is the | |
6d3f7c2f | 293 | ; item tag. |
d13c8be6 SM |
294 | |
295 | ;; Inline markup (`ilm') | |
296 | (ilm-pfx (:alt "^" hws-prt "[-'\"([{<\u2018\u201c\u00ab\u2019/:]")) | |
297 | (ilm-sfx (:alt "$" hws-prt "[]-'\")}>\u2019\u201d\u00bb/:.,;!?\\]")) | |
298 | ||
299 | ;; Inline markup content (`ilc') | |
6d3f7c2f SM |
300 | (ilcsgl-tag "\\S ") ; A single non-white character. |
301 | (ilcast-prt (:alt "[^*\\]" "\\\\.")) ; Part of non-asterisk content. | |
302 | (ilcbkq-prt (:alt "[^`\\]" "\\\\.")) ; Part of non-backquote content. | |
d13c8be6 | 303 | (ilcbkqdef-prt (:alt "[^`\\\n]" "\\\\.")) ; Part of non-backquote |
6d3f7c2f SM |
304 | ; definition. |
305 | (ilcbar-prt (:alt "[^|\\]" "\\\\.")) ; Part of non-vertical-bar content. | |
d13c8be6 | 306 | (ilcbardef-prt (:alt "[^|\\\n]" "\\\\.")) ; Part of non-vertical-bar |
6d3f7c2f SM |
307 | ; definition. |
308 | (ilcast-sfx "[^\t *\\]") ; Suffix of non-asterisk content. | |
309 | (ilcbkq-sfx "[^\t `\\]") ; Suffix of non-backquote content. | |
310 | (ilcbar-sfx "[^\t |\\]") ; Suffix of non-vertical-bar content. | |
311 | (ilcrep-hlp ,(format "\\{0,%d\\}" rst-max-inline-length)) ; Repeat count. | |
d13c8be6 SM |
312 | (ilcast-tag (:alt ilcsgl-tag |
313 | (:seq ilcsgl-tag | |
314 | ilcast-prt ilcrep-hlp | |
6d3f7c2f | 315 | ilcast-sfx))) ; Non-asterisk content. |
d13c8be6 SM |
316 | (ilcbkq-tag (:alt ilcsgl-tag |
317 | (:seq ilcsgl-tag | |
318 | ilcbkq-prt ilcrep-hlp | |
6d3f7c2f | 319 | ilcbkq-sfx))) ; Non-backquote content. |
d13c8be6 SM |
320 | (ilcbkqdef-tag (:alt ilcsgl-tag |
321 | (:seq ilcsgl-tag | |
322 | ilcbkqdef-prt ilcrep-hlp | |
6d3f7c2f | 323 | ilcbkq-sfx))) ; Non-backquote definition. |
d13c8be6 SM |
324 | (ilcbar-tag (:alt ilcsgl-tag |
325 | (:seq ilcsgl-tag | |
326 | ilcbar-prt ilcrep-hlp | |
6d3f7c2f | 327 | ilcbar-sfx))) ; Non-vertical-bar content. |
d13c8be6 SM |
328 | (ilcbardef-tag (:alt ilcsgl-tag |
329 | (:seq ilcsgl-tag | |
330 | ilcbardef-prt ilcrep-hlp | |
6d3f7c2f | 331 | ilcbar-sfx))) ; Non-vertical-bar definition. |
d13c8be6 SM |
332 | |
333 | ;; Fields (`fld') | |
6d3f7c2f SM |
334 | (fldnam-prt (:alt "[^:\n]" "\\\\:")) ; Part of a field name. |
335 | (fldnam-tag fldnam-prt "+") ; A field name. | |
336 | (fld-tag ":" fldnam-tag ":") ; A field marker. | |
d13c8be6 SM |
337 | |
338 | ;; Options (`opt') | |
6d3f7c2f SM |
339 | (optsta-tag (:alt "[-+/]" "--")) ; Start of an option. |
340 | (optnam-tag "\\sw" (:alt "-" "\\sw") "*") ; Name of an option. | |
341 | (optarg-tag (:shy "[ =]\\S +")) ; Option argument. | |
342 | (optsep-tag (:shy "," hws-prt)) ; Separator between options. | |
343 | (opt-tag (:shy optsta-tag optnam-tag optarg-tag "?")) ; A complete option. | |
d13c8be6 SM |
344 | |
345 | ;; Footnotes and citations (`fnc') | |
6d3f7c2f SM |
346 | (fncnam-prt "[^\]\n]") ; Part of a footnote or citation name. |
347 | (fncnam-tag fncnam-prt "+") ; A footnote or citation name. | |
348 | (fnc-tag "\\[" fncnam-tag "]") ; A complete footnote or citation tag. | |
d13c8be6 SM |
349 | (fncdef-tag-2 (:grp exm-sta) |
350 | (:grp fnc-tag)) ; A complete footnote or citation definition | |
6d3f7c2f | 351 | ; tag. First group is the explicit markup |
d13c8be6 | 352 | ; start, second group is the footnote / |
6d3f7c2f | 353 | ; citation tag. |
d13c8be6 | 354 | (fnc-sta-2 fncdef-tag-2 bli-sfx) ; Start of a footnote or citation |
6d3f7c2f | 355 | ; definition. First group is the explicit |
d13c8be6 | 356 | ; markup start, second group is the |
6d3f7c2f | 357 | ; footnote / citation tag. |
d13c8be6 SM |
358 | |
359 | ;; Substitutions (`sub') | |
6d3f7c2f | 360 | (sub-tag "|" ilcbar-tag "|") ; A complete substitution tag. |
d13c8be6 | 361 | (subdef-tag "|" ilcbardef-tag "|") ; A complete substitution definition |
6d3f7c2f | 362 | ; tag. |
d13c8be6 SM |
363 | |
364 | ;; Symbol (`sym') | |
365 | (sym-tag (:shy "\\sw+" (:shy "\\s_\\sw+") "*")) | |
366 | ||
367 | ;; URIs (`uri') | |
368 | (uri-tag (:alt ,@rst-uri-schemes)) | |
369 | ||
370 | ;; Adornment (`ado') | |
371 | (ado-prt "[" ,(concat rst-adornment-chars) "]") | |
372 | (adorep3-hlp "\\{3,\\}") ; There must be at least 3 characters because | |
373 | ; otherwise explicit markup start would be | |
6d3f7c2f | 374 | ; recognized. |
d13c8be6 | 375 | (adorep2-hlp "\\{2,\\}") ; As `adorep3-hlp' but when the first of three |
6d3f7c2f | 376 | ; characters is matched differently. |
d13c8be6 SM |
377 | (ado-tag-1-1 (:grp ado-prt) |
378 | "\\1" adorep2-hlp) ; A complete adornment, group is the first | |
379 | ; adornment character and MUST be the FIRST | |
6d3f7c2f | 380 | ; group in the whole expression. |
d13c8be6 SM |
381 | (ado-tag-1-2 (:grp ado-prt) |
382 | "\\2" adorep2-hlp) ; A complete adornment, group is the first | |
383 | ; adornment character and MUST be the | |
6d3f7c2f | 384 | ; SECOND group in the whole expression. |
d13c8be6 SM |
385 | (ado-beg-2-1 "^" (:grp ado-tag-1-2) |
386 | lin-end) ; A complete adornment line; first group is the whole | |
387 | ; adornment and MUST be the FIRST group in the whole | |
388 | ; expression; second group is the first adornment | |
6d3f7c2f | 389 | ; character. |
d13c8be6 SM |
390 | |
391 | ;; Titles (`ttl') | |
6d3f7c2f SM |
392 | (ttl-tag "\\S *\\w\\S *") ; A title text. |
393 | (ttl-beg lin-beg ttl-tag) ; A title text at the beginning of a line. | |
d13c8be6 SM |
394 | |
395 | ;; Directives and substitution definitions (`dir') | |
396 | (dir-tag-3 (:grp exm-sta) | |
397 | (:grp (:shy subdef-tag hws-sta) "?") | |
398 | (:grp sym-tag dcl-tag)) ; A directive or substitution definition | |
6d3f7c2f | 399 | ; tag. First group is explicit markup |
d13c8be6 SM |
400 | ; start, second group is a possibly |
401 | ; empty substitution tag, third group is | |
402 | ; the directive tag including the double | |
6d3f7c2f | 403 | ; colon. |
d13c8be6 | 404 | (dir-sta-3 dir-tag-3 bli-sfx) ; Start of a directive or substitution |
6d3f7c2f | 405 | ; definition. Groups are as in dir-tag-3. |
d13c8be6 SM |
406 | |
407 | ;; Literal block (`lit') | |
408 | (lit-sta-2 (:grp (:alt "[^.\n]" "\\.[^.\n]") ".*") "?" | |
6d3f7c2f | 409 | (:grp dcl-tag) "$") ; Start of a literal block. First group is |
d13c8be6 SM |
410 | ; any text before the double colon tag which |
411 | ; may not exist, second group is the double | |
6d3f7c2f | 412 | ; colon tag. |
d13c8be6 SM |
413 | |
414 | ;; Comments (`cmt') | |
415 | (cmt-sta-1 (:grp exm-sta) "[^\[|_\n]" | |
416 | (:alt "[^:\n]" (:seq ":" (:alt "[^:\n]" "$"))) | |
417 | "*$") ; Start of a comment block; first group is explicit markup | |
6d3f7c2f | 418 | ; start. |
d13c8be6 SM |
419 | |
420 | ;; Paragraphs (`par') | |
421 | (par-tag- (:alt itmany-tag fld-tag opt-tag fncdef-tag-2 dir-tag-3 exm-tag) | |
422 | ) ; Tag at the beginning of a paragraph; there may be groups in | |
6d3f7c2f | 423 | ; certain cases. |
d13c8be6 SM |
424 | ) |
425 | "Definition alist of relevant regexes. | |
426 | Each entry consists of the symbol naming the regex and an | |
427 | argument list for `rst-re'.") | |
428 | ||
6d3f7c2f | 429 | ;; FIXME: Use `sregex` or `rx` instead of re-inventing the wheel. |
d13c8be6 SM |
430 | (defun rst-re (&rest args) |
431 | "Interpret ARGS as regular expressions and return a regex string. | |
432 | Each element of ARGS may be one of the following: | |
433 | ||
434 | A string which is inserted unchanged. | |
435 | ||
436 | A character which is resolved to a quoted regex. | |
437 | ||
438 | A symbol which is resolved to a string using `rst-re-alist-def'. | |
439 | ||
6d3f7c2f SM |
440 | A list with a keyword in the car. Each element of the cdr of such |
441 | a list is recursively interpreted as ARGS. The results of this | |
d13c8be6 SM |
442 | interpretation are concatenated according to the keyword. |
443 | ||
444 | For the keyword `:seq' the results are simply concatenated. | |
445 | ||
446 | For the keyword `:shy' the results are concatenated and | |
447 | surrounded by a shy-group (\"\\(?:...\\)\"). | |
448 | ||
449 | For the keyword `:alt' the results form an alternative (\"\\|\") | |
450 | which is shy-grouped (\"\\(?:...\\)\"). | |
451 | ||
452 | For the keyword `:grp' the results are concatenated and form a | |
6d3f7c2f | 453 | referenceable group (\"\\(...\\)\"). |
d13c8be6 SM |
454 | |
455 | After interpretation of ARGS the results are concatenated as for | |
6d3f7c2f | 456 | `:seq'." |
d13c8be6 SM |
457 | (apply 'concat |
458 | (mapcar | |
459 | (lambda (re) | |
460 | (cond | |
461 | ((stringp re) | |
462 | re) | |
463 | ((symbolp re) | |
464 | (cadr (assoc re rst-re-alist))) | |
8f6b6da8 | 465 | ((characterp re) |
d13c8be6 SM |
466 | (regexp-quote (char-to-string re))) |
467 | ((listp re) | |
468 | (let ((nested | |
469 | (mapcar (lambda (elt) | |
470 | (rst-re elt)) | |
471 | (cdr re)))) | |
472 | (cond | |
473 | ((eq (car re) :seq) | |
474 | (mapconcat 'identity nested "")) | |
475 | ((eq (car re) :shy) | |
476 | (concat "\\(?:" (mapconcat 'identity nested "") "\\)")) | |
477 | ((eq (car re) :grp) | |
478 | (concat "\\(" (mapconcat 'identity nested "") "\\)")) | |
479 | ((eq (car re) :alt) | |
480 | (concat "\\(?:" (mapconcat 'identity nested "\\|") "\\)")) | |
481 | (t | |
482 | (error "Unknown list car: %s" (car re)))))) | |
483 | (t | |
484 | (error "Unknown object type for building regex: %s" re)))) | |
485 | args))) | |
486 | ||
51fa99f1 SM |
487 | ;; FIXME: Remove circular dependency between `rst-re' and `rst-re-alist'. |
488 | (defconst rst-re-alist | |
489 | ;; Shadow global value we are just defining so we can construct it step by | |
6d3f7c2f | 490 | ;; step. |
51fa99f1 SM |
491 | (let (rst-re-alist) |
492 | (dolist (re rst-re-alist-def) | |
493 | (setq rst-re-alist | |
494 | (nconc rst-re-alist | |
495 | (list (list (car re) (apply 'rst-re (cdr re))))))) | |
496 | rst-re-alist) | |
497 | "Alist mapping symbols from `rst-re-alist-def' to regex strings.") | |
498 | ||
94e9c286 SM |
499 | \f |
500 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
6d3f7c2f | 501 | ;; Mode definition |
d13c8be6 SM |
502 | |
503 | (defun rst-define-key (keymap key def &rest deprecated) | |
6d3f7c2f SM |
504 | "Bind like `define-key' but add deprecated key definitions. |
505 | KEYMAP, KEY, and DEF are as in `define-key'. DEPRECATED key | |
506 | definitions should be in vector notation. These are defined as | |
507 | well but give an additional message." | |
d13c8be6 SM |
508 | (define-key keymap key def) |
509 | (dolist (dep-key deprecated) | |
6d3f7c2f SM |
510 | (define-key keymap dep-key |
511 | `(lambda () | |
512 | ,(format "Deprecated binding for %s, use \\[%s] instead." def def) | |
513 | (interactive) | |
514 | (call-interactively ',def) | |
515 | (message "[Deprecated use of key %s; use key %s instead]" | |
516 | (key-description (this-command-keys)) | |
517 | (key-description ,key)))))) | |
d13c8be6 | 518 | |
94e9c286 SM |
519 | ;; Key bindings. |
520 | (defvar rst-mode-map | |
521 | (let ((map (make-sparse-keymap))) | |
522 | ||
6d3f7c2f | 523 | ;; \C-c is the general keymap. |
d13c8be6 SM |
524 | (rst-define-key map [?\C-c ?\C-h] 'describe-prefix-bindings) |
525 | ||
94e9c286 | 526 | ;; |
6d3f7c2f | 527 | ;; Section Adornments |
94e9c286 | 528 | ;; |
d13c8be6 SM |
529 | ;; The adjustment function that adorns or rotates a section title. |
530 | (rst-define-key map [?\C-c ?\C-=] 'rst-adjust [?\C-c ?\C-a t]) | |
531 | (rst-define-key map [?\C-=] 'rst-adjust) ; (Does not work on the Mac OSX.) | |
532 | ||
6d3f7c2f | 533 | ;; \C-c \C-a is the keymap for adornments. |
d13c8be6 | 534 | (rst-define-key map [?\C-c ?\C-a ?\C-h] 'describe-prefix-bindings) |
6d3f7c2f SM |
535 | ;; Display the hierarchy of adornments implied by the current document |
536 | ;; contents. | |
d13c8be6 SM |
537 | (rst-define-key map [?\C-c ?\C-a ?\C-d] 'rst-display-adornments-hierarchy) |
538 | ;; Homogenize the adornments in the document. | |
539 | (rst-define-key map [?\C-c ?\C-a ?\C-s] 'rst-straighten-adornments | |
540 | [?\C-c ?\C-s]) | |
94e9c286 SM |
541 | |
542 | ;; | |
6d3f7c2f | 543 | ;; Section Movement and Selection |
94e9c286 SM |
544 | ;; |
545 | ;; Mark the subsection where the cursor is. | |
d13c8be6 | 546 | (rst-define-key map [?\C-\M-h] 'rst-mark-section |
6d3f7c2f | 547 | ;; Same as mark-defun sgml-mark-current-element. |
d13c8be6 | 548 | [?\C-c ?\C-m]) |
94e9c286 | 549 | ;; Move forward/backward between section titles. |
d13c8be6 | 550 | (rst-define-key map [?\C-\M-a] 'rst-forward-section |
6d3f7c2f | 551 | ;; Same as beginning-of-defun. |
d13c8be6 SM |
552 | [?\C-c ?\C-n]) |
553 | (rst-define-key map [?\C-\M-e] 'rst-backward-section | |
6d3f7c2f | 554 | ;; Same as end-of-defun. |
d13c8be6 | 555 | [?\C-c ?\C-p]) |
94e9c286 SM |
556 | |
557 | ;; | |
6d3f7c2f | 558 | ;; Operating on regions |
94e9c286 | 559 | ;; |
6d3f7c2f | 560 | ;; \C-c \C-r is the keymap for regions. |
d13c8be6 SM |
561 | (rst-define-key map [?\C-c ?\C-r ?\C-h] 'describe-prefix-bindings) |
562 | ;; Makes region a line-block. | |
563 | (rst-define-key map [?\C-c ?\C-r ?\C-l] 'rst-line-block-region | |
564 | [?\C-c ?\C-d]) | |
6d3f7c2f | 565 | ;; Shift region left or right according to tabs. |
d13c8be6 SM |
566 | (rst-define-key map [?\C-c ?\C-r tab] 'rst-shift-region |
567 | [?\C-c ?\C-r t] [?\C-c ?\C-l t]) | |
568 | ||
569 | ;; | |
6d3f7c2f | 570 | ;; Operating on lists |
d13c8be6 | 571 | ;; |
6d3f7c2f | 572 | ;; \C-c \C-l is the keymap for lists. |
d13c8be6 | 573 | (rst-define-key map [?\C-c ?\C-l ?\C-h] 'describe-prefix-bindings) |
94e9c286 | 574 | ;; Makes paragraphs in region as a bullet list. |
d13c8be6 SM |
575 | (rst-define-key map [?\C-c ?\C-l ?\C-b] 'rst-bullet-list-region |
576 | [?\C-c ?\C-b]) | |
94e9c286 | 577 | ;; Makes paragraphs in region as a enumeration. |
d13c8be6 SM |
578 | (rst-define-key map [?\C-c ?\C-l ?\C-e] 'rst-enumerate-region |
579 | [?\C-c ?\C-e]) | |
94e9c286 | 580 | ;; Converts bullets to an enumeration. |
d13c8be6 SM |
581 | (rst-define-key map [?\C-c ?\C-l ?\C-c] 'rst-convert-bullets-to-enumeration |
582 | [?\C-c ?\C-v]) | |
94e9c286 | 583 | ;; Make sure that all the bullets in the region are consistent. |
d13c8be6 SM |
584 | (rst-define-key map [?\C-c ?\C-l ?\C-s] 'rst-straighten-bullets-region |
585 | [?\C-c ?\C-w]) | |
6d3f7c2f | 586 | ;; Insert a list item. |
d13c8be6 | 587 | (rst-define-key map [?\C-c ?\C-l ?\C-i] 'rst-insert-list) |
94e9c286 SM |
588 | |
589 | ;; | |
6d3f7c2f | 590 | ;; Table-of-Contents Features |
94e9c286 | 591 | ;; |
6d3f7c2f | 592 | ;; \C-c \C-t is the keymap for table of contents. |
d13c8be6 | 593 | (rst-define-key map [?\C-c ?\C-t ?\C-h] 'describe-prefix-bindings) |
94e9c286 | 594 | ;; Enter a TOC buffer to view and move to a specific section. |
d13c8be6 | 595 | (rst-define-key map [?\C-c ?\C-t ?\C-t] 'rst-toc) |
94e9c286 | 596 | ;; Insert a TOC here. |
d13c8be6 SM |
597 | (rst-define-key map [?\C-c ?\C-t ?\C-i] 'rst-toc-insert |
598 | [?\C-c ?\C-i]) | |
94e9c286 | 599 | ;; Update the document's TOC (without changing the cursor position). |
d13c8be6 SM |
600 | (rst-define-key map [?\C-c ?\C-t ?\C-u] 'rst-toc-update |
601 | [?\C-c ?\C-u]) | |
6d3f7c2f | 602 | ;; Go to the section under the cursor (cursor must be in TOC). |
d13c8be6 SM |
603 | (rst-define-key map [?\C-c ?\C-t ?\C-j] 'rst-goto-section |
604 | [?\C-c ?\C-f]) | |
94e9c286 SM |
605 | |
606 | ;; | |
6d3f7c2f | 607 | ;; Converting Documents from Emacs |
94e9c286 | 608 | ;; |
6d3f7c2f | 609 | ;; \C-c \C-c is the keymap for compilation. |
d13c8be6 | 610 | (rst-define-key map [?\C-c ?\C-c ?\C-h] 'describe-prefix-bindings) |
94e9c286 | 611 | ;; Run one of two pre-configured toolset commands on the document. |
d13c8be6 SM |
612 | (rst-define-key map [?\C-c ?\C-c ?\C-c] 'rst-compile |
613 | [?\C-c ?1]) | |
614 | (rst-define-key map [?\C-c ?\C-c ?\C-a] 'rst-compile-alt-toolset | |
615 | [?\C-c ?2]) | |
94e9c286 | 616 | ;; Convert the active region to pseudo-xml using the docutils tools. |
d13c8be6 SM |
617 | (rst-define-key map [?\C-c ?\C-c ?\C-x] 'rst-compile-pseudo-region |
618 | [?\C-c ?3]) | |
94e9c286 | 619 | ;; Convert the current document to PDF and launch a viewer on the results. |
d13c8be6 SM |
620 | (rst-define-key map [?\C-c ?\C-c ?\C-p] 'rst-compile-pdf-preview |
621 | [?\C-c ?4]) | |
94e9c286 | 622 | ;; Convert the current document to S5 slides and view in a web browser. |
d13c8be6 SM |
623 | (rst-define-key map [?\C-c ?\C-c ?\C-s] 'rst-compile-slides-preview |
624 | [?\C-c ?5]) | |
94e9c286 SM |
625 | |
626 | map) | |
e6438428 | 627 | "Keymap for reStructuredText mode commands. |
b4747519 | 628 | This inherits from Text mode.") |
94e9c286 SM |
629 | |
630 | ||
631 | ;; Abbrevs. | |
94e9c286 | 632 | (define-abbrev-table 'rst-mode-abbrev-table |
32845226 SM |
633 | (mapcar (lambda (x) (append x '(nil 0 system))) |
634 | '(("contents" ".. contents::\n..\n ") | |
635 | ("con" ".. contents::\n..\n ") | |
636 | ("cont" "[...]") | |
637 | ("skip" "\n\n[...]\n\n ") | |
638 | ("seq" "\n\n[...]\n\n ") | |
639 | ;; FIXME: Add footnotes, links, and more. | |
6d3f7c2f SM |
640 | )) |
641 | "Abbrev table used while in `rst-mode'.") | |
94e9c286 SM |
642 | |
643 | ||
644 | ;; Syntax table. | |
645 | (defvar rst-mode-syntax-table | |
646 | (let ((st (copy-syntax-table text-mode-syntax-table))) | |
647 | ||
648 | (modify-syntax-entry ?$ "." st) | |
649 | (modify-syntax-entry ?% "." st) | |
650 | (modify-syntax-entry ?& "." st) | |
651 | (modify-syntax-entry ?' "." st) | |
652 | (modify-syntax-entry ?* "." st) | |
d13c8be6 | 653 | (modify-syntax-entry ?+ "_" st) |
94e9c286 SM |
654 | (modify-syntax-entry ?. "_" st) |
655 | (modify-syntax-entry ?/ "." st) | |
d13c8be6 | 656 | (modify-syntax-entry ?: "_" st) |
94e9c286 SM |
657 | (modify-syntax-entry ?< "." st) |
658 | (modify-syntax-entry ?= "." st) | |
659 | (modify-syntax-entry ?> "." st) | |
660 | (modify-syntax-entry ?\\ "\\" st) | |
661 | (modify-syntax-entry ?| "." st) | |
d13c8be6 SM |
662 | (modify-syntax-entry ?_ "_" st) |
663 | (modify-syntax-entry ?\u00ab "." st) | |
664 | (modify-syntax-entry ?\u00bb "." st) | |
665 | (modify-syntax-entry ?\u2018 "." st) | |
666 | (modify-syntax-entry ?\u2019 "." st) | |
667 | (modify-syntax-entry ?\u201c "." st) | |
668 | (modify-syntax-entry ?\u201d "." st) | |
94e9c286 SM |
669 | |
670 | st) | |
671 | "Syntax table used while in `rst-mode'.") | |
672 | ||
673 | ||
674 | (defcustom rst-mode-hook nil | |
d13c8be6 SM |
675 | "Hook run when `rst-mode' is turned on. |
676 | The hook for `text-mode' is run before this one." | |
94e9c286 SM |
677 | :group 'rst |
678 | :type '(hook)) | |
679 | ||
680 | ||
0667a132 SM |
681 | ;; Use rst-mode for *.rst and *.rest files. Many ReStructured-Text files |
682 | ;; use *.txt, but this is too generic to be set as a default. | |
1e8780b1 | 683 | ;;;###autoload (add-to-list 'auto-mode-alist (purecopy '("\\.re?st\\'" . rst-mode))) |
94e9c286 SM |
684 | ;;;###autoload |
685 | (define-derived-mode rst-mode text-mode "ReST" | |
94e9c286 | 686 | "Major mode for editing reStructuredText documents. |
e6438428 | 687 | \\<rst-mode-map> |
94e9c286 | 688 | |
e6438428 JB |
689 | Turning on `rst-mode' calls the normal hooks `text-mode-hook' |
690 | and `rst-mode-hook'. This mode also supports font-lock | |
d13c8be6 | 691 | highlighting. |
e6438428 JB |
692 | |
693 | \\{rst-mode-map}" | |
536db356 JPW |
694 | :abbrev-table rst-mode-abbrev-table |
695 | :syntax-table rst-mode-syntax-table | |
696 | :group 'rst | |
94e9c286 | 697 | |
6d3f7c2f | 698 | ;; Paragraph recognition. |
d13c8be6 SM |
699 | (set (make-local-variable 'paragraph-separate) |
700 | (rst-re '(:alt | |
701 | "\f" | |
702 | lin-end))) | |
94e9c286 | 703 | (set (make-local-variable 'paragraph-start) |
d13c8be6 SM |
704 | (rst-re '(:alt |
705 | "\f" | |
706 | lin-end | |
707 | (:seq hws-tag par-tag- bli-sfx)))) | |
94e9c286 | 708 | |
6d3f7c2f | 709 | ;; Indenting and filling. |
d13c8be6 SM |
710 | (set (make-local-variable 'indent-line-function) 'rst-indent-line) |
711 | (set (make-local-variable 'adaptive-fill-mode) t) | |
712 | (set (make-local-variable 'adaptive-fill-regexp) | |
713 | (rst-re 'hws-tag 'par-tag- "?" 'hws-tag)) | |
714 | (set (make-local-variable 'adaptive-fill-function) 'rst-adaptive-fill) | |
715 | (set (make-local-variable 'fill-paragraph-handle-comment) nil) | |
94e9c286 | 716 | |
6d3f7c2f | 717 | ;; Comments. |
94e9c286 | 718 | (set (make-local-variable 'comment-start) ".. ") |
d13c8be6 SM |
719 | (set (make-local-variable 'comment-start-skip) |
720 | (rst-re 'lin-beg 'exm-tag 'bli-sfx)) | |
721 | (set (make-local-variable 'comment-continue) " ") | |
722 | (set (make-local-variable 'comment-multi-line) t) | |
723 | (set (make-local-variable 'comment-use-syntax) nil) | |
724 | ;; reStructuredText has not really a comment ender but nil is not really a | |
6d3f7c2f | 725 | ;; permissible value. |
d13c8be6 SM |
726 | (set (make-local-variable 'comment-end) "") |
727 | (set (make-local-variable 'comment-end-skip) nil) | |
728 | ||
6d3f7c2f SM |
729 | ;; Commenting in reStructuredText is very special so use our own set of |
730 | ;; functions. | |
d13c8be6 SM |
731 | (set (make-local-variable 'comment-line-break-function) |
732 | 'rst-comment-line-break) | |
733 | (set (make-local-variable 'comment-indent-function) | |
734 | 'rst-comment-indent) | |
735 | (set (make-local-variable 'comment-insert-comment-function) | |
736 | 'rst-comment-insert-comment) | |
737 | (set (make-local-variable 'comment-region-function) | |
738 | 'rst-comment-region) | |
739 | (set (make-local-variable 'uncomment-region-function) | |
740 | 'rst-uncomment-region) | |
94e9c286 | 741 | |
6d3f7c2f SM |
742 | ;; Font lock. |
743 | (set (make-local-variable 'font-lock-defaults) | |
744 | '(rst-font-lock-keywords | |
745 | t nil nil nil | |
746 | (font-lock-multiline . t) | |
747 | (font-lock-mark-block-function . mark-paragraph))) | |
d13c8be6 SM |
748 | (add-hook 'font-lock-extend-region-functions 'rst-font-lock-extend-region t) |
749 | ||
6d3f7c2f | 750 | ;; Text after a changed line may need new fontification. |
d13c8be6 | 751 | (set (make-local-variable 'jit-lock-contextually) t)) |
94e9c286 SM |
752 | |
753 | ;;;###autoload | |
754 | (define-minor-mode rst-minor-mode | |
ac6c8639 CY |
755 | "Toggle ReST minor mode. |
756 | With a prefix argument ARG, enable ReST minor mode if ARG is | |
757 | positive, and disable it otherwise. If called from Lisp, enable | |
758 | the mode if ARG is omitted or nil. | |
94e9c286 | 759 | |
92439579 JB |
760 | When ReST minor mode is enabled, the ReST mode keybindings |
761 | are installed on top of the major mode bindings. Use this | |
762 | for modes derived from Text mode, like Mail mode." | |
94e9c286 SM |
763 | ;; The initial value. |
764 | nil | |
765 | ;; The indicator for the mode line. | |
766 | " ReST" | |
767 | ;; The minor mode bindings. | |
768 | rst-mode-map | |
769 | :group 'rst) | |
770 | ||
771 | ;; FIXME: can I somehow install these too? | |
6d3f7c2f SM |
772 | ;; :abbrev-table rst-mode-abbrev-table |
773 | ;; :syntax-table rst-mode-syntax-table | |
94e9c286 | 774 | |
94e9c286 SM |
775 | \f |
776 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
d13c8be6 SM |
777 | ;; Section Adornment Adjustment |
778 | ;; ============================ | |
94e9c286 SM |
779 | ;; |
780 | ;; The following functions implement a smart automatic title sectioning feature. | |
781 | ;; The idea is that with the cursor sitting on a section title, we try to get as | |
782 | ;; much information from context and try to do the best thing automatically. | |
783 | ;; This function can be invoked many times and/or with prefix argument to rotate | |
d13c8be6 | 784 | ;; between the various sectioning adornments. |
94e9c286 SM |
785 | ;; |
786 | ;; Definitions: the two forms of sectioning define semantically separate section | |
d13c8be6 | 787 | ;; levels. A sectioning ADORNMENT consists in: |
94e9c286 SM |
788 | ;; |
789 | ;; - a CHARACTER | |
790 | ;; | |
791 | ;; - a STYLE which can be either of 'simple' or 'over-and-under'. | |
792 | ;; | |
793 | ;; - an INDENT (meaningful for the over-and-under style only) which determines | |
794 | ;; how many characters and over-and-under style is hanging outside of the | |
795 | ;; title at the beginning and ending. | |
796 | ;; | |
d13c8be6 | 797 | ;; Here are two examples of adornments (| represents the window border, column |
94e9c286 SM |
798 | ;; 0): |
799 | ;; | |
800 | ;; | | |
801 | ;; 1. char: '-' e |Some Title | |
802 | ;; style: simple |---------- | |
803 | ;; | | |
804 | ;; 2. char: '=' |============== | |
805 | ;; style: over-and-under | Some Title | |
806 | ;; indent: 2 |============== | |
807 | ;; | | |
808 | ;; | |
809 | ;; Some notes: | |
810 | ;; | |
811 | ;; - The underlining character that is used depends on context. The file is | |
812 | ;; scanned to find other sections and an appropriate character is selected. | |
813 | ;; If the function is invoked on a section that is complete, the character is | |
d13c8be6 | 814 | ;; rotated among the existing section adornments. |
94e9c286 SM |
815 | ;; |
816 | ;; Note that when rotating the characters, if we come to the end of the | |
d13c8be6 SM |
817 | ;; hierarchy of adornments, the variable rst-preferred-adornments is |
818 | ;; consulted to propose a new underline adornment, and if continued, we cycle | |
819 | ;; the adornments all over again. Set this variable to nil if you want to | |
820 | ;; limit the underlining character propositions to the existing adornments in | |
94e9c286 SM |
821 | ;; the file. |
822 | ;; | |
94e9c286 SM |
823 | ;; - An underline/overline that is not extended to the column at which it should |
824 | ;; be hanging is dubbed INCOMPLETE. For example:: | |
825 | ;; | |
826 | ;; |Some Title | |
827 | ;; |------- | |
828 | ;; | |
829 | ;; Examples of default invocation: | |
830 | ;; | |
831 | ;; |Some Title ---> |Some Title | |
832 | ;; | |---------- | |
833 | ;; | |
834 | ;; |Some Title ---> |Some Title | |
835 | ;; |----- |---------- | |
836 | ;; | |
837 | ;; | |------------ | |
838 | ;; | Some Title ---> | Some Title | |
839 | ;; | |------------ | |
840 | ;; | |
841 | ;; In over-and-under style, when alternating the style, a variable is | |
842 | ;; available to select how much default indent to use (it can be zero). Note | |
d13c8be6 | 843 | ;; that if the current section adornment already has an indent, we don't |
94e9c286 SM |
844 | ;; adjust it to the default, we rather use the current indent that is already |
845 | ;; there for adjustment (unless we cycle, in which case we use the indent | |
846 | ;; that has been found previously). | |
847 | ||
848 | (defgroup rst-adjust nil | |
d13c8be6 | 849 | "Settings for adjustment and cycling of section title adornments." |
94e9c286 SM |
850 | :group 'rst |
851 | :version "21.1") | |
852 | ||
d13c8be6 SM |
853 | (define-obsolete-variable-alias |
854 | 'rst-preferred-decorations 'rst-preferred-adornments "r6506") | |
855 | (defcustom rst-preferred-adornments '((?= over-and-under 1) | |
856 | (?= simple 0) | |
857 | (?- simple 0) | |
858 | (?~ simple 0) | |
859 | (?+ simple 0) | |
860 | (?` simple 0) | |
861 | (?# simple 0) | |
862 | (?@ simple 0)) | |
863 | "Preferred hierarchy of section title adornments. | |
864 | ||
865 | A list consisting of lists of the form (CHARACTER STYLE INDENT). | |
6d3f7c2f SM |
866 | CHARACTER is the character used. STYLE is one of the symbols |
867 | OVER-AND-UNDER or SIMPLE. INDENT is an integer giving the wanted | |
868 | indentation for STYLE OVER-AND-UNDER. CHARACTER and STYLE are | |
869 | always used when a section adornment is described. In other | |
d13c8be6 SM |
870 | places t instead of a list stands for a transition. |
871 | ||
872 | This sequence is consulted to offer a new adornment suggestion | |
94e9c286 SM |
873 | when we rotate the underlines at the end of the existing |
874 | hierarchy of characters, or when there is no existing section | |
d13c8be6 SM |
875 | title in the file. |
876 | ||
877 | Set this to an empty list to use only the adornment found in the | |
878 | file." | |
879 | :group 'rst-adjust | |
880 | :type `(repeat | |
881 | (group :tag "Adornment specification" | |
882 | (choice :tag "Adornment character" | |
883 | ,@(mapcar (lambda (char) | |
884 | (list 'const | |
885 | :tag (char-to-string char) char)) | |
886 | rst-adornment-chars)) | |
887 | (radio :tag "Adornment type" | |
888 | (const :tag "Overline and underline" over-and-under) | |
889 | (const :tag "Underline only" simple)) | |
890 | (integer :tag "Indentation for overline and underline type" | |
891 | :value 0)))) | |
94e9c286 SM |
892 | |
893 | (defcustom rst-default-indent 1 | |
894 | "Number of characters to indent the section title. | |
895 | ||
d13c8be6 SM |
896 | This is used for when toggling adornment styles, when switching |
897 | from a simple adornment style to a over-and-under adornment | |
94e9c286 | 898 | style." |
d13c8be6 SM |
899 | :group 'rst-adjust |
900 | :type '(integer)) | |
94e9c286 SM |
901 | |
902 | ||
d13c8be6 SM |
903 | (defun rst-compare-adornments (ado1 ado2) |
904 | "Compare adornments. | |
905 | Return true if both ADO1 and ADO2 adornments are equal, | |
94e9c286 | 906 | according to restructured text semantics (only the character and |
92439579 | 907 | the style are compared, the indentation does not matter)." |
d13c8be6 SM |
908 | (and (eq (car ado1) (car ado2)) |
909 | (eq (cadr ado1) (cadr ado2)))) | |
94e9c286 SM |
910 | |
911 | ||
d13c8be6 SM |
912 | (defun rst-get-adornment-match (hier ado) |
913 | "Return the index (level) in hierarchy HIER of adornment ADO. | |
94e9c286 | 914 | This basically just searches for the item using the appropriate |
92439579 | 915 | comparison and returns the index. Return nil if the item is |
94e9c286 SM |
916 | not found." |
917 | (let ((cur hier)) | |
d13c8be6 | 918 | (while (and cur (not (rst-compare-adornments (car cur) ado))) |
94e9c286 SM |
919 | (setq cur (cdr cur))) |
920 | cur)) | |
921 | ||
922 | ||
d13c8be6 SM |
923 | (defun rst-suggest-new-adornment (allados &optional prev) |
924 | "Suggest a new, different adornment from all that have been seen. | |
94e9c286 | 925 | |
d13c8be6 SM |
926 | ALLADOS is the set of all adornments, including the line numbers. |
927 | PREV is the optional previous adornment, in order to suggest a | |
92439579 | 928 | better match." |
94e9c286 | 929 | |
d13c8be6 | 930 | ;; For all the preferred adornments... |
94e9c286 SM |
931 | (let* ( |
932 | ;; If 'prev' is given, reorder the list to start searching after the | |
933 | ;; match. | |
934 | (fplist | |
d13c8be6 | 935 | (cdr (rst-get-adornment-match rst-preferred-adornments prev))) |
94e9c286 SM |
936 | |
937 | ;; List of candidates to search. | |
d13c8be6 | 938 | (curpotential (append fplist rst-preferred-adornments))) |
94e9c286 | 939 | (while |
d13c8be6 SM |
940 | ;; For all the adornments... |
941 | (let ((cur allados) | |
94e9c286 SM |
942 | found) |
943 | (while (and cur (not found)) | |
d13c8be6 | 944 | (if (rst-compare-adornments (car cur) (car curpotential)) |
94e9c286 SM |
945 | ;; Found it! |
946 | (setq found (car curpotential)) | |
947 | (setq cur (cdr cur)))) | |
948 | found) | |
949 | ||
950 | (setq curpotential (cdr curpotential))) | |
951 | ||
b4747519 | 952 | (copy-sequence (car curpotential)))) |
94e9c286 SM |
953 | |
954 | (defun rst-delete-entire-line () | |
955 | "Delete the entire current line without using the `kill-ring'." | |
b4747519 SM |
956 | (delete-region (line-beginning-position) |
957 | (line-beginning-position 2))) | |
94e9c286 SM |
958 | |
959 | (defun rst-update-section (char style &optional indent) | |
d13c8be6 | 960 | "Unconditionally update the style of a section adornment. |
94e9c286 | 961 | |
92439579 JB |
962 | Do this using the given character CHAR, with STYLE 'simple |
963 | or 'over-and-under, and with indent INDENT. If the STYLE | |
964 | is 'simple, whitespace before the title is removed (indent | |
965 | is always assumed to be 0). | |
94e9c286 SM |
966 | |
967 | If there are existing overline and/or underline from the | |
d13c8be6 SM |
968 | existing adornment, they are removed before adding the |
969 | requested adornment." | |
970 | (end-of-line) | |
8bbb7dd8 SM |
971 | (let ((marker (point-marker)) |
972 | len) | |
94e9c286 | 973 | |
6d3f7c2f | 974 | ;; Fixup whitespace at the beginning and end of the line. |
94e9c286 SM |
975 | (if (or (null indent) (eq style 'simple)) |
976 | (setq indent 0)) | |
977 | (beginning-of-line) | |
978 | (delete-horizontal-space) | |
979 | (insert (make-string indent ? )) | |
980 | ||
981 | (end-of-line) | |
982 | (delete-horizontal-space) | |
983 | ||
6d3f7c2f | 984 | ;; Set the current column, we're at the end of the title line. |
94e9c286 SM |
985 | (setq len (+ (current-column) indent)) |
986 | ||
6d3f7c2f | 987 | ;; Remove previous line if it is an adornment. |
94e9c286 SM |
988 | (save-excursion |
989 | (forward-line -1) | |
d13c8be6 SM |
990 | (if (and (looking-at (rst-re 'ado-beg-2-1)) |
991 | ;; Avoid removing the underline of a title right above us. | |
992 | (save-excursion (forward-line -1) | |
993 | (not (looking-at (rst-re 'ttl-beg))))) | |
994 | (rst-delete-entire-line))) | |
995 | ||
6d3f7c2f | 996 | ;; Remove following line if it is an adornment. |
94e9c286 SM |
997 | (save-excursion |
998 | (forward-line +1) | |
d13c8be6 SM |
999 | (if (looking-at (rst-re 'ado-beg-2-1)) |
1000 | (rst-delete-entire-line)) | |
94e9c286 | 1001 | ;; Add a newline if we're at the end of the buffer, for the subsequence |
6d3f7c2f | 1002 | ;; inserting of the underline. |
94e9c286 SM |
1003 | (if (= (point) (buffer-end 1)) |
1004 | (newline 1))) | |
1005 | ||
6d3f7c2f | 1006 | ;; Insert overline. |
94e9c286 SM |
1007 | (if (eq style 'over-and-under) |
1008 | (save-excursion | |
1009 | (beginning-of-line) | |
1010 | (open-line 1) | |
1011 | (insert (make-string len char)))) | |
1012 | ||
6d3f7c2f | 1013 | ;; Insert underline. |
94e9c286 SM |
1014 | (forward-line +1) |
1015 | (open-line 1) | |
1016 | (insert (make-string len char)) | |
1017 | ||
1018 | (forward-line +1) | |
1019 | (goto-char marker) | |
1020 | )) | |
1021 | ||
d13c8be6 SM |
1022 | (defun rst-classify-adornment (adornment end) |
1023 | "Classify adornment for section titles and transitions. | |
1024 | ADORNMENT is the complete adornment string as found in the buffer | |
6d3f7c2f | 1025 | with optional trailing whitespace. END is the point after the |
d13c8be6 | 1026 | last character of ADORNMENT. |
94e9c286 | 1027 | |
6d3f7c2f SM |
1028 | Return a list. The first entry is t for a transition or a |
1029 | cons (CHARACTER . STYLE). Check `rst-preferred-adornments' for | |
d13c8be6 | 1030 | the meaning of CHARACTER and STYLE. |
94e9c286 | 1031 | |
d13c8be6 | 1032 | The remaining list forms four match groups as returned by |
6d3f7c2f SM |
1033 | `match-data'. Match group 0 matches the whole construct. Match |
1034 | group 1 matches the overline adornment if present. Match group 2 | |
1035 | matches the section title text or the transition. Match group 3 | |
d13c8be6 | 1036 | matches the underline adornment. |
94e9c286 | 1037 | |
d13c8be6 SM |
1038 | Return nil if no syntactically valid adornment is found." |
1039 | (save-excursion | |
1040 | (save-match-data | |
1041 | (when (string-match (rst-re 'ado-beg-2-1) adornment) | |
1042 | (goto-char end) | |
1043 | (let* ((ado-ch (string-to-char (match-string 2 adornment))) | |
1044 | (ado-re (rst-re ado-ch 'adorep3-hlp)) | |
1045 | (end-pnt (point)) | |
1046 | (beg-pnt (progn | |
1047 | (forward-line 0) | |
1048 | (point))) | |
c846da43 | 1049 | (nxt-emp ; Next line nonexistent or empty |
d13c8be6 SM |
1050 | (save-excursion |
1051 | (or (not (zerop (forward-line 1))) | |
1052 | (looking-at (rst-re 'lin-end))))) | |
c846da43 | 1053 | (prv-emp ; Previous line nonexistent or empty |
d13c8be6 SM |
1054 | (save-excursion |
1055 | (or (not (zerop (forward-line -1))) | |
1056 | (looking-at (rst-re 'lin-end))))) | |
6d3f7c2f | 1057 | (ttl-blw ; Title found below starting here. |
d13c8be6 SM |
1058 | (save-excursion |
1059 | (and | |
1060 | (zerop (forward-line 1)) | |
1061 | (looking-at (rst-re 'ttl-beg)) | |
1062 | (point)))) | |
6d3f7c2f | 1063 | (ttl-abv ; Title found above starting here. |
d13c8be6 SM |
1064 | (save-excursion |
1065 | (and | |
1066 | (zerop (forward-line -1)) | |
1067 | (looking-at (rst-re 'ttl-beg)) | |
1068 | (point)))) | |
6d3f7c2f | 1069 | (und-fnd ; Matching underline found starting here. |
d13c8be6 SM |
1070 | (save-excursion |
1071 | (and ttl-blw | |
1072 | (zerop (forward-line 2)) | |
1073 | (looking-at (rst-re ado-re 'lin-end)) | |
1074 | (point)))) | |
6d3f7c2f | 1075 | (ovr-fnd ; Matching overline found starting here. |
d13c8be6 SM |
1076 | (save-excursion |
1077 | (and ttl-abv | |
1078 | (zerop (forward-line -2)) | |
1079 | (looking-at (rst-re ado-re 'lin-end)) | |
1080 | (point)))) | |
1081 | key beg-ovr end-ovr beg-txt end-txt beg-und end-und) | |
1082 | (cond | |
1083 | ((and nxt-emp prv-emp) | |
6d3f7c2f | 1084 | ;; A transition. |
d13c8be6 SM |
1085 | (setq key t |
1086 | beg-txt beg-pnt | |
1087 | end-txt end-pnt)) | |
1088 | ((or und-fnd ovr-fnd) | |
6d3f7c2f | 1089 | ;; An overline with an underline. |
d13c8be6 | 1090 | (setq key (cons ado-ch 'over-and-under)) |
6d3f7c2f | 1091 | (let (;; Prefer overline match over underline match. |
d13c8be6 SM |
1092 | (und-pnt (if ovr-fnd beg-pnt und-fnd)) |
1093 | (ovr-pnt (if ovr-fnd ovr-fnd beg-pnt)) | |
1094 | (txt-pnt (if ovr-fnd ttl-abv ttl-blw))) | |
1095 | (goto-char ovr-pnt) | |
1096 | (setq beg-ovr (point) | |
1097 | end-ovr (line-end-position)) | |
1098 | (goto-char txt-pnt) | |
1099 | (setq beg-txt (point) | |
1100 | end-txt (line-end-position)) | |
1101 | (goto-char und-pnt) | |
1102 | (setq beg-und (point) | |
1103 | end-und (line-end-position)))) | |
1104 | (ttl-abv | |
6d3f7c2f | 1105 | ;; An underline. |
d13c8be6 SM |
1106 | (setq key (cons ado-ch 'simple) |
1107 | beg-und beg-pnt | |
1108 | end-und end-pnt) | |
1109 | (goto-char ttl-abv) | |
1110 | (setq beg-txt (point) | |
1111 | end-txt (line-end-position))) | |
1112 | (t | |
6d3f7c2f | 1113 | ;; Invalid adornment. |
d13c8be6 SM |
1114 | (setq key nil))) |
1115 | (if key | |
1116 | (list key | |
1117 | (or beg-ovr beg-txt beg-und) | |
1118 | (or end-und end-txt end-ovr) | |
1119 | beg-ovr end-ovr beg-txt end-txt beg-und end-und))))))) | |
1120 | ||
1121 | (defun rst-find-title-line () | |
1122 | "Find a section title line around point and return its characteristics. | |
1123 | If the point is on an adornment line find the respective title | |
6d3f7c2f SM |
1124 | line. If the point is on an empty line check previous or next |
1125 | line whether it is a suitable title line and use it if so. If | |
d13c8be6 SM |
1126 | point is on a suitable title line use it. |
1127 | ||
1128 | If no title line is found return nil. | |
1129 | ||
6d3f7c2f | 1130 | Otherwise return as `rst-classify-adornment' does. However, if |
d13c8be6 | 1131 | the title line has no syntactically valid adornment STYLE is nil |
6d3f7c2f | 1132 | in the first element. If there is no adornment around the title |
d13c8be6 SM |
1133 | CHARACTER is also nil and match groups for overline and underline |
1134 | are nil." | |
1135 | (save-excursion | |
1136 | (forward-line 0) | |
1137 | (let ((orig-pnt (point)) | |
1138 | (orig-end (line-end-position))) | |
1139 | (cond | |
1140 | ((looking-at (rst-re 'ado-beg-2-1)) | |
1141 | (let ((char (string-to-char (match-string-no-properties 2))) | |
1142 | (r (rst-classify-adornment (match-string-no-properties 0) | |
1143 | (match-end 0)))) | |
1144 | (cond | |
1145 | ((not r) | |
6d3f7c2f | 1146 | ;; Invalid adornment - check whether this is an incomplete overline. |
d13c8be6 SM |
1147 | (if (and |
1148 | (zerop (forward-line 1)) | |
1149 | (looking-at (rst-re 'ttl-beg))) | |
1150 | (list (cons char nil) orig-pnt (line-end-position) | |
1151 | orig-pnt orig-end (point) (line-end-position) nil nil))) | |
1152 | ((consp (car r)) | |
6d3f7c2f | 1153 | ;; A section title - not a transition. |
d13c8be6 SM |
1154 | r)))) |
1155 | ((looking-at (rst-re 'lin-end)) | |
1156 | (or | |
1157 | (save-excursion | |
1158 | (if (and (zerop (forward-line -1)) | |
1159 | (looking-at (rst-re 'ttl-beg))) | |
1160 | (list (cons nil nil) (point) (line-end-position) | |
1161 | nil nil (point) (line-end-position) nil nil))) | |
1162 | (save-excursion | |
1163 | (if (and (zerop (forward-line 1)) | |
1164 | (looking-at (rst-re 'ttl-beg))) | |
1165 | (list (cons nil nil) (point) (line-end-position) | |
1166 | nil nil (point) (line-end-position) nil nil))))) | |
1167 | ((looking-at (rst-re 'ttl-beg)) | |
6d3f7c2f | 1168 | ;; Try to use the underline. |
d13c8be6 | 1169 | (let ((r (rst-classify-adornment |
8f6b6da8 | 1170 | (buffer-substring-no-properties |
d13c8be6 SM |
1171 | (line-beginning-position 2) (line-end-position 2)) |
1172 | (line-end-position 2)))) | |
1173 | (if r | |
1174 | r | |
6d3f7c2f | 1175 | ;; No valid adornment found. |
d13c8be6 SM |
1176 | (list (cons nil nil) (point) (line-end-position) |
1177 | nil nil (point) (line-end-position) nil nil)))))))) | |
1178 | ||
1179 | ;; The following function and variables are used to maintain information about | |
1180 | ;; current section adornment in a buffer local cache. Thus they can be used for | |
1181 | ;; font-locking and manipulation commands. | |
1182 | ||
d13c8be6 SM |
1183 | (defvar rst-all-sections nil |
1184 | "All section adornments in the buffer as found by `rst-find-all-adornments'. | |
1185 | t when no section adornments were found.") | |
1186 | (make-variable-buffer-local 'rst-all-sections) | |
1187 | ||
1188 | ;; FIXME: If this variable is set to a different value font-locking of section | |
6d3f7c2f | 1189 | ;; headers is wrong. |
d13c8be6 SM |
1190 | (defvar rst-section-hierarchy nil |
1191 | "Section hierarchy in the buffer as determined by `rst-get-hierarchy'. | |
6d3f7c2f | 1192 | t when no section adornments were found. Value depends on |
d13c8be6 SM |
1193 | `rst-all-sections'.") |
1194 | (make-variable-buffer-local 'rst-section-hierarchy) | |
1195 | ||
8f6b6da8 JB |
1196 | (defun rst-reset-section-caches () |
1197 | "Reset all section cache variables. | |
1198 | Should be called by interactive functions which deal with sections." | |
1199 | (setq rst-all-sections nil | |
1200 | rst-section-hierarchy nil)) | |
1201 | ||
d13c8be6 SM |
1202 | (defun rst-find-all-adornments () |
1203 | "Return all the section adornments in the current buffer. | |
1204 | Return a list of (LINE . ADORNMENT) with ascending LINE where | |
6d3f7c2f | 1205 | LINE is the line containing the section title. ADORNMENT consists |
d13c8be6 SM |
1206 | of a (CHARACTER STYLE INDENT) triple as described for |
1207 | `rst-preferred-adornments'. | |
1208 | ||
1209 | Uses and sets `rst-all-sections'." | |
1210 | (unless rst-all-sections | |
1211 | (let (positions) | |
1212 | ;; Iterate over all the section titles/adornments in the file. | |
1213 | (save-excursion | |
1214 | (goto-char (point-min)) | |
1215 | (while (re-search-forward (rst-re 'ado-beg-2-1) nil t) | |
1216 | (let ((ado-data (rst-classify-adornment | |
1217 | (match-string-no-properties 0) (point)))) | |
1218 | (when (and ado-data | |
6d3f7c2f | 1219 | (consp (car ado-data))) ; Ignore transitions. |
d13c8be6 | 1220 | (set-match-data (cdr ado-data)) |
6d3f7c2f | 1221 | (goto-char (match-beginning 2)) ; Goto the title start. |
d13c8be6 SM |
1222 | (push (cons (1+ (count-lines (point-min) (point))) |
1223 | (list (caar ado-data) | |
1224 | (cdar ado-data) | |
1225 | (current-indentation))) | |
1226 | positions) | |
6d3f7c2f | 1227 | (goto-char (match-end 0))))) ; Go beyond the whole thing. |
d13c8be6 SM |
1228 | (setq positions (nreverse positions)) |
1229 | (setq rst-all-sections (or positions t))))) | |
1230 | (if (eq rst-all-sections t) | |
1231 | nil | |
1232 | rst-all-sections)) | |
1233 | ||
1234 | (defun rst-infer-hierarchy (adornments) | |
1235 | "Build a hierarchy of adornments using the list of given ADORNMENTS. | |
1236 | ||
1237 | ADORNMENTS is a list of (CHARACTER STYLE INDENT) adornment | |
94e9c286 | 1238 | specifications, in order that they appear in a file, and will |
d13c8be6 SM |
1239 | infer a hierarchy of section levels by removing adornments that |
1240 | have already been seen in a forward traversal of the adornments, | |
1241 | comparing just CHARACTER and STYLE. | |
94e9c286 | 1242 | |
d13c8be6 | 1243 | Similarly returns a list of (CHARACTER STYLE INDENT), where each |
94e9c286 | 1244 | list element should be unique." |
d13c8be6 SM |
1245 | (let (hierarchy-alist) |
1246 | (dolist (x adornments) | |
94e9c286 SM |
1247 | (let ((char (car x)) |
1248 | (style (cadr x))) | |
1249 | (unless (assoc (cons char style) hierarchy-alist) | |
d13c8be6 SM |
1250 | (push (cons (cons char style) x) hierarchy-alist)))) |
1251 | (mapcar 'cdr (nreverse hierarchy-alist)))) | |
94e9c286 | 1252 | |
d13c8be6 | 1253 | (defun rst-get-hierarchy (&optional ignore) |
94e9c286 SM |
1254 | "Return the hierarchy of section titles in the file. |
1255 | ||
d13c8be6 | 1256 | Return a list of adornments that represents the hierarchy of |
6d3f7c2f SM |
1257 | section titles in the file. Each element consists of (CHARACTER |
1258 | STYLE INDENT) as described for `rst-find-all-adornments'. If the | |
d13c8be6 SM |
1259 | line number in IGNORE is specified, a possibly adornment found on |
1260 | that line is not taken into account when building the hierarchy. | |
1261 | ||
1262 | Uses and sets `rst-section-hierarchy' unless IGNORE is given." | |
1263 | (if (and (not ignore) rst-section-hierarchy) | |
1264 | (if (eq rst-section-hierarchy t) | |
1265 | nil | |
1266 | rst-section-hierarchy) | |
1267 | (let ((r (rst-infer-hierarchy | |
1268 | (mapcar 'cdr | |
1269 | (assq-delete-all | |
1270 | ignore | |
1271 | (rst-find-all-adornments)))))) | |
1272 | (setq rst-section-hierarchy | |
1273 | (if ignore | |
1274 | ;; Clear cache reflecting that a possible update is not | |
6d3f7c2f | 1275 | ;; reflected. |
d13c8be6 SM |
1276 | nil |
1277 | (or r t))) | |
1278 | r))) | |
1279 | ||
1280 | (defun rst-get-adornments-around () | |
1281 | "Return the adornments around point. | |
1282 | Return a list of the previous and next adornments." | |
1283 | (let* ((all (rst-find-all-adornments)) | |
94e9c286 SM |
1284 | (curline (line-number-at-pos)) |
1285 | prev next | |
1286 | (cur all)) | |
1287 | ||
d13c8be6 | 1288 | ;; Search for the adornments around the current line. |
94e9c286 SM |
1289 | (while (and cur (< (caar cur) curline)) |
1290 | (setq prev cur | |
1291 | cur (cdr cur))) | |
d13c8be6 | 1292 | ;; 'cur' is the following adornment. |
94e9c286 SM |
1293 | |
1294 | (if (and cur (caar cur)) | |
1295 | (setq next (if (= curline (caar cur)) (cdr cur) cur))) | |
1296 | ||
1297 | (mapcar 'cdar (list prev next)) | |
1298 | )) | |
1299 | ||
1300 | ||
d13c8be6 SM |
1301 | (defun rst-adornment-complete-p (ado) |
1302 | "Return true if the adornment ADO around point is complete." | |
94e9c286 SM |
1303 | ;; Note: we assume that the detection of the overline as being the underline |
1304 | ;; of a preceding title has already been detected, and has been eliminated | |
d13c8be6 | 1305 | ;; from the adornment that is given to us. |
94e9c286 SM |
1306 | |
1307 | ;; There is some sectioning already present, so check if the current | |
1308 | ;; sectioning is complete and correct. | |
d13c8be6 SM |
1309 | (let* ((char (car ado)) |
1310 | (style (cadr ado)) | |
1311 | (indent (caddr ado)) | |
94e9c286 SM |
1312 | (endcol (save-excursion (end-of-line) (current-column))) |
1313 | ) | |
1314 | (if char | |
d13c8be6 | 1315 | (let ((exps (rst-re "^" char (format "\\{%d\\}" (+ endcol indent)) "$"))) |
94e9c286 SM |
1316 | (and |
1317 | (save-excursion (forward-line +1) | |
1318 | (beginning-of-line) | |
1319 | (looking-at exps)) | |
1320 | (or (not (eq style 'over-and-under)) | |
1321 | (save-excursion (forward-line -1) | |
1322 | (beginning-of-line) | |
1323 | (looking-at exps)))) | |
1324 | )) | |
1325 | )) | |
1326 | ||
1327 | ||
d13c8be6 SM |
1328 | (defun rst-get-next-adornment |
1329 | (curado hier &optional suggestion reverse-direction) | |
1330 | "Get the next adornment for CURADO, in given hierarchy HIER. | |
1331 | If suggesting, suggest for new adornment SUGGESTION. | |
94e9c286 SM |
1332 | REVERSE-DIRECTION is used to reverse the cycling order." |
1333 | ||
1334 | (let* ( | |
d13c8be6 SM |
1335 | (char (car curado)) |
1336 | (style (cadr curado)) | |
94e9c286 | 1337 | |
d13c8be6 SM |
1338 | ;; Build a new list of adornments for the rotation. |
1339 | (rotados | |
94e9c286 | 1340 | (append hier |
d13c8be6 | 1341 | ;; Suggest a new adornment. |
94e9c286 | 1342 | (list suggestion |
d13c8be6 | 1343 | ;; If nothing to suggest, use first adornment. |
94e9c286 SM |
1344 | (car hier)))) ) |
1345 | (or | |
d13c8be6 | 1346 | ;; Search for next adornment. |
94e9c286 | 1347 | (cadr |
d13c8be6 SM |
1348 | (let ((cur (if reverse-direction rotados |
1349 | (reverse rotados)))) | |
94e9c286 SM |
1350 | (while (and cur |
1351 | (not (and (eq char (caar cur)) | |
1352 | (eq style (cadar cur))))) | |
1353 | (setq cur (cdr cur))) | |
1354 | cur)) | |
1355 | ||
d13c8be6 | 1356 | ;; If not found, take the first of all adornments. |
94e9c286 SM |
1357 | suggestion |
1358 | ))) | |
1359 | ||
1360 | ||
6d3f7c2f | 1361 | ;; FIXME: A line "``/`` full" is not accepted as a section title. |
d13c8be6 SM |
1362 | (defun rst-adjust (pfxarg) |
1363 | "Auto-adjust the adornment around point. | |
94e9c286 | 1364 | |
6d3f7c2f SM |
1365 | Adjust/rotate the section adornment for the section title around |
1366 | point or promote/demote the adornments inside the region, | |
94e9c286 | 1367 | depending on if the region is active. This function is meant to |
fffa137c | 1368 | be invoked possibly multiple times, and can vary its behavior |
6d3f7c2f SM |
1369 | with a positive PFXARG (toggle style), or with a negative |
1370 | PFXARG (alternate behavior). | |
94e9c286 | 1371 | |
6d3f7c2f SM |
1372 | This function is a bit of a swiss knife. It is meant to adjust |
1373 | the adornments of a section title in reStructuredText. It tries | |
d13c8be6 SM |
1374 | to deal with all the possible cases gracefully and to do `the |
1375 | right thing' in all cases. | |
94e9c286 | 1376 | |
d13c8be6 | 1377 | See the documentations of `rst-adjust-adornment-work' and |
94e9c286 SM |
1378 | `rst-promote-region' for full details. |
1379 | ||
1380 | Prefix Arguments | |
1381 | ================ | |
1382 | ||
1383 | The method can take either (but not both) of | |
1384 | ||
1385 | a. a (non-negative) prefix argument, which means to toggle the | |
6d3f7c2f | 1386 | adornment style. Invoke with a prefix argument for example; |
94e9c286 SM |
1387 | |
1388 | b. a negative numerical argument, which generally inverts the | |
1389 | direction of search in the file or hierarchy. Invoke with C-- | |
1390 | prefix for example." | |
d13c8be6 | 1391 | (interactive "P") |
94e9c286 SM |
1392 | |
1393 | (let* (;; Save our original position on the current line. | |
8bbb7dd8 | 1394 | (origpt (point-marker)) |
94e9c286 | 1395 | |
d13c8be6 SM |
1396 | (reverse-direction (and pfxarg (< (prefix-numeric-value pfxarg) 0))) |
1397 | (toggle-style (and pfxarg (not reverse-direction)))) | |
94e9c286 SM |
1398 | |
1399 | (if (rst-portable-mark-active-p) | |
d13c8be6 SM |
1400 | ;; Adjust adornments within region. |
1401 | (rst-promote-region (and pfxarg t)) | |
1402 | ;; Adjust adornment around point. | |
1403 | (rst-adjust-adornment-work toggle-style reverse-direction)) | |
94e9c286 SM |
1404 | |
1405 | ;; Run the hooks to run after adjusting. | |
1406 | (run-hooks 'rst-adjust-hook) | |
1407 | ||
1408 | ;; Make sure to reset the cursor position properly after we're done. | |
1409 | (goto-char origpt) | |
1410 | ||
1411 | )) | |
1412 | ||
d13c8be6 SM |
1413 | (defcustom rst-adjust-hook nil |
1414 | "Hooks to be run after running `rst-adjust'." | |
1415 | :group 'rst-adjust | |
1416 | :type '(hook) | |
1417 | :package-version '(rst . "1.1.0")) | |
94e9c286 | 1418 | |
d13c8be6 SM |
1419 | (defcustom rst-new-adornment-down nil |
1420 | "Controls level of new adornment for section headers." | |
1421 | :group 'rst-adjust | |
1422 | :type '(choice | |
1423 | (const :tag "Same level as previous one" nil) | |
1424 | (const :tag "One level down relative to the previous one" t)) | |
1425 | :package-version '(rst . "1.1.0")) | |
94e9c286 | 1426 | |
d13c8be6 SM |
1427 | (defun rst-adjust-adornment (pfxarg) |
1428 | "Call `rst-adjust-adornment-work' interactively. | |
1429 | ||
6d3f7c2f SM |
1430 | Keep this for compatibility for older bindings (are there any?). |
1431 | Argument PFXARG has the same meaning as for `rst-adjust'." | |
d13c8be6 SM |
1432 | (interactive "P") |
1433 | ||
1434 | (let* ((reverse-direction (and pfxarg (< (prefix-numeric-value pfxarg) 0))) | |
1435 | (toggle-style (and pfxarg (not reverse-direction)))) | |
1436 | (rst-adjust-adornment-work toggle-style reverse-direction))) | |
1437 | ||
1438 | (defun rst-adjust-adornment-work (toggle-style reverse-direction) | |
1439 | "Adjust/rotate the section adornment for the section title around point. | |
94e9c286 SM |
1440 | |
1441 | This function is meant to be invoked possibly multiple times, and | |
fffa137c | 1442 | can vary its behavior with a true TOGGLE-STYLE argument, or with |
94e9c286 SM |
1443 | a REVERSE-DIRECTION argument. |
1444 | ||
92439579 JB |
1445 | General Behavior |
1446 | ================ | |
94e9c286 SM |
1447 | |
1448 | The next action it takes depends on context around the point, and | |
1449 | it is meant to be invoked possibly more than once to rotate among | |
1450 | the various possibilities. Basically, this function deals with: | |
1451 | ||
d13c8be6 | 1452 | - adding a adornment if the title does not have one; |
94e9c286 SM |
1453 | |
1454 | - adjusting the length of the underline characters to fit a | |
1455 | modified title; | |
1456 | ||
d13c8be6 SM |
1457 | - rotating the adornment in the set of already existing |
1458 | sectioning adornments used in the file; | |
94e9c286 SM |
1459 | |
1460 | - switching between simple and over-and-under styles. | |
1461 | ||
1462 | You should normally not have to read all the following, just | |
1463 | invoke the method and it will do the most obvious thing that you | |
1464 | would expect. | |
1465 | ||
1466 | ||
d13c8be6 SM |
1467 | Adornment Definitions |
1468 | ===================== | |
94e9c286 | 1469 | |
d13c8be6 | 1470 | The adornments consist in |
94e9c286 SM |
1471 | |
1472 | 1. a CHARACTER | |
1473 | ||
1474 | 2. a STYLE which can be either of 'simple' or 'over-and-under'. | |
1475 | ||
1476 | 3. an INDENT (meaningful for the over-and-under style only) | |
1477 | which determines how many characters and over-and-under | |
1478 | style is hanging outside of the title at the beginning and | |
1479 | ending. | |
1480 | ||
1481 | See source code for mode details. | |
1482 | ||
1483 | ||
92439579 JB |
1484 | Detailed Behavior Description |
1485 | ============================= | |
94e9c286 SM |
1486 | |
1487 | Here are the gory details of the algorithm (it seems quite | |
1488 | complicated, but really, it does the most obvious thing in all | |
1489 | the particular cases): | |
1490 | ||
d13c8be6 | 1491 | Before applying the adornment change, the cursor is placed on |
94e9c286 SM |
1492 | the closest line that could contain a section title. |
1493 | ||
d13c8be6 SM |
1494 | Case 1: No Adornment |
1495 | -------------------- | |
94e9c286 | 1496 | |
d13c8be6 | 1497 | If the current line has no adornment around it, |
94e9c286 | 1498 | |
d13c8be6 SM |
1499 | - search backwards for the last previous adornment, and apply |
1500 | the adornment one level lower to the current line. If there | |
1501 | is no defined level below this previous adornment, we suggest | |
1502 | the most appropriate of the `rst-preferred-adornments'. | |
94e9c286 SM |
1503 | |
1504 | If REVERSE-DIRECTION is true, we simply use the previous | |
d13c8be6 | 1505 | adornment found directly. |
94e9c286 | 1506 | |
d13c8be6 SM |
1507 | - if there is no adornment found in the given direction, we use |
1508 | the first of `rst-preferred-adornments'. | |
94e9c286 | 1509 | |
d13c8be6 | 1510 | TOGGLE-STYLE forces a toggle of the prescribed adornment style. |
94e9c286 | 1511 | |
d13c8be6 SM |
1512 | Case 2: Incomplete Adornment |
1513 | ---------------------------- | |
94e9c286 | 1514 | |
d13c8be6 SM |
1515 | If the current line does have an existing adornment, but the |
1516 | adornment is incomplete, that is, the underline/overline does | |
94e9c286 SM |
1517 | not extend to exactly the end of the title line (it is either too |
1518 | short or too long), we simply extend the length of the | |
1519 | underlines/overlines to fit exactly the section title. | |
1520 | ||
d13c8be6 | 1521 | If TOGGLE-STYLE we toggle the style of the adornment as well. |
94e9c286 SM |
1522 | |
1523 | REVERSE-DIRECTION has no effect in this case. | |
1524 | ||
d13c8be6 SM |
1525 | Case 3: Complete Existing Adornment |
1526 | ----------------------------------- | |
94e9c286 | 1527 | |
d13c8be6 | 1528 | If the adornment is complete (i.e. the underline (overline) |
94e9c286 SM |
1529 | length is already adjusted to the end of the title line), we |
1530 | search/parse the file to establish the hierarchy of all the | |
d13c8be6 SM |
1531 | adornments (making sure not to include the adornment around |
1532 | point), and we rotate the current title's adornment from within | |
94e9c286 SM |
1533 | that list (by default, going *down* the hierarchy that is present |
1534 | in the file, i.e. to a lower section level). This is meant to be | |
d13c8be6 | 1535 | used potentially multiple times, until the desired adornment is |
94e9c286 SM |
1536 | found around the title. |
1537 | ||
1538 | If we hit the boundary of the hierarchy, exactly one choice from | |
d13c8be6 SM |
1539 | the list of preferred adornments is suggested/chosen, the first |
1540 | of those adornment that has not been seen in the file yet (and | |
1541 | not including the adornment around point), and the next | |
94e9c286 SM |
1542 | invocation rolls over to the other end of the hierarchy (i.e. it |
1543 | cycles). This allows you to avoid having to set which character | |
92439579 | 1544 | to use. |
94e9c286 SM |
1545 | |
1546 | If REVERSE-DIRECTION is true, the effect is to change the | |
d13c8be6 | 1547 | direction of rotation in the hierarchy of adornments, thus |
94e9c286 SM |
1548 | instead going *up* the hierarchy. |
1549 | ||
d13c8be6 SM |
1550 | However, if TOGGLE-STYLE, we do not rotate the adornment, but |
1551 | instead simply toggle the style of the current adornment (this | |
1552 | should be the most common way to toggle the style of an existing | |
1553 | complete adornment). | |
94e9c286 SM |
1554 | |
1555 | ||
1556 | Point Location | |
1557 | ============== | |
1558 | ||
1559 | The invocation of this function can be carried out anywhere | |
1560 | within the section title line, on an existing underline or | |
1561 | overline, as well as on an empty line following a section title. | |
1562 | This is meant to be as convenient as possible. | |
1563 | ||
1564 | ||
1565 | Indented Sections | |
1566 | ================= | |
1567 | ||
1568 | Indented section titles such as :: | |
1569 | ||
1570 | My Title | |
1571 | -------- | |
1572 | ||
d13c8be6 | 1573 | are invalid in reStructuredText and thus not recognized by the |
94e9c286 SM |
1574 | parser. This code will thus not work in a way that would support |
1575 | indented sections (it would be ambiguous anyway). | |
1576 | ||
1577 | ||
1578 | Joint Sections | |
1579 | ============== | |
1580 | ||
1581 | Section titles that are right next to each other may not be | |
1582 | treated well. More work might be needed to support those, and | |
d13c8be6 | 1583 | special conditions on the completeness of existing adornments |
94e9c286 SM |
1584 | might be required to make it non-ambiguous. |
1585 | ||
d13c8be6 SM |
1586 | For now we assume that the adornments are disjoint, that is, |
1587 | there is at least a single line between the titles/adornment | |
1588 | lines." | |
1589 | (rst-reset-section-caches) | |
1590 | (let ((ttl-fnd (rst-find-title-line)) | |
1591 | (orig-pnt (point))) | |
1592 | (when ttl-fnd | |
1593 | (set-match-data (cdr ttl-fnd)) | |
1594 | (goto-char (match-beginning 2)) | |
1595 | (let* ((moved (- (line-number-at-pos) (line-number-at-pos orig-pnt))) | |
1596 | (char (caar ttl-fnd)) | |
1597 | (style (cdar ttl-fnd)) | |
1598 | (indent (current-indentation)) | |
1599 | (curado (list char style indent)) | |
1600 | char-new style-new indent-new) | |
1601 | (cond | |
1602 | ;;------------------------------------------------------------------- | |
1603 | ;; Case 1: No valid adornment | |
1604 | ((not style) | |
1605 | (let ((prev (car (rst-get-adornments-around))) | |
1606 | cur | |
1607 | (hier (rst-get-hierarchy))) | |
1608 | ;; Advance one level down. | |
1609 | (setq cur | |
1610 | (if prev | |
1611 | (if (or (and rst-new-adornment-down reverse-direction) | |
1612 | (and (not rst-new-adornment-down) | |
1613 | (not reverse-direction))) | |
1614 | prev | |
1615 | (or (cadr (rst-get-adornment-match hier prev)) | |
1616 | (rst-suggest-new-adornment hier prev))) | |
1617 | (copy-sequence (car rst-preferred-adornments)))) | |
1618 | ;; Invert the style if requested. | |
1619 | (if toggle-style | |
1620 | (setcar (cdr cur) (if (eq (cadr cur) 'simple) | |
1621 | 'over-and-under 'simple)) ) | |
1622 | (setq char-new (car cur) | |
1623 | style-new (cadr cur) | |
1624 | indent-new (caddr cur)))) | |
1625 | ;;------------------------------------------------------------------- | |
1626 | ;; Case 2: Incomplete Adornment | |
1627 | ((not (rst-adornment-complete-p curado)) | |
1628 | ;; Invert the style if requested. | |
1629 | (if toggle-style | |
1630 | (setq style (if (eq style 'simple) 'over-and-under 'simple))) | |
1631 | (setq char-new char | |
1632 | style-new style | |
1633 | indent-new indent)) | |
1634 | ;;------------------------------------------------------------------- | |
1635 | ;; Case 3: Complete Existing Adornment | |
1636 | (t | |
1637 | (if toggle-style | |
1638 | ;; Simply switch the style of the current adornment. | |
1639 | (setq char-new char | |
1640 | style-new (if (eq style 'simple) 'over-and-under 'simple) | |
1641 | indent-new rst-default-indent) | |
1642 | ;; Else, we rotate, ignoring the adornment around the current | |
1643 | ;; line... | |
1644 | (let* ((hier (rst-get-hierarchy (line-number-at-pos))) | |
6d3f7c2f | 1645 | ;; Suggestion, in case we need to come up with something new. |
d13c8be6 SM |
1646 | (suggestion (rst-suggest-new-adornment |
1647 | hier | |
1648 | (car (rst-get-adornments-around)))) | |
1649 | (nextado (rst-get-next-adornment | |
1650 | curado hier suggestion reverse-direction))) | |
1651 | ;; Indent, if present, always overrides the prescribed indent. | |
1652 | (setq char-new (car nextado) | |
1653 | style-new (cadr nextado) | |
1654 | indent-new (caddr nextado)))))) | |
1655 | ;; Override indent with present indent! | |
1656 | (setq indent-new (if (> indent 0) indent indent-new)) | |
1657 | (if (and char-new style-new) | |
1658 | (rst-update-section char-new style-new indent-new)) | |
1659 | ;; Correct the position of the cursor to more accurately reflect where | |
1660 | ;; it was located when the function was invoked. | |
1661 | (unless (zerop moved) | |
1662 | (forward-line (- moved)) | |
1663 | (end-of-line)))))) | |
94e9c286 SM |
1664 | |
1665 | ;; Maintain an alias for compatibility. | |
1666 | (defalias 'rst-adjust-section-title 'rst-adjust) | |
1667 | ||
1668 | ||
d13c8be6 | 1669 | (defun rst-promote-region (demote) |
94e9c286 SM |
1670 | "Promote the section titles within the region. |
1671 | ||
e6438428 JB |
1672 | With argument DEMOTE or a prefix argument, demote the section |
1673 | titles instead. The algorithm used at the boundaries of the | |
d13c8be6 SM |
1674 | hierarchy is similar to that used by `rst-adjust-adornment-work'." |
1675 | (interactive "P") | |
1676 | (rst-reset-section-caches) | |
1677 | (let* ((cur (rst-find-all-adornments)) | |
1678 | (hier (rst-get-hierarchy)) | |
1679 | (suggestion (rst-suggest-new-adornment hier)) | |
94e9c286 SM |
1680 | |
1681 | (region-begin-line (line-number-at-pos (region-beginning))) | |
1682 | (region-end-line (line-number-at-pos (region-end))) | |
1683 | ||
1684 | marker-list | |
1685 | ) | |
1686 | ||
6d3f7c2f | 1687 | ;; Skip the markers that come before the region beginning. |
94e9c286 SM |
1688 | (while (and cur (< (caar cur) region-begin-line)) |
1689 | (setq cur (cdr cur))) | |
1690 | ||
d13c8be6 | 1691 | ;; Create a list of markers for all the adornments which are found within |
94e9c286 SM |
1692 | ;; the region. |
1693 | (save-excursion | |
8bbb7dd8 | 1694 | (let (line) |
94e9c286 | 1695 | (while (and cur (< (setq line (caar cur)) region-end-line)) |
e6ce8c42 GM |
1696 | (goto-char (point-min)) |
1697 | (forward-line (1- line)) | |
8bbb7dd8 | 1698 | (push (list (point-marker) (cdar cur)) marker-list) |
94e9c286 SM |
1699 | (setq cur (cdr cur)) )) |
1700 | ||
1701 | ;; Apply modifications. | |
8bbb7dd8 | 1702 | (dolist (p marker-list) |
d13c8be6 SM |
1703 | ;; Go to the adornment to promote. |
1704 | (goto-char (car p)) | |
8bbb7dd8 | 1705 | |
d13c8be6 SM |
1706 | ;; Update the adornment. |
1707 | (apply 'rst-update-section | |
1708 | ;; Rotate the next adornment. | |
1709 | (rst-get-next-adornment | |
1710 | (cadr p) hier suggestion demote)) | |
8bbb7dd8 | 1711 | |
d13c8be6 SM |
1712 | ;; Clear marker to avoid slowing down the editing after we're done. |
1713 | (set-marker (car p) nil)) | |
94e9c286 | 1714 | (setq deactivate-mark nil) |
8bbb7dd8 | 1715 | ))) |
94e9c286 SM |
1716 | |
1717 | ||
1718 | ||
d13c8be6 SM |
1719 | (defun rst-display-adornments-hierarchy (&optional adornments) |
1720 | "Display the current file's section title adornments hierarchy. | |
1721 | This function expects a list of (CHARACTER STYLE INDENT) triples | |
1722 | in ADORNMENTS." | |
94e9c286 | 1723 | (interactive) |
d13c8be6 SM |
1724 | (rst-reset-section-caches) |
1725 | (if (not adornments) | |
1726 | (setq adornments (rst-get-hierarchy))) | |
94e9c286 SM |
1727 | (with-output-to-temp-buffer "*rest section hierarchy*" |
1728 | (let ((level 1)) | |
1729 | (with-current-buffer standard-output | |
d13c8be6 | 1730 | (dolist (x adornments) |
94e9c286 SM |
1731 | (insert (format "\nSection Level %d" level)) |
1732 | (apply 'rst-update-section x) | |
1733 | (goto-char (point-max)) | |
1734 | (insert "\n") | |
1735 | (incf level) | |
1736 | )) | |
1737 | ))) | |
1738 | ||
b4747519 SM |
1739 | (defun rst-position (elem list) |
1740 | "Return position of ELEM in LIST or nil." | |
1741 | (let ((tail (member elem list))) | |
1742 | (if tail (- (length list) (length tail))))) | |
1743 | ||
d13c8be6 SM |
1744 | (defun rst-straighten-adornments () |
1745 | "Redo all the adornments in the current buffer. | |
1746 | This is done using our preferred set of adornments. This can be | |
94e9c286 SM |
1747 | used, for example, when using somebody else's copy of a document, |
1748 | in order to adapt it to our preferred style." | |
1749 | (interactive) | |
d13c8be6 | 1750 | (rst-reset-section-caches) |
94e9c286 | 1751 | (save-excursion |
6d3f7c2f | 1752 | (let (;; Get a list of pairs of (level . marker). |
d13c8be6 SM |
1753 | (levels-and-markers (mapcar |
1754 | (lambda (ado) | |
1755 | (cons (rst-position (cdr ado) | |
1756 | (rst-get-hierarchy)) | |
1757 | (progn | |
1758 | (goto-char (point-min)) | |
1759 | (forward-line (1- (car ado))) | |
1760 | (point-marker)))) | |
1761 | (rst-find-all-adornments)))) | |
94e9c286 | 1762 | (dolist (lm levels-and-markers) |
6d3f7c2f | 1763 | ;; Go to the appropriate position. |
94e9c286 SM |
1764 | (goto-char (cdr lm)) |
1765 | ||
6d3f7c2f | 1766 | ;; Apply the new style. |
d13c8be6 | 1767 | (apply 'rst-update-section (nth (car lm) rst-preferred-adornments)) |
94e9c286 | 1768 | |
6d3f7c2f | 1769 | ;; Reset the market to avoid slowing down editing until it gets GC'ed. |
94e9c286 SM |
1770 | (set-marker (cdr lm) nil) |
1771 | ) | |
1772 | ))) | |
1773 | ||
1774 | ||
d13c8be6 SM |
1775 | \f |
1776 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1777 | ;; Insert list items | |
1778 | ;; ================= | |
1779 | ||
1780 | ||
1781 | ;================================================= | |
6d3f7c2f | 1782 | ; Borrowed from a2r.el (version 1.3), by Lawrence Mitchell <wence@gmx.li>. |
d13c8be6 SM |
1783 | ; I needed to make some tiny changes to the functions, so I put it here. |
1784 | ; -- Wei-Wei Guo | |
1785 | ||
1786 | (defconst rst-arabic-to-roman | |
1787 | '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD") | |
1788 | (100 . "C") (90 . "XC") (50 . "L") (40 . "XL") | |
1789 | (10 . "X") (9 . "IX") (5 . "V") (4 . "IV") | |
1790 | (1 . "I")) | |
1791 | "List of maps between Arabic numbers and their Roman numeral equivalents.") | |
1792 | ||
1793 | (defun rst-arabic-to-roman (num &optional arg) | |
1794 | "Convert Arabic number NUM to its Roman numeral representation. | |
1795 | ||
1796 | Obviously, NUM must be greater than zero. Don't blame me, blame the | |
1797 | Romans, I mean \"what have the Romans ever _done_ for /us/?\" (with | |
1798 | apologies to Monty Python). | |
1799 | If optional prefix ARG is non-nil, insert in current buffer." | |
1800 | (let ((map rst-arabic-to-roman) | |
1801 | res) | |
1802 | (while (and map (> num 0)) | |
1803 | (if (or (= num (caar map)) | |
1804 | (> num (caar map))) | |
1805 | (setq res (concat res (cdar map)) | |
1806 | num (- num (caar map))) | |
1807 | (setq map (cdr map)))) | |
1808 | res)) | |
1809 | ||
1810 | (defun rst-roman-to-arabic (string &optional arg) | |
1811 | "Convert STRING of Roman numerals to an Arabic number. | |
1812 | ||
1813 | If STRING contains a letter which isn't a valid Roman numeral, the rest | |
1814 | of the string from that point onwards is ignored. | |
1815 | ||
1816 | Hence: | |
1817 | MMD == 2500 | |
1818 | and | |
1819 | MMDFLXXVI == 2500. | |
1820 | If optional ARG is non-nil, insert in current buffer." | |
1821 | (let ((res 0) | |
1822 | (map rst-arabic-to-roman)) | |
1823 | (while map | |
1824 | (if (string-match (concat "^" (cdar map)) string) | |
1825 | (setq res (+ res (caar map)) | |
1826 | string (replace-match "" nil t string)) | |
1827 | (setq map (cdr map)))) | |
1828 | res)) | |
1829 | ;================================================= | |
94e9c286 SM |
1830 | |
1831 | (defun rst-find-pfx-in-region (beg end pfx-re) | |
1832 | "Find all the positions of prefixes in region between BEG and END. | |
6d3f7c2f | 1833 | This is used to find bullets and enumerated list items. PFX-RE is |
d13c8be6 | 1834 | a regular expression for matching the lines after indentation |
6d3f7c2f | 1835 | with items. Returns a list of cons cells consisting of the point |
d13c8be6 | 1836 | and the column of the point." |
8bbb7dd8 | 1837 | (let ((pfx ())) |
94e9c286 SM |
1838 | (save-excursion |
1839 | (goto-char beg) | |
1840 | (while (< (point) end) | |
1841 | (back-to-indentation) | |
1842 | (when (and | |
d13c8be6 | 1843 | (looking-at pfx-re) ; pfx found and... |
94e9c286 SM |
1844 | (let ((pfx-col (current-column))) |
1845 | (save-excursion | |
d13c8be6 | 1846 | (forward-line -1) ; ...previous line is... |
94e9c286 | 1847 | (back-to-indentation) |
d13c8be6 SM |
1848 | (or (looking-at (rst-re 'lin-end)) ; ...empty, |
1849 | (> (current-column) pfx-col) ; ...deeper level, or | |
94e9c286 | 1850 | (and (= (current-column) pfx-col) |
6d3f7c2f | 1851 | (looking-at pfx-re)))))) ; ...pfx at same level. |
b4747519 SM |
1852 | (push (cons (point) (current-column)) |
1853 | pfx)) | |
94e9c286 SM |
1854 | (forward-line 1)) ) |
1855 | (nreverse pfx))) | |
1856 | ||
d13c8be6 | 1857 | (defun rst-insert-list-pos (newitem) |
6d3f7c2f | 1858 | "Arrange relative position of a newly inserted list item of style NEWITEM. |
d13c8be6 SM |
1859 | |
1860 | Adding a new list might consider three situations: | |
94e9c286 | 1861 | |
d13c8be6 SM |
1862 | (a) Current line is a blank line. |
1863 | (b) Previous line is a blank line. | |
1864 | (c) Following line is a blank line. | |
94e9c286 | 1865 | |
d13c8be6 | 1866 | When (a) and (b), just add the new list at current line. |
94e9c286 | 1867 | |
d13c8be6 SM |
1868 | when (a) and not (b), a blank line is added before adding the new list. |
1869 | ||
1870 | When not (a), first forward point to the end of the line, and add two | |
1871 | blank lines, then add the new list. | |
1872 | ||
1873 | Other situations are just ignored and left to users themselves." | |
1874 | (if (save-excursion | |
1875 | (beginning-of-line) | |
1876 | (looking-at (rst-re 'lin-end))) | |
1877 | (if (save-excursion | |
1878 | (forward-line -1) | |
1879 | (looking-at (rst-re 'lin-end))) | |
1880 | (insert newitem " ") | |
1881 | (insert "\n" newitem " ")) | |
1882 | (end-of-line) | |
1883 | (insert "\n\n" newitem " "))) | |
1884 | ||
1885 | (defvar rst-initial-enums | |
1886 | (let (vals) | |
1887 | (dolist (fmt '("%s." "(%s)" "%s)")) | |
1888 | (dolist (c '("1" "a" "A" "I" "i")) | |
1889 | (push (format fmt c) vals))) | |
1890 | (cons "#." (nreverse vals))) | |
1891 | "List of initial enumerations.") | |
1892 | ||
1893 | (defvar rst-initial-items | |
1894 | (append (mapcar 'char-to-string rst-bullets) rst-initial-enums) | |
1895 | "List of initial items. It's collection of bullets and enumerations.") | |
1896 | ||
1897 | (defun rst-insert-list-new-item () | |
1898 | "Insert a new list item. | |
1899 | ||
1900 | User is asked to select the item style first, for example (a), i), +. Use TAB | |
c846da43 | 1901 | for completion and choices. |
d13c8be6 SM |
1902 | |
1903 | If user selects bullets or #, it's just added with position arranged by | |
1904 | `rst-insert-list-pos'. | |
1905 | ||
6d3f7c2f | 1906 | If user selects enumerations, a further prompt is given. User need to input a |
d13c8be6 SM |
1907 | starting item, for example 'e' for 'A)' style. The position is also arranged by |
1908 | `rst-insert-list-pos'." | |
1909 | (interactive) | |
6d3f7c2f | 1910 | ;; FIXME: Make this comply to `interactive' standards. |
d13c8be6 SM |
1911 | (let* ((itemstyle (completing-read |
1912 | "Select preferred item style [#.]: " | |
1913 | rst-initial-items nil t nil nil "#.")) | |
1914 | (cnt (if (string-match (rst-re 'cntexp-tag) itemstyle) | |
1915 | (match-string 0 itemstyle))) | |
1916 | (no | |
1917 | (save-match-data | |
6d3f7c2f | 1918 | ;; FIXME: Make this comply to `interactive' standards. |
d13c8be6 SM |
1919 | (cond |
1920 | ((equal cnt "a") | |
1921 | (let ((itemno (read-string "Give starting value [a]: " | |
1922 | nil nil "a"))) | |
1923 | (downcase (substring itemno 0 1)))) | |
1924 | ((equal cnt "A") | |
1925 | (let ((itemno (read-string "Give starting value [A]: " | |
1926 | nil nil "A"))) | |
1927 | (upcase (substring itemno 0 1)))) | |
1928 | ((equal cnt "I") | |
1929 | (let ((itemno (read-number "Give starting value [1]: " 1))) | |
1930 | (rst-arabic-to-roman itemno))) | |
1931 | ((equal cnt "i") | |
1932 | (let ((itemno (read-number "Give starting value [1]: " 1))) | |
1933 | (downcase (rst-arabic-to-roman itemno)))) | |
1934 | ((equal cnt "1") | |
1935 | (let ((itemno (read-number "Give starting value [1]: " 1))) | |
1936 | (number-to-string itemno))))))) | |
1937 | (if no | |
1938 | (setq itemstyle (replace-match no t t itemstyle))) | |
1939 | (rst-insert-list-pos itemstyle))) | |
1940 | ||
1941 | (defcustom rst-preferred-bullets | |
1942 | '(?* ?- ?+) | |
1943 | "List of favorite bullets." | |
1944 | :group 'rst | |
1945 | :type `(repeat | |
1946 | (choice ,@(mapcar (lambda (char) | |
1947 | (list 'const | |
1948 | :tag (char-to-string char) char)) | |
1949 | rst-bullets))) | |
1950 | :package-version '(rst . "1.1.0")) | |
1951 | ||
1952 | (defun rst-insert-list-continue (curitem prefer-roman) | |
6d3f7c2f SM |
1953 | "Insert a list item with list start CURITEM including its indentation level. |
1954 | If PREFER-ROMAN roman numbering is preferred over using letters." | |
d13c8be6 SM |
1955 | (end-of-line) |
1956 | (insert | |
6d3f7c2f | 1957 | "\n" ; FIXME: Separating lines must be possible. |
d13c8be6 SM |
1958 | (cond |
1959 | ((string-match (rst-re '(:alt enmaut-tag | |
1960 | bul-tag)) curitem) | |
1961 | curitem) | |
1962 | ((string-match (rst-re 'num-tag) curitem) | |
1963 | (replace-match (number-to-string | |
1964 | (1+ (string-to-number (match-string 0 curitem)))) | |
1965 | nil nil curitem)) | |
1966 | ((and (string-match (rst-re 'rom-tag) curitem) | |
1967 | (save-match-data | |
6d3f7c2f | 1968 | (if (string-match (rst-re 'ltr-tag) curitem) ; Also a letter tag. |
d13c8be6 SM |
1969 | (save-excursion |
1970 | ;; FIXME: Assumes one line list items without separating | |
6d3f7c2f | 1971 | ;; empty lines. |
d13c8be6 SM |
1972 | (if (and (zerop (forward-line -1)) |
1973 | (looking-at (rst-re 'enmexp-beg))) | |
1974 | (string-match | |
1975 | (rst-re 'rom-tag) | |
6d3f7c2f SM |
1976 | (match-string 0)) ; Previous was a roman tag. |
1977 | prefer-roman)) ; Don't know - use flag. | |
1978 | t))) ; Not a letter tag. | |
d13c8be6 SM |
1979 | (replace-match |
1980 | (let* ((old (match-string 0 curitem)) | |
1981 | (new (save-match-data | |
1982 | (rst-arabic-to-roman | |
1983 | (1+ (rst-roman-to-arabic | |
1984 | (upcase old))))))) | |
1985 | (if (equal old (upcase old)) | |
1986 | (upcase new) | |
1987 | (downcase new))) | |
1988 | t nil curitem)) | |
1989 | ((string-match (rst-re 'ltr-tag) curitem) | |
1990 | (replace-match (char-to-string | |
1991 | (1+ (string-to-char (match-string 0 curitem)))) | |
1992 | nil nil curitem))))) | |
1993 | ||
1994 | ||
1995 | (defun rst-insert-list (&optional prefer-roman) | |
1996 | "Insert a list item at the current point. | |
1997 | ||
6d3f7c2f SM |
1998 | The command can insert a new list or a continuing list. When it is called at a |
1999 | non-list line, it will promote to insert new list. When it is called at a list | |
d13c8be6 SM |
2000 | line, it will insert a list with the same list style. |
2001 | ||
2002 | 1. When inserting a new list: | |
2003 | ||
6d3f7c2f | 2004 | User is asked to select the item style first, for example (a), i), +. Use TAB |
c846da43 | 2005 | for completion and choices. |
d13c8be6 SM |
2006 | |
2007 | (a) If user selects bullets or #, it's just added. | |
2008 | (b) If user selects enumerations, a further prompt is given. User needs to | |
2009 | input a starting item, for example 'e' for 'A)' style. | |
2010 | ||
2011 | The position of the new list is arranged according to whether or not the | |
2012 | current line and the previous line are blank lines. | |
2013 | ||
2014 | 2. When continuing a list, one thing need to be noticed: | |
2015 | ||
2016 | List style alphabetical list, such as 'a.', and roman numerical list, such as | |
2017 | 'i.', have some overlapping items, for example 'v.' The function can deal with | |
2018 | the problem elegantly in most situations. But when those overlapped list are | |
2019 | preceded by a blank line, it is hard to determine which type to use | |
2020 | automatically. The function uses alphabetical list by default. If you want | |
6d3f7c2f | 2021 | roman numerical list, just use a prefix to set PREFER-ROMAN." |
d13c8be6 SM |
2022 | (interactive "P") |
2023 | (beginning-of-line) | |
2024 | (if (looking-at (rst-re 'itmany-beg-1)) | |
2025 | (rst-insert-list-continue (match-string 0) prefer-roman) | |
2026 | (rst-insert-list-new-item))) | |
94e9c286 SM |
2027 | |
2028 | (defun rst-straighten-bullets-region (beg end) | |
2029 | "Make all the bulleted list items in the region consistent. | |
2030 | The region is specified between BEG and END. You can use this | |
2031 | after you have merged multiple bulleted lists to make them use | |
2032 | the same/correct/consistent bullet characters. | |
2033 | ||
2034 | See variable `rst-preferred-bullets' for the list of bullets to | |
2035 | adjust. If bullets are found on levels beyond the | |
2036 | `rst-preferred-bullets' list, they are not modified." | |
2037 | (interactive "r") | |
2038 | ||
d13c8be6 | 2039 | (let ((bullets (rst-find-pfx-in-region beg end (rst-re 'bul-sta))) |
94e9c286 SM |
2040 | (levtable (make-hash-table :size 4))) |
2041 | ||
2042 | ;; Create a map of levels to list of positions. | |
2043 | (dolist (x bullets) | |
2044 | (let ((key (cdr x))) | |
2045 | (puthash key | |
2046 | (append (gethash key levtable (list)) | |
2047 | (list (car x))) | |
2048 | levtable))) | |
2049 | ||
2050 | ;; Sort this map and create a new map of prefix char and list of positions. | |
b4747519 SM |
2051 | (let ((poslist ())) ; List of (indent . positions). |
2052 | (maphash (lambda (x y) (push (cons x y) poslist)) levtable) | |
2053 | ||
2054 | (let ((bullets rst-preferred-bullets)) | |
2055 | (dolist (x (sort poslist 'car-less-than-car)) | |
2056 | (when bullets | |
2057 | ;; Apply the characters. | |
2058 | (dolist (pos (cdr x)) | |
2059 | (goto-char pos) | |
2060 | (delete-char 1) | |
2061 | (insert (string (car bullets)))) | |
2062 | (setq bullets (cdr bullets)))))))) | |
94e9c286 | 2063 | |
d13c8be6 SM |
2064 | \f |
2065 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2066 | ;; Table of contents | |
2067 | ;; ================= | |
94e9c286 SM |
2068 | |
2069 | (defun rst-get-stripped-line () | |
2070 | "Return the line at cursor, stripped from whitespace." | |
d13c8be6 | 2071 | (re-search-forward (rst-re "\\S .*\\S ") (line-end-position)) |
94e9c286 SM |
2072 | (buffer-substring-no-properties (match-beginning 0) |
2073 | (match-end 0)) ) | |
2074 | ||
d13c8be6 | 2075 | (defun rst-section-tree () |
94e9c286 SM |
2076 | "Get the hierarchical tree of section titles. |
2077 | ||
2078 | Returns a hierarchical tree of the sections titles in the | |
6d3f7c2f SM |
2079 | document. This can be used to generate a table of contents for |
2080 | the document. The top node will always be a nil node, with the | |
d13c8be6 SM |
2081 | top level titles as children (there may potentially be more than |
2082 | one). | |
94e9c286 SM |
2083 | |
2084 | Each section title consists in a cons of the stripped title | |
2085 | string and a marker to the section in the original text document. | |
2086 | ||
2087 | If there are missing section levels, the section titles are | |
2088 | inserted automatically, and the title string is set to nil, and | |
2089 | the marker set to the first non-nil child of itself. | |
6d3f7c2f | 2090 | Conceptually, the nil nodes--i.e.\ those which have no title--are |
94e9c286 SM |
2091 | to be considered as being the same line as their first non-nil |
2092 | child. This has advantages later in processing the graph." | |
2093 | ||
d13c8be6 SM |
2094 | (let ((hier (rst-get-hierarchy)) |
2095 | (levels (make-hash-table :test 'equal :size 10)) | |
2096 | lines) | |
94e9c286 SM |
2097 | |
2098 | (let ((lev 0)) | |
d13c8be6 | 2099 | (dolist (ado hier) |
94e9c286 | 2100 | ;; Compare just the character and indent in the hash table. |
d13c8be6 | 2101 | (puthash (cons (car ado) (cadr ado)) lev levels) |
94e9c286 SM |
2102 | (incf lev))) |
2103 | ||
2104 | ;; Create a list of lines that contains (text, level, marker) for each | |
d13c8be6 | 2105 | ;; adornment. |
94e9c286 SM |
2106 | (save-excursion |
2107 | (setq lines | |
d13c8be6 | 2108 | (mapcar (lambda (ado) |
e6ce8c42 | 2109 | (goto-char (point-min)) |
d13c8be6 SM |
2110 | (forward-line (1- (car ado))) |
2111 | (list (gethash (cons (cadr ado) (caddr ado)) levels) | |
94e9c286 | 2112 | (rst-get-stripped-line) |
8bbb7dd8 | 2113 | (progn |
94e9c286 | 2114 | (beginning-of-line 1) |
8bbb7dd8 | 2115 | (point-marker)))) |
d13c8be6 | 2116 | (rst-find-all-adornments)))) |
94e9c286 SM |
2117 | (let ((lcontnr (cons nil lines))) |
2118 | (rst-section-tree-rec lcontnr -1)))) | |
2119 | ||
2120 | ||
d13c8be6 | 2121 | (defun rst-section-tree-rec (ados lev) |
94e9c286 | 2122 | "Recursive guts of the section tree construction. |
d13c8be6 SM |
2123 | ADOS is a cons cell whose cdr is the remaining list of |
2124 | adornments, and we change it as we consume them. LEV is | |
e6438428 | 2125 | the current level of that node. This function returns a |
d13c8be6 | 2126 | pair of the subtree that was built. This treats the ADOS |
e6438428 | 2127 | list destructively." |
94e9c286 | 2128 | |
d13c8be6 | 2129 | (let ((nado (cadr ados)) |
94e9c286 SM |
2130 | node |
2131 | children) | |
2132 | ||
6d3f7c2f | 2133 | ;; If the next adornment matches our level. |
d13c8be6 | 2134 | (when (and nado (= (car nado) lev)) |
6d3f7c2f | 2135 | ;; Pop the next adornment and create the current node with it. |
d13c8be6 SM |
2136 | (setcdr ados (cddr ados)) |
2137 | (setq node (cdr nado)) ) | |
94e9c286 SM |
2138 | ;; Else we let the node title/marker be unset. |
2139 | ||
6d3f7c2f | 2140 | ;; Build the child nodes. |
d13c8be6 | 2141 | (while (and (cdr ados) (> (caadr ados) lev)) |
94e9c286 | 2142 | (setq children |
d13c8be6 | 2143 | (cons (rst-section-tree-rec ados (1+ lev)) |
94e9c286 SM |
2144 | children))) |
2145 | (setq children (reverse children)) | |
2146 | ||
2147 | ;; If node is still unset, we use the marker of the first child. | |
2148 | (when (eq node nil) | |
2149 | (setq node (cons nil (cdaar children)))) | |
2150 | ||
2151 | ;; Return this node with its children. | |
2152 | (cons node children) | |
2153 | )) | |
2154 | ||
2155 | ||
2156 | (defun rst-section-tree-point (node &optional point) | |
2157 | "Find tree node at point. | |
2158 | Given a computed and valid section tree in NODE and a point | |
2159 | POINT (default being the current point in the current buffer), | |
6d3f7c2f | 2160 | find and return the node within the section tree where the cursor |
94e9c286 SM |
2161 | lives. |
2162 | ||
92439579 JB |
2163 | Return values: a pair of (parent path, container subtree). |
2164 | The parent path is simply a list of the nodes above the | |
2165 | container subtree node that we're returning." | |
94e9c286 SM |
2166 | |
2167 | (let (path outtree) | |
2168 | ||
2169 | (let* ((curpoint (or point (point)))) | |
2170 | ||
2171 | ;; Check if we are before the current node. | |
2172 | (if (and (cadar node) (>= curpoint (cadar node))) | |
2173 | ||
2174 | ;; Iterate all the children, looking for one that might contain the | |
2175 | ;; current section. | |
2176 | (let ((curnode (cdr node)) | |
2177 | last) | |
2178 | ||
2179 | (while (and curnode (>= curpoint (cadaar curnode))) | |
2180 | (setq last curnode | |
2181 | curnode (cdr curnode))) | |
2182 | ||
2183 | (if last | |
2184 | (let ((sub (rst-section-tree-point (car last) curpoint))) | |
2185 | (setq path (car sub) | |
2186 | outtree (cdr sub))) | |
2187 | (setq outtree node)) | |
2188 | ||
2189 | ))) | |
2190 | (cons (cons (car node) path) outtree) | |
2191 | )) | |
2192 | ||
2193 | ||
b4747519 SM |
2194 | (defgroup rst-toc nil |
2195 | "Settings for reStructuredText table of contents." | |
2196 | :group 'rst | |
2197 | :version "21.1") | |
2198 | ||
2199 | (defcustom rst-toc-indent 2 | |
2200 | "Indentation for table-of-contents display. | |
2201 | Also used for formatting insertion, when numbering is disabled." | |
2202 | :group 'rst-toc) | |
2203 | ||
2204 | (defcustom rst-toc-insert-style 'fixed | |
2205 | "Insertion style for table-of-contents. | |
2206 | Set this to one of the following values to determine numbering and | |
2207 | indentation style: | |
2208 | - plain: no numbering (fixed indentation) | |
2209 | - fixed: numbering, but fixed indentation | |
2210 | - aligned: numbering, titles aligned under each other | |
2211 | - listed: numbering, with dashes like list items (EXPERIMENTAL)" | |
2212 | :group 'rst-toc) | |
2213 | ||
2214 | (defcustom rst-toc-insert-number-separator " " | |
2215 | "Separator that goes between the TOC number and the title." | |
2216 | :group 'rst-toc) | |
2217 | ||
2218 | ;; This is used to avoid having to change the user's mode. | |
2219 | (defvar rst-toc-insert-click-keymap | |
2220 | (let ((map (make-sparse-keymap))) | |
2221 | (define-key map [mouse-1] 'rst-toc-mode-mouse-goto) | |
2222 | map) | |
2223 | "(Internal) What happens when you click on propertized text in the TOC.") | |
2224 | ||
2225 | (defcustom rst-toc-insert-max-level nil | |
2226 | "If non-nil, maximum depth of the inserted TOC." | |
2227 | :group 'rst-toc) | |
2228 | ||
2229 | ||
94e9c286 SM |
2230 | (defun rst-toc-insert (&optional pfxarg) |
2231 | "Insert a simple text rendering of the table of contents. | |
2232 | By default the top level is ignored if there is only one, because | |
2233 | we assume that the document will have a single title. | |
2234 | ||
2235 | If a numeric prefix argument PFXARG is given, insert the TOC up | |
2236 | to the specified level. | |
2237 | ||
2238 | The TOC is inserted indented at the current column." | |
94e9c286 | 2239 | (interactive "P") |
d13c8be6 | 2240 | (rst-reset-section-caches) |
6d3f7c2f | 2241 | (let* (;; Check maximum level override. |
94e9c286 SM |
2242 | (rst-toc-insert-max-level |
2243 | (if (and (integerp pfxarg) (> (prefix-numeric-value pfxarg) 0)) | |
2244 | (prefix-numeric-value pfxarg) rst-toc-insert-max-level)) | |
2245 | ||
2246 | ;; Get the section tree for the current cursor point. | |
2247 | (sectree-pair | |
2248 | (rst-section-tree-point | |
d13c8be6 | 2249 | (rst-section-tree))) |
94e9c286 SM |
2250 | |
2251 | ;; Figure out initial indent. | |
2252 | (initial-indent (make-string (current-column) ? )) | |
2253 | (init-point (point))) | |
2254 | ||
2255 | (when (cddr sectree-pair) | |
2256 | (rst-toc-insert-node (cdr sectree-pair) 0 initial-indent "") | |
2257 | ||
2258 | ;; Fixup for the first line. | |
2259 | (delete-region init-point (+ init-point (length initial-indent))) | |
2260 | ||
2261 | ;; Delete the last newline added. | |
d355a0b7 | 2262 | (delete-char -1) |
94e9c286 SM |
2263 | ))) |
2264 | ||
94e9c286 SM |
2265 | (defun rst-toc-insert-node (node level indent pfx) |
2266 | "Insert tree node NODE in table-of-contents. | |
92439579 JB |
2267 | Recursive function that does printing of the inserted toc. |
2268 | LEVEL is the depth level of the sections in the tree. | |
2269 | INDENT is the indentation string. PFX is the prefix numbering, | |
2270 | that includes the alignment necessary for all the children of | |
2271 | level to align." | |
94e9c286 SM |
2272 | |
2273 | ;; Note: we do child numbering from the parent, so we start number the | |
2274 | ;; children one level before we print them. | |
2275 | (let ((do-print (> level 0)) | |
2276 | (count 1)) | |
2277 | (when do-print | |
2278 | (insert indent) | |
2279 | (let ((b (point))) | |
2280 | (unless (equal rst-toc-insert-style 'plain) | |
2281 | (insert pfx rst-toc-insert-number-separator)) | |
2282 | (insert (or (caar node) "[missing node]")) | |
2283 | ;; Add properties to the text, even though in normal text mode it | |
2284 | ;; won't be doing anything for now. Not sure that I want to change | |
2285 | ;; mode stuff. At least the highlighting gives the idea that this | |
2286 | ;; is generated automatically. | |
2287 | (put-text-property b (point) 'mouse-face 'highlight) | |
2288 | (put-text-property b (point) 'rst-toc-target (cadar node)) | |
2289 | (put-text-property b (point) 'keymap rst-toc-insert-click-keymap) | |
2290 | ||
2291 | ) | |
2292 | (insert "\n") | |
2293 | ||
2294 | ;; Prepare indent for children. | |
2295 | (setq indent | |
2296 | (cond | |
2297 | ((eq rst-toc-insert-style 'plain) | |
2298 | (concat indent (make-string rst-toc-indent ? ))) | |
2299 | ||
2300 | ((eq rst-toc-insert-style 'fixed) | |
2301 | (concat indent (make-string rst-toc-indent ? ))) | |
2302 | ||
2303 | ((eq rst-toc-insert-style 'aligned) | |
2304 | (concat indent (make-string (+ (length pfx) 2) ? ))) | |
2305 | ||
2306 | ((eq rst-toc-insert-style 'listed) | |
2307 | (concat (substring indent 0 -3) | |
2308 | (concat (make-string (+ (length pfx) 2) ? ) " - "))) | |
2309 | )) | |
2310 | ) | |
2311 | ||
2312 | (if (or (eq rst-toc-insert-max-level nil) | |
2313 | (< level rst-toc-insert-max-level)) | |
2314 | (let ((do-child-numbering (>= level 0)) | |
2315 | fmt) | |
2316 | (if do-child-numbering | |
2317 | (progn | |
6d3f7c2f | 2318 | ;; Add a separating dot if there is already a prefix. |
d13c8be6 SM |
2319 | (when (> (length pfx) 0) |
2320 | (string-match (rst-re "[ \t\n]*\\'") pfx) | |
2321 | (setq pfx (concat (replace-match "" t t pfx) "."))) | |
94e9c286 SM |
2322 | |
2323 | ;; Calculate the amount of space that the prefix will require | |
2324 | ;; for the numbers. | |
2325 | (if (cdr node) | |
2326 | (setq fmt (format "%%-%dd" | |
2327 | (1+ (floor (log10 (length | |
2328 | (cdr node)))))))) | |
2329 | )) | |
2330 | ||
2331 | (dolist (child (cdr node)) | |
2332 | (rst-toc-insert-node child | |
2333 | (1+ level) | |
2334 | indent | |
2335 | (if do-child-numbering | |
2336 | (concat pfx (format fmt count)) pfx)) | |
2337 | (incf count))) | |
2338 | ||
2339 | ))) | |
2340 | ||
2341 | ||
94e9c286 SM |
2342 | (defun rst-toc-update () |
2343 | "Automatically find the contents section of a document and update. | |
2344 | Updates the inserted TOC if present. You can use this in your | |
2345 | file-write hook to always make it up-to-date automatically." | |
2346 | (interactive) | |
d13c8be6 SM |
2347 | (save-excursion |
2348 | ;; Find and delete an existing comment after the first contents directive. | |
2349 | ;; Delete that region. | |
2350 | (goto-char (point-min)) | |
2351 | ;; We look for the following and the following only (in other words, if your | |
2352 | ;; syntax differs, this won't work.). | |
2353 | ;; | |
2354 | ;; .. contents:: [...anything here...] | |
2355 | ;; [:field: value]... | |
2356 | ;; .. | |
2357 | ;; XXXXXXXX | |
2358 | ;; XXXXXXXX | |
2359 | ;; [more lines] | |
2360 | (let ((beg (re-search-forward | |
2361 | (rst-re "^" 'exm-sta "contents" 'dcl-tag ".*\n" | |
2362 | "\\(?:" 'hws-sta 'fld-tag ".*\n\\)*" 'exm-tag) nil t)) | |
2363 | last-real) | |
2364 | (when beg | |
2365 | ;; Look for the first line that starts at the first column. | |
2366 | (forward-line 1) | |
2367 | (while (and | |
2368 | (< (point) (point-max)) | |
2369 | (or (if (looking-at | |
6d3f7c2f | 2370 | (rst-re 'hws-sta "\\S ")) ; indented content. |
d13c8be6 | 2371 | (setq last-real (point))) |
6d3f7c2f | 2372 | (looking-at (rst-re 'lin-end)))) ; empty line. |
d13c8be6 SM |
2373 | (forward-line 1)) |
2374 | (if last-real | |
2375 | (progn | |
2376 | (goto-char last-real) | |
2377 | (end-of-line) | |
2378 | (delete-region beg (point))) | |
2379 | (goto-char beg)) | |
2380 | (insert "\n ") | |
2381 | (rst-toc-insert)))) | |
94e9c286 | 2382 | ;; Note: always return nil, because this may be used as a hook. |
d13c8be6 | 2383 | nil) |
94e9c286 SM |
2384 | |
2385 | ;; Note: we cannot bind the TOC update on file write because it messes with | |
2386 | ;; undo. If we disable undo, since it adds and removes characters, the | |
2387 | ;; positions in the undo list are not making sense anymore. Dunno what to do | |
2388 | ;; with this, it would be nice to update when saving. | |
2389 | ;; | |
2390 | ;; (add-hook 'write-contents-hooks 'rst-toc-update-fun) | |
2391 | ;; (defun rst-toc-update-fun () | |
2392 | ;; ;; Disable undo for the write file hook. | |
2393 | ;; (let ((buffer-undo-list t)) (rst-toc-update) )) | |
2394 | ||
d13c8be6 | 2395 | (defalias 'rst-toc-insert-update 'rst-toc-update) ; backwards compat. |
94e9c286 SM |
2396 | |
2397 | ;;------------------------------------------------------------------------------ | |
2398 | ||
2399 | (defun rst-toc-node (node level) | |
2400 | "Recursive function that does insert NODE at LEVEL in the table-of-contents." | |
2401 | ||
2402 | (if (> level 0) | |
2403 | (let ((b (point))) | |
2404 | ;; Insert line text. | |
2405 | (insert (make-string (* rst-toc-indent (1- level)) ? )) | |
2406 | (insert (or (caar node) "[missing node]")) | |
2407 | ||
2408 | ;; Highlight lines. | |
2409 | (put-text-property b (point) 'mouse-face 'highlight) | |
2410 | ||
2411 | ;; Add link on lines. | |
2412 | (put-text-property b (point) 'rst-toc-target (cadar node)) | |
2413 | ||
2414 | (insert "\n") | |
2415 | )) | |
2416 | ||
2417 | (dolist (child (cdr node)) | |
2418 | (rst-toc-node child (1+ level)))) | |
2419 | ||
2420 | (defun rst-toc-count-lines (node target-node) | |
2421 | "Count the number of lines from NODE to the TARGET-NODE node. | |
2422 | This recursive function returns a cons of the number of | |
92439579 JB |
2423 | additional lines that have been counted for its node and |
2424 | children, and t if the node has been found." | |
94e9c286 SM |
2425 | |
2426 | (let ((count 1) | |
2427 | found) | |
2428 | (if (eq node target-node) | |
2429 | (setq found t) | |
2430 | (let ((child (cdr node))) | |
2431 | (while (and child (not found)) | |
2432 | (let ((cl (rst-toc-count-lines (car child) target-node))) | |
2433 | (setq count (+ count (car cl)) | |
2434 | found (cdr cl) | |
2435 | child (cdr child)))))) | |
2436 | (cons count found))) | |
2437 | ||
b4747519 SM |
2438 | (defvar rst-toc-buffer-name "*Table of Contents*" |
2439 | "Name of the Table of Contents buffer.") | |
2440 | ||
d13c8be6 SM |
2441 | (defvar rst-toc-return-wincfg nil |
2442 | "Window configuration to which to return when leaving the TOC.") | |
b4747519 | 2443 | |
94e9c286 SM |
2444 | |
2445 | (defun rst-toc () | |
2446 | "Display a table-of-contents. | |
d13c8be6 | 2447 | Finds all the section titles and their adornments in the |
94e9c286 SM |
2448 | file, and displays a hierarchically-organized list of the |
2449 | titles, which is essentially a table-of-contents of the | |
2450 | document. | |
2451 | ||
2452 | The Emacs buffer can be navigated, and selecting a section | |
2453 | brings the cursor in that section." | |
2454 | (interactive) | |
d13c8be6 SM |
2455 | (rst-reset-section-caches) |
2456 | (let* ((curbuf (list (current-window-configuration) (point-marker))) | |
2457 | (sectree (rst-section-tree)) | |
94e9c286 SM |
2458 | |
2459 | (our-node (cdr (rst-section-tree-point sectree))) | |
2460 | line | |
2461 | ||
2462 | ;; Create a temporary buffer. | |
2463 | (buf (get-buffer-create rst-toc-buffer-name)) | |
2464 | ) | |
2465 | ||
2466 | (with-current-buffer buf | |
2467 | (let ((inhibit-read-only t)) | |
2468 | (rst-toc-mode) | |
2469 | (delete-region (point-min) (point-max)) | |
2470 | (insert (format "Table of Contents: %s\n" (or (caar sectree) ""))) | |
2471 | (put-text-property (point-min) (point) | |
2472 | 'face (list '(background-color . "gray"))) | |
2473 | (rst-toc-node sectree 0) | |
2474 | ||
2475 | ;; Count the lines to our found node. | |
2476 | (let ((linefound (rst-toc-count-lines sectree our-node))) | |
2477 | (setq line (if (cdr linefound) (car linefound) 0))) | |
2478 | )) | |
2479 | (display-buffer buf) | |
2480 | (pop-to-buffer buf) | |
2481 | ||
2482 | ;; Save the buffer to return to. | |
d13c8be6 | 2483 | (set (make-local-variable 'rst-toc-return-wincfg) curbuf) |
94e9c286 SM |
2484 | |
2485 | ;; Move the cursor near the right section in the TOC. | |
e6ce8c42 GM |
2486 | (goto-char (point-min)) |
2487 | (forward-line (1- line)) | |
94e9c286 SM |
2488 | )) |
2489 | ||
2490 | ||
2491 | (defun rst-toc-mode-find-section () | |
2492 | "Get the section from text property at point." | |
2493 | (let ((pos (get-text-property (point) 'rst-toc-target))) | |
2494 | (unless pos | |
2495 | (error "No section on this line")) | |
2496 | (unless (buffer-live-p (marker-buffer pos)) | |
2497 | (error "Buffer for this section was killed")) | |
2498 | pos)) | |
2499 | ||
d13c8be6 SM |
2500 | ;; FIXME: Cursor before or behind the list must be handled properly; before the |
2501 | ;; list should jump to the top and behind the list to the last normal | |
6d3f7c2f | 2502 | ;; paragraph. |
94e9c286 | 2503 | (defun rst-goto-section (&optional kill) |
6d3f7c2f SM |
2504 | "Go to the section the current line describes. |
2505 | If KILL a toc buffer is destroyed." | |
94e9c286 SM |
2506 | (interactive) |
2507 | (let ((pos (rst-toc-mode-find-section))) | |
2508 | (when kill | |
6d3f7c2f | 2509 | ;; FIXME: This should rather go to `rst-toc-mode-goto-section'. |
d13c8be6 | 2510 | (set-window-configuration (car rst-toc-return-wincfg)) |
94e9c286 SM |
2511 | (kill-buffer (get-buffer rst-toc-buffer-name))) |
2512 | (pop-to-buffer (marker-buffer pos)) | |
2513 | (goto-char pos) | |
2514 | ;; FIXME: make the recentering conditional on scroll. | |
2515 | (recenter 5))) | |
2516 | ||
2517 | (defun rst-toc-mode-goto-section () | |
92439579 | 2518 | "Go to the section the current line describes and kill the TOC buffer." |
94e9c286 SM |
2519 | (interactive) |
2520 | (rst-goto-section t)) | |
2521 | ||
2522 | (defun rst-toc-mode-mouse-goto (event) | |
2523 | "In `rst-toc' mode, go to the occurrence whose line you click on. | |
2524 | EVENT is the input event." | |
2525 | (interactive "e") | |
8bbb7dd8 | 2526 | (let ((pos |
d13c8be6 SM |
2527 | (with-current-buffer (window-buffer (posn-window (event-end event))) |
2528 | (save-excursion | |
2529 | (goto-char (posn-point (event-end event))) | |
8bbb7dd8 | 2530 | (rst-toc-mode-find-section))))) |
94e9c286 SM |
2531 | (pop-to-buffer (marker-buffer pos)) |
2532 | (goto-char pos) | |
2533 | (recenter 5))) | |
2534 | ||
2535 | (defun rst-toc-mode-mouse-goto-kill (event) | |
6d3f7c2f SM |
2536 | "Same as `rst-toc-mode-mouse-goto', but kill TOC buffer as well. |
2537 | EVENT is the input event." | |
94e9c286 SM |
2538 | (interactive "e") |
2539 | (call-interactively 'rst-toc-mode-mouse-goto event) | |
2540 | (kill-buffer (get-buffer rst-toc-buffer-name))) | |
2541 | ||
94e9c286 | 2542 | (defun rst-toc-quit-window () |
b4747519 | 2543 | "Leave the current TOC buffer." |
94e9c286 | 2544 | (interactive) |
d13c8be6 SM |
2545 | (let ((retbuf rst-toc-return-wincfg)) |
2546 | (set-window-configuration (car retbuf)) | |
2547 | (goto-char (cadr retbuf)))) | |
94e9c286 SM |
2548 | |
2549 | (defvar rst-toc-mode-map | |
2550 | (let ((map (make-sparse-keymap))) | |
2551 | (define-key map [mouse-1] 'rst-toc-mode-mouse-goto-kill) | |
2552 | (define-key map [mouse-2] 'rst-toc-mode-mouse-goto) | |
2553 | (define-key map "\C-m" 'rst-toc-mode-goto-section) | |
2554 | (define-key map "f" 'rst-toc-mode-goto-section) | |
2555 | (define-key map "q" 'rst-toc-quit-window) | |
2556 | (define-key map "z" 'kill-this-buffer) | |
2557 | map) | |
2558 | "Keymap for `rst-toc-mode'.") | |
2559 | ||
2560 | (put 'rst-toc-mode 'mode-class 'special) | |
2561 | ||
b4747519 SM |
2562 | ;; Could inherit from the new `special-mode'. |
2563 | (define-derived-mode rst-toc-mode nil "ReST-TOC" | |
94e9c286 | 2564 | "Major mode for output from \\[rst-toc], the table-of-contents for the document." |
b4747519 | 2565 | (setq buffer-read-only t)) |
94e9c286 SM |
2566 | |
2567 | ;; Note: use occur-mode (replace.el) as a good example to complete missing | |
2568 | ;; features. | |
2569 | ||
94e9c286 | 2570 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
d13c8be6 SM |
2571 | ;; Section movement commands |
2572 | ;; ========================= | |
94e9c286 SM |
2573 | |
2574 | (defun rst-forward-section (&optional offset) | |
d13c8be6 | 2575 | "Skip to the next reStructuredText section title. |
b4747519 SM |
2576 | OFFSET specifies how many titles to skip. Use a negative OFFSET to move |
2577 | backwards in the file (default is to use 1)." | |
94e9c286 | 2578 | (interactive) |
d13c8be6 | 2579 | (rst-reset-section-caches) |
94e9c286 SM |
2580 | (let* (;; Default value for offset. |
2581 | (offset (or offset 1)) | |
2582 | ||
d13c8be6 SM |
2583 | ;; Get all the adornments in the file, with their line numbers. |
2584 | (allados (rst-find-all-adornments)) | |
94e9c286 SM |
2585 | |
2586 | ;; Get the current line. | |
2587 | (curline (line-number-at-pos)) | |
2588 | ||
d13c8be6 | 2589 | (cur allados) |
94e9c286 SM |
2590 | (idx 0) |
2591 | ) | |
2592 | ||
d13c8be6 | 2593 | ;; Find the index of the "next" adornment w.r.t. to the current line. |
94e9c286 SM |
2594 | (while (and cur (< (caar cur) curline)) |
2595 | (setq cur (cdr cur)) | |
2596 | (incf idx)) | |
d13c8be6 | 2597 | ;; 'cur' is the adornment on or following the current line. |
94e9c286 SM |
2598 | |
2599 | (if (and (> offset 0) cur (= (caar cur) curline)) | |
2600 | (incf idx)) | |
2601 | ||
2602 | ;; Find the final index. | |
2603 | (setq idx (+ idx (if (> offset 0) (- offset 1) offset))) | |
d13c8be6 | 2604 | (setq cur (nth idx allados)) |
94e9c286 SM |
2605 | |
2606 | ;; If the index is positive, goto the line, otherwise go to the buffer | |
2607 | ;; boundaries. | |
2608 | (if (and cur (>= idx 0)) | |
e6ce8c42 GM |
2609 | (progn |
2610 | (goto-char (point-min)) | |
2611 | (forward-line (1- (car cur)))) | |
94e9c286 SM |
2612 | (if (> offset 0) (goto-char (point-max)) (goto-char (point-min)))) |
2613 | )) | |
2614 | ||
2615 | (defun rst-backward-section () | |
e6438428 | 2616 | "Like `rst-forward-section', except move back one title." |
94e9c286 SM |
2617 | (interactive) |
2618 | (rst-forward-section -1)) | |
2619 | ||
6d3f7c2f SM |
2620 | ;; FIXME: What is `allow-extend' for? |
2621 | (defun rst-mark-section (&optional count allow-extend) | |
2622 | "Select COUNT sections around point. | |
2623 | Mark following sections for positive COUNT or preceding sections | |
2624 | for negative COUNT." | |
94e9c286 SM |
2625 | ;; Cloned from mark-paragraph. |
2626 | (interactive "p\np") | |
6d3f7c2f SM |
2627 | (unless count (setq count 1)) |
2628 | (when (zerop count) | |
94e9c286 SM |
2629 | (error "Cannot mark zero sections")) |
2630 | (cond ((and allow-extend | |
2631 | (or (and (eq last-command this-command) (mark t)) | |
2632 | (rst-portable-mark-active-p))) | |
2633 | (set-mark | |
2634 | (save-excursion | |
2635 | (goto-char (mark)) | |
6d3f7c2f | 2636 | (rst-forward-section count) |
94e9c286 SM |
2637 | (point)))) |
2638 | (t | |
6d3f7c2f | 2639 | (rst-forward-section count) |
94e9c286 | 2640 | (push-mark nil t t) |
6d3f7c2f | 2641 | (rst-forward-section (- count))))) |
94e9c286 | 2642 | |
94e9c286 SM |
2643 | \f |
2644 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2645 | ;; Functions to work on item lists (e.g. indent/dedent, enumerate), which are | |
2646 | ;; always 2 or 3 characters apart horizontally with rest. | |
2647 | ||
94e9c286 | 2648 | (defun rst-find-leftmost-column (beg end) |
d13c8be6 SM |
2649 | "Return the leftmost column in region BEG to END." |
2650 | (let (mincol) | |
94e9c286 SM |
2651 | (save-excursion |
2652 | (goto-char beg) | |
2653 | (while (< (point) end) | |
2654 | (back-to-indentation) | |
d13c8be6 SM |
2655 | (unless (looking-at (rst-re 'lin-end)) |
2656 | (setq mincol (if mincol | |
2657 | (min mincol (current-column)) | |
2658 | (current-column)))) | |
2659 | (forward-line 1))) | |
94e9c286 SM |
2660 | mincol)) |
2661 | ||
6d3f7c2f SM |
2662 | ;; FIXME: This definition is old and deprecated. We need to move to the newer |
2663 | ;; version below. | |
94e9c286 SM |
2664 | (defmacro rst-iterate-leftmost-paragraphs |
2665 | (beg end first-only body-consequent body-alternative) | |
6d3f7c2f SM |
2666 | ;; FIXME: The following comment is pretty useless. |
2667 | "Call FUN at the beginning of each line, with an argument that | |
94e9c286 SM |
2668 | specifies whether we are at the first line of a paragraph that |
2669 | starts at the leftmost column of the given region BEG and END. | |
2670 | Set FIRST-ONLY to true if you want to callback on the first line | |
2671 | of each paragraph only." | |
2672 | `(save-excursion | |
2673 | (let ((leftcol (rst-find-leftmost-column ,beg ,end)) | |
8bbb7dd8 | 2674 | (endm (copy-marker ,end))) |
94e9c286 | 2675 | |
6d3f7c2f | 2676 | (do* (;; Iterate lines. |
94e9c286 SM |
2677 | (l (progn (goto-char ,beg) (back-to-indentation)) |
2678 | (progn (forward-line 1) (back-to-indentation))) | |
2679 | ||
2680 | (previous nil valid) | |
2681 | ||
2682 | (curcol (current-column) | |
2683 | (current-column)) | |
2684 | ||
2685 | (valid (and (= curcol leftcol) | |
d13c8be6 | 2686 | (not (looking-at (rst-re 'lin-end)))) |
94e9c286 | 2687 | (and (= curcol leftcol) |
d13c8be6 | 2688 | (not (looking-at (rst-re 'lin-end))))) |
94e9c286 | 2689 | ) |
b4747519 | 2690 | ((>= (point) endm)) |
94e9c286 SM |
2691 | |
2692 | (if (if ,first-only | |
2693 | (and valid (not previous)) | |
2694 | valid) | |
2695 | ,body-consequent | |
2696 | ,body-alternative) | |
2697 | ||
2698 | )))) | |
2699 | ||
6d3f7c2f SM |
2700 | ;; FIXME: This needs to be refactored. Probably this is simply a function |
2701 | ;; applying BODY rather than a macro. | |
94e9c286 SM |
2702 | (defmacro rst-iterate-leftmost-paragraphs-2 (spec &rest body) |
2703 | "Evaluate BODY for each line in region defined by BEG END. | |
2704 | LEFTMOST is set to true if the line is one of the leftmost of the | |
b4747519 | 2705 | entire paragraph. PARABEGIN is set to true if the line is the |
94e9c286 | 2706 | first of a paragraph." |
b4747519 | 2707 | (declare (indent 1) (debug (sexp body))) |
94e9c286 SM |
2708 | (destructuring-bind |
2709 | (beg end parabegin leftmost isleftmost isempty) spec | |
2710 | ||
2711 | `(save-excursion | |
2712 | (let ((,leftmost (rst-find-leftmost-column ,beg ,end)) | |
8bbb7dd8 | 2713 | (endm (copy-marker ,end))) |
94e9c286 | 2714 | |
6d3f7c2f | 2715 | (do* (;; Iterate lines. |
94e9c286 SM |
2716 | (l (progn (goto-char ,beg) (back-to-indentation)) |
2717 | (progn (forward-line 1) (back-to-indentation))) | |
2718 | ||
2719 | (empty-line-previous nil ,isempty) | |
2720 | ||
d13c8be6 SM |
2721 | (,isempty (looking-at (rst-re 'lin-end)) |
2722 | (looking-at (rst-re 'lin-end))) | |
94e9c286 SM |
2723 | |
2724 | (,parabegin (not ,isempty) | |
2725 | (and empty-line-previous | |
2726 | (not ,isempty))) | |
2727 | ||
2728 | (,isleftmost (and (not ,isempty) | |
2729 | (= (current-column) ,leftmost)) | |
2730 | (and (not ,isempty) | |
2731 | (= (current-column) ,leftmost))) | |
2732 | ) | |
b4747519 | 2733 | ((>= (point) endm)) |
94e9c286 SM |
2734 | |
2735 | (progn ,@body) | |
2736 | ||
2737 | ))))) | |
2738 | ||
d13c8be6 SM |
2739 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2740 | ;; Indentation | |
2741 | ||
2742 | ;; FIXME: At the moment only block comments with leading empty comment line are | |
6d3f7c2f SM |
2743 | ;; supported. Comment lines with leading comment markup should be also |
2744 | ;; supported. May be a customizable option could control which style to | |
2745 | ;; prefer. | |
d13c8be6 | 2746 | |
c846da43 | 2747 | (defgroup rst-indent nil "Settings for indentation in reStructuredText. |
d13c8be6 | 2748 | |
c846da43 | 2749 | In reStructuredText indentation points are usually determined by |
d13c8be6 | 2750 | preceding lines. Sometimes the syntax allows arbitrary |
c846da43 | 2751 | indentation points such as where to start the first line |
d13c8be6 SM |
2752 | following a directive. These indentation widths can be customized |
2753 | here." | |
2754 | :group 'rst | |
2755 | :package-version '(rst . "1.1.0")) | |
2756 | ||
2757 | (define-obsolete-variable-alias | |
2758 | 'rst-shift-basic-offset 'rst-indent-width "r6713") | |
2759 | (defcustom rst-indent-width 2 | |
2760 | "Indentation when there is no more indentation point given." | |
2761 | :group 'rst-indent | |
2762 | :type '(integer)) | |
2763 | ||
2764 | (defcustom rst-indent-field 3 | |
6d3f7c2f | 2765 | "Indentation for first line after a field or 0 to always indent for content." |
d13c8be6 SM |
2766 | :group 'rst-indent |
2767 | :type '(integer)) | |
2768 | ||
2769 | (defcustom rst-indent-literal-normal 3 | |
6d3f7c2f | 2770 | "Default indentation for literal block after a markup on an own line." |
d13c8be6 SM |
2771 | :group 'rst-indent |
2772 | :type '(integer)) | |
2773 | ||
2774 | (defcustom rst-indent-literal-minimized 2 | |
6d3f7c2f | 2775 | "Default indentation for literal block after a minimized markup." |
d13c8be6 SM |
2776 | :group 'rst-indent |
2777 | :type '(integer)) | |
2778 | ||
2779 | (defcustom rst-indent-comment 3 | |
c846da43 | 2780 | "Default indentation for first line of a comment." |
d13c8be6 SM |
2781 | :group 'rst-indent |
2782 | :type '(integer)) | |
2783 | ||
2784 | ;; FIXME: Must consider other tabs: | |
6d3f7c2f SM |
2785 | ;; * Line blocks |
2786 | ;; * Definition lists | |
2787 | ;; * Option lists | |
d13c8be6 SM |
2788 | (defun rst-line-tabs () |
2789 | "Return tabs of the current line or nil for no tab. | |
2790 | The list is sorted so the tab where writing continues most likely | |
6d3f7c2f SM |
2791 | is the first one. Each tab is of the form (COLUMN . INNER). |
2792 | COLUMN is the column of the tab. INNER is non-nil if this is an | |
2793 | inner tab. I.e. a tab which does come from the basic indentation | |
d13c8be6 SM |
2794 | and not from inner alignment points." |
2795 | (save-excursion | |
2796 | (forward-line 0) | |
2797 | (save-match-data | |
2798 | (unless (looking-at (rst-re 'lin-end)) | |
2799 | (back-to-indentation) | |
6d3f7c2f | 2800 | ;; Current indendation is always the least likely tab. |
d13c8be6 | 2801 | (let ((tabs (list (list (point) 0 nil)))) ; (POINT OFFSET INNER) |
6d3f7c2f | 2802 | ;; Push inner tabs more likely to continue writing. |
d13c8be6 | 2803 | (cond |
6d3f7c2f | 2804 | ;; Item. |
d13c8be6 SM |
2805 | ((looking-at (rst-re '(:grp itmany-tag hws-sta) '(:grp "\\S ") "?")) |
2806 | (when (match-string 2) | |
2807 | (push (list (match-beginning 2) 0 t) tabs))) | |
6d3f7c2f | 2808 | ;; Field. |
d13c8be6 SM |
2809 | ((looking-at (rst-re '(:grp fld-tag) '(:grp hws-tag) |
2810 | '(:grp "\\S ") "?")) | |
2811 | (unless (zerop rst-indent-field) | |
2812 | (push (list (match-beginning 1) rst-indent-field t) tabs)) | |
2813 | (if (match-string 3) | |
2814 | (push (list (match-beginning 3) 0 t) tabs) | |
2815 | (if (zerop rst-indent-field) | |
2816 | (push (list (match-end 2) | |
2817 | (if (string= (match-string 2) "") 1 0) | |
2818 | t) tabs)))) | |
6d3f7c2f | 2819 | ;; Directive. |
d13c8be6 SM |
2820 | ((looking-at (rst-re 'dir-sta-3 '(:grp "\\S ") "?")) |
2821 | (push (list (match-end 1) 0 t) tabs) | |
2822 | (unless (string= (match-string 2) "") | |
2823 | (push (list (match-end 2) 0 t) tabs)) | |
2824 | (when (match-string 4) | |
2825 | (push (list (match-beginning 4) 0 t) tabs))) | |
6d3f7c2f | 2826 | ;; Footnote or citation definition. |
d13c8be6 SM |
2827 | ((looking-at (rst-re 'fnc-sta-2 '(:grp "\\S ") "?")) |
2828 | (push (list (match-end 1) 0 t) tabs) | |
2829 | (when (match-string 3) | |
2830 | (push (list (match-beginning 3) 0 t) tabs))) | |
6d3f7c2f | 2831 | ;; Comment. |
d13c8be6 SM |
2832 | ((looking-at (rst-re 'cmt-sta-1)) |
2833 | (push (list (point) rst-indent-comment t) tabs))) | |
6d3f7c2f | 2834 | ;; Start of literal block. |
d13c8be6 SM |
2835 | (when (looking-at (rst-re 'lit-sta-2)) |
2836 | (let ((tab0 (first tabs))) | |
2837 | (push (list (first tab0) | |
2838 | (+ (second tab0) | |
2839 | (if (match-string 1) | |
2840 | rst-indent-literal-minimized | |
2841 | rst-indent-literal-normal)) | |
2842 | t) tabs))) | |
2843 | (mapcar (lambda (tab) | |
2844 | (goto-char (first tab)) | |
2845 | (cons (+ (current-column) (second tab)) (third tab))) | |
2846 | tabs)))))) | |
2847 | ||
2848 | (defun rst-compute-tabs (pt) | |
2849 | "Build the list of possible tabs for all lines above. | |
2850 | Search backwards from point PT to build the list of possible | |
6d3f7c2f SM |
2851 | tabs. Return a list of tabs sorted by likeliness to continue |
2852 | writing like `rst-line-tabs'. Nearer lines have generally a | |
2853 | higher likeliness than farther lines. Return nil if no tab is found | |
d13c8be6 SM |
2854 | in the text above." |
2855 | (save-excursion | |
2856 | (goto-char pt) | |
6d3f7c2f SM |
2857 | (let (leftmost ; Leftmost column found so far. |
2858 | innermost ; Leftmost column for inner tab. | |
d13c8be6 SM |
2859 | tablist) |
2860 | (while (and (zerop (forward-line -1)) | |
2861 | (or (not leftmost) | |
2862 | (> leftmost 0))) | |
2863 | (let* ((tabs (rst-line-tabs)) | |
2864 | (leftcol (if tabs (apply 'min (mapcar 'car tabs))))) | |
2865 | (when tabs | |
6d3f7c2f | 2866 | ;; Consider only lines indented less or same if not INNERMOST. |
d13c8be6 SM |
2867 | (when (or (not leftmost) |
2868 | (< leftcol leftmost) | |
2869 | (and (not innermost) (= leftcol leftmost))) | |
2870 | (dolist (tab tabs) | |
2871 | (let ((inner (cdr tab)) | |
2872 | (newcol (car tab))) | |
2873 | (when (and | |
2874 | (or | |
2875 | (and (not inner) | |
2876 | (or (not leftmost) | |
2877 | (< newcol leftmost))) | |
2878 | (and inner | |
2879 | (or (not innermost) | |
2880 | (< newcol innermost)))) | |
2881 | (not (memq newcol tablist))) | |
2882 | (push newcol tablist)))) | |
2883 | (setq innermost (if (some 'identity | |
6d3f7c2f | 2884 | (mapcar 'cdr tabs)) ; Has inner. |
d13c8be6 SM |
2885 | leftcol |
2886 | innermost)) | |
2887 | (setq leftmost leftcol))))) | |
2888 | (nreverse tablist)))) | |
2889 | ||
2890 | (defun rst-indent-line (&optional dflt) | |
2891 | "Indent current line to next best reStructuredText tab. | |
2892 | The next best tab is taken from the tab list returned by | |
6d3f7c2f SM |
2893 | `rst-compute-tabs' which is used in a cyclic manner. If the |
2894 | current indentation does not end on a tab use the first one. If | |
2895 | the current indentation is on a tab use the next tab. This allows | |
d13c8be6 | 2896 | a repeated use of \\[indent-for-tab-command] to cycle through all |
6d3f7c2f SM |
2897 | possible tabs. If no indentation is possible return `noindent' or |
2898 | use DFLT. Return the indentation indented to. When point is in | |
2899 | indentation it ends up at its end. Otherwise the point is kept | |
d13c8be6 SM |
2900 | relative to the content." |
2901 | (let* ((pt (point-marker)) | |
2902 | (cur (current-indentation)) | |
2903 | (clm (current-column)) | |
2904 | (tabs (rst-compute-tabs (point))) | |
2905 | (fnd (position cur tabs)) | |
2906 | ind) | |
2907 | (if (and (not tabs) (not dflt)) | |
2908 | 'noindent | |
2909 | (if (not tabs) | |
2910 | (setq ind dflt) | |
2911 | (if (not fnd) | |
2912 | (setq fnd 0) | |
2913 | (setq fnd (1+ fnd)) | |
2914 | (if (>= fnd (length tabs)) | |
2915 | (setq fnd 0))) | |
2916 | (setq ind (nth fnd tabs))) | |
2917 | (indent-line-to ind) | |
2918 | (if (> clm cur) | |
2919 | (goto-char pt)) | |
2920 | (set-marker pt nil) | |
2921 | ind))) | |
2922 | ||
2923 | (defun rst-shift-region (beg end cnt) | |
2924 | "Shift region BEG to END by CNT tabs. | |
2925 | Shift by one tab to the right (CNT > 0) or left (CNT < 0) or | |
6d3f7c2f SM |
2926 | remove all indentation (CNT = 0). A tab is taken from the text |
2927 | above. If no suitable tab is found `rst-indent-width' is used." | |
d13c8be6 SM |
2928 | (interactive "r\np") |
2929 | (let ((tabs (sort (rst-compute-tabs beg) (lambda (x y) (<= x y)))) | |
2930 | (leftmostcol (rst-find-leftmost-column beg end))) | |
2931 | (when (or (> leftmostcol 0) (> cnt 0)) | |
6d3f7c2f | 2932 | ;; Apply the indent. |
d13c8be6 SM |
2933 | (indent-rigidly |
2934 | beg end | |
2935 | (if (zerop cnt) | |
2936 | (- leftmostcol) | |
6d3f7c2f | 2937 | ;; Find the next tab after the leftmost column. |
d13c8be6 SM |
2938 | (let* ((cmp (if (> cnt 0) '> '<)) |
2939 | (tabs (if (> cnt 0) tabs (reverse tabs))) | |
2940 | (len (length tabs)) | |
6d3f7c2f SM |
2941 | (dir (signum cnt)) ; Direction to take. |
2942 | (abs (abs cnt)) ; Absolute number of steps to take. | |
2943 | ;; Get the position of the first tab beyond leftmostcol. | |
d13c8be6 SM |
2944 | (fnd (position-if (lambda (elt) |
2945 | (funcall cmp elt leftmostcol)) | |
2946 | tabs)) | |
6d3f7c2f | 2947 | ;; Virtual position of tab. |
d13c8be6 SM |
2948 | (pos (+ (or fnd len) (1- abs))) |
2949 | (tab (if (< pos len) | |
6d3f7c2f | 2950 | ;; Tab exists - use it. |
d13c8be6 | 2951 | (nth pos tabs) |
6d3f7c2f | 2952 | ;; Column needs to be computed. |
d13c8be6 | 2953 | (let ((col (+ (or (car (last tabs)) leftmostcol) |
6d3f7c2f SM |
2954 | ;; Base on last known column. |
2955 | (* (- pos (1- len)) ; Distance left. | |
2956 | dir ; Direction to take. | |
d13c8be6 SM |
2957 | rst-indent-width)))) |
2958 | (if (< col 0) 0 col))))) | |
2959 | (- tab leftmostcol))))))) | |
2960 | ||
2961 | ;; FIXME: A paragraph with an (incorrectly) indented second line is not filled | |
6d3f7c2f | 2962 | ;; correctly:: |
d13c8be6 | 2963 | ;; |
6d3f7c2f SM |
2964 | ;; Some start |
2965 | ;; continued wrong | |
d13c8be6 SM |
2966 | (defun rst-adaptive-fill () |
2967 | "Return fill prefix found at point. | |
2968 | Value for `adaptive-fill-function'." | |
2969 | (let ((fnd (if (looking-at adaptive-fill-regexp) | |
2970 | (match-string-no-properties 0)))) | |
2971 | (if (save-match-data | |
2972 | (not (string-match comment-start-skip fnd))) | |
6d3f7c2f | 2973 | ;; An non-comment prefix is fine. |
d13c8be6 | 2974 | fnd |
6d3f7c2f | 2975 | ;; Matches a comment - return whitespace instead. |
d13c8be6 SM |
2976 | (make-string (- |
2977 | (save-excursion | |
2978 | (goto-char (match-end 0)) | |
2979 | (current-column)) | |
2980 | (save-excursion | |
2981 | (goto-char (match-beginning 0)) | |
2982 | (current-column))) ? )))) | |
2983 | ||
2984 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2985 | ;; Comments | |
2986 | ||
2987 | (defun rst-comment-line-break (&optional soft) | |
2988 | "Break line and indent, continuing reStructuredText comment if within one. | |
6d3f7c2f SM |
2989 | Value for `comment-line-break-function'. If SOFT use soft |
2990 | newlines as mandated by `comment-line-break-function'." | |
d13c8be6 SM |
2991 | (if soft |
2992 | (insert-and-inherit ?\n) | |
2993 | (newline 1)) | |
2994 | (save-excursion | |
2995 | (forward-char -1) | |
2996 | (delete-horizontal-space)) | |
2997 | (delete-horizontal-space) | |
2998 | (let ((tabs (rst-compute-tabs (point)))) | |
2999 | (when tabs | |
3000 | (indent-line-to (car tabs))))) | |
3001 | ||
3002 | (defun rst-comment-indent () | |
3003 | "Return indentation for current comment line." | |
3004 | (car (rst-compute-tabs (point)))) | |
3005 | ||
3006 | (defun rst-comment-insert-comment () | |
3007 | "Insert a comment in the current line." | |
3008 | (rst-indent-line 0) | |
3009 | (insert comment-start)) | |
3010 | ||
3011 | (defun rst-comment-region (beg end &optional arg) | |
6d3f7c2f SM |
3012 | "Comment or uncomment the current region. |
3013 | Region is from from BEG to END. Uncomment if ARG." | |
d13c8be6 SM |
3014 | (save-excursion |
3015 | (if (consp arg) | |
3016 | (rst-uncomment-region beg end arg) | |
3017 | (goto-char beg) | |
3018 | (let ((ind (current-indentation)) | |
3019 | bol) | |
3020 | (forward-line 0) | |
3021 | (setq bol (point)) | |
3022 | (indent-rigidly bol end rst-indent-comment) | |
3023 | (goto-char bol) | |
3024 | (open-line 1) | |
3025 | (indent-line-to ind) | |
3026 | (insert (comment-string-strip comment-start t t)))))) | |
3027 | ||
3028 | (defun rst-uncomment-region (beg end &optional arg) | |
3029 | "Uncomment the current region. | |
6d3f7c2f | 3030 | Region is from BEG to END. ARG is ignored" |
d13c8be6 SM |
3031 | (save-excursion |
3032 | (let (bol eol) | |
3033 | (goto-char beg) | |
3034 | (forward-line 0) | |
3035 | (setq bol (point)) | |
3036 | (forward-line 1) | |
3037 | (setq eol (point)) | |
3038 | (indent-rigidly eol end (- rst-indent-comment)) | |
3039 | (delete-region bol eol)))) | |
94e9c286 | 3040 | |
b4747519 SM |
3041 | ;;------------------------------------------------------------------------------ |
3042 | ||
6d3f7c2f SM |
3043 | ;; FIXME: These next functions should become part of a larger effort to redo |
3044 | ;; the bullets in bulleted lists. The enumerate would just be one of | |
3045 | ;; the possible outputs. | |
b4747519 | 3046 | ;; |
d13c8be6 | 3047 | ;; FIXME: We need to do the enumeration removal as well. |
b4747519 | 3048 | |
d13c8be6 | 3049 | (defun rst-enumerate-region (beg end all) |
b4747519 | 3050 | "Add enumeration to all the leftmost paragraphs in the given region. |
d13c8be6 | 3051 | The region is specified between BEG and END. With ALL, |
b4747519 | 3052 | do all lines instead of just paragraphs." |
d13c8be6 | 3053 | (interactive "r\nP") |
b4747519 SM |
3054 | (let ((count 0) |
3055 | (last-insert-len nil)) | |
3056 | (rst-iterate-leftmost-paragraphs | |
d13c8be6 | 3057 | beg end (not all) |
b4747519 SM |
3058 | (let ((ins-string (format "%d. " (incf count)))) |
3059 | (setq last-insert-len (length ins-string)) | |
3060 | (insert ins-string)) | |
3061 | (insert (make-string last-insert-len ?\ )) | |
3062 | ))) | |
3063 | ||
d13c8be6 | 3064 | (defun rst-bullet-list-region (beg end all) |
b4747519 | 3065 | "Add bullets to all the leftmost paragraphs in the given region. |
d13c8be6 | 3066 | The region is specified between BEG and END. With ALL, |
b4747519 | 3067 | do all lines instead of just paragraphs." |
d13c8be6 | 3068 | (interactive "r\nP") |
b4747519 | 3069 | (rst-iterate-leftmost-paragraphs |
d13c8be6 SM |
3070 | beg end (not all) |
3071 | (insert (car rst-preferred-bullets) " ") | |
b4747519 SM |
3072 | (insert " ") |
3073 | )) | |
3074 | ||
6d3f7c2f SM |
3075 | ;; FIXME: Does not deal with a varying number of digits appropriately. |
3076 | ;; FIXME: Does not deal with multiple levels independently. | |
3077 | ;; FIXME: Does not indent a multiline item correctly. | |
94e9c286 | 3078 | (defun rst-convert-bullets-to-enumeration (beg end) |
d13c8be6 | 3079 | "Convert the bulleted and enumerated items in the region to enumerated lists. |
6d3f7c2f | 3080 | Renumber as necessary. Region is from BEG to END." |
94e9c286 SM |
3081 | (interactive "r") |
3082 | (let* (;; Find items and convert the positions to markers. | |
3083 | (items (mapcar | |
3084 | (lambda (x) | |
8bbb7dd8 | 3085 | (cons (copy-marker (car x)) |
94e9c286 | 3086 | (cdr x))) |
d13c8be6 | 3087 | (rst-find-pfx-in-region beg end (rst-re 'itmany-sta-1)))) |
94e9c286 SM |
3088 | (count 1) |
3089 | ) | |
3090 | (save-excursion | |
3091 | (dolist (x items) | |
3092 | (goto-char (car x)) | |
d13c8be6 SM |
3093 | (looking-at (rst-re 'itmany-beg-1)) |
3094 | (replace-match (format "%d." count) nil nil nil 1) | |
94e9c286 SM |
3095 | (incf count) |
3096 | )) | |
3097 | )) | |
3098 | ||
3099 | ||
3100 | ||
3101 | ;;------------------------------------------------------------------------------ | |
3102 | ||
3103 | (defun rst-line-block-region (rbeg rend &optional pfxarg) | |
b4747519 | 3104 | "Toggle line block prefixes for a region. |
6d3f7c2f | 3105 | Region is from RBEG to REND. With PFXARG set the empty lines too." |
94e9c286 SM |
3106 | (interactive "r\nP") |
3107 | (let ((comment-start "| ") | |
3108 | (comment-end "") | |
3109 | (comment-start-skip "| ") | |
3110 | (comment-style 'indent) | |
b4747519 | 3111 | (force (not (not pfxarg)))) |
94e9c286 | 3112 | (rst-iterate-leftmost-paragraphs-2 |
b4747519 SM |
3113 | (rbeg rend parbegin leftmost isleft isempty) |
3114 | (when (or force (not isempty)) | |
3115 | (move-to-column leftmost force) | |
3116 | (delete-region (point) (+ (point) (- (current-indentation) leftmost))) | |
3117 | (insert "| "))))) | |
94e9c286 SM |
3118 | |
3119 | ||
3120 | \f | |
3121 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
d13c8be6 SM |
3122 | ;; Font lock |
3123 | ;; ========= | |
94e9c286 SM |
3124 | |
3125 | (require 'font-lock) | |
3126 | ||
6d3f7c2f | 3127 | ;; FIXME: The obsolete variables need to disappear. |
d13c8be6 | 3128 | |
92439579 | 3129 | (defgroup rst-faces nil "Faces used in Rst Mode." |
94e9c286 SM |
3130 | :group 'rst |
3131 | :group 'faces | |
3132 | :version "21.1") | |
3133 | ||
2b1400b9 GM |
3134 | (defface rst-block '((t :inherit font-lock-keyword-face)) |
3135 | "Face used for all syntax marking up a special block." | |
3136 | :version "24.1" | |
3137 | :group 'rst-faces) | |
3138 | ||
3139 | (defcustom rst-block-face 'rst-block | |
b4747519 | 3140 | "All syntax marking up a special block." |
2b1400b9 | 3141 | :version "24.1" |
94e9c286 SM |
3142 | :group 'rst-faces |
3143 | :type '(face)) | |
2b1400b9 GM |
3144 | (make-obsolete-variable 'rst-block-face |
3145 | "customize the face `rst-block' instead." | |
3146 | "24.1") | |
3147 | ||
3148 | (defface rst-external '((t :inherit font-lock-type-face)) | |
3149 | "Face used for field names and interpreted text." | |
3150 | :version "24.1" | |
3151 | :group 'rst-faces) | |
94e9c286 | 3152 | |
2b1400b9 | 3153 | (defcustom rst-external-face 'rst-external |
b4747519 | 3154 | "Field names and interpreted text." |
2b1400b9 | 3155 | :version "24.1" |
94e9c286 SM |
3156 | :group 'rst-faces |
3157 | :type '(face)) | |
2b1400b9 GM |
3158 | (make-obsolete-variable 'rst-external-face |
3159 | "customize the face `rst-external' instead." | |
3160 | "24.1") | |
94e9c286 | 3161 | |
2b1400b9 GM |
3162 | (defface rst-definition '((t :inherit font-lock-function-name-face)) |
3163 | "Face used for all other defining constructs." | |
3164 | :version "24.1" | |
3165 | :group 'rst-faces) | |
3166 | ||
3167 | (defcustom rst-definition-face 'rst-definition | |
b4747519 | 3168 | "All other defining constructs." |
2b1400b9 | 3169 | :version "24.1" |
94e9c286 SM |
3170 | :group 'rst-faces |
3171 | :type '(face)) | |
2b1400b9 GM |
3172 | (make-obsolete-variable 'rst-definition-face |
3173 | "customize the face `rst-definition' instead." | |
3174 | "24.1") | |
3175 | ||
3176 | ;; XEmacs compatibility (?). | |
3177 | (defface rst-directive (if (boundp 'font-lock-builtin-face) | |
3178 | '((t :inherit font-lock-builtin-face)) | |
3179 | '((t :inherit font-lock-preprocessor-face))) | |
3180 | "Face used for directives and roles." | |
3181 | :version "24.1" | |
3182 | :group 'rst-faces) | |
3183 | ||
3184 | (defcustom rst-directive-face 'rst-directive | |
b4747519 | 3185 | "Directives and roles." |
94e9c286 SM |
3186 | :group 'rst-faces |
3187 | :type '(face)) | |
2b1400b9 GM |
3188 | (make-obsolete-variable 'rst-directive-face |
3189 | "customize the face `rst-directive' instead." | |
3190 | "24.1") | |
94e9c286 | 3191 | |
2b1400b9 GM |
3192 | (defface rst-comment '((t :inherit font-lock-comment-face)) |
3193 | "Face used for comments." | |
3194 | :version "24.1" | |
3195 | :group 'rst-faces) | |
3196 | ||
3197 | (defcustom rst-comment-face 'rst-comment | |
b4747519 | 3198 | "Comments." |
2b1400b9 | 3199 | :version "24.1" |
94e9c286 SM |
3200 | :group 'rst-faces |
3201 | :type '(face)) | |
2b1400b9 GM |
3202 | (make-obsolete-variable 'rst-comment-face |
3203 | "customize the face `rst-comment' instead." | |
3204 | "24.1") | |
3205 | ||
3206 | (defface rst-emphasis1 '((t :inherit italic)) | |
3207 | "Face used for simple emphasis." | |
3208 | :version "24.1" | |
3209 | :group 'rst-faces) | |
94e9c286 | 3210 | |
2b1400b9 | 3211 | (defcustom rst-emphasis1-face 'rst-emphasis1 |
b4747519 | 3212 | "Simple emphasis." |
2b1400b9 | 3213 | :version "24.1" |
94e9c286 SM |
3214 | :group 'rst-faces |
3215 | :type '(face)) | |
2b1400b9 GM |
3216 | (make-obsolete-variable 'rst-emphasis1-face |
3217 | "customize the face `rst-emphasis1' instead." | |
3218 | "24.1") | |
94e9c286 | 3219 | |
2b1400b9 GM |
3220 | (defface rst-emphasis2 '((t :inherit bold)) |
3221 | "Face used for double emphasis." | |
3222 | :version "24.1" | |
3223 | :group 'rst-faces) | |
3224 | ||
3225 | (defcustom rst-emphasis2-face 'rst-emphasis2 | |
b4747519 | 3226 | "Double emphasis." |
94e9c286 SM |
3227 | :group 'rst-faces |
3228 | :type '(face)) | |
2b1400b9 GM |
3229 | (make-obsolete-variable 'rst-emphasis2-face |
3230 | "customize the face `rst-emphasis2' instead." | |
3231 | "24.1") | |
3232 | ||
3233 | (defface rst-literal '((t :inherit font-lock-string-face)) | |
3234 | "Face used for literal text." | |
3235 | :version "24.1" | |
3236 | :group 'rst-faces) | |
94e9c286 | 3237 | |
2b1400b9 | 3238 | (defcustom rst-literal-face 'rst-literal |
b4747519 | 3239 | "Literal text." |
2b1400b9 | 3240 | :version "24.1" |
94e9c286 SM |
3241 | :group 'rst-faces |
3242 | :type '(face)) | |
2b1400b9 GM |
3243 | (make-obsolete-variable 'rst-literal-face |
3244 | "customize the face `rst-literal' instead." | |
3245 | "24.1") | |
94e9c286 | 3246 | |
2b1400b9 GM |
3247 | (defface rst-reference '((t :inherit font-lock-variable-name-face)) |
3248 | "Face used for references to a definition." | |
3249 | :version "24.1" | |
3250 | :group 'rst-faces) | |
3251 | ||
3252 | (defcustom rst-reference-face 'rst-reference | |
b4747519 | 3253 | "References to a definition." |
2b1400b9 | 3254 | :version "24.1" |
94e9c286 SM |
3255 | :group 'rst-faces |
3256 | :type '(face)) | |
2b1400b9 GM |
3257 | (make-obsolete-variable 'rst-reference-face |
3258 | "customize the face `rst-reference' instead." | |
3259 | "24.1") | |
94e9c286 SM |
3260 | |
3261 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3262 | ||
3263 | (defgroup rst-faces-defaults nil | |
3264 | "Values used to generate default faces for section titles on all levels. | |
3265 | Tweak these if you are content with how section title faces are built in | |
3266 | general but you do not like the details." | |
3267 | :group 'rst-faces | |
3268 | :version "21.1") | |
3269 | ||
94e9c286 | 3270 | (defun rst-set-level-default (sym val) |
6d3f7c2f SM |
3271 | "Set custom variable SYM affecting section title text face. |
3272 | Recompute the faces. VAL is the value to set." | |
94e9c286 | 3273 | (custom-set-default sym val) |
6d3f7c2f | 3274 | ;; Also defines the faces initially when all values are available. |
94e9c286 SM |
3275 | (and (boundp 'rst-level-face-max) |
3276 | (boundp 'rst-level-face-format-light) | |
3277 | (boundp 'rst-level-face-base-color) | |
3278 | (boundp 'rst-level-face-step-light) | |
3279 | (boundp 'rst-level-face-base-light) | |
b4747519 | 3280 | (fboundp 'rst-define-level-faces) |
94e9c286 SM |
3281 | (rst-define-level-faces))) |
3282 | ||
6d3f7c2f | 3283 | ;; Faces for displaying items on several levels. These definitions define |
e4769531 | 3284 | ;; different shades of gray where the lightest one (i.e. least contrasting) is |
6d3f7c2f | 3285 | ;; used for level 1. |
94e9c286 SM |
3286 | (defcustom rst-level-face-max 6 |
3287 | "Maximum depth of levels for which section title faces are defined." | |
3288 | :group 'rst-faces-defaults | |
3289 | :type '(integer) | |
3290 | :set 'rst-set-level-default) | |
3291 | (defcustom rst-level-face-base-color "grey" | |
d13c8be6 | 3292 | "Base name of the color for creating background colors in section title faces." |
94e9c286 SM |
3293 | :group 'rst-faces-defaults |
3294 | :type '(string) | |
3295 | :set 'rst-set-level-default) | |
3296 | (defcustom rst-level-face-base-light | |
3297 | (if (eq frame-background-mode 'dark) | |
3298 | 15 | |
3299 | 85) | |
b4747519 SM |
3300 | "The lightness factor for the base color. This value is used for level 1. |
3301 | The default depends on whether the value of `frame-background-mode' is | |
3302 | `dark' or not." | |
94e9c286 SM |
3303 | :group 'rst-faces-defaults |
3304 | :type '(integer) | |
3305 | :set 'rst-set-level-default) | |
3306 | (defcustom rst-level-face-format-light "%2d" | |
3307 | "The format for the lightness factor appended to the base name of the color. | |
3308 | This value is expanded by `format' with an integer." | |
3309 | :group 'rst-faces-defaults | |
3310 | :type '(string) | |
3311 | :set 'rst-set-level-default) | |
3312 | (defcustom rst-level-face-step-light | |
3313 | (if (eq frame-background-mode 'dark) | |
3314 | 7 | |
3315 | -7) | |
b4747519 SM |
3316 | "The step width to use for the next color. |
3317 | The formula | |
94e9c286 SM |
3318 | |
3319 | `rst-level-face-base-light' | |
3320 | + (`rst-level-face-max' - 1) * `rst-level-face-step-light' | |
3321 | ||
3322 | must result in a color level which appended to `rst-level-face-base-color' | |
3323 | using `rst-level-face-format-light' results in a valid color such as `grey50'. | |
3324 | This color is used as background for section title text on level | |
3325 | `rst-level-face-max'." | |
3326 | :group 'rst-faces-defaults | |
3327 | :type '(integer) | |
3328 | :set 'rst-set-level-default) | |
3329 | ||
3330 | (defcustom rst-adornment-faces-alist | |
3331 | (let ((alist '((t . font-lock-keyword-face) | |
3332 | (nil . font-lock-keyword-face))) | |
3333 | (i 1)) | |
3334 | (while (<= i rst-level-face-max) | |
3335 | (nconc alist (list (cons i (intern (format "rst-level-%d-face" i))))) | |
3336 | (setq i (1+ i))) | |
3337 | alist) | |
b4747519 SM |
3338 | "Faces for the various adornment types. |
3339 | Key is a number (for the section title text of that level), | |
3340 | t (for transitions) or nil (for section title adornment). | |
3341 | If you generally do not like how section title text faces are | |
3342 | set up tweak here. If the general idea is ok for you but you do not like the | |
94e9c286 SM |
3343 | details check the Rst Faces Defaults group." |
3344 | :group 'rst-faces | |
3345 | :type '(alist | |
3346 | :key-type | |
3347 | (choice | |
3348 | (integer | |
3349 | :tag | |
3350 | "Section level (may not be bigger than `rst-level-face-max')") | |
3351 | (boolean :tag "transitions (on) / section title adornment (off)")) | |
3352 | :value-type (face)) | |
3353 | :set-after '(rst-level-face-max)) | |
3354 | ||
6d3f7c2f | 3355 | ;; FIXME: It should be possible to give "#RRGGBB" type of color values. |
b4747519 SM |
3356 | (defun rst-define-level-faces () |
3357 | "Define the faces for the section title text faces from the values." | |
6d3f7c2f | 3358 | ;; All variables used here must be checked in `rst-set-level-default'. |
b4747519 SM |
3359 | (let ((i 1)) |
3360 | (while (<= i rst-level-face-max) | |
3361 | (let ((sym (intern (format "rst-level-%d-face" i))) | |
3362 | (doc (format "Face for showing section title text at level %d" i)) | |
3363 | (col (format (concat "%s" rst-level-face-format-light) | |
3364 | rst-level-face-base-color | |
3365 | (+ (* (1- i) rst-level-face-step-light) | |
3366 | rst-level-face-base-light)))) | |
1d3cdbc7 | 3367 | (unless (facep sym) |
51d5b4ec JD |
3368 | (make-empty-face sym) |
3369 | (set-face-doc-string sym doc) | |
3370 | (set-face-background sym col) | |
3371 | (set sym sym)) | |
d13c8be6 | 3372 | (setq i (1+ i)))))) |
b4747519 | 3373 | |
0e90a43c | 3374 | (rst-define-level-faces) |
94e9c286 | 3375 | |
94e9c286 | 3376 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
94e9c286 | 3377 | |
d13c8be6 | 3378 | (defvar rst-font-lock-keywords |
94e9c286 | 3379 | ;; The reST-links in the comments below all relate to sections in |
6d3f7c2f | 3380 | ;; http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html. |
d13c8be6 | 3381 | `(;; FIXME: Block markup is not recognized in blocks after explicit markup |
6d3f7c2f | 3382 | ;; start. |
d13c8be6 SM |
3383 | |
3384 | ;; Simple `Body Elements`_ | |
3385 | ;; `Bullet Lists`_ | |
6d3f7c2f | 3386 | ;; FIXME: A bullet directly after a field name is not recognized. |
d13c8be6 SM |
3387 | (,(rst-re 'lin-beg '(:grp bul-sta)) |
3388 | 1 rst-block-face) | |
3389 | ;; `Enumerated Lists`_ | |
3390 | (,(rst-re 'lin-beg '(:grp enmany-sta)) | |
3391 | 1 rst-block-face) | |
6d3f7c2f SM |
3392 | ;; `Definition Lists`_ |
3393 | ;; FIXME: missing. | |
d13c8be6 SM |
3394 | ;; `Field Lists`_ |
3395 | (,(rst-re 'lin-beg '(:grp fld-tag) 'bli-sfx) | |
3396 | 1 rst-external-face) | |
3397 | ;; `Option Lists`_ | |
3398 | (,(rst-re 'lin-beg '(:grp opt-tag (:shy optsep-tag opt-tag) "*") | |
3399 | '(:alt "$" (:seq hws-prt "\\{2\\}"))) | |
3400 | 1 rst-block-face) | |
3401 | ;; `Line Blocks`_ | |
6d3f7c2f | 3402 | ;; Only for lines containing no more bar - to distinguish from tables. |
d13c8be6 SM |
3403 | (,(rst-re 'lin-beg '(:grp "|" bli-sfx) "[^|\n]*$") |
3404 | 1 rst-block-face) | |
3405 | ||
6d3f7c2f SM |
3406 | ;; `Tables`_ |
3407 | ;; FIXME: missing | |
d13c8be6 SM |
3408 | |
3409 | ;; All the `Explicit Markup Blocks`_ | |
3410 | ;; `Footnotes`_ / `Citations`_ | |
3411 | (,(rst-re 'lin-beg 'fnc-sta-2) | |
3412 | (1 rst-definition-face) | |
3413 | (2 rst-definition-face)) | |
3414 | ;; `Directives`_ / `Substitution Definitions`_ | |
3415 | (,(rst-re 'lin-beg 'dir-sta-3) | |
3416 | (1 rst-directive-face) | |
3417 | (2 rst-definition-face) | |
3418 | (3 rst-directive-face)) | |
3419 | ;; `Hyperlink Targets`_ | |
3420 | (,(rst-re 'lin-beg | |
3421 | '(:grp exm-sta "_" (:alt | |
3422 | (:seq "`" ilcbkqdef-tag "`") | |
3423 | (:seq (:alt "[^:\\\n]" "\\\\.") "+")) ":") | |
3424 | 'bli-sfx) | |
3425 | 1 rst-definition-face) | |
3426 | (,(rst-re 'lin-beg '(:grp "__") 'bli-sfx) | |
3427 | 1 rst-definition-face) | |
3428 | ||
6d3f7c2f SM |
3429 | ;; All `Inline Markup`_ |
3430 | ;; Most of them may be multiline though this is uninteresting. | |
d13c8be6 SM |
3431 | |
3432 | ;; FIXME: Condition 5 preventing fontification of e.g. "*" not implemented | |
6d3f7c2f | 3433 | ;; `Strong Emphasis`_. |
d13c8be6 SM |
3434 | (,(rst-re 'ilm-pfx '(:grp "\\*\\*" ilcast-tag "\\*\\*") 'ilm-sfx) |
3435 | 1 rst-emphasis2-face) | |
3436 | ;; `Emphasis`_ | |
3437 | (,(rst-re 'ilm-pfx '(:grp "\\*" ilcast-tag "\\*") 'ilm-sfx) | |
3438 | 1 rst-emphasis1-face) | |
3439 | ;; `Inline Literals`_ | |
3440 | (,(rst-re 'ilm-pfx '(:grp "``" ilcbkq-tag "``") 'ilm-sfx) | |
3441 | 1 rst-literal-face) | |
3442 | ;; `Inline Internal Targets`_ | |
3443 | (,(rst-re 'ilm-pfx '(:grp "_`" ilcbkq-tag "`") 'ilm-sfx) | |
3444 | 1 rst-definition-face) | |
3445 | ;; `Hyperlink References`_ | |
6d3f7c2f | 3446 | ;; FIXME: `Embedded URIs`_ not considered. |
c846da43 | 3447 | ;; FIXME: Directly adjacent marked up words are not fontified correctly |
6d3f7c2f | 3448 | ;; unless they are not separated by two spaces: foo_ bar_. |
d13c8be6 SM |
3449 | (,(rst-re 'ilm-pfx '(:grp (:alt (:seq "`" ilcbkq-tag "`") |
3450 | (:seq "\\sw" (:alt "\\sw" "-") "+\\sw")) | |
3451 | "__?") 'ilm-sfx) | |
3452 | 1 rst-reference-face) | |
3453 | ;; `Interpreted Text`_ | |
3454 | (,(rst-re 'ilm-pfx '(:grp (:shy ":" sym-tag ":") "?") | |
3455 | '(:grp "`" ilcbkq-tag "`") | |
3456 | '(:grp (:shy ":" sym-tag ":") "?") 'ilm-sfx) | |
3457 | (1 rst-directive-face) | |
3458 | (2 rst-external-face) | |
3459 | (3 rst-directive-face)) | |
3460 | ;; `Footnote References`_ / `Citation References`_ | |
3461 | (,(rst-re 'ilm-pfx '(:grp fnc-tag "_") 'ilm-sfx) | |
3462 | 1 rst-reference-face) | |
3463 | ;; `Substitution References`_ | |
3464 | ;; FIXME: References substitutions like |this|_ or |this|__ are not | |
6d3f7c2f | 3465 | ;; fontified correctly. |
d13c8be6 SM |
3466 | (,(rst-re 'ilm-pfx '(:grp sub-tag) 'ilm-sfx) |
3467 | 1 rst-reference-face) | |
3468 | ;; `Standalone Hyperlinks`_ | |
6d3f7c2f | 3469 | ;; FIXME: This takes it easy by using a whitespace as delimiter. |
d13c8be6 SM |
3470 | (,(rst-re 'ilm-pfx '(:grp uri-tag ":\\S +") 'ilm-sfx) |
3471 | 1 rst-definition-face) | |
3472 | (,(rst-re 'ilm-pfx '(:grp sym-tag "@" sym-tag ) 'ilm-sfx) | |
3473 | 1 rst-definition-face) | |
3474 | ||
6d3f7c2f | 3475 | ;; Do all block fontification as late as possible so 'append works. |
d13c8be6 | 3476 | |
6d3f7c2f SM |
3477 | ;; Sections_ / Transitions_ |
3478 | ;; For sections this is multiline. | |
d13c8be6 SM |
3479 | (,(rst-re 'ado-beg-2-1) |
3480 | (rst-font-lock-handle-adornment-matcher | |
3481 | (rst-font-lock-handle-adornment-pre-match-form | |
3482 | (match-string-no-properties 1) (match-end 1)) | |
3483 | nil | |
3484 | (1 (cdr (assoc nil rst-adornment-faces-alist)) append t) | |
3485 | (2 (cdr (assoc rst-font-lock-adornment-level | |
3486 | rst-adornment-faces-alist)) append t) | |
3487 | (3 (cdr (assoc nil rst-adornment-faces-alist)) append t))) | |
3488 | ||
3489 | ;; FIXME: FACESPEC could be used instead of ordinary faces to set | |
3490 | ;; properties on comments and literal blocks so they are *not* | |
6d3f7c2f | 3491 | ;; inline fontified. See (elisp)Search-based Fontification. |
d13c8be6 SM |
3492 | |
3493 | ;; FIXME: And / or use `syntax-propertize` functions as in `octave-mod.el` | |
6d3f7c2f SM |
3494 | ;; and other V24 modes. May make `font-lock-extend-region` |
3495 | ;; superfluous. | |
d13c8be6 | 3496 | |
6d3f7c2f SM |
3497 | ;; `Comments`_ |
3498 | ;; This is multiline. | |
d13c8be6 SM |
3499 | (,(rst-re 'lin-beg 'cmt-sta-1) |
3500 | (1 rst-comment-face) | |
3501 | (rst-font-lock-find-unindented-line-match | |
3502 | (rst-font-lock-find-unindented-line-limit (match-end 1)) | |
3503 | nil | |
3504 | (0 rst-comment-face append))) | |
3505 | (,(rst-re 'lin-beg '(:grp exm-tag) '(:grp hws-tag) "$") | |
3506 | (1 rst-comment-face) | |
3507 | (2 rst-comment-face) | |
3508 | (rst-font-lock-find-unindented-line-match | |
3509 | (rst-font-lock-find-unindented-line-limit 'next) | |
3510 | nil | |
3511 | (0 rst-comment-face append))) | |
3512 | ||
3513 | ;; FIXME: This is not rendered as comment:: | |
6d3f7c2f SM |
3514 | ;; .. .. list-table:: |
3515 | ;; :stub-columns: 1 | |
3516 | ;; :header-rows: 1 | |
d13c8be6 SM |
3517 | |
3518 | ;; FIXME: This is rendered wrong:: | |
3519 | ;; | |
3520 | ;; xxx yyy:: | |
3521 | ;; | |
3522 | ;; ----|> KKKKK <|---- | |
3523 | ;; / \ | |
3524 | ;; -|> AAAAAAAAAAPPPPPP <|- -|> AAAAAAAAAABBBBBBB <|- | |
3525 | ;; | | | | | |
3526 | ;; | | | | | |
3527 | ;; PPPPPP PPPPPPDDDDDDD BBBBBBB PPPPPPBBBBBBB | |
3528 | ;; | |
3529 | ;; Indentation needs to be taken from the line with the ``::`` and not from | |
3530 | ;; the first content line. | |
94e9c286 | 3531 | |
6d3f7c2f SM |
3532 | ;; `Indented Literal Blocks`_ |
3533 | ;; This is multiline. | |
d13c8be6 SM |
3534 | (,(rst-re 'lin-beg 'lit-sta-2) |
3535 | (2 rst-block-face) | |
3536 | (rst-font-lock-find-unindented-line-match | |
3537 | (rst-font-lock-find-unindented-line-limit t) | |
3538 | nil | |
3539 | (0 rst-literal-face append))) | |
94e9c286 | 3540 | |
6d3f7c2f SM |
3541 | ;; FIXME: `Quoted Literal Blocks`_ missing. |
3542 | ;; This is multiline. | |
94e9c286 | 3543 | |
d13c8be6 SM |
3544 | ;; `Doctest Blocks`_ |
3545 | ;; FIXME: This is wrong according to the specification: | |
3546 | ;; | |
3547 | ;; Doctest blocks are text blocks which begin with ">>> ", the Python | |
3548 | ;; interactive interpreter main prompt, and end with a blank line. | |
3549 | ;; Doctest blocks are treated as a special case of literal blocks, | |
3550 | ;; without requiring the literal block syntax. If both are present, the | |
3551 | ;; literal block syntax takes priority over Doctest block syntax: | |
3552 | ;; | |
3553 | ;; This is an ordinary paragraph. | |
3554 | ;; | |
3555 | ;; >>> print 'this is a Doctest block' | |
3556 | ;; this is a Doctest block | |
3557 | ;; | |
3558 | ;; The following is a literal block:: | |
3559 | ;; | |
3560 | ;; >>> This is not recognized as a doctest block by | |
3561 | ;; reStructuredText. It *will* be recognized by the doctest | |
3562 | ;; module, though! | |
3563 | ;; | |
3564 | ;; Indentation is not required for doctest blocks. | |
3565 | (,(rst-re 'lin-beg '(:grp (:alt ">>>" ell-tag)) '(:grp ".+")) | |
3566 | (1 rst-block-face) | |
3567 | (2 rst-literal-face)) | |
3568 | ) | |
3569 | "Keywords to highlight in rst mode.") | |
3570 | ||
8f6b6da8 JB |
3571 | (defvar font-lock-beg) |
3572 | (defvar font-lock-end) | |
3573 | ||
d13c8be6 | 3574 | (defun rst-font-lock-extend-region () |
6d3f7c2f SM |
3575 | "Extend the font-lock region if it might be in a multi-line construct. |
3576 | Return non-nil if so. Font-lock region is from `font-lock-beg' | |
3577 | to `font-lock-end'." | |
d13c8be6 SM |
3578 | (let ((r (rst-font-lock-extend-region-internal font-lock-beg font-lock-end))) |
3579 | (when r | |
3580 | (setq font-lock-beg (car r)) | |
3581 | (setq font-lock-end (cdr r)) | |
3582 | t))) | |
3583 | ||
3584 | (defun rst-font-lock-extend-region-internal (beg end) | |
6d3f7c2f | 3585 | "Check the region BEG / END for being in the middle of a multi-line construct. |
d13c8be6 SM |
3586 | Return nil if not or a cons with new values for BEG / END" |
3587 | (let ((nbeg (rst-font-lock-extend-region-extend beg -1)) | |
3588 | (nend (rst-font-lock-extend-region-extend end 1))) | |
3589 | (if (or nbeg nend) | |
3590 | (cons (or nbeg beg) (or nend end))))) | |
3591 | ||
3592 | (defun rst-forward-line (&optional n) | |
6d3f7c2f SM |
3593 | "Like `forward-line' but always end up in column 0 and return accordingly. |
3594 | Move N lines forward just as `forward-line'." | |
d13c8be6 SM |
3595 | (let ((moved (forward-line n))) |
3596 | (if (bolp) | |
3597 | moved | |
3598 | (forward-line 0) | |
3599 | (- moved (signum n))))) | |
3600 | ||
3601 | (defun rst-font-lock-extend-region-extend (pt dir) | |
3602 | "Extend the region starting at point PT and extending in direction DIR. | |
3603 | Return extended point or nil if not moved." | |
3604 | ;; There are many potential multiline constructs but there are two groups | |
3605 | ;; which are really relevant. The first group consists of | |
3606 | ;; | |
3607 | ;; * comment lines without leading explicit markup tag and | |
3608 | ;; | |
3609 | ;; * literal blocks following "::" | |
3610 | ;; | |
c846da43 | 3611 | ;; which are both indented. Thus indentation is the first thing recognized |
d13c8be6 SM |
3612 | ;; here. The second criteria is an explicit markup tag which may be a comment |
3613 | ;; or a double colon at the end of a line. | |
3614 | ;; | |
3615 | ;; The second group consists of the adornment cases. | |
3616 | (if (not (get-text-property pt 'font-lock-multiline)) | |
6d3f7c2f | 3617 | ;; Move only if we don't start inside a multiline construct already. |
d13c8be6 | 3618 | (save-excursion |
6d3f7c2f SM |
3619 | (let (;; Non-empty non-indented line, explicit markup tag or literal |
3620 | ;; block tag. | |
d13c8be6 SM |
3621 | (stop-re (rst-re '(:alt "[^ \t\n]" |
3622 | (:seq hws-tag exm-tag) | |
3623 | (:seq ".*" dcl-tag lin-end))))) | |
6d3f7c2f | 3624 | ;; The comments below are for dir == -1 / dir == 1. |
d13c8be6 SM |
3625 | (goto-char pt) |
3626 | (forward-line 0) | |
3627 | (setq pt (point)) | |
3628 | (while (and (not (looking-at stop-re)) | |
3629 | (zerop (rst-forward-line dir)))) ; try previous / next | |
6d3f7c2f | 3630 | ; line if it exists. |
d13c8be6 | 3631 | (if (looking-at (rst-re 'ado-beg-2-1)) ; may be an underline / |
6d3f7c2f | 3632 | ; overline. |
d13c8be6 SM |
3633 | (if (zerop (rst-forward-line dir)) |
3634 | (if (looking-at (rst-re 'ttl-beg)) ; title found, i.e. | |
3635 | ; underline / overline | |
6d3f7c2f | 3636 | ; found. |
d13c8be6 SM |
3637 | (if (zerop (rst-forward-line dir)) |
3638 | (if (not | |
3639 | (looking-at (rst-re 'ado-beg-2-1))) ; no | |
3640 | ; overline / | |
6d3f7c2f | 3641 | ; underline. |
d13c8be6 | 3642 | (rst-forward-line (- dir)))) ; step back to title |
6d3f7c2f SM |
3643 | ; / adornment. |
3644 | (if (< dir 0) ; keep downward adornment. | |
3645 | (rst-forward-line (- dir))))) ; step back to adornment. | |
3646 | (if (looking-at (rst-re 'ttl-beg)) ; may be a title. | |
d13c8be6 SM |
3647 | (if (zerop (rst-forward-line dir)) |
3648 | (if (not | |
3649 | (looking-at (rst-re 'ado-beg-2-1))) ; no overline / | |
6d3f7c2f SM |
3650 | ; underline. |
3651 | (rst-forward-line (- dir)))))) ; step back to line. | |
d13c8be6 SM |
3652 | (if (not (= (point) pt)) |
3653 | (point)))))) | |
94e9c286 SM |
3654 | |
3655 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3656 | ;; Indented blocks | |
3657 | ||
3658 | (defun rst-forward-indented-block (&optional column limit) | |
3659 | "Move forward across one indented block. | |
3660 | Find the next non-empty line which is not indented at least to COLUMN (defaults | |
b4747519 SM |
3661 | to the column of the point). Moves point to first character of this line or the |
3662 | first empty line immediately before it and returns that position. If there is | |
94e9c286 SM |
3663 | no such line before LIMIT (defaults to the end of the buffer) returns nil and |
3664 | point is not moved." | |
3665 | (interactive) | |
3666 | (let ((clm (or column (current-column))) | |
3667 | (start (point)) | |
3668 | fnd beg cand) | |
3669 | (if (not limit) | |
3670 | (setq limit (point-max))) | |
3671 | (save-match-data | |
3672 | (while (and (not fnd) (< (point) limit)) | |
3673 | (forward-line 1) | |
3674 | (when (< (point) limit) | |
3675 | (setq beg (point)) | |
d13c8be6 | 3676 | (if (looking-at (rst-re 'lin-end)) |
6d3f7c2f | 3677 | (setq cand (or cand beg)) ; An empty line is a candidate. |
94e9c286 SM |
3678 | (move-to-column clm) |
3679 | ;; FIXME: No indentation [(zerop clm)] must be handled in some | |
6d3f7c2f SM |
3680 | ;; useful way - though it is not clear what this should mean |
3681 | ;; at all. | |
94e9c286 | 3682 | (if (string-match |
d13c8be6 SM |
3683 | (rst-re 'linemp-tag) |
3684 | (buffer-substring-no-properties beg (point))) | |
6d3f7c2f | 3685 | (setq cand nil) ; An indented line resets a candidate. |
94e9c286 SM |
3686 | (setq fnd (or cand beg))))))) |
3687 | (goto-char (or fnd start)) | |
3688 | fnd)) | |
3689 | ||
d13c8be6 | 3690 | (defvar rst-font-lock-find-unindented-line-begin nil |
6d3f7c2f | 3691 | "Beginning of the match if `rst-font-lock-find-unindented-line-end'.") |
d13c8be6 SM |
3692 | |
3693 | (defvar rst-font-lock-find-unindented-line-end nil | |
3694 | "End of the match as determined by `rst-font-lock-find-unindented-line-limit'. | |
3695 | Also used as a trigger for | |
3696 | `rst-font-lock-find-unindented-line-match'.") | |
3697 | ||
3698 | (defun rst-font-lock-find-unindented-line-limit (ind-pnt) | |
c846da43 | 3699 | "Find the next unindented line relative to indentation at IND-PNT. |
d13c8be6 SM |
3700 | Return this point, the end of the buffer or nil if nothing found. |
3701 | If IND-PNT is `next' take the indentation from the next line if | |
6d3f7c2f | 3702 | this is not empty and indented more than the current one. If |
d13c8be6 SM |
3703 | IND-PNT is non-nil but not a number take the indentation from the |
3704 | next non-empty line if this is indented more than the current | |
3705 | one." | |
3706 | (setq rst-font-lock-find-unindented-line-begin ind-pnt) | |
3707 | (setq rst-font-lock-find-unindented-line-end | |
3708 | (save-excursion | |
3709 | (when (not (numberp ind-pnt)) | |
6d3f7c2f | 3710 | ;; Find indentation point in next line if any. |
d13c8be6 SM |
3711 | (setq ind-pnt |
3712 | ;; FIXME: Should be refactored to two different functions | |
3713 | ;; giving their result to this function, may be | |
6d3f7c2f | 3714 | ;; integrated in caller. |
d13c8be6 SM |
3715 | (save-match-data |
3716 | (let ((cur-ind (current-indentation))) | |
3717 | (if (eq ind-pnt 'next) | |
3718 | (when (and (zerop (forward-line 1)) | |
3719 | (< (point) (point-max))) | |
6d3f7c2f | 3720 | ;; Not at EOF. |
d13c8be6 SM |
3721 | (setq rst-font-lock-find-unindented-line-begin |
3722 | (point)) | |
3723 | (when (and (not (looking-at (rst-re 'lin-end))) | |
3724 | (> (current-indentation) cur-ind)) | |
6d3f7c2f | 3725 | ;; Use end of indentation if non-empty line. |
d13c8be6 SM |
3726 | (looking-at (rst-re 'hws-tag)) |
3727 | (match-end 0))) | |
6d3f7c2f | 3728 | ;; Skip until non-empty line or EOF. |
d13c8be6 SM |
3729 | (while (and (zerop (forward-line 1)) |
3730 | (< (point) (point-max)) | |
3731 | (looking-at (rst-re 'lin-end)))) | |
3732 | (when (< (point) (point-max)) | |
6d3f7c2f | 3733 | ;; Not at EOF. |
d13c8be6 SM |
3734 | (setq rst-font-lock-find-unindented-line-begin |
3735 | (point)) | |
3736 | (when (> (current-indentation) cur-ind) | |
6d3f7c2f | 3737 | ;; Indentation bigger than line of departure. |
d13c8be6 SM |
3738 | (looking-at (rst-re 'hws-tag)) |
3739 | (match-end 0)))))))) | |
3740 | (when ind-pnt | |
3741 | (goto-char ind-pnt) | |
3742 | (or (rst-forward-indented-block nil (point-max)) | |
3743 | (point-max)))))) | |
3744 | ||
3745 | (defun rst-font-lock-find-unindented-line-match (limit) | |
6d3f7c2f SM |
3746 | "Set the match found earlier if match were found. |
3747 | Match has been found by | |
d13c8be6 | 3748 | `rst-font-lock-find-unindented-line-limit' the first time called |
6d3f7c2f SM |
3749 | or no match is found. Return non-nil if match was found. LIMIT |
3750 | is not used but mandated by the caller." | |
d13c8be6 SM |
3751 | (when rst-font-lock-find-unindented-line-end |
3752 | (set-match-data | |
3753 | (list rst-font-lock-find-unindented-line-begin | |
3754 | rst-font-lock-find-unindented-line-end)) | |
3755 | (put-text-property rst-font-lock-find-unindented-line-begin | |
3756 | rst-font-lock-find-unindented-line-end | |
3757 | 'font-lock-multiline t) | |
6d3f7c2f | 3758 | ;; Make sure this is called only once. |
d13c8be6 SM |
3759 | (setq rst-font-lock-find-unindented-line-end nil) |
3760 | t)) | |
94e9c286 SM |
3761 | |
3762 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3763 | ;; Adornments | |
3764 | ||
d13c8be6 SM |
3765 | (defvar rst-font-lock-adornment-level nil |
3766 | "Storage for `rst-font-lock-handle-adornment-matcher'. | |
3767 | Either section level of the current adornment or t for a transition.") | |
3768 | ||
3769 | (defun rst-adornment-level (key) | |
3770 | "Return section level for adornment KEY. | |
3771 | KEY is the first element of the return list of | |
6d3f7c2f SM |
3772 | `rst-classify-adornment'. If KEY is not a cons return it. If KEY is found |
3773 | in the hierarchy return its level. Otherwise return a level one | |
d13c8be6 SM |
3774 | beyond the existing hierarchy." |
3775 | (if (not (consp key)) | |
3776 | key | |
3777 | (let* ((hier (rst-get-hierarchy)) | |
3778 | (char (car key)) | |
3779 | (style (cdr key))) | |
3780 | (1+ (or (position-if (lambda (elt) | |
3781 | (and (equal (car elt) char) | |
3782 | (equal (cadr elt) style))) hier) | |
3783 | (length hier)))))) | |
3784 | ||
3785 | (defvar rst-font-lock-adornment-match nil | |
3786 | "Storage for match for current adornment. | |
6d3f7c2f | 3787 | Set by `rst-font-lock-handle-adornment-pre-match-form'. Also used |
d13c8be6 SM |
3788 | as a trigger for `rst-font-lock-handle-adornment-matcher'.") |
3789 | ||
3790 | (defun rst-font-lock-handle-adornment-pre-match-form (ado ado-end) | |
6d3f7c2f SM |
3791 | "Determine limit for adornments. |
3792 | Determine all things necessary for font-locking section titles | |
3793 | and transitions and put the result to | |
d13c8be6 | 3794 | `rst-font-lock-adornment-match' and |
6d3f7c2f SM |
3795 | `rst-font-lock-adornment-level'. ADO is the complete adornment |
3796 | matched. ADO-END is the point where ADO ends. Return the point | |
d13c8be6 SM |
3797 | where the whole adorned construct ends. |
3798 | ||
3799 | Called as a PRE-MATCH-FORM in the sense of `font-lock-keywords'." | |
3800 | (let ((ado-data (rst-classify-adornment ado ado-end))) | |
3801 | (if (not ado-data) | |
3802 | (setq rst-font-lock-adornment-level nil | |
3803 | rst-font-lock-adornment-match nil) | |
3804 | (setq rst-font-lock-adornment-level | |
3805 | (rst-adornment-level (car ado-data))) | |
3806 | (setq rst-font-lock-adornment-match (cdr ado-data)) | |
6d3f7c2f SM |
3807 | (goto-char (nth 1 ado-data)) ; Beginning of construct. |
3808 | (nth 2 ado-data)))) ; End of construct. | |
d13c8be6 SM |
3809 | |
3810 | (defun rst-font-lock-handle-adornment-matcher (limit) | |
6d3f7c2f SM |
3811 | "Set the match found earlier if match were found. |
3812 | Match has been found by | |
3813 | `rst-font-lock-handle-adornment-pre-match-form' the first time | |
3814 | called or no match is found. Return non-nil if match was found. | |
d13c8be6 | 3815 | |
6d3f7c2f SM |
3816 | Called as a MATCHER in the sense of `font-lock-keywords'. |
3817 | LIMIT is not used but mandated by the caller." | |
d13c8be6 | 3818 | (let ((match rst-font-lock-adornment-match)) |
6d3f7c2f | 3819 | ;; May run only once - enforce this. |
d13c8be6 SM |
3820 | (setq rst-font-lock-adornment-match nil) |
3821 | (when match | |
3822 | (set-match-data match) | |
3823 | (goto-char (match-end 0)) | |
3824 | (put-text-property (match-beginning 0) (match-end 0) | |
3825 | 'font-lock-multiline t) | |
3826 | t))) | |
94e9c286 SM |
3827 | |
3828 | \f | |
3829 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
d13c8be6 | 3830 | ;; Compilation |
94e9c286 SM |
3831 | |
3832 | (defgroup rst-compile nil | |
3833 | "Settings for support of conversion of reStructuredText | |
3834 | document with \\[rst-compile]." | |
3835 | :group 'rst | |
3836 | :version "21.1") | |
3837 | ||
ef4271fe GM |
3838 | (defcustom rst-compile-toolsets |
3839 | `((html ,(if (executable-find "rst2html.py") "rst2html.py" "rst2html") | |
3840 | ".html" nil) | |
3841 | (latex ,(if (executable-find "rst2latex.py") "rst2latex.py" "rst2latex") | |
3842 | ".tex" nil) | |
3843 | (newlatex ,(if (executable-find "rst2newlatex.py") "rst2newlatex.py" | |
3844 | "rst2newlatex") | |
3845 | ".tex" nil) | |
3846 | (pseudoxml ,(if (executable-find "rst2pseudoxml.py") "rst2pseudoxml.py" | |
3847 | "rst2pseudoxml") | |
3848 | ".xml" nil) | |
3849 | (xml ,(if (executable-find "rst2xml.py") "rst2xml.py" "rst2xml") | |
3850 | ".xml" nil) | |
3851 | (pdf ,(if (executable-find "rst2pdf.py") "rst2pdf.py" "rst2pdf") | |
3852 | ".pdf" nil) | |
3853 | (s5 ,(if (executable-find "rst2s5.py") "rst2s5.py" "rst2s5") | |
3854 | ".html" nil)) | |
6d3f7c2f SM |
3855 | "Table describing the command to use for each tool-set. |
3856 | An association list of the tool-set to a list of the (command to use, | |
94e9c286 | 3857 | extension of produced filename, options to the tool (nil or a |
ef4271fe | 3858 | string)) to be used for converting the document." |
d13c8be6 | 3859 | ;; FIXME: These are not options but symbols which may be referenced by |
6d3f7c2f | 3860 | ;; `rst-compile-*-toolset` below. |
3f1b6eb2 | 3861 | :type '(alist :options (html latex newlatex pseudoxml xml pdf s5) |
ef4271fe GM |
3862 | :key-type symbol |
3863 | :value-type (list :tag "Specification" | |
3864 | (file :tag "Command") | |
3865 | (string :tag "File extension") | |
3866 | (choice :tag "Command options" | |
3867 | (const :tag "No options" nil) | |
3868 | (string :tag "Options")))) | |
3869 | :group 'rst | |
3870 | :version "24.1") | |
94e9c286 | 3871 | |
6d3f7c2f | 3872 | ;; FIXME: Must be `defcustom`. |
94e9c286 | 3873 | (defvar rst-compile-primary-toolset 'html |
6d3f7c2f | 3874 | "The default tool-set for `rst-compile'.") |
94e9c286 | 3875 | |
6d3f7c2f | 3876 | ;; FIXME: Must be `defcustom`. |
94e9c286 | 3877 | (defvar rst-compile-secondary-toolset 'latex |
6d3f7c2f | 3878 | "The default tool-set for `rst-compile' with a prefix argument.") |
94e9c286 SM |
3879 | |
3880 | (defun rst-compile-find-conf () | |
3881 | "Look for the configuration file in the parents of the current path." | |
3882 | (interactive) | |
3883 | (let ((file-name "docutils.conf") | |
3884 | (buffer-file (buffer-file-name))) | |
3885 | ;; Move up in the dir hierarchy till we find a change log file. | |
3886 | (let* ((dir (file-name-directory buffer-file)) | |
3887 | (prevdir nil)) | |
3888 | (while (and (or (not (string= dir prevdir)) | |
3889 | (setq dir nil) | |
3890 | nil) | |
3891 | (not (file-exists-p (concat dir file-name)))) | |
3892 | ;; Move up to the parent dir and try again. | |
3893 | (setq prevdir dir) | |
3894 | (setq dir (expand-file-name (file-name-directory | |
3895 | (directory-file-name | |
3896 | (file-name-directory dir))))) | |
3897 | ) | |
3898 | (or (and dir (concat dir file-name)) nil) | |
3899 | ))) | |
3900 | ||
3901 | ||
3902 | (require 'compile) | |
3903 | ||
d13c8be6 | 3904 | (defun rst-compile (&optional use-alt) |
94e9c286 SM |
3905 | "Compile command to convert reST document into some output file. |
3906 | Attempts to find configuration file, if it can, overrides the | |
d13c8be6 | 3907 | options. There are two commands to choose from, with USE-ALT, |
6d3f7c2f | 3908 | select the alternative tool-set." |
94e9c286 SM |
3909 | (interactive "P") |
3910 | ;; Note: maybe we want to check if there is a Makefile too and not do anything | |
3911 | ;; if that is the case. I dunno. | |
d13c8be6 | 3912 | (let* ((toolset (cdr (assq (if use-alt |
94e9c286 SM |
3913 | rst-compile-secondary-toolset |
3914 | rst-compile-primary-toolset) | |
3915 | rst-compile-toolsets))) | |
3916 | (command (car toolset)) | |
3917 | (extension (cadr toolset)) | |
3918 | (options (caddr toolset)) | |
3919 | (conffile (rst-compile-find-conf)) | |
3920 | (bufname (file-name-nondirectory buffer-file-name)) | |
3921 | (outname (file-name-sans-extension bufname))) | |
3922 | ||
3923 | ;; Set compile-command before invocation of compile. | |
3924 | (set (make-local-variable 'compile-command) | |
3925 | (mapconcat 'identity | |
3926 | (list command | |
3927 | (or options "") | |
3928 | (if conffile | |
d13c8be6 | 3929 | (concat "--config=" (shell-quote-argument conffile)) |
94e9c286 | 3930 | "") |
d13c8be6 SM |
3931 | (shell-quote-argument bufname) |
3932 | (shell-quote-argument (concat outname extension))) | |
94e9c286 SM |
3933 | " ")) |
3934 | ||
3935 | ;; Invoke the compile command. | |
d13c8be6 | 3936 | (if (or compilation-read-command use-alt) |
94e9c286 SM |
3937 | (call-interactively 'compile) |
3938 | (compile compile-command)) | |
3939 | )) | |
3940 | ||
3941 | (defun rst-compile-alt-toolset () | |
6d3f7c2f | 3942 | "Compile command with the alternative tool-set." |
94e9c286 | 3943 | (interactive) |
d13c8be6 | 3944 | (rst-compile t)) |
94e9c286 SM |
3945 | |
3946 | (defun rst-compile-pseudo-region () | |
6d3f7c2f SM |
3947 | "Show pseudo-XML rendering. |
3948 | Rendering is done of the current active region, or of the entire | |
3949 | buffer, if the region is not selected." | |
3950 | ;; FIXME: The region should be given interactively. | |
94e9c286 SM |
3951 | (interactive) |
3952 | (with-output-to-temp-buffer "*pseudoxml*" | |
3953 | (shell-command-on-region | |
3954 | (if mark-active (region-beginning) (point-min)) | |
3955 | (if mark-active (region-end) (point-max)) | |
ef4271fe | 3956 | (cadr (assq 'pseudoxml rst-compile-toolsets)) |
94e9c286 SM |
3957 | standard-output))) |
3958 | ||
6d3f7c2f | 3959 | ;; FIXME: Should be `defcustom`. |
94e9c286 SM |
3960 | (defvar rst-pdf-program "xpdf" |
3961 | "Program used to preview PDF files.") | |
3962 | ||
3963 | (defun rst-compile-pdf-preview () | |
3964 | "Convert the document to a PDF file and launch a preview program." | |
3965 | (interactive) | |
d13c8be6 SM |
3966 | (let* ((tmp-filename (make-temp-file "rst_el" nil ".pdf")) |
3967 | (command (format "%s %s %s && %s %s ; rm %s" | |
ef4271fe | 3968 | (cadr (assq 'pdf rst-compile-toolsets)) |
94e9c286 | 3969 | buffer-file-name tmp-filename |
d13c8be6 | 3970 | rst-pdf-program tmp-filename tmp-filename))) |
94e9c286 SM |
3971 | (start-process-shell-command "rst-pdf-preview" nil command) |
3972 | ;; Note: you could also use (compile command) to view the compilation | |
3973 | ;; output. | |
3974 | )) | |
3975 | ||
6d3f7c2f | 3976 | ;; FIXME: Should be `defcustom` or use something like `browse-url`. |
94e9c286 SM |
3977 | (defvar rst-slides-program "firefox" |
3978 | "Program used to preview S5 slides.") | |
3979 | ||
3980 | (defun rst-compile-slides-preview () | |
3981 | "Convert the document to an S5 slide presentation and launch a preview program." | |
3982 | (interactive) | |
d13c8be6 SM |
3983 | (let* ((tmp-filename (make-temp-file "rst_el" nil ".html")) |
3984 | (command (format "%s %s %s && %s %s ; rm %s" | |
ef4271fe | 3985 | (cadr (assq 's5 rst-compile-toolsets)) |
94e9c286 | 3986 | buffer-file-name tmp-filename |
d13c8be6 | 3987 | rst-slides-program tmp-filename tmp-filename))) |
94e9c286 SM |
3988 | (start-process-shell-command "rst-slides-preview" nil command) |
3989 | ;; Note: you could also use (compile command) to view the compilation | |
3990 | ;; output. | |
3991 | )) | |
3992 | ||
94e9c286 SM |
3993 | \f |
3994 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
94e9c286 | 3995 | ;; Generic text functions that are more convenient than the defaults. |
94e9c286 | 3996 | |
6d3f7c2f | 3997 | ;; FIXME: Unbound command - should be bound or removed. |
94e9c286 | 3998 | (defun rst-replace-lines (fromchar tochar) |
6d3f7c2f | 3999 | "Replace flush-left lines of FROMCHAR with equal-length lines of TOCHAR." |
94e9c286 SM |
4000 | (interactive "\ |
4001 | cSearch for flush-left lines of char: | |
4002 | cand replace with char: ") | |
4003 | (save-excursion | |
d13c8be6 | 4004 | (let ((searchre (rst-re "^" fromchar "+\\( *\\)$")) |
b4747519 SM |
4005 | (found 0)) |
4006 | (while (search-forward-regexp searchre nil t) | |
4007 | (setq found (1+ found)) | |
4008 | (goto-char (match-beginning 1)) | |
4009 | (let ((width (current-column))) | |
4010 | (rst-delete-entire-line) | |
4011 | (insert-char tochar width))) | |
4012 | (message (format "%d lines replaced." found))))) | |
94e9c286 | 4013 | |
6d3f7c2f | 4014 | ;; FIXME: Unbound command - should be bound or removed. |
94e9c286 SM |
4015 | (defun rst-join-paragraph () |
4016 | "Join lines in current paragraph into one line, removing end-of-lines." | |
4017 | (interactive) | |
6d3f7c2f | 4018 | (let ((fill-column 65000)) ; some big number. |
94e9c286 SM |
4019 | (call-interactively 'fill-paragraph))) |
4020 | ||
6d3f7c2f | 4021 | ;; FIXME: Unbound command - should be bound or removed. |
94e9c286 SM |
4022 | (defun rst-force-fill-paragraph () |
4023 | "Fill paragraph at point, first joining the paragraph's lines into one. | |
4024 | This is useful for filling list item paragraphs." | |
4025 | (interactive) | |
4026 | (rst-join-paragraph) | |
4027 | (fill-paragraph nil)) | |
4028 | ||
4029 | ||
6d3f7c2f | 4030 | ;; FIXME: Unbound command - should be bound or removed. |
94e9c286 SM |
4031 | ;; Generic character repeater function. |
4032 | ;; For sections, better to use the specialized function above, but this can | |
4033 | ;; be useful for creating separators. | |
d13c8be6 | 4034 | (defun rst-repeat-last-character (use-next) |
6d3f7c2f SM |
4035 | "Fill the current line using the last character on the current line. |
4036 | Fill up to the length of the preceding line or up to | |
4037 | `fill-column' if preceding line is empty. | |
94e9c286 | 4038 | |
d13c8be6 | 4039 | If USE-NEXT, use the next line rather than the preceding line. |
94e9c286 SM |
4040 | |
4041 | If the current line is longer than the desired length, shave the characters off | |
4042 | the current line to fit the desired length. | |
4043 | ||
4044 | As an added convenience, if the command is repeated immediately, the alternative | |
4045 | column is used (fill-column vs. end of previous/next line)." | |
d13c8be6 | 4046 | (interactive "P") |
94e9c286 SM |
4047 | (let* ((curcol (current-column)) |
4048 | (curline (+ (count-lines (point-min) (point)) | |
d13c8be6 | 4049 | (if (zerop curcol) 1 0))) |
94e9c286 | 4050 | (lbp (line-beginning-position 0)) |
d13c8be6 | 4051 | (prevcol (if (and (= curline 1) (not use-next)) |
94e9c286 SM |
4052 | fill-column |
4053 | (save-excursion | |
d13c8be6 | 4054 | (forward-line (if use-next 1 -1)) |
94e9c286 SM |
4055 | (end-of-line) |
4056 | (skip-chars-backward " \t" lbp) | |
4057 | (let ((cc (current-column))) | |
d13c8be6 | 4058 | (if (zerop cc) fill-column cc))))) |
94e9c286 | 4059 | (rightmost-column |
d13c8be6 | 4060 | (cond ((equal last-command 'rst-repeat-last-character) |
94e9c286 SM |
4061 | (if (= curcol fill-column) prevcol fill-column)) |
4062 | (t (save-excursion | |
d13c8be6 | 4063 | (if (zerop prevcol) fill-column prevcol))) |
94e9c286 SM |
4064 | )) ) |
4065 | (end-of-line) | |
4066 | (if (> (current-column) rightmost-column) | |
6d3f7c2f | 4067 | ;; Shave characters off the end. |
94e9c286 SM |
4068 | (delete-region (- (point) |
4069 | (- (current-column) rightmost-column)) | |
4070 | (point)) | |
6d3f7c2f | 4071 | ;; Fill with last characters. |
94e9c286 SM |
4072 | (insert-char (preceding-char) |
4073 | (- rightmost-column (current-column)))) | |
4074 | )) | |
4075 | ||
4076 | ||
4077 | (defun rst-portable-mark-active-p () | |
6d3f7c2f SM |
4078 | "Return non-nil if the mark is active. |
4079 | This is a portable function." | |
94e9c286 SM |
4080 | (cond |
4081 | ((fboundp 'region-active-p) (region-active-p)) | |
ef4271fe GM |
4082 | ((boundp 'transient-mark-mode) (and transient-mark-mode mark-active)) |
4083 | (t mark-active))) | |
94e9c286 SM |
4084 | |
4085 | \f | |
4086 | (provide 'rst) | |
6d3f7c2f SM |
4087 | |
4088 | ;; LocalWords: docutils http sourceforge rst html wp svn svnroot txt reST regex | |
4089 | ;; LocalWords: regexes alist seq alt grp keymap abbrev overline overlines toc | |
4090 | ;; LocalWords: XML PNT propertized referencable | |
4091 | ||
4092 | ;; Local Variables: | |
4093 | ;; sentence-end-double-space: t | |
4094 | ;; End: | |
4095 | ||
4096 | ;;; rst.el ends here. |