(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / progmodes / hideshow.el
blobd4229cf44c66e7987517eb33577e26bd73ab7c4d
1 ;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01 Free Software Foundation
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.58.2.4
9 ;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; * Commands provided
32 ;; This file provides Hideshow Minor Mode. When active, nine commands
33 ;; are available, implementing block hiding and showing. They (and their
34 ;; keybindings) are:
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
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.
50 ;; The command `M-x hs-minor-mode' toggles the minor mode or sets it
51 ;; (similar to other minor modes).
53 ;; * Suggested usage
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:
59 ;; (load-library "hideshow")
60 ;; (add-hook 'X-mode-hook ; other modes similarly
61 ;; (lambda () (hs-minor-mode 1)))
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'.
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.
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)))
82 ;; [Your hideshow hacks here!]
84 ;; * Customization
86 ;; You can use `M-x customize-variable' on the following variables:
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
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:
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)
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:
113 ;; (unless (memq 'hs-headline mode-line-format)
114 ;; (setq mode-line-format
115 ;; (append '("-" hs-headline) mode-line-format)))
117 ;; See documentation for `mode-line-format' for more info.
119 ;; Hooks are run after some commands:
121 ;; hs-hide-hook in hs-hide-block, hs-hide-all, hs-hide-level
122 ;; hs-show-hook hs-show-block, hs-show-all
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.
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:
136 ;; (add-to-list 'hs-special-modes-alist '(my-mode "{{" "}}" ...))
138 ;; If you have an entry that works particularly well, consider
139 ;; submitting it for inclusion in hideshow.el. See docstring for
140 ;; `hs-special-modes-alist' for more info on the entry format.
142 ;; See also variable `hs-set-up-overlay' for per-block customization of
143 ;; appearance or other effects associated with overlays. For example:
145 ;; (setq hs-set-up-overlay
146 ;; (defun my-display-code-line-counts (ov)
147 ;; (when (eq 'code (overlay-get ov 'hs))
148 ;; (overlay-put ov 'display
149 ;; (propertize
150 ;; (format " ... <%d>"
151 ;; (count-lines (overlay-start ov)
152 ;; (overlay-end ov)))
153 ;; 'face 'font-lock-type-face)))))
155 ;; * Bugs
157 ;; (1) Hideshow does not work w/ emacs 18 because emacs 18 lacks the
158 ;; function `forward-comment' (among other things). If someone
159 ;; writes this, please send me a copy.
161 ;; (2) Sometimes `hs-headline' can become out of sync. To reset, type
162 ;; `M-x hs-minor-mode' twice (that is, deactivate then re-activate
163 ;; hideshow).
165 ;; (3) Hideshow 5.x is developed and tested on GNU Emacs 20.7.
166 ;; XEmacs compatibility may have bitrotted since 4.29.
168 ;; (4) Some buffers can't be `byte-compile-file'd properly. This is because
169 ;; `byte-compile-file' inserts the file to be compiled in a temporary
170 ;; buffer and switches `normal-mode' on. In the case where you have
171 ;; `hs-hide-initial-comment-block' in `hs-minor-mode-hook', the hiding of
172 ;; the initial comment sometimes hides parts of the first statement (seems
173 ;; to be only in `normal-mode'), so there are unbalanced "(" and ")".
175 ;; The workaround is to clear `hs-minor-mode-hook' when byte-compiling:
177 ;; (defadvice byte-compile-file (around
178 ;; byte-compile-file-hideshow-off
179 ;; act)
180 ;; (let ((hs-minor-mode-hook nil))
181 ;; ad-do-it))
183 ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the
184 ;; suggested workaround is to turn off hideshow entirely, for example:
186 ;; (defun turn-off-hideshow () (hs-minor-mode -1))
187 ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow)
188 ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow)
190 ;; In the case of `vc-diff', here is a less invasive workaround:
192 ;; (add-hook 'vc-before-checkin-hook
193 ;; (lambda ()
194 ;; (goto-char (point-min))
195 ;; (hs-show-block)))
197 ;; Unfortunately, these workarounds do not restore hideshow state.
198 ;; If someone figures out a better way, please let me know.
200 ;; * Correspondance
202 ;; Correspondance welcome; please indicate version number. Send bug
203 ;; reports and inquiries to <ttn@gnu.org>.
205 ;; * Thanks
207 ;; Thanks go to the following people for valuable ideas, code and
208 ;; bug reports.
210 ;; Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave
211 ;; Love, Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray,
212 ;; Moody Ahmad, Preston F. Crow, Lars Lindberg, Reto Zimmermann,
213 ;; Keith Sheffield, Chew Meng Kuan, Tony Lam, Pete Ware, François
214 ;; Pinard, Stefan Monnier, Joseph Eydelnant, Michael Ernst
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.
222 ;; * History
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.
233 ;;; Code:
235 (require 'easymenu)
237 ;;---------------------------------------------------------------------------
238 ;; user-configurable variables
240 (defgroup hideshow nil
241 "Minor mode for hiding and showing program and comment blocks."
242 :prefix "hs-"
243 :group 'languages)
245 ;;;###autoload
246 (defcustom hs-hide-comments-when-hiding-all t
247 "*Hide the comments too when you do an `hs-hide-all'."
248 :type 'boolean
249 :group 'hideshow)
251 (defcustom hs-minor-mode-hook nil
252 "*Hook called when hideshow minor mode is activated or deactivated."
253 :type 'hook
254 :group 'hideshow
255 :version "21.1")
257 (defcustom hs-isearch-open 'code
258 "*What kind of hidden blocks to open when doing `isearch'.
259 One of the following symbols:
261 code -- open only code blocks
262 comment -- open only comment blocks
263 t -- open both code and comment blocks
264 nil -- open neither code nor comment blocks
266 This has effect iff `search-invisible' is set to `open'."
267 :type '(choice (const :tag "open only code blocks" code)
268 (const :tag "open only comment blocks" comment)
269 (const :tag "open both code and comment blocks" t)
270 (const :tag "don't open any of them" nil))
271 :group 'hideshow)
273 ;;;###autoload
274 (defvar hs-special-modes-alist
275 '((c-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
276 (c++-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning)
277 (bibtex-mode ("^@\\S(*\\(\\s(\\)" 1))
278 (java-mode "{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning))
279 "*Alist for initializing the hideshow variables for different modes.
280 Each element has the form
281 (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
283 If non-nil, hideshow will use these values as regexps to define blocks
284 and comments, respectively for major mode MODE.
286 START, END and COMMENT-START are regular expressions. A block is
287 defined as text surrounded by START and END.
289 As a special case, START may be a list of the form (COMPLEX-START
290 MDATA-SELECTOR), where COMPLEX-START is a regexp w/ multiple parts and
291 MDATA-SELECTOR an integer that specifies which sub-match is the proper
292 place to adjust point, before calling `hs-forward-sexp-func'. Point
293 is adjusted to the beginning of the specified match. For example,
294 see the `hs-special-modes-alist' entry for `bibtex-mode'.
296 For some major modes, `forward-sexp' does not work properly. In those
297 cases, FORWARD-SEXP-FUNC specifies another function to use instead.
299 See the documentation for `hs-adjust-block-beginning' to see what is the
300 use of ADJUST-BEG-FUNC.
302 If any of the elements is left nil or omitted, hideshow tries to guess
303 appropriate values. The regexps should not contain leading or trailing
304 whitespace. Case does not matter.")
306 (defvar hs-hide-all-non-comment-function nil
307 "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
309 (defvar hs-hide-hook nil
310 "*Hook called (with `run-hooks') at the end of commands to hide text.
311 These commands include the toggling commands (when the result is to hide
312 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
314 (defvar hs-show-hook nil
315 "*Hook called (with `run-hooks') at the end of commands to show text.
316 These commands include the toggling commands (when the result is to show
317 a block), `hs-show-all' and `hs-show-block'..")
319 (defvar hs-set-up-overlay nil
320 "*Function called with one arg, OV, a newly initialized overlay.
321 Hideshow puts a unique overlay on each range of text to be hidden
322 in the buffer. Here is a simple example of how to use this variable:
324 (defun display-code-line-counts (ov)
325 (when (eq 'code (overlay-get ov 'hs))
326 (overlay-put ov 'display
327 (format \"... / %d\"
328 (count-lines (overlay-start ov)
329 (overlay-end ov))))))
331 (setq hs-set-up-overlay 'display-code-line-counts)
333 This example shows how to get information from the overlay as well
334 as how to set its `display' property. See `hs-make-overlay' and
335 info node `(elisp)Overlays'.")
337 ;;---------------------------------------------------------------------------
338 ;; internal variables
340 (defvar hs-minor-mode nil
341 "Non-nil if using hideshow mode as a minor mode of some other mode.
342 Use the command `hs-minor-mode' to toggle or set this variable.")
344 (defvar hs-minor-mode-map nil
345 "Keymap for hideshow minor mode.")
347 (defvar hs-minor-mode-menu nil
348 "Menu for hideshow minor mode.")
350 (defvar hs-c-start-regexp nil
351 "Regexp for beginning of comments.
352 Differs from mode-specific comment regexps in that
353 surrounding whitespace is stripped.")
355 (defvar hs-block-start-regexp nil
356 "Regexp for beginning of block.")
358 (defvar hs-block-start-mdata-select nil
359 "Element in `hs-block-start-regexp' match data to consider as block start.
360 The internal function `hs-forward-sexp' moves point to the beginning of this
361 element (using `match-beginning') before calling `hs-forward-sexp-func'.")
363 (defvar hs-block-end-regexp nil
364 "Regexp for end of block.")
366 (defvar hs-forward-sexp-func 'forward-sexp
367 "Function used to do a `forward-sexp'.
368 Should change for Algol-ish modes. For single-character block
369 delimiters -- ie, the syntax table regexp for the character is
370 either `(' or `)' -- `hs-forward-sexp-func' would just be
371 `forward-sexp'. For other modes such as simula, a more specialized
372 function is necessary.")
374 (defvar hs-adjust-block-beginning nil
375 "Function used to tweak the block beginning.
376 The block is hidden from the position returned by this function,
377 as opposed to hiding it from the position returned when searching
378 for `hs-block-start-regexp'.
380 For example, in c-like modes, if we wish to also hide the curly braces
381 \(if you think they occupy too much space on the screen), this function
382 should return the starting point (at the end of line) of the hidden
383 region.
385 It is called with a single argument ARG which is the position in
386 buffer after the block beginning.
388 It should return the position from where we should start hiding.
390 It should not move the point.
392 See `hs-c-like-adjust-block-beginning' for an example of using this.")
394 (defvar hs-headline nil
395 "Text of the line where a hidden block begins, set during isearch.
396 You can display this in the mode line by adding the symbol `hs-headline'
397 to the variable `mode-line-format'. For example,
399 (unless (memq 'hs-headline mode-line-format)
400 (setq mode-line-format
401 (append '(\"-\" hs-headline) mode-line-format)))
403 Note that `mode-line-format' is buffer-local.")
405 ;;---------------------------------------------------------------------------
406 ;; system dependency
408 (defalias 'hs-match-data 'match-data)
410 ;;---------------------------------------------------------------------------
411 ;; support functions
413 (defun hs-discard-overlays (from to)
414 "Delete hideshow overlays in region defined by FROM and TO."
415 (when (< to from)
416 (setq from (prog1 to (setq to from))))
417 (dolist (ov (overlays-in from to))
418 (when (overlay-get ov 'hs)
419 (delete-overlay ov))))
421 (defun hs-make-overlay (b e kind &optional b-offset e-offset)
422 "Return a new overlay in region defined by B and E with type KIND.
423 KIND is either `code' or `comment'. Optional fourth arg B-OFFSET
424 when added to B specifies the actual buffer position where the block
425 begins. Likewise for optional fifth arg E-OFFSET. If unspecified
426 they are taken to be 0 (zero). The following properties are set
427 in the overlay: 'invisible 'hs 'hs-b-offset 'hs-e-offset. Also,
428 depending on variable `hs-isearch-open', the following properties may
429 be present: 'isearch-open-invisible 'isearch-open-invisible-temporary.
430 If variable `hs-set-up-overlay' is non-nil it should specify a function
431 to call with the newly initialized overlay."
432 (unless b-offset (setq b-offset 0))
433 (unless e-offset (setq e-offset 0))
434 (let ((ov (make-overlay b e))
435 (io (if (eq 'block hs-isearch-open)
436 ;; backward compatibility -- `block'<=>`code'
437 'code
438 hs-isearch-open)))
439 (overlay-put ov 'invisible 'hs)
440 (overlay-put ov 'hs kind)
441 (overlay-put ov 'hs-b-offset b-offset)
442 (overlay-put ov 'hs-e-offset e-offset)
443 (when (or (eq io t) (eq io kind))
444 (overlay-put ov 'isearch-open-invisible 'hs-isearch-show)
445 (overlay-put ov 'isearch-open-invisible-temporary
446 'hs-isearch-show-temporary))
447 (when hs-set-up-overlay (funcall hs-set-up-overlay ov))
448 ov))
450 (defun hs-isearch-show (ov)
451 "Delete overlay OV, and set `hs-headline' to nil.
453 This function is meant to be used as the `isearch-open-invisible'
454 property of an overlay."
455 (setq hs-headline nil)
456 (delete-overlay ov))
458 (defun hs-isearch-show-temporary (ov hide-p)
459 "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
460 If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
461 Otherwise, `hs-headline' is set to the line of text at the head of OV, and
462 OV is shown.
464 This function is meant to be used as the `isearch-open-invisible-temporary'
465 property of an overlay."
466 (setq hs-headline
467 (if hide-p
469 (or hs-headline
470 (let ((start (overlay-start ov)))
471 (buffer-substring
472 (save-excursion (goto-char start)
473 (beginning-of-line)
474 (skip-chars-forward " \t")
475 (point))
476 start)))))
477 (force-mode-line-update)
478 ;; handle `display' property specially
479 (let (value)
480 (if hide-p
481 (when (setq value (overlay-get ov 'hs-isearch-display))
482 (overlay-put ov 'display value)
483 (overlay-put ov 'hs-isearch-display nil))
484 (when (setq value (overlay-get ov 'display))
485 (overlay-put ov 'hs-isearch-display value)
486 (overlay-put ov 'display nil))))
487 (overlay-put ov 'invisible (and hide-p 'hs)))
489 (defun hs-forward-sexp (match-data arg)
490 "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
491 Original match data is restored upon return."
492 (save-match-data
493 (set-match-data match-data)
494 (goto-char (match-beginning hs-block-start-mdata-select))
495 (funcall hs-forward-sexp-func arg)))
497 (defun hs-hide-comment-region (beg end &optional repos-end)
498 "Hide a region from BEG to END, marking it as a comment.
499 Optional arg REPOS-END means reposition at end."
500 (let ((beg-eol (progn (goto-char beg) (end-of-line) (point)))
501 (end-eol (progn (goto-char end) (end-of-line) (point))))
502 (hs-discard-overlays beg-eol end-eol)
503 (hs-make-overlay beg-eol end-eol 'comment beg end))
504 (goto-char (if repos-end end beg)))
506 (defun hs-hide-block-at-point (&optional end comment-reg)
507 "Hide block iff on block beginning.
508 Optional arg END means reposition at end.
509 Optional arg COMMENT-REG is a list of the form (BEGIN END) and
510 specifies the limits of the comment, or nil if the block is not
511 a comment.
513 The block beginning is adjusted by `hs-adjust-block-beginning'
514 and then further adjusted to be at the end of the line."
515 (if comment-reg
516 (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
517 (when (looking-at hs-block-start-regexp)
518 (let* ((mdata (hs-match-data t))
519 (pure-p (match-end 0))
521 ;; `p' is the point at the end of the block beginning,
522 ;; which may need to be adjusted
523 (save-excursion
524 (goto-char (funcall (or hs-adjust-block-beginning
525 'identity)
526 pure-p))
527 ;; whatever the adjustment, we move to eol
528 (end-of-line)
529 (point)))
531 ;; `q' is the point at the end of the block
532 (progn (hs-forward-sexp mdata 1)
533 (end-of-line)
534 (point))))
535 (when (and (< p (point)) (> (count-lines p q) 1))
536 (hs-discard-overlays p q)
537 (hs-make-overlay p q 'code (- pure-p p)))
538 (goto-char (if end q (min p pure-p)))))))
540 (defun hs-safety-is-job-n ()
541 "Warn if `buffer-invisibility-spec' does not contain symbol `hs'."
542 (unless (and (listp buffer-invisibility-spec)
543 (assq 'hs buffer-invisibility-spec))
544 (message "Warning: `buffer-invisibility-spec' does not contain hs!!")
545 (sit-for 2)))
547 (defun hs-inside-comment-p ()
548 "Return non-nil if point is inside a comment, otherwise nil.
549 Actually, return a list containing the buffer position of the start
550 and the end of the comment. A comment block can be hidden only if on
551 its starting line there is only whitespace preceding the actual comment
552 beginning. If we are inside of a comment but this condition is not met,
553 we return a list having a nil as its car and the end of comment position
554 as cdr."
555 (save-excursion
556 ;; the idea is to look backwards for a comment start regexp, do a
557 ;; forward comment, and see if we are inside, then extend extend
558 ;; forward and backward as long as we have comments
559 (let ((q (point)))
560 (when (or (looking-at hs-c-start-regexp)
561 (re-search-backward hs-c-start-regexp (point-min) t))
562 ;; first get to the beginning of this comment...
563 (while (and (not (bobp))
564 (= (point) (progn (forward-comment -1) (point))))
565 (forward-char -1))
566 ;; ...then extend backwards
567 (forward-comment (- (buffer-size)))
568 (skip-chars-forward " \t\n\f")
569 (let ((p (point))
570 (hidable t))
571 (beginning-of-line)
572 (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
573 ;; we are in this situation: (example)
574 ;; (defun bar ()
575 ;; (foo)
576 ;; ) ; comment
577 ;; ^
578 ;; the point was here before doing (beginning-of-line)
579 ;; here we should advance till the next comment which
580 ;; eventually has only white spaces preceding it on the same
581 ;; line
582 (goto-char p)
583 (forward-comment 1)
584 (skip-chars-forward " \t\n\f")
585 (setq p (point))
586 (while (and (< (point) q)
587 (> (point) p)
588 (not (looking-at hs-c-start-regexp)))
589 (setq p (point)) ;; use this to avoid an infinite cycle
590 (forward-comment 1)
591 (skip-chars-forward " \t\n\f"))
592 (when (or (not (looking-at hs-c-start-regexp))
593 (> (point) q))
594 ;; we cannot hide this comment block
595 (setq hidable nil)))
596 ;; goto the end of the comment
597 (forward-comment (buffer-size))
598 (skip-chars-backward " \t\n\f")
599 (end-of-line)
600 (when (>= (point) q)
601 (list (and hidable p) (point))))))))
603 (defun hs-grok-mode-type ()
604 "Set up hideshow variables for new buffers.
605 If `hs-special-modes-alist' has information associated with the
606 current buffer's major mode, use that.
607 Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
608 function; and adjust-block-beginning function."
609 (if (and (boundp 'comment-start)
610 (boundp 'comment-end)
611 comment-start comment-end)
612 (let* ((lookup (assoc major-mode hs-special-modes-alist))
613 (start-elem (or (nth 1 lookup) "\\s(")))
614 (if (listp start-elem)
615 ;; handle (START-REGEXP MDATA-SELECT)
616 (setq hs-block-start-regexp (car start-elem)
617 hs-block-start-mdata-select (cadr start-elem))
618 ;; backwards compatibility: handle simple START-REGEXP
619 (setq hs-block-start-regexp start-elem
620 hs-block-start-mdata-select 0))
621 (setq hs-block-end-regexp (or (nth 2 lookup) "\\s)")
622 hs-c-start-regexp (or (nth 3 lookup)
623 (let ((c-start-regexp
624 (regexp-quote comment-start)))
625 (if (string-match " +$" c-start-regexp)
626 (substring c-start-regexp
627 0 (1- (match-end 0)))
628 c-start-regexp)))
629 hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
630 hs-adjust-block-beginning (nth 5 lookup)))
631 (setq hs-minor-mode nil)
632 (error "%s Mode doesn't support Hideshow Minor Mode" mode-name)))
634 (defun hs-find-block-beginning ()
635 "Reposition point at block-start.
636 Return point, or nil if original point was not in a block."
637 (let ((done nil)
638 (here (point)))
639 ;; look if current line is block start
640 (if (looking-at hs-block-start-regexp)
641 (point)
642 ;; look backward for the start of a block that contains the cursor
643 (while (and (re-search-backward hs-block-start-regexp nil t)
644 (not (setq done
645 (< here (save-excursion
646 (hs-forward-sexp (hs-match-data t) 1)
647 (point)))))))
648 (if done
649 (point)
650 (goto-char here)
651 nil))))
653 (defun hs-hide-level-recursive (arg minp maxp)
654 "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
655 (when (hs-find-block-beginning)
656 (setq minp (1+ (point)))
657 (funcall hs-forward-sexp-func 1)
658 (setq maxp (1- (point))))
659 (hs-discard-overlays minp maxp) ; eliminate weirdness
660 (goto-char minp)
661 (while (progn
662 (forward-comment (buffer-size))
663 (and (< (point) maxp)
664 (re-search-forward hs-block-start-regexp maxp t)))
665 (if (> arg 1)
666 (hs-hide-level-recursive (1- arg) minp maxp)
667 (goto-char (match-beginning hs-block-start-mdata-select))
668 (hs-hide-block-at-point t)))
669 (hs-safety-is-job-n)
670 (goto-char maxp))
672 (defmacro hs-life-goes-on (&rest body)
673 "Evaluate BODY forms iff variable `hs-minor-mode' is non-nil.
674 In the dynamic context of this macro, `inhibit-point-motion-hooks'
675 and `case-fold-search' are both t."
676 `(when hs-minor-mode
677 (let ((inhibit-point-motion-hooks t)
678 (case-fold-search t))
679 ,@body)))
681 (put 'hs-life-goes-on 'edebug-form-spec '(&rest form))
683 (defun hs-already-hidden-p ()
684 "Return non-nil if point is in an already-hidden block, otherwise nil."
685 (save-excursion
686 (let ((c-reg (hs-inside-comment-p)))
687 (if (and c-reg (nth 0 c-reg))
688 ;; point is inside a comment, and that comment is hidable
689 (goto-char (nth 0 c-reg))
690 (when (and (not c-reg)
691 (hs-find-block-beginning)
692 (looking-at hs-block-start-regexp))
693 ;; point is inside a block
694 (goto-char (match-end 0)))))
695 (end-of-line)
696 (let ((overlays (overlays-at (point)))
697 (found nil))
698 (while (and (not found) (overlayp (car overlays)))
699 (setq found (overlay-get (car overlays) 'hs)
700 overlays (cdr overlays)))
701 found)))
703 (defun hs-c-like-adjust-block-beginning (initial)
704 "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
705 Actually, point is never moved; a new position is returned that is
706 the end of the C-function header. This adjustment function is meant
707 to be assigned to `hs-adjust-block-beginning' for C-like modes."
708 (save-excursion
709 (goto-char (1- initial))
710 (forward-comment (- (buffer-size)))
711 (point)))
713 ;;---------------------------------------------------------------------------
714 ;; commands
716 (defun hs-hide-all ()
717 "Hide all top level blocks, displaying only first and last lines.
718 Move point to the beginning of the line, and run the normal hook
719 `hs-hide-hook'. See documentation for `run-hooks'.
720 If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
721 (interactive)
722 (hs-life-goes-on
723 (message "Hiding all blocks ...")
724 (save-excursion
725 (hs-discard-overlays (point-min) (point-max)) ; eliminate weirdness
726 (goto-char (point-min))
727 (let ((count 0)
728 (re (concat "\\("
729 hs-block-start-regexp
730 "\\)"
731 (if hs-hide-comments-when-hiding-all
732 (concat "\\|\\("
733 hs-c-start-regexp
734 "\\)")
735 ""))))
736 (while (progn
737 (unless hs-hide-comments-when-hiding-all
738 (forward-comment (point-max)))
739 (re-search-forward re (point-max) t))
740 (if (match-beginning 1)
741 ;; we have found a block beginning
742 (progn
743 (goto-char (match-beginning 1))
744 (if hs-hide-all-non-comment-function
745 (funcall hs-hide-all-non-comment-function)
746 (hs-hide-block-at-point t)))
747 ;; found a comment, probably
748 (let ((c-reg (hs-inside-comment-p))) ; blech!
749 (when (and c-reg (car c-reg))
750 (if (> (count-lines (car c-reg) (nth 1 c-reg)) 1)
751 (hs-hide-block-at-point t c-reg)
752 (goto-char (nth 1 c-reg))))))
753 (message "Hiding ... %d" (setq count (1+ count)))))
754 (hs-safety-is-job-n))
755 (beginning-of-line)
756 (message "Hiding all blocks ... done")
757 (run-hooks 'hs-hide-hook)))
759 (defun hs-show-all ()
760 "Show everything then run `hs-show-hook'. See `run-hooks'."
761 (interactive)
762 (hs-life-goes-on
763 (message "Showing all blocks ...")
764 (hs-discard-overlays (point-min) (point-max))
765 (message "Showing all blocks ... done")
766 (run-hooks 'hs-show-hook)))
768 (defun hs-hide-block (&optional end)
769 "Select a block and hide it. With prefix arg, reposition at END.
770 Upon completion, point is repositioned and the normal hook
771 `hs-hide-hook' is run. See documentation for `run-hooks'."
772 (interactive "P")
773 (hs-life-goes-on
774 (let ((c-reg (hs-inside-comment-p)))
775 (cond
776 ((and c-reg (or (null (nth 0 c-reg))
777 (<= (count-lines (car c-reg) (nth 1 c-reg)) 1)))
778 (message "(not enough comment lines to hide)"))
779 ((or c-reg
780 (looking-at hs-block-start-regexp)
781 (hs-find-block-beginning))
782 (hs-hide-block-at-point end c-reg)
783 (hs-safety-is-job-n)
784 (run-hooks 'hs-hide-hook))))))
786 (defun hs-show-block (&optional end)
787 "Select a block and show it.
788 With prefix arg, reposition at END. Upon completion, point is
789 repositioned and the normal hook `hs-show-hook' is run.
790 See documentation for functions `hs-hide-block' and `run-hooks'."
791 (interactive "P")
792 (hs-life-goes-on
794 ;; first see if we have something at the end of the line
795 (catch 'eol-begins-hidden-region-p
796 (let ((here (point)))
797 (dolist (ov (save-excursion (end-of-line) (overlays-at (point))))
798 (when (overlay-get ov 'hs)
799 (goto-char
800 (cond (end (overlay-end ov))
801 ((eq 'comment (overlay-get ov 'hs)) here)
802 (t (+ (overlay-start ov) (overlay-get ov 'hs-b-offset)))))
803 (delete-overlay ov)
804 (throw 'eol-begins-hidden-region-p t)))
805 nil))
806 ;; not immediately obvious, look for a suitable block
807 (let ((c-reg (hs-inside-comment-p))
808 p q)
809 (cond (c-reg
810 (when (car c-reg)
811 (setq p (car c-reg)
812 q (cadr c-reg))))
813 ((and (hs-find-block-beginning)
814 (looking-at hs-block-start-regexp)) ; fresh match-data, ugh
815 (setq p (point)
816 q (progn (hs-forward-sexp (hs-match-data t) 1) (point)))))
817 (when (and p q)
818 (hs-discard-overlays p q)
819 (goto-char (if end q (1+ p)))))
820 (hs-safety-is-job-n)
821 (run-hooks 'hs-show-hook))))
823 (defun hs-hide-level (arg)
824 "Hide all blocks ARG levels below this block.
825 The hook `hs-hide-hook' is run; see `run-hooks'."
826 (interactive "p")
827 (hs-life-goes-on
828 (save-excursion
829 (message "Hiding blocks ...")
830 (hs-hide-level-recursive arg (point-min) (point-max))
831 (message "Hiding blocks ... done"))
832 (hs-safety-is-job-n)
833 (run-hooks 'hs-hide-hook)))
835 (defun hs-toggle-hiding ()
836 "Toggle hiding/showing of a block.
837 See `hs-hide-block' and `hs-show-block'."
838 (interactive)
839 (hs-life-goes-on
840 (if (hs-already-hidden-p)
841 (hs-show-block)
842 (hs-hide-block))))
844 (defun hs-mouse-toggle-hiding (e)
845 "Toggle hiding/showing of a block.
846 This command should be bound to a mouse key.
847 Argument E is a mouse event used by `mouse-set-point'.
848 See `hs-hide-block' and `hs-show-block'."
849 (interactive "@e")
850 (hs-life-goes-on
851 (mouse-set-point e)
852 (hs-toggle-hiding)))
854 (defun hs-hide-initial-comment-block ()
855 "Hide the first block of comments in a file.
856 This can be useful if you have huge RCS logs in those comments."
857 (interactive)
858 (hs-life-goes-on
859 (let ((c-reg (save-excursion
860 (goto-char (point-min))
861 (skip-chars-forward " \t\n\f")
862 (hs-inside-comment-p))))
863 (when c-reg
864 (let ((beg (car c-reg)) (end (cadr c-reg)))
865 ;; see if we have enough comment lines to hide
866 (when (> (count-lines beg end) 1)
867 (hs-hide-comment-region beg end)))))))
869 ;;;###autoload
870 (defun hs-minor-mode (&optional arg)
871 "Toggle hideshow minor mode.
872 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
873 When hideshow minor mode is on, the menu bar is augmented with hideshow
874 commands and the hideshow commands are enabled.
875 The value '(hs . t) is added to `buffer-invisibility-spec'.
877 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
878 `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
879 `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
881 Turning hideshow minor mode off reverts the menu bar and the
882 variables to default values and disables the hideshow commands.
884 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
886 Key bindings:
887 \\{hs-minor-mode-map}"
889 (interactive "P")
890 (setq hs-headline nil
891 hs-minor-mode (if (null arg)
892 (not hs-minor-mode)
893 (> (prefix-numeric-value arg) 0)))
894 (if hs-minor-mode
895 (progn
896 (hs-grok-mode-type)
897 (easy-menu-add hs-minor-mode-menu)
898 (set (make-local-variable 'line-move-ignore-invisible) t)
899 (add-to-invisibility-spec '(hs . t)))
900 (easy-menu-remove hs-minor-mode-menu)
901 (remove-from-invisibility-spec '(hs . t)))
902 (run-hooks 'hs-minor-mode-hook))
904 ;;---------------------------------------------------------------------------
905 ;; load-time actions
907 ;; keymaps and menus
908 (unless hs-minor-mode-map
909 (setq hs-minor-mode-map (make-sparse-keymap))
910 (easy-menu-define hs-minor-mode-menu
911 hs-minor-mode-map
912 "Menu used when hideshow minor mode is active."
913 (cons "Hide/Show"
914 (mapcar
915 ;; Interpret each table entry as follows: first, populate keymap
916 ;; with elements 2 and 1; then, for easymenu, use entry directly
917 ;; unless element 0 is nil, in which case the entry is "omitted".
918 (lambda (ent)
919 (define-key hs-minor-mode-map (aref ent 2) (aref ent 1))
920 (if (aref ent 0) ent "-----"))
921 ;; These bindings roughly imitate those used by Outline mode.
922 ;; menu entry command key
923 '(["Hide Block" hs-hide-block "\C-c@\C-h"]
924 ["Show Block" hs-show-block "\C-c@\C-s"]
925 ["Hide All" hs-hide-all "\C-c@\C-\M-h"]
926 ["Show All" hs-show-all "\C-c@\C-\M-s"]
927 ["Hide Level" hs-hide-level "\C-c@\C-l"]
928 ["Toggle Hiding" hs-toggle-hiding "\C-c@\C-c"]
929 [nil hs-mouse-toggle-hiding [(shift mouse-2)]]
930 )))))
932 ;; some housekeeping
933 (add-to-list 'minor-mode-map-alist (cons 'hs-minor-mode hs-minor-mode-map))
934 (add-to-list 'minor-mode-alist '(hs-minor-mode " hs") t)
936 ;; make some variables permanently buffer-local
937 (dolist (var '(hs-minor-mode
938 hs-c-start-regexp
939 hs-block-start-regexp
940 hs-block-start-mdata-select
941 hs-block-end-regexp
942 hs-forward-sexp-func
943 hs-adjust-block-beginning))
944 (make-variable-buffer-local var)
945 (put var 'permanent-local t))
947 ;;---------------------------------------------------------------------------
948 ;; that's it
950 (provide 'hideshow)
952 ;;; arch-tag: 378b6852-e82a-466a-aee8-d9c73859a65e
953 ;;; hideshow.el ends here