forked from jaalto/project-emacs--folding-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
folding.texi
462 lines (355 loc) · 15.6 KB
/
folding.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
@c -*- mode: texinfo -*-
\input texinfo
@c %**start of header
@setfilename info/folding
@settitle folding
@documentencoding ISO-8859-1
@c %**end of header
@c This file was trivially adapted from emacs-goodies-el.texi
@c EDITION 30.2
@c UPDATED 25 Nov 2009
@c Copyright: 2000-2003 Roland Mas <[email protected]>,
@c 2005-2014 Peter S Galbraith <[email protected]>
@c 2014 Julian Gilbey <[email protected]>
@c License: GPL-2+
@dircategory Emacs
@direntry
* Folding: (folding). Folding editor minor mode for Emacs.
@end direntry
@node Top, folding Installation, (dir), (dir)
@noindent Author: Jamie Lokier <jamie@@imbolc.ucc.ie>
Jari Aalto <jari.aalto@@poboxes.com>
Anders Lindgren <andersl@@csd.uu.se>
@menu
* folding Installation::
* folding Documentation::
* folding Customization::
* folding Examples::
@end menu
This package provides a minor mode, compatible with all major
editing modes, for folding (hiding) parts of the edited text or
program.
Folding mode handles a document as a tree, where each branch
is bounded by special markers @{@{@{ and @}@}@}. A branch can be
placed inside another branch, creating a complete hierarchical
structure.
Folding mode can CLOSE a fold, leaving only the initial `@{@{@{'
and possibly a comment visible.
It can also ENTER a fold, which means that only the current
fold will be visible, all text above `@{@{@{' and below `@}@}@}'
will be invisible.
Please note, that the maintainers do not recommend to use only
folding for you your code layout and navigation. Folding.el is
on its best when it can "chunk" large sections of code inside
folds. The larger the chunks, the more the usability of
folding will increase. Folding.el is not meant to hide
individual functions: you may be better served by hideshow.el
or imenu.el (which can parse the function indexes)
@node folding Installation
The best way to use folding is to turn it on explicitely when
needed using @command{M-x folding-mode}.
But if you always use folding, then consider adding these lines
to your @file{~/.emacs} file:
@example
(if (load "folding" 'nomessage 'noerror)
(folding-mode-add-find-file-hook))
@end example
The same folding marks can be used in `vim' editor command
"set fdm=marker".
@noindent To remove folding, call `M-x' `folding-uninstall'.
@node folding Documentation, folding Customization, folding Installation, Top
@noindent Tutorial
To start folding mode, give the command: @command{M-x
folding-mode}. The mode line should contain the string "Fld"
indicating that folding mode is activated.
When loading a document containing fold marks, Folding mode is
automatically started and all folds are closed. For example when
loading my init file, only the following lines (plus a few lines of
comments) are visible:
@verbatim
;;{{{ General...
;;{{{ Keyboard...
;;{{{ Packages...
;;{{{ Major modes...
;;{{{ Minor modes...
;;{{{ Debug...
@end verbatim
To enter a fold, use @key{C-c @ >}. To show it without entering, use
@key{C-c @ C-s}, which produces this display:
@verbatim
;;{{{ Minor modes
;;{{{ Follow mode...
;;{{{ Font-lock mode...
;;{{{ Folding...
;;}}}
@end verbatim
To show everything, just as the file would look like if Folding
mode hadn't been activated, give the command @command{M-x
folding-open-buffer}, normally bound to @key{C-c @ C-o}. To
close all folds and go to the top level, the command
@command{folding-whole-buffer} could be used.
@noindent Mouse support
Folding mode v2.0 introduced mouse support. Folds can be shown or
hidden by simply clicking on a fold mark using mouse button 3. The
mouse routines have been designed to call the original function bound
to button 3 when the user didn't click on a fold mark.
@noindent The menu
A menu is placed in the "Tools" menu. Should no Tools menu exist
(Emacs 19.28) the menu will be placed in the menu bar.
@noindent ISearch
When searching using the incremental search (C-s) facilities,
folds will be automagically entered and closed.
@noindent Problems
Uneven fold marks
Oops, I just deleted some text, and a fold mark got deleted!
What should I do? Trust me, you will eventually do this sometime. the
easiest way is to open the buffer using @command{folding-open-buffer}
(@key{C-c @ C-o}) and add the fold mark by hand. To find mismatching
fold marks, the package `occur' is useful. The command:
@verbatim
M-x occur RET {{{\|}}} RET
@end verbatim
will extract all lines containing folding marks and present them
in a separate buffer.
Even though all folding marks are correct, Folding mode sometimes
gets confused, especially when entering and leaving folds very
often. To get it back on track, press @key{C-g} a few times and give the
command @command{folding-open-buffer} (@key{C-c @ C-o}).
Fold must have a label
When you make a fold, be sure to write some text for the name of
the fold, otherwise there may be an error "extraneous fold mark..."
Write like this:
@verbatim
;;{{{ Note
;;}}}
@end verbatim
instead of
@verbatim
;;{{{
;;}}}
@end verbatim
folding-whole-buffer doesn't fold whole buffer
If you call commands @command{folding-open-buffer} and
@command{folding-whole-buffer} and notice that there are open fold
sections in the buffer, then you have mismatch of folds somewhere. Run
@command{M-x occur} and type the regexp @code{@{@{@{\|@}@}@}} to check where
is the extra open or closing fold mark.
@noindent Folding and outline modes
Folding mode is not the same as Outline mode, a major and minor
mode which is part of the Emacs distribution. The two packages do,
however, resemble each other very much. The main differences between
the two packages are:
@itemize @bullet
@item
Folding mode uses explicit marks, @code{@{@{@{} and `@}@}@}', to
mark the beginning and the end of a branch.
Outline, on the other other hand, tries to use already
existing marks, like the `\section' string in a TeX
document.
@item
Outline mode has no end marker which means that it is
impossible for text to follow a sub-branch.
@item
Folding mode use the same markers for branches on all depths,
Outline mode requires that marks should be longer the
further, down in the tree you go, e.g `\chap', \section',
`\subsection', `\subsubsection'. This is needed to
distinguish the next mark at the current or higher levels
from a sub-branch, a problem caused by the lack of
end-markers.
@item
Folding mode has mouse support, you can navigate through a
folded document by clicking on fold marks. (The XEmacs version
of Outline mode has mouse support.)
@item
The Isearch facilities of Folding is capable of
automatically to open folds. Under Outline, the the entire
document must be opened prior isearch.
@end itemize
In conclusion, Outline mode is useful when the document being
edited contains natural markers, like LaTeX. When writing code natural
markers are hard to find, except if you're happy with one function per
fold (I'm not).
@noindent Personal reflections by Anders Lindgren
When writing this, version 2.0 of Folding mode is just about to
be released. The current version has proven itself stable during a
months of testing period. In other words: we haven't had time to touch
the folding for quite some time.
Our plan was from the beginning to rewrite the entire package,
including replacing the core of the program, written using old Emacs
technology (selective display), and replace it with modern
equivalences, like overlays or text-properties for Emacs and extents
for XEmacs.
Unfortunately, this has not yet been done, even though we have
implemented most other items on our to-do agenda.
It is not likely that any of us, even in the near future, will
find the time required to rewrite the core of the package. Since the
package, in it's current state, is much more powerful than the
original, we have decided to release this code.
@node folding Customization, folding Examples, folding Documentation, Top
@noindent Customization: general
The behavior of Folding mode is controlled mainly by a set of
Emacs Lisp variables. This section will discuss the most useful ones,
for more details please see the code. The descriptions below assumes
that you know a bit about how to use simple Emacs Lisp and knows how
to edit ~/.emacs, your init file.
@noindent Customization: hooks
The normal procedure when customizing a package is to write a
function doing the customization. The function is then added to a hook
which is called at an appropriate time. (Please see the example
section below.) The following hooks are available:
@table @samp
@item folding-mode-hook
Called when folding mode is activated.
@item <major mode>-folding-hook
Called when starting folding mode in a buffer with major
mode set to <major mode>. (e.g. When editing C code
the hook `c-mode-folding-hook' is called.)
@item folding-load-hook
Called when folding mode is loaded into Emacs.
@end table
@noindent Customization: The Mouse
The variable @code{folding-behave-table} contains the actions
which should be performed when the user clicks on an open fold, a
closed fold etc. For example, if you prefer to `enter' a fold rather
than `open' it you should rebind this variable.
The variable @code{folding-default-mouse-keys-function} contains
the name of the function used to bind your mouse keys. To use your own
mouse bindings, create a function, say
@command{my-folding-bind-mouse}, and set this variable to it.
@noindent Customization: Keymaps
When Emacs 19.29 was released, the keymap was divided into strict
parts. (This division existed before, but a lot of packages, even the
ones delivered with Emacs, ignored them.)
@verbatim
C-c <letter> -- Reserved for the users private keymap.
C-c C-<letter> -- Major mode. (Some other keys are
reserved as well.)
C-c <Punctuation Char> <Whatever>
-- Reserved for minor modes.
@end verbatim
The reason why @key{C-c@@} was chosen as the default prefix is
that it is used by outline-minor-mode. It is not likely that few
people will try to use folding and outline at the same time.
However, old key bindings have been kept if possible. The
variable @code{folding-default-keys-function} specifies which function
should be called to bind the keys. There are various function to
choose from how user can select the keybindings. To use the old key
bindings, add the following line to your init file:
@verbatim
(setq folding-default-keys-function
'folding-bind-backward-compatible-keys)
@end verbatim
To define keys similar to the keys used by Outline mode, use:
@verbatim
(setq folding-default-keys-function
'folding-bind-outline-compatible-keys)
@end verbatim
@noindent Customization: adding new major modes
To add fold marks for a new major mode, use the function
@command{folding-add-to-marks-list}. Example:
@verbatim
(folding-add-to-marks-list
'c-mode "/* {{{ " "/* }}} */" " */" t)
(folding-add-to-marks-list
'java-mode "// {{{ " "// }}}" nil t)
@end verbatim
@noindent Customization: ISearch
If you don't like the extension folding.el applies to isearch,
set the variable `folding-isearch-install' to nil before loading this
package.
@node folding Examples, , folding Customization, Top
@noindent Example: personal setup
To define your own key binding instead of using the standard
ones, you can do like this:
@verbatim
(setq folding-mode-prefix-key "\C-c")
;;
(setq folding-default-keys-function
'(folding-bind-backward-compatible-keys))
;;
(setq folding-load-hook 'my-folding-load-hook)
(defun my-folding-load-hook ()
"Folding setup."
(folding-install) ;; just to be sure
;; ............................................... markers ...
;; Change text-mode fold marks. Handy for quick
;; sh/perl/awk code
(defvar folding-mode-marks-alist nil)
(let* ((ptr (assq 'text-mode folding-mode-marks-alist)))
(setcdr ptr (list "# {{{" "# }}}")))
;; ........................................ bindings ...
;; Put `folding-whole-buffer' and `folding-open-buffer'
;; close together.
(defvar folding-mode-prefix-map nil)
(define-key folding-mode-prefix-map "\C-w" nil)
(define-key folding-mode-prefix-map "\C-s"
'folding-show-current-entry)
(define-key folding-mode-prefix-map "\C-p"
'folding-whole-buffer))
@end verbatim
@noindent Example: changing default fold marks
In case you're not happy with the default folding marks, you can
change them easily. Here is an example
@verbatim
(setq folding-load-hook 'my-folding-load-hook)
(defun my-folding-load-hook ()
"Folding vars setup."
(let* ((ptr (assq 'text-mode folding-mode-marks-alist)))
(setcdr ptr (list "# {{{" "# }}}"))))
@end verbatim
@noindent Example: choosing different fold marks for mode
Suppose you sometimes want to use different fold marks for the
major mode: e.g. to alternate between "# @{@{@{" and "@{@{@{" in `text-mode'
Call @command{M-x my-folding-text-mode-setup} to change the marks.
@verbatim
(defun my-folding-text-mode-setup (&optional use-custom-folding-marks)
(interactive
(list (y-or-n-p "Use Custom fold marks now? ")))
(let* ((ptr (assq major-mode folding-mode-marks-alist))
(default-begin "# {{{")
(default-end "# }}}")
(begin "{{{")
(end "}}}"))
(when (eq major-mode 'text-mode)
(unless use-custom-folding-marks
(setq begin default-begin end default-end)))
(setcdr ptr (list begin end))
(folding-set-marks begin end)))
@end verbatim
@noindent Example: AucTex setup
Suppose you're using comment.sty with AucTeX for editing LaTeX2e
documents and you have these comment types. You would like to be able
to set which of these 3 is to be folded at any one time, using a
simple key sequence: move back and forth easily between the different
comment types, e.g., "unfold everything then fold on \x".
@verbatim
\O ... \endO
\L ... \endL
\B ... \endB
(setq folding-load-hook 'my-folding-load-hook)
(defun my-folding-load-hook ()
"Folding vars setup."
(let ((ptr (assq 'text-mode folding-mode-marks-alist)))
(setcdr ptr (list "\\O" "\\endO"))
(define-key folding-mode-prefix-map "C"
'my-folding-marks-change)))
(defun my-folding-marks-change (&optional selection)
"Select folding marks: prefixes nil, C-u and C-u C-u."
(interactive "P")
(let ((ptr (assq major-mode folding-mode-marks-alist))
input)
(when (string-match "^\\(plain-\\|la\\|auc\\)?tex-"
(symbol-name major-mode))
(setq input
(read-string "Latex \\end(X) Marker (default O): "
nil nil "O" nil))
(setq input (upcase input))
(turn-off-folding-mode)
(folding-add-to-marks-list
major-mode
(concat "\\" input) (concat "\\end" input) nil nil t)
;; (setcdr ptr (list (concat "\\" input) (concat "\\end" input)))
(turn-on-folding-mode))))
;; End of example
@end verbatim