1 ;;; counsel.el --- Various completion functions using Ivy -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((emacs "24.1") (swiper "0.3.0"))
9 ;; Keywords: completion, matching
11 ;; This file is part of GNU Emacs.
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)
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.
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
28 ;; Just call one of the interactive functions in this file to complete
29 ;; the corresponding thing using `ivy'.
31 ;; Currently available: Elisp symbols, Clojure symbols, Git files.
38 "Elisp completion at point."
41 (lambda (str) (all-completions str obarray))))
43 (defvar counsel-describe-map
44 (let ((map (make-sparse-keymap)))
45 (define-key map (kbd "C-.") 'counsel-find-symbol)
48 (defun counsel-find-symbol ()
49 "Jump to the definition of the current symbol."
51 (setq ivy--action 'counsel--find-symbol)
55 (defun counsel--find-symbol ()
56 (let ((sym (read ivy--current)))
63 (prin1-to-string sym)))
64 (find-library (prin1-to-string sym)))
66 (error "Couldn't fild definition of %s"
69 (defun counsel-describe-variable (variable &optional buffer frame)
70 "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
72 (let ((v (variable-at-point))
73 (enable-recursive-minibuffers t)
74 (preselect (thing-at-point 'symbol))
76 (setq ivy--action nil)
80 "Describe variable (default %s): " v)
81 "Describe variable: ")
85 (when (or (get vv 'variable-documentation)
86 (and (boundp vv) (not (keywordp vv))))
87 (push (symbol-name vv) cands))))
89 nil nil counsel-describe-map preselect
91 (list (if (equal val "")
94 (unless (eq ivy--action 'counsel--find-symbol)
95 (describe-variable variable buffer frame)))
97 (defun counsel-describe-function (function)
98 "Forward to (`describe-function' FUNCTION) with ivy completion."
100 (let ((fn (function-called-at-point))
101 (enable-recursive-minibuffers t)
102 (preselect (thing-at-point 'symbol))
104 (setq ivy--action nil)
105 (setq val (ivy-read (if fn
106 (format "Describe function (default %s): " fn)
107 "Describe function: ")
112 (push (symbol-name x) cands))))
114 nil nil counsel-describe-map preselect
116 (list (if (equal val "")
118 (unless (eq ivy--action 'counsel--find-symbol)
119 (describe-function function)))
121 (defvar info-lookup-mode)
122 (declare-function info-lookup->completions "info-look")
123 (declare-function info-lookup->mode-value "info-look")
124 (declare-function info-lookup-select-mode "info-look")
125 (declare-function info-lookup-change-mode "info-look")
126 (declare-function info-lookup "info-look")
128 (defun counsel-info-lookup-symbol (symbol &optional mode)
129 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
133 (let* ((topic 'symbol)
134 (mode (cond (current-prefix-arg
135 (info-lookup-change-mode topic))
136 ((info-lookup->mode-value
137 topic (info-lookup-select-mode))
139 ((info-lookup-change-mode topic))))
140 (completions (info-lookup->completions topic mode))
141 (enable-recursive-minibuffers t)
144 (mapcar #'car completions))))
145 (list value info-lookup-mode))))
146 (info-lookup 'symbol symbol mode))
148 (defun counsel-unicode-char ()
149 "Insert a Unicode character at point."
151 (let* ((minibuffer-allow-text-properties t)
152 (char (ivy-read "Unicode name: "
155 (format "% -60s%c" (car x) (cdr x))
158 (insert-char (get-text-property 0 'result char))))
160 (declare-function cider-sync-request:complete "ext:cider-client")
161 (defun counsel-clj ()
162 "Clojure completion at point."
168 (cider-sync-request:complete str ":same")))))
170 (defun counsel-git ()
171 "Find file in the current Git repository."
173 (let* ((default-directory (locate-dominating-file
174 default-directory ".git"))
176 (shell-command-to-string
177 "git ls-files --full-name --")
180 (file (ivy-read "Find file: " cands)))
184 (defun counsel-git-grep-count (str)
185 "Quickly count the amount of git grep STR matches."
186 (let ((out (shell-command-to-string
187 (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
189 (string-to-number out)))
191 (defvar counsel--git-grep-count nil
192 "Store the line count in current repository.")
194 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
195 "Grep in the current git repository for STRING."
196 (if (and (> counsel--git-grep-count 20000)
197 (< (length string) 3))
199 (setq ivy--full-length counsel--git-grep-count)
201 (format "%d chars more" (- 3 (length ivy-text)))))
202 (let ((cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
203 (ivy--regex string t)))
205 (if (<= counsel--git-grep-count 20000)
207 (setq res (shell-command-to-string cmd))
208 (setq ivy--full-length nil))
209 (setq res (shell-command-to-string (concat cmd " | head -n 5000")))
210 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
211 (split-string res "\n" t))))
213 (defun counsel-git-grep ()
214 "Grep for a string in the current git repository."
217 (let* ((counsel--git-grep-count (counsel-git-grep-count ""))
218 (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
219 'counsel-git-grep-function))
220 (git-dir (locate-dominating-file
221 default-directory ".git"))
222 (ivy--persistent-action (lambda (x)
223 (setq lst (split-string x ":"))
224 (find-file (expand-file-name (car lst) git-dir))
225 (goto-char (point-min))
226 (forward-line (1- (string-to-number (cadr lst))))
227 (setq swiper--window (selected-window))
229 (swiper--add-overlays (ivy--regex ivy-text))))
230 (val (ivy-read "pattern: " 'counsel-git-grep-function))
233 (funcall ivy--persistent-action val)))
236 (defun counsel-locate-function (str &rest _u)
237 (if (< (length str) 3)
239 (format "%d chars more" (- 3 (length ivy-text))))
241 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
243 (defun counsel-locate ()
246 (let* ((ivy--dynamic-function 'counsel-locate-function)
247 (val (ivy-read "pattern: " 'counsel-locate-function)))
251 (defun counsel--generic (completion-fn)
252 "Complete thing at point with COMPLETION-FN."
253 (let* ((bnd (bounds-of-thing-at-point 'symbol))
255 (buffer-substring-no-properties
258 (candidates (funcall completion-fn str))
260 (res (ivy-read (format "pattern (%s): " str)
264 (delete-region (car bnd) (cdr bnd)))
269 ;;; counsel.el ends here