Last edit
Summary: Material on reading the registry from ElispCookbook
Added:
> === Read Windows Registry ===
> This fn reads a registry key - retrieves all the values and the names of all the subkeys.
> (defun w32reg-read-key (key)
> "Read all values and subkeys for a key path in the Windows registry.
> The return value is a list (KEYNAME VALUES SUBKEYS). KEYNAME is
> the name of the key. VALUES is a list of values, each one
> following this form: (NAME TYPE VALUE) where each are strings,
> and the TYPE is like \"REG_DWORD\" and so on.
> SUBKEYS is a simple list of strings.
> If the path does not exist, it returns nil.
> "
> (let ((reg.exe (concat (getenv "windir") "\\system32\\reg.exe"))
> keyname values subkeys (state 0))
> (with-temp-buffer
> (insert (shell-command-to-string
> (concat reg.exe " query " "\"" key "\"")))
> (while (not (= (point-min) (point-max)))
> (goto-char (point-min))
> (let ((start (point))
> (end (line-end-position))
> line this-value)
> (setq line (buffer-substring-no-properties start end))
> (delete-region start end)
> (delete-char 1) ;; NL
> (cond
> ((string/starts-with line "ERROR:")
> nil)
> ((string= "" line)
> (setq state (1+ state)))
> ((not keyname)
> (setq keyname line
> state 1))
> ((eq state 1)
> (let ((parts (split-string line nil t)))
> (setq this-value (mapconcat 'identity (cddr parts) " "))
> ;; convert to integer, maybe
> (if (string= (nth 1 parts) "REG_DWORD")
> (setq this-value
> (string-to-number (substring this-value 2))))
> (setq values (cons (list (nth 0 parts)
> (nth 1 parts)
> this-value) values))))
> ((eq state 2)
> (setq subkeys (cons
> (if (string/starts-with line keyname)
> (substring line (1+ (length keyname)))
> line)
> subkeys)))
> (t nil)))))
> (and keyname
> (list keyname values subkeys))))
> === Read one Registry Value ===
> This fn depends on the previous one, shown above.
> (defun w32reg-read-value (key value)
> "Read a value from a key location in the registry. The result
> is a list like (NAME TYPE VALUE), where the first two items are
> strings. TYPE is like \"REG_DWORD\", \"REG_SZ\", \"REG_BINARY\",
> and so on. If TYPE is \"REG_DWORD\", then the VALUE is a number.
> If the key value does not exist, it returns nil.
> "
> (let ((all (w32reg-read-key key))
> (c 0) L n r values)
> (and all
> (setq values (nth 1 all)
> L (length values))
> (while (and (not r) (< c L))
> (setq n (nth c values)
> c (1+ c))
> (if (string= value (car n))
> (setq r n))))
> r))
> === Read IE Proxy Configuration ===
> This fn depends on the prior fn that allows reading a specific value from the Windows registry.
> (defun w32reg-get-ie-proxy-config ()
> "Return the Proxy Server settings configured for IE, if enabled.
> The result is a list of cons cells; like this:
> ((\"http\" . \"127.0.0.1:8888\")
> (\"https\" . \"127.0.0.1:8888\"))
> ...which is suitable for use with (setq url-proxy-services ...)
> "
> (let* ((rpath "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
> (enabled (w32reg-read-value rpath "ProxyEnable"))
> r)
> (if (and enabled
> (numberp (nth 2 enabled))
> (> (nth 2 enabled) 0))
> (let ((proxy (w32reg-read-value rpath "ProxyServer")))
> (mapcar `(lambda (elt)
> (let ((x (split-string elt "=" t)))
> (cons (car x) (cadr x))))
> (split-string (nth 2 proxy) ";" t))))))
This page will cover the GNU Emacs and the Windows Registry: using the Registry to customize Emacs. It is set up as a FAQ. You may want to jump straight to an example of setting the registry
Emacs.Alpha
Emacs.AutoRaiseLower
Emacs.Background
Emacs.BackgroundMode
Emacs.BitmapIcon
Emacs.BorderColor
Emacs.BorderWidth
Emacs.BufferPredicate
Emacs.CursorBlink
Emacs.CursorHeight
Emacs.CursorType
Emacs.Face.AttributeBackground
Emacs.Face.AttributeBackgroundPixmap
Emacs.Face.AttributeBold
Emacs.Face.AttributeBox
Emacs.Face.AttributeFamily
Emacs.Face.AttributeFont
Emacs.Face.AttributeForeground
Emacs.Face.AttributeHeight
Emacs.Face.AttributeInherit
Emacs.Face.AttributeInverse
Emacs.Face.AttributeItalic
Emacs.Face.AttributeOverline
Emacs.Face.AttributeSlant
Emacs.Face.AttributeStipple
Emacs.Face.AttributeStrikeThrough
Emacs.Face.AttributeUnderline
Emacs.Face.AttributeWeight
Emacs.Face.AttributeWidth
Emacs.Font
Emacs.Foreground
Emacs.Fullscreen
Emacs.Geometry
Emacs.LineSpacing
Emacs.MenuBar
Emacs.Minibuffer
Emacs.Name
Emacs.ReverseVideo
Emacs.ScreenGamma
Emacs.ScrollBarWidth
Emacs.Title
Emacs.ToolBar
Emacs.internalBorder
Emacs.internalBorderWidth MEADOW.Alpha
MEADOW.AutoRaiseLower
MEADOW.Background
MEADOW.BackgroundMode
MEADOW.BitmapIcon
MEADOW.BorderColor
MEADOW.BorderWidth
MEADOW.BufferPredicate
MEADOW.CursorBlink
MEADOW.CursorHeight
MEADOW.CursorType
MEADOW.Face.AttributeBackground
MEADOW.Face.AttributeBackgroundPixmap
MEADOW.Face.AttributeBold
MEADOW.Face.AttributeBox
MEADOW.Face.AttributeFamily
MEADOW.Face.AttributeFont
MEADOW.Face.AttributeForeground
MEADOW.Face.AttributeHeight
MEADOW.Face.AttributeInherit
MEADOW.Face.AttributeInverse
MEADOW.Face.AttributeItalic
MEADOW.Face.AttributeOverline
MEADOW.Face.AttributeSlant
MEADOW.Face.AttributeStipple
MEADOW.Face.AttributeStrikeThrough
MEADOW.Face.AttributeUnderline
MEADOW.Face.AttributeWeight
MEADOW.Face.AttributeWidth
MEADOW.Font
MEADOW.Foreground
MEADOW.Fullscreen
MEADOW.Geometry
MEADOW.IME-Font
MEADOW.LineSpacing
MEADOW.MenuBar
MEADOW.Minibuffer
MEADOW.Name
MEADOW.ReverseVideo
MEADOW.ScreenGamma
MEADOW.ScrollBarWidth
MEADOW.Title
MEADOW.ToolBar
MEADOW.internalBorder
MEADOW.internalBorderWidth Emacs.Background: black
Emacs.Foreground: white
Emacs.Font: -*-Lucida Console-normal-r-*-*-11-82-96-96-c-*-iso8859-1
Emacs.Geometry: 90x60 REGEDIT4
[HKEY_CURRENT_USER\SOFTWARE\GNU\Emacs]
"Emacs.Background"="black"
"Emacs.Foreground"="white"
"Emacs.Font"="-*-Lucida Console-normal-r-*-*-11-82-96-96-c-*-iso8859-1"
"Emacs.Geometry"="90x60"This fn reads a registry key - retrieves all the values and the names of all the subkeys.
(defun w32reg-read-key (key)
"Read all values and subkeys for a key path in the Windows registry.
The return value is a list (KEYNAME VALUES SUBKEYS). KEYNAME is
the name of the key. VALUES is a list of values, each one
following this form: (NAME TYPE VALUE) where each are strings,
and the TYPE is like \"REG_DWORD\" and so on.SUBKEYS is a simple list of strings.
If the path does not exist, it returns nil.
"
(let ((reg.exe (concat (getenv "windir") "\\system32\\reg.exe"))
keyname values subkeys (state 0)) (with-temp-buffer
(insert (shell-command-to-string
(concat reg.exe " query " "\"" key "\""))) (while (not (= (point-min) (point-max)))
(goto-char (point-min))
(let ((start (point))
(end (line-end-position))
line this-value)
(setq line (buffer-substring-no-properties start end))
(delete-region start end)
(delete-char 1) ;; NL (cond
((string/starts-with line "ERROR:")
nil) ((string= "" line)
(setq state (1+ state))) ((not keyname)
(setq keyname line
state 1)) ((eq state 1)
(let ((parts (split-string line nil t)))
(setq this-value (mapconcat 'identity (cddr parts) " ")) ;; convert to integer, maybe
(if (string= (nth 1 parts) "REG_DWORD")
(setq this-value
(string-to-number (substring this-value 2)))) (setq values (cons (list (nth 0 parts)
(nth 1 parts)
this-value) values))))
((eq state 2)
(setq subkeys (cons
(if (string/starts-with line keyname)
(substring line (1+ (length keyname)))
line)
subkeys)))
(t nil))))) (and keyname
(list keyname values subkeys))))This fn depends on the previous one, shown above.
(defun w32reg-read-value (key value)
"Read a value from a key location in the registry. The result
is a list like (NAME TYPE VALUE), where the first two items are
strings. TYPE is like \"REG_DWORD\", \"REG_SZ\", \"REG_BINARY\",
and so on. If TYPE is \"REG_DWORD\", then the VALUE is a number. If the key value does not exist, it returns nil.
"
(let ((all (w32reg-read-key key))
(c 0) L n r values)
(and all
(setq values (nth 1 all)
L (length values))
(while (and (not r) (< c L))
(setq n (nth c values)
c (1+ c))
(if (string= value (car n))
(setq r n))))
r))This fn depends on the prior fn that allows reading a specific value from the Windows registry.
(defun w32reg-get-ie-proxy-config ()
"Return the Proxy Server settings configured for IE, if enabled.
The result is a list of cons cells; like this: ((\"http\" . \"127.0.0.1:8888\")
(\"https\" . \"127.0.0.1:8888\"))...which is suitable for use with (setq url-proxy-services ...)
"
(let* ((rpath "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
(enabled (w32reg-read-value rpath "ProxyEnable"))
r)
(if (and enabled
(numberp (nth 2 enabled))
(> (nth 2 enabled) 0))
(let ((proxy (w32reg-read-value rpath "ProxyServer")))
(mapcar `(lambda (elt)
(let ((x (split-string elt "=" t)))
(cons (car x) (cadr x))))
(split-string (nth 2 proxy) ";" t))))))