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, 2009 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 of the License, or
17 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 ;; * Commands provided
31 ;; This file provides Hideshow Minor Mode. When active, nine commands
32 ;; are available, implementing block hiding and showing. They (and their
35 ;; hs-hide-block C-c @ C-h
36 ;; hs-show-block C-c @ C-s
37 ;; hs-hide-all C-c @ C-M-h
38 ;; hs-show-all C-c @ C-M-s
39 ;; hs-hide-level C-c @ C-l
40 ;; hs-toggle-hiding C-c @ C-c
41 ;; hs-mouse-toggle-hiding [(shift mouse-2)]
42 ;; hs-hide-initial-comment-block
44 ;; Blocks are defined per mode. In c-mode, c++-mode and java-mode, they
45 ;; are simply text between curly braces, while in Lisp-ish modes parens
46 ;; are used. Multi-line comment blocks can also be hidden. Read-only
47 ;; buffers are not a problem, since hideshow doesn't modify the text.
49 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
50 ;; (similar to other minor modes).
54 ;; First make sure hideshow.el is in a directory in your `load-path'.
55 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
56 ;; Then, add the following to your ~/.emacs:
58 ;; (load-library "hideshow")
59 ;; (add-hook 'X-mode-hook ; other modes similarly
60 ;; (lambda () (hs-minor-mode 1)))
62 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
63 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
64 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
66 ;; Additionally, Joseph Eydelnant writes:
67 ;; I enjoy your package hideshow.el Ver. 5.24 2001/02/13
68 ;; a lot and I've been looking for the following functionality:
69 ;; toggle hide/show all with a single key.
70 ;; Here are a few lines of code that lets me do just that.
72 ;; (defvar my-hs-hide nil "Current state of hideshow for toggling all.")
74 ;; (defun my-toggle-hideshow-all () "Toggle hideshow all."
76 ;; (setq my-hs-hide (not my-hs-hide))
81 ;; [Your hideshow hacks here!]
85 ;; You can use `M-x customize-variable' on the following variables:
87 ;; - hs-hide-comments-when-hiding-all -- self-explanatory!
88 ;; - hs-hide-all-non-comment-function -- if non-nil, when doing a
89 ;; `hs-hide-all', this function
90 ;; is called w/ no arguments
91 ;; - hs-isearch-open -- what kind of hidden blocks to
92 ;; open when doing isearch
94 ;; Some languages (e.g., Java) are deeply nested, so the normal behavior
95 ;; of `hs-hide-all' (hiding all but top-level blocks) results in very
96 ;; little information shown, which is not very useful. You can use the
97 ;; variable `hs-hide-all-non-comment-function' to implement your idea of
98 ;; what is more useful. For example, the following code shows the next
99 ;; nested level in addition to the top-level:
101 ;; (defun ttn-hs-hide-level-1 ()
104 ;; (setq hs-hide-all-non-comment-function 'ttn-hs-hide-level-1)
106 ;; Hideshow works w/ incremental search (isearch) by setting the variable
107 ;; `hs-headline', which is the line of text at the beginning of a hidden
108 ;; block that contains a match for the search. You can have this show up
109 ;; in the mode line by modifying the variable `mode-line-format'. For
110 ;; example, the following code prepends this info to the mode line:
112 ;; (unless (memq 'hs-headline mode-line-format)
113 ;; (setq mode-line-format
114 ;; (append '("-" hs-headline) mode-line-format)))
116 ;; See documentation for `mode-line-format' for more info.
118 ;; Hooks are run after some commands:
120 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
121 ;; hs-show-hook hs-show-block, hs-show-all
123 ;; One of `hs-hide-hook' or `hs-show-hook' is run for the toggling
124 ;; commands when the result of the toggle is to hide or show blocks,
125 ;; respectively. All hooks are run w/ `run-hooks'. See docs for each
126 ;; variable or hook for more info.
128 ;; Normally, hideshow tries to determine appropriate values for block
129 ;; and comment definitions by examining the buffer's major mode. If
130 ;; there are problems, hideshow will not activate and in that case you
131 ;; may wish to override hideshow's heuristics by adding an entry to
132 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
133 ;; do something like:
135 ;; (add-to-list 'hs-special-modes-alist '(my-mode "{{" "}}" ...))
137 ;; If you have an entry that works particularly well, consider
138 ;; submitting it for inclusion in hideshow.el. See docstring for
139 ;; `hs-special-modes-alist' for more info on the entry format.
141 ;; See also variable `hs-set-up-overlay' for per-block customization of
142 ;; appearance or other effects associated with overlays. For example:
144 ;; (setq hs-set-up-overlay
145 ;; (defun my-display-code-line-counts (ov)
146 ;; (when (eq 'code (overlay-get ov 'hs))
147 ;; (overlay-put ov 'display
149 ;; (format " ... <%d>"
150 ;; (count-lines (overlay-start ov)
151 ;; (overlay-end ov)))
152 ;; 'face 'font-lock-type-face)))))
156 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
157 ;; function `forward-comment' (among other things). If someone
158 ;; writes this, please send me a copy.
160 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
161 ;; `M-x hs-minor-mode' twice (that is, deactivate then re-activate
164 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.7.
165 ;; XEmacs compatibility may have bitrotted since 4.29.
167 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
168 ;; `byte-compile-file' inserts the file to be compiled in a temporary
169 ;; buffer and switches `normal-mode' on. In the case where you have
170 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
171 ;; the initial comment sometimes hides parts of the first statement (seems
172 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
174 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
176 ;; (defadvice byte-compile-file (around
177 ;; byte-compile-file-hideshow-off
179 ;; (let ((hs-minor-mode-hook nil))
182 ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the
183 ;; suggested workaround is to turn off hideshow entirely, for example:
185 ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow)
186 ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow)
188 ;; In the case of `vc-diff', here is a less invasive workaround:
190 ;; (add-hook 'vc-before-checkin-hook
192 ;; (goto-char (point-min))
195 ;; Unfortunately, these workarounds do not restore hideshow state.
196 ;; If someone figures out a better way, please let me know.
200 ;; Correspondance welcome; please indicate version number. Send bug
201 ;; reports and inquiries to <ttn@gnu.org>.
205 ;; Thanks go to the following people for valuable ideas, code and
208 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave Love,
209 ;; Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray, Moody Ahmad,
210 ;; Preston F. Crow, Lars Lindberg, Reto Zimmermann, Keith Sheffield,
211 ;; Chew Meng Kuan, Tony Lam, Pete Ware, François Pinard, Stefan Monnier,
212 ;; Joseph Eydelnant, Michael Ernst, Peter Heslin
214 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
215 ;; overlays (rather than selective display), added isearch magic, folded
216 ;; in custom.el compatibility, generalized comment handling, incorporated
217 ;; mouse support, and maintained the code in general. Version 4.0 is
218 ;; largely due to his efforts.
222 ;; Hideshow was inspired when I learned about selective display. It was
223 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
224 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
225 ;; been lost. XEmacs support is reliable as of 4.29. State save and
226 ;; restore was added in 3.5 (not widely distributed), and reliable as of
227 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
228 ;; Version 5.x uses new algorithms for block selection and traversal,
229 ;; unbundles state save and restore, and includes more isearch support.
233 ;;---------------------------------------------------------------------------
234 ;; user-configurable variables
236 (defgroup hideshow nil
237 "Minor mode for hiding and showing program and comment blocks."
241 (defcustom hs-hide-comments-when-hiding-all t
242 "*Hide the comments too when you do an `hs-hide-all'."
246 (defcustom hs-minor-mode-hook nil
247 "*Hook called when hideshow minor mode is activated or deactivated."
252 (defcustom hs-isearch-open
'code
253 "*What kind of hidden blocks to open when doing `isearch'.
254 One of the following symbols:
256 code -- open only code blocks
257 comment -- open only comment blocks
258 t -- open both code and comment blocks
259 nil -- open neither code nor comment blocks
261 This has effect only if `search-invisible' is set to `open'."
262 :type
'(choice (const :tag
"open only code blocks" code
)
263 (const :tag
"open only comment blocks" comment
)
264 (const :tag
"open both code and comment blocks" t
)
265 (const :tag
"don't open any of them" nil
))
269 (defvar hs-special-modes-alist
270 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
271 (c++-mode
"{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
)
272 (bibtex-mode ("@\\S(*\\(\\s(\\)" 1))
273 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning
))
274 "*Alist for initializing the hideshow variables for different modes.
275 Each element has the form
276 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
278 If non-nil, hideshow will use these values as regexps to define blocks
279 and comments, respectively for major mode MODE.
281 START, END and COMMENT-START are regular expressions. A block is
282 defined as text surrounded by START and END.
284 As a special case, START may be a list of the form (COMPLEX-START
285 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
286 MDATA-SELECTOR an integer that specifies which sub-match is the proper
287 place to adjust point, before calling `hs-forward-sexp-func'. Point
288 is adjusted to the beginning of the specified match. For example,
289 see the `hs-special-modes-alist' entry for `bibtex-mode'.
291 For some major modes, `forward-sexp' does not work properly. In those
292 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
294 See the documentation for `hs-adjust-block-beginning' to see what is the
295 use of ADJUST-BEG-FUNC.
297 If any of the elements is left nil or omitted, hideshow tries to guess
298 appropriate values. The regexps should not contain leading or trailing
299 whitespace. Case does not matter.")
301 (defvar hs-hide-all-non-comment-function nil
302 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
304 (defvar hs-allow-nesting nil
305 "*If non-nil, hiding remembers internal blocks.
306 This means that when the outer block is shown again, any
307 previously hidden internal blocks remain hidden.")
309 (defvar hs-hide-hook nil
310 "*Hook called (with `run-hooks') at the end of commands to hide text.
311 These commands include the toggling commands (when the result is to hide
312 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
314 (defvar hs-show-hook nil
315 "*Hook called (with `run-hooks') at the end of commands to show text.
316 These commands include the toggling commands (when the result is to show
317 a block), `hs-show-all' and `hs-show-block'..")
319 (defvar hs-set-up-overlay nil
320 "*Function called with one arg, OV, a newly initialized overlay.
321 Hideshow puts a unique overlay on each range of text to be hidden
322 in the buffer. Here is a simple example of how to use this variable:
324 (defun display-code-line-counts (ov)
325 (when (eq 'code (overlay-get ov 'hs))
326 (overlay-put ov 'display
328 (count-lines (overlay-start ov)
329 (overlay-end ov))))))
331 (setq hs-set-up-overlay 'display-code-line-counts)
333 This example shows how to get information from the overlay as well
334 as how to set its `display' property. See `hs-make-overlay' and
335 info node `(elisp)Overlays'.")
337 ;;---------------------------------------------------------------------------
338 ;; internal variables
340 (defvar hs-minor-mode nil
341 "Non-nil if using hideshow mode as a minor mode of some other mode.
342 Use the command `hs-minor-mode' to toggle or set this variable.")
344 (defvar hs-minor-mode-map
345 (let ((map (make-sparse-keymap)))
346 ;; These bindings roughly imitate those used by Outline mode.
347 (define-key map
"\C-c@\C-h" 'hs-hide-block
)
348 (define-key map
"\C-c@\C-s" 'hs-show-block
)
349 (define-key map
"\C-c@\C-\M-h" 'hs-hide-all
)
350 (define-key map
"\C-c@\C-\M-s" 'hs-show-all
)
351 (define-key map
"\C-c@\C-l" 'hs-hide-level
)
352 (define-key map
"\C-c@\C-c" 'hs-toggle-hiding
)
353 (define-key map
[(shift mouse-2
)] 'hs-mouse-toggle-hiding
)
355 "Keymap for hideshow minor mode.")
357 (easy-menu-define hs-minor-mode-menu hs-minor-mode-map
358 "Menu used when hideshow minor mode is active."
360 ["Hide Block" hs-hide-block
361 :help
"Hide the code or comment block at point"]
362 ["Show Block" hs-show-block
363 :help
"Show the code or comment block at point"]
364 ["Hide All" hs-hide-all
365 :help
"Hide all the blocks in the buffer"]
366 ["Show All" hs-show-all
367 :help
"Show all the blocks in the buffer"]
368 ["Hide Level" hs-hide-level
369 :help
"Hide all block at levels below the current block"]
370 ["Toggle Hiding" hs-toggle-hiding
371 :help
"Toggle the hiding state of the current block"]
373 ["Hide comments when hiding all"
374 (setq hs-hide-comments-when-hiding-all
375 (not hs-hide-comments-when-hiding-all
))
376 :help
"If t also hide comment blocks when doing `hs-hide-all'"
377 :style toggle
:selected hs-hide-comments-when-hiding-all
]
379 ["Code blocks" (setq hs-isearch-open
'code
)
380 :help
"Show hidden code blocks when isearch matches inside them"
381 :active t
:style radio
:selected
(eq hs-isearch-open
'code
)]
382 ["Comment blocks" (setq hs-isearch-open
'comment
)
383 :help
"Show hidden comment blocks when isearch matches inside them"
384 :active t
:style radio
:selected
(eq hs-isearch-open
'comment
)]
385 ["Code and Comment blocks" (setq hs-isearch-open t
)
386 :help
"Show both hidden code and comment blocks when isearch matches inside them"
387 :active t
:style radio
:selected
(eq hs-isearch-open t
)]
388 ["None" (setq hs-isearch-open nil
)
389 :help
"Do not hidden code or comment blocks when isearch matches inside them"
390 :active t
:style radio
:selected
(eq hs-isearch-open nil
)])))
392 (defvar hs-c-start-regexp nil
393 "Regexp for beginning of comments.
394 Differs from mode-specific comment regexps in that
395 surrounding whitespace is stripped.")
396 (make-variable-buffer-local 'hs-c-start-regexp
)
398 (defvar hs-block-start-regexp nil
399 "Regexp for beginning of block.")
400 (make-variable-buffer-local 'hs-block-start-regexp
)
402 (defvar hs-block-start-mdata-select nil
403 "Element in `hs-block-start-regexp' match data to consider as block start.
404 The internal function `hs-forward-sexp' moves point to the beginning of this
405 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
406 (make-variable-buffer-local 'hs-block-start-mdata-select
)
408 (defvar hs-block-end-regexp nil
409 "Regexp for end of block.")
411 (defvar hs-forward-sexp-func
'forward-sexp
412 "Function used to do a `forward-sexp'.
413 Should change for Algol-ish modes. For single-character block
414 delimiters -- ie, the syntax table regexp for the character is
415 either `(' or `)' -- `hs-forward-sexp-func' would just be
416 `forward-sexp'. For other modes such as simula, a more specialized
417 function is necessary.")
418 (make-variable-buffer-local 'hs-forward-sexp-func
)
420 (defvar hs-adjust-block-beginning nil
421 "Function used to tweak the block beginning.
422 The block is hidden from the position returned by this function,
423 as opposed to hiding it from the position returned when searching
424 for `hs-block-start-regexp'.
426 For example, in c-like modes, if we wish to also hide the curly braces
427 \(if you think they occupy too much space on the screen), this function
428 should return the starting point (at the end of line) of the hidden
431 It is called with a single argument ARG which is the position in
432 buffer after the block beginning.
434 It should return the position from where we should start hiding.
436 It should not move the point.
438 See `hs-c-like-adjust-block-beginning' for an example of using this.")
439 (make-variable-buffer-local 'hs-adjust-block-beginning
)
441 (defvar hs-headline nil
442 "Text of the line where a hidden block begins, set during isearch.
443 You can display this in the mode line by adding the symbol `hs-headline'
444 to the variable `mode-line-format'. For example,
446 (unless (memq 'hs-headline mode-line-format)
447 (setq mode-line-format
448 (append '(\"-\" hs-headline) mode-line-format)))
450 Note that `mode-line-format' is buffer-local.")
452 ;;---------------------------------------------------------------------------
455 (defun hs-discard-overlays (from to
)
456 "Delete hideshow overlays in region defined by FROM and TO.
457 Skip \"internal\" overlays if `hs-allow-nesting' is non-nil."
459 (setq from
(prog1 to
(setq to from
))))
462 (while (> to
(setq from
(next-overlay-change from
)))
463 (when (setq ov
(hs-overlay-at from
))
464 (setq from
(overlay-end ov
))
465 (delete-overlay ov
))))
466 (dolist (ov (overlays-in from to
))
467 (when (overlay-get ov
'hs
)
468 (delete-overlay ov
)))))
470 (defun hs-make-overlay (b e kind
&optional b-offset e-offset
)
471 "Return a new overlay in region defined by B and E with type KIND.
472 KIND is either `code' or `comment'. Optional fourth arg B-OFFSET
473 when added to B specifies the actual buffer position where the block
474 begins. Likewise for optional fifth arg E-OFFSET. If unspecified
475 they are taken to be 0 (zero). The following properties are set
476 in the overlay: 'invisible 'hs 'hs-b-offset 'hs-e-offset. Also,
477 depending on variable `hs-isearch-open', the following properties may
478 be present: 'isearch-open-invisible 'isearch-open-invisible-temporary.
479 If variable `hs-set-up-overlay' is non-nil it should specify a function
480 to call with the newly initialized overlay."
481 (unless b-offset
(setq b-offset
0))
482 (unless e-offset
(setq e-offset
0))
483 (let ((ov (make-overlay b e
))
484 (io (if (eq 'block hs-isearch-open
)
485 ;; backward compatibility -- `block'<=>`code'
488 (overlay-put ov
'invisible
'hs
)
489 (overlay-put ov
'hs kind
)
490 (overlay-put ov
'hs-b-offset b-offset
)
491 (overlay-put ov
'hs-e-offset e-offset
)
492 (when (or (eq io t
) (eq io kind
))
493 (overlay-put ov
'isearch-open-invisible
'hs-isearch-show
)
494 (overlay-put ov
'isearch-open-invisible-temporary
495 'hs-isearch-show-temporary
))
496 (when hs-set-up-overlay
(funcall hs-set-up-overlay ov
))
499 (defun hs-isearch-show (ov)
500 "Delete overlay OV, and set `hs-headline' to nil.
502 This function is meant to be used as the `isearch-open-invisible'
503 property of an overlay."
504 (setq hs-headline nil
)
507 (defun hs-isearch-show-temporary (ov hide-p
)
508 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
509 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
510 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
513 This function is meant to be used as the `isearch-open-invisible-temporary'
514 property of an overlay."
519 (let ((start (overlay-start ov
)))
521 (save-excursion (goto-char start
)
523 (skip-chars-forward " \t")
526 (force-mode-line-update)
527 ;; handle `display' property specially
530 (when (setq value
(overlay-get ov
'hs-isearch-display
))
531 (overlay-put ov
'display value
)
532 (overlay-put ov
'hs-isearch-display nil
))
533 (when (setq value
(overlay-get ov
'display
))
534 (overlay-put ov
'hs-isearch-display value
)
535 (overlay-put ov
'display nil
))))
536 (overlay-put ov
'invisible
(and hide-p
'hs
)))
538 (defun hs-forward-sexp (match-data arg
)
539 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
540 Original match data is restored upon return."
542 (set-match-data match-data
)
543 (goto-char (match-beginning hs-block-start-mdata-select
))
544 (funcall hs-forward-sexp-func arg
)))
546 (defun hs-hide-comment-region (beg end
&optional repos-end
)
547 "Hide a region from BEG to END, marking it as a comment.
548 Optional arg REPOS-END means reposition at end."
549 (let ((beg-eol (progn (goto-char beg
) (line-end-position)))
550 (end-eol (progn (goto-char end
) (line-end-position))))
551 (hs-discard-overlays beg-eol end-eol
)
552 (hs-make-overlay beg-eol end-eol
'comment beg end
))
553 (goto-char (if repos-end end beg
)))
555 (defun hs-hide-block-at-point (&optional end comment-reg
)
556 "Hide block if on block beginning.
557 Optional arg END means reposition at end.
558 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
559 specifies the limits of the comment, or nil if the block is not
562 The block beginning is adjusted by `hs-adjust-block-beginning'
563 and then further adjusted to be at the end of the line."
565 (hs-hide-comment-region (car comment-reg
) (cadr comment-reg
) end
)
566 (when (looking-at hs-block-start-regexp
)
567 (let* ((mdata (match-data t
))
568 (pure-p (match-end 0))
570 ;; `p' is the point at the end of the block beginning,
571 ;; which may need to be adjusted
573 (goto-char (funcall (or hs-adjust-block-beginning
576 ;; whatever the adjustment, we move to eol
577 (line-end-position)))
579 ;; `q' is the point at the end of the block
580 (progn (hs-forward-sexp mdata
1)
584 (when (and (< p
(point)) (> (count-lines p q
) 1))
585 (cond ((and hs-allow-nesting
(setq ov
(hs-overlay-at p
)))
587 ((not hs-allow-nesting
)
588 (hs-discard-overlays p q
)))
589 (hs-make-overlay p q
'code
(- pure-p p
)))
590 (goto-char (if end q
(min p pure-p
)))))))
592 (defun hs-inside-comment-p ()
593 "Return non-nil if point is inside a comment, otherwise nil.
594 Actually, return a list containing the buffer position of the start
595 and the end of the comment. A comment block can be hidden only if on
596 its starting line there is only whitespace preceding the actual comment
597 beginning. If we are inside of a comment but this condition is not met,
598 we return a list having a nil as its car and the end of comment position
601 ;; the idea is to look backwards for a comment start regexp, do a
602 ;; forward comment, and see if we are inside, then extend extend
603 ;; forward and backward as long as we have comments
605 (when (or (looking-at hs-c-start-regexp
)
606 (re-search-backward hs-c-start-regexp
(point-min) t
))
607 ;; first get to the beginning of this comment...
608 (while (and (not (bobp))
609 (= (point) (progn (forward-comment -
1) (point))))
611 ;; ...then extend backwards
612 (forward-comment (- (buffer-size)))
613 (skip-chars-forward " \t\n\f")
617 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp
))
618 ;; we are in this situation: (example)
623 ;; the point was here before doing (beginning-of-line)
624 ;; here we should advance till the next comment which
625 ;; eventually has only white spaces preceding it on the same
629 (skip-chars-forward " \t\n\f")
631 (while (and (< (point) q
)
633 (not (looking-at hs-c-start-regexp
)))
634 ;; avoid an infinite cycle
637 (skip-chars-forward " \t\n\f"))
638 (when (or (not (looking-at hs-c-start-regexp
))
640 ;; we cannot hide this comment block
642 ;; goto the end of the comment
643 (forward-comment (buffer-size))
644 (skip-chars-backward " \t\n\f")
647 (list (and hidable p
) (point))))))))
649 (defun hs-grok-mode-type ()
650 "Set up hideshow variables for new buffers.
651 If `hs-special-modes-alist' has information associated with the
652 current buffer's major mode, use that.
653 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
654 function; and adjust-block-beginning function."
655 (if (and (boundp 'comment-start
)
656 (boundp 'comment-end
)
657 comment-start comment-end
)
658 (let* ((lookup (assoc major-mode hs-special-modes-alist
))
659 (start-elem (or (nth 1 lookup
) "\\s(")))
660 (if (listp start-elem
)
661 ;; handle (START-REGEXP MDATA-SELECT)
662 (setq hs-block-start-regexp
(car start-elem
)
663 hs-block-start-mdata-select
(cadr start-elem
))
664 ;; backwards compatibility: handle simple START-REGEXP
665 (setq hs-block-start-regexp start-elem
666 hs-block-start-mdata-select
0))
667 (setq hs-block-end-regexp
(or (nth 2 lookup
) "\\s)")
668 hs-c-start-regexp
(or (nth 3 lookup
)
669 (let ((c-start-regexp
670 (regexp-quote comment-start
)))
671 (if (string-match " +$" c-start-regexp
)
672 (substring c-start-regexp
673 0 (1- (match-end 0)))
675 hs-forward-sexp-func
(or (nth 4 lookup
) 'forward-sexp
)
676 hs-adjust-block-beginning
(nth 5 lookup
)))
677 (setq hs-minor-mode nil
)
678 (error "%s Mode doesn't support Hideshow Minor Mode"
679 (format-mode-line mode-name
))))
681 (defun hs-find-block-beginning ()
682 "Reposition point at block-start.
683 Return point, or nil if original point was not in a block."
686 ;; look if current line is block start
687 (if (looking-at hs-block-start-regexp
)
689 ;; look backward for the start of a block that contains the cursor
690 (while (and (re-search-backward hs-block-start-regexp nil t
)
692 (< here
(save-excursion
693 (hs-forward-sexp (match-data t
) 1)
700 (defun hs-hide-level-recursive (arg minp maxp
)
701 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
702 (when (hs-find-block-beginning)
703 (setq minp
(1+ (point)))
704 (funcall hs-forward-sexp-func
1)
705 (setq maxp
(1- (point))))
706 (unless hs-allow-nesting
707 (hs-discard-overlays minp maxp
))
710 (forward-comment (buffer-size))
711 (and (< (point) maxp
)
712 (re-search-forward hs-block-start-regexp maxp t
)))
714 (hs-hide-level-recursive (1- arg
) minp maxp
)
715 (goto-char (match-beginning hs-block-start-mdata-select
))
716 (hs-hide-block-at-point t
)))
719 (defmacro hs-life-goes-on
(&rest body
)
720 "Evaluate BODY forms if variable `hs-minor-mode' is non-nil.
721 In the dynamic context of this macro, `inhibit-point-motion-hooks'
722 and `case-fold-search' are both t."
724 (let ((inhibit-point-motion-hooks t
)
725 (case-fold-search t
))
728 (put 'hs-life-goes-on
'edebug-form-spec
'(&rest form
))
730 (defun hs-overlay-at (position)
731 "Return hideshow overlay at POSITION, or nil if none to be found."
732 (let ((overlays (overlays-at position
))
734 (while (and (not found
) (setq ov
(car overlays
)))
735 (setq found
(and (overlay-get ov
'hs
) ov
)
736 overlays
(cdr overlays
)))
739 (defun hs-already-hidden-p ()
740 "Return non-nil if point is in an already-hidden block, otherwise nil."
742 (let ((c-reg (hs-inside-comment-p)))
743 (if (and c-reg
(nth 0 c-reg
))
744 ;; point is inside a comment, and that comment is hidable
745 (goto-char (nth 0 c-reg
))
747 (when (and (not c-reg
)
748 (hs-find-block-beginning)
749 (looking-at hs-block-start-regexp
))
750 ;; point is inside a block
751 (goto-char (match-end 0)))))
753 (hs-overlay-at (point))))
755 (defun hs-c-like-adjust-block-beginning (initial)
756 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
757 Actually, point is never moved; a new position is returned that is
758 the end of the C-function header. This adjustment function is meant
759 to be assigned to `hs-adjust-block-beginning' for C-like modes."
761 (goto-char (1- initial
))
762 (forward-comment (- (buffer-size)))
765 ;;---------------------------------------------------------------------------
768 (defun hs-hide-all ()
769 "Hide all top level blocks, displaying only first and last lines.
770 Move point to the beginning of the line, and run the normal hook
771 `hs-hide-hook'. See documentation for `run-hooks'.
772 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
776 (unless hs-allow-nesting
777 (hs-discard-overlays (point-min) (point-max)))
778 (goto-char (point-min))
779 (let ((spew (make-progress-reporter "Hiding all blocks..."
780 (point-min) (point-max)))
782 hs-block-start-regexp
784 (if hs-hide-comments-when-hiding-all
790 (unless hs-hide-comments-when-hiding-all
791 (forward-comment (point-max)))
792 (re-search-forward re
(point-max) t
))
793 (if (match-beginning 1)
794 ;; we have found a block beginning
796 (goto-char (match-beginning 1))
797 (if hs-hide-all-non-comment-function
798 (funcall hs-hide-all-non-comment-function
)
799 (hs-hide-block-at-point t
)))
800 ;; found a comment, probably
801 (let ((c-reg (hs-inside-comment-p)))
802 (when (and c-reg
(car c-reg
))
803 (if (> (count-lines (car c-reg
) (nth 1 c-reg
)) 1)
804 (hs-hide-block-at-point t c-reg
)
805 (goto-char (nth 1 c-reg
))))))
806 (progress-reporter-update spew
(point)))
807 (progress-reporter-done spew
)))
809 (run-hooks 'hs-hide-hook
)))
811 (defun hs-show-all ()
812 "Show everything then run `hs-show-hook'. See `run-hooks'."
815 (message "Showing all blocks ...")
816 (let ((hs-allow-nesting nil
))
817 (hs-discard-overlays (point-min) (point-max)))
818 (message "Showing all blocks ... done")
819 (run-hooks 'hs-show-hook
)))
821 (defun hs-hide-block (&optional end
)
822 "Select a block and hide it. With prefix arg, reposition at END.
823 Upon completion, point is repositioned and the normal hook
824 `hs-hide-hook' is run. See documentation for `run-hooks'."
827 (let ((c-reg (hs-inside-comment-p)))
829 ((and c-reg
(or (null (nth 0 c-reg
))
830 (<= (count-lines (car c-reg
) (nth 1 c-reg
)) 1)))
831 (message "(not enough comment lines to hide)"))
833 (looking-at hs-block-start-regexp
)
834 (hs-find-block-beginning))
835 (hs-hide-block-at-point end c-reg
)
836 (run-hooks 'hs-hide-hook
))))))
838 (defun hs-show-block (&optional end
)
839 "Select a block and show it.
840 With prefix arg, reposition at END. Upon completion, point is
841 repositioned and the normal hook `hs-show-hook' is run.
842 See documentation for functions `hs-hide-block' and `run-hooks'."
846 ;; first see if we have something at the end of the line
847 (let ((ov (hs-overlay-at (line-end-position)))
851 (cond (end (overlay-end ov
))
852 ((eq 'comment
(overlay-get ov
'hs
)) here
)
853 (t (+ (overlay-start ov
) (overlay-get ov
'hs-b-offset
)))))
856 ;; not immediately obvious, look for a suitable block
857 (let ((c-reg (hs-inside-comment-p))
863 ((and (hs-find-block-beginning)
864 ;; ugh, fresh match-data
865 (looking-at hs-block-start-regexp
))
867 q
(progn (hs-forward-sexp (match-data t
) 1) (point)))))
869 (hs-discard-overlays p q
)
870 (goto-char (if end q
(1+ p
)))))
871 (run-hooks 'hs-show-hook
))))
873 (defun hs-hide-level (arg)
874 "Hide all blocks ARG levels below this block.
875 The hook `hs-hide-hook' is run; see `run-hooks'."
879 (message "Hiding blocks ...")
880 (hs-hide-level-recursive arg
(point-min) (point-max))
881 (message "Hiding blocks ... done"))
882 (run-hooks 'hs-hide-hook
)))
884 (defun hs-toggle-hiding ()
885 "Toggle hiding/showing of a block.
886 See `hs-hide-block' and `hs-show-block'."
889 (if (hs-already-hidden-p)
893 (defun hs-mouse-toggle-hiding (e)
894 "Toggle hiding/showing of a block.
895 This command should be bound to a mouse key.
896 Argument E is a mouse event used by `mouse-set-point'.
897 See `hs-hide-block' and `hs-show-block'."
903 (defun hs-hide-initial-comment-block ()
904 "Hide the first block of comments in a file.
905 This can be useful if you have huge RCS logs in those comments."
908 (let ((c-reg (save-excursion
909 (goto-char (point-min))
910 (skip-chars-forward " \t\n\f")
911 (hs-inside-comment-p))))
913 (let ((beg (car c-reg
)) (end (cadr c-reg
)))
914 ;; see if we have enough comment lines to hide
915 (when (> (count-lines beg end
) 1)
916 (hs-hide-comment-region beg end
)))))))
919 (define-minor-mode hs-minor-mode
920 "Minor mode to selectively hide/show code and comment blocks.
921 When hideshow minor mode is on, the menu bar is augmented with hideshow
922 commands and the hideshow commands are enabled.
923 The value '(hs . t) is added to `buffer-invisibility-spec'.
925 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
926 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
927 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
929 Turning hideshow minor mode off reverts the menu bar and the
930 variables to default values and disables the hideshow commands.
932 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
935 \\{hs-minor-mode-map}"
938 :keymap hs-minor-mode-map
939 (setq hs-headline nil
)
943 ;; Turn off this mode if we change major modes.
944 (add-hook 'change-major-mode-hook
947 (easy-menu-add hs-minor-mode-menu
)
948 (set (make-local-variable 'line-move-ignore-invisible
) t
)
949 (add-to-invisibility-spec '(hs . t
)))
950 (remove-from-invisibility-spec '(hs . t
))
951 ;; hs-show-all does nothing unless h-m-m is non-nil.
952 (let ((hs-minor-mode t
))
956 (defun turn-off-hideshow ()
957 "Unconditionally turn off `hs-minor-mode'."
960 ;;---------------------------------------------------------------------------
965 ;; arch-tag: 378b6852-e82a-466a-aee8-d9c73859a65e
966 ;;; hideshow.el ends here