共享 Emacs 配置文件在 Emacs 23 和 Emacs 24 之间的使用。

5
我正在尝试将所有的emacs配置放在版本控制下,以便轻松地在不同计算机之间切换。实际上,我的首选系统是OSX(10.8.3),使用http://emacsformacosx.com/提供的emacs 24.3。但我也可以在其他系统中工作(更可能是基于Linux的,虽然不同发行版如ubuntu/scientific-linux),这些系统通常配备的是emacs 23.4。我想要的是一个初始化文件,它可以检查emacs的版本和操作系统,并从emacs包管理器中加载所需的软件包。到目前为止,在OSX上,针对emacs 24.3的我的.emacs初始化文件如下:
(require 'package)
(setq package-archives '(
    ("marmalade" . "http://marmalade-repo.org/packages/")
    ("org" . "http://orgmode.org/elpa/")
    ("melpa" . "http://melpa.milkbox.net/packages/")))
(package-initialize)

之后是配置(例如单独加载的)(

(load "python-sy")

这个项目使用了一些不默认安装的包,特别是:

color-theme
org-mode
theme-changer
ess-site
magit
auctex
python.el (fgallina implementation)

除了依赖于已构建的软件包之外,还有其他一些需要的内容。 我承认我不知道如何开始编写一个可以在所有设备上通用的.emacs初始化文件。此外,我还希望有一种根据系统配置加载url-proxy-services的方法。

(setq url-proxy-services '(("http" . "proxy.server.com:8080")))

感谢您的帮助。

你可以在 Emacs 23 上获取 package.el 的版本;请访问 ELPA EmacsWiki 页面 上的链接。(如果相关系统升级到 Emacs 24,请确保将其移开。) - legoscia
3个回答

4
相关变量是system-typeemacs-major-version。您可以使用类似以下的内容:
(if (>= emacs-major-version 24)
    (progn
      ;; Do something for Emacs 24 or later
      )
  ;; Do something else for Emacs 23 or less
  )

(cond
 ((eq system-type 'windows-nt)
  ;; Do something on Windows NT
  )
 ((eq system-type 'darwind)
  ;; Do something on MAC OS
  )
 ((eq system-type 'gnu/linux)
  ;; Do something on GNU/Linux
  )
 ;; ...
 (t
  ;; Do something in any other case
  ))

1
除了 giornado 的答案,您还可以将特定于包的设置放置在一种方式中,以便仅在测试 (require) 结果时出现该包时才进行评估。例如,使用 bbdb 包:
(when (require 'bbdb nil t)
    (progn ...put your (setq) and other stuff here... ))

“eval-after-load” 不是更好吗?这样可以稍微减少启动时间,并且只在必要时执行代码。 - giordano
亲爱的 Giordano,你可以为我制作一个 eval-after-load 使用示例吗? - Nicola Vianello
2
@giordano: eval-after-load 允许仅在加载包时执行代码。在我的例子中,我想在启动时加载多个包,但如果该包不存在则不会出现错误(我习惯在 Linux、Windows 和 Mac 之间共享我的 .emacs,而不是每个主机都安装相同的包)。 - Seki
1
@NicolaVianello: eval-after-load 允许在文件加载后执行代码,而不一定是在启动时。例如:(eval-after-load "file" '(progn ....))。Seki 建议的略有不同:(when (require ...) ...) 总是在启动时执行代码。您可以选择适合您需求的方法。 - giordano

0
在这种情况下,我会在.emacs的顶部定义一些常量:
(defconst --xemacsp (featurep 'xemacs) "Is this XEmacs?")
(defconst --emacs24p (and (not --xemacsp) (>= emacs-major-version 24)))
(defconst --emacs23p (and (not --xemacsp) (>= emacs-major-version 23)))
(defconst --emacs22p (and (not --xemacsp) (>= emacs-major-version 22)))
(defconst --emacs21p (and (not --xemacsp) (>= emacs-major-version 21)))

使用示例:

(when --emacs24p
    (require 'epa-file)
    (epa-file-enable)
    (setq epa-file-cache-passphrase-for-symmetric-encryption t) ; default is nil
    )

或者:

  (if --emacs22p
      (c-toggle-auto-newline 1)
    (c-toggle-auto-state 1))

等等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接