メールを書くときに jit-lock を使う

普通 wl でメールを書いてるときは、C-l しないと色が付かない。これはちょっと面倒だとずっと思ってた。

んで、さっき jit-lock に jit-lock-register って関数があることを知った。この関数がすることは:

  • そのバッファで jit-lock で色付けする関数を登録する(font-lock-fontify-region は使わない)。
  • そのバッファを jit-lock-mode にする。

これは使えそう!という事でメールを書くときにこれを使うようにしてみた。

;;; Wanderlust の draft-mode で jit-lock を使う
(defun wl-draft-inside-header-p (pos)
  "POS の位置がヘッダ内かを返す。"
  (save-excursion
    (goto-char pos)
    (re-search-forward 
     (concat "^" (regexp-quote mail-header-separator) "$")
     nil t)))

(defun wl-draft-highlight-region (start end)
  "START と END の範囲に色を付ける。
ただし、表示されていない箇所には色を付けない。"
  (interactive "r")
  (let ((modified (buffer-modified-p))
	(goto-next-invisible-change
	 (lambda (end)
	   (goto-char (or (next-single-property-change
			   (point) 'invisible nil end)
			  end)))))
    (unwind-protect
	(save-excursion
	  (setq start
		(progn (goto-char start) (line-beginning-position)))
	  (setq end
		(progn (goto-char end) (forward-line) (point)))
	  (goto-char start)
	  (when (get-text-property (point) 'invisible)
	    (funcall goto-next-invisible-change end))
	  (while (< (point) end)
	    (let ((sub-start (point))
		  (sub-end (funcall goto-next-invisible-change end)))
	      (put-text-property sub-start sub-end 'face nil)
	      (wl-highlight-message sub-start sub-end t
				    (not (wl-draft-inside-header-p sub-start)))
	      (funcall goto-next-invisible-change end))))
      (set-buffer-modified-p modified))))

(defun wl-draft-highlight-buffer ()
  (interactive)
  (wl-draft-highlight-region (point-min) (point-max)))

;; jit-lock で色を付ける
(add-hook 'wl-draft-mode-hook
	  (lambda ()
	    (jit-lock-register 'wl-draft-highlight-region)))
  • [2007-02-23]: wl-highlight-body-too を nil にすると preview とかで色をつけてくれないのでやめた。
  • [2007-02-17]: 非表示の部分は色を付けないようにした。