| 1 | ;;; double.el --- support for keyboard remapping with double clicking |
| 2 | |
| 3 | ;; Copyright (C) 1994, 1997-1998, 2001-2014 Free Software Foundation, |
| 4 | ;; Inc. |
| 5 | |
| 6 | ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> |
| 7 | ;; Keywords: i18n |
| 8 | |
| 9 | ;; This file is part of GNU Emacs. |
| 10 | |
| 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. |
| 15 | |
| 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. |
| 20 | |
| 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/>. |
| 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. |
| 28 | ;; |
| 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 |
| 36 | ;; convention when pressed twice. |
| 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 | ;; |
| 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. |
| 50 | |
| 51 | ;;; Code: |
| 52 | |
| 53 | (defgroup double nil |
| 54 | "Remap keyboard, but get original by typing the same key twice." |
| 55 | :group 'i18n) |
| 56 | |
| 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. |
| 65 | |
| 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")))) |
| 74 | |
| 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) |
| 81 | |
| 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 | |
| 96 | (global-set-key [ignore] 'ignore) |
| 97 | |
| 98 | (or (boundp 'isearch-mode-map) |
| 99 | (load-library "isearch")) |
| 100 | |
| 101 | (define-key isearch-mode-map [ignore] |
| 102 | (function (lambda () (interactive) (isearch-update)))) |
| 103 | |
| 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 | (vector 127)) |
| 127 | (setq unread-command-events (list new)) |
| 128 | [ignore]))) |
| 129 | ((eq key 'magic-end) |
| 130 | ;; End of double event. Ignore. |
| 131 | [ignore]) |
| 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 | |
| 140 | ;;; Mode |
| 141 | |
| 142 | ;; This feature seemed useless and it confused describe-mode, |
| 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) |
| 147 | |
| 148 | ;;;###autoload |
| 149 | (define-minor-mode double-mode |
| 150 | "Toggle special insertion on double keypresses (Double mode). |
| 151 | With a prefix argument ARG, enable Double mode if ARG is |
| 152 | positive, and disable it otherwise. If called from Lisp, enable |
| 153 | the mode if ARG is omitted or nil. |
| 154 | |
| 155 | When Double mode is enabled, some keys will insert different |
| 156 | strings when pressed twice. See `double-map' for details." |
| 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))))) |
| 170 | |
| 171 | (provide 'double) |
| 172 | |
| 173 | ;;; double.el ends here |