无需保存时更新标签栏

时间:2013-03-31 22:11:33

标签: emacs elisp

我正在使用tab-bar来标记我在Emacs中的工作,到目前为止,我发现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函数是我尝试至少达到恢复的缓冲区状态。 也不起作用。

第一段代码,顺便说一下,只是添加了一个原创的空格tabbar。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我知道您几年前曾问过这个问题,但这是一个正确支持还原的有效示例。请注意,它在以下情况下正确支持标签栏状态:

  • 缓冲区还原
  • 撤消回到上次保存的状态
  • 更改文本以匹配上次保存之前的内容
    (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)