3

我将一些东西保存在一个文件“XXX.gpg”中,我使用 emacs 自动加密模式读取和编辑该文件。

在某些情况下,我会长时间忘记关闭缓冲区,这是一种风险。

我想让缓冲区在一段时间(不活动)后自动保存和关闭。

4

3 回答 3

2

您可以使用它run-with-idle-timer来构造一个完全符合您要求的函数。

于 2013-03-07T20:07:27.940 回答
1

这就是我使用的:

(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)

它尽量不关闭最近显示的缓冲区,因此只要有视图切换活动,缓冲区的终止就会延迟。

于 2013-04-06T18:13:34.100 回答
0

现在我在我的 ~/.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))))))
于 2013-03-10T18:47:46.223 回答