]> code.delx.au - gnu-emacs-elpa/blob - counsel.el
counsel.el (counsel-find-file): Extend `find-file'
[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 (defun counsel-find-file ()
263 "Forward to `find-file'."
264 (interactive)
265 (ivy-read "Find file: " 'read-file-name-internal
266 :matcher #'counsel--find-file-matcher
267 :action
268 (lambda (x)
269 (find-file (expand-file-name x ivy--directory)))))
270
271 (defcustom counsel-find-file-ignore-regexp "\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)"
272 "A regexp of files to ignore while in `counsel-find-file'.
273 These files are un-ignored if `ivy-text' matches them.
274 The common way to show all files is to start `ivy-text' with a dot.")
275
276 (defun counsel--find-file-matcher (regexp candidates)
277 "Return REGEXP-matching CANDIDATES.
278 Skip some dotfiles unless `ivy-text' requires them."
279 (let ((res (cl-remove-if-not
280 (lambda (x)
281 (string-match regexp x))
282 candidates)))
283 (if (string-match counsel-find-file-ignore-regexp ivy-text)
284 res
285 (cl-remove-if
286 (lambda (x)
287 (string-match counsel-find-file-ignore-regexp x))
288 res))))
289
290 (defun counsel-git-grep-matcher (regexp candidates)
291 (or (and (equal regexp ivy--old-re)
292 ivy--old-cands)
293 (prog1
294 (setq ivy--old-cands
295 (cl-remove-if-not
296 (lambda (x)
297 (ignore-errors
298 (when (string-match "^[^:]+:[^:]+:" x)
299 (setq x (substring x (match-end 0)))
300 (if (stringp regexp)
301 (string-match regexp x)
302 (let ((res t))
303 (dolist (re regexp)
304 (setq res
305 (and res
306 (ignore-errors
307 (if (cdr re)
308 (string-match (car re) x)
309 (not (string-match (car re) x)))))))
310 res)))))
311 candidates))
312 (setq ivy--old-re regexp))))
313
314 (defun counsel-locate-function (str &rest _u)
315 (if (< (length str) 3)
316 (list ""
317 (format "%d chars more" (- 3 (length ivy-text))))
318 (split-string
319 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
320
321 (defun counsel-locate ()
322 "Call locate."
323 (interactive)
324 (let* ((ivy--dynamic-function 'counsel-locate-function)
325 (val (ivy-read "pattern: " 'counsel-locate-function)))
326 (when val
327 (find-file val))))
328
329 (defun counsel--generic (completion-fn)
330 "Complete thing at point with COMPLETION-FN."
331 (let* ((bnd (bounds-of-thing-at-point 'symbol))
332 (str (if bnd
333 (buffer-substring-no-properties
334 (car bnd) (cdr bnd))
335 ""))
336 (candidates (funcall completion-fn str))
337 (ivy-height 7)
338 (res (ivy-read (format "pattern (%s): " str)
339 candidates)))
340 (when (stringp res)
341 (when bnd
342 (delete-region (car bnd) (cdr bnd)))
343 (insert res))))
344
345 (defun counsel-directory-parent (dir)
346 "Return the directory parent of directory DIR."
347 (concat (file-name-nondirectory
348 (directory-file-name dir)) "/"))
349
350 (defun counsel-string-compose (prefix str)
351 "Make PREFIX the display prefix of STR though text properties."
352 (let ((str (copy-sequence str)))
353 (put-text-property
354 0 1 'display
355 (concat prefix (substring str 0 1))
356 str)
357 str))
358
359 (defun counsel-load-library ()
360 "Load a selected the Emacs Lisp library.
361 The libraries are offered from `load-path'."
362 (interactive)
363 (let ((dirs load-path)
364 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
365 (cands (make-hash-table :test #'equal))
366 short-name
367 old-val
368 dir-parent
369 res)
370 (dolist (dir dirs)
371 (when (file-directory-p dir)
372 (dolist (file (file-name-all-completions "" dir))
373 (when (string-match suffix file)
374 (unless (string-match "pkg.elc?$" file)
375 (setq short-name (substring file 0 (match-beginning 0)))
376 (if (setq old-val (gethash short-name cands))
377 (progn
378 ;; assume going up directory once will resolve name clash
379 (setq dir-parent (counsel-directory-parent (cdr old-val)))
380 (puthash short-name
381 (cons
382 (counsel-string-compose dir-parent (car old-val))
383 (cdr old-val))
384 cands)
385 (setq dir-parent (counsel-directory-parent dir))
386 (puthash (concat dir-parent short-name)
387 (cons
388 (propertize
389 (counsel-string-compose
390 dir-parent short-name)
391 'full-name (expand-file-name file dir))
392 dir)
393 cands))
394 (puthash short-name
395 (cons (propertize
396 short-name
397 'full-name (expand-file-name file dir))
398 dir) cands)))))))
399 (maphash (lambda (_k v) (push (car v) res)) cands)
400 (ivy-read "Load library: " (nreverse res)
401 :action (lambda (x)
402 (load-library
403 (get-text-property 0 'full-name x)))
404 :keymap counsel-describe-map)))
405
406 (provide 'counsel)
407
408 ;;; counsel.el ends here