Checking Windows version with Emacs Lisp

elisp code example for programmatically checking the version of Microsoft Windows on which Emacs is running:

(when (eq system-type 'windows-nt)
  (print "Hello, MS Windows.")
  (let ((winversion
         (when (string-match (rx "nt"
                                 (group (+ digit))
                                 (group (+ digit))
                                 (+ digit)
            (string-to-number (match-string 1 system-configuration))
            (string-to-number (match-string 2 system-configuration))))))
    (when (null winversion)
      (warn "(8qzv4lwb) Strange, some unexpected form of system-configuration."))
    (when (version-list-<= '(5 1) winversion)
      (print "Hello, MS Windows XP or higher."))
    (when (version-list-<= '(6 1) winversion)
      (print "Hello, MS Windows 7+ or Windows Server 2008 R2+."))
    (when (version-list-<= '(6 2) winversion)
      (print "Hello, MS Windows 8+ or Windows Server 2012+."))))

If you want to enable something only for Windows XP and higher, you can replace the line:

(print "Hello, MS Windows XP or higher.")

with your own code.

You might also want to look into feature detection instead of checking the version of Windows.

Further reading:

  • To see how the string-match, rx, match-string idiom works, visit the relevant link in Living with Emacs Lisp, if any.
  • For programmatically checking the OS instead (or Emacs version), visit this article by Xah Lee.
  • For checking other system stuff, visit: OS Environment (in the elisp manual).
This entry was posted in Emacs, Lisp and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s