-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpicker.el
248 lines (216 loc) · 9.11 KB
/
gpicker.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
;;; gpicker.el --- gpicker integration for Emacs
;; Copyright (C) 2008 Aliaksey Kandratsenka
;; Author: Aliaksey Kandratsenka <[email protected]>
;; Version: 1.0
;; Keywords: autocompletion
;; 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
;;
;; Arrange loading of this file. One way is to add
;;
;; (load "/usr/local/share/gpicker/gpicker.el")
;;
;; to your .emacs
;;
;; Run `gpicker-visit-project' to specify project directory.
;;
;; Open files by running `gpicker-find-file' or
;; `gpicker-find-file-other-window' or
;; `gpicker-find-file-other-frame'.
;;
;; I prefer to globally bind them to Super-f, C-x 4 Super-f and
;; C-x 5 Super-f correspondingly. You can do same by adding the following
;; elisp snippet to your .emacs
;;
;; (global-set-key [8388710] 'gpicker-find-file)
;; (global-set-key [?\C-x ?4 8388710] 'gpicker-find-file-other-window)
;; (global-set-key [?\C-x ?5 8388710] 'gpicker-find-file-other-frame)
;;
;; On most keyboards Super is a key with flag between Ctrl and Alt. On
;; mac keyboards it's Command key.
;;; Code
(require 'ffap)
(defvar *gpicker-path* "gpicker")
(defvar *gpicker-simple-path* nil)
(defvar *gpicker-extra-args* '("--disable-bzr" ;; bzr builtin file listing is just too slow
"--load-stdin-too"
"--multiselect"))
(defvar *gpicker-project-dir* nil)
(defvar *gpicker-project-type* "guess")
(defvar *gpicker-errors-log* (expand-file-name "~/gpicker-errors.log"))
(defvar *gpicker-buffers-list* (expand-file-name "~/gpicker-buffers-list"))
(defun gpicker-delete-file (path)
(condition-case e
(delete-file path)
(error nil)))
(defun gpicker-get-simple-path ()
(or *gpicker-simple-path*
(concat *gpicker-path* "-simple")))
(defun gpicker-grab-stdout (&rest call-process-args)
(with-temp-buffer
(unwind-protect (let ((status (apply #'call-process
(car call-process-args)
(and (file-exists-p *gpicker-buffers-list*)
*gpicker-buffers-list*)
(list (current-buffer) *gpicker-errors-log*)
nil
(cdr call-process-args))))
(if (eql status 0)
(buffer-string)
(message "%s exited with status %d" (car call-process-args) status)
(save-excursion
(set-buffer "*Messages*")
(goto-char (point-max))
(insert-file-contents *gpicker-errors-log*))
nil))
(progn
(gpicker-delete-file *gpicker-errors-log*)
(gpicker-delete-file *gpicker-buffers-list*)))))
(defun gpicker-pick (dir)
(unless *gpicker-project-dir*
(error "visit gpicker project via 'gpicker-visit-project first!"))
(let ((gpicker-args (append *gpicker-extra-args*
(list "-t"
(or *gpicker-project-type* "default")
dir)))
(at-point (ffap-string-at-point)))
(when (and at-point
(> (string-bytes at-point) 0))
(setq gpicker-args (list* "--init-filter" at-point gpicker-args)))
(with-temp-file *gpicker-buffers-list*
(let ((standard-output (current-buffer)))
(dolist (b (buffer-list))
(let ((name (buffer-name b)))
(unless (or (eq (current-buffer) b)
(string= (substring name 0 1) " ")
(buffer-file-name b))
(princ (buffer-name b))
(princ "\0"))))))
(unwind-protect (let ((rv (apply #'gpicker-grab-stdout
*gpicker-path*
gpicker-args)))
(and rv
(split-string rv "\0" t)))
(discard-input))))
(defun gpicker-set-project-type (type)
"Sets type of current gpicker project"
(interactive (list (completing-read "Choose gpicker project type: "
'("guess" "git" "hg"
"bzr" "default" "mlocate")
nil t nil nil "guess")))
(setq *gpicker-project-type* type))
(defun gpicker-visit-project (dir)
"Selects current project directory for gpicker"
(interactive "DSelect directory:")
(cd dir)
(setq *gpicker-project-type* "guess")
(setq *gpicker-project-dir* (expand-file-name dir)))
(defun gpicker-call-find-function (find-function original-file)
(let ((file (expand-file-name file *gpicker-project-dir*)))
(if (file-exists-p file)
(let ((revert-without-query (list (regexp-quote (abbreviate-file-name file)))))
(funcall find-function file))
(let ((b (get-buffer original-file)))
(and b (display-buffer b))))))
(defun gpicker-do-find (find-function)
(let ((files (gpicker-pick *gpicker-project-dir*)))
(if (and (eql find-function #'find-file)
(eql (length files) 2))
(progn
(gpicker-call-find-function #'find-file (car files))
(gpicker-call-find-function #'find-file-other-window (cadr files)))
(dolist (file files)
(gpicker-call-find-function find-function file)))))
(defun gpicker-find-file ()
(interactive)
(gpicker-do-find #'find-file))
(defun gpicker-find-file-other-window ()
(interactive)
(gpicker-do-find #'find-file-other-window))
(defun gpicker-find-file-other-frame ()
(interactive)
(gpicker-do-find #'find-file-other-frame))
;;; ido integration
(defvar *gpicker-hook-ido* t
"*Use gpicker for filtering ido results")
(defadvice ido-set-matches-1 (around gpicker-ido-set-matches-1 activate)
"Choose between the regular ido-set-matches-1 and gpicker-my-ido-match"
(if *gpicker-hook-ido*
(setq ad-return-value (gpicker-my-ido-match ido-text (ad-get-arg 0)))
ad-do-it))
(defun gpicker-ido-toggle ()
"Toggle gpicker-ido integration"
(interactive)
(setq *gpicker-hook-ido* (not *gpicker-hook-ido*)))
(defun gpicker-my-ido-match (str items)
(setq items (reverse items))
(with-temp-file *gpicker-buffers-list*
(let ((standard-output (current-buffer)))
(dolist (item items)
(princ item)
(princ "\0"))))
(let ((args (list "-n\\0" str)))
(when (or ido-rotate (string= str ""))
(push "-S" args)) ;; dont_sort
(let ((out (apply #'gpicker-grab-stdout
(gpicker-get-simple-path)
args)))
(split-string out "\0" t))))
(defun gpicker-complete-list (list &optional init-filter)
(with-temp-file *gpicker-buffers-list*
(let ((standard-output (current-buffer)))
(dolist (name list)
(princ name)
(princ "\0"))))
(unwind-protect (let ((rv (gpicker-grab-stdout *gpicker-path* "-l" "-d" "\01"
"--init-filter" (or init-filter (ffap-string-at-point))
"-")))
(and rv
(split-string rv "\0" t)))
(discard-input)))
(defun alk-obarray-to-list (obarray)
(let ((rv))
(mapatoms (lambda (sym)
(setq rv (cons (symbol-name sym) rv)))
obarray)
rv))
(defun gpicker-goto-tag ()
(interactive)
(find-tag (car (gpicker-complete-list (alk-obarray-to-list (tags-completion-table))))))
(defun gpicker-imenu ()
(interactive)
(require 'imenu)
(let* ((imenu-alist (imenu--make-index-alist))
(imenu (apply #'append
(mapcar (lambda (pair)
(let ((val (cdr pair)))
(if (listp val)
nil ;; (mapcar #'car val)
(cons (car pair) nil))))
imenu-alist)))
(selected (car (gpicker-complete-list imenu))))
(when selected
(imenu-default-goto-function selected (cdr (assoc selected imenu-alist)))
(recenter-top-bottom))))
(defun gpicker-isearch ()
(interactive)
(let ((old-current-buffer (current-buffer)))
(with-temp-file *gpicker-buffers-list*
(let ((standard-output (current-buffer)))
(princ (with-current-buffer old-current-buffer
(buffer-substring 1 (buffer-end 1)))))))
(let ((rv (gpicker-grab-stdout *gpicker-path* "-IlP" "-d" "\\n" "-n" "\\n" "-")))
(and (> (length rv) 0)
(let ((line-num (1+ (string-to-number rv 10))))
(goto-line line-num)))))
(provide 'gpicker)