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