unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 0d7a7a88a6fb0810f8970127c6bd066e2fdb965c 20202 bytes (raw)
name: lisp/foldout.el 	 # note: path name is non-authoritative(*)

  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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
 
;;; foldout.el --- folding extensions for outline-mode and outline-minor-mode

;; Copyright (C) 1994, 2001-2020 Free Software Foundation, Inc.

;; Author: Kevin Broadey <KevinB@bartley.demon.co.uk>
;; Maintainer: emacs-devel@gnu.org
;; Created: 27 Jan 1994
;; Version: 1.10
;; Keywords: folding, outlines

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This file provides folding editor extensions for outline-mode and
;; outline-minor-mode buffers.  What's a "folding editor"?  Read on...
;;
;; Imagine you're in an outline-mode buffer and you've hidden all the text and
;; subheadings under your level-1 headings.  You now want to look at the stuff
;; hidden under one of these headings.  Normally you'd do C-c C-e (show-entry)
;; to expose the body or C-c C-i to expose the child (level-2) headings.
;;
;; With foldout, you do C-c C-z (foldout-zoom-subtree).  This exposes the body
;; and child subheadings and narrows the buffer so that only the level-1
;; heading, the body and the level-2 headings are visible.  If you now want to
;; look under one of the level-2 headings, position the cursor on it and do C-c
;; C-z again.  This exposes the level-2 body and its level-3 child subheadings
;; and narrows the buffer again.  You can keep on zooming in on successive
;; subheadings as much as you like.  A string in the mode line tells you how
;; deep you've gone.
;;
;; When zooming in on a heading you might only want to see the child
;; subheadings.  You do this by specifying a numeric argument: C-u C-c C-z.
;; You can specify the number of levels of children too (c.f. show-children):
;; e.g. M-2 C-c C-z exposes two levels of child subheadings.  Alternatively,
;; you might only be interested in the body.  You do this by specifying a
;; negative argument: M-- C-c C-z.  You can also cause the whole subtree to be
;; expanded, similar to C-c C-s (show-subtree), by specifying a zero argument:
;; M-0 C-c C-z.
;;
;; While you're zoomed in you can still use outline-mode's exposure and hiding
;; functions.  It won't upset foldout at all.  Also, since the buffer is
;; narrowed, "global" editing actions will only affect the stuff under the
;; zoomed-in heading.  This is useful for restricting changes to a particular
;; chapter or section of your document.
;;
;; You unzoom (exit) a fold by doing C-c C-x (foldout-exit-fold).  This hides
;; all the text and subheadings under the top-level heading and returns you to
;; the previous view of the buffer.  Specifying a numeric argument exits that
;; many folds.  Specifying a zero argument exits *all* folds.
;;
;; You might want to exit a fold *without* hiding the text and subheadings.
;; You do this by specifying a negative argument.  For example, M--2 C-c C-x
;; exits two folds and leaves the text and subheadings exposed.
;;
;; Foldout also provides mouse bindings for entering and exiting folds and for
;; showing and hiding text.  Hold down Meta and Control, then click a mouse
;; button as follows:-
;;
;;   mouse-1 (foldout-mouse-zoom) zooms in on the heading clicked on:-
;;
;;     single click	expose body
;;     double click	expose subheadings
;;     triple click	expose body and subheadings
;;     quad click	expose entire subtree
;;
;;   mouse-2 (foldout-mouse-show) exposes text under the heading clicked on:-
;;
;;     single click	expose body
;;     double click	expose subheadings
;;     triple click	expose body and subheadings
;;     quad click	expose entire subtree
;;
;;   mouse-3 (foldout-mouse-hide-or-exit) hides text under the heading clicked
;;   on or exits the fold:-
;;
;;     single click	hide subtree
;;     double click	exit fold and hide text
;;     triple click	exit fold without hiding text
;;     quad click	exit all folds and hide text
;;
;; You can change the modifier keys used by setting `foldout-mouse-modifiers'.

;;; Installation:

;; To use foldout, put this in your .emacs:-
;;
;;    (require 'foldout)
;;
;; If you don't want it loaded until you need it, try this instead:-
;;
;;    (eval-after-load "outline" '(require 'foldout))

;;; Advertisements:

;; Get out-xtra.el by Per Abrahamsen <abraham@iesd.auc.dk> for more
;; outline-mode goodies.  In particular, `outline-hide-sublevels' makes
;; setup a lot easier.
;;
;; folding.el by Jamie Lokier <u90jl@ecs.ox.ac.uk> supports folding by
;; recognizing special marker text in you file.
;;
;; c-outline.el (by me) provides outline-mode support to recognize `C'
;; statements as outline headings, so with foldout you can have a folding `C'
;; code editor without having to put in start- and end-of-fold markers.  This
;; is a real winner!

;;; ChangeLog:

;; 1.10   21-Mar-94
;; foldout.el is now part of the GNU Emacs distribution!!
;; Put in changes made by RMS to version 1.8 to keep the diffs to a minimum.
;; bugfix: numeric arg to foldout-exit-fold wasn't working - looks like I don't
;; know how to use the Common LISP `loop' macro after all, so use `while'
;; instead.

;; 1.9    15-Mar-94
;; Didn't test that very well, did I?  The change to foldout-zoom-subtree
;; affected foldout-mouse-zoom: if the heading under the `level n' one clicked
;; on was at `level n+2' then it didn't get exposed.  Sorry about that!

;; 1.8	  15-Mar-94
;; Changed meaning of prefix arg to foldout-zoom-subtree.  arg > 0 now means
;; "expose that many children" instead of just "expose children" so it is more
;; like `show-children' (C-c C-i).  Arg of C-u on its own only shows one level
;; of children, though, so you can still zoom by doing C-u C-c C-z.
;;
;; I can't think of a good meaning for the value of a negative prefix.  Any
;; suggestions?
;;
;; Added advertisement for my c-outline.el package.  Now you can have a folding
;; editor for c-mode without any effort!

;; 1.7	  7-Mar-94
;; I got fed up trying to work out how many blank lines there were outside the
;; narrowed region when inside a fold.  Now *all* newlines before the following
;; heading are *in* the narrowed region.  Thus, if the cursor is at point-max,
;; the number of blank lines above it is the number you'll get above the next
;; heading.
;;
;; Since all newlines are now inside the narrowed region, when exiting a fold
;; add a newline at the end of the region if there isn't one so that the
;; following heading doesn't accidentally get joined to the body text.
;;
;; Bugfix: `foldout-mouse-modifiers' should be `defvar', not `defconst'.
;;
;; Use "cond" instead of "case" so that lemacs-19.9 users can use the mouse.
;;
;; Improve "Commentary" entry on using the mouse.
;;
;; Add "Installation" keyword.

;; 1.6	  3-Mar-94
;; Add mouse support functions foldout-mouse-zoom, foldout-mouse-show,
;; foldout-mouse-hide-or-exit.

;; 1.5	  11-Feb-94
;; Rename `foldout-enter-subtree' to `foldout-zoom-subtree' and change
;; keystroke from C-g to C-z.  This is more mnemonic and leaves C-g alone, as
;; users expect this to cancel the current key sequence.
;;
;; Added better commentary at the request of RMS.  Added stuff to comply with
;; the lisp-mnt.el conventions.  Added instructions on how best to load the
;; package.

;; 1.4	  2-Feb-94
;; Bugfix: end-of-fold marking was wrong:-
;;
;;   End of narrowed region should be one character on from
;;   (outline-end-of-subtree) so it includes the end-of-line at the end of the
;;   last line of the subtree.
;;
;;   End-of-fold marker should be outside the narrowed region so text inserted
;;   at the end of the region goes before the marker.  Need to make a special
;;   case for end-of-buffer because it is impossible to set a marker that will
;;   follow eob.  Bummer.

;; 1.3	  28-Jan-94
;; Changed `foldout-zoom-subtree'.  A zero arg now makes it expose the entire
;; subtree on entering the fold.  As before, < 0 shows only the body and > 0
;; shows only the subheadings.

;; 1.2	  28-Jan-94
;; Fixed a dumb bug - didn't make `foldout-mode-line-string' buffer-local :-(
;;
;; Changed `foldout-exit-fold' to use prefix arg to say how many folds to exit.
;; Negative arg means exit but don't hide text.  Zero arg means exit all folds.
;;
;; Added `foldout-inhibit-key-bindings' to inhibit key bindings.

;; 1.1	  27-Jan-94
;; Released to the net.  Inspired by a question in gnu.emacs.help from
;; Jason D Lohn <jlohn@eng.umd.edu>.

;;; Code:

(require 'outline)

(defvar foldout-fold-list nil
  "List of start and end markers for the folds currently entered.
An end marker of nil means the fold ends after (point-max).")
(make-variable-buffer-local 'foldout-fold-list)

(defvar foldout-mode-line-string nil
  "Mode line string announcing that we are in an outline fold.")
(make-variable-buffer-local 'foldout-mode-line-string)

;; put our minor mode string immediately following outline-minor-mode's
(or (assq 'foldout-mode-line-string minor-mode-alist)
    (let ((outl-entry (memq (assq 'outline-minor-mode minor-mode-alist)
			    minor-mode-alist))
	  (foldout-entry '((foldout-mode-line-string foldout-mode-line-string))))

      ;; something's wrong with outline if we can't find it
      (if (null outl-entry)
	  (error "Can't find outline-minor-mode in minor-mode-alist"))

      ;; slip our fold announcement into the list
      (setcdr outl-entry (nconc foldout-entry (cdr outl-entry)))
      ))

;; outline-flag-region has different `flag' values in outline.el and
;; noutline.el for hiding and showing text.

(defconst foldout-hide-flag
  (if (featurep 'noutline) t ?\^M))

(defconst foldout-show-flag
  (if (featurep 'noutline) nil ?\n))
\f

(defun foldout-zoom-subtree (&optional exposure)
  "Open the subtree under the current heading and narrow to it.

Normally the body and the immediate subheadings are exposed, but
optional arg EXPOSURE \(interactively with prefix arg) changes this:-

	EXPOSURE > 0	exposes n levels of subheadings (c.f. show-children)
	EXPOSURE < 0	exposes only the body
	EXPOSURE = 0	exposes the entire subtree"
  (interactive "P")
  (save-excursion
    (widen)
    (outline-back-to-heading)
    (let* ((exposure-value (prefix-numeric-value exposure))
	   (start (point))
	   (start-marker (point-marker))
	   (end (progn (outline-end-of-subtree)
		       (skip-chars-forward "\n\^M")
		       (point)))
	   ;; I need a marker that will follow the end of the region even when
	   ;; text is inserted right at the end.  Text gets inserted *after*
	   ;; markers, so I need it at end+1.  Unfortunately I can't set a
	   ;; marker at (point-max)+1, so I use nil to mean the region ends at
	   ;; (point-max).
	   (end-marker (if (eobp) nil (set-marker (make-marker) (1+ end))))
	   )

      ;; narrow to this subtree
      (narrow-to-region start end)

      ;; show the body and/or subheadings for this heading
      (goto-char start)
      (cond
       ((null exposure)
	(outline-show-entry)
	(outline-show-children))
       ((< exposure-value 0)
	(outline-show-entry))
       ((consp exposure)
	(outline-show-children))
       ((> exposure-value 0)
	(outline-show-children exposure-value))
       (t
	(outline-show-subtree))
       )

      ;; save the location of the fold we are entering
      (setq foldout-fold-list (cons (cons start-marker end-marker)
				    foldout-fold-list))

      ;; update the mode line
      (foldout-update-mode-line)
      )))
\f

(defun foldout-exit-fold (&optional num-folds)
  "Return to the ARG'th enclosing fold view.  With ARG = 0 exit all folds.

Normally causes exited folds to be hidden, but with ARG < 0, -ARG folds are
exited and text is left visible."
  (interactive "p")
  (let ((hide-fold t) start-marker end-marker
	beginning-of-heading end-of-subtree)

    ;; check there are some folds to leave
    (if (null foldout-fold-list)
	(error "Not in a fold!"))

    (cond
     ;; catch a request to leave all folds
     ((zerop num-folds)
      (setq num-folds (length foldout-fold-list)))

     ;; have we been told not to hide the fold?
     ((< num-folds 0)
      (setq hide-fold nil
	    num-folds (- num-folds)))
     )

    ;; limit the number of folds if we've been told to exit too many
    (setq num-folds (min num-folds (length foldout-fold-list)))

    ;; exit the folds
    (widen)
    (while (not (zerop num-folds))
      ;; get the fold at the top of the stack
      (setq start-marker (car (car foldout-fold-list))
	    end-marker (cdr (car foldout-fold-list))
	    foldout-fold-list (cdr foldout-fold-list)
	    num-folds (1- num-folds))

      ;; Make sure there is a newline at the end of this fold,
      ;; otherwise the following heading will get joined to the body
      ;; text.
      (if end-marker
	  (progn
	    (goto-char end-marker)
	    (forward-char -1)
	    (or (memq (preceding-char) '(?\n ?\^M))
		(insert ?\n))))

      ;; If this is the last fold to exit, hide the text unless we've
      ;; been told not to.  Note that at the moment point is at the
      ;; beginning of the following heading if there is one.

      ;; Also, make sure that the newline before the following heading
      ;; is \n otherwise it will be hidden.  If there is a newline
      ;; before this one, make it visible too so we do the same as
      ;; outline.el and leave a blank line before the heading.
      (when (zerop num-folds)
	(if end-marker
	    (setq beginning-of-heading (point)
		  end-of-subtree (progn (forward-char -1)
					(if (memq (preceding-char)
						  '(?\n ?\^M))
					    (forward-char -1))
					(point))))
	;; hide the subtree
	(when hide-fold
	  (goto-char start-marker)
	  (outline-hide-subtree))

	;; make sure the next heading is exposed
	(if end-marker
	    (outline-flag-region end-of-subtree beginning-of-heading
				 foldout-show-flag)))

      ;; zap the markers so they don't slow down editing
      (set-marker start-marker nil)
      (if end-marker (set-marker end-marker nil)))

    ;; narrow to the enclosing fold if there is one
    (if foldout-fold-list
	(progn
	  (setq start-marker (car (car foldout-fold-list))
		end-marker (cdr (car foldout-fold-list)))
	  (narrow-to-region start-marker
			    (if end-marker
				(1- (marker-position end-marker))
			      (point-max)))))
    (recenter)

    ;; update the mode line
    (foldout-update-mode-line)))
\f

(defun foldout-update-mode-line ()
  "Set the mode line to indicate our fold depth."
  (let ((depth (length foldout-fold-list)))
    (setq foldout-mode-line-string
	  (cond
	   ;; if we're not in a fold, keep quiet
	   ((zerop depth)
	    nil)
	   ;; in outline-minor-mode we're after "Outl:xx" in the mode line
	   (outline-minor-mode
	    (format ":%d" depth))
	   ;; otherwise just announce the depth (I guess we're in outline-mode)
	   ((= depth 1)
	    " Inside 1 fold")
	   (t
	    (format " Inside %d folds" depth))))))
\f

(defun foldout-mouse-zoom (event)
  "Zoom in on the heading clicked on.

How much is exposed by the zoom depends on the number of mouse clicks:-

	1	expose body
	2	expose subheadings
	3	expose body and subheadings
	4	expose entire subtree"
  (interactive "@e")

  ;; swallow intervening mouse events so we only get the final click-count.
  (setq event (foldout-mouse-swallow-events event))

  ;; go to the heading clicked on
  (foldout-mouse-goto-heading event)

  ;; zoom away
  (foldout-zoom-subtree
   (let ((nclicks (event-click-count event)))
     (cond
      ((= nclicks 1) -1)		; body only
      ((= nclicks 2) '(1))		; subheadings only
      ((= nclicks 3) nil)		; body and subheadings
      (t 0)))))				; entire subtree

(defun foldout-mouse-show (event)
  "Show what is hidden under the heading clicked on.

What gets exposed depends on the number of mouse clicks:-

	1	expose body
	2	expose subheadings
	3	expose body and subheadings
	4	expose entire subtree"
  (interactive "@e")

  ;; swallow intervening mouse events so we only get the final click-count.
  (setq event (foldout-mouse-swallow-events event))

  ;; expose the text
  (foldout-mouse-goto-heading event)
  (let ((nclicks (event-click-count event)))
    (cond
     ((= nclicks 1) (outline-show-entry))
     ((= nclicks 2) (outline-show-children))
     ((= nclicks 3) (outline-show-entry) (outline-show-children))
     (t (outline-show-subtree)))))

(defun foldout-mouse-hide-or-exit (event)
  "Hide the subtree under the heading clicked on, or exit a fold.

What happens depends on the number of mouse clicks:-

	1	hide subtree
	2	exit fold and hide text
	3	exit fold without hiding text
	4	exit all folds and hide text"
  (interactive "@e")

  ;; swallow intervening mouse events so we only get the final click-count.
  (setq event (foldout-mouse-swallow-events event))

  ;; hide or exit
  (let ((nclicks (event-click-count event)))
    (if (= nclicks 1)
	(progn
	  (foldout-mouse-goto-heading event)
	  (outline-hide-subtree))
      (foldout-exit-fold
       (cond
	((= nclicks 2) 1)		; exit and hide
	((= nclicks 3) -1)		; exit don't hide
	(t 0))))))			; exit all
\f

(defun foldout-mouse-swallow-events (event)
  "Swallow intervening mouse events so we only get the final click-count.
Signal an error if the final event isn't the same type as the first one."
  (let ((initial-event-type (event-basic-type event)))
    (while (null (sit-for (/ double-click-time 1000.0) 'nodisplay))
      (setq event (read-event)))
    (or (eq initial-event-type (event-basic-type event))
	(error "")))
  event)

(defun foldout-mouse-goto-heading (event)
  "Go to the heading where the mouse event started.  Signal an error
if the event didn't occur on a heading."
  (goto-char (posn-point (event-start event)))
  (or (outline-on-heading-p)
      ;; outline.el sometimes treats beginning-of-buffer as a heading
      ;; even though outline-on-heading returns nil.
      (save-excursion (beginning-of-line) (bobp))
      (error "Not a heading line")))
\f

;;; Keymaps:

(defvar foldout-inhibit-key-bindings nil
  "Set non-nil before loading foldout to inhibit key bindings.")

(defvar foldout-mouse-modifiers '(meta control)
  "List of modifier keys to apply to foldout's mouse events.

The default (meta control) makes foldout bind its functions to
M-C-down-mouse-{1,2,3}.

Valid modifiers are shift, control, meta, alt, hyper and super.")

(if foldout-inhibit-key-bindings
    ()
  (define-key outline-mode-map "\C-c\C-z" 'foldout-zoom-subtree)
  (define-key outline-mode-map "\C-c\C-x" 'foldout-exit-fold)
  (let ((map (lookup-key outline-minor-mode-map outline-minor-mode-prefix)))
    (unless map
      (setq map (make-sparse-keymap))
      (define-key outline-minor-mode-map outline-minor-mode-prefix map))
    (define-key map "\C-z" 'foldout-zoom-subtree)
    (define-key map "\C-x" 'foldout-exit-fold))
  (let* ((modifiers (apply 'concat
			   (mapcar (function
				    (lambda (modifier)
				      (vector
				       (cond
					 ((eq modifier 'shift) ?S)
					 ((eq modifier 'control) ?C)
					 ((eq modifier 'meta) ?M)
					 ((eq modifier 'alt) ?A)
					 ((eq modifier 'hyper) ?H)
					 ((eq modifier 'super) ?s)
					 (t (error "invalid mouse modifier %s"
						   modifier)))
				       ?-)))
				   foldout-mouse-modifiers)))
	 (mouse-1 (vector (intern (concat modifiers "down-mouse-1"))))
	 (mouse-2 (vector (intern (concat modifiers "down-mouse-2"))))
	 (mouse-3 (vector (intern (concat modifiers "down-mouse-3")))))

    (define-key outline-mode-map mouse-1 'foldout-mouse-zoom)
    (define-key outline-mode-map mouse-2 'foldout-mouse-show)
    (define-key outline-mode-map mouse-3 'foldout-mouse-hide-or-exit)

    (define-key outline-minor-mode-map mouse-1 'foldout-mouse-zoom)
    (define-key outline-minor-mode-map mouse-2 'foldout-mouse-show)
    (define-key outline-minor-mode-map mouse-3 'foldout-mouse-hide-or-exit)
    ))

(provide 'foldout)

;;; foldout.el ends here

debug log:

solving 0d7a7a88a6 ...
found 0d7a7a88a6 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).