]> code.delx.au - gnu-emacs/blob - lisp/progmodes/hideshow.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / progmodes / hideshow.el
1 ;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks
2
3 ;; Copyright (C) 1994-2011 Free Software Foundation, Inc.
4
5 ;; Author: Thien-Thi Nguyen <ttn@gnu.org>
6 ;; Dan Nicolaescu <dann@ics.uci.edu>
7 ;; Keywords: C C++ java lisp tools editing comments blocks hiding outlines
8 ;; Maintainer-Version: 5.65.2.2
9 ;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
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 ;; * Commands provided
29 ;;
30 ;; This file provides Hideshow Minor Mode. When active, nine commands
31 ;; are available, implementing block hiding and showing. They (and their
32 ;; keybindings) are:
33 ;;
34 ;; hs-hide-block C-c @ C-h
35 ;; hs-show-block C-c @ C-s
36 ;; hs-hide-all C-c @ C-M-h
37 ;; hs-show-all C-c @ C-M-s
38 ;; hs-hide-level C-c @ C-l
39 ;; hs-toggle-hiding C-c @ C-c
40 ;; hs-mouse-toggle-hiding [(shift mouse-2)]
41 ;; hs-hide-initial-comment-block
42 ;;
43 ;; Blocks are defined per mode. In c-mode, c++-mode and java-mode, they
44 ;; are simply text between curly braces, while in Lisp-ish modes parens
45 ;; are used. Multi-line comment blocks can also be hidden. Read-only
46 ;; buffers are not a problem, since hideshow doesn't modify the text.
47 ;;
48 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
49 ;; (similar to other minor modes).
50
51 ;; * Suggested usage
52 ;;
53 ;; First make sure hideshow.el is in a directory in your `load-path'.
54 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
55 ;; Then, add the following to your ~/.emacs:
56 ;;
57 ;; (load-library "hideshow")
58 ;; (add-hook 'X-mode-hook ; other modes similarly
59 ;; (lambda () (hs-minor-mode 1)))
60 ;;
61 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
62 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
63 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
64 ;;
65 ;; Additionally, Joseph Eydelnant writes:
66 ;; I enjoy your package hideshow.el Ver. 5.24 2001/02/13
67 ;; a lot and I've been looking for the following functionality:
68 ;; toggle hide/show all with a single key.
69 ;; Here are a few lines of code that lets me do just that.
70 ;;
71 ;; (defvar my-hs-hide nil "Current state of hideshow for toggling all.")
72 ;; ;;;###autoload
73 ;; (defun my-toggle-hideshow-all () "Toggle hideshow all."
74 ;; (interactive)
75 ;; (setq my-hs-hide (not my-hs-hide))
76 ;; (if my-hs-hide
77 ;; (hs-hide-all)
78 ;; (hs-show-all)))
79 ;;
80 ;; [Your hideshow hacks here!]
81
82 ;; * Customization
83 ;;
84 ;; You can use `M-x customize-variable' on the following variables:
85 ;;
86 ;; - hs-hide-comments-when-hiding-all -- self-explanatory!
87 ;; - hs-hide-all-non-comment-function -- if non-nil, when doing a
88 ;; `hs-hide-all', this function
89 ;; is called w/ no arguments
90 ;; - hs-isearch-open -- what kind of hidden blocks to
91 ;; open when doing isearch
92 ;;
93 ;; Some languages (e.g., Java) are deeply nested, so the normal behavior
94 ;; of `hs-hide-all' (hiding all but top-level blocks) results in very
95 ;; little information shown, which is not very useful. You can use the
96 ;; variable `hs-hide-all-non-comment-function' to implement your idea of
97 ;; what is more useful. For example, the following code shows the next
98 ;; nested level in addition to the top-level:
99 ;;
100 ;; (defun ttn-hs-hide-level-1 ()
101 ;; (hs-hide-level 1)
102 ;; (forward-sexp 1))
103 ;; (setq hs-hide-all-non-comment-function 'ttn-hs-hide-level-1)
104 ;;
105 ;; Hideshow works w/ incremental search (isearch) by setting the variable
106 ;; `hs-headline', which is the line of text at the beginning of a hidden
107 ;; block that contains a match for the search. You can have this show up
108 ;; in the mode line by modifying the variable `mode-line-format'. For
109 ;; example, the following code prepends this info to the mode line:
110 ;;
111 ;; (unless (memq 'hs-headline mode-line-format)
112 ;; (setq mode-line-format
113 ;; (append '("-" hs-headline) mode-line-format)))
114 ;;
115 ;; See documentation for `mode-line-format' for more info.
116 ;;
117 ;; Hooks are run after some commands:
118 ;;
119 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
120 ;; hs-show-hook hs-show-block, hs-show-all
121 ;;
122 ;; One of `hs-hide-hook' or `hs-show-hook' is run for the toggling
123 ;; commands when the result of the toggle is to hide or show blocks,
124 ;; respectively. All hooks are run w/ `run-hooks'. See docs for each
125 ;; variable or hook for more info.
126 ;;
127 ;; Normally, hideshow tries to determine appropriate values for block
128 ;; and comment definitions by examining the buffer's major mode. If
129 ;; there are problems, hideshow will not activate and in that case you
130 ;; may wish to override hideshow's heuristics by adding an entry to
131 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
132 ;; do something like:
133 ;;
134 ;; (add-to-list 'hs-special-modes-alist '(my-mode "{{" "}}" ...))
135 ;;
136 ;; If you have an entry that works particularly well, consider
137 ;; submitting it for inclusion in hideshow.el. See docstring for
138 ;; `hs-special-modes-alist' for more info on the entry format.
139 ;;
140 ;; See also variable `hs-set-up-overlay' for per-block customization of
141 ;; appearance or other effects associated with overlays. For example:
142 ;;
143 ;; (setq hs-set-up-overlay
144 ;; (defun my-display-code-line-counts (ov)
145 ;; (when (eq 'code (overlay-get ov 'hs))
146 ;; (overlay-put ov 'display
147 ;; (propertize
148 ;; (format " ... <%d>"
149 ;; (count-lines (overlay-start ov)
150 ;; (overlay-end ov)))
151 ;; 'face 'font-lock-type-face)))))
152
153 ;; * Bugs
154 ;;
155 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
156 ;; function `forward-comment' (among other things). If someone
157 ;; writes this, please send me a copy.
158 ;;
159 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
160 ;; `M-x hs-minor-mode' twice (that is, deactivate then re-activate
161 ;; hideshow).
162 ;;
163 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.7.
164 ;; XEmacs compatibility may have bitrotted since 4.29.
165 ;;
166 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
167 ;; `byte-compile-file' inserts the file to be compiled in a temporary
168 ;; buffer and switches `normal-mode' on. In the case where you have
169 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
170 ;; the initial comment sometimes hides parts of the first statement (seems
171 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
172 ;;
173 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
174 ;;
175 ;; (defadvice byte-compile-file (around
176 ;; byte-compile-file-hideshow-off
177 ;; act)
178 ;; (let ((hs-minor-mode-hook nil))
179 ;; ad-do-it))
180 ;;
181 ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the
182 ;; suggested workaround is to turn off hideshow entirely, for example:
183 ;;
184 ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow)
185 ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow)
186 ;;
187 ;; In the case of `vc-diff', here is a less invasive workaround:
188 ;;
189 ;; (add-hook 'vc-before-checkin-hook
190 ;; (lambda ()
191 ;; (goto-char (point-min))
192 ;; (hs-show-block)))
193 ;;
194 ;; Unfortunately, these workarounds do not restore hideshow state.
195 ;; If someone figures out a better way, please let me know.
196
197 ;; * Correspondance
198 ;;
199 ;; Correspondance welcome; please indicate version number. Send bug
200 ;; reports and inquiries to <ttn@gnu.org>.
201
202 ;; * Thanks
203 ;;
204 ;; Thanks go to the following people for valuable ideas, code and
205 ;; bug reports.
206 ;;
207 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave Love,
208 ;; Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray, Moody Ahmad,
209 ;; Preston F. Crow, Lars Lindberg, Reto Zimmermann, Keith Sheffield,
210 ;; Chew Meng Kuan, Tony Lam, Pete Ware, François Pinard, Stefan Monnier,
211 ;; Joseph Eydelnant, Michael Ernst, Peter Heslin
212 ;;
213 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
214 ;; overlays (rather than selective display), added isearch magic, folded
215 ;; in custom.el compatibility, generalized comment handling, incorporated
216 ;; mouse support, and maintained the code in general. Version 4.0 is
217 ;; largely due to his efforts.
218
219 ;; * History
220 ;;
221 ;; Hideshow was inspired when I learned about selective display. It was
222 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
223 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
224 ;; been lost. XEmacs support is reliable as of 4.29. State save and
225 ;; restore was added in 3.5 (not widely distributed), and reliable as of
226 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
227 ;; Version 5.x uses new algorithms for block selection and traversal,
228 ;; unbundles state save and restore, and includes more isearch support.
229
230 ;;; Code:
231
232 ;;---------------------------------------------------------------------------
233 ;; user-configurable variables
234
235 (defgroup hideshow nil
236 "Minor mode for hiding and showing program and comment blocks."
237 :prefix "hs-"
238 :group 'languages)
239
240 (defcustom hs-hide-comments-when-hiding-all t
241 "*Hide the comments too when you do an `hs-hide-all'."
242 :type 'boolean
243 :group 'hideshow)
244
245 (defcustom hs-minor-mode-hook nil
246 "*Hook called when hideshow minor mode is activated or deactivated."
247 :type 'hook
248 :group 'hideshow
249 :version "21.1")
250
251 (defcustom hs-isearch-open 'code
252 "*What kind of hidden blocks to open when doing `isearch'.
253 One of the following symbols:
254
255 code -- open only code blocks
256 comment -- open only comment blocks
257 t -- open both code and comment blocks
258 nil -- open neither code nor comment blocks
259
260 This has effect only if `search-invisible' is set to `open'."
261 :type '(choice (const :tag "open only code blocks" code)
262 (const :tag "open only comment blocks" comment)
263 (const :tag "open both code and comment blocks" t)
264 (const :tag "don't open any of them" nil))
265 :group 'hideshow)
266
267 ;;;###autoload
268 (defvar hs-special-modes-alist
269 (mapcar 'purecopy
270 '((c-mode "{" "}" "/[*/]" nil nil)
271 (c++-mode "{" "}" "/[*/]" nil nil)
272 (bibtex-mode ("@\\S(*\\(\\s(\\)" 1))
273 (java-mode "{" "}" "/[*/]" nil nil)
274 (js-mode "{" "}" "/[*/]" nil)))
275 "*Alist for initializing the hideshow variables for different modes.
276 Each element has the form
277 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
278
279 If non-nil, hideshow will use these values as regexps to define blocks
280 and comments, respectively for major mode MODE.
281
282 START, END and COMMENT-START are regular expressions. A block is
283 defined as text surrounded by START and END.
284
285 As a special case, START may be a list of the form (COMPLEX-START
286 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
287 MDATA-SELECTOR an integer that specifies which sub-match is the proper
288 place to adjust point, before calling `hs-forward-sexp-func'. Point
289 is adjusted to the beginning of the specified match. For example,
290 see the `hs-special-modes-alist' entry for `bibtex-mode'.
291
292 For some major modes, `forward-sexp' does not work properly. In those
293 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
294
295 See the documentation for `hs-adjust-block-beginning' to see what is the
296 use of ADJUST-BEG-FUNC.
297
298 If any of the elements is left nil or omitted, hideshow tries to guess
299 appropriate values. The regexps should not contain leading or trailing
300 whitespace. Case does not matter.")
301
302 (defvar hs-hide-all-non-comment-function nil
303 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
304
305 (defvar hs-allow-nesting nil
306 "*If non-nil, hiding remembers internal blocks.
307 This means that when the outer block is shown again,
308 any previously hidden internal blocks remain hidden.")
309
310 (defvar hs-hide-hook nil
311 "*Hook called (with `run-hooks') at the end of commands to hide text.
312 These commands include the toggling commands (when the result is to hide
313 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
314
315 (defvar hs-show-hook nil
316 "*Hook called (with `run-hooks') at the end of commands to show text.
317 These commands include the toggling commands (when the result is to show
318 a block), `hs-show-all' and `hs-show-block'.")
319
320 (defvar hs-set-up-overlay nil
321 "*Function called with one arg, OV, a newly initialized overlay.
322 Hideshow puts a unique overlay on each range of text to be hidden
323 in the buffer. Here is a simple example of how to use this variable:
324
325 (defun display-code-line-counts (ov)
326 (when (eq 'code (overlay-get ov 'hs))
327 (overlay-put ov 'display
328 (format \"... / %d\"
329 (count-lines (overlay-start ov)
330 (overlay-end ov))))))
331
332 (setq hs-set-up-overlay 'display-code-line-counts)
333
334 This example shows how to get information from the overlay as well
335 as how to set its `display' property. See `hs-make-overlay' and
336 info node `(elisp)Overlays'.")
337
338 ;;---------------------------------------------------------------------------
339 ;; internal variables
340
341 (defvar hs-minor-mode nil
342 "Non-nil if using hideshow mode as a minor mode of some other mode.
343 Use the command `hs-minor-mode' to toggle or set this variable.")
344
345 (defvar hs-minor-mode-map
346 (let ((map (make-sparse-keymap)))
347 ;; These bindings roughly imitate those used by Outline mode.
348 (define-key map "\C-c@\C-h" 'hs-hide-block)
349 (define-key map "\C-c@\C-s" 'hs-show-block)
350 (define-key map "\C-c@\C-\M-h" 'hs-hide-all)
351 (define-key map "\C-c@\C-\M-s" 'hs-show-all)
352 (define-key map "\C-c@\C-l" 'hs-hide-level)
353 (define-key map "\C-c@\C-c" 'hs-toggle-hiding)
354 (define-key map [(shift mouse-2)] 'hs-mouse-toggle-hiding)
355 map)
356 "Keymap for hideshow minor mode.")
357
358 (easy-menu-define hs-minor-mode-menu hs-minor-mode-map
359 "Menu used when hideshow minor mode is active."
360 '("Hide/Show"
361 ["Hide Block" hs-hide-block
362 :help "Hide the code or comment block at point"]
363 ["Show Block" hs-show-block
364 :help "Show the code or comment block at point"]
365 ["Hide All" hs-hide-all
366 :help "Hide all the blocks in the buffer"]
367 ["Show All" hs-show-all
368 :help "Show all the blocks in the buffer"]
369 ["Hide Level" hs-hide-level
370 :help "Hide all block at levels below the current block"]
371 ["Toggle Hiding" hs-toggle-hiding
372 :help "Toggle the hiding state of the current block"]
373 "----"
374 ["Hide comments when hiding all"
375 (setq hs-hide-comments-when-hiding-all
376 (not hs-hide-comments-when-hiding-all))
377 :help "If t also hide comment blocks when doing `hs-hide-all'"
378 :style toggle :selected hs-hide-comments-when-hiding-all]
379 ("Reveal on isearch"
380 ["Code blocks" (setq hs-isearch-open 'code)
381 :help "Show hidden code blocks when isearch matches inside them"
382 :active t :style radio :selected (eq hs-isearch-open 'code)]
383 ["Comment blocks" (setq hs-isearch-open 'comment)
384 :help "Show hidden comment blocks when isearch matches inside them"
385 :active t :style radio :selected (eq hs-isearch-open 'comment)]
386 ["Code and Comment blocks" (setq hs-isearch-open t)
387 :help "Show both hidden code and comment blocks when isearch matches inside them"
388 :active t :style radio :selected (eq hs-isearch-open t)]
389 ["None" (setq hs-isearch-open nil)
390 :help "Do not hidden code or comment blocks when isearch matches inside them"
391 :active t :style radio :selected (eq hs-isearch-open nil)])))
392
393 (defvar hs-c-start-regexp nil
394 "Regexp for beginning of comments.
395 Differs from mode-specific comment regexps in that
396 surrounding whitespace is stripped.")
397 (make-variable-buffer-local 'hs-c-start-regexp)
398
399 (defvar hs-block-start-regexp nil
400 "Regexp for beginning of block.")
401 (make-variable-buffer-local 'hs-block-start-regexp)
402
403 (defvar hs-block-start-mdata-select nil
404 "Element in `hs-block-start-regexp' match data to consider as block start.
405 The internal function `hs-forward-sexp' moves point to the beginning of this
406 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
407 (make-variable-buffer-local 'hs-block-start-mdata-select)
408
409 (defvar hs-block-end-regexp nil
410 "Regexp for end of block.")
411
412 (defvar hs-forward-sexp-func 'forward-sexp
413 "Function used to do a `forward-sexp'.
414 Should change for Algol-ish modes. For single-character block
415 delimiters -- ie, the syntax table regexp for the character is
416 either `(' or `)' -- `hs-forward-sexp-func' would just be
417 `forward-sexp'. For other modes such as simula, a more specialized
418 function is necessary.")
419 (make-variable-buffer-local 'hs-forward-sexp-func)
420
421 (defvar hs-adjust-block-beginning nil
422 "Function used to tweak the block beginning.
423 The block is hidden from the position returned by this function,
424 as opposed to hiding it from the position returned when searching
425 for `hs-block-start-regexp'.
426
427 For example, in c-like modes, if we wish to also hide the curly braces
428 \(if you think they occupy too much space on the screen), this function
429 should return the starting point (at the end of line) of the hidden
430 region.
431
432 It is called with a single argument ARG which is the position in
433 buffer after the block beginning.
434
435 It should return the position from where we should start hiding.
436
437 It should not move the point.
438
439 See `hs-c-like-adjust-block-beginning' for an example of using this.")
440 (make-variable-buffer-local 'hs-adjust-block-beginning)
441
442 (defvar hs-headline nil
443 "Text of the line where a hidden block begins, set during isearch.
444 You can display this in the mode line by adding the symbol `hs-headline'
445 to the variable `mode-line-format'. For example,
446
447 (unless (memq 'hs-headline mode-line-format)
448 (setq mode-line-format
449 (append '(\"-\" hs-headline) mode-line-format)))
450
451 Note that `mode-line-format' is buffer-local.")
452
453 ;;---------------------------------------------------------------------------
454 ;; support functions
455
456 (defun hs-discard-overlays (from to)
457 "Delete hideshow overlays in region defined by FROM and TO.
458 Skip \"internal\" overlays if `hs-allow-nesting' is non-nil."
459 (when (< to from)
460 (setq from (prog1 to (setq to from))))
461 (if hs-allow-nesting
462 (let (ov)
463 (while (> to (setq from (next-overlay-change from)))
464 (when (setq ov (hs-overlay-at from))
465 (setq from (overlay-end ov))
466 (delete-overlay ov))))
467 (dolist (ov (overlays-in from to))
468 (when (overlay-get ov 'hs)
469 (delete-overlay ov)))))
470
471 (defun hs-make-overlay (b e kind &optional b-offset e-offset)
472 "Return a new overlay in region defined by B and E with type KIND.
473 KIND is either `code' or `comment'. Optional fourth arg B-OFFSET
474 when added to B specifies the actual buffer position where the block
475 begins. Likewise for optional fifth arg E-OFFSET. If unspecified
476 they are taken to be 0 (zero). The following properties are set
477 in the overlay: 'invisible 'hs 'hs-b-offset 'hs-e-offset. Also,
478 depending on variable `hs-isearch-open', the following properties may
479 be present: 'isearch-open-invisible 'isearch-open-invisible-temporary.
480 If variable `hs-set-up-overlay' is non-nil it should specify a function
481 to call with the newly initialized overlay."
482 (unless b-offset (setq b-offset 0))
483 (unless e-offset (setq e-offset 0))
484 (let ((ov (make-overlay b e))
485 (io (if (eq 'block hs-isearch-open)
486 ;; backward compatibility -- `block'<=>`code'
487 'code
488 hs-isearch-open)))
489 (overlay-put ov 'invisible 'hs)
490 (overlay-put ov 'hs kind)
491 (overlay-put ov 'hs-b-offset b-offset)
492 (overlay-put ov 'hs-e-offset e-offset)
493 (when (or (eq io t) (eq io kind))
494 (overlay-put ov 'isearch-open-invisible 'hs-isearch-show)
495 (overlay-put ov 'isearch-open-invisible-temporary
496 'hs-isearch-show-temporary))
497 (when hs-set-up-overlay (funcall hs-set-up-overlay ov))
498 ov))
499
500 (defun hs-isearch-show (ov)
501 "Delete overlay OV, and set `hs-headline' to nil.
502
503 This function is meant to be used as the `isearch-open-invisible'
504 property of an overlay."
505 (setq hs-headline nil)
506 (delete-overlay ov))
507
508 (defun hs-isearch-show-temporary (ov hide-p)
509 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
510 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
511 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
512 OV is shown.
513
514 This function is meant to be used as the `isearch-open-invisible-temporary'
515 property of an overlay."
516 (setq hs-headline
517 (if hide-p
518 nil
519 (or hs-headline
520 (let ((start (overlay-start ov)))
521 (buffer-substring
522 (save-excursion (goto-char start)
523 (beginning-of-line)
524 (skip-chars-forward " \t")
525 (point))
526 start)))))
527 (force-mode-line-update)
528 ;; handle `display' property specially
529 (let (value)
530 (if hide-p
531 (when (setq value (overlay-get ov 'hs-isearch-display))
532 (overlay-put ov 'display value)
533 (overlay-put ov 'hs-isearch-display nil))
534 (when (setq value (overlay-get ov 'display))
535 (overlay-put ov 'hs-isearch-display value)
536 (overlay-put ov 'display nil))))
537 (overlay-put ov 'invisible (and hide-p 'hs)))
538
539 (defun hs-forward-sexp (match-data arg)
540 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
541 Original match data is restored upon return."
542 (save-match-data
543 (set-match-data match-data)
544 (goto-char (match-beginning hs-block-start-mdata-select))
545 (funcall hs-forward-sexp-func arg)))
546
547 (defun hs-hide-comment-region (beg end &optional repos-end)
548 "Hide a region from BEG to END, marking it as a comment.
549 Optional arg REPOS-END means reposition at end."
550 (let ((beg-eol (progn (goto-char beg) (line-end-position)))
551 (end-eol (progn (goto-char end) (line-end-position))))
552 (hs-discard-overlays beg-eol end-eol)
553 (hs-make-overlay beg-eol end-eol 'comment beg end))
554 (goto-char (if repos-end end beg)))
555
556 (defun hs-hide-block-at-point (&optional end comment-reg)
557 "Hide block if on block beginning.
558 Optional arg END means reposition at end.
559 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
560 specifies the limits of the comment, or nil if the block is not
561 a comment.
562
563 The block beginning is adjusted by `hs-adjust-block-beginning'
564 and then further adjusted to be at the end of the line."
565 (if comment-reg
566 (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
567 (when (looking-at hs-block-start-regexp)
568 (let* ((mdata (match-data t))
569 (header-beg (match-beginning 0))
570 (header-end (match-end 0))
571 p q ov)
572 ;; `p' is the point at the end of the block beginning, which
573 ;; may need to be adjusted
574 (save-excursion
575 (if hs-adjust-block-beginning
576 (goto-char (funcall hs-adjust-block-beginning
577 header-end))
578 (goto-char header-end))
579 (setq p (line-end-position)))
580 ;; `q' is the point at the end of the block
581 (hs-forward-sexp mdata 1)
582 (setq q (if (looking-back hs-block-end-regexp)
583 (match-beginning 0)
584 (point)))
585 (when (and (< p q) (> (count-lines p q) 1))
586 (cond ((and hs-allow-nesting (setq ov (hs-overlay-at p)))
587 (delete-overlay ov))
588 ((not hs-allow-nesting)
589 (hs-discard-overlays p q)))
590 (hs-make-overlay p q 'code (- header-end p)))
591 (goto-char (if end q (min p header-end)))))))
592
593 (defun hs-inside-comment-p ()
594 "Return non-nil if point is inside a comment, otherwise nil.
595 Actually, return a list containing the buffer position of the start
596 and the end of the comment. A comment block can be hidden only if on
597 its starting line there is only whitespace preceding the actual comment
598 beginning. If we are inside of a comment but this condition is not met,
599 we return a list having a nil as its car and the end of comment position
600 as cdr."
601 (save-excursion
602 ;; the idea is to look backwards for a comment start regexp, do a
603 ;; forward comment, and see if we are inside, then extend extend
604 ;; forward and backward as long as we have comments
605 (let ((q (point)))
606 (when (or (looking-at hs-c-start-regexp)
607 (re-search-backward hs-c-start-regexp (point-min) t))
608 ;; first get to the beginning of this comment...
609 (while (and (not (bobp))
610 (= (point) (progn (forward-comment -1) (point))))
611 (forward-char -1))
612 ;; ...then extend backwards
613 (forward-comment (- (buffer-size)))
614 (skip-chars-forward " \t\n\f")
615 (let ((p (point))
616 (hidable t))
617 (beginning-of-line)
618 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
619 ;; we are in this situation: (example)
620 ;; (defun bar ()
621 ;; (foo)
622 ;; ) ; comment
623 ;; ^
624 ;; the point was here before doing (beginning-of-line)
625 ;; here we should advance till the next comment which
626 ;; eventually has only white spaces preceding it on the same
627 ;; line
628 (goto-char p)
629 (forward-comment 1)
630 (skip-chars-forward " \t\n\f")
631 (setq p (point))
632 (while (and (< (point) q)
633 (> (point) p)
634 (not (looking-at hs-c-start-regexp)))
635 ;; avoid an infinite cycle
636 (setq p (point))
637 (forward-comment 1)
638 (skip-chars-forward " \t\n\f"))
639 (when (or (not (looking-at hs-c-start-regexp))
640 (> (point) q))
641 ;; we cannot hide this comment block
642 (setq hidable nil)))
643 ;; goto the end of the comment
644 (forward-comment (buffer-size))
645 (skip-chars-backward " \t\n\f")
646 (end-of-line)
647 (when (>= (point) q)
648 (list (and hidable p) (point))))))))
649
650 (defun hs-grok-mode-type ()
651 "Set up hideshow variables for new buffers.
652 If `hs-special-modes-alist' has information associated with the
653 current buffer's major mode, use that.
654 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
655 function; and adjust-block-beginning function."
656 (if (and (boundp 'comment-start)
657 (boundp 'comment-end)
658 comment-start comment-end)
659 (let* ((lookup (assoc major-mode hs-special-modes-alist))
660 (start-elem (or (nth 1 lookup) "\\s(")))
661 (if (listp start-elem)
662 ;; handle (START-REGEXP MDATA-SELECT)
663 (setq hs-block-start-regexp (car start-elem)
664 hs-block-start-mdata-select (cadr start-elem))
665 ;; backwards compatibility: handle simple START-REGEXP
666 (setq hs-block-start-regexp start-elem
667 hs-block-start-mdata-select 0))
668 (setq hs-block-end-regexp (or (nth 2 lookup) "\\s)")
669 hs-c-start-regexp (or (nth 3 lookup)
670 (let ((c-start-regexp
671 (regexp-quote comment-start)))
672 (if (string-match " +$" c-start-regexp)
673 (substring c-start-regexp
674 0 (1- (match-end 0)))
675 c-start-regexp)))
676 hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
677 hs-adjust-block-beginning (nth 5 lookup)))
678 (setq hs-minor-mode nil)
679 (error "%s Mode doesn't support Hideshow Minor Mode"
680 (format-mode-line mode-name))))
681
682 (defun hs-find-block-beginning ()
683 "Reposition point at block-start.
684 Return point, or nil if original point was not in a block."
685 (let ((done nil)
686 (here (point)))
687 ;; look if current line is block start
688 (if (looking-at hs-block-start-regexp)
689 (point)
690 ;; look backward for the start of a block that contains the cursor
691 (while (and (re-search-backward hs-block-start-regexp nil t)
692 (not (setq done
693 (< here (save-excursion
694 (hs-forward-sexp (match-data t) 1)
695 (point)))))))
696 (if done
697 (point)
698 (goto-char here)
699 nil))))
700
701 (defun hs-hide-level-recursive (arg minp maxp)
702 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
703 (when (hs-find-block-beginning)
704 (setq minp (1+ (point)))
705 (funcall hs-forward-sexp-func 1)
706 (setq maxp (1- (point))))
707 (unless hs-allow-nesting
708 (hs-discard-overlays minp maxp))
709 (goto-char minp)
710 (while (progn
711 (forward-comment (buffer-size))
712 (and (< (point) maxp)
713 (re-search-forward hs-block-start-regexp maxp t)))
714 (if (> arg 1)
715 (hs-hide-level-recursive (1- arg) minp maxp)
716 (goto-char (match-beginning hs-block-start-mdata-select))
717 (hs-hide-block-at-point t)))
718 (goto-char maxp))
719
720 (defmacro hs-life-goes-on (&rest body)
721 "Evaluate BODY forms if variable `hs-minor-mode' is non-nil.
722 In the dynamic context of this macro, `inhibit-point-motion-hooks'
723 and `case-fold-search' are both t."
724 `(when hs-minor-mode
725 (let ((inhibit-point-motion-hooks t)
726 (case-fold-search t))
727 ,@body)))
728
729 (put 'hs-life-goes-on 'edebug-form-spec '(&rest form))
730
731 (defun hs-overlay-at (position)
732 "Return hideshow overlay at POSITION, or nil if none to be found."
733 (let ((overlays (overlays-at position))
734 ov found)
735 (while (and (not found) (setq ov (car overlays)))
736 (setq found (and (overlay-get ov 'hs) ov)
737 overlays (cdr overlays)))
738 found))
739
740 (defun hs-already-hidden-p ()
741 "Return non-nil if point is in an already-hidden block, otherwise nil."
742 (save-excursion
743 (let ((c-reg (hs-inside-comment-p)))
744 (if (and c-reg (nth 0 c-reg))
745 ;; point is inside a comment, and that comment is hidable
746 (goto-char (nth 0 c-reg))
747 (end-of-line)
748 (when (and (not c-reg)
749 (hs-find-block-beginning)
750 (looking-at hs-block-start-regexp))
751 ;; point is inside a block
752 (goto-char (match-end 0)))))
753 (end-of-line)
754 (hs-overlay-at (point))))
755
756 ;; This function is not used anymore (Bug#700).
757 (defun hs-c-like-adjust-block-beginning (initial)
758 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
759 Actually, point is never moved; a new position is returned that is
760 the end of the C-function header. This adjustment function is meant
761 to be assigned to `hs-adjust-block-beginning' for C-like modes."
762 (save-excursion
763 (goto-char (1- initial))
764 (forward-comment (- (buffer-size)))
765 (point)))
766
767 ;;---------------------------------------------------------------------------
768 ;; commands
769
770 (defun hs-hide-all ()
771 "Hide all top level blocks, displaying only first and last lines.
772 Move point to the beginning of the line, and run the normal hook
773 `hs-hide-hook'. See documentation for `run-hooks'.
774 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
775 (interactive)
776 (hs-life-goes-on
777 (save-excursion
778 (unless hs-allow-nesting
779 (hs-discard-overlays (point-min) (point-max)))
780 (goto-char (point-min))
781 (let ((spew (make-progress-reporter "Hiding all blocks..."
782 (point-min) (point-max)))
783 (re (concat "\\("
784 hs-block-start-regexp
785 "\\)"
786 (if hs-hide-comments-when-hiding-all
787 (concat "\\|\\("
788 hs-c-start-regexp
789 "\\)")
790 ""))))
791 (while (progn
792 (unless hs-hide-comments-when-hiding-all
793 (forward-comment (point-max)))
794 (re-search-forward re (point-max) t))
795 (if (match-beginning 1)
796 ;; we have found a block beginning
797 (progn
798 (goto-char (match-beginning 1))
799 (if hs-hide-all-non-comment-function
800 (funcall hs-hide-all-non-comment-function)
801 (hs-hide-block-at-point t)))
802 ;; found a comment, probably
803 (let ((c-reg (hs-inside-comment-p)))
804 (when (and c-reg (car c-reg))
805 (if (> (count-lines (car c-reg) (nth 1 c-reg)) 1)
806 (hs-hide-block-at-point t c-reg)
807 (goto-char (nth 1 c-reg))))))
808 (progress-reporter-update spew (point)))
809 (progress-reporter-done spew)))
810 (beginning-of-line)
811 (run-hooks 'hs-hide-hook)))
812
813 (defun hs-show-all ()
814 "Show everything then run `hs-show-hook'. See `run-hooks'."
815 (interactive)
816 (hs-life-goes-on
817 (message "Showing all blocks ...")
818 (let ((hs-allow-nesting nil))
819 (hs-discard-overlays (point-min) (point-max)))
820 (message "Showing all blocks ... done")
821 (run-hooks 'hs-show-hook)))
822
823 (defun hs-hide-block (&optional end)
824 "Select a block and hide it. With prefix arg, reposition at END.
825 Upon completion, point is repositioned and the normal hook
826 `hs-hide-hook' is run. See documentation for `run-hooks'."
827 (interactive "P")
828 (hs-life-goes-on
829 (let ((c-reg (hs-inside-comment-p)))
830 (cond
831 ((and c-reg (or (null (nth 0 c-reg))
832 (<= (count-lines (car c-reg) (nth 1 c-reg)) 1)))
833 (message "(not enough comment lines to hide)"))
834 ((or c-reg
835 (looking-at hs-block-start-regexp)
836 (hs-find-block-beginning))
837 (hs-hide-block-at-point end c-reg)
838 (run-hooks 'hs-hide-hook))))))
839
840 (defun hs-show-block (&optional end)
841 "Select a block and show it.
842 With prefix arg, reposition at END. Upon completion, point is
843 repositioned and the normal hook `hs-show-hook' is run.
844 See documentation for functions `hs-hide-block' and `run-hooks'."
845 (interactive "P")
846 (hs-life-goes-on
847 (or
848 ;; first see if we have something at the end of the line
849 (let ((ov (hs-overlay-at (line-end-position)))
850 (here (point)))
851 (when ov
852 (goto-char
853 (cond (end (overlay-end ov))
854 ((eq 'comment (overlay-get ov 'hs)) here)
855 (t (+ (overlay-start ov) (overlay-get ov 'hs-b-offset)))))
856 (delete-overlay ov)
857 t))
858 ;; not immediately obvious, look for a suitable block
859 (let ((c-reg (hs-inside-comment-p))
860 p q)
861 (cond (c-reg
862 (when (car c-reg)
863 (setq p (car c-reg)
864 q (cadr c-reg))))
865 ((and (hs-find-block-beginning)
866 ;; ugh, fresh match-data
867 (looking-at hs-block-start-regexp))
868 (setq p (point)
869 q (progn (hs-forward-sexp (match-data t) 1) (point)))))
870 (when (and p q)
871 (hs-discard-overlays p q)
872 (goto-char (if end q (1+ p))))))
873 (run-hooks 'hs-show-hook)))
874
875 (defun hs-hide-level (arg)
876 "Hide all blocks ARG levels below this block.
877 The hook `hs-hide-hook' is run; see `run-hooks'."
878 (interactive "p")
879 (hs-life-goes-on
880 (save-excursion
881 (message "Hiding blocks ...")
882 (hs-hide-level-recursive arg (point-min) (point-max))
883 (message "Hiding blocks ... done"))
884 (run-hooks 'hs-hide-hook)))
885
886 (defun hs-toggle-hiding ()
887 "Toggle hiding/showing of a block.
888 See `hs-hide-block' and `hs-show-block'."
889 (interactive)
890 (hs-life-goes-on
891 (if (hs-already-hidden-p)
892 (hs-show-block)
893 (hs-hide-block))))
894
895 (defun hs-mouse-toggle-hiding (e)
896 "Toggle hiding/showing of a block.
897 This command should be bound to a mouse key.
898 Argument E is a mouse event used by `mouse-set-point'.
899 See `hs-hide-block' and `hs-show-block'."
900 (interactive "@e")
901 (hs-life-goes-on
902 (mouse-set-point e)
903 (hs-toggle-hiding)))
904
905 (defun hs-hide-initial-comment-block ()
906 "Hide the first block of comments in a file.
907 This can be useful if you have huge RCS logs in those comments."
908 (interactive)
909 (hs-life-goes-on
910 (let ((c-reg (save-excursion
911 (goto-char (point-min))
912 (skip-chars-forward " \t\n\f")
913 (hs-inside-comment-p))))
914 (when c-reg
915 (let ((beg (car c-reg)) (end (cadr c-reg)))
916 ;; see if we have enough comment lines to hide
917 (when (> (count-lines beg end) 1)
918 (hs-hide-comment-region beg end)))))))
919
920 ;;;###autoload
921 (define-minor-mode hs-minor-mode
922 "Minor mode to selectively hide/show code and comment blocks.
923 When hideshow minor mode is on, the menu bar is augmented with hideshow
924 commands and the hideshow commands are enabled.
925 The value '(hs . t) is added to `buffer-invisibility-spec'.
926
927 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
928 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
929 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
930
931 Turning hideshow minor mode off reverts the menu bar and the
932 variables to default values and disables the hideshow commands.
933
934 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
935
936 Key bindings:
937 \\{hs-minor-mode-map}"
938 :group 'hideshow
939 :lighter " hs"
940 :keymap hs-minor-mode-map
941 (setq hs-headline nil)
942 (if hs-minor-mode
943 (progn
944 (hs-grok-mode-type)
945 ;; Turn off this mode if we change major modes.
946 (add-hook 'change-major-mode-hook
947 'turn-off-hideshow
948 nil t)
949 (easy-menu-add hs-minor-mode-menu)
950 (set (make-local-variable 'line-move-ignore-invisible) t)
951 (add-to-invisibility-spec '(hs . t)))
952 (remove-from-invisibility-spec '(hs . t))
953 ;; hs-show-all does nothing unless h-m-m is non-nil.
954 (let ((hs-minor-mode t))
955 (hs-show-all))))
956
957 ;;;###autoload
958 (defun turn-off-hideshow ()
959 "Unconditionally turn off `hs-minor-mode'."
960 (hs-minor-mode -1))
961
962 ;;---------------------------------------------------------------------------
963 ;; that's it
964
965 (provide 'hideshow)
966
967 ;;; hideshow.el ends here