double.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; double.el --- support for keyboard remapping with double clicking
  2. ;; Copyright (C) 1994, 1997-1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  4. ;; Keywords: i18n
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This mode is intended for use with languages that adds a small
  18. ;; number of extra letters not available on the keyboard.
  19. ;;
  20. ;; Examples includes Scandinavian and German with an US keyboard.
  21. ;;
  22. ;; The idea is that certain keys are overloaded. When you press it
  23. ;; once it will insert one string, and when you press it twice the
  24. ;; string will be replaced by another. This can be used for mapping
  25. ;; keys on a US keyboard to generate characters according to the local
  26. ;; keyboard convention when pressed once, and according to US keyboard
  27. ;; convention when pressed twice.
  28. ;;
  29. ;; To use this mode, you must define the variable `double-map' and
  30. ;; then enable double mode with `M-x double-mode'. Read the
  31. ;; documentation for both of them.
  32. ;;
  33. ;; The default mapping is for getting Danish/Norwegian keyboard layout
  34. ;; using ISO Latin 1 on a US keyboard.
  35. ;;
  36. ;; Important node: While I would like to hear comments, bug reports,
  37. ;; suggestions, please do @strong{not} expect me to put other mappings
  38. ;; than the default into this file. There are billions and billions
  39. ;; of such mappings, and just supporting the most common would
  40. ;; increase the size of this nice small file manyfold.
  41. ;;; Code:
  42. (defgroup double nil
  43. "Remap keyboard, but get original by typing the same key twice."
  44. :group 'i18n)
  45. (defcustom double-map
  46. '((?\; "\346" ";")
  47. (?\' "\370" "'")
  48. (?\[ "\345" "[")
  49. (?\: "\306" ":")
  50. (?\" "\330" "\"")
  51. (?\{ "\305" "{"))
  52. "Alist of key translations activated by double mode.
  53. Each entry is a list with three elements:
  54. 1. The key activating the translation.
  55. 2. The string to be inserted when the key is pressed once.
  56. 3. The string to be inserted when the key is pressed twice."
  57. :group 'double
  58. :type '(repeat (list (character :tag "Key")
  59. (string :tag "Once")
  60. (string :tag "Twice"))))
  61. (defcustom double-prefix-only t
  62. "Non-nil means that Double mode mapping only works for prefix keys.
  63. That is, for any key `X' in `double-map', `X' alone will be mapped
  64. but not `C-u X' or `ESC X' since the X is not the prefix key."
  65. :group 'double
  66. :type 'boolean)
  67. ;;; Read Event
  68. (defvar double-last-event nil)
  69. ;; The last key that generated a double key event.
  70. (defun double-read-event (prompt)
  71. ;; Read an event
  72. (if isearch-mode (isearch-update))
  73. (if prompt
  74. (prog2 (message "%s%c" prompt double-last-event)
  75. (read-event)
  76. (message ""))
  77. (read-event)))
  78. (global-set-key [ignore] 'ignore)
  79. (or (boundp 'isearch-mode-map)
  80. (load-library "isearch"))
  81. (define-key isearch-mode-map [ignore]
  82. (function (lambda () (interactive) (isearch-update))))
  83. (defun double-translate-key (prompt)
  84. ;; Translate input events using double map.
  85. (let ((key last-input-event))
  86. (cond (unread-command-events
  87. ;; Artificial event, ignore it.
  88. (vector key))
  89. ((and double-prefix-only
  90. (> (length (this-command-keys)) 1))
  91. ;; This is not a prefix key, ignore it.
  92. (vector key))
  93. ((eq key 'magic-start)
  94. ;; End of generated event. See if he will repeat it...
  95. (let ((new (double-read-event prompt))
  96. (entry (assoc double-last-event double-map)))
  97. (force-window-update (selected-window))
  98. (if (eq new double-last-event)
  99. (progn
  100. (setq unread-command-events
  101. (append (make-list (1- (length (nth 1 entry)))
  102. 127)
  103. (nth 2 entry)
  104. '(magic-end)))
  105. (vector 127))
  106. (setq unread-command-events (list new))
  107. [ignore])))
  108. ((eq key 'magic-end)
  109. ;; End of double event. Ignore.
  110. [ignore])
  111. (t
  112. ;; New key.
  113. (let ((exp (nth 1 (assoc key double-map))))
  114. (setq double-last-event key)
  115. (setq unread-command-events
  116. (append (substring exp 1) '(magic-start)))
  117. (vector (aref exp 0)))))))
  118. ;;; Mode
  119. ;; This feature seemed useless and it confused describe-mode,
  120. ;; so I deleted it.
  121. ;; (defvar double-mode-name "Double")
  122. ;; ;; Name of current double mode.
  123. ;; (make-variable-buffer-local 'double-mode-name)
  124. ;;;###autoload
  125. (define-minor-mode double-mode
  126. "Toggle special insertion on double keypresses (Double mode).
  127. With a prefix argument ARG, enable Double mode if ARG is
  128. positive, and disable it otherwise. If called from Lisp, enable
  129. the mode if ARG is omitted or nil.
  130. When Double mode is enabled, some keys will insert different
  131. strings when pressed twice. See `double-map' for details."
  132. :lighter " Double"
  133. :link '(emacs-commentary-link "double")
  134. (kill-local-variable 'key-translation-map)
  135. (when double-mode
  136. ;; Set up key-translation-map as indicated by `double-map'.
  137. ;; XXX I don't think key-translation-map should be made local here. -- Lorentey
  138. (make-local-variable 'key-translation-map)
  139. (let ((map (make-sparse-keymap)))
  140. (set-keymap-parent map key-translation-map)
  141. (setq key-translation-map map)
  142. (dolist (entry (append double-map '((magic-start) (magic-end))))
  143. (define-key map
  144. (vector (nth 0 entry)) 'double-translate-key)))))
  145. (provide 'double)
  146. ;;; double.el ends here