Skip to content
This repository
Newer
Older
100644 309 lines (259 sloc) 11.002 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)
51
52
3d768cc4 »
2012-03-19 * helm-config.el: Move regexp and occur code to helm-regexp.el.
53 (defvar helm-occur-map
54 (let ((map (make-sparse-keymap)))
55 (set-keymap-parent map helm-map)
56 (define-key map (kbd "C-M-%") 'helm-occur-run-query-replace-regexp)
57 map)
58 "Keymap for `helm-occur'.")
59
60 (defvar helm-build-regexp-history nil)
61 (defun helm-c-query-replace-regexp (candidate)
62 "Query replace regexp from `helm-regexp'.
63 With a prefix arg replace only matches surrounded by word boundaries,
64 i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
65 (let ((regexp (funcall (helm-attr 'regexp))))
66 (apply 'query-replace-regexp
67 (helm-c-query-replace-args regexp))))
68
69 (defun helm-c-kill-regexp-as-sexp (candidate)
70 "Kill regexp in a format usable in lisp code."
71 (helm-c-regexp-kill-new
72 (prin1-to-string (funcall (helm-attr 'regexp)))))
73
74 (defun helm-c-kill-regexp (candidate)
75 "Kill regexp as it is in `helm-pattern'."
76 (helm-c-regexp-kill-new (funcall (helm-attr 'regexp))))
77
78 (defun helm-c-query-replace-args (regexp)
79 "create arguments of `query-replace-regexp' action in `helm-regexp'."
80 (let ((region-only (helm-region-active-p)))
81 (list
82 regexp
83 (query-replace-read-to regexp
84 (format "Query replace %sregexp %s"
85 (if helm-current-prefix-arg "word " "")
86 (if region-only "in region " ""))
87 t)
88 helm-current-prefix-arg
89 (when region-only (region-beginning))
90 (when region-only (region-end)))))
91
92 (defvar helm-c-source-regexp
93 '((name . "Regexp Builder")
94 (init . (lambda ()
95 (helm-candidate-buffer helm-current-buffer)))
96 (candidates-in-buffer)
97 (get-line . helm-c-regexp-get-line)
98 (persistent-action . helm-c-regexp-persistent-action)
99 (persistent-help . "Show this line")
100 (multiline)
101 (delayed)
102 (requires-pattern . 2)
103 (mode-line . "Press TAB to select action.")
104 (regexp . (lambda () helm-input))
105 (action . (("Kill Regexp as sexp" . helm-c-kill-regexp-as-sexp)
106 ("Query Replace Regexp (C-u Not inside word.)"
107 . helm-c-query-replace-regexp)
108 ("Kill Regexp" . helm-c-kill-regexp)))))
109
110 (defun helm-c-regexp-get-line (s e)
111 (propertize
112 (apply 'concat
113 ;; Line contents
114 (format "%5d: %s" (line-number-at-pos (1- s)) (buffer-substring s e))
115 ;; subexps
116 (loop for i from 0 to (1- (/ (length (match-data)) 2))
117 collect (format "\n %s'%s'"
118 (if (zerop i) "Group 0: " (format "Group %d: " i))
119 (match-string i))))
120 ;; match beginning
121 ;; KLUDGE: point of helm-candidate-buffer is +1 than that of helm-current-buffer.
122 ;; It is implementation problem of candidates-in-buffer.
123 'helm-realvalue
124 (1- s)))
125
126 (defun helm-c-regexp-persistent-action (pt)
127 (helm-goto-char pt)
128 (helm-persistent-highlight-point))
129
130 (defun helm-c-regexp-kill-new (input)
131 (kill-new input)
132 (message "Killed: %s" input))
133
134 (defun helm-quote-whitespace (candidate)
135 "Quote whitespace, if some, in string CANDIDATE."
136 (replace-regexp-in-string " " "\\\\ " candidate))
137
138 ;;; Occur
139 ;;
140 ;;
141 (defun helm-c-occur-init ()
142 "Create the initial helm occur buffer.
143 If region is active use region as buffer contents
144 instead of whole buffer."
145 (with-current-buffer (helm-candidate-buffer 'global)
146 (erase-buffer)
147 (let ((buf-contents
148 (with-helm-current-buffer
149 (if (helm-region-active-p)
150 (buffer-substring (region-beginning) (region-end))
151 (buffer-substring (point-min) (point-max))))))
152 (insert buf-contents))))
153
154 (defun helm-c-occur-get-line (s e)
155 (format "%7d:%s" (line-number-at-pos (1- s)) (buffer-substring s e)))
156
157 (defun helm-c-occur-query-replace-regexp (candidate)
158 "Query replace regexp starting from CANDIDATE.
159 If region is active ignore CANDIDATE and replace only in region.
160 With a prefix arg replace only matches surrounded by word boundaries,
161 i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
162 (let ((regexp helm-input))
163 (unless (helm-region-active-p)
164 (helm-c-action-line-goto candidate))
165 (apply 'query-replace-regexp
166 (helm-c-query-replace-args regexp))))
167
168 (defun helm-occur-run-query-replace-regexp ()
169 "Run `query-replace-regexp' in helm occur from keymap."
170 (interactive)
171 (helm-c-quit-and-execute-action
172 'helm-c-occur-query-replace-regexp))
173
174 (defvar helm-c-source-occur
175 `((name . "Occur")
176 (init . helm-c-occur-init)
177 (candidates-in-buffer)
178 (migemo)
179 (get-line . helm-c-occur-get-line)
180 (display-to-real . helm-c-display-to-real-line)
181 (action . (("Go to Line" . helm-c-action-line-goto)
182 ("Query replace regexp (C-u Not inside word.)"
183 . helm-c-occur-query-replace-regexp)))
184 (recenter)
185 (mode-line . helm-occur-mode-line)
186 (keymap . ,helm-occur-map)
187 (requires-pattern . 1)
188 (delayed)))
189
f5d0706e »
2012-03-22 * helm-config.el: Move browse-code to helm-regexp.el
190
191 ;;; Helm browse code.
192 (defun helm-c-browse-code-get-line (beg end)
193 "Select line if it match the regexp corresponding to current `major-mode'.
194 Line is parsed for BEG position to END position."
195 (let ((str-line (buffer-substring beg end))
196 (regexp (assoc-default major-mode
197 helm-c-browse-code-regexp-alist))
198 (num-line (if (string= helm-pattern "") beg (1- beg))))
199 (when (and regexp (string-match regexp str-line))
200 (format "%4d:%s" (line-number-at-pos num-line) str-line))))
201
202 (defvar helm-c-source-browse-code
203 '((name . "Browse code")
204 (init . (lambda ()
205 (helm-candidate-buffer helm-current-buffer)
206 (with-helm-current-buffer
207 (jit-lock-fontify-now))))
208 (candidate-number-limit . 9999)
209 (candidates-in-buffer)
210 (get-line . helm-c-browse-code-get-line)
211 (type . line)
212 (recenter)))
213
7974fc55 »
2012-03-28 Create autoload system in helm-config.
214 ;;; Type attributes
215 ;;
216 ;;
217 (define-helm-type-attribute 'line
218 '((display-to-real . helm-c-display-to-real-line)
219 (action ("Go to Line" . helm-c-action-line-goto)))
220 "LINENO:CONTENT string, eg. \" 16:foo\".
221
222 Optional `target-file' attribute is a name of target file.
223
224 Optional `before-jump-hook' attribute is a function with no
225 arguments which is called before jumping to position.
226
227 Optional `after-jump-hook' attribute is a function with no
228 arguments which is called after jumping to position.
229
230 If `adjust' attribute is specified, searches the line whose
231 content is CONTENT near the LINENO.
232
233 If `recenter' attribute is specified, the line is displayed at
234 the center of window, otherwise at the top of window.")
235
236 (define-helm-type-attribute 'file-line
237 `((filtered-candidate-transformer helm-c-filtered-candidate-transformer-file-line)
238 (multiline)
239 (action ("Go to" . helm-c-action-file-line-goto)))
240 "FILENAME:LINENO:CONTENT string, eg. \"~/.emacs:16:;; comment\".
241
242 Optional `default-directory' attribute is a default-directory
243 FILENAME is interpreted.
244
245 Optional `before-jump-hook' attribute is a function with no
246 arguments which is called before jumping to position.
247
248 Optional `after-jump-hook' attribute is a function with no
249 arguments which is called after jumping to position.
250
251 If `adjust' attribute is specified, searches the line whose
252 content is CONTENT near the LINENO.
253
254 If `recenter' attribute is specified, the line is displayed at
255 the center of window, otherwise at the top of window.")
256
f97a20cf »
2012-03-20 * helm-config.el: Move *find-files and related to helm-files.el
257 ;;;###autoload
258 (defun helm-regexp ()
259 "Preconfigured helm to build regexps.
260 `query-replace-regexp' can be run from there against found regexp."
261 (interactive)
262 (save-restriction
263 (let ((helm-compile-source-functions
264 ;; rule out helm-match-plugin because the input is one regexp.
265 (delq 'helm-compile-source--match-plugin
266 (copy-sequence helm-compile-source-functions))))
267 (when (and (helm-region-active-p)
268 ;; Don't narrow to region if buffer is already narrowed.
269 (not (helm-current-buffer-narrowed-p)))
270 (narrow-to-region (region-beginning) (region-end)))
271 (helm :sources helm-c-source-regexp
272 :buffer "*helm regexp*"
273 :prompt "Regexp: "
274 :history 'helm-build-regexp-history))))
275
e21f4c8f »
2012-03-20 * helm-config.el: Move lisp completion code to helm-elisp.el
276 ;;;###autoload
277 (defun helm-occur ()
278 "Preconfigured Helm for Occur source.
279 If region is active, search only in region,
280 otherwise search in whole buffer."
281 (interactive)
282 (let ((helm-compile-source-functions
283 ;; rule out helm-match-plugin because the input is one regexp.
284 (delq 'helm-compile-source--match-plugin
285 (copy-sequence helm-compile-source-functions))))
286 (helm :sources 'helm-c-source-occur
287 :buffer "*Helm Occur*"
288 :history 'helm-c-grep-history)))
289
f5d0706e »
2012-03-22 * helm-config.el: Move browse-code to helm-regexp.el
290 ;;;###autoload
291 (defun helm-browse-code ()
292 "Preconfigured helm to browse code."
293 (interactive)
294 (helm :sources 'helm-c-source-browse-code
295 :buffer "*helm browse code*"
296 :default (thing-at-point 'symbol)))
297
298
3d768cc4 »
2012-03-19 * helm-config.el: Move regexp and occur code to helm-regexp.el.
299 (provide 'helm-regexp)
300
37b42017 »
2012-03-28 Fix dependencies and autoloads. Reorder.
301 ;; Local Variables:
a7ee65b7 »
2012-07-30 Disable compile warnings and add new keyword to helm.
302 ;; byte-compile-warnings: (not cl-functions obsolete)
37b42017 »
2012-03-28 Fix dependencies and autoloads. Reorder.
303 ;; coding: utf-8
304 ;; indent-tabs-mode: nil
305 ;; byte-compile-dynamic: t
306 ;; End:
307
3d768cc4 »
2012-03-19 * helm-config.el: Move regexp and occur code to helm-regexp.el.
308 ;;; helm-regexp.el ends here
Something went wrong with that request. Please try again.