Monthly Archives: January 2012

Unicode table in EMACS

Unicode table in EMACS

Motivation

Often when writing, I need to insert the degrees sign – for instance 25.3°C. Since it is not on my keyboard, I used to look it up on wikipedia.

Later I found some elisp code for dumping an ASCII table, but I still used valuable seconds looking for the sign. This made me discover the get-char-code-property function which adds a description. Now I could just do an I-search for “deg” and I would get to my invaluable degree-sign character.

But now I wanted the whole thing: the entire unicode table…

To the Point

;; show Unicode table
;; inspired by http://www.chrislott.org/geek/emacs/dotemacs.html
(defun unicode-table ()
  "Print the utf16 table. Based on a defun by Alex Schroeder <asc@bsiag.com>"
  (interactive)
  (switch-to-buffer "*Unicode Table*")
  (erase-buffer)
  (insert (format "Unicode characters:\n"))

  ;; Generate list of all unicode code points
  ;; See http://en.wikipedia.org/wiki/Unicode_plane#Overview
  (setq code-points (append (number-sequence  ?\x0000  ?\xffff)
                               (number-sequence ?\x10000 ?\x1ffff)
                               (number-sequence ?\x20000 ?\x2ffff)
                               (number-sequence ?\xe0000 ?\xeffff)))

  ;; Iterate code points
  (dolist (code-point code-points)
                      ;; Get description from emacs internals
                      (let ((description (get-char-code-property code-point
                                                                 'name)))
                        ;; Insert code-point, character and description
                        (insert (format "%4d 0x%02X %c %s\n"
                                        code-point
                                        code-point
                                        code-point
                                        description))))
  ;; Jump to beginning of buffer
  (beginning-of-buffer))

Calling this function with M-x unicode-table reveals

Unicode characters:
 0 0x00 ^@ <control>
 1 0x01  <control>
 2 0x02  <control>
 3 0x03  <control>
 4 0x04  <control>
 5 0x05  <control>
 6 0x06  <control>
 7 0x07  <control>
 8 0x08  <control>
 9 0x09 <control>
 10 0x0A
 <control>
 11 0x0B  <control>
 12 0x0C  <control>

I hope that this is useful. If you like it, feel free to leave a comment.

Update me when site is updated