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 (defvar counsel-describe-symbol-history nil
70 "History for `counsel-describe-variable' and `counsel-describe-function'.")
72 (defun counsel-describe-variable (variable &optional buffer frame)
73 "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
75 (let ((v (variable-at-point))
76 (enable-recursive-minibuffers t)
77 (preselect (thing-at-point 'symbol))
79 (setq ivy--action nil)
85 (when (or (get vv 'variable-documentation)
86 (and (boundp vv) (not (keywordp vv))))
87 (push (symbol-name vv) cands))))
89 :keymap counsel-describe-map
91 :history 'counsel-describe-symbol-history
94 (list (if (equal val "")
97 (unless (eq ivy--action 'counsel--find-symbol)
98 (describe-variable variable buffer frame)))
100 (defun counsel-describe-function (function)
101 "Forward to (`describe-function' FUNCTION) with ivy completion."
103 (let ((fn (function-called-at-point))
104 (enable-recursive-minibuffers t)
105 (preselect (thing-at-point 'symbol))
107 (setq ivy--action nil)
108 (setq val (ivy-read "Describe function: "
113 (push (symbol-name x) cands))))
115 :keymap counsel-describe-map
117 :history 'counsel-describe-symbol-history
120 (list (if (equal val "")
122 (unless (eq ivy--action 'counsel--find-symbol)
123 (describe-function function)))
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")
132 (defun counsel-info-lookup-symbol (symbol &optional mode)
133 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
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))
143 ((info-lookup-change-mode topic))))
144 (completions (info-lookup->completions topic mode))
145 (enable-recursive-minibuffers t)
148 (mapcar #'car completions))))
149 (list value info-lookup-mode))))
150 (info-lookup 'symbol symbol mode))
152 (defun counsel-unicode-char ()
153 "Insert a Unicode character at point."
155 (let* ((minibuffer-allow-text-properties t)
156 (char (ivy-read "Unicode name: "
159 (format "% -60s%c" (car x) (cdr x))
162 (insert-char (get-text-property 0 'result char))))
164 (declare-function cider-sync-request:complete "ext:cider-client")
165 (defun counsel-clj ()
166 "Clojure completion at point."
172 (cider-sync-request:complete str ":same")))))
174 (defun counsel-git ()
175 "Find file in the current Git repository."
177 (let* ((default-directory (locate-dominating-file
178 default-directory ".git"))
180 (shell-command-to-string
181 "git ls-files --full-name --")
184 (file (ivy-read "Find file: " cands)))
188 (defvar counsel--git-grep-dir nil
189 "Store the base git directory.")
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}'"
197 (string-to-number out)))
199 (defvar counsel--git-grep-count nil
200 "Store the line count in current repository.")
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))
207 (setq ivy--full-length counsel--git-grep-count)
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)))
214 (if (<= counsel--git-grep-count 20000)
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))))
222 (defun counsel-git-grep ()
223 "Grep for a string in the current git repository."
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 (let ((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))
238 (swiper--add-overlays (ivy--regex ivy-text)))))
239 (val (ivy-read "pattern: " 'counsel-git-grep-function)))
241 (funcall ivy--persistent-action val)))
244 (defun counsel-locate-function (str &rest _u)
245 (if (< (length str) 3)
247 (format "%d chars more" (- 3 (length ivy-text))))
249 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
251 (defun counsel-locate ()
254 (let* ((ivy--dynamic-function 'counsel-locate-function)
255 (val (ivy-read "pattern: " 'counsel-locate-function)))
259 (defun counsel--generic (completion-fn)
260 "Complete thing at point with COMPLETION-FN."
261 (let* ((bnd (bounds-of-thing-at-point 'symbol))
263 (buffer-substring-no-properties
266 (candidates (funcall completion-fn str))
268 (res (ivy-read (format "pattern (%s): " str)
272 (delete-region (car bnd) (cdr bnd)))
277 ;;; counsel.el ends here