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