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