Skip to content

HTTPS clone URL

Subversion checkout URL

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