]> code.delx.au - gnu-emacs-elpa/blob - counsel.el
Add info lookup binding to counsel-describe-map
[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 (info-lookup 'symbol symbol mode))
152
153 (defun counsel-unicode-char ()
154 "Insert a Unicode character at point."
155 (interactive)
156 (let* ((minibuffer-allow-text-properties t)
157 (char (ivy-read "Unicode name: "
158 (mapcar (lambda (x)
159 (propertize
160 (format "% -60s%c" (car x) (cdr x))
161 'result (cdr x)))
162 (ucs-names)))))
163 (insert-char (get-text-property 0 'result char))))
164
165 (declare-function cider-sync-request:complete "ext:cider-client")
166 (defun counsel-clj ()
167 "Clojure completion at point."
168 (interactive)
169 (counsel--generic
170 (lambda (str)
171 (mapcar
172 #'cl-caddr
173 (cider-sync-request:complete str ":same")))))
174
175 (defun counsel-git ()
176 "Find file in the current Git repository."
177 (interactive)
178 (let* ((default-directory (locate-dominating-file
179 default-directory ".git"))
180 (cands (split-string
181 (shell-command-to-string
182 "git ls-files --full-name --")
183 "\n"
184 t))
185 (action (lambda (x) (find-file x))))
186 (ivy-read "Find file: " cands
187 :action action)))
188
189 (defvar counsel--git-grep-dir nil
190 "Store the base git directory.")
191
192 (defun counsel-git-grep-count (str)
193 "Quickly count the amount of git grep STR matches."
194 (let* ((default-directory counsel--git-grep-dir)
195 (out (shell-command-to-string
196 (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
197 (ivy--regex str)))))
198 (string-to-number out)))
199
200 (defvar counsel--git-grep-count nil
201 "Store the line count in current repository.")
202
203 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
204 "Grep in the current git repository for STRING."
205 (if (and (> counsel--git-grep-count 20000)
206 (< (length string) 3))
207 (progn
208 (setq ivy--full-length counsel--git-grep-count)
209 (list ""
210 (format "%d chars more" (- 3 (length ivy-text)))))
211 (let* ((default-directory counsel--git-grep-dir)
212 (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
213 (ivy--regex string t)))
214 res)
215 (if (<= counsel--git-grep-count 20000)
216 (progn
217 (setq res (shell-command-to-string cmd))
218 (setq ivy--full-length nil))
219 (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
220 (setq ivy--full-length (counsel-git-grep-count ivy-text)))
221 (split-string res "\n" t))))
222
223 (defvar counsel-git-grep-map
224 (let ((map (make-sparse-keymap)))
225 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
226 map))
227
228 (defun counsel-git-grep-recenter ()
229 (interactive)
230 (with-selected-window (ivy-state-window ivy-last)
231 (counsel-git-grep-action)
232 (recenter-top-bottom)))
233
234 (defun counsel-git-grep-action (x)
235 (let ((lst (split-string x ":")))
236 (find-file (expand-file-name (car lst) counsel--git-grep-dir))
237 (goto-char (point-min))
238 (forward-line (1- (string-to-number (cadr lst))))
239 (unless (eq ivy-exit 'done)
240 (setq swiper--window (selected-window))
241 (swiper--cleanup)
242 (swiper--add-overlays (ivy--regex ivy-text)))))
243
244 (defun counsel-git-grep (&optional initial-input)
245 "Grep for a string in the current git repository."
246 (interactive)
247 (setq counsel--git-grep-dir
248 (locate-dominating-file default-directory ".git"))
249 (if (null counsel--git-grep-dir)
250 (error "Not in a git repository")
251 (setq counsel--git-grep-count (counsel-git-grep-count ""))
252 (ivy-read "pattern: " 'counsel-git-grep-function
253 :initial-input initial-input
254 :matcher #'counsel-git-grep-matcher
255 :dynamic-collection (when (> counsel--git-grep-count 20000)
256 'counsel-git-grep-function)
257 :keymap counsel-git-grep-map
258 :action #'counsel-git-grep-action
259 :unwind #'swiper--cleanup)))
260
261 (defun counsel-git-grep-matcher (regexp candidates)
262 (or (and (equal regexp ivy--old-re)
263 ivy--old-cands)
264 (prog1
265 (setq ivy--old-cands
266 (cl-remove-if-not
267 (lambda (x)
268 (ignore-errors
269 (when (string-match "^[^:]+:[^:]+:" x)
270 (setq x (substring x (match-end 0)))
271 (if (stringp regexp)
272 (string-match regexp x)
273 (let ((res t))
274 (dolist (re regexp)
275 (setq res
276 (and res
277 (ignore-errors
278 (if (cdr re)
279 (string-match (car re) x)
280 (not (string-match (car re) x)))))))
281 res)))))
282 candidates))
283 (setq ivy--old-re regexp))))
284
285 (defun counsel-locate-function (str &rest _u)
286 (if (< (length str) 3)
287 (list ""
288 (format "%d chars more" (- 3 (length ivy-text))))
289 (split-string
290 (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
291
292 (defun counsel-locate ()
293 "Call locate."
294 (interactive)
295 (let* ((ivy--dynamic-function 'counsel-locate-function)
296 (val (ivy-read "pattern: " 'counsel-locate-function)))
297 (when val
298 (find-file val))))
299
300 (defun counsel--generic (completion-fn)
301 "Complete thing at point with COMPLETION-FN."
302 (let* ((bnd (bounds-of-thing-at-point 'symbol))
303 (str (if bnd
304 (buffer-substring-no-properties
305 (car bnd) (cdr bnd))
306 ""))
307 (candidates (funcall completion-fn str))
308 (ivy-height 7)
309 (res (ivy-read (format "pattern (%s): " str)
310 candidates)))
311 (when (stringp res)
312 (when bnd
313 (delete-region (car bnd) (cdr bnd)))
314 (insert res))))
315
316 (defun counsel-directory-parent (dir)
317 "Return the directory parent of directory DIR."
318 (concat (file-name-nondirectory
319 (directory-file-name dir)) "/"))
320
321 (defun counsel-string-compose (prefix str)
322 "Make PREFIX the display prefix of STR though text properties."
323 (let ((str (copy-sequence str)))
324 (put-text-property
325 0 1 'display
326 (concat prefix (substring str 0 1))
327 str)
328 str))
329
330 (defun counsel-load-library ()
331 "Load a selected the Emacs Lisp library.
332 The libraries are offered from `load-path'."
333 (interactive)
334 (let ((dirs load-path)
335 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
336 (cands (make-hash-table :test #'equal))
337 short-name
338 old-val
339 dir-parent
340 res)
341 (dolist (dir dirs)
342 (when (file-directory-p dir)
343 (dolist (file (file-name-all-completions "" dir))
344 (when (string-match suffix file)
345 (unless (string-match "pkg.elc?$" file)
346 (setq short-name (substring file 0 (match-beginning 0)))
347 (if (setq old-val (gethash short-name cands))
348 (progn
349 ;; assume going up directory once will resolve name clash
350 (setq dir-parent (counsel-directory-parent (cdr old-val)))
351 (puthash short-name
352 (cons
353 (counsel-string-compose dir-parent (car old-val))
354 (cdr old-val))
355 cands)
356 (setq dir-parent (counsel-directory-parent dir))
357 (puthash (concat dir-parent short-name)
358 (cons
359 (propertize
360 (counsel-string-compose
361 dir-parent short-name)
362 'full-name (expand-file-name file dir))
363 dir)
364 cands))
365 (puthash short-name
366 (cons (propertize
367 short-name
368 'full-name (expand-file-name file dir))
369 dir) cands)))))))
370 (maphash (lambda (_k v) (push (car v) res)) cands)
371 (ivy-read "Load library: " (nreverse res)
372 :action (lambda (x)
373 (load-library
374 (get-text-property 0 'full-name x)))
375 :keymap counsel-describe-map)))
376
377 (provide 'counsel)
378
379 ;;; counsel.el ends here