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 ;; 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 @ 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 mouse-2)]
43 ;; hs-hide-initial-comment-block
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.
50 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
51 ;; (similar to other minor modes).
55 ;; First make sure hideshow.el is in a directory in your `load-path'.
56 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
57 ;; Then, add the following to your ~/.emacs:
59 ;; (load-library "hideshow")
60 ;; (add-hook 'X-mode-hook ; other modes similarly
61 ;; '(lambda () (hs-minor-mode 1)))
63 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
64 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
65 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
67 ;; Additionally, Joseph Eydelnant writes:
68 ;; I enjoy your package hideshow.el Ver. 5.24 2001/02/13
69 ;; a lot and I've been looking for the following functionality:
70 ;; toggle hide/show all with a single key.
71 ;; Here are a few lines of code that lets me do just that.
73 ;; (defvar my-hs-hide nil "Current state of hideshow for toggling all.")
75 ;; (defun my-toggle-hideshow-all () "Toggle hideshow all."
77 ;; (setq my-hs-hide (not my-hs-hide))
82 ;; [Your hideshow hacks here!]
86 ;; You can use `M-x customize-variable' on the following variables:
88 ;; - hs-hide-comments-when-hiding-all -- self-explanatory!
89 ;; - hs-hide-all-non-comment-function -- if non-nil, when doing a
90 ;; `hs-hide-all', this function
91 ;; is called w/ no arguments
92 ;; - hs-isearch-open -- what kind of hidden blocks to
93 ;; open when doing isearch
95 ;; Some languages (e.g., Java) are deeply nested, so the normal behavior
96 ;; of `hs-hide-all' (hiding all but top-level blocks) results in very
97 ;; little information shown, which is not very useful. You can use the
98 ;; variable `hs-hide-all-non-comment-function' to implement your idea of
99 ;; what is more useful. For example, the following code shows the next
100 ;; nested level in addition to the top-level:
102 ;; (defun ttn-hs-hide-level-1 ()
105 ;; (setq hs-hide-all-non-comment-function 'ttn-hs-hide-level-1)
107 ;; Hideshow works w/ incremental search (isearch) by setting the variable
108 ;; `hs-headline', which is the line of text at the beginning of a hidden
109 ;; block that contains a match for the search. You can have this show up
110 ;; in the mode line by modifying the variable `mode-line-format'. For
111 ;; example, the following code prepends this info to the mode line:
113 ;; (unless (memq 'hs-headline mode-line-format)
114 ;; (setq mode-line-format
115 ;; (append '("-" hs-headline) mode-line-format)))
117 ;; See documentation for `mode-line-format' for more info.
119 ;; Hooks are run after some commands:
121 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
122 ;; hs-show-hook hs-show-block, hs-show-all
124 ;; One of `hs-hide-hook' or `hs-show-hook' is run for the toggling
125 ;; commands when the result of the toggle is to hide or show blocks,
126 ;; respectively. All hooks are run w/ `run-hooks'. See docs for each
127 ;; variable or hook for more info.
129 ;; Normally, hideshow tries to determine appropriate values for block
130 ;; and comment definitions by examining the buffer's major mode. If
131 ;; there are problems, hideshow will not activate and in that case you
132 ;; may wish to override hideshow's heuristics by adding an entry to
133 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
134 ;; do something like:
136 ;; (let ((my-mode-hs-info '(my-mode "{{" "}}" ...)))
137 ;; (if (not (member my-mode-hs-info hs-special-modes-alist))
138 ;; (setq hs-special-modes-alist
139 ;; (cons my-mode-hs-info hs-special-modes-alist))))
141 ;; If you have an entry that works particularly well, consider
142 ;; submitting it for inclusion in hideshow.el. See docstring for
143 ;; `hs-special-modes-alist' for more info on the entry format.
147 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
148 ;; function `forward-comment' (among other things). If someone
149 ;; writes this, please send me a copy.
151 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
152 ;; `M-x hs-minor-mode' twice (that is, deactivate then re-activate
155 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.7.
156 ;; XEmacs compatibility may have bitrotted since 4.29.
158 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
159 ;; `byte-compile-file' inserts the file to be compiled in a temporary
160 ;; buffer and switches `normal-mode' on. In the case where you have
161 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
162 ;; the initial comment sometimes hides parts of the first statement (seems
163 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
165 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
167 ;; (defadvice byte-compile-file (around
168 ;; byte-compile-file-hideshow-off
170 ;; (let ((hs-minor-mode-hook nil))
173 ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the
174 ;; suggested workaround is to turn off hideshow entirely, for example:
176 ;; (defun turn-off-hideshow () (hs-minor-mode -1))
177 ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow)
178 ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow)
180 ;; In the case of `vc-diff', here is a less invasive workaround:
182 ;; (add-hook 'vc-before-checkin-hook
184 ;; (goto-char (point-min))
187 ;; Unfortunately, these workarounds do not restore hideshow state.
188 ;; If someone figures out a better way, please let me know.
192 ;; Correspondance welcome; please indicate version number. Send bug
193 ;; reports and inquiries to <ttn@gnu.org>.
197 ;; Thanks go to the following people for valuable ideas, code and
200 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave
201 ;; Love, Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray,
202 ;; Moody Ahmad, Preston F. Crow, Lars Lindberg, Reto Zimmermann,
203 ;; Keith Sheffield, Chew Meng Kuan, Tony Lam, Pete Ware, François
204 ;; Pinard, Stefan Monnier, Joseph Eydelnant, Michael Ernst
206 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
207 ;; overlays (rather than selective display), added isearch magic, folded
208 ;; in custom.el compatibility, generalized comment handling, incorporated
209 ;; mouse support, and maintained the code in general. Version 4.0 is
210 ;; largely due to his efforts.
214 ;; Hideshow was inspired when I learned about selective display. It was
215 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
216 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
217 ;; been lost. XEmacs support is reliable as of 4.29. State save and
218 ;; restore was added in 3.5 (not widely distributed), and reliable as of
219 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
220 ;; Version 5.x uses new algorithms for block selection and traversal,
221 ;; unbundles state save and restore, and includes more isearch support.
227 ;;---------------------------------------------------------------------------
228 ;; user-configurable variables
230 (defgroup hideshow nil
231 "Minor mode for hiding and showing program and comment blocks."
236 (defcustom hs-hide-comments-when-hiding-all t
237 "*Hide the comments too when you do an `hs-hide-all'."
241 (defcustom hs-minor-mode-hook nil
242 "*Hook called when hideshow minor mode is activated or deactivated."
247 (defcustom hs-isearch-open
'code
248 "*What kind of hidden blocks to open when doing `isearch'.
249 One of the following symbols:
251 code -- open only code blocks
252 comment -- open only comment blocks
253 t -- open both code and comment blocks
254 nil -- open neither code nor comment blocks
256 This has effect iff `search-invisible' is set to `open'."
257 :type
'(choice (const :tag
"open only code blocks" code
)
258 (const :tag
"open only comment blocks" comment
)
259 (const :tag
"open both code and comment blocks" t
)
260 (const :tag
"don't open any of them" nil
))
264 (defvar hs-special-modes-alist
265 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
266 (c++-mode
"{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
267 (bibtex-mode ("^@\\S(*\\(\\s(\\)" 1))
268 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
270 "*Alist for initializing the hideshow variables for different modes.
271 Each element has the form
272 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
274 If non-nil, hideshow will use these values as regexps to define blocks
275 and comments, respectively for major mode MODE.
277 START, END and COMMENT-START are regular expressions. A block is
278 defined as text surrounded by START and END.
280 As a special case, START may be a list of the form (COMPLEX-START
281 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
282 MDATA-SELECTOR an integer that specifies which sub-match is the proper
283 place to adjust point, before calling `hs-forward-sexp-func'. Point
284 is adjusted to the beginning of the specified match. For example,
285 see the `hs-special-modes-alist' entry for `bibtex-mode'.
287 For some major modes, `forward-sexp' does not work properly. In those
288 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
290 See the documentation for `hs-adjust-block-beginning' to see what is the
291 use of ADJUST-BEG-FUNC.
293 If any of the elements is left nil or omitted, hideshow tries to guess
294 appropriate values. The regexps should not contain leading or trailing
295 whitespace. Case does not matter.")
297 (defvar hs-hide-all-non-comment-function nil
298 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
300 (defvar hs-hide-hook nil
301 "*Hook called (with `run-hooks') at the end of commands to hide text.
302 These commands include the toggling commands (when the result is to hide
303 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
305 (defvar hs-show-hook nil
306 "*Hook called (with `run-hooks') at the end of commands to show text.
307 These commands include the toggling commands (when the result is to show
308 a block), `hs-show-all' and `hs-show-block'..")
310 ;;---------------------------------------------------------------------------
311 ;; internal variables
313 (defvar hs-minor-mode nil
314 "Non-nil if using hideshow mode as a minor mode of some other mode.
315 Use the command `hs-minor-mode' to toggle or set this variable.")
317 (defvar hs-minor-mode-map nil
318 "Keymap for hideshow minor mode.")
320 (defvar hs-minor-mode-menu nil
321 "Menu for hideshow minor mode.")
323 (defvar hs-c-start-regexp nil
324 "Regexp for beginning of comments.
325 Differs from mode-specific comment regexps in that
326 surrounding whitespace is stripped.")
328 (defvar hs-block-start-regexp nil
329 "Regexp for beginning of block.")
331 (defvar hs-block-start-mdata-select nil
332 "Element in `hs-block-start-regexp' match data to consider as block start.
333 The internal function `hs-forward-sexp' moves point to the beginning of this
334 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
336 (defvar hs-block-end-regexp nil
337 "Regexp for end of block.")
339 (defvar hs-forward-sexp-func
'forward-sexp
340 "Function used to do a `forward-sexp'.
341 Should change for Algol-ish modes. For single-character block
342 delimiters -- ie, the syntax table regexp for the character is
343 either `(' or `)' -- `hs-forward-sexp-func' would just be
344 `forward-sexp'. For other modes such as simula, a more specialized
345 function is necessary.")
347 (defvar hs-adjust-block-beginning nil
348 "Function used to tweak the block beginning.
349 The block is hidden from the position returned by this function,
350 as opposed to hiding it from the position returned when searching
351 for `hs-block-start-regexp'.
353 For example, in c-like modes, if we wish to also hide the curly braces
354 \(if you think they occupy too much space on the screen), this function
355 should return the starting point (at the end of line) of the hidden
358 It is called with a single argument ARG which is the position in
359 buffer after the block beginning.
361 It should return the position from where we should start hiding.
363 It should not move the point.
365 See `hs-c-like-adjust-block-beginning' for an example of using this.")
367 (defvar hs-headline nil
368 "Text of the line where a hidden block begins, set during isearch.
369 You can display this in the mode line by adding the symbol `hs-headline'
370 to the variable `mode-line-format'. For example,
372 (unless (memq 'hs-headline mode-line-format)
373 (setq mode-line-format
374 (append '(\"-\" hs-headline) mode-line-format)))
376 Note that `mode-line-format' is buffer-local.")
378 ;;---------------------------------------------------------------------------
381 ; ;; xemacs compatibility
382 ; (when (string-match "xemacs\\|lucid" emacs-version)
383 ; ;; use pre-packaged compatiblity layer
384 ; (require 'overlay))
386 ; ;; xemacs and emacs-19 compatibility
387 ; (when (or (not (fboundp 'add-to-invisibility-spec))
388 ; (not (fboundp 'remove-from-invisibility-spec)))
389 ; ;; `buffer-invisibility-spec' mutators snarfed from Emacs 20.3 lisp/subr.el
390 ; (defun add-to-invisibility-spec (arg)
392 ; ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
393 ; (setq buffer-invisibility-spec (list arg)))
395 ; (setq buffer-invisibility-spec
396 ; (cons arg buffer-invisibility-spec)))))
397 ; (defun remove-from-invisibility-spec (arg)
398 ; (when buffer-invisibility-spec
399 ; (setq buffer-invisibility-spec
400 ; (delete arg buffer-invisibility-spec)))))
403 (defalias 'hs-match-data
'match-data
)
405 ;;---------------------------------------------------------------------------
408 (defun hs-discard-overlays (from to
)
409 "Delete hideshow overlays in region defined by FROM and TO."
411 (setq from
(prog1 to
(setq to from
))))
412 (let ((ovs (overlays-in from to
)))
414 (let ((ov (car ovs
)))
415 (when (overlay-get ov
'hs
)
416 (delete-overlay ov
)))
417 (setq ovs
(cdr ovs
)))))
419 (defun hs-isearch-show (ov)
420 "Delete overlay OV, and set `hs-headline' to nil.
422 This function is meant to be used as the `isearch-open-invisible'
423 property of an overlay."
424 (setq hs-headline nil
)
427 (defun hs-isearch-show-temporary (ov hide-p
)
428 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
429 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
430 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
433 This function is meant to be used as the `isearch-open-invisible-temporary'
434 property of an overlay."
439 (let ((start (overlay-start ov
)))
441 (save-excursion (goto-char start
)
443 (skip-chars-forward " \t")
446 (force-mode-line-update)
447 (overlay-put ov
'invisible
(and hide-p
'hs
)))
449 (defun hs-flag-region (from to flag
)
450 "Hide or show lines from FROM to TO, according to FLAG.
451 If FLAG is nil then text is shown, while if FLAG is non-nil the text is
452 hidden. FLAG must be one of the symbols `code' or `comment', depending
453 on what kind of block is to be hidden."
455 ;; first clear it all out
456 (hs-discard-overlays from to
)
457 ;; now create overlays if needed
459 (let ((overlay (make-overlay from to
)))
460 (overlay-put overlay
'invisible
'hs
)
461 (overlay-put overlay
'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 (overlay-put overlay
'isearch-open-invisible
'hs-isearch-show
)
469 'isearch-open-invisible-temporary
470 'hs-isearch-show-temporary
))
473 (defun hs-forward-sexp (match-data arg
)
474 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
475 Original match data is restored upon return."
477 (set-match-data match-data
)
478 (goto-char (match-beginning hs-block-start-mdata-select
))
479 (funcall hs-forward-sexp-func arg
)))
481 (defun hs-hide-comment-region (beg end
&optional repos-end
)
482 "Hide a region from BEG to END, marking it as a comment.
483 Optional arg REPOS-END means reposition at end."
484 (hs-flag-region (progn (goto-char beg
) (end-of-line) (point))
485 (progn (goto-char end
) (end-of-line) (point))
487 (goto-char (if repos-end end beg
)))
489 (defun hs-hide-block-at-point (&optional end comment-reg
)
490 "Hide block iff on block beginning.
491 Optional arg END means reposition at end.
492 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
493 specifies the limits of the comment, or nil if the block is not
496 The block beginning is adjusted by `hs-adjust-block-beginning'
497 and then further adjusted to be at the end of the line."
499 (hs-hide-comment-region (car comment-reg
) (cadr comment-reg
) end
)
500 (when (looking-at hs-block-start-regexp
)
501 (let* ((mdata (hs-match-data t
))
502 (pure-p (match-end 0))
504 ;; `p' is the point at the end of the block beginning,
505 ;; which may need to be adjusted
507 (goto-char (funcall (or hs-adjust-block-beginning
510 ;; whatever the adjustment, we move to eol
514 ;; `q' is the point at the end of the block
515 (progn (hs-forward-sexp mdata
1)
518 (when (and (< p
(point)) (> (count-lines p q
) 1))
519 (overlay-put (hs-flag-region p q
'code
)
522 (goto-char (if end q
(min p pure-p
)))))))
524 (defun hs-safety-is-job-n ()
525 "Warn if `buffer-invisibility-spec' does not contain symbol `hs'."
526 (unless (and (listp buffer-invisibility-spec
)
527 (assq 'hs buffer-invisibility-spec
))
528 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
531 (defun hs-inside-comment-p ()
532 "Return non-nil if point is inside a comment, otherwise nil.
533 Actually, return a list containing the buffer position of the start
534 and the end of the comment. A comment block can be hidden only if on
535 its starting line there is only whitespace preceding the actual comment
536 beginning. If we are inside of a comment but this condition is not met,
537 we return a list having a nil as its car and the end of comment position
540 ;; the idea is to look backwards for a comment start regexp, do a
541 ;; forward comment, and see if we are inside, then extend extend
542 ;; forward and backward as long as we have comments
544 (when (or (looking-at hs-c-start-regexp
)
545 (re-search-backward hs-c-start-regexp
(point-min) t
))
546 (forward-comment (- (buffer-size)))
547 (skip-chars-forward " \t\n\f")
551 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp
))
552 ;; we are in this situation: (example)
557 ;; the point was here before doing (beginning-of-line)
558 ;; here we should advance till the next comment which
559 ;; eventually has only white spaces preceding it on the same
563 (skip-chars-forward " \t\n\f")
565 (while (and (< (point) q
)
567 (not (looking-at hs-c-start-regexp
)))
568 (setq p
(point));; use this to avoid an infinite cycle
570 (skip-chars-forward " \t\n\f"))
571 (when (or (not (looking-at hs-c-start-regexp
))
573 ;; we cannot hide this comment block
574 (setq not-hidable t
)))
575 ;; goto the end of the comment
576 (forward-comment (buffer-size))
577 (skip-chars-backward " \t\n\f")
580 (list (if not-hidable nil p
) (point))))))))
582 (defun hs-grok-mode-type ()
583 "Set up hideshow variables for new buffers.
584 If `hs-special-modes-alist' has information associated with the
585 current buffer's major mode, use that.
586 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
587 function; and adjust-block-beginning function."
588 (if (and (boundp 'comment-start
)
589 (boundp 'comment-end
)
590 comment-start comment-end
)
591 (let* ((lookup (assoc major-mode hs-special-modes-alist
))
592 (start-elem (or (nth 1 lookup
) "\\s(")))
593 (if (listp start-elem
)
594 ;; handle (START-REGEXP MDATA-SELECT)
595 (setq hs-block-start-regexp
(car start-elem
)
596 hs-block-start-mdata-select
(cadr start-elem
))
597 ;; backwards compatibility: handle simple START-REGEXP
598 (setq hs-block-start-regexp start-elem
599 hs-block-start-mdata-select
0))
600 (setq hs-block-end-regexp
(or (nth 2 lookup
) "\\s)")
601 hs-c-start-regexp
(or (nth 3 lookup
)
602 (let ((c-start-regexp
603 (regexp-quote comment-start
)))
604 (if (string-match " +$" c-start-regexp
)
605 (substring c-start-regexp
606 0 (1- (match-end 0)))
608 hs-forward-sexp-func
(or (nth 4 lookup
) 'forward-sexp
)
609 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, `inhibit-point-motion-hooks'
654 and `case-fold-search' are both t."
656 (let ((inhibit-point-motion-hooks t
)
657 (case-fold-search t
))
660 (put 'hs-life-goes-on
'edebug-form-spec
'(&rest form
))
662 (defun hs-already-hidden-p ()
663 "Return non-nil if point is in an already-hidden block, otherwise nil."
665 (let ((c-reg (hs-inside-comment-p)))
666 (if (and c-reg
(nth 0 c-reg
))
667 ;; point is inside a comment, and that comment is hidable
668 (goto-char (nth 0 c-reg
))
669 (when (and (not c-reg
)
670 (hs-find-block-beginning)
671 (looking-at hs-block-start-regexp
))
672 ;; point is inside a block
673 (goto-char (match-end 0)))))
675 (let ((overlays (overlays-at (point)))
677 (while (and (not found
) (overlayp (car overlays
)))
678 (setq found
(overlay-get (car overlays
) 'hs
)
679 overlays
(cdr overlays
)))
682 (defun hs-c-like-adjust-block-beginning (initial)
683 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
684 Actually, point is never moved; a new position is returned that is
685 the end of the C-function header. This adjustment function is meant
686 to be assigned to `hs-adjust-block-beginning' for C-like modes."
688 (goto-char (1- initial
))
689 (forward-comment (- (buffer-size)))
692 ;;---------------------------------------------------------------------------
695 (defun hs-hide-all ()
696 "Hide all top level blocks, displaying only first and last lines.
697 Move point to the beginning of the line, and run the normal hook
698 `hs-hide-hook'. See documentation for `run-hooks'.
699 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
702 (message "Hiding all blocks ...")
704 (hs-flag-region (point-min) (point-max) nil
) ; eliminate weirdness
705 (goto-char (point-min))
708 hs-block-start-regexp
710 (if hs-hide-comments-when-hiding-all
716 (unless hs-hide-comments-when-hiding-all
717 (forward-comment (point-max)))
718 (re-search-forward re
(point-max) t
))
719 (if (match-beginning 1)
720 ;; we have found a block beginning
722 (goto-char (match-beginning 1))
723 (if hs-hide-all-non-comment-function
724 (funcall hs-hide-all-non-comment-function
)
725 (hs-hide-block-at-point t
)))
726 ;; found a comment, probably
727 (let ((c-reg (hs-inside-comment-p))) ; blech!
728 (when (and c-reg
(car c-reg
))
729 (if (> (count-lines (car c-reg
) (nth 1 c-reg
)) 1)
730 (hs-hide-block-at-point t c-reg
)
731 (goto-char (nth 1 c-reg
))))))
732 (message "Hiding ... %d" (setq count
(1+ count
)))))
733 (hs-safety-is-job-n))
735 (message "Hiding all blocks ... done")
736 (run-hooks 'hs-hide-hook
)))
738 (defun hs-show-all ()
739 "Show everything then run `hs-show-hook'. See `run-hooks'."
742 (message "Showing all blocks ...")
743 (hs-flag-region (point-min) (point-max) nil
)
744 (message "Showing all blocks ... done")
745 (run-hooks 'hs-show-hook
)))
747 (defun hs-hide-block (&optional end
)
748 "Select a block and hide it. With prefix arg, reposition at END.
749 Upon completion, point is repositioned and the normal hook
750 `hs-hide-hook' is run. See documentation for `run-hooks'."
753 (let ((c-reg (hs-inside-comment-p)))
755 ((and c-reg
(or (null (nth 0 c-reg
))
756 (<= (count-lines (car c-reg
) (nth 1 c-reg
)) 1)))
757 (message "(not enough comment lines to hide)"))
759 (looking-at hs-block-start-regexp
)
760 (hs-find-block-beginning))
761 (hs-hide-block-at-point end c-reg
)
763 (run-hooks 'hs-hide-hook
))))))
765 (defun hs-show-block (&optional end
)
766 "Select a block and show it.
767 With prefix arg, reposition at END. Upon completion, point is
768 repositioned and the normal hook `hs-show-hook' is run.
769 See documentation for functions `hs-hide-block' and `run-hooks'."
773 ;; first see if we have something at the end of the line
774 (catch 'eol-begins-hidden-region-p
776 (ovs (save-excursion (end-of-line) (overlays-at (point)))))
778 (let ((ov (car ovs
)))
779 (when (overlay-get ov
'hs
)
781 (cond (end (overlay-end ov
))
782 ((eq 'comment
(overlay-get ov
'hs
)) here
)
783 (t (+ (overlay-start ov
) (overlay-get ov
'hs-ofs
)))))
785 (throw 'eol-begins-hidden-region-p t
)))
786 (setq ovs
(cdr ovs
)))
788 ;; not immediately obvious, look for a suitable block
789 (let ((c-reg (hs-inside-comment-p))
795 ((and (hs-find-block-beginning)
796 (looking-at hs-block-start-regexp
)) ; fresh match-data, ugh
798 q
(progn (hs-forward-sexp (hs-match-data t
) 1) (point)))))
800 (hs-flag-region p q nil
)
801 (goto-char (if end q
(1+ p
)))))
803 (run-hooks 'hs-show-hook
))))
805 (defun hs-hide-level (arg)
806 "Hide all blocks ARG levels below this block.
807 The hook `hs-hide-hook' is run; see `run-hooks'."
811 (message "Hiding blocks ...")
812 (hs-hide-level-recursive arg
(point-min) (point-max))
813 (message "Hiding blocks ... done"))
815 (run-hooks 'hs-hide-hook
)))
817 (defun hs-toggle-hiding ()
818 "Toggle hiding/showing of a block.
819 See `hs-hide-block' and `hs-show-block'."
822 (if (hs-already-hidden-p)
826 (defun hs-mouse-toggle-hiding (e)
827 "Toggle hiding/showing of a block.
828 This command should be bound to a mouse key.
829 Argument E is a mouse event used by `mouse-set-point'.
830 See `hs-hide-block' and `hs-show-block'."
836 (defun hs-hide-initial-comment-block ()
837 "Hide the first block of comments in a file.
838 This can be useful if you have huge RCS logs in those comments."
841 (let ((c-reg (save-excursion
842 (goto-char (point-min))
843 (skip-chars-forward " \t\n\f")
844 (hs-inside-comment-p))))
846 (let ((beg (car c-reg
)) (end (cadr c-reg
)))
847 ;; see if we have enough comment lines to hide
848 (when (> (count-lines beg end
) 1)
849 (hs-hide-comment-region beg end
)))))))
852 (defun hs-minor-mode (&optional arg
)
853 "Toggle hideshow minor mode.
854 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
855 When hideshow minor mode is on, the menu bar is augmented with hideshow
856 commands and the hideshow commands are enabled.
857 The value '(hs . t) is added to `buffer-invisibility-spec'.
859 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
860 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
861 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
863 Turning hideshow minor mode off reverts the menu bar and the
864 variables to default values and disables the hideshow commands.
866 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
869 \\{hs-minor-mode-map}"
872 (setq hs-headline nil
873 hs-minor-mode
(if (null arg
)
875 (> (prefix-numeric-value arg
) 0)))
879 (easy-menu-add hs-minor-mode-menu
)
880 (set (make-local-variable 'line-move-ignore-invisible
) t
)
881 (add-to-invisibility-spec '(hs . t
)))
882 (easy-menu-remove hs-minor-mode-menu
)
883 (remove-from-invisibility-spec '(hs . t
)))
884 (run-hooks 'hs-minor-mode-hook
))
886 ;;---------------------------------------------------------------------------
890 (unless hs-minor-mode-map
891 (setq hs-minor-mode-map
(make-sparse-keymap))
892 (easy-menu-define hs-minor-mode-menu
894 "Menu used when hideshow minor mode is active."
897 ;; Interpret each table entry as follows: first, populate keymap
898 ;; with elements 2 and 1; then, for easymenu, use entry directly
899 ;; unless element 0 is nil, in which case the entry is "omitted".
901 (define-key hs-minor-mode-map
(aref ent
2) (aref ent
1))
902 (if (aref ent
0) ent
"-----"))
903 ;; These bindings roughly imitate those used by Outline mode.
904 ;; menu entry command key
905 '(["Hide Block" hs-hide-block
"\C-c@\C-h"]
906 ["Show Block" hs-show-block
"\C-c@\C-s"]
907 ["Hide All" hs-hide-all
"\C-c@\C-\M-h"]
908 ["Show All" hs-show-all
"\C-c@\C-\M-s"]
909 ["Hide Level" hs-hide-level
"\C-c@\C-l"]
910 ["Toggle Hiding" hs-toggle-hiding
"\C-c@\C-c"]
911 [nil hs-mouse-toggle-hiding
[(shift mouse-2
)]]
915 (or (assq 'hs-minor-mode minor-mode-map-alist
)
916 (setq minor-mode-map-alist
917 (cons (cons 'hs-minor-mode hs-minor-mode-map
)
918 minor-mode-map-alist
)))
919 (or (assq 'hs-minor-mode minor-mode-alist
)
920 (setq minor-mode-alist
(append minor-mode-alist
921 (list '(hs-minor-mode " hs")))))
923 ;; make some variables permanently buffer-local
924 (let ((vars '(hs-minor-mode
926 hs-block-start-regexp
927 hs-block-start-mdata-select
930 hs-adjust-block-beginning
)))
932 (let ((var (car vars
)))
933 (make-variable-buffer-local var
)
934 (put var
'permanent-local t
))
935 (setq vars
(cdr vars
))))
937 ;;---------------------------------------------------------------------------
942 ;;; arch-tag: 378b6852-e82a-466a-aee8-d9c73859a65e
943 ;;; hideshow.el ends here