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