Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / double.el
CommitLineData
e8af40ee 1;;; double.el --- support for keyboard remapping with double clicking
fc429cfe 2
73b0cd50 3;; Copyright (C) 1994, 1997-1998, 2001-2011 Free Software Foundation, Inc.
fc429cfe 4
3efc86ef 5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
fc429cfe 6;; Keywords: i18n
fc429cfe 7
70d949ea
RS
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
fc429cfe 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
70d949ea
RS
14
15;; GNU Emacs is distributed in the hope that it will be useful,
fc429cfe
RS
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.
70d949ea 19
fc429cfe 20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fc429cfe
RS
22
23;;; Commentary:
24
25;; This mode is intended for use with languages that adds a small
26;; number of extra letters not available on the keyboard.
71296446 27;;
fc429cfe
RS
28;; Examples includes Scandinavian and German with an US keyboard.
29;;
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
71296446 35;; convention when pressed twice.
fc429cfe
RS
36;;
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.
40;;
41;; The default mapping is for getting Danish/Norwegian keyboard layout
42;; using ISO Latin 1 on a US keyboard.
43;;
a7acbbe4 44;; Important node: While I would like to hear comments, bug reports,
fc429cfe 45;; suggestions, please do @strong{not} expect me to put other mappings
a7acbbe4 46;; than the default into this file. There are billions and billions
fc429cfe
RS
47;; of such mappings, and just supporting the most common would
48;; increase the size of this nice small file manyfold.
49
fc429cfe
RS
50;;; Code:
51
7b33b170
KH
52(defgroup double nil
53 "Remap keyboard, but get original by typing the same key twice."
54 :group 'i18n)
55
56(defcustom double-map
fc429cfe
RS
57 '((?\; "\346" ";")
58 (?\' "\370" "'")
59 (?\[ "\345" "[")
60 (?\: "\306" ":")
61 (?\" "\330" "\"")
62 (?\{ "\305" "{"))
63 "Alist of key translations activated by double mode.
64
65Each entry is a list with three elements:
661. The key activating the translation.
672. The string to be inserted when the key is pressed once.
7b33b170
KH
683. 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"))))
fc429cfe 73
7b33b170 74(defcustom double-prefix-only t
c46f60a8 75 "Non-nil means that Double mode mapping only works for prefix keys.
8a3663f3 76That is, for any key `X' in `double-map', `X' alone will be mapped
7b33b170
KH
77but not `C-u X' or `ESC X' since the X is not the prefix key."
78 :group 'double
79 :type 'boolean)
5867a409 80
fc429cfe
RS
81;;; Read Event
82
83(defvar double-last-event nil)
84;; The last key that generated a double key event.
85
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)))
94
c46f60a8 95(global-set-key [ignore] 'ignore)
fc429cfe
RS
96
97(or (boundp 'isearch-mode-map)
98 (load-library "isearch"))
99
71296446 100(define-key isearch-mode-map [ignore]
fc429cfe
RS
101 (function (lambda () (interactive) (isearch-update))))
102
103(defun double-translate-key (prompt)
104 ;; Translate input events using double map.
1e4bd40d 105 (let ((key last-input-event))
fc429cfe
RS
106 (cond (unread-command-events
107 ;; Artificial event, ignore it.
108 (vector key))
5867a409
RS
109 ((and double-prefix-only
110 (> (length (this-command-keys)) 1))
111 ;; This is not a prefix key, ignore it.
112 (vector key))
fc429cfe
RS
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)))
18d2b950 117 (force-window-update (selected-window))
fc429cfe 118 (if (eq new double-last-event)
71296446 119 (progn
fc429cfe
RS
120 (setq unread-command-events
121 (append (make-list (1- (length (nth 1 entry)))
f55e5c6b 122 127)
fc429cfe
RS
123 (nth 2 entry)
124 '(magic-end)))
125 (vector 127))
126 (setq unread-command-events (list new))
f55e5c6b 127 [ignore])))
fc429cfe
RS
128 ((eq key 'magic-end)
129 ;; End of double event. Ignore.
f55e5c6b 130 [ignore])
fc429cfe
RS
131 (t
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)))))))
138
fc429cfe
RS
139;;; Mode
140
48a0ae63 141;; This feature seemed useless and it confused describe-mode,
c46f60a8
SM
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)
fc429cfe
RS
146
147;;;###autoload
c46f60a8 148(define-minor-mode double-mode
48a0ae63 149 "Toggle Double mode.
4837b516
GM
150With prefix argument ARG, turn Double mode on if ARG is positive, otherwise
151turn it off.
fc429cfe 152
e6f1da19
KH
153When Double mode is on, some keys will insert different strings
154when pressed twice. See variable `double-map' for details."
c46f60a8
SM
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)))))
fc429cfe
RS
168
169(provide 'double)
170
171;;; double.el ends here