我将一些东西保存在一个文件“XXX.gpg”中,我使用 emacs 自动加密模式读取和编辑该文件。
在某些情况下,我会长时间忘记关闭缓冲区,这是一种风险。
我想让缓冲区在一段时间(不活动)后自动保存和关闭。
您可以使用它run-with-idle-timer
来构造一个完全符合您要求的函数。
这就是我使用的:
(defun dwim-kill-gpg-buffers ()
(interactive)
(let ((buffers-killed 0))
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (string-match ".*\.gpg$" (buffer-name buffer))
(let ((current-time (second (current-time)))
(last-displayed-time (second buffer-display-time)))
(when (> (- current-time last-displayed-time)
60)
(message "Auto killing .gpg buffer '%s'" (buffer-name buffer))
(when (buffer-modified-p buffer)
(save-buffer))
(kill-buffer buffer)
(incf buffers-killed))))))
(unless (zerop buffers-killed)
(message "%s .gpg buffers have been autosaved and killed" buffers-killed))))
(run-with-idle-timer 60 t 'dwim-kill-gpg-buffers)
它尽量不关闭最近显示的缓冲区,因此只要有视图切换活动,缓冲区的终止就会延迟。
现在我在我的 ~/.emacs 中使用它:
(run-with-idle-timer 60 t (lambda ()
(let ((victim (get-buffer "XXX.gpg")))
(when victim (message "Killing buffer %s" (buffer-name victim)
(kill-buffer victim))))))