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