2008-09-17 describe-hash

One problem with hash tables is that they don’t have a print representation. Thus, if you’re using rcircColoredNicks and want to look at the variable value of rcirc-color-mapping you’ll get the following:

rcircColoredNicks

rcirc-color-mapping is a variable defined in `rcirc-color.el'.
Its value is
#<hash-table 'equal nil 21/65 0x1d70b80>

Documentation:
Hash-map mapping nicks to color names.

Not good!

That’s why I’m using the following piece of code to look at hash tables:

(defun describe-hash (variable &optional buffer)
  "Display the full documentation of VARIABLE (a symbol).
    Returns the documentation as a string, also.
    If VARIABLE has a buffer-local value in BUFFER (default to the current buffer),
    it is displayed along with the global value."
  (interactive
   (let ((v (variable-at-point))
	 (enable-recursive-minibuffers t)
	 val)
     (setq val (completing-read
		(if (and (symbolp v)
			 (hash-table-p (symbol-value v)))
		    (format
		     "Describe hash-map (default %s): " v)
		  "Describe hash-map: ")
		obarray
		(lambda (atom) (and (boundp atom)
				    (hash-table-p (symbol-value atom))))
		t nil nil
		(if (hash-table-p v) (symbol-name v))))
     (list (if (equal val "")
	       v (intern val)))))
  (with-output-to-temp-buffer (help-buffer)
    (maphash (lambda (key value)
	       (pp key)
	       (princ " => ")
	       (pp value)
	       (terpri))
	     (symbol-value variable))))

If you use it on the same variable again, you’ll get a `*Help*` buffer showing the following:

"NickServ" => (foreground-color . "deepskyblue1")

"alephnull" => (foreground-color . "yellow4")

"fsbot" => (foreground-color . "springgreen2")

"delYsid" => (foreground-color . "springgreen3")

"johnw" => (foreground-color . "turquoise2")

etc.

This code is also available on the EmacsWiki HashMap page.

EmacsWiki

​#Emacs