Emacs .dir-locals.el - setting key bindings

≡放荡痞女 提交于 2019-12-05 00:48:39

问题


I'm not sure this is possible, but I'd like to setup some project specific key bindings by using .dir-locals.el

Of course .dir-locals.el has to contain a special list of settings, so I can't do:

(global-set-key [24 down] 'move-text-down)

Is there any way I can inject a lambda to run arbitrary code or some other way to set key bindings in .dir-locals.el?


回答1:


The eval pseudo-variable enables you to specify arbitrary elisp for evaluation with your local variables.

e.g. https://stackoverflow.com/a/7340962/324105

See EmacsWiki for more details.

Note that this is not a particularly useful mechanism for setting key bindings, as every buffer using the keymap in question will be affected. You would probably be better off using the dir-local config to enable a minor mode with the specific keymap for that project. Alternatively, you might adapt this approach to file-local bindings (but a minor mode would be nicer).

That being said...

A fairly minimal form is ((nil . ((eval . (progn BODY))))) with BODY being the expressions to be evaluated. Of course if BODY is only a single expression, you do not need progn.

The following therefore displays a message when you visit any file (under the directory in question):

((nil . ((eval . (message "hello")))))

The car of each list in the dir-locals form is generally a major mode symbol, or nil (as in the above example) in which case the settings apply in any major mode.

The car can also specify a sub-directory string, in which case the cdr is another dir-locals form with settings applicable to that sub-dir.



来源:https://stackoverflow.com/questions/19738195/emacs-dir-locals-el-setting-key-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!