Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 12: Visible Editing Utilities

Previous HourNext Hour

Sections in this Hour:

 

Parentheses Matching


When you write programs, parentheses of different kinds often comes in pairs. In the rest of this section parentheses refers to all the different kinds of parentheses: <>, [], (), and {}. When writing a closing parenthesis, it is useful to be able to see which parenthesis is actually closed. Likewise, when reading the code later, it is very useful to see which parentheses actually match.

The following is three examples where parentheses come in pairs. The first is from Lisp, the second from C, and the third from TCL:

These three examples are merely trivial examples where only a few parentheses are needed. Look at sams-lib.el; you will see lots of parentheses.

There are two different situations where parentheses can be highlighted:

  • When inserting a closing parenthesis

  • When point is positioned at either a closing parenthesis or an opening parenthesis

In most cases you will not see the difference between the two, because the parenthesis will be matched by method two, just after it was inserted and shown by method one. If you dislike blinking parentheses when traveling over them, you can, however, disable this feature, and still have them highlighted when writing the closing parentheses.

Showing Parentheses when Moving over Them

There are two methods of showing parentheses in common between GNU Emacs and XEmacs, namely "Showing the matching parenthesis" and "showing all the text in between". In addition XEmacs has a third method, where the matching parenthesis blinks (that is, it blinks as long as point is located at its match).

In Figures 12.4 and 12.5, you can see the two different styles of showing the matching parenthesis.

Figure 12.4
Showing matching parenthesis.

Figure 12.5
Showing a region between parentheses.

The following five code listings show how to obtain a parentheses-showing method mentioned previously.

Showing matching parentheses in GNU Emacs:


(setq show-paren-mode t) 
(setq show-paren-style 'parenthesis) 

Showing region between parentheses in GNU Emacs:


(setq show-paren-mode t) 
(setq show-paren-style 'expression) 

Showing matching parentheses in XEmacs:


(paren-set-mode 'paren)

Showing region between parentheses in XEmacs:


(paren-set-mode 'sexp)

Blinking matching parentheses in XEmacs:


(paren-set-mode 'blink-paren)

Tip - If the matching parenthesis should be outside the visible part of the window, you can always jump to it using the following functions: forward-sexp or backward-sexp.


Besides showing the matching parentheses, Emacs also indicates to you when you have matched a couple of parentheses incorrectly. For example, if you close a brace with a parenthesis. This indication is done by using a different face (a different color and style). The face used can be customized using the customization library. In GNU Emacs, this is all included in the group called show-paren. In XEmacs, on the other hand, this must be customized separately from the other options concerning parentheses matching. This is done by pressing M-x and typing customize-face pressing return, and typing paren-match or paren-mismatch. The other options concerning parentheses showing is customized in the group called paren-matching.

Showing Parentheses when Writing the Closing Parenthesis

If you do not want the parentheses to be matched when traversing them, you can get a limited edition of parentheses highlighting, namely showing the opening parenthesis when typing the closing one. This is done by inserting the following line into your .emacs file:


(setq blink-matching-paren t)

Even though you have parentheses showing as described previously, you might want to enable this feature anyway, because it has the effect that the text around the opening parenthesis is shown in the minibuffer when writing the closing parenthesis and the opening one is outside the visible part of the buffer, as can be seen in Figure 12.6.

Figure 12.6
The opening parenthesis is outside the visible part of the buffer when closing it.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 12: Visible Editing Utilities

Previous HourNext Hour

Sections in this Hour: