自动恢复Emacs缓冲区时,仅突出显示新添加的行

时间:2018-01-24 13:26:24

标签: emacs buffer hook revert

我有一个Emacs命令,它在日志文件中添加一个分隔线,当在两个连续行上找到时间戳之间的时间超过3秒时:

(defun log-mode-display-separator-line ()
  "Separate blocks of log lines with an horizontal line."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (let (last ov)
      (while (re-search-forward "[0-9]\\{4\\}-[01][1-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]" nil t)
        (when last
          (when (> (- (float-time (date-to-time (match-string 0)))
                      (float-time (date-to-time last)))
                   3)
            (setq ov (make-overlay (line-beginning-position) (line-end-position)))
            (overlay-put ov 'face 'log-mode-separator-face)))
        (setq last (match-string 0))))))

log-mode-separator-face使用overline属性在" semantic"之间可视地放置一条水平线。线块。

我希望在恢复缓冲区时每隔5秒自动插入一条水平线,但是对于不断更新的大型日志文件,处理可能会变得过大。截至目前,每次调用该命令时,都会从头开始扫描整个文件......

那我怎么能意识到这一点呢? (对于Emacs,毫无疑问它是......)

1 个答案:

答案 0 :(得分:1)

首先,无法保证缓冲区每5秒恢复一次。从Emacs 24.4开始,autorevert使用文件通知,这意味着只要文件在磁盘上发生更改,就会恢复缓冲区。如果要取消此操作,请将auto-revert-use-notify设置为nil。

此外,它取决于您希望如何还原日志文件。我想你使用auto-revert-tail-mode。此模式调用before-revert-hookafter-revert-hook中包含的钩子函数。作为一种策略,我会为after-revert-hook编写一个函数,它强调要恢复的缓冲区中的最后一行。像这样:

(defun my-auto-revert-tail-mode-display-separator-line ()
  "Separate blocks of log lines with an horizontal line."
  (save-excursion
    (goto-char (point-max))
    (while (and (= (line-beginning-position) (line-end-position))
        (> (point) (point-min)))
      (forward-line -1))
    (let ((ov (make-overlay (line-beginning-position) (line-end-position))))
      (overlay-put ov 'face 'underline))))

(add-hook 'after-revert-hook 'my-auto-revert-tail-mode-display-separator-line)