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