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