LoginSignup
1

More than 5 years have passed since last update.

EmacsLispでカレントバッファの更新を検知する

Posted at

概要

リアルタイムプレビューに使うバッファが更新確認の関数が欲しかったので作りました。

実装

更新があれば一時バッファに書き込んで一時バッファを更新することで、
カレントバッファが更新されているかどうか検知しています。
更新があればt、 更新がなければnilを返します。

(defun buffer-update-p ()
  (let ((current-buffer-string
         (buffer-substring-no-properties
          (point-min) (point-max))))
    (if (not
         (string=
          current-buffer-string
          (progn
            (set-buffer
             (get-buffer-create  (concat "*" (buffer-name) "*")))
            (buffer-substring-no-properties
             (point-min) (point-max)))))
        (progn
          (erase-buffer)
          (insert current-buffer-string)
          t)
      nil)))

使い方

以下のようにtimer関数の中で実行すると、前回実行してからバッファが更新されたときに処理をすることができます。

(setq hoge-timer 
      (run-at-time t 1 '(lambda ()
                          (if
                              (buffer-update-p)
                              (message "buffer update!!")))))

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1