Debian Codegraph @

folding.el

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