2011-02-15

highlight-parentheses.el を使ってみる

対応する括弧に色を付けてくれる
HighlightParentheses
http://nschum.de/src/emacs/highlight-parentheses/

設定はこんな感じに
(require 'highlight-parentheses)
(setq hl-paren-colors '("red" "blue" "yellow" "green" "magenta" "peru" "cyan"))
(set-face-attribute 'hl-paren-face nil :weight 'bold)
(add-hook 'emacs-lisp-mode-hook 'highlight-parentheses-mode)

結果はこんな風
(defun hl-paren-highlight ()
  "Highlight the parentheses around point."
  (unless (= (point) hl-paren-last-point)
    (setq hl-paren-last-point (point))
    (let ((overlays hl-paren-overlays)
          pos1 pos2
          (pos (point)))
      (save-excursion
        (condition-case err
            (while (and (setq pos1 (cadr (syntax-ppss pos1)))
                        (car overlays))
              (move-overlay (pop overlays) pos1 (1+ pos1))
              (when (setq pos2 (scan-sexps pos1 1))
                (move-overlay (pop overlays) (1- pos2) pos2)))
          (error nil)))
      (dolist (ov overlays)
        (move-overlay ov 1 1)))))


バージョン 2009-03-19 (1.0.1) の時点では、 hl-paren-colors に設定した最後の色が有効にならないので関数 hl-paren-highlight に以下のパッチを当てる。
--- highlight-parentheses.el.old
+++ highlight-parentheses.el
@@ -100,7 +100,7 @@
       (save-excursion
         (condition-case err
             (while (and (setq pos1 (cadr (syntax-ppss pos1)))
-                        (cddr overlays))
+                        (car overlays))
               (move-overlay (pop overlays) pos1 (1+ pos1))
               (when (setq pos2 (scan-sexps pos1 1))
                 (move-overlay (pop overlays) (1- pos2) pos2)