Emacs 24:除了* makefiles之外,保存所有内容* unabify

时间:2014-07-18 19:47:49

标签: 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-saveBSDmakefile-mode正在运行makefile-mode。我怎么能不这样做呢?

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

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

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

3 个答案:

答案 0 :(得分:4)

以下定义了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添加到各种模式挂钩基本上是调用一个函数来添加到另一个钩子的钩子,这个钩子没有实现你想要的功能,并且在概念上很奇怪。

答案 1 :(得分:0)

根据emacs: is before-save-hook a local variable?,(!!我错误地认为)解决方案是改变

(defun untabify-everything-on-save ()
  (add-hook 'before-save-hook 'untabify-everything)
  nil)

(defun untabify-everything-on-save ()
  (add-hook 'write-contents-functions 'untabify-everything)
  nil)

因为我猜前者会感染多个缓冲区,而后者显然是特定缓冲区的本地缓冲区。

编辑:等等,我保存并重新加载了我的.emacs文件并认为它有效,但后来我重新启动了Emacs,现在它在我尝试保存.emacs缓冲区时挂起(可能还有所有调用untabify-everything-on-save)的其他模式。

edit2:我nano'我的.emacs文件注释了untabify-everything-on-saveemacs-lisp-mode文件的行,所以至少我可以编辑在Emacs中的文件,但似乎任何其他模式都无法保存。

答案 2 :(得分:0)

我遇到了同样的问题,就遇到了这个问题。最后我想到了:

(defun tvaughan/untabify ()
  "Preserve initial tab when 'makefile-mode."
  (interactive)
  (save-excursion
    (if (derived-mode-p 'makefile-mode)
      (progn
        (goto-char (point-min))
        (while (not (eobp))
          (skip-chars-forward "\t")
          (untabify (point) (line-end-position))
          (forward-line 1)))
      (untabify (point-min) (point-max)))))

(add-hook 'before-save-hook 'tvaughan/untabify)

我经常将命令跨几行,并且我喜欢将行连续标记排列起来。例如:

.PHONY: import-ova
import-ova: $(VMPACKAGE).ova
    @if ! VBoxManage list vms | grep -cq $(VMNAME);                         \
    then                                                                    \
        VBoxManage import $(VMPACKAGE).ova                                  \
          --vsys 0                                                          \
          --vmname $(VMNAME)                                                \
          --ostype Windows10_64                                             \
          --cpus 2                                                          \
          --memory 2048                                                     \
          --eula accept;                                                    \
    fi

来源:https://gitlab.com/tvaughan/windows-vm/blob/master/Makefile