1 ;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01 Free Software Foundation
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.31
9 ;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
11 ;; XEmacs port by Jerry James <james@xemacs.org>
12 ;; This port consists almost entirely of substituting extent manipulations for
13 ;; overlay manipulations.
15 ;; This file is part of XEmacs.
17 ;; XEmacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; XEmacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with XEmacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
34 ;; * Commands provided
36 ;; This file provides Hideshow Minor Mode. When active, nine commands
37 ;; are available, implementing block hiding and showing. They (and their
40 ;; hs-hide-block C-c @ C-h
41 ;; hs-show-block C-c @ C-s
42 ;; hs-hide-all C-c @ C-M-h
43 ;; hs-show-all C-c @ C-M-s
44 ;; hs-hide-level C-c @ C-l
45 ;; hs-toggle-hiding C-c @ C-c
46 ;; hs-mouse-toggle-hiding [(shift mouse-2)]
47 ;; hs-hide-initial-comment-block
49 ;; Blocks are defined per mode. In c-mode, c++-mode and java-mode, they
50 ;; are simply text between curly braces, while in Lisp-ish modes parens
51 ;; are used. Multi-line comment blocks can also be hidden. Read-only
52 ;; buffers are not a problem, since hideshow doesn't modify the text.
54 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
55 ;; (similar to other minor modes).
59 ;; First make sure hideshow.el is in a directory in your `load-path'.
60 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
61 ;; Then, add the following to your ~/.emacs:
63 ;; (load-library "hideshow")
64 ;; (add-hook 'X-mode-hook ; other modes similarly
65 ;; '(lambda () (hs-minor-mode 1)))
67 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
68 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
69 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
71 ;; Additionally, Joseph Eydelnant writes:
72 ;; I enjoy your package hideshow.el Ver. 5.24 2001/02/13
73 ;; a lot and I've been looking for the following functionality:
74 ;; toggle hide/show all with a single key.
75 ;; Here are a few lines of code that lets me do just that.
77 ;; (defvar my-hs-hide nil "Current state of hideshow for toggling all.")
79 ;; (defun my-toggle-hideshow-all () "Toggle hideshow all."
81 ;; (setq my-hs-hide (not my-hs-hide))
86 ;; [Your hideshow hacks here!]
90 ;; You can use `M-x customize-variable' on the following variables:
92 ;; - hs-hide-comments-when-hiding-all -- self-explanatory!
93 ;; - hs-hide-all-non-comment-function -- if non-nil, when doing a
94 ;; `hs-hide-all', this function
95 ;; is called w/ no arguments
96 ;; - hs-isearch-open -- what kind of hidden blocks to
97 ;; open when doing isearch
99 ;; Some languages (e.g., Java) are deeply nested, so the normal behavior
100 ;; of `hs-hide-all' (hiding all but top-level blocks) results in very
101 ;; little information shown, which is not very useful. You can use the
102 ;; variable `hs-hide-all-non-comment-function' to implement your idea of
103 ;; what is more useful. For example, the following code shows the next
104 ;; nested level in addition to the top-level:
106 ;; (defun ttn-hs-hide-level-1 ()
109 ;; (setq hs-hide-all-non-comment-function 'ttn-hs-hide-level-1)
111 ;; Hideshow works w/ incremental search (isearch) by setting the variable
112 ;; `hs-headline', which is the line of text at the beginning of a hidden
113 ;; block that contains a match for the search. You can have this show up
114 ;; in the mode line by modifying the variable `mode-line-format'. For
115 ;; example, the following code prepends this info to the mode line:
117 ;; (unless (memq 'hs-headline mode-line-format)
118 ;; (setq mode-line-format
119 ;; (append '("-" hs-headline) mode-line-format)))
121 ;; See documentation for `mode-line-format' for more info.
123 ;; Hooks are run after some commands:
125 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
126 ;; hs-show-hook hs-show-block, hs-show-all
128 ;; One of `hs-hide-hook' or `hs-show-hook' is run for the toggling
129 ;; commands when the result of the toggle is to hide or show blocks,
130 ;; respectively. All hooks are run w/ `run-hooks'. See docs for each
131 ;; variable or hook for more info.
133 ;; Normally, hideshow tries to determine appropriate values for block
134 ;; and comment definitions by examining the buffer's major mode. If
135 ;; there are problems, hideshow will not activate and in that case you
136 ;; may wish to override hideshow's heuristics by adding an entry to
137 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
138 ;; do something like:
140 ;; (let ((my-mode-hs-info '(my-mode "{{" "}}" ...)))
141 ;; (if (not (member my-mode-hs-info hs-special-modes-alist))
142 ;; (setq hs-special-modes-alist
143 ;; (cons my-mode-hs-info hs-special-modes-alist))))
145 ;; If you have an entry that works particularly well, consider
146 ;; submitting it for inclusion in hideshow.el. See docstring for
147 ;; `hs-special-modes-alist' for more info on the entry format.
151 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
152 ;; function `forward-comment' (among other things). If someone
153 ;; writes this, please send me a copy.
155 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
156 ;; `M-x hs-minor-mode' twice (that is, deactivate then re-activate
159 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.7.
160 ;; XEmacs compatibility may have bitrotted since 4.29.
161 ;; [Jerry says: XEmacs compatibility is back! Give it a try.]
163 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
164 ;; `byte-compile-file' inserts the file to be compiled in a temporary
165 ;; buffer and switches `normal-mode' on. In the case where you have
166 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
167 ;; the initial comment sometimes hides parts of the first statement (seems
168 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
170 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
172 ;; (defadvice byte-compile-file (around
173 ;; byte-compile-file-hideshow-off
175 ;; (let ((hs-minor-mode-hook nil))
178 ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the
179 ;; suggested workaround is to turn off hideshow entirely, for example:
181 ;; (defun turn-off-hideshow () (hs-minor-mode -1))
182 ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow)
183 ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow)
185 ;; In the case of `vc-diff', here is a less invasive workaround:
187 ;; (add-hook 'vc-before-checkin-hook
189 ;; (goto-char (point-min))
192 ;; Unfortunately, these workarounds do not restore hideshow state.
193 ;; If someone figures out a better way, please let me know.
197 ;; Correspondance welcome; please indicate version number. Send bug
198 ;; reports and inquiries to <ttn@gnu.org>.
199 ;; If the bug appears to be XEmacs-specific, use M-x report-emacs-bug instead.
203 ;; Thanks go to the following people for valuable ideas, code and
206 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave
207 ;; Love, Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray,
208 ;; Moody Ahmad, Preston F. Crow, Lars Lindberg, Reto Zimmermann,
209 ;; Keith Sheffield, Chew Meng Kuan, Tony Lam, Pete Ware, François
210 ;; Pinard, Stefan Monnier, Joseph Eydelnant
212 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
213 ;; overlays (rather than selective display), added isearch magic, folded
214 ;; in custom.el compatibility, generalized comment handling, incorporated
215 ;; mouse support, and maintained the code in general. Version 4.0 is
216 ;; largely due to his efforts.
220 ;; Hideshow was inspired when I learned about selective display. It was
221 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
222 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
223 ;; been lost. XEmacs support is reliable as of 4.29. State save and
224 ;; restore was added in 3.5 (not widely distributed), and reliable as of
225 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
226 ;; Version 5.x uses new algorithms for block selection and traversal,
227 ;; unbundles state save and restore, and includes more isearch support.
233 ;;---------------------------------------------------------------------------
234 ;; user-configurable variables
236 (defgroup hideshow nil
237 "Minor mode for hiding and showing program and comment blocks."
242 (defcustom hs-hide-comments-when-hiding-all t
243 "*Hide the comments too when you do an `hs-hide-all'."
247 (defcustom hs-minor-mode-hook nil
248 "*Hook called when hideshow minor mode is activated or deactivated."
253 (defcustom hs-isearch-open 'code
254 "*What kind of hidden blocks to open when doing `isearch'.
255 One of the following symbols:
257 code -- open only code blocks
258 comment -- open only comment blocks
259 t -- open both code and comment blocks
260 nil -- open neither code nor comment blocks
262 This has effect iff `search-invisible' is set to `open'."
263 :type '(choice (const :tag "open only code blocks" code)
264 (const :tag "open only comment blocks" comment)
265 (const :tag "open both code and comment blocks" t)
266 (const :tag "don't open any of them" nil))
270 (defvar hs-special-modes-alist
271 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
272 (c++-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
273 (bibtex-mode ("^@\\S(*\\(\\s(\\)" 1))
274 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
276 "*Alist for initializing the hideshow variables for different modes.
277 Each element has the form
278 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
280 If non-nil, hideshow will use these values as regexps to define blocks
281 and comments, respectively for major mode MODE.
283 START, END and COMMENT-START are regular expressions. A block is
284 defined as text surrounded by START and END.
286 As a special case, START may be a list of the form (COMPLEX-START
287 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
288 MDATA-SELECTOR an integer that specifies which sub-match is the proper
289 place to adjust point, before calling `hs-forward-sexp-func'. For
290 example, see the `hs-special-modes-alist' entry for `bibtex-mode'.
292 For some major modes, `forward-sexp' does not work properly. In those
293 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
295 See the documentation for `hs-adjust-block-beginning' to see what is the
296 use of ADJUST-BEG-FUNC.
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.")
302 (defvar hs-hide-all-non-comment-function nil
303 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
305 (defvar hs-hide-hook nil
306 "*Hook called (with `run-hooks') at the end of commands to hide text.
307 These commands include the toggling commands (when the result is to hide
308 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
310 (defvar hs-show-hook nil
311 "*Hook called (with `run-hooks') at the end of commands to show text.
312 These commands include the toggling commands (when the result is to show
313 a block), `hs-show-all' and `hs-show-block'..")
315 ;;---------------------------------------------------------------------------
316 ;; internal variables
318 (defvar hs-minor-mode nil
319 "Non-nil if using hideshow mode as a minor mode of some other mode.
320 Use the command `hs-minor-mode' to toggle or set this variable.")
322 (defvar hs-minor-mode-map nil
323 "Keymap for hideshow minor mode.")
325 (defvar hs-minor-mode-menu nil
326 "Menu for hideshow minor mode.")
328 (defvar hs-c-start-regexp nil
329 "Regexp for beginning of comments.
330 Differs from mode-specific comment regexps in that
331 surrounding whitespace is stripped.")
333 (defvar hs-block-start-regexp nil
334 "Regexp for beginning of block.")
336 (defvar hs-block-start-mdata-select nil
337 "Element in `hs-block-start-regexp' match data to consider as block start.
338 The internal function `hs-forward-sexp' moves point to the beginning of this
339 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
341 (defvar hs-block-end-regexp nil
342 "Regexp for end of block.")
344 (defvar hs-forward-sexp-func 'forward-sexp
345 "Function used to do a `forward-sexp'.
346 Should change for Algol-ish modes. For single-character block
347 delimiters -- ie, the syntax table regexp for the character is
348 either `(' or `)' -- `hs-forward-sexp-func' would just be
349 `forward-sexp'. For other modes such as simula, a more specialized
350 function is necessary.")
352 (defvar hs-adjust-block-beginning nil
353 "Function used to tweak the block beginning.
354 The block is hidden from the position returned by this function,
355 as opposed to hiding it from the position returned when searching
356 for `hs-block-start-regexp'.
358 For example, in c-like modes, if we wish to also hide the curly braces
359 (if you think they occupy too much space on the screen), this function
360 should return the starting point (at the end of line) of the hidden
363 It is called with a single argument ARG which is the the position in
364 buffer after the block beginning.
366 It should return the position from where we should start hiding.
368 It should not move the point.
370 See `hs-c-like-adjust-block-beginning' for an example of using this.")
372 (defvar hs-headline nil
373 "Text of the line where a hidden block begins, set during isearch.
374 You can display this in the mode line by adding the symbol `hs-headline'
375 to the variable `mode-line-format'. For example,
377 (unless (memq 'hs-headline mode-line-format)
378 (setq mode-line-format
379 (append '(\"-\" hs-headline) mode-line-format)))
381 Note that `mode-line-format' is buffer-local.")
383 ;;---------------------------------------------------------------------------
386 ;; XEmacs and emacs-19 compatibility
387 (if (not (fboundp 'add-to-invisibility-spec))
388 (defun add-to-invisibility-spec (arg)
389 "Add elements to `buffer-invisibility-spec'.
390 See documentation for `buffer-invisibility-spec' for the kind of elements
392 (if (eq buffer-invisibility-spec t)
393 (setq buffer-invisibility-spec (list t)))
394 (setq buffer-invisibility-spec
395 (cons arg buffer-invisibility-spec))))
397 (if (not (fboundp 'remove-from-invisibility-spec))
398 (defun remove-from-invisibility-spec (arg)
399 "Remove elements from `buffer-invisibility-spec'."
400 (if (consp buffer-invisibility-spec)
401 (setq buffer-invisibility-spec
402 (delete arg buffer-invisibility-spec)))))
405 (defalias 'hs-match-data 'match-data)
407 ;;---------------------------------------------------------------------------
410 (defun hs-discard-extents (from to)
411 "Delete hideshow extents in region defined by FROM and TO."
413 (setq from (prog1 to (setq to from))))
414 (map-extents #'(lambda (ex ignored) (delete-extent ex))
415 (current-buffer) from to nil 'end-closed 'hs))
417 (defun hs-isearch-show (ex)
418 "Delete extent EX, and set `hs-headline' to nil.
420 This function is meant to be used as the `isearch-open-invisible'
421 property of an extent."
422 (setq hs-headline nil)
425 (defun hs-isearch-show-temporary (ex hide-p)
426 "Hide or show extent EX, and set `hs-headline', all depending on HIDE-P.
427 If HIDE-P is non-nil, `hs-headline' is set to nil and extent EX is hidden.
428 Otherwise, `hs-headline' is set to the line of text at the head of EX, and
431 This function is meant to be used as the `isearch-open-invisible-temporary'
432 property of an extent."
437 (let ((start (extent-start-position ex)))
439 (save-excursion (goto-char start)
441 (skip-chars-forward " \t")
444 (force-mode-line-update)
445 (set-extent-property ex 'invisible (and hide-p 'hs)))
447 (defun hs-flag-region (from to flag)
448 "Hide or show lines from FROM to TO, according to FLAG.
449 If FLAG is nil then text is shown, while if FLAG is non-nil the text is
450 hidden. FLAG must be one of the symbols `code' or `comment', depending
451 on what kind of block is to be hidden."
453 (setq from (prog1 to (setq to from))))
455 ;; first clear it all out
456 (hs-discard-extents from to)
457 ;; now create extents if needed
459 (let ((ex (make-extent from to)))
460 (set-extent-property ex 'invisible 'hs)
461 (set-extent-property ex 'hs flag)
462 (when (or (eq hs-isearch-open t)
463 (eq hs-isearch-open flag)
464 ;; deprecated backward compatibility -- `block'<=>`code'
465 (and (eq 'block hs-isearch-open)
467 (set-extent-property ex 'isearch-open-invisible 'hs-isearch-show)
468 (set-extent-property ex 'isearch-open-invisible-temporary
469 'hs-isearch-show-temporary))
472 (defun hs-forward-sexp (match-data arg)
473 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
474 Original match data is restored upon return."
476 (set-match-data match-data)
477 (goto-char (match-beginning hs-block-start-mdata-select))
478 (funcall hs-forward-sexp-func arg)))
480 (defun hs-hide-comment-region (beg end &optional repos-end)
481 "Hide a region from BEG to END, marking it as a comment.
482 Optional arg REPOS-END means reposition at end."
483 (hs-flag-region (progn (goto-char beg) (end-of-line) (point))
484 (progn (goto-char end) (end-of-line) (point))
486 (goto-char (if repos-end end beg)))
488 (defun hs-hide-block-at-point (&optional end comment-reg)
489 "Hide block iff on block beginning.
490 Optional arg END means reposition at end.
491 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
492 specifies the limits of the comment, or nil if the block is not
495 The block beginning is adjusted by `hs-adjust-block-beginning'
496 and then further adjusted to be at the end of the line."
498 (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
499 (if (looking-at hs-block-start-regexp)
500 (let* ((mdata (hs-match-data t))
501 (pure-p (match-end 0))
503 ;; `p' is the point at the end of the block beginning,
504 ;; which may need to be adjusted
506 (goto-char (funcall (or hs-adjust-block-beginning
509 ;; whatever the adjustment, we move to eol
513 ;; `q' is the point at the end of the block
514 (progn (hs-forward-sexp mdata 1)
517 (if (and (< p (point)) (> (count-lines p q) 1))
518 (set-extent-property (hs-flag-region p q 'code)
521 (goto-char (if end q (min p pure-p)))))))
523 (defun hs-safety-is-job-n ()
524 "Warn if `buffer-invisibility-spec' does not contain symbol `hs'."
525 (unless (and (listp buffer-invisibility-spec)
526 (assq 'hs buffer-invisibility-spec))
527 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
530 (defun hs-inside-comment-p ()
531 "Return non-nil if point is inside a comment, otherwise nil.
532 Actually, return a list containing the buffer position of the start
533 and the end of the comment. A comment block can be hidden only if on
534 its starting line there is only whitespace preceding the actual comment
535 beginning. If we are inside of a comment but this condition is not met,
536 we return a list having a nil as its car and the end of comment position
539 ;; the idea is to look backwards for a comment start regexp, do a
540 ;; forward comment, and see if we are inside, then extend extend
541 ;; forward and backward as long as we have comments
543 (when (or (looking-at hs-c-start-regexp)
544 (re-search-backward hs-c-start-regexp (point-min) t))
545 (forward-comment (- (buffer-size)))
546 (skip-chars-forward " \t\n\f")
550 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
551 ;; we are in this situation: (example)
556 ;; the point was here before doing (beginning-of-line)
557 ;; here we should advance till the next comment which
558 ;; eventually has only white spaces preceding it on the same
562 (skip-chars-forward " \t\n\f")
564 (while (and (< (point) q)
566 (not (looking-at hs-c-start-regexp)))
567 (setq p (point));; use this to avoid an infinite cycle
569 (skip-chars-forward " \t\n\f"))
570 (if (or (not (looking-at hs-c-start-regexp))
572 ;; we cannot hide this comment block
573 (setq not-hidable t)))
574 ;; goto the end of the comment
575 (forward-comment (buffer-size))
576 (skip-chars-backward " \t\n\f")
579 (list (if not-hidable nil p) (point))))))))
581 (defun hs-grok-mode-type ()
582 "Set up hideshow variables for new buffers.
583 If `hs-special-modes-alist' has information associated with the
584 current buffer's major mode, use that.
585 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
586 function; and adjust-block-beginning function."
587 (if (and (boundp 'comment-start)
588 (boundp 'comment-end)
589 comment-start comment-end)
590 (let* ((lookup (assoc major-mode hs-special-modes-alist))
591 (start-elem (or (nth 1 lookup) "\\s(")))
592 (if (listp start-elem)
593 ;; handle (START-REGEXP MDATA-SELECT)
594 (setq hs-block-start-regexp (car start-elem)
595 hs-block-start-mdata-select (cadr start-elem))
596 ;; backwards compatibility: handle simple START-REGEXP
597 (setq hs-block-start-regexp start-elem
598 hs-block-start-mdata-select 0))
599 (setq hs-block-end-regexp (or (nth 2 lookup) "\\s)")
600 hs-c-start-regexp (or (nth 3 lookup)
601 (let ((c-start-regexp
602 (regexp-quote comment-start)))
603 (if (string-match " +$" c-start-regexp)
604 (substring c-start-regexp
605 0 (1- (match-end 0)))
607 hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
608 hs-adjust-block-beginning (nth 5 lookup)))
610 (setq hs-minor-mode nil)
611 (error "%s Mode doesn't support Hideshow Minor Mode" mode-name))))
613 (defun hs-find-block-beginning ()
614 "Reposition point at block-start.
615 Return point, or nil if original point was not in a block."
618 ;; look if current line is block start
619 (if (looking-at hs-block-start-regexp)
621 ;; look backward for the start of a block that contains the cursor
622 (while (and (re-search-backward hs-block-start-regexp nil t)
624 (< here (save-excursion
625 (hs-forward-sexp (hs-match-data t) 1)
632 (defun hs-hide-level-recursive (arg minp maxp)
633 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
634 (when (hs-find-block-beginning)
635 (setq minp (1+ (point)))
636 (funcall hs-forward-sexp-func 1)
637 (setq maxp (1- (point))))
638 (hs-flag-region minp maxp nil) ; eliminate weirdness
641 (forward-comment (buffer-size))
642 (and (< (point) maxp)
643 (re-search-forward hs-block-start-regexp maxp t)))
645 (hs-hide-level-recursive (1- arg) minp maxp)
646 (goto-char (match-beginning hs-block-start-mdata-select))
647 (hs-hide-block-at-point t)))
651 (defmacro hs-life-goes-on (&rest body)
652 "Evaluate BODY forms iff variable `hs-minor-mode' is non-nil.
653 In the dynamic context of this macro `case-fold-search' is t."
655 (let ((case-fold-search t))
658 (put 'hs-life-goes-on 'edebug-form-spec '(&rest form))
660 (defun hs-already-hidden-p ()
661 "Return non-nil if point is in an already-hidden block, otherwise nil."
663 (let ((c-reg (hs-inside-comment-p)))
664 (if (and c-reg (nth 0 c-reg))
665 ;; point is inside a comment, and that comment is hidable
666 (goto-char (nth 0 c-reg))
668 (hs-find-block-beginning)
669 (looking-at hs-block-start-regexp))
670 ;; point is inside a block
671 (goto-char (match-end 0)))))
673 (extent-at (point) (current-buffer) 'hs nil 'at)))
675 (defun hs-c-like-adjust-block-beginning (initial)
676 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
677 Actually, point is never moved; a new position is returned that is
678 the end of the C-function header. This adjustment function is meant
679 to be assigned to `hs-adjust-block-beginning' for C-like modes."
681 (goto-char (1- initial))
682 (forward-comment (- (buffer-size)))
685 ;;---------------------------------------------------------------------------
688 (defun hs-hide-all ()
689 "Hide all top level blocks, displaying only first and last lines.
690 Move point to the beginning of the line, and run the normal hook
691 `hs-hide-hook'. See documentation for `run-hooks'.
692 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
695 (message "Hiding all blocks ...")
697 (hs-flag-region (point-min) (point-max) nil) ; eliminate weirdness
698 (goto-char (point-min))
701 hs-block-start-regexp
703 (if hs-hide-comments-when-hiding-all
709 (unless hs-hide-comments-when-hiding-all
710 (forward-comment (point-max)))
711 (re-search-forward re (point-max) t))
712 (if (match-beginning 1)
713 ;; we have found a block beginning
715 (goto-char (match-beginning 1))
716 (if hs-hide-all-non-comment-function
717 (funcall hs-hide-all-non-comment-function)
718 (hs-hide-block-at-point t)))
719 ;; found a comment, probably
720 (let ((c-reg (hs-inside-comment-p))) ; blech!
721 (when (and c-reg (car c-reg))
722 (if (> (count-lines (car c-reg) (nth 1 c-reg)) 1)
723 (hs-hide-block-at-point t c-reg)
724 (goto-char (nth 1 c-reg))))))
725 (message "Hiding ... %d" (setq count (1+ count)))))
726 (hs-safety-is-job-n))
728 (message "Hiding all blocks ... done")
729 (run-hooks 'hs-hide-hook)))
731 (defun hs-show-all ()
732 "Show everything then run `hs-show-hook'. See `run-hooks'."
735 (message "Showing all blocks ...")
736 (hs-flag-region (point-min) (point-max) nil)
737 (message "Showing all blocks ... done")
738 (run-hooks 'hs-show-hook)))
740 (defun hs-hide-block (&optional end)
741 "Select a block and hide it. With prefix arg, reposition at END.
742 Upon completion, point is repositioned and the normal hook
743 `hs-hide-hook' is run. See documentation for `run-hooks'."
746 (let ((c-reg (hs-inside-comment-p)))
748 ((and c-reg (or (null (nth 0 c-reg))
749 (<= (count-lines (car c-reg) (nth 1 c-reg)) 1)))
750 (message "(not enough comment lines to hide)"))
752 (looking-at hs-block-start-regexp)
753 (hs-find-block-beginning))
754 (hs-hide-block-at-point end c-reg)
756 (run-hooks 'hs-hide-hook))))))
758 (defun hs-show-block (&optional end)
759 "Select a block and show it.
760 With prefix arg, reposition at END. Upon completion, point is
761 repositioned and the normal hook `hs-show-hook' is run.
762 See documentation for functions `hs-hide-block' and `run-hooks'."
766 ;; first see if we have something at the end of the line
767 (let* ((here (point))
768 (buf (current-buffer))
769 (ex (extent-at (point-at-eol nil buf) buf 'hs nil 'at)))
772 (cond (end (extent-end-position ex))
773 ((eq 'comment (extent-property ex 'hs)) here)
774 (t (+ (extent-start-position ex)
775 (extent-property ex 'hs-ofs)))))
778 ;; not immediately obvious, look for a suitable block
779 (let ((c-reg (hs-inside-comment-p))
785 ((and (hs-find-block-beginning)
786 (looking-at hs-block-start-regexp)) ; fresh match-data, ugh
788 q (progn (hs-forward-sexp (hs-match-data t) 1) (point)))))
790 (hs-flag-region p q nil)
791 (goto-char (if end q (1+ p)))))
793 (run-hooks 'hs-show-hook))))
795 (defun hs-hide-level (arg)
796 "Hide all blocks ARG levels below this block.
797 The hook `hs-hide-hook' is run; see `run-hooks'."
801 (message "Hiding blocks ...")
802 (hs-hide-level-recursive arg (point-min) (point-max))
803 (message "Hiding blocks ... done"))
805 (run-hooks 'hs-hide-hook)))
807 (defun hs-toggle-hiding ()
808 "Toggle hiding/showing of a block.
809 See `hs-hide-block' and `hs-show-block'."
812 (if (hs-already-hidden-p)
816 (defun hs-mouse-toggle-hiding (e)
817 "Toggle hiding/showing of a block.
818 This command should be bound to a mouse key.
819 Argument E is a mouse event used by `mouse-set-point'.
820 See `hs-hide-block' and `hs-show-block'."
826 (defun hs-hide-initial-comment-block ()
827 "Hide the first block of comments in a file.
828 This can be useful if you have huge RCS logs in those comments."
831 (let ((c-reg (save-excursion
832 (goto-char (point-min))
833 (skip-chars-forward " \t\n\f")
834 (hs-inside-comment-p))))
836 (let ((beg (car c-reg)) (end (cadr c-reg)))
837 ;; see if we have enough comment lines to hide
838 (when (> (count-lines beg end) 1)
839 (hs-hide-comment-region beg end)))))))
842 (defun hs-minor-mode (&optional arg)
843 "Toggle hideshow minor mode.
844 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
845 When hideshow minor mode is on, the menu bar is augmented with hideshow
846 commands and the hideshow commands are enabled.
847 The value '(hs . t) is added to `buffer-invisibility-spec'.
849 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
850 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
851 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
853 Turning hideshow minor mode off reverts the menu bar and the
854 variables to default values and disables the hideshow commands.
856 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
859 \\{hs-minor-mode-map}"
862 (setq hs-headline nil
863 hs-minor-mode (if (null arg)
865 (> (prefix-numeric-value arg) 0)))
869 (easy-menu-add hs-minor-mode-menu)
870 (set (make-local-variable 'line-move-ignore-invisible) t)
871 (add-to-invisibility-spec '(hs . t)))
872 (easy-menu-remove hs-minor-mode-menu)
873 (remove-from-invisibility-spec '(hs . t)))
874 (run-hooks 'hs-minor-mode-hook))
876 ;;---------------------------------------------------------------------------
880 (if hs-minor-mode-map
882 (setq hs-minor-mode-map (make-sparse-keymap))
883 (easy-menu-define hs-minor-mode-menu
885 "Menu used when hideshow minor mode is active."
888 ;; Interpret each table entry as follows: first, populate keymap
889 ;; with elements 2 and 1; then, for easymenu, use entry directly
890 ;; unless element 0 is nil, in which case the entry is "omitted".
892 (define-key hs-minor-mode-map (aref ent 2) (aref ent 1))
893 (if (aref ent 0) ent "-----"))
894 ;; These bindings roughly imitate those used by Outline mode.
895 ;; menu entry command key
896 '(["Hide Block" hs-hide-block "\C-c@\C-h"]
897 ["Show Block" hs-show-block "\C-c@\C-s"]
898 ["Hide All" hs-hide-all "\C-c@\C-\M-h"]
899 ["Show All" hs-show-all "\C-c@\C-\M-s"]
900 ["Hide Level" hs-hide-level "\C-c@\C-l"]
901 ["Toggle Hiding" hs-toggle-hiding "\C-c@\C-c"]
902 [nil hs-mouse-toggle-hiding [(shift mouse-2)]]
906 (or (assq 'hs-minor-mode minor-mode-map-alist)
907 (setq minor-mode-map-alist
908 (cons (cons 'hs-minor-mode hs-minor-mode-map)
909 minor-mode-map-alist)))
910 (or (assq 'hs-minor-mode minor-mode-alist)
911 (setq minor-mode-alist (append minor-mode-alist
912 (list '(hs-minor-mode " hs")))))
914 ;; make some variables permanently buffer-local
915 (let ((vars '(hs-minor-mode
917 hs-block-start-regexp
918 hs-block-start-mdata-select
921 hs-adjust-block-beginning)))
923 (let ((var (car vars)))
924 (make-variable-buffer-local var)
925 (put var 'permanent-local t))
926 (setq vars (cdr vars))))
928 ;;---------------------------------------------------------------------------
933 ;;; hideshow.el ends here