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.4.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 (x)
55 "Find symbol definition that corresponds to string X."
56 (let ((full-name (get-text-property 0 'full-name x)))
58 (find-library full-name)
66 (prin1-to-string sym)))
68 (prin1-to-string sym)))
70 (error "Couldn't fild definition of %s"
73 (defvar counsel-describe-symbol-history nil
74 "History for `counsel-describe-variable' and `counsel-describe-function'.")
76 (defun counsel-describe-variable ()
77 "Forward to `describe-variable'."
79 (let ((enable-recursive-minibuffers t))
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
90 :preselect (thing-at-point 'symbol)
91 :history 'counsel-describe-symbol-history
98 (defun counsel-describe-function ()
99 "Forward to `describe-function'."
101 (let ((enable-recursive-minibuffers t))
102 (ivy-read "Describe function: "
107 (push (symbol-name x) cands))))
109 :keymap counsel-describe-map
110 :preselect (thing-at-point 'symbol)
111 :history 'counsel-describe-symbol-history
118 (defvar info-lookup-mode)
119 (declare-function info-lookup->completions "info-look")
120 (declare-function info-lookup->mode-value "info-look")
121 (declare-function info-lookup-select-mode "info-look")
122 (declare-function info-lookup-change-mode "info-look")
123 (declare-function info-lookup "info-look")
125 (defun counsel-info-lookup-symbol (symbol &optional mode)
126 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
130 (let* ((topic 'symbol)
131 (mode (cond (current-prefix-arg
132 (info-lookup-change-mode topic))
133 ((info-lookup->mode-value
134 topic (info-lookup-select-mode))
136 ((info-lookup-change-mode topic))))
137 (completions (info-lookup->completions topic mode))
138 (enable-recursive-minibuffers t)
141 (mapcar #'car completions)
143 (list value info-lookup-mode))))
144 (info-lookup 'symbol symbol mode))
146 (defun counsel-unicode-char ()
147 "Insert a Unicode character at point."
149 (let* ((minibuffer-allow-text-properties t)
150 (char (ivy-read "Unicode name: "
153 (format "% -60s%c" (car x) (cdr x))
156 (insert-char (get-text-property 0 'result char))))
158 (declare-function cider-sync-request:complete "ext:cider-client")
159 (defun counsel-clj ()
160 "Clojure completion at point."
166 (cider-sync-request:complete str ":same")))))
168 (defun counsel-git ()
169 "Find file in the current Git repository."
171 (let* ((default-directory (locate-dominating-file
172 default-directory ".git"))
174 (shell-command-to-string
175 "git ls-files --full-name --")
178 (action (lambda (x) (find-file x))))
179 (ivy-read "Find file: " cands
182 (defvar counsel--git-grep-dir nil
183 "Store the base git directory.")
185 (defun counsel-git-grep-count (str)
186 "Quickly count the amount of git grep STR matches."
187 (let* ((default-directory counsel--git-grep-dir)
188 (out (shell-command-to-string
189 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
191 (string-to-number out)))
193 (defvar counsel--git-grep-count nil
194 "Store the line count in current repository.")
196 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
197 "Grep in the current git repository for STRING."
198 (if (and (> counsel--git-grep-count 20000)
199 (< (length string) 3))
201 (setq ivy--full-length counsel--git-grep-count)
203 (format "%d chars more" (- 3 (length ivy-text)))))
204 (let* ((default-directory counsel--git-grep-dir)
205 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
206 (ivy--regex string t)))
208 (if (<= counsel--git-grep-count 20000)
210 (setq res (shell-command-to-string cmd))
211 (setq ivy--full-length nil))
212 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
213 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
214 (split-string res "\n" t))))
216 (defvar counsel-git-grep-map
217 (let ((map (make-sparse-keymap)))
218 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
221 (defun counsel-git-grep-recenter ()
223 (with-selected-window (ivy-state-window ivy-last)
224 (counsel-git-grep-action)
225 (recenter-top-bottom)))
227 (defun counsel-git-grep-action (x)
228 (let ((lst (split-string x ":")))
229 (find-file (expand-file-name (car lst) counsel--git-grep-dir))
230 (goto-char (point-min))
231 (forward-line (1- (string-to-number (cadr lst))))
232 (unless (eq ivy-exit 'done)
233 (setq swiper--window (selected-window))
235 (swiper--add-overlays (ivy--regex ivy-text)))))
237 (defun counsel-git-grep (&optional initial-input)
238 "Grep for a string in the current git repository."
240 (setq counsel--git-grep-dir
241 (locate-dominating-file default-directory ".git"))
242 (if (null counsel--git-grep-dir)
243 (error "Not in a git repository")
244 (setq counsel--git-grep-count (counsel-git-grep-count ""))
245 (ivy-read "pattern: " 'counsel-git-grep-function
246 :initial-input initial-input
247 :matcher #'counsel-git-grep-matcher
248 :dynamic-collection (when (> counsel--git-grep-count 20000)
249 'counsel-git-grep-function)
250 :keymap counsel-git-grep-map
251 :action #'counsel-git-grep-action
252 :unwind #'swiper--cleanup)))
254 (defun counsel-git-grep-matcher (x)
256 (when (string-match "^[^:]+:[^:]+:" x)
257 (setq x (substring x (match-end 0)))
258 (if (stringp ivy--old-re)
259 (string-match ivy--old-re x)
261 (dolist (re ivy--old-re)
266 (string-match (car re) x)
267 (not (string-match (car re) x)))))))
270 (defun counsel-locate-function (str &rest _u)
271 (if (< (length str) 3)
273 (format "%d chars more" (- 3 (length ivy-text))))
275 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
277 (defun counsel-locate ()
280 (let* ((ivy--dynamic-function 'counsel-locate-function)
281 (val (ivy-read "pattern: " 'counsel-locate-function)))
285 (defun counsel--generic (completion-fn)
286 "Complete thing at point with COMPLETION-FN."
287 (let* ((bnd (bounds-of-thing-at-point 'symbol))
289 (buffer-substring-no-properties
292 (candidates (funcall completion-fn str))
294 (res (ivy-read (format "pattern (%s): " str)
298 (delete-region (car bnd) (cdr bnd)))
301 (defun counsel-directory-parent (dir)
302 "Return the directory parent of directory DIR."
303 (concat (file-name-nondirectory
304 (directory-file-name dir)) "/"))
306 (defun counsel-string-compose (prefix str)
307 "Make PREFIX the display prefix of STR though text properties."
308 (let ((str (copy-sequence str)))
311 (concat prefix (substring str 0 1))
315 (defun counsel-load-library ()
316 "Load a selected the Emacs Lisp library.
317 The libraries are offered from `load-path'."
319 (let ((dirs load-path)
320 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
321 (cands (make-hash-table :test #'equal))
327 (when (file-directory-p dir)
328 (dolist (file (file-name-all-completions "" dir))
329 (when (string-match suffix file)
330 (unless (string-match "pkg.elc?$" file)
331 (setq short-name (substring file 0 (match-beginning 0)))
332 (if (setq old-val (gethash short-name cands))
334 ;; assume going up directory once will resolve name clash
335 (setq dir-parent (counsel-directory-parent (cdr old-val)))
338 (counsel-string-compose dir-parent (car old-val))
341 (setq dir-parent (counsel-directory-parent dir))
342 (puthash (concat dir-parent short-name)
345 (counsel-string-compose
346 dir-parent short-name)
347 'full-name (expand-file-name file dir))
353 'full-name (expand-file-name file dir))
355 (maphash (lambda (_k v) (push (car v) res)) cands)
356 (ivy-read "Load library: " (nreverse res)
359 (get-text-property 0 'full-name x)))
360 :keymap counsel-describe-map)))
364 ;;; counsel.el ends here