forked from kimvanwyk/ack-and-a-half
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ack-and-a-half.el
529 lines (475 loc) · 20.7 KB
/
ack-and-a-half.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
;;; ack-and-a-half.el --- Yet another front-end for ack
;;
;; Copyright (C) 2013 Jacob Helwig <[email protected]>
;; Alexey Lebedeff <[email protected]>
;; Andrew Pennebaker <[email protected]>
;; Andrew Stine <[email protected]>
;; Derek Chen-Becker <[email protected]>
;; Gleb Peregud <[email protected]>
;; Kim van Wyk <[email protected]>
;; Lars Andersen <[email protected]>
;; Ronaldo M. Ferraz <[email protected]>
;; Ryan Thompson <[email protected]>
;;
;; Author: Jacob Helwig <[email protected]>
;; Homepage: http://technosorcery.net
;; Version: 1.2.0
;; URL: https://github.com/jhelwig/ack-and-a-half
;;
;; This file is NOT part of GNU Emacs.
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy of
;; this software and associated documentation files (the "Software"), to deal in
;; the Software without restriction, including without limitation the rights to
;; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is furnished to do
;; so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;
;;; Commentary:
;;
;; ack-and-a-half.el provides a simple compilation mode for the perl
;; grep-a-like ack (http://petdance.com/ack/).
;;
;; Add the following to your .emacs:
;;
;; (add-to-list 'load-path "/path/to/ack-and-a-half")
;; (require 'ack-and-a-half)
;; (defalias 'ack 'ack-and-a-half)
;; (defalias 'ack-same 'ack-and-a-half-same)
;; (defalias 'ack-find-file 'ack-and-a-half-find-file)
;; (defalias 'ack-find-file-same 'ack-and-a-half-find-file-same)
;;
;; Run `ack' to search for all files and `ack-same' to search for
;; files of the same type as the current buffer.
;;
;; `next-error' and `previous-error' can be used to jump to the
;; matches.
;;
;; `ack-find-file' and `ack-find-same-file' use ack to list the files
;; in the current project. It's a convenient, though slow, way of
;; finding files.
;;
;;; Code:
(eval-when-compile (require 'cl))
(require 'compile)
(require 'grep)
(require 'thingatpt)
(add-to-list 'debug-ignored-errors
"^Moved \\(back before fir\\|past la\\)st match$")
(add-to-list 'debug-ignored-errors "^File .* not found$")
(define-compilation-mode ack-and-a-half-mode "Ack"
"Ack results compilation mode."
(set (make-local-variable 'truncate-lines) t)
(set (make-local-variable 'compilation-disable-input) t)
(let ((smbl 'compilation-ack-nogroup)
(pttrn '("^\\([^:\n]+?\\):\\([0-9]+\\):\\([0-9]+\\):" 1 2 3)))
(set (make-local-variable 'compilation-error-regexp-alist) (list smbl))
(set (make-local-variable 'compilation-error-regexp-alist-alist) (list (cons smbl pttrn))))
(set (make-local-variable 'compilation-process-setup-function) 'ack-and-a-half-mode-setup)
(set (make-local-variable 'compilation-error-face) grep-hit-face))
(defgroup ack-and-a-half nil "Yet another front end for ack."
:group 'tools
:group 'matching)
(defcustom ack-and-a-half-executable (or (executable-find "ack")
(executable-find "ack-grep"))
"*The location of the ack executable."
:group 'ack-and-a-half
:type 'file)
(defcustom ack-and-a-half-buffer-name "*Ack-and-a-half*"
"*The name of the ack-and-a-half buffer."
:group 'ack-and-a-half
:type 'string)
(defun ack-buffer-name (mode) ack-and-a-half-buffer-name)
(defcustom ack-and-a-half-arguments nil
"*Extra arguments to pass to ack."
:group 'ack-and-a-half
:type '(repeat (string)))
(defcustom ack-and-a-half-mode-type-alist nil
"*File type(s) to search per major mode. (ack-and-a-half-same)
This overrides values in `ack-and-a-half-mode-type-default-alist'.
The car in each list element is a major mode, and the rest
is a list of strings passed to the --type flag of ack when running
`ack-and-a-half-same'."
:group 'ack-and-a-half
:type '(repeat (cons (symbol :tag "Major mode")
(repeat (string :tag "ack --type")))))
(defcustom ack-and-a-half-mode-extension-alist nil
"*File extensions to search per major mode. (ack-and-a-half-same)
This overrides values in `ack-and-a-half-mode-extension-default-alist'.
The car in each list element is a major mode, and the rest
is a list of file extensions to be searched in addition to
the type defined in `ack-and-a-half-mode-type-alist' when
running `ack-and-a-half-same'."
:group 'ack-and-a-half
:type '(repeat (cons (symbol :tag "Major mode")
(repeat :tag "File extensions" (string)))))
(defcustom ack-and-a-half-ignore-case 'smart
"*Whether or not to ignore case when searching.
The special value 'smart enables the ack option \"smart-case\"."
:group 'ack-and-a-half
:type '(choice (const :tag "Case sensitive" nil)
(const :tag "Smart case" 'smart)
(const :tag "Case insensitive" t)))
(defcustom ack-and-a-half-regexp-search t
"*Default to regular expression searching.
Giving a prefix argument to `ack-and-a-half' toggles this option."
:group 'ack-and-a-half
:type '(choice (const :tag "Literal searching" nil)
(const :tag "Regular expression searching" t)))
(defcustom ack-and-a-half-use-environment t
"*Use .ackrc and ACK_OPTIONS when searching."
:group 'ack-and-a-half
:type '(choice (const :tag "Ignore environment" nil)
(const :tag "Use environment" t)))
(defcustom ack-and-a-half-root-directory-functions '(ack-and-a-half-guess-project-root)
"*List of functions used to find the base directory to ack from.
These functions are called until one returns a directory. If successful,
`ack-and-a-half' is run from that directory instead of from `default-directory'.
The directory is verified by the user depending on `ack-and-a-half-prompt-for-directory'."
:group 'ack-and-a-half
:type '(repeat function))
(defcustom ack-and-a-half-project-root-file-patterns
'(".project\\'"
".xcodeproj\\'"
".sln\\'"
"\\`Project.ede\\'"
"\\`.git\\'"
"\\`.bzr\\'"
"\\`_darcs\\'"
"\\`.hg\\'")
"*List of file patterns for the project root (used by `ack-and-a-half-guess-project-root').
Each element is a regular expression. If a file matching any element is
found in a directory, then that directory is assumed to be the project
root by `ack-and-a-half-guess-project-root'."
:group 'ack-and-a-half
:type '(repeat (string :tag "Regular expression")))
(defcustom ack-and-a-half-prompt-for-directory 'unless-guessed
"*Prompt for directory in which to run ack.
If this is 'unless-guessed, then the value determined by
`ack-and-a-half-root-directory-functions' is used without
confirmation. If it is nil, then the directory is never
confirmed. If t, then always prompt for the directory to use."
:group 'ack-and-a-half
:type '(choice (const :tag "Don't prompt" nil)
(const :tag "Don't prompt when guessed" unless-guessed)
(const :tag "Always prompt" t)))
(defcustom ack-and-a-half-use-ido nil
"Whether or not ack-and-a-half should use ido to provide
completion suggestions when prompting for directory.")
;;; Default setting lists ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconst ack-and-a-half-mode-type-default-alist
'((actionscript-mode "actionscript")
(LaTeX-mode "tex")
(TeX-mode "tex")
(asm-mode "asm")
(batch-file-mode "batch")
(c++-mode "cpp")
(c-mode "cc")
(cfmx-mode "cfmx")
(cperl-mode "perl")
(csharp-mode "csharp")
(css-mode "css")
(emacs-lisp-mode "elisp")
(erlang-mode "erlang")
(espresso-mode "java")
(fortran-mode "fortran")
(haskell-mode "haskell")
(hexl-mode "binary")
(html-mode "html")
(java-mode "java")
(javascript-mode "js")
(jde-mode "java")
(js2-mode "js")
(jsp-mode "jsp")
(latex-mode "tex")
(lisp-mode "lisp")
(lua-mode "lua")
(makefile-mode "make")
(mason-mode "mason")
(nxml-mode "xml")
(objc-mode "objc" "objcpp")
(ocaml-mode "ocaml")
(parrot-mode "parrot")
(perl-mode "perl")
(php-mode "php")
(plone-mode "plone")
(python-mode "python")
(ruby-mode "ruby")
(scala-mode "scala")
(scheme-mode "scheme")
(shell-script-mode "shell")
(skipped-mode "skipped")
(smalltalk-mode "smalltalk")
(sql-mode "sql")
(tcl-mode "tcl")
(tex-mode "tex")
(tt-mode "tt")
(vb-mode "vb")
(vim-mode "vim")
(xml-mode "xml")
(yaml-mode "yaml"))
"Default values for `ack-and-a-half-mode-type-alist'.")
(defconst ack-and-a-half-mode-extension-default-alist
'((d-mode "d"))
"Default values for `ack-and-a-half-mode-extension-alist'.")
(defun ack-and-a-half-create-type (extensions)
(list "--type-set"
(concat "ack-and-a-half-custom-type=" (mapconcat 'identity extensions ","))
"--type" "ack-and-a-half-custom-type"))
(defun ack-and-a-half-type-for-major-mode (mode)
"Return the --type and --type-set arguments to use with ack for major mode MODE."
(let ((types (cdr (or (assoc mode ack-and-a-half-mode-type-alist)
(assoc mode ack-and-a-half-mode-type-default-alist))))
(ext (cdr (or (assoc mode ack-and-a-half-mode-extension-alist)
(assoc mode ack-and-a-half-mode-extension-default-alist))))
result)
(dolist (type types)
(push type result)
(push "--type" result))
(if ext
(if types
`("--type-add" ,(concat (car types)
"=" (mapconcat 'identity ext ","))
. ,result)
(ack-and-a-half-create-type ext))
result)))
;;; Project root ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ack-and-a-half-guess-project-root ()
"Guess the project root directory.
This is intended to be used in `ack-and-a-half-root-directory-functions'."
(catch 'root
(let ((dir (expand-file-name (if buffer-file-name
(file-name-directory buffer-file-name)
default-directory)))
(pattern (mapconcat 'identity ack-and-a-half-project-root-file-patterns "\\|")))
(while (not (equal dir "/"))
(when (directory-files dir nil pattern t)
(throw 'root dir))
(setq dir (file-name-directory (directory-file-name dir)))))))
;;; Commands ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar ack-and-a-half-directory-history nil
"Directories recently searched with `ack-and-a-half'.")
(defvar ack-and-a-half-literal-history nil
"Strings recently searched for with `ack-and-a-half'.")
(defvar ack-and-a-half-regexp-history nil
"Regular expressions recently searched for with `ack-and-a-half'.")
(defun ack-and-a-half-initial-contents-for-read ()
(when (ack-and-a-half-use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))))
(defun ack-and-a-half-default-for-read ()
(unless (ack-and-a-half-use-region-p)
(thing-at-point 'symbol)))
(defun ack-and-a-half-use-region-p ()
(or (and (fboundp 'use-region-p) (use-region-p))
(and transient-mark-mode mark-active
(> (region-end) (region-beginning)))))
(defsubst ack-and-a-half-read (regexp)
(let* ((default (ack-and-a-half-default-for-read))
(type (if regexp "pattern" "literal search"))
(history-var )
(prompt (if default
(format "ack %s (default %s): " type default)
(format "ack %s: " type))))
(read-string prompt
(ack-and-a-half-initial-contents-for-read)
(if regexp 'ack-regexp-history 'ack-literal-history)
default)))
(defsubst ack-and-a-half-read-args ()
(let* ((default "--type=hh")
(prompt (format "extra args (default %s): " default)))
(read-string prompt nil nil default)))
(defun ack-and-a-half-read-dir ()
(let ((dir (run-hook-with-args-until-success 'ack-and-a-half-root-directory-functions)))
(if ack-and-a-half-prompt-for-directory
(if (and dir (eq ack-and-a-half-prompt-for-directory 'unless-guessed))
dir
(if ack-and-a-half-use-ido
(ido-read-directory-name "Directory: " dir dir t)
(read-directory-name "Directory: " dir dir t)))
(or dir
(and buffer-file-name (file-name-directory buffer-file-name))
default-directory))))
(defsubst ack-and-a-half-xor (a b)
(if a (not b) b))
(defun ack-and-a-half-interactive ()
"Return the (interactive) arguments for `ack-and-a-half' and `ack-and-a-half-same'."
(let ((regexp (ack-and-a-half-xor current-prefix-arg ack-and-a-half-regexp-search)))
(list (ack-and-a-half-read regexp)
regexp
(ack-and-a-half-read-dir))))
(defun ack-and-a-half-interactive-args ()
"Return the (interactive) arguments for `ack-and-a-arg'."
(let ((regexp (ack-and-a-half-xor current-prefix-arg ack-and-a-half-regexp-search)))
(list (ack-and-a-half-read regexp)
regexp
(ack-and-a-half-read-dir)
(ack-and-a-half-read-args))))
(defun ack-and-a-half-type ()
(or (ack-and-a-half-type-for-major-mode major-mode)
(when buffer-file-name
(ack-and-a-half-create-type (list (file-name-extension buffer-file-name))))))
(defun ack-and-a-half-option (name enabled)
(format "--%s%s" (if enabled "" "no") name))
(defun ack-and-a-half-arguments-from-options (regexp)
(let ((arguments (list "--nocolor" "--nogroup" "--column"
(ack-and-a-half-option "smart-case" (eq ack-and-a-half-ignore-case 'smart))
(ack-and-a-half-option "env" ack-and-a-half-use-environment)
)))
(unless ack-and-a-half-ignore-case
(push "-i" arguments))
(unless regexp
(push "--literal" arguments))
arguments))
(defun ack-and-a-half-string-replace (from to string &optional re)
"Replace all occurrences of FROM with TO in STRING.
All arguments are strings. When optional fourth argument (RE) is
non-nil, treat FROM as a regular expression."
(let ((pos 0)
(res "")
(from (if re from (regexp-quote from))))
(while (< pos (length string))
(if (setq beg (string-match from string pos))
(progn
(setq res (concat res
(substring string pos (match-beginning 0))
to))
(setq pos (match-end 0)))
(progn
(setq res (concat res (substring string pos (length string))))
(setq pos (length string)))))
res))
(defun ack-and-a-half-run (directory regexp pattern &rest arguments)
"Run ack in DIRECTORY with ARGUMENTS."
(let ((default-directory (if directory
(file-name-as-directory (expand-file-name directory))
default-directory)))
(setq arguments (append ack-and-a-half-arguments
(ack-and-a-half-arguments-from-options regexp)
arguments
(list "--")
(list (shell-quote-argument pattern))
(when (eq system-type 'windows-nt)
(list (concat " < " null-device)))
))
(make-local-variable 'compilation-buffer-name-function)
(let (compilation-buffer-name-function)
(setq compilation-buffer-name-function 'ack-buffer-name)
(compilation-start (mapconcat 'identity (nconc (list ack-and-a-half-executable) arguments) " ")
'ack-and-a-half-mode))))
(defun ack-and-a-half-read-file (prompt choices)
(if ido-mode
(ido-completing-read prompt choices nil t)
(require 'iswitchb)
(with-no-warnings
(let ((iswitchb-make-buflist-hook
(lambda () (setq iswitchb-temp-buflist choices))))
(iswitchb-read-buffer prompt nil t)))))
(defun ack-and-a-half-list-files (directory &rest arguments)
(with-temp-buffer
(let ((default-directory directory))
(when (eq 0 (apply 'call-process ack-and-a-half-executable nil t nil "-f" "--print0"
arguments))
(goto-char (point-min))
(let ((beg (point-min))
files)
(while (re-search-forward "\0" nil t)
(push (buffer-substring beg (match-beginning 0)) files)
(setq beg (match-end 0)))
files)))))
(defun ack-and-a-half-version-string ()
"Return the ack version string."
(with-temp-buffer
(call-process ack-and-a-half-executable nil t nil "--version")
(goto-char (point-min))
(re-search-forward " +")
(buffer-substring (point) (point-at-eol))))
(defun ack-and-a-half-mode-setup ()
"Setup compilation variables and buffer for `ack-and-a-half'.
Set up `compilation-exit-message-function'."
(set (make-local-variable 'compilation-exit-message-function)
(lambda (status code msg)
(if (eq status 'exit)
(cond ((and (zerop code) (buffer-modified-p))
'("finished (matches found)\n" . "matched"))
((not (buffer-modified-p))
'("finished with no matches found\n" . "no match"))
(t
(cons msg code)))
(cons msg code)))))
;;; Public interface ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;###autoload
(defun ack-and-a-half (pattern &optional regexp directory)
"Run ack.
PATTERN is interpreted as a regular expression, iff REGEXP is
non-nil. If called interactively, the value of REGEXP is
determined by `ack-and-a-half-regexp-search'. A prefix arg
toggles the behavior. DIRECTORY is the root directory. If
called interactively, it is determined by
`ack-and-a-half-project-root-file-patterns'. The user is only
prompted, if `ack-and-a-half-prompt-for-directory' is set."
(interactive (ack-and-a-half-interactive))
(ack-and-a-half-run directory regexp pattern))
;;;###autoload
(defun ack-and-a-half-same (pattern &optional regexp directory)
"Run ack with --type matching the current `major-mode'.
The types of files searched are determined by
`ack-and-a-half-mode-type-alist' and
`ack-and-a-half-mode-extension-alist'. If no type is configured,
the buffer's file extension is used for the search. PATTERN is
interpreted as a regular expression, iff REGEXP is non-nil. If
called interactively, the value of REGEXP is determined by
`ack-and-a-half-regexp-search'. A prefix arg toggles that value.
DIRECTORY is the directory in which to start searching. If
called interactively, it is determined by
`ack-and-a-half-project-root-file-patterns`. The user is only
prompted, if `ack-and-a-half-prompt-for-directory' is set.`"
(interactive (ack-and-a-half-interactive))
(let ((type (ack-and-a-half-type)))
(if type
(apply 'ack-and-a-half-run directory regexp pattern type)
(ack-and-a-half pattern regexp directory))))
(defun ack-and-a-half-args (pattern &optional regexp directory type)
"Run ack.
PATTERN is interpreted as a regular expression, iff REGEXP is
non-nil. If called interactively, the value of REGEXP is
determined by `ack-and-a-half-regexp-search'. A prefix arg
toggles the behavior. DIRECTORY is the root directory. If
called interactively, it is determined by
`ack-and-a-half-project-root-file-patterns'. The user is only
prompted, if `ack-and-a-half-prompt-for-directory' is set.
ARGUMENTS are extra arguments to pass to ack. If called
interactively, it is determined by `ack-and-a-half-read-args' "
(interactive (ack-and-a-half-interactive-args))
(ack-and-a-half-run directory regexp pattern type))
;;;###autoload
(defun ack-and-a-half-find-file (&optional directory)
"Prompt to find a file found by ack in DIRECTORY."
(interactive (list (ack-and-a-half-read-dir)))
(find-file (expand-file-name
(ack-and-a-half-read-file
"Find file: "
(ack-and-a-half-list-files directory))
directory)))
;;;###autoload
(defun ack-and-a-half-find-file-same (&optional directory)
"Prompt to find a file found by ack in DIRECTORY."
(interactive (list (ack-and-a-half-read-dir)))
(find-file (expand-file-name
(ack-and-a-half-read-file
"Find file: "
(apply 'ack-and-a-half-list-files directory (ack-and-a-half-type)))
directory)))
;;; End ack-and-a-half.el ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(provide 'ack-and-a-half)
;;; ack-and-a-half.el ends here