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 (ivy-set-action 'counsel--find-symbol)
54 (defun counsel--find-symbol ()
55 (let ((sym (read ivy--current)))
62 (prin1-to-string sym)))
63 (find-library (prin1-to-string sym)))
65 (error "Couldn't fild definition of %s"
68 (defvar counsel-describe-symbol-history nil
69 "History for `counsel-describe-variable' and `counsel-describe-function'.")
71 (defun counsel-describe-variable ()
72 "Forward to `describe-variable'."
74 (let ((enable-recursive-minibuffers t))
80 (when (or (get vv 'variable-documentation)
81 (and (boundp vv) (not (keywordp vv))))
82 (push (symbol-name vv) cands))))
84 :keymap counsel-describe-map
85 :preselect (thing-at-point 'symbol)
86 :history 'counsel-describe-symbol-history
91 (intern ivy--current))))))
93 (defun counsel-describe-function ()
94 "Forward to `describe-function'."
96 (let ((enable-recursive-minibuffers t))
97 (ivy-read "Describe function: "
102 (push (symbol-name x) cands))))
104 :keymap counsel-describe-map
105 :preselect (thing-at-point 'symbol)
106 :history 'counsel-describe-symbol-history
111 (intern ivy--current))))))
113 (defvar info-lookup-mode)
114 (declare-function info-lookup->completions "info-look")
115 (declare-function info-lookup->mode-value "info-look")
116 (declare-function info-lookup-select-mode "info-look")
117 (declare-function info-lookup-change-mode "info-look")
118 (declare-function info-lookup "info-look")
120 (defun counsel-info-lookup-symbol (symbol &optional mode)
121 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
125 (let* ((topic 'symbol)
126 (mode (cond (current-prefix-arg
127 (info-lookup-change-mode topic))
128 ((info-lookup->mode-value
129 topic (info-lookup-select-mode))
131 ((info-lookup-change-mode topic))))
132 (completions (info-lookup->completions topic mode))
133 (enable-recursive-minibuffers t)
136 (mapcar #'car completions))))
137 (list value info-lookup-mode))))
138 (info-lookup 'symbol symbol mode))
140 (defun counsel-unicode-char ()
141 "Insert a Unicode character at point."
143 (let* ((minibuffer-allow-text-properties t)
144 (char (ivy-read "Unicode name: "
147 (format "% -60s%c" (car x) (cdr x))
150 (insert-char (get-text-property 0 'result char))))
152 (declare-function cider-sync-request:complete "ext:cider-client")
153 (defun counsel-clj ()
154 "Clojure completion at point."
160 (cider-sync-request:complete str ":same")))))
162 (defun counsel-git ()
163 "Find file in the current Git repository."
165 (let* ((default-directory (locate-dominating-file
166 default-directory ".git"))
168 (shell-command-to-string
169 "git ls-files --full-name --")
172 (file (ivy-read "Find file: " cands)))
176 (defvar counsel--git-grep-dir nil
177 "Store the base git directory.")
179 (defun counsel-git-grep-count (str)
180 "Quickly count the amount of git grep STR matches."
181 (let* ((default-directory counsel--git-grep-dir)
182 (out (shell-command-to-string
183 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
185 (string-to-number out)))
187 (defvar counsel--git-grep-count nil
188 "Store the line count in current repository.")
190 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
191 "Grep in the current git repository for STRING."
192 (if (and (> counsel--git-grep-count 20000)
193 (< (length string) 3))
195 (setq ivy--full-length counsel--git-grep-count)
197 (format "%d chars more" (- 3 (length ivy-text)))))
198 (let* ((default-directory counsel--git-grep-dir)
199 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
200 (ivy--regex string t)))
202 (if (<= counsel--git-grep-count 20000)
204 (setq res (shell-command-to-string cmd))
205 (setq ivy--full-length nil))
206 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
207 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
208 (split-string res "\n" t))))
210 (defun counsel-git-grep (&optional initial-input)
211 "Grep for a string in the current git repository."
214 (let* ((counsel--git-grep-dir (locate-dominating-file
215 default-directory ".git"))
216 (counsel--git-grep-count (counsel-git-grep-count ""))
217 (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
218 'counsel-git-grep-function)))
219 (ivy-read "pattern: " 'counsel-git-grep-function
220 :initial-input initial-input
223 (let ((lst (split-string ivy--current ":")))
224 (find-file (expand-file-name (car lst) counsel--git-grep-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))))))
232 (defun counsel-locate-function (str &rest _u)
233 (if (< (length str) 3)
235 (format "%d chars more" (- 3 (length ivy-text))))
237 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
239 (defun counsel-locate ()
242 (let* ((ivy--dynamic-function 'counsel-locate-function)
243 (val (ivy-read "pattern: " 'counsel-locate-function)))
247 (defun counsel--generic (completion-fn)
248 "Complete thing at point with COMPLETION-FN."
249 (let* ((bnd (bounds-of-thing-at-point 'symbol))
251 (buffer-substring-no-properties
254 (candidates (funcall completion-fn str))
256 (res (ivy-read (format "pattern (%s): " str)
260 (delete-region (car bnd) (cdr bnd)))
265 ;;; counsel.el ends here