Debian Codegraph @

folding.el

raw ◀ ▶
1 Debian patch touch this file
1 ;;; folding.el --- A folding-editor-like minor mode.
2
3 ;; This file is not part of Emacs
4
5 ;; Copyright (C) 2000-2025 Jari Aalto
6 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999 Jari Aalto, Anders Lindgren.
7 ;; Copyright (C) 1994 Jari Aalto
8 ;; Copyright (C) 1992, 1993 Jamie Lokier <[email protected]>
9 ;; https://github.com/jlokier
10 ;;
11 ;; Authors: Jamie Lokier <jamie A T imbolc.ucc dt ie>
12 ;; Jari Aalto <jari aalto A T cante dt net>
13 ;; Anders Lindgren <andersl A T csd.uu dt se>
14 ;; Maintainer: Jari Aalto <jari aalto A T cante dt net>
15 ;; Created: 1992
16 ;; Keywords: tools
•17 ;; Package-Version: GENERATED-IN-RULES
18 ;; Vcs-URL: https://github.com/jaalto/project-emacs--folding-mode
19
20 (defconst folding-version-time "2024.0308.0333"
21 "Last edit time in format YYYY.MMDD.HHMM.")
22
23 ;;{{{ GPL
24
25 ;; This file is free software; you can redistribute it and/or modify
26 ;; it under the terms of the GNU General Public License as published by
27 ;; the Free Software Foundation; either version 2, or (at your option)
28 ;; any later version.
29 ;;
30 ;; GNU Emacs is distributed in the hope that it will be useful,
31 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 ;; GNU General Public License for more details.
34 ;;
35 ;; You should have received a copy of the GNU General Public License
36 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
37 ;;
38 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
39
40 ;;}}}
41
42 ;;; Commentary:
43
44 ;;{{{ Introduction
45
46 ;; Preface
47 ;;
48 ;; This package provides a minor mode, compatible with all major
49 ;; editing modes, for folding (hiding) parts of the edited text
50 ;; or program.
51 ;;
52 ;; Folding mode handles a document as a tree, where each branch
53 ;; is bounded by special markers {{{' and }}}'. A branch can be
54 ;; placed inside another branch, creating a complete hierarchical
55 ;; structure. The markers:
56 ;;
57 ;; o Are placed at the beginning of the line. No spaces.
58 ;; o Are prefixed by mode comment and space as needed.
59 ;; For example, in C++ mode, the beginning marker is: "//
60 ;; {{{". See the source code of folding.el and the section
61 ;; "Set some useful default fold marks" for a full listing of
62 ;; the markers. If you need to customize these markers,
63 ;; modify `folding-mode-marks-alist' after loading this
64 ;; package (See the my-folding-load-hook example in this
65 ;; doc).
66 ;;
67 ;; Folding mode can CLOSE a fold, leaving only the initial `{{{'
68 ;; and possibly a comment visible.
69 ;;
70 ;; It can also ENTER a fold, which means that only the current
71 ;; fold will be visible; all text above {{{' and below }}}' will
72 ;; be invisible.
73 ;;
74 ;; Please note that Folding.el is at its best when it can "chunk"
75 ;; large sections of code inside folds. The larger the chunks,
76 ;; the more the usable folding will be. Folding.el is not meant
77 ;; to hide individual functions: you may be better served by
78 ;; other packages like hideshow.el or imenu.el (which can parse
79 ;; the function indexes).
80
81 ;;}}}
82 ;;{{{ Installation
83
84 ;; Installation
85 ;;
86 ;; To install Folding mode, place this file (folding.el) in your
87 ;; Emacs `load-path', such as the directory ~/.emacs.d and
88 ;; optionally byte compile it with `M-x' `byte-compile'.
89 ;;
90 ;; The most efficient way to install folding is to use autoload
91 ;; installation so that folding is loaded only when
92 ;; `folding-mode' is turned on. Add these statements to your
93 ;; Emacs init file. See '(info)Init File' for more information.
94 ;;
95 ;; (autoload 'folding-mode "folding" "Folding mode" t)
96 ;; (autoload 'turn-off-folding-mode "folding" "Folding mode" t)
97 ;; (autoload 'turn-on-folding-mode "folding" "Folding mode" t)
98 ;;
99 ;; If you always use folding, then perhaps you want folding to
100 ;; start automatically when you load a folded file:
101 ;;
102 ;; (if (load "folding" 'nomessage 'noerror)
103 ;; (folding-mode-add-find-file-hook))
104 ;;
105 ;; Add also this at the beginning of file:
106 ;;
107 ;; -*- mode: emacs-lisp; mode: folding; folded-file: t; -*-
108 ;;
109 ;; To activate folding on load without local variables:
110 ::
111 ;; (defun my-folding-mode-activate ()
112 ;; (save-excursion
113 ;; (goto-char (point-min))
114 ;; (when (and (re-search-forward "^.*{{{" nil t)
115 ;; (re-search-forward "^.*}}}" nil t))
116 ;; (turn-on-folding-mode))))
117 ;;
118 ;; (add-hook 'find-file-hook 'my-folding-mode-activate)
119 ;;
120 ;; Folding uses a keymap that conforms with Emacs 19.29 or later.
121 ;; The key bindings are prefixed with "C-c@" instead of old
122 ;; "C-c". To use the old keyboard bindings, add this:
123 ;;
124 ;; (setq folding-default-keys-function
125 ;; 'folding-bind-backward-compatible-keys)
126 ;;
127 ;; Notes
128 ;;
129 ;; The same folding marks can be used in `vim' editor command
130 ;; "set fdm=marker".
131 ;;
132 ;; Uninstallation
133 ;;
134 ;; To remove folding, call `M-x' `folding-uninstall'.
135 ;;
136 ;; To read the manual
137 ;;
138 ;; At any point you can reach the manual with `M-x'
139 ;; `finder-commentary' RET folding RET.
140
141 ;;}}}
142 ;;{{{ DOCUMENTATION
143
144 ;; Compatibility
145 ;;
146 ;; Folding supports following Emacs flavors:
147 ;;
148 ;; Unix Emacs 19.28+ and Win32 Emacs 19.34+
149 ;; Unix XEmacs 19.14+ and Win32 XEmacs 21.0+
150 ;;
151 ;; Compatibility not for old NT Emacs releases
152 ;;
153 ;; NOTE: folding version starting from 2.47 gets around this bug
154 ;; by using adviced kill/yank functions. The advice functions are
155 ;; only instantiated under problematic NT Emacs versions.
156 ;;
157 ;; Windows NT/9x 19.34 - 20.3.1 (i386-*-nt4.0) versions contained
158 ;; a bug which affected using folding. At the time the bug was
159 ;; reported by Trey Jackson <trey A T cs berkeley edu>
160 ;;
161 ;; If you kill folded area and yank it back, the ^M marks are
162 ;; removed for some reason.
163 ;;
164 ;; Before kill
165 ;; ;;{{{ fold...
166 ;;
167 ;; After yank
168 ;; ;;{{{ fold all lines together }}}
169 ;;
170 ;; Related packages or modes
171 ;;
172 ;; Folding.el was designed to be a content organizer and it is most
173 ;; suitable for big files. Sometimes people misunderstand the
174 ;; package's capabilities and try to use folding.el in wrong places,
175 ;; where some other package would do a better job. Trying to wrap
176 ;; individual functions inside fold-marks is not where folding is
177 ;; it's best. Grouping several functions inside a logical fold-block
178 ;; in the other is. So, to choose a best tool for your need,
179 ;; here are some suggestions,:
180 ;;
181 ;; o Navigating between or hiding individual functions -
182 ;; use combination of imenu.el, speedbar.el and
183 ;; hideshow.el
184 ;; o Organizing large blocks - use folding.el
185 ;; o For text, `outline-mode' is more non-intrusive than folding.
186 ;; Look at Emacs NEWS file (`C-x' `n') and you can see beautifully
187 ;; laid out content.
188 ;;
189 ;; Tutorial
190 ;;
191 ;; To start folding mode, give the command: `M-x' `folding-mode'
192 ;; `RET'. The mode line should contain the string "Fld" indicating
193 ;; that folding mode is activated.
194 ;;
195 ;; When loading a document containing fold marks, Folding mode is
196 ;; automatically started and all folds are closed. For example when
197 ;; loading my init file, only the following lines (plus a few lines
198 ;; of comments) are visible:
199 ;;
200 ;; ;;{{{ General...
201 ;; ;;{{{ Keyboard...
202 ;; ;;{{{ Packages...
203 ;; ;;{{{ Major modes...
204 ;; ;;{{{ Minor modes...
205 ;; ;;{{{ Debug...
206 ;;
207 ;; To enter a fold, use `C-c @ >'. To show it without entering,
208 ;; use `C-c @ C-s', which produces this display:
209 ;;
210 ;; ;;{{{ Minor modes
211 ;;
212 ;; ;;{{{ Follow mode...
213 ;; ;;{{{ Font-lock mode...
214 ;; ;;{{{ Folding...
215 ;;
216 ;; ;;}}}
217 ;;
218 ;; To show everything, just as the file would look like if
219 ;; Folding mode hadn't been activated, give the command `M-x'
220 ;; `folding-open-buffer' `RET', normally bound to `C-c' `@'
221 ;; `C-o'. To close all folds and go to the top level, the
222 ;; command `folding-whole-buffer' could be used.
223 ;;
224 ;; Mouse support
225 ;;
226 ;; Folding mode v2.0 introduced mouse support. Folds can be shown
227 ;; or hidden by simply clicking on a fold mark using mouse button
228 ;; 3. The mouse routines have been designed to call the original
229 ;; function bound to button 3 when the user didn't click on a
230 ;; fold mark.
231 ;;
232 ;; The menu
233 ;;
234 ;; A menu is placed in the "Tools" menu. Should no Tools menu exist
235 ;; (Emacs 19.28) the menu will be placed in the menu bar.
236 ;;
237 ;; ISearch
238 ;;
239 ;; When searching using the incremental search (C-s) facilities,
240 ;; folds will be automagically entered and closed.
241 ;;
242 ;; Problems
243 ;;
244 ;; Uneven fold marks
245 ;;
246 ;; Oops, I just deleted some text, and a fold mark got deleted!
247 ;; What should I do? Trust me, you will eventually do this
248 ;; sometime. the easiest way is to open the buffer using
249 ;; `folding-open-buffer' (C-c @ C-o) and add the fold mark by
250 ;; hand. To find mismatching fold marks, the package `occur' is
251 ;; useful. The command:
252 ;;
253 ;; M-x occur RET {{{\|}}} RET
254 ;;
255 ;; will extract all lines containing folding marks and present
256 ;; them in a separate buffer.
257 ;;
258 ;; Even though all folding marks are correct, Folding mode
259 ;; sometimes gets confused, especially when entering and leaving
260 ;; folds very often. To get it back on track, press C-g a few
261 ;; times and give the command `folding-open-buffer' (C-c @ C-o).
262 ;;
263 ;; Fold must have a label
264 ;;
265 ;; When you make a fold, be sure to write some text for the name
266 ;; of the fold, otherwise there may be an error "extraneous fold
267 ;; mark..." Write like this:
268 ;;
269 ;; ;;{{{ Note
270 ;; ;;}}}
271 ;;
272 ;; instead of
273 ;;
274 ;; ;;{{{
275 ;; ;;}}}
276 ;;
277 ;; folding-whole-buffer doesn't fold whole buffer
278 ;;
279 ;; If you call commands `folding-open-buffer' and
280 ;; `folding-whole-buffer' and notice that there are open fold
281 ;; sections in the buffer, then you have mismatch of folds
282 ;; somewhere. Run ` M-x' `occur' and type regexp `{{{\|}}}' to
283 ;; check where is the extra open or closing fold mark.
284 ;;
285 ;; Folding and outline modes
286 ;;
287 ;; Folding mode is not the same as Outline mode, a major and
288 ;; minor mode which is part of the Emacs distribution. The two
289 ;; packages do, however, resemble each other very much. The main
290 ;; differences between the two packages are:
291 ;;
292 ;; o Folding mode uses explicit marks, `{{{' and `}}}', to
293 ;; mark the beginning and the end of a branch.
294 ;; Outline, on the other other hand, tries to use already
295 ;; existing marks, like the `\section' string in a TeX
296 ;; document.
297 ;;
298 ;; o Outline mode has no end marker which means that it is
299 ;; impossible for text to follow a sub-branch.
300 ;;
301 ;; o Folding mode use the same markers for branches on all depths,
302 ;; Outline mode requires that marks should be longer the
303 ;; further, down in the tree you go, e.g `\chap', \section',
304 ;; `\subsection', `\subsubsection'. This is needed to
305 ;; distinguish the next mark at the current or higher levels
306 ;; from a sub-branch, a problem caused by the lack of
307 ;; end-markers.
308 ;;
309 ;; o Folding mode has mouse support, you can navigate through a
310 ;; folded document by clicking on fold marks. (The XEmacs version
311 ;; of Outline mode has mouse support.)
312 ;;
313 ;; o The Isearch facilities of Folding is capable of
314 ;; automatically to open folds. Under Outline, the the entire
315 ;; document must be opened prior isearch.
316 ;;
317 ;; In conclusion, Outline mode is useful when the document being
318 ;; edited contains natural markers, like LaTeX. When writing code
319 ;; natural markers are hard to find, except if you're happy with
320 ;; one function per fold.
321 ;;
322 ;; Future development ideas
323 ;;
324 ;; The plan was from the beginning to rewrite the entire package.
325 ;; Including replacing the core of the program, written using
326 ;; old Emacs technology (selective display), and replace it with
327 ;; modern equivalences, like overlays or text-properties for
328 ;; Emacs and extents for XEmacs.
329 ;;
330 ;; It is not likely that any of this will come true considering
331 ;; the time required to rewrite the core of the package. Since
332 ;; the package, in it's current state, is much more powerful than
333 ;; the original, it would be appropriate to write such package
334 ;; from scratch instead of doing surgery on this one.
335
336 ;;}}}
337
338 ;;{{{ Customization
339
340 ;; Customization: general
341 ;;
342 ;; The behavior of Folding mode is controlled mainly by a set of
343 ;; Emacs Lisp variables. This section will discuss the most
344 ;; useful ones, for more details please see the code. The
345 ;; descriptions below assumes that you know a bit about how to
346 ;; use simple Emacs Lisp and knows how to edit ~/.emacs, your
347 ;; init file.
348 ;;
349 ;; Customization: hooks
350 ;;
351 ;; The normal procedure when customizing a package is to write a
352 ;; function doing the customization. The function is then added
353 ;; to a hook which is called at an appropriate time. (Please see
354 ;; the example section below.) The following hooks are
355 ;; available:
356 ;;
357 ;; o `folding-mode-hook'
358 ;; Called when folding mode is activated.
359 ;; o `<major mode>-folding-hook'
360 ;; Called when starting folding mode in a buffer with major
361 ;; mode set to <major mode>. (e.g. When editing C code
362 ;; the hook `c-mode-folding-hook' is called.)
363 ;; o `folding-load-hook'
364 ;; Called when folding mode is loaded into Emacs.
365 ;;
366 ;; Customization: The Mouse
367 ;;
368 ;; The variable `folding-behave-table' contains the actions which
369 ;; should be performed when the user clicks on an open fold, a
370 ;; closed fold etc. For example, if you prefer to `enter' a fold
371 ;; rather than `open' it you should rebind this variable.
372 ;;
373 ;; The variable `folding-default-mouse-keys-function' contains
374 ;; the name of the function used to bind your mouse keys. To use
375 ;; your own mouse bindings, create a function, say
376 ;; `my-folding-bind-mouse', and set this variable to it.
377 ;;
378 ;; Customization: Keymaps
379 ;;
380 ;; When Emacs 19.29 was released, the keymap was divided into
381 ;; strict parts. (This division existed before, but a lot of
382 ;; packages, even the ones delivered with Emacs, ignored them.)
383 ;;
384 ;; C-c <letter> -- Reserved for the users private keymap.
385 ;; C-c C-<letter> -- Major mode. (Some other keys are
386 ;; reserved as well.)
387 ;; C-c <Punctuation Char> <Whatever>
388 ;; -- Reserved for minor modes.
389 ;;
390 ;; The reason why `C-c@' was chosen as the default prefix is that
391 ;; it is used by outline-minor-mode. It is not likely that few
392 ;; people will try to use folding and outline at the same time.
393 ;;
394 ;; However, old key bindings have been kept if possible. The
395 ;; variable `folding-default-keys-function' specifies which
396 ;; function should be called to bind the keys. There are various
397 ;; function to choose from how user can select the keybindings.
398 ;; To use the old key bindings, add the following line to your
399 ;; init file:
400 ;;
401 ;; (setq folding-default-keys-function
402 ;; 'folding-bind-backward-compatible-keys)
403 ;;
404 ;; To define keys similar to the keys used by Outline mode, use:
405 ;;
406 ;; (setq folding-default-keys-function
407 ;; 'folding-bind-outline-compatible-keys)
408 ;;
409 ;; Customization: adding new major modes
410 ;;
411 ;; To add fold marks for a new major mode, use the function
412 ;; `folding-add-to-marks-list'. The command also replaces
413 ;; existing marks. An example:
414 ;;
415 ;; (folding-add-to-marks-list
416 ;; 'c-mode "/* {{{ " "/* }}} */" " */" t)
417 ;;
418 ;; Customization: ISearch
419 ;;
420 ;; If you don't like the extension folding.el applies to isearch,
421 ;; set the variable `folding-isearch-install' to nil before
422 ;; loading this package.
423
424 ;;}}}
425 ;;{{{ Examples
426
427 ;; Example: personal setup
428 ;;
429 ;; To define your own key binding instead of using the standard
430 ;; ones, you can do like this:
431 ;;
432 ;; (setq folding-mode-prefix-key "\C-c")
433 ;; ;;
434 ;; (setq folding-default-keys-function
435 ;; '(folding-bind-backward-compatible-keys))
436 ;; ;;
437 ;; (setq folding-load-hook 'my-folding-load-hook)
438 ;;
439 ;;
440 ;; (defun my-folding-load-hook ()
441 ;; "Folding setup."
442 ;;
443 ;; (folding-install) ;; just to be sure
444 ;;
445 ;; ;; ............................................... markers ...
446 ;;
447 ;; ;; Change text-mode fold marks. Handy for quick
448 ;; ;; sh/perl/awk code
449 ;;
450 ;; (defvar folding-mode-marks-alist)
451 ;;
452 ;; (let* ((ptr (assq 'text-mode folding-mode-marks-alist)))
453 ;; (setcdr ptr (list "# {{{" "# }}}")))
454 ;;
455 ;; ;; ........................................ bindings ...
456 ;;
457 ;; ;; Put `folding-whole-buffer' and `folding-open-buffer'
458 ;; ;; close together.
459 ;;
460 ;; (defvar folding-mode-prefix-map nil)
461 ;;
462 ;; (define-key folding-mode-prefix-map "\C-w" nil)
463 ;; (define-key folding-mode-prefix-map "\C-s"
464 ;; 'folding-show-current-entry)
465 ;; (define-key folding-mode-prefix-map "\C-p"
466 ;; 'folding-whole-buffer))
467 ;;
468 ;; Example: changing default fold marks
469 ;;
470 ;; In case you're not happy with the default folding marks, you
471 ;; can change them easily. Here is an example
472 ;;
473 ;; (setq folding-load-hook 'my-folding-load-hook)
474 ;;
475 ;; (defun my-folding-load-hook ()
476 ;; "Folding vars setup."
477 ;; ;; Change marks for 'text-mode'
478 ;; (let* ((ptr (assq 'text-mode folding-mode-marks-alist)))
479 ;; (setcdr ptr (list "# {{{" "# }}}"))))
480 ;;
481 ;; Example: choosing different fold marks for mode
482 ;;
483 ;; Suppose you sometimes want to use different fold marks for the
484 ;; major mode: e.g. to alternate between "# {{{" and "{{{" in
485 ;; `text-mode' Call `M-x' `my-folding-text-mode-setup' to change
486 ;; the marks.
487 ;;
488 ;; (defun my-folding-text-mode-setup (&optional use-custom-folding-marks)
489 ;; (interactive
490 ;; (list (y-or-n-p "Use Custom fold marks now? ")))
491 ;; (let* ((ptr (assq major-mode folding-mode-marks-alist))
492 ;; (default-begin "# {{{")
493 ;; (default-end "# }}}")
494 ;; (begin "{{{")
495 ;; (end "}}}"))
496 ;; (when (eq major-mode 'text-mode)
497 ;; (unless use-custom-folding-marks
498 ;; (setq begin default-begin end default-end)))
499 ;; (setcdr ptr (list begin end))
500 ;; (folding-set-marks begin end)))
501 ;;
502 ;; Example: AucTex setup
503 ;;
504 ;; Suppose you're using comment.sty with AucTeX for editing
505 ;; LaTeX2e documents and you have these comment types. You would
506 ;; like to be able to set which of these 3 is to be folded at any
507 ;; one time, using a simple key sequence: move back and forth
508 ;; easily between the different comment types, e.g., "unfold
509 ;; everything then fold on \x".
510 ;;
511 ;; \O ... \endO
512 ;; \L ... \endL
513 ;; \B ... \endB
514 ;;
515 ;; (setq folding-load-hook 'my-folding-load-hook)
516 ;;
517 ;; (defun my-folding-load-hook ()
518 ;; "Folding vars setup."
519 ;; (let ((ptr (assq 'text-mode folding-mode-marks-alist)))
520 ;; (setcdr ptr (list "\\O" "\\endO"))
521 ;; (define-key folding-mode-prefix-map "C"
522 ;; 'my-folding-marks-change)))
523 ;;
524 ;; (defun my-folding-marks-change (&optional selection)
525 ;; "Select folding marks: prefixes nil, C-u and C-u C-u."
526 ;; (interactive "P")
527 ;; (let ((ptr (assq major-mode folding-mode-marks-alist))
528 ;; input)
529 ;; (when (string-match "^\\(plain-\\|la\\|auc\\)?tex-"
530 ;; (symbol-name major-mode))
531 ;; (setq input
532 ;; (read-string "Latex \\end(X) Marker (default O): "
533 ;; nil nil "O" nil))
534 ;; (setq input (upcase input))
535 ;; (turn-off-folding-mode)
536 ;; (folding-add-to-marks-list
537 ;; major-mode
538 ;; (concat "\\" input) (concat "\\end" input) nil nil t)
539 ;; ;; (setcdr ptr (list (concat "\\" input) (concat "\\end" input)))
540 ;; (turn-on-folding-mode))))
541 ;; ;; End of example
542 ;;
543 ;; Bugs: Lazy-shot.el conflict in XEmacs
544 ;;
545 ;; [XEmacs 20.4 lazy-shot-mode]
546 ;; 1998-05-28 Reported by Solofo Ramangalahy <solofo A T mpi-sb mpg de>
547 ;;
548 ;; % xemacs -q folding.el
549 ;; M-x eval-buffer
550 ;; M-x folding-mode
551 ;; M-x font-lock-mode
552 ;; M-x lazy-shot-mode
553 ;; C-s mouse
554 ;;
555 ;; then search for mouse again and again. At some point you will
556 ;; see "Deleting extent" in the minibuffer and XEmacs freezes.
557 ;;
558 ;; The strange point is that I have this bug only under Solaris
559 ;; 2.5 sparc (binaries from ftp.xemacs.org) but not under Solaris
560 ;; 2.6 x86. (XEmacs 20.4, folding 2.35). I will try to access
561 ;; more machines to see if it's the same.
562 ;;
563 ;; I suspect that the culprit is lazy-shot as it is beta, but
564 ;; maybe you will be able to describe the bug more precisely to
565 ;; the XEmacs people I you can reproduce it.
566
567 ;;}}}
568 ;;{{{ Old Documentation
569
570 ;; Old documentation
571 ;;
572 ;; The following text was written by Jamie Lokier for the release
573 ;; of Folding 1.6 in 1992. It is included here for history.
574 ;;
575 ;; Emacs 18:
576 ;; Folding mode has been tested with versions 18.55 and
577 ;; 18.58 of Emacs.
578 ;;
579 ;; Epoch:
580 ;; Folding mode has been tested on Epoch 4.0p2.
581 ;;
582 ;; [X]Emacs:
583 ;; There is code in here to handle some aspects of XEmacs.
584 ;; However, up to version 19.6, there appears to be no way to
585 ;; display folds. Selective-display does not work, and neither do
586 ;; invisible extents, so Folding mode has no chance of
587 ;; working. This is likely to change in future versions of
588 ;; XEmacs.
589 ;;
590 ;; Emacs 19:
591 ;; Tested on version 19.8, appears to be fine. Minor bug:
592 ;; display the buffer in several different frames, then move in
593 ;; and out of folds in the buffer. The frames are automatically
594 ;; moved to the top of the stacking order.
595 ;;
596 ;; Some of the code is quite horrible, generally in order to
597 ;; avoid some Emacs display "features". Some of it is specific to
598 ;; certain versions of Emacs. By the time Emacs 19 is around and
599 ;; everyone is using it, hopefully most of it won't be necessary.
600 ;;
601 ;; More known bugs
602 ;;
603 ;; *** Needs folding-fold-region to be more intelligent about
604 ;; finding a good region. Check folding a whole current fold.
605 ;;
606 ;; *** Now works with 19! But check out what happens when you
607 ;; exit a fold with the file displayed in two frames. Both
608 ;; windows get fronted. Better fix that sometime.
609 ;;
610 ;; Development ideas
611 ;;
612 ;; *** Maybe add `folding-next-error' sometime. It will only
613 ;; work with Emacs versions later than 18.58, because compile.el
614 ;; in earlier versions does not count line-numbers in the right
615 ;; way, when selective display is active.
616 ;;
617 ;; *** Fold titles should be optionally allowed on the closing
618 ;; fold marks, and `folding-tidy-inside' should check that the
619 ;; opening title matches the closing title.
620 ;;
621 ;; *** `folded-file' set in the local variables at the end of a
622 ;; file could encode the type of fold marks used in that file,
623 ;; and other things, like the margins inside folds.
624 ;;
625 ;; *** I can see a lot of use for the newer features of Emacs 19:
626 ;;
627 ;; Using invisible text-properties (I hope they are intended to
628 ;; make text invisible; it isn't implemented like that yet), it
629 ;; will be possible to hide folded text without affecting the
630 ;; text of the buffer. At the moment, Folding mode uses selective
631 ;; display to hide text, which involves substituting
632 ;; carriage-returns for line-feeds in the buffer. This isn't such
633 ;; a good way. It may also be possible to display different folds
634 ;; in different windows in Emacs 19.
635 ;;
636 ;; Using even more text-properties, it may be possible to track
637 ;; pointer movements in and out of folds, and have Folding mode
638 ;; automatically enter or exit folds as necessary to maintain a
639 ;; sensible display. Because the text itself is not modified (if
640 ;; overlays are used to hide text), this is quite safe. It would
641 ;; make it unnecessary to provide functions like
642 ;; `folding-forward-char', `folding-goto-line' or
643 ;; `folding-next-error', and things like I-search would
644 ;; automatically move in and out of folds as necessary.
645 ;;
646 ;; Yet more text-properties/overlays might make it possible to
647 ;; avoid using narrowing. This might allow some major modes to
648 ;; indent text properly, e.g., C++ mode.
649
650 ;;}}}
651
652 ;;; Change Log:
653
654 ;;{{{ History
655
656 ;; NOTE: History records were stopped in 2009 when code was moved under
657 ;; version control. See VCS logs.
658 ;; [person version] = developer and his revision tree number.
659 ;;
660 ;; Sep 20 2009 23.1 [jari git a80c2d6]
661 ;; - Remove 'defmacro custom' for very old Emacs version that did
662 ;; not have custom.
663 ;; - Modernize all macros to use new backquote syntax,
664 ;; - Move `folding-narrow-by-default' variable
665 ;; definition before `folding-advice-instantiate' which
666 ;; refers to it.
667 ;;
668 ;; Feb 20 2009 22.2.1 [jari git 51ada03..56b3089]
669 ;; - Make XEmacs CVS and Savannah git revisions at header more clear
670 ;; - Unify html-mode folds as in other modes: change [[[ ]]] to {{{ }}}
671 ;;
672 ;; Feb 09 2009 22.2.1 [jari git e0c2e92..6a3cff7]
673 ;; - Minor documentaton fixes.
674 ;; - Add new `python-mode' using `folding-add-to-marks-list'.
675 ;; - Add new variable `folding-version-time' to record edit time.
676 ;; Value is automatically updated by developer's Emacs setup.
677 ;;
678 ;; May 06 2007 21.4 [jari 3.38-3.41 2007.0506]
679 ;; - Cleanup. Eol whitespaces removed, extra newlines cleaned.
680 ;; Paren positions corrected.
681 ;; - 'Personal reflections by Anders Lindgren' topic
682 ;; rephrased 'Future development ideas'
683 ;; - (folding-show-current-entry): Run `font-lock-fontify-region'
684 ;; after opening the fold. Font-lock.el treated all closed folds
685 ;; as comments.
686 ;;
687 ;; Nov 16 2006 21.4 [jari 3.36-3.37 2006.1118]
688 ;; - Jeremy Hankins <nowan A T nowan org> sent a patch, which
689 ;; adds variable `folding-narrow-by-default'. The patch affects
690 ;; mostly `folding-shift-in'. This makes it possible to
691 ;; advise viper-search to open folds. Thanks.
692 ;; - Added VCS URL header to the beginning for canonnical location.
693 ;; Updated maintainer section.
694 ;; - Fixed Copyright years.
695 ;;
696 ;; Nov 25 2004 21.3 [jari 3.35 2004.1125]
697 ;; - non-ascii character removed from bibtex-mode.
698 ;; Changed bib-mode '@comment' => '%'. Closes Debian
699 ;; Bug#282388
700 ;;
701 ;; Sep 10 2004 21.3 [jari 2.116 2004.0910]
702 ;; - (folding-fold-region): caused to indent bottom fold
703 ;; some 50 spaces forward in auctex:latex-mode. Disabled
704 ;; running `indent-according-to-mode' while in latex-mode.
705 ;; Bug reported by Uwe Brauer; oub A T mat dot ucm dot es
706 ;; - Removed extra newlines from whole buffer.
707 ;; - Changed version scheme to date based YYYY.MMDD
708 ;; - Removed unnecessary 'all rights reserved'.
709 ;; - (folding-check-folded): Added check for \r character, which
710 ;; - protected all email addresses by removing AT-signs.
711 ;;
712 ;; Apr 01 2004 21.3 [jari 2.111-2.115]
713 ;; - Merged in changes made by 2003-11-12 Adrian Aichner
714 ;; from XEmacs tree 1.15; Typo fixes for docstrings and comments.
715 ;; - Returned to old bug and solved it in a better way (preserve region) by
716 ;; using different expansion macros for XEmacs and Emacs.
717 ;; See See http://list-archive.xemacs.org/xemacs-beta/199810/msg00039.html
718 ;; - (folding-forward-char-1): 2.112 Renamed.
719 ;; Was `folding-forward-char'.
720 ;; (folding-backward-char-1): 2.112 Renamed.
721 ;; Was `folding-backward-char'.
722 ;; (folding-forward-char-macro): 2.112 New. Fix XEmacs
723 ;; region preservation with '_p' interactive spec.
724 ;; (folding-backward-char-macro): 2.112 New. Fix XEmacs
725 ;; region preservation with '_p' interactive spec.
726 ;; (folding-interactive-spec-p): 2.112 New.
727 ;;
728 ;; Sep 11 2003 21.2 [jari 2.107-2.111]
729 ;; - Added new sections "Uninstallation" and "To read the manual".
730 ;; M-x finder can invoke folding too provided that patch to
731 ;; lisp-mnt.el and finder.el is installed. Sent patch to XEmacs and
732 ;; Emacs developers.
733 ;; - Moved fold-mark ";;{{{ Introduction" after the Commentary:
734 ;; tag to have it included in M-x finder-commentary.
735 ;; - If called like this: `folding-uninstall' and immediately
736 ;; `folding-mode', the keybindings were not there any more. Added
737 ;; call to `folding-install' in `folding-mode'.
738 ;; - Completely rewrote `folding-install'. It's now divided into
739 ;; `folding-install-keymaps' and `folding-uninstall-keymaps'
740 ;; - Added support for `php-mode', `javascript-mode',
741 ;; `change-log-mode' and `finder-mode'.
742 ;; - Documentation changes (fit all to 80 characters).
743 ;;
744 ;; Aug 21 2002 21.2 [jari 2.105-2.106]
745 ;; - Added user function `folding-uninstall'.
746 ;; - Removed `interactive' status: `folding-install-hooks' and
747 ;; `folding-uninstall-hooks'
748 ;;
749 ;; Aug 02 2002 20.7 [jari 2.101-2.104]
750 ;; - Added font lock support. Now beginning and end markers are
751 ;; highlighted with user variables `folding-font-lock-begin-mark'
752 ;; `folding-font-lock-end-mark'. Feature suggested by
753 ;; <Claude BOUCHER A T astrium-space com>
754 ;; - Removed LCD entry - unnecessary.
755 ;;
756 ;; Jan 24 2002 20.7 [jari 2.100]
757 ;; - (folding-context-next-action):New user function.
758 ;; Code by Scott Evans <gse A T antisleep com>
759 ;; - (folding-bind-default-keys): Added
760 ;; C-x . to run `folding-context-next-action'
761 ;; - (folding-mouse-call-original): Added `car-safe' to read
762 ;; EVENT, which may be nil.
763 ;;
764 ;; Jul 31 2001 20.7 [jari 2.98-2.99]
765 ;; - Gleb Arshinov <gleb A T barsook com> fixed the broken XEmacs
766 ;; isearch support and sent nice patch.
767 ;;
768 ;; Jul 19 2001 20.7 [jari 2.92-2.97]
769 ;; - Beautified lisp code by removing parens that were alone.
770 ;; - XEmacs latex-mode fix. The folds were strangely indented too
771 ;; far right. The cause was `indent-according-to-mode' which is
772 ;; now disabled in latex. bug reported by
773 ;; Uwe Brauer; oub A T maraton sim ucm es
774 ;; - 2.96 Erroneous `:' in `folding-mode-write-file'
775 ;; when it should have been `;'. Bug reported by
776 ;; Brand Michael; michael brand A T siemens com
777 ;;
778 ;; Apr 04 2001 20.7 [jari 2.89-2.91]
779 ;; - Small corrections to find-func.el::find-function-search-for-symbol
780 ;; implementation.
781 ;;
782 ;; Mar 08 2001 20.6 [jari 2.88]
783 ;; - Dave Masterson <dmasters A T rational com> reported that jumping to a
784 ;; url displayed by the C-h f FUNCTION which told where the function
785 ;; was located died. The reason was that the buffer was folded and
786 ;; find-func.el::find-function-search-for-symbol used regexps that
787 ;; do not take into account folded buffers. The regexps used there
788 ;; rely on syntax tables.
789 ;; - Added two new advices to catch find-func.el and unfold the buffer
790 ;; prior searching: (advice find-file-noselect after) and (advice
791 ;; find-function-search-for-symbol around)
792 ;;
793 ;; Mar 04 2001 20.6 [jari 2.83-2.87]
794 ;; - Added ###autoload statements, tidied up empty lines and lisp syntax.
795 ;; - Run checkdoc.el 0.6.1 and corrected errors.
796 ;;
797 ;; Jan 04 2001 20.6 [jari 2.82]
798 ;; - Added FOLD highlight feature for XEmacs:
799 ;; `folding-mode-motion-highlight-fold'
800 ;; and package `mode-motion' Suggested by
801 ;; Thomas Ruhnau <thomas ruhnau A T intermetall de>
802 ;; - (folding-bind-default-keys): 2.81 New binding C-k
803 ;; `folding-marks-kill'
804 ;; (fold-marks-kill): 2.81 New.
805 ;;
806 ;; Jan 03 2001 20.6 [jari 2.81]
807 ;; - (folding-folding-region): 2.80 Renamed to `folding-fold-region'
808 ;; - (folding-mark-look-at-top-mark-p): 2.80 New.
809 ;; - (folding-mark-look-at-bottom-mark-p): 2.80 New.
810 ;; - (folding-tidy-inside): 2.80 Use `folding-mark-look-at-top-mark-p'
811 ;; and `folding-mark-look-at-bottom-mark-p'.
812 ;; - Didn't accept spaces in front of fold markers.
813 ;; - (folding-fold-region): 2.80 Added `indent-according-to-mode'
814 ;; to indent folds as needed.
815 ;;
816 ;; Dec 16 2000 20.6 [jari 2.79-2.80]
817 ;; - `folding-xemacs-p' now test (featurep 'xemacs)
818 ;; - Added missing folding functions to the menubar
819 ;; - `folding-package-url-location' new variable used by function
820 ;; `folding-insert-advertise-folding-mode'
821 ;; - `folding-keep-hooked' was commented out in `folding-mode'. Added
822 ;; back.
823 ;;
824 ;; Jul 25 2000 20.6 [jari 2.76-2.78]
825 ;; - 2.75 Added support for modes:
826 ;; xrdb-mode, ksh-mode and sql-mode contributed by
827 ;; Juhapekka Tolvanen <juhtolv A T st jyu fi>. Scanned systematically
828 ;; all modes under Emacs 20.6 progmodes and added support for:
829 ;; ada-mode, asm-mode, awk-mode, cperl-mode, fortran-mode, f90-mode,
830 ;; icon-mode, m4-mode, meta-mode, pascal-mode, prolog-mode,
831 ;; simula-mode, vhdl-mode, bibtex-mode, nroff-mode, scribe-mode(*),
832 ;; sgml-mode
833 ;; - Mode marked with (*) was not added.
834 ;; - (folding-insert-advertise-folding-mode): 2.76 New. Suggested by
835 ;; Juhapekka Tolvanen <juhtolv A T st jyu fi>
836 ;; - (folding-bind-default-keys): 2.76
837 ;; folding-insert-advertise-folding-mode Bound to key "I"
838 ;;
839 ;; Apr 24 1999 20.4 [jari 2.73-2.75]
840 ;; - (folding-bind-terminal-keys): 2.74 New. Bind C-f and C-b only at
841 ;; non-window system where they are really needed. Someone may use
842 ;; C-f for `isearch-forward' in windowed Emacs.
843 ;; - (folding-bind-default-keys): 2.74 Use `folding-bind-terminal-keys'
844 ;; - (folding-bind-outline-compatible-keys): 2.74
845 ;; Use `folding-bind-terminal-keys'
846 ;;
847 ;; Feb 13 1999 20.4 [jari 2.71-2.72]
848 ;; - (folding-event-posn): 2.70 Wrong
849 ;; place of paren and the following was malformed call:
850 ;; (let* ((el (funcall (symbol-function 'event-start) event)))
851 ;;
852 ;; Jan 13 1999 20.4 [jari 2.70]
853 ;; - 2.69 The `looking-at' is now smarter with
854 ;; fold beginning marks. The tradition has been the the fold always
855 ;; has a name, so the requirement to search fold is "{{{ ". Now
856 ;; the " " is searched as " *", not requiring a space --> not requiring
857 ;; a fold name.
858 ;; - (folding-skip-folds): >>feature not not enabled<<
859 ;; 2.69 Do not require trailing " " any more.'
860 ;; (folding-tidy-inside): >>feature not not enabled<<
861 ;; 2.69 Do not require trailing " " any more.
862 ;; - (folding-install): 2.69 Fixed indentation.
863 ;; - (folding-mark-look-at): 2.69 The "em" missed "*" and thus pressing
864 ;; mouse-3 at the end-fold didn't collapse the whole fold.
865 ;;
866 ;; Jan 12 1999 20.4 [jari 2.69]
867 ;; (folding-bind-default-mouse): 2.68
868 ;; XEmacs and Emacs Mouse binding was different. Now use common
869 ;; bindings: The S-mouse-2 was superfluous, because mouse-3 already
870 ;; did that, so the binding was removed.
871 ;; mouse-3 folding-mouse-context-sensitive
872 ;; S-mouse-2 folding-hide-current-entry
873 ;; C-S-mouse-2 folding-mouse-pick-move
874 ;;
875 ;;;; Jan 09 1999 20.4 [jari 2.67-2.68]
876 ;; - (folding-event-posn): 2.66 Hide `event-start' From XEmacs
877 ;; (byte compile silencer)
878 ;;
879 ;; Jan 07 1999 20.4 [jari 2.65-2.66]
880 ;; - The Folding begin and AND mark was not case sensitive;
881 ;; that's why a latex styles "\B" and "\endB" fold marks couldn't
882 ;; be used. Added relevant `case-fold-search' settings. Not tested
883 ;; very well, though.
884 ;; - Added standard "turn-on" "turn-off" functions.
885 ;; - (folding-whole-buffer): 2.65 Better
886 ;; Error message. Show used folding-mark on error.
887 ;; - (folding-skip-folds): 2.65 Moved docs in function.
888 ;; - (turn-off-folding-mode): 2.65 New.
889 ;; - (turn-on-folding-mode): 2.65 New.
890 ;; - (folding-mark-look-at): 2.65 `case-fold-search'
891 ;; - (folding-next-visible-heading): 2.65 `case-fold-search'
892 ;; - (folding-find-folding-mark): 2.65 `case-fold-search'
893 ;; - (folding-pick-move): 2.65 `case-fold-search'
894 ;; - (folding-skip-folds): 2.65 `case-fold-search'
895 ;; - (folding-tidy-inside): 2.65 `case-fold-search'
896 ;; - (folding-convert-to-major-folds): 2.65 `case-fold-search'
897 ;;
898 ;; Jan 04 1999 20.4 [jari 2.62-2.64]
899 ;; - (folding-set-local-variables): 2.61 New. Now it is possible to
900 ;; change the folding marks dynamically.
901 ;; - (folding-mode): 2.61 Call `folding-set-local-variables'
902 ;; (folding-mode-marks-alist): 2.61 mention
903 ;; - `folding-set-local-variables'
904 ;; Added documentation section: "Example: AucTex setup"
905 ;; - NT Emacs fix wrapped inside `eval-and-compile'. hs-discard-overlays
906 ;; are now hidden from byte compiler (since the code is not
907 ;; executed anyway)
908 ;;
909 ;; May 24 1999 19.34 [jari 2.59-2.61]
910 ;; - New function `folding-all-comment-blocks-in-region'. Requested by
911 ;; Uwe Brauer <oub A T eucmos sim ucm es>. Bound under "/" key.
912 ;; - (folding-all-comment-blocks-in-region):
913 ;; Check non-whitespace `comment-end'. Added `matlab-mode' to
914 ;; fold list
915 ;; - (folding-event-posn): 2.63 Got rid of the XEmacs/Emacs
916 ;; posn-/event- byte compiler warnings
917 ;; - (folding-mouse-call-original): 2.63 Got rid of the XEmacs
918 ;; `event-button' byte compiler warning.
919 ;;
920 ;; Apr 15 1999 19.34 [jari 2.57]
921 ;; - (folding-mouse-call-original): Samuel Mikes
922 ;; <smikes A T alumni hmc edu> reported that the `concat' function was
923 ;; used to add an integer to "button" event. Applied patch to use
924 ;; `format' instead.
925 ;;
926 ;; Mar 03 1999 19.34 [andersl]
927 ;; - (folding-install): had extra paren. Removed.
928 ;;
929 ;; Feb 22 1999 19.34 [jari 2.56]
930 ;; - folding-install):
931 ;; Check if `folding-mode-prefix-map' is nil and call
932 ;;
933 ;; Feb 19 1999 19.34 [jari 2.55]
934 ;; - (folding-mode-hook-no-re):
935 ;; Renamed to `folding-mode-hook-no-regexp'
936 ;; - (fold-inside-mode-name): Renames to `folding-inside-mode-name'
937 ;; (fold-mode-string): Renamed to `folding-mode-string'
938 ;; - Renamed all `fold-' prefixes to `folding-'
939 ;; - Rewrote chapter `Example: personal setup'
940 ;;
941 ;; Jan 01 1999 19.34 [jari 2.54]
942 ;; - Byte compiler error fix: (folding-bind-outline-compatible-keys):
943 ;; 'folding-show-all lacked the quote.
944 ;;
945 ;; Dec 30 1998 19.34 [jari 2.53]
946 ;; - Jesper Pedersen <blackie A T imada ou dk> reported bug that hiding
947 ;; subtree was broken. This turned out to be a bigger problem in fold
948 ;; handling in general. This release has big relatively big error
949 ;; fixes.
950 ;; - Many of the folding functions were also renamed to mimic Emacs 20.3
951 ;; allout.el names. Outline keybindings were rewritten too.
952 ;; - folding.el (folding-mouse-yank-at-point): Renamed from
953 ;; `folding-mouse-operate-at-point'. The name is similar to Emacs
954 ;; standard variable name. The default value changed from nil --> t
955 ;; according to suggestion by Jesper Pedersen <blackie A T imada ou dk>
956 ;; Message "Info, Ignore [X]Emacs specific..." is now displayed only
957 ;; while byte compiling file.
958 ;; (folding-bind-outline-compatible-keys):
959 ;; Checked the Emacs 20.3 allout.el outline bindings and made
960 ;; folding mimic them
961 ;; (folding-show-subtree): Renamed to `folding-show-current-subtree'
962 ;; according to allout.el
963 ;; (folding-hide-subtree): Renamed to `folding-hide-current-subtree'
964 ;; according to allout.el
965 ;; (folding-enter): Renamed to `folding-shift-in'
966 ;; according to allout.el
967 ;; (folding-exit): Renamed to `folding-shift-out'
968 ;; according to allout.el
969 ;; (folding-move-up): Renamed to `folding-previous-visible-heading'
970 ;; according to allout.el
971 ;; (folding-move): Renamed to `folding-next-visible-heading'
972 ;; according to allout.el
973 ;; (folding-top-level): Renamed to `folding-show-all'
974 ;; according to allout.el
975 ;; (folding-show): Renamed to `folding-show-current-entry'
976 ;; according to allout.el
977 ;; (folding-hide): Renamed to `folding-hide-current-entry'
978 ;; according to allout.el
979 ;; (folding-region-open-close): While loop rewritten so that if user
980 ;; is already on a fold mark, then close current fold. This also
981 ;; fixed the show/hide subtree problem.
982 ;; (folding-hide-current-subtree): If use hide subtree that only had
983 ;; one fold, then calling this function caused error. The reason was
984 ;; error in `folding-pick-move' (folding-pick-move): Test that
985 ;; `moved' variable is integer and only then move point. This is the
986 ;; status indicator from `folding-find-folding-mark'
987 ;; (folding-find-folding-mark): Fixed. mistakenly moved point when
988 ;; checking TOP level marker, status 11. the point was permanently
989 ;; moved to point-min.
990 ;;
991 ;; Dec 29 1998 19.34 [jari 2.51]
992 ;; - Jesper Pedersen <blackie A T imada ou dk> reported that prefix key
993 ;; cannot take vector notation [(key)]. This required changing the way
994 ;; how folding maps the keys. Now uses intermediate keymap
995 ;; `folding-mode-prefix-map'
996 ;; - `folding-kbd' is new.
997 ;; - `folding-mode' function description has better layout.
998 ;; - `folding-get-mode-marks' is now defsubst.
999 ;;
1000 ;; Dec 13 1998 19.34 [jari 2.49-2.50]
1001 ;; - Gleb Arshinov <gleb A T CS Stanford EDU> reported that the XEmacs 21.0
1002 ;; `concat' function won't accept integer argument any more and
1003 ;; provided patch for `folding-set-mode-line'.
1004 ;;
1005 ;; Nov 28 1998 19.34 [jari 2.49-2.50]
1006 ;; - Gleb Arshinov <gleb A T CS Stanford EDU> reported that the
1007 ;; zmacs-region-stays must not be set globally but in the functions
1008 ;; that need it. He tested the change on tested on XEmacs 21.0 beta
1009 ;; and FSF Emacs 19.34.6 on NT and sent a patch . Thank you.
1010 ;; - (folding-preserve-active-region): New macro to set
1011 ;; `zmacs-region-stays' to t in XEmacs.
1012 ;; - (folding-forward-char): Use `folding-preserve-active-region'
1013 ;; - (folding-backward-char): Use `folding-preserve-active-region'
1014 ;; - (folding-end-of-line): Use `folding-preserve-active-region'
1015 ;; - (folding-isearch-general): Variables `is-fold' and
1016 ;; `is narrowed' removed, because they were not used. (Byte
1017 ;; Compilation fix)
1018 ;; - Later: interestingly using `defmacro'
1019 ;; folding-preserve-active-region does not work in XEmacs 21.0 beta,
1020 ;; but `defsubst' does. Reported and corrected by Gleb.
1021 ;;
1022 ;; Oct 22 1998 19.34 [jari 2.47-2.48]
1023 ;; - NT Emacs has had long time a bug where it strips away ^M when
1024 ;; closed fold is copied to kill ring. When pasted, then ^M are
1025 ;; gone. This cover NT Emacs releases 19.34 - 20.3. Bug report has
1026 ;; been filed.
1027 ;; - to cope with the situation I added new advice functions that
1028 ;; get instantiated only for these versions of NT Emacs. See
1029 ;; `kill-new' and `current-kill'
1030 ;;
1031 ;; Oct 21 1998 19.34 [jari 2.46]
1032 ;; - `folding-isearch-general' now enters folds as usual with isearch.
1033 ;; The only test needed was to check `quit-isearch' before calling
1034 ;; `folding-goto-char', because the narrow case was already taken
1035 ;; cared of in the condition case.
1036 ;;
1037 ;; Oct 19 1998 19.34 [jari 2.44]
1038 ;; - 1998-10-19 Uwe Brauer <oub A T sunma4 mat ucm es> reported that
1039 ;; In Netscape version > 4 the {{{ marks cannot be used. For IE they
1040 ;; were fine, but not for Netscape. Some bug there.
1041 ;; --> Marks changed to [[[ ]]]
1042 ;;
1043 ;; Oct 5 1998 19.34 [jari 2.43]
1044 ;; - The "_p" flag does not exist in Emacs 19.34, so the previous patch
1045 ;; was removed. <greg A T alphatech com> (Greg Klanderman) suggested using
1046 ;; `zmacs-region-stays'. Added to the beginning of file.
1047 ;; - todo: folding does not seem to open folds any more with Isearch.
1048 ;;
1049 ;; Oct 5 1998 19.34 [jari 2.42]
1050 ;; - Gleb Arshinov <gleb A T cs stanford edu> reported (and supplied patch):
1051 ;; I am using the latest beta of folding.el with XEmacs 21.0 "Finnish
1052 ;; Landrace" [Lucid] (i386-pc-win32) (same bug is present with folding.el
1053 ;; included with XEmacs). Being a big fan of zmacs-region, I was
1054 ;; disappointed to find that folding mode caused my usual way of
1055 ;; selecting regions (e.g. to select a line C-space, C-a, C-e) to break
1056 ;; :( I discovered that the following 3 functions would unset my mark.
1057 ;; Upon reading some documentation, this seems to be caused by an
1058 ;; argument to interactive used by these functions. With the following
1059 ;; tiny patch, the undesirable behaviour is gone.
1060 ;; - Patch was applied as is. Function affected:
1061 ;; `folding-forward-char' `folding-backward-char'
1062 ;; `folding-end-of-line'. Interactive spec changed from "p" to "_p"
1063 ;;
1064 ;; Sep 28 1998 19.34 [jari 2.41]
1065 ;; - Wrote section "folding-whole-buffer doesn't fold whole buffer" to
1066 ;; Problems topic. Fixed some indentation in documentation so that
1067 ;; command ripdoc.pl folding.el | t2html.pl --simple > folding.html
1068 ;; works properly.
1069 ;;
1070 ;; Sep 24 1998 19.34 [jari 2.40]
1071 ;; - Stephen Smith <steve A T fmrib ox ac uk> wished that the
1072 ;; `folding-comment-fold' should handle modes that have comment-start
1073 ;; and comment-end too. That lead to rewriting the comment function so
1074 ;; that it can be adapted to new modes.
1075 ;; - `folding-pick-move' didn't work in C-mode. Fixed.
1076 ;; (folding-find-folding-mark):
1077 ;; m and re must be protected with `regexp-quote'. This
1078 ;; corrected error eg. in C-mode where `folding-pick-move'
1079 ;; didn't move at all.
1080 ;; (folding-comment-fold): Added support for major modes that
1081 ;; have `comment-start' and `comment-end'. Use
1082 ;; `folding-comment-folding-table'
1083 ;; (folding-comment-c-mode): New.
1084 ;; (folding-uncomment-c-mode): New.
1085 ;; (folding-comment-folding-table): New. To adapt to any major-mode.
1086 ;; (folding-uncomment-mode-generic): New.
1087 ;; (folding-comment-mode-generic): New.
1088 ;;
1089 ;; Aug 08 1998 19.34 [jari 2.39]
1090 ;; - Andrew Maccormack <andrewm A T bristol st com> reported that the
1091 ;; `em' end marker that was defined in the `let' should also have
1092 ;; `[ \t\n]' which is in par with the `bm'. This way fold markers do
1093 ;; not need to be parked to the left any more.
1094 ;;
1095 ;; Jun 05 1998 19.34 [jari 2.37-2.38]
1096 ;; - Alf-Ivar Holm <affi A T osc no> send functions
1097 ;; `folding-toggle-enter-exit' and `folding-toggle-show-hide' which
1098 ;; were integrated. Alf also suggested that Fold marks should now
1099 ;; necessarily be located at the beginning of line, but allow spaces
1100 ;; at front. The patch was applied to `folding-mark-look-at'
1101 ;;
1102 ;; Mar 17 1998 19.34 [Anders]
1103 ;; - Anders: This patch fixes one problem that was reported in the
1104 ;; beginning of May by Ryszard Kubiak <R Kubia A T ipipan gda pl>.
1105 ;; - Finally, I think that I have gotten mouse-context-sensitive
1106 ;; right. Now, when you click on a fold that fold rather than the
1107 ;; one the cursor is on is used, while still not breaking commands
1108 ;; like `mouse-save-then-kill' which assumes that the point hasn't
1109 ;; been moved.
1110 ;; - Jari: Added topic "Fold must have a label" to the Problem section.
1111 ;; as reported by Solofo Ramangalahy <solofo A T mpi-sb mpg de>
1112 ;; - 1998-05-04 Ryszard Kubiak <R Kubiak A T ipipan gda pl> reported: I am
1113 ;; just curious if it is possible to make Emacs' cursor
1114 ;; automatically follow a mouse-click on the {{{ and }}} lines. I
1115 ;; mean by this that a [S-mouse-3] (as defined in my settings below
1116 ;; --- I keep not liking overloading [mouse-3]) first moves the
1117 ;; cursor to where the click happened and then hides or shows a
1118 ;; folded area. I presume that i can write a two-lines long
1119 ;; interactive function to do this. Still, may be this kind of mouse
1120 ;; behaviour is already available.
1121 ;;
1122 ;; Mar 17 1998 19.34 [Jari 2.34-2.35]
1123 ;; - Added "Example: choosing different fold marks for mode"
1124 ;; - corrected `my-folding-text-mode-setup' example.
1125 ;;
1126 ;; Mar 10 1998 19.34 [Jari 2.32-2.33]
1127 ;; - [Anders] responds to mouse-3 handling problem: I have found the
1128 ;; cause of the problem, and I have a suggestion for a fix.
1129 ;;
1130 ;; The problem is caused by two things:
1131 ;; * The "mouse-save-then-kill" checks that the previous command also
1132 ;; was "mouse-save-then-kill".
1133 ;;
1134 ;; * The second (more severe) problem is that
1135 ;; "folding-mouse-context-sensitive" sets the point to the
1136 ;; location of the click, effectively making
1137 ;; "mouse-save-then-kill" mark the area between the point and the
1138 ;; point! (This is why no region appears.)
1139 ;;
1140 ;; The first problem can be easily fixed by setting "this-command"
1141 ;; in "folding-mouse-call-original":
1142 ;;
1143 ;; - Now the good old mouse-3 binding is back again.
1144 ;; - (folding-mouse-context-sensitive): Added `save-excursion' as
1145 ;; Anders suggested before setting `state'.
1146 ;; (folding-mouse-call-original): commented out experimental code and
1147 ;; used (setq this-command orig-func) as Anders suggested.
1148 ;;
1149 ;; Mar 10 1998 19.34 [Jari 2.31]
1150 ;; - (folding-act): Added `event' to `folding-behave-table' calls.
1151 ;; Input argument takes now `event' too
1152 ;; - (folding-mouse-context-sensitive): Added argument `event'
1153 ;; - (folding-mouse-call-original): Added (this-command orig-func)
1154 ;; when calling original command.
1155 ;; - (folding-bind-default-mouse): Changed mouse bindings. The
1156 ;; button-3 can't be mapped by folding, because folding is unable to
1157 ;; call the original function `mouse-save-then-kill'. Passing simple
1158 ;; element to `mouse-save-then-kill' won't do the job. Eg if I
1159 ;; (clicked mouse-1) moved mouse pointer to place X and pressed
1160 ;; mouse-3, the area was not highlighted in folding mode. If folding
1161 ;; mode was off the are was highlighted. I traced the
1162 ;; `folding-mouse-call-original' and it was passing exactly the same
1163 ;; event as without folding mode. I have no clue what to do about
1164 ;; it...That's why I removed default mouse-3 binding and left it to
1165 ;; emacs. This bug was reported by Ryszard Kubiak"
1166 ;; <R Kubiak A T ipipan gda pl>
1167 ;;
1168 ;; Feb 12 1998 19.34 [Jari 2.30]
1169 ;; - (html-mode): New mode added to `folding-mode-marks-alist'
1170 ;; - (folding-get-mode-marks): Rewritten, now return 3rd element too.
1171 ;; - (folding-comment-fold): Added note that function with `comment-end'
1172 ;; is not supported. Function will flag error in those cases.
1173 ;; - (folding-convert-to-major-folds): Conversion failed if eg; you
1174 ;; switched between modes that has 2 and 1 comments, like
1175 ;; /* */ (C) and //(C++). Now the conversion is bit smarter, but it's
1176 ;; impossible to convert from /* */ to // directly because we don't
1177 ;; know how to remove */ mark, you see:
1178 ;;
1179 ;; Original mode was C
1180 ;;
1181 ;; /* {{{ */
1182 ;;
1183 ;; And now used changed it to C++ mode, and ran command
1184 ;; `folding-convert-to-major-folds'. We no longer have information
1185 ;; about old mode's beginning or end comment markers, so we only
1186 ;; can convert the folds to format
1187 ;;
1188 ;; // {{{ */
1189 ;;
1190 ;; Where the ending comment mark from old mode is left there.
1191 ;; This is slightly imperfect situation, but at least the fold
1192 ;; conversion works.
1193 ;;
1194 ;; Jan 28 1998 19.34 [Jari 2.25-2.29]
1195 ;; - Added `generic-mode' to fold list, suggested by Wayne Adams
1196 ;; <wadams A T galaxy sps mot com>
1197 ;; - Finally rewrote the awesome menu-bar code: now uses standard
1198 ;; easy-menu Which works in both XEmacs and Emacs. The menu is no
1199 ;; longer under "Tools", but appear when minor mode is turned on.
1200 ;; - Radical changes: Decided to remove all old lucid and epoch
1201 ;; dependencies. Lot of code removed and reprogrammed.
1202 ;; - I also got rid of the `folding-has-minor-mode-map-alist-p' variable
1203 ;; and old 18.xx function `folding-merge-keymaps'.
1204 ;; - Symbol's value as variable is void ((folding-xemacs-p)) error fixed.
1205 ;; - Optimized 60 `folding-use-overlays-p' calls to only 4 within
1206 ;; `folding-subst-regions'. (Used elp.el). It seems that half of the
1207 ;; time is spent in the function `folding-narrow-to-region'
1208 ;; function. Could it be optimized somehow?
1209 ;; - Changed "lucid" tests to `folding-xemacs-p' variable tests.
1210 ;; - Removed `folding-hack' and print message 'Info, ignore missing
1211 ;; functions.." instead. It's better that we see the missing
1212 ;; functions and not define dummy hacks for them.
1213 ;;
1214 ;; Nov 13 1997 19.34 [Jari 2.18-2.24]
1215 ;; - Added tcl-mode fold marks, suggested by Petteri Kettunen
1216 ;; <Petteri Kettunen A T oulu fi>
1217 ;; - Removed some old code and modified the hook functions a bit.
1218 ;; - Added new user function `folding-convert-to-major-folds', key "%".
1219 ;; - Added missing items to Emacs menubar, didn't dare to touch the
1220 ;; XEmacs part.
1221 ;; - `folding-comment-fold': Small fix. commenting didn't work on
1222 ;; closed folds. or if point was on topmost fold.
1223 ;; - Added `folding-advice-instantiate' And corrected byte compiler
1224 ;; message: Warning: variable oldposn bound but not referenced
1225 ;; Warning: reference to free variable folding-stack
1226 ;; - updated (require 'custom) code
1227 ;;
1228 ;; Nov 6 1997 19.34 [Jari 2.17]
1229 ;; - Uwe Brauer <oub A T sunma4 mat ucm es> used folding for Latex files
1230 ;; and he wished a feature that would allow him to comment away ext
1231 ;; that was inside fold; when compiling the TeX file.
1232 ;; - Added new user function `folding-comment-fold'. And new
1233 ;; keybinding ";".
1234 ;;
1235 ;; Oct 8 1997 19.34 [Jari 2.16]
1236 ;; - Now the minor mode map is always re-installed when this file is
1237 ;; loaded. If user accidentally made mistake in
1238 ;; `folding-default-keys-function', he can simply try again and
1239 ;; reload this file to have the new key definitions.
1240 ;; - Previously user had to manually go and delete the previous map
1241 ;; from the `minor-mode-map-alist' before he could try again.
1242 ;;
1243 ;; Sep 29 1997 19.34 [Jari 2.14-2.15]
1244 ;; - Robert Marshall <rxmarsha A T bechtel com> Sent enhancement to goto-line
1245 ;; code. Now M-g works more intuitively.
1246 ;; - Reformatted totally the documentation so that it can be ripped to
1247 ;; html with jari's ema-doc.pls and t2html.pls Perl scripts.
1248 ;; - Run through checkdoc.el 1.55 and Elint 1.10 and corrected code.
1249 ;; - Added defcustom support. (not tested)
1250 ;;
1251 ;; Sep 19 1997 19.28 [Jari 2.13]
1252 ;; - Robert Marshall <rxmarsha A T bechtel com> Sent small correction to
1253 ;; overlay code, where the 'owner tag was set wrong.
1254 ;;
1255 ;; Aug 14 1997 19.28 [Jari 2.12 ]
1256 ;; - A small regexp bug (extra whitespace was required after closing
1257 ;; fold) cause failing of folding-convert-buffer-for-printing in the
1258 ;; following situation
1259 ;; - Reported by Guide. Fixed now.
1260 ;;
1261 ;; {{{ Main topic
1262 ;; {{{ Subsection
1263 ;; }}} << no space or end tag here!
1264 ;; }}} Main topic
1265 ;;
1266 ;; Aug 14 1997 19.28 [Jari 2.11]
1267 ;; - Guide Van Hoecke <Guido Van Hoecke A T bigfoot com> reported that
1268 ;; he was using closing text for fold like:
1269 ;;
1270 ;; {{{ Main topic
1271 ;; {{{ Subsection
1272 ;; }}} Subsection
1273 ;; }}} Main topic
1274 ;;
1275 ;; And when he did folding-convert-buffer-for-printing, it couldn't
1276 ;; remove those closing marks but threw an error. I modified the
1277 ;; function so that the regexp accepts anything after closing fold.
1278 ;;
1279 ;; Apr 18 1997 19.28 [Jari 2.10]
1280 ;; - Corrected function folding-show-current-subtree, which didn't
1281 ;; find the correct end region, because folding-pick-move needed
1282 ;; point at the top of beginning fold. Bug was reported by Uwe
1283 ;; Brauer <oub A T sunma4 mat ucm es> Also changed folding-mark-look-at,
1284 ;; which now has new call parameter 'move.
1285 ;;
1286 ;; Mar 22 1997 19.28 [Jari 2.9]
1287 ;; - Made the XEmacs20 match more stricter, so that
1288 ;; folding-emacs-version gets value 'XEmacs19. Also added note about
1289 ;; folding in WinNT in the compatibility section.
1290 ;; - Added sh-script-mode indented-text-mode folding marks.
1291 ;; - Moved the version from branch to the root, because the extra
1292 ;; overlay code added, seems to be behaving well and it didn't break
1293 ;; the existing functionality.
1294 ;;
1295 ;; Feb 17 1997 19.28 [Jari 2.8.1.2]
1296 ;; - Cleaned up Dan's changes. First: we must not replace the
1297 ;; selective display code, but offer these two choices: Added
1298 ;; folding-use-overlays-p function which looks variable
1299 ;; folding-allow-overlays.
1300 ;; - Dan uses function from another Emacs specific (19.34+?) package
1301 ;; hs-discard-overlays. This is not available in 19.28. it should
1302 ;; be replaced with some new function... I didn't do that yet.
1303 ;; - The overlays don't exist in XEmacs. XE19.15 has promises: at least
1304 ;; I have heard that they have overlay.el library to mimic Emacs
1305 ;; functions.
1306 ;; - Now the overlay support can be turned on by setting
1307 ;; folding-allow-overlays to non-nil. The default is to use selective
1308 ;; display. Overlay Code is not tested!
1309 ;;
1310 ;; Feb 17 1997 19.28 [Dan 2.8.1.1]
1311 ;; - Dan Nicolaescu <done A T ece arizona edu> sent patch that replaced
1312 ;; selective display code with overlays.
1313 ;;
1314 ;; Feb 10 1997 19.28 [jari 2.8]
1315 ;; - Ricardo Marek <ricky A T ornet co il> Kindly sent patch that
1316 ;; makes code XEmacs 20.0 compatible. Thank you.
1317 ;;
1318 ;; Nov 7 1996 19.28 [jari 2.7]
1319 ;; - When I was on picture-mode and turned on folding, and started
1320 ;; isearch (I don't remember how I got fold mode on exactly) it
1321 ;; gave error that the fold marks were not defined and emacs
1322 ;; locked up due to simultaneous isearch-loop
1323 ;; - Added few fixes to the isearch handling function to avoid
1324 ;; infinite error loops.
1325 ;;
1326 ;; Nov 6 1996 19.28 [jari 2.5 - 2.6]
1327 ;; - Situation: have folded buffer, manually _narrow_ somewhere, C-x n n
1328 ;; - Then try searching --> folding breaks. Now it checks if the
1329 ;; region is true narrow and not folding-narrow before trying
1330 ;; to go outside of region and open a fold
1331 ;; - If it's true narrow, then we stay in that narrowed region.
1332 ;;
1333 ;; folding-isearch-general :+
1334 ;; folding-region-has-folding-marks-p :+
1335 ;;
1336 ;; Oct 23 1996 19.28 [jari 2.4]
1337 ;; folding-display-name :+ new user cmd "C-n"
1338 ;; folding-find-folding-mark :+ new
1339 ;; folding-pick-move :! rewritten, full of bugs
1340 ;; folding-region-open-close :! rewritten, full of bugs
1341 ;;
1342 ;; Oct 22 1996 19.28 [jari 2.3]
1343 ;; - folding-pick-move :! rewritten
1344 ;; folding-region-open-close :+ new user cmd "#"
1345 ;; folding-show-current-subtree :+ new user cmd "C-s", hides too
1346 ;;
1347 ;; Aug 01 1996 19.31 [andersl]
1348 ;; - folding-subst-regions, variable `font-lock-mode' set to nil.
1349 ;; Thanks to <stig A T hackvan com>
1350 ;;
1351 ;; Jun 19 1996 19.31 [andersl]
1352 ;; - The code has proven itself stable through the beta testing phase
1353 ;; which has lasted the past six months.
1354 ;; - A lot of comments written.
1355 ;; - The package `folding-isearch' integrated.
1356 ;; - Some code cleanup:
1357 ;; BOLP -> folding-BOL :! renamed
1358 ;; folding-behave-table :! field `down' removed.
1359 ;;
1360 ;;
1361 ;; Mar 14 1996 19.28 [jari 1.27]
1362 ;; - No code changes. Only some textual corrections/additions.
1363 ;; - Section "about keymaps" added.
1364 ;;
1365 ;; Mar 14 1996 19.28 [jackr 1.26]
1366 ;; - spell-check run over code.
1367 ;;
1368 ;; Mar 14 1996 19.28 [davidm 1.25]
1369 ;; - David Masterson <davidm A T prism kla com> This patch makes the menubar in
1370 ;; XEmacs work better. After I made this patch, the Hyperbole menus
1371 ;; starting working as expected again. I believe the use of
1372 ;; set-buffer-menubar has a problem, so the recommendation in XEmacs
1373 ;; 19.13 is to use set-menubar-dirty-flag.
1374 ;;
1375 ;; Mar 13 1996 19.28 [andersl 1.24]
1376 ;; - Corrected one minor bug in folding-check-if-folding-allowed
1377 ;;
1378 ;; Mar 12 1996 19.28 [jari 1.23]
1379 ;; - Renamed all -func variables to -function.
1380 ;;
1381 ;; mar 12 1996 19.28 [jari 1.22]
1382 ;; - Added new example how to change the fold marks. The automatic folding
1383 ;; was reported to cause unnecessary delays for big files (eg. when using
1384 ;; ediff) Now there is new function variable which can totally disable
1385 ;; automatic folding if the return value is nil.
1386 ;;
1387 ;; folding-check-allow-folding-function :+ new variable
1388 ;; folding-check-if-folding-allowed :+ new func
1389 ;; folding-mode-find-file :! modified
1390 ;; folding-mode-write-file :! better docs
1391 ;; folding-goto-line :! arg "n" --> "N" due to XEmacs 19.13
1392 ;;
1393 ;; Mar 11 1996 19.28 [jari 1.21]
1394 ;; - Integrated changes made by Anders' to v1.19 [folding in beta dir]
1395 ;;
1396 ;; Jan 25 1996 19.28 [jari 1.20]
1397 ;; - ** Mainly cosmetic changes **
1398 ;; - Added some 'Section' codes that can be used with lisp-mnt.el
1399 ;; - Deleted all code in 'special section' because it was never used.
1400 ;; - Moved some old "-v-" named variables to better names.
1401 ;; - Removed folding-mode-flag that was never used.
1402 ;;
1403 ;; Jan 25 1996 19.28 [jari 1.19]
1404 ;; - Put Anders' latest version into RCS tree.
1405 ;;
1406 ;; Jan 03 1996 19.30 [andersl]
1407 ;; - `folding-mouse-call-original' uses `call-interactively'.
1408 ;; `folding-mouse-context-sensitive' doesn't do `save-excursion'.
1409 ;; (More changes will come later.)
1410 ;; `folding-mouse-yank-at-p' macro corrected (quote added).
1411 ;; Error for `epoch::version' removed.
1412 ;; `folding-mark-look-at' Regexp change .* -> [^\n\r]* to avoid error.
1413 ;;
1414 ;; Nov 24 1995 19.28 [andersl]
1415 ;; - (sequencep ) added to the code which checks for the existence
1416 ;; of a tools menu.
1417 ;;
1418 ;; Aug 27 1995 19.28 19.12 [andersl]
1419 ;; - Keybindings restructured. They now conforms with the
1420 ;; new 19.29 styleguide. Old keybindings are still available.
1421 ;; - Menus new goes into the "Tools" menu, if present.
1422 ;; - `folding-mouse-open-close' renamed to
1423 ;; `folding-mouse-context-sensitive'.
1424 ;; - New entry `other' in `folding-behave-table' which defaults to
1425 ;; `folding-calling-original'.
1426 ;; - `folding-calling-original' now gets the event from `last-input-event'
1427 ;; if called without arguments (i.e. the way `folding-act' calls it.)
1428 ;; - XEmacs mouse support added.
1429 ;; - `folding-mouse-call-original' can call functions with or without
1430 ;; the Event argument.
1431 ;; - Byte compiler generates no errors neither for Emacs 19 and XEmacs.
1432 ;;
1433 ;; Aug 24 1995 19.28 [jari 1.17]
1434 ;; - To prevent infinite back calling loop, Anders suggested smart way
1435 ;; to detect that func call chain is started only once.
1436 ;; folding-calling-original :+ v, call chain terminator
1437 ;; "Internal" :! v, all private vars have this string
1438 ;; folding-mouse-call-original :! v, stricter chain check.
1439 ;; "copyright" :! t, newer notice
1440 ;; "commentary" :! t, ripped non-supported emacsen
1441 ;;
1442 ;; Aug 24 1995 19.28 [jari 1.16]
1443 ;; ** mouse interface rewritten
1444 ;; - Anders gave many valuable comments about simplifying the mouse usage,
1445 ;; he suggested that every mouse function should accept standard event,
1446 ;; and it should be called directly.
1447 ;; folding-global :- v, not needed
1448 ;; folding-mode-off-hook :- v, not needed
1449 ;; folding-mouse-action-table :- v, not needed any more
1450 ;; folding-default-keys-function :+ v, key settings
1451 ;; folding-default-mouse-keys-function:+ v, key settings
1452 ;; folding-mouse :- f, unnecessary
1453 ;; 'all mouse funcs' :! f, now accept "e" parameter
1454 ;; folding-default-keys :+ f, defines keys
1455 ;; folding-mouse-call-original :+ f, call orig mouse func
1456 ;; "examples" :! t, radical rewrote, only one left
1457 ;;
1458 ;; Aug 24 1995 19.28 [jari 1.15]
1459 ;; - some minor changes. If we're inside a fold, Mouse-3 will go one
1460 ;; level up if it points END or BEG marker.
1461 ;; folding-mouse-yank-at-point:! v, added 'up 'down
1462 ;; folding-mark-look-at :! f, more return values: '11 and 'end-in
1463 ;; folding-open-close :! f, bug, didn't exit if inside fold
1464 ;; PMIN, PMAX, NEXTP, add-l :+ more macros fom tinylibm.el
1465 ;;
1466 ;; Aug 23 1995 19.28 [andersl 1.14]
1467 ;; - Added `eval-when-compile' around 1.13 byte-compiler fix
1468 ;; to avoid code to be executed when using a byte-compiled version
1469 ;; of folding.el.
1470 ;; - Binds mode keys via `minor-mode-map-alist'
1471 ;; (i.e. `folding-merge-keymaps' is not used in modern Emacsen.)
1472 ;; This means that the user can not bind `folding-mode-map' to a new
1473 ;; keymap, \\(s\\|\\)he must modify the existing one.
1474 ;; - `defvars' for global feature test variables `folding-*-p'.
1475 ;; - `folding-mouse-open-close' now detects when the current fold was been
1476 ;; pressed. (The "current" is the fold around which the buffer is
1477 ;; narrowed.)
1478 ;;
1479 ;; Aug 23 1995 19.28 [jari 1.13]
1480 ;; - 19.28 Byte compile doesn't handle fboundp, boundp well. That's a bug.
1481 ;; Set some dummy functions to get cleaner output.
1482 ;; - The folding-mode-off doesn't seem very useful, because it
1483 ;; is never run when another major-mode is turned on ... maybe we should
1484 ;; utilize kill-all-local-variables-hooks with defadvice around
1485 ;; kill-all-local-variables ...
1486 ;;
1487 ;; folding-emacs-version :+ added. it was in the docs, but not defined
1488 ;; kill-all-local-variables-hooks :! v, moved to variable section
1489 ;; list-buffers-mode-alist :! v, --''--
1490 ;; "compiler hacks" :+ section added
1491 ;; "special" :+ section added
1492 ;; "Compatibility" :! moved at the beginning
1493 ;;
1494 ;; Aug 22 1995 19.28 [jari 1.12]
1495 ;; - Only minor changes
1496 ;; BOLP, BOLPP, EOLP, EOLPP :+ f, macros added from tinylibm.el
1497 ;; folding-mouse-pick-move :! f, when cursor at beolp, move always up
1498 ;; "bindings" :+ added C-cv and C-cC-v
1499 ;;
1500 ;; Aug 22 1995 19.28 [jari 1.11]
1501 ;; - Inspired by mouse so much, that this revision contain substantial
1502 ;; changes and enhancements. Mouse is now powered!
1503 ;; - Anders wanted mouse to operate according to 'mouse cursor', not
1504 ;; current 'point'.
1505 ;; folding-mouse-yank-at-point: controls it. Phwew, I like this
1506 ;; one a lot.
1507 ;;
1508 ;; examples :! t, totally changed, now 2 choices
1509 ;; folding-mode-off-hook :+ v, when folding ends
1510 ;; folding-global :+ v, global store value
1511 ;; folding-mouse-action-table :! v, changed
1512 ;; folding-mouse :! f, stores event to global
1513 ;; folding-mouse-open-close :! f, renamed, mouse activated open
1514 ;; folding-mode :! f, added 'off' hook
1515 ;; folding-event-posn :+ f, handles FSF mouse event
1516 ;; folding-mouse-yank-at-p :+ f, check which mouse mode is on
1517 ;; folding-mouse-point :+ f, return working point
1518 ;; folding-mouse-move :+ f, mouse moving down , obsolete ??
1519 ;; folding-mouse-pick-move :+ f, mouse move accord. fold mark
1520 ;; folding-next-visible-heading :+ f, from tinyfold.el
1521 ;; folding-previous-visible-heading :+ f, from tinyfold.el
1522 ;; folding-pick-move :+ f, from tinyfold.el
1523 ;;
1524 ;;
1525 ;; Aug 22 1995 19.28 [jari 1.10]
1526 ;; - Minor typing errors corrected : fol-open-close 'hide --> 'close
1527 ;; This caused error when trying to close open fold with mouse
1528 ;; when cursor was sitting on fold marker.
1529 ;;
1530 ;; Aug 22 1995 19.28 [jari 1.9]
1531 ;; - Having heard good suggestions from Anders...!
1532 ;; "install" : add-hook for folding missed
1533 ;; folding-open-close : generalized
1534 ;; folding-behave-table : NEW, logical behavior control
1535 ;; folding-:mouse-action-table : now folding-mouse-action-table
1536 ;;
1537 ;; - The mouse function seems to work with FSF emacs only, because
1538 ;; XEmacs doesn't know about double or triple clicks. We're working
1539 ;; on the problem...
1540 ;;
1541 ;; Aug 21 1995 19.28 [jari 1.8]
1542 ;; - Rearranged the file structure so that all variables are at the
1543 ;; beginning of file. With new functions, it easy to open-close
1544 ;; fold. Added word "code:" or "setup:" to the front of code folds,
1545 ;; so that the toplevel folds can be recognized more easily.
1546 ;; - Added example hook to install section for easy mouse use.
1547 ;; - Added new functions.
1548 ;; folding-get-mode-marks : return folding marks
1549 ;; folding-mark-look-at : status of current line, fold mark in it?
1550 ;; folding-mark-mouse : execute action on fold mark
1551 ;;
1552 ;;
1553 ;; Aug 17 1995 19.28/X19.12 [andersl 1.7]
1554 ;; - Failed when loaded into XEmacs, when `folding-mode-map' was
1555 ;; undefined. Folding marks for three new major modes added:
1556 ;; rexx-mode, erlang-mode and xerl-mode.
1557 ;;
1558 ;; Aug 14 1995 19.28 [jari 1.6]
1559 ;; - After I met Anders we exchanged some thoughts about usage philosophy
1560 ;; of error and signal commands. I was annoyed by the fact that they
1561 ;; couldn't be suppressed, when the error was "minor". Later Anders
1562 ;; developed fdb.el, which will be integrated to FSF 19.30. It
1563 ;; offers by-passing error/signal interference.
1564 ;; --> I changed back all the error commands that were taken away.
1565 ;;
1566 ;; Jun 02 1995 19.28 [andersl]
1567 ;; - "Narrow" not present in mode-line when in folding-mode.
1568 ;;
1569 ;; May 12 1995 19.28 [jari 1.5]
1570 ;; - Installation text cleaned: reference to 'install-it' removed,
1571 ;; because such function doesn't exist any more. The installation is
1572 ;; now automatic: it's done when user calls folding mode first time.
1573 ;; - Added 'private vars' section. made 'outside all folds' message
1574 ;; informational, not an error.
1575 ;;
1576 ;; May 12 1995 19.28 [jackr x.x]
1577 ;; - Corrected 'broken menu bar' problem.
1578 ;; - Even though make-sparse-keymap claims its argument (a string to
1579 ;; name the menu) is optional, it's not. Lucid has other
1580 ;; arrangements for the same thing..
1581 ;;
1582 ;; May 10 1995 19.28 [jari 1.2]
1583 ;; - Moved provide to the end of file.
1584 ;; - Rearranged code so that the common functions are at the beginning.
1585 ;; Reprogrammed the whole installation with hooks. Added Write file
1586 ;; hook that makes sure you don't write in 'binary' while folding were
1587 ;; accidentally off.
1588 ;; - Added regexp text for certain files which are not allowed to
1589 ;; 'auto fold' when loaded.
1590 ;; - changed some 'error' commands to 'messages', this prevent screen
1591 ;; mixup when debug-on-error is set to t
1592 ;; + folding-list-delete , folding-msg , folding-mode-find-file ,
1593 ;; folding-mode-write-file , folding-check-folded , folding-keep-hooked
1594 ;;
1595 ;; 1.7.4 May 04 1995 19.28 [jackr 1.11]
1596 ;; - Some compatibility changes:
1597 ;; v.18 doesn't allow an arg to make-sparse-keymap
1598 ;; testing epoch::version is trickier than that
1599 ;; free-variable reference cleanup
1600 ;;
1601 ;; 1.7.3 May 04 1995 19.28 [jari]
1602 ;; - Corrected folding-mode-find-file-hook , so that it has more
1603 ;; 'mode turn on' capabilities through user function
1604 ;; + folding-mode-write-file-hook: Makes sure your file is saved
1605 ;; properly, so that you don't end up saving in 'binary'.
1606 ;; + folding-check-folded: func, default checker provided
1607 ;; + folding-check-folded-file-function variable added, User can put his
1608 ;; 'detect folding.el file' methods here.
1609 ;; + folding-mode-install-it: func, Automatic installation with it
1610 ;;
1611 ;; 1.7.2 Apr 01 1995 19.28 [jackr] , Design support by [jari]
1612 ;; - Added folding to FSF & XEmacs menus
1613 ;;
1614 ;; 1.7.1 Apr 28 1995 19.28 [jackr]
1615 ;; - The folding editor's merge-keymap couldn't handle FSF menu-bar,
1616 ;; so some minor changes were made, previous is '>' and enhancements
1617 ;; are '>'
1618 ;;
1619 ;; < (buffer-disable-undo new-buffer)
1620 ;; ---
1621 ;; > (buffer-flush-undo new-buffer)
1622 ;; 1510,1512c1510
1623 ;; < key (if (symbolp keycode)
1624 ;; < (vector keycode)
1625 ;; < (char-to-string keycode))
1626 ;; ---
1627 ;; > key (char-to-string keycode)
1628 ;; 1802,1808d1799
1629 ;; < ;;{{{ Compatibility hacks for various Emacs versions
1630 ;; <
1631 ;; < (or (fboundp 'buffer-disable-undo)
1632 ;; < (fset 'buffer-disable-undo (symbol-function 'buffer-flush-undo)))
1633 ;; <
1634 ;; < ;;}}}
1635 ;;
1636 ;; X.x Dec 1 1994 19.28 [jari]
1637 ;; - Only minor change. Made the folding mode string user configurable.
1638 ;; Added these variables:
1639 ;; folding-mode-string, folding-inside-string,folding-inside-mode-name
1640 ;; - Changed revision number from 1.6.2 to 1.7 , so that people know
1641 ;; this package has changed.
1642
1643 ;;}}}
1644
1645 ;;; Code:
1646
1647 ;;{{{ setup: require packages
1648
1649 (eval-when-compile
1650 (or (require 'cl-lib nil 'noerr) ;; Emacs 29.x
1651 (require 'cl)))
1652
1653 (eval-and-compile
1654 (autoload 'font-lock-fontify-region "font-lock")
1655 ;; Forward declaration
1656 (defvar global-font-lock-mode))
1657
1658 (require 'easymenu)
1659
1660 (defvar folding-package-url-location
1661 "https://github.com/jaalto/project-emacs--folding-mode.")
1662
1663 ;;}}}
1664 ;;{{{ setup: byte compiler hacks
1665
1666 ;;; - This really only should be evaluated in case we're about to byte
1667 ;;; compile this file. Since `eval-when-compile' is evaluated when
1668 ;;; the uncompiled version is used (great!) we test if the
1669 ;;; byte-compiler is loaded.
1670
1671 ;; Make sure `advice' is loaded when compiling the code.
1672
1673 (eval-and-compile
1674 (require 'advice)
1675 (defvar folding-xemacs-p (or (boundp 'xemacs-logo)
1676 (featurep 'xemacs))
1677 "Folding determines which emacs version it is running. t if Xemacs.")
1678 ;; By loading overlay.el package, it clears some byte compiler messages.
1679 ;; By default folding does not use overlay code.
1680 (if folding-xemacs-p
1681 (or (fboundp 'overlay-start) ;; Already loaded
1682 (load "overlay" 'noerr) ;; No? Try loading it.
1683 (message "\
1684 ** folding.el: XEmacs 19.15+ has package overlay.el, try to get it.
1685 This is only a warning. Folding does not use overlays by
1686 default. You can safely ignore possible overlay byte
1687 compilation error
1688 messages."))))
1689
1690 (eval-when-compile
1691 ;; ARGS: (symbol variable-p library)
1692 (defadvice find-function-search-for-symbol (around folding act)
1693 "Set folding flag for `find-file-noselect' to open all folds."
1694 (let ((file (ad-get-arg 2)))
1695 (when file
1696 (message "FILE %s" file)
1697 (put 'find-file-noselect 'folding file)))
1698 ad-do-it
1699 (put 'find-file-noselect 'folding nil))
1700
1701 (defun folding-find-file-noselect ()
1702 (let* ((file (get 'find-file-noselect 'folding))
1703 (buffer (and file
1704 ;; It may be absolute path name, file.el,
1705 ;; or just "file".
1706 (or (find-buffer-visiting file)
1707 (get-buffer file)
1708 (get-buffer (concat file ".el"))))))
1709 (when buffer
1710 (with-current-buffer buffer
1711 (when (and (boundp 'folding-mode)
1712 (symbol-value 'folding-mode)) ;; Byte compiler silencer
1713 (turn-off-folding-mode))))))
1714
1715 ;; See find.func.el find-function-search-for-symbol
1716 ;; Make C-h f and mouse-click work to jump to a file. Folding mode
1717 ;; Must be turned off due to regexps in find.func.el that can't
1718 ;; search ^M lines.
1719
1720 (defadvice find-file-noselect (after folding act)
1721 "When called by `find-function-search-for-symbol', turn folding off."
1722 (folding-find-file-noselect))
1723 ) ; eval-when-compile
1724
1725 (defsubst folding-preserve-active-region ()
1726 "In XEmacs keep the region alive. In Emacs do nothing."
1727 (if (boundp 'zmacs-region-stays) ;Keep regions alive
1728 (set 'zmacs-region-stays t))) ;use `set' to Quiet Emacs Byte Compiler
1729
1730 (defvar folding-mode) ;; Byte Compiler silencer
1731
1732 (when (locate-library "mode-motion") ;; XEmacs
1733 (defun folding-mode-motion-highlight-fold (event)
1734 "Highlight line under mouse if it has a foldmark."
1735 (when folding-mode
1736 (funcall
1737 ;; Emacs Byte Compiler Shutup fix
1738 (symbol-function 'mode-motion-highlight-internal)
1739 event
1740 (lambda ()
1741 (beginning-of-line)
1742 (if (folding-mark-look-at)
1743 (search-forward-regexp "^[ \t]*")))
1744 (lambda ()
1745 (if (folding-mark-look-at)
1746 (end-of-line))))))
1747 (require 'mode-motion)
1748 (add-hook 'mode-motion-hook 'folding-mode-motion-highlight-fold 'at-end))
1749
1750 ;;}}}
1751
1752 ;;{{{ setup: some variable
1753
1754 ;; This is a list of structures which keep track of folds being entered
1755 ;; and exited. It is a list of (MARKER . MARKER) pairs, followed by the
1756 ;; symbol `folded'. The first of these represents the fold containing
1757 ;; the current one. If the view is currently outside all folds, this
1758 ;; variable has value nil.
1759
1760 (defvar folding-stack nil
1761 "Internal. A list of marker pairs representing folds entered so far.")
1762
1763 (defvar folding-version (substring "$Revision: 3.42 $" 11 15)
1764 "Version number of folding.el.")
1765
1766 ;;}}}
1767 ;;{{{ setup: bind
1768
1769 (defgroup folding nil
1770 "Managing buffers with Folds."
1771 :group 'tools)
1772
1773 (defcustom folding-mode-prefix-key "\C-c@"
1774 "*Prefix key to use for Folding commands in Folding mode."
1775 :type 'string
1776 :group 'folding)
1777
1778 (defcustom folding-goto-key "\M-g"
1779 "*Key to be bound to `folding-goto-line' in folding mode.
1780 The default value is M - g, but you probably don't want folding to
1781 occupy it if you have used M - g got `goto-line'."
1782 :type 'string
1783 :group 'folding)
1784
1785 (defcustom folding-font-lock-begin-mark 'font-lock-reference-face
1786 "Face to highlight beginning fold mark."
1787 :type 'face
1788 :group 'folding)
1789
1790 (defcustom folding-font-lock-end-mark 'font-lock-reference-face
1791 "Face to highlight end fold mark."
1792 :type 'face
1793 :group 'folding)
1794
1795 (defvar folding-mode-map nil
1796 "Keymap used in Folding mode (a minor mode).")
1797
1798 (defvar folding-mode-prefix-map nil
1799 "Keymap used in Folding mode keys sans `folding-mode-prefix-key'.")
1800
1801 ;;;###autoload
1802 (defvar folding-mode nil
1803 "When Non nil, Folding mode is active in the current buffer.")
1804
1805 (make-variable-buffer-local 'folding-mode)
1806 (set-default 'folding-mode nil)
1807
1808 (defmacro folding-kbd (key function)
1809 "Folding: define KEY with FUNCTION to `folding-mode-prefix-map'.
1810 This is used when assigning keybindings to `folding-mode-map'.
1811 See also `folding-mode-prefix-key'."
1812 `(define-key
1813 folding-mode-prefix-map
1814 ,key ,function))
1815
1816 (put 'folding-labels 'lisp-indent-function 0)
1817 (put 'folding-labels 'edebug-form-spec '(body))
1818 (defmacro folding-labels (&rest body)
1819 "Call `cl-labels' if available."
1820 (if (fboundp 'cl-labels)
1821 `(cl-labels ,@body)
1822 `(labels ,@body)))
1823
1824 (defmacro folding-exit-minibuffer ()
1825 "Select `eval-buffer' or `eval-current-buffer'."
1826 (if (fboundp 'exit-minibuffer)
1827 `(exit-minibuffer) ;; Emacs 24.4+
1828 `(isearch-nonincremental-exit-minibuffer)))
1829
1830 ;; (put 'folding-eval-buffer 'lisp-indent-function 0)
1831 (put 'folding-eval-buffer 'edebug-form-spec '(body))
1832 (defmacro folding-eval-buffer (&rest args)
1833 "Select `eval-buffer' or `eval-current-buffer'."
1834 (if (fboundp 'eval-buffer)
1835 `(eval-buffer ,@args)
1836 `(eval-curren-buffer ,@args))) ;; Old Emacs
1837
1838 (defmacro folding-called-interactively-p ()
1839 "Return t if interactive."
1840 (if (fboundp 'called-interactively-p)
1841 `(called-interactively-p 'interactive) ;; Emacs 23+
1842 `(not (or executing-kbd-macro noninteractive))))
1843
1844 (defun folding-bind-default-mouse ()
1845 "Bind default mouse keys used by Folding mode."
1846 (interactive)
1847 (cond
1848 (folding-xemacs-p
1849 (define-key folding-mode-map [(button3)]
1850 'folding-mouse-context-sensitive)
1851 ;; (define-key folding-mode-map '(double button3) 'folding-hide-current-entry)
1852 (define-key folding-mode-map [(control shift button2)]
1853 'folding-mouse-pick-move))
1854 (t
1855 (define-key folding-mode-map [mouse-3] 'folding-mouse-context-sensitive)
1856 (define-key folding-mode-map [C-S-mouse-2] 'folding-mouse-pick-move))))
1857
1858 (defun folding-bind-terminal-keys ()
1859 "In non-window system, rebind C - f and C - b as folding-{forward,backward}-char."
1860 (unless (or (and (boundp 'window-system) ;; Emacs
1861 (symbol-value 'window-system)) ;; Byte compiler silencer
1862 (and (fboundp 'console-type) ;; XEmacs
1863 (let ((val (fboundp 'console-type)))
1864 (not (eq 'tty val)))))
1865 (define-key folding-mode-map "\C-f" 'folding-forward-char)
1866 (define-key folding-mode-map "\C-b" 'folding-backward-char)))
1867
1868 (defun folding-bind-default-keys ()
1869 "Bind the default keys used the `folding-mode'.
1870
1871 The variable `folding-mode-prefix-key' contains the prefix keys,
1872 the default is C - c @.
1873
1874 For the good ol' key bindings, please use the function
1875 `folding-bind-backward-compatible-keys' instead."
1876 (interactive)
1877 (define-key folding-mode-map folding-goto-key 'folding-goto-line)
1878 (folding-bind-terminal-keys)
1879 (define-key folding-mode-map "\C-e" 'folding-end-of-line)
1880 (folding-kbd "\C-f" 'folding-fold-region)
1881 (folding-kbd ">" 'folding-shift-in)
1882 (folding-kbd "<" 'folding-shift-out)
1883 (folding-kbd "\C-t" 'folding-show-all)
1884 (folding-kbd "\C-s" 'folding-show-current-entry)
1885 (folding-kbd "\C-x" 'folding-hide-current-entry)
1886 (folding-kbd "\C-o" 'folding-open-buffer)
1887 (folding-kbd "\C-w" 'folding-whole-buffer)
1888 (folding-kbd "\C-r" 'folding-convert-buffer-for-printing)
1889 (folding-kbd "\C-k" 'folding-marks-kill)
1890 (folding-kbd "\C-v" 'folding-pick-move)
1891 (folding-kbd "v" 'folding-previous-visible-heading)
1892 (folding-kbd " " 'folding-next-visible-heading)
1893 (folding-kbd "." 'folding-context-next-action)
1894 ;; C-u: kinda "up" -- "down"
1895 (folding-kbd "\C-u" 'folding-toggle-enter-exit)
1896 (folding-kbd "\C-q" 'folding-toggle-show-hide)
1897 ;; Think "#" as a 'fence'
1898 (folding-kbd "#" 'folding-region-open-close)
1899 ;; Esc-; is the standard emacs commend add key.
1900 (folding-kbd ";" 'folding-comment-fold)
1901 (folding-kbd "%" 'folding-convert-to-major-folds)
1902 (folding-kbd "/" 'folding-all-comment-blocks-in-region)
1903 (folding-kbd "\C-y" 'folding-show-current-subtree)
1904 (folding-kbd "\C-z" 'folding-hide-current-subtree)
1905 (folding-kbd "\C-n" 'folding-display-name)
1906 (folding-kbd "I" 'folding-insert-advertise-folding-mode))
1907
1908 (defun folding-bind-backward-compatible-keys ()
1909 "Bind keys traditionally used by Folding mode.
1910 For bindings which follow newer Emacs minor mode conventions, please
1911 use the function `folding-bind-default-keys'.
1912
1913 This function sets `folding-mode-prefix-key' to `C-c'."
1914 (interactive)
1915 (setq folding-mode-prefix-key "\C-c")
1916 (folding-bind-default-keys))
1917
1918 (defun folding-bind-outline-compatible-keys ()
1919 "Bind keys used by the minor mode `folding-mode'.
1920 The keys used are as much as possible compatible with
1921 bindings used by Outline mode.
1922
1923 Currently, some outline mode functions doesn't have a corresponding
1924 folding function.
1925
1926 The variable `folding-mode-prefix-key' contains the prefix keys,
1927 the default is C - c @.
1928
1929 For the good ol' key bindings, please use the function
1930 `folding-bind-backward-compatible-keys' instead."
1931 (interactive)
1932 ;; Traditional keys:
1933 (folding-bind-terminal-keys)
1934 (define-key folding-mode-map "\C-e" 'folding-end-of-line)
1935 ;; Mimic Emacs 20.3 allout.el bindings
1936 (folding-kbd ">" 'folding-shift-in)
1937 (folding-kbd "<" 'folding-shift-out)
1938 (folding-kbd "\C-n" 'folding-next-visible-heading)
1939 (folding-kbd "\C-p" 'folding-previous-visible-heading)
1940 ;; ("\C-u" outline-up-current-level)
1941 ;; ("\C-f" outline-forward-current-level)
1942 ;; ("\C-b" outline-backward-current-level)
1943 ;; (folding-kbd "\C-i" 'folding-show-current-subtree)
1944 (folding-kbd "\C-s" 'folding-show-current-subtree)
1945 (folding-kbd "\C-h" 'folding-hide-current-subtree)
1946 (folding-kbd "\C-k" 'folding-marks-kill)
1947 (folding-kbd "!" 'folding-show-all)
1948 (folding-kbd "\C-d" 'folding-hide-current-entry)
1949 (folding-kbd "\C-o" 'folding-show-current-entry)
1950 ;; (" " outline-open-sibtopic)
1951 ;; ("." outline-open-subtopic)
1952 ;; ("," outline-open-supertopic)
1953 ;; Other bindings not in allout.el
1954 (folding-kbd "\C-a" 'folding-open-buffer)
1955 (folding-kbd "\C-q" 'folding-whole-buffer)
1956 (folding-kbd "\C-r" 'folding-convert-buffer-for-printing)
1957 (folding-kbd "\C-w" 'folding-fold-region)
1958 (folding-kbd "I" 'folding-insert-advertise-folding-mode))
1959
1960 ;;{{{ goto-line (advice)
1961
1962 (defcustom folding-advice-instantiate t
1963 "*In non-nil install advice code. Eg for `goto-line'."
1964 :type 'boolean
1965 :group 'folding)
1966
1967 (defcustom folding-shift-in-on-goto t
1968 "*Flag in folding adviced function `goto-line'.
1969 If non-nil, folds are entered when going to a given line.
1970 Otherwise the buffer is unfolded. Can also be set to \\='show.
1971 This variable is used only if `folding-advice-instantiate' was
1972 non-nil when folding was loaded.
1973
1974 See also `folding-goto-key'."
1975 :type 'boolean
1976 :group 'folding)
1977
1978 (defvar folding-narrow-by-default t
1979 "If t (default) things like isearch will enter folds. If nil the
1980 folds will be opened, but not entered.")
1981
1982 (when folding-advice-instantiate
1983 (eval-when-compile (require 'advice))
1984 ;; By Robert Marshall <rxmarsha A T bechtel com>
1985 (defadvice goto-line (around folding-goto-line first activate)
1986 "Go to line ARG, entering folds if `folding-shift-in-on-goto' is t.
1987 It attempts to keep the buffer in the same visibility state as before."
1988 (let () ;; (oldposn (point))
1989 ad-do-it
1990 (if (and folding-mode
1991 (or (folding-point-folded-p (point))
1992 (<= (point) (point-min-marker))
1993 (>= (point) (point-max-marker))))
1994 (let ((line (ad-get-arg 0)))
1995 (if folding-shift-in-on-goto
1996 (progn
1997 (folding-show-all)
1998 (goto-char 1)
1999 (and (< 1 line)
2000 (not (folding-use-overlays-p))
2001 (re-search-forward "[\n\C-m]" nil 0 (1- line)))
2002 (let ((goal (point)))
2003 (while (prog2 (beginning-of-line)
2004 (if folding-shift-in-on-goto
2005 (progn
2006 (folding-show-current-entry t t)
2007 (folding-point-folded-p goal))
2008 (folding-shift-in t))
2009 (goto-char goal)))
2010 (folding-narrow-to-region
2011 (and folding-narrow-by-default (point-min))
2012 (point-max) t)))
2013 (if (or folding-stack (folding-point-folded-p (point)))
2014 (folding-open-buffer))))))))
2015
2016 ;;}}}
2017
2018 (defun folding-bind-foldout-compatible-keys ()
2019 "Bind keys for `folding-mode' compatible with Foldout mode.
2020
2021 The variable `folding-mode-prefix-key' contains the prefix keys,
2022 the default is C - c @."
2023 (interactive)
2024 (folding-kbd "\C-z" 'folding-shift-in)
2025 (folding-kbd "\C-x" 'folding-shift-out))
2026
2027 ;;; This function is here, just in case we ever would like to add
2028 ;;; `hideif' support to folding mode. Currently, it is only used to
2029 ;;; which keys shouldn't be used.
2030
2031 ;;(defun folding-bind-hideif-compatible-keys ()
2032 ;; "Bind keys for `folding-mode' compatible with Hideif mode.
2033 ;;
2034 ;;The variable `folding-mode-prefix-key' contains the prefix keys,
2035 ;;the default is C-c@."
2036 ;; (interactive)
2037 ;; ;; Keys defined by `hideif'
2038 ;; ;; (folding-kbd "d" 'hide-ifdef-define)
2039 ;; ;; (folding-kbd "u" 'hide-ifdef-undef)
2040 ;; ;; (folding-kbd "D" 'hide-ifdef-set-define-alist)
2041 ;; ;; (folding-kbd "U" 'hide-ifdef-use-define-alist)
2042 ;;
2043 ;; ;; (folding-kbd "h") 'hide-ifdefs)
2044 ;; ;; (folding-kbd "s") 'show-ifdefs)
2045 ;; ;; (folding-kbd "\C-d") 'hide-ifdef-block)
2046 ;; ;; (folding-kbd "\C-s") 'show-ifdef-block)
2047 ;;
2048 ;; ;; (folding-kbd "\C-q" 'hide-ifdef-toggle-read-only)
2049 ;; )
2050
2051 ;;; .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .
2052
2053 ;; Not used for modern Emacsen.
2054 (defvar folding-saved-local-keymap nil
2055 "Keymap used to save non-folding keymap.
2056 (so it can be restored when folding mode is turned off.)")
2057
2058 ;;;###autoload
2059 (defcustom folding-default-keys-function 'folding-bind-default-keys
2060 "*Function or list of functions used to define keys for Folding mode.
2061 Possible values are:
2062 folding-bind-default-key
2063 The standard keymap.
2064
2065 `folding-bind-backward-compatible-keys'
2066 Keys used by older versions of Folding mode. This function
2067 does not conform to Emacs 19.29 style conversions concerning
2068 key bindings. The prefix key is C - c
2069
2070 `folding-bind-outline-compatible-keys'
2071 Define keys compatible with Outline mode.
2072
2073 `folding-bind-foldout-compatible-keys'
2074 Define some extra keys compatible with Foldout.
2075
2076 All except `folding-bind-backward-compatible-keys' used the value of
2077 the variable `folding-mode-prefix-key' as prefix the key.
2078 The default is C - c @"
2079 :type 'function
2080 :group 'folding)
2081
2082 ;; Not yet implemented:
2083 ;; folding-bind-hideif-compatible-keys
2084 ;; Define some extra keys compatible with hideif.
2085
2086 ;;;###autoload
2087 (defcustom folding-default-mouse-keys-function 'folding-bind-default-mouse
2088 "*Function to bind default mouse keys to `folding-mode-map'."
2089 :type 'function
2090 :group 'folding)
2091
2092 (defvar folding-mode-menu nil
2093 "Keymap containing the menu for Folding mode.")
2094
2095 (defvar folding-mode-menu-name "Fld" ;; Short menu name
2096 "Name of pull down menu.")
2097
2098 ;;}}}
2099 ;;{{{ setup: hooks
2100
2101 (defcustom folding-mode-hook nil
2102 "*Hook called when Folding mode is entered.
2103
2104 A hook named `<major-mode>-folding-hook' is also called, if it
2105 exists. Eg., `c-mode-folding-hook' is called whenever Folding mode is
2106 started in C mode."
2107 :type 'hook
2108 :group 'folding)
2109
2110 (defcustom folding-load-hook nil
2111 "*Hook run when file is loaded."
2112 :type 'hook
2113 :group 'folding)
2114
2115 ;;}}}
2116 ;;{{{ setup: user config
2117
2118 ;; Q: should this inherit mouse-yank-at-point's value? maybe not.
2119 (defvar folding-mouse-yank-at-point t
2120 "If non-nil, mouse activities are done at point instead of mouse cursor.
2121 Behaves like `mouse-yank-at-point'.")
2122
2123 (defcustom folding-folding-on-startup t
2124 "*If non-nil, buffers are folded when starting Folding mode."
2125 :type 'boolean
2126 :group 'folding)
2127
2128 (defcustom folding-internal-margins 1
2129 "*Number of blank lines left next to fold mark when tidying folds.
2130
2131 This variable is local to each buffer. To set the default value for all
2132 buffers, use `set-default'.
2133
2134 When exiting a fold, and at other times, `folding-tidy-inside' is invoked
2135 to ensure that the fold is in the correct form before leaving it. This
2136 variable specifies the number of blank lines to leave between the
2137 enclosing fold marks and the enclosed text.
2138
2139 If this value is nil or negative, no blank lines are added or removed
2140 inside the fold marks. A value of 0 (zero) is valid, meaning leave no
2141 blank lines.
2142
2143 See also `folding-tidy-inside'."
2144 :type 'boolean
2145 :group 'folding)
2146
2147 (make-variable-buffer-local 'folding-internal-margins)
2148
2149 (defvar folding-mode-string " Fld"
2150 "Buffer-local variable that hold the fold depth description.")
2151
2152 (set-default 'folding-mode-string " Fld")
2153
2154 ;; Sets `folding-mode-string' appropriately. This allows the Folding mode
2155 ;; description in the mode line to reflect the current fold depth.
2156
2157 (defconst folding-inside-string " " ; was ' inside ',
2158 "Mode line addition to show inside levels of fold.")
2159
2160 ;;;###autoload
2161 (defcustom folding-inside-mode-name "Fld"
2162 "*Mode line addition to show inside levels of fold."
2163 :type 'string
2164 :group 'folding)
2165
2166 (defcustom folding-check-folded-file-function
2167 'folding-check-folded
2168 "*Function that return t or nil after examining if the file is folded."
2169 :type 'function
2170 :group 'folding)
2171
2172 (defcustom folding-check-allow-folding-function
2173 'folding-check-if-folding-allowed
2174 "*Function that return t or nil after deciding if automatic folding."
2175 :type 'function
2176 :group 'folding)
2177
2178 ;;;###autoload
2179 (defcustom folding-mode-string "Fld"
2180 "*The minor mode string displayed when mode is on."
2181 :type 'string
2182 :group 'folding)
2183
2184 ;;;###autoload
2185 (defcustom folding-mode-hook-no-regexp "RMAIL"
2186 "*Regexp which disable automatic folding mode turn on for certain files."
2187 :type 'string
2188 :group 'folding)
2189
2190 (defcustom folding-behave-table
2191 '((close folding-hide-current-entry)
2192 (open folding-show-current-entry) ; Could also be `folding-shift-in'.
2193 (up folding-shift-out)
2194 (other folding-mouse-call-original))
2195 "*Table of of logical commands and their associated functions.
2196 If you want fold to behave like `folding-shift-in', when it \"open\"
2197 a fold, you just change the function entry in this table.
2198
2199 Table form:
2200 \\='((LOGICAL-ACTION CMD) ...)"
2201 :type '(repeat
2202 (symbol :tag "logical action")
2203 (function :tag "callback"))
2204 :group 'folding)
2205
2206 ;;;###autoload
2207 (defvar folding-mode-marks-alist nil
2208 "List of (major-mode . fold mark) default combinations to use.
2209 When Folding mode is started, the major mode is checked, and if there
2210 are fold marks for that major mode stored in `folding-mode-marks-alist',
2211 those marks are used by default. If none are found, the default values
2212 of \"{{{ \" and \"}}}\" are used.
2213
2214 Use function `folding-add-to-marks-list' to add more fold marks. The function
2215 also explains the alist use in details.
2216
2217 Use function `folding-set-local-variables' if you change the current mode's
2218 folding marks during the session.")
2219
2220 ;;}}}
2221 ;;{{{ setup: private
2222
2223 (defvar folding-narrow-placeholder nil
2224 "Internal. Mark where \"%n\" used to be in `mode-line-format'.
2225 Must be nil.")
2226
2227 (defvar folding-bottom-mark nil
2228 "Internal marker of the true bottom of a fold.")
2229
2230 (defvar folding-bottom-regexp nil
2231 "Internal. Regexp marking the bottom of a fold.")
2232
2233 (defvar folding-regexp nil
2234 "Internal. Regexp for hunting down the `folding-top-mark' even in comments.")
2235
2236 (defvar folding-secondary-top-mark nil
2237 "Internal. Additional stuff that can be inserted as part of a top marker.")
2238
2239 (defvar folding-top-mark nil
2240 "Internal. The actual string marking the top of a fold.")
2241
2242 (defvar folding-top-regexp nil
2243 "Internal.
2244 Regexp describing the string beginning a fold, possible with
2245 leading comment thingies and like that.")
2246
2247 (defvar folded-file nil
2248 "Enter folding mode when this file is loaded.
2249 (buffer local, use from a local variables list).")
2250
2251 (defvar folding-calling-original nil
2252 "Internal. Non-nil when original mouse binding is executed.")
2253
2254 (defvar folding-narrow-overlays nil
2255 "Internal. Keep the list of overlays.")
2256 (make-variable-buffer-local 'folding-narrow-overlays)
2257
2258 (defcustom folding-allow-overlays nil
2259 "*If non-nil use overlay code. If nil, then selective display is used.
2260 Note, that this code is highly experimental and will not most likely do what
2261 you expect. using value t will not change folding to use overlays
2262 completely. This variable was introduced to experiment with the overlay
2263 interface, but the work never finished and it is unlikely that it
2264 will continued any later time. Folding at present state is designed
2265 too highly for selective display to make the change worthwhile."
2266 :type 'boolean
2267 :group 'folding)
2268
2269 ;;}}}
2270 ;;{{{ Folding install
2271
2272 (defun folding-easy-menu-define ()
2273 "Define folding easy menu."
2274 (interactive)
2275 (easy-menu-define
2276 folding-mode-menu
2277 (if folding-xemacs-p
2278 nil
2279 (list folding-mode-map))
2280 "Folding menu"
2281 (list
2282 folding-mode-menu-name
2283 ["Enter Fold" folding-shift-in t]
2284 ["Exit Fold" folding-shift-out t]
2285 ["Show Fold" folding-show-current-entry t]
2286 ["Hide Fold" folding-hide-current-entry t]
2287 "----"
2288 ["Show Whole Buffer" folding-open-buffer t]
2289 ["Fold Whole Buffer" folding-whole-buffer t]
2290 ["Show subtree" folding-show-current-subtree t]
2291 ["Hide subtree" folding-hide-current-subtree t]
2292 ["Display fold name" folding-display-name t]
2293 "----"
2294 ["Move previous" folding-previous-visible-heading t]
2295 ["Move next" folding-next-visible-heading t]
2296 ["Pick fold" folding-pick-move t]
2297 ["Next action (context)" folding-context-next-action t]
2298 "----"
2299 ["Foldify region" folding-fold-region t]
2300 ["Open or close folds in region" folding-region-open-close t]
2301 ["Open folds to top level" folding-show-all t]
2302 "----"
2303 ["Comment text in fold" folding-comment-fold t]
2304 ["Convert for printing(temp buffer)"
2305 folding-convert-buffer-for-printing t]
2306 ["Convert to major-mode folds" folding-convert-to-major-folds t]
2307 ["Move comments inside folds in region"
2308 folding-all-comment-blocks-in-region t]
2309 ["Delete fold marks in this fold" folding-marks-kill t]
2310 ["Insert folding URL reference"
2311 folding-insert-advertise-folding-mode t]
2312 "----"
2313 ["Toggle enter and exit mode" folding-toggle-enter-exit t]
2314 ["Toggle show and hide" folding-toggle-show-hide t]
2315 "----"
2316 ["Folding mode off" folding-mode t])))
2317
2318 (defun folding-install-keymaps ()
2319 "Install keymaps."
2320 (unless folding-mode-map
2321 (setq folding-mode-map (make-sparse-keymap)))
2322 (unless folding-mode-prefix-map
2323 (setq folding-mode-prefix-map (make-sparse-keymap)))
2324 (if (listp folding-default-keys-function)
2325 (mapc 'funcall folding-default-keys-function)
2326 (funcall folding-default-keys-function))
2327 (funcall folding-default-mouse-keys-function)
2328 (folding-easy-menu-define)
2329 (define-key folding-mode-map
2330 folding-mode-prefix-key folding-mode-prefix-map)
2331 ;; Install the keymap into `minor-mode-map-alist'. The keymap will
2332 ;; be activated as soon as the variable `folding-mode' is set to
2333 ;; non-nil.
2334 (let ((elt (assq 'folding-mode minor-mode-map-alist)))
2335 ;; Always remove old map before adding new definitions.
2336 (if elt
2337 (setq minor-mode-map-alist
2338 (delete elt minor-mode-map-alist)))
2339 (push (cons 'folding-mode folding-mode-map) minor-mode-map-alist))
2340 ;; Update minor-mode-alist
2341 (or (assq 'folding-mode minor-mode-alist)
2342 (push '(folding-mode folding-mode-string) minor-mode-alist))
2343 ;; Needed for XEmacs
2344 (or (fboundp 'buffer-disable-undo)
2345 (fset 'buffer-disable-undo (symbol-function 'buffer-flush-undo))))
2346
2347 (defun folding-uninstall-keymaps ()
2348 "Uninstall keymaps."
2349 (let ((elt (assq 'folding-mode minor-mode-map-alist)))
2350 (if elt
2351 (setq minor-mode-map-alist
2352 (delete elt minor-mode-map-alist)))
2353 (if (setq elt (assq 'folding-mode minor-mode-alist))
2354 (setq minor-mode-alist
2355 (delete elt minor-mode-alist)))
2356 (folding-uninstall-hooks)))
2357
2358 (defun folding-install (&optional uninstall)
2359 "Install or UNINSTALL folding."
2360 (interactive "P")
2361 (cond
2362 (uninstall
2363 (folding-uninstall-keymaps)
2364 (folding-uninstall-hooks))
2365 (t
2366 (folding-install-keymaps))))
2367
2368 (defun folding-uninstall ()
2369 "Uninstall folding."
2370 (interactive)
2371 (folding-install 'uninstall)
2372 ;; Unwrap all buffers.
2373 (dolist (buffer (buffer-list))
2374 (with-current-buffer buffer
2375 (goto-char (point-min))
2376 (when (or folding-mode
2377 ;; To be sure, check this at the same time
2378 ;; Somebody may have just done
2379 ;; (setq folding-mode nil), which is bad thing.
2380 ;; Setting variable won't restore the buffer.
2381 (re-search-forward "{{{" nil t))
2382 (turn-off-folding-mode)))))
2383
2384 ;;}}}
2385 ;;{{{ code: misc
2386
2387 (defsubst folding-get-mode-marks (&optional mode)
2388 "Return fold markers for MODE. default is for current `major-mode'.
2389
2390 Return:
2391 \(beg-marker end-marker\)"
2392 (interactive)
2393 (let* (elt)
2394 (unless (setq elt (assq (or mode major-mode)
2395 folding-mode-marks-alist))
2396 (error "Folding error: mode is not in `folding-mode-marks-alist'"))
2397 (list (nth 1 elt) (nth 2 elt) (nth 3 elt))))
2398
2399 (defun folding-region-has-folding-marks-p (beg end)
2400 "Check is there is fold mark in region BEG END."
2401 (save-excursion
2402 (goto-char beg)
2403 (when (memq (folding-mark-look-at) '(1 11))
2404 (goto-char end)
2405 (memq (folding-mark-look-at) '(end end-in)))))
2406
2407 ;;; - Thumb rule: because "{{{" if more meaningful, all returns values
2408 ;;; are of type integerp if it is found.
2409 ;;;
2410 (defun folding-mark-look-at (&optional mode)
2411 "Check status of current line. Does it contain a fold mark?.
2412
2413 MODE
2414
2415 \\='move move over fold mark
2416
2417 Return:
2418
2419 0 1 numberp, line has fold begin mark
2420 0 = closed, 1 = open,
2421 11 = open, we are inside fold, and this is top marker
2422
2423 \\='end end mark
2424
2425 \\='end-in end mark, inside fold, floor marker
2426
2427 nil no fold marks .."
2428 (let* (case-fold-search
2429 (marks (folding-get-mode-marks))
2430 (stack folding-stack)
2431 (bm (regexp-quote (nth 0 marks))) ;begin mark
2432 (em (concat "^[ \t\n]*" (regexp-quote (nth 1 marks))))
2433 (bm-re (concat
2434 (concat "^[ \t\n]*" bm)
2435 (if (and nil
2436 (string=
2437 " " (substring (nth 0 marks)
2438 (length (nth 1 marks)))))
2439 ;; Like "}}} *"
2440 "*"
2441 "")))
2442 ret
2443 point)
2444 (save-excursion
2445 (beginning-of-line)
2446 (cond
2447 ((looking-at bm-re)
2448 (setq point (point))
2449 (cond
2450 ((looking-at (concat "^[ \t\n]*" bm "[^\r\n]*\r")) ;; closed
2451 (setq ret 0))
2452 (t ;; open fold marker
2453 (goto-char (point-min))
2454 (cond
2455 ((and stack ;; we are inside fold
2456 ;; allow spaces
2457 (looking-at (concat "[ \t\n]*" bm)))
2458 (setq ret 11))
2459 (t
2460 (setq ret 1))))))
2461 ((looking-at em)
2462 (setq point (point))
2463 ;; - The stack is a list if we've entered inside fold. There
2464 ;; is no text after fold END mark
2465 ;; - At bol ".*\n[^\n]*" doesn't work but "\n[^\n]*" at eol does??
2466 (cond
2467 ((progn
2468 (end-of-line)
2469 (or (and stack (eobp)) ;normal ending
2470 (and stack ;empty newlines only, no text ?
2471 (not (looking-at "\n[^ \t\n]*")))))
2472 (setq ret 'end-in))
2473 (t ;all rest are newlines
2474 (setq ret 'end))))))
2475 (cond
2476 ((and mode point)
2477 (goto-char point)
2478 ;; This call breaks if there is no marks on the point,
2479 ;; because there is no parameter 'nil t' in call.
2480 ;; --> there is error in this function if that happens.
2481 (beginning-of-line)
2482 (re-search-forward (concat bm "\\|" em))
2483 (backward-char 1)))
2484 ret))
2485
2486 (defsubst folding-mark-look-at-top-mark-p ()
2487 "Check if line contain folding top marker."
2488 (integerp (folding-mark-look-at)))
2489
2490 (defsubst folding-mark-look-at-bottom-mark-p ()
2491 "Check if line contain folding bottom marker."
2492 (symbolp (folding-mark-look-at)))
2493
2494 (defun folding-act (action &optional event)
2495 "Execute logical ACTION based on EVENT.
2496
2497 References:
2498 `folding-behave-table'"
2499 (let* ((elt (assoc action folding-behave-table)))
2500 (if elt
2501 (funcall (nth 1 elt) event)
2502 (error "Folding mode (folding-act): Unknown action %s" action))))
2503
2504 (defun folding-region-open-close (beg end &optional close)
2505 "Open all folds inside region BEG END. Close if optional CLOSE is non-nil."
2506 (interactive "r\nP")
2507 (let* ((func (if (null close)
2508 'folding-show-current-entry
2509 'folding-hide-current-entry))
2510 tmp)
2511 (save-excursion
2512 ;; make sure the beg is first.
2513 (if (> beg end) ;swap order
2514 (setq tmp beg beg end end tmp))
2515 (goto-char beg)
2516 (while (and
2517 ;; the folding-show-current-entry/hide will move point
2518 ;; to beg-of-line So we must move to the end of
2519 ;; line to continue search.
2520 (if (and close
2521 (eq 0 (folding-mark-look-at))) ;already closed ?
2522 t
2523 (funcall func)
2524 (end-of-line)
2525 t)
2526 (folding-next-visible-heading)
2527 (< (point) end))))))
2528
2529 (defun fold-marks-kill ()
2530 "If over fold, open fold and kill beginning and end fold marker.
2531 Return t ot nil if marks were removed."
2532 (interactive)
2533 (if (not (folding-mark-look-at))
2534 (when (folding-called-interactively-p)
2535 (message "Folding: Cursor not over fold. Can't remove fold marks.")
2536 nil)
2537 (cl-destructuring-bind (beg end)
2538 (folding-show-current-entry)
2539 (let ((kill-whole-line t))
2540 ;; must be done in this order, because point moves after kill.
2541 (goto-char end)
2542 (beginning-of-line)
2543 (kill-line)
2544 (goto-char beg)
2545 (beginning-of-line)
2546 (kill-line)
2547 ;; Return status
2548 t))))
2549
2550 (defun folding-hide-current-subtree ()
2551 "Call `folding-show-current-subtree' with argument \\='hide."
2552 (interactive)
2553 (folding-show-current-subtree 'hide))
2554
2555 (defun folding-show-current-subtree (&optional hide)
2556 "Show or HIDE all folds inside current fold.
2557 Point must be over beginning fold mark."
2558 (interactive "P")
2559 (let* ((stat (folding-mark-look-at 'move))
2560 (beg (point))
2561 end)
2562 (cond
2563 ((memq stat '(0 1 11)) ;It's BEG fold
2564 (when (eq 0 stat) ;it was closed
2565 (folding-show-current-entry)
2566 (goto-char beg)) ;folding-pick-move needs point at fold
2567 (save-excursion
2568 (if (folding-pick-move)
2569 (setq end (point))))
2570 (if (and beg end)
2571 (folding-region-open-close beg end hide)))
2572 (t
2573 (if (folding-called-interactively-p)
2574 (message "point is not at fold beginning."))))))
2575
2576 (defun folding-display-name ()
2577 "Show current active fold name."
2578 (interactive)
2579 (let* ((pos (folding-find-folding-mark))
2580 name)
2581 (when pos
2582 (save-excursion
2583 (goto-char pos)
2584 (if (looking-at ".*[{]+") ;Drop "{" mark away.
2585 (setq pos (match-end 0)))
2586 (setq name (buffer-substring
2587 pos
2588 (progn
2589 (end-of-line)
2590 (point))))))
2591 (if name
2592 (message (format "fold:%s" name)))))
2593
2594 ;;}}}
2595 ;;{{{ code: events
2596
2597 (defun folding-event-posn (act event)
2598 "According to ACT read mouse EVENT struct and return data from it.
2599 Event must be simple click, no dragging.
2600
2601 ACT
2602 \\='mouse-point return the mouse cursor point
2603 \\='window return window pointer
2604 \\='col-row return list (col row)"
2605 (cond
2606 ((not folding-xemacs-p)
2607 ;; short Description of FSF mouse event
2608 ;;
2609 ;; EVENT : (mouse-3 (#<window 34 on *scratch*> 128 (20 . 104) -23723628))
2610 ;; event-start : (#<window 34 on *scratch*> 128 (20 . 104) -23723628))
2611 ;; ^^^MP
2612 ;; mp = mouse point
2613 (let* ((el (funcall (symbol-function 'event-start) event)))
2614 (cond
2615 ((eq act 'mouse-point)
2616 (nth 1 el)) ;is there macro for this ?
2617 ((eq act 'window)
2618 (funcall (symbol-function 'posn-window) el))
2619 ((eq act 'col-row)
2620 (funcall (symbol-function 'posn-col-row) el))
2621 (t
2622 (error "Unknown request %s" act)))))
2623 (folding-xemacs-p
2624 (cond
2625 ((eq act 'mouse-point)
2626 (funcall (symbol-function 'event-point) event))
2627 ((eq act 'window)
2628 (funcall (symbol-function 'event-window) event))
2629 ;; Must be tested! (However, it's not used...)
2630 ((eq act 'col-row)
2631 (list (funcall (symbol-function 'event-x) event)
2632 (funcall (symbol-function 'event-y) event)))
2633 (t
2634 (error "Unknown request %s" act))))
2635 (t
2636 (error "This version of Emacs can't handle events."))))
2637
2638 (defmacro folding-interactive-spec-p ()
2639 "Preserve region during `interactive'.
2640 In XEmacs user could also set `zmacs-region-stays'."
2641 (if folding-xemacs-p
2642 ;; preserve selected region
2643 `'(interactive "_p")
2644 `'(interactive "p")))
2645
2646 (defmacro folding-mouse-yank-at-p ()
2647 "Check if user uses \"yank at mouse point\" feature.
2648 Please see the variable `folding-mouse-yank-at-point'."
2649 'folding-mouse-yank-at-point)
2650
2651 (defun folding-mouse-point (&optional event)
2652 "Return mouse's working point. Optional EVENT is mouse click.
2653 When used on XEmacs, return nil if no character was under the mouse."
2654 (if (or (folding-mouse-yank-at-p)
2655 (null event))
2656 (point)
2657 (folding-event-posn 'mouse-point event)))
2658
2659 ;;}}}
2660
2661 ;;{{{ code: hook
2662
2663 (defun folding-is-hooked ()
2664 "Check if folding hooks are installed."
2665 (and (memq 'folding-mode-write-file write-file-functions)
2666 (memq 'folding-mode-find-file find-file-hook)))
2667
2668 ;;;###autoload
2669 (defun folding-uninstall-hooks ()
2670 "Remove hooks set by folding."
2671 (turn-off-folding-mode)
2672 (remove-hook 'finder-mode-hook 'folding-mode)
2673 (remove-hook 'write-file-functions 'folding-mode-write-file)
2674 (remove-hook 'find-file-hook 'folding-mode-find-file))
2675
2676 ;;;###autoload
2677 (defun folding-install-hooks ()
2678 "Install folding hooks."
2679 (folding-mode-add-find-file-hook)
2680 (add-hook 'finder-mode-hook 'folding-mode)
2681 (or (memq 'folding-mode-write-file write-file-functions)
2682 (add-hook 'write-file-functions 'folding-mode-write-file 'end)))
2683
2684 ;;;###autoload
2685 (defun folding-keep-hooked ()
2686 "Make sure hooks are in their places."
2687 (unless (folding-is-hooked)
2688 (folding-uninstall-hooks)
2689 (folding-install-hooks)))
2690
2691 ;;}}}
2692 ;;{{{ code: Mouse handling
2693
2694 (defun folding-mouse-call-original (&optional event)
2695 "Execute original mouse function using mouse EVENT.
2696
2697 Do nothing if original function does not exist.
2698
2699 Does nothing when called by a function which has earlier been called
2700 by us.
2701
2702 Sets global:
2703 `folding-calling-original'"
2704 (interactive "@e") ;; Was "e"
2705 ;; Without the following test we could easily end up in a endless
2706 ;; loop in case we would call a function which would call us.
2707 ;;
2708 ;; (An easy constructed example is to bind the function
2709 ;; `folding-mouse-context-sensitive' to the same mouse button both in
2710 ;; `folding-mode-map' and in the global map.)
2711 (if folding-calling-original
2712 nil
2713 ;; `folding-calling-original' is global
2714 (setq folding-calling-original t)
2715 (unwind-protect
2716 (progn
2717 (or event
2718 (setq event last-input-event))
2719 (let (mouse-key)
2720 (cond
2721 ((not folding-xemacs-p)
2722 (setq mouse-key (make-vector 1 (car-safe event))))
2723 (folding-xemacs-p
2724 (setq mouse-key
2725 (vector
2726 (append
2727 (event-modifiers event)
2728 (list (intern
2729 (format "button%d"
2730 (funcall
2731 (symbol-function 'event-button)
2732 event))))))))
2733 (t
2734 (error "This version of Emacs can't handle events.")))
2735 ;; Test string: http://www.csd.uu.se/~andersl
2736 ;; andersl A T csd uu se
2737 ;; (I have `ark-goto-url' bound to the same key as
2738 ;; this function.)
2739 ;;
2740 ;; turn off folding, so that we can see the real
2741 ;; function behind it.
2742 ;;
2743 ;; We have to restore the current buffer, otherwise the
2744 ;; let* won't be able to restore the old value of
2745 ;; folding-mode. In my environment, I have bound a
2746 ;; function which starts mail when I click on an e-mail
2747 ;; address. When returning, the current buffer has
2748 ;; changed.
2749 (let* ((folding-mode nil)
2750 (orig-buf (current-buffer))
2751 (orig-func (key-binding mouse-key)))
2752 ;; call only if exist
2753 (when orig-func
2754 ;; Check if the original function has arguments. If
2755 ;; it does, call it with the event as argument.
2756 (unwind-protect
2757 (progn
2758 (setq this-command orig-func)
2759 (call-interactively orig-func))
2760 ;;; #untested, but included here for further reference
2761 ;;; (cond
2762 ;;; ((not (string-match "mouse" (symbol-name orig-func)))
2763 ;;; (call-interactively orig-func))
2764 ;;; ((string-match "^mouse" (symbol-name orig-func))
2765 ;;; (funcall orig-func event))
2766 ;;; (t
2767 ;;; ;; Some other package's mouse command,
2768 ;;; ;; should we do something special here for
2769 ;;; ;; somebody?
2770 ;;; (funcall orig-func event)))
2771 (set-buffer orig-buf))))))
2772 ;; This is always executed, even if the above generates an error.
2773 (setq folding-calling-original nil))))
2774
2775 (defun folding-mouse-context-sensitive (event)
2776 "Perform some operation depending on the context of the mouse pointer.
2777 EVENT is mouse event.
2778
2779 The variable `folding-behave-table' contains a mapping between contexts and
2780 operations to perform.
2781
2782 The following contexts can be handled (They are named after the
2783 natural operation to perform on them):
2784
2785 open - A folded fold.
2786 close - An open fold, which isn't the one current topmost one.
2787 up - The topmost visible fold.
2788 other - Anything else.
2789
2790 Note that the `pointer' can be either the buffer point, or the mouse
2791 pointer depending in the setting of the user option
2792 `folding-mouse-yank-at-point'."
2793 (interactive "e")
2794 (let* ( ;; - Get mouse cursor point, or point
2795 (point (folding-mouse-point event))
2796 state)
2797 (if (null point)
2798 ;; The user didn't click on any text.
2799 (folding-act 'other event)
2800 (save-excursion
2801 (goto-char point)
2802 (setq state (folding-mark-look-at)))
2803 (cond
2804 ((eq state 0)
2805 (folding-act 'open event))
2806 ((eq state 1)
2807 (folding-act 'close event))
2808 ((eq state 11)
2809 (folding-act 'up event))
2810 ((eq 'end state)
2811 (folding-act 'close))
2812 ((eq state 'end-in)
2813 (folding-act 'up event))
2814 (t
2815 (folding-act 'other event))))))
2816
2817 ;;; FIXME: #not used, the pick move handles this too
2818 (defun folding-mouse-move (event)
2819 "Move down if sitting on fold mark using mouse EVENT.
2820
2821 Original function behind the mouse is called if no FOLD action wasn't
2822 taken."
2823 (interactive "e")
2824 (let* (;; Get mouse cursor point, or point
2825 (point (folding-mouse-point event))
2826 state)
2827 (save-excursion
2828 (goto-char point)
2829 (beginning-of-line)
2830 (setq state (folding-mark-look-at)))
2831 (cond
2832 ((not (null state))
2833 (goto-char point)
2834 (folding-next-visible-heading) t)
2835 (t
2836 (folding-mouse-call-original event)))))
2837
2838 (defun folding-mouse-pick-move (event)
2839 "Pick movement if sitting on beg/end fold mark using mouse EVENT.
2840 If mouse if at the `beginning-of-line' point, then always move up.
2841
2842 Original function behind the mouse is called if no FOLD action wasn't
2843 taken."
2844 (interactive "e")
2845 (let* ( ;; - Get mouse cursor point, or point
2846 (point (folding-mouse-point event))
2847 state)
2848 (save-excursion
2849 (goto-char point)
2850 (setq state (folding-mark-look-at)))
2851 (cond
2852 ((not (null state))
2853 (goto-char point)
2854 (if (= point
2855 (save-excursion (beginning-of-line) (point)))
2856 (folding-previous-visible-heading)
2857 (folding-pick-move)))
2858 (t
2859 (folding-mouse-call-original event)))))
2860
2861 ;;}}}
2862 ;;{{{ code: engine
2863
2864 (defun folding-set-mode-line ()
2865 "Update modeline with fold level."
2866 (if (null folding-stack)
2867 (kill-local-variable 'folding-mode-string)
2868 (make-local-variable 'folding-mode-string)
2869 (setq folding-mode-string
2870 (if (eq 'folded (car folding-stack))
2871 (concat
2872 folding-inside-string "1" folding-inside-mode-name)
2873 (concat
2874 folding-inside-string
2875 (int-to-string (length folding-stack))
2876 folding-inside-mode-name)))))
2877
2878 (defun folding-clear-stack ()
2879 "Clear the fold stack, and release all the markers it refers to."
2880 (let ((stack folding-stack))
2881 (setq folding-stack nil)
2882 (while (and stack (not (eq 'folded (car stack))))
2883 (set-marker (car (car stack)) nil)
2884 (set-marker (cdr (car stack)) nil)
2885 (setq stack (cdr stack)))))
2886
2887 (defun folding-check-if-folding-allowed ()
2888 "Return non-nil when buffer allowed to be folded automatically.
2889 When buffer is loaded it may not be desirable to fold it immediately,
2890 because the file may be too large, or it may contain fold marks, that
2891 really are not _real_ folds. (Eg. RMAIL saved files may have the
2892 marks)
2893
2894 This function returns t, if it's okay to proceed checking the fold status
2895 of file. Returning nil means that folding should not touch this file.
2896
2897 The variable `folding-check-allow-folding-function' normally contains this
2898 function. Change the variable to use your own scheme."
2899
2900 (or (let ((file (get 'find-file-noselect 'folding)))
2901 ;; When a file reference is "pushed" is a C-h v buffer that says:
2902 ;; test is a Lisp function in `~/foo/tmp/test.el' A flag gets set
2903 ;; (see adviced code) and we must not fold this buffer, because
2904 ;; it will be immediately searched.
2905 (and file
2906 (not (string-match (regexp-quote file)
2907 (or buffer-file-name "")))))
2908 ;; Do not fold these files
2909 (null (string-match folding-mode-hook-no-regexp (buffer-name)))))
2910
2911 (defun folding-mode-find-file ()
2912 "One of the funcs called whenever a `find-file' is successful.
2913 It checks to see if `folded-file' has been set as a buffer-local
2914 variable, and automatically starts Folding mode if it has.
2915
2916 This allows folded files to be automatically folded when opened.
2917
2918 To make this hook effective, the symbol `folding-mode-find-file-hook'
2919 should be placed at the end of `find-file-hooks'. If you have
2920 some other hook in the list, for example a hook to automatically
2921 uncompress or decrypt a buffer, it should go earlier on in the list.
2922
2923 See also `folding-mode-add-find-file-hook'."
2924 (let* ((check-fold folding-check-folded-file-function)
2925 (allow-fold folding-check-allow-folding-function))
2926 ;; Turn mode on only if it's allowed
2927 (if (funcall allow-fold)
2928 (or (and (and check-fold (funcall check-fold))
2929 (folding-mode 1))
2930 (and (assq 'folded-file (buffer-local-variables))
2931 folded-file
2932 (folding-mode 1)
2933 (kill-local-variable 'folded-file)))
2934 ;; In all other cases, unfold buffer.
2935 (if folding-mode
2936 (turn-off-folding-mode)))))
2937
2938 ;;;###autoload
2939 (defun folding-mode-add-find-file-hook ()
2940 "Append `folding-mode-find-file-hook' to the list `find-file-hooks'.
2941
2942 This has the effect that afterwards, when a folded file is visited, if
2943 appropriate Emacs local variable entries are recognized at the end of
2944 the file, Folding mode is started automatically.
2945
2946 If `inhibit-local-variables' is non-nil, this will not happen regardless
2947 of the setting of `find-file-hooks'.
2948
2949 To declare a file to be folded, put `folded-file: t' in the file's
2950 local variables. eg., at the end of a C source file, put:
2951
2952 /*
2953 Local variables:
2954 folded-file: t
2955 */
2956
2957 The local variables can be inside a fold."
2958 (interactive)
2959 (or (memq 'folding-mode-find-file find-file-hook)
2960 (add-hook 'find-file-hook 'folding-mode-find-file 'end)))
2961
2962 (defun folding-mode-write-file ()
2963 "Folded files must be controlled by folding before saving.
2964 This function turns on the folding mode if it is not activated.
2965 It prevents binary pollution upon save."
2966 (let* ((check-func folding-check-folded-file-function)
2967 (no-re folding-mode-hook-no-regexp)
2968 (bn (or (buffer-name) "")))
2969 (if (and (not (string-match no-re bn))
2970 (boundp 'folding-mode)
2971 (null folding-mode)
2972 (and check-func (funcall check-func)))
2973 (progn
2974 ;; When folding mode is turned on it also 'folds' whole
2975 ;; buffer... can't avoid that, since it's more important
2976 ;; to save safely
2977 (folding-mode 1)))
2978 ;; hook returns nil, good habit
2979 nil))
2980
2981 (defun folding-check-folded ()
2982 "Function to determine if this file is in folded form."
2983 (let* (;; Could use folding-top-regexp , folding-bottom-regexp ,
2984 ;; folding-regexp But they are not available at load time.
2985 (folding-re1 "^.?.?.?{{{")
2986 (folding-re2 "[\r\n].*}}}"))
2987 (save-excursion
2988 (goto-char (point-min))
2989 ;; If we found both, we assume file is folded
2990 (and (re-search-forward folding-re1 nil t)
2991 ;; if file is folded, there are \r's
2992 (search-forward "\r" nil t)
2993 (re-search-forward folding-re2 nil t)))))
2994
2995 ;;}}}
2996
2997 ;;{{{ code: Folding mode
2998
2999 (defun folding-font-lock-keywords (&optional mode)
3000 "Return folding font-lock keywords for MODE."
3001 ;; Add support mode-by-mode basis. Check if mode is already
3002 ;; handled from the property list.
3003 (cl-destructuring-bind (beg end ignore)
3004 (folding-get-mode-marks (or mode major-mode))
3005 ;; `ignore' is not used, add no-op for byte compiler
3006 (or ignore
3007 (setq ignore t))
3008 (setq beg (concat "^[ \t]*" (regexp-quote beg) "[^\r\n]+"))
3009 (setq end (concat "^[ \t]*" (regexp-quote end)))
3010 (list
3011 ;; the `t' says to overwrite any previous highlight.
3012 ;; => Needed because folding marks are in comments.
3013 (list beg 0 folding-font-lock-begin-mark t)
3014 (list end 0 folding-font-lock-end-mark t))))
3015
3016 (defun folding-font-lock-support-instantiate (&optional mode)
3017 "Add fold marks with `font-lock-add-keywords'."
3018 (or mode
3019 (setq mode major-mode))
3020 ;; Hide function from Byte Compiler.
3021 (let ((function 'font-lock-add-keywords))
3022 (when (fboundp function)
3023 (funcall function
3024 mode
3025 (folding-font-lock-keywords mode))
3026 ;; In order to see new keywords font lock must be restarted.
3027 (dolist (buffer (buffer-list))
3028 (with-current-buffer buffer
3029 (when (and (eq major-mode mode)
3030 (or font-lock-mode
3031 (and (boundp 'global-font-lock-mode)
3032 global-font-lock-mode)))
3033 ;; FIXME: Crude fix. should we use font-lock-fontify-buffer instead?
3034 (font-lock-mode -1)
3035 (font-lock-mode 1)))))))
3036
3037 (defun folding-font-lock-support ()
3038 "Add font lock support."
3039 (let ((list (get 'folding-mode 'font-lock)))
3040 (unless (memq major-mode list)
3041 ;; Support added, update known list
3042 (push major-mode list)
3043 (put 'folding-mode 'font-lock list)
3044 (folding-font-lock-support-instantiate major-mode))))
3045
3046 (defun folding-set-local-variables ()
3047 "Set local fold mark variables.
3048 If you're going to change the beginning and end mark in
3049 `folding-mode-marks-alist'; you must call this function."
3050 (set (make-local-variable 'folding-stack) nil)
3051 (make-local-variable 'folding-top-mark)
3052 (make-local-variable 'folding-secondary-top-mark)
3053 (make-local-variable 'folding-top-regexp)
3054 (make-local-variable 'folding-bottom-mark)
3055 (make-local-variable 'folding-bottom-regexp)
3056 (make-local-variable 'folding-regexp)
3057 (or (and (boundp 'folding-top-regexp)
3058 folding-top-regexp
3059 (boundp 'folding-bottom-regexp)
3060 folding-bottom-regexp)
3061 (let ((folding-marks (assq major-mode
3062 folding-mode-marks-alist)))
3063 (if folding-marks
3064 (setq folding-marks (cdr folding-marks))
3065 (setq folding-marks '("{{{" "}}}")))
3066 (apply 'folding-set-marks folding-marks))))
3067
3068 ;;;###autoload
3069 (defun turn-off-folding-mode ()
3070 "Turn off folding."
3071 (interactive)
3072 (folding-mode -1))
3073
3074 ;;;###autoload
3075 (defun turn-on-folding-mode ()
3076 "Turn on folding."
3077 (interactive)
3078 (folding-mode 1))
3079
3080 ;;;###autoload
3081 (defun folding-mode (&optional arg inter)
3082 "A folding-editor-like minor mode. ARG INTER.
3083
3084 These are the basic commands that Folding mode provides:
3085
3086 \\{folding-mode-map}
3087
3088 Keys starting with `folding-mode-prefix-key'
3089
3090 \\{folding-mode-prefix-map}
3091
3092 folding-convert-buffer-for-printing:
3093 `\\[folding-convert-buffer-for-printing]'
3094 Makes a ready-to-print, formatted, unfolded copy in another
3095 buffer.
3096
3097 Read the documentation for the above functions for more
3098 information.
3099
3100 Overview
3101
3102 Folds are a way of hierarchically organizing the text in a
3103 file, so that the text can be viewed and edited at different
3104 levels. It is similar to Outline mode in that parts of the
3105 text can be hidden from view. A fold is a region of text,
3106 surrounded by special \"fold marks\", which act like
3107 brackets, grouping the text. Fold mark pairs can be nested,
3108 and they can have titles. When a fold is folded, the text is
3109 hidden from view, except for the first line, which acts like
3110 a title for the fold.
3111
3112 Folding mode is a minor mode, designed to cooperate with many
3113 other major modes, so that many types of text can be folded
3114 while they are being edited (eg., plain text, program source
3115 code, Texinfo, etc.).
3116
3117 Folding-mode function
3118
3119 If Folding mode is not called
3120 interactively (`(folding-called-interactively-p)' is nil),
3121 and it is called with two or less arguments, all of which are
3122 nil, then the point will not be altered if
3123 `folding-folding-on-startup' is set and
3124 `folding-whole-buffer' is called. This is generally not a
3125 good thing, as it can leave the point inside a hidden region
3126 of a fold, but it is required if the local variables set
3127 \"mode: folding\" when the file is first read (see
3128 `hack-local-variables').
3129
3130 Not that you should ever want to, but to call Folding mode
3131 from a program with the default behavior (toggling the mode),
3132 call it with something like `(folding-mode nil t)'.
3133
3134 Fold marks
3135
3136 For most types of folded file, lines representing folds have
3137 \"{{{\" near the beginning. To enter a fold, move the point
3138 to the folded line and type `\\[folding-shift-in]'. You
3139 should no longer be able to see the rest of the file, just
3140 the contents of the fold, which you couldn\='t see before. You
3141 can use `\\[folding-shift-out]' to leave a fold, and you can
3142 enter and exit folds to move around the structure of the
3143 file.
3144
3145 All of the text is present in a folded file all of the time.
3146 It is just hidden. Folded text shows up as a line (the top
3147 fold mark) with \"...\" at the end. If you are in a fold, the
3148 mode line displays \"inside n folds Narrow\", and because the
3149 buffer is narrowed you can\='t see outside of the current
3150 fold\='s text.
3151
3152 By arranging sections of a large file in folds, and maybe
3153 subsections in sub-folds, you can move around a file quickly
3154 and easily, and only have to scroll through a couple of pages
3155 at a time. If you pick the titles for the folds carefully,
3156 they can be a useful form of documentation, and make moving
3157 though the file a lot easier. In general, searching through a
3158 folded file for a particular item is much easier than without
3159 folds.
3160
3161 Managing folds
3162
3163 To make a new fold, set the mark at one end of the text you
3164 want in the new fold, and move the point to the other end.
3165 Then type `\\[folding-fold-region]'. The text you selected
3166 will be made into a fold, and the fold will be entered. If
3167 you just want a new, empty fold, set the mark where you want
3168 the fold, and then create a new fold there without moving the
3169 point. Don\='t worry if the point is in the middle of a line of
3170 text, `folding-fold-region' will not break text in the middle
3171 of a line. After making a fold, the fold is entered and the
3172 point is positioned ready to enter a title for the fold. Do
3173 not delete the fold marks, which are usually something like
3174 \"{{{\" and \"}}}\". There may also be a bit of fold mark
3175 which goes after the fold title.
3176
3177 If the fold markers get messed up, or you just want to see
3178 the whole unfolded file, use `\\[folding-open-buffer]' to
3179 unfolded the whole file, so you can see all the text and all
3180 the marks. This is useful for checking/correcting unbalanced
3181 fold markers, and for searching for things. Use
3182 `\\[folding-whole-file]' to fold the buffer again.
3183
3184 `folding-shift-out' will attempt to tidy the current fold
3185 just before exiting it. It will remove any extra blank lines
3186 at the top and bottom, \(outside the fold marks). It will
3187 then ensure that fold marks exists, and if they are not, will
3188 add them (after asking). Finally, the number of blank lines
3189 between the fold marks and the contents of the fold is set to
3190 1 (by default).
3191
3192 Folding package customizations
3193
3194 If the fold marks are not set on entry to Folding mode, they
3195 are set to a default for current major mode, as defined by
3196 `folding-mode-marks-alist' or to \"{{{ \" and \"}}}\" if none
3197 are specified.
3198
3199 To bind different commands to keys in Folding mode, set the
3200 bindings in the keymap `folding-mode-map'.
3201
3202 The hooks `folding-mode-hook' and
3203 `<major-mode-name>-folding-hook' are called before folding
3204 the buffer and applying the key bindings in
3205 `folding-mode-map'. This is a good hook to set extra or
3206 different key bindings in `folding-mode-map'. Note that key
3207 bindings in `folding-mode-map' are only examined just after
3208 calling these hooks; new bindings in those maps only take
3209 effect when Folding mode is being started. The hook
3210 `folding-load-hook' is called when Folding mode is loaded
3211 into Emacs.
3212
3213 Mouse behavior
3214
3215 If you want folding to detect point of actual mouse click,
3216 please see variable `folding-mouse-yank-at-p'.
3217
3218 To customise the mouse actions, look at `folding-behave-table'."
3219 (interactive)
3220 (let ((new-folding-mode
3221 (if (not arg)
3222 (not folding-mode)
3223 (> (prefix-numeric-value arg) 0))))
3224 (or (eq new-folding-mode
3225 folding-mode)
3226 (if folding-mode
3227 (progn
3228 ;; turn off folding
3229 (if (null (folding-use-overlays-p))
3230 (setq selective-display nil))
3231 (folding-clear-stack)
3232 (folding-narrow-to-region nil nil)
3233 (folding-subst-regions (list 1 (point-max)) ?\r ?\n)
3234
3235 ;; Restore "%n" (Narrow) in the mode line
3236 (setq mode-line-format
3237 (mapcar
3238 (lambda (item)
3239 (if (equal item 'folding-narrow-placeholder)
3240 "%n" item))
3241 mode-line-format)))
3242 (cond ;; else
3243 ((folding-use-overlays-p)
3244 ;; This may be Emacs specific; how about XEmacs?
3245 ;;
3246 ;; make line-move-ignore-invisible buffer local, matches
3247 ;; outline.el, and the 21 pre-release gets upset if this is
3248 ;; defined globally in shell buffer...
3249 (make-local-variable 'line-move-ignore-invisible)
3250 (setq line-move-ignore-invisible t
3251 buffer-invisibility-spec '((t . t))))
3252 (t
3253 (setq selective-display t)
3254 (setq selective-display-ellipses t)))
3255 (unless (assq 'folding-mode minor-mode-alist)
3256 ;; User has not run folding-install or he did call
3257 ;; folding-uninstall which completely wiped package out.
3258 ;; => anyway now he calls us, so be there for him
3259 (folding-install))
3260 (folding-keep-hooked) ;set hooks if not there
3261 (widen)
3262 (setq folding-narrow-overlays nil)
3263 (folding-set-local-variables)
3264 (folding-font-lock-support)
3265 (unwind-protect
3266 (let ((hook-symbol (intern-soft
3267 (concat
3268 (symbol-name major-mode)
3269 "-folding-hook"))))
3270 (run-hooks 'folding-mode-hook)
3271 (and hook-symbol
3272 (run-hooks hook-symbol)))
3273 (folding-set-mode-line))
3274 (and folding-folding-on-startup
3275 (if (or (folding-called-interactively-p)
3276 arg
3277 inter)
3278 (folding-whole-buffer)
3279 (save-excursion
3280 (folding-whole-buffer))))
3281 (folding-narrow-to-region nil nil t)
3282 ;; Remove "%n" (Narrow) from the mode line
3283 (setq mode-line-format
3284 (mapcar
3285 (lambda (item)
3286 (if (equal item "%n")
3287 'folding-narrow-placeholder item))
3288 mode-line-format))))
3289 (setq folding-mode new-folding-mode)))
3290
3291 ;;}}}
3292 ;;{{{ code: setting fold marks
3293
3294 ;; You think those "\\(\\)" pairs are peculiar? Me too. Emacs regexp
3295 ;; stuff has a bug; sometimes "\\(.*\\)" fails when ".*" succeeds, but
3296 ;; only in a folded file! Strange bug! Must check it out sometime.
3297
3298 (defun folding-set-marks (top bottom &optional secondary)
3299 "Set the folding top and bottom mark for the current buffer.
3300
3301 Input:
3302
3303 TOP The topmost fold mark. Comment start + fold begin string.
3304 BOTTOM The bottom fold mark Comment end + fold end string.
3305 SECONDARY Usually the comment end indicator for the mode. This
3306 is inserted by `folding-fold-region' after the fold top mark,
3307 and is presumed to be put after the title of the fold.
3308
3309 Example:
3310
3311 html-mode:
3312
3313 top: \"<!-- [[[ \"
3314 bot: \"<!-- ]]] -->\"
3315 sec: \" -->\"
3316
3317 Notice that the top marker needs to be closed with SECONDARY comment end string.
3318
3319 Various regular expressions are set with this function, so don't set the
3320 mark variables directly."
3321 (set (make-local-variable 'folding-top-mark)
3322 top)
3323 (set (make-local-variable 'folding-bottom-mark)
3324 bottom)
3325 (set (make-local-variable 'folding-secondary-top-mark)
3326 secondary)
3327 (set (make-local-variable 'folding-top-regexp)
3328 (concat "\\(^\\|\r+\\)[ \t]*"
3329 (regexp-quote folding-top-mark)))
3330 (set (make-local-variable 'folding-bottom-regexp)
3331 (concat "\\(^\\|\r+\\)[ \t]*"
3332 (regexp-quote folding-bottom-mark)))
3333 (set (make-local-variable 'folding-regexp)
3334 (concat "\\(^\\|\r\\)\\([ \t]*\\)\\(\\("
3335 (regexp-quote folding-top-mark)
3336 "\\)\\|\\("
3337 (regexp-quote folding-bottom-mark)
3338 "[ \t]*\\(\\)\\($\\|\r\\)\\)\\)")))
3339
3340 ;;}}}
3341 ;;{{{ code: movement
3342
3343 (defun folding-next-visible-heading (&optional direction)
3344 "Move up/down fold headers.
3345 Backward if DIRECTION is non-nil returns nil if not moved = no next marker."
3346 (interactive)
3347 (let* ((begin-mark (nth 0 (folding-get-mode-marks)))
3348 case-fold-search)
3349 (if direction
3350 (re-search-backward (concat "^" (regexp-quote begin-mark)) nil t)
3351 (re-search-forward (concat "^" (regexp-quote begin-mark)) nil t))))
3352
3353 (defun folding-previous-visible-heading ()
3354 "Move upward fold headers."
3355 (interactive)
3356 (beginning-of-line)
3357 (folding-next-visible-heading 'backward))
3358
3359 (defun folding-find-folding-mark (&optional end-fold)
3360 "Search backward to find beginning fold. Skips subfolds.
3361 Optionally searches forward to find END-FOLD mark.
3362
3363 Return:
3364
3365 nil
3366 point position of fold mark"
3367 (let* (case-fold-search
3368 (elt (folding-get-mode-marks))
3369 (bm (regexp-quote (nth 0 elt))) ; markers defined for mode
3370 (em (regexp-quote (nth 1 elt))) ; markers defined for mode
3371 (re (concat "^" bm "\\|^" em))
3372 (count 0)
3373 stat
3374 moved)
3375 (save-excursion
3376 (cond
3377 (end-fold
3378 (folding-end-of-line)
3379 ;; We must skip over inner folds
3380 (while (and (null moved)
3381 (re-search-forward re nil t))
3382 (setq stat (folding-mark-look-at))
3383 (cond
3384 ((symbolp stat)
3385 (setq count (1- count))
3386 (if (< count 0) ;0 or less means no middle folds
3387 (setq moved t)))
3388 ((memq stat '(1 11)) ;BEG fold
3389 (setq count (1+ count))))) ;; end while
3390 (when moved
3391 (forward-char -3)
3392 (setq moved (point))))
3393 (t
3394 (while (and (null moved)
3395 (re-search-backward re nil t))
3396 (setq stat (folding-mark-look-at))
3397 (cond
3398 ((memq stat '(1 11))
3399 (setq count (1- count))
3400 (if (< count 0) ;0 or less means no middle folds
3401 (setq moved (point))))
3402 ((symbolp stat)
3403 (setq count (1+ count)))))
3404 (when moved ;What's the result
3405 (forward-char 3)
3406 (setq moved (point))))))
3407 moved))
3408
3409 (defun folding-pick-move ()
3410 "Pick the logical movement on fold mark.
3411 If at the end of fold, then move to the beginning and vice versa.
3412
3413 If placed over closed fold moves to the next fold. When no next
3414 folds are visible, stops moving.
3415
3416 Return:
3417 t if moved"
3418 (interactive)
3419 (let* (case-fold-search
3420 (elt (folding-get-mode-marks))
3421 (bm (nth 0 elt)) ; markers defined for mode
3422 (stat (folding-mark-look-at))
3423 moved)
3424 (cond
3425 ((eq 0 stat) ;closed fold
3426 (when (re-search-forward (concat "^" (regexp-quote bm)) nil t)
3427 (setq moved t)
3428 (forward-char 3)))
3429 ((symbolp stat) ;End fold
3430 (setq moved (folding-find-folding-mark)))
3431 ((integerp stat) ;Beg fold
3432 (setq moved (folding-find-folding-mark 'end-fold))))
3433 (if (integerp moved)
3434 (goto-char moved))
3435 moved))
3436
3437 ;;; Idea by Scott Evans <gse A T antisleep com>
3438 (defun folding-context-next-action ()
3439 "Take next action according to point and context.
3440 If point is at:
3441
3442 Begin Fold : toggle open - close
3443 End Fold : close
3444 inside : fold current level."
3445 (interactive)
3446 (let ((state (folding-mark-look-at)))
3447 (cond
3448 ((eq state 0)
3449 (folding-act 'open))
3450 ((eq state 1)
3451 (folding-act 'close))
3452 ((eq state 11)
3453 (folding-act 'up))
3454 ((eq 'end state)
3455 (folding-act 'close))
3456 ((eq state 'end-in)
3457 (folding-act 'up))
3458 (t
3459 (folding-act 'other)))))
3460
3461 (defun folding-forward-char-1 (&optional arg)
3462 "See `folding-forward-char-1' for ARG."
3463 (if (eq arg 1)
3464 ;; Do it a faster way for arg = 1.
3465 (if (eq (following-char) ?\r)
3466 (let ((saved (point))
3467 (inhibit-quit t))
3468 (end-of-line)
3469 (if (not (eobp))
3470 (forward-char)
3471 (goto-char saved)
3472 (error "End of buffer")))
3473 ;; `forward-char' here will do its own error if (eobp).
3474 (forward-char))
3475 (if (> 0 (or arg (setq arg 1)))
3476 (folding-backward-char (- arg))
3477 (let (goal saved)
3478 (while (< 0 arg)
3479 (skip-chars-forward "^\r" (setq goal (+ (point) arg)))
3480 (if (eq goal (point))
3481 (setq arg 0)
3482 (if (eobp)
3483 (error "End of buffer")
3484 (setq arg (- goal 1 (point))
3485 saved (point))
3486 (let ((inhibit-quit t))
3487 (end-of-line)
3488 (if (not (eobp))
3489 (forward-char)
3490 (goto-char saved)
3491 (error "End of buffer"))))))))))
3492
3493 (defmacro folding-forward-char-macro ()
3494 `(defun folding-forward-char (&optional arg)
3495 "Move point right ARG characters, skipping hidden folded regions.
3496 Moves left if ARG is negative. On reaching end of buffer, stop and
3497 signal error."
3498 ,(folding-interactive-spec-p)
3499 ;; (folding-preserve-active-region)
3500 (folding-forward-char-1 arg)))
3501
3502 (folding-forward-char-macro)
3503
3504 (defun folding-backward-char-1 (&optional arg)
3505 "See `folding-backward-char-1' for ARG."
3506 (if (eq arg 1)
3507 ;; Do it a faster way for arg = 1.
3508 ;; Catch the case where we are in a hidden region, and bump into a \r.
3509 (if (or (eq (preceding-char) ?\n)
3510 (eq (preceding-char) ?\r))
3511 (let ((pos (1- (point)))
3512 (inhibit-quit t))
3513 (forward-char -1)
3514 (beginning-of-line)
3515 (skip-chars-forward "^\r" pos))
3516 (forward-char -1))
3517 (if (> 0 (or arg (setq arg 1)))
3518 (folding-forward-char (- arg))
3519 (let (goal)
3520 (while (< 0 arg)
3521 (skip-chars-backward "^\r\n" (max (point-min)
3522 (setq goal (- (point) arg))))
3523 (if (eq goal (point))
3524 (setq arg 0)
3525 (if (bobp)
3526 (error "Beginning of buffer")
3527 (setq arg (- (point) 1 goal)
3528 goal (point))
3529 (let ((inhibit-quit t))
3530 (forward-char -1)
3531 (beginning-of-line)
3532 (skip-chars-forward "^\r" goal)))))))))
3533
3534 (defmacro folding-backward-char-macro ()
3535 `(defun folding-backward-char (&optional arg)
3536 "Move point right ARG characters, skipping hidden folded regions.
3537 Moves left if ARG is negative. On reaching end of buffer, stop and
3538 signal error."
3539 ,(folding-interactive-spec-p)
3540 ;; (folding-preserve-active-region)
3541 (folding-backward-char-1 arg)))
3542
3543 (folding-backward-char-macro)
3544
3545 (defmacro folding-end-of-line-macro ()
3546 `(defun folding-end-of-line (&optional arg)
3547 "Move point to end of current line, but before hidden folded region.
3548 ARG is line count.
3549
3550 Has the same behavior as `end-of-line', except that if the current line
3551 ends with some hidden folded text (represented by an ellipsis), the
3552 point is positioned just before it. This prevents the point from being
3553 placed inside the folded text, which is not normally useful."
3554 ,(folding-interactive-spec-p)
3555 ;;(interactive "p")
3556 ;; (folding-preserve-active-region)
3557 (if (or (eq arg 1)
3558 (not arg))
3559 (beginning-of-line)
3560 ;; `forward-line' also moves point to beginning of line.
3561 (forward-line (1- arg)))
3562 (skip-chars-forward "^\r\n")))
3563
3564 ;; Instantiate
3565 (folding-end-of-line-macro)
3566
3567 (defun folding-skip-ellipsis-backward ()
3568 "Move the point backwards out of folded text.
3569
3570 If the point is inside a folded region, the cursor is displayed at the
3571 end of the ellipsis representing the folded part. This function checks
3572 to see if this is the case, and if so, moves the point backwards until
3573 it is just outside the hidden region, and just before the ellipsis.
3574
3575 Returns t if the point was moved, nil otherwise."
3576 (interactive)
3577 (let ((pos (point))
3578 result)
3579 (save-excursion
3580 (beginning-of-line)
3581 (skip-chars-forward "^\r" pos)
3582 (or (eq pos (point))
3583 (setq pos (point)
3584 result t)))
3585 (goto-char pos)
3586 result))
3587
3588 ;;}}}
3589
3590 ;;{{{ code: Moving in and out of folds
3591
3592 ;;{{{ folding-shift-in
3593
3594 (defun folding-shift-in (&optional noerror)
3595 "Open and enter the fold at or around the point.
3596
3597 Enters the fold that the point is inside, wherever the point is inside
3598 the fold, provided it is a valid fold with balanced top and bottom
3599 marks. Returns nil if the fold entered contains no sub-folds, t
3600 otherwise. If an optional argument NOERROR is non-nil, returns nil if
3601 there are no folds to enter, instead of causing an error.
3602
3603 If the point is inside a folded, hidden region (as represented by an
3604 ellipsis), the position of the point in the buffer is preserved, and as
3605 many folds as necessary are entered to make the surrounding text
3606 visible. This is useful after some commands eg., search commands."
3607 (interactive)
3608 (folding-labels
3609 ((open-fold nil
3610 (let ((data (folding-show-current-entry noerror t)))
3611 (and data
3612 (progn
3613 (when folding-narrow-by-default
3614 (setq folding-stack
3615 (if folding-stack
3616 (cons (cons (point-min-marker)
3617 (point-max-marker))
3618 folding-stack)
3619 '(folded)))
3620 (folding-set-mode-line))
3621 (folding-narrow-to-region
3622 (car data)
3623 (nth 1 data)))))))
3624 (let ((goal (point)))
3625 (while (folding-skip-ellipsis-backward)
3626 (beginning-of-line)
3627 (open-fold)
3628 (goto-char goal))
3629 (if folding-narrow-by-default
3630 (open-fold)
3631 (widen)))))
3632
3633 ;;}}}
3634 ;;{{{ folding-shift-out
3635
3636 (defun folding-shift-out (&optional event)
3637 "Exits the current fold with EVENT."
3638 (interactive)
3639 (if folding-stack
3640 (progn
3641 (folding-tidy-inside)
3642 (cond
3643 ((folding-use-overlays-p)
3644 (folding-subst-regions
3645 (list (overlay-end (car folding-narrow-overlays))
3646 (overlay-start (cdr folding-narrow-overlays))) ?\n ?\r)
3647 ;; So point is correct in other windows.
3648 (goto-char (overlay-end (car folding-narrow-overlays))))
3649 (t
3650 (folding-subst-regions (list (point-min) (point-max)) ?\n ?\r)
3651 ;; So point is correct in other window
3652 (goto-char (point-min))))
3653
3654 (if (eq (car folding-stack) 'folded)
3655 (folding-narrow-to-region nil nil t)
3656 (folding-narrow-to-region
3657 (marker-position (car (car folding-stack)))
3658 (marker-position (cdr (car folding-stack))) t))
3659 (and (consp (car folding-stack))
3660 (set-marker (car (car folding-stack)) nil)
3661 (set-marker (cdr (car folding-stack)) nil))
3662 (setq folding-stack (cdr folding-stack)))
3663 (error "Outside all folds"))
3664 (folding-set-mode-line))
3665
3666 ;;}}}
3667 ;;{{{ folding-show-current-entry
3668
3669 (defun folding-show-current-entry (&optional event noerror noskip)
3670 "Opens the fold that the point is on, but does not enter it.
3671 EVENT and optional arg NOERROR means don't signal an error if there is
3672 no fold, just return nil. NOSKIP means don't jump out of a hidden
3673 region first.
3674
3675 Returns ((START END SUBFOLDS-P). START and END indicate the extents of
3676 the fold that was shown. If SUBFOLDS-P is non-nil, the fold contains
3677 subfolds."
3678 (interactive)
3679 (or noskip
3680 (folding-skip-ellipsis-backward))
3681 (let ((point (point))
3682 backward
3683 forward
3684 start
3685 end
3686 subfolds-not-p)
3687 (unwind-protect
3688 (or (and (integerp
3689 (car-safe (setq backward (folding-skip-folds t))))
3690 (integerp
3691 (car-safe (setq forward (folding-skip-folds nil))))
3692 (progn
3693 (goto-char (car forward))
3694 (skip-chars-forward "^\r\n")
3695 (setq end (point))
3696 (skip-chars-forward "\r\n")
3697 (not (and folding-stack (eobp))))
3698 (progn
3699 (goto-char (car backward))
3700 (skip-chars-backward "^\r\n")
3701 (setq start (point))
3702 (skip-chars-backward "\r\n")
3703 (not (and folding-stack (bobp))))
3704 (progn
3705 (setq point start)
3706 ;; Avoid holding the list through a GC.
3707 (setq subfolds-not-p
3708 (not (or (cdr backward)
3709 (cdr forward))))
3710 (folding-subst-regions
3711 (append backward (nreverse forward))
3712 ?\r ?\n)
3713 ;; FIXME: this should be moved to font-lock:
3714 ;; - When fold is closed, the whole line (with code)
3715 ;; is treated as comment
3716 ;; - Fon-lock changes all fonts to `font-lock-comment-face'
3717 ;; - When you again open fold, all text is in color
3718 ;;
3719 ;; => Font lock should stop at \r, and not use ".*"
3720 ;; which includes \r character
3721 ;; This is a workaround, not an efficient one
3722 (if (or (and (boundp 'global-font-lock-mode)
3723 global-font-lock-mode)
3724 font-lock-mode)
3725 (font-lock-fontify-region start end))
3726 (list start end (not subfolds-not-p))))
3727 (if noerror
3728 nil
3729 (error "Not on a fold")))
3730 (goto-char point))))
3731
3732 ;;}}}
3733 ;;{{{ folding-hide-current-entry
3734
3735 (defun folding-toggle-enter-exit ()
3736 "Run `folding-shift-in' or `folding-shift-out'.
3737 This depends on current line's contents."
3738 (interactive)
3739 (beginning-of-line)
3740 (let ((current-line-mark (folding-mark-look-at)))
3741 (if (and (numberp current-line-mark)
3742 (= current-line-mark 0))
3743 (folding-shift-in)
3744 (folding-shift-out))))
3745
3746 (defun folding-toggle-show-hide ()
3747 "Run `folding-show-current-entry'.
3748 Alternatively `folding-hide-current-entry' depending on current
3749 line\\='s contents."
3750 (interactive)
3751 (beginning-of-line)
3752 (let ((current-line-mark (folding-mark-look-at)))
3753 (if (and (numberp current-line-mark)
3754 (= current-line-mark 0))
3755 (folding-show-current-entry)
3756 (folding-hide-current-entry))))
3757
3758 (defun folding-hide-current-entry (&optional event)
3759 "Close the fold around the point using EVENT.
3760 Undo effect of `folding-show-current-entry'."
3761 (interactive)
3762 (folding-skip-ellipsis-backward)
3763 (let (start end)
3764 (if (and (integerp (setq start (car-safe (folding-skip-folds t))))
3765 (integerp (setq end (car-safe (folding-skip-folds nil)))))
3766 (if (and folding-stack
3767 (or (eq start (point-min))
3768 (eq end (point-max))))
3769 ;;(error "Cannot hide current fold")
3770 (folding-shift-out)
3771 (goto-char start)
3772 (skip-chars-backward "^\r\n")
3773 (folding-subst-regions (list start end) ?\n ?\r))
3774 (error "Not on a fold"))))
3775
3776 ;;}}}
3777 ;;{{{ folding-show-all
3778
3779 (defun folding-show-all ()
3780 "Exits all folds, to the top level."
3781 (interactive)
3782 (while folding-stack
3783 (folding-shift-out)))
3784
3785 ;;}}}
3786 ;;{{{ folding-goto-line
3787
3788 (defun folding-goto-line (line)
3789 "Go to LINE, entering as many folds as possible."
3790 (interactive "NGoto line: ")
3791 (folding-show-all)
3792 (goto-char 1)
3793 (and (< 1 line)
3794 (re-search-forward "[\n\C-m]" nil 0 (1- line)))
3795 (let ((goal (point)))
3796 (while (prog2 (beginning-of-line)
3797 (folding-shift-in t)
3798 (goto-char goal))))
3799 (folding-narrow-to-region
3800 (and folding-narrow-by-default (point-min))
3801 (point-max) t))
3802
3803 ;;}}}
3804
3805 ;;}}}
3806 ;;{{{ code: Searching for fold boundaries
3807
3808 ;;{{{ folding-skip-folds
3809
3810 (defun folding-skip-folds (backward &optional outside)
3811 "Skips forward through the buffer (backward if BACKWARD is non-nil)
3812 until it finds a closing fold mark or the end of the buffer. The
3813 point is not moved. Jumps over balanced folding-mark pairs on the way.
3814 Returns t if the end of buffer was found in an unmatched folding-mark
3815 pair, otherwise a list.
3816
3817 If the point is actually on an fold start mark, the mark is ignored;
3818 if it is on an end mark, the mark is noted. This decision is
3819 reversed if BACKWARD is non-nil. If optional OUTSIDE is non-nil and
3820 BACKWARD is nil, either mark is noted.
3821
3822 The first element of the list is a position in the end of the closing
3823 fold mark if one was found, or nil. It is followed by (END START)
3824 pairs (flattened, not a list of pairs). The pairs indicating the
3825 positions of folds skipped over; they are positions in the fold
3826 marks, not necessarily at the ends of the fold marks. They are in
3827 the opposite order to that in which they were skipped. The point is
3828 left in a meaningless place. If going backwards, the pairs are
3829 \(START END) pairs, as the fold marks are scanned in the opposite
3830 order.
3831
3832 Works by maintaining the position of the top and bottom marks found
3833 so far. They are found separately using a normal string search for
3834 the fixed part of a fold mark (because it is faster than a regexp
3835 search if the string does not occur often outside of fold marks),
3836 checking that it really is a proper fold mark, then considering the
3837 earliest one found. The position of the other (if found) is
3838 maintained to avoid an unnecessary search at the next iteration."
3839 (let ((first-mark (if backward
3840 folding-bottom-mark
3841 folding-top-mark))
3842 (last-mark (if backward
3843 folding-top-mark
3844 folding-bottom-mark))
3845 (top-re folding-top-regexp)
3846 (depth 0)
3847 pairs point
3848 temp
3849 start
3850 first
3851 last
3852 case-fold-search)
3853 ;; Ignore trailing space?
3854 (when nil
3855 (when (and (stringp first-mark)
3856 (string-match "^\\(.*[^ ]+\\) +$" first-mark))
3857 (setq first-mark (match-string 1 first-mark)))
3858 (when (and (stringp last-mark)
3859 (string-match "^\\(.*[^ ]+\\) +$" last-mark))
3860 (setq last-mark (match-string 1 last-mark)))
3861 (when (and (stringp top-re)
3862 (string-match "^\\(.*[^ ]+\\) +$" top-re))
3863 (setq top-re (match-string 1 top-re))))
3864 (save-excursion
3865 (skip-chars-backward "^\r\n")
3866 (unless outside
3867 (and (eq (preceding-char) ?\r)
3868 (forward-char -1))
3869 (if (looking-at top-re)
3870 (if backward
3871 (setq last (match-end 1))
3872 (skip-chars-forward "^\r\n"))))
3873 (while (progn
3874 ;; Find last first, prevents unnecessary searching
3875 ;; for first.
3876 (setq point (point))
3877 (or last
3878 (while (and (if backward
3879 (search-backward last-mark first t)
3880 (search-forward last-mark first t))
3881 (progn
3882 (setq temp (point))
3883 (goto-char (match-beginning 0))
3884 (skip-chars-backward " \t")
3885 (and (not
3886 (setq last
3887 (if (eq (preceding-char) ?\r)
3888 temp
3889 (and (bolp) temp))))
3890 (goto-char temp)))))
3891 (goto-char point))
3892 (or first
3893 (while (and (if backward
3894 (search-backward first-mark last t)
3895 (search-forward first-mark last t))
3896 (progn
3897 (setq temp (point))
3898 (goto-char (match-beginning 0))
3899 (skip-chars-backward " \t")
3900 (and (not
3901 (setq first
3902 (if (eq (preceding-char) ?\r)
3903 temp
3904 (and (bolp) temp))))
3905 (goto-char temp))))))
3906 ;; Return value of conditional says whether to
3907 ;; iterate again.
3908 (if (not last)
3909 ;; Return from this with the result.
3910 (not (setq pairs (if first t (cons nil pairs))))
3911 (if (and first
3912 (if backward
3913 (> first last)
3914 (< first last)))
3915 (progn
3916 (goto-char first)
3917 (if (eq 0 depth)
3918 (setq start first
3919 first nil
3920 depth 1) ;; non-nil value, loop again.
3921 (setq first nil
3922 ;; non-nil value => loop again
3923 depth (1+ depth))))
3924 (goto-char last)
3925 (if (eq 0 depth)
3926 (not (setq pairs (cons last pairs)))
3927 (or (< 0 (setq depth (1- depth)))
3928 (setq pairs (cons last (cons start pairs))))
3929 (setq last nil)
3930 t)))))
3931 pairs)))
3932
3933 ;;}}}
3934
3935 ;;}}}
3936 ;;{{{ code: Functions that actually modify the buffer
3937
3938 ;;{{{ folding-fold-region
3939
3940 (defun folding-fold-region (start end)
3941 "Places fold mark at the beginning and end of a specified region.
3942 The region is specified by two arguments START and END. The point is
3943 left at a suitable place ready to insert the title of the fold.
3944
3945 The fold markers are intended according to mode."
3946 (interactive "r")
3947 (and (< end start)
3948 (setq start (prog1 end
3949 (setq end start))))
3950 (setq end (set-marker (make-marker) end))
3951 (goto-char start)
3952 (beginning-of-line)
3953 (setq start (point))
3954 (insert-before-markers folding-top-mark)
3955 ;; XEmacs latex-mode, after (tex-site), indents the whole
3956 ;; fold 50 characters right. Don't do that.
3957 (unless (string-match "latex" (symbol-name major-mode))
3958 (indent-according-to-mode))
3959 (let ((saved-point (point)))
3960 (and folding-secondary-top-mark
3961 (insert-before-markers folding-secondary-top-mark))
3962 (insert-before-markers ?\n)
3963 (goto-char (marker-position end))
3964 (set-marker end nil)
3965 (and (not (bolp))
3966 (eq 0 (forward-line))
3967 (eobp)
3968 (insert ?\n))
3969 (insert folding-bottom-mark)
3970 (unless (string-match "latex" (symbol-name major-mode))
3971 (indent-according-to-mode))
3972 (insert ?\n)
3973 (setq folding-stack (if folding-stack
3974 (cons (cons (point-min-marker)
3975 (point-max-marker))
3976 folding-stack)
3977 '(folded)))
3978 (folding-narrow-to-region start (1- (point)))
3979 (goto-char saved-point)
3980 (folding-set-mode-line))
3981 (save-excursion (folding-tidy-inside)))
3982
3983 ;;}}}
3984 ;;{{{ folding-tidy-inside
3985
3986 ;; Note to self: The long looking code for checking and modifying those
3987 ;; blank lines is to make sure the text isn't modified unnecessarily.
3988 ;; Don't remove it again!
3989
3990 (defun folding-tidy-inside ()
3991 "Add or remove blank lines at the top and bottom of the current fold.
3992 Also adds fold marks at the top and bottom (after asking), if they are not
3993 there already. The amount of space left depends on the variable
3994 `folding-internal-margins', which is one by default."
3995 (interactive)
3996 (if buffer-read-only nil
3997 (let ()
3998 ;;; (top-re (if (string-match "^\\(.*\\) $" folding-top-mark)
3999 ;;; (match-string 1 folding-top-mark)
4000 ;;; folding-top-mark))
4001 (if (folding-use-overlays-p)
4002 (goto-char (- (overlay-end (car folding-narrow-overlays)) 1))
4003 (goto-char (point-min)))
4004 (and (eolp)
4005 (progn (skip-chars-forward "\n\t ")
4006 (delete-region (point-min) (point))))
4007 (and (if (let (case-fold-search) (folding-mark-look-at-top-mark-p))
4008 (progn (forward-line 1)
4009 (and (eobp) (insert ?\n))
4010 t)
4011 (and (y-or-n-p "Insert missing folding-top-mark? ")
4012 (progn (insert (concat folding-top-mark
4013 "<Replaced missing fold top mark>"
4014 (or folding-secondary-top-mark "")
4015 "\n"))
4016 t)))
4017 folding-internal-margins
4018 (<= 0 folding-internal-margins)
4019 (let* ((p1 (point))
4020 (p2 (progn (skip-chars-forward "\n") (point)))
4021 (p3 (progn (skip-chars-forward "\n\t ")
4022 (skip-chars-backward "\t " p2) (point))))
4023 (if (eq p2 p3)
4024 (or (eq p2 (setq p3 (+ p1 folding-internal-margins)))
4025 (if (< p2 p3)
4026 (newline (- p3 p2))
4027 (delete-region p3 p2)))
4028 (delete-region p1 p3)
4029 (or (eq 0 folding-internal-margins)
4030 (newline folding-internal-margins)))))
4031 (if (folding-use-overlays-p)
4032 (goto-char (overlay-start (cdr folding-narrow-overlays)))
4033 (goto-char (point-max)))
4034 (and (bolp)
4035 (progn (skip-chars-backward "\n")
4036 (delete-region (point) (point-max))))
4037 (beginning-of-line)
4038 (and (or (let (case-fold-search) (folding-mark-look-at-bottom-mark-p))
4039 (progn (goto-char (point-max)) nil)
4040 (and (y-or-n-p "Insert missing folding-bottom-mark? ")
4041 (progn
4042 (insert (concat "\n" folding-bottom-mark))
4043 (beginning-of-line)
4044 t)))
4045 folding-internal-margins
4046 (<= 0 folding-internal-margins)
4047 (let* ((p1 (point))
4048 (p2 (progn (skip-chars-backward "\n") (point)))
4049 (p3 (progn (skip-chars-backward "\n\t ")
4050 (skip-chars-forward "\t " p2) (point))))
4051 (if (eq p2 p3)
4052 (or (eq p2 (setq p3 (- p1 1 folding-internal-margins)))
4053 (if (> p2 p3)
4054 (newline (- p2 p3))
4055 (delete-region p2 p3)))
4056 (delete-region p3 p1)
4057 (newline (1+ folding-internal-margins))))))))
4058
4059 ;;}}}
4060
4061 ;;}}}
4062 ;;{{{ code: Operations on the whole buffer
4063
4064 ;;{{{ folding-whole-buffer
4065
4066 (defun folding-whole-buffer ()
4067 "Folds every fold in the current buffer.
4068 Fails if the fold markers are not balanced correctly.
4069
4070 If the buffer is being viewed in a fold, folds are repeatedly exited to
4071 get to the top level first (this allows the folds to be tidied on the
4072 way out). The buffer modification flag is not affected, and this
4073 function will work on read-only buffers."
4074 (interactive)
4075 (message "Folding buffer...")
4076 (let ((narrow-min (point-min))
4077 (narrow-max (point-max))
4078 folding-list)
4079 (save-excursion
4080 (widen)
4081 (goto-char 1)
4082 (setq folding-list (folding-skip-folds nil t))
4083 (narrow-to-region narrow-min narrow-max)
4084 (and (eq t folding-list)
4085 (error
4086 "Cannot fold whole buffer -- unmatched begin-fold mark `%s' `%s'"
4087 (current-buffer)
4088 folding-top-mark))
4089 (and (integerp (car folding-list))
4090 (error
4091 "Cannot fold whole buffer -- extraneous end-fold mark `%s' `%s'"
4092 (current-buffer)
4093 folding-bottom-mark))
4094 (folding-show-all)
4095 (widen)
4096 (goto-char 1)
4097 ;; Do the modifications forwards.
4098 (folding-subst-regions (nreverse (cdr folding-list)) ?\n ?\r))
4099 (beginning-of-line)
4100 (folding-narrow-to-region nil nil t)
4101 (message "Folding buffer... done")))
4102
4103 ;;}}}
4104 ;;{{{ folding-open-buffer
4105
4106 (defun folding-open-buffer ()
4107 "Unfolds the entire buffer, leaving the point where it is.
4108 Does not affect the buffer-modified flag, and can be used on read-only
4109 buffers."
4110 (interactive)
4111 (message "Unfolding buffer...")
4112 (folding-clear-stack)
4113 (folding-set-mode-line)
4114 (unwind-protect
4115 (progn
4116 (widen)
4117 (folding-subst-regions (list 1 (point-max)) ?\r ?\n))
4118 (folding-narrow-to-region nil nil t))
4119 (message "Unfolding buffer... done"))
4120
4121 ;;}}}
4122 ;;{{{ folding-convert-buffer-for-printing
4123
4124 (defun folding-convert-buffer-for-printing (&optional buffer pre-title post-title pad)
4125 "Remove folds from a buffer, for printing.
4126
4127 It copies the contents of the (hopefully) folded buffer BUFFER into a
4128 buffer called `*Unfolded: <Original-name>*', removing all of the fold
4129 marks. It keeps the titles of the folds, however, and numbers them.
4130 Subfolds are numbered in the form 5.1, 5.2, 5.3 etc., and the titles are
4131 indented to eleven characters.
4132
4133 It accepts four arguments. BUFFER is the name of the buffer to be
4134 operated on, or a buffer. nil means use the current buffer. PRE-TITLE
4135 is the text to go before the replacement fold titles, POST-TITLE is the
4136 text to go afterwards. Finally, if PAD is non-nil, the titles are all
4137 indented to the same column, which is eleven plus the length of
4138 PRE-TITLE. Otherwise just one space is placed between the number and
4139 the title."
4140 (interactive (list (read-buffer "Remove folds from buffer: "
4141 (buffer-name)
4142 t)
4143 (read-string "String to go before enumerated titles: ")
4144 (read-string "String to go after enumerated titles: ")
4145 (y-or-n-p "Pad section numbers with spaces? ")))
4146 (set-buffer (setq buffer (get-buffer buffer)))
4147 (setq pre-title (or pre-title "")
4148 post-title (or post-title ""))
4149 (or folding-mode
4150 (error "Must be in Folding mode before removing folds"))
4151 (let* ((new-buffer (get-buffer-create
4152 (concat "*Unfolded: "
4153 (buffer-name buffer)
4154 "*")))
4155 (section-list '(1))
4156 (section-prefix-list '(""))
4157 (secondary-mark-length (length folding-secondary-top-mark))
4158 (secondary-mark folding-secondary-top-mark)
4159 (mode major-mode)
4160 ;; [jari] Aug 14 1997
4161 ;; Regexp doesn't allow "footer text" like, so we add one more
4162 ;; regexp to loosen the end criteria
4163 ;;
4164 ;; {{{ Subsubsection 1
4165 ;; }}} Subsubsection 1
4166 ;;
4167 ;; was: (regexp folding-regexp)
4168 (regexp
4169 (concat "\\(^\\|\r\\)\\([ \t]*\\)\\(\\("
4170 (regexp-quote folding-top-mark)
4171 "\\)\\|\\("
4172 (regexp-quote folding-bottom-mark)
4173 "[ \t]*.*\\(\\)\\($\\|\r\\)\\)\\)"))
4174 title
4175 prefix)
4176 ;; was obsolete function: (buffer-flush-undo new-buffer)
4177 (buffer-disable-undo new-buffer)
4178 (with-current-buffer new-buffer
4179 (delete-region (point-min)
4180 (point-max)))
4181 (save-restriction
4182 (widen)
4183 (copy-to-buffer new-buffer (point-min) (point-max)))
4184 (display-buffer new-buffer t)
4185 (set-buffer new-buffer)
4186 (subst-char-in-region (point-min) (point-max) ?\r ?\n)
4187 (funcall mode)
4188 (while (re-search-forward regexp nil t)
4189 (if (match-beginning 4)
4190 (progn
4191 (goto-char (match-end 4))
4192
4193 ;; - Move after start fold and read the title from there
4194 ;; - Then move back and kill the fold mark
4195 ;;
4196 (setq title
4197 (buffer-substring (point)
4198 (progn (end-of-line)
4199 (point))))
4200 (delete-region (save-excursion
4201 (goto-char (match-beginning 4))
4202 (skip-chars-backward "\n\r")
4203 (point))
4204 (progn
4205 (skip-chars-forward "\n\r")
4206 (point)))
4207 (and (<= secondary-mark-length
4208 (length title))
4209 (string-equal secondary-mark
4210 (substring title
4211 (- secondary-mark-length)))
4212 (setq title (substring title
4213 0
4214 (- secondary-mark-length))))
4215 (setq section-prefix-list
4216 (cons (setq prefix (concat (car section-prefix-list)
4217 (int-to-string (car section-list))
4218 "."))
4219 section-prefix-list))
4220 (or (cdr section-list)
4221 (insert ?\n))
4222 (setq section-list (cons 1
4223 (cons (1+ (car section-list))
4224 (cdr section-list))))
4225 (setq title (concat prefix
4226 (if pad
4227 (make-string
4228 (max 2 (- 8 (length prefix))) ? )
4229 " ")
4230 title))
4231 (message "Reformatting: %s%s%s"
4232 pre-title
4233 title
4234 post-title)
4235 (insert "\n\n"
4236 pre-title
4237 title
4238 post-title
4239 "\n\n"))
4240 (goto-char (match-beginning 5))
4241 (or (setq section-list (cdr section-list))
4242 (error "Too many bottom-of-fold marks"))
4243
4244 (setq section-prefix-list (cdr section-prefix-list))
4245 (delete-region (point)
4246 (progn
4247 (forward-line 1)
4248 (point)))))
4249 (and (cdr section-list)
4250 (error
4251 "Too many top-of-fold marks -- reached end of file prematurely"))
4252 (goto-char (point-min))
4253 (buffer-enable-undo)
4254 (set-buffer-modified-p nil)
4255 (message "All folds reformatted.")))
4256
4257 ;;}}}
4258 ;;}}}
4259
4260 ;;{{{ code: Standard fold marks for various major modes
4261
4262 ;;{{{ A function to set default marks, `folding-add-to-marks-list'
4263
4264 (defun folding-add-to-marks-list
4265 (mode
4266 top
4267 bottom
4268 &optional
4269 secondary
4270 noforce
4271 message)
4272 "Add/set fold mark list for a particular major mode.
4273 When called interactively, asks for a `major-mode' name, and for
4274 fold marks to be used in that mode. It adds the new set to
4275 `folding-mode-marks-alist', and if the mode name is the same as the current
4276 major mode for the current buffer, the marks in use are also changed.
4277
4278 If called non-interactively, arguments are MODE, TOP, BOTTOM and
4279 SECONDARY. MODE is the symbol for the major mode for which marks are
4280 being set. TOP, BOTTOM and SECONDARY are strings, the three fold marks
4281 to be used. SECONDARY may be nil (as opposed to the empty string), but
4282 the other two must be non-empty strings, and is an optional argument.
4283
4284 Two other optional arguments are NOFORCE, meaning do not change the
4285 marks if marks are already set for the specified mode if non-nil, and
4286 MESSAGE, which causes a message to be displayed if it is non-nil. This
4287 is also the message displayed if the function is called interactively.
4288
4289 To set default fold marks for a particular mode, put something like the
4290 following in your .emacs:
4291
4292 \(folding-add-to-marks-list \\='major-mode \"(** {{{ \" \"(** }}} **)\" \" **)\")
4293
4294 Look at the variable `folding-mode-marks-alist' to see what default settings
4295 already apply.
4296
4297 `folding-set-marks' can be used to set the fold marks in use in the current
4298 buffer without affecting the default value for a particular mode."
4299 (interactive
4300 (let* ((mode (completing-read
4301 (concat "Add fold marks for major mode ("
4302 (symbol-name major-mode)
4303 "): ")
4304 obarray
4305 (lambda (arg)
4306 (and (commandp arg)
4307 (string-match "-mode\\'"
4308 (symbol-name arg))))
4309 t))
4310 (mode (if (equal mode "")
4311 major-mode
4312 (intern mode)))
4313 (object (assq mode folding-mode-marks-alist))
4314 (old-top (and object
4315 (nth 1 object)))
4316 top
4317 (old-bottom (and object
4318 (nth 2 object)))
4319 bottom
4320 (secondary (and object
4321 (nth 3 object)))
4322 (prompt "Top fold marker: "))
4323 (and (equal secondary "")
4324 (setq secondary nil))
4325 (while (not top)
4326 (setq top (read-string prompt (or old-top "{{{ ")))
4327 (and (equal top "")
4328 (setq top nil)))
4329 (setq prompt (concat prompt
4330 top
4331 ", Bottom marker: "))
4332 (while (not bottom)
4333 (setq bottom (read-string prompt (or old-bottom "}}}")))
4334 (and (equal bottom "")
4335 (setq bottom nil)))
4336 (setq prompt (concat prompt
4337 bottom
4338 (if secondary
4339 ", Secondary marker: "
4340 ", Secondary marker (none): "))
4341 secondary (read-string prompt secondary))
4342 (and (equal secondary "")
4343 (setq secondary nil))
4344 (list mode top bottom secondary nil t)))
4345 (let ((object (assq mode folding-mode-marks-alist)))
4346 (if (and object
4347 noforce
4348 message)
4349 (message "Fold markers for `%s' are already set."
4350 (symbol-name mode))
4351 (if object
4352 (or noforce
4353 (setcdr object (if secondary
4354 (list top bottom secondary)
4355 (list top bottom))))
4356 (setq folding-mode-marks-alist
4357 (cons (if secondary
4358 (list mode top bottom secondary)
4359 (list mode top bottom))
4360 folding-mode-marks-alist)))
4361 (and message
4362 (message "Set fold marks for `%s' to \"%s\" and \"%s\"."
4363 (symbol-name mode)
4364 (if secondary
4365 (concat top "name" secondary)
4366 (concat top "name"))
4367 bottom)
4368 (and (eq major-mode mode)
4369 (folding-set-marks top bottom secondary))))))
4370
4371 ;;}}}
4372 ;;{{{ Set some useful default fold marks
4373
4374 (folding-add-to-marks-list 'ada-mode "-- {{{" "-- }}}" nil t)
4375 (folding-add-to-marks-list 'asm-mode "; {{{" "; }}}" nil t)
4376 (folding-add-to-marks-list 'awk-mode "# {{{" "# }}}" nil t)
4377 (folding-add-to-marks-list 'Bison-mode "/* {{{" "/* }}} */" " */" t)
4378 (folding-add-to-marks-list 'latex-mode "%{{{" "%}}}" nil t)
4379 (folding-add-to-marks-list 'LaTeX-mode "%{{{" "%}}}" nil t)
4380 (folding-add-to-marks-list 'TeX-mode "%{{{" "%}}}" nil t)
4381 (folding-add-to-marks-list 'bibtex-mode "%{{{" "%}}} */" nil t)
4382 (folding-add-to-marks-list 'bison-mode "/* {{{" "/* }}} */" " */" t)
4383 (folding-add-to-marks-list 'c++-mode "// {{{" "// }}}" nil t)
4384 (folding-add-to-marks-list 'c-mode "/* {{{" "/* }}} */" " */" t)
4385 (folding-add-to-marks-list 'dcl-mode "! {{{" "! }}}" nil t)
4386 (folding-add-to-marks-list 'change-log-mode "{{{" "}}}" nil t)
4387 (folding-add-to-marks-list 'cperl-mode "# {{{" "# }}}" nil t)
4388 (folding-add-to-marks-list 'emacs-lisp-mode ";;{{{" ";;}}}" nil t)
4389 (folding-add-to-marks-list 'erlang-mode "%%{{{" "%%}}}" nil t)
4390 (folding-add-to-marks-list 'finder-mode "{{{" "}}}" nil t)
4391 (folding-add-to-marks-list 'fortran-mode "! {{{" "! }}}" nil t)
4392 (folding-add-to-marks-list 'f90-mode "! {{{" "! }}}" nil t)
4393 (folding-add-to-marks-list 'generic-mode ";# " ";\$" nil t)
4394 (folding-add-to-marks-list 'gofer-mode "-- {{{" "-- }}}" nil t)
4395 (folding-add-to-marks-list 'html-mode "<!-- {{{ " "<!-- }}} -->" " -->" t)
4396 (folding-add-to-marks-list 'icon-mode "# {{{" "# }}}" nil t)
4397 (folding-add-to-marks-list 'indented-text-mode "{{{" "}}}" nil t)
4398 (folding-add-to-marks-list 'java-mode "// {{{" "// }}}" nil t)
4399 (folding-add-to-marks-list 'javascript-mode "// {{{" "// }}}" nil t)
4400 (folding-add-to-marks-list 'jde-mode "// {{{" "// }}}" nil t)
4401 (folding-add-to-marks-list 'ksh-mode "# {{{" "# }}}" nil t)
4402 (folding-add-to-marks-list 'latex-mode "%{{{" "%}}}" nil t)
4403 (folding-add-to-marks-list 'lisp-interaction-mode ";;{{{" ";;}}}" nil t)
4404 (folding-add-to-marks-list 'lisp-mode ";;{{{" ";;}}}" nil t)
4405 (folding-add-to-marks-list 'm4-mode "# {{{" "# }}}" nil t)
4406 (folding-add-to-marks-list 'makefile-mode "# {{{" "# }}}" nil t)
4407 (folding-add-to-marks-list 'matlab-mode "%%%{{{" "%%%}}}" nil t)
4408 (folding-add-to-marks-list 'meta-mode "% {{{" "% }}}" nil t)
4409 (folding-add-to-marks-list 'ml-mode "(* {{{" "(* }}} *)" " *)" t)
4410 (folding-add-to-marks-list 'modula-2-mode "(* {{{" "(* }}} *)" " *)" t)
4411 (folding-add-to-marks-list 'nroff-mode "\\\\ {{{" "\\\\ }}}" nil t)
4412 (folding-add-to-marks-list 'occam-mode "-- {{{" "-- }}}" nil t)
4413 (folding-add-to-marks-list 'orwell-mode "{{{" "}}}" nil t)
4414 (folding-add-to-marks-list 'pascal-mode "{ ((( " "{ ))) }" " }" t)
4415 (folding-add-to-marks-list 'php-mode "// {{{" "// }}}" nil t)
4416 (folding-add-to-marks-list 'perl-mode "# {{{" "# }}}" nil t)
4417 (folding-add-to-marks-list 'plain-TeX-mode "%{{{" "%}}}" nil t)
4418 (folding-add-to-marks-list 'plain-tex-mode "%{{{" "%}}}" nil t)
4419 (folding-add-to-marks-list 'prolog-mode "% {{{" "% }}}" nil t)
4420 (folding-add-to-marks-list 'python-mode "# {{{" "# }}}" nil t)
4421 (folding-add-to-marks-list 'rexx-mode "/* {{{" "/* }}} */" " */" t)
4422 (folding-add-to-marks-list 'sh-mode "# {{{" "# }}}" nil t)
4423 (folding-add-to-marks-list 'sh-script-mode "# {{{" "# }}}" nil t)
4424 (folding-add-to-marks-list 'shellscript-mode "# {{{" "# }}}" nil t)
4425 (folding-add-to-marks-list 'sgml-mode "<!-- [[[ " "<!-- ]]] -->" " -->" t)
4426 (folding-add-to-marks-list 'simula-mode "! {{{" "! }}}" nil t)
4427 (folding-add-to-marks-list 'sml-mode "(* {{{" "(* }}} *)" " *)" t)
4428 (folding-add-to-marks-list 'sql-mode "-- {{{" "-- }}}" nil t)
4429 (folding-add-to-marks-list 'tcl-mode "#{{{" "#}}}" nil t)
4430 (folding-add-to-marks-list 'tex-mode "%{{{" "%}}}" nil t)
4431 (folding-add-to-marks-list 'texinfo-mode "@c {{{" "@c {{{endfold}}}" " }}}" t)
4432 (folding-add-to-marks-list 'text-mode "{{{" "}}}" nil t)
4433 (folding-add-to-marks-list 'vhdl-mode "# {{{" "# }}}" nil t)
4434 (folding-add-to-marks-list 'xerl-mode "%%{{{" "%%}}}" nil t)
4435 (folding-add-to-marks-list 'xrdb-mode "! {{{" "! }}}" nil t)
4436
4437 ;; heavy shell-perl-awk programmer in fundamental-mode need # prefix...
4438
4439 (folding-add-to-marks-list 'fundamental-mode "# {{{" "# }}}" nil t)
4440
4441 ;;}}}
4442
4443 ;;}}}
4444
4445 ;;{{{ code: Gross, crufty hacks that seem necessary
4446
4447 ;; The functions here have been tested with Emacs 18.55, Emacs 18.58,
4448 ;; Epoch 4.0p2 (based on Emacs 18.58) and XEmacs 19.6.
4449
4450 ;; Note that XEmacs 19.6 can't do selective-display, and its
4451 ;; "invisible extents" don't work either, so Folding mode just won't
4452 ;; work with that version.
4453
4454 ;; They shouldn't do the wrong thing with later versions of Emacs, but
4455 ;; they might not have the special effects either. They may appear to
4456 ;; be excessive; that is not the case. All of the peculiar things these
4457 ;; functions do is done to avoid some side-effect of Emacs' internal
4458 ;; logic that I have met. Some of them work around bugs or unfortunate
4459 ;; (lack of) features in Emacs. In most cases, it would be better to
4460 ;; move this into the Emacs C code.
4461
4462 ;; Folding mode is designed to be simple to cooperate with as many
4463 ;; things as possible. These functions go against that principle at the
4464 ;; coding level, but make life for the user bearable.
4465
4466 ;;{{{ folding-subst-regions
4467
4468 ;; Substitute newlines for carriage returns or vice versa.
4469 ;; Avoid excessive file locking.
4470
4471 ;; Substitutes characters in the buffer, even in a read-only buffer.
4472 ;; Takes LIST, a list of regions specified as sequence in the form
4473 ;; (START1 END1 START2 END2 ...). In every region specified by each
4474 ;; pair, substitutes each occurence of character FIND by REPLACE.
4475
4476 ;; The buffer-modified flag is not affected, undo information is not
4477 ;; kept for the change, and the function works on read-only files. This
4478 ;; function is much more efficient called with a long sequence than
4479 ;; called for each region in the sequence.
4480
4481 ;; If the buffer is not modified when the function is called, the
4482 ;; modified-flag is set before performing all the substitutions, and
4483 ;; locking is temporarily disabled. This prevents Emacs from trying to
4484 ;; make then delete a lock file for *every* substitution, which slows
4485 ;; folding considerably, especially on a slow networked filesystem.
4486 ;; Without this, on my system, folding files on startup (and reading
4487 ;; other peoples' folded files) takes about five times longer. Emacs
4488 ;; still locks the file once for this call under those circumstances; I
4489 ;; can't think of a way around that, but it isn't really a problem.
4490
4491 ;; I consider these problems to be a bug in `subst-char-in-region'.
4492
4493 (defun folding-subst-regions (list find replace)
4494 "Substitute \\r and \\n using LIST FIND REPLACE."
4495 (let ((buffer-read-only buffer-read-only) ;; Protect read-only flag.
4496 (modified (buffer-modified-p))
4497 (font-lock-mode nil)
4498 (lazy-lock-mode nil)
4499 (overlay-p (folding-use-overlays-p))
4500 (ask1 (symbol-function 'ask-user-about-supersession-threat))
4501 (ask2 (symbol-function 'ask-user-about-lock)))
4502 (if lazy-lock-mode ;; no-op: Byte compiler silencer
4503 (setq lazy-lock-mode t))
4504 (unwind-protect
4505 (progn
4506 (setq buffer-read-only nil)
4507 (or modified
4508 (progn
4509 (fset 'ask-user-about-supersession-threat
4510 #'(lambda (&rest x) nil))
4511 (fset 'ask-user-about-lock
4512 #'(lambda (&rest x) nil))
4513 (set-buffer-modified-p t))) ; Prevent file locking in the loop
4514 (while list
4515 (if overlay-p
4516 (folding-flag-region (car list) (nth 1 list) (eq find ?\n))
4517 (subst-char-in-region (car list) (nth 1 list) find replace t))
4518 (setq list (cdr (cdr list)))))
4519 ;; buffer-read-only is restored by the let.
4520 ;; Don't want to change MODIFF time if it was modified before.
4521 (or modified
4522 (unwind-protect
4523 (set-buffer-modified-p nil)
4524 (fset 'ask-user-about-supersession-threat ask1)
4525 (fset 'ask-user-about-lock ask2))))))
4526
4527 ;;}}}
4528 ;;{{{ folding-narrow-to-region
4529
4530 ;; Narrow to region, without surprising displays.
4531
4532 ;; Similar to `narrow-to-region', but also adjusts window-start to be
4533 ;; the start of the narrowed region. If an optional argument CENTRE is
4534 ;; non-nil, the window-start is positioned to leave the point at the
4535 ;; centre of the window, like `recenter'. START may be nil, in which
4536 ;; case the function acts more like `widen'.
4537
4538 ;; Actually, all the window-starts for every window displaying the
4539 ;; buffer, as well as the last_window_start for the buffer are set. The
4540 ;; points in every window are set to the point in the current buffer.
4541 ;; All this logic is necessary to prevent the display getting really
4542 ;; weird occasionally, even if there is only one window. Try making
4543 ;; this function like normal `narrow-to-region' with a touch of
4544 ;; `recenter', then moving around lots of folds in a buffer displayed in
4545 ;; several windows. You'll see what I mean.
4546
4547 ;; last_window_start is set by making sure that the selected window is
4548 ;; displaying the current buffer, then setting the window-start, then
4549 ;; making the selected window display another buffer (which sets
4550 ;; last_window_start), then setting the selected window to redisplay the
4551 ;; buffer it displayed originally.
4552
4553 ;; Note that whenever window-start is set, the point cannot be moved
4554 ;; outside the displayed area until after a proper redisplay. If this
4555 ;; is possible, centre the display on the point.
4556
4557 ;; In Emacs 19; Epoch or XEmacs, searches all screens for all
4558 ;; windows. In Emacs 19, they are called "frames".
4559
4560 (defun folding-narrow-to-region (&optional start end centre)
4561 "Narrow to region START END, possibly CENTRE."
4562 (let* ((the-window (selected-window))
4563 (selected-buffer (window-buffer the-window))
4564 (window-ring the-window)
4565 (window the-window)
4566 (point (point))
4567 (buffer (current-buffer))
4568 temp)
4569 (unwind-protect
4570 (progn
4571 (unwind-protect
4572 (progn
4573 (if (folding-use-overlays-p)
4574 (if start
4575 (folding-narrow-aux start end t)
4576 (folding-narrow-aux nil nil nil))
4577 (if start
4578 (narrow-to-region start end)
4579 (widen)))
4580 (setq point (point))
4581 (set-window-buffer window buffer)
4582 (while (progn
4583 (and (eq buffer (window-buffer window))
4584 (if centre
4585 (progn
4586 (select-window window)
4587 (goto-char point)
4588 (vertical-motion
4589 (- (ash (window-height window) -1)))
4590 (set-window-start window (point))
4591 (set-window-point window point))
4592 (set-window-start window (or start 1))
4593 (set-window-point window point)))
4594
4595 (not (eq (setq window (next-window window nil t))
4596 window-ring)))))
4597 nil ;; epoch screen
4598 (select-window the-window)) ;; unwind-protect INNER
4599 ;; Set last_window_start.
4600 (unwind-protect
4601 (if (not (eq buffer selected-buffer))
4602 (set-window-buffer the-window selected-buffer)
4603 (if (get-buffer "*scratch*")
4604 (set-window-buffer the-window (get-buffer "*scratch*"))
4605 (set-window-buffer
4606 the-window (setq temp (generate-new-buffer " *temp*"))))
4607 (set-window-buffer the-window buffer))
4608 (and temp
4609 (kill-buffer temp))))
4610 ;; Undo this side-effect of set-window-buffer.
4611 (set-buffer buffer)
4612 (goto-char (point)))))
4613
4614 ;;}}}
4615
4616 ;;}}}
4617
4618 ;;{{{ code: folding-end-mode-quickly
4619
4620 (defun folding-end-mode-quickly ()
4621 "Replace all ^M's with linefeeds and widen a folded buffer.
4622 Only has any effect if Folding mode is active.
4623
4624 This should not in general be used for anything. It is used when changing
4625 major modes, by being placed in kill-mode-tidy-alist, to tidy the buffer
4626 slightly. It is similar to `(folding-mode 0)', except that it does not
4627 restore saved keymaps etc. Repeat: Do not use this function. Its
4628 behaviour is liable to change."
4629 (and (boundp 'folding-mode)
4630 (assq 'folding-mode
4631 (buffer-local-variables))
4632 folding-mode
4633 (progn
4634 (if (folding-use-overlays-p)
4635 (folding-narrow-to-region nil nil)
4636 (widen))
4637 (folding-clear-stack)
4638 (folding-subst-regions (list 1 (point-max)) ?\r ?\n))))
4639
4640 ;;{{{ folding-eval-current-buffer-open-folds
4641
4642 (defun folding-eval-current-buffer-open-folds (&optional printflag)
4643 "Evaluate all of a folded buffer as Lisp code.
4644 Unlike `eval-current-buffer', this function will evaluate all of a
4645 buffer, even if it is folded. It will also work correctly on non-folded
4646 buffers, so is a good candidate for being bound to a key if you program
4647 in Emacs-Lisp.
4648
4649 It works by making a copy of the current buffer in another buffer,
4650 unfolding it and evaluating it. It then deletes the copy.
4651
4652 Programs can pass argument PRINTFLAG which controls printing of output:
4653 nil means discard it; anything else is stream for print."
4654 (interactive)
4655 (if (or (and (boundp 'folding-mode)
4656 folding-mode))
4657 (let ((temp-buffer
4658 (generate-new-buffer (buffer-name))))
4659 (message "Evaluating unfolded buffer...")
4660 (save-restriction
4661 (widen)
4662 (copy-to-buffer temp-buffer 1 (point-max)))
4663 (set-buffer temp-buffer)
4664 (subst-char-in-region 1 (point-max) ?\r ?\n)
4665 (let ((real-message-def (symbol-function 'message))
4666 (suppress-eval-message))
4667 (fset 'message
4668 (lambda (&rest args)
4669 (setq suppress-eval-message t)
4670 (fset 'message real-message-def)
4671 (apply 'message args)))
4672 (unwind-protect
4673 (folding-eval-buffer printflag)
4674 (fset 'message real-message-def)
4675 (kill-buffer temp-buffer))
4676 (or suppress-eval-message
4677 (message "Evaluating unfolded buffer... Done"))))
4678 (folding-eval-buffer printflag)))
4679
4680 ;;}}}
4681
4682 ;;}}}
4683
4684 ;;{{{ code: ISearch support, walks in and out of folds
4685
4686 ;; This used to be a package of it's own.
4687 ;; Requires Emacs 19 or XEmacs. Does not work under Emacs 18.
4688
4689 ;;{{{ Variables
4690
4691 (defcustom folding-isearch-install t
4692 "*When non-nil, the isearch commands will handle folds."
4693 :type 'boolean
4694 :group 'folding)
4695
4696 (defvar folding-isearch-stack nil
4697 "Temporary storage for `folding-stack' during isearch.")
4698
4699 ;; Lists of isearch commands to replace
4700
4701 ;; These do normal searching.
4702
4703 (defvar folding-isearch-normal-cmds
4704 '(isearch-repeat-forward
4705 isearch-repeat-backward
4706 isearch-toggle-regexp
4707 isearch-toggle-case-fold
4708 isearch-delete-char
4709 isearch-abort
4710 isearch-quote-char
4711 isearch-other-control-char
4712 isearch-other-meta-char
4713 isearch-return-char
4714 isearch-exit
4715 isearch-printing-char
4716 isearch-whitespace-chars
4717 isearch-yank-word
4718 isearch-yank-line
4719 isearch-yank-kill
4720 isearch-*-char
4721 isearch-\|-char
4722 isearch-mode-help
4723 isearch-yank-x-selection
4724 isearch-yank-x-clipboard)
4725 "List if isearch commands doing normal search.")
4726
4727 ;; Enables the user to edit the search string
4728
4729 ;; Missing, present in XEmacs isearch-mode.el. Not necessary?
4730 ;; isearch-ring-advance-edit, isearch-ring-retreat-edit, isearch-complete-edit
4731 ;; isearch-nonincremental-exit-minibuffer, isearch-yank-x-selection,
4732 ;; isearch-yank-x-clipboard
4733
4734 (defvar folding-isearch-edit-enter-cmds
4735 '(isearch-edit-string
4736 isearch-ring-advance
4737 isearch-ring-retreat
4738 isearch-complete) ; (Could also stay in search mode!)
4739 "List of isearch commands which enters search string edit.")
4740
4741 ;; Continues searching after editing.
4742
4743 (defvar folding-isearch-edit-exit-cmds
4744 '(isearch-forward-exit-minibuffer ; Exits edit
4745 isearch-reverse-exit-minibuffer
4746 isearch-nonincremental-exit-minibuffer)
4747 "List of isearch commands which exits search string edit.")
4748
4749 ;;}}}
4750 ;;{{{ Keymaps (an Isearch hook)
4751
4752 (defvar folding-isearch-mode-map nil
4753 "Modified copy of the isearch keymap.")
4754
4755 ;; Create local copies of the keymaps. The `isearch-mode-map' is
4756 ;; copied to `folding-isearch-mode-map' while `minibuffer-local-isearch-map'
4757 ;; is made local. (Its name is used explicitly.)
4758 ;;
4759 ;; Note: This is called every time the search is started.
4760
4761 (defun folding-isearch-hook-function ()
4762 "Update the isearch keymaps for usage with folding mode."
4763 (if (and (boundp 'folding-mode) folding-mode)
4764 (let ((cmds (append folding-isearch-normal-cmds
4765 folding-isearch-edit-enter-cmds
4766 folding-isearch-edit-exit-cmds)))
4767 (setq folding-isearch-mode-map (copy-keymap isearch-mode-map))
4768 (make-local-variable 'minibuffer-local-isearch-map)
4769 ;; Make sure the destructive operations below doesn't alter
4770 ;; the global instance of the map.
4771 (setq minibuffer-local-isearch-map
4772 (copy-keymap minibuffer-local-isearch-map))
4773 (setq folding-isearch-stack folding-stack)
4774 (while cmds
4775 (substitute-key-definition
4776 (car cmds)
4777 (intern (concat "folding-" (symbol-name (car cmds))))
4778 folding-isearch-mode-map)
4779 (substitute-key-definition
4780 (car cmds)
4781 (intern (concat "folding-" (symbol-name (car cmds))))
4782 minibuffer-local-isearch-map)
4783 (setq cmds (cdr cmds)))
4784 ;; Install our keymap
4785 (cond
4786 (folding-xemacs-p
4787 (let ((f 'set-keymap-name))
4788 (funcall f folding-isearch-mode-map 'folding-isearch-mode-map))
4789 ;; Later version of XEmacs (21.2+) use overriding-local-map
4790 ;; for isearch keymap rather than fiddling with
4791 ;; minor-mode-map-alist. This is so isearch keymaps take
4792 ;; precedence over extent-local keymaps. We will support
4793 ;; both ways here. Keymaps will be restored as side-effect
4794 ;; of isearch-abort and isearch-quit
4795 (cond
4796 ;; if overriding-local-map is in use
4797 ((and (boundp 'overriding-local-map) overriding-local-map)
4798 (set-keymap-parent folding-isearch-mode-map overriding-local-map)
4799 (setq overriding-local-map folding-isearch-mode-map))
4800 ;; otherwise fiddle with minor-mode-map-alist
4801 (t
4802 (setq minor-mode-map-alist
4803 (cons (cons 'isearch-mode folding-isearch-mode-map)
4804 (delq (assoc 'isearch-mode minor-mode-map-alist)
4805 minor-mode-map-alist))))))
4806 ((boundp 'overriding-terminal-local-map)
4807 (funcall (symbol-function 'set)
4808 'overriding-terminal-local-map folding-isearch-mode-map))
4809 ((boundp 'overriding-local-map)
4810 (setq overriding-local-map folding-isearch-mode-map))))))
4811
4812 ;; Undoes the `folding-isearch-hook-function' function.
4813
4814 (defun folding-isearch-end-hook-function ()
4815 "Actions to perform at the end of isearch in folding mode."
4816 (when (and (boundp 'folding-mode) folding-mode)
4817 (kill-local-variable 'minibuffer-local-isearch-map)
4818 (setq folding-stack folding-isearch-stack)))
4819
4820 (when folding-isearch-install
4821 (add-hook 'isearch-mode-hook 'folding-isearch-hook-function)
4822 (add-hook 'isearch-mode-end-hook 'folding-isearch-end-hook-function))
4823
4824 ;;}}}
4825 ;;{{{ Normal search routines
4826
4827 ;; Generate the replacement functions of the form:
4828 ;; (defun folding-isearch-repeat-forward ()
4829 ;; (interactive)
4830 ;; (folding-isearch-general 'isearch-repeat-forward))
4831
4832 (let ((cmds folding-isearch-normal-cmds))
4833 (while cmds
4834 (eval
4835 `(defun ,(intern (concat "folding-" (symbol-name (car cmds))))
4836 nil
4837 "Automatically generated"
4838 (interactive)
4839 (folding-isearch-general (quote ,(car cmds)))))
4840 (setq cmds (cdr cmds))))
4841
4842 ;; The HEART! Executes command and updates the foldings.
4843 ;; This is capable of detecting a `quit'.
4844
4845 (defun folding-isearch-general (function)
4846 "Execute isearch command FUNCTION and adjusts the folding."
4847 (let* ((quit-isearch nil)
4848 (area-beg (point-min))
4849 (area-end (point-max))
4850 pos)
4851 (cond
4852 (t
4853 (save-restriction
4854 (widen)
4855 (condition-case nil
4856 (funcall function)
4857 (quit (setq quit-isearch t)))
4858 (setq pos (point)))
4859 ;; Situation
4860 ;; o user has folded buffer
4861 ;; o He manually narrows, say to function !
4862 ;; --> there is no fold marks at the beg/end --> this is not a fold
4863 (condition-case nil
4864 ;; "current mode has no fold marks..."
4865 (folding-region-has-folding-marks-p area-beg area-end)
4866 (error (setq quit-isearch t)))
4867 (folding-goto-char pos)))
4868 (if quit-isearch
4869 (signal 'quit '(isearch)))))
4870
4871 ;;}}}
4872 ;;{{{ Edit search string support
4873
4874 (defvar folding-isearch-current-buffer nil
4875 "The buffer we are editing, so we can widen it when in minibuffer.")
4876
4877 ;; Functions which enters edit mode.
4878
4879 (defun folding-isearch-edit-string ()
4880 "Replace `isearch-edit-string' when in `folding-mode'."
4881 (interactive)
4882 (folding-isearch-start-edit 'isearch-edit-string))
4883
4884 (defun folding-isearch-ring-advance ()
4885 "Replace `isearch-ring-advance' when in `folding-mode'."
4886 (interactive)
4887 (folding-isearch-start-edit 'isearch-ring-advance))
4888
4889 (defun folding-isearch-ring-retreat ()
4890 "Replace `isearch-ring-retreat' when in `folding-mode'."
4891 (interactive)
4892 (folding-isearch-start-edit 'isearch-ring-retreat))
4893
4894 (defun folding-isearch-complete ()
4895 "Replace `isearch-complete' when in `folding-mode'."
4896 (interactive)
4897 (folding-isearch-start-edit 'isearch-complete))
4898
4899 ;; Start and wait for editing. When (funcall fnk) returns
4900 ;; we are back in interactive search mode.
4901 ;;
4902 ;; Store match data!
4903
4904 (defun folding-isearch-start-edit (function)
4905 "Edit with function FUNCTION."
4906 (let (pos)
4907 (setq folding-isearch-current-buffer (current-buffer))
4908 (save-restriction
4909 (funcall function)
4910 ;; Here, we are widened, by folding-isearch-*-exit-minibuffer.
4911 (setq pos (point)))
4912 (folding-goto-char pos)))
4913
4914 ;; Functions which exits edit mode.
4915
4916 ;; The `widen' below will be caught by the `save-restriction' above, thus
4917 ;; this will not cripple `folding-stack'.
4918
4919 (defun folding-isearch-forward-exit-minibuffer ()
4920 "Replace `isearch-forward-exit-minibuffer' when in `folding-mode'."
4921 (interactive)
4922 ;; Make sure we can continue searching outside narrowing.
4923 (with-current-buffer folding-isearch-current-buffer
4924 (widen))
4925 (isearch-forward-exit-minibuffer))
4926
4927 (defun folding-isearch-reverse-exit-minibuffer ()
4928 "Replace `isearch-reverse-exit-minibuffer' when in `folding-mode'."
4929 (interactive)
4930 ;; Make sure we can continue searching outside narrowing.
4931 (with-current-buffer folding-isearch-current-buffer
4932 (widen))
4933 (isearch-reverse-exit-minibuffer))
4934
4935 (defun folding-isearch-nonincremental-exit-minibuffer ()
4936 "Replace `isearch-reverse-exit-minibuffer' while in `folding-mode'."
4937 (interactive)
4938 ;; Make sure we can continue searching outside narrowing.
4939 (with-current-buffer folding-isearch-current-buffer
4940 (widen))
4941 (folding-exit-minibuffer))
4942
4943 ;;}}}
4944 ;;{{{ Special XEmacs support
4945
4946 ;; In XEmacs, all isearch commands must have the property `isearch-command'.
4947
4948 (if folding-xemacs-p
4949 (let ((cmds (append folding-isearch-normal-cmds
4950 folding-isearch-edit-enter-cmds
4951 folding-isearch-edit-exit-cmds)))
4952 (while cmds
4953 (put (intern (concat "folding-" (symbol-name (car cmds))))
4954 'isearch-command t)
4955 (setq cmds (cdr cmds)))))
4956
4957 ;;}}}
4958 ;;{{{ General purpose function.
4959
4960 (defun folding-goto-char (pos)
4961 "Goto character POS, changing fold if necessary."
4962 ;; Make sure POS is inside the visible area of the buffer.
4963 (goto-char pos)
4964 (if (eq pos (point)) ; Point inside narrowed area?
4965 nil
4966 (folding-show-all) ; Fold everything and goto top.
4967 (goto-char pos))
4968 ;; Enter if point is folded.
4969 (if (folding-point-folded-p pos)
4970 (progn
4971 (folding-shift-in) ; folding-shift-in can change the pos.
4972 (setq folding-isearch-stack folding-stack)
4973 (setq folding-stack '(folded))
4974 (goto-char pos))))
4975
4976 (defun folding-point-folded-p (pos)
4977 "Non-nil when POS is not visible."
4978 (if (folding-use-overlays-p)
4979 (let ((overlays (overlays-at (point)))
4980 (found nil))
4981 (while (and (not found) (overlayp (car overlays)))
4982 (setq found (overlay-get (car overlays) 'fold)
4983 overlays (cdr overlays)))
4984 found)
4985 (save-excursion
4986 (goto-char pos)
4987 (beginning-of-line)
4988 (skip-chars-forward "^\r" pos)
4989 (not (eq pos (point))))))
4990
4991 ;;}}}
4992
4993 ;;}}}
4994 ;;{{{ code: Additional functions
4995
4996 (defvar folding-comment-folding-table
4997 '((c-mode
4998 folding-comment-c-mode
4999 folding-uncomment-c-mode))
5000 "Table of functions to comment and uncomment folds.
5001 Function is called with two arguments:
5002
5003 number start of fold mark
5004 marker end of fold mark
5005
5006 Function must return:
5007
5008 (beg . end) start of fold, end of fold
5009
5010 Table Format:
5011 \\='((MAJOR-MODE COMMENT-FUNCTION UNCOMMENT-FUNCTION) ..)")
5012
5013 (defun folding-insert-advertise-folding-mode ()
5014 "Insert Small text describing where to the get the folding at point.
5015 This may be useful banner to inform other people why your code
5016 is formatted like it is and how to view it correctly."
5017 (interactive)
5018 (let* ((prefix "")
5019 (re (or comment-start-skip
5020 (and comment-start
5021 (concat "^[ \t]*" comment-start "+[ \t]*")))))
5022 (when re
5023 (save-excursion
5024 (beginning-of-line)
5025 (when (or (re-search-forward re nil t)
5026 (progn
5027 (goto-char (point-min))
5028 (re-search-forward re nil t)))
5029 (setq prefix (match-string 0)))))
5030 (beginning-of-line)
5031 (dolist (line
5032 (list
5033 "File layout controlled by Emacs folding.el available at: "
5034 folding-package-url-location))
5035 (insert "\n" prefix line))))
5036
5037 (defun folding-uncomment-mode-generic (beg end tag)
5038 "In region (BEG . END) remove two TAG lines."
5039 (re-search-forward tag (marker-position end))
5040 (beginning-of-line)
5041 (kill-line 1)
5042 (re-search-forward tag (marker-position end))
5043 (beginning-of-line)
5044 (kill-line 1)
5045 (cons beg end))
5046
5047 (defun folding-comment-mode-generic (beg end tag1 &optional tag2)
5048 "Return (BEG . END) and Add two TAG1 and TAG2 lines."
5049 (insert tag1)
5050 (goto-char (marker-position end))
5051 (insert (or tag2 tag1))
5052 (cons beg end))
5053
5054 (defun folding-uncomment-c-mode (beg end)
5055 "Uncomment region BEG END."
5056 (folding-uncomment-mode-generic
5057 beg end (regexp-quote " comment /* FOLDING -COM- */")))
5058
5059 (defun folding-comment-c-mode (beg end)
5060 "Comment region BEG END."
5061 (let* ((tag " /* FOLDING -COM- */"))
5062 (folding-comment-mode-generic
5063 beg
5064 end
5065 (concat "#if comment" tag "\n")
5066 (concat "#endif comment" tag "\n"))))
5067
5068 (defun folding-comment-fold (&optional uncomment)
5069 "Comment or UNCOMMENT all text inside single fold.
5070 If there are subfolds this function does not work as expected.
5071 User must know that there are no subfolds.
5072
5073 The heading has -COM- at the end when the fold is commented.
5074 Point must be over fold heading {{{ when function is called.
5075
5076 Note:
5077
5078 You can use this function only in modes that do not have
5079 `comment-end'. I.e. do not use this function in modes like C (/*
5080 */), because nested comments are not allowed. See this:
5081
5082 /* {{{ fold */
5083 code /* comment of the code */
5084 /* }}} */
5085
5086 Fold cannot know how to comment the code inside fold, because
5087 comments do not nest.
5088
5089 Implementation detail:
5090
5091 {{{ FoldHeader-COM-
5092
5093 If the fold header has -COM- at the end, then the fold is supposed to
5094 be commented. And if there is no -COM- then fold will be considered
5095 as normal fold. Do not loose or add the -COM- yourself or it will
5096 confuse the state of the fold.
5097
5098 References:
5099
5100 `folding-comment-folding-table'"
5101 (interactive "P")
5102 (let* ((state (folding-mark-look-at 'move))
5103 (closed (eq 0 state))
5104 (id "-COM-")
5105 (opoint (point))
5106 (mode-elt (assq major-mode folding-comment-folding-table))
5107 comment
5108 ret
5109 beg
5110 end)
5111 (unless mode-elt
5112 (if (stringp (nth 2 (folding-get-mode-marks major-mode)))
5113 (error "\
5114 Folding: function usage error, mode with `comment-end' is not supported.")))
5115 (when (or (null comment-start)
5116 (not (string-match "[^ \t\n]" comment-start)))
5117 (error "Empty comment-start."))
5118 (unless (memq state '( 0 1 11))
5119 (error "Incorrect fold state. Point must be over {{{."))
5120 ;; There is nothing to do if this fold heading does not have
5121 ;; the ID when uncommenting the fold.
5122 (setq state (looking-at (concat ".*" id)))
5123 (when (or (and uncomment state)
5124 (and (null uncomment) (null state)))
5125 (when closed (save-excursion (folding-show-current-entry)))
5126 (folding-pick-move) ;Go to end
5127 (beginning-of-line)
5128 (setq end (point-marker))
5129 (goto-char opoint) ;And off the fold heading
5130 (forward-line 1)
5131 (setq beg (point))
5132 (setq comment (concat comment-start id))
5133 (cond
5134 (mode-elt
5135 (setq ret
5136 (if uncomment
5137 (funcall (nth 2 mode-elt) (point) end)
5138 (funcall (nth 1 mode-elt) (point) end)))
5139 (goto-char (cdr ret)))
5140 (uncomment
5141 (while (< (point) (marker-position end))
5142 (if (looking-at comment)
5143 (delete-region (point) (match-end 0)))
5144 (forward-line 1)))
5145 (t
5146 (while (< (point) (marker-position end))
5147 (if (not (looking-at comment))
5148 (insert comment))
5149 (forward-line 1))))
5150 (setq end nil) ;kill marker
5151 ;; Remove the possible tag from the fold name line
5152 (goto-char opoint)
5153 (setq id (concat (or comment-start "") id (or comment-end "")))
5154 (if (re-search-forward (regexp-quote id) beg t)
5155 (delete-region (match-beginning 0) (match-end 0)))
5156 (when (null uncomment)
5157 (end-of-line)
5158 (insert id))
5159 (if closed
5160 (folding-hide-current-entry))
5161 (goto-char opoint))))
5162
5163 (defun folding-convert-to-major-folds ()
5164 "Convert fold mark items according to `major-mode'.
5165 This function replaces all fold markings }}} and {{{
5166 with major mode\\='s fold marks.
5167
5168 As a side effect also corrects all foldings to standard notation.
5169 Eg. following, where correct folding-beg should be \"#{{{ \"
5170 Note that /// marks foldings.
5171
5172 /// ;wrong fold
5173 # /// ;too many spaces, fold format error
5174 # ///title ;ok, but title too close
5175
5176 produces
5177
5178 #///
5179 #///
5180 #/// title
5181
5182 You must unfold whole buffer before using this function."
5183 (interactive)
5184 (let (case-fold-search
5185 (bm "{{{") ; begin match mark
5186 (em "}}}") ;
5187 el ; element
5188 b ; begin
5189 e ; end
5190 e2 ; end2
5191 pp)
5192 (catch 'out ; is folding active/loaded ??
5193 (unless (setq el (folding-get-mode-marks major-mode))
5194 (throw 'out t)) ; ** no mode found
5195 ;; ok , we're in business. Search whole buffer and replace.
5196 (setq b (elt el 0)
5197 e (elt el 1)
5198 e2 (or (elt el 2) ""))
5199 (save-excursion
5200 (goto-char (point-min)) ; start from the beginning of buffer
5201 (while (re-search-forward (regexp-quote bm) nil t)
5202 ;; set the end position for fold marker
5203 (setq pp (point))
5204 (beginning-of-line)
5205 (if (looking-at (regexp-quote b)) ; should be mode-marked; ok, ignore
5206 (goto-char pp) ; note that beg-of-l cmd, move rexp
5207 (delete-region (point) pp)
5208 (insert b)
5209 (when (not (string= "" e2))
5210 (unless (looking-at (concat ".*" (regexp-quote e2)))
5211 ;; replace with right fold mark
5212 (end-of-line)
5213 (insert e2)))))
5214 ;; handle end marks , identical func compared to prev.
5215 (goto-char (point-min))
5216 (while (re-search-forward (regexp-quote em)nil t)
5217 (setq pp (point))
5218 (beginning-of-line)
5219 (if (looking-at (regexp-quote e))
5220 (goto-char pp)
5221 (delete-region (point) (progn (end-of-line) (point)))
5222 (insert e)))))))
5223
5224 (defun folding-all-comment-blocks-in-region (beg end)
5225 "Put all comments in folds inside BEG END.
5226 Notice: Make sure there is no interfering folds inside the area,
5227 because the results may and up corrupted.
5228
5229 This only works for modes that DO NOT have `comment-end'.
5230 The `comment-start' must be left flushed in order to counted in.
5231
5232 After this
5233
5234 ;; comment
5235 ;; comment
5236
5237 code
5238
5239 ;; comment
5240 ;; comment
5241
5242 code
5243
5244 The result will be:
5245
5246 ;; {{{ 1
5247
5248 ;; comment
5249 ;; comment
5250
5251 ;; }}}
5252
5253 code
5254
5255 ;; {{{ 2
5256
5257 ;; comment
5258 ;; comment
5259
5260 ;; }}}
5261
5262 code"
5263 (interactive "*r")
5264 (unless comment-start
5265 (error "Folding: Mode does not define `comment-start'"))
5266 (when (and (stringp comment-end)
5267 (string-match "[^ \t]" comment-end))
5268 (error "Folding: Mode defines non-empty `comment-end'."))
5269 (let* ((count 0)
5270 (comment-regexp (concat "^" comment-start))
5271 (marker (point-marker))
5272 done)
5273 (cl-destructuring-bind (left right ignore)
5274 (folding-get-mode-marks)
5275 ;; Bytecomp silencer: variable ignore bound but not referenced
5276 (if ignore (setq ignore ignore))
5277 ;; %%%{{{ --> "%%%"
5278 (string-match (concat (regexp-quote comment-start) "+") left)
5279 (save-excursion
5280 (goto-char beg)
5281 (beginning-of-line)
5282 (while (re-search-forward comment-regexp nil t)
5283 (move-marker marker (point))
5284 (setq done nil)
5285 (beginning-of-line)
5286 (forward-line -1)
5287 ;; 2 previous lines Must not contain FOLD beginning already
5288 (unless (looking-at (regexp-quote left))
5289 (forward-line -1)
5290 (unless (looking-at (regexp-quote left))
5291 (goto-char (marker-position marker))
5292 (beginning-of-line)
5293 (insert left " " (int-to-string count) "\n\n")
5294 (setq count (1+ count))
5295 (setq done t)))
5296 (goto-char (marker-position marker))
5297 (when done
5298 ;; Try finding pat of the comment block
5299 (if (not (re-search-forward "^[ \t]*$" nil t))
5300 (goto-char end))
5301 (open-line 1)
5302 (forward-line 1)
5303 (insert right "\n")))))))
5304
5305 ;;}}}
5306 ;;{{{ code: Overlay support
5307
5308 (defun folding-use-overlays-p ()
5309 "Should folding use overlays?."
5310 (if folding-allow-overlays
5311 (if folding-xemacs-p
5312 ;; See if we can load overlay.el library that comes in 19.15
5313 ;; This call returns t or nil if load was successful
5314 ;; Note: is there provide statement? Load is so radical
5315 (load "overlay" 'noerr)
5316 t)))
5317
5318 (defun folding-flag-region (from to flag)
5319 "Hide or show lines from FROM to TO, according to FLAG.
5320 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
5321 (let ((inhibit-read-only t)
5322 overlay)
5323 (save-excursion
5324 (goto-char from)
5325 (end-of-line)
5326 (cond
5327 (flag
5328 (setq overlay (make-overlay (point) to))
5329 (folding-make-overlay-hidden overlay))
5330 (t
5331 (if (fboundp 'hs-discard-overlays)
5332 (funcall (symbol-function 'hs-discard-overlays)
5333 (point) to 'invisible t)))))))
5334
5335 (defun folding-make-overlay-hidden (overlay)
5336 "Make OVERLAY hidden."
5337 (overlay-put overlay 'fold t)
5338 ;; (overlay-put overlay 'intangible t)
5339 (overlay-put overlay 'invisible t)
5340 (overlay-put overlay 'owner 'folding))
5341
5342 (defun folding-narrow-aux (start end arg)
5343 "Narrow. Make overlay from `point-min' to START.
5344 And from END t `point-min'. If ARG is nil, delete overlays."
5345 (if (null arg)
5346 (cond
5347 (folding-narrow-overlays
5348 (delete-overlay (car folding-narrow-overlays))
5349 (delete-overlay (cdr folding-narrow-overlays))
5350 (setq folding-narrow-overlays nil)))
5351 (let ((overlay-beg (make-overlay (point-min) start))
5352 (overlay-end (make-overlay end (point-max))))
5353 (overlay-put overlay-beg 'folding-narrow t)
5354 (overlay-put overlay-beg 'invisible t)
5355 (overlay-put overlay-beg 'owner 'folding)
5356 (overlay-put overlay-end 'folding-narrow t)
5357 (overlay-put overlay-end 'invisible t)
5358 (overlay-put overlay-end 'owner 'folding)
5359 (setq folding-narrow-overlays (cons overlay-beg overlay-end)))))
5360
5361 ;;}}}
5362 ;;{{{ code: end of file tag, provide
5363
5364 (folding-install)
5365
5366 (provide 'folding)
5367 (provide 'folding-isearch) ;; This used to be a separate package.
5368
5369 (run-hooks 'folding-load-hook)
5370
5371 ;;}}}
5372
5373 ;;; folding.el ends here