hideshow.el 39 KB

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