uni-input.el 4.1 KB

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