]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/counsel.el
Merge remote-tracking branch 'ztree/master'
[gnu-emacs-elpa] / packages / swiper / counsel.el
1 ;;; counsel.el --- Various completion functions using Ivy -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
7 ;; Version: 0.1.0
8 ;; Package-Requires: ((emacs "24.1") (swiper "0.4.0"))
9 ;; Keywords: completion, matching
10
11 ;; This file is part of GNU Emacs.
12
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
28 ;; Just call one of the interactive functions in this file to complete
29 ;; the corresponding thing using `ivy'.
30 ;;
31 ;; Currently available: Elisp symbols, Clojure symbols, Git files.
32
33 ;;; Code:
34
35 (require 'swiper)
36
37 (defun counsel-el ()
38 "Elisp completion at point."
39 (interactive)
40 (counsel--generic
41 (lambda (str) (all-completions str obarray))))
42
43 (defvar counsel-describe-map
44 (let ((map (make-sparse-keymap)))
45 (define-key map (kbd "C-.") 'counsel-find-symbol)
46 map))
47
48 (defun counsel-find-symbol ()
49 "Jump to the definition of the current symbol."
50 (interactive)
51 (ivy-set-action 'counsel--find-symbol)
52 (ivy-done))
53
54 (defun counsel--find-symbol (x)
55 "Find symbol definition that corresponds to string X."
56 (let ((full-name (get-text-property 0 'full-name x)))
57 (if full-name
58 (find-library full-name)
59 (let ((sym (read x)))
60 (cond ((boundp sym)
61 (find-variable sym))
62 ((fboundp sym)
63 (find-function sym))
64 ((or (featurep sym)
65 (locate-library
66 (prin1-to-string sym)))
67 (find-library
68 (prin1-to-string sym)))
69 (t
70 (error "Couldn't fild definition of %s"
71 sym)))))))
72
73 (defvar counsel-describe-symbol-history nil
74 "History for `counsel-describe-variable' and `counsel-describe-function'.")
75
76 (defun counsel-describe-variable ()
77 "Forward to `describe-variable'."
78 (interactive)
79 (let ((enable-recursive-minibuffers t))
80 (ivy-read
81 "Describe variable: "
82 (let (cands)
83 (mapatoms
84 (lambda (vv)
85 (when (or (get vv 'variable-documentation)
86 (and (boundp vv) (not (keywordp vv))))
87 (push (symbol-name vv) cands))))
88 cands)
89 :keymap counsel-describe-map
90 :preselect (thing-at-point 'symbol)
91 :history 'counsel-describe-symbol-history
92 :require-match t
93 :sort t
94 :action (lambda (x)
95 (describe-variable
96 (intern x))))))
97
98 (defun counsel-describe-function ()
99 "Forward to `describe-function'."
100 (interactive)
101 (let ((enable-recursive-minibuffers t))
102 (ivy-read "Describe function: "
103 (let (cands)
104 (mapatoms
105 (lambda (x)
106 (when (fboundp x)
107 (push (symbol-name x) cands))))
108 cands)
109 :keymap counsel-describe-map
110 :preselect (thing-at-point 'symbol)
111 :history 'counsel-describe-symbol-history
112 :require-match t
113 :sort t
114 :action (lambda (x)
115 (describe-function
116 (intern x))))))
117
118 (defvar info-lookup-mode)
119 (declare-function info-lookup->completions "info-look")
120 (declare-function info-lookup->mode-value "info-look")
121 (declare-function info-lookup-select-mode "info-look")
122 (declare-function info-lookup-change-mode "info-look")
123 (declare-function info-lookup "info-look")
124
125 (defun counsel-info-lookup-symbol (symbol &optional mode)
126 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
127 (interactive
128 (progn
129 (require 'info-look)
130 (let* ((topic 'symbol)
131 (mode (cond (current-prefix-arg
132 (info-lookup-change-mode topic))
133 ((info-lookup->mode-value
134 topic (info-lookup-select-mode))
135 info-lookup-mode)
136 ((info-lookup-change-mode topic))))
137 (completions (info-lookup->completions topic mode))
138 (enable-recursive-minibuffers t)
139 (value (ivy-read
140 "Describe symbol: "
141 (mapcar #'car completions)
142 :sort t)))
143 (list value info-lookup-mode))))
144 (info-lookup 'symbol symbol mode))
145
146 (defun counsel-unicode-char ()
147 "Insert a Unicode character at point."
148 (interactive)
149 (let* ((minibuffer-allow-text-properties t)
150 (char (ivy-read "Unicode name: "
151 (mapcar (lambda (x)
152 (propertize
153 (format "% -60s%c" (car x) (cdr x))
154 'result (cdr x)))
155 (ucs-names)))))
156 (insert-char (get-text-property 0 'result char))))
157
158 (declare-function cider-sync-request:complete "ext:cider-client")
159 (defun counsel-clj ()
160 "Clojure completion at point."
161 (interactive)
162 (counsel--generic
163 (lambda (str)
164 (mapcar
165 #'cl-caddr
166 (cider-sync-request:complete str ":same")))))
167
168 (defun counsel-git ()
169 "Find file in the current Git repository."
170 (interactive)
171 (let* ((default-directory (locate-dominating-file
172 default-directory ".git"))
173 (cands (split-string
174 (shell-command-to-string
175 "git ls-files --full-name --")
176 "\n"
177 t))
178 (action (lambda (x) (find-file x))))
179 (ivy-read "Find file: " cands
180 :action action)))
181
182 (defvar counsel--git-grep-dir nil
183 "Store the base git directory.")
184
185 (defun counsel-git-grep-count (str)
186 "Quickly count the amount of git grep STR matches."
187 (let* ((default-directory counsel--git-grep-dir)
188 (out (shell-command-to-string
189 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
190 (ivy--regex str)))))
191 (string-to-number out)))
192
193 (defvar counsel--git-grep-count nil
194 "Store the line count in current repository.")
195
196 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
197 "Grep in the current git repository for STRING."
198 (if (and (> counsel--git-grep-count 20000)
199 (< (length string) 3))
200 (progn
201 (setq ivy--full-length counsel--git-grep-count)
202 (list ""
203 (format "%d chars more" (- 3 (length ivy-text)))))
204 (let* ((default-directory counsel--git-grep-dir)
205 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
206 (ivy--regex string t)))
207 res)
208 (if (<= counsel--git-grep-count 20000)
209 (progn
210 (setq res (shell-command-to-string cmd))
211 (setq ivy--full-length nil))
212 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
213 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
214 (split-string res "\n" t))))
215
216 (defvar counsel-git-grep-map
217 (let ((map (make-sparse-keymap)))
218 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
219 map))
220
221 (defun counsel-git-grep-recenter ()
222 (interactive)
223 (with-selected-window (ivy-state-window ivy-last)
224 (counsel-git-grep-action)
225 (recenter-top-bottom)))
226
227 (defun counsel-git-grep-action (x)
228 (let ((lst (split-string x ":")))
229 (find-file (expand-file-name (car lst) counsel--git-grep-dir))
230 (goto-char (point-min))
231 (forward-line (1- (string-to-number (cadr lst))))
232 (unless (eq ivy-exit 'done)
233 (setq swiper--window (selected-window))
234 (swiper--cleanup)
235 (swiper--add-overlays (ivy--regex ivy-text)))))
236
237 (defun counsel-git-grep (&optional initial-input)
238 "Grep for a string in the current git repository."
239 (interactive)
240 (setq counsel--git-grep-dir
241 (locate-dominating-file default-directory ".git"))
242 (if (null counsel--git-grep-dir)
243 (error "Not in a git repository")
244 (setq counsel--git-grep-count (counsel-git-grep-count ""))
245 (ivy-read "pattern: " 'counsel-git-grep-function
246 :initial-input initial-input
247 :matcher #'counsel-git-grep-matcher
248 :dynamic-collection (when (> counsel--git-grep-count 20000)
249 'counsel-git-grep-function)
250 :keymap counsel-git-grep-map
251 :action #'counsel-git-grep-action
252 :unwind #'swiper--cleanup)))
253
254 (defun counsel-git-grep-matcher (x)
255 (ignore-errors
256 (when (string-match "^[^:]+:[^:]+:" x)
257 (setq x (substring x (match-end 0)))
258 (if (stringp ivy--old-re)
259 (string-match ivy--old-re x)
260 (let ((res t))
261 (dolist (re ivy--old-re)
262 (setq res
263 (and res
264 (ignore-errors
265 (if (cdr re)
266 (string-match (car re) x)
267 (not (string-match (car re) x)))))))
268 res)))))
269
270 (defun counsel-locate-function (str &rest _u)
271 (if (< (length str) 3)
272 (list ""
273 (format "%d chars more" (- 3 (length ivy-text))))
274 (split-string
275 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
276
277 (defun counsel-locate ()
278 "Call locate."
279 (interactive)
280 (let* ((ivy--dynamic-function 'counsel-locate-function)
281 (val (ivy-read "pattern: " 'counsel-locate-function)))
282 (when val
283 (find-file val))))
284
285 (defun counsel--generic (completion-fn)
286 "Complete thing at point with COMPLETION-FN."
287 (let* ((bnd (bounds-of-thing-at-point 'symbol))
288 (str (if bnd
289 (buffer-substring-no-properties
290 (car bnd) (cdr bnd))
291 ""))
292 (candidates (funcall completion-fn str))
293 (ivy-height 7)
294 (res (ivy-read (format "pattern (%s): " str)
295 candidates)))
296 (when (stringp res)
297 (when bnd
298 (delete-region (car bnd) (cdr bnd)))
299 (insert res))))
300
301 (defun counsel-directory-parent (dir)
302 "Return the directory parent of directory DIR."
303 (concat (file-name-nondirectory
304 (directory-file-name dir)) "/"))
305
306 (defun counsel-string-compose (prefix str)
307 "Make PREFIX the display prefix of STR though text properties."
308 (let ((str (copy-sequence str)))
309 (put-text-property
310 0 1 'display
311 (concat prefix (substring str 0 1))
312 str)
313 str))
314
315 (defun counsel-load-library ()
316 "Load a selected the Emacs Lisp library.
317 The libraries are offered from `load-path'."
318 (interactive)
319 (let ((dirs load-path)
320 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
321 (cands (make-hash-table :test #'equal))
322 short-name
323 old-val
324 dir-parent
325 res)
326 (dolist (dir dirs)
327 (when (file-directory-p dir)
328 (dolist (file (file-name-all-completions "" dir))
329 (when (string-match suffix file)
330 (unless (string-match "pkg.elc?$" file)
331 (setq short-name (substring file 0 (match-beginning 0)))
332 (if (setq old-val (gethash short-name cands))
333 (progn
334 ;; assume going up directory once will resolve name clash
335 (setq dir-parent (counsel-directory-parent (cdr old-val)))
336 (puthash short-name
337 (cons
338 (counsel-string-compose dir-parent (car old-val))
339 (cdr old-val))
340 cands)
341 (setq dir-parent (counsel-directory-parent dir))
342 (puthash (concat dir-parent short-name)
343 (cons
344 (propertize
345 (counsel-string-compose
346 dir-parent short-name)
347 'full-name (expand-file-name file dir))
348 dir)
349 cands))
350 (puthash short-name
351 (cons (propertize
352 short-name
353 'full-name (expand-file-name file dir))
354 dir) cands)))))))
355 (maphash (lambda (_k v) (push (car v) res)) cands)
356 (ivy-read "Load library: " (nreverse res)
357 :action (lambda (x)
358 (load-library
359 (get-text-property 0 'full-name x)))
360 :keymap counsel-describe-map)))
361
362 (provide 'counsel)
363
364 ;;; counsel.el ends here