我的.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-mode和makefile-mode。我怎么能让它不这么做呢?
(我的makefile现在有一个解决办法:
.RECIPEPREFIX = +
tree:
+ @tree -L 2 -C $(PROJECT_DIR)但这不是一个令人满意的解决方案。它假设收到我的makefile的每个人都有GNU Make版本> 3.81,我无法保证这一点。
发布于 2014-07-21 02:55:50
下面定义了一个函数来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添加到各种模式钩子基本上是调用一个函数从另一个钩子添加到钩子中,这没有实现您想要的功能,在概念上也很奇怪。
发布于 2014-07-18 19:57:23
根据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-lisp-mode中untabify-everything-on-saveing文件的行,所以至少我可以在Emacs中编辑该文件,但其他模式似乎都无法保存。
发布于 2020-01-22 16:27:55
当我有同样的问题时,我发现了这个问题。最后我想出了:
(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
https://stackoverflow.com/questions/24832699
复制相似问题