ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

来自 GNU Emacs Lisp 参考手册 gnu.org/software/emacs/manual/html_node/elisp/…

s
scottfrazer

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.

E
Endrju

对于 elisp 的新手,示例用法:

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

好的,我在 Elisp 中使用奇怪的分支块多次烧毁自己(if-和 else-部分由换行符分隔,块需要 progn),所以向不熟悉这些怪癖的每个人推荐 - 检查 this answer
@kermit666 实际上 progn 如果您没有 else 案例,则不需要。我的意思是您可以只使用 when 而不是 if,这相当于 (if ... (progn ...) '())
赞成,因为我试图使用“=”但它不起作用。
@metakermit 您可以像这样使用 cond(case system-type ((gnu/linux) "notify-send") ((darwin) "growlnotify -a Emacs.app -m"))
注意 OP:对于单个分支,可以使用 when
G
Gerstmann

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

(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!"))

E
Eric

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


K
Konrad Eisele

现在还有适用于 Windows 的 Linux 子系统(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")
  )

J
Jostein Kjønigsen

这大部分已经回答了,但是对于那些感兴趣的人,我刚刚在 FreeBSD 上测试了这个,报告的值是“berkeley-unix”。


J
JCB

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