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
7 ;; Package-Requires: ((emacs "24.1") (swiper "0.4.0"))
8 ;; Keywords: completion, matching
10 ;; This file is part of GNU Emacs.
12 ;; This file is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; For a full copy of the GNU General Public License
23 ;; see <http://www.gnu.org/licenses/>.
27 ;; Just call one of the interactive functions in this file to complete
28 ;; the corresponding thing using `ivy'.
30 ;; Currently available: Elisp symbols, Clojure symbols, Git files.
38 (defun counsel-more-chars (n)
39 "Return two fake candidates prompting for at least N input."
41 (format "%d chars more" (- n (length ivy-text)))))
43 (defun counsel-unquote-regex-parens (str)
44 (replace-regexp-in-string
46 (replace-regexp-in-string
50 (defun counsel-directory-parent (dir)
51 "Return the directory parent of directory DIR."
52 (concat (file-name-nondirectory
53 (directory-file-name dir)) "/"))
55 (defun counsel-string-compose (prefix str)
56 "Make PREFIX the display prefix of STR though text properties."
57 (let ((str (copy-sequence str)))
60 (concat prefix (substring str 0 1))
65 (defvar counsel--async-time nil
66 "Store the time when a new process was started.
67 Or the time of the last minibuffer update.")
69 (defvar counsel--async-exit-code-plist nil
70 "Associates exit codes with reasons.")
72 (defun counsel-set-async-exit-code (cmd number str)
73 "For CMD, associate NUMBER exit code with STR."
74 (let ((plist (plist-get counsel--async-exit-code-plist cmd)))
75 (setq counsel--async-exit-code-plist
77 counsel--async-exit-code-plist
79 (plist-put plist number str)))))
81 (defvar counsel-async-split-string-re "\n"
82 "Store the regexp for splitting shell command output.")
84 (defun counsel--async-command (cmd &optional process-sentinel process-filter)
85 (let* ((counsel--process " *counsel*")
86 (proc (get-process counsel--process))
87 (buff (get-buffer counsel--process)))
89 (delete-process proc))
92 (setq proc (start-process-shell-command
96 (setq counsel--async-time (current-time))
97 (set-process-sentinel proc (or process-sentinel #'counsel--async-sentinel))
98 (set-process-filter proc (or process-filter #'counsel--async-filter))))
100 (defun counsel--async-sentinel (process event)
101 (if (string= event "finished\n")
103 (with-current-buffer (process-buffer process)
108 counsel-async-split-string-re
110 (if (null ivy--old-cands)
112 (or (ivy--preselect-index
113 (ivy-state-preselect ivy-last)
116 (let ((re (funcall ivy--regex-function ivy-text)))
119 (ivy--recompute-index
120 ivy-text re ivy--all-candidates)))
121 (setq ivy--old-cands ivy--all-candidates))
122 (if (null ivy--all-candidates)
123 (ivy--insert-minibuffer "")
125 (if (string-match "exited abnormally with code \\([0-9]+\\)\n" event)
126 (let* ((exit-code-plist (plist-get counsel--async-exit-code-plist
127 (ivy-state-caller ivy-last)))
128 (exit-num (read (match-string 1 event)))
129 (exit-code (plist-get exit-code-plist exit-num)))
130 (setq ivy--all-candidates
133 (format "error code %d" exit-num))))
134 (setq ivy--old-cands ivy--all-candidates)
137 (defun counsel--async-filter (process str)
138 "Receive from PROCESS the output STR.
139 Update the minibuffer with the amount of lines collected every
140 0.5 seconds since the last update."
141 (with-current-buffer (process-buffer process)
147 (time-since counsel--async-time))
148 (with-current-buffer (process-buffer process)
149 (goto-char (point-min))
150 (setq size (- (buffer-size) (forward-line (buffer-size))))
154 counsel-async-split-string-re
156 (let ((ivy--prompt (format
157 (concat "%d++ " (ivy-state-prompt ivy-last))
159 (ivy--insert-minibuffer
160 (ivy--format ivy--all-candidates)))
161 (setq counsel--async-time (current-time)))))
163 (defun counsel-delete-process ()
164 (let ((process (get-process " *counsel*")))
166 (delete-process process))))
168 ;;* Completion at point
172 "Elisp completion at point."
174 (let* ((bnd (unless (and (looking-at ")")
175 (eq (char-before) ?\())
176 (bounds-of-thing-at-point
179 (buffer-substring-no-properties
184 (funp (eq (char-before (car bnd)) ?\())
188 (setq ivy-completion-beg
189 (move-marker (make-marker) (car bnd)))
190 (setq ivy-completion-end
191 (move-marker (make-marker) (cdr bnd))))
192 (setq ivy-completion-beg nil)
193 (setq ivy-completion-end nil))
198 (push (symbol-name x) symbol-names))))
200 (all-completions str obarray
205 (special-form-p x)))))))
206 (ivy-read "Symbol name: " symbol-names
207 :predicate (and funp #'functionp)
209 :action #'ivy-completion-in-region-action)))
212 (declare-function slime-symbol-start-pos "ext:slime")
213 (declare-function slime-symbol-end-pos "ext:slime")
214 (declare-function slime-contextual-completions "ext:slime-c-p-c")
218 "Common Lisp completion at point."
220 (setq ivy-completion-beg (slime-symbol-start-pos))
221 (setq ivy-completion-end (slime-symbol-end-pos))
222 (ivy-read "Symbol name: "
223 (car (slime-contextual-completions
226 :action #'ivy-completion-in-region-action))
229 (declare-function deferred:sync! "ext:deferred")
230 (declare-function jedi:complete-request "ext:jedi-core")
231 (declare-function jedi:ac-direct-matches "ext:jedi")
233 (defun counsel-jedi ()
234 "Python completion at point."
236 (let ((bnd (bounds-of-thing-at-point 'symbol)))
239 (setq ivy-completion-beg (car bnd))
240 (setq ivy-completion-end (cdr bnd)))
241 (setq ivy-completion-beg nil)
242 (setq ivy-completion-end nil)))
244 (jedi:complete-request))
245 (ivy-read "Symbol name: " (jedi:ac-direct-matches)
246 :action #'counsel--py-action))
248 (defun counsel--py-action (symbol)
249 "Insert SYMBOL, erasing the previous one."
250 (when (stringp symbol)
252 (when ivy-completion-beg
256 (setq ivy-completion-beg
257 (move-marker (make-marker) (point)))
259 (setq ivy-completion-end
260 (move-marker (make-marker) (point)))
261 (when (equal (get-text-property 0 'symbol symbol) "f")
263 (setq ivy-completion-end
264 (move-marker (make-marker) (point)))
265 (backward-char 1)))))
268 (declare-function cider-sync-request:complete "ext:cider-client")
269 (defun counsel--generic (completion-fn)
270 "Complete thing at point with COMPLETION-FN."
271 (let* ((bnd (or (bounds-of-thing-at-point 'symbol)
272 (cons (point) (point))))
273 (str (buffer-substring-no-properties
274 (car bnd) (cdr bnd)))
275 (candidates (funcall completion-fn str))
277 (res (ivy-read (format "pattern (%s): " str)
281 (delete-region (car bnd) (cdr bnd)))
285 (defun counsel-clj ()
286 "Clojure completion at point."
292 (cider-sync-request:complete str ":same")))))
294 ;;** `counsel-unicode-char'
295 (defvar counsel-unicode-char-history nil
296 "History for `counsel-unicode-char'.")
299 (defun counsel-unicode-char ()
300 "Insert a Unicode character at point."
302 (let ((minibuffer-allow-text-properties t))
303 (setq ivy-completion-beg (point))
304 (setq ivy-completion-end (point))
305 (ivy-read "Unicode name: "
308 (format "% -6X% -60s%c" (cdr x) (car x) (cdr x))
311 :action (lambda (char)
313 (delete-region ivy-completion-beg ivy-completion-end)
314 (setq ivy-completion-beg (point))
315 (insert-char (get-text-property 0 'result char))
316 (setq ivy-completion-end (point))))
317 :history 'counsel-unicode-char-history)))
320 ;;** `counsel-describe-variable'
321 (defvar counsel-describe-map
322 (let ((map (make-sparse-keymap)))
323 (define-key map (kbd "C-.") #'counsel-find-symbol)
324 (define-key map (kbd "C-,") #'counsel--info-lookup-symbol)
328 'counsel-describe-variable
329 '(("i" counsel-info-lookup-symbol "info")
330 ("d" counsel--find-symbol "definition")))
332 (defvar counsel-describe-symbol-history nil
333 "History for `counsel-describe-variable' and `counsel-describe-function'.")
335 (defun counsel-find-symbol ()
336 "Jump to the definition of the current symbol."
338 (ivy-exit-with-action #'counsel--find-symbol))
340 (defun counsel--info-lookup-symbol ()
341 "Lookup the current symbol in the info docs."
343 (ivy-exit-with-action #'counsel-info-lookup-symbol))
345 (defun counsel--find-symbol (x)
346 "Find symbol definition that corresponds to string X."
349 (ring-insert find-tag-marker-ring (point-marker)))
350 (let ((full-name (get-text-property 0 'full-name x)))
352 (find-library full-name)
353 (let ((sym (read x)))
354 (cond ((and (eq (ivy-state-caller ivy-last)
355 'counsel-describe-variable)
364 (prin1-to-string sym)))
366 (prin1-to-string sym)))
368 (error "Couldn't fild definition of %s"
371 (define-obsolete-function-alias 'counsel-symbol-at-point
372 'ivy-thing-at-point "0.7.0")
374 (defun counsel-variable-list ()
375 "Return the list of all currently bound variables."
379 (when (or (get vv 'variable-documentation)
380 (and (boundp vv) (not (keywordp vv))))
381 (push (symbol-name vv) cands))))
385 (defun counsel-describe-variable ()
386 "Forward to `describe-variable'."
388 (let ((enable-recursive-minibuffers t))
390 "Describe variable: "
391 (counsel-variable-list)
392 :keymap counsel-describe-map
393 :preselect (ivy-thing-at-point)
394 :history 'counsel-describe-symbol-history
400 :caller 'counsel-describe-variable)))
402 ;;** `counsel-describe-function'
404 'counsel-describe-function
405 '(("i" counsel-info-lookup-symbol "info")
406 ("d" counsel--find-symbol "definition")))
409 (defun counsel-describe-function ()
410 "Forward to `describe-function'."
412 (let ((enable-recursive-minibuffers t))
413 (ivy-read "Describe function: "
418 (push (symbol-name x) cands))))
420 :keymap counsel-describe-map
421 :preselect (ivy-thing-at-point)
422 :history 'counsel-describe-symbol-history
428 :caller 'counsel-describe-function)))
430 ;;** `counsel-info-lookup-symbol'
431 (defvar info-lookup-mode)
432 (declare-function info-lookup->completions "info-look")
433 (declare-function info-lookup->mode-value "info-look")
434 (declare-function info-lookup-select-mode "info-look")
435 (declare-function info-lookup-change-mode "info-look")
436 (declare-function info-lookup "info-look")
439 (defun counsel-info-lookup-symbol (symbol &optional mode)
440 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
444 (let* ((topic 'symbol)
445 (mode (cond (current-prefix-arg
446 (info-lookup-change-mode topic))
447 ((info-lookup->mode-value
448 topic (info-lookup-select-mode))
450 ((info-lookup-change-mode topic))))
451 (completions (info-lookup->completions topic mode))
452 (enable-recursive-minibuffers t)
455 (mapcar #'car completions)
457 (list value info-lookup-mode))))
459 (info-lookup 'symbol symbol mode))
464 '(("d" counsel--find-symbol "definition")))
466 (ivy-set-display-transformer
468 'counsel-M-x-transformer)
470 (defun counsel-M-x-transformer (cmd)
471 "Return CMD appended with the corresponding binding in the current window."
472 (let ((binding (substitute-command-keys (format "\\[%s]" cmd))))
473 (setq binding (replace-regexp-in-string "C-x 6" "<f2>" binding))
474 (if (string-match "^M-x" binding)
477 cmd (propertize binding 'face 'font-lock-keyword-face)))))
479 (defvar smex-initialized-p)
480 (defvar smex-ido-cache)
481 (declare-function smex-initialize "ext:smex")
482 (declare-function smex-detect-new-commands "ext:smex")
483 (declare-function smex-update "ext:smex")
484 (declare-function smex-rank "ext:smex")
486 (defun counsel--M-x-prompt ()
487 "M-x plus the string representation of `current-prefix-arg'."
488 (if (not current-prefix-arg)
491 (if (eq current-prefix-arg '-)
493 (if (integerp current-prefix-arg)
494 (format "%d " current-prefix-arg)
495 (if (= (car current-prefix-arg) 4)
497 (format "%d " (car current-prefix-arg)))))
501 (defun counsel-M-x (&optional initial-input)
502 "Ivy version of `execute-extended-command'.
503 Optional INITIAL-INPUT is the initial input in the minibuffer."
505 (unless initial-input
506 (setq initial-input (cdr (assoc this-command
507 ivy-initial-inputs-alist))))
508 (let* ((cands obarray)
511 (when (require 'smex nil 'noerror)
512 (unless smex-initialized-p
514 (smex-detect-new-commands)
516 (setq cands smex-ido-cache)
519 (ivy-read (counsel--M-x-prompt) cands
522 :history 'extended-command-history
525 (when (featurep 'smex)
526 (smex-rank (intern cmd)))
527 (let ((prefix-arg current-prefix-arg)
528 (this-command (intern cmd)))
529 (command-execute (intern cmd) 'record)))
531 :keymap counsel-describe-map
532 :initial-input initial-input
533 :caller 'counsel-M-x)))
535 ;;** `counsel-load-library'
537 (defun counsel-load-library ()
538 "Load a selected the Emacs Lisp library.
539 The libraries are offered from `load-path'."
541 (let ((dirs load-path)
542 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
543 (cands (make-hash-table :test #'equal))
549 (when (file-directory-p dir)
550 (dolist (file (file-name-all-completions "" dir))
551 (when (string-match suffix file)
552 (unless (string-match "pkg.elc?$" file)
553 (setq short-name (substring file 0 (match-beginning 0)))
554 (if (setq old-val (gethash short-name cands))
556 ;; assume going up directory once will resolve name clash
557 (setq dir-parent (counsel-directory-parent (cdr old-val)))
560 (counsel-string-compose dir-parent (car old-val))
563 (setq dir-parent (counsel-directory-parent dir))
564 (puthash (concat dir-parent short-name)
567 (counsel-string-compose
568 dir-parent short-name)
569 'full-name (expand-file-name file dir))
575 'full-name (expand-file-name file dir))
577 (maphash (lambda (_k v) (push (car v) res)) cands)
578 (ivy-read "Load library: " (nreverse res)
581 (get-text-property 0 'full-name x)))
582 :keymap counsel-describe-map)))
584 ;;** `counsel-load-theme'
585 (declare-function powerline-reset "ext:powerline")
587 (defun counsel-load-theme-action (x)
588 "Disable current themes and load theme X."
591 (mapc #'disable-theme custom-enabled-themes)
592 (load-theme (intern x))
593 (when (fboundp 'powerline-reset)
595 (error "Problem loading theme %s" x)))
598 (defun counsel-load-theme ()
599 "Forward to `load-theme'.
600 Usable with `ivy-resume', `ivy-next-line-and-call' and
601 `ivy-previous-line-and-call'."
603 (ivy-read "Load custom theme: "
605 (custom-available-themes))
606 :action #'counsel-load-theme-action
607 :caller 'counsel-load-theme))
609 ;;** `counsel-descbinds'
612 '(("d" counsel-descbinds-action-find "definition")
613 ("i" counsel-descbinds-action-info "info")))
615 (defvar counsel-descbinds-history nil
616 "History for `counsel-descbinds'.")
618 (defun counsel--descbinds-cands ()
619 (let ((buffer (current-buffer))
620 (re-exclude (regexp-opt
621 '("<vertical-line>" "<bottom-divider>" "<right-divider>"
622 "<mode-line>" "<C-down-mouse-2>" "<left-fringe>"
623 "<right-fringe>" "<header-line>"
624 "<vertical-scroll-bar>" "<horizontal-scroll-bar>")))
627 (let ((indent-tabs-mode t))
628 (describe-buffer-bindings buffer))
629 (goto-char (point-min))
630 ;; Skip the "Key translations" section
631 (re-search-forward "
\f")
634 (when (looking-at "^\\([^\t\n]+\\)\t+\\(.*\\)$")
635 (let ((key (match-string 1))
636 (fun (match-string 2))
638 (unless (or (member fun '("??" "self-insert-command"))
639 (string-match re-exclude key)
640 (not (or (commandp (setq cmd (intern-soft fun)))
641 (member fun '("Prefix Command")))))
645 (propertize key 'face 'font-lock-builtin-face)
652 (defun counsel-descbinds-action-describe (x)
654 (describe-function cmd)))
656 (defun counsel-descbinds-action-find (x)
658 (counsel--find-symbol (symbol-name cmd))))
660 (defun counsel-descbinds-action-info (x)
662 (counsel-info-lookup-symbol (symbol-name cmd))))
665 (defun counsel-descbinds ()
666 "Show a list of all defined keys, and their definitions.
667 Describe the selected candidate."
669 (ivy-read "Bindings: " (counsel--descbinds-cands)
670 :action #'counsel-descbinds-action-describe
671 :history 'counsel-descbinds-history
672 :caller 'counsel-descbinds))
675 (defvar counsel--git-dir nil
676 "Store the base git directory.")
679 (defun counsel-git ()
680 "Find file in the current Git repository."
682 (setq counsel--git-dir (expand-file-name
683 (locate-dominating-file
684 default-directory ".git")))
685 (let* ((default-directory counsel--git-dir)
687 (shell-command-to-string
688 "git ls-files --full-name --")
691 (ivy-read "Find file: " cands
692 :action #'counsel-git-action)))
694 (defun counsel-git-action (x)
696 (let ((default-directory counsel--git-dir))
699 ;;** `counsel-git-grep'
700 (defvar counsel-git-grep-map
701 (let ((map (make-sparse-keymap)))
702 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
703 (define-key map (kbd "M-q") 'counsel-git-grep-query-replace)
704 (define-key map (kbd "C-c C-m") 'counsel-git-grep-switch-cmd)
707 (ivy-set-occur 'counsel-git-grep 'counsel-git-grep-occur)
708 (ivy-set-display-transformer 'counsel-git-grep 'counsel-git-grep-transformer)
710 (defvar counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S"
711 "Store the command for `counsel-git-grep'.")
713 (defvar counsel--git-grep-dir nil
714 "Store the base git directory.")
716 (defvar counsel--git-grep-count nil
717 "Store the line count in current repository.")
719 (defvar counsel-git-grep-history nil
720 "History for `counsel-git-grep'.")
722 (defvar counsel-git-grep-cmd-history
723 '("git --no-pager grep --full-name -n --no-color -i -e %S")
724 "History for `counsel-git-grep' shell commands.")
726 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
727 "Grep in the current git repository for STRING."
728 (if (and (> counsel--git-grep-count 20000)
729 (< (length string) 3))
730 (counsel-more-chars 3)
731 (let* ((default-directory counsel--git-grep-dir)
732 (cmd (format counsel-git-grep-cmd
733 (setq ivy--old-re (ivy--regex string t)))))
734 (if (<= counsel--git-grep-count 20000)
735 (split-string (shell-command-to-string cmd) "\n" t)
736 (counsel--gg-candidates (ivy--regex string))
739 (defun counsel-git-grep-action (x)
740 (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
742 (let ((file-name (match-string-no-properties 1 x))
743 (line-number (match-string-no-properties 2 x)))
744 (find-file (expand-file-name file-name counsel--git-grep-dir))
745 (goto-char (point-min))
746 (forward-line (1- (string-to-number line-number)))
747 (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
748 (unless (eq ivy-exit 'done)
750 (swiper--add-overlays (ivy--regex ivy-text)))))))
752 (defun counsel-git-grep-matcher (regexp candidates)
753 (or (and (equal regexp ivy--old-re)
760 (when (string-match "^[^:]+:[^:]+:" x)
761 (setq x (substring x (match-end 0)))
763 (string-match regexp x)
770 (string-match (car re) x)
771 (not (string-match (car re) x)))))))
774 (setq ivy--old-re regexp))))
776 (defun counsel-git-grep-transformer (str)
777 "Higlight file and line number in STR."
778 (when (string-match "\\`\\([^:]+\\):\\([^:]+\\):" str)
779 (set-text-properties (match-beginning 1)
781 '(face compilation-info)
783 (set-text-properties (match-beginning 2)
785 '(face compilation-line-number)
790 (defun counsel-git-grep (&optional cmd initial-input)
791 "Grep for a string in the current git repository.
792 When CMD is a string, use it as a \"git grep\" command.
793 When CMD is non-nil, prompt for a specific \"git grep\" command.
794 INITIAL-INPUT can be given as the initial minibuffer input."
798 (setq counsel-git-grep-cmd cmd))
800 (setq counsel-git-grep-cmd
801 (ivy-read "cmd: " counsel-git-grep-cmd-history
802 :history 'counsel-git-grep-cmd-history))
803 (setq counsel-git-grep-cmd-history
804 (delete-dups counsel-git-grep-cmd-history)))
806 (setq counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S")))
807 (setq counsel--git-grep-dir
808 (locate-dominating-file default-directory ".git"))
809 (if (null counsel--git-grep-dir)
810 (error "Not in a git repository")
811 (setq counsel--git-grep-count (counsel--gg-count "" t))
812 (ivy-read "git grep: " 'counsel-git-grep-function
813 :initial-input initial-input
814 :matcher #'counsel-git-grep-matcher
815 :dynamic-collection (> counsel--git-grep-count 20000)
816 :keymap counsel-git-grep-map
817 :action #'counsel-git-grep-action
818 :unwind #'swiper--cleanup
819 :history 'counsel-git-grep-history
820 :caller 'counsel-git-grep)))
822 (defun counsel-git-grep-switch-cmd ()
823 "Set `counsel-git-grep-cmd' to a different value."
825 (setq counsel-git-grep-cmd
826 (ivy-read "cmd: " counsel-git-grep-cmd-history
827 :history 'counsel-git-grep-cmd-history))
828 (setq counsel-git-grep-cmd-history
829 (delete-dups counsel-git-grep-cmd-history))
830 (unless (ivy-state-dynamic-collection ivy-last)
831 (setq ivy--all-candidates
832 (all-completions "" 'counsel-git-grep-function))))
834 (defvar counsel-gg-state nil
835 "The current state of candidates / count sync.")
837 (defun counsel--gg-candidates (regex)
838 "Return git grep candidates for REGEX."
839 (setq counsel-gg-state -2)
840 (counsel--gg-count regex)
841 (let* ((default-directory counsel--git-grep-dir)
842 (counsel-gg-process " *counsel-gg*")
843 (proc (get-process counsel-gg-process))
844 (buff (get-buffer counsel-gg-process)))
846 (delete-process proc))
849 (setq proc (start-process-shell-command
853 (format counsel-git-grep-cmd regex)
855 (set-process-sentinel
857 #'counsel--gg-sentinel)))
859 (defun counsel--gg-sentinel (process event)
860 (if (string= event "finished\n")
862 (with-current-buffer (process-buffer process)
863 (setq ivy--all-candidates
864 (or (split-string (buffer-string) "\n" t)
866 (setq ivy--old-cands ivy--all-candidates))
867 (when (= 0 (cl-incf counsel-gg-state))
869 (if (string= event "exited abnormally with code 1\n")
871 (setq ivy--all-candidates '("Error"))
872 (setq ivy--old-cands ivy--all-candidates)
875 (defun counsel--gg-count (regex &optional no-async)
876 "Quickly and asynchronously count the amount of git grep REGEX matches.
877 When NO-ASYNC is non-nil, do it synchronously."
878 (let ((default-directory counsel--git-grep-dir)
882 (replace-regexp-in-string
884 counsel-git-grep-cmd)
885 ;; "git grep -i -c '%s'"
886 (replace-regexp-in-string
888 (replace-regexp-in-string "'" "''" regex)))
889 " | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"))
890 (counsel-ggc-process " *counsel-gg-count*"))
892 (string-to-number (shell-command-to-string cmd))
893 (let ((proc (get-process counsel-ggc-process))
894 (buff (get-buffer counsel-ggc-process)))
896 (delete-process proc))
899 (setq proc (start-process-shell-command
903 (set-process-sentinel
905 #'(lambda (process event)
906 (when (string= event "finished\n")
907 (with-current-buffer (process-buffer process)
908 (setq ivy--full-length (string-to-number (buffer-string))))
909 (when (= 0 (cl-incf counsel-gg-state))
910 (ivy--exhibit)))))))))
912 (defun counsel-git-grep-occur ()
913 "Generate a custom occur buffer for `counsel-git-grep'."
914 (ivy-occur-grep-mode)
915 (setq default-directory counsel--git-grep-dir)
916 (let ((cands (split-string
917 (shell-command-to-string
918 (format counsel-git-grep-cmd
919 (setq ivy--old-re (ivy--regex ivy-text t))))
922 ;; Need precise number of header lines for `wgrep' to work.
923 (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
925 (insert (format "%d candidates:\n" (length cands)))
926 (ivy--occur-insert-lines
928 (lambda (cand) (concat "./" cand))
931 (defun counsel-git-grep-query-replace ()
932 "Start `query-replace' with string to replace from last search string."
934 (if (null (window-minibuffer-p))
936 "Should only be called in the minibuffer through `counsel-git-grep-map'")
937 (let* ((enable-recursive-minibuffers t)
938 (from (ivy--regex ivy-text))
939 (to (query-replace-read-to from "Query replace" t)))
940 (ivy-exit-with-action
943 (dolist (cand ivy--old-cands)
944 (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" cand)
946 (let ((file-name (match-string-no-properties 1 cand)))
947 (setq file-name (expand-file-name file-name counsel--git-grep-dir))
948 (unless (member file-name done-buffers)
949 (push file-name done-buffers)
950 (find-file file-name)
951 (goto-char (point-min)))
952 (perform-replace from to t t nil)))))))))))
954 (defun counsel-git-grep-recenter ()
957 (counsel-git-grep-action ivy--current)
958 (recenter-top-bottom)))
960 ;;** `counsel-git-stash'
961 (defun counsel-git-stash-kill-action (x)
962 (when (string-match "\\([^:]+\\):" x)
963 (kill-new (message (format "git stash apply %s" (match-string 1 x))))))
966 (defun counsel-git-stash ()
967 "Search through all available git stashes."
969 (let ((dir (locate-dominating-file default-directory ".git")))
971 (error "Not in a git repository")
972 (let ((cands (split-string (shell-command-to-string
974 for i in `git stash list --format=\"%gd\"`; do
975 git stash show -p $i | grep -H --label=\"$i\" \"$1\"
977 (ivy-read "git stash: " cands
978 :action 'counsel-git-stash-kill-action
979 :caller 'counsel-git-stash)))))
980 ;;** `counsel-git-log'
981 (defun counsel-git-log-function (input)
982 (if (< (length input) 3)
983 (counsel-more-chars 3)
984 ;; `counsel--yank-pop-format-function' uses this
985 (setq ivy--old-re (funcall ivy--regex-function input))
986 (counsel--async-command
987 ;; "git log --grep" likes to have groups quoted e.g. \(foo\).
988 ;; But it doesn't like the non-greedy ".*?".
989 (format "GIT_PAGER=cat git log --grep '%s'"
990 (replace-regexp-in-string
995 (defun counsel-git-log-action (x)
996 (message "%S" (kill-new x)))
998 (defcustom counsel-yank-pop-truncate-radius 2
999 "When non-nil, truncate the display of long strings."
1004 (defun counsel-git-log ()
1005 "Call the \"git log --grep\" shell command."
1007 (let ((counsel-async-split-string-re "\ncommit ")
1008 (counsel-yank-pop-truncate-radius 5)
1009 (ivy-format-function #'counsel--yank-pop-format-function)
1011 (ivy-read "Grep log: " #'counsel-git-log-function
1012 :dynamic-collection t
1013 :action #'counsel-git-log-action
1014 :unwind #'counsel-delete-process
1015 :caller 'counsel-git-log)))
1018 ;;** `counsel-find-file'
1019 (defvar counsel-find-file-map
1020 (let ((map (make-sparse-keymap)))
1021 (define-key map (kbd "C-DEL") 'counsel-up-directory)
1022 (define-key map (kbd "C-<backspace>") 'counsel-up-directory)
1025 (add-to-list 'ivy-ffap-url-functions 'counsel-github-url-p)
1026 (add-to-list 'ivy-ffap-url-functions 'counsel-emacs-url-p)
1028 (defcustom counsel-find-file-at-point nil
1029 "When non-nil, add file-at-point to the list of candidates."
1033 (defcustom counsel-find-file-ignore-regexp nil
1034 "A regexp of files to ignore while in `counsel-find-file'.
1035 These files are un-ignored if `ivy-text' matches them. The
1036 common way to show all files is to start `ivy-text' with a dot.
1038 Example value: \"\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)\". This will hide
1039 temporary and lock files.
1040 \\<ivy-minibuffer-map>
1041 Choosing the dotfiles option, \"\\`\\.\", might be convenient,
1042 since you can still access the dotfiles if your input starts with
1043 a dot. The generic way to toggle ignored files is \\[ivy-toggle-ignore],
1044 but the leading dot is a lot faster."
1047 (const :tag "None" nil)
1048 (const :tag "Dotfiles" "\\`\\.")
1049 (regexp :tag "Regex")))
1051 (defun counsel--find-file-matcher (regexp candidates)
1052 "Return REGEXP-matching CANDIDATES.
1053 Skip some dotfiles unless `ivy-text' requires them."
1054 (let ((res (ivy--re-filter regexp candidates)))
1055 (if (or (null ivy-use-ignore)
1056 (null counsel-find-file-ignore-regexp)
1057 (string-match "\\`\\." ivy-text))
1062 (string-match counsel-find-file-ignore-regexp x)
1063 (not (member x ivy-extra-directories))))
1067 (declare-function ffap-guesser "ffap")
1070 (defun counsel-find-file (&optional initial-input)
1071 "Forward to `find-file'.
1072 When INITIAL-INPUT is non-nil, use it in the minibuffer during completion."
1074 (ivy-read "Find file: " 'read-file-name-internal
1075 :matcher #'counsel--find-file-matcher
1076 :initial-input initial-input
1080 (find-file (expand-file-name x ivy--directory))))
1081 :preselect (when counsel-find-file-at-point
1084 :require-match 'confirm-after-completion
1085 :history 'file-name-history
1086 :keymap counsel-find-file-map))
1088 (defun counsel-up-directory ()
1089 "Go to the parent directory preselecting the current one."
1091 (let ((dir-file-name
1092 (directory-file-name (expand-file-name ivy--directory))))
1093 (ivy--cd (file-name-directory dir-file-name))
1094 (setf (ivy-state-preselect ivy-last)
1095 (file-name-as-directory (file-name-nondirectory dir-file-name)))))
1097 (defun counsel-at-git-issue-p ()
1098 "Whe point is at an issue in a Git-versioned file, return the issue string."
1099 (and (looking-at "#[0-9]+")
1101 (eq (vc-backend (buffer-file-name)) 'Git)
1102 (memq major-mode '(magit-commit-mode)))
1103 (match-string-no-properties 0)))
1105 (defun counsel-github-url-p ()
1106 "Return a Github issue URL at point."
1107 (let ((url (counsel-at-git-issue-p)))
1109 (let ((origin (shell-command-to-string
1110 "git remote get-url origin"))
1112 (cond ((string-match "\\`git@github.com:\\([^/]+\\)/\\(.*\\)\\.git$"
1114 (setq user (match-string 1 origin))
1115 (setq repo (match-string 2 origin)))
1116 ((string-match "\\`https://github.com/\\([^/]+\\)/\\(.*\\)$"
1118 (setq user (match-string 1 origin))
1119 (setq repo (match-string 2 origin))))
1121 (setq url (format "https://github.com/%s/%s/issues/%s"
1122 user repo (substring url 1))))))))
1124 (defun counsel-emacs-url-p ()
1125 "Return a Debbugs issue URL at point."
1126 (let ((url (counsel-at-git-issue-p)))
1128 (let ((origin (shell-command-to-string
1129 "git remote get-url origin")))
1130 (when (string-match "git.sv.gnu.org:/srv/git/emacs.git" origin)
1131 (format "http://debbugs.gnu.org/cgi/bugreport.cgi?bug=%s"
1132 (substring url 1)))))))
1134 ;;** `counsel-locate'
1135 (defcustom counsel-locate-options nil
1136 "Command line options for `locate`."
1138 :type '(repeat string))
1140 (make-obsolete-variable 'counsel-locate-options 'counsel-locate-cmd "0.7.0")
1142 (defcustom counsel-locate-cmd (if (eq system-type 'darwin)
1143 'counsel-locate-cmd-noregex
1144 'counsel-locate-cmd-default)
1145 "The function for producing a locate command string from the input.
1147 The function takes a string - the current input, and returns a
1148 string - the full shell command to run."
1151 (const :tag "Default" counsel-locate-cmd-default)
1152 (const :tag "No regex" counsel-locate-cmd-noregex)
1153 (const :tag "mdfind" counsel-locate-cmd-mdfind)))
1157 '(("x" counsel-locate-action-extern "xdg-open")
1158 ("d" counsel-locate-action-dired "dired")))
1160 (counsel-set-async-exit-code 'counsel-locate 1 "Nothing found")
1162 (defvar counsel-locate-history nil
1163 "History for `counsel-locate'.")
1165 (defun counsel-locate-action-extern (x)
1166 "Use xdg-open shell command on X."
1167 (call-process shell-file-name nil
1169 shell-command-switch
1171 (if (eq system-type 'darwin)
1174 (shell-quote-argument x))))
1176 (declare-function dired-jump "dired-x")
1178 (defun counsel-locate-action-dired (x)
1179 "Use `dired-jump' on X."
1182 (defun counsel-locate-cmd-default (input)
1183 "Return a shell command based on INPUT."
1184 (format "locate -i --regex '%s'"
1185 (counsel-unquote-regex-parens
1186 (ivy--regex input))))
1188 (defun counsel-locate-cmd-noregex (input)
1189 "Return a shell command based on INPUT."
1190 (format "locate -i '%s'" input))
1192 (defun counsel-locate-cmd-mdfind (input)
1193 "Return a shell command based on INPUT."
1194 (format "mdfind -name '%s'" input))
1196 (defun counsel-locate-function (input)
1197 (if (< (length input) 3)
1198 (counsel-more-chars 3)
1199 (counsel--async-command
1200 (funcall counsel-locate-cmd input))
1201 '("" "working...")))
1204 (defun counsel-locate (&optional initial-input)
1205 "Call the \"locate\" shell command.
1206 INITIAL-INPUT can be given as the initial minibuffer input."
1208 (ivy-read "Locate: " #'counsel-locate-function
1209 :initial-input initial-input
1210 :dynamic-collection t
1211 :history 'counsel-locate-history
1212 :action (lambda (file)
1216 :unwind #'counsel-delete-process
1217 :caller 'counsel-locate))
1221 (defcustom counsel-ag-base-command "ag --vimgrep %s"
1222 "Format string to use in `cousel-ag-function' to construct the
1223 command. %S will be replaced by the regex string. The default is
1224 \"ag --vimgrep %S\"."
1228 (counsel-set-async-exit-code 'counsel-ag 1 "No matches found")
1230 (defun counsel-ag-function (string)
1231 "Grep in the current directory for STRING."
1232 (if (< (length string) 3)
1233 (counsel-more-chars 3)
1234 (let ((default-directory counsel--git-grep-dir)
1235 (regex (counsel-unquote-regex-parens
1237 (ivy--regex string)))))
1238 (counsel--async-command
1239 (format counsel-ag-base-command (shell-quote-argument regex)))
1243 (defun counsel-ag (&optional initial-input initial-directory)
1244 "Grep for a string in the current directory using ag.
1245 INITIAL-INPUT can be given as the initial minibuffer input."
1247 (setq counsel--git-grep-dir (or initial-directory default-directory))
1248 (ivy-read "ag: " 'counsel-ag-function
1249 :initial-input initial-input
1250 :dynamic-collection t
1251 :history 'counsel-git-grep-history
1252 :action #'counsel-git-grep-action
1254 (counsel-delete-process)
1256 :caller 'counsel-ag))
1259 (defun counsel-grep-function (string)
1260 "Grep in the current directory for STRING."
1261 (if (< (length string) 3)
1262 (counsel-more-chars 3)
1263 (let ((regex (counsel-unquote-regex-parens
1265 (ivy--regex string)))))
1266 (counsel--async-command
1267 (format "grep -nP --ignore-case '%s' %s" regex counsel--git-grep-dir))
1270 (defun counsel-grep-action (x)
1271 (when (string-match "\\`\\([0-9]+\\):\\(.*\\)\\'" x)
1273 (let ((file-name counsel--git-grep-dir)
1274 (line-number (match-string-no-properties 1 x)))
1275 (find-file file-name)
1276 (goto-char (point-min))
1277 (forward-line (1- (string-to-number line-number)))
1278 (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
1279 (unless (eq ivy-exit 'done)
1281 (swiper--add-overlays (ivy--regex ivy-text)))))))
1284 (defun counsel-grep ()
1285 "Grep for a string in the current file."
1287 (setq counsel--git-grep-dir (buffer-file-name))
1288 (ivy-read "grep: " 'counsel-grep-function
1289 :dynamic-collection t
1290 :preselect (format "%d:%s"
1291 (line-number-at-pos)
1292 (buffer-substring-no-properties
1293 (line-beginning-position)
1294 (line-end-position)))
1295 :history 'counsel-git-grep-history
1296 :update-fn (lambda ()
1297 (counsel-grep-action ivy--current))
1298 :action #'counsel-grep-action
1300 (counsel-delete-process)
1302 :caller 'counsel-grep))
1303 ;;** `counsel-recoll'
1304 (defun counsel-recoll-function (string)
1305 "Grep in the current directory for STRING."
1306 (if (< (length string) 3)
1307 (counsel-more-chars 3)
1308 (counsel--async-command
1309 (format "recoll -t -b '%s'" string))
1312 ;; This command uses the recollq command line tool that comes together
1313 ;; with the recoll (the document indexing database) source:
1314 ;; http://www.lesbonscomptes.com/recoll/download.html
1315 ;; You need to build it yourself (together with recoll):
1316 ;; cd ./query && make && sudo cp recollq /usr/local/bin
1317 ;; You can try the GUI version of recoll with:
1318 ;; sudo apt-get install recoll
1319 ;; Unfortunately, that does not install recollq.
1320 (defun counsel-recoll (&optional initial-input)
1321 "Search for a string in the recoll database.
1322 You'll be given a list of files that match.
1323 Selecting a file will launch `swiper' for that file.
1324 INITIAL-INPUT can be given as the initial minibuffer input."
1326 (ivy-read "recoll: " 'counsel-recoll-function
1327 :initial-input initial-input
1328 :dynamic-collection t
1329 :history 'counsel-git-grep-history
1331 (when (string-match "file://\\(.*\\)\\'" x)
1332 (let ((file-name (match-string 1 x)))
1333 (find-file file-name)
1334 (unless (string-match "pdf$" x)
1335 (swiper ivy-text)))))
1336 :unwind #'counsel-delete-process
1337 :caller 'counsel-recoll))
1339 ;;** `counsel-org-tag'
1340 (defvar counsel-org-tags nil
1341 "Store the current list of tags.")
1343 (defvar org-outline-regexp)
1344 (defvar org-indent-mode)
1345 (defvar org-indent-indentation-per-level)
1346 (defvar org-tags-column)
1347 (declare-function org-get-tags-string "org")
1348 (declare-function org-move-to-column "org-compat")
1350 (defun counsel-org-change-tags (tags)
1351 (let ((current (org-get-tags-string))
1352 (col (current-column))
1354 ;; Insert new tags at the correct column
1355 (beginning-of-line 1)
1356 (setq level (or (and (looking-at org-outline-regexp)
1357 (- (match-end 0) (point) 1))
1360 ((and (equal current "") (equal tags "")))
1362 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
1368 (goto-char (match-beginning 0))
1369 (let* ((c0 (current-column))
1370 ;; compute offset for the case of org-indent-mode active
1371 (di (if (bound-and-true-p org-indent-mode)
1372 (* (1- org-indent-indentation-per-level) (1- level))
1374 (p0 (if (equal (char-before) ?*) (1+ (point)) (point)))
1375 (tc (+ org-tags-column (if (> org-tags-column 0) (- di) di)))
1376 (c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags)))))
1377 (rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
1378 (replace-match rpl t t)
1379 (and c0 indent-tabs-mode (tabify p0 (point)))
1381 (t (error "Tags alignment failed")))
1382 (org-move-to-column col)))
1384 (defun counsel-org--set-tags ()
1385 (counsel-org-change-tags
1386 (if counsel-org-tags
1388 (mapconcat #'identity counsel-org-tags ":"))
1391 (defvar org-agenda-bulk-marked-entries)
1393 (declare-function org-get-at-bol "org")
1394 (declare-function org-agenda-error "org-agenda")
1396 (defun counsel-org-tag-action (x)
1397 (if (member x counsel-org-tags)
1399 (setq counsel-org-tags (delete x counsel-org-tags)))
1400 (unless (equal x "")
1401 (setq counsel-org-tags (append counsel-org-tags (list x)))
1402 (unless (member x ivy--all-candidates)
1403 (setq ivy--all-candidates (append ivy--all-candidates (list x))))))
1404 (let ((prompt (counsel-org-tag-prompt)))
1405 (setf (ivy-state-prompt ivy-last) prompt)
1406 (setq ivy--prompt (concat "%-4d " prompt)))
1407 (cond ((memq this-command '(ivy-done
1409 ivy-immediate-done))
1410 (if (eq major-mode 'org-agenda-mode)
1411 (if (null org-agenda-bulk-marked-entries)
1412 (let ((hdmarker (or (org-get-at-bol 'org-hd-marker)
1413 (org-agenda-error))))
1414 (with-current-buffer (marker-buffer hdmarker)
1415 (goto-char hdmarker)
1416 (counsel-org--set-tags)))
1417 (let ((add-tags (copy-sequence counsel-org-tags)))
1418 (dolist (m org-agenda-bulk-marked-entries)
1419 (with-current-buffer (marker-buffer m)
1422 (setq counsel-org-tags
1424 (append (split-string (org-get-tags-string) ":" t)
1426 (counsel-org--set-tags))))))
1427 (counsel-org--set-tags)))
1428 ((eq this-command 'ivy-call)
1429 (delete-minibuffer-contents))))
1431 (defun counsel-org-tag-prompt ()
1432 (format "Tags (%s): "
1433 (mapconcat #'identity counsel-org-tags ", ")))
1435 (defvar org-setting-tags)
1436 (defvar org-last-tags-completion-table)
1437 (defvar org-tag-persistent-alist)
1438 (defvar org-tag-alist)
1439 (defvar org-complete-tags-always-offer-all-agenda-tags)
1441 (declare-function org-at-heading-p "org")
1442 (declare-function org-back-to-heading "org")
1443 (declare-function org-get-buffer-tags "org")
1444 (declare-function org-global-tags-completion-table "org")
1445 (declare-function org-agenda-files "org")
1446 (declare-function org-agenda-set-tags "org-agenda")
1449 (defun counsel-org-tag ()
1450 "Add or remove tags in org-mode."
1453 (if (eq major-mode 'org-agenda-mode)
1454 (if org-agenda-bulk-marked-entries
1455 (setq counsel-org-tags nil)
1456 (let ((hdmarker (or (org-get-at-bol 'org-hd-marker)
1457 (org-agenda-error))))
1458 (with-current-buffer (marker-buffer hdmarker)
1459 (goto-char hdmarker)
1460 (setq counsel-org-tags
1461 (split-string (org-get-tags-string) ":" t)))))
1462 (unless (org-at-heading-p)
1463 (org-back-to-heading t))
1464 (setq counsel-org-tags (split-string (org-get-tags-string) ":" t)))
1465 (let ((org-setting-tags t)
1466 (org-last-tags-completion-table
1467 (append org-tag-persistent-alist
1468 (or org-tag-alist (org-get-buffer-tags))
1470 (or org-complete-tags-always-offer-all-agenda-tags
1471 (eq major-mode 'org-agenda-mode))
1472 (org-global-tags-completion-table
1473 (org-agenda-files))))))
1474 (ivy-read (counsel-org-tag-prompt)
1475 (lambda (str &rest _unused)
1477 (all-completions str 'org-tags-completion-function)))
1478 :history 'org-tags-history
1479 :action 'counsel-org-tag-action
1480 :caller 'counsel-org-tag))))
1483 (defun counsel-org-tag-agenda ()
1484 "Set tags for the current agenda item."
1486 (let ((store (symbol-function 'org-set-tags)))
1490 (symbol-function 'counsel-org-tag))
1491 (org-agenda-set-tags nil nil))
1492 (fset 'org-set-tags store))))
1495 (defvar tmm-km-list nil)
1496 (declare-function tmm-get-keymap "tmm")
1497 (declare-function tmm--completion-table "tmm")
1498 (declare-function tmm-get-keybind "tmm")
1500 (defun counsel-tmm-prompt (menu)
1501 "Select and call an item from the MENU keymap."
1505 (setq tmm-km-list nil)
1506 (map-keymap (lambda (k v) (tmm-get-keymap (cons k v))) menu)
1507 (setq tmm-km-list (nreverse tmm-km-list))
1508 (setq out (ivy-read "Menu bar: " (tmm--completion-table tmm-km-list)
1511 (setq choice (cdr (assoc out tmm-km-list)))
1512 (setq chosen-string (car choice))
1513 (setq choice (cdr choice))
1514 (cond ((keymapp choice)
1515 (counsel-tmm-prompt choice))
1516 ((and choice chosen-string)
1517 (setq last-command-event chosen-string)
1518 (call-interactively choice)))))
1520 (defvar tmm-table-undef)
1523 (defun counsel-tmm ()
1524 "Text-mode emulation of looking and choosing from a menubar."
1527 (run-hooks 'menu-bar-update-hook)
1528 (setq tmm-table-undef nil)
1529 (counsel-tmm-prompt (tmm-get-keybind [menu-bar])))
1531 ;;** `counsel-yank-pop'
1532 (defun counsel--yank-pop-truncate (str)
1534 (let* ((lines (split-string str "\n" t))
1536 (first-match (cl-position-if
1537 (lambda (s) (string-match ivy--old-re s))
1539 (beg (max 0 (- first-match
1540 counsel-yank-pop-truncate-radius)))
1541 (end (min n (+ first-match
1542 counsel-yank-pop-truncate-radius
1544 (seq (cl-subseq lines beg end)))
1545 (if (null first-match)
1546 (error "Could not match %s" str)
1548 (setcar seq (concat "[...] " (car seq))))
1551 (concat (car (last seq)) " [...]")))
1552 (mapconcat #'identity seq "\n")))
1555 (defun counsel--yank-pop-format-function (cand-pairs)
1556 (ivy--format-function-generic
1560 (ivy--add-face s 'ivy-current-match))
1562 (counsel--yank-pop-truncate str) "\n" t)
1565 (counsel--yank-pop-truncate str))
1569 (defun counsel-yank-pop-action (s)
1570 "Insert S into the buffer, overwriting the previous yank."
1572 (delete-region ivy-completion-beg
1574 (insert (substring-no-properties s))
1575 (setq ivy-completion-end (point))))
1578 (defun counsel-yank-pop ()
1579 "Ivy replacement for `yank-pop'."
1581 (if (eq last-command 'yank)
1583 (setq ivy-completion-end (point))
1584 (setq ivy-completion-beg
1586 (search-backward (car kill-ring))
1588 (setq ivy-completion-beg (point))
1589 (setq ivy-completion-end (point)))
1590 (let ((candidates (cl-remove-if
1592 (or (< (length s) 3)
1593 (string-match "\\`[\n[:blank:]]+\\'" s)))
1594 (delete-dups kill-ring))))
1595 (let ((ivy-format-function #'counsel--yank-pop-format-function)
1597 (ivy-read "kill-ring: " candidates
1598 :action 'counsel-yank-pop-action
1599 :caller 'counsel-yank-pop))))
1601 ;;** `counsel-imenu'
1602 (defvar imenu-auto-rescan)
1603 (declare-function imenu--subalist-p "imenu")
1604 (declare-function imenu--make-index-alist "imenu")
1606 (defun counsel-imenu-get-candidates-from (alist &optional prefix)
1607 "Create a list of (key . value) from ALIST.
1608 PREFIX is used to create the key."
1609 (cl-mapcan (lambda (elm)
1610 (if (imenu--subalist-p elm)
1611 (counsel-imenu-get-candidates-from
1612 (cl-loop for (e . v) in (cdr elm) collect
1613 (cons e (if (integerp v) (copy-marker v) v)))
1614 ;; pass the prefix to next recursive call
1615 (concat prefix (if prefix ".") (car elm)))
1616 (let ((key (concat prefix (if prefix ".") (car elm))))
1618 ;; create a imenu candidate here
1619 (cons key (if (overlayp (cdr elm))
1620 (overlay-start (cdr elm))
1625 (defun counsel-imenu ()
1626 "Jump to a buffer position indexed by imenu."
1628 (unless (featurep 'imenu)
1629 (require 'imenu nil t))
1630 (let* ((imenu-auto-rescan t)
1631 (items (imenu--make-index-alist t))
1632 (items (delete (assoc "*Rescan*" items) items)))
1633 (ivy-read "imenu items:" (counsel-imenu-get-candidates-from items)
1634 :preselect (thing-at-point 'symbol)
1636 :action (lambda (candidate)
1638 ;; In org-mode, (imenu candidate) will expand child node
1639 ;; after jump to the candidate position
1641 :caller 'counsel-imenu)))
1643 ;;** `counsel-list-processes'
1644 (defun counsel-list-processes-action-delete (x)
1646 (setf (ivy-state-collection ivy-last)
1647 (setq ivy--all-candidates
1648 (delete x ivy--all-candidates))))
1650 (defun counsel-list-processes-action-switch (x)
1651 (let* ((proc (get-process x))
1652 (buf (and proc (process-buffer proc))))
1654 (switch-to-buffer x)
1655 (message "Process %s doesn't have a buffer" x))))
1658 (defun counsel-list-processes ()
1659 "Offer completion for `process-list'
1660 The default action deletes the selected process.
1661 An extra action allows to switch to the process buffer."
1663 (list-processes--refresh)
1664 (ivy-read "Process: " (mapcar #'process-name (process-list))
1668 ("o" counsel-list-processes-action-delete "kill")
1669 ("s" counsel-list-processes-action-switch "switch"))
1670 :caller 'counsel-list-processes))
1673 ;;** `counsel-rhythmbox'
1674 (defvar rhythmbox-library)
1675 (declare-function rhythmbox-load-library "ext:helm-rhythmbox")
1676 (declare-function dbus-call-method "dbus")
1677 (declare-function dbus-get-property "dbus")
1678 (declare-function rhythmbox-song-uri "ext:helm-rhythmbox")
1679 (declare-function helm-rhythmbox-candidates "ext:helm-rhythmbox")
1681 (defun counsel-rhythmbox-enqueue-song (song)
1682 "Let Rhythmbox enqueue SONG."
1683 (let ((service "org.gnome.Rhythmbox3")
1684 (path "/org/gnome/Rhythmbox3/PlayQueue")
1685 (interface "org.gnome.Rhythmbox3.PlayQueue"))
1686 (dbus-call-method :session service path interface
1687 "AddToQueue" (rhythmbox-song-uri song))))
1689 (defvar counsel-rhythmbox-history nil
1690 "History for `counsel-rhythmbox'.")
1692 (defun counsel-rhythmbox-current-song ()
1693 "Return the currently playing song title."
1695 (let* ((entry (dbus-get-property
1697 "org.mpris.MediaPlayer2.rhythmbox"
1698 "/org/mpris/MediaPlayer2"
1699 "org.mpris.MediaPlayer2.Player"
1701 (artist (caar (cadr (assoc "xesam:artist" entry))))
1702 (album (cl-caadr (assoc "xesam:album" entry)))
1703 (title (cl-caadr (assoc "xesam:title" entry))))
1704 (format "%s - %s - %s" artist album title))))
1707 (defun counsel-rhythmbox ()
1708 "Choose a song from the Rhythmbox library to play or enqueue."
1710 (unless (require 'helm-rhythmbox nil t)
1711 (error "Please install `helm-rhythmbox'"))
1712 (unless rhythmbox-library
1713 (rhythmbox-load-library)
1714 (while (null rhythmbox-library)
1716 (ivy-read "Rhythmbox: "
1717 (helm-rhythmbox-candidates)
1718 :history 'counsel-rhythmbox-history
1719 :preselect (counsel-rhythmbox-current-song)
1722 ("p" helm-rhythmbox-play-song "Play song")
1723 ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))
1724 :caller 'counsel-rhythmbox))
1727 (defvar counsel-mode-map
1728 (let ((map (make-sparse-keymap)))
1730 '((execute-extended-command . counsel-M-x)
1731 (describe-bindings . counsel-descbinds)
1732 (describe-function . counsel-describe-function)
1733 (describe-variable . counsel-describe-variable)
1734 (find-file . counsel-find-file)
1735 (imenu . counsel-imenu)
1736 (load-library . counsel-load-library)
1737 (load-theme . counsel-load-theme)
1738 (yank-pop . counsel-yank-pop)))
1739 (define-key map (vector 'remap (car binding)) (cdr binding)))
1741 "Map for `counsel-mode'. Remaps built-in functions to counsel
1745 (define-minor-mode counsel-mode
1746 "Toggle Counsel mode on or off.
1747 Turn Counsel mode on if ARG is positive, off otherwise. Counsel
1748 mode remaps built-in emacs functions that have counsel
1752 :keymap counsel-mode-map
1753 :lighter " counsel")
1757 ;;; counsel.el ends here