]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/counsel.el
Merge commit 'ac0fc8962eaa15625b127c32f75d2b3daa1ca6ef' from swiper
[gnu-emacs-elpa] / packages / swiper / 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.3.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 'ivy)
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 map))
47
48 (defun counsel-find-symbol ()
49 "Jump to the definition of the current symbol."
50 (interactive)
51 (setq ivy--action 'counsel--find-symbol)
52 (setq ivy-exit 'done)
53 (exit-minibuffer))
54
55 (defun counsel--find-symbol ()
56 (let ((sym (read ivy--current)))
57 (cond ((boundp sym)
58 (find-variable sym))
59 ((fboundp sym)
60 (find-function sym))
61 ((or (featurep sym)
62 (locate-library
63 (prin1-to-string sym)))
64 (find-library (prin1-to-string sym)))
65 (t
66 (error "Couldn't fild definition of %s"
67 sym)))))
68
69 (defun counsel-describe-variable (variable &optional buffer frame)
70 "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
71 (interactive
72 (let ((v (variable-at-point))
73 (enable-recursive-minibuffers t)
74 (preselect (thing-at-point 'symbol))
75 val)
76 (setq val (ivy-read
77 (if (symbolp v)
78 (format
79 "Describe variable (default %s): " v)
80 "Describe variable: ")
81 (let (cands)
82 (mapatoms
83 (lambda (vv)
84 (when (or (get vv 'variable-documentation)
85 (and (boundp vv) (not (keywordp vv))))
86 (push (symbol-name vv) cands))))
87 cands)
88 nil nil counsel-describe-map preselect))
89 (list (if (equal val "")
90 v
91 (intern val)))))
92 (describe-variable variable buffer frame))
93
94 (defun counsel-describe-function (function)
95 "Forward to (`describe-function' FUNCTION) with ivy completion."
96 (interactive
97 (let ((fn (function-called-at-point))
98 (enable-recursive-minibuffers t)
99 (preselect (thing-at-point 'symbol))
100 val)
101 (setq val (ivy-read (if fn
102 (format "Describe function (default %s): " fn)
103 "Describe function: ")
104 (let (cands)
105 (mapatoms
106 (lambda (x)
107 (when (fboundp x)
108 (push (symbol-name x) cands))))
109 cands)
110 nil nil counsel-describe-map preselect))
111 (list (if (equal val "")
112 fn (intern val)))))
113 (describe-function function))
114
115 (defvar info-lookup-mode)
116 (declare-function info-lookup->completions "info-look")
117 (declare-function info-lookup->mode-value "info-look")
118 (declare-function info-lookup-select-mode "info-look")
119 (declare-function info-lookup-change-mode "info-look")
120 (declare-function info-lookup "info-look")
121
122 (defun counsel-info-lookup-symbol (symbol &optional mode)
123 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
124 (interactive
125 (progn
126 (require 'info-look)
127 (let* ((topic 'symbol)
128 (mode (cond (current-prefix-arg
129 (info-lookup-change-mode topic))
130 ((info-lookup->mode-value
131 topic (info-lookup-select-mode))
132 info-lookup-mode)
133 ((info-lookup-change-mode topic))))
134 (completions (info-lookup->completions topic mode))
135 (enable-recursive-minibuffers t)
136 (value (ivy-read
137 "Describe symbol: "
138 (mapcar #'car completions))))
139 (list value info-lookup-mode))))
140 (info-lookup 'symbol symbol mode))
141
142 (defun counsel-unicode-char ()
143 "Insert a Unicode character at point."
144 (interactive)
145 (let* ((minibuffer-allow-text-properties t)
146 (char (ivy-read "Unicode name: "
147 (mapcar (lambda (x)
148 (propertize
149 (format "% -60s%c" (car x) (cdr x))
150 'result (cdr x)))
151 (ucs-names)))))
152 (insert-char (get-text-property 0 'result char))))
153
154 (declare-function cider-sync-request:complete "ext:cider-client")
155 (defun counsel-clj ()
156 "Clojure completion at point."
157 (interactive)
158 (counsel--generic
159 (lambda (str)
160 (mapcar
161 #'cl-caddr
162 (cider-sync-request:complete str ":same")))))
163
164 (defun counsel-git ()
165 "Find file in the current Git repository."
166 (interactive)
167 (let* ((default-directory (locate-dominating-file
168 default-directory ".git"))
169 (cands (split-string
170 (shell-command-to-string
171 "git ls-files --full-name --")
172 "\n"
173 t))
174 (file (ivy-read "Find file: " cands)))
175 (when file
176 (find-file file))))
177
178 (defun counsel-git-grep-count (str)
179 "Quickly count the amount of git grep STR matches."
180 (shell-command-to-string
181 (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'" str)))
182
183 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
184 "Grep in the current git repository for STRING."
185 (split-string
186 (shell-command-to-string
187 (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\"" string))
188 "\n"
189 t))
190
191 (defun counsel-git-grep ()
192 "Grep for a string in the current git repository."
193 (interactive)
194 (let ((default-directory (locate-dominating-file
195 default-directory ".git"))
196 (val (ivy-read "pattern: " 'counsel-git-grep-function))
197 lst)
198 (when val
199 (setq lst (split-string val ":"))
200 (find-file (car lst))
201 (goto-char (point-min))
202 (forward-line (1- (string-to-number (cadr lst)))))))
203
204 (defun counsel--generic (completion-fn)
205 "Complete thing at point with COMPLETION-FN."
206 (let* ((bnd (bounds-of-thing-at-point 'symbol))
207 (str (if bnd
208 (buffer-substring-no-properties
209 (car bnd) (cdr bnd))
210 ""))
211 (candidates (funcall completion-fn str))
212 (ivy-height 7)
213 (res (ivy-read (format "pattern (%s): " str)
214 candidates)))
215 (when (stringp res)
216 (when bnd
217 (delete-region (car bnd) (cdr bnd)))
218 (insert res))))
219
220 (provide 'counsel)
221
222 ;;; counsel.el ends here