1 ;;; rst.el --- Mode for viewing and editing reStructuredText-documents.
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Authors: Martin Blais <blais@furius.ca>,
7 ;; Stefan Merten <smerten@oekonux.de>,
8 ;; David Goodger <goodger@python.org>
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; This package provides major mode rst-mode, which supports documents marked up
28 ;; using the reStructuredText format. Support includes font locking as well as
29 ;; some convenience functions for editing. It does this by defining a Emacs
30 ;; major mode: rst-mode (ReST). This mode is derived from text-mode (and
31 ;; inherits much of it). This package also contains:
33 ;; - Functions to automatically adjust and cycle the section underline
35 ;; - A mode that displays the table of contents and allows you to jump anywhere
37 ;; - Functions to insert and automatically update a TOC in your source
39 ;; - Font-lock highlighting of notable reStructuredText structures;
40 ;; - Some other convenience functions.
42 ;; See the accompanying document in the docutils documentation about
43 ;; the contents of this package and how to use it.
45 ;; For more information about reStructuredText, see
46 ;; http://docutils.sourceforge.net/rst.html
48 ;; For full details on how to use the contents of this file, see
49 ;; http://docutils.sourceforge.net/docs/user/emacs.html
52 ;; There are a number of convenient keybindings provided by rst-mode.
55 ;; C-c C-a (also C-=): rst-adjust
57 ;; Updates or rotates the section title around point or promotes/demotes the
58 ;; decorations within the region (see full details below). Note that C-= is a
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-=.
62 ;; For more on bindings, see rst-mode-map below. There are also many variables
63 ;; that can be customized, look for defcustom and defvar in this file.
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::
68 ;; (add-hook 'rst-adjust-hook 'rst-toc-update)
70 ;; Syntax highlighting: font-lock is enabled by default. If you want to turn
71 ;; off syntax highlighting to rst-mode, you can use the following::
73 ;; (setq font-lock-global-modes '(not rst-mode ...))
81 ;; This group contains some general customizable features.
83 ;; The group is contained in the wp group.
87 ;; This group contains all necessary for customizing fonts. The default
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.
91 ;; The group is contained in the faces group as well as in the rst group.
95 ;; This group contains all necessary for customizing the default fonts used for
96 ;; section title faces.
98 ;; The general idea for section title faces is to have a non-default background
99 ;; but do not change the background. The section level is shown by the
100 ;; lightness of the background color. If you like this general idea of
101 ;; generating faces for section titles but do not like the details this group
102 ;; is the point where you can customize the details. If you do not like the
103 ;; general idea, however, you should customize the faces used in
104 ;; rst-adornment-faces-alist.
106 ;; Note: If you are using a dark background please make sure the variable
107 ;; frame-background-mode is set to the symbol dark. This triggers
108 ;; some default values which are probably right for you.
110 ;; The group is contained in the rst-faces group.
112 ;; All customizable features have a comment explaining their meaning.
113 ;; Refer to the customization of your Emacs (try ``M-x customize``).
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
124 ;; Add the following lines to your `.emacs' file:
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
131 ;; provides to set the major mode automatically. For instance you may use::
133 ;; .. -*- mode: rst -*-
135 ;; in the very first line of your file. The following code is useful if you
136 ;; want automatically enter rst-mode from any file with compatible extensions:
138 ;; (setq auto-mode-alist
139 ;; (append '(("\\.txt$" . rst-mode)
140 ;; ("\\.rst$" . rst-mode)
141 ;; ("\\.rest$" . rst-mode)) auto-mode-alist))
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.
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.
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.
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.
186 ;; - We should support imenu in our major mode, with the menu filled with the
187 ;; section titles (this should be really easy).
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.
193 ;; - We need to automatically recenter on rst-forward-section movement commands.
202 (defgroup rst nil
"Support for reStructuredText documents"
205 :link
'(url-link "http://docutils.sourceforge.net/rst.html"))
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;; Define some generic support functions.
213 (eval-when-compile (require 'cl
)) ;; We need this for destructuring-bind below.
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
)
223 (goto-char (point-min))
227 (1+ (count-lines start
(point)))))) )
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 (let ((map (make-sparse-keymap)))
239 ;; Section Decorations.
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)
252 ;; Section Movement and Selection.
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
)
261 ;; Operating on Blocks of Text.
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
)
280 ;; Table-of-Contents Features.
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
)
292 ;; Converting Documents from Emacs.
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
)
305 "Keymap for ReStructuredText mode commands.
306 This inherits from Text mode.")
310 (defvar rst-mode-abbrev-table nil
311 "Abbrev table used while in rst mode.")
312 (define-abbrev-table 'rst-mode-abbrev-table
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.
324 (defvar rst-mode-syntax-table
325 (let ((st (copy-syntax-table text-mode-syntax-table
)))
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
)
343 "Syntax table used while in `rst-mode'.")
346 (defcustom rst-mode-hook nil
347 "Hook run when Rst Mode is turned on.
348 The hook for Text Mode is run before this one."
353 (defcustom rst-mode-lazy t
354 "*If non-nil Rst Mode tries to font-lock multi-line elements correctly.
355 Because this is really slow it should be set to nil if neither `jit-lock-mode'
356 not `lazy-lock-mode' and activated.
358 If nil, comments and literal blocks are font-locked only on the line they start.
360 The value of this variable is used when Rst Mode is turned on."
364 ;; Use rst-mode for *.rst and *.rest files. Many ReStructured-Text files
365 ;; use *.txt, but this is too generic to be set as a default.
366 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.re?st\\'" . rst-mode))
368 (define-derived-mode rst-mode text-mode
"ReST"
369 "Major mode for editing reStructuredText documents.
371 There are a number of convenient keybindings provided by
372 rst-mode. The main one is \[rst-adjust\], it updates or rotates
373 the section title around point or promotes/demotes the
374 decorations within the region (see full details below). Use
375 negative prefix arg to rotate in the other direction.
378 Turning on `rst-mode' calls the normal hooks `text-mode-hook' and
379 `rst-mode-hook'. This mode also supports font-lock highlighting.
380 You may customize `rst-mode-lazy' to toggle font-locking of
382 :abbrev-table rst-mode-abbrev-table
383 :syntax-table rst-mode-syntax-table
386 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
387 (set (make-local-variable 'indent-line-function
) 'indent-relative-maybe
)
388 (set (make-local-variable 'paragraph-start
)
389 "\f\\|>*[ \t]*$\\|>*[ \t]*[-+*] \\|>*[ \t]*[0-9#]+\\. ")
390 (set (make-local-variable 'adaptive-fill-mode
) t
)
392 ;; FIXME: No need to reset this.
393 ;; (set (make-local-variable 'indent-line-function) 'indent-relative)
395 ;; The details of the following comment setup is important because it affects
396 ;; auto-fill, and it is pretty common in running text to have an ellipsis
397 ;; ("...") which trips because of the rest comment syntax (".. ").
398 (set (make-local-variable 'comment-start
) ".. ")
399 (set (make-local-variable 'comment-start-skip
) "^\\.\\. ")
400 (set (make-local-variable 'comment-multi-line
) nil
)
403 (make-local-variable 'rst-adornment-level-alist
)
406 (set (make-local-variable 'font-lock-defaults
)
407 '(rst-font-lock-keywords-function
409 (font-lock-mark-block-function . mark-paragraph
)))
410 ;; `jit-lock-mode' has been the default since Emacs-21.1, so there's no
411 ;; point messing around with font-lock-support-mode any more.
412 ;; (when (boundp 'font-lock-support-mode)
413 ;; ;; rst-mode has its own mind about font-lock-support-mode
414 ;; (make-local-variable 'font-lock-support-mode)
415 ;; ;; jit-lock-mode replaced lazy-lock-mode in GNU Emacs 21.
416 ;; (let ((jit-or-lazy-lock-mode
418 ;; ((fboundp 'lazy-lock-mode) 'lazy-lock-mode)
419 ;; ((fboundp 'jit-lock-mode) 'jit-lock-mode)
420 ;; ;; if neither lazy-lock nor jit-lock is supported,
421 ;; ;; tell user and disable rst-mode-lazy
422 ;; (t (when rst-mode-lazy
423 ;; (message "Disabled lazy fontification, because no known support mode found.")
424 ;; (setq rst-mode-lazy nil))))))
426 ;; ((and (not rst-mode-lazy) (not font-lock-support-mode)))
427 ;; ;; No support mode set and none required - leave it alone
428 ;; ((or (not font-lock-support-mode) ;; No support mode set (but required)
429 ;; (symbolp font-lock-support-mode)) ;; or a fixed mode for all
430 ;; (setq font-lock-support-mode
431 ;; (list (cons 'rst-mode (and rst-mode-lazy jit-or-lazy-lock-mode))
432 ;; (cons t font-lock-support-mode))))
433 ;; ((and (listp font-lock-support-mode)
434 ;; (not (assoc 'rst-mode font-lock-support-mode)))
435 ;; ;; A list of modes missing rst-mode
436 ;; (setq font-lock-support-mode
437 ;; (cons (cons 'rst-mode (and rst-mode-lazy jit-or-lazy-lock-mode))
438 ;; font-lock-support-mode))))))
444 (define-minor-mode rst-minor-mode
446 Toggle ReST minor mode.
447 With no argument, this command toggles the mode.
448 Non-null prefix argument turns on the mode.
449 Null prefix argument turns off the mode.
451 When ReST minor mode is enabled, the ReST mode
452 keybindings are installed on top of the major
453 mode bindings. Use this for modes derived from
454 text-mode, like mail-mode.."
455 ;; The initial value.
457 ;; The indicator for the mode line.
459 ;; The minor mode bindings.
463 ;; FIXME: can I somehow install these too?
464 ;; :abbrev-table rst-mode-abbrev-table
465 ;; :syntax-table rst-mode-syntax-table
471 ;; Bulleted item lists.
472 (defcustom rst-bullets
474 "List of all possible bullet characters for bulleted lists."
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;; Section Decoration Adjusment
482 ;; ============================
484 ;; The following functions implement a smart automatic title sectioning feature.
485 ;; The idea is that with the cursor sitting on a section title, we try to get as
486 ;; much information from context and try to do the best thing automatically.
487 ;; This function can be invoked many times and/or with prefix argument to rotate
488 ;; between the various sectioning decorations.
490 ;; Definitions: the two forms of sectioning define semantically separate section
491 ;; levels. A sectioning DECORATION consists in:
495 ;; - a STYLE which can be either of 'simple' or 'over-and-under'.
497 ;; - an INDENT (meaningful for the over-and-under style only) which determines
498 ;; how many characters and over-and-under style is hanging outside of the
499 ;; title at the beginning and ending.
501 ;; Important note: an existing decoration must be formed by at least two
502 ;; characters to be recognized.
504 ;; Here are two examples of decorations (| represents the window border, column
508 ;; 1. char: '-' e |Some Title
509 ;; style: simple |----------
511 ;; 2. char: '=' |==============
512 ;; style: over-and-under | Some Title
513 ;; indent: 2 |==============
518 ;; - The underlining character that is used depends on context. The file is
519 ;; scanned to find other sections and an appropriate character is selected.
520 ;; If the function is invoked on a section that is complete, the character is
521 ;; rotated among the existing section decorations.
523 ;; Note that when rotating the characters, if we come to the end of the
524 ;; hierarchy of decorations, the variable rst-preferred-decorations is
525 ;; consulted to propose a new underline decoration, and if continued, we cycle
526 ;; the decorations all over again. Set this variable to nil if you want to
527 ;; limit the underlining character propositions to the existing decorations in
530 ;; - A prefix argument can be used to alternate the style.
532 ;; - An underline/overline that is not extended to the column at which it should
533 ;; be hanging is dubbed INCOMPLETE. For example::
538 ;; Examples of default invocation:
540 ;; |Some Title ---> |Some Title
543 ;; |Some Title ---> |Some Title
544 ;; |----- |----------
547 ;; | Some Title ---> | Some Title
550 ;; In over-and-under style, when alternating the style, a variable is
551 ;; available to select how much default indent to use (it can be zero). Note
552 ;; that if the current section decoration already has an indent, we don't
553 ;; adjust it to the default, we rather use the current indent that is already
554 ;; there for adjustment (unless we cycle, in which case we use the indent
555 ;; that has been found previously).
557 (defgroup rst-adjust nil
558 "Settings for adjustment and cycling of section title
563 (defcustom rst-preferred-decorations
'( (?
= over-and-under
1)
571 "Preferred ordering of section title decorations.
573 This sequence is consulted to offer a new decoration suggestion
574 when we rotate the underlines at the end of the existing
575 hierarchy of characters, or when there is no existing section
580 (defcustom rst-default-indent
1
581 "Number of characters to indent the section title.
583 THis is used for when toggling decoration styles, when switching
584 from a simple decoration style to a over-and-under decoration
589 (defvar rst-section-text-regexp
"^[ \t]*\\S-*\\w\\S-*"
590 "Regular expression for valid section title text.")
593 (defun rst-line-homogeneous-p (&optional accept-special
)
594 "Return true if the line is homogeneous.
596 Predicate that returns the unique char if the current line is
597 composed only of a single repeated non-whitespace character. This
598 returns the char even if there is whitespace at the beginning of
601 If ACCEPT-SPECIAL is specified we do not ignore special sequences
602 which normally we would ignore when doing a search on many lines.
603 For example, normally we have cases to ignore commonly occuring
604 patterns, such as :: or ...; with the flag do not ignore them."
606 (back-to-indentation)
607 (unless (looking-at "\n")
608 (let ((c (thing-at-point 'char
)))
609 (if (and (looking-at (format "[%s]+[ \t]*$" c
))
613 (not (looking-at "::[ \t]*$"))
614 (not (looking-at "\\.\\.\\.[ \t]*$"))
615 ;; Discard one char line
616 (not (looking-at ".[ \t]*$"))
622 (defun rst-line-homogeneous-nodent-p (&optional accept-special
)
623 "Return true if the line is homogeneous with no indent.
624 See `rst-line-homogeneous-p' about ACCEPT-SPECIAL."
627 (if (looking-at "^[ \t]+")
629 (rst-line-homogeneous-p accept-special
)
633 (defun rst-compare-decorations (deco1 deco2
)
634 "Compare decorations.
635 Returns true if both DECO1 and DECO2 decorations are equal,
636 according to restructured text semantics (only the character and
637 the style are compared, the indentation does not matter."
638 (and (eq (car deco1
) (car deco2
))
639 (eq (cadr deco1
) (cadr deco2
))))
642 (defun rst-get-decoration-match (hier deco
)
643 "Return the index (level) in hierarchy HIER of decoration DECO.
644 This basically just searches for the item using the appropriate
645 comparison and returns the index. We return nil if the item is
648 (while (and cur
(not (rst-compare-decorations (car cur
) deco
)))
649 (setq cur
(cdr cur
)))
653 (defun rst-suggest-new-decoration (alldecos &optional prev
)
654 "Suggest a new, different decoration from all that have been seen.
656 ALLDECOS is the set of all decorations, including the line
657 numbers. PREV is the optional previous decoration, in order to
658 suggest a better match."
660 ;; For all the preferred decorations...
662 ;; If 'prev' is given, reorder the list to start searching after the
665 (cdr (rst-get-decoration-match rst-preferred-decorations prev
)))
667 ;; List of candidates to search.
668 (curpotential (append fplist rst-preferred-decorations
)))
670 ;; For all the decorations...
673 (while (and cur
(not found
))
674 (if (rst-compare-decorations (car cur
) (car curpotential
))
676 (setq found
(car curpotential
))
677 (setq cur
(cdr cur
))))
680 (setq curpotential
(cdr curpotential
)))
682 (copy-sequence (car curpotential
))))
684 (defun rst-delete-entire-line ()
685 "Delete the entire current line without using the `kill-ring'."
686 (delete-region (line-beginning-position)
687 (line-beginning-position 2)))
689 (defun rst-update-section (char style
&optional indent
)
690 "Unconditionally update the style of a section decoration.
692 Do this using the given character CHAR, with STYLE 'simple or
693 'over-and-under, and with indent INDENT. If the STYLE is
694 'simple, whitespace before the title is removed (indent is always
697 If there are existing overline and/or underline from the
698 existing decoration, they are removed before adding the
699 requested decoration."
706 (setq marker
(point-marker))
708 ;; Fixup whitespace at the beginning and end of the line
709 (if (or (null indent
) (eq style
'simple
))
712 (delete-horizontal-space)
713 (insert (make-string indent ?
))
716 (delete-horizontal-space)
718 ;; Set the current column, we're at the end of the title line
719 (setq len
(+ (current-column) indent
))
721 ;; Remove previous line if it consists only of a single repeated character
724 (and (rst-line-homogeneous-p 1)
725 ;; Avoid removing the underline of a title right above us.
726 (save-excursion (forward-line -
1)
727 (not (looking-at rst-section-text-regexp
)))
728 (rst-delete-entire-line)))
730 ;; Remove following line if it consists only of a single repeated
734 (and (rst-line-homogeneous-p 1)
735 (rst-delete-entire-line))
736 ;; Add a newline if we're at the end of the buffer, for the subsequence
737 ;; inserting of the underline
738 (if (= (point) (buffer-end 1))
742 (if (eq style
'over-and-under
)
746 (insert (make-string len char
))))
751 (insert (make-string len char
))
758 (defun rst-normalize-cursor-position ()
759 "Normalize the cursor position.
760 If the cursor is on a decoration line or an empty line , place it
761 on the section title line (at the end). Returns the line offset
762 by which the cursor was moved. This works both over or under a
764 (if (save-excursion (beginning-of-line)
765 (or (rst-line-homogeneous-p 1)
766 (looking-at "^[ \t]*$")))
770 ((save-excursion (forward-line -
1)
772 (and (looking-at rst-section-text-regexp
)
773 (not (rst-line-homogeneous-p 1))))
774 (progn (forward-line -
1) -
1))
775 ((save-excursion (forward-line +1)
777 (and (looking-at rst-section-text-regexp
)
778 (not (rst-line-homogeneous-p 1))))
779 (progn (forward-line +1) +1))
784 (defun rst-find-all-decorations ()
785 "Find all the decorations in the file.
786 Return a list of (line, decoration) pairs. Each decoration
787 consists in a (char, style, indent) triple.
789 This function does not detect the hierarchy of decorations, it
790 just finds all of them in a file. You can then invoke another
791 function to remove redundancies and inconsistencies."
795 ;; Iterate over all the section titles/decorations in the file.
797 (goto-char (point-min))
798 (while (< (point) (buffer-end 1))
799 (if (rst-line-homogeneous-nodent-p)
801 (setq curline
(+ curline
(rst-normalize-cursor-position)))
803 ;; Here we have found a potential site for a decoration,
805 (let ((deco (rst-get-decoration)))
806 (if (cadr deco
) ;; Style is existing.
807 ;; Found a real decoration site.
809 (push (cons curline deco
) positions
)
810 ;; Push beyond the underline.
812 (setq curline
(+ curline
1))
816 (setq curline
(+ curline
1))
818 (reverse positions
)))
821 (defun rst-infer-hierarchy (decorations)
822 "Build a hierarchy of decorations using the list of given DECORATIONS.
824 This function expects a list of (char, style, indent) decoration
825 specifications, in order that they appear in a file, and will
826 infer a hierarchy of section levels by removing decorations that
827 have already been seen in a forward traversal of the decorations,
828 comparing just the character and style.
830 Similarly returns a list of (char, style, indent), where each
831 list element should be unique."
833 (let ((hierarchy-alist (list)))
834 (dolist (x decorations
)
837 (unless (assoc (cons char style
) hierarchy-alist
)
838 (push (cons (cons char style
) x
) hierarchy-alist
))
841 (mapcar 'cdr
(nreverse hierarchy-alist
))
845 (defun rst-get-hierarchy (&optional alldecos ignore
)
846 "Return the hierarchy of section titles in the file.
848 Return a list of decorations that represents the hierarchy of
849 section titles in the file. Reuse the list of decorations
850 already computed in ALLDECOS if present. If the line number in
851 IGNORE is specified, the decoration found on that line (if there
852 is one) is not taken into account when building the hierarchy."
853 (let ((all (or alldecos
(rst-find-all-decorations))))
854 (setq all
(assq-delete-all ignore all
))
855 (rst-infer-hierarchy (mapcar 'cdr all
))))
858 (defun rst-get-decoration (&optional point
)
859 "Get the decoration at POINT.
861 Looks around point and finds the characteristics of the
862 decoration that is found there. We assume that the cursor is
863 already placed on the title line (and not on the overline or
866 This function returns a (char, style, indent) triple. If the
867 characters of overline and underline are different, we return
868 the underline character. The indent is always calculated. A
869 decoration can be said to exist if the style is not nil.
871 A point can be specified to go to the given location before
872 extracting the decoration."
874 (let (char style indent
)
876 (if point
(goto-char point
))
878 (if (looking-at rst-section-text-regexp
)
879 (let* ((over (save-excursion
881 (rst-line-homogeneous-nodent-p)))
883 (under (save-excursion
885 (rst-line-homogeneous-nodent-p)))
888 ;; Check that the line above the overline is not part of a title
892 (and (equal (forward-line -
2) 0)
893 (looking-at rst-section-text-regexp
))))
897 ;; No decoration found, leave all return values nil.
898 ((and (eq over nil
) (eq under nil
)))
900 ;; Overline only, leave all return values nil.
902 ;; Note: we don't return the overline character, but it could
903 ;; perhaps in some cases be used to do something.
904 ((and over
(eq under nil
)))
907 ((and under
(eq over nil
))
911 ;; Both overline and underline.
914 style
'over-and-under
))
919 (setq indent
(save-excursion (back-to-indentation) (current-column)))
922 (list char style indent
)))
925 (defun rst-get-decorations-around (&optional alldecos
)
926 "Return the decorations around point.
928 Given the list of all decorations ALLDECOS (with positions), find
929 the decorations before and after the given point. A list of the
930 previous and next decorations is returned."
931 (let* ((all (or alldecos
(rst-find-all-decorations)))
932 (curline (line-number-at-pos))
936 ;; Search for the decorations around the current line.
937 (while (and cur
(< (caar cur
) curline
))
940 ;; 'cur' is the following decoration.
942 (if (and cur
(caar cur
))
943 (setq next
(if (= curline
(caar cur
)) (cdr cur
) cur
)))
945 (mapcar 'cdar
(list prev next
))
949 (defun rst-decoration-complete-p (deco)
950 "Return true if the decoration DECO around POINT is complete."
951 ;; Note: we assume that the detection of the overline as being the underline
952 ;; of a preceding title has already been detected, and has been eliminated
953 ;; from the decoration that is given to us.
955 ;; There is some sectioning already present, so check if the current
956 ;; sectioning is complete and correct.
957 (let* ((char (car deco
))
959 (indent (caddr deco
))
960 (endcol (save-excursion (end-of-line) (current-column)))
963 (let ((exps (concat "^"
964 (regexp-quote (make-string (+ endcol indent
) char
))
967 (save-excursion (forward-line +1)
970 (or (not (eq style
'over-and-under
))
971 (save-excursion (forward-line -
1)
978 (defun rst-get-next-decoration
979 (curdeco hier
&optional suggestion reverse-direction
)
980 "Get the next decoration for CURDECO, in given hierarchy HIER.
981 If suggesting, suggest for new decoration SUGGESTION.
982 REVERSE-DIRECTION is used to reverse the cycling order."
986 (style (cadr curdeco
))
988 ;; Build a new list of decorations for the rotation.
991 ;; Suggest a new decoration.
993 ;; If nothing to suggest, use first decoration.
996 ;; Search for next decoration.
998 (let ((cur (if reverse-direction rotdecos
999 (reverse rotdecos
))))
1001 (not (and (eq char
(caar cur
))
1002 (eq style
(cadar cur
)))))
1003 (setq cur
(cdr cur
)))
1006 ;; If not found, take the first of all decorations.
1011 (defun rst-adjust ()
1012 "Auto-adjust the decoration around point.
1014 Adjust/rotate the section decoration for the section title
1015 around point or promote/demote the decorations inside the region,
1016 depending on if the region is active. This function is meant to
1017 be invoked possibly multiple times, and can vary its behavior
1018 with a positive prefix argument (toggle style), or with a
1019 negative prefix argument (alternate behavior).
1021 This function is the main focus of this module and is a bit of a
1022 swiss knife. It is meant as the single most essential function
1023 to be bound to invoke to adjust the decorations of a section
1024 title in restructuredtext. It tries to deal with all the
1025 possible cases gracefully and to do `the right thing' in all
1028 See the documentations of `rst-adjust-decoration' and
1029 `rst-promote-region' for full details.
1034 The method can take either (but not both) of
1036 a. a (non-negative) prefix argument, which means to toggle the
1037 decoration style. Invoke with a prefix arg for example;
1039 b. a negative numerical argument, which generally inverts the
1040 direction of search in the file or hierarchy. Invoke with C--
1041 prefix for example."
1044 (let* (;; Save our original position on the current line.
1045 (origpt (set-marker (make-marker) (point)))
1047 ;; Parse the positive and negative prefix arguments.
1049 (and current-prefix-arg
1050 (< (prefix-numeric-value current-prefix-arg
) 0)))
1052 (and current-prefix-arg
(not reverse-direction
))))
1054 (if (rst-portable-mark-active-p)
1055 ;; Adjust decorations within region.
1056 (rst-promote-region current-prefix-arg
)
1057 ;; Adjust decoration around point.
1058 (rst-adjust-decoration toggle-style reverse-direction
))
1060 ;; Run the hooks to run after adjusting.
1061 (run-hooks 'rst-adjust-hook
)
1063 ;; Make sure to reset the cursor position properly after we're done.
1068 (defvar rst-adjust-hook nil
1069 "Hooks to be run after running `rst-adjust'.")
1071 (defvar rst-new-decoration-down nil
1072 "Non-nil if new decoration is added deeper.
1073 If non-nil, a new decoration being added will be initialized to
1074 be one level down from the previous decoration. If nil, a new
1075 decoration will be equal to the level of the previous
1078 (defun rst-adjust-decoration (&optional toggle-style reverse-direction
)
1079 "Adjust/rotate the section decoration for the section title around point.
1081 This function is meant to be invoked possibly multiple times, and
1082 can vary its behavior with a true TOGGLE-STYLE argument, or with
1083 a REVERSE-DIRECTION argument.
1088 The next action it takes depends on context around the point, and
1089 it is meant to be invoked possibly more than once to rotate among
1090 the various possibilities. Basically, this function deals with:
1092 - adding a decoration if the title does not have one;
1094 - adjusting the length of the underline characters to fit a
1097 - rotating the decoration in the set of already existing
1098 sectioning decorations used in the file;
1100 - switching between simple and over-and-under styles.
1102 You should normally not have to read all the following, just
1103 invoke the method and it will do the most obvious thing that you
1107 Decoration Definitions
1108 ======================
1110 The decorations consist in
1114 2. a STYLE which can be either of 'simple' or 'over-and-under'.
1116 3. an INDENT (meaningful for the over-and-under style only)
1117 which determines how many characters and over-and-under
1118 style is hanging outside of the title at the beginning and
1121 See source code for mode details.
1124 Detailed Behaviour Description
1125 ==============================
1127 Here are the gory details of the algorithm (it seems quite
1128 complicated, but really, it does the most obvious thing in all
1129 the particular cases):
1131 Before applying the decoration change, the cursor is placed on
1132 the closest line that could contain a section title.
1134 Case 1: No Decoration
1135 ---------------------
1137 If the current line has no decoration around it,
1139 - search backwards for the last previous decoration, and apply
1140 the decoration one level lower to the current line. If there
1141 is no defined level below this previous decoration, we suggest
1142 the most appropriate of the `rst-preferred-decorations'.
1144 If REVERSE-DIRECTION is true, we simply use the previous
1145 decoration found directly.
1147 - if there is no decoration found in the given direction, we use
1148 the first of `rst-preferred-decorations'.
1150 The prefix argument forces a toggle of the prescribed decoration
1153 Case 2: Incomplete Decoration
1154 -----------------------------
1156 If the current line does have an existing decoration, but the
1157 decoration is incomplete, that is, the underline/overline does
1158 not extend to exactly the end of the title line (it is either too
1159 short or too long), we simply extend the length of the
1160 underlines/overlines to fit exactly the section title.
1162 If the prefix argument is given, we toggle the style of the
1165 REVERSE-DIRECTION has no effect in this case.
1167 Case 3: Complete Existing Decoration
1168 ------------------------------------
1170 If the decoration is complete (i.e. the underline (overline)
1171 length is already adjusted to the end of the title line), we
1172 search/parse the file to establish the hierarchy of all the
1173 decorations (making sure not to include the decoration around
1174 point), and we rotate the current title's decoration from within
1175 that list (by default, going *down* the hierarchy that is present
1176 in the file, i.e. to a lower section level). This is meant to be
1177 used potentially multiple times, until the desired decoration is
1178 found around the title.
1180 If we hit the boundary of the hierarchy, exactly one choice from
1181 the list of preferred decorations is suggested/chosen, the first
1182 of those decoration that has not been seen in the file yet (and
1183 not including the decoration around point), and the next
1184 invocation rolls over to the other end of the hierarchy (i.e. it
1185 cycles). This allows you to avoid having to set which character
1186 to use by always using the
1188 If REVERSE-DIRECTION is true, the effect is to change the
1189 direction of rotation in the hierarchy of decorations, thus
1190 instead going *up* the hierarchy.
1192 However, if there is a non-negative prefix argument, we do not
1193 rotate the decoration, but instead simply toggle the style of the
1194 current decoration (this should be the most common way to toggle
1195 the style of an existing complete decoration).
1201 The invocation of this function can be carried out anywhere
1202 within the section title line, on an existing underline or
1203 overline, as well as on an empty line following a section title.
1204 This is meant to be as convenient as possible.
1210 Indented section titles such as ::
1215 are illegal in restructuredtext and thus not recognized by the
1216 parser. This code will thus not work in a way that would support
1217 indented sections (it would be ambiguous anyway).
1223 Section titles that are right next to each other may not be
1224 treated well. More work might be needed to support those, and
1225 special conditions on the completeness of existing decorations
1226 might be required to make it non-ambiguous.
1228 For now we assume that the decorations are disjoint, that is,
1229 there is at least a single line between the titles/decoration
1236 We suggest that you bind this function on C-=. It is close to
1237 C-- so a negative argument can be easily specified with a flick
1238 of the right hand fingers and the binding is unused in `text-mode'."
1241 ;; If we were invoked directly, parse the prefix arguments into the
1242 ;; arguments of the function.
1243 (if current-prefix-arg
1244 (setq reverse-direction
1245 (and current-prefix-arg
1246 (< (prefix-numeric-value current-prefix-arg
) 0))
1249 (and current-prefix-arg
(not reverse-direction
))))
1251 (let* (;; Check if we're on an underline around a section title, and move the
1252 ;; cursor to the title if this is the case.
1253 (moved (rst-normalize-cursor-position))
1255 ;; Find the decoration and completeness around point.
1256 (curdeco (rst-get-decoration))
1257 (char (car curdeco
))
1258 (style (cadr curdeco
))
1259 (indent (caddr curdeco
))
1261 ;; New values to be computed.
1262 char-new style-new indent-new
1265 ;; We've moved the cursor... if we're not looking at some text, we have
1267 (if (save-excursion (beginning-of-line)
1268 (looking-at rst-section-text-regexp
))
1271 ;;-------------------------------------------------------------------
1272 ;; Case 1: No Decoration
1273 ((and (eq char nil
) (eq style nil
))
1275 (let* ((alldecos (rst-find-all-decorations))
1277 (around (rst-get-decorations-around alldecos
))
1281 (hier (rst-get-hierarchy alldecos
))
1284 ;; Advance one level down.
1287 (if (not reverse-direction
)
1288 (or (funcall (if rst-new-decoration-down
'cadr
'car
)
1289 (rst-get-decoration-match hier prev
))
1290 (rst-suggest-new-decoration hier prev
))
1292 (copy-sequence (car rst-preferred-decorations
))))
1294 ;; Invert the style if requested.
1296 (setcar (cdr cur
) (if (eq (cadr cur
) 'simple
)
1297 'over-and-under
'simple
)) )
1299 (setq char-new
(car cur
)
1300 style-new
(cadr cur
)
1301 indent-new
(caddr cur
))
1304 ;;-------------------------------------------------------------------
1305 ;; Case 2: Incomplete Decoration
1306 ((not (rst-decoration-complete-p curdeco
))
1308 ;; Invert the style if requested.
1310 (setq style
(if (eq style
'simple
) 'over-and-under
'simple
)))
1316 ;;-------------------------------------------------------------------
1317 ;; Case 3: Complete Existing Decoration
1321 ;; Simply switch the style of the current decoration.
1323 style-new
(if (eq style
'simple
) 'over-and-under
'simple
)
1324 indent-new rst-default-indent
)
1326 ;; Else, we rotate, ignoring the decoration around the current
1328 (let* ((alldecos (rst-find-all-decorations))
1330 (hier (rst-get-hierarchy alldecos
(line-number-at-pos)))
1332 ;; Suggestion, in case we need to come up with something
1334 (suggestion (rst-suggest-new-decoration
1336 (car (rst-get-decorations-around alldecos
))))
1338 (nextdeco (rst-get-next-decoration
1339 curdeco hier suggestion reverse-direction
))
1343 ;; Indent, if present, always overrides the prescribed indent.
1344 (setq char-new
(car nextdeco
)
1345 style-new
(cadr nextdeco
)
1346 indent-new
(caddr nextdeco
))
1351 ;; Override indent with present indent!
1352 (setq indent-new
(if (> indent
0) indent indent-new
))
1354 (if (and char-new style-new
)
1355 (rst-update-section char-new style-new indent-new
))
1359 ;; Correct the position of the cursor to more accurately reflect where it
1360 ;; was located when the function was invoked.
1362 (forward-line (- moved
))
1367 ;; Maintain an alias for compatibility.
1368 (defalias 'rst-adjust-section-title
'rst-adjust
)
1371 (defun rst-promote-region (&optional demote
)
1372 "Promote the section titles within the region.
1374 With argument DEMOTE or a prefix argument, demote the
1375 section titles instead. The algorithm used at the boundaries of
1376 the hierarchy is similar to that used by `rst-adjust-decoration'."
1379 (let* ((demote (or current-prefix-arg demote
))
1380 (alldecos (rst-find-all-decorations))
1383 (hier (rst-get-hierarchy alldecos
))
1384 (suggestion (rst-suggest-new-decoration hier
))
1386 (region-begin-line (line-number-at-pos (region-beginning)))
1387 (region-end-line (line-number-at-pos (region-end)))
1392 ;; Skip the markers that come before the region beginning
1393 (while (and cur
(< (caar cur
) region-begin-line
))
1394 (setq cur
(cdr cur
)))
1396 ;; Create a list of markers for all the decorations which are found within
1400 (while (and cur
(< (setq line
(caar cur
)) region-end-line
))
1401 (setq m
(make-marker))
1403 (push (list (set-marker m
(point)) (cdar cur
)) marker-list
)
1404 (setq cur
(cdr cur
)) ))
1406 ;; Apply modifications.
1408 (dolist (p marker-list
)
1409 ;; Go to the decoration to promote.
1412 ;; Rotate the next decoration.
1413 (setq nextdeco
(rst-get-next-decoration
1414 (cadr p
) hier suggestion demote
))
1416 ;; Update the decoration.
1417 (apply 'rst-update-section nextdeco
)
1419 ;; Clear marker to avoid slowing down the editing after we're done.
1420 (set-marker (car p
) nil
)
1422 (setq deactivate-mark nil
)
1427 (defun rst-display-decorations-hierarchy (&optional decorations
)
1428 "Display the current file's section title decorations hierarchy.
1429 This function expects a list of (char, style, indent) triples in
1433 (if (not decorations
)
1434 (setq decorations
(rst-get-hierarchy)))
1435 (with-output-to-temp-buffer "*rest section hierarchy*"
1437 (with-current-buffer standard-output
1438 (dolist (x decorations
)
1439 (insert (format "\nSection Level %d" level
))
1440 (apply 'rst-update-section x
)
1441 (goto-char (point-max))
1447 (defun rst-position (elem list
)
1448 "Return position of ELEM in LIST or nil."
1449 (let ((tail (member elem list
)))
1450 (if tail
(- (length list
) (length tail
)))))
1452 (defun rst-straighten-decorations ()
1453 "Redo all the decorations in the current buffer.
1454 This is done using our preferred set of decorations. This can be
1455 used, for example, when using somebody else's copy of a document,
1456 in order to adapt it to our preferred style."
1459 (let* ((alldecos (rst-find-all-decorations))
1460 (hier (rst-get-hierarchy alldecos
))
1462 ;; Get a list of pairs of (level . marker)
1463 (levels-and-markers (mapcar
1465 (cons (rst-position (cdr deco
) hier
)
1466 (let ((m (make-marker)))
1467 (goto-line (car deco
))
1468 (set-marker m
(point))
1472 (dolist (lm levels-and-markers
)
1473 ;; Go to the appropriate position
1474 (goto-char (cdr lm
))
1476 ;; Apply the new styule
1477 (apply 'rst-update-section
(nth (car lm
) rst-preferred-decorations
))
1479 ;; Reset the market to avoid slowing down editing until it gets GC'ed
1480 (set-marker (cdr lm
) nil
)
1487 (defun rst-straighten-deco-spacing ()
1488 "Adjust the spacing before and after decorations in the entire document.
1489 The spacing will be set to two blank lines before the first two
1490 section levels, and one blank line before any of the other
1492 ;; FIXME: we need to take care of subtitle at some point.
1495 (let* ((alldecos (rst-find-all-decorations)))
1497 ;; Work the list from the end, so that we don't have to use markers to
1498 ;; adjust for the changes in the document.
1499 (dolist (deco (nreverse alldecos
))
1500 ;; Go to the appropriate position.
1501 (goto-line (car deco
))
1508 (defun rst-find-pfx-in-region (beg end pfx-re
)
1509 "Find all the positions of prefixes in region between BEG and END.
1510 This is used to find bullets and enumerated list items. PFX-RE
1511 is a regular expression for matching the lines with items."
1515 (while (< (point) end
)
1516 (back-to-indentation)
1519 (let ((pfx-col (current-column)))
1522 (back-to-indentation)
1523 (or (looking-at "^[ \t]*$")
1524 (> (current-column) pfx-col
)
1525 (and (= (current-column) pfx-col
)
1526 (looking-at pfx-re
))))))
1527 (push (cons (point) (current-column))
1532 (defvar rst-re-bullets
1533 (format "\\([%s][ \t]\\)[^ \t]" (regexp-quote (concat rst-bullets
)))
1534 "Regexp for finding bullets.")
1536 (defvar rst-re-enumerations
1537 "\\(\\(#\\|[0-9]+\\)\\.[ \t]\\)[^ \t]"
1538 "Regexp for finding bullets.")
1540 (defvar rst-re-items
1541 (format "\\(%s\\|%s\\)[^ \t]"
1542 (format "[%s][ \t]" (regexp-quote (concat rst-bullets
)))
1543 "\\(#\\|[0-9]+\\)\\.[ \t]")
1544 "Regexp for finding bullets.")
1546 (defvar rst-preferred-bullets
1548 "List of favourite bullets to set for straightening bullets.")
1550 (defun rst-straighten-bullets-region (beg end
)
1551 "Make all the bulleted list items in the region consistent.
1552 The region is specified between BEG and END. You can use this
1553 after you have merged multiple bulleted lists to make them use
1554 the same/correct/consistent bullet characters.
1556 See variable `rst-preferred-bullets' for the list of bullets to
1557 adjust. If bullets are found on levels beyond the
1558 `rst-preferred-bullets' list, they are not modified."
1561 (let ((bullets (rst-find-pfx-in-region beg end
1563 (levtable (make-hash-table :size
4)))
1565 ;; Create a map of levels to list of positions.
1567 (let ((key (cdr x
)))
1569 (append (gethash key levtable
(list))
1573 ;; Sort this map and create a new map of prefix char and list of positions.
1574 (let ((poslist ())) ; List of (indent . positions).
1575 (maphash (lambda (x y
) (push (cons x y
) poslist
)) levtable
)
1577 (let ((bullets rst-preferred-bullets
))
1578 (dolist (x (sort poslist
'car-less-than-car
))
1580 ;; Apply the characters.
1581 (dolist (pos (cdr x
))
1584 (insert (string (car bullets
))))
1585 (setq bullets
(cdr bullets
))))))))
1587 (defun rst-rstrip (str)
1588 "Strips the whitespace at the end of string STR."
1589 (string-match "[ \t\n]*\\'" str
)
1590 (substring str
0 (match-beginning 0)))
1592 (defun rst-get-stripped-line ()
1593 "Return the line at cursor, stripped from whitespace."
1594 (re-search-forward "\\S-.*\\S-" (line-end-position))
1595 (buffer-substring-no-properties (match-beginning 0)
1598 (defun rst-section-tree (alldecos)
1599 "Get the hierarchical tree of section titles.
1601 Returns a hierarchical tree of the sections titles in the
1602 document, for decorations ALLDECOS. This can be used to generate
1603 a table of contents for the document. The top node will always
1604 be a nil node, with the top level titles as children (there may
1605 potentially be more than one).
1607 Each section title consists in a cons of the stripped title
1608 string and a marker to the section in the original text document.
1610 If there are missing section levels, the section titles are
1611 inserted automatically, and the title string is set to nil, and
1612 the marker set to the first non-nil child of itself.
1613 Conceptually, the nil nodes--i.e. those which have no title--are
1614 to be considered as being the same line as their first non-nil
1615 child. This has advantages later in processing the graph."
1617 (let* ((hier (rst-get-hierarchy alldecos
))
1618 (levels (make-hash-table :test
'equal
:size
10))
1623 ;; Compare just the character and indent in the hash table.
1624 (puthash (cons (car deco
) (cadr deco
)) lev levels
)
1627 ;; Create a list of lines that contains (text, level, marker) for each
1631 (mapcar (lambda (deco)
1632 (goto-line (car deco
))
1633 (list (gethash (cons (cadr deco
) (caddr deco
)) levels
)
1634 (rst-get-stripped-line)
1635 (let ((m (make-marker)))
1636 (beginning-of-line 1)
1637 (set-marker m
(point)))
1641 (let ((lcontnr (cons nil lines
)))
1642 (rst-section-tree-rec lcontnr -
1))))
1645 (defun rst-section-tree-rec (decos lev
)
1646 "Recursive guts of the section tree construction.
1647 DECOS is a cons cell whose cdr is the remaining list of
1648 decorations, and we change it as we consume them. LEV is the
1649 current level of that node. This function returns a pair of the
1650 subtree that was built. This treats the decos list
1653 (let ((ndeco (cadr decos
))
1657 ;; If the next decoration matches our level
1658 (when (and ndeco
(= (car ndeco
) lev
))
1659 ;; Pop the next decoration and create the current node with it
1660 (setcdr decos
(cddr decos
))
1661 (setq node
(cdr ndeco
)) )
1662 ;; Else we let the node title/marker be unset.
1664 ;; Build the child nodes
1665 (while (and (cdr decos
) (> (caadr decos
) lev
))
1667 (cons (rst-section-tree-rec decos
(1+ lev
))
1669 (setq children
(reverse children
))
1671 ;; If node is still unset, we use the marker of the first child.
1673 (setq node
(cons nil
(cdaar children
))))
1675 ;; Return this node with its children.
1676 (cons node children
)
1680 (defun rst-section-tree-point (node &optional point
)
1681 "Find tree node at point.
1682 Given a computed and valid section tree in NODE and a point
1683 POINT (default being the current point in the current buffer),
1684 find and return the node within the sectree where the cursor
1687 Return values: a pair of (parent path, container subtree). The
1688 parent path is simply a list of the nodes above the container
1689 subtree node that we're returning."
1693 (let* ((curpoint (or point
(point))))
1695 ;; Check if we are before the current node.
1696 (if (and (cadar node
) (>= curpoint
(cadar node
)))
1698 ;; Iterate all the children, looking for one that might contain the
1700 (let ((curnode (cdr node
))
1703 (while (and curnode
(>= curpoint
(cadaar curnode
)))
1705 curnode
(cdr curnode
)))
1708 (let ((sub (rst-section-tree-point (car last
) curpoint
)))
1709 (setq path
(car sub
)
1711 (setq outtree node
))
1714 (cons (cons (car node
) path
) outtree
)
1718 (defgroup rst-toc nil
1719 "Settings for reStructuredText table of contents."
1723 (defcustom rst-toc-indent
2
1724 "Indentation for table-of-contents display.
1725 Also used for formatting insertion, when numbering is disabled."
1728 (defcustom rst-toc-insert-style
'fixed
1729 "Insertion style for table-of-contents.
1730 Set this to one of the following values to determine numbering and
1732 - plain: no numbering (fixed indentation)
1733 - fixed: numbering, but fixed indentation
1734 - aligned: numbering, titles aligned under each other
1735 - listed: numbering, with dashes like list items (EXPERIMENTAL)"
1738 (defcustom rst-toc-insert-number-separator
" "
1739 "Separator that goes between the TOC number and the title."
1742 ;; This is used to avoid having to change the user's mode.
1743 (defvar rst-toc-insert-click-keymap
1744 (let ((map (make-sparse-keymap)))
1745 (define-key map
[mouse-1
] 'rst-toc-mode-mouse-goto
)
1747 "(Internal) What happens when you click on propertized text in the TOC.")
1749 (defcustom rst-toc-insert-max-level nil
1750 "If non-nil, maximum depth of the inserted TOC."
1754 (defun rst-toc-insert (&optional pfxarg
)
1755 "Insert a simple text rendering of the table of contents.
1756 By default the top level is ignored if there is only one, because
1757 we assume that the document will have a single title.
1759 If a numeric prefix argument PFXARG is given, insert the TOC up
1760 to the specified level.
1762 The TOC is inserted indented at the current column."
1766 (let* (;; Check maximum level override
1767 (rst-toc-insert-max-level
1768 (if (and (integerp pfxarg
) (> (prefix-numeric-value pfxarg
) 0))
1769 (prefix-numeric-value pfxarg
) rst-toc-insert-max-level
))
1771 ;; Get the section tree for the current cursor point.
1773 (rst-section-tree-point
1774 (rst-section-tree (rst-find-all-decorations))))
1776 ;; Figure out initial indent.
1777 (initial-indent (make-string (current-column) ?
))
1778 (init-point (point)))
1780 (when (cddr sectree-pair
)
1781 (rst-toc-insert-node (cdr sectree-pair
) 0 initial-indent
"")
1783 ;; Fixup for the first line.
1784 (delete-region init-point
(+ init-point
(length initial-indent
)))
1786 ;; Delete the last newline added.
1787 (delete-backward-char 1)
1790 (defun rst-toc-insert-node (node level indent pfx
)
1791 "Insert tree node NODE in table-of-contents.
1792 Recursive function that does printing of the inserted toc. LEVEL
1793 is the depth level of the sections in the tree. INDENT bis the
1794 indentation string. PFX is the prefix numbering, that includes
1795 the alignment necessary for all the children of level to
1798 ;; Note: we do child numbering from the parent, so we start number the
1799 ;; children one level before we print them.
1800 (let ((do-print (> level
0))
1805 (unless (equal rst-toc-insert-style
'plain
)
1806 (insert pfx rst-toc-insert-number-separator
))
1807 (insert (or (caar node
) "[missing node]"))
1808 ;; Add properties to the text, even though in normal text mode it
1809 ;; won't be doing anything for now. Not sure that I want to change
1810 ;; mode stuff. At least the highlighting gives the idea that this
1811 ;; is generated automatically.
1812 (put-text-property b
(point) 'mouse-face
'highlight
)
1813 (put-text-property b
(point) 'rst-toc-target
(cadar node
))
1814 (put-text-property b
(point) 'keymap rst-toc-insert-click-keymap
)
1819 ;; Prepare indent for children.
1822 ((eq rst-toc-insert-style
'plain
)
1823 (concat indent
(make-string rst-toc-indent ?
)))
1825 ((eq rst-toc-insert-style
'fixed
)
1826 (concat indent
(make-string rst-toc-indent ?
)))
1828 ((eq rst-toc-insert-style
'aligned
)
1829 (concat indent
(make-string (+ (length pfx
) 2) ?
)))
1831 ((eq rst-toc-insert-style
'listed
)
1832 (concat (substring indent
0 -
3)
1833 (concat (make-string (+ (length pfx
) 2) ?
) " - ")))
1837 (if (or (eq rst-toc-insert-max-level nil
)
1838 (< level rst-toc-insert-max-level
))
1839 (let ((do-child-numbering (>= level
0))
1841 (if do-child-numbering
1843 ;; Add a separating dot if there is already a prefix
1844 (if (> (length pfx
) 0)
1845 (setq pfx
(concat (rst-rstrip pfx
) ".")))
1847 ;; Calculate the amount of space that the prefix will require
1850 (setq fmt
(format "%%-%dd"
1851 (1+ (floor (log10 (length
1855 (dolist (child (cdr node
))
1856 (rst-toc-insert-node child
1859 (if do-child-numbering
1860 (concat pfx
(format fmt count
)) pfx
))
1866 (defun rst-toc-insert-find-delete-contents ()
1867 "Find and deletes an existing comment after the first contents directive.
1868 Delete that region. Return t if found and the cursor is left after the comment."
1869 (goto-char (point-min))
1870 ;; We look for the following and the following only (in other words, if your
1871 ;; syntax differs, this won't work. If you would like a more flexible thing,
1872 ;; contact the author, I just can't imagine that this requirement is
1873 ;; unreasonable for now).
1875 ;; .. contents:: [...anything here...]
1882 (re-search-forward "^\\.\\. contents[ \t]*::\\(.*\\)\n\\.\\."
1886 ;; Look for the first line that starts at the first column.
1890 (< (point) (point-max))
1891 (or (and (looking-at "[ \t]+[^ \t]") (setq last-real
(point)) t
)
1892 (looking-at "[ \t]*$")))
1897 (goto-char last-real
)
1899 (delete-region beg
(point)))
1904 (defun rst-toc-update ()
1905 "Automatically find the contents section of a document and update.
1906 Updates the inserted TOC if present. You can use this in your
1907 file-write hook to always make it up-to-date automatically."
1911 (when (rst-toc-insert-find-delete-contents)
1915 ;; Somehow save-excursion does not really work well.
1917 ;; Note: always return nil, because this may be used as a hook.
1920 ;; Note: we cannot bind the TOC update on file write because it messes with
1921 ;; undo. If we disable undo, since it adds and removes characters, the
1922 ;; positions in the undo list are not making sense anymore. Dunno what to do
1923 ;; with this, it would be nice to update when saving.
1925 ;; (add-hook 'write-contents-hooks 'rst-toc-update-fun)
1926 ;; (defun rst-toc-update-fun ()
1927 ;; ;; Disable undo for the write file hook.
1928 ;; (let ((buffer-undo-list t)) (rst-toc-update) ))
1930 (defalias 'rst-toc-insert-update
'rst-toc-update
) ;; backwards compat.
1932 ;;------------------------------------------------------------------------------
1934 (defun rst-toc-node (node level
)
1935 "Recursive function that does insert NODE at LEVEL in the table-of-contents."
1939 ;; Insert line text.
1940 (insert (make-string (* rst-toc-indent
(1- level
)) ?
))
1941 (insert (or (caar node
) "[missing node]"))
1944 (put-text-property b
(point) 'mouse-face
'highlight
)
1946 ;; Add link on lines.
1947 (put-text-property b
(point) 'rst-toc-target
(cadar node
))
1952 (dolist (child (cdr node
))
1953 (rst-toc-node child
(1+ level
))))
1955 (defun rst-toc-count-lines (node target-node
)
1956 "Count the number of lines from NODE to the TARGET-NODE node.
1957 This recursive function returns a cons of the number of
1958 additional lines that have been counted for its node and children
1959 and 't if the node has been found."
1963 (if (eq node target-node
)
1965 (let ((child (cdr node
)))
1966 (while (and child
(not found
))
1967 (let ((cl (rst-toc-count-lines (car child
) target-node
)))
1968 (setq count
(+ count
(car cl
))
1970 child
(cdr child
))))))
1971 (cons count found
)))
1973 (defvar rst-toc-buffer-name
"*Table of Contents*"
1974 "Name of the Table of Contents buffer.")
1976 (defvar rst-toc-return-buffer nil
1977 "Buffer to which to return when leaving the TOC.")
1981 "Display a table-of-contents.
1982 Finds all the section titles and their decorations in the
1983 file, and displays a hierarchically-organized list of the
1984 titles, which is essentially a table-of-contents of the
1987 The Emacs buffer can be navigated, and selecting a section
1988 brings the cursor in that section."
1990 (let* ((curbuf (current-buffer))
1992 ;; Get the section tree
1993 (alldecos (rst-find-all-decorations))
1994 (sectree (rst-section-tree alldecos
))
1996 (our-node (cdr (rst-section-tree-point sectree
)))
1999 ;; Create a temporary buffer.
2000 (buf (get-buffer-create rst-toc-buffer-name
))
2003 (with-current-buffer buf
2004 (let ((inhibit-read-only t
))
2006 (delete-region (point-min) (point-max))
2007 (insert (format "Table of Contents: %s\n" (or (caar sectree
) "")))
2008 (put-text-property (point-min) (point)
2009 'face
(list '(background-color .
"gray")))
2010 (rst-toc-node sectree
0)
2012 ;; Count the lines to our found node.
2013 (let ((linefound (rst-toc-count-lines sectree our-node
)))
2014 (setq line
(if (cdr linefound
) (car linefound
) 0)))
2016 (display-buffer buf
)
2019 ;; Save the buffer to return to.
2020 (set (make-local-variable 'rst-toc-return-buffer
) curbuf
)
2022 ;; Move the cursor near the right section in the TOC.
2027 (defun rst-toc-mode-find-section ()
2028 "Get the section from text property at point."
2029 (let ((pos (get-text-property (point) 'rst-toc-target
)))
2031 (error "No section on this line"))
2032 (unless (buffer-live-p (marker-buffer pos
))
2033 (error "Buffer for this section was killed"))
2036 (defun rst-goto-section (&optional kill
)
2037 "Go to the section the current line describes."
2039 (let ((pos (rst-toc-mode-find-section)))
2041 (kill-buffer (get-buffer rst-toc-buffer-name
)))
2042 (pop-to-buffer (marker-buffer pos
))
2044 ;; FIXME: make the recentering conditional on scroll.
2047 (defun rst-toc-mode-goto-section ()
2048 "Go to the section the current line describes and kill the toc buffer."
2050 (rst-goto-section t
))
2052 (defun rst-toc-mode-mouse-goto (event)
2053 "In `rst-toc' mode, go to the occurrence whose line you click on.
2054 EVENT is the input event."
2057 (with-current-buffer (window-buffer (posn-window (event-end event
)))
2059 (goto-char (posn-point (event-end event
)))
2060 (setq pos
(rst-toc-mode-find-section))))
2061 (pop-to-buffer (marker-buffer pos
))
2065 (defun rst-toc-mode-mouse-goto-kill (event)
2066 "Same as `rst-toc-mode-mouse-goto', but kill TOC buffer as well."
2068 (call-interactively 'rst-toc-mode-mouse-goto event
)
2069 (kill-buffer (get-buffer rst-toc-buffer-name
)))
2071 (defun rst-toc-quit-window ()
2072 "Leave the current TOC buffer."
2075 (pop-to-buffer rst-toc-return-buffer
))
2077 (defvar rst-toc-mode-map
2078 (let ((map (make-sparse-keymap)))
2079 (define-key map
[mouse-1
] 'rst-toc-mode-mouse-goto-kill
)
2080 (define-key map
[mouse-2
] 'rst-toc-mode-mouse-goto
)
2081 (define-key map
"\C-m" 'rst-toc-mode-goto-section
)
2082 (define-key map
"f" 'rst-toc-mode-goto-section
)
2083 (define-key map
"q" 'rst-toc-quit-window
)
2084 (define-key map
"z" 'kill-this-buffer
)
2086 "Keymap for `rst-toc-mode'.")
2088 (put 'rst-toc-mode
'mode-class
'special
)
2090 ;; Could inherit from the new `special-mode'.
2091 (define-derived-mode rst-toc-mode nil
"ReST-TOC"
2092 "Major mode for output from \\[rst-toc], the table-of-contents for the document."
2093 (setq buffer-read-only t
))
2095 ;; Note: use occur-mode (replace.el) as a good example to complete missing
2099 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2101 ;; Section movement commands.
2104 (defun rst-forward-section (&optional offset
)
2105 "Skip to the next restructured text section title.
2106 OFFSET specifies how many titles to skip. Use a negative OFFSET to move
2107 backwards in the file (default is to use 1)."
2109 (let* (;; Default value for offset.
2110 (offset (or offset
1))
2112 ;; Get all the decorations in the file, with their line numbers.
2113 (alldecos (rst-find-all-decorations))
2115 ;; Get the current line.
2116 (curline (line-number-at-pos))
2122 ;; Find the index of the "next" decoration w.r.t. to the current line.
2123 (while (and cur
(< (caar cur
) curline
))
2124 (setq cur
(cdr cur
))
2126 ;; 'cur' is the decoration on or following the current line.
2128 (if (and (> offset
0) cur
(= (caar cur
) curline
))
2131 ;; Find the final index.
2132 (setq idx
(+ idx
(if (> offset
0) (- offset
1) offset
)))
2133 (setq cur
(nth idx alldecos
))
2135 ;; If the index is positive, goto the line, otherwise go to the buffer
2137 (if (and cur
(>= idx
0))
2138 (goto-line (car cur
))
2139 (if (> offset
0) (goto-char (point-max)) (goto-char (point-min))))
2142 (defun rst-backward-section ()
2143 "Like `rst-forward-section', except move back one title.
2144 With a prefix argument, move backward by a page."
2146 (rst-forward-section -
1))
2148 (defun rst-mark-section (&optional arg allow-extend
)
2149 "Select the section that point is currently in."
2150 ;; Cloned from mark-paragraph.
2151 (interactive "p\np")
2152 (unless arg
(setq arg
1))
2154 (error "Cannot mark zero sections"))
2155 (cond ((and allow-extend
2156 (or (and (eq last-command this-command
) (mark t
))
2157 (rst-portable-mark-active-p)))
2161 (rst-forward-section arg
)
2164 (rst-forward-section arg
)
2166 (rst-forward-section (- arg
)))))
2173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2174 ;; Functions to work on item lists (e.g. indent/dedent, enumerate), which are
2175 ;; always 2 or 3 characters apart horizontally with rest.
2177 ;; (FIXME: there is currently a bug that makes the region go away when we do that.)
2178 (defvar rst-shift-fill-region nil
2179 "If non-nil, automatically re-fill the region that is being shifted.")
2181 (defun rst-find-leftmost-column (beg end
)
2182 "Find the leftmost column in the region."
2183 (let ((mincol 1000))
2186 (while (< (point) end
)
2187 (back-to-indentation)
2188 (unless (looking-at "[ \t]*$")
2189 (setq mincol
(min mincol
(current-column))))
2195 ;; What we really need to do is compute all the possible alignment possibilities
2196 ;; and then select one.
2210 ;; Move backwards, accumulate the beginning positions, and also the second
2211 ;; positions, in case the line matches the bullet pattern, and then sort.
2213 (defun rst-compute-bullet-tabs (&optional pt
)
2214 "Build the list of possible horizontal alignment points.
2215 Search backwards from point (or point PT if specified) to
2216 build the list of possible horizontal alignment points that
2217 includes the beginning and contents of a restructuredtext
2218 bulleted or enumerated list item. Return a sorted list
2219 of (COLUMN-NUMBER . LINE) pairs."
2221 (when pt
(goto-char pt
))
2223 ;; We work our way backwards and towards the left.
2224 (let ((leftcol 100000) ;; Current column.
2225 (tablist nil
) ;; List of tab positions.
2228 ;; Start by skipping the current line.
2229 (beginning-of-line 0)
2231 ;; Search backwards for each line.
2232 (while (and (> (point) (point-min))
2235 ;; Skip empty lines.
2236 (unless (looking-at "^[ \t]*$")
2237 ;; Inspect the current non-empty line
2238 (back-to-indentation)
2240 ;; Skip lines that are beyond the current column (we want to move
2241 ;; towards the left).
2242 (let ((col (current-column)))
2243 (when (< col leftcol
)
2245 ;; Add the beginning of the line as a tabbing point.
2246 (unless (memq col
(mapcar 'car tablist
))
2247 (push (cons col
(point)) tablist
))
2249 ;; Look at the line to figure out if it is a bulleted or enumerate
2254 "\\(\\(?:[0-9a-zA-Z#]\\{1,3\\}[.):-]\\|[*+-]\\)[ \t]+\\)[^ \t\n]"
2256 (format "\\(%s%s+[ \t]+\\)[^ \t\n]"
2257 (regexp-quote (thing-at-point 'char
))
2258 (regexp-quote (thing-at-point 'char
)))
2261 ;; Add the column of the contained item.
2262 (let* ((matchlen (length (or (match-string 1) (match-string 2))))
2263 (newcol (+ col matchlen
)))
2264 (unless (or (>= newcol leftcol
)
2265 (memq (+ col matchlen
) (mapcar 'car tablist
)))
2266 (push (cons (+ col matchlen
) (+ (point) matchlen
))
2273 ;; Move backwards one line.
2274 (beginning-of-line 0))
2276 (sort tablist
(lambda (x y
) (<= (car x
) (car y
))))
2279 (defun rst-debug-print-tabs (tablist)
2280 "Insert a line and place special characters at the tab points in TABLIST."
2282 (insert (concat "\n" (make-string 1000 ?
) "\n"))
2283 (beginning-of-line 0)
2284 (dolist (col tablist
)
2286 (forward-char (car col
))
2291 (defun rst-debug-mark-found (tablist)
2292 "Insert a line and place special characters at the tab points in TABLIST."
2293 (dolist (col tablist
)
2295 (goto-char (cdr col
))
2299 (defvar rst-shift-basic-offset
2
2300 "Basic horizontal shift distance when there is no preceding alignment tabs.")
2302 (defun rst-shift-region-guts (find-next-fun offset-fun
)
2303 "(See `rst-shift-region-right' for a description)."
2304 (let* ((mbeg (set-marker (make-marker) (region-beginning)))
2305 (mend (set-marker (make-marker) (region-end)))
2306 (tabs (rst-compute-bullet-tabs mbeg
))
2307 (leftmostcol (rst-find-leftmost-column (region-beginning) (region-end)))
2309 ;; Add basic offset tabs at the end of the list. This is a better
2310 ;; implementation technique than hysteresis and a basic offset because it
2311 ;; insures that movement in both directions is consistently using the same
2312 ;; column positions. This makes it more predictable.
2315 (mapcar (lambda (x) (cons x nil
))
2317 (max-lisp-eval-depth 2000))
2322 (+ x rst-shift-basic-offset
))))))
2323 (addnum (or (caar (last tabs
)) 0))))
2327 ;;; (save-excursion (goto-char mbeg) (forward-char -1) (rst-debug-print-tabs tabs))))
2329 ;;; (save-excursion (rst-debug-mark-found tabs))
2331 ;; Apply the indent.
2335 ;; Find the next tab after the leftmost columnt.
2336 (let ((tab (funcall find-next-fun tabs leftmostcol
)))
2341 (message "Aligned on '%s'"
2343 (goto-char (cdar tab
))
2344 (buffer-substring-no-properties
2345 (line-beginning-position)
2346 (line-end-position))))
2348 (- (caar tab
) leftmostcol
)) ;; Num chars.
2350 ;; Otherwise use the basic offset
2351 (funcall offset-fun rst-shift-basic-offset
)
2354 ;; Optionally reindent.
2355 (when rst-shift-fill-region
2356 (fill-region mbeg mend
))
2359 (defun rst-shift-region-right (pfxarg)
2360 "Indent region ridigly, by a few characters to the right.
2361 This function first computes all possible alignment columns by
2362 inspecting the lines preceding the region for bulleted or
2363 enumerated list items. If the leftmost column is beyond the
2364 preceding lines, the region is moved to the right by
2365 `rst-shift-basic-offset'. With a prefix argument, do not
2366 automatically fill the region."
2368 (let ((rst-shift-fill-region
2369 (if (not pfxarg
) rst-shift-fill-region
)))
2370 (rst-shift-region-guts (lambda (tabs leftmostcol
)
2372 (while (and cur
(<= (caar cur
) leftmostcol
))
2373 (setq cur
(cdr cur
)))
2378 (defun rst-shift-region-left (pfxarg)
2379 "Like `rst-shift-region-right', except we move to the left.
2380 Also, if invoked with a negative prefix arg, the entire
2381 indentation is removed, up to the leftmost character in the
2382 region, and automatic filling is disabled."
2384 (let ((mbeg (set-marker (make-marker) (region-beginning)))
2385 (mend (set-marker (make-marker) (region-end)))
2386 (leftmostcol (rst-find-leftmost-column
2387 (region-beginning) (region-end)))
2388 (rst-shift-fill-region
2389 (if (not pfxarg
) rst-shift-fill-region
)))
2391 (when (> leftmostcol
0)
2392 (if (and pfxarg
(< (prefix-numeric-value pfxarg
) 0))
2394 (indent-rigidly (region-beginning) (region-end) (- leftmostcol
))
2395 (when rst-shift-fill-region
2396 (fill-region mbeg mend
))
2398 (rst-shift-region-guts (lambda (tabs leftmostcol
)
2399 (let ((cur (reverse tabs
)))
2400 (while (and cur
(>= (caar cur
) leftmostcol
))
2401 (setq cur
(cdr cur
)))
2407 (defmacro rst-iterate-leftmost-paragraphs
2408 (beg end first-only body-consequent body-alternative
)
2409 "FIXME This definition is old and deprecated / we need to move
2410 to the newer version below:
2412 Call FUN at the beginning of each line, with an argument that
2413 specifies whether we are at the first line of a paragraph that
2414 starts at the leftmost column of the given region BEG and END.
2415 Set FIRST-ONLY to true if you want to callback on the first line
2416 of each paragraph only."
2418 (let ((leftcol (rst-find-leftmost-column ,beg
,end
))
2419 (endm (set-marker (make-marker) ,end
))
2422 (do* (;; Iterate lines
2423 (l (progn (goto-char ,beg
) (back-to-indentation))
2424 (progn (forward-line 1) (back-to-indentation)))
2426 (previous nil valid
)
2428 (curcol (current-column)
2431 (valid (and (= curcol leftcol
)
2432 (not (looking-at "[ \t]*$")))
2433 (and (= curcol leftcol
)
2434 (not (looking-at "[ \t]*$"))))
2439 (and valid
(not previous
))
2447 (defmacro rst-iterate-leftmost-paragraphs-2
(spec &rest body
)
2448 "Evaluate BODY for each line in region defined by BEG END.
2449 LEFTMOST is set to true if the line is one of the leftmost of the
2450 entire paragraph. PARABEGIN is set to true if the line is the
2451 first of a paragraph."
2452 (declare (indent 1) (debug (sexp body
)))
2454 (beg end parabegin leftmost isleftmost isempty
) spec
2457 (let ((,leftmost
(rst-find-leftmost-column ,beg
,end
))
2458 (endm (set-marker (make-marker) ,end
))
2461 (do* (;; Iterate lines
2462 (l (progn (goto-char ,beg
) (back-to-indentation))
2463 (progn (forward-line 1) (back-to-indentation)))
2465 (empty-line-previous nil
,isempty
)
2467 (,isempty
(looking-at "[ \t]*$")
2468 (looking-at "[ \t]*$"))
2470 (,parabegin
(not ,isempty
)
2471 (and empty-line-previous
2474 (,isleftmost
(and (not ,isempty
)
2475 (= (current-column) ,leftmost
))
2477 (= (current-column) ,leftmost
)))
2486 ;;------------------------------------------------------------------------------
2488 ;; FIXME: these next functions should become part of a larger effort to redo the
2489 ;; bullets in bulletted lists. The enumerate would just be one of the possible
2492 ;; FIXME: TODO we need to do the enumeration removal as well.
2494 (defun rst-enumerate-region (beg end
)
2495 "Add enumeration to all the leftmost paragraphs in the given region.
2496 The region is specified between BEG and END. With prefix argument,
2497 do all lines instead of just paragraphs."
2500 (last-insert-len nil
))
2501 (rst-iterate-leftmost-paragraphs
2502 beg end
(not current-prefix-arg
)
2503 (let ((ins-string (format "%d. " (incf count
))))
2504 (setq last-insert-len
(length ins-string
))
2505 (insert ins-string
))
2506 (insert (make-string last-insert-len ?\
))
2509 (defun rst-bullet-list-region (beg end
)
2510 "Add bullets to all the leftmost paragraphs in the given region.
2511 The region is specified between BEG and END. With prefix argument,
2512 do all lines instead of just paragraphs."
2514 (rst-iterate-leftmost-paragraphs
2515 beg end
(not current-prefix-arg
)
2521 ;; FIXME: there are some problems left with the following function
2524 ;; * It does not deal with a varying number of digits appropriately
2525 ;; * It does not deal with multiple levels independently, and it should.
2527 ;; I suppose it does 90% of the job for now.
2529 (defun rst-convert-bullets-to-enumeration (beg end
)
2530 "Convert all the bulleted items and enumerated items in the
2531 region to enumerated lists, renumbering as necessary."
2533 (let* (;; Find items and convert the positions to markers.
2536 (cons (let ((m (make-marker)))
2537 (set-marker m
(car x
))
2540 (rst-find-pfx-in-region beg end rst-re-items
)))
2546 (looking-at rst-re-items
)
2547 (replace-match (format "%d. " count
) nil nil nil
1)
2554 ;;------------------------------------------------------------------------------
2556 (defun rst-line-block-region (rbeg rend
&optional pfxarg
)
2557 "Toggle line block prefixes for a region.
2558 With prefix argument set the empty lines too."
2559 (interactive "r\nP")
2560 (let ((comment-start "| ")
2562 (comment-start-skip "| ")
2563 (comment-style 'indent
)
2564 (force (not (not pfxarg
))))
2565 (rst-iterate-leftmost-paragraphs-2
2566 (rbeg rend parbegin leftmost isleft isempty
)
2567 (when (or force
(not isempty
))
2568 (move-to-column leftmost force
)
2569 (delete-region (point) (+ (point) (- (current-indentation) leftmost
)))
2574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2576 (require 'font-lock
)
2578 (defgroup rst-faces nil
"Faces used in Rst Mode"
2583 (defcustom rst-block-face
'font-lock-keyword-face
2584 "All syntax marking up a special block."
2588 (defcustom rst-external-face
'font-lock-type-face
2589 "Field names and interpreted text."
2593 (defcustom rst-definition-face
'font-lock-function-name-face
2594 "All other defining constructs."
2598 (defcustom rst-directive-face
2599 ;; XEmacs compatibility
2600 (if (boundp 'font-lock-builtin-face
)
2601 'font-lock-builtin-face
2602 'font-lock-preprocessor-face
)
2603 "Directives and roles."
2607 (defcustom rst-comment-face
'font-lock-comment-face
2612 (defcustom rst-emphasis1-face
2613 ;; XEmacs compatibility
2621 (defcustom rst-emphasis2-face
2622 ;; XEmacs compatibility
2630 (defcustom rst-literal-face
'font-lock-string-face
2635 (defcustom rst-reference-face
'font-lock-variable-name-face
2636 "References to a definition."
2640 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2642 (defgroup rst-faces-defaults nil
2643 "Values used to generate default faces for section titles on all levels.
2644 Tweak these if you are content with how section title faces are built in
2645 general but you do not like the details."
2649 (defun rst-set-level-default (sym val
)
2650 "Set custom var SYM affecting section title text face and recompute the faces."
2651 (custom-set-default sym val
)
2652 ;; Also defines the faces initially when all values are available
2653 (and (boundp 'rst-level-face-max
)
2654 (boundp 'rst-level-face-format-light
)
2655 (boundp 'rst-level-face-base-color
)
2656 (boundp 'rst-level-face-step-light
)
2657 (boundp 'rst-level-face-base-light
)
2658 (fboundp 'rst-define-level-faces
)
2659 (rst-define-level-faces)))
2661 ;; Faces for displaying items on several levels; these definitions define
2662 ;; different shades of grey where the lightest one (i.e. least contrasting) is
2664 (defcustom rst-level-face-max
6
2665 "Maximum depth of levels for which section title faces are defined."
2666 :group
'rst-faces-defaults
2668 :set
'rst-set-level-default
)
2669 (defcustom rst-level-face-base-color
"grey"
2670 "The base name of the color to be used for creating background colors in
2671 ection title faces for all levels."
2672 :group
'rst-faces-defaults
2674 :set
'rst-set-level-default
)
2675 (defcustom rst-level-face-base-light
2676 (if (eq frame-background-mode
'dark
)
2679 "The lightness factor for the base color. This value is used for level 1.
2680 The default depends on whether the value of `frame-background-mode' is
2682 :group
'rst-faces-defaults
2684 :set
'rst-set-level-default
)
2685 (defcustom rst-level-face-format-light
"%2d"
2686 "The format for the lightness factor appended to the base name of the color.
2687 This value is expanded by `format' with an integer."
2688 :group
'rst-faces-defaults
2690 :set
'rst-set-level-default
)
2691 (defcustom rst-level-face-step-light
2692 (if (eq frame-background-mode
'dark
)
2695 "The step width to use for the next color.
2698 `rst-level-face-base-light'
2699 + (`rst-level-face-max' - 1) * `rst-level-face-step-light'
2701 must result in a color level which appended to `rst-level-face-base-color'
2702 using `rst-level-face-format-light' results in a valid color such as `grey50'.
2703 This color is used as background for section title text on level
2704 `rst-level-face-max'."
2705 :group
'rst-faces-defaults
2707 :set
'rst-set-level-default
)
2709 (defcustom rst-adornment-faces-alist
2710 (let ((alist '((t . font-lock-keyword-face
)
2711 (nil . font-lock-keyword-face
)))
2713 (while (<= i rst-level-face-max
)
2714 (nconc alist
(list (cons i
(intern (format "rst-level-%d-face" i
)))))
2717 "Faces for the various adornment types.
2718 Key is a number (for the section title text of that level),
2719 t (for transitions) or nil (for section title adornment).
2720 If you generally do not like how section title text faces are
2721 set up tweak here. If the general idea is ok for you but you do not like the
2722 details check the Rst Faces Defaults group."
2729 "Section level (may not be bigger than `rst-level-face-max')")
2730 (boolean :tag
"transitions (on) / section title adornment (off)"))
2732 :set-after
'(rst-level-face-max))
2734 (defun rst-define-level-faces ()
2735 "Define the faces for the section title text faces from the values."
2736 ;; All variables used here must be checked in `rst-set-level-default'
2738 (while (<= i rst-level-face-max
)
2739 (let ((sym (intern (format "rst-level-%d-face" i
)))
2740 (doc (format "Face for showing section title text at level %d" i
))
2741 (col (format (concat "%s" rst-level-face-format-light
)
2742 rst-level-face-base-color
2743 (+ (* (1- i
) rst-level-face-step-light
)
2744 rst-level-face-base-light
))))
2745 (make-empty-face sym
)
2746 (set-face-doc-string sym doc
)
2747 (set-face-background sym col
)
2751 (rst-define-level-faces)
2754 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2757 (defvar rst-use-char-classes
2758 (string-match "[[:alpha:]]" "b")
2759 "Non-nil if we can use the character classes in our regexps.")
2761 (defun rst-font-lock-keywords-function ()
2762 "Return keywords to highlight in rst mode according to current settings."
2763 ;; The reST-links in the comments below all relate to sections in
2764 ;; http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
2765 (let* ( ;; This gets big - so let's define some abbreviations
2766 ;; horizontal white space
2768 ;; beginning of line with possible indentation
2769 (re-bol (concat "^" re-hws
"*"))
2770 ;; Separates block lead-ins from their content
2771 (re-blksep1 (concat "\\(" re-hws
"+\\|$\\)"))
2772 ;; explicit markup tag
2774 ;; explicit markup start
2775 (re-ems (concat re-emt re-hws
"+"))
2776 ;; inline markup prefix
2777 (re-imp1 (concat "\\(^\\|" re-hws
"\\|[-'\"([{</:]\\)"))
2778 ;; inline markup suffix
2779 (re-ims1 (concat "\\(" re-hws
"\\|[]-'\")}>/:.,;!?\\]\\|$\\)"))
2781 (re-sym1 "\\(\\sw\\|\\s_\\)")
2782 ;; inline markup content begin
2783 (re-imbeg2 "\\(\\S \\|\\S \\([^")
2785 ;; There seems to be a bug leading to error "Stack overflow in regexp
2786 ;; matcher" when "|" or "\\*" are the characters searched for
2788 (if (< emacs-major-version
21)
2791 ;; inline markup content end
2792 (re-imend (concat re-imendbeg
"\\)*[^\t \\\\]\\)"))
2793 ;; inline markup content without asterisk
2794 (re-ima2 (concat re-imbeg2
"*" re-imend
))
2795 ;; inline markup content without backquote
2796 (re-imb2 (concat re-imbeg2
"`" re-imend
))
2797 ;; inline markup content without vertical bar
2798 (re-imv2 (concat re-imbeg2
"|" re-imend
))
2799 ;; Supported URI schemes
2800 (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\\)")
2801 ;; Line starting with adornment and optional whitespace; complete
2802 ;; adornment is in (match-string 1); there must be at least 3
2803 ;; characters because otherwise explicit markup start would be
2805 (re-ado2 (concat "^\\(\\(["
2806 (if rst-use-char-classes
2807 "^[:word:][:space:][:cntrl:]" "^\\w \t\x00-\x1F")
2808 "]\\)\\2\\2+\\)" re-hws
"*$"))
2811 ;; FIXME: Block markup is not recognized in blocks after explicit markup
2814 ;; Simple `Body Elements`_
2817 (concat re-bol
"\\([-*+]" re-blksep1
"\\)")
2819 ;; `Enumerated Lists`_
2821 (concat re-bol
"\\((?\\(#\\|[0-9]+\\|[A-Za-z]\\|[IVXLCMivxlcm]+\\)[.)]"
2824 ;; `Definition Lists`_ FIXME: missing
2827 (concat re-bol
"\\(:[^:\n]+:\\)" re-blksep1
)
2828 1 rst-external-face
)
2831 (concat re-bol
"\\(\\(\\(\\([-+/]\\|--\\)\\sw\\(-\\|\\sw\\)*"
2832 "\\([ =]\\S +\\)?\\)\\(,[\t ]\\)?\\)+\\)\\($\\|[\t ]\\{2\\}\\)")
2835 ;; `Tables`_ FIXME: missing
2837 ;; All the `Explicit Markup Blocks`_
2838 ;; `Footnotes`_ / `Citations`_
2840 (concat re-bol
"\\(" re-ems
"\\[[^[\n]+\\]\\)" re-blksep1
)
2841 1 rst-definition-face
)
2842 ;; `Directives`_ / `Substitution Definitions`_
2844 (concat re-bol
"\\(" re-ems
"\\)\\(\\(|[^|\n]+|[\t ]+\\)?\\)\\("
2845 re-sym1
"+::\\)" re-blksep1
)
2846 (list 1 rst-directive-face
)
2847 (list 2 rst-definition-face
)
2848 (list 4 rst-directive-face
))
2849 ;; `Hyperlink Targets`_
2851 (concat re-bol
"\\(" re-ems
"_\\([^:\\`\n]\\|\\\\.\\|`[^`\n]+`\\)+:\\)"
2853 1 rst-definition-face
)
2855 (concat re-bol
"\\(__\\)" re-blksep1
)
2856 1 rst-definition-face
)
2858 ;; All `Inline Markup`_
2859 ;; FIXME: Condition 5 preventing fontification of e.g. "*" not implemented
2860 ;; `Strong Emphasis`_
2862 (concat re-imp1
"\\(\\*\\*" re-ima2
"\\*\\*\\)" re-ims1
)
2863 2 rst-emphasis2-face
)
2866 (concat re-imp1
"\\(\\*" re-ima2
"\\*\\)" re-ims1
)
2867 2 rst-emphasis1-face
)
2868 ;; `Inline Literals`_
2870 (concat re-imp1
"\\(``" re-imb2
"``\\)" re-ims1
)
2872 ;; `Inline Internal Targets`_
2874 (concat re-imp1
"\\(_`" re-imb2
"`\\)" re-ims1
)
2875 2 rst-definition-face
)
2876 ;; `Hyperlink References`_
2877 ;; FIXME: `Embedded URIs`_ not considered
2879 (concat re-imp1
"\\(\\(`" re-imb2
"`\\|\\(\\sw\\(\\sw\\|-\\)+\\sw\\)\\)__?\\)" re-ims1
)
2880 2 rst-reference-face
)
2881 ;; `Interpreted Text`_
2883 (concat re-imp1
"\\(\\(:" re-sym1
"+:\\)?\\)\\(`" re-imb2
"`\\)\\(\\(:"
2884 re-sym1
"+:\\)?\\)" re-ims1
)
2885 (list 2 rst-directive-face
)
2886 (list 5 rst-external-face
)
2887 (list 8 rst-directive-face
))
2888 ;; `Footnote References`_ / `Citation References`_
2890 (concat re-imp1
"\\(\\[[^]]+\\]_\\)" re-ims1
)
2891 2 rst-reference-face
)
2892 ;; `Substitution References`_
2894 (concat re-imp1
"\\(|" re-imv2
"|\\)" re-ims1
)
2895 2 rst-reference-face
)
2896 ;; `Standalone Hyperlinks`_
2898 ;; FIXME: This takes it easy by using a whitespace as delimiter
2899 (concat re-imp1
"\\(" re-uris1
":\\S +\\)" re-ims1
)
2900 2 rst-definition-face
)
2902 (concat re-imp1
"\\(" re-sym1
"+@" re-sym1
"+\\)" re-ims1
)
2903 2 rst-definition-face
)
2905 ;; Do all block fontification as late as possible so 'append works
2907 ;; Sections_ / Transitions_
2911 (if (not rst-mode-lazy
)
2912 (list 1 rst-block-face
)
2914 (list 'rst-font-lock-handle-adornment
2916 (setq rst-font-lock-adornment-point
(match-end 1))
2919 (list 1 '(cdr (assoc nil rst-adornment-faces-alist
))
2921 (list 2 '(cdr (assoc rst-font-lock-level
2922 rst-adornment-faces-alist
))
2924 (list 3 '(cdr (assoc nil rst-adornment-faces-alist
))
2930 (concat re-bol
"\\(" re-ems
"\\)\[^[|_]\\([^:\n]\\|:\\([^:\n]\\|$\\)\\)*$")
2932 (list 1 rst-comment-face
))
2935 (list 'rst-font-lock-find-unindented-line
2937 (setq rst-font-lock-indentation-point
(match-end 1))
2940 (list 0 rst-comment-face
'append
)))))
2943 (concat re-bol
"\\(" re-emt
"\\)\\(\\s *\\)$")
2944 (list 1 rst-comment-face
)
2945 (list 2 rst-comment-face
))
2948 (list 'rst-font-lock-find-unindented-line
2950 (setq rst-font-lock-indentation-point
'next
)
2953 (list 0 rst-comment-face
'append
)))))
2955 ;; `Literal Blocks`_
2958 (concat re-bol
"\\(\\([^.\n]\\|\\.[^.\n]\\).*\\)?\\(::\\)$")
2959 (list 3 rst-block-face
))
2962 (list 'rst-font-lock-find-unindented-line
2964 (setq rst-font-lock-indentation-point t
)
2967 (list 0 rst-literal-face
'append
)))))
2969 ;; `Doctest Blocks`_
2972 (concat re-bol
"\\(>>>\\|\\.\\.\\.\\)\\(.+\\)")
2973 (list 1 rst-block-face
)
2974 (list 2 rst-literal-face
)))
2979 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2982 (defun rst-forward-indented-block (&optional column limit
)
2983 "Move forward across one indented block.
2984 Find the next non-empty line which is not indented at least to COLUMN (defaults
2985 to the column of the point). Moves point to first character of this line or the
2986 first empty line immediately before it and returns that position. If there is
2987 no such line before LIMIT (defaults to the end of the buffer) returns nil and
2988 point is not moved."
2990 (let ((clm (or column
(current-column)))
2994 (setq limit
(point-max)))
2996 (while (and (not fnd
) (< (point) limit
))
2998 (when (< (point) limit
)
3000 (if (looking-at "\\s *$")
3001 (setq cand
(or cand beg
)) ; An empty line is a candidate
3002 (move-to-column clm
)
3003 ;; FIXME: No indentation [(zerop clm)] must be handled in some
3004 ;; useful way - though it is not clear what this should mean at all
3006 "^\\s *$" (buffer-substring-no-properties beg
(point)))
3007 (setq cand nil
) ; An indented line resets a candidate
3008 (setq fnd
(or cand beg
)))))))
3009 (goto-char (or fnd start
))
3012 ;; Stores the point where the current indentation ends if a number. If `next'
3013 ;; indicates `rst-font-lock-find-unindented-line' shall take the indentation
3014 ;; from the next line if this is not empty. If non-nil indicates
3015 ;; `rst-font-lock-find-unindented-line' shall take the indentation from the
3016 ;; next non-empty line. Also used as a trigger for
3017 ;; `rst-font-lock-find-unindented-line'.
3018 (defvar rst-font-lock-indentation-point nil
)
3020 (defun rst-font-lock-find-unindented-line (limit)
3021 (let* ((ind-pnt rst-font-lock-indentation-point
)
3023 ;; May run only once - enforce this
3024 (setq rst-font-lock-indentation-point nil
)
3025 (when (and ind-pnt
(not (numberp ind-pnt
)))
3026 ;; Find indentation point in next line if any
3030 (if (eq ind-pnt
'next
)
3031 (when (and (zerop (forward-line 1)) (< (point) limit
))
3032 (setq beg-pnt
(point))
3033 (when (not (looking-at "\\s *$"))
3034 (looking-at "\\s *")
3036 (while (and (zerop (forward-line 1)) (< (point) limit
)
3037 (looking-at "\\s *$")))
3038 (when (< (point) limit
)
3039 (setq beg-pnt
(point))
3040 (looking-at "\\s *")
3044 ;; Always succeeds because the limit set by PRE-MATCH-FORM is the
3045 ;; ultimate point to find
3046 (goto-char (or (rst-forward-indented-block nil limit
) limit
))
3048 ;; Include subsequent empty lines in the font-lock block,
3049 ;; in case the user subsequently changes the indentation of the next
3050 ;; non-empty line to move it into the indented element.
3051 (skip-chars-forward " \t\n")
3052 (put-text-property beg-pnt
(point) 'font-lock-multiline t
))
3053 (set-match-data (list beg-pnt
(point)))
3056 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3059 (defvar rst-font-lock-adornment-point nil
3060 "Stores the point where the current adornment ends. Also used as a trigger
3061 for `rst-font-lock-handle-adornment'.")
3063 ;; Here `rst-font-lock-handle-adornment' stores the section level of the
3064 ;; current adornment or t for a transition.
3065 (defvar rst-font-lock-level nil
)
3067 ;; FIXME: It would be good if this could be used to markup section titles of
3068 ;; given level with a special key; it would be even better to be able to
3069 ;; customize this so it can be used for a generally available personal style
3071 ;; FIXME: There should be some way to reset and reload this variable - probably
3074 ;; FIXME: Some support for `outline-mode' would be nice which should be based
3075 ;; on this information
3076 (defvar rst-adornment-level-alist nil
3077 "Associates adornments with section levels.
3078 The key is a two character string. The first character is the adornment
3079 character. The second character distinguishes underline section titles (`u')
3080 from overline/underline section titles (`o'). The value is the section level.
3082 This is made buffer local on start and adornments found during font lock are
3085 ;; Returns section level for adornment key KEY. Adds new section level if KEY
3086 ;; is not found and ADD. If KEY is not a string it is simply returned.
3087 (defun rst-adornment-level (key &optional add
)
3088 (let ((fnd (assoc key rst-adornment-level-alist
))
3091 ((not (stringp key
))
3096 (while (rassoc new rst-adornment-level-alist
)
3097 (setq new
(1+ new
)))
3098 (setq rst-adornment-level-alist
3099 (append rst-adornment-level-alist
(list (cons key new
))))
3102 ;; Classifies adornment for section titles and transitions. ADORNMENT is the
3103 ;; complete adornment string as found in the buffer. END is the point after the
3104 ;; last character of ADORNMENT. For overline section adornment LIMIT limits the
3105 ;; search for the matching underline. Returns a list. The first entry is t for
3106 ;; a transition, or a key string for `rst-adornment-level' for a section title.
3107 ;; The following eight values forming four match groups as can be used for
3108 ;; `set-match-data'. First match group contains the maximum points of the whole
3109 ;; construct. Second and last match group matched pure section title adornment
3110 ;; while third match group matched the section title text or the transition.
3111 ;; Each group but the first may or may not exist.
3112 (defun rst-classify-adornment (adornment end limit
)
3116 (let ((ado-ch (aref adornment
0))
3117 (ado-re (regexp-quote adornment
))
3124 (or (not (zerop (forward-line 1)))
3125 (looking-at "\\s *$"))))
3128 (or (not (zerop (forward-line -
1)))
3129 (looking-at "\\s *$"))))
3130 key beg-ovr end-ovr beg-txt end-txt beg-und end-und
)
3132 ((and nxt-emp prv-emp
)
3135 (setq beg-txt beg-pnt
)
3136 (setq end-txt end-pnt
))
3139 (setq key
(concat (list ado-ch
) "o"))
3140 (setq beg-ovr beg-pnt
)
3141 (setq end-ovr end-pnt
)
3143 (setq beg-txt
(point))
3144 (while (and (< (point) limit
) (not end-txt
))
3145 (if (looking-at "\\s *$")
3146 ;; No underline found
3147 (setq end-txt
(1- (point)))
3148 (when (looking-at (concat "\\(" ado-re
"\\)\\s *$"))
3149 (setq end-und
(match-end 1))
3150 (setq beg-und
(point))
3151 (setq end-txt
(1- beg-und
))))
3155 (setq key
(concat (list ado-ch
) "u"))
3156 (setq beg-und beg-pnt
)
3157 (setq end-und end-pnt
)
3158 (setq end-txt
(1- beg-und
))
3159 (setq beg-txt
(progn
3160 (if (re-search-backward "^\\s *$" 1 'move
)
3164 (or beg-ovr beg-txt beg-und
)
3165 (or end-und end-txt end-und
)
3166 beg-ovr end-ovr beg-txt end-txt beg-und end-und
)))))
3168 ;; Handles adornments for font-locking section titles and transitions. Returns
3169 ;; three match groups. First and last match group matched pure overline /
3170 ;; underline adornment while second group matched section title text. Each
3171 ;; group may not exist.
3172 (defun rst-font-lock-handle-adornment (limit)
3173 (let ((ado-pnt rst-font-lock-adornment-point
))
3174 ;; May run only once - enforce this
3175 (setq rst-font-lock-adornment-point nil
)
3177 (let* ((ado (rst-classify-adornment (match-string-no-properties 1)
3181 (setq rst-font-lock-level
(rst-adornment-level key t
))
3182 (goto-char (nth 1 mtc
))
3183 (put-text-property (nth 0 mtc
) (nth 1 mtc
) 'font-lock-multiline t
)
3184 (set-match-data mtc
)
3190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3191 ;; Support for conversion from within Emacs
3193 (defgroup rst-compile nil
3194 "Settings for support of conversion of reStructuredText
3195 document with \\[rst-compile]."
3199 (defvar rst-compile-toolsets
3200 '((html .
("rst2html.py" ".html" nil
))
3201 (latex .
("rst2latex.py" ".tex" nil
))
3202 (newlatex .
("rst2newlatex.py" ".tex" nil
))
3203 (pseudoxml .
("rst2pseudoxml.py" ".xml" nil
))
3204 (xml .
("rst2xml.py" ".xml" nil
)))
3205 "Table describing the command to use for each toolset.
3206 An association list of the toolset to a list of the (command to use,
3207 extension of produced filename, options to the tool (nil or a
3208 string)) to be used for converting the document.")
3210 ;; Note for Python programmers not familiar with association lists: you can set
3211 ;; values in an alists like this, e.g. :
3212 ;; (setcdr (assq 'html rst-compile-toolsets)
3213 ;; '("rst2html.py" ".htm" "--stylesheet=/docutils.css"))
3216 (defvar rst-compile-primary-toolset
'html
3217 "The default toolset for `rst-compile'.")
3219 (defvar rst-compile-secondary-toolset
'latex
3220 "The default toolset for `rst-compile' with a prefix argument.")
3222 (defun rst-compile-find-conf ()
3223 "Look for the configuration file in the parents of the current path."
3225 (let ((file-name "docutils.conf")
3226 (buffer-file (buffer-file-name)))
3227 ;; Move up in the dir hierarchy till we find a change log file.
3228 (let* ((dir (file-name-directory buffer-file
))
3230 (while (and (or (not (string= dir prevdir
))
3233 (not (file-exists-p (concat dir file-name
))))
3234 ;; Move up to the parent dir and try again.
3236 (setq dir
(expand-file-name (file-name-directory
3237 (directory-file-name
3238 (file-name-directory dir
)))))
3240 (or (and dir
(concat dir file-name
)) nil
)
3246 (defun rst-compile (&optional pfxarg
)
3247 "Compile command to convert reST document into some output file.
3248 Attempts to find configuration file, if it can, overrides the
3249 options. There are two commands to choose from, with a prefix
3250 argument, select the alternative toolset."
3252 ;; Note: maybe we want to check if there is a Makefile too and not do anything
3253 ;; if that is the case. I dunno.
3254 (let* ((toolset (cdr (assq (if pfxarg
3255 rst-compile-secondary-toolset
3256 rst-compile-primary-toolset
)
3257 rst-compile-toolsets
)))
3258 (command (car toolset
))
3259 (extension (cadr toolset
))
3260 (options (caddr toolset
))
3261 (conffile (rst-compile-find-conf))
3262 (bufname (file-name-nondirectory buffer-file-name
))
3263 (outname (file-name-sans-extension bufname
)))
3265 ;; Set compile-command before invocation of compile.
3266 (set (make-local-variable 'compile-command
)
3267 (mapconcat 'identity
3271 (concat "--config=\"" conffile
"\"")
3274 (concat outname extension
))
3277 ;; Invoke the compile command.
3278 (if (or compilation-read-command current-prefix-arg
)
3279 (call-interactively 'compile
)
3280 (compile compile-command
))
3283 (defun rst-compile-alt-toolset ()
3284 "Compile command with the alternative toolset."
3288 (defun rst-compile-pseudo-region ()
3289 "Show the pseudo-XML rendering of the current active region,
3290 or of the entire buffer, if the region is not selected."
3292 (with-output-to-temp-buffer "*pseudoxml*"
3293 (shell-command-on-region
3294 (if mark-active
(region-beginning) (point-min))
3295 (if mark-active
(region-end) (point-max))
3299 (defvar rst-pdf-program
"xpdf"
3300 "Program used to preview PDF files.")
3302 (defun rst-compile-pdf-preview ()
3303 "Convert the document to a PDF file and launch a preview program."
3305 (let* ((tmp-filename "/tmp/out.pdf")
3306 (command (format "rst2pdf.py %s %s && %s %s"
3307 buffer-file-name tmp-filename
3308 rst-pdf-program tmp-filename
)))
3309 (start-process-shell-command "rst-pdf-preview" nil command
)
3310 ;; Note: you could also use (compile command) to view the compilation
3314 (defvar rst-slides-program
"firefox"
3315 "Program used to preview S5 slides.")
3317 (defun rst-compile-slides-preview ()
3318 "Convert the document to an S5 slide presentation and launch a preview program."
3320 (let* ((tmp-filename "/tmp/slides.html")
3321 (command (format "rst2s5.py %s %s && %s %s"
3322 buffer-file-name tmp-filename
3323 rst-slides-program tmp-filename
)))
3324 (start-process-shell-command "rst-slides-preview" nil command
)
3325 ;; Note: you could also use (compile command) to view the compilation
3331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3333 ;; Generic text functions that are more convenient than the defaults.
3336 (defun rst-replace-lines (fromchar tochar
)
3337 "Replace flush-left lines, consisting of multiple FROMCHAR characters,
3338 with equal-length lines of TOCHAR."
3340 cSearch for flush-left lines of char:
3341 cand replace with char: ")
3343 (let ((searchre (concat "^" (regexp-quote (string fromchar
)) "+\\( *\\)$"))
3345 (while (search-forward-regexp searchre nil t
)
3346 (setq found
(1+ found
))
3347 (goto-char (match-beginning 1))
3348 (let ((width (current-column)))
3349 (rst-delete-entire-line)
3350 (insert-char tochar width
)))
3351 (message (format "%d lines replaced." found
)))))
3353 (defun rst-join-paragraph ()
3354 "Join lines in current paragraph into one line, removing end-of-lines."
3356 (let ((fill-column 65000)) ; some big number
3357 (call-interactively 'fill-paragraph
)))
3359 (defun rst-force-fill-paragraph ()
3360 "Fill paragraph at point, first joining the paragraph's lines into one.
3361 This is useful for filling list item paragraphs."
3363 (rst-join-paragraph)
3364 (fill-paragraph nil
))
3367 ;; Generic character repeater function.
3368 ;; For sections, better to use the specialized function above, but this can
3369 ;; be useful for creating separators.
3370 (defun rst-repeat-last-character (&optional tofill
)
3371 "Fills the current line up to the length of the preceding line (if not
3372 empty), using the last character on the current line. If the preceding line is
3373 empty, we use the `fill-column'.
3375 If a prefix argument is provided, use the next line rather than the preceding
3378 If the current line is longer than the desired length, shave the characters off
3379 the current line to fit the desired length.
3381 As an added convenience, if the command is repeated immediately, the alternative
3382 column is used (fill-column vs. end of previous/next line)."
3384 (let* ((curcol (current-column))
3385 (curline (+ (count-lines (point-min) (point))
3386 (if (eq curcol
0) 1 0)))
3387 (lbp (line-beginning-position 0))
3388 (prevcol (if (and (= curline
1) (not current-prefix-arg
))
3391 (forward-line (if current-prefix-arg
1 -
1))
3393 (skip-chars-backward " \t" lbp
)
3394 (let ((cc (current-column)))
3395 (if (= cc
0) fill-column cc
)))))
3397 (cond (tofill fill-column
)
3398 ((equal last-command
'rst-repeat-last-character
)
3399 (if (= curcol fill-column
) prevcol fill-column
))
3401 (if (= prevcol
0) fill-column prevcol
)))
3404 (if (> (current-column) rightmost-column
)
3405 ;; shave characters off the end
3406 (delete-region (- (point)
3407 (- (current-column) rightmost-column
))
3409 ;; fill with last characters
3410 (insert-char (preceding-char)
3411 (- rightmost-column
(current-column))))
3415 (defun rst-portable-mark-active-p ()
3416 "A portable function that returns non-nil if the mark is active."
3418 ((fboundp 'region-active-p
) (region-active-p))
3419 ((boundp 'transient-mark-mode
) transient-mark-mode mark-active
)))
3425 ;; arch-tag: 255ac0a3-a689-44cb-8643-04ca55ae490d
3426 ;;; rst.el ends here