balancing your parentheses

One of the biggests problems in the world today are unbalanced parentheses - i.e., an opening '(' without a corresponding ')'. Especially the closing ones are often forgotten, and not even the invention of :-) smileys has restored the balance. Fortunately, as with most problems, emacs has a solution: show-paren-mode.

show-paren-mode will visually indicate corresponding parentheses, so you are much less likely to forget them. Note that 'parentheses' are not just '()', but also '[]', '{}'; and others (for example, in html-mode, '<>' are also considered to be parentheses). Also note that 'parentheses' is the plural of 'parenthesis'.

Even without show-paren-mode, emacs gives small visual cues by temporarily moving the cursor to the opening parenthesis when it is positioned just after the closing one. What show-paren-mode does is to improve upon that, and decide exactly how that happens. This is best explained using an example; to put in your .emacs:

(setq show-paren-delay 0)           ; how long to wait?
(show-paren-mode t) ; turn paren-mode on
(setq show-paren-style 'expression) ; alternatives are 'parenthesis' and 'mixed'

This will enable show-paren-mode globally. The show-paren-delay determines how long to wait before any visual cues; if you set it to, say, 1, you won't see anything when you're quickly scrolling, but only after you're 'standing still' for a second.

Then, show-paren-style:


  • if you set it to 'expression, the whole expression will be highlighted - when your cursor is just before the 'world', you'll get:
    (hello)world

  • if you set it to 'parenthesis, only the corresponding parenthesis will be highlighted:
    (hello)world

  • finally, if you set it to 'mixed, it will behave like 'parenthesis when the matching parenthesis is visible, and like 'expression otherwise.

Now, of course it does not stop here. You can determine exactly what color, boldness, underline and other properties are used, by setting the faces that show-paren-mode uses. For example, if we add:
(set-face-background 'show-paren-match-face "#aaaaaa")
(set-face-attribute 'show-paren-match-face nil
:weight 'bold :underline nil :overline nil :slant 'normal)

This will give the whole expression a grayish background (the "#aaaaaa"), and makes it bold. There is lots of room for experimentation here, obviously. Note that show-paren-mode can also detect mismatches, e.g. when try to close a '(' with a ']'. You can tune this with show-paren-mismatch-face, e.g.:
(set-face-foreground 'show-paren-mismatch-face "red") 
(set-face-attribute 'show-paren-mismatch-face nil
:weight 'bold :underline t :overline nil :slant 'normal)

No comments:

Post a Comment

Followers

Popular Posts