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