1 ;;; rebind.el --- Rebind keys
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: 2008-01-20T12:04:37+0100 Sun
11 ;; Features that might be required by this library:
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;; See `rebind-keys-mode' for information.
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This program is free software; you can redistribute it and/or
29 ;; modify it under the terms of the GNU General Public License as
30 ;; published by the Free Software Foundation; either version 2, or
31 ;; (at your option) any later version.
33 ;; This program is distributed in the hope that it will be useful,
34 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
35 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 ;; General Public License for more details.
38 ;; You should have received a copy of the GNU General Public License
39 ;; along with this program; see the file COPYING. If not, write to
40 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
41 ;; Floor, Boston, MA 02110-1301, USA.
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 (eval-when-compile (require 'new-key-seq-widget nil t))
48 (eval-when-compile (require 'ourcomments-widgets nil t))
51 (defun rebind-toggle-first-modifier (orig-key-seq mod)
52 (let* ((first (elt orig-key-seq 0))
53 (new-key-seq (copy-sequence orig-key-seq)))
54 (setq first (if (memq mod first)
57 (aset new-key-seq 0 first)
59 ;; (rebind-toggle-first-modifier (key-description-to-vector "C-c a") 'shift)
60 ;; (rebind-toggle-first-modifier (key-description-to-vector "C-S-c a") 'shift)
62 (defvar widget-commandp-prompt-value-history nil)
66 "Customizaton group for `rebind-keys-mode'."
69 :group 'editing-basics
72 ;; (customize-option-other-window 'rebind-keys)
73 ;; (Fetched key bindings from http://www.davidco.com/tips_tools/tip45.html)
74 (defcustom rebind-keys
76 ("MS Windows - often used key bindings" t
80 "C-a on w32 normally means 'select all'. In Emacs it is `beginning-of-line'."
83 ourcomments-mark-whole-buffer-or-field)
86 "C-o on w32 normally means 'open file'. In Emacs it is `open-line'."
92 "C-f is commonly search on w32. In Emacs it is `forward-char'."
98 "C-s is normally 'save file' on w32. In Emacs it is `isearch-forward'."
104 "C-w is often something like kill-buffer on w32. In Emacs it is `kill-region'."
110 "C-p is nearly always print on w32. In Emacs it is `previous-line'."
116 "HOME normally stays in a field. By default it does not do that in Emacs."
119 ourcomments-move-beginning-of-line)
122 "C-+ often increases font size (in web browsers for example)."
128 "C-- often decreases font size (in web browsers for example)."
134 "C-0 often resets font size (in web browsers for example)."
139 "Normal Emacs keys that are remapped to follow some other standard.
140 The purpose of this variable is to make it easy to switch between
141 Emacs key bindings and other standards.
143 The new bindings are made in the global minor mode
144 `rebind-keys-mode' and will only have effect when this mode is
147 *Note:* You can only move functions bound in the global key map
149 *Note:* To get CUA keys you should turn on option `cua-mode'.
150 *Note:* To get vi key bindings call function `viper-mode'.
151 *Note:* `text-scale-adjust' already have default key bindings."
154 (string :tag "For what")
155 (boolean :tag "Group on/off")
158 (key-sequence :tag "Emacs key binding")
159 (string :tag "Why rebind")
160 (boolean :tag "Rebinding on/off")
161 (choice :tag "Move original by"
162 (const :tag "Don't put it on any new binding" nil)
163 (choice :tag "Add key binding modifier"
167 (key-sequence :tag "New binding for original function"))
168 (command :tag "New command on above key"))
170 :set (lambda (sym val)
171 (set-default sym val)
172 (when (featurep 'rebind)
173 (rebind-update-keymap)))
176 (defvar rebind-keys-mode-map nil)
178 (defvar rebind--emul-keymap-alist nil)
180 ;;(rebind-update-keymap)
181 (defun rebind-update-keymap ()
182 (let ((m (make-sparse-keymap)))
183 (dolist (group rebind-keys)
185 (dolist (v (nth 2 group))
186 (let* ((orig-key (nth 0 v))
189 (new-choice (nth 3 v))
191 (orig-fun (lookup-key global-map orig-key))
195 (if (memq new-choice '(meta control shift))
196 (setq new-key (rebind-toggle-first-modifier orig-key new-choice))
197 (setq new-key new-choice))
198 (define-key m new-key orig-fun))
199 (define-key m orig-key new-fun))))
200 (setq rebind-keys-mode-map m))))
201 (setq rebind--emul-keymap-alist (list (cons 'rebind-keys-mode rebind-keys-mode-map))))
204 (define-minor-mode rebind-keys-mode
205 "Rebind keys as defined in `rebind-keys'.
206 The key bindings will override almost all other key bindings
207 since it is put on emulation level, like for example ``cua-mode'
210 This is for using for example C-a to mark the whole buffer \(or a
211 field). There are some predifined keybindings for this."
212 :keymap rebind-keys-mode-map
217 (rebind-update-keymap)
218 ;;(rebind-keys-post-command)
219 (add-hook 'post-command-hook 'rebind-keys-post-command t))
220 (remove-hook 'post-command-hook 'rebind-keys-post-command)
221 (setq emulation-mode-map-alists (delq 'rebind--emul-keymap-alist emulation-mode-map-alists))))
223 (defun rebind-keys-post-command ()
224 "Make sure we are first in the list when turned on.
225 This is reasonable since we are using this mode to really get the
226 key bindings we want!"
227 (unless (eq 'rebind--emul-keymap-alist (car emulation-mode-map-alists))
228 (setq emulation-mode-map-alists (delq 'rebind--emul-keymap-alist emulation-mode-map-alists))
229 (when rebind-keys-mode
230 (add-to-list 'emulation-mode-map-alists 'rebind--emul-keymap-alist))))
233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 ;;; Interactive functions for the keymap
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
240 ;;; rebind.el ends here