Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 241 lines (210 sloc) 9.037 kb
6ffe9b7 Thierry Volpiatto * 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)
37b4201 Thierry Volpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
26 ;(require 'helm-mode)
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
27
ba17f09 Thierry Volpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
28
29 (defgroup helm-locate nil
30 "Locate related Applications and libraries for Helm."
31 :group 'helm)
32
33 (defcustom helm-locate-db-file-regexp "m?locate\.db$"
34 "Default regexp to match locate database.
35 If nil Search in all files."
36 :type 'string
37 :group 'helm-locate)
38
39 (defcustom helm-ff-locate-db-filename "locate.db"
40 "The basename of the locatedb file you use locally in your directories.
41 When this is set and `helm' find such a file in the directory from
42 where you launch locate, it will use this file and will not prompt you
43 for a db file.
44 Note that this happen only when locate is launched with a prefix arg."
45 :group 'helm-locate
46 :type 'string)
47
48 (defcustom helm-c-locate-command nil
49 "A list of arguments for locate program.
50 If nil it will be calculated when `helm-locate' startup
51 with these default values for different systems:
52
53 Gnu/linux: \"locate -i -r %s\"
54 berkeley-unix: \"locate -i %s\"
55 windows-nt: \"es -i -r %s\"
56 Others: \"locate %s\"
57
58 This string will be passed to format so it should end with `%s'.
59 The \"-r\" option must be the last option."
60 :type 'string
61 :group 'helm-locate)
62
37b4201 Thierry Volpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
63 (defvar helm-generic-files-map
64 (let ((map (make-sparse-keymap)))
65 (set-keymap-parent map helm-map)
213797e Thierry Volpiatto * helm-files.el (helm-c-highlight-files): Use basename only if `helm-ff-...
thierryvolpiatto authored
66 (define-key map (kbd "C-]") 'helm-ff-run-toggle-basename)
37b4201 Thierry Volpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
67 (define-key map (kbd "M-g s") 'helm-ff-run-grep)
68 (define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
69 (define-key map (kbd "M-g p") 'helm-ff-run-pdfgrep)
70 (define-key map (kbd "M-D") 'helm-ff-run-delete-file)
71 (define-key map (kbd "C-=") 'helm-ff-run-ediff-file)
72 (define-key map (kbd "C-c =") 'helm-ff-run-ediff-merge-file)
73 (define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
74 (define-key map (kbd "M-i") 'helm-ff-properties-persistent)
75 (define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
76 (define-key map (kbd "C-w") 'helm-yank-text-at-point)
77 (define-key map (kbd "C-c ?") 'helm-generic-file-help)
78 map)
79 "Generic Keymap for files.")
80
81
ba17f09 Thierry Volpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
82
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
83 (defun helm-ff-find-locatedb (&optional from-ff)
84 "Try to find if a local locatedb file is available.
85 The search is done in `helm-ff-default-directory' or
86 fall back to `default-directory' if FROM-FF is nil."
87 (when helm-ff-locate-db-filename
88 (cond ((and helm-ff-default-directory
89 from-ff
90 (file-exists-p (expand-file-name
91 helm-ff-locate-db-filename
92 helm-ff-default-directory))
93 (expand-file-name
94 helm-ff-locate-db-filename
95 helm-ff-default-directory)))
96 ((and (not from-ff)
97 (file-exists-p (expand-file-name
98 helm-ff-locate-db-filename
99 default-directory))
100 (expand-file-name
101 helm-ff-locate-db-filename
102 default-directory))))))
103
104 (defun helm-locate-1 (&optional localdb init from-ff)
105 "Generic function to run Locate.
106 if LOCALDB is non--nil search and use a local locate db file.
107 INIT is a string to use as initial input in prompt.
108 See `helm-locate-with-db' and `helm-locate'."
109 (helm-locate-with-db
110 (and localdb
111 (or (helm-ff-find-locatedb from-ff)
112 (helm-c-read-file-name
113 "LocateDBFiles: "
114 :initial-input (or helm-ff-default-directory
115 default-directory)
116 :marked-candidates t
117 :preselect helm-locate-db-file-regexp
118 :test #'(lambda (x)
119 (if helm-locate-db-file-regexp
120 ;; Select only locate db files and directories
121 ;; to allow navigation.
122 (or (string-match
123 helm-locate-db-file-regexp x)
124 (file-directory-p x))
125 x)))))
126 init))
127
3f303fe Thierry Volpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored
128 (defun helm-locate-set-command ()
129 "Setup `helm-c-locate-command' if not already defined."
130 (unless helm-c-locate-command
131 (setq helm-c-locate-command
132 (case system-type
133 ('gnu/linux "locate -i -r %s")
134 ('berkeley-unix "locate -i %s")
135 ('windows-nt "es -i -r %s")
136 (t "locate %s")))))
137
2d09d7b Thierry Volpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode conf...
thierryvolpiatto authored
138 (defvar helm-file-name-history nil)
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
139 (defun helm-locate-with-db (&optional db initial-input)
140 "Run locate -d DB.
141 If DB is not given or nil use locate without -d option.
142 Argument DB can be given as a string or list of db files.
143 Argument INITIAL-INPUT is a string to use as initial-input.
144 See also `helm-locate'."
145 (when (and db (stringp db)) (setq db (list db)))
3f303fe Thierry Volpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored
146 (helm-locate-set-command)
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
147 (let ((helm-c-locate-command
148 (if db
149 (replace-regexp-in-string
150 "locate"
151 (format "locate -d %s"
152 (mapconcat 'identity
153 ;; Remove eventually
154 ;; marked directories by error.
155 (loop for i in db
156 unless (file-directory-p i)
157 collect i) ":"))
158 helm-c-locate-command)
159 helm-c-locate-command)))
2d09d7b Thierry Volpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode conf...
thierryvolpiatto authored
160 (setq helm-file-name-history (mapcar 'helm-c-basename file-name-history))
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
161 (helm :sources 'helm-c-source-locate
162 :buffer "*helm locate*"
163 :input initial-input
2d09d7b Thierry Volpiatto * helm-mode (helm-completing-read-default): Fix issue #41 helm-mode conf...
thierryvolpiatto authored
164 :history 'helm-file-name-history)))
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
165
166 (defun helm-c-locate-init ()
167 "Initialize async locate process for `helm-c-source-locate'."
168 (setq mode-line-format
169 '(" " mode-line-buffer-identification " "
170 (line-number-mode "%l") " "
171 (:eval (propertize "(Locate Process Running) "
172 'face '((:foreground "red"))))))
173 (prog1
174 (start-process-shell-command "locate-process" nil
175 (format helm-c-locate-command
176 helm-pattern))
177 (set-process-sentinel (get-process "locate-process")
178 #'(lambda (process event)
179 (when (string= event "finished\n")
180 (with-helm-window
181 (force-mode-line-update nil)
182 (helm-update-move-first-line)))))))
183
184 (defvar helm-c-source-locate
185 `((name . "Locate")
3f303fe Thierry Volpiatto Bugfix locate not starting when use with helm-for-files on startup.
thierryvolpiatto authored
186 (init . helm-locate-set-command)
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
187 (candidates . helm-c-locate-init)
188 (type . file)
189 (requires-pattern . 3)
6ff63d4 Thierry Volpiatto * helm.el (helm-read-pattern-maybe): Allow using history attr in sources...
thierryvolpiatto authored
190 (history . ,'helm-file-name-history)
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
191 (keymap . ,helm-generic-files-map)
192 (help-message . helm-generic-file-help-message)
193 (candidate-number-limit . 9999)
194 (mode-line . helm-generic-file-mode-line-string)
195 (delayed))
196 "Find files matching the current input pattern with locate.")
197
198 (defun helm-c-locate-read-file-name (prompt &optional init)
199 "Search a file with locate and return it's filename.
200 Use argument PROMPT and INIT for `helm' arguments
201 prompt and input."
202 (helm :sources
203 '((name . "Locate")
204 (candidates . helm-c-locate-init)
205 (action . identity)
206 (requires-pattern . 3)
207 (candidate-number-limit . 9999)
208 (mode-line . helm-generic-file-mode-line-string)
209 (delayed))
210 :prompt prompt
211 :input init
212 :buffer "*helm locate rfn*"))
213
214 ;;;###autoload
215 (defun helm-locate (arg)
216 "Preconfigured `helm' for Locate.
217 Note: you can add locate options after entering pattern.
218 See 'man locate' for valid options.
219
220 You can specify a specific database with prefix argument ARG \(C-u\).
221 Many databases can be used: navigate and mark them.
222 See also `helm-locate-with-db'.
223
224 To create a user specific db, use
225 \"updatedb -l 0 -o db_path -U directory\".
226 Where db_path is a filename matched by
227 `helm-locate-db-file-regexp'."
228 (interactive "P")
229 (setq helm-ff-default-directory default-directory)
230 (helm-locate-1 arg))
231
232 (provide 'helm-locate)
233
37b4201 Thierry Volpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
234 ;; Local Variables:
235 ;; coding: utf-8
236 ;; indent-tabs-mode: nil
237 ;; byte-compile-dynamic: t
238 ;; End:
239
6ffe9b7 Thierry Volpiatto * helm-config.el: Move locate to helm-locate.el
thierryvolpiatto authored
240 ;;; helm-locate.el ends here
Something went wrong with that request. Please try again.