-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-insert-unicode.el
94 lines (72 loc) · 3.06 KB
/
quick-insert-unicode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;; quick-insert-unicode.el --- Quick insert unicode characters -*- lexical-binding: t; -*-
;; Copyright (C) 2022 qdzhang
;; Copyright (C) 2015 Emanuel Evans
;; Author: qdzhang <qdzhangcn@gmail.com>
;; Maintainer: qdzhang <qdzhangcn@gmail.com>
;; URL: https://github.com/qdzhang/quick-insert-unicode
;; Version: 0.0.1
;; Package-Requires: ((emacs "26.1"))
;; Keywords: tools, wp
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; quick-insert-unicode
;; ====================
;; A completing-read command for looking up unicode characters by name
;; 😉. Modified from <https://github.com/qdzhang/ivy-unicode> and
;; <https://github.com/bomgar/helm-unicode>.
;; Installation
;; ~~~~~~~~~~~~
;; Clone this repository, and put `quick-insert-unicode.el' file to your
;; `load-path', then require it.
;; ,----
;; | (require 'quick-insert-unicode)
;; `----
;; Usage
;; ~~~~~
;; `quick-insert-unicode'
;; The main function to use, bind it to a key whatever you like. This
;; function use `completing-read' to select unicode characters, and
;; insert the selected item to current posotion.
;;
;;; Code:
(defvar quick-insert-unicode-names nil
"Internal cache variable for unicode characters.
Should not be changed by the user.")
(defun quick-insert-unicode-format-char-pair (char-pair)
"Formats a CHAR-PAIR for quick-insert unicode search."
(let ((name (car char-pair))
(symbol (cdr char-pair)))
(format "%s %c" name symbol)))
(defun quick-insert-unicode-build-candidates ()
"Builds the candidate list."
(let ((unames (if (hash-table-p (ucs-names))
(mapcar (lambda (elem) `(,elem . ,(gethash elem (ucs-names)))) (hash-table-keys (ucs-names)))
(ucs-names))))
(sort
(mapcar 'quick-insert-unicode-format-char-pair unames)
#'string-lessp)))
(defun quick-insert-unicode-insert-char (candidate)
"Insert CANDIDATE into the main buffer."
(insert (substring candidate -1)))
;;;###autoload
(defun quick-insert-unicode (arg)
"Quick insert unicode characters.
Use `completing-read' for looking up unicode characters by name, and insert the
selected item. With prefix ARG, reinitialize the cache."
(interactive "P")
(when arg (setq quick-insert-unicode-names nil))
((lambda (x) (quick-insert-unicode-insert-char x))
(completing-read "Unicode-char: " (quick-insert-unicode-build-candidates))))
(provide 'quick-insert-unicode)
;;; quick-insert-unicode.el ends here