1 ;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Thien-Thi Nguyen <ttn@gnu.org>
7 ;; Dan Nicolaescu <dann@ics.uci.edu>
8 ;; Keywords: C C++ java lisp tools editing comments blocks hiding outlines
9 ;; Maintainer-Version: 5.65.2.2
10 ;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; * Commands provided
33 ;; This file provides Hideshow Minor Mode. When active, nine commands
34 ;; are available, implementing block hiding and showing. They (and their
37 ;; hs-hide-block C-c @ C-h
38 ;; hs-show-block C-c @ C-s
39 ;; hs-hide-all C-c @ C-M-h
40 ;; hs-show-all C-c @ C-M-s
41 ;; hs-hide-level C-c @ C-l
42 ;; hs-toggle-hiding C-c @ C-c
43 ;; hs-mouse-toggle-hiding [(shift mouse-2)]
44 ;; hs-hide-initial-comment-block
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 ;; First make sure hideshow.el is in a directory in your `load-path'.
57 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
58 ;; Then, add the following to your ~/.emacs:
60 ;; (load-library "hideshow")
61 ;; (add-hook 'X-mode-hook ; other modes similarly
62 ;; (lambda () (hs-minor-mode 1)))
64 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
65 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
66 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
68 ;; Additionally, Joseph Eydelnant writes:
69 ;; I enjoy your package hideshow.el Ver. 5.24 2001/02/13
70 ;; a lot and I've been looking for the following functionality:
71 ;; toggle hide/show all with a single key.
72 ;; Here are a few lines of code that lets me do just that.
74 ;; (defvar my-hs-hide nil "Current state of hideshow for toggling all.")
76 ;; (defun my-toggle-hideshow-all () "Toggle hideshow all."
78 ;; (setq my-hs-hide (not my-hs-hide))
83 ;; [Your hideshow hacks here!]
87 ;; You can use `M-x customize-variable' on the following variables:
89 ;; - hs-hide-comments-when-hiding-all -- self-explanatory!
90 ;; - hs-hide-all-non-comment-function -- if non-nil, when doing a
91 ;; `hs-hide-all', this function
92 ;; is called w/ no arguments
93 ;; - hs-isearch-open -- what kind of hidden blocks to
94 ;; open when doing isearch
96 ;; Some languages (e.g., Java) are deeply nested, so the normal behavior
97 ;; of `hs-hide-all' (hiding all but top-level blocks) results in very
98 ;; little information shown, which is not very useful. You can use the
99 ;; variable `hs-hide-all-non-comment-function' to implement your idea of
100 ;; what is more useful. For example, the following code shows the next
101 ;; nested level in addition to the top-level:
103 ;; (defun ttn-hs-hide-level-1 ()
106 ;; (setq hs-hide-all-non-comment-function 'ttn-hs-hide-level-1)
108 ;; Hideshow works w/ incremental search (isearch) by setting the variable
109 ;; `hs-headline', which is the line of text at the beginning of a hidden
110 ;; block that contains a match for the search. You can have this show up
111 ;; in the mode line by modifying the variable `mode-line-format'. For
112 ;; example, the following code prepends this info to the mode line:
114 ;; (unless (memq 'hs-headline mode-line-format)
115 ;; (setq mode-line-format
116 ;; (append '("-" hs-headline) mode-line-format)))
118 ;; See documentation for `mode-line-format' for more info.
120 ;; Hooks are run after some commands:
122 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
123 ;; hs-show-hook hs-show-block, hs-show-all
125 ;; One of `hs-hide-hook' or `hs-show-hook' is run for the toggling
126 ;; commands when the result of the toggle is to hide or show blocks,
127 ;; respectively. All hooks are run w/ `run-hooks'. See docs for each
128 ;; variable or hook for more info.
130 ;; Normally, hideshow tries to determine appropriate values for block
131 ;; and comment definitions by examining the buffer's major mode. If
132 ;; there are problems, hideshow will not activate and in that case you
133 ;; may wish to override hideshow's heuristics by adding an entry to
134 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
135 ;; do something like:
137 ;; (add-to-list 'hs-special-modes-alist '(my-mode "{{" "}}" ...))
139 ;; If you have an entry that works particularly well, consider
140 ;; submitting it for inclusion in hideshow.el. See docstring for
141 ;; `hs-special-modes-alist' for more info on the entry format.
143 ;; See also variable `hs-set-up-overlay' for per-block customization of
144 ;; appearance or other effects associated with overlays. For example:
146 ;; (setq hs-set-up-overlay
147 ;; (defun my-display-code-line-counts (ov)
148 ;; (when (eq 'code (overlay-get ov 'hs))
149 ;; (overlay-put ov 'display
151 ;; (format " ... <%d>"
152 ;; (count-lines (overlay-start ov)
153 ;; (overlay-end ov)))
154 ;; 'face 'font-lock-type-face)))))
158 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
159 ;; function `forward-comment' (among other things). If someone
160 ;; writes this, please send me a copy.
162 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
163 ;; `M-x hs-minor-mode' twice (that is, deactivate then re-activate
166 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.7.
167 ;; XEmacs compatibility may have bitrotted since 4.29.
169 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
170 ;; `byte-compile-file' inserts the file to be compiled in a temporary
171 ;; buffer and switches `normal-mode' on. In the case where you have
172 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
173 ;; the initial comment sometimes hides parts of the first statement (seems
174 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
176 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
178 ;; (defadvice byte-compile-file (around
179 ;; byte-compile-file-hideshow-off
181 ;; (let ((hs-minor-mode-hook nil))
184 ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the
185 ;; suggested workaround is to turn off hideshow entirely, for example:
187 ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow)
188 ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow)
190 ;; In the case of `vc-diff', here is a less invasive workaround:
192 ;; (add-hook 'vc-before-checkin-hook
194 ;; (goto-char (point-min))
197 ;; Unfortunately, these workarounds do not restore hideshow state.
198 ;; If someone figures out a better way, please let me know.
202 ;; Correspondance welcome; please indicate version number. Send bug
203 ;; reports and inquiries to <ttn@gnu.org>.
207 ;; Thanks go to the following people for valuable ideas, code and
210 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave Love,
211 ;; Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray, Moody Ahmad,
212 ;; Preston F. Crow, Lars Lindberg, Reto Zimmermann, Keith Sheffield,
213 ;; Chew Meng Kuan, Tony Lam, Pete Ware, François Pinard, Stefan Monnier,
214 ;; Joseph Eydelnant, Michael Ernst, Peter Heslin
216 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
217 ;; overlays (rather than selective display), added isearch magic, folded
218 ;; in custom.el compatibility, generalized comment handling, incorporated
219 ;; mouse support, and maintained the code in general. Version 4.0 is
220 ;; largely due to his efforts.
224 ;; Hideshow was inspired when I learned about selective display. It was
225 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
226 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
227 ;; been lost. XEmacs support is reliable as of 4.29. State save and
228 ;; restore was added in 3.5 (not widely distributed), and reliable as of
229 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
230 ;; Version 5.x uses new algorithms for block selection and traversal,
231 ;; unbundles state save and restore, and includes more isearch support.
237 ;;---------------------------------------------------------------------------
238 ;; user-configurable variables
240 (defgroup hideshow nil
241 "Minor mode for hiding and showing program and comment blocks."
245 (defcustom hs-hide-comments-when-hiding-all t
246 "*Hide the comments too when you do an `hs-hide-all'."
250 (defcustom hs-minor-mode-hook nil
251 "*Hook called when hideshow minor mode is activated or deactivated."
256 (defcustom hs-isearch-open
'code
257 "*What kind of hidden blocks to open when doing `isearch'.
258 One of the following symbols:
260 code -- open only code blocks
261 comment -- open only comment blocks
262 t -- open both code and comment blocks
263 nil -- open neither code nor comment blocks
265 This has effect only if `search-invisible' is set to `open'."
266 :type
'(choice (const :tag
"open only code blocks" code
)
267 (const :tag
"open only comment blocks" comment
)
268 (const :tag
"open both code and comment blocks" t
)
269 (const :tag
"don't open any of them" nil
))
273 (defvar hs-special-modes-alist
274 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
275 (c++-mode
"{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
276 (bibtex-mode ("^@\\S(*\\(\\s(\\)" 1))
277 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
))
278 "*Alist for initializing the hideshow variables for different modes.
279 Each element has the form
280 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
282 If non-nil, hideshow will use these values as regexps to define blocks
283 and comments, respectively for major mode MODE.
285 START, END and COMMENT-START are regular expressions. A block is
286 defined as text surrounded by START and END.
288 As a special case, START may be a list of the form (COMPLEX-START
289 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
290 MDATA-SELECTOR an integer that specifies which sub-match is the proper
291 place to adjust point, before calling `hs-forward-sexp-func'. Point
292 is adjusted to the beginning of the specified match. For example,
293 see the `hs-special-modes-alist' entry for `bibtex-mode'.
295 For some major modes, `forward-sexp' does not work properly. In those
296 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
298 See the documentation for `hs-adjust-block-beginning' to see what is the
299 use of ADJUST-BEG-FUNC.
301 If any of the elements is left nil or omitted, hideshow tries to guess
302 appropriate values. The regexps should not contain leading or trailing
303 whitespace. Case does not matter.")
305 (defvar hs-hide-all-non-comment-function nil
306 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
308 (defvar hs-allow-nesting nil
309 "*If non-nil, hiding remembers internal blocks.
310 This means that when the outer block is shown again, any
311 previously hidden internal blocks remain hidden.")
313 (defvar hs-hide-hook nil
314 "*Hook called (with `run-hooks') at the end of commands to hide text.
315 These commands include the toggling commands (when the result is to hide
316 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
318 (defvar hs-show-hook nil
319 "*Hook called (with `run-hooks') at the end of commands to show text.
320 These commands include the toggling commands (when the result is to show
321 a block), `hs-show-all' and `hs-show-block'..")
323 (defvar hs-set-up-overlay nil
324 "*Function called with one arg, OV, a newly initialized overlay.
325 Hideshow puts a unique overlay on each range of text to be hidden
326 in the buffer. Here is a simple example of how to use this variable:
328 (defun display-code-line-counts (ov)
329 (when (eq 'code (overlay-get ov 'hs))
330 (overlay-put ov 'display
332 (count-lines (overlay-start ov)
333 (overlay-end ov))))))
335 (setq hs-set-up-overlay 'display-code-line-counts)
337 This example shows how to get information from the overlay as well
338 as how to set its `display' property. See `hs-make-overlay' and
339 info node `(elisp)Overlays'.")
341 ;;---------------------------------------------------------------------------
342 ;; internal variables
344 (defvar hs-minor-mode nil
345 "Non-nil if using hideshow mode as a minor mode of some other mode.
346 Use the command `hs-minor-mode' to toggle or set this variable.")
348 (defvar hs-minor-mode-map
349 (let ((map (make-sparse-keymap)))
350 ;; These bindings roughly imitate those used by Outline mode.
351 (define-key map
"\C-c@\C-h" 'hs-hide-block
)
352 (define-key map
"\C-c@\C-s" 'hs-show-block
)
353 (define-key map
"\C-c@\C-\M-h" 'hs-hide-all
)
354 (define-key map
"\C-c@\C-\M-s" 'hs-show-all
)
355 (define-key map
"\C-c@\C-l" 'hs-hide-level
)
356 (define-key map
"\C-c@\C-c" 'hs-toggle-hiding
)
357 (define-key map
[(shift mouse-2
)] 'hs-mouse-toggle-hiding
)
359 "Keymap for hideshow minor mode.")
361 (easy-menu-define hs-minor-mode-menu hs-minor-mode-map
362 "Menu used when hideshow minor mode is active."
364 ["Hide Block" hs-hide-block
365 :help
"Hide the code or comment block at point"]
366 ["Show Block" hs-show-block
367 :help
"Show the code or comment block at point"]
368 ["Hide All" hs-hide-all
369 :help
"Hide all the blocks in the buffer"]
370 ["Show All" hs-show-all
371 :help
"Show all the clocks in the buffer"]
372 ["Hide Level" hs-hide-level
373 :help
"Hide all block at levels below the current block"]
374 ["Toggle Hiding" hs-toggle-hiding
375 :help
"Toggle the hiding state of the current block"]))
377 (defvar hs-c-start-regexp nil
378 "Regexp for beginning of comments.
379 Differs from mode-specific comment regexps in that
380 surrounding whitespace is stripped.")
381 (make-variable-buffer-local 'hs-c-start-regexp
)
383 (defvar hs-block-start-regexp nil
384 "Regexp for beginning of block.")
385 (make-variable-buffer-local 'hs-block-start-regexp
)
387 (defvar hs-block-start-mdata-select nil
388 "Element in `hs-block-start-regexp' match data to consider as block start.
389 The internal function `hs-forward-sexp' moves point to the beginning of this
390 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
391 (make-variable-buffer-local 'hs-block-start-mdata-select
)
393 (defvar hs-block-end-regexp nil
394 "Regexp for end of block.")
396 (defvar hs-forward-sexp-func
'forward-sexp
397 "Function used to do a `forward-sexp'.
398 Should change for Algol-ish modes. For single-character block
399 delimiters -- ie, the syntax table regexp for the character is
400 either `(' or `)' -- `hs-forward-sexp-func' would just be
401 `forward-sexp'. For other modes such as simula, a more specialized
402 function is necessary.")
403 (make-variable-buffer-local 'hs-forward-sexp-func
)
405 (defvar hs-adjust-block-beginning nil
406 "Function used to tweak the block beginning.
407 The block is hidden from the position returned by this function,
408 as opposed to hiding it from the position returned when searching
409 for `hs-block-start-regexp'.
411 For example, in c-like modes, if we wish to also hide the curly braces
412 \(if you think they occupy too much space on the screen), this function
413 should return the starting point (at the end of line) of the hidden
416 It is called with a single argument ARG which is the position in
417 buffer after the block beginning.
419 It should return the position from where we should start hiding.
421 It should not move the point.
423 See `hs-c-like-adjust-block-beginning' for an example of using this.")
424 (make-variable-buffer-local 'hs-adjust-block-beginning
)
426 (defvar hs-headline nil
427 "Text of the line where a hidden block begins, set during isearch.
428 You can display this in the mode line by adding the symbol `hs-headline'
429 to the variable `mode-line-format'. For example,
431 (unless (memq 'hs-headline mode-line-format)
432 (setq mode-line-format
433 (append '(\"-\" hs-headline) mode-line-format)))
435 Note that `mode-line-format' is buffer-local.")
437 ;;---------------------------------------------------------------------------
440 (defun hs-discard-overlays (from to
)
441 "Delete hideshow overlays in region defined by FROM and TO.
442 Skip \"internal\" overlays if `hs-allow-nesting' is non-nil."
444 (setq from
(prog1 to
(setq to from
))))
447 (while (> to
(setq from
(next-overlay-change from
)))
448 (when (setq ov
(hs-overlay-at from
))
449 (setq from
(overlay-end ov
))
450 (delete-overlay ov
))))
451 (dolist (ov (overlays-in from to
))
452 (when (overlay-get ov
'hs
)
453 (delete-overlay ov
)))))
455 (defun hs-make-overlay (b e kind
&optional b-offset e-offset
)
456 "Return a new overlay in region defined by B and E with type KIND.
457 KIND is either `code' or `comment'. Optional fourth arg B-OFFSET
458 when added to B specifies the actual buffer position where the block
459 begins. Likewise for optional fifth arg E-OFFSET. If unspecified
460 they are taken to be 0 (zero). The following properties are set
461 in the overlay: 'invisible 'hs 'hs-b-offset 'hs-e-offset. Also,
462 depending on variable `hs-isearch-open', the following properties may
463 be present: 'isearch-open-invisible 'isearch-open-invisible-temporary.
464 If variable `hs-set-up-overlay' is non-nil it should specify a function
465 to call with the newly initialized overlay."
466 (unless b-offset
(setq b-offset
0))
467 (unless e-offset
(setq e-offset
0))
468 (let ((ov (make-overlay b e
))
469 (io (if (eq 'block hs-isearch-open
)
470 ;; backward compatibility -- `block'<=>`code'
473 (overlay-put ov
'invisible
'hs
)
474 (overlay-put ov
'hs kind
)
475 (overlay-put ov
'hs-b-offset b-offset
)
476 (overlay-put ov
'hs-e-offset e-offset
)
477 (when (or (eq io t
) (eq io kind
))
478 (overlay-put ov
'isearch-open-invisible
'hs-isearch-show
)
479 (overlay-put ov
'isearch-open-invisible-temporary
480 'hs-isearch-show-temporary
))
481 (when hs-set-up-overlay
(funcall hs-set-up-overlay ov
))
484 (defun hs-isearch-show (ov)
485 "Delete overlay OV, and set `hs-headline' to nil.
487 This function is meant to be used as the `isearch-open-invisible'
488 property of an overlay."
489 (setq hs-headline nil
)
492 (defun hs-isearch-show-temporary (ov hide-p
)
493 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
494 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
495 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
498 This function is meant to be used as the `isearch-open-invisible-temporary'
499 property of an overlay."
504 (let ((start (overlay-start ov
)))
506 (save-excursion (goto-char start
)
508 (skip-chars-forward " \t")
511 (force-mode-line-update)
512 ;; handle `display' property specially
515 (when (setq value
(overlay-get ov
'hs-isearch-display
))
516 (overlay-put ov
'display value
)
517 (overlay-put ov
'hs-isearch-display nil
))
518 (when (setq value
(overlay-get ov
'display
))
519 (overlay-put ov
'hs-isearch-display value
)
520 (overlay-put ov
'display nil
))))
521 (overlay-put ov
'invisible
(and hide-p
'hs
)))
523 (defun hs-forward-sexp (match-data arg
)
524 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
525 Original match data is restored upon return."
527 (set-match-data match-data
)
528 (goto-char (match-beginning hs-block-start-mdata-select
))
529 (funcall hs-forward-sexp-func arg
)))
531 (defun hs-hide-comment-region (beg end
&optional repos-end
)
532 "Hide a region from BEG to END, marking it as a comment.
533 Optional arg REPOS-END means reposition at end."
534 (let ((beg-eol (progn (goto-char beg
) (line-end-position)))
535 (end-eol (progn (goto-char end
) (line-end-position))))
536 (hs-discard-overlays beg-eol end-eol
)
537 (hs-make-overlay beg-eol end-eol
'comment beg end
))
538 (goto-char (if repos-end end beg
)))
540 (defun hs-hide-block-at-point (&optional end comment-reg
)
541 "Hide block if on block beginning.
542 Optional arg END means reposition at end.
543 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
544 specifies the limits of the comment, or nil if the block is not
547 The block beginning is adjusted by `hs-adjust-block-beginning'
548 and then further adjusted to be at the end of the line."
550 (hs-hide-comment-region (car comment-reg
) (cadr comment-reg
) end
)
551 (when (looking-at hs-block-start-regexp
)
552 (let* ((mdata (match-data t
))
553 (pure-p (match-end 0))
555 ;; `p' is the point at the end of the block beginning,
556 ;; which may need to be adjusted
558 (goto-char (funcall (or hs-adjust-block-beginning
561 ;; whatever the adjustment, we move to eol
562 (line-end-position)))
564 ;; `q' is the point at the end of the block
565 (progn (hs-forward-sexp mdata
1)
569 (when (and (< p
(point)) (> (count-lines p q
) 1))
570 (cond ((and hs-allow-nesting
(setq ov
(hs-overlay-at p
)))
572 ((not hs-allow-nesting
)
573 (hs-discard-overlays p q
)))
574 (hs-make-overlay p q
'code
(- pure-p p
)))
575 (goto-char (if end q
(min p pure-p
)))))))
577 (defun hs-inside-comment-p ()
578 "Return non-nil if point is inside a comment, otherwise nil.
579 Actually, return a list containing the buffer position of the start
580 and the end of the comment. A comment block can be hidden only if on
581 its starting line there is only whitespace preceding the actual comment
582 beginning. If we are inside of a comment but this condition is not met,
583 we return a list having a nil as its car and the end of comment position
586 ;; the idea is to look backwards for a comment start regexp, do a
587 ;; forward comment, and see if we are inside, then extend extend
588 ;; forward and backward as long as we have comments
590 (when (or (looking-at hs-c-start-regexp
)
591 (re-search-backward hs-c-start-regexp
(point-min) t
))
592 ;; first get to the beginning of this comment...
593 (while (and (not (bobp))
594 (= (point) (progn (forward-comment -
1) (point))))
596 ;; ...then extend backwards
597 (forward-comment (- (buffer-size)))
598 (skip-chars-forward " \t\n\f")
602 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp
))
603 ;; we are in this situation: (example)
608 ;; the point was here before doing (beginning-of-line)
609 ;; here we should advance till the next comment which
610 ;; eventually has only white spaces preceding it on the same
614 (skip-chars-forward " \t\n\f")
616 (while (and (< (point) q
)
618 (not (looking-at hs-c-start-regexp
)))
619 ;; avoid an infinite cycle
622 (skip-chars-forward " \t\n\f"))
623 (when (or (not (looking-at hs-c-start-regexp
))
625 ;; we cannot hide this comment block
627 ;; goto the end of the comment
628 (forward-comment (buffer-size))
629 (skip-chars-backward " \t\n\f")
632 (list (and hidable p
) (point))))))))
634 (defun hs-grok-mode-type ()
635 "Set up hideshow variables for new buffers.
636 If `hs-special-modes-alist' has information associated with the
637 current buffer's major mode, use that.
638 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
639 function; and adjust-block-beginning function."
640 (if (and (boundp 'comment-start
)
641 (boundp 'comment-end
)
642 comment-start comment-end
)
643 (let* ((lookup (assoc major-mode hs-special-modes-alist
))
644 (start-elem (or (nth 1 lookup
) "\\s(")))
645 (if (listp start-elem
)
646 ;; handle (START-REGEXP MDATA-SELECT)
647 (setq hs-block-start-regexp
(car start-elem
)
648 hs-block-start-mdata-select
(cadr start-elem
))
649 ;; backwards compatibility: handle simple START-REGEXP
650 (setq hs-block-start-regexp start-elem
651 hs-block-start-mdata-select
0))
652 (setq hs-block-end-regexp
(or (nth 2 lookup
) "\\s)")
653 hs-c-start-regexp
(or (nth 3 lookup
)
654 (let ((c-start-regexp
655 (regexp-quote comment-start
)))
656 (if (string-match " +$" c-start-regexp
)
657 (substring c-start-regexp
658 0 (1- (match-end 0)))
660 hs-forward-sexp-func
(or (nth 4 lookup
) 'forward-sexp
)
661 hs-adjust-block-beginning
(nth 5 lookup
)))
662 (setq hs-minor-mode nil
)
663 (error "%s Mode doesn't support Hideshow Minor Mode"
664 (format-mode-line mode-name
))))
666 (defun hs-find-block-beginning ()
667 "Reposition point at block-start.
668 Return point, or nil if original point was not in a block."
671 ;; look if current line is block start
672 (if (looking-at hs-block-start-regexp
)
674 ;; look backward for the start of a block that contains the cursor
675 (while (and (re-search-backward hs-block-start-regexp nil t
)
677 (< here
(save-excursion
678 (hs-forward-sexp (match-data t
) 1)
685 (defun hs-hide-level-recursive (arg minp maxp
)
686 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
687 (when (hs-find-block-beginning)
688 (setq minp
(1+ (point)))
689 (funcall hs-forward-sexp-func
1)
690 (setq maxp
(1- (point))))
691 (unless hs-allow-nesting
692 (hs-discard-overlays minp maxp
))
695 (forward-comment (buffer-size))
696 (and (< (point) maxp
)
697 (re-search-forward hs-block-start-regexp maxp t
)))
699 (hs-hide-level-recursive (1- arg
) minp maxp
)
700 (goto-char (match-beginning hs-block-start-mdata-select
))
701 (hs-hide-block-at-point t
)))
704 (defmacro hs-life-goes-on
(&rest body
)
705 "Evaluate BODY forms if variable `hs-minor-mode' is non-nil.
706 In the dynamic context of this macro, `inhibit-point-motion-hooks'
707 and `case-fold-search' are both t."
709 (let ((inhibit-point-motion-hooks t
)
710 (case-fold-search t
))
713 (put 'hs-life-goes-on
'edebug-form-spec
'(&rest form
))
715 (defun hs-overlay-at (position)
716 "Return hideshow overlay at POSITION, or nil if none to be found."
717 (let ((overlays (overlays-at position
))
719 (while (and (not found
) (setq ov
(car overlays
)))
720 (setq found
(and (overlay-get ov
'hs
) ov
)
721 overlays
(cdr overlays
)))
724 (defun hs-already-hidden-p ()
725 "Return non-nil if point is in an already-hidden block, otherwise nil."
727 (let ((c-reg (hs-inside-comment-p)))
728 (if (and c-reg
(nth 0 c-reg
))
729 ;; point is inside a comment, and that comment is hidable
730 (goto-char (nth 0 c-reg
))
732 (when (and (not c-reg
)
733 (hs-find-block-beginning)
734 (looking-at hs-block-start-regexp
))
735 ;; point is inside a block
736 (goto-char (match-end 0)))))
738 (hs-overlay-at (point))))
740 (defun hs-c-like-adjust-block-beginning (initial)
741 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
742 Actually, point is never moved; a new position is returned that is
743 the end of the C-function header. This adjustment function is meant
744 to be assigned to `hs-adjust-block-beginning' for C-like modes."
746 (goto-char (1- initial
))
747 (forward-comment (- (buffer-size)))
750 ;;---------------------------------------------------------------------------
753 (defun hs-hide-all ()
754 "Hide all top level blocks, displaying only first and last lines.
755 Move point to the beginning of the line, and run the normal hook
756 `hs-hide-hook'. See documentation for `run-hooks'.
757 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
761 (unless hs-allow-nesting
762 (hs-discard-overlays (point-min) (point-max)))
763 (goto-char (point-min))
764 (let ((spew (make-progress-reporter "Hiding all blocks..."
765 (point-min) (point-max)))
767 hs-block-start-regexp
769 (if hs-hide-comments-when-hiding-all
775 (unless hs-hide-comments-when-hiding-all
776 (forward-comment (point-max)))
777 (re-search-forward re
(point-max) t
))
778 (if (match-beginning 1)
779 ;; we have found a block beginning
781 (goto-char (match-beginning 1))
782 (if hs-hide-all-non-comment-function
783 (funcall hs-hide-all-non-comment-function
)
784 (hs-hide-block-at-point t
)))
785 ;; found a comment, probably
786 (let ((c-reg (hs-inside-comment-p)))
787 (when (and c-reg
(car c-reg
))
788 (if (> (count-lines (car c-reg
) (nth 1 c-reg
)) 1)
789 (hs-hide-block-at-point t c-reg
)
790 (goto-char (nth 1 c-reg
))))))
791 (progress-reporter-update spew
(point)))
792 (progress-reporter-done spew
)))
794 (run-hooks 'hs-hide-hook
)))
796 (defun hs-show-all ()
797 "Show everything then run `hs-show-hook'. See `run-hooks'."
800 (message "Showing all blocks ...")
801 (let ((hs-allow-nesting nil
))
802 (hs-discard-overlays (point-min) (point-max)))
803 (message "Showing all blocks ... done")
804 (run-hooks 'hs-show-hook
)))
806 (defun hs-hide-block (&optional end
)
807 "Select a block and hide it. With prefix arg, reposition at END.
808 Upon completion, point is repositioned and the normal hook
809 `hs-hide-hook' is run. See documentation for `run-hooks'."
812 (let ((c-reg (hs-inside-comment-p)))
814 ((and c-reg
(or (null (nth 0 c-reg
))
815 (<= (count-lines (car c-reg
) (nth 1 c-reg
)) 1)))
816 (message "(not enough comment lines to hide)"))
818 (looking-at hs-block-start-regexp
)
819 (hs-find-block-beginning))
820 (hs-hide-block-at-point end c-reg
)
821 (run-hooks 'hs-hide-hook
))))))
823 (defun hs-show-block (&optional end
)
824 "Select a block and show it.
825 With prefix arg, reposition at END. Upon completion, point is
826 repositioned and the normal hook `hs-show-hook' is run.
827 See documentation for functions `hs-hide-block' and `run-hooks'."
831 ;; first see if we have something at the end of the line
832 (let ((ov (hs-overlay-at (line-end-position)))
836 (cond (end (overlay-end ov
))
837 ((eq 'comment
(overlay-get ov
'hs
)) here
)
838 (t (+ (overlay-start ov
) (overlay-get ov
'hs-b-offset
)))))
841 ;; not immediately obvious, look for a suitable block
842 (let ((c-reg (hs-inside-comment-p))
848 ((and (hs-find-block-beginning)
849 ;; ugh, fresh match-data
850 (looking-at hs-block-start-regexp
))
852 q
(progn (hs-forward-sexp (match-data t
) 1) (point)))))
854 (hs-discard-overlays p q
)
855 (goto-char (if end q
(1+ p
)))))
856 (run-hooks 'hs-show-hook
))))
858 (defun hs-hide-level (arg)
859 "Hide all blocks ARG levels below this block.
860 The hook `hs-hide-hook' is run; see `run-hooks'."
864 (message "Hiding blocks ...")
865 (hs-hide-level-recursive arg
(point-min) (point-max))
866 (message "Hiding blocks ... done"))
867 (run-hooks 'hs-hide-hook
)))
869 (defun hs-toggle-hiding ()
870 "Toggle hiding/showing of a block.
871 See `hs-hide-block' and `hs-show-block'."
874 (if (hs-already-hidden-p)
878 (defun hs-mouse-toggle-hiding (e)
879 "Toggle hiding/showing of a block.
880 This command should be bound to a mouse key.
881 Argument E is a mouse event used by `mouse-set-point'.
882 See `hs-hide-block' and `hs-show-block'."
888 (defun hs-hide-initial-comment-block ()
889 "Hide the first block of comments in a file.
890 This can be useful if you have huge RCS logs in those comments."
893 (let ((c-reg (save-excursion
894 (goto-char (point-min))
895 (skip-chars-forward " \t\n\f")
896 (hs-inside-comment-p))))
898 (let ((beg (car c-reg
)) (end (cadr c-reg
)))
899 ;; see if we have enough comment lines to hide
900 (when (> (count-lines beg end
) 1)
901 (hs-hide-comment-region beg end
)))))))
904 (define-minor-mode hs-minor-mode
905 "Minor mode to selectively hide/show code and comment blocks.
906 When hideshow minor mode is on, the menu bar is augmented with hideshow
907 commands and the hideshow commands are enabled.
908 The value '(hs . t) is added to `buffer-invisibility-spec'.
910 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
911 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
912 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
914 Turning hideshow minor mode off reverts the menu bar and the
915 variables to default values and disables the hideshow commands.
917 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
920 \\{hs-minor-mode-map}"
923 :keymap hs-minor-mode-map
924 (setq hs-headline nil
)
928 ;; Turn off this mode if we change major modes.
929 (add-hook 'change-major-mode-hook
932 (easy-menu-add hs-minor-mode-menu
)
933 (set (make-local-variable 'line-move-ignore-invisible
) t
)
934 (add-to-invisibility-spec '(hs . t
)))
935 (remove-from-invisibility-spec '(hs . t
))
936 ;; hs-show-all does nothing unless h-m-m is non-nil.
937 (let ((hs-minor-mode t
))
941 (defun turn-off-hideshow ()
942 "Unconditionally turn off `hs-minor-mode'."
945 ;;---------------------------------------------------------------------------
950 ;; arch-tag: 378b6852-e82a-466a-aee8-d9c73859a65e
951 ;;; hideshow.el ends here