Folding Mode for Emacs

The folding editor seems to be installed on caucus. To call it up, add the following to your .emacs file:
;;{{{ folding stuff

         (load "folding" 'nomessage 'noerror)
         (folding-mode-add-find-file-hook)

 (fold-add-to-marks-list 'Prolog-mode "%{{{ " "%}}}" nil)
 (fold-add-to-marks-list 'prolog-mode "%{{{ " "%}}}" nil)
;;}}}
However, if you are using the new version of folding, you will need to say
 (folding-add-to-marks-list 'Prolog-mode "%{{{ " "%}}}" nil)
 (folding-add-to-marks-list 'prolog-mode "%{{{ " "%}}}" nil)
instead, since the internal variable names seem to have changed with the latest release.

The symbols ;;{{{ and ;;}}} are the begin and end fold marks for lisp mode. For LaTeX mode the markers are %{{{ and %}}}. Markers for other modes are listed in the code folding.el. If a desired emacs mode is not listed in folding.el, it can be added in the same way I added Prolog-mode above. (The percent sign is the comment symbol for prolog.)

Thus the entire folding structure is usually encoded as comments in your (otherwise unadulterated) text file. Only a duly empowered emacs can interpret the folds. A fold must have a name (folding stuff in the example above). The third mouse button folds or unfolds a fold, but the cursor itself (not just the mouse) must be on the opening fold symbol at the time - i.e., usually you end up doing two mouse clicks - button one to summon the renegade cursor and button three to fold or unfold. It takes some practice to resist hitting button 2 to dump a lot of garbage in the middle of your fold mark. Anything more complicated can be handled by the "fld" menu at the top of the screen.

If you can't access the fold menu or the mouse for whatever reason, you can add your own keyboard shortcuts to your .emacs file thus:

 (defun my-folding-bind-keys ()
            "My favourite folding keys"
          (define-key  folding-mode-map "\C-cf"
                      'folding-toggle-show-hide)
          (define-key  folding-mode-map "\C-co"
                      'folding-open-buffer)
          (define-key  folding-mode-map "\C-cc"
                      'folding-whole-buffer)
          )

 (setq folding-default-keys-function       'my-folding-bind-keys)
"\C-cf" means "control-c f" and so forth. If you are using an older version of folding.el and the above doesn't work, try the following instead:
 (defun my-fold-bind-keys ()
            "My favourite folding keys"
          (define-key  folding-mode-map "\C-cf"
                      'fold-toggle-show-hide)
          (define-key  folding-mode-map "\C-co"
                      'fold-open-buffer)
          (define-key  folding-mode-map "\C-cc"
                      'fold-whole-buffer)
          )
 
 (setq fold-default-keys-function       'my-fold-bind-keys)
You can test them by opening the file folding.el in emacs and trying to fold and unfold its folds.

Remarks: The underlying method of displaying and hiding folds is not optimal - in fact, it seems the folded material is really there hiding on one line titled

%{{{ your fold name here
I have backspaced into a folded fold accidentally, apparently erasing part of the end fold mark and making my fold inaccessible. Fortunately by blindly typing a few extra }}'s, the fold can be restored, and in any case the underlying file is unaffected. Another problem with this really big virtual line containing the fold is error stack overflow in regexp matcher (or a sudden crash of emacs) on large latex files - or should I say, large latex folds. This seems to happen when the highlighting is matching an end of line, so if you're not using highlighting this error shouldn't come up.

To use folding on a computer where it isn't installed, just download folding.el, put it somewhere safe in your account and add the following line to your .emacs, before the ones mentioned above:

(setq load-path (cons "/export/home/you/somewheresafe/folding.el" load-path))
where, of course, /export/home/you/somewheresafe/ must be altered according to your situation. For more information on folding and other emacs extensions, see Anders Lindgren's Emacs Page.
Return to the LaTeX page