forked from semiosis/pen.el
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpen-counsel.el
220 lines (186 loc) · 8.09 KB
/
pen-counsel.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
(require 'ivy)
(require 'pen-compatibility)
;; to fix a bug if the var is not available
(if (not (fboundp 'ivy-highlight-grep-commands))
(defset ivy-highlight-grep-commands nil))
(if (not (fboundp 'ivy-make-magic-action))
(defun ivy-make-magic-action (a b) nil))
(require 'counsel)
(load-library "counsel")
; vim +/"ivy-height" "$EMACSD/config/pen-ivy.el"
;; Spacemacs' find file was annoying because of C-h.
;; Remove the keymap from M-m f f
;; DONE Actually, make my own keymap. M-D does not work to delete line backwards. I have to use M-DEL instead. Super annoying. Or I could add a new map.
;; FIXED
(define-key counsel-mode-map (kbd "M-D") (kbd "M-DEL"))
(defun counsel-find-file (&optional initial-input)
"Forward to `find-file'.
When INITIAL-INPUT is non-nil, use it in the minibuffer during completion."
(interactive)
(ivy-read "Find file: " 'read-file-name-internal
:matcher #'counsel--find-file-matcher
:initial-input initial-input
:action #'counsel-find-file-action
:preselect (counsel--preselect-file)
:require-match 'confirm-after-completion
:history 'file-name-history
:keymap nil
:caller 'counsel-find-file))
(setq counsel-ag-base-command "pen-counsel-ag-cmd %s")
;; DONE This and counsel-ag could be used to create a genertic fuzzy finder
(defun counsel-ag-function (string)
"Grep in the current directory for STRING."
(let* ((command-args (counsel--split-command-args string))
(search-term (cdr command-args)))
(or
(let ((ivy-text search-term))
(ivy-more-chars))
(let* ((default-directory (ivy-state-directory ivy-last))
;; (regex (counsel--grep-regex search-term))
(switches (concat (car command-args)
;; (counsel--ag-extra-switches regex)
;; Don't add extra arguments or generic programs other than ag will have problems
;; (if (ivy--case-fold-p string)
;; " -i "
;; " -s ")
)))
(counsel--async-command (counsel--format-ag-command
switches
(funcall (if (listp counsel-ag-command) #'identity
#'shell-quote-argument)
;; regex
string
)))
nil))))
(cl-defun counsel-ag (&optional initial-input initial-directory extra-ag-args ag-prompt
&key caller &key histvar)
"Grep for a string in a root directory using ag.
By default, the root directory is the first directory containing a .git subdirectory.
INITIAL-INPUT can be given as the initial minibuffer input.
INITIAL-DIRECTORY, if non-nil, is used as the root directory for search.
EXTRA-AG-ARGS, if non-nil, is appended to `counsel-ag-base-command'.
AG-PROMPT, if non-nil, is passed as `ivy-read' prompt argument.
CALLER is passed to `ivy-read'.
With a `\\[universal-argument]' prefix argument, prompt for INITIAL-DIRECTORY.
With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
prompt additionally for EXTRA-AG-ARGS."
(interactive)
(if (not histvar)
(setq histvar 'counsel-git-grep-history))
(setq counsel-ag-command counsel-ag-base-command)
(setq counsel--regex-look-around counsel--grep-tool-look-around)
(counsel-require-program counsel-ag-command)
(let ((prog-name (car (if (listp counsel-ag-command) counsel-ag-command
(split-string counsel-ag-command))))
(arg (prefix-numeric-value current-prefix-arg)))
(when (>= arg 4)
(setq initial-directory
(or initial-directory
(pen-pwd)
;; (counsel-read-directory-name (concat
;; prog-name
;; " in directory: "))
)))
(when (>= arg 16)
(setq extra-ag-args
(or extra-ag-args
(read-from-minibuffer (format "%s args: " prog-name)))))
(setq counsel-ag-command (counsel--format-ag-command (or extra-ag-args "") "%s"))
(let ((default-directory (or initial-directory
(counsel--git-root)
default-directory)))
(ivy-read (or ag-prompt
(concat prog-name ": "))
#'counsel-ag-function
:initial-input initial-input
:dynamic-collection t
:keymap counsel-ag-map
:history histvar
:action #'counsel-git-grep-action
:require-match t
:caller (or caller 'counsel-ag)))))
;; (defun helm-google-suggest-action (candidate)
;; "Default action to jump to a Google suggested candidate."
;; (let ((arg (format helm-google-suggest-search-url
;; (url-hexify-string candidate))))
;; (helm-aif helm-google-suggest-default-browser-function
;; (funcall it arg)
;; (helm-browse-url arg))))
(defun counsel-search-action (x)
"Search for X."
;; This is what gives helm-google its prompt
(helm-google 'google x)
;; (browse-url
;; (concat
;; (nth 2 (assoc counsel-search-engine counsel-search-engines-alist))
;; (url-hexify-string x)))
)
;; I need to urldecode
(defun helm-google-search (&optional engine)
"Query the search engine, parse the response and fontify the candidates."
(let* ((engine (or engine helm-google-default-engine))
(results (helm-google--search helm-pattern engine)))
(let ((ret
(mapcar (lambda (result)
(let ((cite (plist-get result :cite)))
(cons
(concat
(propertize
(plist-get result :title)
'face 'font-lock-variable-name-face)
"\n"
(plist-get result :content)
"\n"
(when cite
(concat
(propertize
cite
'face 'link)
"\n"))
(propertize
(url-unhex-string
(plist-get result :url))
'face (if cite 'glyphless-char 'link)))
(url-unhex-string (plist-get result :url)))))
results)))
;; (tv (pps ret))
ret)))
(setq counsel-search-engine 'google)
;; This is used to run counsel search prompt;
;; I should build custom ones that do prompting instead.
;; But I have to make a fast prompt mechanism.
;; j:counsel-search-function
(defun counsel-search ()
"Ivy interface for dynamically querying a search engine."
(interactive)
(require 'request)
(require 'json)
;; The search function generates candidates
(ivy-read "search: " #'counsel-search-function
;; helm-google-suggest-actions
;; The action runs the final google search
:action #'counsel-search-action
:dynamic-collection t
:caller 'counsel-search))
(defun fz-gl (query)
(interactive (list (read-string-hist "fz-gl: ")))
(let ((result (fz (pen-snc (concat (pen-cmd "gl") " " query))
nil nil "fz-gl result: ")))
(eww result)))
(defun counsel-gl ()
"Ivy interface for dynamically querying a search engine."
(interactive)
(require 'request)
(require 'json)
(ivy-read "search: " #'counsel-search-function
;; helm-google-suggest-actions
:action #'fz-gl
:dynamic-collection t
:caller 'counsel-search))
(defun counsel-fzf-here ()
(interactive)
(counsel-fzf nil default-directory))
(define-key pen-map (kbd "M-q M-q") 'counsel-fzf-here)
;; (setq counsel-fzf-cmd "fzf -f \"%s\"")
(setq counsel-fzf-cmd "helm-fzf-d2.sh -f \"%s\"")
(provide 'pen-counsel)