在 Emacs 24 中取消保存时不使用 make-local-hook

在 Emacs 24 中取消保存时不使用 make-local-hook

每当我保存缓冲区时,我想取消制表符。这工作得很好,直到我升级到 Emacs 24,它破坏了它,因为不再有make-local-hook。看一看:

;;; untabify on C-x C-s (except for GNUmakefiles that needs tabs)
;;;   (note: `stringp` check to avoid bug (?) in HTML-mode)
(defun untab-all ()
  (unless (and (stringp mode-name)
               (string= mode-name "GNUmakefile") )
    (untabify (point-min) (point-max)) ))

;; TODO: doesn't work anymore as no make-local-hook in Emacs 24 - 
;; it won't work with simply dropping that line,
;; or doing that, and passing a 4th `t` argument in the second
;; add-hook call (which is the LOCAL parameter)

;; (add-hook 'emacs-lisp-mode-hook
;;    '(lambda ()
;;       (make-local-hook 'write-contents-hooks)
;;       (add-hook 'write-contents-hooks 'untab-all) ))

编辑

通过以下帮助让它可以工作并且地点。

具有讽刺意味的是,使用我的旧解决方案(使用make-local-hook),不知何故我得到了这个全球(这就是为什么我必须添加对 GNU makefile 的检查)。

现在,这似乎是本地的事情:这很好,因为不需要在每次保存时都进行检查;只是,您必须说明应该执行什么模式 untabify。 (我想你可以在每次发现选项卡时一一添加它们,直到完成为止。虽然感觉你正在使用 5-6 种模式,但当你想到这一点时,你会使用吨!)

但是,更令人困惑的是,HTML 模式似乎可以做到这一点,而无需任何干预!如果您阅读上面的代码,您会发现即使对于旧的解决方案,HTML 也是白貂中的猫......

无论如何,这似乎有效:

(defun untab-all ()
  (untabify (point-min) (point-max))
   nil ) ; did not write buffer to disk

(defun add-write-contents-hooks-hook ()
  (add-hook
   'write-contents-hooks
   'untab-all
     nil  ; APPEND  unrelated, explicit default nil as optional :)
     t )) ; LOCAL   non-nil => make hook local

;; more modes: http://www.emacswiki.org/CategoryModes
(add-hook 'emacs-lisp-mode-hook #'add-write-contents-hooks-hook)
(add-hook 'c-mode-common-hook   #'add-write-contents-hooks-hook)
(add-hook 'sh-mode-hook         #'add-write-contents-hooks-hook)
(add-hook 'text-mode-hook       #'add-write-contents-hooks-hook)
(add-hook 'sql-mode-hook        #'add-write-contents-hooks-hook)
(add-hook 'css-mode-hook        #'add-write-contents-hooks-hook)

答案1

Emacs 24 使用LOCAL参数add-hook代替make-local-hook。 (我相信这是在 Emacs 21.1 中添加的,但make-local-hook直到 Emacs 24 才被删除。)

尝试这个:

(add-hook 'emacs-lisp-mode-hook
    '(lambda ()
       (add-hook 'write-contents-hooks 'untab-all nil t) ))

您的另一个问题是untab-all必须返回nil以表明它没有将缓冲区写入磁盘(如 文档中所述write-contents-hooks,或者现在称为write-contents-functions):

(defun untab-all ()
  (unless (and (stringp mode-name)
               (string= mode-name "GNUmakefile") )
    (untabify (point-min) (point-max)) )
  nil) ; did not write buffer to disk

相关内容