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))
79 "Describe variable (default %s): " v)
80 "Describe variable: ")
84 (when (or (get vv 'variable-documentation)
85 (and (boundp vv) (not (keywordp vv))))
86 (push (symbol-name vv) cands))))
88 nil nil counsel-describe-map preselect))
89 (list (if (equal val "")
92 (describe-variable variable buffer frame))
94 (defun counsel-describe-function (function)
95 "Forward to (`describe-function' FUNCTION) with ivy completion."
97 (let ((fn (function-called-at-point))
98 (enable-recursive-minibuffers t)
99 (preselect (thing-at-point 'symbol))
101 (setq val (ivy-read (if fn
102 (format "Describe function (default %s): " fn)
103 "Describe function: ")
108 (push (symbol-name x) cands))))
110 nil nil counsel-describe-map preselect))
111 (list (if (equal val "")
113 (describe-function function))
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")
122 (defun counsel-info-lookup-symbol (symbol &optional mode)
123 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
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))
133 ((info-lookup-change-mode topic))))
134 (completions (info-lookup->completions topic mode))
135 (enable-recursive-minibuffers t)
138 (mapcar #'car completions))))
139 (list value info-lookup-mode))))
140 (info-lookup 'symbol symbol mode))
142 (defun counsel-unicode-char ()
143 "Insert a Unicode character at point."
145 (let* ((minibuffer-allow-text-properties t)
146 (char (ivy-read "Unicode name: "
149 (format "% -60s%c" (car x) (cdr x))
152 (insert-char (get-text-property 0 'result char))))
154 (declare-function cider-sync-request:complete "ext:cider-client")
155 (defun counsel-clj ()
156 "Clojure completion at point."
162 (cider-sync-request:complete str ":same")))))
164 (defun counsel-git ()
165 "Find file in the current Git repository."
167 (let* ((default-directory (locate-dominating-file
168 default-directory ".git"))
170 (shell-command-to-string
171 "git ls-files --full-name --")
174 (file (ivy-read "Find file: " cands)))
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}'"
183 (string-to-number out)))
185 (defvar counsel--git-grep-count nil
186 "Store the line count in current repository.")
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))
193 (setq ivy--full-length counsel--git-grep-count)
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\"")
198 (if (<= counsel--git-grep-count 20000)
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))))
206 (defun counsel-git-grep ()
207 "Grep for a string in the current git repository."
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))
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)))))))
222 (defun counsel-locate-function (str &rest _u)
223 (if (< (length str) 3)
225 (format "%d chars more" (- 3 (length ivy-text))))
227 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
229 (defun counsel-locate ()
232 (let* ((ivy--dynamic-function 'counsel-locate-function)
233 (val (ivy-read "pattern: " 'counsel-locate-function)))
237 (defun counsel--generic (completion-fn)
238 "Complete thing at point with COMPLETION-FN."
239 (let* ((bnd (bounds-of-thing-at-point 'symbol))
241 (buffer-substring-no-properties
244 (candidates (funcall completion-fn str))
246 (res (ivy-read (format "pattern (%s): " str)
250 (delete-region (car bnd) (cdr bnd)))
255 ;;; counsel.el ends here