author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Thu, 30 Mar 2017 00:34:44 +0300 | |
changeset 1521 | df20553d4bd2 |
parent 1515 | a28c181c1412 |
child 1522 | bbc10891c9a0 |
permissions | -rw-r--r-- |
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
1 |
;;; ag.el --- Ag frontend |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
2 |
|
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
3 |
;;; Commentary: |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
4 |
;; |
1492 | 5 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
6 |
(defun my-ag-goto () |
1492 | 7 |
(interactive) |
8 |
(save-excursion |
|
9 |
(let ( lineno end ) |
|
10 |
(forward-line 1) |
|
11 |
(backward-char 1) |
|
12 |
(setq end (point)) |
|
13 |
(forward-line 0) |
|
14 |
(if (not (search-forward-regexp "^\\([1-9][0-9]*\\):" end t)) |
|
15 |
(message "Not at line number...") |
|
1521
df20553d4bd2
Fix: Warning: "string-to-int" is an obsolete function (as of 22.1); use
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1515
diff
changeset
|
16 |
(setq lineno (string-to-number (match-string 1))) |
1492 | 17 |
(if (search-backward-regexp "^$" nil t) |
18 |
(forward-char) |
|
19 |
(goto-char (point-min))) |
|
20 |
(search-forward-regexp "^.*") |
|
21 |
(find-file-other-window (match-string 0)) |
|
22 |
(goto-line lineno))))) |
|
23 |
||
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
24 |
(defun my-ag-kill-process () |
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
25 |
(interactive) |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
26 |
(let ( (proc (get-buffer-process (current-buffer))) ) |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
27 |
(if proc |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
28 |
(interrupt-process proc) |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
29 |
(error "The %s process is not running" (downcase mode-name))))) |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
30 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
31 |
(defvar my-ag-mode-map (make-sparse-keymap)) |
1492 | 32 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
33 |
(define-key my-ag-mode-map (kbd "RET") 'my-ag-goto) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
34 |
(define-key my-ag-mode-map (kbd "C-c C-k") 'my-ag-kill-process) |
1492 | 35 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
36 |
(defface my-ag/lineno-face |
1492 | 37 |
'((t :inherit warning)) |
38 |
"Face for line number.") |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
39 |
(defface my-ag/path-face |
1492 | 40 |
'((t :inherit success)) |
41 |
"Face for line number.") |
|
42 |
||
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
43 |
(defvar my-ag/keywords |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
44 |
'(("^[1-9][0-9]\\{0,5\\}:" . (0 'my-ag/lineno-face)) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
45 |
("^[^:]\\{6\\}.*" . (0 'my-ag/path-face)))) |
1492 | 46 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
47 |
(defun my-ag/font-lock-extend-region () |
1492 | 48 |
(save-excursion |
49 |
(goto-char font-lock-beg) |
|
50 |
(forward-line 2) |
|
51 |
(setq font-lock-beg (point)))) |
|
52 |
||
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
53 |
(define-derived-mode my-ag-mode fundamental-mode "Ag" |
1492 | 54 |
"Major mode for Ag parsing." |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
55 |
(setq font-lock-defaults '(my-ag/keywords t nil nil)) ; (font-lock-multiline . t) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
56 |
;; (add-hook 'font-lock-extend-region-functions 'my-ag/font-lock-extend-region) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
57 |
(use-local-map my-ag-mode-map)) |
1492 | 58 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
59 |
(defvar my-ag/buffer-name "*ag*") |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
60 |
(defvar my-ag/buffer nil) |
1492 | 61 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
62 |
(defun my-ag/filter (proc str) |
1492 | 63 |
(when (buffer-live-p (process-buffer proc)) |
64 |
(with-current-buffer (process-buffer proc) |
|
65 |
(save-excursion |
|
66 |
(goto-char (process-mark proc)) |
|
67 |
(insert str) |
|
68 |
(set-marker (process-mark proc) (point))) |
|
69 |
))) |
|
70 |
||
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
71 |
(defvar my-ag/regex-history nil) |
1492 | 72 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
73 |
(defun my-ag/setup-buffer (dir) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
74 |
(setq my-ag/buffer (get-buffer-create my-ag/buffer-name)) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
75 |
(with-current-buffer my-ag/buffer |
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
76 |
(setq default-directory dir) |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
77 |
(erase-buffer) |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
78 |
(my-ag-mode)) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
79 |
(display-buffer my-ag/buffer)) |
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
80 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
81 |
(defun my-ag/run (regex) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
82 |
(let ( (default-directory (buffer-local-value 'default-directory my-ag/buffer)) ) |
1496
4b336f352950
(vc-root-dir) selectively work depending on buffer mode. I need root based on
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1493
diff
changeset
|
83 |
(make-process |
4b336f352950
(vc-root-dir) selectively work depending on buffer mode. I need root based on
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1493
diff
changeset
|
84 |
:name "ag" |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
85 |
:buffer my-ag/buffer |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
86 |
:filter 'my-ag/filter |
1496
4b336f352950
(vc-root-dir) selectively work depending on buffer mode. I need root based on
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1493
diff
changeset
|
87 |
:command (list "ag" "--group" "--nocolor" regex)))) |
4b336f352950
(vc-root-dir) selectively work depending on buffer mode. I need root based on
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1493
diff
changeset
|
88 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
89 |
(defun my-ag/project-root () |
1501
636e49ed6d27
Prevent error if there is no repository. Fix highlighting of line numbers.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1498
diff
changeset
|
90 |
(condition-case err |
636e49ed6d27
Prevent error if there is no repository. Fix highlighting of line numbers.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1498
diff
changeset
|
91 |
(let ( backend ) |
636e49ed6d27
Prevent error if there is no repository. Fix highlighting of line numbers.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1498
diff
changeset
|
92 |
(setq backend (vc-responsible-backend default-directory)) |
636e49ed6d27
Prevent error if there is no repository. Fix highlighting of line numbers.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1498
diff
changeset
|
93 |
(if backend |
1496
4b336f352950
(vc-root-dir) selectively work depending on buffer mode. I need root based on
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1493
diff
changeset
|
94 |
(vc-call-backend backend 'root default-directory) |
1501
636e49ed6d27
Prevent error if there is no repository. Fix highlighting of line numbers.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1498
diff
changeset
|
95 |
default-directory)) |
636e49ed6d27
Prevent error if there is no repository. Fix highlighting of line numbers.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1498
diff
changeset
|
96 |
(error default-directory))) |
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
97 |
|
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
98 |
(defun my-ag/read-regex () |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
99 |
(let* ( (def (when my-ag/regex-history (car my-ag/regex-history))) |
1502
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
100 |
(part (when def (if (< (length def) 20) |
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
101 |
def |
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
102 |
(concat (substring def 0 20) "...")))) ) |
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
103 |
(read-string |
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
104 |
(if part (format "Regex [%s]: " part) "Regex: ") |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
105 |
"" 'my-ag/regex-history def t))) |
1502
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
106 |
|
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
107 |
;;;###autoload |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
108 |
(defun my-ag (regex) |
1502
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
109 |
"Search in 'ag' recursively from VCS root directory and fall to |
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
110 |
current directory if VCS root is not defined." |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
111 |
(interactive (list (my-ag/read-regex))) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
112 |
(my-ag/setup-buffer (if current-prefix-arg default-directory (my-ag/project-root))) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
113 |
(my-ag/run regex)) |
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
114 |
|
1502
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
115 |
;;;###autoload |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
116 |
(defun my-ag-default-directory (regex) |
1502
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
117 |
"Search in 'ag' recursively from current directory." |
1515
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
118 |
(interactive (list (my-ag/read-regex))) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
119 |
(my-ag/setup-buffer default-directory) |
a28c181c1412
Rename public function so that they wouldn't have '/' symbol in name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1502
diff
changeset
|
120 |
(my-ag/run regex)) |
1502
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
121 |
|
72c63cea35d3
It much easy to call ag for current directory via modifier then prefix argument.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1501
diff
changeset
|
122 |
|
1493
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
123 |
(provide 'ag) |
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
124 |
|
d242edfd1321
Function for killing process. Simplify code. Limit search for current
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1492
diff
changeset
|
125 |
;;; ag.el ends here |