]> code.delx.au - gnu-emacs-elpa/blob - counsel.el
counsel.el (counsel-find-file-at-point): New defcustom
[gnu-emacs-elpa] / counsel.el
1 ;;; counsel.el --- Various completion functions using Ivy -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
7 ;; Version: 0.1.0
8 ;; Package-Requires: ((emacs "24.1") (swiper "0.4.0"))
9 ;; Keywords: completion, matching
10
11 ;; This file is part of GNU Emacs.
12
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)
16 ;; any later version.
17
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.
22
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
28 ;; Just call one of the interactive functions in this file to complete
29 ;; the corresponding thing using `ivy'.
30 ;;
31 ;; Currently available: Elisp symbols, Clojure symbols, Git files.
32
33 ;;; Code:
34
35 (require 'swiper)
36
37 (defun counsel-el ()
38 "Elisp completion at point."
39 (interactive)
40 (counsel--generic
41 (lambda (str) (all-completions str obarray))))
42
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)
47 map))
48
49 (defun counsel-find-symbol ()
50 "Jump to the definition of the current symbol."
51 (interactive)
52 (ivy-set-action #'counsel--find-symbol)
53 (ivy-done))
54
55 (defun counsel--info-lookup-symbol ()
56 "Lookup the current symbol in the info docs."
57 (interactive)
58 (ivy-set-action #'counsel-info-lookup-symbol)
59 (ivy-done))
60
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)))
64 (if full-name
65 (find-library full-name)
66 (let ((sym (read x)))
67 (cond ((boundp sym)
68 (find-variable sym))
69 ((fboundp sym)
70 (find-function sym))
71 ((or (featurep sym)
72 (locate-library
73 (prin1-to-string sym)))
74 (find-library
75 (prin1-to-string sym)))
76 (t
77 (error "Couldn't fild definition of %s"
78 sym)))))))
79
80 (defvar counsel-describe-symbol-history nil
81 "History for `counsel-describe-variable' and `counsel-describe-function'.")
82
83 (defun counsel-describe-variable ()
84 "Forward to `describe-variable'."
85 (interactive)
86 (let ((enable-recursive-minibuffers t))
87 (ivy-read
88 "Describe variable: "
89 (let (cands)
90 (mapatoms
91 (lambda (vv)
92 (when (or (get vv 'variable-documentation)
93 (and (boundp vv) (not (keywordp vv))))
94 (push (symbol-name vv) cands))))
95 cands)
96 :keymap counsel-describe-map
97 :preselect (thing-at-point 'symbol)
98 :history 'counsel-describe-symbol-history
99 :require-match t
100 :sort t
101 :action (lambda (x)
102 (describe-variable
103 (intern x))))))
104
105 (defun counsel-describe-function ()
106 "Forward to `describe-function'."
107 (interactive)
108 (let ((enable-recursive-minibuffers t))
109 (ivy-read "Describe function: "
110 (let (cands)
111 (mapatoms
112 (lambda (x)
113 (when (fboundp x)
114 (push (symbol-name x) cands))))
115 cands)
116 :keymap counsel-describe-map
117 :preselect (thing-at-point 'symbol)
118 :history 'counsel-describe-symbol-history
119 :require-match t
120 :sort t
121 :action (lambda (x)
122 (describe-function
123 (intern x))))))
124
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")
131
132 (defun counsel-info-lookup-symbol (symbol &optional mode)
133 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
134 (interactive
135 (progn
136 (require 'info-look)
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))
142 info-lookup-mode)
143 ((info-lookup-change-mode topic))))
144 (completions (info-lookup->completions topic mode))
145 (enable-recursive-minibuffers t)
146 (value (ivy-read
147 "Describe symbol: "
148 (mapcar #'car completions)
149 :sort t)))
150 (list value info-lookup-mode))))
151 (require 'info-look)
152 (info-lookup 'symbol symbol mode))
153
154 (defun counsel-unicode-char ()
155 "Insert a Unicode character at point."
156 (interactive)
157 (let* ((minibuffer-allow-text-properties t)
158 (char (ivy-read "Unicode name: "
159 (mapcar (lambda (x)
160 (propertize
161 (format "% -60s%c" (car x) (cdr x))
162 'result (cdr x)))
163 (ucs-names)))))
164 (insert-char (get-text-property 0 'result char))))
165
166 (declare-function cider-sync-request:complete "ext:cider-client")
167 (defun counsel-clj ()
168 "Clojure completion at point."
169 (interactive)
170 (counsel--generic
171 (lambda (str)
172 (mapcar
173 #'cl-caddr
174 (cider-sync-request:complete str ":same")))))
175
176 (defun counsel-git ()
177 "Find file in the current Git repository."
178 (interactive)
179 (let* ((default-directory (locate-dominating-file
180 default-directory ".git"))
181 (cands (split-string
182 (shell-command-to-string
183 "git ls-files --full-name --")
184 "\n"
185 t))
186 (action (lambda (x) (find-file x))))
187 (ivy-read "Find file: " cands
188 :action action)))
189
190 (defvar counsel--git-grep-dir nil
191 "Store the base git directory.")
192
193 (defun counsel-git-grep-count (str)
194 "Quickly count the amount of git grep STR matches."
195 (let* ((default-directory counsel--git-grep-dir)
196 (out (shell-command-to-string
197 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
198 (ivy--regex str)))))
199 (string-to-number out)))
200
201 (defvar counsel--git-grep-count nil
202 "Store the line count in current repository.")
203
204 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
205 "Grep in the current git repository for STRING."
206 (if (and (> counsel--git-grep-count 20000)
207 (< (length string) 3))
208 (progn
209 (setq ivy--full-length counsel--git-grep-count)
210 (list ""
211 (format "%d chars more" (- 3 (length ivy-text)))))
212 (let* ((default-directory counsel--git-grep-dir)
213 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
214 (ivy--regex string t)))
215 res)
216 (if (<= counsel--git-grep-count 20000)
217 (progn
218 (setq res (shell-command-to-string cmd))
219 (setq ivy--full-length nil))
220 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
221 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
222 (split-string res "\n" t))))
223
224 (defvar counsel-git-grep-map
225 (let ((map (make-sparse-keymap)))
226 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
227 map))
228
229 (defun counsel-git-grep-recenter ()
230 (interactive)
231 (with-selected-window (ivy-state-window ivy-last)
232 (counsel-git-grep-action)
233 (recenter-top-bottom)))
234
235 (defun counsel-git-grep-action (x)
236 (let ((lst (split-string x ":")))
237 (find-file (expand-file-name (car lst) counsel--git-grep-dir))
238 (goto-char (point-min))
239 (forward-line (1- (string-to-number (cadr lst))))
240 (unless (eq ivy-exit 'done)
241 (setq swiper--window (selected-window))
242 (swiper--cleanup)
243 (swiper--add-overlays (ivy--regex ivy-text)))))
244
245 (defun counsel-git-grep (&optional initial-input)
246 "Grep for a string in the current git repository."
247 (interactive)
248 (setq counsel--git-grep-dir
249 (locate-dominating-file default-directory ".git"))
250 (if (null counsel--git-grep-dir)
251 (error "Not in a git repository")
252 (setq counsel--git-grep-count (counsel-git-grep-count ""))
253 (ivy-read "pattern: " 'counsel-git-grep-function
254 :initial-input initial-input
255 :matcher #'counsel-git-grep-matcher
256 :dynamic-collection (when (> counsel--git-grep-count 20000)
257 'counsel-git-grep-function)
258 :keymap counsel-git-grep-map
259 :action #'counsel-git-grep-action
260 :unwind #'swiper--cleanup)))
261
262 (defcustom counsel-find-file-at-point nil
263 "When non-nil, add file-at-point to the list of candidates."
264 :type 'boolean)
265
266 (defun counsel-find-file ()
267 "Forward to `find-file'."
268 (interactive)
269 (ivy-read "Find file: " 'read-file-name-internal
270 :matcher #'counsel--find-file-matcher
271 :action
272 (lambda (x)
273 (find-file (expand-file-name x ivy--directory)))
274 :preselect (when counsel-find-file-at-point
275 (require 'ffap)
276 (ffap-guesser))))
277
278 (defcustom counsel-find-file-ignore-regexp "\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)"
279 "A regexp of files to ignore while in `counsel-find-file'.
280 These files are un-ignored if `ivy-text' matches them.
281 The common way to show all files is to start `ivy-text' with a dot.")
282
283 (defun counsel--find-file-matcher (regexp candidates)
284 "Return REGEXP-matching CANDIDATES.
285 Skip some dotfiles unless `ivy-text' requires them."
286 (let ((res (cl-remove-if-not
287 (lambda (x)
288 (string-match regexp x))
289 candidates)))
290 (if (string-match counsel-find-file-ignore-regexp ivy-text)
291 res
292 (cl-remove-if
293 (lambda (x)
294 (string-match counsel-find-file-ignore-regexp x))
295 res))))
296
297 (defun counsel-git-grep-matcher (regexp candidates)
298 (or (and (equal regexp ivy--old-re)
299 ivy--old-cands)
300 (prog1
301 (setq ivy--old-cands
302 (cl-remove-if-not
303 (lambda (x)
304 (ignore-errors
305 (when (string-match "^[^:]+:[^:]+:" x)
306 (setq x (substring x (match-end 0)))
307 (if (stringp regexp)
308 (string-match regexp x)
309 (let ((res t))
310 (dolist (re regexp)
311 (setq res
312 (and res
313 (ignore-errors
314 (if (cdr re)
315 (string-match (car re) x)
316 (not (string-match (car re) x)))))))
317 res)))))
318 candidates))
319 (setq ivy--old-re regexp))))
320
321 (defun counsel-locate-function (str &rest _u)
322 (if (< (length str) 3)
323 (list ""
324 (format "%d chars more" (- 3 (length ivy-text))))
325 (split-string
326 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
327
328 (defun counsel-locate ()
329 "Call locate."
330 (interactive)
331 (let* ((ivy--dynamic-function 'counsel-locate-function)
332 (val (ivy-read "pattern: " 'counsel-locate-function)))
333 (when val
334 (find-file val))))
335
336 (defun counsel--generic (completion-fn)
337 "Complete thing at point with COMPLETION-FN."
338 (let* ((bnd (bounds-of-thing-at-point 'symbol))
339 (str (if bnd
340 (buffer-substring-no-properties
341 (car bnd) (cdr bnd))
342 ""))
343 (candidates (funcall completion-fn str))
344 (ivy-height 7)
345 (res (ivy-read (format "pattern (%s): " str)
346 candidates)))
347 (when (stringp res)
348 (when bnd
349 (delete-region (car bnd) (cdr bnd)))
350 (insert res))))
351
352 (defun counsel-directory-parent (dir)
353 "Return the directory parent of directory DIR."
354 (concat (file-name-nondirectory
355 (directory-file-name dir)) "/"))
356
357 (defun counsel-string-compose (prefix str)
358 "Make PREFIX the display prefix of STR though text properties."
359 (let ((str (copy-sequence str)))
360 (put-text-property
361 0 1 'display
362 (concat prefix (substring str 0 1))
363 str)
364 str))
365
366 (defun counsel-load-library ()
367 "Load a selected the Emacs Lisp library.
368 The libraries are offered from `load-path'."
369 (interactive)
370 (let ((dirs load-path)
371 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
372 (cands (make-hash-table :test #'equal))
373 short-name
374 old-val
375 dir-parent
376 res)
377 (dolist (dir dirs)
378 (when (file-directory-p dir)
379 (dolist (file (file-name-all-completions "" dir))
380 (when (string-match suffix file)
381 (unless (string-match "pkg.elc?$" file)
382 (setq short-name (substring file 0 (match-beginning 0)))
383 (if (setq old-val (gethash short-name cands))
384 (progn
385 ;; assume going up directory once will resolve name clash
386 (setq dir-parent (counsel-directory-parent (cdr old-val)))
387 (puthash short-name
388 (cons
389 (counsel-string-compose dir-parent (car old-val))
390 (cdr old-val))
391 cands)
392 (setq dir-parent (counsel-directory-parent dir))
393 (puthash (concat dir-parent short-name)
394 (cons
395 (propertize
396 (counsel-string-compose
397 dir-parent short-name)
398 'full-name (expand-file-name file dir))
399 dir)
400 cands))
401 (puthash short-name
402 (cons (propertize
403 short-name
404 'full-name (expand-file-name file dir))
405 dir) cands)))))))
406 (maphash (lambda (_k v) (push (car v) res)) cands)
407 (ivy-read "Load library: " (nreverse res)
408 :action (lambda (x)
409 (load-library
410 (get-text-property 0 'full-name x)))
411 :keymap counsel-describe-map)))
412
413 (provide 'counsel)
414
415 ;;; counsel.el ends here