]> code.delx.au - gnu-emacs-elpa/blob - packages/swiper/counsel.el
Merge commit '901db7732bf61d9809712e8adfad9f84adc2eb56' from context-coloring
[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 (file (ivy-read "Find file: " cands)))
56 (when file
57 (find-file file))))
58
59 (defun counsel--generic (completion-fn)
60 "Complete thing at point with COMPLETION-FN."
61 (let* ((bnd (bounds-of-thing-at-point 'symbol))
62 (str (if bnd
63 (buffer-substring-no-properties
64 (car bnd) (cdr bnd))
65 ""))
66 (candidates (funcall completion-fn str))
67 (ivy-height 7)
68 (res (ivy-read (format "pattern (%s): " str)
69 candidates)))
70 (when (stringp res)
71 (when bnd
72 (delete-region (car bnd) (cdr bnd)))
73 (insert res))))
74
75 (provide 'counsel)
76
77 ;;; counsel.el ends here