如何在elisp中确定操作系统?


Answers:


98

system-type变量:

system-type is a variable defined in `C source code'.
Its value is darwin

Documentation:
Value is symbol indicating type of operating system you are using.
Special values:
  `gnu'         compiled for a GNU Hurd system.
  `gnu/linux'   compiled for a GNU/Linux system.
  `darwin'      compiled for Darwin (GNU-Darwin, Mac OS X, ...).
  `ms-dos'      compiled as an MS-DOS application.
  `windows-nt'  compiled as a native W32 application.
  `cygwin'      compiled using the Cygwin library.
Anything else indicates some sort of Unix system.

83

对于刚接触elisp的人们,可以使用以下示例:

(if (eq system-type 'darwin)
  ; something for OS X if true
  ; optional something if not
)

好吧,我在Elisp中用怪异的分支代码块烧了自己好几次(if和else-部分用换行符隔开,progn这对于代码块是必要的),所以给不熟悉怪癖的每个人一个建议-查看此答案
metakermit,2013年

1
progn如果您没有其他情况,则实际上不需要@ kermit666 。我的意思是,您可以使用when代替if,它等效于(if ... (progn ...) '())
电动咖啡

1
由于我正尝试使用“ =”而无法使用,因此遭到支持。
菲利普·丹尼尔斯

3
@metakermit,您可以这样使用cond(case system-type ((gnu/linux) "notify-send") ((darwin) "growlnotify -a Emacs.app -m"))
ealfonso

我的意思case不是condcase可以正常工作,因为它system-type是一个符号,例如'gnu/linuxdarwin,而不是字符串
-ealfonso

22

我创建了一个简单的宏,可以轻松地根据系统类型运行代码:

(defmacro with-system (type &rest body)
  "Evaluate BODY if `system-type' equals TYPE."
  (declare (indent defun))
  `(when (eq system-type ',type)
     ,@body))

(with-system gnu/linux
  (message "Free as in Beer")
  (message "Free as in Freedom!"))

11

在.emacs中,不仅存在system-type,而且还存在window-system变量。当您要在仅x选项,终端或macos设置之间进行选择时,这很有用。


5

现在也有Linux的子系统的基于Windows(Windows 10下的bash),其中system-typegnu/linux。要检测此系统类型,请使用:

(if
    (string-match "Microsoft"
         (with-temp-buffer (shell-command "uname -r" t)
                           (goto-char (point-max))
                           (delete-char -1)
                           (buffer-string)))
    (message "Running under Linux subsystem for Windows")
    (message "Not running under Linux subsystem for Windows")
  )


0

system-configuration如果要针对构建系统中的差异进行调整,则还有(至少在24-26版中)。但是,此变量的文档没有像变量的文档那样描述它可能包含的可能值 system-type

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.