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