How to determine operating system in elisp?

和自甴很熟 提交于 2019-12-20 08:31:09

问题


How do I programmatically determine which OS Emacs is running under in ELisp?

I would like to run different code in .emacs depending on the OS.


回答1:


The system-type variable:

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:


For folks newer to elisp, a sample usage:

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



回答3:


I created a simple macro to easily run code depending on the system-type:

(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:


In a .emacs, there is not only the system-type, but also the window-system variable. This is useful when you want to choose between some x only option, or a terminal, or macos setting.




回答5:


Now there is also Linux Subsystem for Windows (bash under Windows 10) where system-type is gnu/linux. To detect this system type use:

(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:


This is mostly already answered, but for those interested, I just tested this on FreeBSD and there the reported value was "berkeley-unix".




回答7:


There's also (in 24/25 at least) system-configuration, if you want to adjust for differences in build system.



来源:https://stackoverflow.com/questions/1817257/how-to-determine-operating-system-in-elisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!