Mercurial > emacs
annotate lisp/textmodes/rst.el @ 96126:fcd827c4a553
Fix up docstring conventions.
Move vars to before their first use.
(rst-mode): Don't mess with font-lock-support-mode.
(rst-suggest-new-decoration, rst-adjust-decoration):
Avoid CL's copy-list.
(rst-delete-entire-line): Use line-beginning-position.
(rst-position): New fun.
(rst-straighten-decorations): Use it instead of CL's position.
(rst-straighten-bullets-region): Avoid CL's mapcar*.
(rst-toc-mode): Use define-derived-mode.
(rst-iterate-leftmost-paragraphs, rst-iterate-leftmost-paragraphs-2):
Remove unused var `in-par'. Use `point' rather than `point-marker'.
(rst-line-block-region): Reduce redundancy. Use the `pfxarg' arg.
(rst-replace-lines): Simplify.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Fri, 20 Jun 2008 17:53:42 +0000 |
parents | 8e11daf373e3 |
children | 8b630d07bb3f |
rev | line source |
---|---|
96117 | 1 ;;; rst.el --- Mode for viewing and editing reStructuredText-documents. |
2 | |
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 | |
4 ;; Free Software Foundation, Inc. | |
5 | |
6 ;; Authors: Martin Blais <blais@furius.ca>, | |
7 ;; Stefan Merten <smerten@oekonux.de>, | |
8 ;; David Goodger <goodger@python.org> | |
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 | |
27 ;; This package provides major mode rst-mode, which supports documents marked up | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
28 ;; using the reStructuredText format. Support includes font locking as well as |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
29 ;; some convenience functions for editing. It does this by defining a Emacs |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
30 ;; major mode: rst-mode (ReST). This mode is derived from text-mode (and |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
31 ;; inherits much of it). This package also contains: |
96117 | 32 ;; |
33 ;; - Functions to automatically adjust and cycle the section underline | |
34 ;; decorations; | |
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; | |
39 ;; - Font-lock highlighting of notable reStructuredText structures; | |
40 ;; - Some other convenience functions. | |
41 ;; | |
42 ;; See the accompanying document in the docutils documentation about | |
43 ;; the contents of this package and how to use it. | |
44 ;; | |
45 ;; For more information about reStructuredText, see | |
46 ;; http://docutils.sourceforge.net/rst.html | |
47 ;; | |
48 ;; For full details on how to use the contents of this file, see | |
49 ;; http://docutils.sourceforge.net/docs/user/emacs.html | |
50 ;; | |
51 ;; | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
52 ;; There are a number of convenient keybindings provided by rst-mode. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
53 ;; The main one is |
96117 | 54 ;; |
55 ;; C-c C-a (also C-=): rst-adjust | |
56 ;; | |
57 ;; Updates or rotates the section title around point or promotes/demotes the | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
58 ;; decorations within the region (see full details below). Note that C-= is a |
96117 | 59 ;; good binding, since it allows you to specify a negative arg easily with C-- |
60 ;; C-= (easy to type), as well as ordinary prefix arg with C-u C-=. | |
61 ;; | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
62 ;; For more on bindings, see rst-mode-map below. There are also many variables |
96117 | 63 ;; that can be customized, look for defcustom and defvar in this file. |
64 ;; | |
65 ;; If you use the table-of-contents feature, you may want to add a hook to | |
66 ;; update the TOC automatically everytime you adjust a section title:: | |
67 ;; | |
68 ;; (add-hook 'rst-adjust-hook 'rst-toc-update) | |
69 ;; | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
70 ;; Syntax highlighting: font-lock is enabled by default. If you want to turn |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
71 ;; off syntax highlighting to rst-mode, you can use the following:: |
96117 | 72 ;; |
73 ;; (setq font-lock-global-modes '(not rst-mode ...)) | |
74 ;; | |
75 | |
76 | |
77 ;; CUSTOMIZATION | |
78 ;; | |
79 ;; rst | |
80 ;; --- | |
81 ;; This group contains some general customizable features. | |
82 ;; | |
83 ;; The group is contained in the wp group. | |
84 ;; | |
85 ;; rst-faces | |
86 ;; --------- | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
87 ;; This group contains all necessary for customizing fonts. The default |
96117 | 88 ;; settings use standard font-lock-*-face's so if you set these to your |
89 ;; liking they are probably good in rst-mode also. | |
90 ;; | |
91 ;; The group is contained in the faces group as well as in the rst group. | |
92 ;; | |
93 ;; rst-faces-defaults | |
94 ;; ------------------ | |
95 ;; This group contains all necessary for customizing the default fonts used for | |
96 ;; section title faces. | |
97 ;; | |
98 ;; The general idea for section title faces is to have a non-default background | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
99 ;; but do not change the background. The section level is shown by the |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
100 ;; lightness of the background color. If you like this general idea of |
96117 | 101 ;; generating faces for section titles but do not like the details this group |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
102 ;; is the point where you can customize the details. If you do not like the |
96117 | 103 ;; general idea, however, you should customize the faces used in |
104 ;; rst-adornment-faces-alist. | |
105 ;; | |
106 ;; Note: If you are using a dark background please make sure the variable | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
107 ;; frame-background-mode is set to the symbol dark. This triggers |
96117 | 108 ;; some default values which are probably right for you. |
109 ;; | |
110 ;; The group is contained in the rst-faces group. | |
111 ;; | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
112 ;; All customizable features have a comment explaining their meaning. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
113 ;; Refer to the customization of your Emacs (try ``M-x customize``). |
96117 | 114 |
115 | |
116 ;;; DOWNLOAD | |
117 | |
118 ;; The latest version of this file lies in the docutils source code repository: | |
119 ;; http://svn.berlios.de/svnroot/repos/docutils/trunk/docutils/tools/editors/emacs/rst.el | |
120 | |
121 | |
122 ;;; INSTALLATION | |
123 | |
124 ;; Add the following lines to your `.emacs' file: | |
125 ;; | |
126 ;; (require 'rst) | |
127 ;; | |
128 ;; If you are using `.txt' as a standard extension for reST files as | |
129 ;; http://docutils.sourceforge.net/FAQ.html#what-s-the-standard-filename-extension-for-a-restructuredtext-file | |
130 ;; suggests you may use one of the `Local Variables in Files' mechanism Emacs | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
131 ;; provides to set the major mode automatically. For instance you may use:: |
96117 | 132 ;; |
133 ;; .. -*- mode: rst -*- | |
134 ;; | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
135 ;; in the very first line of your file. The following code is useful if you |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
136 ;; want automatically enter rst-mode from any file with compatible extensions: |
96117 | 137 ;; |
138 ;; (setq auto-mode-alist | |
139 ;; (append '(("\\.txt$" . rst-mode) | |
140 ;; ("\\.rst$" . rst-mode) | |
141 ;; ("\\.rest$" . rst-mode)) auto-mode-alist)) | |
142 ;; | |
143 | |
144 ;;; BUGS | |
145 | |
146 ;; - rst-enumeration-region: Select a single paragraph, with the top at one | |
147 ;; blank line before the beginning, and it will fail. | |
148 ;; - The active region goes away when we shift it left or right, and this | |
149 ;; prevents us from refilling it automatically when shifting many times. | |
150 ;; - The suggested decorations when adjusting should not have to cycle | |
151 ;; below one below the last section decoration level preceding the | |
152 ;; cursor. We need to fix that. | |
153 | |
154 ;;; TODO LIST | |
155 | |
156 ;; rst-toc-insert features | |
157 ;; ------------------------ | |
158 ;; - rst-toc-insert: We should parse the contents:: options to figure out how | |
159 ;; deep to render the inserted TOC. | |
160 ;; - On load, detect any existing TOCs and set the properties for links. | |
161 ;; - TOC insertion should have an option to add empty lines. | |
162 ;; - TOC insertion should deal with multiple lines. | |
163 ;; - There is a bug on redo after undo of adjust when rst-adjust-hook uses the | |
164 ;; automatic toc update. The cursor ends up in the TOC and this is | |
165 ;; annoying. Gotta fix that. | |
166 ;; - numbering: automatically detect if we have a section-numbering directive in | |
167 ;; the corresponding section, to render the toc. | |
168 ;; | |
169 ;; bulleted and enumerated list items | |
170 ;; ---------------------------------- | |
171 ;; - We need to provide way to rebullet bulleted lists, and that would include | |
172 ;; automatic enumeration as well. | |
173 ;; | |
174 ;; Other | |
175 ;; ----- | |
176 ;; - It would be nice to differentiate between text files using | |
177 ;; reStructuredText_ and other general text files. If we had a | |
178 ;; function to automatically guess whether a .txt file is following the | |
179 ;; reStructuredText_ conventions, we could trigger rst-mode without | |
180 ;; having to hard-code this in every text file, nor forcing the user to | |
181 ;; add a local mode variable at the top of the file. | |
182 ;; We could perform this guessing by searching for a valid decoration | |
183 ;; at the top of the document or searching for reStructuredText_ | |
184 ;; directives further on. | |
185 ;; | |
186 ;; - We should support imenu in our major mode, with the menu filled with the | |
187 ;; section titles (this should be really easy). | |
188 ;; | |
189 ;; - We should rename "adornment" to "decoration" or vice-versa in this | |
190 ;; document (Stefan's code ("adornment") vs Martin ("decoration")), maybe some | |
191 ;; functions even overlap. | |
192 ;; | |
193 ;; - We need to automatically recenter on rst-forward-section movement commands. | |
194 | |
195 | |
196 ;;; HISTORY | |
197 ;; | |
198 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
199 ;;; Code: |
96117 | 200 |
201 | |
202 (defgroup rst nil "Support for reStructuredText documents" | |
203 :group 'wp | |
204 :version "23.1" | |
205 :link '(url-link "http://docutils.sourceforge.net/rst.html")) | |
206 | |
207 | |
208 | |
209 | |
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
211 ;; Define some generic support functions. | |
212 | |
213 (eval-when-compile (require 'cl)) ;; We need this for destructuring-bind below. | |
214 | |
215 | |
216 ;; From Emacs-22 | |
217 (unless (fboundp 'line-number-at-pos) | |
218 (defun line-number-at-pos (&optional pos) | |
219 "Return (narrowed) buffer line number at position POS. | |
220 If POS is nil, use current buffer location." | |
221 (let ((opoint (or pos (point))) start) | |
222 (save-excursion | |
223 (goto-char (point-min)) | |
224 (setq start (point)) | |
225 (goto-char opoint) | |
226 (forward-line 0) | |
227 (1+ (count-lines start (point)))))) ) | |
228 | |
229 | |
230 | |
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
232 ;; Mode definition. | |
233 | |
234 ;; Key bindings. | |
235 (defvar rst-mode-map | |
236 (let ((map (make-sparse-keymap))) | |
237 | |
238 ;; | |
239 ;; Section Decorations. | |
240 ;; | |
241 ;; The adjustment function that decorates or rotates a section title. | |
242 (define-key map [(control c) (control a)] 'rst-adjust) | |
243 (define-key map [(control c) (control ?=)] 'rst-adjust) | |
244 (define-key map [(control ?=)] 'rst-adjust) ;; (Does not work on the Mac OSX.) | |
245 ;; Display the hierarchy of decorations implied by the current document contents. | |
246 (define-key map [(control c) (control h)] 'rst-display-decorations-hierarchy) | |
247 ;; Homogeneize the decorations in the document. | |
248 (define-key map [(control c) (control s)] 'rst-straighten-decorations) | |
249 ;; (define-key map [(control c) (control s)] 'rst-straighten-deco-spacing) | |
250 | |
251 ;; | |
252 ;; Section Movement and Selection. | |
253 ;; | |
254 ;; Mark the subsection where the cursor is. | |
255 (define-key map [(control c) (control m)] 'rst-mark-section) | |
256 ;; Move forward/backward between section titles. | |
257 (define-key map [(control c) (control n)] 'rst-forward-section) | |
258 (define-key map [(control c) (control p)] 'rst-backward-section) | |
259 | |
260 ;; | |
261 ;; Operating on Blocks of Text. | |
262 ;; | |
263 ;; Makes paragraphs in region as a bullet list. | |
264 (define-key map [(control c) (control b)] 'rst-bullet-list-region) | |
265 ;; Makes paragraphs in region as a enumeration. | |
266 (define-key map [(control c) (control e)] 'rst-enumerate-region) | |
267 ;; Converts bullets to an enumeration. | |
268 (define-key map [(control c) (control v)] 'rst-convert-bullets-to-enumeration) | |
269 ;; Makes region a line-block. | |
270 (define-key map [(control c) (control d)] 'rst-line-block-region) | |
271 ;; Make sure that all the bullets in the region are consistent. | |
272 (define-key map [(control c) (control w)] 'rst-straighten-bullets-region) | |
273 ;; Shift region left or right (taking into account of enumerations/bullets, etc.). | |
274 (define-key map [(control c) (control l)] 'rst-shift-region-left) | |
275 (define-key map [(control c) (control r)] 'rst-shift-region-right) | |
276 ;; Comment/uncomment the active region. | |
277 (define-key map [(control c) (control c)] 'comment-region) | |
278 | |
279 ;; | |
280 ;; Table-of-Contents Features. | |
281 ;; | |
282 ;; Enter a TOC buffer to view and move to a specific section. | |
283 (define-key map [(control c) (control t)] 'rst-toc) | |
284 ;; Insert a TOC here. | |
285 (define-key map [(control c) (control i)] 'rst-toc-insert) | |
286 ;; Update the document's TOC (without changing the cursor position). | |
287 (define-key map [(control c) (control u)] 'rst-toc-update) | |
288 ;; Got to the section under the cursor (cursor must be in TOC). | |
289 (define-key map [(control c) (control f)] 'rst-goto-section) | |
290 | |
291 ;; | |
292 ;; Converting Documents from Emacs. | |
293 ;; | |
294 ;; Run one of two pre-configured toolset commands on the document. | |
295 (define-key map [(control c) (?1)] 'rst-compile) | |
296 (define-key map [(control c) (?2)] 'rst-compile-alt-toolset) | |
297 ;; Convert the active region to pseudo-xml using the docutils tools. | |
298 (define-key map [(control c) (?3)] 'rst-compile-pseudo-region) | |
299 ;; Convert the current document to PDF and launch a viewer on the results. | |
300 (define-key map [(control c) (?4)] 'rst-compile-pdf-preview) | |
301 ;; Convert the current document to S5 slides and view in a web browser. | |
302 (define-key map [(control c) (?5)] 'rst-compile-slides-preview) | |
303 | |
304 map) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
305 "Keymap for ReStructuredText mode commands. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
306 This inherits from Text mode.") |
96117 | 307 |
308 | |
309 ;; Abbrevs. | |
310 (defvar rst-mode-abbrev-table nil | |
311 "Abbrev table used while in rst mode.") | |
312 (define-abbrev-table 'rst-mode-abbrev-table | |
313 '( | |
314 ("contents" ".. contents::\n..\n " nil 0) | |
315 ("con" ".. contents::\n..\n " nil 0) | |
316 ("cont" "[...]" nil 0) | |
317 ("skip" "\n\n[...]\n\n " nil 0) | |
318 ("seq" "\n\n[...]\n\n " nil 0) | |
319 ;; FIXME: Add footnotes, links, and more. | |
320 )) | |
321 | |
322 | |
323 ;; Syntax table. | |
324 (defvar rst-mode-syntax-table | |
325 (let ((st (copy-syntax-table text-mode-syntax-table))) | |
326 | |
327 (modify-syntax-entry ?$ "." st) | |
328 (modify-syntax-entry ?% "." st) | |
329 (modify-syntax-entry ?& "." st) | |
330 (modify-syntax-entry ?' "." st) | |
331 (modify-syntax-entry ?* "." st) | |
332 (modify-syntax-entry ?+ "." st) | |
333 (modify-syntax-entry ?. "_" st) | |
334 (modify-syntax-entry ?/ "." st) | |
335 (modify-syntax-entry ?< "." st) | |
336 (modify-syntax-entry ?= "." st) | |
337 (modify-syntax-entry ?> "." st) | |
338 (modify-syntax-entry ?\\ "\\" st) | |
339 (modify-syntax-entry ?| "." st) | |
340 (modify-syntax-entry ?_ "." st) | |
341 | |
342 st) | |
343 "Syntax table used while in `rst-mode'.") | |
344 | |
345 | |
346 (defcustom rst-mode-hook nil | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
347 "Hook run when Rst Mode is turned on. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
348 The hook for Text Mode is run before this one." |
96117 | 349 :group 'rst |
350 :type '(hook)) | |
351 | |
352 | |
353 (defcustom rst-mode-lazy t | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
354 "*If non-nil Rst Mode tries to font-lock multi-line elements correctly. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
355 Because this is really slow it should be set to nil if neither `jit-lock-mode' |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
356 not `lazy-lock-mode' and activated. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
357 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
358 If nil, comments and literal blocks are font-locked only on the line they start. |
96117 | 359 |
360 The value of this variable is used when Rst Mode is turned on." | |
361 :group 'rst | |
362 :type '(boolean)) | |
363 | |
364 | |
365 ;;;###autoload | |
366 (define-derived-mode rst-mode text-mode "ReST" | |
367 :abbrev-table rst-mode-abbrev-table | |
368 :syntax-table rst-mode-syntax-table | |
369 :group 'rst | |
370 "Major mode for editing reStructuredText documents. | |
371 | |
372 There are a number of convenient keybindings provided by | |
373 rst-mode. The main one is \[rst-adjust\], it updates or rotates | |
374 the section title around point or promotes/demotes the | |
375 decorations within the region (see full details below). Use | |
376 negative prefix arg to rotate in the other direction. | |
377 \\{rst-mode-map} | |
378 | |
379 Turning on `rst-mode' calls the normal hooks `text-mode-hook' and | |
380 `rst-mode-hook'. This mode also supports font-lock highlighting. | |
381 You may customize `rst-mode-lazy' to toggle font-locking of | |
382 blocks." | |
383 | |
384 (set (make-local-variable 'paragraph-separate) paragraph-start) | |
385 (set (make-local-variable 'indent-line-function) 'indent-relative-maybe) | |
386 (set (make-local-variable 'paragraph-start) | |
387 "\f\\|>*[ \t]*$\\|>*[ \t]*[-+*] \\|>*[ \t]*[0-9#]+\\. ") | |
388 (set (make-local-variable 'adaptive-fill-mode) t) | |
389 | |
390 ;; FIXME: No need to reset this. | |
391 ;; (set (make-local-variable 'indent-line-function) 'indent-relative) | |
392 | |
393 ;; The details of the following comment setup is important because it affects | |
394 ;; auto-fill, and it is pretty common in running text to have an ellipsis | |
395 ;; ("...") which trips because of the rest comment syntax (".. "). | |
396 (set (make-local-variable 'comment-start) ".. ") | |
397 (set (make-local-variable 'comment-start-skip) "^\\.\\. ") | |
398 (set (make-local-variable 'comment-multi-line) nil) | |
399 | |
400 ;; Special variables | |
401 (make-local-variable 'rst-adornment-level-alist) | |
402 | |
403 ;; Font lock | |
404 (set (make-local-variable 'font-lock-defaults) | |
405 '(rst-font-lock-keywords-function | |
406 t nil nil nil | |
407 (font-lock-multiline . t) | |
408 (font-lock-mark-block-function . mark-paragraph))) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
409 ;; `jit-lock-mode' has been the default since Emacs-21.1, so there's no |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
410 ;; point messing around with font-lock-support-mode any more. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
411 ;; (when (boundp 'font-lock-support-mode) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
412 ;; ;; rst-mode has its own mind about font-lock-support-mode |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
413 ;; (make-local-variable 'font-lock-support-mode) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
414 ;; ;; jit-lock-mode replaced lazy-lock-mode in GNU Emacs 21. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
415 ;; (let ((jit-or-lazy-lock-mode |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
416 ;; (cond |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
417 ;; ((fboundp 'lazy-lock-mode) 'lazy-lock-mode) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
418 ;; ((fboundp 'jit-lock-mode) 'jit-lock-mode) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
419 ;; ;; if neither lazy-lock nor jit-lock is supported, |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
420 ;; ;; tell user and disable rst-mode-lazy |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
421 ;; (t (when rst-mode-lazy |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
422 ;; (message "Disabled lazy fontification, because no known support mode found.") |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
423 ;; (setq rst-mode-lazy nil)))))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
424 ;; (cond |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
425 ;; ((and (not rst-mode-lazy) (not font-lock-support-mode))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
426 ;; ;; No support mode set and none required - leave it alone |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
427 ;; ((or (not font-lock-support-mode) ;; No support mode set (but required) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
428 ;; (symbolp font-lock-support-mode)) ;; or a fixed mode for all |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
429 ;; (setq font-lock-support-mode |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
430 ;; (list (cons 'rst-mode (and rst-mode-lazy jit-or-lazy-lock-mode)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
431 ;; (cons t font-lock-support-mode)))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
432 ;; ((and (listp font-lock-support-mode) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
433 ;; (not (assoc 'rst-mode font-lock-support-mode))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
434 ;; ;; A list of modes missing rst-mode |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
435 ;; (setq font-lock-support-mode |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
436 ;; (cons (cons 'rst-mode (and rst-mode-lazy jit-or-lazy-lock-mode)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
437 ;; font-lock-support-mode)))))) |
96117 | 438 |
439 ) | |
440 | |
441 | |
442 ;;;###autoload | |
443 (define-minor-mode rst-minor-mode | |
444 "ReST Minor Mode. | |
445 Toggle ReST minor mode. | |
446 With no argument, this command toggles the mode. | |
447 Non-null prefix argument turns on the mode. | |
448 Null prefix argument turns off the mode. | |
449 | |
450 When ReST minor mode is enabled, the ReST mode | |
451 keybindings are installed on top of the major | |
452 mode bindings. Use this for modes derived from | |
453 text-mode, like mail-mode.." | |
454 ;; The initial value. | |
455 nil | |
456 ;; The indicator for the mode line. | |
457 " ReST" | |
458 ;; The minor mode bindings. | |
459 rst-mode-map | |
460 :group 'rst) | |
461 | |
462 ;; FIXME: can I somehow install these too? | |
463 ;; :abbrev-table rst-mode-abbrev-table | |
464 ;; :syntax-table rst-mode-syntax-table | |
465 | |
466 | |
467 | |
468 | |
469 | |
470 ;; Bulleted item lists. | |
471 (defcustom rst-bullets | |
472 '(?- ?* ?+) | |
473 "List of all possible bullet characters for bulleted lists." | |
474 :group 'rst) | |
475 | |
476 | |
477 | |
478 | |
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
480 ;; Section Decoration Adjusment | |
481 ;; ============================ | |
482 ;; | |
483 ;; The following functions implement a smart automatic title sectioning feature. | |
484 ;; The idea is that with the cursor sitting on a section title, we try to get as | |
485 ;; much information from context and try to do the best thing automatically. | |
486 ;; This function can be invoked many times and/or with prefix argument to rotate | |
487 ;; between the various sectioning decorations. | |
488 ;; | |
489 ;; Definitions: the two forms of sectioning define semantically separate section | |
490 ;; levels. A sectioning DECORATION consists in: | |
491 ;; | |
492 ;; - a CHARACTER | |
493 ;; | |
494 ;; - a STYLE which can be either of 'simple' or 'over-and-under'. | |
495 ;; | |
496 ;; - an INDENT (meaningful for the over-and-under style only) which determines | |
497 ;; how many characters and over-and-under style is hanging outside of the | |
498 ;; title at the beginning and ending. | |
499 ;; | |
500 ;; Important note: an existing decoration must be formed by at least two | |
501 ;; characters to be recognized. | |
502 ;; | |
503 ;; Here are two examples of decorations (| represents the window border, column | |
504 ;; 0): | |
505 ;; | |
506 ;; | | |
507 ;; 1. char: '-' e |Some Title | |
508 ;; style: simple |---------- | |
509 ;; | | |
510 ;; 2. char: '=' |============== | |
511 ;; style: over-and-under | Some Title | |
512 ;; indent: 2 |============== | |
513 ;; | | |
514 ;; | |
515 ;; Some notes: | |
516 ;; | |
517 ;; - The underlining character that is used depends on context. The file is | |
518 ;; scanned to find other sections and an appropriate character is selected. | |
519 ;; If the function is invoked on a section that is complete, the character is | |
520 ;; rotated among the existing section decorations. | |
521 ;; | |
522 ;; Note that when rotating the characters, if we come to the end of the | |
523 ;; hierarchy of decorations, the variable rst-preferred-decorations is | |
524 ;; consulted to propose a new underline decoration, and if continued, we cycle | |
525 ;; the decorations all over again. Set this variable to nil if you want to | |
526 ;; limit the underlining character propositions to the existing decorations in | |
527 ;; the file. | |
528 ;; | |
529 ;; - A prefix argument can be used to alternate the style. | |
530 ;; | |
531 ;; - An underline/overline that is not extended to the column at which it should | |
532 ;; be hanging is dubbed INCOMPLETE. For example:: | |
533 ;; | |
534 ;; |Some Title | |
535 ;; |------- | |
536 ;; | |
537 ;; Examples of default invocation: | |
538 ;; | |
539 ;; |Some Title ---> |Some Title | |
540 ;; | |---------- | |
541 ;; | |
542 ;; |Some Title ---> |Some Title | |
543 ;; |----- |---------- | |
544 ;; | |
545 ;; | |------------ | |
546 ;; | Some Title ---> | Some Title | |
547 ;; | |------------ | |
548 ;; | |
549 ;; In over-and-under style, when alternating the style, a variable is | |
550 ;; available to select how much default indent to use (it can be zero). Note | |
551 ;; that if the current section decoration already has an indent, we don't | |
552 ;; adjust it to the default, we rather use the current indent that is already | |
553 ;; there for adjustment (unless we cycle, in which case we use the indent | |
554 ;; that has been found previously). | |
555 | |
556 (defgroup rst-adjust nil | |
557 "Settings for adjustment and cycling of section title | |
558 decorations." | |
559 :group 'rst | |
560 :version "21.1") | |
561 | |
562 (defcustom rst-preferred-decorations '( (?= over-and-under 1) | |
563 (?= simple 0) | |
564 (?- simple 0) | |
565 (?~ simple 0) | |
566 (?+ simple 0) | |
567 (?` simple 0) | |
568 (?# simple 0) | |
569 (?@ simple 0) ) | |
570 "Preferred ordering of section title decorations. | |
571 | |
572 This sequence is consulted to offer a new decoration suggestion | |
573 when we rotate the underlines at the end of the existing | |
574 hierarchy of characters, or when there is no existing section | |
575 title in the file." | |
576 :group 'rst-adjust) | |
577 | |
578 | |
579 (defcustom rst-default-indent 1 | |
580 "Number of characters to indent the section title. | |
581 | |
582 THis is used for when toggling decoration styles, when switching | |
583 from a simple decoration style to a over-and-under decoration | |
584 style." | |
585 :group 'rst-adjust) | |
586 | |
587 | |
588 (defvar rst-section-text-regexp "^[ \t]*\\S-*\\w\\S-*" | |
589 "Regular expression for valid section title text.") | |
590 | |
591 | |
592 (defun rst-line-homogeneous-p (&optional accept-special) | |
593 "Return true if the line is homogeneous. | |
594 | |
595 Predicate that returns the unique char if the current line is | |
596 composed only of a single repeated non-whitespace character. This | |
597 returns the char even if there is whitespace at the beginning of | |
598 the line. | |
599 | |
600 If ACCEPT-SPECIAL is specified we do not ignore special sequences | |
601 which normally we would ignore when doing a search on many lines. | |
602 For example, normally we have cases to ignore commonly occuring | |
603 patterns, such as :: or ...; with the flag do not ignore them." | |
604 (save-excursion | |
605 (back-to-indentation) | |
606 (unless (looking-at "\n") | |
607 (let ((c (thing-at-point 'char))) | |
608 (if (and (looking-at (format "[%s]+[ \t]*$" c)) | |
609 (or accept-special | |
610 (and | |
611 ;; Common patterns. | |
612 (not (looking-at "::[ \t]*$")) | |
613 (not (looking-at "\\.\\.\\.[ \t]*$")) | |
614 ;; Discard one char line | |
615 (not (looking-at ".[ \t]*$")) | |
616 ))) | |
617 (string-to-char c)) | |
618 )) | |
619 )) | |
620 | |
621 (defun rst-line-homogeneous-nodent-p (&optional accept-special) | |
622 "Return true if the line is homogeneous with no indent. | |
623 See `rst-line-homogeneous-p' about ACCEPT-SPECIAL." | |
624 (save-excursion | |
625 (beginning-of-line) | |
626 (if (looking-at "^[ \t]+") | |
627 nil | |
628 (rst-line-homogeneous-p accept-special) | |
629 ))) | |
630 | |
631 | |
632 (defun rst-compare-decorations (deco1 deco2) | |
633 "Compare decorations. | |
634 Returns true if both DECO1 and DECO2 decorations are equal, | |
635 according to restructured text semantics (only the character and | |
636 the style are compared, the indentation does not matter." | |
637 (and (eq (car deco1) (car deco2)) | |
638 (eq (cadr deco1) (cadr deco2)))) | |
639 | |
640 | |
641 (defun rst-get-decoration-match (hier deco) | |
642 "Return the index (level) in hierarchy HIER of decoration DECO. | |
643 This basically just searches for the item using the appropriate | |
644 comparison and returns the index. We return nil if the item is | |
645 not found." | |
646 (let ((cur hier)) | |
647 (while (and cur (not (rst-compare-decorations (car cur) deco))) | |
648 (setq cur (cdr cur))) | |
649 cur)) | |
650 | |
651 | |
652 (defun rst-suggest-new-decoration (alldecos &optional prev) | |
653 "Suggest a new, different decoration from all that have been seen. | |
654 | |
655 ALLDECOS is the set of all decorations, including the line | |
656 numbers. PREV is the optional previous decoration, in order to | |
657 suggest a better match." | |
658 | |
659 ;; For all the preferred decorations... | |
660 (let* ( | |
661 ;; If 'prev' is given, reorder the list to start searching after the | |
662 ;; match. | |
663 (fplist | |
664 (cdr (rst-get-decoration-match rst-preferred-decorations prev))) | |
665 | |
666 ;; List of candidates to search. | |
667 (curpotential (append fplist rst-preferred-decorations))) | |
668 (while | |
669 ;; For all the decorations... | |
670 (let ((cur alldecos) | |
671 found) | |
672 (while (and cur (not found)) | |
673 (if (rst-compare-decorations (car cur) (car curpotential)) | |
674 ;; Found it! | |
675 (setq found (car curpotential)) | |
676 (setq cur (cdr cur)))) | |
677 found) | |
678 | |
679 (setq curpotential (cdr curpotential))) | |
680 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
681 (copy-sequence (car curpotential)))) |
96117 | 682 |
683 (defun rst-delete-entire-line () | |
684 "Delete the entire current line without using the `kill-ring'." | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
685 (delete-region (line-beginning-position) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
686 (line-beginning-position 2))) |
96117 | 687 |
688 (defun rst-update-section (char style &optional indent) | |
689 "Unconditionally update the style of a section decoration. | |
690 | |
691 Do this using the given character CHAR, with STYLE 'simple or | |
692 'over-and-under, and with indent INDENT. If the STYLE is | |
693 'simple, whitespace before the title is removed (indent is always | |
694 assume to be 0). | |
695 | |
696 If there are existing overline and/or underline from the | |
697 existing decoration, they are removed before adding the | |
698 requested decoration." | |
699 | |
700 (interactive) | |
701 (let (marker | |
702 len) | |
703 | |
704 (end-of-line) | |
705 (setq marker (point-marker)) | |
706 | |
707 ;; Fixup whitespace at the beginning and end of the line | |
708 (if (or (null indent) (eq style 'simple)) | |
709 (setq indent 0)) | |
710 (beginning-of-line) | |
711 (delete-horizontal-space) | |
712 (insert (make-string indent ? )) | |
713 | |
714 (end-of-line) | |
715 (delete-horizontal-space) | |
716 | |
717 ;; Set the current column, we're at the end of the title line | |
718 (setq len (+ (current-column) indent)) | |
719 | |
720 ;; Remove previous line if it consists only of a single repeated character | |
721 (save-excursion | |
722 (forward-line -1) | |
723 (and (rst-line-homogeneous-p 1) | |
724 ;; Avoid removing the underline of a title right above us. | |
725 (save-excursion (forward-line -1) | |
726 (not (looking-at rst-section-text-regexp))) | |
727 (rst-delete-entire-line))) | |
728 | |
729 ;; Remove following line if it consists only of a single repeated | |
730 ;; character | |
731 (save-excursion | |
732 (forward-line +1) | |
733 (and (rst-line-homogeneous-p 1) | |
734 (rst-delete-entire-line)) | |
735 ;; Add a newline if we're at the end of the buffer, for the subsequence | |
736 ;; inserting of the underline | |
737 (if (= (point) (buffer-end 1)) | |
738 (newline 1))) | |
739 | |
740 ;; Insert overline | |
741 (if (eq style 'over-and-under) | |
742 (save-excursion | |
743 (beginning-of-line) | |
744 (open-line 1) | |
745 (insert (make-string len char)))) | |
746 | |
747 ;; Insert underline | |
748 (forward-line +1) | |
749 (open-line 1) | |
750 (insert (make-string len char)) | |
751 | |
752 (forward-line +1) | |
753 (goto-char marker) | |
754 )) | |
755 | |
756 | |
757 (defun rst-normalize-cursor-position () | |
758 "Normalize the cursor position. | |
759 If the cursor is on a decoration line or an empty line , place it | |
760 on the section title line (at the end). Returns the line offset | |
761 by which the cursor was moved. This works both over or under a | |
762 line." | |
763 (if (save-excursion (beginning-of-line) | |
764 (or (rst-line-homogeneous-p 1) | |
765 (looking-at "^[ \t]*$"))) | |
766 (progn | |
767 (beginning-of-line) | |
768 (cond | |
769 ((save-excursion (forward-line -1) | |
770 (beginning-of-line) | |
771 (and (looking-at rst-section-text-regexp) | |
772 (not (rst-line-homogeneous-p 1)))) | |
773 (progn (forward-line -1) -1)) | |
774 ((save-excursion (forward-line +1) | |
775 (beginning-of-line) | |
776 (and (looking-at rst-section-text-regexp) | |
777 (not (rst-line-homogeneous-p 1)))) | |
778 (progn (forward-line +1) +1)) | |
779 (t 0))) | |
780 0 )) | |
781 | |
782 | |
783 (defun rst-find-all-decorations () | |
784 "Find all the decorations in the file. | |
785 Return a list of (line, decoration) pairs. Each decoration | |
786 consists in a (char, style, indent) triple. | |
787 | |
788 This function does not detect the hierarchy of decorations, it | |
789 just finds all of them in a file. You can then invoke another | |
790 function to remove redundancies and inconsistencies." | |
791 | |
792 (let (positions | |
793 (curline 1)) | |
794 ;; Iterate over all the section titles/decorations in the file. | |
795 (save-excursion | |
796 (goto-char (point-min)) | |
797 (while (< (point) (buffer-end 1)) | |
798 (if (rst-line-homogeneous-nodent-p) | |
799 (progn | |
800 (setq curline (+ curline (rst-normalize-cursor-position))) | |
801 | |
802 ;; Here we have found a potential site for a decoration, | |
803 ;; characterize it. | |
804 (let ((deco (rst-get-decoration))) | |
805 (if (cadr deco) ;; Style is existing. | |
806 ;; Found a real decoration site. | |
807 (progn | |
808 (push (cons curline deco) positions) | |
809 ;; Push beyond the underline. | |
810 (forward-line 1) | |
811 (setq curline (+ curline 1)) | |
812 ))) | |
813 )) | |
814 (forward-line 1) | |
815 (setq curline (+ curline 1)) | |
816 )) | |
817 (reverse positions))) | |
818 | |
819 | |
820 (defun rst-infer-hierarchy (decorations) | |
821 "Build a hierarchy of decorations using the list of given DECORATIONS. | |
822 | |
823 This function expects a list of (char, style, indent) decoration | |
824 specifications, in order that they appear in a file, and will | |
825 infer a hierarchy of section levels by removing decorations that | |
826 have already been seen in a forward traversal of the decorations, | |
827 comparing just the character and style. | |
828 | |
829 Similarly returns a list of (char, style, indent), where each | |
830 list element should be unique." | |
831 | |
832 (let ((hierarchy-alist (list))) | |
833 (dolist (x decorations) | |
834 (let ((char (car x)) | |
835 (style (cadr x))) | |
836 (unless (assoc (cons char style) hierarchy-alist) | |
837 (push (cons (cons char style) x) hierarchy-alist)) | |
838 )) | |
839 | |
840 (mapcar 'cdr (nreverse hierarchy-alist)) | |
841 )) | |
842 | |
843 | |
844 (defun rst-get-hierarchy (&optional alldecos ignore) | |
845 "Return the hierarchy of section titles in the file. | |
846 | |
847 Return a list of decorations that represents the hierarchy of | |
848 section titles in the file. Reuse the list of decorations | |
849 already computed in ALLDECOS if present. If the line number in | |
850 IGNORE is specified, the decoration found on that line (if there | |
851 is one) is not taken into account when building the hierarchy." | |
852 (let ((all (or alldecos (rst-find-all-decorations)))) | |
853 (setq all (assq-delete-all ignore all)) | |
854 (rst-infer-hierarchy (mapcar 'cdr all)))) | |
855 | |
856 | |
857 (defun rst-get-decoration (&optional point) | |
858 "Get the decoration at POINT. | |
859 | |
860 Looks around point and finds the characteristics of the | |
861 decoration that is found there. We assume that the cursor is | |
862 already placed on the title line (and not on the overline or | |
863 underline). | |
864 | |
865 This function returns a (char, style, indent) triple. If the | |
866 characters of overline and underline are different, we return | |
867 the underline character. The indent is always calculated. A | |
868 decoration can be said to exist if the style is not nil. | |
869 | |
870 A point can be specified to go to the given location before | |
871 extracting the decoration." | |
872 | |
873 (let (char style indent) | |
874 (save-excursion | |
875 (if point (goto-char point)) | |
876 (beginning-of-line) | |
877 (if (looking-at rst-section-text-regexp) | |
878 (let* ((over (save-excursion | |
879 (forward-line -1) | |
880 (rst-line-homogeneous-nodent-p))) | |
881 | |
882 (under (save-excursion | |
883 (forward-line +1) | |
884 (rst-line-homogeneous-nodent-p))) | |
885 ) | |
886 | |
887 ;; Check that the line above the overline is not part of a title | |
888 ;; above it. | |
889 (if (and over | |
890 (save-excursion | |
891 (and (equal (forward-line -2) 0) | |
892 (looking-at rst-section-text-regexp)))) | |
893 (setq over nil)) | |
894 | |
895 (cond | |
896 ;; No decoration found, leave all return values nil. | |
897 ((and (eq over nil) (eq under nil))) | |
898 | |
899 ;; Overline only, leave all return values nil. | |
900 ;; | |
901 ;; Note: we don't return the overline character, but it could | |
902 ;; perhaps in some cases be used to do something. | |
903 ((and over (eq under nil))) | |
904 | |
905 ;; Underline only. | |
906 ((and under (eq over nil)) | |
907 (setq char under | |
908 style 'simple)) | |
909 | |
910 ;; Both overline and underline. | |
911 (t | |
912 (setq char under | |
913 style 'over-and-under)) | |
914 ) | |
915 ) | |
916 ) | |
917 ;; Find indentation. | |
918 (setq indent (save-excursion (back-to-indentation) (current-column))) | |
919 ) | |
920 ;; Return values. | |
921 (list char style indent))) | |
922 | |
923 | |
924 (defun rst-get-decorations-around (&optional alldecos) | |
925 "Return the decorations around point. | |
926 | |
927 Given the list of all decorations ALLDECOS (with positions), find | |
928 the decorations before and after the given point. A list of the | |
929 previous and next decorations is returned." | |
930 (let* ((all (or alldecos (rst-find-all-decorations))) | |
931 (curline (line-number-at-pos)) | |
932 prev next | |
933 (cur all)) | |
934 | |
935 ;; Search for the decorations around the current line. | |
936 (while (and cur (< (caar cur) curline)) | |
937 (setq prev cur | |
938 cur (cdr cur))) | |
939 ;; 'cur' is the following decoration. | |
940 | |
941 (if (and cur (caar cur)) | |
942 (setq next (if (= curline (caar cur)) (cdr cur) cur))) | |
943 | |
944 (mapcar 'cdar (list prev next)) | |
945 )) | |
946 | |
947 | |
948 (defun rst-decoration-complete-p (deco) | |
949 "Return true if the decoration DECO around POINT is complete." | |
950 ;; Note: we assume that the detection of the overline as being the underline | |
951 ;; of a preceding title has already been detected, and has been eliminated | |
952 ;; from the decoration that is given to us. | |
953 | |
954 ;; There is some sectioning already present, so check if the current | |
955 ;; sectioning is complete and correct. | |
956 (let* ((char (car deco)) | |
957 (style (cadr deco)) | |
958 (indent (caddr deco)) | |
959 (endcol (save-excursion (end-of-line) (current-column))) | |
960 ) | |
961 (if char | |
962 (let ((exps (concat "^" | |
963 (regexp-quote (make-string (+ endcol indent) char)) | |
964 "$"))) | |
965 (and | |
966 (save-excursion (forward-line +1) | |
967 (beginning-of-line) | |
968 (looking-at exps)) | |
969 (or (not (eq style 'over-and-under)) | |
970 (save-excursion (forward-line -1) | |
971 (beginning-of-line) | |
972 (looking-at exps)))) | |
973 )) | |
974 )) | |
975 | |
976 | |
977 (defun rst-get-next-decoration | |
978 (curdeco hier &optional suggestion reverse-direction) | |
979 "Get the next decoration for CURDECO, in given hierarchy HIER. | |
980 If suggesting, suggest for new decoration SUGGESTION. | |
981 REVERSE-DIRECTION is used to reverse the cycling order." | |
982 | |
983 (let* ( | |
984 (char (car curdeco)) | |
985 (style (cadr curdeco)) | |
986 | |
987 ;; Build a new list of decorations for the rotation. | |
988 (rotdecos | |
989 (append hier | |
990 ;; Suggest a new decoration. | |
991 (list suggestion | |
992 ;; If nothing to suggest, use first decoration. | |
993 (car hier)))) ) | |
994 (or | |
995 ;; Search for next decoration. | |
996 (cadr | |
997 (let ((cur (if reverse-direction rotdecos | |
998 (reverse rotdecos)))) | |
999 (while (and cur | |
1000 (not (and (eq char (caar cur)) | |
1001 (eq style (cadar cur))))) | |
1002 (setq cur (cdr cur))) | |
1003 cur)) | |
1004 | |
1005 ;; If not found, take the first of all decorations. | |
1006 suggestion | |
1007 ))) | |
1008 | |
1009 | |
1010 (defun rst-adjust () | |
1011 "Auto-adjust the decoration around point. | |
1012 | |
1013 Adjust/rotate the section decoration for the section title | |
1014 around point or promote/demote the decorations inside the region, | |
1015 depending on if the region is active. This function is meant to | |
1016 be invoked possibly multiple times, and can vary its behaviour | |
1017 with a positive prefix argument (toggle style), or with a | |
1018 negative prefix argument (alternate behaviour). | |
1019 | |
1020 This function is the main focus of this module and is a bit of a | |
1021 swiss knife. It is meant as the single most essential function | |
1022 to be bound to invoke to adjust the decorations of a section | |
1023 title in restructuredtext. It tries to deal with all the | |
1024 possible cases gracefully and to do `the right thing' in all | |
1025 cases. | |
1026 | |
1027 See the documentations of `rst-adjust-decoration' and | |
1028 `rst-promote-region' for full details. | |
1029 | |
1030 Prefix Arguments | |
1031 ================ | |
1032 | |
1033 The method can take either (but not both) of | |
1034 | |
1035 a. a (non-negative) prefix argument, which means to toggle the | |
1036 decoration style. Invoke with a prefix arg for example; | |
1037 | |
1038 b. a negative numerical argument, which generally inverts the | |
1039 direction of search in the file or hierarchy. Invoke with C-- | |
1040 prefix for example." | |
1041 (interactive) | |
1042 | |
1043 (let* (;; Save our original position on the current line. | |
1044 (origpt (set-marker (make-marker) (point))) | |
1045 | |
1046 ;; Parse the positive and negative prefix arguments. | |
1047 (reverse-direction | |
1048 (and current-prefix-arg | |
1049 (< (prefix-numeric-value current-prefix-arg) 0))) | |
1050 (toggle-style | |
1051 (and current-prefix-arg (not reverse-direction)))) | |
1052 | |
1053 (if (rst-portable-mark-active-p) | |
1054 ;; Adjust decorations within region. | |
1055 (rst-promote-region current-prefix-arg) | |
1056 ;; Adjust decoration around point. | |
1057 (rst-adjust-decoration toggle-style reverse-direction)) | |
1058 | |
1059 ;; Run the hooks to run after adjusting. | |
1060 (run-hooks 'rst-adjust-hook) | |
1061 | |
1062 ;; Make sure to reset the cursor position properly after we're done. | |
1063 (goto-char origpt) | |
1064 | |
1065 )) | |
1066 | |
1067 (defvar rst-adjust-hook nil | |
1068 "Hooks to be run after running `rst-adjust'.") | |
1069 | |
1070 (defvar rst-new-decoration-down nil | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1071 "Non-nil if new decoration is added deeper. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1072 If non-nil, a new decoration being added will be initialized to |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1073 be one level down from the previous decoration. If nil, a new |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1074 decoration will be equal to the level of the previous |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1075 decoration.") |
96117 | 1076 |
1077 (defun rst-adjust-decoration (&optional toggle-style reverse-direction) | |
1078 "Adjust/rotate the section decoration for the section title around point. | |
1079 | |
1080 This function is meant to be invoked possibly multiple times, and | |
1081 can vary its behaviour with a true TOGGLE-STYLE argument, or with | |
1082 a REVERSE-DIRECTION argument. | |
1083 | |
1084 General Behaviour | |
1085 ================= | |
1086 | |
1087 The next action it takes depends on context around the point, and | |
1088 it is meant to be invoked possibly more than once to rotate among | |
1089 the various possibilities. Basically, this function deals with: | |
1090 | |
1091 - adding a decoration if the title does not have one; | |
1092 | |
1093 - adjusting the length of the underline characters to fit a | |
1094 modified title; | |
1095 | |
1096 - rotating the decoration in the set of already existing | |
1097 sectioning decorations used in the file; | |
1098 | |
1099 - switching between simple and over-and-under styles. | |
1100 | |
1101 You should normally not have to read all the following, just | |
1102 invoke the method and it will do the most obvious thing that you | |
1103 would expect. | |
1104 | |
1105 | |
1106 Decoration Definitions | |
1107 ====================== | |
1108 | |
1109 The decorations consist in | |
1110 | |
1111 1. a CHARACTER | |
1112 | |
1113 2. a STYLE which can be either of 'simple' or 'over-and-under'. | |
1114 | |
1115 3. an INDENT (meaningful for the over-and-under style only) | |
1116 which determines how many characters and over-and-under | |
1117 style is hanging outside of the title at the beginning and | |
1118 ending. | |
1119 | |
1120 See source code for mode details. | |
1121 | |
1122 | |
1123 Detailed Behaviour Description | |
1124 ============================== | |
1125 | |
1126 Here are the gory details of the algorithm (it seems quite | |
1127 complicated, but really, it does the most obvious thing in all | |
1128 the particular cases): | |
1129 | |
1130 Before applying the decoration change, the cursor is placed on | |
1131 the closest line that could contain a section title. | |
1132 | |
1133 Case 1: No Decoration | |
1134 --------------------- | |
1135 | |
1136 If the current line has no decoration around it, | |
1137 | |
1138 - search backwards for the last previous decoration, and apply | |
1139 the decoration one level lower to the current line. If there | |
1140 is no defined level below this previous decoration, we suggest | |
1141 the most appropriate of the `rst-preferred-decorations'. | |
1142 | |
1143 If REVERSE-DIRECTION is true, we simply use the previous | |
1144 decoration found directly. | |
1145 | |
1146 - if there is no decoration found in the given direction, we use | |
1147 the first of `rst-preferred-decorations'. | |
1148 | |
1149 The prefix argument forces a toggle of the prescribed decoration | |
1150 style. | |
1151 | |
1152 Case 2: Incomplete Decoration | |
1153 ----------------------------- | |
1154 | |
1155 If the current line does have an existing decoration, but the | |
1156 decoration is incomplete, that is, the underline/overline does | |
1157 not extend to exactly the end of the title line (it is either too | |
1158 short or too long), we simply extend the length of the | |
1159 underlines/overlines to fit exactly the section title. | |
1160 | |
1161 If the prefix argument is given, we toggle the style of the | |
1162 decoration as well. | |
1163 | |
1164 REVERSE-DIRECTION has no effect in this case. | |
1165 | |
1166 Case 3: Complete Existing Decoration | |
1167 ------------------------------------ | |
1168 | |
1169 If the decoration is complete (i.e. the underline (overline) | |
1170 length is already adjusted to the end of the title line), we | |
1171 search/parse the file to establish the hierarchy of all the | |
1172 decorations (making sure not to include the decoration around | |
1173 point), and we rotate the current title's decoration from within | |
1174 that list (by default, going *down* the hierarchy that is present | |
1175 in the file, i.e. to a lower section level). This is meant to be | |
1176 used potentially multiple times, until the desired decoration is | |
1177 found around the title. | |
1178 | |
1179 If we hit the boundary of the hierarchy, exactly one choice from | |
1180 the list of preferred decorations is suggested/chosen, the first | |
1181 of those decoration that has not been seen in the file yet (and | |
1182 not including the decoration around point), and the next | |
1183 invocation rolls over to the other end of the hierarchy (i.e. it | |
1184 cycles). This allows you to avoid having to set which character | |
1185 to use by always using the | |
1186 | |
1187 If REVERSE-DIRECTION is true, the effect is to change the | |
1188 direction of rotation in the hierarchy of decorations, thus | |
1189 instead going *up* the hierarchy. | |
1190 | |
1191 However, if there is a non-negative prefix argument, we do not | |
1192 rotate the decoration, but instead simply toggle the style of the | |
1193 current decoration (this should be the most common way to toggle | |
1194 the style of an existing complete decoration). | |
1195 | |
1196 | |
1197 Point Location | |
1198 ============== | |
1199 | |
1200 The invocation of this function can be carried out anywhere | |
1201 within the section title line, on an existing underline or | |
1202 overline, as well as on an empty line following a section title. | |
1203 This is meant to be as convenient as possible. | |
1204 | |
1205 | |
1206 Indented Sections | |
1207 ================= | |
1208 | |
1209 Indented section titles such as :: | |
1210 | |
1211 My Title | |
1212 -------- | |
1213 | |
1214 are illegal in restructuredtext and thus not recognized by the | |
1215 parser. This code will thus not work in a way that would support | |
1216 indented sections (it would be ambiguous anyway). | |
1217 | |
1218 | |
1219 Joint Sections | |
1220 ============== | |
1221 | |
1222 Section titles that are right next to each other may not be | |
1223 treated well. More work might be needed to support those, and | |
1224 special conditions on the completeness of existing decorations | |
1225 might be required to make it non-ambiguous. | |
1226 | |
1227 For now we assume that the decorations are disjoint, that is, | |
1228 there is at least a single line between the titles/decoration | |
1229 lines. | |
1230 | |
1231 | |
1232 Suggested Binding | |
1233 ================= | |
1234 | |
1235 We suggest that you bind this function on C-=. It is close to | |
1236 C-- so a negative argument can be easily specified with a flick | |
1237 of the right hand fingers and the binding is unused in `text-mode'." | |
1238 (interactive) | |
1239 | |
1240 ;; If we were invoked directly, parse the prefix arguments into the | |
1241 ;; arguments of the function. | |
1242 (if current-prefix-arg | |
1243 (setq reverse-direction | |
1244 (and current-prefix-arg | |
1245 (< (prefix-numeric-value current-prefix-arg) 0)) | |
1246 | |
1247 toggle-style | |
1248 (and current-prefix-arg (not reverse-direction)))) | |
1249 | |
1250 (let* (;; Check if we're on an underline around a section title, and move the | |
1251 ;; cursor to the title if this is the case. | |
1252 (moved (rst-normalize-cursor-position)) | |
1253 | |
1254 ;; Find the decoration and completeness around point. | |
1255 (curdeco (rst-get-decoration)) | |
1256 (char (car curdeco)) | |
1257 (style (cadr curdeco)) | |
1258 (indent (caddr curdeco)) | |
1259 | |
1260 ;; New values to be computed. | |
1261 char-new style-new indent-new | |
1262 ) | |
1263 | |
1264 ;; We've moved the cursor... if we're not looking at some text, we have | |
1265 ;; nothing to do. | |
1266 (if (save-excursion (beginning-of-line) | |
1267 (looking-at rst-section-text-regexp)) | |
1268 (progn | |
1269 (cond | |
1270 ;;------------------------------------------------------------------- | |
1271 ;; Case 1: No Decoration | |
1272 ((and (eq char nil) (eq style nil)) | |
1273 | |
1274 (let* ((alldecos (rst-find-all-decorations)) | |
1275 | |
1276 (around (rst-get-decorations-around alldecos)) | |
1277 (prev (car around)) | |
1278 cur | |
1279 | |
1280 (hier (rst-get-hierarchy alldecos)) | |
1281 ) | |
1282 | |
1283 ;; Advance one level down. | |
1284 (setq cur | |
1285 (if prev | |
1286 (if (not reverse-direction) | |
1287 (or (funcall (if rst-new-decoration-down 'cadr 'car) | |
1288 (rst-get-decoration-match hier prev)) | |
1289 (rst-suggest-new-decoration hier prev)) | |
1290 prev) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1291 (copy-sequence (car rst-preferred-decorations)))) |
96117 | 1292 |
1293 ;; Invert the style if requested. | |
1294 (if toggle-style | |
1295 (setcar (cdr cur) (if (eq (cadr cur) 'simple) | |
1296 'over-and-under 'simple)) ) | |
1297 | |
1298 (setq char-new (car cur) | |
1299 style-new (cadr cur) | |
1300 indent-new (caddr cur)) | |
1301 )) | |
1302 | |
1303 ;;------------------------------------------------------------------- | |
1304 ;; Case 2: Incomplete Decoration | |
1305 ((not (rst-decoration-complete-p curdeco)) | |
1306 | |
1307 ;; Invert the style if requested. | |
1308 (if toggle-style | |
1309 (setq style (if (eq style 'simple) 'over-and-under 'simple))) | |
1310 | |
1311 (setq char-new char | |
1312 style-new style | |
1313 indent-new indent)) | |
1314 | |
1315 ;;------------------------------------------------------------------- | |
1316 ;; Case 3: Complete Existing Decoration | |
1317 (t | |
1318 (if toggle-style | |
1319 | |
1320 ;; Simply switch the style of the current decoration. | |
1321 (setq char-new char | |
1322 style-new (if (eq style 'simple) 'over-and-under 'simple) | |
1323 indent-new rst-default-indent) | |
1324 | |
1325 ;; Else, we rotate, ignoring the decoration around the current | |
1326 ;; line... | |
1327 (let* ((alldecos (rst-find-all-decorations)) | |
1328 | |
1329 (hier (rst-get-hierarchy alldecos (line-number-at-pos))) | |
1330 | |
1331 ;; Suggestion, in case we need to come up with something | |
1332 ;; new | |
1333 (suggestion (rst-suggest-new-decoration | |
1334 hier | |
1335 (car (rst-get-decorations-around alldecos)))) | |
1336 | |
1337 (nextdeco (rst-get-next-decoration | |
1338 curdeco hier suggestion reverse-direction)) | |
1339 | |
1340 ) | |
1341 | |
1342 ;; Indent, if present, always overrides the prescribed indent. | |
1343 (setq char-new (car nextdeco) | |
1344 style-new (cadr nextdeco) | |
1345 indent-new (caddr nextdeco)) | |
1346 | |
1347 ))) | |
1348 ) | |
1349 | |
1350 ;; Override indent with present indent! | |
1351 (setq indent-new (if (> indent 0) indent indent-new)) | |
1352 | |
1353 (if (and char-new style-new) | |
1354 (rst-update-section char-new style-new indent-new)) | |
1355 )) | |
1356 | |
1357 | |
1358 ;; Correct the position of the cursor to more accurately reflect where it | |
1359 ;; was located when the function was invoked. | |
1360 (unless (= moved 0) | |
1361 (forward-line (- moved)) | |
1362 (end-of-line)) | |
1363 | |
1364 )) | |
1365 | |
1366 ;; Maintain an alias for compatibility. | |
1367 (defalias 'rst-adjust-section-title 'rst-adjust) | |
1368 | |
1369 | |
1370 (defun rst-promote-region (&optional demote) | |
1371 "Promote the section titles within the region. | |
1372 | |
1373 With argument DEMOTE or a prefix argument, demote the | |
1374 section titles instead. The algorithm used at the boundaries of | |
1375 the hierarchy is similar to that used by `rst-adjust-decoration'." | |
1376 (interactive) | |
1377 | |
1378 (let* ((demote (or current-prefix-arg demote)) | |
1379 (alldecos (rst-find-all-decorations)) | |
1380 (cur alldecos) | |
1381 | |
1382 (hier (rst-get-hierarchy alldecos)) | |
1383 (suggestion (rst-suggest-new-decoration hier)) | |
1384 | |
1385 (region-begin-line (line-number-at-pos (region-beginning))) | |
1386 (region-end-line (line-number-at-pos (region-end))) | |
1387 | |
1388 marker-list | |
1389 ) | |
1390 | |
1391 ;; Skip the markers that come before the region beginning | |
1392 (while (and cur (< (caar cur) region-begin-line)) | |
1393 (setq cur (cdr cur))) | |
1394 | |
1395 ;; Create a list of markers for all the decorations which are found within | |
1396 ;; the region. | |
1397 (save-excursion | |
1398 (let (m line) | |
1399 (while (and cur (< (setq line (caar cur)) region-end-line)) | |
1400 (setq m (make-marker)) | |
1401 (goto-line line) | |
1402 (push (list (set-marker m (point)) (cdar cur)) marker-list) | |
1403 (setq cur (cdr cur)) )) | |
1404 | |
1405 ;; Apply modifications. | |
1406 (let (nextdeco) | |
1407 (dolist (p marker-list) | |
1408 ;; Go to the decoration to promote. | |
1409 (goto-char (car p)) | |
1410 | |
1411 ;; Rotate the next decoration. | |
1412 (setq nextdeco (rst-get-next-decoration | |
1413 (cadr p) hier suggestion demote)) | |
1414 | |
1415 ;; Update the decoration. | |
1416 (apply 'rst-update-section nextdeco) | |
1417 | |
1418 ;; Clear marker to avoid slowing down the editing after we're done. | |
1419 (set-marker (car p) nil) | |
1420 )) | |
1421 (setq deactivate-mark nil) | |
1422 ))) | |
1423 | |
1424 | |
1425 | |
1426 (defun rst-display-decorations-hierarchy (&optional decorations) | |
1427 "Display the current file's section title decorations hierarchy. | |
1428 This function expects a list of (char, style, indent) triples in | |
1429 DECORATIONS." | |
1430 (interactive) | |
1431 | |
1432 (if (not decorations) | |
1433 (setq decorations (rst-get-hierarchy))) | |
1434 (with-output-to-temp-buffer "*rest section hierarchy*" | |
1435 (let ((level 1)) | |
1436 (with-current-buffer standard-output | |
1437 (dolist (x decorations) | |
1438 (insert (format "\nSection Level %d" level)) | |
1439 (apply 'rst-update-section x) | |
1440 (goto-char (point-max)) | |
1441 (insert "\n") | |
1442 (incf level) | |
1443 )) | |
1444 ))) | |
1445 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1446 (defun rst-position (elem list) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1447 "Return position of ELEM in LIST or nil." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1448 (let ((tail (member elem list))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1449 (if tail (- (length list) (length tail))))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1450 |
96117 | 1451 (defun rst-straighten-decorations () |
1452 "Redo all the decorations in the current buffer. | |
1453 This is done using our preferred set of decorations. This can be | |
1454 used, for example, when using somebody else's copy of a document, | |
1455 in order to adapt it to our preferred style." | |
1456 (interactive) | |
1457 (save-excursion | |
1458 (let* ((alldecos (rst-find-all-decorations)) | |
1459 (hier (rst-get-hierarchy alldecos)) | |
1460 | |
1461 ;; Get a list of pairs of (level . marker) | |
1462 (levels-and-markers (mapcar | |
1463 (lambda (deco) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1464 (cons (rst-position (cdr deco) hier) |
96117 | 1465 (let ((m (make-marker))) |
1466 (goto-line (car deco)) | |
1467 (set-marker m (point)) | |
1468 m))) | |
1469 alldecos)) | |
1470 ) | |
1471 (dolist (lm levels-and-markers) | |
1472 ;; Go to the appropriate position | |
1473 (goto-char (cdr lm)) | |
1474 | |
1475 ;; Apply the new styule | |
1476 (apply 'rst-update-section (nth (car lm) rst-preferred-decorations)) | |
1477 | |
1478 ;; Reset the market to avoid slowing down editing until it gets GC'ed | |
1479 (set-marker (cdr lm) nil) | |
1480 ) | |
1481 ))) | |
1482 | |
1483 | |
1484 | |
1485 | |
1486 (defun rst-straighten-deco-spacing () | |
1487 "Adjust the spacing before and after decorations in the entire document. | |
1488 The spacing will be set to two blank lines before the first two | |
1489 section levels, and one blank line before any of the other | |
1490 section levels." | |
1491 ;; FIXME: we need to take care of subtitle at some point. | |
1492 (interactive) | |
1493 (save-excursion | |
1494 (let* ((alldecos (rst-find-all-decorations))) | |
1495 | |
1496 ;; Work the list from the end, so that we don't have to use markers to | |
1497 ;; adjust for the changes in the document. | |
1498 (dolist (deco (nreverse alldecos)) | |
1499 ;; Go to the appropriate position. | |
1500 (goto-line (car deco)) | |
1501 (insert "@\n") | |
1502 ;; FIXME: todo, we | |
1503 ) | |
1504 ))) | |
1505 | |
1506 | |
1507 (defun rst-find-pfx-in-region (beg end pfx-re) | |
1508 "Find all the positions of prefixes in region between BEG and END. | |
1509 This is used to find bullets and enumerated list items. PFX-RE | |
1510 is a regular expression for matching the lines with items." | |
1511 (let (pfx) | |
1512 (save-excursion | |
1513 (goto-char beg) | |
1514 (while (< (point) end) | |
1515 (back-to-indentation) | |
1516 (when (and | |
1517 (looking-at pfx-re) | |
1518 (let ((pfx-col (current-column))) | |
1519 (save-excursion | |
1520 (forward-line -1) | |
1521 (back-to-indentation) | |
1522 (or (looking-at "^[ \t]*$") | |
1523 (> (current-column) pfx-col) | |
1524 (and (= (current-column) pfx-col) | |
1525 (looking-at pfx-re)))))) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1526 (push (cons (point) (current-column)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1527 pfx)) |
96117 | 1528 (forward-line 1)) ) |
1529 (nreverse pfx))) | |
1530 | |
1531 (defvar rst-re-bullets | |
1532 (format "\\([%s][ \t]\\)[^ \t]" (regexp-quote (concat rst-bullets))) | |
1533 "Regexp for finding bullets.") | |
1534 | |
1535 (defvar rst-re-enumerations | |
1536 "\\(\\(#\\|[0-9]+\\)\\.[ \t]\\)[^ \t]" | |
1537 "Regexp for finding bullets.") | |
1538 | |
1539 (defvar rst-re-items | |
1540 (format "\\(%s\\|%s\\)[^ \t]" | |
1541 (format "[%s][ \t]" (regexp-quote (concat rst-bullets))) | |
1542 "\\(#\\|[0-9]+\\)\\.[ \t]") | |
1543 "Regexp for finding bullets.") | |
1544 | |
1545 (defvar rst-preferred-bullets | |
1546 '(?- ?* ?+) | |
1547 "List of favourite bullets to set for straightening bullets.") | |
1548 | |
1549 (defun rst-straighten-bullets-region (beg end) | |
1550 "Make all the bulleted list items in the region consistent. | |
1551 The region is specified between BEG and END. You can use this | |
1552 after you have merged multiple bulleted lists to make them use | |
1553 the same/correct/consistent bullet characters. | |
1554 | |
1555 See variable `rst-preferred-bullets' for the list of bullets to | |
1556 adjust. If bullets are found on levels beyond the | |
1557 `rst-preferred-bullets' list, they are not modified." | |
1558 (interactive "r") | |
1559 | |
1560 (let ((bullets (rst-find-pfx-in-region beg end | |
1561 rst-re-bullets)) | |
1562 (levtable (make-hash-table :size 4))) | |
1563 | |
1564 ;; Create a map of levels to list of positions. | |
1565 (dolist (x bullets) | |
1566 (let ((key (cdr x))) | |
1567 (puthash key | |
1568 (append (gethash key levtable (list)) | |
1569 (list (car x))) | |
1570 levtable))) | |
1571 | |
1572 ;; Sort this map and create a new map of prefix char and list of positions. | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1573 (let ((poslist ())) ; List of (indent . positions). |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1574 (maphash (lambda (x y) (push (cons x y) poslist)) levtable) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1575 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1576 (let ((bullets rst-preferred-bullets)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1577 (dolist (x (sort poslist 'car-less-than-car)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1578 (when bullets |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1579 ;; Apply the characters. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1580 (dolist (pos (cdr x)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1581 (goto-char pos) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1582 (delete-char 1) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1583 (insert (string (car bullets)))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1584 (setq bullets (cdr bullets)))))))) |
96117 | 1585 |
1586 (defun rst-rstrip (str) | |
1587 "Strips the whitespace at the end of string STR." | |
1588 (string-match "[ \t\n]*\\'" str) | |
1589 (substring str 0 (match-beginning 0))) | |
1590 | |
1591 (defun rst-get-stripped-line () | |
1592 "Return the line at cursor, stripped from whitespace." | |
1593 (re-search-forward "\\S-.*\\S-" (line-end-position)) | |
1594 (buffer-substring-no-properties (match-beginning 0) | |
1595 (match-end 0)) ) | |
1596 | |
1597 (defun rst-section-tree (alldecos) | |
1598 "Get the hierarchical tree of section titles. | |
1599 | |
1600 Returns a hierarchical tree of the sections titles in the | |
1601 document, for decorations ALLDECOS. This can be used to generate | |
1602 a table of contents for the document. The top node will always | |
1603 be a nil node, with the top level titles as children (there may | |
1604 potentially be more than one). | |
1605 | |
1606 Each section title consists in a cons of the stripped title | |
1607 string and a marker to the section in the original text document. | |
1608 | |
1609 If there are missing section levels, the section titles are | |
1610 inserted automatically, and the title string is set to nil, and | |
1611 the marker set to the first non-nil child of itself. | |
1612 Conceptually, the nil nodes--i.e. those which have no title--are | |
1613 to be considered as being the same line as their first non-nil | |
1614 child. This has advantages later in processing the graph." | |
1615 | |
1616 (let* ((hier (rst-get-hierarchy alldecos)) | |
1617 (levels (make-hash-table :test 'equal :size 10)) | |
1618 lines) | |
1619 | |
1620 (let ((lev 0)) | |
1621 (dolist (deco hier) | |
1622 ;; Compare just the character and indent in the hash table. | |
1623 (puthash (cons (car deco) (cadr deco)) lev levels) | |
1624 (incf lev))) | |
1625 | |
1626 ;; Create a list of lines that contains (text, level, marker) for each | |
1627 ;; decoration. | |
1628 (save-excursion | |
1629 (setq lines | |
1630 (mapcar (lambda (deco) | |
1631 (goto-line (car deco)) | |
1632 (list (gethash (cons (cadr deco) (caddr deco)) levels) | |
1633 (rst-get-stripped-line) | |
1634 (let ((m (make-marker))) | |
1635 (beginning-of-line 1) | |
1636 (set-marker m (point))) | |
1637 )) | |
1638 alldecos))) | |
1639 | |
1640 (let ((lcontnr (cons nil lines))) | |
1641 (rst-section-tree-rec lcontnr -1)))) | |
1642 | |
1643 | |
1644 (defun rst-section-tree-rec (decos lev) | |
1645 "Recursive guts of the section tree construction. | |
1646 DECOS is a cons cell whose cdr is the remaining list of | |
1647 decorations, and we change it as we consume them. LEV is the | |
1648 current level of that node. This function returns a pair of the | |
1649 subtree that was built. This treats the decos list | |
1650 destructively." | |
1651 | |
1652 (let ((ndeco (cadr decos)) | |
1653 node | |
1654 children) | |
1655 | |
1656 ;; If the next decoration matches our level | |
1657 (when (and ndeco (= (car ndeco) lev)) | |
1658 ;; Pop the next decoration and create the current node with it | |
1659 (setcdr decos (cddr decos)) | |
1660 (setq node (cdr ndeco)) ) | |
1661 ;; Else we let the node title/marker be unset. | |
1662 | |
1663 ;; Build the child nodes | |
1664 (while (and (cdr decos) (> (caadr decos) lev)) | |
1665 (setq children | |
1666 (cons (rst-section-tree-rec decos (1+ lev)) | |
1667 children))) | |
1668 (setq children (reverse children)) | |
1669 | |
1670 ;; If node is still unset, we use the marker of the first child. | |
1671 (when (eq node nil) | |
1672 (setq node (cons nil (cdaar children)))) | |
1673 | |
1674 ;; Return this node with its children. | |
1675 (cons node children) | |
1676 )) | |
1677 | |
1678 | |
1679 (defun rst-section-tree-point (node &optional point) | |
1680 "Find tree node at point. | |
1681 Given a computed and valid section tree in NODE and a point | |
1682 POINT (default being the current point in the current buffer), | |
1683 find and return the node within the sectree where the cursor | |
1684 lives. | |
1685 | |
1686 Return values: a pair of (parent path, container subtree). The | |
1687 parent path is simply a list of the nodes above the container | |
1688 subtree node that we're returning." | |
1689 | |
1690 (let (path outtree) | |
1691 | |
1692 (let* ((curpoint (or point (point)))) | |
1693 | |
1694 ;; Check if we are before the current node. | |
1695 (if (and (cadar node) (>= curpoint (cadar node))) | |
1696 | |
1697 ;; Iterate all the children, looking for one that might contain the | |
1698 ;; current section. | |
1699 (let ((curnode (cdr node)) | |
1700 last) | |
1701 | |
1702 (while (and curnode (>= curpoint (cadaar curnode))) | |
1703 (setq last curnode | |
1704 curnode (cdr curnode))) | |
1705 | |
1706 (if last | |
1707 (let ((sub (rst-section-tree-point (car last) curpoint))) | |
1708 (setq path (car sub) | |
1709 outtree (cdr sub))) | |
1710 (setq outtree node)) | |
1711 | |
1712 ))) | |
1713 (cons (cons (car node) path) outtree) | |
1714 )) | |
1715 | |
1716 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1717 (defgroup rst-toc nil |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1718 "Settings for reStructuredText table of contents." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1719 :group 'rst |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1720 :version "21.1") |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1721 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1722 (defcustom rst-toc-indent 2 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1723 "Indentation for table-of-contents display. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1724 Also used for formatting insertion, when numbering is disabled." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1725 :group 'rst-toc) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1726 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1727 (defcustom rst-toc-insert-style 'fixed |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1728 "Insertion style for table-of-contents. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1729 Set this to one of the following values to determine numbering and |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1730 indentation style: |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1731 - plain: no numbering (fixed indentation) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1732 - fixed: numbering, but fixed indentation |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1733 - aligned: numbering, titles aligned under each other |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1734 - listed: numbering, with dashes like list items (EXPERIMENTAL)" |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1735 :group 'rst-toc) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1736 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1737 (defcustom rst-toc-insert-number-separator " " |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1738 "Separator that goes between the TOC number and the title." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1739 :group 'rst-toc) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1740 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1741 ;; This is used to avoid having to change the user's mode. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1742 (defvar rst-toc-insert-click-keymap |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1743 (let ((map (make-sparse-keymap))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1744 (define-key map [mouse-1] 'rst-toc-mode-mouse-goto) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1745 map) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1746 "(Internal) What happens when you click on propertized text in the TOC.") |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1747 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1748 (defcustom rst-toc-insert-max-level nil |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1749 "If non-nil, maximum depth of the inserted TOC." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1750 :group 'rst-toc) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1751 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1752 |
96117 | 1753 (defun rst-toc-insert (&optional pfxarg) |
1754 "Insert a simple text rendering of the table of contents. | |
1755 By default the top level is ignored if there is only one, because | |
1756 we assume that the document will have a single title. | |
1757 | |
1758 If a numeric prefix argument PFXARG is given, insert the TOC up | |
1759 to the specified level. | |
1760 | |
1761 The TOC is inserted indented at the current column." | |
1762 | |
1763 (interactive "P") | |
1764 | |
1765 (let* (;; Check maximum level override | |
1766 (rst-toc-insert-max-level | |
1767 (if (and (integerp pfxarg) (> (prefix-numeric-value pfxarg) 0)) | |
1768 (prefix-numeric-value pfxarg) rst-toc-insert-max-level)) | |
1769 | |
1770 ;; Get the section tree for the current cursor point. | |
1771 (sectree-pair | |
1772 (rst-section-tree-point | |
1773 (rst-section-tree (rst-find-all-decorations)))) | |
1774 | |
1775 ;; Figure out initial indent. | |
1776 (initial-indent (make-string (current-column) ? )) | |
1777 (init-point (point))) | |
1778 | |
1779 (when (cddr sectree-pair) | |
1780 (rst-toc-insert-node (cdr sectree-pair) 0 initial-indent "") | |
1781 | |
1782 ;; Fixup for the first line. | |
1783 (delete-region init-point (+ init-point (length initial-indent))) | |
1784 | |
1785 ;; Delete the last newline added. | |
1786 (delete-backward-char 1) | |
1787 ))) | |
1788 | |
1789 (defun rst-toc-insert-node (node level indent pfx) | |
1790 "Insert tree node NODE in table-of-contents. | |
1791 Recursive function that does printing of the inserted toc. LEVEL | |
1792 is the depth level of the sections in the tree. INDENT bis the | |
1793 indentation string. PFX is the prefix numbering, that includes | |
1794 the alignment necessary for all the children of level to | |
1795 align." | |
1796 | |
1797 ;; Note: we do child numbering from the parent, so we start number the | |
1798 ;; children one level before we print them. | |
1799 (let ((do-print (> level 0)) | |
1800 (count 1)) | |
1801 (when do-print | |
1802 (insert indent) | |
1803 (let ((b (point))) | |
1804 (unless (equal rst-toc-insert-style 'plain) | |
1805 (insert pfx rst-toc-insert-number-separator)) | |
1806 (insert (or (caar node) "[missing node]")) | |
1807 ;; Add properties to the text, even though in normal text mode it | |
1808 ;; won't be doing anything for now. Not sure that I want to change | |
1809 ;; mode stuff. At least the highlighting gives the idea that this | |
1810 ;; is generated automatically. | |
1811 (put-text-property b (point) 'mouse-face 'highlight) | |
1812 (put-text-property b (point) 'rst-toc-target (cadar node)) | |
1813 (put-text-property b (point) 'keymap rst-toc-insert-click-keymap) | |
1814 | |
1815 ) | |
1816 (insert "\n") | |
1817 | |
1818 ;; Prepare indent for children. | |
1819 (setq indent | |
1820 (cond | |
1821 ((eq rst-toc-insert-style 'plain) | |
1822 (concat indent (make-string rst-toc-indent ? ))) | |
1823 | |
1824 ((eq rst-toc-insert-style 'fixed) | |
1825 (concat indent (make-string rst-toc-indent ? ))) | |
1826 | |
1827 ((eq rst-toc-insert-style 'aligned) | |
1828 (concat indent (make-string (+ (length pfx) 2) ? ))) | |
1829 | |
1830 ((eq rst-toc-insert-style 'listed) | |
1831 (concat (substring indent 0 -3) | |
1832 (concat (make-string (+ (length pfx) 2) ? ) " - "))) | |
1833 )) | |
1834 ) | |
1835 | |
1836 (if (or (eq rst-toc-insert-max-level nil) | |
1837 (< level rst-toc-insert-max-level)) | |
1838 (let ((do-child-numbering (>= level 0)) | |
1839 fmt) | |
1840 (if do-child-numbering | |
1841 (progn | |
1842 ;; Add a separating dot if there is already a prefix | |
1843 (if (> (length pfx) 0) | |
1844 (setq pfx (concat (rst-rstrip pfx) "."))) | |
1845 | |
1846 ;; Calculate the amount of space that the prefix will require | |
1847 ;; for the numbers. | |
1848 (if (cdr node) | |
1849 (setq fmt (format "%%-%dd" | |
1850 (1+ (floor (log10 (length | |
1851 (cdr node)))))))) | |
1852 )) | |
1853 | |
1854 (dolist (child (cdr node)) | |
1855 (rst-toc-insert-node child | |
1856 (1+ level) | |
1857 indent | |
1858 (if do-child-numbering | |
1859 (concat pfx (format fmt count)) pfx)) | |
1860 (incf count))) | |
1861 | |
1862 ))) | |
1863 | |
1864 | |
1865 (defun rst-toc-insert-find-delete-contents () | |
1866 "Find and deletes an existing comment after the first contents directive. | |
1867 Delete that region. Return t if found and the cursor is left after the comment." | |
1868 (goto-char (point-min)) | |
1869 ;; We look for the following and the following only (in other words, if your | |
1870 ;; syntax differs, this won't work. If you would like a more flexible thing, | |
1871 ;; contact the author, I just can't imagine that this requirement is | |
1872 ;; unreasonable for now). | |
1873 ;; | |
1874 ;; .. contents:: [...anything here...] | |
1875 ;; .. | |
1876 ;; XXXXXXXX | |
1877 ;; XXXXXXXX | |
1878 ;; [more lines] | |
1879 ;; | |
1880 (let ((beg | |
1881 (re-search-forward "^\\.\\. contents[ \t]*::\\(.*\\)\n\\.\\." | |
1882 nil t)) | |
1883 last-real) | |
1884 (when beg | |
1885 ;; Look for the first line that starts at the first column. | |
1886 (forward-line 1) | |
1887 (beginning-of-line) | |
1888 (while (and | |
1889 (< (point) (point-max)) | |
1890 (or (and (looking-at "[ \t]+[^ \t]") (setq last-real (point)) t) | |
1891 (looking-at "[ \t]*$"))) | |
1892 (forward-line 1) | |
1893 ) | |
1894 (if last-real | |
1895 (progn | |
1896 (goto-char last-real) | |
1897 (end-of-line) | |
1898 (delete-region beg (point))) | |
1899 (goto-char beg)) | |
1900 t | |
1901 ))) | |
1902 | |
1903 (defun rst-toc-update () | |
1904 "Automatically find the contents section of a document and update. | |
1905 Updates the inserted TOC if present. You can use this in your | |
1906 file-write hook to always make it up-to-date automatically." | |
1907 (interactive) | |
1908 (let ((p (point))) | |
1909 (save-excursion | |
1910 (when (rst-toc-insert-find-delete-contents) | |
1911 (insert "\n ") | |
1912 (rst-toc-insert) | |
1913 )) | |
1914 ;; Somehow save-excursion does not really work well. | |
1915 (goto-char p)) | |
1916 ;; Note: always return nil, because this may be used as a hook. | |
1917 ) | |
1918 | |
1919 ;; Note: we cannot bind the TOC update on file write because it messes with | |
1920 ;; undo. If we disable undo, since it adds and removes characters, the | |
1921 ;; positions in the undo list are not making sense anymore. Dunno what to do | |
1922 ;; with this, it would be nice to update when saving. | |
1923 ;; | |
1924 ;; (add-hook 'write-contents-hooks 'rst-toc-update-fun) | |
1925 ;; (defun rst-toc-update-fun () | |
1926 ;; ;; Disable undo for the write file hook. | |
1927 ;; (let ((buffer-undo-list t)) (rst-toc-update) )) | |
1928 | |
1929 (defalias 'rst-toc-insert-update 'rst-toc-update) ;; backwards compat. | |
1930 | |
1931 ;;------------------------------------------------------------------------------ | |
1932 | |
1933 (defun rst-toc-node (node level) | |
1934 "Recursive function that does insert NODE at LEVEL in the table-of-contents." | |
1935 | |
1936 (if (> level 0) | |
1937 (let ((b (point))) | |
1938 ;; Insert line text. | |
1939 (insert (make-string (* rst-toc-indent (1- level)) ? )) | |
1940 (insert (or (caar node) "[missing node]")) | |
1941 | |
1942 ;; Highlight lines. | |
1943 (put-text-property b (point) 'mouse-face 'highlight) | |
1944 | |
1945 ;; Add link on lines. | |
1946 (put-text-property b (point) 'rst-toc-target (cadar node)) | |
1947 | |
1948 (insert "\n") | |
1949 )) | |
1950 | |
1951 (dolist (child (cdr node)) | |
1952 (rst-toc-node child (1+ level)))) | |
1953 | |
1954 (defun rst-toc-count-lines (node target-node) | |
1955 "Count the number of lines from NODE to the TARGET-NODE node. | |
1956 This recursive function returns a cons of the number of | |
1957 additional lines that have been counted for its node and children | |
1958 and 't if the node has been found." | |
1959 | |
1960 (let ((count 1) | |
1961 found) | |
1962 (if (eq node target-node) | |
1963 (setq found t) | |
1964 (let ((child (cdr node))) | |
1965 (while (and child (not found)) | |
1966 (let ((cl (rst-toc-count-lines (car child) target-node))) | |
1967 (setq count (+ count (car cl)) | |
1968 found (cdr cl) | |
1969 child (cdr child)))))) | |
1970 (cons count found))) | |
1971 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1972 (defvar rst-toc-buffer-name "*Table of Contents*" |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1973 "Name of the Table of Contents buffer.") |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1974 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1975 (defvar rst-toc-return-buffer nil |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1976 "Buffer to which to return when leaving the TOC.") |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
1977 |
96117 | 1978 |
1979 (defun rst-toc () | |
1980 "Display a table-of-contents. | |
1981 Finds all the section titles and their decorations in the | |
1982 file, and displays a hierarchically-organized list of the | |
1983 titles, which is essentially a table-of-contents of the | |
1984 document. | |
1985 | |
1986 The Emacs buffer can be navigated, and selecting a section | |
1987 brings the cursor in that section." | |
1988 (interactive) | |
1989 (let* ((curbuf (current-buffer)) | |
1990 | |
1991 ;; Get the section tree | |
1992 (alldecos (rst-find-all-decorations)) | |
1993 (sectree (rst-section-tree alldecos)) | |
1994 | |
1995 (our-node (cdr (rst-section-tree-point sectree))) | |
1996 line | |
1997 | |
1998 ;; Create a temporary buffer. | |
1999 (buf (get-buffer-create rst-toc-buffer-name)) | |
2000 ) | |
2001 | |
2002 (with-current-buffer buf | |
2003 (let ((inhibit-read-only t)) | |
2004 (rst-toc-mode) | |
2005 (delete-region (point-min) (point-max)) | |
2006 (insert (format "Table of Contents: %s\n" (or (caar sectree) ""))) | |
2007 (put-text-property (point-min) (point) | |
2008 'face (list '(background-color . "gray"))) | |
2009 (rst-toc-node sectree 0) | |
2010 | |
2011 ;; Count the lines to our found node. | |
2012 (let ((linefound (rst-toc-count-lines sectree our-node))) | |
2013 (setq line (if (cdr linefound) (car linefound) 0))) | |
2014 )) | |
2015 (display-buffer buf) | |
2016 (pop-to-buffer buf) | |
2017 | |
2018 ;; Save the buffer to return to. | |
2019 (set (make-local-variable 'rst-toc-return-buffer) curbuf) | |
2020 | |
2021 ;; Move the cursor near the right section in the TOC. | |
2022 (goto-line line) | |
2023 )) | |
2024 | |
2025 | |
2026 (defun rst-toc-mode-find-section () | |
2027 "Get the section from text property at point." | |
2028 (let ((pos (get-text-property (point) 'rst-toc-target))) | |
2029 (unless pos | |
2030 (error "No section on this line")) | |
2031 (unless (buffer-live-p (marker-buffer pos)) | |
2032 (error "Buffer for this section was killed")) | |
2033 pos)) | |
2034 | |
2035 (defun rst-goto-section (&optional kill) | |
2036 "Go to the section the current line describes." | |
2037 (interactive) | |
2038 (let ((pos (rst-toc-mode-find-section))) | |
2039 (when kill | |
2040 (kill-buffer (get-buffer rst-toc-buffer-name))) | |
2041 (pop-to-buffer (marker-buffer pos)) | |
2042 (goto-char pos) | |
2043 ;; FIXME: make the recentering conditional on scroll. | |
2044 (recenter 5))) | |
2045 | |
2046 (defun rst-toc-mode-goto-section () | |
2047 "Go to the section the current line describes and kill the toc buffer." | |
2048 (interactive) | |
2049 (rst-goto-section t)) | |
2050 | |
2051 (defun rst-toc-mode-mouse-goto (event) | |
2052 "In `rst-toc' mode, go to the occurrence whose line you click on. | |
2053 EVENT is the input event." | |
2054 (interactive "e") | |
2055 (let (pos) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2056 (with-current-buffer (window-buffer (posn-window (event-end event))) |
96117 | 2057 (save-excursion |
2058 (goto-char (posn-point (event-end event))) | |
2059 (setq pos (rst-toc-mode-find-section)))) | |
2060 (pop-to-buffer (marker-buffer pos)) | |
2061 (goto-char pos) | |
2062 (recenter 5))) | |
2063 | |
2064 (defun rst-toc-mode-mouse-goto-kill (event) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2065 "Same as `rst-toc-mode-mouse-goto', but kill TOC buffer as well." |
96117 | 2066 (interactive "e") |
2067 (call-interactively 'rst-toc-mode-mouse-goto event) | |
2068 (kill-buffer (get-buffer rst-toc-buffer-name))) | |
2069 | |
2070 (defun rst-toc-quit-window () | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2071 "Leave the current TOC buffer." |
96117 | 2072 (interactive) |
2073 (quit-window) | |
2074 (pop-to-buffer rst-toc-return-buffer)) | |
2075 | |
2076 (defvar rst-toc-mode-map | |
2077 (let ((map (make-sparse-keymap))) | |
2078 (define-key map [mouse-1] 'rst-toc-mode-mouse-goto-kill) | |
2079 (define-key map [mouse-2] 'rst-toc-mode-mouse-goto) | |
2080 (define-key map "\C-m" 'rst-toc-mode-goto-section) | |
2081 (define-key map "f" 'rst-toc-mode-goto-section) | |
2082 (define-key map "q" 'rst-toc-quit-window) | |
2083 (define-key map "z" 'kill-this-buffer) | |
2084 map) | |
2085 "Keymap for `rst-toc-mode'.") | |
2086 | |
2087 (put 'rst-toc-mode 'mode-class 'special) | |
2088 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2089 ;; Could inherit from the new `special-mode'. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2090 (define-derived-mode rst-toc-mode nil "ReST-TOC" |
96117 | 2091 "Major mode for output from \\[rst-toc], the table-of-contents for the document." |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2092 (setq buffer-read-only t)) |
96117 | 2093 |
2094 ;; Note: use occur-mode (replace.el) as a good example to complete missing | |
2095 ;; features. | |
2096 | |
2097 | |
2098 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2099 ;; | |
2100 ;; Section movement commands. | |
2101 ;; | |
2102 | |
2103 (defun rst-forward-section (&optional offset) | |
2104 "Skip to the next restructured text section title. | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2105 OFFSET specifies how many titles to skip. Use a negative OFFSET to move |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2106 backwards in the file (default is to use 1)." |
96117 | 2107 (interactive) |
2108 (let* (;; Default value for offset. | |
2109 (offset (or offset 1)) | |
2110 | |
2111 ;; Get all the decorations in the file, with their line numbers. | |
2112 (alldecos (rst-find-all-decorations)) | |
2113 | |
2114 ;; Get the current line. | |
2115 (curline (line-number-at-pos)) | |
2116 | |
2117 (cur alldecos) | |
2118 (idx 0) | |
2119 ) | |
2120 | |
2121 ;; Find the index of the "next" decoration w.r.t. to the current line. | |
2122 (while (and cur (< (caar cur) curline)) | |
2123 (setq cur (cdr cur)) | |
2124 (incf idx)) | |
2125 ;; 'cur' is the decoration on or following the current line. | |
2126 | |
2127 (if (and (> offset 0) cur (= (caar cur) curline)) | |
2128 (incf idx)) | |
2129 | |
2130 ;; Find the final index. | |
2131 (setq idx (+ idx (if (> offset 0) (- offset 1) offset))) | |
2132 (setq cur (nth idx alldecos)) | |
2133 | |
2134 ;; If the index is positive, goto the line, otherwise go to the buffer | |
2135 ;; boundaries. | |
2136 (if (and cur (>= idx 0)) | |
2137 (goto-line (car cur)) | |
2138 (if (> offset 0) (goto-char (point-max)) (goto-char (point-min)))) | |
2139 )) | |
2140 | |
2141 (defun rst-backward-section () | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2142 "Like `rst-forward-section', except move back one title. |
96117 | 2143 With a prefix argument, move backward by a page." |
2144 (interactive) | |
2145 (rst-forward-section -1)) | |
2146 | |
2147 (defun rst-mark-section (&optional arg allow-extend) | |
2148 "Select the section that point is currently in." | |
2149 ;; Cloned from mark-paragraph. | |
2150 (interactive "p\np") | |
2151 (unless arg (setq arg 1)) | |
2152 (when (zerop arg) | |
2153 (error "Cannot mark zero sections")) | |
2154 (cond ((and allow-extend | |
2155 (or (and (eq last-command this-command) (mark t)) | |
2156 (rst-portable-mark-active-p))) | |
2157 (set-mark | |
2158 (save-excursion | |
2159 (goto-char (mark)) | |
2160 (rst-forward-section arg) | |
2161 (point)))) | |
2162 (t | |
2163 (rst-forward-section arg) | |
2164 (push-mark nil t t) | |
2165 (rst-forward-section (- arg))))) | |
2166 | |
2167 | |
2168 | |
2169 | |
2170 | |
2171 | |
2172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2173 ;; Functions to work on item lists (e.g. indent/dedent, enumerate), which are | |
2174 ;; always 2 or 3 characters apart horizontally with rest. | |
2175 | |
2176 ;; (FIXME: there is currently a bug that makes the region go away when we do that.) | |
2177 (defvar rst-shift-fill-region nil | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2178 "If non-nil, automatically re-fill the region that is being shifted.") |
96117 | 2179 |
2180 (defun rst-find-leftmost-column (beg end) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2181 "Find the leftmost column in the region." |
96117 | 2182 (let ((mincol 1000)) |
2183 (save-excursion | |
2184 (goto-char beg) | |
2185 (while (< (point) end) | |
2186 (back-to-indentation) | |
2187 (unless (looking-at "[ \t]*$") | |
2188 (setq mincol (min mincol (current-column)))) | |
2189 (forward-line 1) | |
2190 )) | |
2191 mincol)) | |
2192 | |
2193 | |
2194 ;; What we really need to do is compute all the possible alignment possibilities | |
2195 ;; and then select one. | |
2196 ;; | |
2197 ;; .. line-block:: | |
2198 ;; | |
2199 ;; a) sdjsds | |
2200 ;; | |
2201 ;; - sdjsd jsjds | |
2202 ;; | |
2203 ;; sdsdsjdsj | |
2204 ;; | |
2205 ;; 11. sjdss jddjs | |
2206 ;; | |
2207 ;; * * * * * * * | |
2208 ;; | |
2209 ;; Move backwards, accumulate the beginning positions, and also the second | |
2210 ;; positions, in case the line matches the bullet pattern, and then sort. | |
2211 | |
2212 (defun rst-compute-bullet-tabs (&optional pt) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2213 "Build the list of possible horizontal alignment points. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2214 Search backwards from point (or point PT if specified) to |
96117 | 2215 build the list of possible horizontal alignment points that |
2216 includes the beginning and contents of a restructuredtext | |
2217 bulleted or enumerated list item. Return a sorted list | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2218 of (COLUMN-NUMBER . LINE) pairs." |
96117 | 2219 (save-excursion |
2220 (when pt (goto-char pt)) | |
2221 | |
2222 ;; We work our way backwards and towards the left. | |
2223 (let ((leftcol 100000) ;; Current column. | |
2224 (tablist nil) ;; List of tab positions. | |
2225 ) | |
2226 | |
2227 ;; Start by skipping the current line. | |
2228 (beginning-of-line 0) | |
2229 | |
2230 ;; Search backwards for each line. | |
2231 (while (and (> (point) (point-min)) | |
2232 (> leftcol 0)) | |
2233 | |
2234 ;; Skip empty lines. | |
2235 (unless (looking-at "^[ \t]*$") | |
2236 ;; Inspect the current non-empty line | |
2237 (back-to-indentation) | |
2238 | |
2239 ;; Skip lines that are beyond the current column (we want to move | |
2240 ;; towards the left). | |
2241 (let ((col (current-column))) | |
2242 (when (< col leftcol) | |
2243 | |
2244 ;; Add the beginning of the line as a tabbing point. | |
2245 (unless (memq col (mapcar 'car tablist)) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2246 (push (cons col (point)) tablist)) |
96117 | 2247 |
2248 ;; Look at the line to figure out if it is a bulleted or enumerate | |
2249 ;; list item. | |
2250 (when (looking-at | |
2251 (concat | |
2252 "\\(?:" | |
2253 "\\(\\(?:[0-9a-zA-Z#]\\{1,3\\}[.):-]\\|[*+-]\\)[ \t]+\\)[^ \t\n]" | |
2254 "\\|" | |
2255 (format "\\(%s%s+[ \t]+\\)[^ \t\n]" | |
2256 (regexp-quote (thing-at-point 'char)) | |
2257 (regexp-quote (thing-at-point 'char))) | |
2258 "\\)" | |
2259 )) | |
2260 ;; Add the column of the contained item. | |
2261 (let* ((matchlen (length (or (match-string 1) (match-string 2)))) | |
2262 (newcol (+ col matchlen))) | |
2263 (unless (or (>= newcol leftcol) | |
2264 (memq (+ col matchlen) (mapcar 'car tablist))) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2265 (push (cons (+ col matchlen) (+ (point) matchlen)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2266 tablist))) |
96117 | 2267 ) |
2268 | |
2269 (setq leftcol col) | |
2270 ))) | |
2271 | |
2272 ;; Move backwards one line. | |
2273 (beginning-of-line 0)) | |
2274 | |
2275 (sort tablist (lambda (x y) (<= (car x) (car y)))) | |
2276 ))) | |
2277 | |
2278 (defun rst-debug-print-tabs (tablist) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2279 "Insert a line and place special characters at the tab points in TABLIST." |
96117 | 2280 (beginning-of-line) |
2281 (insert (concat "\n" (make-string 1000 ? ) "\n")) | |
2282 (beginning-of-line 0) | |
2283 (dolist (col tablist) | |
2284 (beginning-of-line) | |
2285 (forward-char (car col)) | |
2286 (delete-char 1) | |
2287 (insert "@") | |
2288 )) | |
2289 | |
2290 (defun rst-debug-mark-found (tablist) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2291 "Insert a line and place special characters at the tab points in TABLIST." |
96117 | 2292 (dolist (col tablist) |
2293 (when (cdr col) | |
2294 (goto-char (cdr col)) | |
2295 (insert "@")))) | |
2296 | |
2297 | |
2298 (defvar rst-shift-basic-offset 2 | |
2299 "Basic horizontal shift distance when there is no preceding alignment tabs.") | |
2300 | |
2301 (defun rst-shift-region-guts (find-next-fun offset-fun) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2302 "(See `rst-shift-region-right' for a description)." |
96117 | 2303 (let* ((mbeg (set-marker (make-marker) (region-beginning))) |
2304 (mend (set-marker (make-marker) (region-end))) | |
2305 (tabs (rst-compute-bullet-tabs mbeg)) | |
2306 (leftmostcol (rst-find-leftmost-column (region-beginning) (region-end))) | |
2307 ) | |
2308 ;; Add basic offset tabs at the end of the list. This is a better | |
2309 ;; implementation technique than hysteresis and a basic offset because it | |
2310 ;; insures that movement in both directions is consistently using the same | |
2311 ;; column positions. This makes it more predictable. | |
2312 (setq tabs | |
2313 (append tabs | |
2314 (mapcar (lambda (x) (cons x nil)) | |
2315 (let ((maxcol 120) | |
2316 (max-lisp-eval-depth 2000)) | |
2317 (flet ((addnum (x) | |
2318 (if (> x maxcol) | |
2319 nil | |
2320 (cons x (addnum | |
2321 (+ x rst-shift-basic-offset)))))) | |
2322 (addnum (or (caar (last tabs)) 0)))) | |
2323 ))) | |
2324 | |
2325 ;; (For debugging.) | |
2326 ;;; (save-excursion (goto-char mbeg) (forward-char -1) (rst-debug-print-tabs tabs)))) | |
2327 ;;; (print tabs) | |
2328 ;;; (save-excursion (rst-debug-mark-found tabs)) | |
2329 | |
2330 ;; Apply the indent. | |
2331 (indent-rigidly | |
2332 mbeg mend | |
2333 | |
2334 ;; Find the next tab after the leftmost columnt. | |
2335 (let ((tab (funcall find-next-fun tabs leftmostcol))) | |
2336 | |
2337 (if tab | |
2338 (progn | |
2339 (when (cdar tab) | |
2340 (message "Aligned on '%s'" | |
2341 (save-excursion | |
2342 (goto-char (cdar tab)) | |
2343 (buffer-substring-no-properties | |
2344 (line-beginning-position) | |
2345 (line-end-position)))) | |
2346 ) | |
2347 (- (caar tab) leftmostcol)) ;; Num chars. | |
2348 | |
2349 ;; Otherwise use the basic offset | |
2350 (funcall offset-fun rst-shift-basic-offset) | |
2351 ))) | |
2352 | |
2353 ;; Optionally reindent. | |
2354 (when rst-shift-fill-region | |
2355 (fill-region mbeg mend)) | |
2356 )) | |
2357 | |
2358 (defun rst-shift-region-right (pfxarg) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2359 "Indent region ridigly, by a few characters to the right. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2360 This function first computes all possible alignment columns by |
96117 | 2361 inspecting the lines preceding the region for bulleted or |
2362 enumerated list items. If the leftmost column is beyond the | |
2363 preceding lines, the region is moved to the right by | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2364 `rst-shift-basic-offset'. With a prefix argument, do not |
96117 | 2365 automatically fill the region." |
2366 (interactive "P") | |
2367 (let ((rst-shift-fill-region | |
2368 (if (not pfxarg) rst-shift-fill-region))) | |
2369 (rst-shift-region-guts (lambda (tabs leftmostcol) | |
2370 (let ((cur tabs)) | |
2371 (while (and cur (<= (caar cur) leftmostcol)) | |
2372 (setq cur (cdr cur))) | |
2373 cur)) | |
2374 'identity | |
2375 ))) | |
2376 | |
2377 (defun rst-shift-region-left (pfxarg) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2378 "Like `rst-shift-region-right', except we move to the left. |
96117 | 2379 Also, if invoked with a negative prefix arg, the entire |
2380 indentation is removed, up to the leftmost character in the | |
2381 region, and automatic filling is disabled." | |
2382 (interactive "P") | |
2383 (let ((mbeg (set-marker (make-marker) (region-beginning))) | |
2384 (mend (set-marker (make-marker) (region-end))) | |
2385 (leftmostcol (rst-find-leftmost-column | |
2386 (region-beginning) (region-end))) | |
2387 (rst-shift-fill-region | |
2388 (if (not pfxarg) rst-shift-fill-region))) | |
2389 | |
2390 (when (> leftmostcol 0) | |
2391 (if (and pfxarg (< (prefix-numeric-value pfxarg) 0)) | |
2392 (progn | |
2393 (indent-rigidly (region-beginning) (region-end) (- leftmostcol)) | |
2394 (when rst-shift-fill-region | |
2395 (fill-region mbeg mend)) | |
2396 ) | |
2397 (rst-shift-region-guts (lambda (tabs leftmostcol) | |
2398 (let ((cur (reverse tabs))) | |
2399 (while (and cur (>= (caar cur) leftmostcol)) | |
2400 (setq cur (cdr cur))) | |
2401 cur)) | |
2402 '- | |
2403 )) | |
2404 ))) | |
2405 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2406 (defmacro rst-iterate-leftmost-paragraphs |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2407 (beg end first-only body-consequent body-alternative) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2408 "FIXME This definition is old and deprecated / we need to move |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2409 to the newer version below: |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2410 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2411 Call FUN at the beginning of each line, with an argument that |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2412 specifies whether we are at the first line of a paragraph that |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2413 starts at the leftmost column of the given region BEG and END. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2414 Set FIRST-ONLY to true if you want to callback on the first line |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2415 of each paragraph only." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2416 `(save-excursion |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2417 (let ((leftcol (rst-find-leftmost-column ,beg ,end)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2418 (endm (set-marker (make-marker) ,end)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2419 ) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2420 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2421 (do* (;; Iterate lines |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2422 (l (progn (goto-char ,beg) (back-to-indentation)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2423 (progn (forward-line 1) (back-to-indentation))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2424 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2425 (previous nil valid) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2426 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2427 (curcol (current-column) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2428 (current-column)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2429 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2430 (valid (and (= curcol leftcol) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2431 (not (looking-at "[ \t]*$"))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2432 (and (= curcol leftcol) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2433 (not (looking-at "[ \t]*$")))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2434 ) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2435 ((>= (point) endm)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2436 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2437 (if (if ,first-only |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2438 (and valid (not previous)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2439 valid) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2440 ,body-consequent |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2441 ,body-alternative) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2442 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2443 )))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2444 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2445 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2446 (defmacro rst-iterate-leftmost-paragraphs-2 (spec &rest body) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2447 "Evaluate BODY for each line in region defined by BEG END. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2448 LEFTMOST is set to true if the line is one of the leftmost of the |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2449 entire paragraph. PARABEGIN is set to true if the line is the |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2450 first of a paragraph." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2451 (declare (indent 1) (debug (sexp body))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2452 (destructuring-bind |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2453 (beg end parabegin leftmost isleftmost isempty) spec |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2454 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2455 `(save-excursion |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2456 (let ((,leftmost (rst-find-leftmost-column ,beg ,end)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2457 (endm (set-marker (make-marker) ,end)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2458 ) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2459 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2460 (do* (;; Iterate lines |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2461 (l (progn (goto-char ,beg) (back-to-indentation)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2462 (progn (forward-line 1) (back-to-indentation))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2463 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2464 (empty-line-previous nil ,isempty) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2465 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2466 (,isempty (looking-at "[ \t]*$") |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2467 (looking-at "[ \t]*$")) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2468 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2469 (,parabegin (not ,isempty) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2470 (and empty-line-previous |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2471 (not ,isempty))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2472 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2473 (,isleftmost (and (not ,isempty) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2474 (= (current-column) ,leftmost)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2475 (and (not ,isempty) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2476 (= (current-column) ,leftmost))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2477 ) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2478 ((>= (point) endm)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2479 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2480 (progn ,@body) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2481 |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2482 ))))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2483 |
96117 | 2484 |
2485 ;;------------------------------------------------------------------------------ | |
2486 | |
2487 ;; FIXME: these next functions should become part of a larger effort to redo the | |
2488 ;; bullets in bulletted lists. The enumerate would just be one of the possible | |
2489 ;; outputs. | |
2490 ;; | |
2491 ;; FIXME: TODO we need to do the enumeration removal as well. | |
2492 | |
2493 (defun rst-enumerate-region (beg end) | |
2494 "Add enumeration to all the leftmost paragraphs in the given region. | |
2495 The region is specified between BEG and END. With prefix argument, | |
2496 do all lines instead of just paragraphs." | |
2497 (interactive "r") | |
2498 (let ((count 0) | |
2499 (last-insert-len nil)) | |
2500 (rst-iterate-leftmost-paragraphs | |
2501 beg end (not current-prefix-arg) | |
2502 (let ((ins-string (format "%d. " (incf count)))) | |
2503 (setq last-insert-len (length ins-string)) | |
2504 (insert ins-string)) | |
2505 (insert (make-string last-insert-len ?\ )) | |
2506 ))) | |
2507 | |
2508 (defun rst-bullet-list-region (beg end) | |
2509 "Add bullets to all the leftmost paragraphs in the given region. | |
2510 The region is specified between BEG and END. With prefix argument, | |
2511 do all lines instead of just paragraphs." | |
2512 (interactive "r") | |
2513 (rst-iterate-leftmost-paragraphs | |
2514 beg end (not current-prefix-arg) | |
2515 (insert "- ") | |
2516 (insert " ") | |
2517 )) | |
2518 | |
2519 | |
2520 ;; FIXME: there are some problems left with the following function | |
2521 ;; implementation: | |
2522 ;; | |
2523 ;; * It does not deal with a varying number of digits appropriately | |
2524 ;; * It does not deal with multiple levels independently, and it should. | |
2525 ;; | |
2526 ;; I suppose it does 90% of the job for now. | |
2527 | |
2528 (defun rst-convert-bullets-to-enumeration (beg end) | |
2529 "Convert all the bulleted items and enumerated items in the | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2530 region to enumerated lists, renumbering as necessary." |
96117 | 2531 (interactive "r") |
2532 (let* (;; Find items and convert the positions to markers. | |
2533 (items (mapcar | |
2534 (lambda (x) | |
2535 (cons (let ((m (make-marker))) | |
2536 (set-marker m (car x)) | |
2537 m) | |
2538 (cdr x))) | |
2539 (rst-find-pfx-in-region beg end rst-re-items))) | |
2540 (count 1) | |
2541 ) | |
2542 (save-excursion | |
2543 (dolist (x items) | |
2544 (goto-char (car x)) | |
2545 (looking-at rst-re-items) | |
2546 (replace-match (format "%d. " count) nil nil nil 1) | |
2547 (incf count) | |
2548 )) | |
2549 )) | |
2550 | |
2551 | |
2552 | |
2553 ;;------------------------------------------------------------------------------ | |
2554 | |
2555 (defun rst-line-block-region (rbeg rend &optional pfxarg) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2556 "Toggle line block prefixes for a region. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2557 With prefix argument set the empty lines too." |
96117 | 2558 (interactive "r\nP") |
2559 (let ((comment-start "| ") | |
2560 (comment-end "") | |
2561 (comment-start-skip "| ") | |
2562 (comment-style 'indent) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2563 (force (not (not pfxarg)))) |
96117 | 2564 (rst-iterate-leftmost-paragraphs-2 |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2565 (rbeg rend parbegin leftmost isleft isempty) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2566 (when (or force (not isempty)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2567 (move-to-column leftmost force) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2568 (delete-region (point) (+ (point) (- (current-indentation) leftmost))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2569 (insert "| "))))) |
96117 | 2570 |
2571 | |
2572 | |
2573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2574 | |
2575 (require 'font-lock) | |
2576 | |
2577 (defgroup rst-faces nil "Faces used in Rst Mode" | |
2578 :group 'rst | |
2579 :group 'faces | |
2580 :version "21.1") | |
2581 | |
2582 (defcustom rst-block-face 'font-lock-keyword-face | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2583 "All syntax marking up a special block." |
96117 | 2584 :group 'rst-faces |
2585 :type '(face)) | |
2586 | |
2587 (defcustom rst-external-face 'font-lock-type-face | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2588 "Field names and interpreted text." |
96117 | 2589 :group 'rst-faces |
2590 :type '(face)) | |
2591 | |
2592 (defcustom rst-definition-face 'font-lock-function-name-face | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2593 "All other defining constructs." |
96117 | 2594 :group 'rst-faces |
2595 :type '(face)) | |
2596 | |
2597 (defcustom rst-directive-face | |
2598 ;; XEmacs compatibility | |
2599 (if (boundp 'font-lock-builtin-face) | |
2600 'font-lock-builtin-face | |
2601 'font-lock-preprocessor-face) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2602 "Directives and roles." |
96117 | 2603 :group 'rst-faces |
2604 :type '(face)) | |
2605 | |
2606 (defcustom rst-comment-face 'font-lock-comment-face | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2607 "Comments." |
96117 | 2608 :group 'rst-faces |
2609 :type '(face)) | |
2610 | |
2611 (defcustom rst-emphasis1-face | |
2612 ;; XEmacs compatibility | |
2613 (if (facep 'italic) | |
2614 ''italic | |
2615 'italic) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2616 "Simple emphasis." |
96117 | 2617 :group 'rst-faces |
2618 :type '(face)) | |
2619 | |
2620 (defcustom rst-emphasis2-face | |
2621 ;; XEmacs compatibility | |
2622 (if (facep 'bold) | |
2623 ''bold | |
2624 'bold) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2625 "Double emphasis." |
96117 | 2626 :group 'rst-faces |
2627 :type '(face)) | |
2628 | |
2629 (defcustom rst-literal-face 'font-lock-string-face | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2630 "Literal text." |
96117 | 2631 :group 'rst-faces |
2632 :type '(face)) | |
2633 | |
2634 (defcustom rst-reference-face 'font-lock-variable-name-face | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2635 "References to a definition." |
96117 | 2636 :group 'rst-faces |
2637 :type '(face)) | |
2638 | |
2639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2640 | |
2641 (defgroup rst-faces-defaults nil | |
2642 "Values used to generate default faces for section titles on all levels. | |
2643 Tweak these if you are content with how section title faces are built in | |
2644 general but you do not like the details." | |
2645 :group 'rst-faces | |
2646 :version "21.1") | |
2647 | |
2648 (defun rst-set-level-default (sym val) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2649 "Set custom var SYM affecting section title text face and recompute the faces." |
96117 | 2650 (custom-set-default sym val) |
2651 ;; Also defines the faces initially when all values are available | |
2652 (and (boundp 'rst-level-face-max) | |
2653 (boundp 'rst-level-face-format-light) | |
2654 (boundp 'rst-level-face-base-color) | |
2655 (boundp 'rst-level-face-step-light) | |
2656 (boundp 'rst-level-face-base-light) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2657 (fboundp 'rst-define-level-faces) |
96117 | 2658 (rst-define-level-faces))) |
2659 | |
2660 ;; Faces for displaying items on several levels; these definitions define | |
2661 ;; different shades of grey where the lightest one (i.e. least contrasting) is | |
2662 ;; used for level 1 | |
2663 (defcustom rst-level-face-max 6 | |
2664 "Maximum depth of levels for which section title faces are defined." | |
2665 :group 'rst-faces-defaults | |
2666 :type '(integer) | |
2667 :set 'rst-set-level-default) | |
2668 (defcustom rst-level-face-base-color "grey" | |
2669 "The base name of the color to be used for creating background colors in | |
2670 ection title faces for all levels." | |
2671 :group 'rst-faces-defaults | |
2672 :type '(string) | |
2673 :set 'rst-set-level-default) | |
2674 (defcustom rst-level-face-base-light | |
2675 (if (eq frame-background-mode 'dark) | |
2676 15 | |
2677 85) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2678 "The lightness factor for the base color. This value is used for level 1. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2679 The default depends on whether the value of `frame-background-mode' is |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2680 `dark' or not." |
96117 | 2681 :group 'rst-faces-defaults |
2682 :type '(integer) | |
2683 :set 'rst-set-level-default) | |
2684 (defcustom rst-level-face-format-light "%2d" | |
2685 "The format for the lightness factor appended to the base name of the color. | |
2686 This value is expanded by `format' with an integer." | |
2687 :group 'rst-faces-defaults | |
2688 :type '(string) | |
2689 :set 'rst-set-level-default) | |
2690 (defcustom rst-level-face-step-light | |
2691 (if (eq frame-background-mode 'dark) | |
2692 7 | |
2693 -7) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2694 "The step width to use for the next color. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2695 The formula |
96117 | 2696 |
2697 `rst-level-face-base-light' | |
2698 + (`rst-level-face-max' - 1) * `rst-level-face-step-light' | |
2699 | |
2700 must result in a color level which appended to `rst-level-face-base-color' | |
2701 using `rst-level-face-format-light' results in a valid color such as `grey50'. | |
2702 This color is used as background for section title text on level | |
2703 `rst-level-face-max'." | |
2704 :group 'rst-faces-defaults | |
2705 :type '(integer) | |
2706 :set 'rst-set-level-default) | |
2707 | |
2708 (defcustom rst-adornment-faces-alist | |
2709 (let ((alist '((t . font-lock-keyword-face) | |
2710 (nil . font-lock-keyword-face))) | |
2711 (i 1)) | |
2712 (while (<= i rst-level-face-max) | |
2713 (nconc alist (list (cons i (intern (format "rst-level-%d-face" i))))) | |
2714 (setq i (1+ i))) | |
2715 alist) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2716 "Faces for the various adornment types. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2717 Key is a number (for the section title text of that level), |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2718 t (for transitions) or nil (for section title adornment). |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2719 If you generally do not like how section title text faces are |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2720 set up tweak here. If the general idea is ok for you but you do not like the |
96117 | 2721 details check the Rst Faces Defaults group." |
2722 :group 'rst-faces | |
2723 :type '(alist | |
2724 :key-type | |
2725 (choice | |
2726 (integer | |
2727 :tag | |
2728 "Section level (may not be bigger than `rst-level-face-max')") | |
2729 (boolean :tag "transitions (on) / section title adornment (off)")) | |
2730 :value-type (face)) | |
2731 :set-after '(rst-level-face-max)) | |
2732 | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2733 (defun rst-define-level-faces () |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2734 "Define the faces for the section title text faces from the values." |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2735 ;; All variables used here must be checked in `rst-set-level-default' |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2736 (let ((i 1)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2737 (while (<= i rst-level-face-max) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2738 (let ((sym (intern (format "rst-level-%d-face" i))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2739 (doc (format "Face for showing section title text at level %d" i)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2740 (col (format (concat "%s" rst-level-face-format-light) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2741 rst-level-face-base-color |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2742 (+ (* (1- i) rst-level-face-step-light) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2743 rst-level-face-base-light)))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2744 (make-empty-face sym) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2745 (set-face-doc-string sym doc) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2746 (set-face-background sym col) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2747 (set sym sym) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2748 (setq i (1+ i)))))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2749 |
96117 | 2750 |
2751 | |
2752 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2753 ;; Font lock | |
2754 | |
2755 (defvar rst-use-char-classes | |
2756 (string-match "[[:alpha:]]" "b") | |
2757 "Non-nil if we can use the character classes in our regexps.") | |
2758 | |
2759 (defun rst-font-lock-keywords-function () | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2760 "Return keywords to highlight in rst mode according to current settings." |
96117 | 2761 ;; The reST-links in the comments below all relate to sections in |
2762 ;; http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html | |
2763 (let* ( ;; This gets big - so let's define some abbreviations | |
2764 ;; horizontal white space | |
2765 (re-hws "[\t ]") | |
2766 ;; beginning of line with possible indentation | |
2767 (re-bol (concat "^" re-hws "*")) | |
2768 ;; Separates block lead-ins from their content | |
2769 (re-blksep1 (concat "\\(" re-hws "+\\|$\\)")) | |
2770 ;; explicit markup tag | |
2771 (re-emt "\\.\\.") | |
2772 ;; explicit markup start | |
2773 (re-ems (concat re-emt re-hws "+")) | |
2774 ;; inline markup prefix | |
2775 (re-imp1 (concat "\\(^\\|" re-hws "\\|[-'\"([{</:]\\)")) | |
2776 ;; inline markup suffix | |
2777 (re-ims1 (concat "\\(" re-hws "\\|[]-'\")}>/:.,;!?\\]\\|$\\)")) | |
2778 ;; symbol character | |
2779 (re-sym1 "\\(\\sw\\|\\s_\\)") | |
2780 ;; inline markup content begin | |
2781 (re-imbeg2 "\\(\\S \\|\\S \\([^") | |
2782 | |
2783 ;; There seems to be a bug leading to error "Stack overflow in regexp | |
2784 ;; matcher" when "|" or "\\*" are the characters searched for | |
2785 (re-imendbeg | |
2786 (if (< emacs-major-version 21) | |
2787 "]" | |
2788 "\\]\\|\\\\.")) | |
2789 ;; inline markup content end | |
2790 (re-imend (concat re-imendbeg "\\)*[^\t \\\\]\\)")) | |
2791 ;; inline markup content without asterisk | |
2792 (re-ima2 (concat re-imbeg2 "*" re-imend)) | |
2793 ;; inline markup content without backquote | |
2794 (re-imb2 (concat re-imbeg2 "`" re-imend)) | |
2795 ;; inline markup content without vertical bar | |
2796 (re-imv2 (concat re-imbeg2 "|" re-imend)) | |
2797 ;; Supported URI schemes | |
2798 (re-uris1 "\\(acap\\|cid\\|data\\|dav\\|fax\\|file\\|ftp\\|gopher\\|http\\|https\\|imap\\|ldap\\|mailto\\|mid\\|modem\\|news\\|nfs\\|nntp\\|pop\\|prospero\\|rtsp\\|service\\|sip\\|tel\\|telnet\\|tip\\|urn\\|vemmi\\|wais\\)") | |
2799 ;; Line starting with adornment and optional whitespace; complete | |
2800 ;; adornment is in (match-string 1); there must be at least 3 | |
2801 ;; characters because otherwise explicit markup start would be | |
2802 ;; recognized | |
2803 (re-ado2 (concat "^\\(\\([" | |
2804 (if rst-use-char-classes | |
2805 "^[:word:][:space:][:cntrl:]" "^\\w \t\x00-\x1F") | |
2806 "]\\)\\2\\2+\\)" re-hws "*$")) | |
2807 ) | |
2808 (list | |
2809 ;; FIXME: Block markup is not recognized in blocks after explicit markup | |
2810 ;; start | |
2811 | |
2812 ;; Simple `Body Elements`_ | |
2813 ;; `Bullet Lists`_ | |
2814 (list | |
2815 (concat re-bol "\\([-*+]" re-blksep1 "\\)") | |
2816 1 rst-block-face) | |
2817 ;; `Enumerated Lists`_ | |
2818 (list | |
2819 (concat re-bol "\\((?\\(#\\|[0-9]+\\|[A-Za-z]\\|[IVXLCMivxlcm]+\\)[.)]" | |
2820 re-blksep1 "\\)") | |
2821 1 rst-block-face) | |
2822 ;; `Definition Lists`_ FIXME: missing | |
2823 ;; `Field Lists`_ | |
2824 (list | |
2825 (concat re-bol "\\(:[^:\n]+:\\)" re-blksep1) | |
2826 1 rst-external-face) | |
2827 ;; `Option Lists`_ | |
2828 (list | |
2829 (concat re-bol "\\(\\(\\(\\([-+/]\\|--\\)\\sw\\(-\\|\\sw\\)*" | |
2830 "\\([ =]\\S +\\)?\\)\\(,[\t ]\\)?\\)+\\)\\($\\|[\t ]\\{2\\}\\)") | |
2831 1 rst-block-face) | |
2832 | |
2833 ;; `Tables`_ FIXME: missing | |
2834 | |
2835 ;; All the `Explicit Markup Blocks`_ | |
2836 ;; `Footnotes`_ / `Citations`_ | |
2837 (list | |
2838 (concat re-bol "\\(" re-ems "\\[[^[\n]+\\]\\)" re-blksep1) | |
2839 1 rst-definition-face) | |
2840 ;; `Directives`_ / `Substitution Definitions`_ | |
2841 (list | |
2842 (concat re-bol "\\(" re-ems "\\)\\(\\(|[^|\n]+|[\t ]+\\)?\\)\\(" | |
2843 re-sym1 "+::\\)" re-blksep1) | |
2844 (list 1 rst-directive-face) | |
2845 (list 2 rst-definition-face) | |
2846 (list 4 rst-directive-face)) | |
2847 ;; `Hyperlink Targets`_ | |
2848 (list | |
2849 (concat re-bol "\\(" re-ems "_\\([^:\\`\n]\\|\\\\.\\|`[^`\n]+`\\)+:\\)" | |
2850 re-blksep1) | |
2851 1 rst-definition-face) | |
2852 (list | |
2853 (concat re-bol "\\(__\\)" re-blksep1) | |
2854 1 rst-definition-face) | |
2855 | |
2856 ;; All `Inline Markup`_ | |
2857 ;; FIXME: Condition 5 preventing fontification of e.g. "*" not implemented | |
2858 ;; `Strong Emphasis`_ | |
2859 (list | |
2860 (concat re-imp1 "\\(\\*\\*" re-ima2 "\\*\\*\\)" re-ims1) | |
2861 2 rst-emphasis2-face) | |
2862 ;; `Emphasis`_ | |
2863 (list | |
2864 (concat re-imp1 "\\(\\*" re-ima2 "\\*\\)" re-ims1) | |
2865 2 rst-emphasis1-face) | |
2866 ;; `Inline Literals`_ | |
2867 (list | |
2868 (concat re-imp1 "\\(``" re-imb2 "``\\)" re-ims1) | |
2869 2 rst-literal-face) | |
2870 ;; `Inline Internal Targets`_ | |
2871 (list | |
2872 (concat re-imp1 "\\(_`" re-imb2 "`\\)" re-ims1) | |
2873 2 rst-definition-face) | |
2874 ;; `Hyperlink References`_ | |
2875 ;; FIXME: `Embedded URIs`_ not considered | |
2876 (list | |
2877 (concat re-imp1 "\\(\\(`" re-imb2 "`\\|\\(\\sw\\(\\sw\\|-\\)+\\sw\\)\\)__?\\)" re-ims1) | |
2878 2 rst-reference-face) | |
2879 ;; `Interpreted Text`_ | |
2880 (list | |
2881 (concat re-imp1 "\\(\\(:" re-sym1 "+:\\)?\\)\\(`" re-imb2 "`\\)\\(\\(:" | |
2882 re-sym1 "+:\\)?\\)" re-ims1) | |
2883 (list 2 rst-directive-face) | |
2884 (list 5 rst-external-face) | |
2885 (list 8 rst-directive-face)) | |
2886 ;; `Footnote References`_ / `Citation References`_ | |
2887 (list | |
2888 (concat re-imp1 "\\(\\[[^]]+\\]_\\)" re-ims1) | |
2889 2 rst-reference-face) | |
2890 ;; `Substitution References`_ | |
2891 (list | |
2892 (concat re-imp1 "\\(|" re-imv2 "|\\)" re-ims1) | |
2893 2 rst-reference-face) | |
2894 ;; `Standalone Hyperlinks`_ | |
2895 (list | |
2896 ;; FIXME: This takes it easy by using a whitespace as delimiter | |
2897 (concat re-imp1 "\\(" re-uris1 ":\\S +\\)" re-ims1) | |
2898 2 rst-definition-face) | |
2899 (list | |
2900 (concat re-imp1 "\\(" re-sym1 "+@" re-sym1 "+\\)" re-ims1) | |
2901 2 rst-definition-face) | |
2902 | |
2903 ;; Do all block fontification as late as possible so 'append works | |
2904 | |
2905 ;; Sections_ / Transitions_ | |
2906 (append | |
2907 (list | |
2908 re-ado2) | |
2909 (if (not rst-mode-lazy) | |
2910 (list 1 rst-block-face) | |
2911 (list | |
2912 (list 'rst-font-lock-handle-adornment | |
2913 '(progn | |
2914 (setq rst-font-lock-adornment-point (match-end 1)) | |
2915 (point-max)) | |
2916 nil | |
2917 (list 1 '(cdr (assoc nil rst-adornment-faces-alist)) | |
2918 'append t) | |
2919 (list 2 '(cdr (assoc rst-font-lock-level | |
2920 rst-adornment-faces-alist)) | |
2921 'append t) | |
2922 (list 3 '(cdr (assoc nil rst-adornment-faces-alist)) | |
2923 'append t))))) | |
2924 | |
2925 ;; `Comments`_ | |
2926 (append | |
2927 (list | |
2928 (concat re-bol "\\(" re-ems "\\)\[^[|_]\\([^:\n]\\|:\\([^:\n]\\|$\\)\\)*$") | |
2929 | |
2930 (list 1 rst-comment-face)) | |
2931 (if rst-mode-lazy | |
2932 (list | |
2933 (list 'rst-font-lock-find-unindented-line | |
2934 '(progn | |
2935 (setq rst-font-lock-indentation-point (match-end 1)) | |
2936 (point-max)) | |
2937 nil | |
2938 (list 0 rst-comment-face 'append))))) | |
2939 (append | |
2940 (list | |
2941 (concat re-bol "\\(" re-emt "\\)\\(\\s *\\)$") | |
2942 (list 1 rst-comment-face) | |
2943 (list 2 rst-comment-face)) | |
2944 (if rst-mode-lazy | |
2945 (list | |
2946 (list 'rst-font-lock-find-unindented-line | |
2947 '(progn | |
2948 (setq rst-font-lock-indentation-point 'next) | |
2949 (point-max)) | |
2950 nil | |
2951 (list 0 rst-comment-face 'append))))) | |
2952 | |
2953 ;; `Literal Blocks`_ | |
2954 (append | |
2955 (list | |
2956 (concat re-bol "\\(\\([^.\n]\\|\\.[^.\n]\\).*\\)?\\(::\\)$") | |
2957 (list 3 rst-block-face)) | |
2958 (if rst-mode-lazy | |
2959 (list | |
2960 (list 'rst-font-lock-find-unindented-line | |
2961 '(progn | |
2962 (setq rst-font-lock-indentation-point t) | |
2963 (point-max)) | |
2964 nil | |
2965 (list 0 rst-literal-face 'append))))) | |
2966 | |
2967 ;; `Doctest Blocks`_ | |
2968 (append | |
2969 (list | |
2970 (concat re-bol "\\(>>>\\|\\.\\.\\.\\)\\(.+\\)") | |
2971 (list 1 rst-block-face) | |
2972 (list 2 rst-literal-face))) | |
2973 ))) | |
2974 | |
2975 | |
2976 | |
2977 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2978 ;; Indented blocks | |
2979 | |
2980 (defun rst-forward-indented-block (&optional column limit) | |
2981 "Move forward across one indented block. | |
2982 Find the next non-empty line which is not indented at least to COLUMN (defaults | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2983 to the column of the point). Moves point to first character of this line or the |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
2984 first empty line immediately before it and returns that position. If there is |
96117 | 2985 no such line before LIMIT (defaults to the end of the buffer) returns nil and |
2986 point is not moved." | |
2987 (interactive) | |
2988 (let ((clm (or column (current-column))) | |
2989 (start (point)) | |
2990 fnd beg cand) | |
2991 (if (not limit) | |
2992 (setq limit (point-max))) | |
2993 (save-match-data | |
2994 (while (and (not fnd) (< (point) limit)) | |
2995 (forward-line 1) | |
2996 (when (< (point) limit) | |
2997 (setq beg (point)) | |
2998 (if (looking-at "\\s *$") | |
2999 (setq cand (or cand beg)) ; An empty line is a candidate | |
3000 (move-to-column clm) | |
3001 ;; FIXME: No indentation [(zerop clm)] must be handled in some | |
3002 ;; useful way - though it is not clear what this should mean at all | |
3003 (if (string-match | |
3004 "^\\s *$" (buffer-substring-no-properties beg (point))) | |
3005 (setq cand nil) ; An indented line resets a candidate | |
3006 (setq fnd (or cand beg))))))) | |
3007 (goto-char (or fnd start)) | |
3008 fnd)) | |
3009 | |
3010 ;; Stores the point where the current indentation ends if a number. If `next' | |
3011 ;; indicates `rst-font-lock-find-unindented-line' shall take the indentation | |
3012 ;; from the next line if this is not empty. If non-nil indicates | |
3013 ;; `rst-font-lock-find-unindented-line' shall take the indentation from the | |
3014 ;; next non-empty line. Also used as a trigger for | |
3015 ;; `rst-font-lock-find-unindented-line'. | |
3016 (defvar rst-font-lock-indentation-point nil) | |
3017 | |
3018 (defun rst-font-lock-find-unindented-line (limit) | |
3019 (let* ((ind-pnt rst-font-lock-indentation-point) | |
3020 (beg-pnt ind-pnt)) | |
3021 ;; May run only once - enforce this | |
3022 (setq rst-font-lock-indentation-point nil) | |
3023 (when (and ind-pnt (not (numberp ind-pnt))) | |
3024 ;; Find indentation point in next line if any | |
3025 (setq ind-pnt | |
3026 (save-excursion | |
3027 (save-match-data | |
3028 (if (eq ind-pnt 'next) | |
3029 (when (and (zerop (forward-line 1)) (< (point) limit)) | |
3030 (setq beg-pnt (point)) | |
3031 (when (not (looking-at "\\s *$")) | |
3032 (looking-at "\\s *") | |
3033 (match-end 0))) | |
3034 (while (and (zerop (forward-line 1)) (< (point) limit) | |
3035 (looking-at "\\s *$"))) | |
3036 (when (< (point) limit) | |
3037 (setq beg-pnt (point)) | |
3038 (looking-at "\\s *") | |
3039 (match-end 0))))))) | |
3040 (when ind-pnt | |
3041 (goto-char ind-pnt) | |
3042 ;; Always succeeds because the limit set by PRE-MATCH-FORM is the | |
3043 ;; ultimate point to find | |
3044 (goto-char (or (rst-forward-indented-block nil limit) limit)) | |
3045 (set-match-data (list beg-pnt (point))) | |
3046 t))) | |
3047 | |
3048 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3049 ;; Adornments | |
3050 | |
3051 ;; Stores the point where the current adornment ends. Also used as a trigger | |
3052 ;; for `rst-font-lock-handle-adornment'. | |
3053 (defvar rst-font-lock-adornment-point nil) | |
3054 | |
3055 ;; Here `rst-font-lock-handle-adornment' stores the section level of the | |
3056 ;; current adornment or t for a transition. | |
3057 (defvar rst-font-lock-level nil) | |
3058 | |
3059 ;; FIXME: It would be good if this could be used to markup section titles of | |
3060 ;; given level with a special key; it would be even better to be able to | |
3061 ;; customize this so it can be used for a generally available personal style | |
3062 ;; | |
3063 ;; FIXME: There should be some way to reset and reload this variable - probably | |
3064 ;; a special key | |
3065 ;; | |
3066 ;; FIXME: Some support for `outline-mode' would be nice which should be based | |
3067 ;; on this information | |
3068 (defvar rst-adornment-level-alist nil | |
3069 "Associates adornments with section levels. | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3070 The key is a two character string. The first character is the adornment |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3071 character. The second character distinguishes underline section titles (`u') |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3072 from overline/underline section titles (`o'). The value is the section level. |
96117 | 3073 |
3074 This is made buffer local on start and adornments found during font lock are | |
3075 entered.") | |
3076 | |
3077 ;; Returns section level for adornment key KEY. Adds new section level if KEY | |
3078 ;; is not found and ADD. If KEY is not a string it is simply returned. | |
3079 (defun rst-adornment-level (key &optional add) | |
3080 (let ((fnd (assoc key rst-adornment-level-alist)) | |
3081 (new 1)) | |
3082 (cond | |
3083 ((not (stringp key)) | |
3084 key) | |
3085 (fnd | |
3086 (cdr fnd)) | |
3087 (add | |
3088 (while (rassoc new rst-adornment-level-alist) | |
3089 (setq new (1+ new))) | |
3090 (setq rst-adornment-level-alist | |
3091 (append rst-adornment-level-alist (list (cons key new)))) | |
3092 new)))) | |
3093 | |
3094 ;; Classifies adornment for section titles and transitions. ADORNMENT is the | |
3095 ;; complete adornment string as found in the buffer. END is the point after the | |
3096 ;; last character of ADORNMENT. For overline section adornment LIMIT limits the | |
3097 ;; search for the matching underline. Returns a list. The first entry is t for | |
3098 ;; a transition, or a key string for `rst-adornment-level' for a section title. | |
3099 ;; The following eight values forming four match groups as can be used for | |
3100 ;; `set-match-data'. First match group contains the maximum points of the whole | |
3101 ;; construct. Second and last match group matched pure section title adornment | |
3102 ;; while third match group matched the section title text or the transition. | |
3103 ;; Each group but the first may or may not exist. | |
3104 (defun rst-classify-adornment (adornment end limit) | |
3105 (save-excursion | |
3106 (save-match-data | |
3107 (goto-char end) | |
3108 (let ((ado-ch (aref adornment 0)) | |
3109 (ado-re (regexp-quote adornment)) | |
3110 (end-pnt (point)) | |
3111 (beg-pnt (progn | |
3112 (forward-line 0) | |
3113 (point))) | |
3114 (nxt-emp | |
3115 (save-excursion | |
3116 (or (not (zerop (forward-line 1))) | |
3117 (looking-at "\\s *$")))) | |
3118 (prv-emp | |
3119 (save-excursion | |
3120 (or (not (zerop (forward-line -1))) | |
3121 (looking-at "\\s *$")))) | |
3122 key beg-ovr end-ovr beg-txt end-txt beg-und end-und) | |
3123 (cond | |
3124 ((and nxt-emp prv-emp) | |
3125 ;; A transition | |
3126 (setq key t) | |
3127 (setq beg-txt beg-pnt) | |
3128 (setq end-txt end-pnt)) | |
3129 (prv-emp | |
3130 ;; An overline | |
3131 (setq key (concat (list ado-ch) "o")) | |
3132 (setq beg-ovr beg-pnt) | |
3133 (setq end-ovr end-pnt) | |
3134 (forward-line 1) | |
3135 (setq beg-txt (point)) | |
3136 (while (and (< (point) limit) (not end-txt)) | |
3137 (if (looking-at "\\s *$") | |
3138 ;; No underline found | |
3139 (setq end-txt (1- (point))) | |
3140 (when (looking-at (concat "\\(" ado-re "\\)\\s *$")) | |
3141 (setq end-und (match-end 1)) | |
3142 (setq beg-und (point)) | |
3143 (setq end-txt (1- beg-und)))) | |
3144 (forward-line 1))) | |
3145 (t | |
3146 ;; An underline | |
3147 (setq key (concat (list ado-ch) "u")) | |
3148 (setq beg-und beg-pnt) | |
3149 (setq end-und end-pnt) | |
3150 (setq end-txt (1- beg-und)) | |
3151 (setq beg-txt (progn | |
3152 (if (re-search-backward "^\\s *$" 1 'move) | |
3153 (forward-line 1)) | |
3154 (point))))) | |
3155 (list key | |
3156 (or beg-ovr beg-txt beg-und) | |
3157 (or end-und end-txt end-und) | |
3158 beg-ovr end-ovr beg-txt end-txt beg-und end-und))))) | |
3159 | |
3160 ;; Handles adornments for font-locking section titles and transitions. Returns | |
3161 ;; three match groups. First and last match group matched pure overline / | |
3162 ;; underline adornment while second group matched section title text. Each | |
3163 ;; group may not exist. | |
3164 (defun rst-font-lock-handle-adornment (limit) | |
3165 (let ((ado-pnt rst-font-lock-adornment-point)) | |
3166 ;; May run only once - enforce this | |
3167 (setq rst-font-lock-adornment-point nil) | |
3168 (if ado-pnt | |
3169 (let* ((ado (rst-classify-adornment (match-string-no-properties 1) | |
3170 ado-pnt limit)) | |
3171 (key (car ado)) | |
3172 (mtc (cdr ado))) | |
3173 (setq rst-font-lock-level (rst-adornment-level key t)) | |
3174 (goto-char (nth 1 mtc)) | |
3175 (set-match-data mtc) | |
3176 t)))) | |
3177 | |
3178 | |
3179 | |
3180 | |
3181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3182 ;; Support for conversion from within Emacs | |
3183 | |
3184 (defgroup rst-compile nil | |
3185 "Settings for support of conversion of reStructuredText | |
3186 document with \\[rst-compile]." | |
3187 :group 'rst | |
3188 :version "21.1") | |
3189 | |
3190 (defvar rst-compile-toolsets | |
3191 '((html . ("rst2html.py" ".html" nil)) | |
3192 (latex . ("rst2latex.py" ".tex" nil)) | |
3193 (newlatex . ("rst2newlatex.py" ".tex" nil)) | |
3194 (pseudoxml . ("rst2pseudoxml.py" ".xml" nil)) | |
3195 (xml . ("rst2xml.py" ".xml" nil))) | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3196 "Table describing the command to use for each toolset. |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3197 An association list of the toolset to a list of the (command to use, |
96117 | 3198 extension of produced filename, options to the tool (nil or a |
3199 string)) to be used for converting the document.") | |
3200 | |
3201 ;; Note for Python programmers not familiar with association lists: you can set | |
3202 ;; values in an alists like this, e.g. : | |
3203 ;; (setcdr (assq 'html rst-compile-toolsets) | |
3204 ;; '("rst2html.py" ".htm" "--stylesheet=/docutils.css")) | |
3205 | |
3206 | |
3207 (defvar rst-compile-primary-toolset 'html | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3208 "The default toolset for `rst-compile'.") |
96117 | 3209 |
3210 (defvar rst-compile-secondary-toolset 'latex | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3211 "The default toolset for `rst-compile' with a prefix argument.") |
96117 | 3212 |
3213 (defun rst-compile-find-conf () | |
3214 "Look for the configuration file in the parents of the current path." | |
3215 (interactive) | |
3216 (let ((file-name "docutils.conf") | |
3217 (buffer-file (buffer-file-name))) | |
3218 ;; Move up in the dir hierarchy till we find a change log file. | |
3219 (let* ((dir (file-name-directory buffer-file)) | |
3220 (prevdir nil)) | |
3221 (while (and (or (not (string= dir prevdir)) | |
3222 (setq dir nil) | |
3223 nil) | |
3224 (not (file-exists-p (concat dir file-name)))) | |
3225 ;; Move up to the parent dir and try again. | |
3226 (setq prevdir dir) | |
3227 (setq dir (expand-file-name (file-name-directory | |
3228 (directory-file-name | |
3229 (file-name-directory dir))))) | |
3230 ) | |
3231 (or (and dir (concat dir file-name)) nil) | |
3232 ))) | |
3233 | |
3234 | |
3235 (require 'compile) | |
3236 | |
3237 (defun rst-compile (&optional pfxarg) | |
3238 "Compile command to convert reST document into some output file. | |
3239 Attempts to find configuration file, if it can, overrides the | |
3240 options. There are two commands to choose from, with a prefix | |
3241 argument, select the alternative toolset." | |
3242 (interactive "P") | |
3243 ;; Note: maybe we want to check if there is a Makefile too and not do anything | |
3244 ;; if that is the case. I dunno. | |
3245 (let* ((toolset (cdr (assq (if pfxarg | |
3246 rst-compile-secondary-toolset | |
3247 rst-compile-primary-toolset) | |
3248 rst-compile-toolsets))) | |
3249 (command (car toolset)) | |
3250 (extension (cadr toolset)) | |
3251 (options (caddr toolset)) | |
3252 (conffile (rst-compile-find-conf)) | |
3253 (bufname (file-name-nondirectory buffer-file-name)) | |
3254 (outname (file-name-sans-extension bufname))) | |
3255 | |
3256 ;; Set compile-command before invocation of compile. | |
3257 (set (make-local-variable 'compile-command) | |
3258 (mapconcat 'identity | |
3259 (list command | |
3260 (or options "") | |
3261 (if conffile | |
3262 (concat "--config=\"" conffile "\"") | |
3263 "") | |
3264 bufname | |
3265 (concat outname extension)) | |
3266 " ")) | |
3267 | |
3268 ;; Invoke the compile command. | |
3269 (if (or compilation-read-command current-prefix-arg) | |
3270 (call-interactively 'compile) | |
3271 (compile compile-command)) | |
3272 )) | |
3273 | |
3274 (defun rst-compile-alt-toolset () | |
3275 "Compile command with the alternative toolset." | |
3276 (interactive) | |
3277 (rst-compile 't)) | |
3278 | |
3279 (defun rst-compile-pseudo-region () | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3280 "Show the pseudo-XML rendering of the current active region, |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3281 or of the entire buffer, if the region is not selected." |
96117 | 3282 (interactive) |
3283 (with-output-to-temp-buffer "*pseudoxml*" | |
3284 (shell-command-on-region | |
3285 (if mark-active (region-beginning) (point-min)) | |
3286 (if mark-active (region-end) (point-max)) | |
3287 "rst2pseudoxml.py" | |
3288 standard-output))) | |
3289 | |
3290 (defvar rst-pdf-program "xpdf" | |
3291 "Program used to preview PDF files.") | |
3292 | |
3293 (defun rst-compile-pdf-preview () | |
3294 "Convert the document to a PDF file and launch a preview program." | |
3295 (interactive) | |
3296 (let* ((tmp-filename "/tmp/out.pdf") | |
3297 (command (format "rst2pdf.py %s %s && %s %s" | |
3298 buffer-file-name tmp-filename | |
3299 rst-pdf-program tmp-filename))) | |
3300 (start-process-shell-command "rst-pdf-preview" nil command) | |
3301 ;; Note: you could also use (compile command) to view the compilation | |
3302 ;; output. | |
3303 )) | |
3304 | |
3305 (defvar rst-slides-program "firefox" | |
3306 "Program used to preview S5 slides.") | |
3307 | |
3308 (defun rst-compile-slides-preview () | |
3309 "Convert the document to an S5 slide presentation and launch a preview program." | |
3310 (interactive) | |
3311 (let* ((tmp-filename "/tmp/slides.html") | |
3312 (command (format "rst2s5.py %s %s && %s %s" | |
3313 buffer-file-name tmp-filename | |
3314 rst-slides-program tmp-filename))) | |
3315 (start-process-shell-command "rst-slides-preview" nil command) | |
3316 ;; Note: you could also use (compile command) to view the compilation | |
3317 ;; output. | |
3318 )) | |
3319 | |
3320 | |
3321 | |
3322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3323 ;; | |
3324 ;; Generic text functions that are more convenient than the defaults. | |
3325 ;; | |
3326 | |
3327 (defun rst-replace-lines (fromchar tochar) | |
3328 "Replace flush-left lines, consisting of multiple FROMCHAR characters, | |
3329 with equal-length lines of TOCHAR." | |
3330 (interactive "\ | |
3331 cSearch for flush-left lines of char: | |
3332 cand replace with char: ") | |
3333 (save-excursion | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3334 (let ((searchre (concat "^" (regexp-quote (string fromchar)) "+\\( *\\)$")) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3335 (found 0)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3336 (while (search-forward-regexp searchre nil t) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3337 (setq found (1+ found)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3338 (goto-char (match-beginning 1)) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3339 (let ((width (current-column))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3340 (rst-delete-entire-line) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3341 (insert-char tochar width))) |
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3342 (message (format "%d lines replaced." found))))) |
96117 | 3343 |
3344 (defun rst-join-paragraph () | |
3345 "Join lines in current paragraph into one line, removing end-of-lines." | |
3346 (interactive) | |
3347 (let ((fill-column 65000)) ; some big number | |
3348 (call-interactively 'fill-paragraph))) | |
3349 | |
3350 (defun rst-force-fill-paragraph () | |
3351 "Fill paragraph at point, first joining the paragraph's lines into one. | |
3352 This is useful for filling list item paragraphs." | |
3353 (interactive) | |
3354 (rst-join-paragraph) | |
3355 (fill-paragraph nil)) | |
3356 | |
3357 | |
3358 ;; Generic character repeater function. | |
3359 ;; For sections, better to use the specialized function above, but this can | |
3360 ;; be useful for creating separators. | |
3361 (defun rst-repeat-last-character (&optional tofill) | |
3362 "Fills the current line up to the length of the preceding line (if not | |
3363 empty), using the last character on the current line. If the preceding line is | |
96126
fcd827c4a553
Fix up docstring conventions.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
96117
diff
changeset
|
3364 empty, we use the `fill-column'. |
96117 | 3365 |
3366 If a prefix argument is provided, use the next line rather than the preceding | |
3367 line. | |
3368 | |
3369 If the current line is longer than the desired length, shave the characters off | |
3370 the current line to fit the desired length. | |
3371 | |
3372 As an added convenience, if the command is repeated immediately, the alternative | |
3373 column is used (fill-column vs. end of previous/next line)." | |
3374 (interactive) | |
3375 (let* ((curcol (current-column)) | |
3376 (curline (+ (count-lines (point-min) (point)) | |
3377 (if (eq curcol 0) 1 0))) | |
3378 (lbp (line-beginning-position 0)) | |
3379 (prevcol (if (and (= curline 1) (not current-prefix-arg)) | |
3380 fill-column | |
3381 (save-excursion | |
3382 (forward-line (if current-prefix-arg 1 -1)) | |
3383 (end-of-line) | |
3384 (skip-chars-backward " \t" lbp) | |
3385 (let ((cc (current-column))) | |
3386 (if (= cc 0) fill-column cc))))) | |
3387 (rightmost-column | |
3388 (cond (tofill fill-column) | |
3389 ((equal last-command 'rst-repeat-last-character) | |
3390 (if (= curcol fill-column) prevcol fill-column)) | |
3391 (t (save-excursion | |
3392 (if (= prevcol 0) fill-column prevcol))) | |
3393 )) ) | |
3394 (end-of-line) | |
3395 (if (> (current-column) rightmost-column) | |
3396 ;; shave characters off the end | |
3397 (delete-region (- (point) | |
3398 (- (current-column) rightmost-column)) | |
3399 (point)) | |
3400 ;; fill with last characters | |
3401 (insert-char (preceding-char) | |
3402 (- rightmost-column (current-column)))) | |
3403 )) | |
3404 | |
3405 | |
3406 (defun rst-portable-mark-active-p () | |
3407 "A portable function that returns non-nil if the mark is active." | |
3408 (cond | |
3409 ((fboundp 'region-active-p) (region-active-p)) | |
3410 ((boundp 'transient-mark-mode) transient-mark-mode mark-active))) | |
3411 | |
3412 | |
3413 | |
3414 (provide 'rst) | |
3415 ;;; rst.el ends here |