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

ljs*_*ljs 82 emacs elisp

如何以编程方式确定ELisp中运行的Emacs操作系统?

我想.emacs根据操作系统运行不同的代码.

sco*_*zer 92

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.
Run Code Online (Sandbox Code Playgroud)


小智 76

对于较新的elisp人员,示例用法:

(if (eq system-type 'darwin)
  ; something for OS X if true
  ; optional something if not
)
Run Code Online (Sandbox Code Playgroud)

  • @kermit666 实际上,如果你没有 else 情况,则不需要 `progn` 。我的意思是你可以只使用 `when` 而不是 `if`,它相当于 `(if ... (progn ...) '())` (3认同)
  • @metakermit你可以像这样使用`cond`:`(case system-type((gnu/linux)"notify-send")((darwin)"growlnotify -a Emacs.app -m"))` (3认同)
  • 好吧,我在 Elisp 中用奇怪的分支块烧伤了自己好几次(if-和 else-部分由换行符分隔,块需要 `progn`),所以向每个不熟悉这些怪癖的人推荐 - 检查[这个答案]( http://stackoverflow.com/a/912397/544059)出来。 (2认同)

Ger*_*ann 21

我创建了一个简单的宏来轻松运行代码,具体取决于系统类型:

(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!"))
Run Code Online (Sandbox Code Playgroud)


Eri*_*ric 11

在.emacs中,不仅system-typewindow-system变量,还有变量.当您想要在某些x选项,终端或macos设置之间进行选择时,这非常有用.


Kon*_*ele 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")
  )
Run Code Online (Sandbox Code Playgroud)