-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathone-key-ext.el
277 lines (230 loc) · 10.9 KB
/
one-key-ext.el
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
;;; one-key-ext.el --- Extension functions for One-key
;; Copyright (C) 2010
;; Author:
;; Keywords:
;; 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; `one-key-visit-dir' can be used to navigate directory and apply functions to
;; files. It's not an interactive function, so you should write wrapping functions
;; yourself. You can refer to `one-key-print-filename' as an example, which print
;; current file's name using one-key in minibuffer.
;;; Code:
(require 'one-key)
(defvar one-key-ext/max-lisp-eval-depth 2000
"The `max-lisp-eval-depth' when using one-key-ext.el.
Because one-key related functions don't exit until the one-key menu buffer is killed,
either because a snippet is inserted or an unknown keystroke. Setting this to a larg
number can avoid error of `Lisp nesting exceeds max-lisp-eval-depth")
(defvar one-key-current-filename nil
"Current file's name which is visited by one-key.")
(defvar one-key-visit-func nil
"The function that will be applied to file.")
(defvar one-key-filename-map-func nil
"The function that will be used to get the file name displayed in one-key menu.")
(defvar one-key-current-dir nil
"Current director which is visited by one-key.")
(defvar one-key-back-to-topdir-key "SPC"
"Keybinding that will be used to back to parent directory.")
(defvar one-key-visit-dir/topdir "~/"
"The fixed top level dir that `one-key-visit-dir' can explore the subdirs of,
but can't go above this dir.")
(defconst one-key-ext/alphabets-and-numbers
(let (alphabets-and-numbers)
(dotimes (i 26)
(push (- ?Z i) alphabets-and-numbers))
(dotimes (i 26)
(push (- ?z i) alphabets-and-numbers))
(dotimes (i 10)
(push (- ?9 i) alphabets-and-numbers))
alphabets-and-numbers)
"A list contains characters [0-9a-zA-Z].
This list will be used when generating keys in `one-key-ext/generate-key'")
(defun one-key-visit-dir (dir func &optional filename-map-func)
"Visit DIR using one-key.
For each sub-dir of DIR, the associated command will be `(one-key-visit-dir sub-dir func)',
for each file under DIR, the associated command will be `(funcall func)'.
In FUNC, `one-key-current-filename' can be used to do operations on current file.
The optional FILENAME-MAP-FUNC specifies a function to be called on each file name,
it has one argument (string), the original file name, and returns a string, the
new file name which will be displayed in the one-key menu.
DIR should be `one-key-visit-dir/topdir' or under `one-key-visit-dir/topdir' in the
directory tree."
(unless (file-directory-p dir)
(error "one-key-visit-dir called with a non-directory"))
(unless (functionp func)
(error "one-key-visit-dir called with a non-funciton."))
(unless (one-key-visit-dir/legal-dir-p dir)
(error "one-key-visit-dir called with an illegal directory."))
(if (symbolp func)
(setq one-key-visit-func (symbol-function func))
(setq one-key-visit-func func))
(if filename-map-func
(if (symbolp filename-map-func)
(setq one-key-filename-map-func (symbol-function filename-map-func))
(setq one-key-filename-map-func filename-map-func))
(setq one-key-filename-map-func nil))
(setq one-key-current-dir dir)
(let ((old-max-lisp-eval-depth max-lisp-eval-depth))
(setq max-lisp-eval-depth one-key-ext/max-lisp-eval-depth)
(unwind-protect
(let* ((dir-name (file-name-as-directory (file-truename dir)))
(key-name-list (one-key-ext/build-key-name-list dir (not (one-key-visit-dir/descendant-p dir))))
(one-key-menu-ext/dir-alist (one-key-ext/build-menu-alist
key-name-list
one-key-visit-func
one-key-filename-map-func)))
(flet ((one-key-menu-ext-func ()
(one-key-menu (concat dir-name "\n") one-key-menu-ext/dir-alist)))
(one-key-menu-ext-func)))
(setq max-lisp-eval-depth old-max-lisp-eval-depth))
(setq max-lisp-eval-depth old-max-lisp-eval-depth)))
(defun one-key-ext/build-menu-alist (key-name-list func &optional filename-map-func)
"Return the menu alist that will be used by One-key.
KEY-NAME-LIST is generated by `one-key-ext/build-key-name-list'.
FUNC is the function that will be applied to normal files."
(let (menu-alist)
(dolist (key-name key-name-list)
(if (fourth key-name) ; A directory
(push (cons (cons (first key-name) (second key-name))
`(lambda ()
(interactive)
(one-key-visit-dir (third ',key-name) ,func ,filename-map-func)))
menu-alist)
(push (cons (cons (first key-name)
(if filename-map-func
(funcall filename-map-func (second key-name))
(second key-name)))
`(lambda ()
(interactive)
(setq one-key-current-filename (concat (third ',key-name) (second ',key-name)))
(funcall ,func)))
menu-alist)))
menu-alist))
(defun one-key-ext/build-key-name-list (dir &optional dont-show-parent)
"Build the key name list for directory DIR.
Each element of the returned list has the following form:
(KEY FILE-NAME FULLPATH DIRP)
If optional DONT-SHOW-PARENT is non-nil, there will not be a
`one-key-back-to-topdir-key' \"Back to parent directory\" item."
(unless (file-directory-p dir)
(error "one-key-ext/build-key-name-list called with a non-directory."))
(let* ((dir-name (file-name-as-directory (file-truename dir)))
(sub-dirs (mapcar #'file-name-nondirectory (one-key-ext/subdirs dir)))
(files (mapcar #'file-name-nondirectory (one-key-ext/subdirs dir t)))
(keys (if dont-show-parent '("q") `(,one-key-back-to-topdir-key "q")))
(key-name-list nil))
;; build key for sub-dirs
(dolist (sub-dir sub-dirs)
(let ((key (one-key-ext/generate-key sub-dir keys)))
(push key keys)
(push `(,key ,(concat sub-dir "/")
,(concat (file-name-as-directory dir-name) sub-dir)
t)
key-name-list)))
;; build key for files
(dolist (file files)
(let ((key (one-key-ext/generate-key file keys)))
(push key keys)
(push `(,key ,(file-name-nondirectory (file-truename (concat dir-name file)))
,dir-name nil)
key-name-list)))
;; Here we push the `one-key-back-to-topdir-key'
(unless dont-show-parent
(push `(,one-key-back-to-topdir-key ,(concat "Back to parent directory: "
(file-name-nondirectory (expand-file-name ".." dir)))
,(expand-file-name ".." dir-name) t)
key-name-list))
key-name-list))
(defun one-key-ext/generate-key (file-name keys)
"Return the generated key for file named FILE-NAME.
The generated key will be used in one-key menu. FILE-NAME is a string.
KEYS contains all the alredy used keys.
"
(let (key)
(dolist (element one-key-ext/alphabets-and-numbers)
(let ((normal-key (char-to-string element)))
(when (one-key-ext/key-not-used normal-key keys)
(setq key normal-key)
(return))))
(unless key
(error "Can not generate a unique key for file : %s" file-name))
key))
(defun one-key-ext/key-not-used (key key-name-list)
"Return t if KEY is not used in KEY-NAME-LIST."
(dolist (key-name key-name-list t)
(if (string= key key-name)
(return nil))))
(defun one-key-ext/subdirs (directory &optional file?)
"Return subdirs or files of DIRECTORY according to FILE?."
(remove-if (lambda (file)
(or (string-match "^\\."
(file-name-nondirectory file))
(string-match "~$"
(file-name-nondirectory file))
(if file?
(file-directory-p file)
(not (file-directory-p file)))))
(directory-files directory t)))
(defun one-key-visit-dir/legal-dir-p (dir)
"Return t if DIR is `one-key-visit-dir/topdir' or a descendant of `one-key-visit-dir/topdir'."
(or (string= (file-name-as-directory (file-truename dir))
(file-name-as-directory (file-truename one-key-visit-dir/topdir)))
(one-key-visit-dir/descendant-p dir)))
(defun one-key-visit-dir/descendant-p (dir)
"Return t if DIR is a descendant of `one-key-visit-dir/topdir'."
(let ((topdir-name (file-name-as-directory (file-truename one-key-visit-dir/topdir)))
(dir-name (file-name-as-directory (file-truename dir))))
(and (not (string= topdir-name dir-name))
(= (- (abs (compare-strings topdir-name 0 nil dir-name 0 nil)) 1) (length topdir-name)))))
; (string-prefix-p topdir-name dir-name))))
;;;;;;;;;; example function ;;;;;;;;;;;;;;
(require 'ido)
(defvar one-key-menu-print-filename-alist
'(
(("p" . "Print current file name") . (lambda ()
(interactive)
(message "current file name: %s" one-key-current-filename)
(one-key-menu "Print Filename" one-key-menu-print-filename-alist)))
(("SPC" . "Back to previous menu") . (lambda ()
(interactive)
(one-key-visit-dir one-key-current-dir one-key-visit-func)))))
(defun one-key-menu-print-filename ()
(interactive)
(one-key-menu "Print Filename" one-key-menu-print-filename-alist))
(defun one-key-identify-filename (filename)
"Function that simply returns current file's name"
filename)
(defun one-key-reverse-filename (filename)
"Function that reverses current file's name"
(concat (reverse (string-to-list filename))))
(defun one-key-print-filename (dir)
"Print current file's name using one-key in minibuffer."
(interactive (list (ido-read-directory-name "Directory for root of tree: " default-directory)))
(one-key-visit-dir dir 'one-key-menu-print-filename))
;; using lambda expression as function
(defun one-key-print-filename2 (dir)
"Print current file's name using one-key in minibuffer."
(interactive (list (ido-read-directory-name "Directory for root of tree: " default-directory)))
(one-key-visit-dir dir (lambda ()
(interactive)
(one-key-menu "Print Filename" one-key-menu-print-filename-alist))))
(defun one-key-print-filename3 (dir)
"Print current file's name using one-key in minibuffer."
(interactive (list (ido-read-directory-name "Directory for root of tree: " default-directory)))
(one-key-visit-dir dir 'one-key-menu-print-filename 'one-key-reverse-filename))
(defun one-key-print-filename4 (dir)
"Print current file's name using one-key in minibuffer."
(interactive (list (ido-read-directory-name "Directory for root of tree: " default-directory)))
(one-key-visit-dir dir 'one-key-menu-print-filename '(lambda (filename)
filename)))
(provide 'one-key-ext)
;;; one-key-ext.el ends here