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