]> code.delx.au - gnu-emacs-elpa/blob - packages/auctex-11.86/tex-fold.el
(debbugs-emacs): New function and modes for listing the Emacs bugs, reading them...
[gnu-emacs-elpa] / packages / auctex-11.86 / tex-fold.el
1 ;;; tex-fold.el --- Fold TeX macros.
2
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Ralf Angeli <angeli@caeruleus.net>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2004-07-04
8 ;; Keywords: tex, wp
9
10 ;; This file is part of AUCTeX.
11
12 ;; AUCTeX is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; AUCTeX is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with AUCTeX; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 ;; 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This file provides support for hiding and unhiding TeX, LaTeX,
30 ;; ContTeXt, Texinfo and similar macros and environments inside of
31 ;; AUCTeX.
32 ;;
33 ;; Caveats:
34 ;;
35 ;; The display string of content which should display part of itself
36 ;; is made by copying the text from the buffer together with its text
37 ;; properties. If fontification has not happened when this is done
38 ;; (e.g. because of lazy or just-in-time font locking) the intended
39 ;; fontification will not show up. Maybe this could be improved by
40 ;; using some sort of "lazy folding" or refreshing the window upon
41 ;; scrolling. As a workaround fontification of the whole buffer
42 ;; currently is forced before folding it.
43
44 ;;; Code:
45
46 (when (featurep 'xemacs)
47 (require 'overlay))
48 (require 'tex)
49 (autoload 'LaTeX-forward-paragraph "latex")
50 (autoload 'LaTeX-backward-paragraph "latex")
51 (autoload 'LaTeX-find-matching-begin "latex")
52 (autoload 'LaTeX-find-matching-end "latex")
53 (autoload 'ConTeXt-find-matching-start "context")
54 (autoload 'ConTeXt-find-matching-stop "context")
55 (autoload 'Texinfo-find-env-start "tex-info")
56 (autoload 'Texinfo-find-env-end "tex-info")
57
58 (defgroup TeX-fold nil
59 "Fold TeX macros."
60 :group 'AUCTeX)
61
62 (defcustom TeX-fold-type-list '(env macro math)
63 "List of item types to consider when folding.
64 Valid items are the symbols 'env for environments, 'macro for
65 macros, 'math for math macros and 'comment for comments."
66 :type '(set (const :tag "Environments" env)
67 (const :tag "Macros" macro)
68 (const :tag "Math Macros" math)
69 (const :tag "Comments" comment))
70 :group 'TeX-fold)
71
72 (defcustom TeX-fold-macro-spec-list
73 `(("[f]" ("footnote" "marginpar"))
74 ("[c]" ("cite"))
75 ("[l]" ("label"))
76 ("[r]" ("ref" "pageref" "eqref"))
77 ("[i]" ("index" "glossary"))
78 ("[1]:||*" ("item"))
79 ("..." ("dots"))
80 ("(C)" ("copyright"))
81 ("(R)" ("textregistered"))
82 ("TM" ("texttrademark"))
83 (1 ("part" "chapter" "section" "subsection" "subsubsection"
84 "paragraph" "subparagraph"
85 "part*" "chapter*" "section*" "subsection*" "subsubsection*"
86 "paragraph*" "subparagraph*"
87 "emph" "textit" "textsl" "textmd" "textrm" "textsf" "texttt"
88 "textbf" "textsc" "textup")))
89 "List of replacement specifiers and macros to fold.
90
91 The first element of each item can be a string, an integer or a
92 function symbol. The second element is a list of macros two fold
93 without the leading backslash.
94
95 If the first element is a string, it will be used as a display
96 replacement for the whole macro. Numbers in braces, brackets,
97 parens or angle brackets will be replaced by the respective macro
98 argument. For example \"{1}\" will be replaced by the first
99 mandatory argument of the macro. One can also define
100 alternatives within the specifier which are used if an argument
101 is not found. Alternatives are separated by \"||\". They are
102 most useful with optional arguments. As an example, the default
103 specifier for \\item is \"[1]:||*\" which means that if there is
104 an optional argument, its value is shown followed by a colon. If
105 there is no optional argument, only an asterisk is used as the
106 display string.
107
108 If the first element is an integer, the macro will be replaced by
109 the respective macro argument.
110
111 If the first element is a function symbol, the function will be
112 called with all mandatory arguments of the macro and the result
113 of the function call will be used as a replacement for the macro.
114
115 Setting this variable does not take effect immediately. Use
116 Customize or reset the mode."
117 :type '(repeat (group (choice (string :tag "Display String")
118 (integer :tag "Number of argument" :value 1)
119 (function :tag "Function to execute"))
120 (repeat :tag "Macros" (string))))
121 :group 'TeX-fold)
122
123 (defvar TeX-fold-macro-spec-list-internal nil
124 "Internal list of display strings and macros to fold.
125 Is updated when the TeX Fold mode is being activated and then
126 contains all constructs to fold for the given buffer or mode
127 respectively, i.e. contents of both `TeX-fold-macro-spec-list'
128 and <mode-prefix>-fold-macro-spec-list.")
129 (make-variable-buffer-local 'TeX-fold-macro-spec-list-internal)
130
131 (defcustom TeX-fold-env-spec-list
132 '(("[comment]" ("comment")))
133 "List of display strings and environments to fold."
134 :type '(repeat (group (choice (string :tag "Display String")
135 (integer :tag "Number of argument" :value 1))
136 (repeat :tag "Environments" (string))))
137 :group 'TeX-fold)
138
139 (defvar TeX-fold-env-spec-list-internal nil
140 "Internal list of display strings and environments to fold.
141 Is updated when the TeX Fold mode is being activated and then
142 contains all constructs to fold for the given buffer or mode
143 respectively, i.e. contents of both `TeX-fold-env-spec-list'
144 and <mode-prefix>-fold-env-spec-list.")
145 (make-variable-buffer-local 'TeX-fold-env-spec-list-internal)
146
147 (defcustom TeX-fold-math-spec-list nil
148 "List of display strings and math macros to fold."
149 :type '(repeat (group (choice (string :tag "Display String")
150 (integer :tag "Number of argument" :value 1))
151 (repeat :tag "Math Macros" (string))))
152 :group 'TeX-fold)
153
154 (defvar TeX-fold-math-spec-list-internal nil
155 "Internal list of display strings and math macros to fold.
156 Is updated when the TeX Fold mode is being activated and then
157 contains all constructs to fold for the given buffer or mode
158 respectively, i.e. contents of both `TeX-fold-math-spec-list'
159 and <mode-prefix>-fold-math-spec-list.")
160 (make-variable-buffer-local 'TeX-fold-math-spec-list-internal)
161
162 (defcustom TeX-fold-unspec-macro-display-string "[m]"
163 "Display string for unspecified macros.
164 This string will be displayed if a single macro is being hidden
165 which is not specified in `TeX-fold-macro-spec-list'."
166 :type '(string)
167 :group 'TeX-fold)
168
169 (defcustom TeX-fold-unspec-env-display-string "[env]"
170 "Display string for unspecified environments.
171 This string will be displayed if a single environment is being
172 hidden which is not specified in `TeX-fold-env-spec-list'."
173 :type '(string)
174 :group 'TeX-fold)
175
176 (defcustom TeX-fold-unspec-use-name t
177 "If non-nil use the name of an unspecified item as display string.
178 Set it to nil if you want to use the values of the variables
179 `TeX-fold-unspec-macro-display-string' or
180 `TeX-fold-unspec-env-display-string' respectively as a display
181 string for any unspecified macro or environment."
182 :type 'boolean
183 :group 'TeX-fold)
184
185 (defcustom TeX-fold-preserve-comments nil
186 "If non-nil do not fold in comments."
187 :type 'boolean
188 :group 'TeX-fold)
189
190 (defcustom TeX-fold-unfold-around-mark t
191 "Unfold text around the mark, if active."
192 :type 'boolean
193 :group 'TeX-fold)
194
195 (defcustom TeX-fold-help-echo-max-length 70
196 "Maximum length of help echo message for folded overlays.
197 Set it to zero in order to disable help echos."
198 :type 'integer
199 :group 'TeX-fold)
200
201 (defcustom TeX-fold-force-fontify t
202 "Force the buffer to be fully fontified by folding it."
203 :group 'TeX-fold
204 :type 'boolean)
205
206 (defcustom TeX-fold-auto nil
207 "If non-nil, fold macros automatically after `TeX-insert-macro'."
208 :group 'TeX-fold
209 :type 'boolean)
210
211 (defface TeX-fold-folded-face
212 '((((class color) (background light))
213 (:foreground "SlateBlue"))
214 (((class color) (background dark))
215 (:foreground "SlateBlue1"))
216 (((class grayscale) (background light))
217 (:foreground "DimGray"))
218 (((class grayscale) (background dark))
219 (:foreground "LightGray"))
220 (t (:slant italic)))
221 "Face for the display string of folded content."
222 :group 'TeX-fold)
223
224 (defvar TeX-fold-folded-face 'TeX-fold-folded-face
225 "Face for the display string of folded content.")
226
227 (defface TeX-fold-unfolded-face
228 '((((class color) (background light))
229 (:background "#f2f0fd"))
230 (((class color) (background dark))
231 (:background "#38405d"))
232 (((class grayscale) (background light))
233 (:background "LightGray"))
234 (((class grayscale) (background dark))
235 (:background "DimGray"))
236 (t (:inverse-video t)))
237 "Face for folded content when it is temporarily opened."
238 :group 'TeX-fold)
239
240 (defvar TeX-fold-unfolded-face 'TeX-fold-unfolded-face
241 "Face for folded content when it is temporarily opened.")
242
243 (defvar TeX-fold-ellipsis "..."
244 "String used as display string for overlays instead of a zero-length string.")
245
246 (defvar TeX-fold-open-spots nil)
247 (make-variable-buffer-local 'TeX-fold-open-spots)
248
249 (defcustom TeX-fold-command-prefix "\C-c\C-o"
250 "Prefix key to use for commands in TeX Fold mode.
251 The value of this variable is checked as part of loading TeX Fold mode.
252 After that, changing the prefix key requires manipulating keymaps."
253 :type 'string
254 :group 'TeX-fold)
255
256 (defvar TeX-fold-keymap
257 (let ((map (make-sparse-keymap)))
258 (define-key map "\C-o" 'TeX-fold-dwim)
259 (define-key map "\C-b" 'TeX-fold-buffer)
260 (define-key map "\C-r" 'TeX-fold-region)
261 (define-key map "\C-p" 'TeX-fold-paragraph)
262 (define-key map "\C-m" 'TeX-fold-macro)
263 (define-key map "\C-e" 'TeX-fold-env)
264 (define-key map "\C-c" 'TeX-fold-comment)
265 (define-key map "b" 'TeX-fold-clearout-buffer)
266 (define-key map "r" 'TeX-fold-clearout-region)
267 (define-key map "p" 'TeX-fold-clearout-paragraph)
268 (define-key map "i" 'TeX-fold-clearout-item)
269 map))
270
271
272 ;;; Folding
273
274 (defun TeX-fold-dwim ()
275 "Hide or show items according to the current context.
276 If there is folded content, unfold it. If there is a marked
277 region, fold all configured content in this region. If there is
278 no folded content but a macro or environment, fold it."
279 (interactive)
280 (cond ((TeX-fold-clearout-item))
281 ((TeX-active-mark) (TeX-fold-region (mark) (point)))
282 ((TeX-fold-item 'macro))
283 ((TeX-fold-item 'math))
284 ((TeX-fold-item 'env))
285 ((TeX-fold-comment))))
286
287 (defun TeX-fold-buffer ()
288 "Hide all configured macros and environments in the current buffer.
289 The relevant macros are specified in the variable `TeX-fold-macro-spec-list'
290 and `TeX-fold-math-spec-list', and environments in `TeX-fold-env-spec-list'."
291 (interactive)
292 (TeX-fold-clearout-region (point-min) (point-max))
293 (when (and TeX-fold-force-fontify
294 (boundp 'jit-lock-mode)
295 jit-lock-mode
296 (fboundp 'jit-lock-fontify-now))
297 ;; We force fontification here only because it should rarely be
298 ;; needed for the other folding commands.
299 (jit-lock-fontify-now))
300 (TeX-fold-region (point-min) (point-max)))
301
302 (defun TeX-fold-paragraph ()
303 "Hide all configured macros and environments in the current paragraph.
304 The relevant macros are specified in the variable `TeX-fold-macro-spec-list'
305 and `TeX-fold-math-spec-list', and environments in `TeX-fold-env-spec-list'."
306 (interactive)
307 (save-excursion
308 (let ((end (progn (LaTeX-forward-paragraph) (point)))
309 (start (progn (LaTeX-backward-paragraph) (point))))
310 (TeX-fold-clearout-region start end)
311 (TeX-fold-region start end))))
312
313 (defun TeX-fold-region (start end)
314 "Fold all items in region from START to END."
315 (interactive "r")
316 (when (and (memq 'env TeX-fold-type-list)
317 (not (eq major-mode 'plain-tex-mode)))
318 (TeX-fold-region-macro-or-env start end 'env))
319 (when (memq 'macro TeX-fold-type-list)
320 (TeX-fold-region-macro-or-env start end 'macro))
321 (when (memq 'math TeX-fold-type-list)
322 (TeX-fold-region-macro-or-env start end 'math))
323 (when (memq 'comment TeX-fold-type-list)
324 (TeX-fold-region-comment start end)))
325
326 (defun TeX-fold-region-macro-or-env (start end type)
327 "Fold all items of type TYPE in region from START to END.
328 TYPE can be one of the symbols 'env for environments, 'macro
329 for macros and 'math for math macros."
330 (save-excursion
331 (let (fold-list item-list regexp)
332 (dolist (item (cond ((eq type 'env) TeX-fold-env-spec-list-internal)
333 ((eq type 'math) TeX-fold-math-spec-list-internal)
334 (t TeX-fold-macro-spec-list-internal)))
335 (dolist (i (cadr item))
336 (add-to-list 'fold-list (list i (car item)))
337 (add-to-list 'item-list i)))
338 (when item-list
339 (setq regexp (cond ((and (eq type 'env)
340 (eq major-mode 'context-mode))
341 (concat (regexp-quote TeX-esc)
342 "start" (regexp-opt item-list t)))
343 ((and (eq type 'env)
344 (eq major-mode 'texinfo-mode))
345 (concat (regexp-quote TeX-esc)
346 (regexp-opt item-list t)))
347 ((eq type 'env)
348 (concat (regexp-quote TeX-esc)
349 "begin[ \t]*{"
350 (regexp-opt item-list t) "}"))
351 (t
352 (concat (regexp-quote TeX-esc)
353 (regexp-opt item-list t)))))
354 (save-restriction
355 (narrow-to-region start end)
356 ;; Start from the bottom so that it is easier to prioritize
357 ;; nested macros.
358 (goto-char (point-max))
359 (let ((case-fold-search nil)
360 item-name)
361 (while (re-search-backward regexp nil t)
362 (setq item-name (match-string 1))
363 (unless (or (and TeX-fold-preserve-comments
364 (TeX-in-commented-line))
365 ;; Make sure no partially matched macros are
366 ;; folded. For macros consisting of letters
367 ;; this means there should be none of the
368 ;; characters [A-Za-z@*] after the matched
369 ;; string. Single-char non-letter macros like
370 ;; \, don't have this requirement.
371 (and (memq type '(macro math))
372 (save-match-data
373 (string-match "[A-Za-z]" item-name))
374 (save-match-data
375 (string-match "[A-Za-z@*]"
376 (string (char-after
377 (match-end 0)))))))
378 (let* ((item-start (match-beginning 0))
379 (display-string-spec (cadr (assoc item-name
380 fold-list)))
381 (item-end (TeX-fold-item-end item-start type))
382 (ov (TeX-fold-make-overlay item-start item-end type
383 display-string-spec)))
384 (TeX-fold-hide-item ov))))))))))
385
386 (defun TeX-fold-region-comment (start end)
387 "Fold all comments in region from START to END."
388 (save-excursion
389 (goto-char start)
390 (let (beg)
391 (while (setq beg (TeX-search-forward-comment-start end))
392 (goto-char beg)
393 ;; Determine the start of the region to be folded just behind
394 ;; the comment starter.
395 (looking-at TeX-comment-start-regexp)
396 (setq beg (match-end 0))
397 ;; Search for the end of the comment.
398 (while (TeX-comment-forward))
399 (end-of-line 0)
400 ;; Hide the whole region.
401 (TeX-fold-hide-item (TeX-fold-make-overlay beg (point) 'comment
402 TeX-fold-ellipsis))))))
403
404 (defun TeX-fold-macro ()
405 "Hide the macro on which point currently is located."
406 (interactive)
407 (unless (TeX-fold-item 'macro)
408 (message "No macro found")))
409
410 (defun TeX-fold-math ()
411 "Hide the math macro on which point currently is located."
412 (interactive)
413 (unless (TeX-fold-item 'math)
414 (message "No macro found")))
415
416 (defun TeX-fold-env ()
417 "Hide the environment on which point currently is located."
418 (interactive)
419 (unless (TeX-fold-item 'env)
420 (message "No environment found")))
421
422 (defun TeX-fold-comment ()
423 "Hide the comment on which point currently is located."
424 (interactive)
425 (unless (TeX-fold-comment-do)
426 (message "No comment found")))
427
428 (defun TeX-fold-item (type)
429 "Hide the item on which point currently is located.
430 TYPE specifies the type of item and can be one of the symbols
431 'env for environments, 'macro for macros or 'math for math
432 macros.
433 Return non-nil if an item was found and folded, nil otherwise."
434 (if (and (eq type 'env)
435 (eq major-mode 'plain-tex-mode))
436 (message
437 "Folding of environments is not supported in current mode")
438 (let ((item-start (cond ((and (eq type 'env)
439 (eq major-mode 'context-mode))
440 (save-excursion
441 (ConTeXt-find-matching-start) (point)))
442 ((and (eq type 'env)
443 (eq major-mode 'texinfo-mode))
444 (save-excursion
445 (Texinfo-find-env-start) (point)))
446 ((eq type 'env)
447 (condition-case nil
448 (save-excursion
449 (LaTeX-find-matching-begin) (point))
450 (error nil)))
451 (t
452 (TeX-find-macro-start)))))
453 (when item-start
454 (let* ((item-name (save-excursion
455 (goto-char item-start)
456 (looking-at
457 (cond ((and (eq type 'env)
458 (eq major-mode 'context-mode))
459 (concat (regexp-quote TeX-esc)
460 "start\\([A-Za-z]+\\)"))
461 ((and (eq type 'env)
462 (eq major-mode 'texinfo-mode))
463 (concat (regexp-quote TeX-esc)
464 "\\([A-Za-z]+\\)"))
465 ((eq type 'env)
466 (concat (regexp-quote TeX-esc)
467 "begin[ \t]*{"
468 "\\([A-Za-z]+\\)}"))
469 (t
470 (concat (regexp-quote TeX-esc)
471 "\\([A-Za-z@*]+\\)"))))
472 (if (fboundp 'match-string-no-properties)
473 (match-string-no-properties 1)
474 (match-string 1))))
475 (fold-list (cond ((eq type 'env) TeX-fold-env-spec-list-internal)
476 ((eq type 'math)
477 TeX-fold-math-spec-list-internal)
478 (t TeX-fold-macro-spec-list-internal)))
479 fold-item
480 (display-string-spec
481 (or (catch 'found
482 (while fold-list
483 (setq fold-item (car fold-list))
484 (setq fold-list (cdr fold-list))
485 (when (member item-name (cadr fold-item))
486 (throw 'found (car fold-item)))))
487 ;; Item is not specified.
488 (if TeX-fold-unspec-use-name
489 (concat "[" item-name "]")
490 (if (eq type 'env)
491 TeX-fold-unspec-env-display-string
492 TeX-fold-unspec-macro-display-string))))
493 (item-end (TeX-fold-item-end item-start type))
494 (ov (TeX-fold-make-overlay item-start item-end type
495 display-string-spec)))
496 (TeX-fold-hide-item ov))))))
497
498 (defun TeX-fold-comment-do ()
499 "Hide the comment on which point currently is located.
500 This is the function doing the work for `TeX-fold-comment'. It
501 is an internal function communicating with return values rather
502 than with messages for the user.
503 Return non-nil if a comment was found and folded, nil otherwise."
504 (if (and (not (TeX-in-comment)) (not (TeX-in-line-comment)))
505 nil
506 (let (beg)
507 (save-excursion
508 (while (progn
509 (beginning-of-line 0)
510 (and (TeX-in-line-comment)
511 (not (bobp)))))
512 (goto-char (TeX-search-forward-comment-start (line-end-position 2)))
513 (looking-at TeX-comment-start-regexp)
514 (setq beg (match-end 0))
515 (while (TeX-comment-forward))
516 (end-of-line 0)
517 (when (> (point) beg)
518 (TeX-fold-hide-item (TeX-fold-make-overlay beg (point) 'comment
519 TeX-fold-ellipsis)))))))
520
521
522 ;;; Utilities
523
524 (defun TeX-fold-make-overlay (ov-start ov-end type display-string-spec)
525 "Make a TeX-fold overlay extending from OV-START to OV-END.
526 TYPE is a symbol which is used to describe the content to hide
527 and may be 'macro for macros, 'math for math macro and 'env for
528 environments.
529 DISPLAY-STRING-SPEC is the original specification of the display
530 string in the variables `TeX-fold-macro-spec-list' or
531 `TeX-fold-env-spec-list' and may be a string or an integer."
532 ;; Calculate priority before the overlay is instantiated. We don't
533 ;; want `TeX-overlay-prioritize' to pick up a non-prioritized one.
534 (let ((priority (TeX-overlay-prioritize ov-start ov-end))
535 (ov (make-overlay ov-start ov-end (current-buffer) t nil)))
536 (overlay-put ov 'category 'TeX-fold)
537 (overlay-put ov 'priority priority)
538 (overlay-put ov 'evaporate t)
539 (overlay-put ov 'TeX-fold-type type)
540 (overlay-put ov 'TeX-fold-display-string-spec display-string-spec)
541 ov))
542
543 (defun TeX-fold-item-end (start type)
544 "Return the end of an item of type TYPE starting at START.
545 TYPE can be either 'env for environments, 'macro for macros or
546 'math for math macros."
547 (save-excursion
548 (cond ((and (eq type 'env)
549 (eq major-mode 'context-mode))
550 (goto-char start)
551 (ConTeXt-find-matching-stop)
552 (point))
553 ((and (eq type 'env)
554 (eq major-mode 'texinfo-mode))
555 (goto-char (1+ start))
556 (Texinfo-find-env-end)
557 (point))
558 ((eq type 'env)
559 (goto-char (1+ start))
560 (LaTeX-find-matching-end)
561 (point))
562 (t
563 (goto-char start)
564 (TeX-find-macro-end)))))
565
566 (defun TeX-fold-overfull-p (ov-start ov-end display-string)
567 "Return t if an overfull line will result after adding an overlay.
568 The overlay extends from OV-START to OV-END and will display the
569 string DISPLAY-STRING."
570 (and (not (featurep 'xemacs)) ; Linebreaks in glyphs don't
571 ; work in XEmacs anyway.
572 (save-excursion
573 (goto-char ov-end)
574 (search-backward "\n" ov-start t))
575 (not (string-match "\n" display-string))
576 (> (+ (- ov-start
577 (save-excursion
578 (goto-char ov-start)
579 (line-beginning-position)))
580 (length display-string)
581 (- (save-excursion
582 (goto-char ov-end)
583 (line-end-position))
584 ov-end))
585 (current-fill-column))))
586
587 (defun TeX-fold-macro-nth-arg (n macro-start &optional macro-end delims)
588 "Return a property list of the argument number N of a macro.
589 The start of the macro to examine is given by MACRO-START, its
590 end optionally by MACRO-END. With DELIMS the type of delimiters
591 can be specified as a cons cell containing the opening char as
592 the car and the closing char as the cdr. The chars have to have
593 opening and closing syntax as defined in
594 `TeX-search-syntax-table'.
595
596 The first item in the returned list is the string specified in
597 the argument, the second item may be a face if the argument
598 string was fontified. In Emacs the string holds text properties
599 as well, so the second item is always nil. In XEmacs the string
600 does not enclose any faces, so these are given in the second item
601 of the resulting list."
602 (save-excursion
603 (let* ((macro-end (or macro-end
604 (save-excursion (goto-char macro-start)
605 (TeX-find-macro-end))))
606 (open-char (if delims (car delims) ?{))
607 (open-string (char-to-string open-char))
608 (close-char (if delims (cdr delims) ?}))
609 (close-string (char-to-string close-char))
610 content-start content-end)
611 (goto-char macro-start)
612 (if (condition-case nil
613 (progn
614 (while (> n 0)
615 (skip-chars-forward (concat "^" open-string) macro-end)
616 (when (= (point) macro-end)
617 (error nil))
618 (setq content-start (progn
619 (skip-chars-forward
620 (concat open-string " \t"))
621 (point)))
622 (goto-char
623 (if delims
624 (with-syntax-table
625 (TeX-search-syntax-table open-char close-char)
626 (scan-lists (point) 1 1))
627 (TeX-find-closing-brace)))
628 (setq content-end (save-excursion
629 (backward-char)
630 (skip-chars-backward " \t")
631 (point)))
632 (setq n (1- n)))
633 t)
634 (error nil))
635 (list (TeX-fold-buffer-substring content-start content-end)
636 (when (and (featurep 'xemacs)
637 (extent-at content-start))
638 ;; A glyph in XEmacs does not seem to be able to hold more
639 ;; than one face, so we just use the first one we get.
640 (car (extent-property (extent-at content-start) 'face))))
641 nil))))
642
643 (defun TeX-fold-buffer-substring (start end)
644 "Return the contents of buffer from START to END as a string.
645 Like `buffer-substring' but copy overlay display strings as well."
646 ;; Swap values of `start' and `end' if necessary.
647 (when (> start end) (let ((tmp start)) (setq start end end tmp)))
648 (let ((overlays (overlays-in start end))
649 result)
650 ;; Get rid of overlays not under our control or not completely
651 ;; inside the specified region.
652 (dolist (ov overlays)
653 (when (or (not (eq (overlay-get ov 'category) 'TeX-fold))
654 (< (overlay-start ov) start)
655 (> (overlay-end ov) end))
656 (setq overlays (remove ov overlays))))
657 (if (null overlays)
658 (buffer-substring start end)
659 ;; Sort list according to ascending starts.
660 (setq overlays (sort (copy-sequence overlays)
661 (lambda (a b)
662 (< (overlay-start a) (overlay-start b)))))
663 ;; Get the string from the start of the region up to the first overlay.
664 (setq result (buffer-substring start (overlay-start (car overlays))))
665 (let (ov)
666 (while overlays
667 (setq ov (car overlays)
668 overlays (cdr overlays))
669 ;; Add the display string of the overlay.
670 (setq result (concat result (overlay-get ov 'display)))
671 ;; Remove overlays contained in the current one.
672 (dolist (elt overlays)
673 (when (< (overlay-start elt) (overlay-end ov))
674 (setq overlays (remove elt overlays))))
675 ;; Add the string from the end of the current overlay up to
676 ;; the next overlay or the end of the specified region.
677 (setq result (concat result (buffer-substring (overlay-end ov)
678 (if overlays
679 (overlay-start
680 (car overlays))
681 end))))))
682 result)))
683
684 (defun TeX-fold-make-help-echo (start end)
685 "Return a string to be used as the help echo of folded overlays.
686 The text between START and END will be used for this but cropped
687 to the length defined by `TeX-fold-help-echo-max-length'. Line
688 breaks will be replaced by spaces."
689 (let* ((spill (+ start TeX-fold-help-echo-max-length))
690 (lines (split-string (buffer-substring start (min end spill)) "\n"))
691 (result (pop lines)))
692 (dolist (line lines)
693 ;; Strip leading whitespace
694 (when (string-match "^[ \t]+" line)
695 (setq line (replace-match "" nil nil line)))
696 ;; Strip trailing whitespace
697 (when (string-match "[ \t]+$" line)
698 (setq line (replace-match "" nil nil line)))
699 (setq result (concat result " " line)))
700 (when (> end spill) (setq result (concat result "...")))
701 result))
702
703 (defun TeX-fold-update-at-point ()
704 "Update all TeX-fold overlays at point displaying computed content."
705 (let (overlays)
706 ;; Get all overlays at point under our control.
707 (dolist (ov (overlays-at (point)))
708 (when (and (eq (overlay-get ov 'category) 'TeX-fold)
709 (numberp (overlay-get ov 'TeX-fold-display-string-spec)))
710 (add-to-list 'overlays ov)))
711 (when overlays
712 ;; Sort list according to descending starts.
713 (setq overlays (sort (copy-sequence overlays)
714 (lambda (a b)
715 (> (overlay-start a) (overlay-start b)))))
716 (dolist (ov overlays)
717 (TeX-fold-hide-item ov)))))
718
719
720 ;;; Removal
721
722 (defun TeX-fold-clearout-buffer ()
723 "Permanently show all macros in the buffer."
724 (interactive)
725 (TeX-fold-clearout-region (point-min) (point-max)))
726
727 (defun TeX-fold-clearout-paragraph ()
728 "Permanently show all macros in the paragraph point is located in."
729 (interactive)
730 (save-excursion
731 (let ((end (progn (LaTeX-forward-paragraph) (point)))
732 (start (progn (LaTeX-backward-paragraph) (point))))
733 (TeX-fold-clearout-region start end))))
734
735 (defun TeX-fold-clearout-region (start end)
736 "Permanently show all macros in region starting at START and ending at END."
737 (interactive "r")
738 (let ((overlays (overlays-in start end)))
739 (TeX-fold-remove-overlays overlays)))
740
741 (defun TeX-fold-clearout-item ()
742 "Permanently show the macro on which point currently is located."
743 (interactive)
744 (let ((overlays (overlays-at (point))))
745 (TeX-fold-remove-overlays overlays)))
746
747 (defun TeX-fold-remove-overlays (overlays)
748 "Remove all overlays set by TeX-fold in OVERLAYS.
749 Return non-nil if a removal happened, nil otherwise."
750 (let (found)
751 (while overlays
752 (when (eq (overlay-get (car overlays) 'category) 'TeX-fold)
753 (delete-overlay (car overlays))
754 (setq found t))
755 (setq overlays (cdr overlays)))
756 found))
757
758
759 ;;; Toggling
760
761 (defun TeX-fold-expand-spec (spec ov-start ov-end)
762 "Expand instances of {<num>}, [<num>], <<num>>, and (<num>).
763 Replace them with the respective macro argument."
764 (let ((spec-list (split-string spec "||"))
765 (delims '((?{ . ?}) (?[ . ?]) (?< . ?>) (?\( . ?\))))
766 match-end success)
767 (catch 'success
768 ;; Iterate over alternatives.
769 (dolist (elt spec-list)
770 (setq spec elt)
771 ;; Find and expand every placeholder.
772 (while (and (string-match "\\([[{<]\\)\\([1-9]\\)\\([]}>]\\)" elt
773 match-end)
774 ;; Does the closing delim fit to the opening one?
775 (string-equal
776 (match-string 3 elt)
777 (char-to-string
778 (cdr (assq (string-to-char (match-string 1 elt))
779 delims)))))
780 (setq match-end (match-beginning 0))
781 (let ((arg (car (save-match-data
782 ;; Get the argument.
783 (TeX-fold-macro-nth-arg
784 (string-to-number (match-string 2 elt))
785 ov-start ov-end
786 (assoc (string-to-char (match-string 1 elt))
787 delims))))))
788 (when arg (setq success t))
789 ;; Replace the placeholder in the string.
790 (setq elt (replace-match (or arg TeX-fold-ellipsis) nil t elt)
791 spec elt)))
792 (when success (throw 'success nil))))
793 spec))
794
795 (defun TeX-fold-hide-item (ov)
796 "Hide a single macro or environment.
797 That means, put respective properties onto overlay OV."
798 (let* ((ov-start (overlay-start ov))
799 (ov-end (overlay-end ov))
800 (spec (overlay-get ov 'TeX-fold-display-string-spec))
801 (computed (cond
802 ((stringp spec)
803 (TeX-fold-expand-spec spec ov-start ov-end))
804 ((functionp spec)
805 (let (arg arg-list
806 (n 1))
807 (while (setq arg (TeX-fold-macro-nth-arg
808 n ov-start ov-end))
809 (add-to-list 'arg-list (car arg) t)
810 (setq n (1+ n)))
811 (or (condition-case nil
812 (apply spec arg-list)
813 (error nil))
814 "[Error: No content or function found]")))
815 (t (or (TeX-fold-macro-nth-arg spec ov-start ov-end)
816 "[Error: No content found]"))))
817 (display-string (if (listp computed) (car computed) computed))
818 (face (when (listp computed) (cadr computed))))
819 ;; Cater for zero-length display strings.
820 (when (string= display-string "") (setq display-string TeX-fold-ellipsis))
821 ;; Add a linebreak to the display string and adjust the overlay end
822 ;; in case of an overfull line.
823 (when (TeX-fold-overfull-p ov-start ov-end display-string)
824 (setq display-string (concat display-string "\n"))
825 (move-overlay ov ov-start (save-excursion
826 (goto-char ov-end)
827 (skip-chars-forward " \t")
828 (point))))
829 (overlay-put ov 'mouse-face 'highlight)
830 (overlay-put ov 'display display-string)
831 (if (featurep 'xemacs)
832 (let ((glyph (make-glyph (if (listp display-string)
833 (car display-string)
834 display-string))))
835 (overlay-put ov 'invisible t)
836 (when font-lock-mode
837 (if face
838 (set-glyph-property glyph 'face face)
839 (set-glyph-property glyph 'face TeX-fold-folded-face)))
840 (set-extent-property ov 'end-glyph glyph))
841 (when font-lock-mode
842 (overlay-put ov 'face TeX-fold-folded-face))
843 (unless (zerop TeX-fold-help-echo-max-length)
844 (overlay-put ov 'help-echo (TeX-fold-make-help-echo
845 (overlay-start ov) (overlay-end ov)))))))
846
847 (defun TeX-fold-show-item (ov)
848 "Show a single LaTeX macro or environment.
849 Remove the respective properties from the overlay OV."
850 (overlay-put ov 'mouse-face nil)
851 (if (featurep 'xemacs)
852 (progn
853 (set-extent-property ov 'end-glyph nil)
854 (overlay-put ov 'invisible nil))
855 (overlay-put ov 'display nil)
856 (overlay-put ov 'help-echo nil)
857 (when font-lock-mode
858 (overlay-put ov 'face TeX-fold-unfolded-face))))
859
860 ;; Copy and adaption of `reveal-post-command' from reveal.el in GNU
861 ;; Emacs on 2004-07-04.
862 (defun TeX-fold-post-command ()
863 ;; `with-local-quit' is not supported in XEmacs.
864 (condition-case nil
865 (let ((inhibit-quit nil))
866 (condition-case err
867 (let* ((spots (TeX-fold-partition-list
868 (lambda (x)
869 ;; We refresh any spot in the current
870 ;; window as well as any spots associated
871 ;; with a dead window or a window which
872 ;; does not show this buffer any more.
873 (or (eq (car x) (selected-window))
874 (not (window-live-p (car x)))
875 (not (eq (window-buffer (car x))
876 (current-buffer)))))
877 TeX-fold-open-spots))
878 (old-ols (mapcar 'cdr (car spots))))
879 (setq TeX-fold-open-spots (cdr spots))
880 (when (or (and (boundp 'disable-point-adjustment)
881 disable-point-adjustment)
882 (and (boundp 'global-disable-point-adjustment)
883 global-disable-point-adjustment)
884 ;; See preview.el on how to make this configurable.
885 (memq this-command (list (key-binding [left])
886 (key-binding [right])
887 'mouse-set-point)))
888 ;; Open new overlays.
889 (dolist (ol (nconc (when (and TeX-fold-unfold-around-mark
890 (boundp 'mark-active)
891 mark-active)
892 (overlays-at (mark)))
893 (overlays-at (point))))
894 (when (eq (overlay-get ol 'category) 'TeX-fold)
895 (push (cons (selected-window) ol) TeX-fold-open-spots)
896 (setq old-ols (delq ol old-ols))
897 (TeX-fold-show-item ol))))
898 ;; Close old overlays.
899 (dolist (ol old-ols)
900 (when (and (eq (current-buffer) (overlay-buffer ol))
901 (not (rassq ol TeX-fold-open-spots))
902 (or (not (featurep 'xemacs))
903 (and (featurep 'xemacs)
904 (not (extent-detached-p ol)))))
905 (if (and (>= (point) (overlay-start ol))
906 (<= (point) (overlay-end ol)))
907 ;; Still near the overlay: keep it open.
908 (push (cons (selected-window) ol) TeX-fold-open-spots)
909 ;; Really close it.
910 (TeX-fold-hide-item ol)))))
911 (error (message "TeX-fold: %s" err))))
912 (quit (setq quit-flag t))))
913
914
915 ;;; Misc
916
917 ;; Copy and adaption of `cvs-partition' from pcvs-util.el in GNU Emacs
918 ;; on 2004-07-05 to make tex-fold.el mainly self-contained.
919 (defun TeX-fold-partition-list (p l)
920 "Partition a list L into two lists based on predicate P.
921 The function returns a `cons' cell where the `car' contains
922 elements of L for which P is true while the `cdr' contains
923 the other elements. The ordering among elements is maintained."
924 (let (car cdr)
925 (dolist (x l)
926 (if (funcall p x) (push x car) (push x cdr)))
927 (cons (nreverse car) (nreverse cdr))))
928
929
930 ;;; The mode
931
932 ;; This autoload cookie had to be changed because of XEmacs. This is
933 ;; very dissatisfactory, because we now don't have the full doc string
934 ;; available to tell people what to expect when using this mode before
935 ;; loading it.
936
937 ;;;###autoload (autoload 'TeX-fold-mode "tex-fold" "Minor mode for hiding and revealing macros and environments." t)
938 (define-minor-mode TeX-fold-mode
939 "Minor mode for hiding and revealing macros and environments.
940
941 Called interactively, with no prefix argument, toggle the mode.
942 With universal prefix ARG (or if ARG is nil) turn mode on.
943 With zero or negative ARG turn mode off."
944 nil nil (list (cons TeX-fold-command-prefix TeX-fold-keymap))
945 (if TeX-fold-mode
946 (progn
947 (set (make-local-variable 'search-invisible) t)
948 (add-hook 'post-command-hook 'TeX-fold-post-command nil t)
949 (add-hook 'LaTeX-fill-newline-hook 'TeX-fold-update-at-point nil t)
950 (add-hook 'TeX-after-insert-macro-hook
951 (lambda ()
952 (when (and TeX-fold-mode TeX-fold-auto)
953 (save-excursion
954 (backward-char)
955 (or (TeX-fold-item 'macro)
956 (TeX-fold-item 'math)
957 (TeX-fold-item 'env))))))
958 ;; Update the `TeX-fold-*-spec-list-internal' variables.
959 (dolist (elt '("macro" "env" "math"))
960 (set (intern (format "TeX-fold-%s-spec-list-internal" elt))
961 ;; Append the value of `TeX-fold-*-spec-list' to the
962 ;; mode-specific `<mode-prefix>-fold-*-spec-list' variable.
963 (append (symbol-value (intern (format "TeX-fold-%s-spec-list"
964 elt)))
965 (let ((symbol (intern (format "%s-fold-%s-spec-list"
966 (TeX-mode-prefix) elt))))
967 (when (boundp symbol)
968 (symbol-value symbol)))))))
969 (kill-local-variable 'search-invisible)
970 (remove-hook 'post-command-hook 'TeX-fold-post-command t)
971 (remove-hook 'LaTeX-fill-newline-hook 'TeX-fold-update-at-point t)
972 (TeX-fold-clearout-buffer))
973 (TeX-set-mode-name))
974
975 ;;;###autoload
976 (defalias 'tex-fold-mode 'TeX-fold-mode)
977
978 (provide 'tex-fold)
979
980 ;;; tex-fold.el ends here