English 中文(简体)
How to determine operating system in elisp?
原标题:
  • 时间:2009-11-30 00:14:28
  •  标签:
  • emacs
  • elisp

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.

最佳回答

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.
问题回答

For folks newer to elisp, a sample usage:

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

Or, if we don t care for the else-form and have multiple then-forms,

(when (eq system-type  darwin)
  ; do this
  ; and this ...
)

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

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.

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

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

There s also (in versions 24-26 at least) system-configuration, if you want to adjust for differences in build system. However, the documentation of this variable does not describe the possible vales that it may contain like the documentation of the system-type variable does.

The easiest way to do this is to do a pattern match on the system-type variable, like this:

(pcase system-type
  ;; GNU/Linux or WSL
  (gnu/linux
   (message "This is GNU/Linux"))

  ;; macOS
  (darwin
   (message "This is macOS"))

  ;; Windows
  (windows-nt
   (message "This is Windows")) 

  ;; BSDs
  (berkeley-unix
    (message "This is a BSD"))

  ;; Other operating system
  (_
   (message "Unknown operating system")))

For more information, and additional types of operating systems, see the full documentation on system-type in https://www.gnu.org/software/emacs/manual/html_node/elisp/System-Environment.html

(An easy way to test the above code is to paste it in your *scratch* buffer and press C-j after the outermost parenthesis)





相关问题
suppress additional braces in emacs electric mode

I started using ruby-electric-mode. I like it except that I am used to closing open brackets myself (the other pairing are still useful to me). How can I make emacs suppress additional brackets when ...

Setting up an emacs environment in windows?

I am currently constrained to a windows dev box and I want to migrate my projects from eclipse to emacs. What are some good references on setting up an emacs dev environment for windows? Anything ...

Emacs Setting which-function-mode

I would like to have which-function-mode on by default when I open up Emacs. I ve added the following lines to my .emacs file. (setq which-func-mode t) (setq which-function-mode t) When I open ...

Enabling go mode for emacs

I don t seem to be able to enable a go mode for emacs. C mode doesn t work without semicolons. The best I have found is the JavaScript mode by Karl Landstrom, since JavaScript also doesn t require ...

Custom Emacs key bindings are not working

The key bindings I ve defined in my .emacs file aren t working. Here s the file: ;init modes (menu-bar-mode 0) (tool-bar-mode 0) (cua-mode) (column-number-mode) (fset perl-mode cperl-mode) (cperl-...

eval during emacs lisp macro expansion

How can I fix the simple macro foo in (elisp)Eval During Expansion? None of the followings work: (defmacro foo1 (a) `(setq (eval ,a) t)) (defmacro foo2 (a) `(setq ,(eval a) t)) (defmacro foo3 (...

Get local cvs comment history when committing file/s in emacs

I often commit files with similar cvs comment but not in a single operation. I would like to able to bring up previous comments I ve used in a previous commit when I am in the process of writing a ...

热门标签