有没有办法编写始终保持向上兼容的 .emacs?
我从 Emacs 18 版开始就一直在使用 Emacs。Emacs Lisp 不是我的常规编程语言,但几年前我投入了一些时间研究它,以至于创建了一个比迄今为止任何 GUI IDE 更好的 .emacs(对我来说)。
那是一次性的努力,从那时起,我完全忘记了如何用 lisp 编程。
唉,每次我升级我的 Emacs(18 > 19 > 20 > 21 > 22 > 23)时,我的 .emacs 中的某些东西就会损坏,我最终花费了太多的时间(有时是几天)来修复这些问题。
在我经常编程的许多其他语言中,可以编写永远不会过时的代码。另一方面,在 emacs 中,我永远无法预测事情会如何变化。例如,[M-TAB]
在版本 21.4.1 之前一直有效,在版本 23 中不再有效,必须替换为 "\M-\t"
。另一个例子是 dired-omit-toggle,它曾经在版本 21 中工作,但在版本 22 中停止工作,并被 dired-omit-mode 取代。
现在,我知道如果 .emacs 没有做太多事情,那么可以编写“几乎空的”.emacs,它可以(可能)与未来版本保持兼容。
但我的 .emacs 很大,设计用于在许多不同的操作系统(及其不同的风格、版本和版本,包括非 GUI 系统)上运行,无需进行任何更改。我希望有一个核心 API 或子集可以保证始终有效。
真的有这样一种方法可以编写始终保持向上兼容的 .emacs 吗?
如果是这样,我在哪里可以找到实现这一目标的“烹饪书”或权威指南?
I have been using Emacs since version 18. Emacs Lisp isn't my routine programming language, but years ago I invested some time studying it to the point of creating a .emacs that's better (for me) than any GUI IDE to date.
That was a one-time effort and since then, I forgot completely how to program in lisp.
Alas, every time I upgrade my Emacs (18 > 19 > 20 > 21 > 22 > 23), something in my .emacs breaks, and I end up spending way too many hours (sometimes days) fixing these.
In many other languages I program routinely, it is possible to write code that never becomes obsolete. In emacs, on the other hand, I can never predict how things will change. For example [M-TAB]
that used to work up until version 21.4.1, no longer works in version 23 and must be replaced with "\M-\t"
. Another example is dired-omit-toggle
that used to work in version 21 but stopped working in version 22, being replaced by dired-omit-mode
.
Now, I know that if a .emacs doesn't do much, it's possible to write "an almost empty" .emacs that can (probably) stay compatible with future versions.
But my .emacs is huge, designed to run on many different operating systems (and their different flavors, editions and versions, including non-GUI systems) without a single change. I wish there was a core-API or subset that is guaranteed to always work.
Is there really such a way to write a .emacs that will always stay upward compatible?
If so, where can I find a "cook book" or authoritative guidelines for accomplishing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在依赖于特定功能的任何代码片段周围使用
(when (boundp 'some-variable) ...)
或(when (fboundp 'some-function) ...)
。 (当你这样做时,对于那些没有内置when
的旧版本,从(require 'cl)
开始。)当所有其他方法都失败时,区分在
emacs-major-version
、emacs-minor-version
和running-xemacs
上(定义为(string-match "XEmacs\ \|清醒” emacs-版本)
)。避免仅在较新版本中支持的语法和库函数。
当您坚持使用已记录的接口时,很少会出现问题。因此,尝试寻找一种方法来执行手册中提到的操作,并且只有在没有其他方法时才回退到修改一些随机的不可自定义变量。根据我的经验,如果您允许偶尔使用条件,总有一种相当简单的方法可以让某些东西在所有版本中工作(至少对于 GNU Emacs,XEmacs 有时更难满足)。
例如,
"\e\t"
始终适用于M-TAB
。使用dired-omit-mode
,如果前者不存在,则回退到dired-omit-toggle
:是的,这有点痛苦,但没有你想象的那么戏剧性出来了。我维护了一个
.emacs
,它可以在所有 DOS、Windows 和 unix 上使用 19.23 以来的任何版本(IIRC,自从我使用 19.34 之前的版本进行测试以来已经有一段时间了)。事实上,与 Windows 的兼容性比跟上 GNU Emacs 版本的变化更多的是一种负担。至于食谱,版本之间的大多数差异都与使用不是很广泛的功能有关(否则维护人员会更加重视严格的向上兼容性)。由于 Emacs 很大,毫无疑问有很多你使用的东西,但烹饪书的作者不会听说过。你必须见机行事。幸运的是,这并不太难。
Use
(when (boundp 'some-variable) …)
or(when (fboundp 'some-function) …)
around any piece of code that relies on a specific feature. (While you're at it, start with(require 'cl)
for those older versions that don't havewhen
built-in.)When all else fails, discriminate on
emacs-major-version
,emacs-minor-version
andrunning-xemacs
(defined as(string-match "XEmacs\\|Lucid" emacs-version)
).Avoid syntax and library functions that are only supported in newer versions.
It's rare that something breaks when you stick to documented interfaces. So try to look for a way to do things that's mentioned in the manual, and only fall back to modifying some random non-customizable variable if you see no other way. In my experience there is always a reasonably easy way to get something to work in all versions (at least with GNU Emacs, XEmacs is sometimes harder to please) if you allow the occasional conditional.
For example,
"\e\t"
has always worked forM-TAB
. Withdired-omit-mode
, fall back todired-omit-toggle
if the former does not exist:Yes, it's slightly painful, but not as dramatic as you make it out to be. I maintain a
.emacs
that works with anything since 19.23 (IIRC, it's been a while since I tested with anything older than 19.34) on all of DOS, Windows and unix. Compatibility with Windows is in fact more of a burden than keeping up with GNU Emacs version changes.As for a cookbook, well, most differences between versions have to do with features that aren't very widely used (otherwise the maintainers would give more importance to strict upward compatibility). Since Emacs is big, there's undoubtedly plenty of it that you use but the cookbook writer wouldn't have heard of. You'll have to play it by ear. Fortunately it's not too hard.
由于无法预测任何事物未来的变化,因此没有人可以保证未来的兼容性。
我认为以下几点是使维护 .emacs 或 init.el 变得容易(我现在正在练习)的许多通用方法中的一些。
使用
when
加载某个版本的 emacs 的特定自定义。例如:
(当(= emacs-主要版本 24)
;;加载我的 24 小时设置
)
Since Its impossible to predict future changes of anything, no one can guarantee the future compatibility.
I think the following points are few of many general ways to make it easy to maintain .emacs or init.el (which I am practicing right now.)
use
when
to load specific customizations for a version of emacs.for eg:
(when (= emacs-major-version 24)
;;load my settings for 24
)
我同情你,我分担你的痛苦。答案是否定的,恐怕没有这样的圣杯。正如您所说,如果您的初始化文件足够简单,那么您可能不需要更改任何内容。事实上,即使这样也不是真的:init 文件名和 Emacs 查找它的位置都随着时间的推移而改变。
I sympathize, and I share your pain. The answer is no, there is no such holy grail, I'm afraid. As you said, if your init file is simple enough then you might not need to change anything. Actually, even that's not true: the init file name and where Emacs looks for it have both changed over time.