1 ;;; hideshow.el --- minor mode cmds to selectively display blocks of code
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99 Free Software Foundation
5 ;; Author: Thien-Thi Nguyen <ttn@netcom.com>
6 ;; Dan Nicolaescu <dann@ics.uci.edu>
7 ;; Keywords: C C++ java lisp tools editing comments blocks hiding outlines
8 ;; Maintainer-Version: 5.11
9 ;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
11 ;; This file is part of GNU Emacs.
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)
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.
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.
30 ;; * Commands provided
32 ;; This file provides Hideshow Minor Mode. When active, nine commands
33 ;; are available, implementing block hiding and showing. They (and their
36 ;; hs-hide-block C-c h
37 ;; hs-show-block C-c s
40 ;; hs-show-region C-c R
41 ;; hs-hide-level C-c L
43 ;; hs-mouse-toggle-hiding [(shift button-2)]
44 ;; hs-hide-initial-comment-block
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.
51 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
52 ;; (similar to other minor modes).
56 ;; You can use `M-x customize-variable' on the following variables:
58 ;; hs-hide-comments-when-hiding-all -- self-explanatory!
59 ;; hs-isearch-open -- what kind of hidden blocks to
60 ;; open when doing isearch
62 ;; Hideshow works w/ incremental search (isearch) by setting the variable
63 ;; `hs-headline', which is the line of text at the beginning of a hidden
64 ;; block that contains a match for the search. You can have this show up
65 ;; in the mode line by modifying the variable `mode-line-format'. For
66 ;; example, the following code prepends this info to the mode line:
68 ;; (unless (memq 'hs-headline mode-line-format)
69 ;; (setq mode-line-format
70 ;; (append '("-" hs-headline) mode-line-format)))
72 ;; See documentation for `mode-line-format' for more info.
74 ;; Hooks are run after some commands:
76 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
77 ;; hs-show-hook hs-show-block, hs-show-all, hs-show-region
79 ;; All hooks are run w/ `run-hooks'. See docs for each variable or hook
82 ;; Normally, hideshow tries to determine appropriate values for block
83 ;; and comment definitions by examining the buffer's major mode. If
84 ;; there are problems, hideshow will not activate and in that case you
85 ;; may wish to override hideshow's heuristics by adding an entry to
86 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
89 ;; (let ((my-mode-hs-info '(my-mode "{{" "}}" ...)))
90 ;; (if (not (member my-mode-hs-info hs-special-modes-alist))
91 ;; (setq hs-special-modes-alist
92 ;; (cons my-mode-hs-info hs-special-modes-alist))))
94 ;; If you have an entry that works particularly well, consider
95 ;; submitting it for inclusion in hideshow.el. See docstring for
96 ;; `hs-special-modes-alist' for more info on the entry format.
100 ;; First make sure hideshow.el is in a directory in your `load-path'.
101 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
102 ;; Then, add the following to your ~/.emacs:
104 ;; (load-library "hideshow")
105 ;; (add-hook 'X-mode-hook 'hs-minor-mode) ; other modes similarly
107 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
108 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
109 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
113 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
114 ;; function `forward-comment' (among other things). If someone
115 ;; writes this, please send me a copy.
117 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
118 ;; `M-x hs-minor-mode' twice (that is, deactivate then activate
121 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.4.
122 ;; XEmacs compatibility may have bitrotted since 4.29.
124 ;; Correspondance welcome; please indicate version number. Send bug
125 ;; reports and inquiries to <ttn@netcom.com>.
129 ;; Thanks go to the following people for valuable ideas, code and
132 ;; adahome@ix.netcom.com Dean Andrews
133 ;; alfh@ifi.uio.no Alf-Ivar Holm
134 ;; bauer@itsm.uni-stuttgart.de Holger Bauer
135 ;; christoph.conrad@post.rwth-aachen.de Christoph Conrad
136 ;; d.love@dl.ac.uk Dave Love
137 ;; dirk@ida.ing.tu-bs.de Dirk Herrmann
138 ;; gael@gnlab030.grenoble.hp.com Gael Marziou
139 ;; jan.djarv@sa.erisoft.se Jan Djarv
140 ;; leray@dev-lme.pcc.philips.com Guillaume Leray
141 ;; moody@mwt.net Moody Ahmad
142 ;; preston.f.crow@dartmouth.edu Preston F. Crow
143 ;; qhslali@aom.ericsson.se Lars Lindberg
144 ;; reto@synopsys.com Reto Zimmermann
145 ;; sheff@edcsgw2.cr.usgs.gov Keith Sheffield
146 ;; smes@post1.com Chew Meng Kuan
147 ;; tonyl@eng.sun.com Tony Lam
148 ;; ware@cis.ohio-state.edu Pete Ware
150 ;; Special thanks go to Dan Nicolaescu <dann@ics.uci.edu>, who reimplemented
151 ;; hideshow using overlays (rather than selective display), added isearch
152 ;; magic, folded in custom.el compatibility, generalized comment handling,
153 ;; incorporated mouse support, and maintained the code in general. Version
154 ;; 4.0 is largely due to his efforts.
158 ;; Hideshow was inspired when I learned about selective display. It was
159 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
160 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
161 ;; been lost. XEmacs support is reliable as of 4.29. State save and
162 ;; restore was added in 3.5 (not widely distributed), and reliable as of
163 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
164 ;; Version 5.x uses new algorithms for block selection and traversal,
165 ;; unbundles state save and restore, and includes more isearch support.
171 ;;---------------------------------------------------------------------------
172 ;; user-configurable variables
174 (defgroup hideshow nil
175 "Minor mode for hiding and showing program and comment blocks."
180 (defcustom hs-hide-comments-when-hiding-all t
181 "*Hide the comments too when you do an `hs-hide-all'."
185 (defcustom hs-minor-mode-hook nil
186 "*Hook called when hideshow minor mode is activated or deactivated."
190 (defcustom hs-isearch-open
'block
191 "*What kind of hidden blocks to open when doing `isearch'.
192 One of the following symbols:
194 block -- open only blocks
195 comment -- open only comments
196 t -- open both blocks and comments
197 nil -- open neither blocks nor comments
199 This has effect iff `search-invisible' is set to `open'."
200 :type
'(choice (const :tag
"open only blocks" block
)
201 (const :tag
"open only comments" comment
)
202 (const :tag
"open both blocks and comments" t
)
203 (const :tag
"don't open any of them" nil
))
207 (defvar hs-special-modes-alist
208 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
209 (c++-mode
"{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
210 (bibtex-mode ("^@\\S(*\\(\\s(\\)" 1))
211 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
213 "*Alist for initializing the hideshow variables for different modes.
214 Each element has the form
215 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
217 If non-nil, hideshow will use these values as regexps to define blocks
218 and comments, respectively for major mode MODE.
220 START, END and COMMENT-START are regular expressions. A block is
221 defined as text surrounded by START and END.
223 As a special case, START may be a list of the form (COMPLEX-START
224 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
225 MDATA-SELECTOR an integer that specifies which sub-match is the proper
226 place to adjust point, before calling `hs-forward-sexp-func'. For
227 example, see the `hs-special-modes-alist' entry for `bibtex-mode'.
229 For some major modes, `forward-sexp' does not work properly. In those
230 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
232 See the documentation for `hs-adjust-block-beginning' to see what is the
233 use of ADJUST-BEG-FUNC.
235 If any of the elements is left nil or omitted, hideshow tries to guess
236 appropriate values. The regexps should not contain leading or trailing
237 whitespace. Case does not matter.")
239 (defvar hs-hide-hook nil
240 "*Hook called (with `run-hooks') at the end of commands to hide text.
241 These commands include `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
243 (defvar hs-show-hook nil
244 "*Hook called (with `run-hooks') at the end of commands to show text.
245 These commands include `hs-show-all', `hs-show-block' and `hs-show-region'.")
247 ;;---------------------------------------------------------------------------
248 ;; internal variables
250 (defvar hs-minor-mode nil
251 "Non-nil if using hideshow mode as a minor mode of some other mode.
252 Use the command `hs-minor-mode' to toggle or set this variable.")
254 (defvar hs-minor-mode-map nil
255 "Keymap for hideshow minor mode.")
257 (defvar hs-minor-mode-menu nil
258 "Menu for hideshow minor mode.")
260 (defvar hs-c-start-regexp nil
261 "Regexp for beginning of comments.
262 Differs from mode-specific comment regexps in that
263 surrounding whitespace is stripped.")
265 (defvar hs-block-start-regexp nil
266 "Regexp for beginning of block.")
268 (defvar hs-block-start-mdata-select nil
269 "Element in `hs-block-start-regexp' match data to consider as block start.
270 The internal function `hs-forward-sexp' moves point to the beginning of this
271 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
273 (defvar hs-block-end-regexp nil
274 "Regexp for end of block.")
276 (defvar hs-forward-sexp-func
'forward-sexp
277 "Function used to do a `forward-sexp'.
278 Should change for Algol-ish modes. For single-character block
279 delimiters -- ie, the syntax table regexp for the character is
280 either `(' or `)' -- `hs-forward-sexp-func' would just be
281 `forward-sexp'. For other modes such as simula, a more specialized
282 function is necessary.")
284 (defvar hs-adjust-block-beginning nil
285 "Function used to tweak the block beginning.
286 The block is hidden from the position returned by this function,
287 as opposed to hiding it from the position returned when searching
288 for `hs-block-start-regexp'.
290 For example, in c-like modes, if we wish to also hide the curly braces
291 (if you think they occupy too much space on the screen), this function
292 should return the starting point (at the end of line) of the hidden
295 It is called with a single argument ARG which is the the position in
296 buffer after the block beginning.
298 It should return the position from where we should start hiding.
300 It should not move the point.
302 See `hs-c-like-adjust-block-beginning' for an example of using this.")
304 (defvar hs-headline nil
305 "Text of the line where a hidden block begins, set during isearch.
306 You can display this in the mode line by adding the symbol `hs-headline'
307 to the variable `mode-line-format'. For example,
309 (unless (memq 'hs-headline mode-line-format)
310 (setq mode-line-format
311 (append '(\"-\" hs-headline) mode-line-format)))
313 Note that `mode-line-format' is buffer-local.")
315 ;;---------------------------------------------------------------------------
318 ; ;; xemacs compatibility
319 ; (when (string-match "xemacs\\|lucid" emacs-version)
320 ; ;; use pre-packaged compatiblity layer
321 ; (require 'overlay))
323 ; ;; xemacs and emacs-19 compatibility
324 ; (when (or (not (fboundp 'add-to-invisibility-spec))
325 ; (not (fboundp 'remove-from-invisibility-spec)))
326 ; ;; `buffer-invisibility-spec' mutators snarfed from Emacs 20.3 lisp/subr.el
327 ; (defun add-to-invisibility-spec (arg)
329 ; ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
330 ; (setq buffer-invisibility-spec (list arg)))
332 ; (setq buffer-invisibility-spec
333 ; (cons arg buffer-invisibility-spec)))))
334 ; (defun remove-from-invisibility-spec (arg)
335 ; (if buffer-invisibility-spec
336 ; (setq buffer-invisibility-spec
337 ; (delete arg buffer-invisibility-spec)))))
340 (defalias 'hs-match-data
'match-data
)
342 ;;---------------------------------------------------------------------------
345 (defun hs-discard-overlays (from to
)
346 "Delete hideshow overlays in region defined by FROM and TO."
348 (setq from
(prog1 to
(setq to from
))))
349 (let ((ovs (overlays-in from to
)))
351 (let ((ov (car ovs
)))
352 (when (overlay-get ov
'hs
)
353 (delete-overlay ov
)))
354 (setq ovs
(cdr ovs
)))))
356 (defun hs-isearch-show (ov)
357 "Delete overlay OV, and set `hs-headline' to nil.
359 This function is meant to be used as the `isearch-open-invisible'
360 property of an overlay."
361 (setq hs-headline nil
)
364 (defun hs-isearch-show-temporary (ov hide-p
)
365 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
366 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
367 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
370 This function is meant to be used as the `isearch-open-invisible-temporary'
371 property of an overlay."
376 (let ((start (overlay-start ov
)))
378 (save-excursion (goto-char start
)
380 (skip-chars-forward " \t")
383 (force-mode-line-update)
384 (overlay-put ov
'invisible
(and hide-p
'hs
)))
386 (defun hs-flag-region (from to flag
)
387 "Hide or show lines from FROM to TO, according to FLAG.
388 If FLAG is nil then text is shown, while if FLAG is non-nil the text is
389 hidden. Actually flag is really either `comment' or `block' depending
390 on what kind of block it is suppose to hide."
392 ;; first clear it all out
393 (hs-discard-overlays from to
)
394 ;; now create overlays if needed
396 (let ((overlay (make-overlay from to
)))
397 (overlay-put overlay
'invisible
'hs
)
398 (overlay-put overlay
'hs flag
)
399 (when (or (eq hs-isearch-open t
) (eq hs-isearch-open flag
))
400 (overlay-put overlay
'isearch-open-invisible
'hs-isearch-show
)
402 'isearch-open-invisible-temporary
403 'hs-isearch-show-temporary
))
406 (defun hs-forward-sexp (match-data arg
)
407 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
408 Original match data is restored upon return."
410 (set-match-data match-data
)
411 (goto-char (match-beginning hs-block-start-mdata-select
))
412 (funcall hs-forward-sexp-func arg
)))
414 (defun hs-hide-comment-region (beg end
&optional repos-end
)
415 "Hide a region from BEG to END, marking it as a comment.
416 Optional arg REPOS-END means reposition at end."
417 (hs-flag-region (progn (goto-char beg
) (end-of-line) (point))
418 (progn (goto-char end
) (end-of-line) (point))
420 (goto-char (if repos-end end beg
)))
422 (defun hs-hide-block-at-point (&optional end comment-reg
)
423 "Hide block iff on block beginning.
424 Optional arg END means reposition at end.
425 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
426 specifies the limits of the comment, or nil if the block is not
429 The block beginning is adjusted by `hs-adjust-block-beginning'
430 and then further adjusted to be at the end of the line."
432 (hs-hide-comment-region (car comment-reg
) (cadr comment-reg
) end
)
433 (if (looking-at hs-block-start-regexp
)
434 (let* ((mdata (hs-match-data t
))
435 (pure-p (match-end 0))
437 ;; `p' is the point at the end of the block beginning,
438 ;; which may need to be adjusted
440 (goto-char (funcall (or hs-adjust-block-beginning
443 ;; whatever the adjustment, we move to eol
447 ;; `q' is the point at the end of the block
448 (progn (hs-forward-sexp mdata
1)
451 (if (and (< p
(point)) (> (count-lines p q
) 1))
452 (overlay-put (hs-flag-region p q
'block
)
455 (goto-char (if end q
(min p pure-p
)))))))
457 (defun hs-safety-is-job-n ()
458 "Warn if `buffer-invisibility-spec' does not contain symbol `hs'."
459 (unless (and (listp buffer-invisibility-spec
)
460 (assq 'hs buffer-invisibility-spec
))
461 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
464 (defun hs-inside-comment-p ()
465 "Return non-nil if point is inside a comment, otherwise nil.
466 Actually, return a list containing the buffer position of the start
467 and the end of the comment. A comment block can be hidden only if on
468 its starting line there is only whitespace preceding the actual comment
469 beginning. If we are inside of a comment but this condition is not met,
470 we return a list having a nil as its car and the end of comment position
473 ;; the idea is to look backwards for a comment start regexp, do a
474 ;; forward comment, and see if we are inside, then extend extend
475 ;; forward and backward as long as we have comments
477 (when (or (looking-at hs-c-start-regexp
)
478 (re-search-backward hs-c-start-regexp
(point-min) t
))
479 (forward-comment (- (buffer-size)))
480 (skip-chars-forward " \t\n\f")
484 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp
))
485 ;; we are in this situation: (example)
490 ;; the point was here before doing (beginning-of-line)
491 ;; here we should advance till the next comment which
492 ;; eventually has only white spaces preceding it on the same
496 (skip-chars-forward " \t\n\f")
498 (while (and (< (point) q
)
500 (not (looking-at hs-c-start-regexp
)))
501 (setq p
(point));; use this to avoid an infinite cycle
503 (skip-chars-forward " \t\n\f"))
504 (if (or (not (looking-at hs-c-start-regexp
))
506 ;; we cannot hide this comment block
507 (setq not-hidable t
)))
508 ;; goto the end of the comment
509 (forward-comment (buffer-size))
510 (skip-chars-backward " \t\n\f")
513 (list (if not-hidable nil p
) (point))))))))
515 (defun hs-grok-mode-type ()
516 "Set up hideshow variables for new buffers.
517 If `hs-special-modes-alist' has information associated with the
518 current buffer's major mode, use that.
519 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
520 function; and adjust-block-beginning function."
521 (if (and (boundp 'comment-start
)
522 (boundp 'comment-end
)
523 comment-start comment-end
)
524 (let* ((lookup (assoc major-mode hs-special-modes-alist
))
525 (start-elem (or (nth 1 lookup
) "\\s(")))
526 (if (listp start-elem
)
527 ;; handle (START-REGEXP MDATA-SELECT)
528 (setq hs-block-start-regexp
(car start-elem
)
529 hs-block-start-mdata-select
(cadr start-elem
))
530 ;; backwards compatibility: handle simple START-REGEXP
531 (setq hs-block-start-regexp start-elem
532 hs-block-start-mdata-select
0))
533 (setq hs-block-end-regexp
(or (nth 2 lookup
) "\\s)")
534 hs-c-start-regexp
(or (nth 3 lookup
)
535 (let ((c-start-regexp
536 (regexp-quote comment-start
)))
537 (if (string-match " +$" c-start-regexp
)
538 (substring c-start-regexp
539 0 (1- (match-end 0)))
541 hs-forward-sexp-func
(or (nth 4 lookup
) 'forward-sexp
)
542 hs-adjust-block-beginning
(nth 5 lookup
)))
544 (setq hs-minor-mode nil
)
545 (error "%s Mode doesn't support Hideshow Minor Mode" mode-name
))))
547 (defun hs-find-block-beginning ()
548 "Reposition point at block-start.
549 Return point, or nil if original point was not in a block."
552 ;; look if current line is block start
553 (if (looking-at hs-block-start-regexp
)
555 ;; look backward for the start of a block that contains the cursor
556 (while (and (re-search-backward hs-block-start-regexp nil t
)
558 (< here
(save-excursion
559 (hs-forward-sexp (hs-match-data t
) 1)
566 (defun hs-hide-level-recursive (arg minp maxp
)
567 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
568 (when (hs-find-block-beginning)
569 (setq minp
(1+ (point)))
570 (funcall hs-forward-sexp-func
1)
571 (setq maxp
(1- (point))))
572 (hs-flag-region minp maxp nil
) ; eliminate weirdness
575 (forward-comment (buffer-size))
576 (and (< (point) maxp
)
577 (re-search-forward hs-block-start-regexp maxp t
)))
579 (hs-hide-level-recursive (1- arg
) minp maxp
)
580 (goto-char (match-beginning hs-block-start-mdata-select
))
581 (hs-hide-block-at-point t
)))
585 (defmacro hs-life-goes-on
(&rest body
)
586 "Evaluate BODY forms iff variable `hs-minor-mode' is non-nil.
587 In the dynamic context of this macro, `inhibit-point-motion-hooks'
588 and `case-fold-search' are both t."
590 (let ((inhibit-point-motion-hooks t
)
591 (case-fold-search t
))
594 (put 'hs-life-goes-on
'edebug-form-spec
'(&rest form
))
596 (defun hs-already-hidden-p ()
597 "Return non-nil if point is in an already-hidden block, otherwise nil."
599 (let ((c-reg (hs-inside-comment-p)))
600 (if (and c-reg
(nth 0 c-reg
))
601 ;; point is inside a comment, and that comment is hidable
602 (goto-char (nth 0 c-reg
))
604 (hs-find-block-beginning)
605 (looking-at hs-block-start-regexp
))
606 ;; point is inside a block
607 (goto-char (match-end 0)))))
609 (let ((overlays (overlays-at (point)))
611 (while (and (not found
) (overlayp (car overlays
)))
612 (setq found
(overlay-get (car overlays
) 'hs
)
613 overlays
(cdr overlays
)))
616 (defun hs-c-like-adjust-block-beginning (initial)
617 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
618 Actually, point is never moved; a new position is returned that is
619 the end of the C-function header. This adjustment function is meant
620 to be assigned to `hs-adjust-block-beginning' for C-like modes."
622 (goto-char (1- initial
))
623 (forward-comment (- (buffer-size)))
626 ;;---------------------------------------------------------------------------
629 (defun hs-hide-all ()
630 "Hide all top level blocks, displaying only first and last lines.
631 Move point to the beginning of the line, and run the normal hook
632 `hs-hide-hook'. See documentation for `run-hooks'.
633 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
636 (message "Hiding all blocks ...")
638 (hs-flag-region (point-min) (point-max) nil
) ; eliminate weirdness
639 (goto-char (point-min))
640 (if hs-hide-comments-when-hiding-all
643 (block-and-comment-re
645 hs-block-start-regexp
649 (while (re-search-forward block-and-comment-re
(point-max) t
)
650 (if (match-beginning 1) ;; we have found a block beginning
652 (goto-char (match-beginning 1))
653 (hs-hide-block-at-point t
)
654 (message "Hiding ... %d" (setq count
(1+ count
))))
656 (setq c-reg
(hs-inside-comment-p))
657 (if (and c-reg
(car c-reg
))
658 (if (> (count-lines (car c-reg
) (nth 1 c-reg
)) 1)
660 (hs-hide-block-at-point t c-reg
)
661 (message "Hiding ... %d" (setq count
(1+ count
))))
662 (goto-char (nth 1 c-reg
)))))))
664 (buf-size (buffer-size)))
667 (forward-comment buf-size
)
668 (re-search-forward hs-block-start-regexp
(point-max) t
))
669 (goto-char (match-beginning 0))
670 (hs-hide-block-at-point t
)
671 (message "Hiding ... %d" (setq count
(1+ count
))))))
672 (hs-safety-is-job-n))
674 (message "Hiding all blocks ... done")
675 (run-hooks 'hs-hide-hook
)))
677 (defun hs-show-all ()
678 "Show everything then run `hs-show-hook'. See `run-hooks'."
681 (message "Showing all blocks ...")
682 (hs-flag-region (point-min) (point-max) nil
)
683 (message "Showing all blocks ... done")
684 (run-hooks 'hs-show-hook
)))
686 (defun hs-hide-block (&optional end
)
687 "Select a block and hide it. With prefix arg, reposition at END.
688 Upon completion, point is repositioned and the normal hook
689 `hs-hide-hook' is run. See documentation for `run-hooks'."
692 (let ((c-reg (hs-inside-comment-p)))
694 ((and c-reg
(or (null (nth 0 c-reg
))
695 (<= (count-lines (car c-reg
) (nth 1 c-reg
)) 1)))
696 (message "(not enough comment lines to hide)"))
698 (looking-at hs-block-start-regexp
)
699 (hs-find-block-beginning))
700 (hs-hide-block-at-point end c-reg
)
702 (run-hooks 'hs-hide-hook
))))))
704 (defun hs-show-block (&optional end
)
705 "Select a block and show it.
706 With prefix arg, reposition at END. Upon completion, point is
707 repositioned and the normal hook `hs-show-hook' is run.
708 See documentation for functions `hs-hide-block' and `run-hooks'."
712 ;; first see if we have something at the end of the line
713 (catch 'eol-begins-hidden-region-p
715 (ovs (save-excursion (end-of-line) (overlays-at (point)))))
717 (let ((ov (car ovs
)))
718 (when (overlay-get ov
'hs
)
720 (cond (end (overlay-end ov
))
721 ((eq 'comment
(overlay-get ov
'hs
)) here
)
722 (t (+ (overlay-start ov
) (overlay-get ov
'hs-ofs
)))))
724 (throw 'eol-begins-hidden-region-p t
)))
725 (setq ovs
(cdr ovs
)))
727 ;; not immediately obvious, look for a suitable block
728 (let ((c-reg (hs-inside-comment-p))
734 ((and (hs-find-block-beginning)
735 (looking-at hs-block-start-regexp
)) ; fresh match-data, ugh
737 q
(progn (hs-forward-sexp (hs-match-data t
) 1) (point)))))
739 (hs-flag-region p q nil
)
740 (goto-char (if end q
(1+ p
)))))
742 (run-hooks 'hs-show-hook
))))
744 (defun hs-show-region (beg end
)
745 "Show all lines from BEG to END, without doing any block analysis.
746 Note: `hs-show-region' is intended for use when `hs-show-block' signals
747 \"unbalanced parentheses\" and so is an emergency measure only. You may
748 become very confused if you use this command indiscriminately.
749 The hook `hs-show-hook' is run; see `run-hooks'."
752 (hs-flag-region beg end nil
)
754 (run-hooks 'hs-show-hook
)))
756 (defun hs-hide-level (arg)
757 "Hide all blocks ARG levels below this block.
758 The hook `hs-hide-hook' is run; see `run-hooks'."
762 (message "Hiding blocks ...")
763 (hs-hide-level-recursive arg
(point-min) (point-max))
764 (message "Hiding blocks ... done"))
766 (run-hooks 'hs-hide-hook
)))
768 (defun hs-toggle-hiding ()
769 "Toggle hiding/showing of a block.
770 See `hs-hide-block' and `hs-show-block'."
773 (if (hs-already-hidden-p)
777 (defun hs-mouse-toggle-hiding (e)
778 "Toggle hiding/showing of a block.
779 This command should be bound to a mouse key.
780 Argument E is a mouse event used by `mouse-set-point'.
781 See `hs-hide-block' and `hs-show-block'."
787 (defun hs-hide-initial-comment-block ()
788 "Hide the first block of comments in a file.
789 This can be useful if you have huge RCS logs in those comments."
792 (let ((c-reg (save-excursion
793 (goto-char (point-min))
794 (skip-chars-forward " \t\n\f")
795 (hs-inside-comment-p))))
797 (let ((beg (car c-reg
)) (end (cadr c-reg
)))
798 ;; see if we have enough comment lines to hide
799 (when (> (count-lines beg end
) 1)
800 (hs-hide-comment-region beg end
)))))))
803 (defun hs-minor-mode (&optional arg
)
804 "Toggle hideshow minor mode.
805 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
806 When hideshow minor mode is on, the menu bar is augmented with hideshow
807 commands and the hideshow commands are enabled.
808 The value '(hs . t) is added to `buffer-invisibility-spec'.
810 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
811 `hs-show-block', `hs-hide-level' and `hs-show-region'. There is also
812 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
814 Turning hideshow minor mode off reverts the menu bar and the
815 variables to default values and disables the hideshow commands.
817 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
820 \\{hs-minor-mode-map}"
823 (setq hs-headline nil
824 hs-minor-mode
(if (null arg
)
826 (> (prefix-numeric-value arg
) 0)))
830 (easy-menu-add hs-minor-mode-menu
)
831 (make-variable-buffer-local 'line-move-ignore-invisible
)
832 (setq line-move-ignore-invisible t
)
833 (add-to-invisibility-spec '(hs . t
)))
834 (easy-menu-remove hs-minor-mode-menu
)
835 (remove-from-invisibility-spec '(hs . t
)))
836 (run-hooks 'hs-minor-mode-hook
))
838 ;;---------------------------------------------------------------------------
842 (if hs-minor-mode-map
844 (setq hs-minor-mode-map
(make-sparse-keymap))
845 (easy-menu-define hs-minor-mode-menu
847 "Menu used when hideshow minor mode is active."
850 ;; Interpret each table entry as follows: first, populate keymap
851 ;; with elements 2 and 1; then, for easymenu, use entry directly
852 ;; unless element 0 is nil, in which case the entry is "omitted".
854 (define-key hs-minor-mode-map
(aref ent
2) (aref ent
1))
855 (if (aref ent
0) ent
"-----"))
856 ;; I believe there is nothing bound on these keys.
857 ;; menu entry command key
858 '(["Hide Block" hs-hide-block
"\C-ch"]
859 ["Show Block" hs-show-block
"\C-cs"]
860 ["Hide All" hs-hide-all
"\C-cH"]
861 ["Show All" hs-show-all
"\C-cS"]
862 ["Hide Level" hs-hide-level
"\C-cL"]
863 ["Show Region" hs-show-region
"\C-cR"]
864 [nil hs-mouse-toggle-hiding
[(shift button2
)]]
868 (or (assq 'hs-minor-mode minor-mode-map-alist
)
869 (setq minor-mode-map-alist
870 (cons (cons 'hs-minor-mode hs-minor-mode-map
)
871 minor-mode-map-alist
)))
872 (or (assq 'hs-minor-mode minor-mode-alist
)
873 (setq minor-mode-alist
(append minor-mode-alist
874 (list '(hs-minor-mode " hs")))))
876 ;; make some variables permanently buffer-local
877 (let ((vars '(hs-minor-mode
879 hs-block-start-regexp
880 hs-block-start-mdata-select
883 hs-adjust-block-beginning
)))
885 (let ((var (car vars
)))
886 (make-variable-buffer-local var
)
887 (put var
'permanent-local t
))
888 (setq vars
(cdr vars
))))
890 ;;---------------------------------------------------------------------------
895 ;;; hideshow.el ends here