]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/counsel.el
Merge commit 'e2452a3e77ef6ea6b193292d2c0bbbe93dd4b078' from swiper
[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.3.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 (setq ivy--action 'counsel--find-symbol)
52 (setq ivy-exit 'done)
53 (exit-minibuffer))
54
55 (defun counsel--find-symbol ()
56 (let ((sym (read ivy--current)))
57 (cond ((boundp sym)
58 (find-variable sym))
59 ((fboundp sym)
60 (find-function sym))
61 ((or (featurep sym)
62 (locate-library
63 (prin1-to-string sym)))
64 (find-library (prin1-to-string sym)))
65 (t
66 (error "Couldn't fild definition of %s"
67 sym)))))
68
69 (defvar counsel-describe-symbol-history nil
70 "History for `counsel-describe-variable' and `counsel-describe-function'.")
71
72 (defun counsel-describe-variable (variable &optional buffer frame)
73 "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
74 (interactive
75 (let ((v (variable-at-point))
76 (enable-recursive-minibuffers t)
77 (preselect (thing-at-point 'symbol))
78 val)
79 (setq ivy--action nil)
80 (setq val (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 preselect
91 :history 'counsel-describe-symbol-history
92 :require-match t
93 :sort t))
94 (list (if (equal val "")
95 v
96 (intern val)))))
97 (unless (eq ivy--action 'counsel--find-symbol)
98 (describe-variable variable buffer frame)))
99
100 (defun counsel-describe-function (function)
101 "Forward to (`describe-function' FUNCTION) with ivy completion."
102 (interactive
103 (let ((fn (function-called-at-point))
104 (enable-recursive-minibuffers t)
105 (preselect (thing-at-point 'symbol))
106 val)
107 (setq ivy--action nil)
108 (setq val (ivy-read "Describe function: "
109 (let (cands)
110 (mapatoms
111 (lambda (x)
112 (when (fboundp x)
113 (push (symbol-name x) cands))))
114 cands)
115 :keymap counsel-describe-map
116 :preselect preselect
117 :history 'counsel-describe-symbol-history
118 :require-match t
119 :sort t))
120 (list (if (equal val "")
121 fn (intern val)))))
122 (unless (eq ivy--action 'counsel--find-symbol)
123 (describe-function function)))
124
125 (defvar info-lookup-mode)
126 (declare-function info-lookup->completions "info-look")
127 (declare-function info-lookup->mode-value "info-look")
128 (declare-function info-lookup-select-mode "info-look")
129 (declare-function info-lookup-change-mode "info-look")
130 (declare-function info-lookup "info-look")
131
132 (defun counsel-info-lookup-symbol (symbol &optional mode)
133 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
134 (interactive
135 (progn
136 (require 'info-look)
137 (let* ((topic 'symbol)
138 (mode (cond (current-prefix-arg
139 (info-lookup-change-mode topic))
140 ((info-lookup->mode-value
141 topic (info-lookup-select-mode))
142 info-lookup-mode)
143 ((info-lookup-change-mode topic))))
144 (completions (info-lookup->completions topic mode))
145 (enable-recursive-minibuffers t)
146 (value (ivy-read
147 "Describe symbol: "
148 (mapcar #'car completions))))
149 (list value info-lookup-mode))))
150 (info-lookup 'symbol symbol mode))
151
152 (defun counsel-unicode-char ()
153 "Insert a Unicode character at point."
154 (interactive)
155 (let* ((minibuffer-allow-text-properties t)
156 (char (ivy-read "Unicode name: "
157 (mapcar (lambda (x)
158 (propertize
159 (format "% -60s%c" (car x) (cdr x))
160 'result (cdr x)))
161 (ucs-names)))))
162 (insert-char (get-text-property 0 'result char))))
163
164 (declare-function cider-sync-request:complete "ext:cider-client")
165 (defun counsel-clj ()
166 "Clojure completion at point."
167 (interactive)
168 (counsel--generic
169 (lambda (str)
170 (mapcar
171 #'cl-caddr
172 (cider-sync-request:complete str ":same")))))
173
174 (defun counsel-git ()
175 "Find file in the current Git repository."
176 (interactive)
177 (let* ((default-directory (locate-dominating-file
178 default-directory ".git"))
179 (cands (split-string
180 (shell-command-to-string
181 "git ls-files --full-name --")
182 "\n"
183 t))
184 (file (ivy-read "Find file: " cands)))
185 (when file
186 (find-file file))))
187
188 (defvar counsel--git-grep-dir nil
189 "Store the base git directory.")
190
191 (defun counsel-git-grep-count (str)
192 "Quickly count the amount of git grep STR matches."
193 (let* ((default-directory counsel--git-grep-dir)
194 (out (shell-command-to-string
195 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
196 (ivy--regex str)))))
197 (string-to-number out)))
198
199 (defvar counsel--git-grep-count nil
200 "Store the line count in current repository.")
201
202 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
203 "Grep in the current git repository for STRING."
204 (if (and (> counsel--git-grep-count 20000)
205 (< (length string) 3))
206 (progn
207 (setq ivy--full-length counsel--git-grep-count)
208 (list ""
209 (format "%d chars more" (- 3 (length ivy-text)))))
210 (let* ((default-directory counsel--git-grep-dir)
211 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
212 (ivy--regex string t)))
213 res)
214 (if (<= counsel--git-grep-count 20000)
215 (progn
216 (setq res (shell-command-to-string cmd))
217 (setq ivy--full-length nil))
218 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
219 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
220 (split-string res "\n" t))))
221
222 (defun counsel-git-grep ()
223 "Grep for a string in the current git repository."
224 (interactive)
225 (unwind-protect
226 (let* ((counsel--git-grep-dir (locate-dominating-file
227 default-directory ".git"))
228 (counsel--git-grep-count (counsel-git-grep-count ""))
229 (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
230 'counsel-git-grep-function))
231 (ivy--persistent-action (lambda (x)
232 (setq lst (split-string x ":"))
233 (find-file (expand-file-name (car lst) counsel--git-grep-dir))
234 (goto-char (point-min))
235 (forward-line (1- (string-to-number (cadr lst))))
236 (setq swiper--window (selected-window))
237 (swiper--cleanup)
238 (swiper--add-overlays (ivy--regex ivy-text))))
239 (val (ivy-read "pattern: " 'counsel-git-grep-function))
240 lst)
241 (when val
242 (funcall ivy--persistent-action val)))
243 (swiper--cleanup)))
244
245 (defun counsel-locate-function (str &rest _u)
246 (if (< (length str) 3)
247 (list ""
248 (format "%d chars more" (- 3 (length ivy-text))))
249 (split-string
250 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
251
252 (defun counsel-locate ()
253 "Call locate."
254 (interactive)
255 (let* ((ivy--dynamic-function 'counsel-locate-function)
256 (val (ivy-read "pattern: " 'counsel-locate-function)))
257 (when val
258 (find-file val))))
259
260 (defun counsel--generic (completion-fn)
261 "Complete thing at point with COMPLETION-FN."
262 (let* ((bnd (bounds-of-thing-at-point 'symbol))
263 (str (if bnd
264 (buffer-substring-no-properties
265 (car bnd) (cdr bnd))
266 ""))
267 (candidates (funcall completion-fn str))
268 (ivy-height 7)
269 (res (ivy-read (format "pattern (%s): " str)
270 candidates)))
271 (when (stringp res)
272 (when bnd
273 (delete-region (car bnd) (cdr bnd)))
274 (insert res))))
275
276 (provide 'counsel)
277
278 ;;; counsel.el ends here