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