Jak programowo określić, który system operacyjny Emacs działa w ELisp?
Chciałbym uruchomić inny kod w .emacszależności od systemu operacyjnego.
Odpowiedzi:
system-typeZmienna:
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.
Dla nowszych użytkowników, przykładowe użycie:
(if (eq system-type 'darwin)
; something for OS X if true
; optional something if not
)
prognniezbędna dla bloków), więc rekomendacja dla wszystkich nieobeznanych z dziwactwami - sprawdź tę odpowiedź .
prognnie jest potrzebne, jeśli nie masz innego przypadku. Rozumiem przez to, że możesz po prostu użyć whenzamiast if, co jest równoważne z(if ... (progn ...) '())
condsposób:(case system-type ((gnu/linux) "notify-send") ((darwin) "growlnotify -a Emacs.app -m"))
casenie cond. casedziała, ponieważ system-typejest symbolem takim jak 'gnu/linuxlub darwin, a nie ciągiem
Stworzyłem proste makro, aby łatwo uruchamiać kod w zależności od typu systemu:
(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!"))
Teraz jest też podsystem Linux dla Windows (bash pod Windows 10), gdzie system-typejest gnu/linux. Aby wykryć ten typ systemu, użyj:
(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")
)
Na to w większości już udzielono odpowiedzi, ale dla zainteresowanych właśnie przetestowałem to na FreeBSD i tam zgłoszono wartość „berkeley-unix”.
Jest też (przynajmniej w wersjach 24-26) system-configuration, jeśli chcesz dostosować się do różnic w systemie kompilacji. Jednak dokumentacja tej zmiennej nie opisuje możliwych wartości, które może zawierać, tak jak system-typerobi to dokumentacja zmiennej.