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:

Starting with the Showing and Hiding Functions

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

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: