Skip to content
Newer
Older
100644 196 lines (170 sloc) 7.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)
22
23 (defvar helm-occur-map
24 (let ((map (make-sparse-keymap)))
25 (set-keymap-parent map helm-map)
26 (define-key map (kbd "C-M-%") 'helm-occur-run-query-replace-regexp)
27 map)
28 "Keymap for `helm-occur'.")
29
30 (defvar helm-build-regexp-history nil)
31 (defun helm-c-query-replace-regexp (candidate)
32 "Query replace regexp from `helm-regexp'.
33 With a prefix arg replace only matches surrounded by word boundaries,
34 i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
35 (let ((regexp (funcall (helm-attr 'regexp))))
36 (apply 'query-replace-regexp
37 (helm-c-query-replace-args regexp))))
38
39 (defun helm-c-kill-regexp-as-sexp (candidate)
40 "Kill regexp in a format usable in lisp code."
41 (helm-c-regexp-kill-new
42 (prin1-to-string (funcall (helm-attr 'regexp)))))
43
44 (defun helm-c-kill-regexp (candidate)
45 "Kill regexp as it is in `helm-pattern'."
46 (helm-c-regexp-kill-new (funcall (helm-attr 'regexp))))
47
48 (defun helm-c-query-replace-args (regexp)
49 "create arguments of `query-replace-regexp' action in `helm-regexp'."
50 (let ((region-only (helm-region-active-p)))
51 (list
52 regexp
53 (query-replace-read-to regexp
54 (format "Query replace %sregexp %s"
55 (if helm-current-prefix-arg "word " "")
56 (if region-only "in region " ""))
57 t)
58 helm-current-prefix-arg
59 (when region-only (region-beginning))
60 (when region-only (region-end)))))
61
62 (defvar helm-c-source-regexp
63 '((name . "Regexp Builder")
64 (init . (lambda ()
65 (helm-candidate-buffer helm-current-buffer)))
66 (candidates-in-buffer)
67 (get-line . helm-c-regexp-get-line)
68 (persistent-action . helm-c-regexp-persistent-action)
69 (persistent-help . "Show this line")
70 (multiline)
71 (delayed)
72 (requires-pattern . 2)
73 (mode-line . "Press TAB to select action.")
74 (regexp . (lambda () helm-input))
75 (action . (("Kill Regexp as sexp" . helm-c-kill-regexp-as-sexp)
76 ("Query Replace Regexp (C-u Not inside word.)"
77 . helm-c-query-replace-regexp)
78 ("Kill Regexp" . helm-c-kill-regexp)))))
79
80 (defun helm-c-regexp-get-line (s e)
81 (propertize
82 (apply 'concat
83 ;; Line contents
84 (format "%5d: %s" (line-number-at-pos (1- s)) (buffer-substring s e))
85 ;; subexps
86 (loop for i from 0 to (1- (/ (length (match-data)) 2))
87 collect (format "\n %s'%s'"
88 (if (zerop i) "Group 0: " (format "Group %d: " i))
89 (match-string i))))
90 ;; match beginning
91 ;; KLUDGE: point of helm-candidate-buffer is +1 than that of helm-current-buffer.
92 ;; It is implementation problem of candidates-in-buffer.
93 'helm-realvalue
94 (1- s)))
95
96 (defun helm-c-regexp-persistent-action (pt)
97 (helm-goto-char pt)
98 (helm-persistent-highlight-point))
99
100 (defun helm-c-regexp-kill-new (input)
101 (kill-new input)
102 (message "Killed: %s" input))
103
104 (defun helm-quote-whitespace (candidate)
105 "Quote whitespace, if some, in string CANDIDATE."
106 (replace-regexp-in-string " " "\\\\ " candidate))
107
108 ;;; Occur
109 ;;
110 ;;
111 (defun helm-c-occur-init ()
112 "Create the initial helm occur buffer.
113 If region is active use region as buffer contents
114 instead of whole buffer."
115 (with-current-buffer (helm-candidate-buffer 'global)
116 (erase-buffer)
117 (let ((buf-contents
118 (with-helm-current-buffer
119 (if (helm-region-active-p)
120 (buffer-substring (region-beginning) (region-end))
121 (buffer-substring (point-min) (point-max))))))
122 (insert buf-contents))))
123
124 (defun helm-c-occur-get-line (s e)
125 (format "%7d:%s" (line-number-at-pos (1- s)) (buffer-substring s e)))
126
127 (defun helm-c-occur-query-replace-regexp (candidate)
128 "Query replace regexp starting from CANDIDATE.
129 If region is active ignore CANDIDATE and replace only in region.
130 With a prefix arg replace only matches surrounded by word boundaries,
131 i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
132 (let ((regexp helm-input))
133 (unless (helm-region-active-p)
134 (helm-c-action-line-goto candidate))
135 (apply 'query-replace-regexp
136 (helm-c-query-replace-args regexp))))
137
138 (defun helm-occur-run-query-replace-regexp ()
139 "Run `query-replace-regexp' in helm occur from keymap."
140 (interactive)
141 (helm-c-quit-and-execute-action
142 'helm-c-occur-query-replace-regexp))
143
144 (defvar helm-c-source-occur
145 `((name . "Occur")
146 (init . helm-c-occur-init)
147 (candidates-in-buffer)
148 (migemo)
149 (get-line . helm-c-occur-get-line)
150 (display-to-real . helm-c-display-to-real-line)
151 (action . (("Go to Line" . helm-c-action-line-goto)
152 ("Query replace regexp (C-u Not inside word.)"
153 . helm-c-occur-query-replace-regexp)))
154 (recenter)
155 (mode-line . helm-occur-mode-line)
156 (keymap . ,helm-occur-map)
157 (requires-pattern . 1)
158 (delayed)))
159
f97a20c @thierryvolpiatto * helm-config.el: Move *find-files and related to helm-files.el
thierryvolpiatto authored
160 ;;;###autoload
161 (defun helm-regexp ()
162 "Preconfigured helm to build regexps.
163 `query-replace-regexp' can be run from there against found regexp."
164 (interactive)
165 (save-restriction
166 (let ((helm-compile-source-functions
167 ;; rule out helm-match-plugin because the input is one regexp.
168 (delq 'helm-compile-source--match-plugin
169 (copy-sequence helm-compile-source-functions))))
170 (when (and (helm-region-active-p)
171 ;; Don't narrow to region if buffer is already narrowed.
172 (not (helm-current-buffer-narrowed-p)))
173 (narrow-to-region (region-beginning) (region-end)))
174 (helm :sources helm-c-source-regexp
175 :buffer "*helm regexp*"
176 :prompt "Regexp: "
177 :history 'helm-build-regexp-history))))
178
e21f4c8 @thierryvolpiatto * helm-config.el: Move lisp completion code to helm-elisp.el
thierryvolpiatto authored
179 ;;;###autoload
180 (defun helm-occur ()
181 "Preconfigured Helm for Occur source.
182 If region is active, search only in region,
183 otherwise search in whole buffer."
184 (interactive)
185 (let ((helm-compile-source-functions
186 ;; rule out helm-match-plugin because the input is one regexp.
187 (delq 'helm-compile-source--match-plugin
188 (copy-sequence helm-compile-source-functions))))
189 (helm :sources 'helm-c-source-occur
190 :buffer "*Helm Occur*"
191 :history 'helm-c-grep-history)))
192
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
193 (provide 'helm-regexp)
194
195 ;;; helm-regexp.el ends here
Something went wrong with that request. Please try again.