3

我在 Emacs 中使用 tab-bar 来标记我的工作,到目前为止,我发现 tabbar-ruler 基本上添加了一个指示器来显示文件何时被修改。显然,它本身不起作用。

我添加了一些代码让 tabbar 知道文件已被修改,但是当我恢复我的工作时它不起作用。如果可能的话,我需要一些钩子让emacs知道在达到“不必保存”状态时,它会更新标签栏。

这是到目前为止的代码:

(defadvice tabbar-buffer-tab-label (after fixup_tab_label_space_and_flag activate)
(setq ad-return-value
     (if (and (buffer-modified-p (tabbar-tab-value tab))
              (buffer-file-name (tabbar-tab-value tab)))
         (concat " " (concat ad-return-value " "))
       (concat " " (concat ad-return-value " ")))))

;; Called each time the modification state of the buffer changed.
(defun er/modification-state-change ()
    (tabbar-set-template tabbar-current-tabset nil)
    (tabbar-display-update))
(add-hook 'after-save-hook 'er/modification-state-change)

;; First-change-hook is called BEFORE the change is made.
(defun er/on-buffer-modification ()
    (set-buffer-modified-p t)
    (er/modification-state-change))
(add-hook 'first-change-hook 'er/on-buffer-modification)

;; Retrieve original state after revert is made.
(defun er/on-buffer-revert ()
    (set-buffer-modified-p nil)
    (er/modification-state-change))
(add-hook 'after-revert-hook 'er/on-buffer-revert)

er/on-buffer-revert 函数是我尝试至少达到恢复的缓冲区状态。也不行。

第一段代码,顺便说一句,只是添加一个额外的空格标签栏,原来没有。

有什么想法吗?

4

1 回答 1

0

我知道你几年前问过这个问题,但这里有一个正确支持还原的工作示例。请注意,它在以下情况下正确支持 tabbar 状态:

  • 缓冲恢复
  • 撤消回到上次保存的状态
  • 更改文本以匹配上次保存之前的内容
    (defun my-ignore-buffer-p ()
      (not (buffer-file-name)))

    (setq my-buffer-hashes (make-hash-table :test 'equal))
    (defun my-set-buffer-hash ()
      (puthash (current-buffer) (buffer-hash) my-buffer-hashes))
    (defun my-get-buffer-hash ()
      (gethash (current-buffer) my-buffer-hashes))
    (defun my-buffer-hash-changed-p ()
      (not (string= (buffer-hash) (my-get-buffer-hash))))

    ;; Hook save and change events to show modified buffers in tabbar
    (defun on-saving-buffer ()
      (tabbar-set-template tabbar-current-tabset nil)
      (tabbar-display-update))
    (defun on-modifying-buffer ()
      (unless (my-ignore-buffer-p)
        (my-set-buffer-hash)
        (set-buffer-modified-p (buffer-modified-p))
        (tabbar-set-template tabbar-current-tabset nil)
        (tabbar-display-update)))
    (defun after-modifying-buffer (begin end length)
      (unless (my-ignore-buffer-p)
        (set-buffer-modified-p (my-buffer-hash-changed-p))
        (tabbar-set-template tabbar-current-tabset nil)
        (tabbar-display-update)))

    (add-hook 'after-save-hook 'on-saving-buffer)
    (add-hook 'first-change-hook 'on-modifying-buffer)
    (add-hook 'after-change-functions 'after-modifying-buffer)
于 2020-03-11T04:36:58.027 回答