Regenerated.
[emacs.git] / lisp / progmodes / hideshow.el
bloba1d601412f2294dc794b14a0123c8d1b0acbf7ab
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)
16 ;; any later version.
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.
28 ;;; Commentary:
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
34 ;; keybindings) are:
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).
53 ;; * Suggested usage
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.")
74 ;; ;;;###autoload
75 ;; (defun my-toggle-hideshow-all () "Toggle hideshow all."
76 ;; (interactive)
77 ;; (setq my-hs-hide (not my-hs-hide))
78 ;; (if my-hs-hide
79 ;; (hs-hide-all)
80 ;; (hs-show-all)))
82 ;; [Your hideshow hacks here!]
84 ;; * Customization
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 ()
103 ;; (hs-hide-level 1)
104 ;; (forward-sexp 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.
145 ;; * Bugs
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
153 ;; hideshow).
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
169 ;; act)
170 ;; (let ((hs-minor-mode-hook nil))
171 ;; ad-do-it))
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
183 ;; '(lambda ()
184 ;; (goto-char (point-min))
185 ;; (hs-show-block)))
187 ;; Unfortunately, these workarounds do not restore hideshow state.
188 ;; If someone figures out a better way, please let me know.
190 ;; * Correspondance
192 ;; Correspondance welcome; please indicate version number. Send bug
193 ;; reports and inquiries to <ttn@gnu.org>.
195 ;; * Thanks
197 ;; Thanks go to the following people for valuable ideas, code and
198 ;; bug reports.
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
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.
212 ;; * History
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.
223 ;;; Code:
225 (require 'easymenu)
227 ;;---------------------------------------------------------------------------
228 ;; user-configurable variables
230 (defgroup hideshow nil
231 "Minor mode for hiding and showing program and comment blocks."
232 :prefix "hs-"
233 :group 'languages)
235 ;;;###autoload
236 (defcustom hs-hide-comments-when-hiding-all t
237 "*Hide the comments too when you do an `hs-hide-all'."
238 :type 'boolean
239 :group 'hideshow)
241 (defcustom hs-minor-mode-hook nil
242 "*Hook called when hideshow minor mode is activated or deactivated."
243 :type 'hook
244 :group 'hideshow
245 :version "21.1")
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))
261 :group 'hideshow)
263 ;;;###autoload
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'. For
284 example, see the `hs-special-modes-alist' entry for `bibtex-mode'.
286 For some major modes, `forward-sexp' does not work properly. In those
287 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
289 See the documentation for `hs-adjust-block-beginning' to see what is the
290 use of ADJUST-BEG-FUNC.
292 If any of the elements is left nil or omitted, hideshow tries to guess
293 appropriate values. The regexps should not contain leading or trailing
294 whitespace. Case does not matter.")
296 (defvar hs-hide-all-non-comment-function nil
297 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
299 (defvar hs-hide-hook nil
300 "*Hook called (with `run-hooks') at the end of commands to hide text.
301 These commands include the toggling commands (when the result is to hide
302 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
304 (defvar hs-show-hook nil
305 "*Hook called (with `run-hooks') at the end of commands to show text.
306 These commands include the toggling commands (when the result is to show
307 a block), `hs-show-all' and `hs-show-block'..")
309 ;;---------------------------------------------------------------------------
310 ;; internal variables
312 (defvar hs-minor-mode nil
313 "Non-nil if using hideshow mode as a minor mode of some other mode.
314 Use the command `hs-minor-mode' to toggle or set this variable.")
316 (defvar hs-minor-mode-map nil
317 "Keymap for hideshow minor mode.")
319 (defvar hs-minor-mode-menu nil
320 "Menu for hideshow minor mode.")
322 (defvar hs-c-start-regexp nil
323 "Regexp for beginning of comments.
324 Differs from mode-specific comment regexps in that
325 surrounding whitespace is stripped.")
327 (defvar hs-block-start-regexp nil
328 "Regexp for beginning of block.")
330 (defvar hs-block-start-mdata-select nil
331 "Element in `hs-block-start-regexp' match data to consider as block start.
332 The internal function `hs-forward-sexp' moves point to the beginning of this
333 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
335 (defvar hs-block-end-regexp nil
336 "Regexp for end of block.")
338 (defvar hs-forward-sexp-func 'forward-sexp
339 "Function used to do a `forward-sexp'.
340 Should change for Algol-ish modes. For single-character block
341 delimiters -- ie, the syntax table regexp for the character is
342 either `(' or `)' -- `hs-forward-sexp-func' would just be
343 `forward-sexp'. For other modes such as simula, a more specialized
344 function is necessary.")
346 (defvar hs-adjust-block-beginning nil
347 "Function used to tweak the block beginning.
348 The block is hidden from the position returned by this function,
349 as opposed to hiding it from the position returned when searching
350 for `hs-block-start-regexp'.
352 For example, in c-like modes, if we wish to also hide the curly braces
353 \(if you think they occupy too much space on the screen), this function
354 should return the starting point (at the end of line) of the hidden
355 region.
357 It is called with a single argument ARG which is the position in
358 buffer after the block beginning.
360 It should return the position from where we should start hiding.
362 It should not move the point.
364 See `hs-c-like-adjust-block-beginning' for an example of using this.")
366 (defvar hs-headline nil
367 "Text of the line where a hidden block begins, set during isearch.
368 You can display this in the mode line by adding the symbol `hs-headline'
369 to the variable `mode-line-format'. For example,
371 (unless (memq 'hs-headline mode-line-format)
372 (setq mode-line-format
373 (append '(\"-\" hs-headline) mode-line-format)))
375 Note that `mode-line-format' is buffer-local.")
377 ;;---------------------------------------------------------------------------
378 ;; system dependency
380 ; ;; xemacs compatibility
381 ; (when (string-match "xemacs\\|lucid" emacs-version)
382 ; ;; use pre-packaged compatiblity layer
383 ; (require 'overlay))
385 ; ;; xemacs and emacs-19 compatibility
386 ; (when (or (not (fboundp 'add-to-invisibility-spec))
387 ; (not (fboundp 'remove-from-invisibility-spec)))
388 ; ;; `buffer-invisibility-spec' mutators snarfed from Emacs 20.3 lisp/subr.el
389 ; (defun add-to-invisibility-spec (arg)
390 ; (cond
391 ; ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
392 ; (setq buffer-invisibility-spec (list arg)))
393 ; (t
394 ; (setq buffer-invisibility-spec
395 ; (cons arg buffer-invisibility-spec)))))
396 ; (defun remove-from-invisibility-spec (arg)
397 ; (if buffer-invisibility-spec
398 ; (setq buffer-invisibility-spec
399 ; (delete arg buffer-invisibility-spec)))))
401 ;; hs-match-data
402 (defalias 'hs-match-data 'match-data)
404 ;;---------------------------------------------------------------------------
405 ;; support functions
407 (defun hs-discard-overlays (from to)
408 "Delete hideshow overlays in region defined by FROM and TO."
409 (when (< to from)
410 (setq from (prog1 to (setq to from))))
411 (let ((ovs (overlays-in from to)))
412 (while ovs
413 (let ((ov (car ovs)))
414 (when (overlay-get ov 'hs)
415 (delete-overlay ov)))
416 (setq ovs (cdr ovs)))))
418 (defun hs-isearch-show (ov)
419 "Delete overlay OV, and set `hs-headline' to nil.
421 This function is meant to be used as the `isearch-open-invisible'
422 property of an overlay."
423 (setq hs-headline nil)
424 (delete-overlay ov))
426 (defun hs-isearch-show-temporary (ov hide-p)
427 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
428 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
429 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
430 OV is shown.
432 This function is meant to be used as the `isearch-open-invisible-temporary'
433 property of an overlay."
434 (setq hs-headline
435 (if hide-p
437 (or hs-headline
438 (let ((start (overlay-start ov)))
439 (buffer-substring
440 (save-excursion (goto-char start)
441 (beginning-of-line)
442 (skip-chars-forward " \t")
443 (point))
444 start)))))
445 (force-mode-line-update)
446 (overlay-put ov 'invisible (and hide-p 'hs)))
448 (defun hs-flag-region (from to flag)
449 "Hide or show lines from FROM to TO, according to FLAG.
450 If FLAG is nil then text is shown, while if FLAG is non-nil the text is
451 hidden. FLAG must be one of the symbols `code' or `comment', depending
452 on what kind of block is to be hidden."
453 (save-excursion
454 ;; first clear it all out
455 (hs-discard-overlays from to)
456 ;; now create overlays if needed
457 (when flag
458 (let ((overlay (make-overlay from to)))
459 (overlay-put overlay 'invisible 'hs)
460 (overlay-put overlay 'hs flag)
461 (when (or (eq hs-isearch-open t)
462 (eq hs-isearch-open flag)
463 ;; deprecated backward compatibility -- `block'<=>`code'
464 (and (eq 'block hs-isearch-open)
465 (eq 'code flag)))
466 (overlay-put overlay 'isearch-open-invisible 'hs-isearch-show)
467 (overlay-put overlay
468 'isearch-open-invisible-temporary
469 'hs-isearch-show-temporary))
470 overlay))))
472 (defun hs-forward-sexp (match-data arg)
473 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
474 Original match data is restored upon return."
475 (save-match-data
476 (set-match-data match-data)
477 (goto-char (match-beginning hs-block-start-mdata-select))
478 (funcall hs-forward-sexp-func arg)))
480 (defun hs-hide-comment-region (beg end &optional repos-end)
481 "Hide a region from BEG to END, marking it as a comment.
482 Optional arg REPOS-END means reposition at end."
483 (hs-flag-region (progn (goto-char beg) (end-of-line) (point))
484 (progn (goto-char end) (end-of-line) (point))
485 'comment)
486 (goto-char (if repos-end end beg)))
488 (defun hs-hide-block-at-point (&optional end comment-reg)
489 "Hide block iff on block beginning.
490 Optional arg END means reposition at end.
491 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
492 specifies the limits of the comment, or nil if the block is not
493 a comment.
495 The block beginning is adjusted by `hs-adjust-block-beginning'
496 and then further adjusted to be at the end of the line."
497 (if comment-reg
498 (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
499 (if (looking-at hs-block-start-regexp)
500 (let* ((mdata (hs-match-data t))
501 (pure-p (match-end 0))
503 ;; `p' is the point at the end of the block beginning,
504 ;; which may need to be adjusted
505 (save-excursion
506 (goto-char (funcall (or hs-adjust-block-beginning
507 'identity)
508 pure-p))
509 ;; whatever the adjustment, we move to eol
510 (end-of-line)
511 (point)))
513 ;; `q' is the point at the end of the block
514 (progn (hs-forward-sexp mdata 1)
515 (end-of-line)
516 (point))))
517 (if (and (< p (point)) (> (count-lines p q) 1))
518 (overlay-put (hs-flag-region p q 'code)
519 'hs-ofs
520 (- pure-p p)))
521 (goto-char (if end q (min p pure-p)))))))
523 (defun hs-safety-is-job-n ()
524 "Warn if `buffer-invisibility-spec' does not contain symbol `hs'."
525 (unless (and (listp buffer-invisibility-spec)
526 (assq 'hs buffer-invisibility-spec))
527 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
528 (sit-for 2)))
530 (defun hs-inside-comment-p ()
531 "Return non-nil if point is inside a comment, otherwise nil.
532 Actually, return a list containing the buffer position of the start
533 and the end of the comment. A comment block can be hidden only if on
534 its starting line there is only whitespace preceding the actual comment
535 beginning. If we are inside of a comment but this condition is not met,
536 we return a list having a nil as its car and the end of comment position
537 as cdr."
538 (save-excursion
539 ;; the idea is to look backwards for a comment start regexp, do a
540 ;; forward comment, and see if we are inside, then extend extend
541 ;; forward and backward as long as we have comments
542 (let ((q (point)))
543 (when (or (looking-at hs-c-start-regexp)
544 (re-search-backward hs-c-start-regexp (point-min) t))
545 (forward-comment (- (buffer-size)))
546 (skip-chars-forward " \t\n\f")
547 (let ((p (point))
548 (not-hidable nil))
549 (beginning-of-line)
550 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
551 ;; we are in this situation: (example)
552 ;; (defun bar ()
553 ;; (foo)
554 ;; ) ; comment
555 ;; ^
556 ;; the point was here before doing (beginning-of-line)
557 ;; here we should advance till the next comment which
558 ;; eventually has only white spaces preceding it on the same
559 ;; line
560 (goto-char p)
561 (forward-comment 1)
562 (skip-chars-forward " \t\n\f")
563 (setq p (point))
564 (while (and (< (point) q)
565 (> (point) p)
566 (not (looking-at hs-c-start-regexp)))
567 (setq p (point));; use this to avoid an infinite cycle
568 (forward-comment 1)
569 (skip-chars-forward " \t\n\f"))
570 (if (or (not (looking-at hs-c-start-regexp))
571 (> (point) q))
572 ;; we cannot hide this comment block
573 (setq not-hidable t)))
574 ;; goto the end of the comment
575 (forward-comment (buffer-size))
576 (skip-chars-backward " \t\n\f")
577 (end-of-line)
578 (if (>= (point) q)
579 (list (if not-hidable nil p) (point))))))))
581 (defun hs-grok-mode-type ()
582 "Set up hideshow variables for new buffers.
583 If `hs-special-modes-alist' has information associated with the
584 current buffer's major mode, use that.
585 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
586 function; and adjust-block-beginning function."
587 (if (and (boundp 'comment-start)
588 (boundp 'comment-end)
589 comment-start comment-end)
590 (let* ((lookup (assoc major-mode hs-special-modes-alist))
591 (start-elem (or (nth 1 lookup) "\\s(")))
592 (if (listp start-elem)
593 ;; handle (START-REGEXP MDATA-SELECT)
594 (setq hs-block-start-regexp (car start-elem)
595 hs-block-start-mdata-select (cadr start-elem))
596 ;; backwards compatibility: handle simple START-REGEXP
597 (setq hs-block-start-regexp start-elem
598 hs-block-start-mdata-select 0))
599 (setq hs-block-end-regexp (or (nth 2 lookup) "\\s)")
600 hs-c-start-regexp (or (nth 3 lookup)
601 (let ((c-start-regexp
602 (regexp-quote comment-start)))
603 (if (string-match " +$" c-start-regexp)
604 (substring c-start-regexp
605 0 (1- (match-end 0)))
606 c-start-regexp)))
607 hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
608 hs-adjust-block-beginning (nth 5 lookup)))
609 (progn
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."
616 (let ((done nil)
617 (here (point)))
618 ;; look if current line is block start
619 (if (looking-at hs-block-start-regexp)
620 (point)
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)
623 (not (setq done
624 (< here (save-excursion
625 (hs-forward-sexp (hs-match-data t) 1)
626 (point)))))))
627 (if done
628 (point)
629 (goto-char here)
630 nil))))
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
639 (goto-char minp)
640 (while (progn
641 (forward-comment (buffer-size))
642 (and (< (point) maxp)
643 (re-search-forward hs-block-start-regexp maxp t)))
644 (if (> arg 1)
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)))
648 (hs-safety-is-job-n)
649 (goto-char maxp))
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."
655 `(when hs-minor-mode
656 (let ((inhibit-point-motion-hooks t)
657 (case-fold-search t))
658 ,@body)))
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."
664 (save-excursion
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 (if (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)))))
674 (end-of-line)
675 (let ((overlays (overlays-at (point)))
676 (found nil))
677 (while (and (not found) (overlayp (car overlays)))
678 (setq found (overlay-get (car overlays) 'hs)
679 overlays (cdr overlays)))
680 found)))
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."
687 (save-excursion
688 (goto-char (1- initial))
689 (forward-comment (- (buffer-size)))
690 (point)))
692 ;;---------------------------------------------------------------------------
693 ;; commands
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."
700 (interactive)
701 (hs-life-goes-on
702 (message "Hiding all blocks ...")
703 (save-excursion
704 (hs-flag-region (point-min) (point-max) nil) ; eliminate weirdness
705 (goto-char (point-min))
706 (let ((count 0)
707 (re (concat "\\("
708 hs-block-start-regexp
709 "\\)"
710 (if hs-hide-comments-when-hiding-all
711 (concat "\\|\\("
712 hs-c-start-regexp
713 "\\)")
714 ""))))
715 (while (progn
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
721 (progn
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))
734 (beginning-of-line)
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'."
740 (interactive)
741 (hs-life-goes-on
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'."
751 (interactive "P")
752 (hs-life-goes-on
753 (let ((c-reg (hs-inside-comment-p)))
754 (cond
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)"))
758 ((or c-reg
759 (looking-at hs-block-start-regexp)
760 (hs-find-block-beginning))
761 (hs-hide-block-at-point end c-reg)
762 (hs-safety-is-job-n)
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'."
770 (interactive "P")
771 (hs-life-goes-on
773 ;; first see if we have something at the end of the line
774 (catch 'eol-begins-hidden-region-p
775 (let ((here (point))
776 (ovs (save-excursion (end-of-line) (overlays-at (point)))))
777 (while ovs
778 (let ((ov (car ovs)))
779 (when (overlay-get ov 'hs)
780 (goto-char
781 (cond (end (overlay-end ov))
782 ((eq 'comment (overlay-get ov 'hs)) here)
783 (t (+ (overlay-start ov) (overlay-get ov 'hs-ofs)))))
784 (delete-overlay ov)
785 (throw 'eol-begins-hidden-region-p t)))
786 (setq ovs (cdr ovs)))
787 nil))
788 ;; not immediately obvious, look for a suitable block
789 (let ((c-reg (hs-inside-comment-p))
790 p q)
791 (cond (c-reg
792 (when (car c-reg)
793 (setq p (car c-reg)
794 q (cadr c-reg))))
795 ((and (hs-find-block-beginning)
796 (looking-at hs-block-start-regexp)) ; fresh match-data, ugh
797 (setq p (point)
798 q (progn (hs-forward-sexp (hs-match-data t) 1) (point)))))
799 (when (and p q)
800 (hs-flag-region p q nil)
801 (goto-char (if end q (1+ p)))))
802 (hs-safety-is-job-n)
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'."
808 (interactive "p")
809 (hs-life-goes-on
810 (save-excursion
811 (message "Hiding blocks ...")
812 (hs-hide-level-recursive arg (point-min) (point-max))
813 (message "Hiding blocks ... done"))
814 (hs-safety-is-job-n)
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'."
820 (interactive)
821 (hs-life-goes-on
822 (if (hs-already-hidden-p)
823 (hs-show-block)
824 (hs-hide-block))))
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'."
831 (interactive "@e")
832 (hs-life-goes-on
833 (mouse-set-point e)
834 (hs-toggle-hiding)))
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."
839 (interactive)
840 (hs-life-goes-on
841 (let ((c-reg (save-excursion
842 (goto-char (point-min))
843 (skip-chars-forward " \t\n\f")
844 (hs-inside-comment-p))))
845 (when c-reg
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)))))))
851 ;;;###autoload
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'.
868 Key bindings:
869 \\{hs-minor-mode-map}"
871 (interactive "P")
872 (setq hs-headline nil
873 hs-minor-mode (if (null arg)
874 (not hs-minor-mode)
875 (> (prefix-numeric-value arg) 0)))
876 (if hs-minor-mode
877 (progn
878 (hs-grok-mode-type)
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 ;;---------------------------------------------------------------------------
887 ;; load-time actions
889 ;; keymaps and menus
890 (if hs-minor-mode-map
892 (setq hs-minor-mode-map (make-sparse-keymap))
893 (easy-menu-define hs-minor-mode-menu
894 hs-minor-mode-map
895 "Menu used when hideshow minor mode is active."
896 (cons "Hide/Show"
897 (mapcar
898 ;; Interpret each table entry as follows: first, populate keymap
899 ;; with elements 2 and 1; then, for easymenu, use entry directly
900 ;; unless element 0 is nil, in which case the entry is "omitted".
901 (lambda (ent)
902 (define-key hs-minor-mode-map (aref ent 2) (aref ent 1))
903 (if (aref ent 0) ent "-----"))
904 ;; These bindings roughly imitate those used by Outline mode.
905 ;; menu entry command key
906 '(["Hide Block" hs-hide-block "\C-c@\C-h"]
907 ["Show Block" hs-show-block "\C-c@\C-s"]
908 ["Hide All" hs-hide-all "\C-c@\C-\M-h"]
909 ["Show All" hs-show-all "\C-c@\C-\M-s"]
910 ["Hide Level" hs-hide-level "\C-c@\C-l"]
911 ["Toggle Hiding" hs-toggle-hiding "\C-c@\C-c"]
912 [nil hs-mouse-toggle-hiding [(shift mouse-2)]]
913 )))))
915 ;; some housekeeping
916 (or (assq 'hs-minor-mode minor-mode-map-alist)
917 (setq minor-mode-map-alist
918 (cons (cons 'hs-minor-mode hs-minor-mode-map)
919 minor-mode-map-alist)))
920 (or (assq 'hs-minor-mode minor-mode-alist)
921 (setq minor-mode-alist (append minor-mode-alist
922 (list '(hs-minor-mode " hs")))))
924 ;; make some variables permanently buffer-local
925 (let ((vars '(hs-minor-mode
926 hs-c-start-regexp
927 hs-block-start-regexp
928 hs-block-start-mdata-select
929 hs-block-end-regexp
930 hs-forward-sexp-func
931 hs-adjust-block-beginning)))
932 (while vars
933 (let ((var (car vars)))
934 (make-variable-buffer-local var)
935 (put var 'permanent-local t))
936 (setq vars (cdr vars))))
938 ;;---------------------------------------------------------------------------
939 ;; that's it
941 (provide 'hideshow)
943 ;;; hideshow.el ends here