(hs-minor-mode): Use `make-local-variable' not `make-variable-buffer-local'.
[emacs.git] / lisp / progmodes / hideshow.el
bloba94672ac7bc36fd423516a3d34f707eacaab1f9f
1 ;;; hideshow.el --- minor mode cmds to selectively display blocks of code
3 ;; Copyright (C) 1994, 95, 96, 97, 98 Free Software Foundation
5 ;; Author: Thien-Thi Nguyen <ttn@netcom.com>
6 ;; Dan Nicolaescu <done@ece.arizona.edu>
7 ;; Keywords: C C++ java lisp tools editing comments blocks hiding outlines
8 ;; Maintainer-Version: 4.20
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 `hs-minor-mode'. When active, seven commands:
34 ;; hs-{hide,show}-{all,block}, hs-show-region,
35 ;; hs-hide-level and hs-minor-mode
37 ;; are available, implementing block hiding and showing. Blocks are
38 ;; defined per mode. In c-mode or c++-mode, they are simply curly braces,
39 ;; while in Lisp-ish modes they are parens. Multi-line comments can also
40 ;; be hidden. The command `M-x hs-minor-mode' toggles the minor mode or
41 ;; sets it (similar to outline minor mode).
43 ;; - Customization
45 ;; Variables control things thusly:
47 ;; hs-hide-comments-when-hiding-all -- self-explanatory!
48 ;; hs-show-hidden-short-form -- whether or not the last line in a form
49 ;; is omitted (saving screen space)
50 ;; hs-isearch-open -- what kind of hidden blocks to open when
51 ;; doing isearch
52 ;; hs-special-modes-alist -- keeps at bay hideshow's heuristics with
53 ;; respect to block definitions
55 ;; Hooks are run after some commands:
57 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
58 ;; hs-show-hook hs-show-block, hs-show-all, hs-show-region
60 ;; See docs for each variable or hook for more info.
62 ;; - Suggested usage
64 ;; (load-library "hideshow")
65 ;; (add-hook 'X-mode-hook 'hs-minor-mode) ; other modes similarly
67 ;; where X = {emacs-lisp,c,c++,perl,...}. See the doc for the variable
68 ;; `hs-special-modes-alist' if you'd like to use hideshow w/ other modes.
70 ;; - Bugs / caveats
72 ;; 1. Hideshow does not work w/ emacs 18 because emacs 18 lacks the
73 ;; function `forward-comment' (among other things). If someone writes
74 ;; this, please send me a copy.
76 ;; 2. Users of cc-mode.el should not hook hideshow into
77 ;; c-mode-common-hook since at that stage of the call sequence, the
78 ;; variables `comment-start' and `comment-end' are not yet provided.
79 ;; Instead, use c-mode-hook and c++-mode-hook as suggested above.
81 ;; - Thanks and feedback
83 ;; Thanks go to the following people for valuable ideas, code and bug
84 ;; reports.
85 ;; adahome@ix.netcom.com Dean Andrews
86 ;; alfh@ifi.uio.no Alf-Ivar Holm
87 ;; gael@gnlab030.grenoble.hp.com Gael Marziou
88 ;; jan.djarv@sa.erisoft.se Jan Djarv
89 ;; preston.f.crow@dartmouth.edu Preston F. Crow
90 ;; qhslali@aom.ericsson.se Lars Lindberg
91 ;; sheff@edcsgw2.cr.usgs.gov Keith Sheffield
92 ;; ware@cis.ohio-state.edu Pete Ware
94 ;; Special thanks go to Dan Nicolaescu <done@ece.arizona.edu>, who
95 ;; reimplemented hideshow using overlays (rather than selective display),
96 ;; added isearch magic, folded in custom.el compatibility, generalized
97 ;; comment handling, incorporated mouse support, and maintained the code
98 ;; in general. Version 4.0 is largely due to his efforts.
100 ;; Correspondance welcome; please indicate version number.
102 ;;; Code:
104 (require 'easymenu)
106 ;;;----------------------------------------------------------------------------
107 ;;; user-configurable variables
109 (defgroup hideshow nil
110 "Minor mode for hiding and showing program and comment blocks."
111 :prefix "hs-"
112 :group 'languages)
114 ;;;###autoload
115 (defcustom hs-hide-comments-when-hiding-all t
116 "Hide the comments too when you do an `hs-hide-all'."
117 :type 'boolean
118 :group 'hideshow)
120 ;;;###autoload
121 (defcustom hs-show-hidden-short-form t
122 "Leave only the first line visible in a hidden block.
123 If non-nil only the first line is visible when a block is in the
124 hidden state, else both the first line and the last line are shown.
125 A nil value disables `hs-adjust-block-beginning', which see.
127 An example of how this works: (in C mode)
128 original:
130 /* My function main
131 some more stuff about main
134 main(void)
136 int x=0;
137 return 0;
141 hidden and `hs-show-hidden-short-form' is nil
142 /* My function main...
145 main(void)
146 {...
149 hidden and `hs-show-hidden-short-form' is t
150 /* My function main...
152 main(void)...
154 For the last case you have to be on the line containing the
155 ellipsis when you do `hs-show-block'."
156 :type 'boolean
157 :group 'hideshow)
159 (defcustom hs-minor-mode-hook 'hs-hide-initial-comment-block
160 "Hook called when `hs-minor-mode' is installed.
161 A good value for this would be `hs-hide-initial-comment-block' to
162 hide all the comments at the beginning of the file."
163 :type 'hook
164 :group 'hideshow)
166 (defcustom hs-isearch-open 'block
167 "What kind of hidden blocks to open when doing `isearch'.
168 One of the following values:
170 block -- open only blocks
171 comment -- open only comments
172 t -- open both blocks and comments
173 nil -- open neither blocks nor comments
175 This has effect iff `search-invisible' is set to `open'."
176 :type '(choice (const :tag "open only blocks" block)
177 (const :tag "open only comments" comment)
178 (const :tag "open both blocks and comments" t)
179 (const :tag "don't open any of them" nil))
180 :group 'hideshow)
182 ;;;###autoload
183 (defvar hs-special-modes-alist
184 '((c-mode "{" "}" nil nil hs-c-like-adjust-block-beginning)
185 (c++-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
186 (java-mode "\\(\\(\\([ \t]*\\(\\(abstract\\|final\\|native\\|p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|s\\(tatic\\|ynchronized\\)\\)[ \t\n]+\\)*[.a-zA-Z0-9_:]+[ \t\n]*\\(\\[[ \t\n]*\\][ \t\n]*\\)?\\([a-zA-Z0-9_:]+[ \t\n]*\\)([^)]*)\\([ \n\t]+throws[ \t\n][^{]+\\)?\\)\\|\\([ \t]*static[^{]*\\)\\)[ \t\n]*{\\)" "}" "/[*/]" java-hs-forward-sexp hs-c-like-adjust-block-beginning))
187 ; I tested the java regexp using the following:
188 ;(defvar hsj-public)
189 ;(defvar hsj-type)
190 ;(defvar hsj-fname)
191 ;(defvar hsj-par)
192 ;(defvar hsj-throws)
193 ;(defvar hsj-static)
195 ;(setq hsj-public
196 ; (concat "[ \t]*\\("
197 ; (regexp-opt '("public" "private" "protected" "abstract"
198 ; "synchronized" "static" "final" "native") 1)
199 ; "[ \t\n]+\\)*"))
201 ;(setq hsj-type "[.a-zA-Z0-9_:]+[ \t\n]*\\(\\[[ \t\n]*\\][ \t\n]*\\)?")
202 ;(setq hsj-fname "\\([a-zA-Z0-9_:]+[ \t\n]*\\)")
203 ;(setq hsj-par "([^)]*)")
204 ;(setq hsj-throws "\\([ \n\t]+throws[ \t\n][^{]+\\)?")
206 ;(setq hsj-static "[ \t]*static[^{]*")
209 ;(setq hs-block-start-regexp (concat
210 ; "\\("
211 ; "\\("
212 ; "\\("
213 ; hsj-public
214 ; hsj-type
215 ; hsj-fname
216 ; hsj-par
217 ; hsj-throws
218 ; "\\)"
219 ; "\\|"
220 ; "\\("
221 ; hsj-static
222 ; "\\)"
223 ; "\\)"
224 ; "[ \t\n]*{"
225 ; "\\)"
226 ; ))
228 "*Alist for initializing the hideshow variables for different modes.
229 It has the form
230 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
231 If present, hideshow will use these values as regexps for start, end
232 and comment-start, respectively. Since Algol-ish languages do not have
233 single-character block delimiters, the function `forward-sexp' used
234 by hideshow doesn't work. In this case, if a similar function is
235 available, you can register it and have hideshow use it instead of
236 `forward-sexp'. See the documentation for `hs-adjust-block-beginning'
237 to see what is the use of ADJUST-BEG-FUNC.
239 If any of those is left nil, hideshow will try to guess some values
240 using function `hs-grok-mode-type'.
242 Note that the regexps should not contain leading or trailing whitespace.")
244 (defvar hs-hide-hook nil
245 "*Hooks called at the end of commands to hide text.
246 These commands include `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
248 (defvar hs-show-hook nil
249 "*Hooks called at the end of commands to show text.
250 These commands include `hs-show-all', `hs-show-block' and `hs-show-region'.")
252 (defvar hs-minor-mode-prefix "\C-c"
253 "*Prefix key to use for hideshow commands in hideshow minor mode.")
255 ;;;----------------------------------------------------------------------------
256 ;;; internal variables
258 (defvar hs-minor-mode nil
259 "Non-nil if using hideshow mode as a minor mode of some other mode.
260 Use the command `hs-minor-mode' to toggle this variable.")
262 (defvar hs-minor-mode-map nil
263 "Mode map for hideshow minor mode.")
265 ;(defvar hs-menu-bar nil
266 ; "Menu bar for hideshow minor mode (Xemacs only).")
268 (defvar hs-c-start-regexp nil
269 "Regexp for beginning of comments.
270 Differs from mode-specific comment regexps in that
271 surrounding whitespace is stripped.")
273 (defvar hs-block-start-regexp nil
274 "Regexp for beginning of block.")
276 (defvar hs-block-end-regexp nil
277 "Regexp for end of block.")
279 (defvar hs-forward-sexp-func 'forward-sexp
280 "Function used to do a `forward-sexp'.
281 Should change for Algol-ish modes. For single-character block
282 delimiters -- ie, the syntax table regexp for the character is
283 either `(' or `)' -- `hs-forward-sexp-func' would just be
284 `forward-sexp'. For other modes such as simula, a more specialized
285 function is necessary.")
287 (defvar hs-adjust-block-beginning nil
288 "Function used to tweak the block beginning.
289 It has effect only if `hs-show-hidden-short-form' is non-nil.
290 The block it is hidden from the point returned by this function,
291 as opposed to hiding it from the point returned when searching
292 `hs-block-start-regexp'. In c-like modes, if we wish to also hide the
293 curly braces (if you think they occupy too much space on the screen),
294 this function should return the starting point (at the end of line) of
295 the hidden region.
297 It is called with a single argument ARG which is the the position in
298 buffer after the block beginning.
300 It should return the position from where we should start hiding.
302 It should not move the point.
304 See `hs-c-like-adjust-block-beginning' for an example of using this.")
306 ;(defvar hs-emacs-type 'fsf
307 ; "Used to support both Emacs and Xemacs.")
309 ;(eval-when-compile
310 ; (if (string-match "xemacs\\|lucid" emacs-version)
311 ; (progn
312 ; (defvar current-menubar nil "")
313 ; (defun set-buffer-menubar (arg1))
314 ; (defun add-menu (arg1 arg2 arg3)))))
316 ;;;----------------------------------------------------------------------------
317 ;;; support funcs
319 ;; snarfed from outline.el;
320 (defun hs-flag-region (from to flag)
321 "Hide or show lines from FROM to TO, according to FLAG.
322 If FLAG is nil then text is shown, while if FLAG is non-nil the text
323 is hidden. Actually flag is really either `comment' or `block'
324 depending on what kind of block it is suppose to hide."
325 (save-excursion
326 (goto-char from)
327 (end-of-line)
328 (hs-discard-overlays (point) to 'invisible 'hs)
329 (if flag
330 (let ((overlay (make-overlay (point) to)))
331 ;; Make overlay hidden and intangible.
332 (overlay-put overlay 'invisible 'hs)
333 (overlay-put overlay 'hs t)
334 (when (or (eq hs-isearch-open t) (eq hs-isearch-open flag))
335 (overlay-put overlay 'isearch-open-invisible
336 'hs-isearch-open-invisible))
337 (overlay-put overlay 'intangible t)))))
339 ;; This is set as an `isearch-open-invisible' property to hidden
340 ;; overlays.
341 (defun hs-isearch-open-invisible (ov)
342 (save-excursion
343 (goto-char (overlay-start ov))
344 (hs-show-block)))
346 ;; Remove from the region BEG ... END all overlays
347 ;; with a PROP property equal to VALUE.
348 ;; Overlays with a PROP property different from VALUE are not touched.
349 (defun hs-discard-overlays (beg end prop value)
350 (if (< end beg)
351 (setq beg (prog1 end (setq end beg))))
352 (save-excursion
353 (goto-char beg)
354 (let ((overlays (overlays-in beg end))
356 (while overlays
357 (setq o (car overlays))
358 (if (eq (overlay-get o prop) value)
359 (delete-overlay o))
360 (setq overlays (cdr overlays))))))
362 (defun hs-hide-block-at-point (&optional end comment-reg)
363 "Hide block iff on block beginning.
364 Optional arg END means reposition at end.
365 Optional arg COMMENT-REG is a list of the form (BEGIN . END) and
366 specifies the limits of the comment, or nil if the block is not
367 a comment."
368 (if comment-reg
369 (progn
370 ;; goto the end of line at the end of the comment
371 (goto-char (nth 1 comment-reg))
372 (unless hs-show-hidden-short-form (forward-line -1))
373 (end-of-line)
374 (hs-flag-region (car comment-reg) (point) 'comment)
375 (goto-char (if end (nth 1 comment-reg) (car comment-reg))))
376 (if (looking-at hs-block-start-regexp)
377 (let* ((p ;; p is the point at the end of the block beginning
378 (if (and hs-show-hidden-short-form
379 hs-adjust-block-beginning)
380 ;; we need to adjust the block beginning
381 (funcall hs-adjust-block-beginning (match-end 0))
382 (match-end 0)))
383 ;; q is the point at the end of the block
384 (q (progn (funcall hs-forward-sexp-func 1) (point))))
385 ;; position the point so we can call `hs-flag-region'
386 (unless hs-show-hidden-short-form (forward-line -1))
387 (end-of-line)
388 (if (and (< p (point)) (> (count-lines p q)
389 (if hs-show-hidden-short-form 1 2)))
390 (hs-flag-region p (point) 'block))
391 (goto-char (if end q p))))))
393 (defun hs-show-block-at-point (&optional end comment-reg)
394 "Show block iff on block beginning.
395 Optional arg END means reposition at end.
396 Optional arg COMMENT-REG is a list of the forme (BEGIN . END) and
397 specifies the limits of the comment. It should be nil when hiding
398 a block."
399 (if comment-reg
400 (when (car comment-reg)
401 (hs-flag-region (car comment-reg) (nth 1 comment-reg) nil)
402 (goto-char (if end (nth 1 comment-reg) (car comment-reg))))
403 (if (looking-at hs-block-start-regexp)
404 (let* ((p (point))
406 (condition-case error ; probably unbalanced paren
407 (progn
408 (funcall hs-forward-sexp-func 1)
409 (point))
410 (error
411 ;; try to get out of rat's nest and expose the whole func
412 (if (/= (current-column) 0) (beginning-of-defun))
413 (setq p (point))
414 (re-search-forward (concat "^" hs-block-start-regexp)
415 (point-max) t 2)
416 (point)))))
417 (hs-flag-region p q nil)
418 (goto-char (if end (1+ (point)) p))))))
420 (defun hs-safety-is-job-n ()
421 "Warn if `buffer-invisibility-spec' does not contain hs."
422 (if (or buffer-invisibility-spec (assq 'hs buffer-invisibility-spec) )
424 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
425 (sit-for 2)))
427 (defun hs-hide-initial-comment-block ()
428 (interactive)
429 "Hide the first block of comments in a file.
430 This is useful when a part of `hs-minor-mode-hook', especially with
431 huge header-comment RCS logs."
432 (let ((p (point))
433 c-reg)
434 (goto-char (point-min))
435 (skip-chars-forward " \t\n^L")
436 (setq c-reg (hs-inside-comment-p))
437 ;; see if we have enough comment lines to hide
438 (if (and c-reg (> (count-lines (car c-reg) (nth 1 c-reg))
439 (if hs-show-hidden-short-form 1 2)))
440 (hs-hide-block)
441 (goto-char p))))
443 (defun hs-inside-comment-p ()
444 "Return non-nil if point is inside a comment, otherwise nil.
445 Actually, returns a list containing the buffer position of the start
446 and the end of the comment. A comment block can be hidden only if on
447 its starting line there is only whitespace preceding the actual comment
448 beginning. If we are inside of a comment but this condition is not met,
449 we return a list having a nil as its car and the end of comment position
450 as cdr."
451 (save-excursion
452 ;; the idea is to look backwards for a comment start regexp, do a
453 ;; forward comment, and see if we are inside, then extend extend
454 ;; forward and backward as long as we have comments
455 (let ((q (point)))
456 (when (or (looking-at hs-c-start-regexp)
457 (re-search-backward hs-c-start-regexp (point-min) t))
458 (forward-comment (- (buffer-size)))
459 (skip-chars-forward " \t\n\f")
460 (let ((p (point))
461 (not-hidable nil))
462 (beginning-of-line)
463 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
464 ;; we are in this situation: (example)
465 ;; (defun bar ()
466 ;; (foo)
467 ;; ) ; comment
468 ;; ^
469 ;; the point was here before doing (beginning-of-line)
470 ;; here we should advance till the next comment which
471 ;; eventually has only white spaces preceding it on the same
472 ;; line
473 (goto-char p)
474 (forward-comment 1)
475 (skip-chars-forward " \t\n\f")
476 (setq p (point))
477 (while (and (< (point) q)
478 (> (point) p)
479 (not (looking-at hs-c-start-regexp)))
480 (setq p (point)) ;; use this to avoid an infinit cycle.
481 (forward-comment 1)
482 (skip-chars-forward " \t\n\f"))
483 (if (or (not (looking-at hs-c-start-regexp))
484 (> (point) q))
485 ;; we cannot hide this comment block
486 (setq not-hidable t)))
487 ;; goto the end of the comment
488 (forward-comment (buffer-size))
489 (skip-chars-backward " \t\n\f")
490 (end-of-line)
491 (if (>= (point) q)
492 (list (if not-hidable nil p) (point))))))))
494 (defun hs-grok-mode-type ()
495 "Set up hideshow variables for new buffers.
496 If `hs-special-modes-alist' has information associated with the
497 current buffer's major mode, use that.
498 Otherwise, guess start, end and comment-start regexps; forward-sexp
499 function; and adjust-block-beginning function."
500 (when (and (boundp 'comment-start)
501 (boundp 'comment-end))
502 (let ((lookup (assoc major-mode hs-special-modes-alist)))
503 (setq hs-block-start-regexp (or (nth 1 lookup) "\\s\(")
504 hs-block-end-regexp (or (nth 2 lookup) "\\s\)")
505 hs-c-start-regexp (or (nth 3 lookup)
506 (let ((c-start-regexp
507 (regexp-quote comment-start)))
508 (if (string-match " +$" c-start-regexp)
509 (substring c-start-regexp 0 (1- (match-end 0)))
510 c-start-regexp)))
511 hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
512 hs-adjust-block-beginning (nth 5 lookup)))))
514 (defun hs-find-block-beginning ()
515 "Reposition point at block-start.
516 Return point, or nil if top-level."
517 (let (done
518 (try-again t)
519 (here (point))
520 (both-regexps (concat "\\(" hs-block-start-regexp "\\)\\|\\("
521 hs-block-end-regexp "\\)"))
522 (buf-size (buffer-size)))
523 (beginning-of-line)
524 ;; A block beginning can span on multiple lines, if the point
525 ;; is on one of those lines, trying a regexp search from
526 ;; that point would fail to find the block beginning, so we look
527 ;; backwards for the block beginning, or a block end.
528 (while try-again
529 (setq try-again nil)
530 (if (and (re-search-backward both-regexps (point-min) t)
531 (match-beginning 1)) ; found a block beginning
532 (if (save-match-data (hs-inside-comment-p))
533 ;;but it was inside a comment, so we have to look for
534 ;;it again
535 (setq try-again t)
536 ;; that's what we were looking for
537 (setq done (match-beginning 0)))
538 ;; we found a block end, or we reached the beginning of the
539 ;; buffer look to see if we were on a block beginning when we
540 ;; started
541 (if (and
542 (re-search-forward hs-block-start-regexp (point-max) t)
544 (and (>= here (match-beginning 0)) (< here (match-end 0)))
545 (and hs-show-hidden-short-form hs-adjust-block-beginning
546 (save-match-data
547 (= 1 (count-lines
548 (funcall hs-adjust-block-beginning
549 (match-end 0)) here))))))
550 (setq done (match-beginning 0)))))
551 (goto-char here)
552 (while (and (not done)
553 ;; This had problems because the regexp can match something
554 ;; inside of a comment!
555 ;; Since inside a comment we can have incomplete sexps
556 ;; this would have signaled an error.
557 (or (forward-comment (- buf-size)) t); `or' is a hack to
558 ; make it return t
559 (re-search-backward both-regexps (point-min) t))
560 (if (match-beginning 1) ; start of start-regexp
561 (setq done (match-beginning 0))
562 (goto-char (match-end 0)) ; end of end-regexp
563 (funcall hs-forward-sexp-func -1)))
564 (goto-char (or done here))
565 done))
567 (defun hs-hide-level-recursive (arg minp maxp)
568 "Hide blocks ARG levels below this block recursively."
569 (when (hs-find-block-beginning)
570 (setq minp (1+ (point)))
571 (forward-sexp)
572 (setq maxp (1- (point))))
573 (hs-flag-region minp maxp ?\n) ; eliminate weirdness
574 (goto-char minp)
575 (while (progn
576 (forward-comment (buffer-size))
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 0))
581 (hs-hide-block-at-point t)))
582 (hs-safety-is-job-n)
583 (goto-char maxp))
585 (defmacro hs-life-goes-on (&rest body)
586 "Execute optional BODY iff variable `hs-minor-mode' is non-nil."
587 `(let ((inhibit-point-motion-hooks t))
588 (when hs-minor-mode
589 ,@body)))
591 (put 'hs-life-goes-on 'edebug-form-spec '(&rest form))
593 (defun hs-already-hidden-p ()
594 "Return non-nil if point is in an already-hidden block, otherwise nil."
595 (save-excursion
596 (let ((c-reg (hs-inside-comment-p)))
597 (if (and c-reg (nth 0 c-reg))
598 ;; point is inside a comment, and that comment is hidable
599 (goto-char (nth 0 c-reg))
600 (if (and (not c-reg) (hs-find-block-beginning)
601 (looking-at hs-block-start-regexp))
602 ;; point is inside a block
603 (goto-char (match-end 0)))))
604 (end-of-line)
605 (let ((overlays (overlays-at (point)))
606 (found nil))
607 (while (and (not found) (overlayp (car overlays)))
608 (setq found (overlay-get (car overlays) 'hs)
609 overlays (cdr overlays)))
610 found)))
612 (defun java-hs-forward-sexp (arg)
613 "Function used by `hs-minor-mode' for `forward-sexp' in Java mode."
614 (if (< arg 0)
615 (backward-sexp 1)
616 (if (looking-at hs-block-start-regexp)
617 (progn
618 (goto-char (match-end 0))
619 (forward-char -1)
620 (forward-sexp 1))
621 (forward-sexp 1))))
623 (defun hs-c-like-adjust-block-beginning (arg)
624 "Function to be assigned to `hs-adjust-block-beginning' for C-like modes.
625 Arg is a position in buffer just after {. This goes back to the end of
626 the function header. The purpose is to save some space on the screen
627 when displaying hidden blocks."
628 (save-excursion
629 (goto-char arg)
630 (forward-char -1)
631 (forward-comment (- (buffer-size)))
632 (point)))
634 ;;;----------------------------------------------------------------------------
635 ;;; commands
637 ;;;###autoload
638 (defun hs-hide-all ()
639 "Hide all top-level blocks, displaying only first and last lines.
640 Move point to the beginning of the line, and it run the normal hook
641 `hs-hide-hook'. See documentation for `run-hooks'.
642 If `hs-hide-comments-when-hiding-all' is t, also hide the comments."
643 (interactive)
644 (hs-life-goes-on
645 (message "Hiding all blocks ...")
646 (save-excursion
647 (hs-flag-region (point-min) (point-max) nil) ; eliminate weirdness
648 (goto-char (point-min))
649 (if hs-hide-comments-when-hiding-all
650 (let (c-reg
651 (count 0)
652 (block-and-comment-re ;; this should match
653 (concat "\\(^" ;; the block beginning and comment start
654 hs-block-start-regexp
655 "\\)\\|\\(" hs-c-start-regexp "\\)")))
656 (while (re-search-forward block-and-comment-re (point-max) t)
657 (if (match-beginning 1) ;; we have found a block beginning
658 (progn
659 (goto-char (match-beginning 1))
660 (hs-hide-block-at-point t)
661 (message "Hiding ... %d" (setq count (1+ count))))
662 ;;found a comment
663 (setq c-reg (hs-inside-comment-p))
664 (if (and c-reg (car c-reg))
665 (if (> (count-lines (car c-reg) (nth 1 c-reg))
666 (if hs-show-hidden-short-form 1 2))
667 (progn
668 (hs-hide-block-at-point t c-reg)
669 (message "Hiding ... %d" (setq count (1+ count))))
670 (goto-char (nth 1 c-reg)))))))
671 (let ((count 0)
672 (top-level-re (concat "^" hs-block-start-regexp))
673 (buf-size (buffer-size)))
674 (while
675 (progn
676 (forward-comment buf-size)
677 (re-search-forward top-level-re (point-max) t))
678 (goto-char (match-beginning 0))
679 (hs-hide-block-at-point t)
680 (message "Hiding ... %d" (setq count (1+ count))))))
681 (hs-safety-is-job-n))
682 (beginning-of-line)
683 (message "Hiding all blocks ... done")
684 (run-hooks 'hs-hide-hook)))
686 (defun hs-show-all ()
687 "Show all top-level blocks.
688 Point is unchanged; run the normal hook `hs-show-hook'.
689 See documentation for `run-hooks'."
690 (interactive)
691 (hs-life-goes-on
692 (message "Showing all blocks ...")
693 (hs-flag-region (point-min) (point-max) nil)
694 (message "Showing all blocks ... done")
695 (run-hooks 'hs-show-hook)))
697 (defun hs-hide-block (&optional end)
698 "Select a block and hide it.
699 With prefix arg, reposition at end. Block is defined as a sexp for
700 Lispish modes, mode-specific otherwise. Comments are blocks, too.
701 Upon completion, point is repositioned and the normal hook
702 `hs-hide-hook' is run. See documentation for `run-hooks'."
703 (interactive "P")
704 (hs-life-goes-on
705 (let ((c-reg (hs-inside-comment-p)))
706 (cond
707 ((and c-reg (or (null (nth 0 c-reg))
708 (<= (count-lines (car c-reg) (nth 1 c-reg))
709 (if hs-show-hidden-short-form 1 2))))
710 (message "Not enough comment lines to hide!"))
711 ((or c-reg (looking-at hs-block-start-regexp)
712 (hs-find-block-beginning))
713 (hs-hide-block-at-point end c-reg)
714 (hs-safety-is-job-n)
715 (run-hooks 'hs-hide-hook))))))
717 (defun hs-show-block (&optional end)
718 "Select a block and show it.
719 With prefix arg, reposition at end. Upon completion, point is
720 repositioned and the normal hook `hs-show-hook' is run.
721 See documentation for `hs-hide-block' and `run-hooks'."
722 (interactive "P")
723 (hs-life-goes-on
724 (let ((c-reg (hs-inside-comment-p)))
725 (if (or c-reg
726 (looking-at hs-block-start-regexp)
727 (hs-find-block-beginning))
728 (progn
729 (hs-show-block-at-point end c-reg)
730 (hs-safety-is-job-n)
731 (run-hooks 'hs-show-hook))))))
733 (defun hs-show-region (beg end)
734 "Show all lines from BEG to END, without doing any block analysis.
735 Note: `hs-show-region' is intended for use when `hs-show-block' signals
736 \"unbalanced parentheses\" and so is an emergency measure only. You may
737 become very confused if you use this command indiscriminately."
738 (interactive "r")
739 (hs-life-goes-on
740 (hs-flag-region beg end nil)
741 (hs-safety-is-job-n)
742 (run-hooks 'hs-show-hook)))
744 (defun hs-hide-level (arg)
745 "Hide all blocks ARG levels below this block."
746 (interactive "p")
747 (hs-life-goes-on
748 (save-excursion
749 (message "Hiding blocks ...")
750 (hs-hide-level-recursive arg (point-min) (point-max))
751 (message "Hiding blocks ... done"))
752 (hs-safety-is-job-n)
753 (run-hooks 'hs-hide-hook)))
755 ;;;###autoload
756 (defun hs-mouse-toggle-hiding (e)
757 "Toggle hiding/showing of a block.
758 Should be bound to a mouse key."
759 (interactive "@e")
760 (mouse-set-point e)
761 (if (hs-already-hidden-p)
762 (hs-show-block)
763 (hs-hide-block)))
765 ;;;###autoload
766 (defun hs-minor-mode (&optional arg)
767 "Toggle hideshow minor mode.
768 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
769 When hideshow minor mode is on, the menu bar is augmented with hideshow
770 commands and the hideshow commands are enabled.
771 The value '(hs . t) is added to `buffer-invisibility-spec'.
772 Last, the normal hook `hs-minor-mode-hook' is run; see the doc
773 for `run-hooks'.
775 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
776 `hs-show-block', `hs-hide-level' and `hs-show-region'.
777 Also see the documentation for the variable `hs-show-hidden-short-form'.
779 Turning hideshow minor mode off reverts the menu bar and the
780 variables to default values and disables the hideshow commands.
782 Key bindings:
783 \\{hs-minor-mode-map}"
785 (interactive "P")
786 (setq hs-minor-mode
787 (if (null arg)
788 (not hs-minor-mode)
789 (> (prefix-numeric-value arg) 0)))
790 (if hs-minor-mode
791 (progn
792 ; (if (eq hs-emacs-type 'lucid)
793 ; (progn
794 ; (set-buffer-menubar (copy-sequence current-menubar))
795 ; (add-menu nil (car hs-menu-bar) (cdr hs-menu-bar))))
796 (make-local-variable 'line-move-ignore-invisible)
797 (setq line-move-ignore-invisible t)
798 (add-to-invisibility-spec '(hs . t)) ;;hs invisible
799 (hs-grok-mode-type)
800 (run-hooks 'hs-minor-mode-hook))
801 ; (if (eq hs-emacs-type 'lucid)
802 ; (set-buffer-menubar (delete hs-menu-bar current-menubar)))
803 (remove-from-invisibility-spec '(hs . t))))
806 ;;;----------------------------------------------------------------------------
807 ;;; load-time setup routines
809 ;; which emacs being used?
810 ;(setq hs-emacs-type
811 ; (if (string-match "xemacs\\|lucid" emacs-version)
812 ; 'lucid
813 ; 'fsf))
815 ;; keymaps and menus
816 (if hs-minor-mode-map
818 (setq hs-minor-mode-map (make-sparse-keymap))
819 (easy-menu-define hs-minor-mode-menu
820 hs-minor-mode-map
821 "Menu used when hideshow minor mode is active."
822 (cons "Hide/Show"
823 (mapcar
824 ;; populate keymap then massage entry for easymenu
825 (lambda (ent)
826 (define-key hs-minor-mode-map (aref ent 2) (aref ent 1))
827 (aset ent 2 (not (vectorp (aref ent 2)))) ; disable mousy stuff
828 ent)
829 ;; I believe there is nothing bound on these keys
830 ;; menu entry command key
831 '(["Hide Block" hs-hide-block "\C-ch"]
832 ["Show Block" hs-show-block "\C-cs"]
833 ["Hide All" hs-hide-all "\C-cH"]
834 ["Show All" hs-show-all "\C-cS"]
835 ["Hide Level" hs-hide-level "\C-cL"]
836 ["Show Region" hs-show-region "\C-cR"]
837 ["Toggle Hiding" hs-mouse-toggle-hiding [S-mouse-2]]
838 )))))
840 ;; some housekeeping
841 (or (assq 'hs-minor-mode minor-mode-map-alist)
842 (setq minor-mode-map-alist
843 (cons (cons 'hs-minor-mode hs-minor-mode-map)
844 minor-mode-map-alist)))
845 (or (assq 'hs-minor-mode minor-mode-alist)
846 (setq minor-mode-alist (append minor-mode-alist
847 (list '(hs-minor-mode " hs")))))
849 ;; make some variables permanently buffer-local
850 (mapcar (lambda (var)
851 (make-variable-buffer-local var)
852 (put var 'permanent-local t))
853 '(hs-minor-mode
854 hs-c-start-regexp
855 hs-block-start-regexp
856 hs-block-end-regexp
857 hs-forward-sexp-func
858 hs-adjust-block-beginning))
860 ;;;----------------------------------------------------------------------------
861 ;;; that's it
863 (provide 'hideshow)
865 ;;; hideshow.el ends here