hideshow.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. (make-variable-buffer-local 'hs-block-end-regexp)
  367. (defvar hs-forward-sexp-func 'forward-sexp
  368. "Function used to do a `forward-sexp'.
  369. Should change for Algol-ish modes. For single-character block
  370. delimiters -- ie, the syntax table regexp for the character is
  371. either `(' or `)' -- `hs-forward-sexp-func' would just be
  372. `forward-sexp'. For other modes such as simula, a more specialized
  373. function is necessary.")
  374. (make-variable-buffer-local 'hs-forward-sexp-func)
  375. (defvar hs-adjust-block-beginning nil
  376. "Function used to tweak the block beginning.
  377. The block is hidden from the position returned by this function,
  378. as opposed to hiding it from the position returned when searching
  379. 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.
  384. It is called with a single argument ARG which is the position in
  385. buffer after the block beginning.
  386. It should return the position from where we should start hiding.
  387. It should not move the point.
  388. See `hs-c-like-adjust-block-beginning' for an example of using this.")
  389. (make-variable-buffer-local 'hs-adjust-block-beginning)
  390. (defvar hs-headline nil
  391. "Text of the line where a hidden block begins, set during isearch.
  392. You can display this in the mode line by adding the symbol `hs-headline'
  393. to the variable `mode-line-format'. For example,
  394. (unless (memq 'hs-headline mode-line-format)
  395. (setq mode-line-format
  396. (append '(\"-\" hs-headline) mode-line-format)))
  397. Note that `mode-line-format' is buffer-local.")
  398. ;;---------------------------------------------------------------------------
  399. ;; support functions
  400. (defun hs-discard-overlays (from to)
  401. "Delete hideshow overlays in region defined by FROM and TO.
  402. Skip \"internal\" overlays if `hs-allow-nesting' is non-nil."
  403. (when (< to from)
  404. (setq from (prog1 to (setq to from))))
  405. (if hs-allow-nesting
  406. (let (ov)
  407. (while (> to (setq from (next-overlay-change from)))
  408. (when (setq ov (hs-overlay-at from))
  409. (setq from (overlay-end ov))
  410. (delete-overlay ov))))
  411. (dolist (ov (overlays-in from to))
  412. (when (overlay-get ov 'hs)
  413. (delete-overlay ov)))))
  414. (defun hs-make-overlay (b e kind &optional b-offset e-offset)
  415. "Return a new overlay in region defined by B and E with type KIND.
  416. KIND is either `code' or `comment'. Optional fourth arg B-OFFSET
  417. when added to B specifies the actual buffer position where the block
  418. begins. Likewise for optional fifth arg E-OFFSET. If unspecified
  419. they are taken to be 0 (zero). The following properties are set
  420. in the overlay: 'invisible 'hs 'hs-b-offset 'hs-e-offset. Also,
  421. depending on variable `hs-isearch-open', the following properties may
  422. be present: 'isearch-open-invisible 'isearch-open-invisible-temporary.
  423. If variable `hs-set-up-overlay' is non-nil it should specify a function
  424. to call with the newly initialized overlay."
  425. (unless b-offset (setq b-offset 0))
  426. (unless e-offset (setq e-offset 0))
  427. (let ((ov (make-overlay b e))
  428. (io (if (eq 'block hs-isearch-open)
  429. ;; backward compatibility -- `block'<=>`code'
  430. 'code
  431. hs-isearch-open)))
  432. (overlay-put ov 'invisible 'hs)
  433. (overlay-put ov 'hs kind)
  434. (overlay-put ov 'hs-b-offset b-offset)
  435. (overlay-put ov 'hs-e-offset e-offset)
  436. (when (or (eq io t) (eq io kind))
  437. (overlay-put ov 'isearch-open-invisible 'hs-isearch-show)
  438. (overlay-put ov 'isearch-open-invisible-temporary
  439. 'hs-isearch-show-temporary))
  440. (when hs-set-up-overlay (funcall hs-set-up-overlay ov))
  441. ov))
  442. (defun hs-isearch-show (ov)
  443. "Delete overlay OV, and set `hs-headline' to nil.
  444. This function is meant to be used as the `isearch-open-invisible'
  445. property of an overlay."
  446. (setq hs-headline nil)
  447. (delete-overlay ov))
  448. (defun hs-isearch-show-temporary (ov hide-p)
  449. "Hide or show overlay OV, and set `hs-headline', all depending on HIDE-P.
  450. If HIDE-P is non-nil, `hs-headline' is set to nil and overlay OV is hidden.
  451. Otherwise, `hs-headline' is set to the line of text at the head of OV, and
  452. OV is shown.
  453. This function is meant to be used as the `isearch-open-invisible-temporary'
  454. property of an overlay."
  455. (setq hs-headline
  456. (if hide-p
  457. nil
  458. (or hs-headline
  459. (let ((start (overlay-start ov)))
  460. (buffer-substring
  461. (save-excursion (goto-char start)
  462. (beginning-of-line)
  463. (skip-chars-forward " \t")
  464. (point))
  465. start)))))
  466. (force-mode-line-update)
  467. ;; handle `display' property specially
  468. (let (value)
  469. (if hide-p
  470. (when (setq value (overlay-get ov 'hs-isearch-display))
  471. (overlay-put ov 'display value)
  472. (overlay-put ov 'hs-isearch-display nil))
  473. (when (setq value (overlay-get ov 'display))
  474. (overlay-put ov 'hs-isearch-display value)
  475. (overlay-put ov 'display nil))))
  476. (overlay-put ov 'invisible (and hide-p 'hs)))
  477. (defun hs-looking-at-block-start-p ()
  478. "Return non-nil if the point is at the block start."
  479. (and (looking-at hs-block-start-regexp)
  480. (save-match-data (not (nth 8 (syntax-ppss))))))
  481. (defun hs-forward-sexp (match-data arg)
  482. "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
  483. Original match data is restored upon return."
  484. (save-match-data
  485. (set-match-data match-data)
  486. (goto-char (match-beginning hs-block-start-mdata-select))
  487. (funcall hs-forward-sexp-func arg)))
  488. (defun hs-hide-comment-region (beg end &optional repos-end)
  489. "Hide a region from BEG to END, marking it as a comment.
  490. Optional arg REPOS-END means reposition at end."
  491. (let ((beg-eol (progn (goto-char beg) (line-end-position)))
  492. (end-eol (progn (goto-char end) (line-end-position))))
  493. (hs-discard-overlays beg-eol end-eol)
  494. (hs-make-overlay beg-eol end-eol 'comment beg end))
  495. (goto-char (if repos-end end beg)))
  496. (defun hs-hide-block-at-point (&optional end comment-reg)
  497. "Hide block if on block beginning.
  498. Optional arg END means reposition at end.
  499. Optional arg COMMENT-REG is a list of the form (BEGIN END) and
  500. specifies the limits of the comment, or nil if the block is not
  501. a comment.
  502. The block beginning is adjusted by `hs-adjust-block-beginning'
  503. and then further adjusted to be at the end of the line."
  504. (if comment-reg
  505. (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
  506. (when (hs-looking-at-block-start-p)
  507. (let ((mdata (match-data t))
  508. (header-end (match-end 0))
  509. p q ov)
  510. ;; `p' is the point at the end of the block beginning, which
  511. ;; may need to be adjusted
  512. (save-excursion
  513. (if hs-adjust-block-beginning
  514. (goto-char (funcall hs-adjust-block-beginning
  515. header-end))
  516. (goto-char header-end))
  517. (setq p (line-end-position)))
  518. ;; `q' is the point at the end of the block
  519. (hs-forward-sexp mdata 1)
  520. (setq q (if (looking-back hs-block-end-regexp)
  521. (match-beginning 0)
  522. (point)))
  523. (when (and (< p q) (> (count-lines p q) 1))
  524. (cond ((and hs-allow-nesting (setq ov (hs-overlay-at p)))
  525. (delete-overlay ov))
  526. ((not hs-allow-nesting)
  527. (hs-discard-overlays p q)))
  528. (hs-make-overlay p q 'code (- header-end p)))
  529. (goto-char (if end q (min p header-end)))))))
  530. (defun hs-inside-comment-p ()
  531. "Return non-nil if point is inside a comment, otherwise nil.
  532. Actually, return a list containing the buffer position of the start
  533. and the end of the comment. A comment block can be hidden only if on
  534. its starting line there is only whitespace preceding the actual comment
  535. beginning. If we are inside of a comment but this condition is not met,
  536. we return a list having a nil as its car and the end of comment position
  537. as cdr."
  538. (save-excursion
  539. ;; the idea is to look backwards for a comment start regexp, do a
  540. ;; forward comment, and see if we are inside, then extend
  541. ;; forward and backward as long as we have comments
  542. (let ((q (point)))
  543. (skip-chars-forward "[:blank:]")
  544. (when (or (looking-at hs-c-start-regexp)
  545. (re-search-backward hs-c-start-regexp (point-min) t))
  546. ;; first get to the beginning of this comment...
  547. (while (and (not (bobp))
  548. (= (point) (progn (forward-comment -1) (point))))
  549. (forward-char -1))
  550. ;; ...then extend backwards
  551. (forward-comment (- (buffer-size)))
  552. (skip-chars-forward " \t\n\f")
  553. (let ((p (point))
  554. (hidable t))
  555. (beginning-of-line)
  556. (unless (looking-at (concat "[ \t]*" hs-c-start-regexp))
  557. ;; we are in this situation: (example)
  558. ;; (defun bar ()
  559. ;; (foo)
  560. ;; ) ; comment
  561. ;; ^
  562. ;; the point was here before doing (beginning-of-line)
  563. ;; here we should advance till the next comment which
  564. ;; eventually has only white spaces preceding it on the same
  565. ;; line
  566. (goto-char p)
  567. (forward-comment 1)
  568. (skip-chars-forward " \t\n\f")
  569. (setq p (point))
  570. (while (and (< (point) q)
  571. (> (point) p)
  572. (not (looking-at hs-c-start-regexp)))
  573. ;; avoid an infinite cycle
  574. (setq p (point))
  575. (forward-comment 1)
  576. (skip-chars-forward " \t\n\f"))
  577. (when (or (not (looking-at hs-c-start-regexp))
  578. (> (point) q))
  579. ;; we cannot hide this comment block
  580. (setq hidable nil)))
  581. ;; goto the end of the comment
  582. (forward-comment (buffer-size))
  583. (skip-chars-backward " \t\n\f")
  584. (end-of-line)
  585. (when (>= (point) q)
  586. (list (and hidable p) (point))))))))
  587. (defun hs-grok-mode-type ()
  588. "Set up hideshow variables for new buffers.
  589. If `hs-special-modes-alist' has information associated with the
  590. current buffer's major mode, use that.
  591. Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
  592. function; and adjust-block-beginning function."
  593. (if (and (boundp 'comment-start)
  594. (boundp 'comment-end)
  595. comment-start comment-end)
  596. (let* ((lookup (assoc major-mode hs-special-modes-alist))
  597. (start-elem (or (nth 1 lookup) "\\s(")))
  598. (if (listp start-elem)
  599. ;; handle (START-REGEXP MDATA-SELECT)
  600. (setq hs-block-start-regexp (car start-elem)
  601. hs-block-start-mdata-select (cadr start-elem))
  602. ;; backwards compatibility: handle simple START-REGEXP
  603. (setq hs-block-start-regexp start-elem
  604. hs-block-start-mdata-select 0))
  605. (setq hs-block-end-regexp (or (nth 2 lookup) "\\s)")
  606. hs-c-start-regexp (or (nth 3 lookup)
  607. (let ((c-start-regexp
  608. (regexp-quote comment-start)))
  609. (if (string-match " +$" c-start-regexp)
  610. (substring c-start-regexp
  611. 0 (1- (match-end 0)))
  612. c-start-regexp)))
  613. hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp)
  614. hs-adjust-block-beginning (nth 5 lookup)))
  615. (setq hs-minor-mode nil)
  616. (error "%s Mode doesn't support Hideshow Minor Mode"
  617. (format-mode-line mode-name))))
  618. (defun hs-find-block-beginning ()
  619. "Reposition point at block-start.
  620. Return point, or nil if original point was not in a block."
  621. (let ((done nil)
  622. (here (point)))
  623. ;; look if current line is block start
  624. (if (hs-looking-at-block-start-p)
  625. (point)
  626. ;; look backward for the start of a block that contains the cursor
  627. (while (and (re-search-backward hs-block-start-regexp nil t)
  628. ;; go again if in a comment or a string
  629. (or (save-match-data (nth 8 (syntax-ppss)))
  630. (not (setq done
  631. (< here (save-excursion
  632. (hs-forward-sexp (match-data t) 1)
  633. (point))))))))
  634. (if done
  635. (point)
  636. (goto-char here)
  637. nil))))
  638. (defun hs-hide-level-recursive (arg minp maxp)
  639. "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
  640. (when (hs-find-block-beginning)
  641. (setq minp (1+ (point)))
  642. (funcall hs-forward-sexp-func 1)
  643. (setq maxp (1- (point))))
  644. (unless hs-allow-nesting
  645. (hs-discard-overlays minp maxp))
  646. (goto-char minp)
  647. (while (progn
  648. (forward-comment (buffer-size))
  649. (and (< (point) maxp)
  650. (re-search-forward hs-block-start-regexp maxp t)))
  651. (when (save-match-data
  652. (not (nth 8 (syntax-ppss)))) ; not inside comments or strings
  653. (if (> arg 1)
  654. (hs-hide-level-recursive (1- arg) minp maxp)
  655. (goto-char (match-beginning hs-block-start-mdata-select))
  656. (hs-hide-block-at-point t))))
  657. (goto-char maxp))
  658. (defmacro hs-life-goes-on (&rest body)
  659. "Evaluate BODY forms if variable `hs-minor-mode' is non-nil.
  660. In the dynamic context of this macro, `inhibit-point-motion-hooks'
  661. and `case-fold-search' are both t."
  662. `(when hs-minor-mode
  663. (let ((inhibit-point-motion-hooks t)
  664. (case-fold-search t))
  665. ,@body)))
  666. (put 'hs-life-goes-on 'edebug-form-spec '(&rest form))
  667. (defun hs-overlay-at (position)
  668. "Return hideshow overlay at POSITION, or nil if none to be found."
  669. (let ((overlays (overlays-at position))
  670. ov found)
  671. (while (and (not found) (setq ov (car overlays)))
  672. (setq found (and (overlay-get ov 'hs) ov)
  673. overlays (cdr overlays)))
  674. found))
  675. (defun hs-already-hidden-p ()
  676. "Return non-nil if point is in an already-hidden block, otherwise nil."
  677. (save-excursion
  678. (let ((c-reg (hs-inside-comment-p)))
  679. (if (and c-reg (nth 0 c-reg))
  680. ;; point is inside a comment, and that comment is hidable
  681. (goto-char (nth 0 c-reg))
  682. (end-of-line)
  683. (when (and (not c-reg)
  684. (hs-find-block-beginning)
  685. (hs-looking-at-block-start-p))
  686. ;; point is inside a block
  687. (goto-char (match-end 0)))))
  688. (end-of-line)
  689. (hs-overlay-at (point))))
  690. ;; This function is not used anymore (Bug#700).
  691. (defun hs-c-like-adjust-block-beginning (initial)
  692. "Adjust INITIAL, the buffer position after `hs-block-start-regexp'.
  693. Actually, point is never moved; a new position is returned that is
  694. the end of the C-function header. This adjustment function is meant
  695. to be assigned to `hs-adjust-block-beginning' for C-like modes."
  696. (save-excursion
  697. (goto-char (1- initial))
  698. (forward-comment (- (buffer-size)))
  699. (point)))
  700. ;;---------------------------------------------------------------------------
  701. ;; commands
  702. (defun hs-hide-all ()
  703. "Hide all top level blocks, displaying only first and last lines.
  704. Move point to the beginning of the line, and run the normal hook
  705. `hs-hide-hook'. See documentation for `run-hooks'.
  706. If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
  707. (interactive)
  708. (hs-life-goes-on
  709. (save-excursion
  710. (unless hs-allow-nesting
  711. (hs-discard-overlays (point-min) (point-max)))
  712. (goto-char (point-min))
  713. (let ((spew (make-progress-reporter "Hiding all blocks..."
  714. (point-min) (point-max)))
  715. (re (concat "\\("
  716. hs-block-start-regexp
  717. "\\)"
  718. (if hs-hide-comments-when-hiding-all
  719. (concat "\\|\\("
  720. hs-c-start-regexp
  721. "\\)")
  722. ""))))
  723. (while (progn
  724. (unless hs-hide-comments-when-hiding-all
  725. (forward-comment (point-max)))
  726. (re-search-forward re (point-max) t))
  727. (if (match-beginning 1)
  728. ;; we have found a block beginning
  729. (progn
  730. (goto-char (match-beginning 1))
  731. (if hs-hide-all-non-comment-function
  732. (funcall hs-hide-all-non-comment-function)
  733. (hs-hide-block-at-point t)))
  734. ;; found a comment, probably
  735. (let ((c-reg (hs-inside-comment-p)))
  736. (when (and c-reg (car c-reg))
  737. (if (> (count-lines (car c-reg) (nth 1 c-reg)) 1)
  738. (hs-hide-block-at-point t c-reg)
  739. (goto-char (nth 1 c-reg))))))
  740. (progress-reporter-update spew (point)))
  741. (progress-reporter-done spew)))
  742. (beginning-of-line)
  743. (run-hooks 'hs-hide-hook)))
  744. (defun hs-show-all ()
  745. "Show everything then run `hs-show-hook'. See `run-hooks'."
  746. (interactive)
  747. (hs-life-goes-on
  748. (message "Showing all blocks ...")
  749. (let ((hs-allow-nesting nil))
  750. (hs-discard-overlays (point-min) (point-max)))
  751. (message "Showing all blocks ... done")
  752. (run-hooks 'hs-show-hook)))
  753. (defun hs-hide-block (&optional end)
  754. "Select a block and hide it. With prefix arg, reposition at END.
  755. Upon completion, point is repositioned and the normal hook
  756. `hs-hide-hook' is run. See documentation for `run-hooks'."
  757. (interactive "P")
  758. (hs-life-goes-on
  759. (let ((c-reg (hs-inside-comment-p)))
  760. (cond
  761. ((and c-reg (or (null (nth 0 c-reg))
  762. (<= (count-lines (car c-reg) (nth 1 c-reg)) 1)))
  763. (message "(not enough comment lines to hide)"))
  764. ((or c-reg
  765. (hs-looking-at-block-start-p)
  766. (hs-find-block-beginning))
  767. (hs-hide-block-at-point end c-reg)
  768. (run-hooks 'hs-hide-hook))))))
  769. (defun hs-show-block (&optional end)
  770. "Select a block and show it.
  771. With prefix arg, reposition at END. Upon completion, point is
  772. repositioned and the normal hook `hs-show-hook' is run.
  773. See documentation for functions `hs-hide-block' and `run-hooks'."
  774. (interactive "P")
  775. (hs-life-goes-on
  776. (or
  777. ;; first see if we have something at the end of the line
  778. (let ((ov (hs-overlay-at (line-end-position)))
  779. (here (point)))
  780. (when ov
  781. (goto-char
  782. (cond (end (overlay-end ov))
  783. ((eq 'comment (overlay-get ov 'hs)) here)
  784. (t (+ (overlay-start ov) (overlay-get ov 'hs-b-offset)))))
  785. (delete-overlay ov)
  786. t))
  787. ;; not immediately obvious, look for a suitable block
  788. (let ((c-reg (hs-inside-comment-p))
  789. p q)
  790. (cond (c-reg
  791. (when (car c-reg)
  792. (setq p (car c-reg)
  793. q (cadr c-reg))))
  794. ((and (hs-find-block-beginning)
  795. ;; ugh, fresh match-data
  796. (hs-looking-at-block-start-p))
  797. (setq p (point)
  798. q (progn (hs-forward-sexp (match-data t) 1) (point)))))
  799. (when (and p q)
  800. (hs-discard-overlays p q)
  801. (goto-char (if end q (1+ p))))))
  802. (run-hooks 'hs-show-hook)))
  803. (defun hs-hide-level (arg)
  804. "Hide all blocks ARG levels below this block.
  805. The hook `hs-hide-hook' is run; see `run-hooks'."
  806. (interactive "p")
  807. (hs-life-goes-on
  808. (save-excursion
  809. (message "Hiding blocks ...")
  810. (hs-hide-level-recursive arg (point-min) (point-max))
  811. (message "Hiding blocks ... done"))
  812. (run-hooks 'hs-hide-hook)))
  813. (defun hs-toggle-hiding ()
  814. "Toggle hiding/showing of a block.
  815. See `hs-hide-block' and `hs-show-block'."
  816. (interactive)
  817. (hs-life-goes-on
  818. (if (hs-already-hidden-p)
  819. (hs-show-block)
  820. (hs-hide-block))))
  821. (defun hs-mouse-toggle-hiding (e)
  822. "Toggle hiding/showing of a block.
  823. This command should be bound to a mouse key.
  824. Argument E is a mouse event used by `mouse-set-point'.
  825. See `hs-hide-block' and `hs-show-block'."
  826. (interactive "@e")
  827. (hs-life-goes-on
  828. (mouse-set-point e)
  829. (hs-toggle-hiding)))
  830. (defun hs-hide-initial-comment-block ()
  831. "Hide the first block of comments in a file.
  832. This can be useful if you have huge RCS logs in those comments."
  833. (interactive)
  834. (hs-life-goes-on
  835. (let ((c-reg (save-excursion
  836. (goto-char (point-min))
  837. (skip-chars-forward " \t\n\f")
  838. (hs-inside-comment-p))))
  839. (when c-reg
  840. (let ((beg (car c-reg)) (end (cadr c-reg)))
  841. ;; see if we have enough comment lines to hide
  842. (when (> (count-lines beg end) 1)
  843. (hs-hide-comment-region beg end)))))))
  844. ;;;###autoload
  845. (define-minor-mode hs-minor-mode
  846. "Minor mode to selectively hide/show code and comment blocks.
  847. With a prefix argument ARG, enable the mode if ARG is positive,
  848. and disable it otherwise. If called from Lisp, enable the mode
  849. if ARG is omitted or nil.
  850. When hideshow minor mode is on, the menu bar is augmented with hideshow
  851. commands and the hideshow commands are enabled.
  852. The value '(hs . t) is added to `buffer-invisibility-spec'.
  853. The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block',
  854. `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also
  855. `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'.
  856. Turning hideshow minor mode off reverts the menu bar and the
  857. variables to default values and disables the hideshow commands.
  858. Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'.
  859. Key bindings:
  860. \\{hs-minor-mode-map}"
  861. :group 'hideshow
  862. :lighter " hs"
  863. :keymap hs-minor-mode-map
  864. (setq hs-headline nil)
  865. (if hs-minor-mode
  866. (progn
  867. (hs-grok-mode-type)
  868. ;; Turn off this mode if we change major modes.
  869. (add-hook 'change-major-mode-hook
  870. 'turn-off-hideshow
  871. nil t)
  872. (easy-menu-add hs-minor-mode-menu)
  873. (set (make-local-variable 'line-move-ignore-invisible) t)
  874. (add-to-invisibility-spec '(hs . t)))
  875. (remove-from-invisibility-spec '(hs . t))
  876. ;; hs-show-all does nothing unless h-m-m is non-nil.
  877. (let ((hs-minor-mode t))
  878. (hs-show-all))))
  879. ;;;###autoload
  880. (defun turn-off-hideshow ()
  881. "Unconditionally turn off `hs-minor-mode'."
  882. (hs-minor-mode -1))
  883. ;;---------------------------------------------------------------------------
  884. ;; that's it
  885. (provide 'hideshow)
  886. ;;; hideshow.el ends here