Skip to content
Newer
Older
100644 268 lines (236 sloc) 10.5 KB
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
1 ;;; helm-locate.el --- helm interface for locate.
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;; NOTE for WINDOZE users:
19 ;; You have to install Everything with his command line interface here:
20 ;; http://www.voidtools.com/download.php
21
22 ;;; Code:
23
24 (eval-when-compile (require 'cl))
25 (require 'helm)
26
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored Mar 23, 2012
27
28 (defgroup helm-locate nil
29 "Locate related Applications and libraries for Helm."
30 :group 'helm)
31
32 (defcustom helm-locate-db-file-regexp "m?locate\.db$"
33 "Default regexp to match locate database.
34 If nil Search in all files."
35 :type 'string
36 :group 'helm-locate)
37
38 (defcustom helm-ff-locate-db-filename "locate.db"
39 "The basename of the locatedb file you use locally in your directories.
40 When this is set and `helm' find such a file in the directory from
41 where you launch locate, it will use this file and will not prompt you
42 for a db file.
43 Note that this happen only when locate is launched with a prefix arg."
44 :group 'helm-locate
45 :type 'string)
46
47 (defcustom helm-c-locate-command nil
48 "A list of arguments for locate program.
49 If nil it will be calculated when `helm-locate' startup
50 with these default values for different systems:
51
52 Gnu/linux: \"locate -i -r %s\"
53 berkeley-unix: \"locate -i %s\"
54 windows-nt: \"es -i -r %s\"
55 Others: \"locate %s\"
56
57 This string will be passed to format so it should end with `%s'.
58 The \"-r\" option must be the last option."
59 :type 'string
60 :group 'helm-locate)
61
d6c876f @thierryvolpiatto * helm-locate.el: Allow creating a locate.db with persistent action.
thierryvolpiatto authored Jul 24, 2012
62 (defcustom helm-locate-create-db-command
63 "updatedb -l 0 -o %s -U %s"
64 "Command used to create a locale locate db file."
65 :type 'string
66 :group 'helm-locate)
67
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored Mar 28, 2012
68 (defvar helm-generic-files-map
69 (let ((map (make-sparse-keymap)))
70 (set-keymap-parent map helm-map)
213797e @thierryvolpiatto * helm-files.el (helm-c-highlight-files): Use basename only if `helm-…
thierryvolpiatto authored May 24, 2012
71 (define-key map (kbd "C-]") 'helm-ff-run-toggle-basename)
cec8359 @thierryvolpiatto * helm-files.el: Bind C-s to grep in helm-find-files-map.
thierryvolpiatto authored Jul 28, 2012
72 (define-key map (kbd "C-s") 'helm-ff-run-grep)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored Mar 28, 2012
73 (define-key map (kbd "M-g s") 'helm-ff-run-grep)
74 (define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
75 (define-key map (kbd "M-g p") 'helm-ff-run-pdfgrep)
76 (define-key map (kbd "M-D") 'helm-ff-run-delete-file)
77 (define-key map (kbd "C-=") 'helm-ff-run-ediff-file)
78 (define-key map (kbd "C-c =") 'helm-ff-run-ediff-merge-file)
79 (define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
80 (define-key map (kbd "M-i") 'helm-ff-properties-persistent)
81 (define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
82 (define-key map (kbd "C-w") 'helm-yank-text-at-point)
83 (define-key map (kbd "C-c ?") 'helm-generic-file-help)
84 map)
85 "Generic Keymap for files.")
86
87
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored Mar 23, 2012
88
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
89 (defun helm-ff-find-locatedb (&optional from-ff)
90 "Try to find if a local locatedb file is available.
91 The search is done in `helm-ff-default-directory' or
92 fall back to `default-directory' if FROM-FF is nil."
93 (when helm-ff-locate-db-filename
94 (cond ((and helm-ff-default-directory
95 from-ff
96 (file-exists-p (expand-file-name
97 helm-ff-locate-db-filename
98 helm-ff-default-directory))
99 (expand-file-name
100 helm-ff-locate-db-filename
101 helm-ff-default-directory)))
102 ((and (not from-ff)
103 (file-exists-p (expand-file-name
104 helm-ff-locate-db-filename
105 default-directory))
106 (expand-file-name
107 helm-ff-locate-db-filename
108 default-directory))))))
109
110 (defun helm-locate-1 (&optional localdb init from-ff)
111 "Generic function to run Locate.
112 if LOCALDB is non--nil search and use a local locate db file.
113 INIT is a string to use as initial input in prompt.
114 See `helm-locate-with-db' and `helm-locate'."
f3d6825 @thierryvolpiatto Require helm-mode in many places for *read-file-name.
thierryvolpiatto authored Jun 7, 2012
115 (require 'helm-mode)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
116 (helm-locate-with-db
117 (and localdb
118 (or (helm-ff-find-locatedb from-ff)
119 (helm-c-read-file-name
b380bbf @thierryvolpiatto * helm.el (helm-select-persistent-action-window): Use member.
thierryvolpiatto authored Jul 31, 2012
120 "Choose or create Locate Db file (locate.db): "
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
121 :initial-input (or helm-ff-default-directory
122 default-directory)
123 :marked-candidates t
124 :preselect helm-locate-db-file-regexp
d6c876f @thierryvolpiatto * helm-locate.el: Allow creating a locate.db with persistent action.
thierryvolpiatto authored Jul 24, 2012
125 :persistent-action #'(lambda (candidate)
b15efd8 @thierryvolpiatto * helm-locate.el (helm-locate-1): Prevent user trying to create a db …
thierryvolpiatto authored Jul 31, 2012
126 (if (file-directory-p candidate)
127 (message "Error: The locate Db should be a file")
128 (shell-command
129 (format helm-locate-create-db-command
130 candidate
131 helm-ff-default-directory))
132 (helm-force-update
133 (if helm-ff-transformer-show-only-basename
134 (helm-c-basename candidate)
135 candidate))))
d6c876f @thierryvolpiatto * helm-locate.el: Allow creating a locate.db with persistent action.
thierryvolpiatto authored Jul 24, 2012
136 :must-match t
137 :persistent-help "Create locale locate Db"
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
138 :test #'(lambda (x)
139 (if helm-locate-db-file-regexp
140 ;; Select only locate db files and directories
141 ;; to allow navigation.
142 (or (string-match
143 helm-locate-db-file-regexp x)
144 (file-directory-p x))
145 x)))))
146 init))
147
3f303fe @thierryvolpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored Apr 2, 2012
148 (defun helm-locate-set-command ()
149 "Setup `helm-c-locate-command' if not already defined."
150 (unless helm-c-locate-command
151 (setq helm-c-locate-command
152 (case system-type
153 ('gnu/linux "locate -i -r %s")
154 ('berkeley-unix "locate -i %s")
155 ('windows-nt "es -i -r %s")
156 (t "locate %s")))))
157
2d09d7b @thierryvolpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode c…
thierryvolpiatto authored Apr 18, 2012
158 (defvar helm-file-name-history nil)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
159 (defun helm-locate-with-db (&optional db initial-input)
160 "Run locate -d DB.
161 If DB is not given or nil use locate without -d option.
162 Argument DB can be given as a string or list of db files.
163 Argument INITIAL-INPUT is a string to use as initial-input.
164 See also `helm-locate'."
165 (when (and db (stringp db)) (setq db (list db)))
3f303fe @thierryvolpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored Apr 2, 2012
166 (helm-locate-set-command)
f8c4f48 @thierryvolpiatto * helm-locate.el (helm-locate-with-db): let-bind `helm-ff-transformer…
thierryvolpiatto authored Jun 20, 2012
167 (let ((helm-ff-transformer-show-only-basename nil)
168 (helm-c-locate-command
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
169 (if db
170 (replace-regexp-in-string
171 "locate"
172 (format "locate -d %s"
173 (mapconcat 'identity
174 ;; Remove eventually
175 ;; marked directories by error.
176 (loop for i in db
177 unless (file-directory-p i)
178 collect i) ":"))
179 helm-c-locate-command)
180 helm-c-locate-command)))
2d09d7b @thierryvolpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode c…
thierryvolpiatto authored Apr 18, 2012
181 (setq helm-file-name-history (mapcar 'helm-c-basename file-name-history))
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
182 (helm :sources 'helm-c-source-locate
183 :buffer "*helm locate*"
184 :input initial-input
2d09d7b @thierryvolpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode c…
thierryvolpiatto authored Apr 18, 2012
185 :history 'helm-file-name-history)))
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
186
187 (defun helm-c-locate-init ()
188 "Initialize async locate process for `helm-c-source-locate'."
ada576e @thierryvolpiatto * helm-locate.el (helm-c-locate-init): Use pipe.
thierryvolpiatto authored Jul 17, 2012
189 (let (process-connection-type)
190 (prog1
191 (start-process-shell-command
192 "locate-process" helm-buffer
193 (format helm-c-locate-command helm-pattern))
194 (set-process-sentinel (get-process "locate-process")
195 #'(lambda (process event)
948cf69 @thierryvolpiatto * helm-locate.el (helm-c-locate-init): Fix mode-line.
thierryvolpiatto authored Jul 21, 2012
196 (if (string= event "finished\n")
197 (with-helm-window
198 (setq mode-line-format
199 '(" " mode-line-buffer-identification " "
200 (line-number-mode "%l") " "
201 (:eval (propertize
202 (format "[Locate Process Finish- (%s results)]"
203 (max (1- (count-lines
204 (point-min) (point-max))) 0))
205 'face 'helm-grep-finish))))
206 (force-mode-line-update))
207 (helm-log "Error: Locate %s"
208 (replace-regexp-in-string "\n" "" event))))))))
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
209
210 (defvar helm-c-source-locate
211 `((name . "Locate")
3f303fe @thierryvolpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored Apr 2, 2012
212 (init . helm-locate-set-command)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
213 (candidates . helm-c-locate-init)
214 (type . file)
215 (requires-pattern . 3)
6ff63d4 @thierryvolpiatto * helm.el (helm-read-pattern-maybe): Allow using history attr in sour…
thierryvolpiatto authored Apr 19, 2012
216 (history . ,'helm-file-name-history)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
217 (keymap . ,helm-generic-files-map)
218 (help-message . helm-generic-file-help-message)
219 (candidate-number-limit . 9999)
220 (mode-line . helm-generic-file-mode-line-string)
221 (delayed))
222 "Find files matching the current input pattern with locate.")
223
224 (defun helm-c-locate-read-file-name (prompt &optional init)
225 "Search a file with locate and return it's filename.
226 Use argument PROMPT and INIT for `helm' arguments
227 prompt and input."
228 (helm :sources
229 '((name . "Locate")
230 (candidates . helm-c-locate-init)
231 (action . identity)
232 (requires-pattern . 3)
233 (candidate-number-limit . 9999)
234 (mode-line . helm-generic-file-mode-line-string)
235 (delayed))
236 :prompt prompt
237 :input init
238 :buffer "*helm locate rfn*"))
239
240 ;;;###autoload
241 (defun helm-locate (arg)
242 "Preconfigured `helm' for Locate.
243 Note: you can add locate options after entering pattern.
244 See 'man locate' for valid options.
245
246 You can specify a specific database with prefix argument ARG \(C-u\).
247 Many databases can be used: navigate and mark them.
248 See also `helm-locate-with-db'.
249
250 To create a user specific db, use
251 \"updatedb -l 0 -o db_path -U directory\".
252 Where db_path is a filename matched by
253 `helm-locate-db-file-regexp'."
254 (interactive "P")
255 (setq helm-ff-default-directory default-directory)
256 (helm-locate-1 arg))
257
258 (provide 'helm-locate)
259
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored Mar 28, 2012
260 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored Jul 30, 2012
261 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored Mar 28, 2012
262 ;; coding: utf-8
263 ;; indent-tabs-mode: nil
264 ;; byte-compile-dynamic: t
265 ;; End:
266
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored Mar 20, 2012
267 ;;; helm-locate.el ends here
Something went wrong with that request. Please try again.