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