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)
46 (define-key map (kbd "C-,") #'counsel--info-lookup-symbol)
49 (defun counsel-find-symbol ()
50 "Jump to the definition of the current symbol."
52 (ivy-set-action #'counsel--find-symbol)
55 (defun counsel--info-lookup-symbol ()
56 "Lookup the current symbol in the info docs."
58 (ivy-set-action #'counsel-info-lookup-symbol)
61 (defun counsel--find-symbol (x)
62 "Find symbol definition that corresponds to string X."
63 (let ((full-name (get-text-property 0 'full-name x)))
65 (find-library full-name)
73 (prin1-to-string sym)))
75 (prin1-to-string sym)))
77 (error "Couldn't fild definition of %s"
80 (defvar counsel-describe-symbol-history nil
81 "History for `counsel-describe-variable' and `counsel-describe-function'.")
83 (defun counsel-symbol-at-point ()
84 "Return current symbol at point as a string."
85 (let ((s (thing-at-point 'symbol)))
87 (if (string-match "\\'\\(.*\\)'\\'" s)
91 (defun counsel-describe-variable ()
92 "Forward to `describe-variable'."
94 (let ((enable-recursive-minibuffers t))
100 (when (or (get vv 'variable-documentation)
101 (and (boundp vv) (not (keywordp vv))))
102 (push (symbol-name vv) cands))))
104 :keymap counsel-describe-map
105 :preselect (counsel-symbol-at-point)
106 :history 'counsel-describe-symbol-history
113 (defun counsel-describe-function ()
114 "Forward to `describe-function'."
116 (let ((enable-recursive-minibuffers t))
117 (ivy-read "Describe function: "
122 (push (symbol-name x) cands))))
124 :keymap counsel-describe-map
125 :preselect (counsel-symbol-at-point)
126 :history 'counsel-describe-symbol-history
133 (defvar info-lookup-mode)
134 (declare-function info-lookup->completions "info-look")
135 (declare-function info-lookup->mode-value "info-look")
136 (declare-function info-lookup-select-mode "info-look")
137 (declare-function info-lookup-change-mode "info-look")
138 (declare-function info-lookup "info-look")
140 (defun counsel-info-lookup-symbol (symbol &optional mode)
141 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
145 (let* ((topic 'symbol)
146 (mode (cond (current-prefix-arg
147 (info-lookup-change-mode topic))
148 ((info-lookup->mode-value
149 topic (info-lookup-select-mode))
151 ((info-lookup-change-mode topic))))
152 (completions (info-lookup->completions topic mode))
153 (enable-recursive-minibuffers t)
156 (mapcar #'car completions)
158 (list value info-lookup-mode))))
160 (info-lookup 'symbol symbol mode))
162 (defun counsel-unicode-char ()
163 "Insert a Unicode character at point."
165 (let* ((minibuffer-allow-text-properties t)
166 (char (ivy-read "Unicode name: "
169 (format "% -60s%c" (car x) (cdr x))
172 (insert-char (get-text-property 0 'result char))))
174 (declare-function cider-sync-request:complete "ext:cider-client")
175 (defun counsel-clj ()
176 "Clojure completion at point."
182 (cider-sync-request:complete str ":same")))))
184 (defun counsel-git ()
185 "Find file in the current Git repository."
187 (let* ((default-directory (locate-dominating-file
188 default-directory ".git"))
190 (shell-command-to-string
191 "git ls-files --full-name --")
194 (action (lambda (x) (find-file x))))
195 (ivy-read "Find file: " cands
198 (defvar counsel--git-grep-dir nil
199 "Store the base git directory.")
201 (defun counsel-git-grep-count (str)
202 "Quickly count the amount of git grep STR matches."
203 (let* ((default-directory counsel--git-grep-dir)
204 (out (shell-command-to-string
205 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
207 (string-to-number out)))
209 (defvar counsel--git-grep-count nil
210 "Store the line count in current repository.")
212 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
213 "Grep in the current git repository for STRING."
214 (if (and (> counsel--git-grep-count 20000)
215 (< (length string) 3))
217 (setq ivy--full-length counsel--git-grep-count)
219 (format "%d chars more" (- 3 (length ivy-text)))))
220 (let* ((default-directory counsel--git-grep-dir)
221 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
222 (ivy--regex string t)))
224 (if (<= counsel--git-grep-count 20000)
226 (setq res (shell-command-to-string cmd))
227 (setq ivy--full-length nil))
228 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
229 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
230 (split-string res "\n" t))))
232 (defvar counsel-git-grep-map
233 (let ((map (make-sparse-keymap)))
234 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
237 (defun counsel-git-grep-recenter ()
239 (with-selected-window (ivy-state-window ivy-last)
240 (counsel-git-grep-action)
241 (recenter-top-bottom)))
243 (defun counsel-git-grep-action (x)
244 (let ((lst (split-string x ":")))
245 (find-file (expand-file-name (car lst) counsel--git-grep-dir))
246 (goto-char (point-min))
247 (forward-line (1- (string-to-number (cadr lst))))
248 (unless (eq ivy-exit 'done)
249 (setq swiper--window (selected-window))
251 (swiper--add-overlays (ivy--regex ivy-text)))))
253 (defun counsel-git-grep (&optional initial-input)
254 "Grep for a string in the current git repository."
256 (setq counsel--git-grep-dir
257 (locate-dominating-file default-directory ".git"))
258 (if (null counsel--git-grep-dir)
259 (error "Not in a git repository")
260 (setq counsel--git-grep-count (counsel-git-grep-count ""))
261 (ivy-read "pattern: " 'counsel-git-grep-function
262 :initial-input initial-input
263 :matcher #'counsel-git-grep-matcher
264 :dynamic-collection (when (> counsel--git-grep-count 20000)
265 'counsel-git-grep-function)
266 :keymap counsel-git-grep-map
267 :action #'counsel-git-grep-action
268 :unwind #'swiper--cleanup)))
270 (defcustom counsel-find-file-at-point nil
271 "When non-nil, add file-at-point to the list of candidates."
274 (defun counsel-find-file ()
275 "Forward to `find-file'."
277 (ivy-read "Find file: " 'read-file-name-internal
278 :matcher #'counsel--find-file-matcher
281 (find-file (expand-file-name x ivy--directory)))
282 :preselect (when counsel-find-file-at-point
286 (defcustom counsel-find-file-ignore-regexp "\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)"
287 "A regexp of files to ignore while in `counsel-find-file'.
288 These files are un-ignored if `ivy-text' matches them.
289 The common way to show all files is to start `ivy-text' with a dot.")
291 (defun counsel--find-file-matcher (regexp candidates)
292 "Return REGEXP-matching CANDIDATES.
293 Skip some dotfiles unless `ivy-text' requires them."
294 (let ((res (cl-remove-if-not
296 (string-match regexp x))
298 (if (string-match counsel-find-file-ignore-regexp ivy-text)
302 (string-match counsel-find-file-ignore-regexp x))
305 (defun counsel-git-grep-matcher (regexp candidates)
306 (or (and (equal regexp ivy--old-re)
313 (when (string-match "^[^:]+:[^:]+:" x)
314 (setq x (substring x (match-end 0)))
316 (string-match regexp x)
323 (string-match (car re) x)
324 (not (string-match (car re) x)))))))
327 (setq ivy--old-re regexp))))
329 (defun counsel-locate-function (str &rest _u)
330 (if (< (length str) 3)
332 (format "%d chars more" (- 3 (length ivy-text))))
334 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
336 (defun counsel-locate ()
339 (let* ((ivy--dynamic-function 'counsel-locate-function)
340 (val (ivy-read "pattern: " 'counsel-locate-function)))
344 (defun counsel--generic (completion-fn)
345 "Complete thing at point with COMPLETION-FN."
346 (let* ((bnd (bounds-of-thing-at-point 'symbol))
348 (buffer-substring-no-properties
351 (candidates (funcall completion-fn str))
353 (res (ivy-read (format "pattern (%s): " str)
357 (delete-region (car bnd) (cdr bnd)))
360 (defun counsel-directory-parent (dir)
361 "Return the directory parent of directory DIR."
362 (concat (file-name-nondirectory
363 (directory-file-name dir)) "/"))
365 (defun counsel-string-compose (prefix str)
366 "Make PREFIX the display prefix of STR though text properties."
367 (let ((str (copy-sequence str)))
370 (concat prefix (substring str 0 1))
374 (defun counsel-load-library ()
375 "Load a selected the Emacs Lisp library.
376 The libraries are offered from `load-path'."
378 (let ((dirs load-path)
379 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
380 (cands (make-hash-table :test #'equal))
386 (when (file-directory-p dir)
387 (dolist (file (file-name-all-completions "" dir))
388 (when (string-match suffix file)
389 (unless (string-match "pkg.elc?$" file)
390 (setq short-name (substring file 0 (match-beginning 0)))
391 (if (setq old-val (gethash short-name cands))
393 ;; assume going up directory once will resolve name clash
394 (setq dir-parent (counsel-directory-parent (cdr old-val)))
397 (counsel-string-compose dir-parent (car old-val))
400 (setq dir-parent (counsel-directory-parent dir))
401 (puthash (concat dir-parent short-name)
404 (counsel-string-compose
405 dir-parent short-name)
406 'full-name (expand-file-name file dir))
412 'full-name (expand-file-name file dir))
414 (maphash (lambda (_k v) (push (car v) res)) cands)
415 (ivy-read "Load library: " (nreverse res)
418 (get-text-property 0 'full-name x)))
419 :keymap counsel-describe-map)))
423 ;;; counsel.el ends here