【问题标题】:How to determine operating system in elisp?如何确定elisp中的操作系统?
【发布时间】:2010-12-21 11:53:39
【问题描述】:

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

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

【问题讨论】:

标签: emacs elisp


【解决方案1】:

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.

【讨论】:

    【解决方案2】:

    对于刚接触 elisp 的人,示例用法:

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

    【讨论】:

    • 好的,我在 Elisp 中用奇怪的分支块烧了自己好几次(if- 和 else-part 由换行符分隔,progn 是块所必需的),所以向不熟悉的每个人推荐怪癖 - 检查this answer out.
    • @kermit666 实际上 progn 如果您没有 else 案例,则不需要。我的意思是你可以只使用when 而不是if,相当于(if ... (progn ...) '())
    • 投了赞成票,因为我试图使用“=”但它不起作用。
    • @metakermit 你可以像这样使用cond(case system-type ((gnu/linux) "notify-send") ((darwin) "growlnotify -a Emacs.app -m"))
    • 我的意思是case,而不是condcase 有效,因为 system-type'gnu/linuxdarwin 之类的符号,而不是字符串
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        现在还有适用于 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")
          )
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 2010-09-22
              • 2017-03-08
              • 1970-01-01
              • 2016-12-11
              • 2019-04-13
              • 1970-01-01
              • 2013-01-02
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多