]> code.delx.au - gnu-emacs/blob - lisp/progmodes/hideshow.el
(sh-mark-line): Add help-echo to mouse-highlighted text.
[gnu-emacs] / lisp / progmodes / hideshow.el
1 ;;; hideshow.el --- minor mode cmds to selectively display blocks of code
2
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01 Free Software Foundation
4
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.26
9 ;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29
30 ;; * Commands provided
31 ;;
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:
35 ;;
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 button-2)]
43 ;; hs-hide-initial-comment-block
44 ;;
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.
49 ;;
50 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
51 ;; (similar to other minor modes).
52
53 ;; * Customization
54 ;;
55 ;; You can use `M-x customize-variable' on the following variables:
56 ;;
57 ;; - hs-hide-comments-when-hiding-all -- self-explanatory!
58 ;; - hs-hide-all-non-comment-function -- if non-nil, when doing a
59 ;; `hs-hide-all', this function
60 ;; is called w/ no arguments
61 ;; - hs-isearch-open -- what kind of hidden blocks to
62 ;; open when doing isearch
63 ;;
64 ;; Some languages (e.g., Java) are deeply nested, so the normal behavior
65 ;; of `hs-hide-all' (hiding all but top-level blocks) results in very
66 ;; little information shown, which is not very useful. You can use the
67 ;; variable `hs-hide-all-non-comment-function' to implement your idea of
68 ;; what is more useful. For example, the following code shows the next
69 ;; nested level in addition to the top-level:
70 ;;
71 ;; (defun ttn-hs-hide-level-1 ()
72 ;; (hs-hide-level 1)
73 ;; (forward-sexp 1))
74 ;; (setq hs-hide-all-non-comment-function 'ttn-hs-hide-level-1)
75 ;;
76 ;; Hideshow works w/ incremental search (isearch) by setting the variable
77 ;; `hs-headline', which is the line of text at the beginning of a hidden
78 ;; block that contains a match for the search. You can have this show up
79 ;; in the mode line by modifying the variable `mode-line-format'. For
80 ;; example, the following code prepends this info to the mode line:
81 ;;
82 ;; (unless (memq 'hs-headline mode-line-format)
83 ;; (setq mode-line-format
84 ;; (append '("-" hs-headline) mode-line-format)))
85 ;;
86 ;; See documentation for `mode-line-format' for more info.
87 ;;
88 ;; Hooks are run after some commands:
89 ;;
90 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
91 ;; hs-show-hook hs-show-block, hs-show-all
92 ;;
93 ;; One of `hs-hide-hook' or `hs-show-hook' is run for the toggling
94 ;; commands when the result of the toggle is to hide or show blocks,
95 ;; respectively. All hooks are run w/ `run-hooks'. See docs for each
96 ;; variable or hook for more info.
97 ;;
98 ;; Normally, hideshow tries to determine appropriate values for block
99 ;; and comment definitions by examining the buffer's major mode. If
100 ;; there are problems, hideshow will not activate and in that case you
101 ;; may wish to override hideshow's heuristics by adding an entry to
102 ;; variable `hs-special-modes-alist'. Packages that use hideshow should
103 ;; do something like:
104 ;;
105 ;; (let ((my-mode-hs-info '(my-mode "{{" "}}" ...)))
106 ;; (if (not (member my-mode-hs-info hs-special-modes-alist))
107 ;; (setq hs-special-modes-alist
108 ;; (cons my-mode-hs-info hs-special-modes-alist))))
109 ;;
110 ;; If you have an entry that works particularly well, consider
111 ;; submitting it for inclusion in hideshow.el. See docstring for
112 ;; `hs-special-modes-alist' for more info on the entry format.
113
114 ;; * Suggested usage
115 ;;
116 ;; First make sure hideshow.el is in a directory in your `load-path'.
117 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
118 ;; Then, add the following to your ~/.emacs:
119 ;;
120 ;; (load-library "hideshow")
121 ;; (add-hook 'X-mode-hook ; other modes similarly
122 ;; '(lambda () (hs-minor-mode 1)))
123 ;;
124 ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle
125 ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is
126 ;; activated or deactivated, `hs-minor-mode-hook' is run w/ `run-hooks'.
127
128 ;; * Bugs
129 ;;
130 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
131 ;; function `forward-comment' (among other things). If someone
132 ;; writes this, please send me a copy.
133 ;;
134 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
135 ;; `M-x hs-minor-mode' twice (that is, deactivate then activate
136 ;; hideshow).
137 ;;
138 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.4.
139 ;; XEmacs compatibility may have bitrotted since 4.29.
140 ;;
141 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
142 ;; `byte-compile-file' inserts the file to be compiled in a temporary
143 ;; buffer and switches `normal-mode' on. In the case where you have
144 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
145 ;; the initial comment sometimes hides parts of the first statement (seems
146 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
147 ;;
148 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
149 ;;
150 ;; (defadvice byte-compile-file (around
151 ;; byte-compile-file-hideshow-off
152 ;; act)
153 ;; (let ((hs-minor-mode-hook nil))
154 ;; ad-do-it))
155
156 ;; Correspondance welcome; please indicate version number. Send bug
157 ;; reports and inquiries to <ttn@gnu.org>.
158
159 ;; * Thanks
160 ;;
161 ;; Thanks go to the following people for valuable ideas, code and
162 ;; bug reports.
163 ;;
164 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave
165 ;; Love, Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray,
166 ;; Moody Ahmad, Preston F. Crow, Lars Lindberg, Reto Zimmermann,
167 ;; Keith Sheffield, Chew Meng Kuan, Tony Lam, Pete Ware, François Pinard
168 ;;
169 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
170 ;; overlays (rather than selective display), added isearch magic, folded
171 ;; in custom.el compatibility, generalized comment handling, incorporated
172 ;; mouse support, and maintained the code in general. Version 4.0 is
173 ;; largely due to his efforts.
174
175 ;; * History
176 ;;
177 ;; Hideshow was inspired when I learned about selective display. It was
178 ;; reimplemented to use overlays for 4.0 (see above). WRT older history,
179 ;; entries in the masterfile corresponding to versions 1.x and 2.x have
180 ;; been lost. XEmacs support is reliable as of 4.29. State save and
181 ;; restore was added in 3.5 (not widely distributed), and reliable as of
182 ;; 4.30. Otherwise, the code seems stable. Passes checkdoc as of 4.32.
183 ;; Version 5.x uses new algorithms for block selection and traversal,
184 ;; unbundles state save and restore, and includes more isearch support.
185
186 ;;; Code:
187
188 (require 'easymenu)
189
190 ;;---------------------------------------------------------------------------
191 ;; user-configurable variables
192
193 (defgroup hideshow nil
194 "Minor mode for hiding and showing program and comment blocks."
195 :prefix "hs-"
196 :group 'languages)
197
198 ;;;###autoload
199 (defcustom hs-hide-comments-when-hiding-all t
200 "*Hide the comments too when you do an `hs-hide-all'."
201 :type 'boolean
202 :group 'hideshow)
203
204 (defcustom hs-minor-mode-hook nil
205 "*Hook called when hideshow minor mode is activated or deactivated."
206 :type 'hook
207 :group 'hideshow
208 :version "21.1")
209
210 (defcustom hs-isearch-open 'block
211 "*What kind of hidden blocks to open when doing `isearch'.
212 One of the following symbols:
213
214 block -- open only blocks
215 comment -- open only comments
216 t -- open both blocks and comments
217 nil -- open neither blocks nor comments
218
219 This has effect iff `search-invisible' is set to `open'."
220 :type '(choice (const :tag "open only blocks" block)
221 (const :tag "open only comments" comment)
222 (const :tag "open both blocks and comments" t)
223 (const :tag "don't open any of them" nil))
224 :group 'hideshow)
225
226 ;;;###autoload
227 (defvar hs-special-modes-alist
228 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
229 (c++-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
230 (bibtex-mode ("^@\\S(*\\(\\s(\\)" 1))
231 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
232 )
233 "*Alist for initializing the hideshow variables for different modes.
234 Each element has the form
235 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
236
237 If non-nil, hideshow will use these values as regexps to define blocks
238 and comments, respectively for major mode MODE.
239
240 START, END and COMMENT-START are regular expressions. A block is
241 defined as text surrounded by START and END.
242
243 As a special case, START may be a list of the form (COMPLEX-START
244 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
245 MDATA-SELECTOR an integer that specifies which sub-match is the proper
246 place to adjust point, before calling `hs-forward-sexp-func'. For
247 example, see the `hs-special-modes-alist' entry for `bibtex-mode'.
248
249 For some major modes, `forward-sexp' does not work properly. In those
250 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
251
252 See the documentation for `hs-adjust-block-beginning' to see what is the
253 use of ADJUST-BEG-FUNC.
254
255 If any of the elements is left nil or omitted, hideshow tries to guess
256 appropriate values. The regexps should not contain leading or trailing
257 whitespace. Case does not matter.")
258
259 (defvar hs-hide-all-non-comment-function nil
260 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
261
262 (defvar hs-hide-hook nil
263 "*Hook called (with `run-hooks') at the end of commands to hide text.
264 These commands include the toggling commands (when the result is to hide
265 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
266
267 (defvar hs-show-hook nil
268 "*Hook called (with `run-hooks') at the end of commands to show text.
269 These commands include the toggling commands (when the result is to show
270 a block), `hs-show-all' and `hs-show-block'..")
271
272 ;;---------------------------------------------------------------------------
273 ;; internal variables
274
275 (defvar hs-minor-mode nil
276 "Non-nil if using hideshow mode as a minor mode of some other mode.
277 Use the command `hs-minor-mode' to toggle or set this variable.")
278
279 (defvar hs-minor-mode-map nil
280 "Keymap for hideshow minor mode.")
281
282 (defvar hs-minor-mode-menu nil
283 "Menu for hideshow minor mode.")
284
285 (defvar hs-c-start-regexp nil
286 "Regexp for beginning of comments.
287 Differs from mode-specific comment regexps in that
288 surrounding whitespace is stripped.")
289
290 (defvar hs-block-start-regexp nil
291 "Regexp for beginning of block.")
292
293 (defvar hs-block-start-mdata-select nil
294 "Element in `hs-block-start-regexp' match data to consider as block start.
295 The internal function `hs-forward-sexp' moves point to the beginning of this
296 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
297
298 (defvar hs-block-end-regexp nil
299 "Regexp for end of block.")
300
301 (defvar hs-forward-sexp-func 'forward-sexp
302 "Function used to do a `forward-sexp'.
303 Should change for Algol-ish modes. For single-character block
304 delimiters -- ie, the syntax table regexp for the character is
305 either `(' or `)' -- `hs-forward-sexp-func' would just be
306 `forward-sexp'. For other modes such as simula, a more specialized
307 function is necessary.")
308
309 (defvar hs-adjust-block-beginning nil
310 "Function used to tweak the block beginning.
311 The block is hidden from the position returned by this function,
312 as opposed to hiding it from the position returned when searching
313 for `hs-block-start-regexp'.
314
315 For example, in c-like modes, if we wish to also hide the curly braces
316 (if you think they occupy too much space on the screen), this function
317 should return the starting point (at the end of line) of the hidden
318 region.
319
320 It is called with a single argument ARG which is the the position in
321 buffer after the block beginning.
322
323 It should return the position from where we should start hiding.
324
325 It should not move the point.
326
327 See `hs-c-like-adjust-block-beginning' for an example of using this.")
328
329 (defvar hs-headline nil
330 "Text of the line where a hidden block begins, set during isearch.
331 You can display this in the mode line by adding the symbol `hs-headline'
332 to the variable `mode-line-format'. For example,
333
334 (unless (memq 'hs-headline mode-line-format)
335 (setq mode-line-format
336 (append '(\"-\" hs-headline) mode-line-format)))
337
338 Note that `mode-line-format' is buffer-local.")
339
340 ;;---------------------------------------------------------------------------
341 ;; system dependency
342
343 ; ;; xemacs compatibility
344 ; (when (string-match "xemacs\\|lucid" emacs-version)
345 ; ;; use pre-packaged compatiblity layer
346 ; (require 'overlay))
347 ;
348 ; ;; xemacs and emacs-19 compatibility
349 ; (when (or (not (fboundp 'add-to-invisibility-spec))
350 ; (not (fboundp 'remove-from-invisibility-spec)))
351 ; ;; `buffer-invisibility-spec' mutators snarfed from Emacs 20.3 lisp/subr.el
352 ; (defun add-to-invisibility-spec (arg)
353 ; (cond
354 ; ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
355 ; (setq buffer-invisibility-spec (list arg)))
356 ; (t
357 ; (setq buffer-invisibility-spec
358 ; (cons arg buffer-invisibility-spec)))))
359 ; (defun remove-from-invisibility-spec (arg)
360 ; (if buffer-invisibility-spec
361 ; (setq buffer-invisibility-spec
362 ; (delete arg buffer-invisibility-spec)))))
363
364 ;; hs-match-data
365 (defalias 'hs-match-data 'match-data)
366
367 ;;---------------------------------------------------------------------------
368 ;; support functions
369
370 (defun hs-discard-overlays (from to)
371 "Delete hideshow overlays in region defined by FROM and TO."
372 (when (< to from)
373 (setq from (prog1 to (setq to from))))
374 (let ((ovs (overlays-in from to)))
375 (while ovs
376 (let ((ov (car ovs)))
377 (when (overlay-get ov 'hs)
378 (delete-overlay ov)))
379 (setq ovs (cdr ovs)))))
380
381 (defun hs-isearch-show (ov)
382 "Delete overlay OV, and set `hs-headline' to nil.
383
384 This function is meant to be used as the `isearch-open-invisible'
385 property of an overlay."
386 (setq hs-headline nil)
387 (delete-overlay ov))
388
389 (defun hs-isearch-show-temporary (ov hide-p)
390 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
391 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
392 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
393 OV is shown.
394
395 This function is meant to be used as the `isearch-open-invisible-temporary'
396 property of an overlay."
397 (setq hs-headline
398 (if hide-p
399 nil
400 (or hs-headline
401 (let ((start (overlay-start ov)))
402 (buffer-substring
403 (save-excursion (goto-char start)
404 (beginning-of-line)
405 (skip-chars-forward " \t")
406 (point))
407 start)))))
408 (force-mode-line-update)
409 (overlay-put ov 'invisible (and hide-p 'hs)))
410
411 (defun hs-flag-region (from to flag)
412 "Hide or show lines from FROM to TO, according to FLAG.
413 If FLAG is nil then text is shown, while if FLAG is non-nil the text is
414 hidden. Actually flag is really either `comment' or `block' depending
415 on what kind of block it is supposed to hide."
416 (save-excursion
417 ;; first clear it all out
418 (hs-discard-overlays from to)
419 ;; now create overlays if needed
420 (when flag
421 (let ((overlay (make-overlay from to)))
422 (overlay-put overlay 'invisible 'hs)
423 (overlay-put overlay 'hs flag)
424 (when (or (eq hs-isearch-open t) (eq hs-isearch-open flag))
425 (overlay-put overlay 'isearch-open-invisible 'hs-isearch-show)
426 (overlay-put overlay
427 'isearch-open-invisible-temporary
428 'hs-isearch-show-temporary))
429 overlay))))
430
431 (defun hs-forward-sexp (match-data arg)
432 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
433 Original match data is restored upon return."
434 (save-match-data
435 (set-match-data match-data)
436 (goto-char (match-beginning hs-block-start-mdata-select))
437 (funcall hs-forward-sexp-func arg)))
438
439 (defun hs-hide-comment-region (beg end &optional repos-end)
440 "Hide a region from BEG to END, marking it as a comment.
441 Optional arg REPOS-END means reposition at end."
442 (hs-flag-region (progn (goto-char beg) (end-of-line) (point))
443 (progn (goto-char end) (end-of-line) (point))
444 'comment)
445 (goto-char (if repos-end end beg)))
446
447 (defun hs-hide-block-at-point (&optional end comment-reg)
448 "Hide block iff on block beginning.
449 Optional arg END means reposition at end.
450 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
451 specifies the limits of the comment, or nil if the block is not
452 a comment.
453
454 The block beginning is adjusted by `hs-adjust-block-beginning'
455 and then further adjusted to be at the end of the line."
456 (if comment-reg
457 (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
458 (if (looking-at hs-block-start-regexp)
459 (let* ((mdata (hs-match-data t))
460 (pure-p (match-end 0))
461 (p
462 ;; `p' is the point at the end of the block beginning,
463 ;; which may need to be adjusted
464 (save-excursion
465 (goto-char (funcall (or hs-adjust-block-beginning
466 'identity)
467 pure-p))
468 ;; whatever the adjustment, we move to eol
469 (end-of-line)
470 (point)))
471 (q
472 ;; `q' is the point at the end of the block
473 (progn (hs-forward-sexp mdata 1)
474 (end-of-line)
475 (point))))
476 (if (and (< p (point)) (> (count-lines p q) 1))
477 (overlay-put (hs-flag-region p q 'block)
478 'hs-ofs
479 (- pure-p p)))
480 (goto-char (if end q (min p pure-p)))))))
481
482 (defun hs-safety-is-job-n ()
483 "Warn if `buffer-invisibility-spec' does not contain symbol `hs'."
484 (unless (and (listp buffer-invisibility-spec)
485 (assq 'hs buffer-invisibility-spec))
486 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
487 (sit-for 2)))
488
489 (defun hs-inside-comment-p ()
490 "Return non-nil if point is inside a comment, otherwise nil.
491 Actually, return a list containing the buffer position of the start
492 and the end of the comment. A comment block can be hidden only if on
493 its starting line there is only whitespace preceding the actual comment
494 beginning. If we are inside of a comment but this condition is not met,
495 we return a list having a nil as its car and the end of comment position
496 as cdr."
497 (save-excursion
498 ;; the idea is to look backwards for a comment start regexp, do a
499 ;; forward comment, and see if we are inside, then extend extend
500 ;; forward and backward as long as we have comments
501 (let ((q (point)))
502 (when (or (looking-at hs-c-start-regexp)
503 (re-search-backward hs-c-start-regexp (point-min) t))
504 (forward-comment (- (buffer-size)))
505 (skip-chars-forward " \t\n\f")
506 (let ((p (point))
507 (not-hidable nil))
508 (beginning-of-line)
509 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
510 ;; we are in this situation: (example)
511 ;; (defun bar ()
512 ;; (foo)
513 ;; ) ; comment
514 ;; ^
515 ;; the point was here before doing (beginning-of-line)
516 ;; here we should advance till the next comment which
517 ;; eventually has only white spaces preceding it on the same
518 ;; line
519 (goto-char p)
520 (forward-comment 1)
521 (skip-chars-forward " \t\n\f")
522 (setq p (point))
523 (while (and (< (point) q)
524 (> (point) p)
525 (not (looking-at hs-c-start-regexp)))
526 (setq p (point));; use this to avoid an infinite cycle
527 (forward-comment 1)
528 (skip-chars-forward " \t\n\f"))
529 (if (or (not (looking-at hs-c-start-regexp))
530 (> (point) q))
531 ;; we cannot hide this comment block
532 (setq not-hidable t)))
533 ;; goto the end of the comment
534 (forward-comment (buffer-size))
535 (skip-chars-backward " \t\n\f")
536 (end-of-line)
537 (if (>= (point) q)
538 (list (if not-hidable nil p) (point))))))))
539
540 (defun hs-grok-mode-type ()
541 "Set up hideshow variables for new buffers.
542 If `hs-special-modes-alist' has information associated with the
543 current buffer's major mode, use that.
544 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
545 function; and adjust-block-beginning function."
546 (if (and (boundp 'comment-start)
547 (boundp 'comment-end)
548 comment-start comment-end)
549 (let* ((lookup (assoc major-mode hs-special-modes-alist))
550 (start-elem (or (nth 1 lookup) "\\s(")))
551 (if (listp start-elem)
552 ;; handle (START-REGEXP MDATA-SELECT)
553 (setq hs-block-start-regexp (car start-elem)
554 hs-block-start-mdata-select (cadr start-elem))
555 ;; backwards compatibility: handle simple START-REGEXP
556 (setq hs-block-start-regexp start-elem
557 hs-block-start-mdata-select 0))
558 (setq hs-block-end-regexp (or (nth 2 lookup) "\\s)")
559 hs-c-start-regexp (or (nth 3 lookup)
560 (let ((c-start-regexp
561 (regexp-quote comment-start)))
562 (if (string-match " +$" c-start-regexp)
563 (substring c-start-regexp
564 0 (1- (match-end 0)))
565 c-start-regexp)))
566 hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
567 hs-adjust-block-beginning (nth 5 lookup)))
568 (progn
569 (setq hs-minor-mode nil)
570 (error "%s Mode doesn't support Hideshow Minor Mode" mode-name))))
571
572 (defun hs-find-block-beginning ()
573 "Reposition point at block-start.
574 Return point, or nil if original point was not in a block."
575 (let ((done nil)
576 (here (point)))
577 ;; look if current line is block start
578 (if (looking-at hs-block-start-regexp)
579 (point)
580 ;; look backward for the start of a block that contains the cursor
581 (while (and (re-search-backward hs-block-start-regexp nil t)
582 (not (setq done
583 (< here (save-excursion
584 (hs-forward-sexp (hs-match-data t) 1)
585 (point)))))))
586 (if done
587 (point)
588 (goto-char here)
589 nil))))
590
591 (defun hs-hide-level-recursive (arg minp maxp)
592 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
593 (when (hs-find-block-beginning)
594 (setq minp (1+ (point)))
595 (funcall hs-forward-sexp-func 1)
596 (setq maxp (1- (point))))
597 (hs-flag-region minp maxp nil) ; eliminate weirdness
598 (goto-char minp)
599 (while (progn
600 (forward-comment (buffer-size))
601 (and (< (point) maxp)
602 (re-search-forward hs-block-start-regexp maxp t)))
603 (if (> arg 1)
604 (hs-hide-level-recursive (1- arg) minp maxp)
605 (goto-char (match-beginning hs-block-start-mdata-select))
606 (hs-hide-block-at-point t)))
607 (hs-safety-is-job-n)
608 (goto-char maxp))
609
610 (defmacro hs-life-goes-on (&rest body)
611 "Evaluate BODY forms iff variable `hs-minor-mode' is non-nil.
612 In the dynamic context of this macro, `inhibit-point-motion-hooks'
613 and `case-fold-search' are both t."
614 `(when hs-minor-mode
615 (let ((inhibit-point-motion-hooks t)
616 (case-fold-search t))
617 ,@body)))
618
619 (put 'hs-life-goes-on 'edebug-form-spec '(&rest form))
620
621 (defun hs-already-hidden-p ()
622 "Return non-nil if point is in an already-hidden block, otherwise nil."
623 (save-excursion
624 (let ((c-reg (hs-inside-comment-p)))
625 (if (and c-reg (nth 0 c-reg))
626 ;; point is inside a comment, and that comment is hidable
627 (goto-char (nth 0 c-reg))
628 (if (and (not c-reg)
629 (hs-find-block-beginning)
630 (looking-at hs-block-start-regexp))
631 ;; point is inside a block
632 (goto-char (match-end 0)))))
633 (end-of-line)
634 (let ((overlays (overlays-at (point)))
635 (found nil))
636 (while (and (not found) (overlayp (car overlays)))
637 (setq found (overlay-get (car overlays) 'hs)
638 overlays (cdr overlays)))
639 found)))
640
641 (defun hs-c-like-adjust-block-beginning (initial)
642 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
643 Actually, point is never moved; a new position is returned that is
644 the end of the C-function header. This adjustment function is meant
645 to be assigned to `hs-adjust-block-beginning' for C-like modes."
646 (save-excursion
647 (goto-char (1- initial))
648 (forward-comment (- (buffer-size)))
649 (point)))
650
651 ;;---------------------------------------------------------------------------
652 ;; commands
653
654 (defun hs-hide-all ()
655 "Hide all top level blocks, displaying only first and last lines.
656 Move point to the beginning of the line, and run the normal hook
657 `hs-hide-hook'. See documentation for `run-hooks'.
658 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
659 (interactive)
660 (hs-life-goes-on
661 (message "Hiding all blocks ...")
662 (save-excursion
663 (hs-flag-region (point-min) (point-max) nil) ; eliminate weirdness
664 (goto-char (point-min))
665 (let ((count 0)
666 (re (concat "\\("
667 hs-block-start-regexp
668 "\\)"
669 (if hs-hide-comments-when-hiding-all
670 (concat "\\|\\("
671 hs-c-start-regexp
672 "\\)")
673 ""))))
674 (while (progn
675 (unless hs-hide-comments-when-hiding-all
676 (forward-comment (point-max)))
677 (re-search-forward re (point-max) t))
678 (if (match-beginning 1)
679 ;; we have found a block beginning
680 (progn
681 (goto-char (match-beginning 1))
682 (if hs-hide-all-non-comment-function
683 (funcall hs-hide-all-non-comment-function)
684 (hs-hide-block-at-point t)))
685 ;; found a comment, probably
686 (let ((c-reg (hs-inside-comment-p))) ; blech!
687 (when (and c-reg (car c-reg))
688 (if (> (count-lines (car c-reg) (nth 1 c-reg)) 1)
689 (hs-hide-block-at-point t c-reg)
690 (goto-char (nth 1 c-reg))))))
691 (message "Hiding ... %d" (setq count (1+ count)))))
692 (hs-safety-is-job-n))
693 (beginning-of-line)
694 (message "Hiding all blocks ... done")
695 (run-hooks 'hs-hide-hook)))
696
697 (defun hs-show-all ()
698 "Show everything then run `hs-show-hook'. See `run-hooks'."
699 (interactive)
700 (hs-life-goes-on
701 (message "Showing all blocks ...")
702 (hs-flag-region (point-min) (point-max) nil)
703 (message "Showing all blocks ... done")
704 (run-hooks 'hs-show-hook)))
705
706 (defun hs-hide-block (&optional end)
707 "Select a block and hide it. With prefix arg, reposition at END.
708 Upon completion, point is repositioned and the normal hook
709 `hs-hide-hook' is run. See documentation for `run-hooks'."
710 (interactive "P")
711 (hs-life-goes-on
712 (let ((c-reg (hs-inside-comment-p)))
713 (cond
714 ((and c-reg (or (null (nth 0 c-reg))
715 (<= (count-lines (car c-reg) (nth 1 c-reg)) 1)))
716 (message "(not enough comment lines to hide)"))
717 ((or c-reg
718 (looking-at hs-block-start-regexp)
719 (hs-find-block-beginning))
720 (hs-hide-block-at-point end c-reg)
721 (hs-safety-is-job-n)
722 (run-hooks 'hs-hide-hook))))))
723
724 (defun hs-show-block (&optional end)
725 "Select a block and show it.
726 With prefix arg, reposition at END. Upon completion, point is
727 repositioned and the normal hook `hs-show-hook' is run.
728 See documentation for functions `hs-hide-block' and `run-hooks'."
729 (interactive "P")
730 (hs-life-goes-on
731 (or
732 ;; first see if we have something at the end of the line
733 (catch 'eol-begins-hidden-region-p
734 (let ((here (point))
735 (ovs (save-excursion (end-of-line) (overlays-at (point)))))
736 (while ovs
737 (let ((ov (car ovs)))
738 (when (overlay-get ov 'hs)
739 (goto-char
740 (cond (end (overlay-end ov))
741 ((eq 'comment (overlay-get ov 'hs)) here)
742 (t (+ (overlay-start ov) (overlay-get ov 'hs-ofs)))))
743 (delete-overlay ov)
744 (throw 'eol-begins-hidden-region-p t)))
745 (setq ovs (cdr ovs)))
746 nil))
747 ;; not immediately obvious, look for a suitable block
748 (let ((c-reg (hs-inside-comment-p))
749 p q)
750 (cond (c-reg
751 (when (car c-reg)
752 (setq p (car c-reg)
753 q (cadr c-reg))))
754 ((and (hs-find-block-beginning)
755 (looking-at hs-block-start-regexp)) ; fresh match-data, ugh
756 (setq p (point)
757 q (progn (hs-forward-sexp (hs-match-data t) 1) (point)))))
758 (when (and p q)
759 (hs-flag-region p q nil)
760 (goto-char (if end q (1+ p)))))
761 (hs-safety-is-job-n)
762 (run-hooks 'hs-show-hook))))
763
764 (defun hs-hide-level (arg)
765 "Hide all blocks ARG levels below this block.
766 The hook `hs-hide-hook' is run; see `run-hooks'."
767 (interactive "p")
768 (hs-life-goes-on
769 (save-excursion
770 (message "Hiding blocks ...")
771 (hs-hide-level-recursive arg (point-min) (point-max))
772 (message "Hiding blocks ... done"))
773 (hs-safety-is-job-n)
774 (run-hooks 'hs-hide-hook)))
775
776 (defun hs-toggle-hiding ()
777 "Toggle hiding/showing of a block.
778 See `hs-hide-block' and `hs-show-block'."
779 (interactive)
780 (hs-life-goes-on
781 (if (hs-already-hidden-p)
782 (hs-show-block)
783 (hs-hide-block))))
784
785 (defun hs-mouse-toggle-hiding (e)
786 "Toggle hiding/showing of a block.
787 This command should be bound to a mouse key.
788 Argument E is a mouse event used by `mouse-set-point'.
789 See `hs-hide-block' and `hs-show-block'."
790 (interactive "@e")
791 (hs-life-goes-on
792 (mouse-set-point e)
793 (hs-toggle-hiding)))
794
795 (defun hs-hide-initial-comment-block ()
796 "Hide the first block of comments in a file.
797 This can be useful if you have huge RCS logs in those comments."
798 (interactive)
799 (hs-life-goes-on
800 (let ((c-reg (save-excursion
801 (goto-char (point-min))
802 (skip-chars-forward " \t\n\f")
803 (hs-inside-comment-p))))
804 (when c-reg
805 (let ((beg (car c-reg)) (end (cadr c-reg)))
806 ;; see if we have enough comment lines to hide
807 (when (> (count-lines beg end) 1)
808 (hs-hide-comment-region beg end)))))))
809
810 ;;;###autoload
811 (defun hs-minor-mode (&optional arg)
812 "Toggle hideshow minor mode.
813 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
814 When hideshow minor mode is on, the menu bar is augmented with hideshow
815 commands and the hideshow commands are enabled.
816 The value '(hs . t) is added to `buffer-invisibility-spec'.
817
818 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
819 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
820 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
821
822 Turning hideshow minor mode off reverts the menu bar and the
823 variables to default values and disables the hideshow commands.
824
825 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
826
827 Key bindings:
828 \\{hs-minor-mode-map}"
829
830 (interactive "P")
831 (setq hs-headline nil
832 hs-minor-mode (if (null arg)
833 (not hs-minor-mode)
834 (> (prefix-numeric-value arg) 0)))
835 (if hs-minor-mode
836 (progn
837 (hs-grok-mode-type)
838 (easy-menu-add hs-minor-mode-menu)
839 (make-variable-buffer-local 'line-move-ignore-invisible)
840 (setq line-move-ignore-invisible t)
841 (add-to-invisibility-spec '(hs . t)))
842 (easy-menu-remove hs-minor-mode-menu)
843 (remove-from-invisibility-spec '(hs . t)))
844 (run-hooks 'hs-minor-mode-hook))
845
846 ;;---------------------------------------------------------------------------
847 ;; load-time actions
848
849 ;; keymaps and menus
850 (if hs-minor-mode-map
851 nil
852 (setq hs-minor-mode-map (make-sparse-keymap))
853 (easy-menu-define hs-minor-mode-menu
854 hs-minor-mode-map
855 "Menu used when hideshow minor mode is active."
856 (cons "Hide/Show"
857 (mapcar
858 ;; Interpret each table entry as follows: first, populate keymap
859 ;; with elements 2 and 1; then, for easymenu, use entry directly
860 ;; unless element 0 is nil, in which case the entry is "omitted".
861 (lambda (ent)
862 (define-key hs-minor-mode-map (aref ent 2) (aref ent 1))
863 (if (aref ent 0) ent "-----"))
864 ;; These bindings roughly imitate those used by Outline mode.
865 ;; menu entry command key
866 '(["Hide Block" hs-hide-block "\C-c@\C-h"]
867 ["Show Block" hs-show-block "\C-c@\C-s"]
868 ["Hide All" hs-hide-all "\C-c@\C-\M-h"]
869 ["Show All" hs-show-all "\C-c@\C-\M-s"]
870 ["Hide Level" hs-hide-level "\C-c@\C-l"]
871 ["Toggle Hiding" hs-toggle-hiding "\C-c@\C-c"]
872 [nil hs-mouse-toggle-hiding [(shift button2)]]
873 )))))
874
875 ;; some housekeeping
876 (or (assq 'hs-minor-mode minor-mode-map-alist)
877 (setq minor-mode-map-alist
878 (cons (cons 'hs-minor-mode hs-minor-mode-map)
879 minor-mode-map-alist)))
880 (or (assq 'hs-minor-mode minor-mode-alist)
881 (setq minor-mode-alist (append minor-mode-alist
882 (list '(hs-minor-mode " hs")))))
883
884 ;; make some variables permanently buffer-local
885 (let ((vars '(hs-minor-mode
886 hs-c-start-regexp
887 hs-block-start-regexp
888 hs-block-start-mdata-select
889 hs-block-end-regexp
890 hs-forward-sexp-func
891 hs-adjust-block-beginning)))
892 (while vars
893 (let ((var (car vars)))
894 (make-variable-buffer-local var)
895 (put var 'permanent-local t))
896 (setq vars (cdr vars))))
897
898 ;;---------------------------------------------------------------------------
899 ;; that's it
900
901 (provide 'hideshow)
902
903 ;;; hideshow.el ends here