FoldingMode

Last edit

Changed:

< * [http://git.savannah.gnu.org/cgit/emacs-tiny-tools.git/tree/lisp/other/folding.el?h=devel folding.el] '''The latest code'''. HEAD devel revision at Savannah TinyTools project. (Link appears dead 2014-04-03)

to

> * [https://raw.githubusercontent.com/jaalto/project-emacs--folding-mode/master/folding.el|folding.el] '''The latest code'''. HEAD devel revision at JariAalto's [https://github.com/jaalto/project-emacs--folding-mode|GitHub]


Description

Folding mode is a MinorMode that uses a special string to find the regions to hide: {{{ and }}}. The variable fold-mode-marks-alist defines the strings for each MajorMode separately. From the folding menu you can enter, show, hide, etc. regions. With the Mouse-3 (the rightmost mouse button) when there is no other context menu defined a right mouse click will toggle the visibility of fold.

Folding mode was originally written by Jamie Lokier and later extended by Jari Aalto and Anders Lindgren.

Download

The latest code is maintained by JariAalto. Report bugs to Savannah bug tracker.

Install

Add the following to your ~/.emacs InitFile and then every opened file will be searched for folding marks:

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

Folding mode is based on comments, and it uses the folding-top-mark and folding-bottom-mark to distinguish which region should be folded.

Example of How to Use it with Ruby

After adding those two lines above, also add:

  (folding-add-to-marks-list 'ruby-mode "#{{{" "#}}}" nil t)

Now say you have some ruby code you want to make foldable. Wrap it in #{{{ #}}} thus:

  # this code does blah
  #{{{
  def blah
    do some blah
    do some more blah
  end
  #}}}

Remember to M-x folding-mode <RET> to enable FoldingMode. Now, anywhere within those boundaries you can do C-c @ C-q to toggle it open and closed.

Caveats

Always make sure that you have a closing fold mark for every opening mark! Otherwise the opening of files might be difficult. Using M-x occur to search for opening and closing marks is a good idea for this.

To set own folding marks for a given mode, study the following example.

  (folding-add-to-marks-list 'php-mode                "//{"  "//}"  nil t)
  (folding-add-to-marks-list 'prolog-mode             "%{{{" "%}}}" nil t)
  (folding-add-to-marks-list 'html-mode               "<!-- {{{ " "<!-- }}} -->" " -->" nil t)

Note: In plain text mode the symbol for open fold is “{{{ “ (that is, with a space at the end).

An example - Folding Mode for C Code

While editing C programs, folding-mode use ‘/* {{{ ‘ and ‘/* }}}’ as the top and bottom mark. And it is easy for you to use the interactive function fold-fold-region to fold a region marked by Emacs set-mark. If you want a many level folded file, you have to do nothing but to fold a region in a folded region like this:

     /* {{{ This is the parent level */
     main()
     {
	/* {{{ This is the first child level (var initialized) */
	int i,j,k;
	float l,m,n;
	char a,b,c;
	/* }}} child level ended */
	/* {{{ Here is the second child level (function) */
	/* {{{ grand-son level (function used to get character) */
	char getChar()
	{
	    ...
	}
	/* }}} grand-son level ended */
	int getInt()
	{
	    ...
	}
	float getFloat()
	{
	    ...
	}
	/* }}} second child level ended */
	/* and so on */
	 ...
	/* }}} */
    }

After folding this buffer in Emacs, it looks like that:

  /* {{{ This is the parent level ...

When you use enter this level, you will see:

  /* {{{ This is the first child level ...
  /* {{{ Here is the second child level ...
  /* {{{ and so on ...

Note: Actually, hs-minor-mode which comes with Emacs CVS (as of May 25th 2003) does work like this, hiding only the first containing block. I guess it also works correctly with “older” Emacs 21.2 and 21.3.

Ideas

Michael Grasso suggested `C-u 2 C-x $' (`C-x $' to clear), see Groups:folding&selm=199312202324.AA16992@macchi.lochard.com.au

See also HideShow.

If you want to see how you can use FoldingMode to organize your file you might consider Lisp:eukleides.el and Lisp:moikrug.el as examples.

Notes

Both 2.107 and the older 2.99 appear to work perfectly with my GNU Emacs CVS. – TimAnderson

The folding.el (version: 2.117+) from the XEmacs CVS does not appear to be working on my GNU Emacs 22.0.55.1 – RudiGrinberg?

I found folding-mode is not compatiable with GNU Emacs 22.0.50.1. The newlines of file save with folding-mode often replace with ^M and this will lead to many problems for some files, such as elisp. fold.el works in my emacs. I recommend use it in GNU Emacs. Download: http://uuid.com/~marcus/computing/fold-mode.html. I add some command in Lisp:fold.el – Ye_Wenbin

folding.el (version 2006.1118), folding-2.107.el, and folding-2.99 all work on plain text in GNU Emacs (GNU Emacs 22.1.50.1 (powerpc-apple-darwin8.11.0, Carbon Version 1.6.0) of 2008-01-17 on cf-ppc-macosx.usr.sourceforge.jp). However all have unpleasant interaction with narrowing and widening in LaTeX.

Is there a way to change the face of the folding indicator (i.e. the “…”)? I would also like to change that folding indicator text if possible…

And while I’m at it, any way of not narrowing every time I enter a fold?

(setq folding-narrow-by-default nil)

yafolding.el - Yet another folding extension for the Emacs editor


CategoryHideStuff CategoryOutline