【发布时间】:2010-12-21 11:53:39
【问题描述】:
我如何以编程方式确定在 ELisp 中运行 Emacs 的操作系统?
我想根据操作系统在.emacs 中运行不同的代码。
【问题讨论】:
-
来自 GNU Emacs Lisp 参考手册gnu.org/software/emacs/manual/html_node/elisp/…
我如何以编程方式确定在 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
)
【讨论】:
progn 是块所必需的),所以向不熟悉的每个人推荐怪癖 - 检查this answer out.
progn 如果您没有 else 案例,则不需要。我的意思是你可以只使用when 而不是if,相当于(if ... (progn ...) '())
cond:(case system-type ((gnu/linux) "notify-send") ((darwin) "growlnotify -a Emacs.app -m"))
case,而不是cond。 case 有效,因为 system-type 是 'gnu/linux 或 darwin 之类的符号,而不是字符串
我创建了一个简单的宏来根据系统类型轻松运行代码:
(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 选项、终端或 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 变量的文档那样描述它可能包含的可能值。
【讨论】: