Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 419 lines (358 sloc) 15.03 kb
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
1 ;;; helm-regexp.el --- In buffer regexp searching and replacement for helm.
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 ;;; Code:
19
20 (require 'cl)
21 (require 'helm)
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
22 (require 'helm-utils)
23
24
25 (defgroup helm-regexp nil
26 "Regexp related Applications and libraries for Helm."
27 :group 'helm)
28
29 (defcustom helm-c-browse-code-regexp-lisp
30 "^ *\(def\\(un\\|subst\\|macro\\|face\\|alias\\|advice\\|struct\\|\
31 type\\|theme\\|var\\|group\\|custom\\|const\\|method\\|class\\)"
32 "Regexp used to parse lisp buffer when browsing code."
33 :type 'string
34 :group 'helm-regexp)
35
36 (defcustom helm-c-browse-code-regexp-python
37 "\\<def\\>\\|\\<class\\>"
38 "Regexp used to parse python buffer when browsing code."
39 :type 'string
40 :group 'helm-regexp)
41
42 (defcustom helm-c-browse-code-regexp-alist
43 `((lisp-interaction-mode . ,helm-c-browse-code-regexp-lisp)
44 (emacs-lisp-mode . ,helm-c-browse-code-regexp-lisp)
45 (lisp-mode . ,helm-c-browse-code-regexp-lisp)
46 (python-mode . ,helm-c-browse-code-regexp-python))
47 "Alist to store regexps for browsing code corresponding \
48 to a specific `major-mode'."
d079fc3 @thierryvolpiatto Issue #95 Fix defcustoms in various places.
thierryvolpiatto authored
49 :type '(alist :key-type symbol :value-type regexp)
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
50 :group 'helm-regexp)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
51
52 (defface helm-moccur-buffer
53 '((t (:foreground "DarkTurquoise" :underline t)))
54 "Face used to highlight moccur buffer names."
55 :group 'helm-regexp)
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
56
57
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
58 (defvar helm-occur-map
59 (let ((map (make-sparse-keymap)))
60 (set-keymap-parent map helm-map)
61 (define-key map (kbd "C-M-%") 'helm-occur-run-query-replace-regexp)
62 map)
63 "Keymap for `helm-occur'.")
64
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
65 (defvar helm-c-moccur-map
66 (let ((map (make-sparse-keymap)))
67 (set-keymap-parent map helm-map)
68 (define-key map (kbd "M-<down>") 'helm-c-goto-next-file)
69 (define-key map (kbd "M-<up>") 'helm-c-goto-precedent-file)
70 (define-key map (kbd "C-w") 'helm-yank-text-at-point)
b3194af @thierryvolpiatto Add help for moccur.
thierryvolpiatto authored
71 (define-key map (kbd "C-c ?") 'helm-moccur-help)
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
72 (delq nil map))
73 "Keymap used in Moccur source.")
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
74
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
75 (defvar helm-build-regexp-history nil)
76 (defun helm-c-query-replace-regexp (candidate)
77 "Query replace regexp from `helm-regexp'.
78 With a prefix arg replace only matches surrounded by word boundaries,
79 i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
80 (let ((regexp (funcall (helm-attr 'regexp))))
81 (apply 'query-replace-regexp
82 (helm-c-query-replace-args regexp))))
83
84 (defun helm-c-kill-regexp-as-sexp (candidate)
85 "Kill regexp in a format usable in lisp code."
86 (helm-c-regexp-kill-new
87 (prin1-to-string (funcall (helm-attr 'regexp)))))
88
89 (defun helm-c-kill-regexp (candidate)
90 "Kill regexp as it is in `helm-pattern'."
91 (helm-c-regexp-kill-new (funcall (helm-attr 'regexp))))
92
93 (defun helm-c-query-replace-args (regexp)
94 "create arguments of `query-replace-regexp' action in `helm-regexp'."
95 (let ((region-only (helm-region-active-p)))
96 (list
97 regexp
98 (query-replace-read-to regexp
99 (format "Query replace %sregexp %s"
100 (if helm-current-prefix-arg "word " "")
101 (if region-only "in region " ""))
102 t)
103 helm-current-prefix-arg
104 (when region-only (region-beginning))
105 (when region-only (region-end)))))
106
107 (defvar helm-c-source-regexp
108 '((name . "Regexp Builder")
109 (init . (lambda ()
110 (helm-candidate-buffer helm-current-buffer)))
111 (candidates-in-buffer)
112 (get-line . helm-c-regexp-get-line)
113 (persistent-action . helm-c-regexp-persistent-action)
114 (persistent-help . "Show this line")
115 (multiline)
116 (delayed)
117 (requires-pattern . 2)
118 (mode-line . "Press TAB to select action.")
119 (regexp . (lambda () helm-input))
120 (action . (("Kill Regexp as sexp" . helm-c-kill-regexp-as-sexp)
121 ("Query Replace Regexp (C-u Not inside word.)"
122 . helm-c-query-replace-regexp)
123 ("Kill Regexp" . helm-c-kill-regexp)))))
124
125 (defun helm-c-regexp-get-line (s e)
126 (propertize
127 (apply 'concat
128 ;; Line contents
129 (format "%5d: %s" (line-number-at-pos (1- s)) (buffer-substring s e))
130 ;; subexps
131 (loop for i from 0 to (1- (/ (length (match-data)) 2))
132 collect (format "\n %s'%s'"
133 (if (zerop i) "Group 0: " (format "Group %d: " i))
134 (match-string i))))
135 ;; match beginning
136 ;; KLUDGE: point of helm-candidate-buffer is +1 than that of helm-current-buffer.
137 ;; It is implementation problem of candidates-in-buffer.
138 'helm-realvalue
139 (1- s)))
140
141 (defun helm-c-regexp-persistent-action (pt)
142 (helm-goto-char pt)
143 (helm-persistent-highlight-point))
144
145 (defun helm-c-regexp-kill-new (input)
146 (kill-new input)
147 (message "Killed: %s" input))
148
149 (defun helm-quote-whitespace (candidate)
150 "Quote whitespace, if some, in string CANDIDATE."
151 (replace-regexp-in-string " " "\\\\ " candidate))
152
153 ;;; Occur
154 ;;
155 ;;
156 (defun helm-c-occur-init ()
157 "Create the initial helm occur buffer.
158 If region is active use region as buffer contents
159 instead of whole buffer."
160 (with-current-buffer (helm-candidate-buffer 'global)
161 (erase-buffer)
162 (let ((buf-contents
163 (with-helm-current-buffer
164 (if (helm-region-active-p)
165 (buffer-substring (region-beginning) (region-end))
166 (buffer-substring (point-min) (point-max))))))
167 (insert buf-contents))))
168
169 (defun helm-c-occur-get-line (s e)
170 (format "%7d:%s" (line-number-at-pos (1- s)) (buffer-substring s e)))
171
172 (defun helm-c-occur-query-replace-regexp (candidate)
173 "Query replace regexp starting from CANDIDATE.
174 If region is active ignore CANDIDATE and replace only in region.
175 With a prefix arg replace only matches surrounded by word boundaries,
176 i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
177 (let ((regexp helm-input))
178 (unless (helm-region-active-p)
179 (helm-c-action-line-goto candidate))
180 (apply 'query-replace-regexp
181 (helm-c-query-replace-args regexp))))
182
183 (defun helm-occur-run-query-replace-regexp ()
184 "Run `query-replace-regexp' in helm occur from keymap."
185 (interactive)
186 (helm-c-quit-and-execute-action
187 'helm-c-occur-query-replace-regexp))
188
189 (defvar helm-c-source-occur
190 `((name . "Occur")
191 (init . helm-c-occur-init)
192 (candidates-in-buffer)
193 (get-line . helm-c-occur-get-line)
194 (display-to-real . helm-c-display-to-real-line)
195 (action . (("Go to Line" . helm-c-action-line-goto)
196 ("Query replace regexp (C-u Not inside word.)"
197 . helm-c-occur-query-replace-regexp)))
198 (recenter)
199 (mode-line . helm-occur-mode-line)
200 (keymap . ,helm-occur-map)
201 (requires-pattern . 1)
202 (delayed)))
203
f5d0706 @thierryvolpiatto * helm-config.el: Move browse-code to helm-regexp.el
thierryvolpiatto authored
204
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
205 ;;; Multi occur
206 ;;
207 ;;
208 (defun helm-m-occur-init (buffers)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
209 "Create the initial helm multi occur buffer."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
210 (helm-init-candidates-in-buffer
c23a0af @thierryvolpiatto * helm.el (helm-init-candidates-in-buffer): Allow a symbol for BUFFER ar...
thierryvolpiatto authored
211 'global
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
212 (loop for buf in buffers
213 for bufstr = (with-current-buffer buf (buffer-string))
214 do (add-text-properties
215 0 (length bufstr)
216 `(buffer-name ,(buffer-name (get-buffer buf)))
217 bufstr)
218 concat bufstr)))
219
220 (defun helm-m-occur-get-line (s e)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
221 "Format line for `helm-c-source-moccur'."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
222 (format "%s:%d:%s"
223 (get-text-property (point-at-bol) 'buffer-name)
224 (save-restriction
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
225 (narrow-to-region (previous-single-property-change
226 (point) 'buffer-name)
227 (next-single-property-change
228 (point) 'buffer-name))
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
229 (line-number-at-pos s))
230 (buffer-substring s e)))
231
232 (defun* helm-m-occur-action (candidate
233 &optional (method (quote buffer)))
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
234 "Jump to CANDIDATE with METHOD.
235 arg METHOD can be one of buffer, buffer-other-window, buffer-other-frame."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
236 (let* ((split (split-string candidate ":" t))
237 (buf (car split))
238 (lineno (string-to-number (nth 1 split))))
239 (case method
240 (buffer (switch-to-buffer buf))
241 (buffer-other-window (switch-to-buffer-other-window buf))
242 (buffer-other-frame (switch-to-buffer-other-frame buf)))
243 (helm-goto-line lineno)))
244
245 (defun helm-m-occur-goto-line (candidate)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
246 "From multi occur, switch to buffer and go to nth 1 CANDIDATE line."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
247 (helm-m-occur-action candidate))
248
249 (defvar helm-c-source-moccur
250 `((name . "Moccur")
251 (init . (lambda ()
252 (helm-m-occur-init buffers)))
253 (candidates-in-buffer)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
254 (filtered-candidate-transformer . helm-m-occur-transformer)
255 (nohighlight)
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
256 (get-line . helm-m-occur-get-line)
257 (action . (("Go to Line" . helm-m-occur-goto-line)))
258 (recenter)
259 (candidate-number-limit . 9999)
b3194af @thierryvolpiatto Add help for moccur.
thierryvolpiatto authored
260 (mode-line . helm-moccur-mode-line)
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
261 (keymap . ,helm-c-moccur-map)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
262 (requires-pattern . 3))
263 "Helm source for multi occur.")
264
265 (defun helm-m-occur-transformer (candidates source)
266 "Transformer function for `helm-c-source-moccur'."
267 (require 'helm-grep)
268 (loop for i in candidates
269 for split = (split-string i ":" t)
270 for buf = (car split)
271 for lineno = (nth 1 split)
272 for str = (nth 2 split)
273 collect (cons (concat (propertize buf 'face 'helm-moccur-buffer
274 'buffer-name buf)
275 ":"
276 (propertize lineno 'face 'helm-grep-lineno)
277 ":"
278 (helm-c-grep-highlight-match str))
279 i)))
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
280
281 (defun helm-multi-occur-1 (buffers)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
282 "Main function to call `helm-c-source-moccur' with BUFFERS list."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
283 (declare (special buffers))
284 (helm :sources 'helm-c-source-moccur
285 :buffer "*helm moccur*"))
286
287
f5d0706 @thierryvolpiatto * helm-config.el: Move browse-code to helm-regexp.el
thierryvolpiatto authored
288 ;;; Helm browse code.
289 (defun helm-c-browse-code-get-line (beg end)
290 "Select line if it match the regexp corresponding to current `major-mode'.
291 Line is parsed for BEG position to END position."
292 (let ((str-line (buffer-substring beg end))
293 (regexp (assoc-default major-mode
294 helm-c-browse-code-regexp-alist))
295 (num-line (if (string= helm-pattern "") beg (1- beg))))
296 (when (and regexp (string-match regexp str-line))
297 (format "%4d:%s" (line-number-at-pos num-line) str-line))))
298
299 (defvar helm-c-source-browse-code
300 '((name . "Browse code")
301 (init . (lambda ()
302 (helm-candidate-buffer helm-current-buffer)
303 (with-helm-current-buffer
304 (jit-lock-fontify-now))))
305 (candidate-number-limit . 9999)
306 (candidates-in-buffer)
307 (get-line . helm-c-browse-code-get-line)
308 (type . line)
309 (recenter)))
310
7974fc5 @thierryvolpiatto Create autoload system in helm-config.
thierryvolpiatto authored
311 ;;; Type attributes
312 ;;
313 ;;
314 (define-helm-type-attribute 'line
315 '((display-to-real . helm-c-display-to-real-line)
316 (action ("Go to Line" . helm-c-action-line-goto)))
317 "LINENO:CONTENT string, eg. \" 16:foo\".
318
319 Optional `target-file' attribute is a name of target file.
320
321 Optional `before-jump-hook' attribute is a function with no
322 arguments which is called before jumping to position.
323
324 Optional `after-jump-hook' attribute is a function with no
325 arguments which is called after jumping to position.
326
327 If `adjust' attribute is specified, searches the line whose
328 content is CONTENT near the LINENO.
329
330 If `recenter' attribute is specified, the line is displayed at
331 the center of window, otherwise at the top of window.")
332
333 (define-helm-type-attribute 'file-line
334 `((filtered-candidate-transformer helm-c-filtered-candidate-transformer-file-line)
335 (multiline)
336 (action ("Go to" . helm-c-action-file-line-goto)))
337 "FILENAME:LINENO:CONTENT string, eg. \"~/.emacs:16:;; comment\".
338
339 Optional `default-directory' attribute is a default-directory
340 FILENAME is interpreted.
341
342 Optional `before-jump-hook' attribute is a function with no
343 arguments which is called before jumping to position.
344
345 Optional `after-jump-hook' attribute is a function with no
346 arguments which is called after jumping to position.
347
348 If `adjust' attribute is specified, searches the line whose
349 content is CONTENT near the LINENO.
350
351 If `recenter' attribute is specified, the line is displayed at
352 the center of window, otherwise at the top of window.")
353
f97a20c @thierryvolpiatto * helm-config.el: Move *find-files and related to helm-files.el
thierryvolpiatto authored
354 ;;;###autoload
355 (defun helm-regexp ()
356 "Preconfigured helm to build regexps.
357 `query-replace-regexp' can be run from there against found regexp."
358 (interactive)
359 (save-restriction
360 (let ((helm-compile-source-functions
361 ;; rule out helm-match-plugin because the input is one regexp.
362 (delq 'helm-compile-source--match-plugin
363 (copy-sequence helm-compile-source-functions))))
364 (when (and (helm-region-active-p)
365 ;; Don't narrow to region if buffer is already narrowed.
366 (not (helm-current-buffer-narrowed-p)))
367 (narrow-to-region (region-beginning) (region-end)))
368 (helm :sources helm-c-source-regexp
369 :buffer "*helm regexp*"
370 :prompt "Regexp: "
371 :history 'helm-build-regexp-history))))
372
e21f4c8 @thierryvolpiatto * helm-config.el: Move lisp completion code to helm-elisp.el
thierryvolpiatto authored
373 ;;;###autoload
374 (defun helm-occur ()
375 "Preconfigured Helm for Occur source.
376 If region is active, search only in region,
377 otherwise search in whole buffer."
378 (interactive)
379 (let ((helm-compile-source-functions
380 ;; rule out helm-match-plugin because the input is one regexp.
381 (delq 'helm-compile-source--match-plugin
382 (copy-sequence helm-compile-source-functions))))
383 (helm :sources 'helm-c-source-occur
384 :buffer "*Helm Occur*"
385 :history 'helm-c-grep-history)))
386
f5d0706 @thierryvolpiatto * helm-config.el: Move browse-code to helm-regexp.el
thierryvolpiatto authored
387 ;;;###autoload
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
388 (defun helm-multi-occur ()
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
389 "Preconfigured helm for multi occur."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
390 (interactive)
9f15d07 @thierryvolpiatto Add transformer function to multi occur.
thierryvolpiatto authored
391 (let ((helm-compile-source-functions
392 ;; rule out helm-match-plugin because the input is one regexp.
393 (delq 'helm-compile-source--match-plugin
394 (copy-sequence helm-compile-source-functions)))
395 (buffers (helm-comp-read
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
396 "Buffers: " (helm-c-buffer-list)
397 :marked-candidates t)))
398 (helm-multi-occur-1 buffers)))
399
400 ;;;###autoload
f5d0706 @thierryvolpiatto * helm-config.el: Move browse-code to helm-regexp.el
thierryvolpiatto authored
401 (defun helm-browse-code ()
402 "Preconfigured helm to browse code."
403 (interactive)
404 (helm :sources 'helm-c-source-browse-code
405 :buffer "*helm browse code*"
406 :default (thing-at-point 'symbol)))
407
408
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
409 (provide 'helm-regexp)
410
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
411 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
412 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
413 ;; coding: utf-8
414 ;; indent-tabs-mode: nil
415 ;; byte-compile-dynamic: t
416 ;; End:
417
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
418 ;;; helm-regexp.el ends here
Something went wrong with that request. Please try again.