1 ;;; counsel.el --- Various completion functions using Ivy -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
8 ;; Package-Requires: ((emacs "24.1") (swiper "0.2.1"))
9 ;; Keywords: completion, matching
11 ;; This file is part of GNU Emacs.
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)
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.
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
28 ;; Just call one of the interactive functions in this file to complete
29 ;; the corresponding thing using `ivy'.
31 ;; Currently available: Elisp symbols, Clojure symbols, Git files.
38 "Elisp completion at point."
41 (lambda (str) (all-completions str obarray))))
43 (defun counsel-describe-variable (variable &optional buffer frame)
44 "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
46 (let ((v (variable-at-point))
47 (enable-recursive-minibuffers t)
48 (preselect (thing-at-point 'symbol))
53 "Describe variable (default %s): " v)
54 "Describe variable: ")
58 (when (or (get vv 'variable-documentation)
59 (and (boundp vv) (not (keywordp vv))))
60 (push (symbol-name vv) cands))))
62 nil nil nil preselect))
63 (list (if (equal val "")
66 (describe-variable variable buffer frame))
68 (defun counsel-describe-function (function)
69 "Forward to (`describe-function' FUNCTION) with ivy completion."
71 (let ((fn (function-called-at-point))
72 (enable-recursive-minibuffers t)
73 (preselect (thing-at-point 'symbol))
75 (setq val (ivy-read (if fn
76 (format "Describe function (default %s): " fn)
77 "Describe function: ")
82 (push (symbol-name x) cands))))
84 nil nil nil preselect))
85 (list (if (equal val "")
87 (describe-function function))
89 (defvar info-lookup-mode)
90 (declare-function info-lookup->completions "info-look")
91 (declare-function info-lookup->mode-value "info-look")
92 (declare-function info-lookup-select-mode "info-look")
93 (declare-function info-lookup-change-mode "info-look")
94 (declare-function info-lookup "info-look")
96 (defun counsel-info-lookup-symbol (symbol &optional mode)
97 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
101 (let* ((topic 'symbol)
102 (mode (cond (current-prefix-arg
103 (info-lookup-change-mode topic))
104 ((info-lookup->mode-value
105 topic (info-lookup-select-mode))
107 ((info-lookup-change-mode topic))))
108 (completions (info-lookup->completions topic mode))
109 (enable-recursive-minibuffers t)
112 (mapcar #'car completions))))
113 (list value info-lookup-mode))))
114 (info-lookup 'symbol symbol mode))
116 (defun counsel-unicode-char ()
117 "Insert a Unicode character at point."
119 (let* ((minibuffer-allow-text-properties t)
120 (char (ivy-read "Unicode name: "
123 (format "% -60s%c" (car x) (cdr x))
126 (insert-char (get-text-property 0 'result char))))
128 (declare-function cider-sync-request:complete "ext:cider-client")
129 (defun counsel-clj ()
130 "Clojure completion at point."
136 (cider-sync-request:complete str ":same")))))
138 (defun counsel-git ()
139 "Find file in the current Git repository."
141 (let* ((default-directory (locate-dominating-file
142 default-directory ".git"))
144 (shell-command-to-string
145 "git ls-files --full-name --")
148 (file (ivy-read "Find file: " cands)))
152 (defun counsel--generic (completion-fn)
153 "Complete thing at point with COMPLETION-FN."
154 (let* ((bnd (bounds-of-thing-at-point 'symbol))
156 (buffer-substring-no-properties
159 (candidates (funcall completion-fn str))
161 (res (ivy-read (format "pattern (%s): " str)
165 (delete-region (car bnd) (cdr bnd)))
170 ;;; counsel.el ends here