123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- (defgroup double nil
- "Remap keyboard, but get original by typing the same key twice."
- :group 'i18n)
- (defcustom double-map
- '((?\
- (?\' "\370" "'")
- (?\[ "\345" "[")
- (?\: "\306" ":")
- (?\" "\330" "\"")
- (?\{ "\305" "{"))
- "Alist of key translations activated by double mode.
- Each entry is a list with three elements:
- 1. The key activating the translation.
- 2. The string to be inserted when the key is pressed once.
- 3. The string to be inserted when the key is pressed twice."
- :group 'double
- :type '(repeat (list (character :tag "Key")
- (string :tag "Once")
- (string :tag "Twice"))))
- (defcustom double-prefix-only t
- "Non-nil means that Double mode mapping only works for prefix keys.
- That is, for any key `X' in `double-map', `X' alone will be mapped
- but not `C-u X' or `ESC X' since the X is not the prefix key."
- :group 'double
- :type 'boolean)
- (defvar double-last-event nil)
- (defun double-read-event (prompt)
-
- (if isearch-mode (isearch-update))
- (if prompt
- (prog2 (message "%s%c" prompt double-last-event)
- (read-event)
- (message ""))
- (read-event)))
- (global-set-key [ignore] 'ignore)
- (or (boundp 'isearch-mode-map)
- (load-library "isearch"))
- (define-key isearch-mode-map [ignore]
- (function (lambda () (interactive) (isearch-update))))
- (defun double-translate-key (prompt)
-
- (let ((key last-input-event))
- (cond (unread-command-events
-
- (vector key))
- ((and double-prefix-only
- (> (length (this-command-keys)) 1))
-
- (vector key))
- ((eq key 'magic-start)
-
- (let ((new (double-read-event prompt))
- (entry (assoc double-last-event double-map)))
- (force-window-update (selected-window))
- (if (eq new double-last-event)
- (progn
- (setq unread-command-events
- (append (make-list (1- (length (nth 1 entry)))
- 127)
- (nth 2 entry)
- '(magic-end)))
- (vector 127))
- (setq unread-command-events (list new))
- [ignore])))
- ((eq key 'magic-end)
-
- [ignore])
- (t
-
- (let ((exp (nth 1 (assoc key double-map))))
- (setq double-last-event key)
- (setq unread-command-events
- (append (substring exp 1) '(magic-start)))
- (vector (aref exp 0)))))))
- (define-minor-mode double-mode
- "Toggle special insertion on double keypresses (Double mode).
- With a prefix argument ARG, enable Double mode if ARG is
- positive, and disable it otherwise. If called from Lisp, enable
- the mode if ARG is omitted or nil.
- When Double mode is enabled, some keys will insert different
- strings when pressed twice. See `double-map' for details."
- :lighter " Double"
- :link '(emacs-commentary-link "double")
- (kill-local-variable 'key-translation-map)
- (when double-mode
-
-
- (make-local-variable 'key-translation-map)
- (let ((map (make-sparse-keymap)))
- (set-keymap-parent map key-translation-map)
- (setq key-translation-map map)
- (dolist (entry (append double-map '((magic-start) (magic-end))))
- (define-key map
- (vector (nth 0 entry)) 'double-translate-key)))))
- (provide 'double)
|