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