]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/counsel.el
Merge commit '4a6a31d6d4d479720f4b66091892b0cda2377346' from hydra
[gnu-emacs-elpa] / packages / swiper / counsel.el
1 ;;; consel.el --- Elisp completion at point -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; For a full copy of the GNU General Public License
20 ;; see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Just call `counsel' to start completing the `obarray'.
25 ;; The initial (optional) input is thing-at-point.
26
27 ;;; Code:
28
29 (require 'ivy)
30
31 (defun counsel ()
32 "Elisp completion at point."
33 (interactive)
34 (counsel--generic
35 (lambda (str) (all-completions str obarray))))
36
37 (defun couns-clj ()
38 "Clojure completion at point."
39 (interactive)
40 (counsel--generic
41 (lambda (str)
42 (mapcar
43 #'cl-caddr
44 (cider-sync-request:complete str ":same")))))
45
46 (defun couns-git ()
47 "Find file in the current Git repository."
48 (interactive)
49 (let* ((default-directory (locate-dominating-file
50 default-directory ".git"))
51 (cands (split-string
52 (shell-command-to-string
53 "git ls-files --full-name --")
54 "\n"
55 t))
56 (file (ivy-read "Find file: " cands)))
57 (when file
58 (find-file file))))
59
60 (defun counsel--generic (completion-fn)
61 "Complete thing at point with COMPLETION-FN."
62 (let* ((bnd (bounds-of-thing-at-point 'symbol))
63 (str (if bnd
64 (buffer-substring-no-properties
65 (car bnd) (cdr bnd))
66 ""))
67 (candidates (funcall completion-fn str))
68 (ivy-height 7)
69 (res (ivy-read (format "pattern (%s): " str)
70 candidates)))
71 (when (stringp res)
72 (when bnd
73 (delete-region (car bnd) (cdr bnd)))
74 (insert res))))
75
76 (provide 'counsel)
77
78 ;;; counsel.el ends here