I want my emacs buffer to have a different name than the file name. Rather than setting this manually every time, I want to have this happen automatically based on the file contents, something like:
我希望我的emacs缓冲区具有与文件名不同的名称。我不想每次都手动设置,而是希望根据文件内容自动执行此操作,例如:
// Local Variables:
// buffer-name: MyName
// End:
//局部变量:// buffer-name:MyName //结束:
But this doesn't work because buffer-name is a function, not a variable. How can I do this?
但这不起作用,因为buffer-name是一个函数,而不是一个变量。我怎样才能做到这一点?
2 个解决方案
#1
12
You could say:
你可以说:
// Local Variables:
// eval: (rename-buffer "my-buffer-name-here")
// end:
It is a trick though.
这是一个技巧。
You could otherwise program a find-file-hook
hook in your .emacs
which rename the buffer to a specific local variable contents. Something like:
否则,您可以在.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)
Then in your specific file you have
然后在您的特定文件中
// Local Variables:
// pdp-buffer-name: "pierre"
// end:
With more brain power you could have a nicer solution.
凭借更强大的脑力,您可以拥有更好的解决方案。
Note that there could already exist an extension for your need. Look in the Emacs wiki.
请注意,您的需求可能已存在扩展。查看Emacs维基。
#2
3
Thanks Pierre. Your pdp-buffer-name elisp example worked very well.
谢谢皮埃尔。您的pdp-buffer-name elisp示例运行良好。
I made one enhancement because I noticed emacs was treating the local variable as "unsafe" i.e., always prompting to ask if the value should be applied. Since I want this to work with many different values without cluttering up my .emacs with a list of "safe" values, I added a piece of advice. With the nomenclature of the previous example, it looks like this:
我做了一个增强,因为我注意到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))
)
#1
12
You could say:
你可以说:
// Local Variables:
// eval: (rename-buffer "my-buffer-name-here")
// end:
It is a trick though.
这是一个技巧。
You could otherwise program a find-file-hook
hook in your .emacs
which rename the buffer to a specific local variable contents. Something like:
否则,您可以在.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)
Then in your specific file you have
然后在您的特定文件中
// Local Variables:
// pdp-buffer-name: "pierre"
// end:
With more brain power you could have a nicer solution.
凭借更强大的脑力,您可以拥有更好的解决方案。
Note that there could already exist an extension for your need. Look in the Emacs wiki.
请注意,您的需求可能已存在扩展。查看Emacs维基。
#2
3
Thanks Pierre. Your pdp-buffer-name elisp example worked very well.
谢谢皮埃尔。您的pdp-buffer-name elisp示例运行良好。
I made one enhancement because I noticed emacs was treating the local variable as "unsafe" i.e., always prompting to ask if the value should be applied. Since I want this to work with many different values without cluttering up my .emacs with a list of "safe" values, I added a piece of advice. With the nomenclature of the previous example, it looks like this:
我做了一个增强,因为我注意到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))
)