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