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