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