如何使用文件局部变量设置Emacs缓冲区名称?
我希望emacs缓冲区的名称与文件名不同。我不希望每次都手动设置,而是希望根据文件内容自动进行设置,例如:如何使用文件局部变量设置Emacs缓冲区名称?,emacs,Emacs,我希望emacs缓冲区的名称与文件名不同。我不希望每次都手动设置,而是希望根据文件内容自动进行设置,例如: (defvar pdp-buffer-name nil) (defun pdp-rename-buffer-if-necessary () "Rename the current buffer according to the value of variable" (interactive) (if (and pdp-buffer-name (stringp pdp-buff
(defvar pdp-buffer-name nil)
(defun pdp-rename-buffer-if-necessary ()
"Rename the current buffer according to the value of variable"
(interactive)
(if (and pdp-buffer-name (stringp pdp-buffer-name))
(rename-buffer pdp-buffer-name)))
(add-hook 'find-file-hook 'pdp-rename-buffer-if-necessary)
//局部变量://缓冲区名称:MyName
//完: 但这不起作用,因为缓冲区名称是一个函数,而不是一个变量。我该怎么做呢?你可以说:
// Local Variables:
// eval: (rename-buffer "my-buffer-name-here")
// end:
不过这是一个骗局
您可以在.emacs
中编写一个find file hook
钩子,将缓冲区重命名为特定的局部变量内容。比如:
(defvar pdp-buffer-name nil)
(defun pdp-rename-buffer-if-necessary ()
"Rename the current buffer according to the value of variable"
(interactive)
(if (and pdp-buffer-name (stringp pdp-buffer-name))
(rename-buffer pdp-buffer-name)))
(add-hook 'find-file-hook 'pdp-rename-buffer-if-necessary)
然后在您的特定文件中
// Local Variables:
// pdp-buffer-name: "pierre"
// end:
有了更多的脑力,你可能会有一个更好的解决方案
请注意,可能已经存在满足您需要的扩展。请看地图。谢谢皮埃尔。您的pdp缓冲区名称elisp示例运行得非常好 我做了一个增强,因为我注意到emacs将局部变量视为“不安全”,即总是提示询问是否应该应用该值。由于我希望它能够与许多不同的值一起工作,而不会将.emacs与“安全”值列表混在一起,因此我添加了一条建议。根据上一个示例的命名法,它如下所示:
;; allow all values for "pdp-buffer-name"
(defadvice safe-local-variable-p (after allow-pdp-buffer-name (sym val) activate)
(if (eq sym 'pdp-buffer-name)
(setq ad-return-value t))
)
我猜Emacs每次都会警告“eval”,但这是个好主意!是的,对,它每次都会警告“eval”。如果变量“enable local variable”困扰您,则可以将其设置为t。这是不安全的,虽然…这应该完成的解决方案肯定。美好的