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