Source

emacs.d / k2-mode.el

Full commit
  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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
;;; k2-mode.el -- A minor mode with Some keybindings and misc functions for numeric keypads

;;; I wrote k2-mode in 2001 when I was a complete newb and it's full
;;; of stuff I don't use these days.
;;;
;;; PURPOSE: to avoid hideous finger jumps and the nasty [CTRL] key,
;;; and make my most used commands readily accessible
;;;
;;; NOTES:
;;; See http://xahlee.org/emacs/keyboard_shortcuts.html
;;;
;;; (global-set-key [f5] 'goto-buffers-list)
;;; or use (define-key k2-mode-map [f7] ctl-x-map)
;;; or something like this vector list (global-set-key [f6] "\C-x\C-c")
;;; or maybe something like this (keyboard-translate [f6] ?\C-x)
;;;
;;; don't use Scroll-Lock (key-20) !  it interferes with the function keys


(defun k2-backward-comment ()
  (interactive)
  (search-backward comment-start))

(defun k2-forward-comment ()
  (interactive)
  (search-forward comment-start))

(defun k2-mark-comment ()
  (interactive)
  (beginning-of-line)
  (search-forward comment-start)
  (backward-char (length comment-start))
  (set-mark-command nil)
  (end-of-line))
(defun k2-mark-whole-sexp ()
  (interactive)
  (backward-sexp 1)
  (mark-sexp))
(defun k2-mark-whole-sentence ()
  (interactive)
  (backward-sentence 1)
  (set-mark-command nil)
  (forward-sentence))
(defun k2-mark-whole-line ()
  (interactive)
  (beginning-of-line)
  (set-mark-command nil)
  (end-of-line))

(defun k2-kill-whole-sexp ()
  (interactive)
  (mark-sexp 1)
  (kill-sexp 1))
(defun k2-kill-whole-word ()
  (interactive)
  (mark-word 1)
  (kill-word 1))
(defun k2-kill-whole-paragraph ()
  (interactive)
  (mark-paragraph)
  (kill-region (region-beginning) (region-end)))
(defun k2-kill-whole-line ()
  (interactive)
  (k2-mark-whole-line)
  (kill-region (region-beginning) (region-end)))
(defun k2-kill-whole-defun ()
  (interactive)
  (mark-defun)
  (kill-region (region-beginning) (region-end)))

(defun k2-copy-comment ()
  (interactive)
  (save-excursion
    (k2-mark-comment)
    (kill-ring-save (region-beginning) (region-end))))
(defun k2-copy-buffer ()
  (interactive)
  (save-excursion
    (mark-whole-buffer)
    (kill-ring-save (region-beginning) (region-end))))
(defun k2-copy-whole-sexp ()
  (interactive)
  (save-excursion
    (mark-sexp 1)
    (kill-ring-save (region-beginning) (region-end))))
(defun k2-copy-whole-word ()
  (interactive)
  (save-excursion
    (mark-word)
    (kill-ring-save (region-beginning) (region-end))))
(defun k2-copy-whole-paragraph ()
  (interactive)
  (save-excursion
    (mark-paragraph)
    (kill-ring-save (region-beginning) (region-end))))
(defun k2-copy-whole-line ()
  (interactive)
  (save-excursion
    (k2-mark-whole-line)
    (kill-ring-save (region-beginning) (region-end))))
(defun k2-copy-whole-defun ()
  (interactive)
  (save-excursion
    (mark-defun)
    (kill-ring-save (region-beginning) (region-end))))

(defun k2-toggle-mark ()
  (interactive)
  (cond
   ((not (equal mark-active nil))
    (setq mark-active nil))
   ((equal mark-active nil)
    (setq mark-active t))))

(defun k2-mark-rest-of-buffer()
  (interactive)
  (set-mark-command nil)
  (goto-char (point-max)))


(defun k2-horiz-rule(c)
  "make a horizontal rule on the next line that spans all the text on the current line"
  (interactive "s")
  (end-of-line)
  (let ((l (current-column)))
    (insert "\n")
    (insert-char (string-to-char c) l)
    (insert "\n")))

(defvar k2-mode nil
  "On/Off switch variable for k2-mode")

(defvar k2-mode-map nil
  "Local keymap for k2-mode buffers")

(if k2-mode-map nil
    (setq k2-mode-map (make-sparse-keymap))

    ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;
    ;; Extra Prefix Keys - for standard maps and
    ;; for my custom ones
    ;;

    ;; set prefix keys for C-x-C-[char] shortcuts  --- see below
    (defvar prefix-kp-multiply-map (make-keymap))
    (define-key k2-mode-map [kp-multiply] prefix-kp-multiply-map)
    (define-key k2-mode-map [f9] prefix-kp-multiply-map)

    ;; set prefix keys for M-C-[char] shortcuts   --- see below
    (defvar prefix-kp-subtract-map (make-keymap))
    (define-key k2-mode-map [kp-subtract] prefix-kp-subtract-map)
    (define-key k2-mode-map [f11] prefix-kp-subtract-map)

    (defvar prefix-kp-add-map (make-keymap))
    (define-key k2-mode-map [kp-add] prefix-kp-add-map)

    (defvar prefix-kp-divide-map (make-keymap))
    (define-key k2-mode-map [kp-divide] prefix-kp-divide-map)


    (defvar prefix-s-kp-divide-map (make-keymap))
    (define-key k2-mode-map [s-kp-divide] prefix-s-kp-divide-map)


    ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;
    ;; Basic Redefinitions
    ;;

    ;; random functions
    (define-key k2-mode-map [s-end]     'k2-horiz-rule) ;


    ;; Basic File-Buffer-Program Operations (Open, Save, Close, Write, Open Desktop, Exit)


    (define-key k2-mode-map [C-kp-down] 'find-file) ; C-x C-f
    (define-key k2-mode-map [s-M-kp-down] 'find-file-read-only) ; C-x C-r
    (define-key k2-mode-map [s-C-M-kp-down] 'find-alternate-file) ; note- same as C-M-S-kp-begin
    (define-key k2-mode-map [s-kp-down] 'save-buffer) ; C-x C-s
    (define-key k2-mode-map [s-return] 'save-buffer) ; C-x C-s
    (define-key k2-mode-map [C-M-kp-2] 'kill-buffer) ; C-x k - Same as C-M-S-kp-down

    (define-key k2-mode-map [M-kp-down] 'write-file)
    (define-key k2-mode-map [M-kp-down] 'write-region)
    (define-key k2-mode-map [C-M-kp-down] '(lambda (file)
                                           (interactive "FWrite to file: ")
                                           (write-region (region-beginning)
                                                         (region-end) file t))) ; appends region to file
    ;;  Insert/ Delete / Undo  Commands
    (define-key k2-mode-map [C-insert] 'yank-pop) ; M-y
    (define-key k2-mode-map [C-M-insert] 'yank-rectangle)
    (define-key k2-mode-map [M-insert] 'insert-register)
    (define-key k2-mode-map [C-M-S-insert] 'insert-file)

    ;; Basic Cursor/Point Movement Commands

                                        ; M-previous 'Pg Up' and M-next 'Pg Dn' to
                                        ; scroll to other window

    (define-key k2-mode-map [home] 'beginning-of-line)
    (define-key k2-mode-map [end] 'end-of-line)

    ;; Block Cursor/Point Movement Commands
    (define-key k2-mode-map [\e up]        'backward-paragraph) ; up
    ;; Mark Commands

    (define-key k2-mode-map [C-kp-add] 'k2-toggle-mark) ; [C-+] toggle the mark active/not

    (define-key prefix-kp-add-map [kp-home] 'mark-word) ; [7] word
    (define-key prefix-kp-add-map [kp-prior] 'mark-paragraph) ; [9] paragraph

    (define-key prefix-kp-add-map [kp-left] 'k2-mark-whole-line) ; [4] line
    (define-key prefix-kp-add-map [kp-begin] 'mark-sexp) ; [5] sexp
    (define-key prefix-kp-add-map [kp-right] 'mark-defun) ; [6] defun

    (define-key prefix-kp-add-map [kp-end] 'k2-mark-comment) ; [1] comment
    (define-key prefix-kp-add-map [kp-down] 'mark-whole-buffer) ; [2] buffer - not applicable

    (define-key prefix-kp-add-map [delete] 'point-to-register) ; [.] register - point to register
    (define-key prefix-kp-add-map "\C-d" 'point-to-register) ; [.] register - point to register

    ;; Kill Commands
    (define-key prefix-kp-subtract-map [kp-home] 'k2-kill-whole-word) ; [7] word
    (define-key prefix-kp-subtract-map [kp-prior] 'k2-kill-whole-paragraph) ; [9] paragraph

    (define-key prefix-kp-subtract-map [kp-left] 'k2-kill-whole-line) ; [4] line
    (define-key prefix-kp-subtract-map [kp-begin] 'k2-kill-whole-sexp) ; [5] sexp
    (define-key prefix-kp-subtract-map [kp-right] 'k2-kill-whole-defun) ; [6] defun

    (define-key prefix-kp-subtract-map [kp-end] 'kill-comment) ; [1] comment
    (define-key prefix-kp-subtract-map [kp-down] 'kill-buffer) ; [2] buffer - not applicable
    (define-key prefix-kp-subtract-map [kp-next] 'kill-rectangle) ; [3] rectangle -
                                                                  ; retrieve it with
                                                                  ; [kp-add kp-next]
                                        ; add x-clipboard functionality


    ;; Copy Commands
                                        ; Add x-clipboard stuff
                                        ;

    (define-key prefix-kp-multiply-map [kp-multiply] 'kill-ring-save) ;

    (define-key prefix-kp-multiply-map [kp-home] 'k2-copy-whole-word) ; [7] word
    (define-key prefix-kp-multiply-map [kp-prior] 'k2-copy-whole-paragraph) ; [9] paragraph

    (define-key prefix-kp-multiply-map [kp-left] 'k2-copy-whole-line) ; [4] line
    (define-key prefix-kp-multiply-map [kp-begin] 'k2-copy-whole-sexp) ; [5] sexp
    (define-key prefix-kp-multiply-map [kp-right] 'k2-copy-whole-defun) ; [6] defun

    (define-key prefix-kp-multiply-map [kp-end] 'k2-copy-comment) ; [1] comment
    (define-key prefix-kp-multiply-map [kp-down] 'k2-copy-buffer) ; [2] buffer
    (define-key prefix-kp-multiply-map [kp-next] '(lambda ()
                                                    (interactive)
                                                    (save-excursion
                                                      (kill-rectangle
                                                       (region-beginning) (region-end))
                                                      (exchange-point-and-mark)
                                                      (yank-rectangle))))


                                        ; Register Copy Commands
    (define-key prefix-kp-multiply-map [delete] 'copy-to-register) ; [.] copies the
                                                                   ; region to
                                                                   ; register
    (define-key prefix-kp-multiply-map [M-delete] 'copy-rectangle-to-register) ; [3]
                                                                               ; rectangle
                                                                               ; to
                                                                               ; register

    ;; Comment Commands

                                        ; M-kp-divide to comment and C-M-kp-divide to
                                        ; uncommment

    (define-key k2-mode-map [M-kp-divide kp-divide] 'comment-region) ;

    ;; Un-Comment Commands
    (define-key k2-mode-map [C-M-kp-divide kp-divide] '(lambda ()
                                                             (interactive)
                                                             (comment-region
                                                              (region-beginning) (region-end) -1)))


    ;; Block Comment Commands

                                        ; C-M-S-kp-divide - create (auto-name
                                        ; matching for either side of block) -
                                        ; movement - outline show/hide - field move -
                                        ; region-select

    ;; Transposition Commands
    (define-key prefix-kp-divide-map [kp-divide] 'exchange-point-and-mark) ;

    (define-key prefix-kp-divide-map [kp-home] 'transpose-words) ; [7] word
    (define-key prefix-kp-divide-map [kp-up] 'transpose-sentences) ; [8] sentence
    (define-key prefix-kp-divide-map [kp-prior] 'transpose-paragraphs) ; [9] paragraph

    (define-key prefix-kp-divide-map [kp-left] 'transpose-lines) ; [4] line
    (define-key prefix-kp-divide-map [kp-begin] 'transpose-sexps) ; [5] sexp
                                        ; [6] defun - transposition not possible yet


    ;; Rectangle Commands
                                        ;'copy-rectangle-to-register
                                        ;'string-rectangle
                                        ;'open-rectangle
                                        ;'clear-rectangle
                                        ;'close-rectangle
                                        ;'delete-rectangle
                                        ;'yank-rectangle
                                        ;'delete-whitespace-rectangle

    ;; Eval Commands  M-*


                                        ; add hooks for relevant modes

                                        ; plus prefix for count, case, spell, print,
                                        ; shell-command, reverse, TAC, filter-region
                                        ; through shell command, etc.

                                        ; reverse-region, ispell-region,
                                        ; print-region, upcase-region,
                                        ; shell-command-on-region,
                                        ; count-lines-region, capitalize-region,
                                        ; eval-region, spell-region



                                        ; eval-last-sexp C-x C-e Command: Evaluate
                                        ;   sexp before point; print value in
                                        ;   minibuffer.

                                        ; eval-print-last-sexp M-x
                                        ;   eval-print-last-sexp RET Command:
                                        ;   Evaluate sexp before point; print value
                                        ;   into current buffer.

                                        ; indent-sexp M-x indent-sexp RET Command:
                                        ; Indent each line of the list starting just
                                        ; after point.

    ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


    ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;
    ;; keymap definition for C-x-C-[char] shortcuts - the prefix keys are f9 and kp-multiply *

    (define-key prefix-kp-multiply-map "2" 'pop-global-mark) ;shortcut to \C-x\C-@
    (define-key prefix-kp-multiply-map "b" 'list-buffers)
    (define-key prefix-kp-multiply-map "c" 'save-buffers-kill-emacs)
    (define-key prefix-kp-multiply-map "d" 'list-directory)
    (define-key prefix-kp-multiply-map "e" 'eval-las-sexp)
    (define-key prefix-kp-multiply-map "f" 'find-file)
    (define-key prefix-kp-multiply-map "k" 'edit-kbd-macro)
    (define-key prefix-kp-multiply-map "l" 'downcase-region)
    (define-key prefix-kp-multiply-map "n" 'set-goal-column)
    (define-key prefix-kp-multiply-map "o" 'delete-blank-lines)
    (define-key prefix-kp-multiply-map "p" 'mark-page)
    (define-key prefix-kp-multiply-map "q" 'vc-toggle-read-only)
    (define-key prefix-kp-multiply-map "r" 'find-file-read-only)
    (define-key prefix-kp-multiply-map "s" 'save-buffer)
    (define-key prefix-kp-multiply-map "t" 'transpose-lines)
    (define-key prefix-kp-multiply-map "v" 'find-alternate-file)
    (define-key prefix-kp-multiply-map "w" 'write-file)
    (define-key prefix-kp-multiply-map "x" 'exchange-point-and-mark)
    (define-key prefix-kp-multiply-map "z" 'iconify-or-deiconify-frame)

    ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;
    ;; keymap definition for M-C-[char] shortcuts - the prefix keys are f11 and kp-subtract [-]
    ;;
    ;; -also used as 'mark then kill' prefix for the keypad normal keys (not the numbers)

    (define-key prefix-kp-subtract-map "@" 'mark-sexp)
    (define-key prefix-kp-subtract-map "a" 'beginning-of-defun)
    (define-key prefix-kp-subtract-map "b" 'backward-sexp)
    (define-key prefix-kp-subtract-map "c" 'exit-recursive-edit)
    (define-key prefix-kp-subtract-map "d" 'down-list)
    (define-key prefix-kp-subtract-map "e" 'end-of-defun)
    (define-key prefix-kp-subtract-map "f" 'forward-sexp)
    (define-key prefix-kp-subtract-map "h" 'mark-defun)
    (define-key prefix-kp-subtract-map "j" 'indent-new-comment-line)
    (define-key prefix-kp-subtract-map "k" 'kill-sexp)
    (define-key prefix-kp-subtract-map "l" 'reposition-window)
    (define-key prefix-kp-subtract-map "n" 'forward-list)
    (define-key prefix-kp-subtract-map "o" 'split-line)
    (define-key prefix-kp-subtract-map "p" 'backward-list)
    (define-key prefix-kp-subtract-map "r" 'isearch-backward-regexp)
    (define-key prefix-kp-subtract-map "s" 'isearch-forward-regexp)
    (define-key prefix-kp-subtract-map "t" 'transpose-sexps)
    (define-key prefix-kp-subtract-map "u" 'backward-up-list)
    (define-key prefix-kp-subtract-map "v" 'scroll-other-window)
    (define-key prefix-kp-subtract-map "w" 'append-next-kill)
    (define-key prefix-kp-subtract-map "\\" 'indent-region))


(defun k2-mode (&optional arg)
  "Shortcut keymap minor mode"
  (interactive "P")
  (setq k2-mode (not (or (and (null arg) k2-mode)
                         (<= (prefix-numeric-value arg) 0))))
  (or (assoc 'k2-mode minor-mode-alist)
      (setq minor-mode-alist
            (cons '(k2-mode " K2") minor-mode-alist)))
  (or (assoc 'k2-mode minor-mode-map-alist)
      (setq minor-mode-map-alist (cons (cons k2-mode k2-mode-map)
                                       minor-mode-map-alist))))
(provide 'k2-mode)