Commit | Line | Data |
---|---|---|
7115232b DL |
1 | ;;; uni-input.el --- Hex Unicode input method |
2 | ||
95df8112 | 3 | ;; Copyright (C) 2001-2011 Free Software Foundation, Inc. |
5df4f04c | 4 | ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
698218a2 KH |
5 | ;; National Institute of Advanced Industrial Science and Technology (AIST) |
6 | ;; Registration Number H14PRO021 | |
7115232b DL |
7 | |
8 | ;; Author: Dave Love <fx@gnu.org> | |
9 | ;; Keywords: i18n | |
10 | ||
041f4d74 PJ |
11 | ;; This file is part of GNU Emacs. |
12 | ||
3d544458 | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7115232b | 14 | ;; it under the terms of the GNU General Public License as published by |
3d544458 GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
7115232b | 17 | |
3d544458 | 18 | ;; GNU Emacs is distributed in the hope that it will be useful, |
7115232b DL |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
3d544458 | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
7115232b DL |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; Provides an input method for entering characters by hex unicode in | |
29 | ;; the form `uxxxx', similarly to the Yudit editor. | |
30 | ||
31 | ;; This is not really a Quail method, but uses some Quail functions. | |
32 | ;; There is probably A Better Way. | |
33 | ||
d6e5e4db DL |
34 | ;; You can get a similar effect by using C-q with |
35 | ;; `read-quoted-char-radix' set to 16. | |
7115232b | 36 | |
5d9c1e7a DL |
37 | ;; Note that this only allows you to enter BMP values unless someone |
38 | ;; extends it to use variable numbers of digits. | |
39 | ||
7115232b DL |
40 | ;;; Code: |
41 | ||
42 | (require 'quail) | |
43 | ||
ad7d24c4 KH |
44 | (defun ucs-input-insert-char (char) |
45 | (insert char) | |
46 | (move-overlay quail-overlay (overlay-start quail-overlay) (point))) | |
47 | ||
7115232b DL |
48 | (defun ucs-input-method (key) |
49 | (if (or buffer-read-only | |
50 | (and (/= key ?U) (/= key ?u))) | |
51 | (list key) | |
52 | (quail-setup-overlays nil) | |
ad7d24c4 | 53 | (ucs-input-insert-char key) |
7115232b DL |
54 | (let ((modified-p (buffer-modified-p)) |
55 | (buffer-undo-list t) | |
56 | (input-method-function nil) | |
57 | (echo-keystrokes 0) | |
58 | (help-char nil) | |
59 | (events (list key)) | |
60 | (str " ")) | |
61 | (unwind-protect | |
62 | (catch 'non-digit | |
63 | (progn | |
64 | (dotimes (i 4) | |
65 | (let ((seq (read-key-sequence nil)) | |
66 | key) | |
67 | (if (and (stringp seq) | |
68 | (= 1 (length seq)) | |
69 | (setq key (aref seq 0)) | |
70 | (memq key '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?a | |
d6e5e4db | 71 | ?b ?c ?d ?e ?f ?A ?B ?C ?D ?E ?F))) |
7115232b DL |
72 | (progn |
73 | (push key events) | |
ad7d24c4 | 74 | (ucs-input-insert-char key)) |
7115232b DL |
75 | (quail-delete-region) |
76 | (throw 'non-digit (append (reverse events) | |
77 | (listify-key-sequence seq)))))) | |
78 | (quail-delete-region) | |
d6e5e4db DL |
79 | (let ((n (string-to-number (apply 'string |
80 | (cdr (nreverse events))) | |
81 | 16))) | |
82 | (if (characterp n) | |
83 | (list n))))) | |
7115232b DL |
84 | (quail-delete-overlays) |
85 | (set-buffer-modified-p modified-p) | |
86 | (run-hooks 'input-method-after-insert-chunk-hook))))) | |
87 | ||
88 | (defun ucs-input-activate (&optional arg) | |
89 | "Activate UCS input method. | |
90 | With arg, activate UCS input method if and only if arg is positive. | |
91 | ||
92 | While this input method is active, the variable | |
93 | `input-method-function' is bound to the function `ucs-input-method'." | |
94 | (if (and arg | |
95 | (< (prefix-numeric-value arg) 0)) | |
96 | (unwind-protect | |
97 | (progn | |
ad7d24c4 | 98 | (quail-hide-guidance) |
7115232b DL |
99 | (quail-delete-overlays) |
100 | (setq describe-current-input-method-function nil)) | |
101 | (kill-local-variable 'input-method-function)) | |
102 | (setq inactivate-current-input-method-function 'ucs-input-inactivate) | |
103 | (setq describe-current-input-method-function 'ucs-input-help) | |
104 | (quail-delete-overlays) | |
105 | (if (eq (selected-window) (minibuffer-window)) | |
106 | (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)) | |
7115232b DL |
107 | (set (make-local-variable 'input-method-function) |
108 | 'ucs-input-method))) | |
109 | ||
110 | (defun ucs-input-inactivate () | |
111 | "Inactivate UCS input method." | |
112 | (interactive) | |
113 | (ucs-input-activate -1)) | |
114 | ||
115 | (defun ucs-input-help () | |
116 | (interactive) | |
117 | (with-output-to-temp-buffer "*Help*" | |
118 | (princ "\ | |
e4208ff7 | 119 | Input method: ucs (mode line indicator:U+) |
7115232b DL |
120 | |
121 | Input as Unicode: U<hex> or u<hex>, where <hex> is a four-digit hex number."))) | |
122 | ||
ad7d24c4 KH |
123 | ;; The file ../leim-ext.el contains the following call. |
124 | ;; (register-input-method "ucs" "UTF-8" 'ucs-input-activate "U+" | |
125 | ;; "Unicode input as hex in the form Uxxxx.") | |
7115232b DL |
126 | |
127 | (provide 'uni-input) | |
128 | ||
129 | ;;; uni-input.el ends here |