根据文件位置如何运行钩子 [英] How to run hook depending on file location
问题描述
我参与了使用标签的python项目,但是我并没有在我写的每个其他代码中使用它们,所以在该特定项目中使用它们至关重要。项目位于特定目录下的一个目录中。 IE:
I am involved in python project where tabs are used, however i am not using them in every other code i write, it is vital to use them in that particular project. Projects are located in one directory under specific directories. I.E:
\main_folder
\project1
\project2
\project3
...etc
我有几个功能/挂钩在文件打开并保存,untabify并列出整个缓冲区的工作。
I have couple functions/hooks on file open and save that untabify and tabify whole buffer i work on.
;; My Functions
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))
(defun tabify-buffer ()
"Tabify current buffer"
(interactive)
(tabify (point-min) (point-max)))
;; HOOKS
; untabify buffer on open
(add-hook 'find-file-hook 'untabify-buffer)
; tabify on save
(add-hook 'before-save-hook 'tabify-buffer)
如果我把它放在.emacs文件,它运行在我打开的每个.py文件,这不是我想要的。我想要拥有的是让这些挂钩只能在一个特定的文件夹中使用相应的子文件夹。尝试了.dir_locals,但它只适用于没有钩子的属性。因为几乎所有的项目都是用python编写的,所以我不能在特定模式下使用钩子(即python模式)。说实话,我试着写elisp条件保存但失败。
If i put it in .emacs file it is run on every .py file i open which is not what i want. What i`d like to have is to have these hooks used only in one particular folder with respective subfolders. Tried .dir_locals but it works only for properties not hooks. I can not use hooks in specific modes (i.e. python-mode) as almost all projects are written in python. To be honest i tried writing elisp conditional save but failed.
推荐答案
一个非常简单的解决方案是添加一个配置变量,用于禁用挂钩。例如:
A very easy solution is to just add a configuration variable that can be used to disable the hooks. For example:
(defvar tweak-tabs t)
(add-hook 'find-file-hook
(lambda () (when tweak-tabs (untabify (point-min) (point-max)))))
(add-hook 'before-save-hook
(lambda () (when tweak-tabs (tabify (point-min) (point-max)))))
您可以在相关目录中添加 .dir-locals.el
文件,将 tweak-tabs
设置为 nil
,禁用此功能。
Now you can add a .dir-locals.el
file in the relevant directories, setting tweak-tabs
to nil
, disabling this feature there.
(但另一个问题是这是一个非常糟糕的处理标签的方法。例如,在您保存文件后,您可以执行查看其中的标签。)
(But another problem is that this is a pretty bad way to deal with tabs. For example, after you save a file you do see the tabs in it.)
这篇关于根据文件位置如何运行钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!