Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.58 KB | None | 0 0
  1. ;; -*- lexical-binding: t; -*-
  2.  
  3. (require 'ido)
  4.  
  5. (defun ido-read-char-sort-predicate (a b)
  6.   "Order character names first by increasing length, then by lexicographic order."
  7.   (let ((alen (length a))
  8.         (blen (length b)))
  9.     (or (< alen blen) (and (= alen blen) (string-lessp a b)))))
  10.  
  11. (defconst ucs-names-is-alist (listp (ucs-names)))
  12.  
  13. (let (completions)
  14.   (defun ido-read-char-completions ()
  15.     (or completions
  16.         (setq completions
  17.               (sort (if ucs-names-is-alist
  18.                         (cl-delete-if (lambda (s) (= ?< (aref s 0))) (mapcar 'car (ucs-names)))
  19.                       (let ((names nil))
  20.                         (maphash (lambda (k _) (if (/= ?< (aref k 0)) (push k names))) (ucs-names))
  21.                         names))
  22.                     'ido-read-char-sort-predicate)))))
  23.  
  24. (defun ido-read-char-by-name (prompt)
  25.   "Replacement for read-char-by-name that uses ido to read character names."
  26.   (let* ((completion-ignore-case t)
  27.          (completions (ido-read-char-completions))
  28.          (ido-enable-flex-matching nil)
  29.      (input (ido-completing-read prompt completions)))
  30.     (cond
  31.      ((string-match-p "^[0-9a-fA-F]+$" input)
  32.       (string-to-number input 16))
  33.      ((string-match-p "^#" input)
  34.       (read input))
  35.      (t
  36.       (if ucs-names-is-alist
  37.           (cdr (assoc-string input (ucs-names) t))
  38.         (gethash input (ucs-names)))))))
  39.  
  40. (defun interactively-read-unicode-char (&rest _ignore)
  41.   (interactive (list (ido-read-char-by-name "Unicode (name or hex): "))))
  42.  
  43. (advice-add 'insert-char :before #'interactively-read-unicode-char)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement