-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-repeat-mode.el
More file actions
194 lines (145 loc) · 5.76 KB
/
smart-repeat-mode.el
File metadata and controls
194 lines (145 loc) · 5.76 KB
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
;;;
;; Filename: smart-repeat-mode.el
;; Description: Support multi repeat command
;; Author: <[email protected]>
;; Maintainer: zhsfei
;; Copyright (C) :2013 zhsfei all rights reserved.
;; Created: :2013-11-29
;; Version: 0.0.1
;; Keywords: multi repeat command
;; 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 3, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation,
;; Installation:
;; Put the smart-repeat-mode.el to your load-path.
;; And add to .emacs: (require 'smart-repeat-mode)
;; example 1
;; (smart-repeat-define-alist '("\C-l" "\C-cl" )
;; '(("l" recenter-top-bottom )
;; ("\C-l" recenter-top-bottom)
;; ("b" (lambda () (interactive) (move-to-window-line (- -1 scroll-margin))))
;; ("2" (lambda () (interactive) (move-to-window-line (- -1 scroll-margin))))
;; ("t" (lambda () (interactive) (move-to-window-line scroll-margin)))
;; ("1" (lambda () (interactive) (move-to-window-line scroll-margin)))
;; )
;; )
;; example 2
;; (smart-repeat-define-alist '("\C-ce" "\C-ca")
;; '((("e" "n") end-of-defun )
;; (("a" "p") beginning-of-defun)) )
(defvar smart-repeat-keymap nil)
(defvar smart-repeat-old-key-alist nil)
(defun smart-repeat-close-mode ( )
(interactive)
(smart-repeat-mode -1)
(push last-command-event unread-command-events)
(message "repeat-mode off"))
(unless smart-repeat-keymap
(setq smart-repeat-keymap (make-keymap))
(set-char-table-range (nth 1 smart-repeat-keymap) nil 'smart-repeat-close-mode)
(suppress-keymap smart-repeat-keymap )
)
(defun smart-repeat-undefine-last-alist(old-key-alist)
(dolist (key old-key-alist)
(let ((key-value (car key)))
(if (listp key-value)
(dolist (one-key key-value)
(define-key smart-repeat-keymap one-key 'smart-repeat-close-mode))
(define-key smart-repeat-keymap key-value 'smart-repeat-close-mode)))))
(defun smart-repeat-define-internal-one-key (key-value fun-value)
(when (listp fun-value)
(setq fun-value (car fun-value)))
(define-key smart-repeat-keymap key-value fun-value))
(defun smart-repeat-define-internal-alist-key (key-alist key-fun)
(dolist (one-key key-alist)
(smart-repeat-define-internal-one-key one-key key-fun)))
(defun smart-repeat-define-internal (key-alist)
(dolist (key key-alist)
(let ((fun-value (cdr key))
(key-value (car key)))
(if (listp key-value)
(smart-repeat-define-internal-alist-key key-value fun-value)
(smart-repeat-define-internal-one-key key-value fun-value)))))
(defun smart-repeat--get-keychar (char)
(if (vectorp char)
(aref char 0)
(string-to-char char)))
(defun smart-repeat-check-alist-last (key-or-alist event)
(let (foundp )
(dolist (akey key-or-alist)
(when (eq event (smart-repeat--get-keychar akey))
(setq foundp t)))
foundp))
(defun smart-repeat-check-akey-last-base-key (key-or-alist event)
(let ( (base-key (event-basic-type event) )
)
(if (eq base-key (smart-repeat--get-keychar key-or-alist))
t
nil))
)
(defun smart-repeat-check-akey-last (key-or-alist event)
(if (eq event (smart-repeat--get-keychar key-or-alist))
t
nil))
(defun smart-repeat-check-last-command (alist)
(let ( foundFlag-p (event last-command-event) key-or-alist)
(setq foundFlag-p nil )
(while (and (not foundFlag-p)
(not (null alist)))
(setq key-or-alist (caar alist) )
(if (listp key-or-alist)
(setq foundFlag-p (smart-repeat-check-alist-last key-or-alist event))
(setq foundFlag-p (smart-repeat-check-akey-last key-or-alist event)))
(when foundFlag-p
(setq foundFlag-p t )
(push last-command-event unread-command-events))
(unless foundFlag-p
(setq foundFlag-p (smart-repeat-check-akey-last-base-key key-or-alist event))
(when
(push (character-to-event (event-basic-type event)) unread-command-events)
))
(setq alist (cdr alist)))))
(define-minor-mode smart-repeat-mode ""
nil
" sRepeat"
smart-repeat-keymap
:global t
:after-hook ;; (message ;; "sRepeat on"
;; )
)
(defun smart-repeat-define-alist ( key-alist fun-alist &optional back-unread keymap)
(let ((one-key) )
(unless keymap
(setq keymap global-map))
(while (and (not (null key-alist)))
(setq one-key (car key-alist))
(smart-repeat-define-key one-key fun-alist back-unread keymap)
(setq key-alist (cdr key-alist)))))
(defun smart-repeat-process-multi-command (key-alist push-back-read)
(if smart-repeat-old-key-alist
(smart-repeat-undefine-last-alist smart-repeat-old-key-alist))
(smart-repeat-define-internal key-alist)
(setq smart-repeat-old-key-alist key-alist)
(when push-back-read
(smart-repeat-check-last-command key-alist))
(smart-repeat-mode 1))
(defun smart-repeat-define-key ( key-set alist back-unread keymap)
(lexical-let
((key-alist alist)
(push-back-read (not back-unread)))
(define-key keymap key-set
(lambda ()
"smart-repeat-define-key "
(interactive)
(smart-repeat-process-multi-command key-alist push-back-read))
)))
(provide 'smart-repeat-mode)
;; end file