comparison lisp/modes/hideshow.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; hideshow.el --- minor mode cmds to selectively display blocks of code
2
3 ;;; Copyright (C) 1994,1995 Free Software Foundation
4
5 ;;; Author: Thien-Thi Nguyen <ttn@netcom.com>
6 ;;; Version: 3.4
7 ;;; Keywords: C C++ lisp tools editing
8 ;;; Time-of-Day-Author-Most-Likely-to-be-Recalcitrant: early morning
9
10 ;;; This file is part of GNU Emacs.
11
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by the
14 ;;; Free Software Foundation; either version 2 of the License, or (at your
15 ;;; option) any later version.
16 ;;;
17 ;;; GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
18 ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 ;;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 ;;; for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License along
23 ;;; with this program; if not, write to the Free Software Foundation, Inc.,
24 ;;; 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; LCD Archive Entry:
27 ;;; hideshow|Thien-Thi Nguyen|ttn@netcom.com|
28 ;;; minor mode commands to selectively display blocks of code|
29 ;;; 18-Oct-1994|3.4|~/modes/hideshow.el.Z|
30
31 ;;; Commentary:
32
33 ;;; This file provides `hs-minor-mode'. When active, six commands:
34 ;;; hs-{hide,show}-{all,block}, hs-show-region and hs-minor-mode
35 ;;; are available. They implement block hiding and showing. Blocks are
36 ;;; defined in mode-specific way. In c-mode or c++-mode, they are simply
37 ;;; curly braces, while in lisp-ish modes they are parens. Multi-line
38 ;;; comments (c-mode) can also be hidden. The command M-x hs-minor-mode
39 ;;; toggles the minor mode or sets it (similar to outline minor mode).
40 ;;; See documentation for each command for more info.
41 ;;;
42 ;;; The variable `hs-unbalance-handler-method' controls hideshow's behavior
43 ;;; in the case of "unbalanced parentheses". See doc for more info.
44
45 ;;; Suggested usage:
46
47 ;;; (load-library "hideshow")
48 ;;; (defun my-hs-setup () "enables hideshow and binds some commands"
49 ;;; (hs-minor-mode 1)
50 ;;; (define-key hs-minor-mode-map "\C-ch" 'hs-hide-block)
51 ;;; (define-key hs-minor-mode-map "\C-cs" 'hs-show-block)
52 ;;; (define-key hs-minro-mode-map "\C-cH" 'hs-hide-all)
53 ;;; (define-key hs-minro-mode-map "\C-cS" 'hs-show-all)
54 ;;; (define-key hs-minor-mode-map "\C-cR" 'hs-show-region))
55 ;;; (add-hook 'X-mode-hook 'my-hs-setup t) ; other modes similarly
56 ;;;
57 ;;; where X = {emacs-lisp,c,c++,perl,...}. See the doc for the variable
58 ;;; `hs-special-modes-alist' if you'd like to use hideshow w/ other modes.
59
60 ;;; Etc:
61
62 ;;; Bug reports and fixes welcome (comments, too). Thanks go to
63 ;;; Dean Andrews <adahome@ix.netcom.com>
64 ;;; Preston F. Crow <preston.f.crow@dartmouth.edu>
65 ;;; Gael Marziou <gael@gnlab030.grenoble.hp.com>
66 ;;; Keith Sheffield <sheff@edcsgw2.cr.usgs.gov>
67 ;;; Jan Djarv <jan.djarv@sa.erisoft.se>
68 ;;; Lars Lindberg <qhslali@aom.ericsson.se>
69 ;;; Alf-Ivar Holm <alfh@ifi.uio.no>
70 ;;; for valuable feedback, code and bug reports.
71
72 ;;; Code:
73
74
75 ;;;----------------------------------------------------------------------------
76 ;;; user-configurable variables
77
78 (defvar hs-unbalance-handler-method 'top-level
79 "*Symbol representing how \"unbalanced parentheses\" should be handled.
80 This error is usually signalled by hs-show-block. One of four values:
81 `top-level', `next-line', `signal' or `ignore'. Default is `top-level'.
82
83 - `top-level' -- Show top-level block containing the currently troublesome
84 block.
85 - `next-line' -- Use the fact that, for an already hidden block, its end
86 will be on the next line. Attempt to show this block.
87 - `signal' -- Pass the error through, stopping execution.
88 - `ignore' -- Ignore the error, continuing execution.
89
90 Values other than these four will be interpreted as `signal'.")
91
92 (defvar hs-special-modes-alist '((c-mode "{" "}")
93 (c++-mode "{" "}"))
94 "*Alist of the form (MODE START-RE END-RE FORWARD-SEXP-FUNC).
95 If present, hideshow will use these values for the start and end regexps,
96 respectively. Since Algol-ish languages do not have single-character
97 block delimiters, the function `forward-sexp' which is used by hideshow
98 doesn't work. In this case, if a similar function is provided, you can
99 register it and have hideshow use it instead of `forward-sexp'. To add
100 more values, use
101
102 \t(pushnew '(new-mode st-re end-re function-name)
103 \t hs-special-modes-alist :test 'equal)
104
105 For example:
106
107 \t(pushnew '(simula-mode \"begin\" \"end\" simula-next-statement)
108 \t hs-special-modes-alist :test 'equal)
109
110 Note that the regexps should not contain leading or trailing whitespace.")
111
112 (defvar hs-hide-hooks nil
113 "*Hooks called at the end of hs-hide-all and hs-hide-block.")
114
115 (defvar hs-show-hooks nil
116 "*Hooks called at the end of hs-show-all, hs-show-block and hs-show-region.")
117
118 (defvar hs-minor-mode-prefix "\C-c"
119 "*Prefix key to use for hideshow commands in hideshow minor mode.")
120
121
122 ;;;----------------------------------------------------------------------------
123 ;;; internal variables
124
125 ;;;###autoload
126 (defvar hs-minor-mode nil
127 "Non-nil if using hideshow mode as a minor mode of some other mode.
128 Use the command `hs-minor-mode' to toggle this variable.")
129
130 (defvar hs-minor-mode-map nil
131 "Mode map for hideshow minor mode.")
132
133 (defvar hs-menu-bar nil
134 "Menu bar for hideshow minor mode (Xemacs only).")
135
136 (defvar hs-c-start-regexp nil
137 "Regexp for beginning of comments. Buffer-local.
138 Differs from mode-specific comment regexps in that surrounding
139 whitespace is stripped.")
140
141 (defvar hs-c-end-regexp nil
142 "Regexp for end of comments. Buffer-local.
143 See `hs-c-start-regexp'.")
144
145 (defvar hs-block-start-regexp nil
146 "Regexp for beginning of block. Buffer-local.")
147
148 (defvar hs-block-end-regexp nil
149 "Regexp for end of block. Buffer-local.")
150
151 (defvar hs-forward-sexp-func 'forward-sexp
152 "Function used to do a forward-sexp. Should change for Algol-ish modes.
153 For single-character block delimiters -- ie, the syntax table regexp for the
154 character is either `(' or `)' -- `hs-forward-sexp-func' would just be
155 `forward-sexp'. For other modes such as simula, a more specialized function
156 is necessary.")
157
158 (defvar hs-emacs-type 'fsf
159 "Used to support both FSF Emacs and Xemacs.")
160
161 (eval-when-compile
162 (if (string-match "xemacs\\|lucid" emacs-version)
163 (progn
164 (defvar current-menubar nil "")
165 (defun set-buffer-menubar (arg1))
166 (defun add-menu (arg1 arg2 arg3)))))
167
168
169 ;;;----------------------------------------------------------------------------
170 ;;; support funcs
171
172 ;; snarfed from outline.el, but added buffer-read-only
173 (defun hs-flag-region (from to flag)
174 "Hides or shows lines from FROM to TO, according to FLAG.
175 If FLAG is \\n (newline character) then text is shown, while if FLAG
176 is \\^M \(control-M) the text is hidden."
177 (let ((modp (buffer-modified-p))
178 buffer-read-only) ; nothing is immune
179 (unwind-protect (progn
180 (subst-char-in-region
181 from to
182 (if (= flag ?\n) ?\C-m ?\n)
183 flag t))
184 (set-buffer-modified-p modp))))
185
186 (defun hs-hide-block-at-point (&optional end)
187 "Hide block iff on block beginning, optional END means reposition at end."
188 (if (looking-at hs-block-start-regexp)
189 (let* ((p (point))
190 (q (progn (funcall hs-forward-sexp-func 1) (point))))
191 (forward-line -1) (end-of-line)
192 (if (and (< p (point)) (> (count-lines p q) 1))
193 (hs-flag-region p (point) ?\C-m))
194 (goto-char (if end q p)))))
195
196 (defun hs-show-block-at-point (&optional end)
197 "Show block iff on block beginning. Optional END means reposition at end."
198 (if (looking-at hs-block-start-regexp)
199 (let* ((p (point))
200 (q
201 (condition-case error ; probably unbalanced paren
202 (progn
203 (funcall hs-forward-sexp-func 1)
204 (point))
205 (error
206 (cond
207 ((eq hs-unbalance-handler-method 'ignore)
208 ;; just ignore this block
209 (point))
210 ((eq hs-unbalance-handler-method 'top-level)
211 ;; try to get out of rat's nest and expose the whole func
212 (if (/= (current-column) 0) (beginning-of-defun))
213 (setq p (point))
214 (re-search-forward (concat "^" hs-block-start-regexp)
215 (point-max) t 2)
216 (point))
217 ((eq hs-unbalance-handler-method 'next-line)
218 ;; assumption is that user knows what s/he's doing
219 (beginning-of-line) (setq p (point))
220 (end-of-line 2) (point))
221 (t
222 ;; pass error through -- this applies to `signal', too
223 (signal (car error) (cdr error))))))))
224 (hs-flag-region p q ?\n)
225 (goto-char (if end (1+ (point)) p)))))
226
227 (defun hs-safety-is-job-n ()
228 "Warns if selective-display or selective-display-ellipses is nil."
229 (let ((str ""))
230 (or selective-display
231 (setq str "selective-display nil "))
232 (or selective-display-ellipses
233 (setq str (concat str "selective-display-ellipses nil")))
234 (if (= (length str) 0)
235 nil
236 (message "warning: %s" str)
237 (sit-for 2))))
238
239 (defun hs-inside-comment-p ()
240 "Returns non-nil if point is inside a comment, otherwise nil.
241 Actually, for multi-line-able comments, returns a list containing
242 the buffer position of the start and the end of the comment."
243 ;; is it single-line-only or multi-line-able?
244 (save-excursion
245 (let ((p (point))
246 q)
247 (if (string= comment-end "") ; single line
248 (let (found)
249 (beginning-of-line)
250 (setq found (re-search-forward hs-c-start-regexp p t))
251 (and found (not (search-forward "\"" p t))))
252 (re-search-forward hs-c-end-regexp (point-max) 1)
253 (setq q (point))
254 (forward-comment -1)
255 (re-search-forward hs-c-start-regexp (point-max) 1)
256 (if (< (- (point) (length comment-start)) p)
257 (list (match-beginning 0) q))))))
258
259 (defun hs-grok-mode-type ()
260 "Setup variables for new buffers where applicable."
261 (if (and (boundp 'comment-start)
262 (boundp 'comment-end))
263 (progn
264 (setq hs-c-start-regexp (regexp-quote comment-start))
265 (if (string-match " +$" hs-c-start-regexp)
266 (setq hs-c-start-regexp
267 (substring hs-c-start-regexp 0 (1- (match-end 0)))))
268 (setq hs-c-end-regexp (if (string= "" comment-end) "\n"
269 (regexp-quote comment-end)))
270 (if (string-match "^ +" hs-c-end-regexp)
271 (setq hs-c-end-regexp
272 (substring hs-c-end-regexp (match-end 0))))
273 (let ((lookup (assoc major-mode hs-special-modes-alist)))
274 (setq hs-block-start-regexp (or (nth 1 lookup) "\\s\(")
275 hs-block-end-regexp (or (nth 2 lookup) "\\s\)")
276 hs-forward-sexp-func (or (nth 3 lookup) 'forward-sexp))))))
277
278 (defun hs-find-block-beginning ()
279 "Repositions point at block-start. Return point, or nil if top-level."
280 (let (done
281 (here (point))
282 (both-regexps (concat "\\(" hs-block-start-regexp "\\)\\|\\("
283 hs-block-end-regexp "\\)")))
284 (while (and (not done)
285 (re-search-backward both-regexps (point-min) t))
286 (if (match-beginning 1) ; start of start-regexp
287 (setq done (match-beginning 1))
288 (goto-char (match-end 2)) ; end of end-regexp
289 (funcall hs-forward-sexp-func -1)))
290 (goto-char (or done here))
291 done))
292
293 (defmacro hs-life-goes-on (&rest body)
294 "Executes optional BODY iff variable `hs-minor-mode' is non-nil."
295 (list 'if 'hs-minor-mode (cons 'progn body)))
296
297
298 ;;;----------------------------------------------------------------------------
299 ;;; commands
300
301 ;;;###autoload
302 (defun hs-hide-all ()
303 "Hides all top-level blocks, displaying only first and last lines.
304 When done, point is repositioned at the beginning of the line, and
305 hs-hide-hooks is called. See documentation for `run-hooks'."
306 (interactive)
307 (hs-life-goes-on
308 (message "hiding all blocks ...")
309 (save-excursion
310 (hs-flag-region (point-min) (point-max) ?\n) ; eliminate weirdness
311 (goto-char (point-min))
312 (let ((count 0)
313 (top-level-re (concat "^" hs-block-start-regexp)))
314 (while (progn
315 (forward-comment (buffer-size))
316 (re-search-forward top-level-re (point-max) t))
317 (goto-char (match-beginning 0))
318 (hs-hide-block-at-point t)
319 (message "hiding ... %d" (setq count (1+ count)))))
320 (hs-safety-is-job-n))
321 (beginning-of-line)
322 (message "hiding all blocks ... done")
323 (run-hooks 'hs-hide-hooks)))
324
325 (defun hs-show-all ()
326 "Shows all top-level blocks.
327 When done, point is unchanged, and hs-show-hooks is called. See
328 documentation for `run-hooks'."
329 (interactive)
330 (hs-life-goes-on
331 (message "showing all blocks ...")
332 (hs-flag-region (point-min) (point-max) ?\n)
333 (message "showing all blocks ... done")
334 (run-hooks 'hs-show-hooks)))
335
336 ;;;###autoload
337 (defun hs-hide-block (&optional end)
338 "Selects a block and hides it. With prefix arg, reposition at end.
339 Block is defined as a sexp for lispish modes, mode-specific otherwise.
340 Comments are blocks, too. Upon completion, point is at repositioned and
341 hs-hide-hooks is called. See documentation for `run-hooks'."
342 (interactive "P")
343 (hs-life-goes-on
344 (let ((c-reg (hs-inside-comment-p)))
345 (if c-reg
346 (cond ((string= comment-end "")
347 (message "can't hide a single-line comment"))
348 ((< (count-lines (car c-reg) (nth 1 c-reg)) 2)
349 (message "not enougn comment lines to hide"))
350 (t
351 (goto-char (nth 1 c-reg))
352 (forward-line -1)
353 (hs-flag-region (car c-reg) (point) ?\C-m)
354 (goto-char (if end (nth 1 c-reg) (car c-reg)))
355 (hs-safety-is-job-n)
356 (run-hooks 'hs-hide-hooks)))
357 (if (or (looking-at hs-block-start-regexp)
358 (hs-find-block-beginning))
359 (progn
360 (hs-hide-block-at-point end)
361 (hs-safety-is-job-n)
362 (run-hooks 'hs-hide-hooks)))))))
363
364 (defun hs-show-block (&optional end)
365 "Selects a block and shows it. With prefix arg, reposition at end.
366 Upon completion, point is repositioned hs-show-hooks are called. See
367 documetation for `hs-hide-block' and `run-hooks'."
368 (interactive "P")
369 (hs-life-goes-on
370 (let ((c-reg (hs-inside-comment-p)))
371 (if c-reg
372 (cond ((string= comment-end "")
373 (message "already looking at the entire comment"))
374 (t
375 (hs-flag-region (car c-reg) (nth 1 c-reg) ?\n)
376 (goto-char (if end (nth 1 c-reg) (car c-reg)))))
377 (if (or (looking-at hs-block-start-regexp)
378 (hs-find-block-beginning))
379 (progn
380 (hs-show-block-at-point end)
381 (hs-safety-is-job-n)
382 (run-hooks 'hs-show-hooks)))))))
383
384 (defun hs-show-region (beg end)
385 "Shows all lines from BEG to END, without doing any block analysis.
386 Note: hs-show-region is intended for use when when hs-show-block signals
387 `unbalanced parentheses' and so is an emergency measure only. You may
388 become very confused if you use this command indiscriminately."
389 (interactive "r")
390 (hs-life-goes-on
391 (hs-flag-region beg end ?\n)
392 (hs-safety-is-job-n)
393 (run-hooks 'hs-show-hooks)))
394
395 ;;;###autoload
396 (defun hs-minor-mode (&optional arg)
397 "Toggle hideshow minor mode.
398 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise.
399 When hideshow minor mode is on, the menu bar is augmented with hideshow
400 commands and the hideshow commands are enabled. The variables\n
401 \tselective-display\n\tselective-display-ellipses\n
402 are set to t. Lastly, the hooks set in hs-minor-mode-hook are called.
403 See documentation for `run-hooks'.\n
404 Turning hideshow minor mode off reverts the menu bar and the
405 variables to default values and disables the hideshow commands."
406 (interactive "P")
407 (setq hs-minor-mode
408 (if (null arg)
409 (not hs-minor-mode)
410 (> (prefix-numeric-value arg) 0)))
411 (if hs-minor-mode
412 (progn
413 (if (eq hs-emacs-type 'lucid)
414 (progn
415 (set-buffer-menubar (copy-sequence current-menubar))
416 (add-menu nil (car hs-menu-bar) (cdr hs-menu-bar))))
417 (setq selective-display t
418 selective-display-ellipses t)
419 (hs-grok-mode-type)
420 (run-hooks 'hs-minor-mode-hook))
421 (if (eq hs-emacs-type 'lucid)
422 (set-buffer-menubar (delete hs-menu-bar current-menubar)))
423 (kill-local-variable 'selective-display)
424 (kill-local-variable 'selective-display-ellipses)))
425
426
427 ;;;----------------------------------------------------------------------------
428 ;;; load-time setup routines
429
430 ;; which emacs being used?
431 (setq hs-emacs-type
432 (if (string-match "XEmacs\\|Lucid" emacs-version)
433 'lucid
434 'fsf))
435
436 ;; keymaps and menus
437 (if (not hs-minor-mode-map)
438 (setq hs-minor-mode-map (make-sparse-keymap))
439 ;; XEmacs: need this for the change in add-minor-mode
440 (fset 'hs-minor-mode-map hs-minor-mode-map)
441 (cond
442 ((eq hs-emacs-type 'lucid)
443 (setq hs-menu-bar ; build top down for lucid
444 '("hideshow"
445 ["Hide Block" hs-hide-block t]
446 ["Show Block" hs-show-block t]
447 ["Hide All" hs-hide-all t]
448 ["Show All" hs-show-all t]
449 ["Show Region" hs-show-region t])))
450 (t ; build bottom up for others
451 (define-key hs-minor-mode-map [menu-bar hideshow]
452 (cons "hideshow" (make-sparse-keymap "hideshow")))
453 (define-key hs-minor-mode-map [menu-bar hideshow hs-show-region]
454 '("Show Region" . hs-show-region))
455 (define-key hs-minor-mode-map [menu-bar hideshow hs-show-all]
456 '("Show All" . hs-show-all))
457 (define-key hs-minor-mode-map [menu-bar hideshow hs-hide-all]
458 '("Hide All" . hs-hide-all))
459 (define-key hs-minor-mode-map [menu-bar hideshow hs-show-block]
460 '("Show Block" . hs-show-block))
461 (define-key hs-minor-mode-map [menu-bar hideshow hs-hide-block]
462 '("Hide Block" . hs-hide-block)))))
463
464 ;; some housekeeping
465 ;(or (assq 'hs-minor-mode minor-mode-map-alist)
466 ; (setq minor-mode-map-alist
467 ; (cons (cons 'hs-minor-mode hs-minor-mode-map)
468 ; minor-mode-map-alist)))
469 ;(or (assq 'hs-minor-mode minor-mode-alist)
470 ; (setq minor-mode-alist (append minor-mode-alist
471 ; (list '(hs-minor-mode " hs")))))
472 ;; XEmacs: do it right.
473 ;;;###autoload
474 (add-minor-mode 'hs-minor-mode " hs" 'hs-minor-mode-map)
475
476 ;; make some variables buffer-local
477 (make-variable-buffer-local 'hs-minor-mode)
478 (make-variable-buffer-local 'hs-c-start-regexp)
479 (make-variable-buffer-local 'hs-c-end-regexp)
480 (make-variable-buffer-local 'hs-block-start-regexp)
481 (make-variable-buffer-local 'hs-block-end-regexp)
482 (make-variable-buffer-local 'hs-forward-sexp-func)
483 (put 'hs-minor-mode 'permanent-local t)
484 (put 'hs-c-start-regexp 'permanent-local t)
485 (put 'hs-c-end-regexp 'permanent-local t)
486 (put 'hs-block-start-regexp 'permanent-local t)
487 (put 'hs-block-end-regexp 'permanent-local t)
488 (put 'hs-forward-sexp-func 'permanent-local t)
489
490
491 ;;;----------------------------------------------------------------------------
492 ;;; that's it
493
494 (provide 'hideshow)
495
496 ;;; hideshow.el ends here