如何以编程方式确定 ELisp 中运行的操作系统 Emacs?
我想根据操作系统在 .emacs
中运行不同的代码。
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.
对于 elisp 的新手,示例用法:
(if (eq system-type 'darwin)
; something for OS X if true
; optional something if not
)
我创建了一个简单的宏来根据系统类型轻松运行代码:
(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!"))
在 .emacs 中,不仅有 system-type
,还有 window-system
变量。当您想在某些 x only 选项、终端或 macos 设置之间进行选择时,这很有用。
现在还有适用于 Windows 的 Linux 子系统(Windows 10 下的 bash),其中 system-type
是 gnu/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")
)
这大部分已经回答了,但是对于那些感兴趣的人,我刚刚在 FreeBSD 上测试了这个,报告的值是“berkeley-unix”。
还有(至少在版本 24-26 中)system-configuration
,如果您想调整构建系统的差异。但是,此变量的文档没有像 system-type
变量的文档那样描述它可能包含的可能值。
不定期副业成功案例分享
progn
),所以向不熟悉这些怪癖的每个人推荐 - 检查 this answer。progn
如果您没有 else 案例,则不需要。我的意思是您可以只使用when
而不是if
,这相当于(if ... (progn ...) '())
cond
:(case system-type ((gnu/linux) "notify-send") ((darwin) "growlnotify -a Emacs.app -m"))
when
。