Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 15: Getting an Overview of a File |  | Previous HourNext Hour
Sections in this Hour: |  |

## Folding Text: A Sort of Categorized Outline

In the section "Configuring Outline," you saw an example of how to set up a regular expression for using outline mode with the programming language C. (If you don't recall this, it might be because it was in the section I offered for you to skip.)

The main drawback with this solution is that you are incapable of ordering the outline that is dictated by the indentation of the text. What you might want to do is to group a number of functions into one group which you could show or hide simultaneously. You might in fact want this grouping at any level (for example, some statements of a function might be grouped).

For this purpose the library called folding exists. It is not part of the Emacs installation, but it is located on the CD for this book (see Appendix A, "Installing Functions and Packages from the CD," for an installation guide). In Figure 15.7 you can see a buffer that has been folded.

Figure 15.7
A folded buffer.

Special markers in the text are required for folding to work. Each fold must start with a special mark and end in a special mark. This special mark depends on the major mode for the given buffer. This ensures that folding marks never interfere with the text of the buffer. After the opening mark comes a description of the fold. This is for your eyes only.

Don't get scared that you now have to learn a set of folding marks for each of your major modes. The library contains an menu item in which an entry called foldify-region exists. This function puts fold marks around a region for you. If there isn't support for a given major mode you can add it by using the function fold-add-to-marks-list.

In Figure 15.7, the special folding opening mark is ;;{{{ (two semicolons and three opening braces). As you can see in the mode-line, this buffer's major mode is Emacs-Lisp, which in fact uses the semicolon as comment characters. Thus to the Lisp interpreter that reads this file, the folding marks are merely comments.

Folds can nest; thus, it is possible to have subfolds within folds, like is the case in outline mode. Furthermore, as folds have explicit marks to end a fold, you can have text in a given fold after a subfold. This is impossible in outline. Think about how you would make some text part of a chapter, after a section in a book.

### Folding Commands

When folding mode is started, an item called Fld is inserted into the menus, and the buffer is automatically folded, so you get an overview of it.

Tip - To start playing with folding mode right away, load the library with the following command: M-x load-file RET The folding.el file Next read the file folding.el into Emacs (this file is written in folding mode), and start folding mode with this command: M-x folding-mode RET

Folding mode works much like outline mode. There are two major ways in which it is different:

- It does not contain as many functions for showing hiding text as outline mode does.

- It works together with narrowing that makes it possible to enter a fold , which means that only the text of this fold is shown.

### Starting with the Showing and Hiding Functions

Folding mode contains the following functions for showing and hiding text:

- C-c @ C-w (fold-whole-buffer) and C-c @ C-o (fold-open-buffer)--These two functions fold the whole buffer (that is, hide the body of every fold, and show only the header of the topmost folds), and show the whole buffer respectively.

- C-c @ C-x (fold-hide) and C-c @ C-s (fold-show)--These are the functions that in the menus are called hide level and show level. These functions show or hide a given fold. When a fold is shown, its body text is shown and the heading lines of the subfolds are shown, but not their body lines or their subfolds.

- C-c @ C-y (fold-show-subtree)--This function shows all the content of a fold: That is, its body text and all its subfolds.

As previously mentioned, folding mode makes it possible to enter a fold, which means that the buffer is narrowed to the given fold. The body text of the fold is shown, but the content of its subfolds are hidden. You can furthermore enter subfolds, which narrow further down to show only the subfold. When you exit a subfold, the buffer is widened to show the fold in which the subfold was located or to show the whole buffer if the subfold is located at the outermost level.

Folds are entered with C-c @ > (fold-enter) and exited with C-c @ < (fold-exit). If you have entered subfolds within a given fold, all folds can be exited by pressing C-c @ C-t (fold-top-level).

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 15: Getting an Overview of a File |  | Previous HourNext Hour
Sections in this Hour: |  |

© Copyright Macmillan USA. All rights reserved.