Emacs 24 : untabify on save for everything *except* makefiles

标签 emacs makefile

我的 .emacs 中有以下代码:

;; untabify on save                                                                                                                                                                                               
;; source: http://www.emacswiki.org/emacs/UntabifyUponSave and                                                                                                                                                    
;; http://stackoverflow.com/questions/318553/getting-emacs-to-untabify-when-saving-certain-file-types-and-only-those-file-ty                                                                                      
;; and a little help from http://ergoemacs.org/emacs/emacs_avoid_lambda_in_hook.html                                                                                                                              
;; and help from http://stackoverflow.com/questions/1931784/emacs-is-before-save-hook-a-local-variable                                                                                                            
(defun untabify-everything ()
  (untabify (point-min) (point-max)))
(defun untabify-everything-on-save ()
  (add-hook 'before-save-hook 'untabify-everything)
  nil)

;; I think the c-mode-common-hook includes the makefile-modes, so it's untabifying those                                                                                                                          
;; maybe not?                                                                                                                                                                                                     
(add-hook 'c-mode-common-hook 'untabify-everything-on-save)
;; (add-hook 'c-mode-hook 'untabify-everything-on-save)                                                                                                                                                           
;; (add-hook 'c++-mode-hook 'untabify-everything-on-save)                                                                                                                                                         
;; (add-hook 'java-mode-hook 'untabify-everything-on-save)                                                                                                                                                        
(add-hook 'python-mode-hook 'untabify-everything-on-save)
(add-hook 'latex-mode-hook 'untabify-everything-on-save)
(add-hook 'org-mode-hook 'untabify-everything-on-save)
(add-hook 'css-mode-hook 'untabify-everything-on-save)
(add-hook 'html-mode-hook 'untabify-everything-on-save)
(add-hook 'emacs-lisp-mode-hook 'untabify-everything-on-save)

但是 untabify-everything-on-save 似乎正在为 BSDmakefile-modemakefile-mode 运行。我如何让它不那样做?

(我的 makefile 现在有一个解决方法:

.RECIPEPREFIX = +
tree:
+ @tree -L 2 -C $(PROJECT_DIR)

但这不是一个令人满意的解决方案。它假设每个收到我的 makefile 的人都拥有 > 3.81 的 GNU Make 版本,我不能保证这一点。)

最佳答案

下面定义了一个函数来untabify 整个缓冲区,除非它的主要模式是makefile-mode 或其衍生物之一。我们可以将该函数放在 before-save-hook 中以获得您想要的功能:

(defun untabify-except-makefiles ()
  "Replace tabs with spaces except in makefiles."
  (unless (derived-mode-p 'makefile-mode)
    (untabify (point-min) (point-max))))

(add-hook 'before-save-hook 'untabify-except-makefiles)

请注意,原始 untabify-everything-on-save 所做的唯一一件事就是将原始 untabify-everything 添加到 before-save- hook,因此它被应用于您尝试保存到文件的每个缓冲区,包括 make 文件。将原始的 untabify-everything-on-save 添加到各种模式 Hook 基本上是调用一个函数从另一个 Hook 添加到一个 Hook ,它没有实现您想要的功能并且在概念上很奇怪。

关于Emacs 24 : untabify on save for everything *except* makefiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24832699/

相关文章:

emacs - 通过 Emacs 在代码注释中插入图片

windows - 粘贴文本的 Emacs 编码

c++ - Emacs 缩进模板类/函数

c++ - Makefile 在同一目录中找不到文件

emacs - 将组织模式表转换为LaTeX

c++ - 制作 : No rule to make a header file?

c - Makefile递归不传递CPPFLAGS

c++ - 如何使用来自不同目录的make文件中的文件

c++ - Libnoise 不想编译 : Cannot find -lnoise

emacs - 如何在控制台模式下不使用颜色主题?