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