summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/rebind.el
blob: cf4700cd5a7ba76cc583bb05d30296315f050af0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
;;; rebind.el --- Rebind keys
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2008-01-20T12:04:37+0100 Sun
;; Version:
;; Last-Updated:
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; See `rebind-keys-mode' for information.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(eval-when-compile (require 'new-key-seq-widget nil t))
(eval-when-compile (require 'ourcomments-widgets nil t))


(defun rebind-toggle-first-modifier (orig-key-seq mod)
  (let* ((first (elt orig-key-seq 0))
         (new-key-seq (copy-sequence orig-key-seq)))
    (setq first (if (memq mod first)
                    (delq mod first)
                  (cons mod first)))
    (aset new-key-seq 0 first)
    new-key-seq))
;; (rebind-toggle-first-modifier (key-description-to-vector "C-c a") 'shift)
;; (rebind-toggle-first-modifier (key-description-to-vector "C-S-c a") 'shift)

(defvar widget-commandp-prompt-value-history nil)

;;;###autoload
(defgroup rebind nil
  "Customizaton group for `rebind-keys-mode'."
  :group 'convenience
  :group 'emulations
  :group 'editing-basics
  :group 'emacsw32)

;; (customize-option-other-window 'rebind-keys)
;; (Fetched key bindings from http://www.davidco.com/tips_tools/tip45.html)
(defcustom rebind-keys
  '(
    ("MS Windows - often used key bindings" t
      (
       (
        [(control ?a)]
        "C-a on w32 normally means 'select all'. In Emacs it is `beginning-of-line'."
        t
        shift
        ourcomments-mark-whole-buffer-or-field)
      (
       [(control ?o)]
       "C-o on w32 normally means 'open file'. In Emacs it is `open-line'."
       nil
       shift
       find-file)
      (
       [(control ?f)]
       "C-f is commonly search on w32. In Emacs it is `forward-char'."
       nil
       shift
       isearch-forward)
      (
       [(control ?s)]
       "C-s is normally 'save file' on w32. In Emacs it is `isearch-forward'."
       nil
       nil
       save-buffer)
      (
       [(control ?w)]
       "C-w is often something like kill-buffer on w32. In Emacs it is `kill-region'."
       t
       shift
       kill-buffer)
      (
       [(control ?p)]
       "C-p is nearly always print on w32. In Emacs it is `previous-line'."
       t
       shift
       hfyview-buffer)
      (
       [(home)]
       "HOME normally stays in a field. By default it does not do that in Emacs."
       t
       nil
       ourcomments-move-beginning-of-line)
      (
       [(control ?+)]
       "C-+ often increases font size (in web browsers for example)."
       t
       shift
       text-scale-adjust)
      (
       [(control ?-)]
       "C-- often decreases font size (in web browsers for example)."
       t
       shift
       text-scale-adjust)
      (
       [(control ?0)]
       "C-0 often resets font size (in web browsers for example)."
       t
       shift
       text-scale-adjust)
       )))
  "Normal Emacs keys that are remapped to follow some other standard.
The purpose of this variable is to make it easy to switch between
Emacs key bindings and other standards.

The new bindings are made in the global minor mode
`rebind-keys-mode' and will only have effect when this mode is
on.

*Note:* You can only move functions bound in the global key map
        this way.
*Note:* To get CUA keys you should turn on option `cua-mode'.
*Note:* To get vi key bindings call function `viper-mode'.
*Note:* `text-scale-adjust' already have default key bindings."
  :type '(repeat
          (list
           (string :tag "For what")
           (boolean :tag "Group on/off")
           (repeat
            (list
             (key-sequence :tag "Emacs key binding")
             (string :tag "Why rebind")
             (boolean :tag "Rebinding on/off")
             (choice :tag "Move original by"
                     (const :tag "Don't put it on any new binding" nil)
                     (choice :tag "Add key binding modifier"
                             (const meta)
                             (const control)
                             (const shift))
                     (key-sequence :tag "New binding for original function"))
             (command :tag "New command on above key"))
            )))
  :set (lambda (sym val)
	 (set-default sym val)
	 (when (featurep 'rebind)
	   (rebind-update-keymap)))
  :group 'rebind)

(defvar rebind-keys-mode-map nil)

(defvar rebind--emul-keymap-alist nil)

;;(rebind-update-keymap)
(defun rebind-update-keymap ()
  (let ((m (make-sparse-keymap)))
    (dolist (group rebind-keys)
      (when (nth 1 group)
        (dolist (v (nth 2 group))
          (let* ((orig-key   (nth 0 v))
                 (comment    (nth 1 v))
                 (enabled    (nth 2 v))
                 (new-choice (nth 3 v))
                 (new-fun    (nth 4 v))
                 (orig-fun (lookup-key global-map orig-key))
                 new-key)
            (when enabled
              (when new-choice
                (if (memq new-choice '(meta control shift))
                    (setq new-key (rebind-toggle-first-modifier orig-key new-choice))
                  (setq new-key new-choice))
                (define-key m new-key orig-fun))
              (define-key m orig-key new-fun))))
        (setq rebind-keys-mode-map m))))
  (setq rebind--emul-keymap-alist (list (cons 'rebind-keys-mode rebind-keys-mode-map))))

;;;###autoload
(define-minor-mode rebind-keys-mode
  "Rebind keys as defined in `rebind-keys'.
The key bindings will override almost all other key bindings
since it is put on emulation level, like for example ``cua-mode'
and `viper-mode'.

This is for using for example C-a to mark the whole buffer \(or a
field). There are some predifined keybindings for this."
  :keymap rebind-keys-mode-map
  :global t
  :group 'rebind
  (if rebind-keys-mode
      (progn
        (rebind-update-keymap)
        ;;(rebind-keys-post-command)
        (add-hook 'post-command-hook 'rebind-keys-post-command t))
    (remove-hook 'post-command-hook 'rebind-keys-post-command)
    (setq emulation-mode-map-alists (delq 'rebind--emul-keymap-alist emulation-mode-map-alists))))

(defun rebind-keys-post-command ()
  "Make sure we are first in the list when turned on.
This is reasonable since we are using this mode to really get the
key bindings we want!"
  (unless (eq 'rebind--emul-keymap-alist (car emulation-mode-map-alists))
    (setq emulation-mode-map-alists (delq 'rebind--emul-keymap-alist emulation-mode-map-alists))
    (when rebind-keys-mode
      (add-to-list 'emulation-mode-map-alists 'rebind--emul-keymap-alist))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Interactive functions for the keymap



(provide 'rebind)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; rebind.el ends here