Skip to content
Newer
Older
100644 291 lines (258 sloc) 11.3 KB
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
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
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\"
f35c4db @thierryvolpiatto * helm-locate.el (helm-locate-case-fold-search): Issue #115 New user …
thierryvolpiatto authored
54 windows-nt: \"es -r %s\"
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
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
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
f35c4db @thierryvolpiatto * helm-locate.el (helm-locate-case-fold-search): Issue #115 New user …
thierryvolpiatto authored
68 (defcustom helm-locate-case-fold-search helm-case-fold-search
69 "It have the same meaning as `helm-case-fold-search'.
70 The -i option of locate will be used depending of value of
71 `helm-pattern' when this is set to 'smart.
72 When nil \"-i\" will not be used at all.
73 and when non--nil it will always be used.
74 NOTE: the -i option of the \"es\" command used on windows does
75 the opposite of \"locate\" command."
76 :group 'helm-locate
77 :type 'symbol)
78
79
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
80 (defvar helm-generic-files-map
81 (let ((map (make-sparse-keymap)))
82 (set-keymap-parent map helm-map)
213797e @thierryvolpiatto * helm-files.el (helm-c-highlight-files): Use basename only if `helm-…
thierryvolpiatto authored
83 (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
84 (define-key map (kbd "C-s") 'helm-ff-run-grep)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
85 (define-key map (kbd "M-g s") 'helm-ff-run-grep)
86 (define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
87 (define-key map (kbd "M-g p") 'helm-ff-run-pdfgrep)
88 (define-key map (kbd "M-D") 'helm-ff-run-delete-file)
89 (define-key map (kbd "C-=") 'helm-ff-run-ediff-file)
90 (define-key map (kbd "C-c =") 'helm-ff-run-ediff-merge-file)
91 (define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
92 (define-key map (kbd "M-i") 'helm-ff-properties-persistent)
93 (define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
775bdcf @thierryvolpiatto * helm-locate.el: Bind etags to generic keymap.
thierryvolpiatto authored
94 (define-key map (kbd "M-.") 'helm-ff-run-etags)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
95 (define-key map (kbd "C-w") 'helm-yank-text-at-point)
96 (define-key map (kbd "C-c ?") 'helm-generic-file-help)
97 map)
98 "Generic Keymap for files.")
99
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
100
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
101 (defun helm-ff-find-locatedb (&optional from-ff)
102 "Try to find if a local locatedb file is available.
103 The search is done in `helm-ff-default-directory' or
104 fall back to `default-directory' if FROM-FF is nil."
105 (when helm-ff-locate-db-filename
106 (cond ((and helm-ff-default-directory
107 from-ff
108 (file-exists-p (expand-file-name
109 helm-ff-locate-db-filename
110 helm-ff-default-directory))
111 (expand-file-name
112 helm-ff-locate-db-filename
113 helm-ff-default-directory)))
114 ((and (not from-ff)
115 (file-exists-p (expand-file-name
116 helm-ff-locate-db-filename
117 default-directory))
118 (expand-file-name
119 helm-ff-locate-db-filename
120 default-directory))))))
121
e13a5c6 @thierryvolpiatto * helm-files.el (helm-ff-locate): basename of cand is now available a…
thierryvolpiatto authored
122 (defun helm-locate-1 (&optional localdb init from-ff default)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
123 "Generic function to run Locate.
124 if LOCALDB is non--nil search and use a local locate db file.
125 INIT is a string to use as initial input in prompt.
126 See `helm-locate-with-db' and `helm-locate'."
f3d6825 @thierryvolpiatto Require helm-mode in many places for *read-file-name.
thierryvolpiatto authored
127 (require 'helm-mode)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
128 (helm-locate-with-db
129 (and localdb
130 (or (helm-ff-find-locatedb from-ff)
131 (helm-c-read-file-name
b380bbf @thierryvolpiatto * helm.el (helm-select-persistent-action-window): Use member.
thierryvolpiatto authored
132 "Choose or create Locate Db file (locate.db): "
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
133 :initial-input (or helm-ff-default-directory
134 default-directory)
135 :marked-candidates t
136 :preselect helm-locate-db-file-regexp
d6c876f @thierryvolpiatto * helm-locate.el: Allow creating a locate.db with persistent action.
thierryvolpiatto authored
137 :persistent-action #'(lambda (candidate)
b15efd8 @thierryvolpiatto * helm-locate.el (helm-locate-1): Prevent user trying to create a db …
thierryvolpiatto authored
138 (if (file-directory-p candidate)
139 (message "Error: The locate Db should be a file")
140 (shell-command
141 (format helm-locate-create-db-command
142 candidate
143 helm-ff-default-directory))
144 (helm-force-update
145 (if helm-ff-transformer-show-only-basename
146 (helm-c-basename candidate)
147 candidate))))
d6c876f @thierryvolpiatto * helm-locate.el: Allow creating a locate.db with persistent action.
thierryvolpiatto authored
148 :persistent-help "Create locale locate Db"
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
149 :test #'(lambda (x)
150 (if helm-locate-db-file-regexp
151 ;; Select only locate db files and directories
152 ;; to allow navigation.
153 (or (string-match
154 helm-locate-db-file-regexp x)
155 (file-directory-p x))
156 x)))))
e13a5c6 @thierryvolpiatto * helm-files.el (helm-ff-locate): basename of cand is now available a…
thierryvolpiatto authored
157 init default))
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
158
3f303fe @thierryvolpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored
159 (defun helm-locate-set-command ()
160 "Setup `helm-c-locate-command' if not already defined."
161 (unless helm-c-locate-command
162 (setq helm-c-locate-command
163 (case system-type
f35c4db @thierryvolpiatto * helm-locate.el (helm-locate-case-fold-search): Issue #115 New user …
thierryvolpiatto authored
164 ('gnu/linux "locate %s -r %s")
165 ('berkeley-unix "locate %s %s")
166 ('windows-nt "es %s -r %s")
3f303fe @thierryvolpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored
167 (t "locate %s")))))
168
2d09d7b @thierryvolpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode c…
thierryvolpiatto authored
169 (defvar helm-file-name-history nil)
e13a5c6 @thierryvolpiatto * helm-files.el (helm-ff-locate): basename of cand is now available a…
thierryvolpiatto authored
170 (defun helm-locate-with-db (&optional db initial-input default)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
171 "Run locate -d DB.
172 If DB is not given or nil use locate without -d option.
173 Argument DB can be given as a string or list of db files.
174 Argument INITIAL-INPUT is a string to use as initial-input.
175 See also `helm-locate'."
176 (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
177 (helm-locate-set-command)
f8c4f48 @thierryvolpiatto * helm-locate.el (helm-locate-with-db): let-bind `helm-ff-transformer…
thierryvolpiatto authored
178 (let ((helm-ff-transformer-show-only-basename nil)
179 (helm-c-locate-command
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
180 (if db
181 (replace-regexp-in-string
182 "locate"
183 (format "locate -d %s"
184 (mapconcat 'identity
185 ;; Remove eventually
186 ;; marked directories by error.
187 (loop for i in db
188 unless (file-directory-p i)
189 collect i) ":"))
190 helm-c-locate-command)
191 helm-c-locate-command)))
2d09d7b @thierryvolpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode c…
thierryvolpiatto authored
192 (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
193 (helm :sources 'helm-c-source-locate
194 :buffer "*helm locate*"
195 :input initial-input
e13a5c6 @thierryvolpiatto * helm-files.el (helm-ff-locate): basename of cand is now available a…
thierryvolpiatto authored
196 :default default
2d09d7b @thierryvolpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode c…
thierryvolpiatto authored
197 :history 'helm-file-name-history)))
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
198
199 (defun helm-c-locate-init ()
200 "Initialize async locate process for `helm-c-source-locate'."
f35c4db @thierryvolpiatto * helm-locate.el (helm-locate-case-fold-search): Issue #115 New user …
thierryvolpiatto authored
201 (let ((locate-is-es (string-match "^es" helm-c-locate-command))
202 process-connection-type)
ada576e @thierryvolpiatto * helm-locate.el (helm-c-locate-init): Use pipe.
thierryvolpiatto authored
203 (prog1
204 (start-process-shell-command
205 "locate-process" helm-buffer
f35c4db @thierryvolpiatto * helm-locate.el (helm-locate-case-fold-search): Issue #115 New user …
thierryvolpiatto authored
206 (format helm-c-locate-command
207 (case helm-locate-case-fold-search
208 (smart (let ((case-fold-search nil))
209 (if (string-match "[A-Z]" helm-pattern)
210 (if locate-is-es "-i" "")
211 (if locate-is-es "" "-i"))))
212 (t (if helm-locate-case-fold-search
213 (if locate-is-es "-i" "")
214 (if locate-is-es "" "-i"))))
215 helm-pattern))
216 (set-process-sentinel
217 (get-process "locate-process")
218 #'(lambda (process event)
219 (if (string= event "finished\n")
220 (with-helm-window
221 (setq mode-line-format
222 '(" " mode-line-buffer-identification " "
223 (line-number-mode "%l") " "
224 (:eval (propertize
225 (format "[Locate Process Finish- (%s results)]"
226 (max (1- (count-lines
227 (point-min) (point-max))) 0))
228 'face 'helm-grep-finish))))
229 (force-mode-line-update))
230 (helm-log "Error: Locate %s"
231 (replace-regexp-in-string "\n" "" event))))))))
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
232
233 (defvar helm-c-source-locate
234 `((name . "Locate")
3f303fe @thierryvolpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored
235 (init . helm-locate-set-command)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
236 (candidates . helm-c-locate-init)
237 (type . file)
238 (requires-pattern . 3)
6ff63d4 @thierryvolpiatto * helm.el (helm-read-pattern-maybe): Allow using history attr in sour…
thierryvolpiatto authored
239 (history . ,'helm-file-name-history)
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
240 (keymap . ,helm-generic-files-map)
241 (help-message . helm-generic-file-help-message)
242 (candidate-number-limit . 9999)
243 (mode-line . helm-generic-file-mode-line-string)
244 (delayed))
245 "Find files matching the current input pattern with locate.")
246
247 (defun helm-c-locate-read-file-name (prompt &optional init)
248 "Search a file with locate and return it's filename.
249 Use argument PROMPT and INIT for `helm' arguments
250 prompt and input."
251 (helm :sources
252 '((name . "Locate")
253 (candidates . helm-c-locate-init)
254 (action . identity)
255 (requires-pattern . 3)
256 (candidate-number-limit . 9999)
257 (mode-line . helm-generic-file-mode-line-string)
258 (delayed))
259 :prompt prompt
260 :input init
261 :buffer "*helm locate rfn*"))
262
263 ;;;###autoload
264 (defun helm-locate (arg)
265 "Preconfigured `helm' for Locate.
266 Note: you can add locate options after entering pattern.
267 See 'man locate' for valid options.
268
269 You can specify a specific database with prefix argument ARG \(C-u\).
270 Many databases can be used: navigate and mark them.
271 See also `helm-locate-with-db'.
272
273 To create a user specific db, use
274 \"updatedb -l 0 -o db_path -U directory\".
275 Where db_path is a filename matched by
276 `helm-locate-db-file-regexp'."
277 (interactive "P")
278 (setq helm-ff-default-directory default-directory)
279 (helm-locate-1 arg))
280
281 (provide 'helm-locate)
282
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
283 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
284 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
285 ;; coding: utf-8
286 ;; indent-tabs-mode: nil
287 ;; byte-compile-dynamic: t
288 ;; End:
289
6ffe9b7 @thierryvolpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
290 ;;; helm-locate.el ends here
Something went wrong with that request. Please try again.