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 (defun counsel--async-command (cmd)
70 (let* ((counsel--process " *counsel*")
71 (proc (get-process counsel--process))
72 (buff (get-buffer counsel--process)))
74 (delete-process proc))
77 (setq proc (start-process-shell-command
81 (setq counsel--async-time (current-time))
82 (set-process-sentinel proc #'counsel--async-sentinel)
83 (set-process-filter proc #'counsel--async-filter)))
85 (defun counsel--async-sentinel (process event)
86 (if (string= event "finished\n")
88 (with-current-buffer (process-buffer process)
91 (split-string (buffer-string) "\n" t)))
92 (if (null ivy--old-cands)
94 (or (ivy--preselect-index
95 (ivy-state-preselect ivy-last)
98 (let ((re (funcall ivy--regex-function ivy-text)))
101 (ivy--recompute-index
102 ivy-text re ivy--all-candidates)))
103 (setq ivy--old-cands ivy--all-candidates))
104 (if (null ivy--all-candidates)
105 (ivy--insert-minibuffer "")
107 (if (string= event "exited abnormally with code 1\n")
109 (setq ivy--all-candidates '("Error"))
110 (setq ivy--old-cands ivy--all-candidates)
113 (defun counsel--async-filter (process str)
114 "Receive from PROCESS the output STR.
115 Update the minibuffer with the amount of lines collected every
116 0.5 seconds since the last update."
117 (with-current-buffer (process-buffer process)
123 (time-since counsel--async-time))
124 (with-current-buffer (process-buffer process)
125 (goto-char (point-min))
126 (setq size (- (buffer-size) (forward-line (buffer-size))))
128 (split-string (buffer-string) "\n" t)))
129 (let ((ivy--prompt (format
130 (concat "%d++ " (ivy-state-prompt ivy-last))
132 (ivy--insert-minibuffer
133 (ivy--format ivy--all-candidates)))
134 (setq counsel--async-time (current-time)))))
136 (defun counsel-delete-process ()
137 (let ((process (get-process " *counsel*")))
139 (delete-process process))))
141 ;;* Completion at point
145 "Elisp completion at point."
147 (let* ((bnd (unless (and (looking-at ")")
148 (eq (char-before) ?\())
149 (bounds-of-thing-at-point
152 (buffer-substring-no-properties
157 (funp (eq (char-before (car bnd)) ?\())
161 (setq ivy-completion-beg
162 (move-marker (make-marker) (car bnd)))
163 (setq ivy-completion-end
164 (move-marker (make-marker) (cdr bnd))))
165 (setq ivy-completion-beg nil)
166 (setq ivy-completion-end nil))
171 (push (symbol-name x) symbol-names))))
173 (all-completions str obarray
178 (special-form-p x)))))))
179 (ivy-read "Symbol name: " symbol-names
180 :predicate (and funp #'functionp)
182 :action #'ivy-completion-in-region-action)))
185 (declare-function slime-symbol-start-pos "ext:slime")
186 (declare-function slime-symbol-end-pos "ext:slime")
187 (declare-function slime-contextual-completions "ext:slime-c-p-c")
191 "Common Lisp completion at point."
193 (setq ivy-completion-beg (slime-symbol-start-pos))
194 (setq ivy-completion-end (slime-symbol-end-pos))
195 (ivy-read "Symbol name: "
196 (car (slime-contextual-completions
199 :action #'ivy-completion-in-region-action))
202 (declare-function deferred:sync! "ext:deferred")
203 (declare-function jedi:complete-request "ext:jedi-core")
204 (declare-function jedi:ac-direct-matches "ext:jedi")
206 (defun counsel-jedi ()
207 "Python completion at point."
209 (let ((bnd (bounds-of-thing-at-point 'symbol)))
212 (setq ivy-completion-beg (car bnd))
213 (setq ivy-completion-end (cdr bnd)))
214 (setq ivy-completion-beg nil)
215 (setq ivy-completion-end nil)))
217 (jedi:complete-request))
218 (ivy-read "Symbol name: " (jedi:ac-direct-matches)
219 :action #'counsel--py-action))
221 (defun counsel--py-action (symbol)
222 "Insert SYMBOL, erasing the previous one."
223 (when (stringp symbol)
225 (when ivy-completion-beg
229 (setq ivy-completion-beg
230 (move-marker (make-marker) (point)))
232 (setq ivy-completion-end
233 (move-marker (make-marker) (point)))
234 (when (equal (get-text-property 0 'symbol symbol) "f")
236 (setq ivy-completion-end
237 (move-marker (make-marker) (point)))
238 (backward-char 1)))))
241 (declare-function cider-sync-request:complete "ext:cider-client")
242 (defun counsel--generic (completion-fn)
243 "Complete thing at point with COMPLETION-FN."
244 (let* ((bnd (bounds-of-thing-at-point 'symbol))
246 (buffer-substring-no-properties
249 (candidates (funcall completion-fn str))
251 (res (ivy-read (format "pattern (%s): " str)
255 (delete-region (car bnd) (cdr bnd)))
259 (defun counsel-clj ()
260 "Clojure completion at point."
266 (cider-sync-request:complete str ":same")))))
268 ;;** `counsel-unicode-char'
269 (defvar counsel-unicode-char-history nil
270 "History for `counsel-unicode-char'.")
273 (defun counsel-unicode-char ()
274 "Insert a Unicode character at point."
276 (let ((minibuffer-allow-text-properties t))
277 (setq ivy-completion-beg (point))
278 (setq ivy-completion-end (point))
279 (ivy-read "Unicode name: "
282 (format "% -6X% -60s%c" (cdr x) (car x) (cdr x))
285 :action (lambda (char)
287 (delete-region ivy-completion-beg ivy-completion-end)
288 (setq ivy-completion-beg (point))
289 (insert-char (get-text-property 0 'result char))
290 (setq ivy-completion-end (point))))
291 :history 'counsel-unicode-char-history)))
294 ;;** `counsel-describe-variable'
295 (defvar counsel-describe-map
296 (let ((map (make-sparse-keymap)))
297 (define-key map (kbd "C-.") #'counsel-find-symbol)
298 (define-key map (kbd "C-,") #'counsel--info-lookup-symbol)
302 'counsel-describe-variable
303 '(("i" counsel-info-lookup-symbol "info")
304 ("d" counsel--find-symbol "definition")))
306 (defvar counsel-describe-symbol-history nil
307 "History for `counsel-describe-variable' and `counsel-describe-function'.")
309 (defun counsel-find-symbol ()
310 "Jump to the definition of the current symbol."
312 (ivy-exit-with-action #'counsel--find-symbol))
314 (defun counsel--info-lookup-symbol ()
315 "Lookup the current symbol in the info docs."
317 (ivy-exit-with-action #'counsel-info-lookup-symbol))
319 (defun counsel--find-symbol (x)
320 "Find symbol definition that corresponds to string X."
323 (ring-insert find-tag-marker-ring (point-marker)))
324 (let ((full-name (get-text-property 0 'full-name x)))
326 (find-library full-name)
327 (let ((sym (read x)))
328 (cond ((and (eq (ivy-state-caller ivy-last)
329 'counsel-describe-variable)
338 (prin1-to-string sym)))
340 (prin1-to-string sym)))
342 (error "Couldn't fild definition of %s"
345 (defun counsel-symbol-at-point ()
346 "Return current symbol at point as a string."
347 (let ((s (thing-at-point 'symbol)))
349 (if (string-match "\\`[`']?\\(.*?\\)'?\\'" s)
353 (defun counsel-variable-list ()
354 "Return the list of all currently bound variables."
358 (when (or (get vv 'variable-documentation)
359 (and (boundp vv) (not (keywordp vv))))
360 (push (symbol-name vv) cands))))
364 (defun counsel-describe-variable ()
365 "Forward to `describe-variable'."
367 (let ((enable-recursive-minibuffers t))
369 "Describe variable: "
370 (counsel-variable-list)
371 :keymap counsel-describe-map
372 :preselect (counsel-symbol-at-point)
373 :history 'counsel-describe-symbol-history
379 :caller 'counsel-describe-variable)))
381 ;;** `counsel-describe-function'
383 'counsel-describe-function
384 '(("i" counsel-info-lookup-symbol "info")
385 ("d" counsel--find-symbol "definition")))
388 (defun counsel-describe-function ()
389 "Forward to `describe-function'."
391 (let ((enable-recursive-minibuffers t))
392 (ivy-read "Describe function: "
397 (push (symbol-name x) cands))))
399 :keymap counsel-describe-map
400 :preselect (counsel-symbol-at-point)
401 :history 'counsel-describe-symbol-history
407 :caller 'counsel-describe-function)))
409 ;;** `counsel-info-lookup-symbol'
410 (defvar info-lookup-mode)
411 (declare-function info-lookup->completions "info-look")
412 (declare-function info-lookup->mode-value "info-look")
413 (declare-function info-lookup-select-mode "info-look")
414 (declare-function info-lookup-change-mode "info-look")
415 (declare-function info-lookup "info-look")
418 (defun counsel-info-lookup-symbol (symbol &optional mode)
419 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
423 (let* ((topic 'symbol)
424 (mode (cond (current-prefix-arg
425 (info-lookup-change-mode topic))
426 ((info-lookup->mode-value
427 topic (info-lookup-select-mode))
429 ((info-lookup-change-mode topic))))
430 (completions (info-lookup->completions topic mode))
431 (enable-recursive-minibuffers t)
434 (mapcar #'car completions)
436 (list value info-lookup-mode))))
438 (info-lookup 'symbol symbol mode))
443 '(("d" counsel--find-symbol "definition")))
445 (defun counsel--M-x-transformer (cand-pair)
446 "Add a binding to CAND-PAIR cdr if the car is bound in the current window.
447 CAND-PAIR is (command-name . extra-info)."
448 (let* ((command-name (car cand-pair))
449 (extra-info (cdr cand-pair))
450 (binding (substitute-command-keys (format "\\[%s]" command-name))))
451 (setq binding (replace-regexp-in-string "C-x 6" "<f2>" binding))
452 (if (string-match "^M-x" binding)
456 (format " %s (%s)" extra-info (propertize binding 'face 'font-lock-keyword-face))
457 (format " (%s)" (propertize binding 'face 'font-lock-keyword-face)))))))
459 (defvar smex-initialized-p)
460 (defvar smex-ido-cache)
461 (declare-function smex-initialize "ext:smex")
462 (declare-function smex-detect-new-commands "ext:smex")
463 (declare-function smex-update "ext:smex")
464 (declare-function smex-rank "ext:smex")
466 (defun counsel--M-x-prompt ()
467 "M-x plus the string representation of `current-prefix-arg'."
468 (if (not current-prefix-arg)
471 (if (eq current-prefix-arg '-)
473 (if (integerp current-prefix-arg)
474 (format "%d " current-prefix-arg)
475 (if (= (car current-prefix-arg) 4)
477 (format "%d " (car current-prefix-arg)))))
481 (defun counsel-M-x (&optional initial-input)
482 "Ivy version of `execute-extended-command'.
483 Optional INITIAL-INPUT is the initial input in the minibuffer."
485 (unless initial-input
486 (setq initial-input (cdr (assoc this-command
487 ivy-initial-inputs-alist))))
488 (let* ((store ivy-format-function)
494 (mapcar #'counsel--M-x-transformer cand-pairs)))))
498 (when (require 'smex nil 'noerror)
499 (unless smex-initialized-p
501 (smex-detect-new-commands)
503 (setq cands smex-ido-cache)
506 (ivy-read (counsel--M-x-prompt) cands
509 :history 'extended-command-history
512 (when (featurep 'smex)
513 (smex-rank (intern cmd)))
514 (let ((prefix-arg current-prefix-arg)
515 (ivy-format-function store)
516 (this-command (intern cmd)))
517 (command-execute (intern cmd) 'record)))
519 :keymap counsel-describe-map
520 :initial-input initial-input
521 :caller 'counsel-M-x)))
523 ;;** `counsel-load-library'
525 (defun counsel-load-library ()
526 "Load a selected the Emacs Lisp library.
527 The libraries are offered from `load-path'."
529 (let ((dirs load-path)
530 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
531 (cands (make-hash-table :test #'equal))
537 (when (file-directory-p dir)
538 (dolist (file (file-name-all-completions "" dir))
539 (when (string-match suffix file)
540 (unless (string-match "pkg.elc?$" file)
541 (setq short-name (substring file 0 (match-beginning 0)))
542 (if (setq old-val (gethash short-name cands))
544 ;; assume going up directory once will resolve name clash
545 (setq dir-parent (counsel-directory-parent (cdr old-val)))
548 (counsel-string-compose dir-parent (car old-val))
551 (setq dir-parent (counsel-directory-parent dir))
552 (puthash (concat dir-parent short-name)
555 (counsel-string-compose
556 dir-parent short-name)
557 'full-name (expand-file-name file dir))
563 'full-name (expand-file-name file dir))
565 (maphash (lambda (_k v) (push (car v) res)) cands)
566 (ivy-read "Load library: " (nreverse res)
569 (get-text-property 0 'full-name x)))
570 :keymap counsel-describe-map)))
572 ;;** `counsel-load-theme'
573 (declare-function powerline-reset "ext:powerline")
575 (defun counsel-load-theme-action (x)
576 "Disable current themes and load theme X."
579 (mapc #'disable-theme custom-enabled-themes)
580 (load-theme (intern x))
581 (when (fboundp 'powerline-reset)
583 (error "Problem loading theme %s" x)))
586 (defun counsel-load-theme ()
587 "Forward to `load-theme'.
588 Usable with `ivy-resume', `ivy-next-line-and-call' and
589 `ivy-previous-line-and-call'."
591 (ivy-read "Load custom theme: "
593 (custom-available-themes))
594 :action #'counsel-load-theme-action
595 :caller 'counsel-load-theme))
597 ;;** `counsel-descbinds'
600 '(("d" counsel-descbinds-action-find "definition")
601 ("i" counsel-descbinds-action-info "info")))
603 (defvar counsel-descbinds-history nil
604 "History for `counsel-descbinds'.")
606 (defun counsel--descbinds-cands ()
607 (let ((buffer (current-buffer))
608 (re-exclude (regexp-opt
609 '("<vertical-line>" "<bottom-divider>" "<right-divider>"
610 "<mode-line>" "<C-down-mouse-2>" "<left-fringe>"
611 "<right-fringe>" "<header-line>"
612 "<vertical-scroll-bar>" "<horizontal-scroll-bar>")))
615 (let ((indent-tabs-mode t))
616 (describe-buffer-bindings buffer))
617 (goto-char (point-min))
618 ;; Skip the "Key translations" section
619 (re-search-forward "
\f")
622 (when (looking-at "^\\([^\t\n]+\\)\t+\\(.*\\)$")
623 (let ((key (match-string 1))
624 (fun (match-string 2))
626 (unless (or (member fun '("??" "self-insert-command"))
627 (string-match re-exclude key)
628 (not (or (commandp (setq cmd (intern-soft fun)))
629 (member fun '("Prefix Command")))))
633 (propertize key 'face 'font-lock-builtin-face)
640 (defun counsel-descbinds-action-describe (x)
642 (describe-function cmd)))
644 (defun counsel-descbinds-action-find (x)
646 (counsel--find-symbol (symbol-name cmd))))
648 (defun counsel-descbinds-action-info (x)
650 (counsel-info-lookup-symbol (symbol-name cmd))))
653 (defun counsel-descbinds ()
654 "Show a list of all defined keys, and their definitions.
655 Describe the selected candidate."
657 (ivy-read "Bindings: " (counsel--descbinds-cands)
658 :action #'counsel-descbinds-action-describe
659 :history 'counsel-descbinds-history
660 :caller 'counsel-descbinds))
663 (defvar counsel--git-dir nil
664 "Store the base git directory.")
667 (defun counsel-git ()
668 "Find file in the current Git repository."
670 (setq counsel--git-dir (expand-file-name
671 (locate-dominating-file
672 default-directory ".git")))
673 (let* ((default-directory counsel--git-dir)
675 (shell-command-to-string
676 "git ls-files --full-name --")
679 (ivy-read "Find file: " cands
680 :action #'counsel-git-action)))
682 (defun counsel-git-action (x)
684 (let ((default-directory counsel--git-dir))
687 ;;** `counsel-git-grep'
688 (defvar counsel-git-grep-map
689 (let ((map (make-sparse-keymap)))
690 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
691 (define-key map (kbd "M-q") 'counsel-git-grep-query-replace)
694 (ivy-set-occur 'counsel-git-grep 'counsel-git-grep-occur)
696 (defvar counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S"
697 "Store the command for `counsel-git-grep'.")
699 (defvar counsel--git-grep-dir nil
700 "Store the base git directory.")
702 (defvar counsel--git-grep-count nil
703 "Store the line count in current repository.")
705 (defvar counsel-git-grep-history nil
706 "History for `counsel-git-grep'.")
708 (defvar counsel-git-grep-cmd-history
709 '("git --no-pager grep --full-name -n --no-color -i -e %S")
710 "History for `counsel-git-grep' shell commands.")
712 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
713 "Grep in the current git repository for STRING."
714 (if (and (> counsel--git-grep-count 20000)
715 (< (length string) 3))
716 (counsel-more-chars 3)
717 (let* ((default-directory counsel--git-grep-dir)
718 (cmd (format counsel-git-grep-cmd
719 (setq ivy--old-re (ivy--regex string t)))))
720 (if (<= counsel--git-grep-count 20000)
721 (split-string (shell-command-to-string cmd) "\n" t)
722 (counsel--gg-candidates (ivy--regex string))
725 (defun counsel-git-grep-action (x)
726 (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
728 (let ((file-name (match-string-no-properties 1 x))
729 (line-number (match-string-no-properties 2 x)))
730 (find-file (expand-file-name file-name counsel--git-grep-dir))
731 (goto-char (point-min))
732 (forward-line (1- (string-to-number line-number)))
733 (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
734 (unless (eq ivy-exit 'done)
736 (swiper--add-overlays (ivy--regex ivy-text)))))))
738 (defun counsel-git-grep-matcher (regexp candidates)
739 (or (and (equal regexp ivy--old-re)
746 (when (string-match "^[^:]+:[^:]+:" x)
747 (setq x (substring x (match-end 0)))
749 (string-match regexp x)
756 (string-match (car re) x)
757 (not (string-match (car re) x)))))))
760 (setq ivy--old-re regexp))))
763 (defun counsel-git-grep (&optional cmd initial-input)
764 "Grep for a string in the current git repository.
765 When CMD is a string, use it as a \"git grep\" command.
766 When CMD is non-nil, prompt for a specific \"git grep\" command.
767 INITIAL-INPUT can be given as the initial minibuffer input."
771 (setq counsel-git-grep-cmd cmd))
773 (setq counsel-git-grep-cmd
774 (ivy-read "cmd: " counsel-git-grep-cmd-history
775 :history 'counsel-git-grep-cmd-history))
776 (setq counsel-git-grep-cmd-history
777 (delete-dups counsel-git-grep-cmd-history)))
779 (setq counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S")))
780 (setq counsel--git-grep-dir
781 (locate-dominating-file default-directory ".git"))
782 (if (null counsel--git-grep-dir)
783 (error "Not in a git repository")
784 (setq counsel--git-grep-count (counsel--gg-count "" t))
785 (ivy-read "git grep: " 'counsel-git-grep-function
786 :initial-input initial-input
787 :matcher #'counsel-git-grep-matcher
788 :dynamic-collection (> counsel--git-grep-count 20000)
789 :keymap counsel-git-grep-map
790 :action #'counsel-git-grep-action
791 :unwind #'swiper--cleanup
792 :history 'counsel-git-grep-history
793 :caller 'counsel-git-grep)))
795 (defvar counsel-gg-state nil
796 "The current state of candidates / count sync.")
798 (defun counsel--gg-candidates (regex)
799 "Return git grep candidates for REGEX."
800 (setq counsel-gg-state -2)
801 (counsel--gg-count regex)
802 (let* ((default-directory counsel--git-grep-dir)
803 (counsel-gg-process " *counsel-gg*")
804 (proc (get-process counsel-gg-process))
805 (buff (get-buffer counsel-gg-process)))
807 (delete-process proc))
810 (setq proc (start-process-shell-command
814 (format counsel-git-grep-cmd regex)
816 (set-process-sentinel
818 #'counsel--gg-sentinel)))
820 (defun counsel--gg-sentinel (process event)
821 (if (string= event "finished\n")
823 (with-current-buffer (process-buffer process)
824 (setq ivy--all-candidates
825 (or (split-string (buffer-string) "\n" t)
827 (setq ivy--old-cands ivy--all-candidates))
828 (when (= 0 (cl-incf counsel-gg-state))
830 (if (string= event "exited abnormally with code 1\n")
832 (setq ivy--all-candidates '("Error"))
833 (setq ivy--old-cands ivy--all-candidates)
836 (defun counsel--gg-count (regex &optional no-async)
837 "Quickly and asynchronously count the amount of git grep REGEX matches.
838 When NO-ASYNC is non-nil, do it synchronously."
839 (let ((default-directory counsel--git-grep-dir)
843 (replace-regexp-in-string
845 counsel-git-grep-cmd)
846 ;; "git grep -i -c '%s'"
847 (replace-regexp-in-string
849 (replace-regexp-in-string "'" "''" regex)))
850 " | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"))
851 (counsel-ggc-process " *counsel-gg-count*"))
853 (string-to-number (shell-command-to-string cmd))
854 (let ((proc (get-process counsel-ggc-process))
855 (buff (get-buffer counsel-ggc-process)))
857 (delete-process proc))
860 (setq proc (start-process-shell-command
864 (set-process-sentinel
866 #'(lambda (process event)
867 (when (string= event "finished\n")
868 (with-current-buffer (process-buffer process)
869 (setq ivy--full-length (string-to-number (buffer-string))))
870 (when (= 0 (cl-incf counsel-gg-state))
871 (ivy--exhibit)))))))))
873 (defun counsel-git-grep-occur ()
874 "Generate a custom occur buffer for `counsel-git-grep'."
875 (ivy-occur-grep-mode)
876 (setq default-directory counsel--git-grep-dir)
877 (let ((cands (split-string
878 (shell-command-to-string
879 (format counsel-git-grep-cmd
880 (if (stringp ivy--old-re)
882 (caar ivy--old-re))))
885 ;; Need precise number of header lines for `wgrep' to work.
886 (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
888 (insert (format "%d candidates:\n" (length cands)))
889 (ivy--occur-insert-lines
891 (lambda (cand) (concat "./" cand))
894 (defun counsel-git-grep-query-replace ()
895 "Start `query-replace' with string to replace from last search string."
897 (if (null (window-minibuffer-p))
899 "Should only be called in the minibuffer through `counsel-git-grep-map'")
900 (let* ((enable-recursive-minibuffers t)
901 (from (ivy--regex ivy-text))
902 (to (query-replace-read-to from "Query replace" t)))
903 (ivy-exit-with-action
906 (dolist (cand ivy--old-cands)
907 (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" cand)
909 (let ((file-name (match-string-no-properties 1 cand)))
910 (setq file-name (expand-file-name file-name counsel--git-grep-dir))
911 (unless (member file-name done-buffers)
912 (push file-name done-buffers)
913 (find-file file-name)
914 (goto-char (point-min)))
915 (perform-replace from to t t nil)))))))))))
917 (defun counsel-git-grep-recenter ()
920 (counsel-git-grep-action ivy--current)
921 (recenter-top-bottom)))
923 ;;** `counsel-git-stash'
924 (defun counsel-git-stash-kill-action (x)
925 (when (string-match "\\([^:]+\\):" x)
926 (kill-new (message (format "git stash apply %s" (match-string 1 x))))))
929 (defun counsel-git-stash ()
930 "Search through all available git stashes."
932 (let ((dir (locate-dominating-file default-directory ".git")))
934 (error "Not in a git repository")
935 (let ((cands (split-string (shell-command-to-string
937 for i in `git stash list --format=\"%gd\"`; do
938 git stash show -p $i | grep -H --label=\"$i\" \"$1\"
940 (ivy-read "git stash: " cands
941 :action 'counsel-git-stash-kill-action
942 :caller 'counsel-git-stash)))))
944 ;;** `counsel-find-file'
945 (defvar counsel-find-file-map
946 (let ((map (make-sparse-keymap)))
947 (define-key map (kbd "C-DEL") 'counsel-up-directory)
948 (define-key map (kbd "C-<backspace>") 'counsel-up-directory)
951 (add-to-list 'ivy-ffap-url-functions 'counsel-github-url-p)
952 (add-to-list 'ivy-ffap-url-functions 'counsel-emacs-url-p)
954 (defcustom counsel-find-file-at-point nil
955 "When non-nil, add file-at-point to the list of candidates."
959 (defcustom counsel-find-file-ignore-regexp nil
960 "A regexp of files to ignore while in `counsel-find-file'.
961 These files are un-ignored if `ivy-text' matches them.
962 The common way to show all files is to start `ivy-text' with a dot.
963 Possible value: \"\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)\"."
966 (const :tag "None" nil)
967 (regexp :tag "Regex")))
969 (defun counsel--find-file-matcher (regexp candidates)
970 "Return REGEXP-matching CANDIDATES.
971 Skip some dotfiles unless `ivy-text' requires them."
972 (let ((res (ivy--re-filter regexp candidates)))
973 (if (or (null ivy-use-ignore)
974 (null counsel-find-file-ignore-regexp)
975 (string-match counsel-find-file-ignore-regexp ivy-text))
979 (string-match counsel-find-file-ignore-regexp x))
983 (declare-function ffap-guesser "ffap")
986 (defun counsel-find-file (&optional initial-input)
987 "Forward to `find-file'.
988 When INITIAL-INPUT is non-nil, use it in the minibuffer during completion."
990 (ivy-read "Find file: " 'read-file-name-internal
991 :matcher #'counsel--find-file-matcher
992 :initial-input initial-input
996 (find-file (expand-file-name x ivy--directory))))
997 :preselect (when counsel-find-file-at-point
1000 :require-match 'confirm-after-completion
1001 :history 'file-name-history
1002 :keymap counsel-find-file-map))
1004 (defun counsel-up-directory ()
1005 "Go to the parent directory preselecting the current one."
1007 (let ((dir-file-name
1008 (directory-file-name (expand-file-name ivy--directory))))
1009 (ivy--cd (file-name-directory dir-file-name))
1010 (setf (ivy-state-preselect ivy-last)
1011 (file-name-as-directory (file-name-nondirectory dir-file-name)))))
1013 (defun counsel-at-git-issue-p ()
1014 "Whe point is at an issue in a Git-versioned file, return the issue string."
1015 (and (looking-at "#[0-9]+")
1017 (eq (vc-backend (buffer-file-name)) 'Git)
1018 (memq major-mode '(magit-commit-mode)))
1019 (match-string-no-properties 0)))
1021 (defun counsel-github-url-p ()
1022 "Return a Github issue URL at point."
1023 (let ((url (counsel-at-git-issue-p)))
1025 (let ((origin (shell-command-to-string
1026 "git remote get-url origin"))
1028 (cond ((string-match "\\`git@github.com:\\([^/]+\\)/\\(.*\\)\\.git$"
1030 (setq user (match-string 1 origin))
1031 (setq repo (match-string 2 origin)))
1032 ((string-match "\\`https://github.com/\\([^/]+\\)/\\(.*\\)$"
1034 (setq user (match-string 1 origin))
1035 (setq repo (match-string 2 origin))))
1037 (setq url (format "https://github.com/%s/%s/issues/%s"
1038 user repo (substring url 1))))))))
1040 (defun counsel-emacs-url-p ()
1041 "Return a Debbugs issue URL at point."
1042 (let ((url (counsel-at-git-issue-p)))
1044 (let ((origin (shell-command-to-string
1045 "git remote get-url origin")))
1046 (when (string-match "git.sv.gnu.org:/srv/git/emacs.git" origin)
1047 (format "http://debbugs.gnu.org/cgi/bugreport.cgi?bug=%s"
1048 (substring url 1)))))))
1050 ;;** `counsel-locate'
1051 (defcustom counsel-locate-options nil
1052 "Command line options for `locate`."
1054 :type '(repeat string))
1056 (make-obsolete-variable 'counsel-locate-options 'counsel-locate-cmd "0.7.0")
1058 (defcustom counsel-locate-cmd (if (eq system-type 'darwin)
1059 'counsel-locate-cmd-noregex
1060 'counsel-locate-cmd-default)
1061 "The function for producing a locate command string from the input.
1063 The function takes a string - the current input, and returns a
1064 string - the full shell command to run."
1067 (const :tag "Default" counsel-locate-cmd-default)
1068 (const :tag "No regex" counsel-locate-cmd-noregex)
1069 (const :tag "mdfind" counsel-locate-cmd-mdfind)))
1073 '(("x" counsel-locate-action-extern "xdg-open")
1074 ("d" counsel-locate-action-dired "dired")))
1076 (defvar counsel-locate-history nil
1077 "History for `counsel-locate'.")
1079 (defun counsel-locate-action-extern (x)
1080 "Use xdg-open shell command on X."
1081 (call-process shell-file-name nil
1083 shell-command-switch
1085 (if (eq system-type 'darwin)
1088 (shell-quote-argument x))))
1090 (declare-function dired-jump "dired-x")
1092 (defun counsel-locate-action-dired (x)
1093 "Use `dired-jump' on X."
1096 (defun counsel-locate-cmd-default (input)
1097 "Return a shell command based on INPUT."
1098 (format "locate -i --regex '%s'"
1099 (counsel-unquote-regex-parens
1100 (ivy--regex input))))
1102 (defun counsel-locate-cmd-noregex (input)
1103 "Return a shell command based on INPUT."
1104 (format "locate -i '%s'" input))
1106 (defun counsel-locate-cmd-mdfind (input)
1107 "Return a shell command based on INPUT."
1108 (format "mdfind -name '%s'" input))
1110 (defun counsel-locate-function (input)
1111 (if (< (length input) 3)
1112 (counsel-more-chars 3)
1113 (counsel--async-command
1114 (funcall counsel-locate-cmd input))
1115 '("" "working...")))
1118 (defun counsel-locate (&optional initial-input)
1119 "Call the \"locate\" shell command.
1120 INITIAL-INPUT can be given as the initial minibuffer input."
1122 (ivy-read "Locate: " #'counsel-locate-function
1123 :initial-input initial-input
1124 :dynamic-collection t
1125 :history 'counsel-locate-history
1126 :action (lambda (file)
1130 :unwind #'counsel-delete-process
1131 :caller 'counsel-locate))
1135 (defcustom counsel-ag-base-command "ag --vimgrep %S"
1136 "Format string to use in `cousel-ag-function' to construct the
1137 command. %S will be replaced by the regex string. The default is
1138 \"ag --vimgrep %S\"."
1142 (defun counsel-ag-function (string)
1143 "Grep in the current directory for STRING."
1144 (if (< (length string) 3)
1145 (counsel-more-chars 3)
1146 (let ((default-directory counsel--git-grep-dir)
1147 (regex (counsel-unquote-regex-parens
1149 (ivy--regex string)))))
1150 (counsel--async-command
1151 (format counsel-ag-base-command regex))
1155 (defun counsel-ag (&optional initial-input initial-directory)
1156 "Grep for a string in the current directory using ag.
1157 INITIAL-INPUT can be given as the initial minibuffer input."
1159 (setq counsel--git-grep-dir (or initial-directory default-directory))
1160 (ivy-read "ag: " 'counsel-ag-function
1161 :initial-input initial-input
1162 :dynamic-collection t
1163 :history 'counsel-git-grep-history
1164 :action #'counsel-git-grep-action
1166 (counsel-delete-process)
1168 :caller 'counsel-ag))
1171 (defun counsel-grep-function (string)
1172 "Grep in the current directory for STRING."
1173 (if (< (length string) 3)
1174 (counsel-more-chars 3)
1175 (let ((regex (counsel-unquote-regex-parens
1177 (ivy--regex string)))))
1178 (counsel--async-command
1179 (format "grep -nP --ignore-case '%s' %s" regex counsel--git-grep-dir))
1182 (defun counsel-grep-action (x)
1183 (when (string-match "\\`\\([0-9]+\\):\\(.*\\)\\'" x)
1185 (let ((file-name counsel--git-grep-dir)
1186 (line-number (match-string-no-properties 1 x)))
1187 (find-file file-name)
1188 (goto-char (point-min))
1189 (forward-line (1- (string-to-number line-number)))
1190 (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
1191 (unless (eq ivy-exit 'done)
1193 (swiper--add-overlays (ivy--regex ivy-text)))))))
1196 (defun counsel-grep ()
1197 "Grep for a string in the current file."
1199 (setq counsel--git-grep-dir (buffer-file-name))
1200 (ivy-read "grep: " 'counsel-grep-function
1201 :dynamic-collection t
1202 :preselect (format "%d:%s"
1203 (line-number-at-pos)
1204 (buffer-substring-no-properties
1205 (line-beginning-position)
1206 (line-end-position)))
1207 :history 'counsel-git-grep-history
1208 :update-fn (lambda ()
1209 (counsel-grep-action ivy--current))
1210 :action #'counsel-grep-action
1212 (counsel-delete-process)
1214 :caller 'counsel-grep))
1215 ;;** `counsel-recoll'
1216 (defun counsel-recoll-function (string)
1217 "Grep in the current directory for STRING."
1218 (if (< (length string) 3)
1219 (counsel-more-chars 3)
1220 (counsel--async-command
1221 (format "recoll -t -b '%s'" string))
1224 ;; This command uses the recollq command line tool that comes together
1225 ;; with the recoll (the document indexing database) source:
1226 ;; http://www.lesbonscomptes.com/recoll/download.html
1227 ;; You need to build it yourself (together with recoll):
1228 ;; cd ./query && make && sudo cp recollq /usr/local/bin
1229 ;; You can try the GUI version of recoll with:
1230 ;; sudo apt-get install recoll
1231 ;; Unfortunately, that does not install recollq.
1232 (defun counsel-recoll (&optional initial-input)
1233 "Search for a string in the recoll database.
1234 You'll be given a list of files that match.
1235 Selecting a file will launch `swiper' for that file.
1236 INITIAL-INPUT can be given as the initial minibuffer input."
1238 (ivy-read "recoll: " 'counsel-recoll-function
1239 :initial-input initial-input
1240 :dynamic-collection t
1241 :history 'counsel-git-grep-history
1243 (when (string-match "file://\\(.*\\)\\'" x)
1244 (let ((file-name (match-string 1 x)))
1245 (find-file file-name)
1246 (unless (string-match "pdf$" x)
1247 (swiper ivy-text)))))
1248 :caller 'counsel-recoll))
1250 ;;** `counsel-org-tag'
1251 (defvar counsel-org-tags nil
1252 "Store the current list of tags.")
1254 (defvar org-outline-regexp)
1255 (defvar org-indent-mode)
1256 (defvar org-indent-indentation-per-level)
1257 (defvar org-tags-column)
1258 (declare-function org-get-tags-string "org")
1259 (declare-function org-move-to-column "org-compat")
1261 (defun counsel-org-change-tags (tags)
1262 (let ((current (org-get-tags-string))
1263 (col (current-column))
1265 ;; Insert new tags at the correct column
1266 (beginning-of-line 1)
1267 (setq level (or (and (looking-at org-outline-regexp)
1268 (- (match-end 0) (point) 1))
1271 ((and (equal current "") (equal tags "")))
1273 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
1279 (goto-char (match-beginning 0))
1280 (let* ((c0 (current-column))
1281 ;; compute offset for the case of org-indent-mode active
1282 (di (if (bound-and-true-p org-indent-mode)
1283 (* (1- org-indent-indentation-per-level) (1- level))
1285 (p0 (if (equal (char-before) ?*) (1+ (point)) (point)))
1286 (tc (+ org-tags-column (if (> org-tags-column 0) (- di) di)))
1287 (c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags)))))
1288 (rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
1289 (replace-match rpl t t)
1290 (and c0 indent-tabs-mode (tabify p0 (point)))
1292 (t (error "Tags alignment failed")))
1293 (org-move-to-column col)))
1295 (defun counsel-org--set-tags ()
1296 (counsel-org-change-tags
1297 (if counsel-org-tags
1299 (mapconcat #'identity counsel-org-tags ":"))
1302 (defvar org-agenda-bulk-marked-entries)
1304 (declare-function org-get-at-bol "org")
1305 (declare-function org-agenda-error "org-agenda")
1307 (defun counsel-org-tag-action (x)
1308 (if (member x counsel-org-tags)
1310 (setq counsel-org-tags (delete x counsel-org-tags)))
1311 (unless (equal x "")
1312 (setq counsel-org-tags (append counsel-org-tags (list x)))
1313 (unless (member x ivy--all-candidates)
1314 (setq ivy--all-candidates (append ivy--all-candidates (list x))))))
1315 (let ((prompt (counsel-org-tag-prompt)))
1316 (setf (ivy-state-prompt ivy-last) prompt)
1317 (setq ivy--prompt (concat "%-4d " prompt)))
1318 (cond ((memq this-command '(ivy-done
1320 ivy-immediate-done))
1321 (if (eq major-mode 'org-agenda-mode)
1322 (if (null org-agenda-bulk-marked-entries)
1323 (let ((hdmarker (or (org-get-at-bol 'org-hd-marker)
1324 (org-agenda-error))))
1325 (with-current-buffer (marker-buffer hdmarker)
1326 (goto-char hdmarker)
1327 (counsel-org--set-tags)))
1328 (let ((add-tags (copy-sequence counsel-org-tags)))
1329 (dolist (m org-agenda-bulk-marked-entries)
1330 (with-current-buffer (marker-buffer m)
1333 (setq counsel-org-tags
1335 (append (split-string (org-get-tags-string) ":" t)
1337 (counsel-org--set-tags))))))
1338 (counsel-org--set-tags)))
1339 ((eq this-command 'ivy-call)
1340 (delete-minibuffer-contents))))
1342 (defun counsel-org-tag-prompt ()
1343 (format "Tags (%s): "
1344 (mapconcat #'identity counsel-org-tags ", ")))
1346 (defvar org-setting-tags)
1347 (defvar org-last-tags-completion-table)
1348 (defvar org-tag-persistent-alist)
1349 (defvar org-tag-alist)
1350 (defvar org-complete-tags-always-offer-all-agenda-tags)
1352 (declare-function org-at-heading-p "org")
1353 (declare-function org-back-to-heading "org")
1354 (declare-function org-get-buffer-tags "org")
1355 (declare-function org-global-tags-completion-table "org")
1356 (declare-function org-agenda-files "org")
1357 (declare-function org-agenda-set-tags "org-agenda")
1360 (defun counsel-org-tag ()
1361 "Add or remove tags in org-mode."
1364 (if (eq major-mode 'org-agenda-mode)
1365 (if org-agenda-bulk-marked-entries
1366 (setq counsel-org-tags nil)
1367 (let ((hdmarker (or (org-get-at-bol 'org-hd-marker)
1368 (org-agenda-error))))
1369 (with-current-buffer (marker-buffer hdmarker)
1370 (goto-char hdmarker)
1371 (setq counsel-org-tags
1372 (split-string (org-get-tags-string) ":" t)))))
1373 (unless (org-at-heading-p)
1374 (org-back-to-heading t))
1375 (setq counsel-org-tags (split-string (org-get-tags-string) ":" t)))
1376 (let ((org-setting-tags t)
1377 (org-last-tags-completion-table
1378 (append org-tag-persistent-alist
1379 (or org-tag-alist (org-get-buffer-tags))
1381 (or org-complete-tags-always-offer-all-agenda-tags
1382 (eq major-mode 'org-agenda-mode))
1383 (org-global-tags-completion-table
1384 (org-agenda-files))))))
1385 (ivy-read (counsel-org-tag-prompt)
1386 (lambda (str &rest _unused)
1388 (all-completions str 'org-tags-completion-function)))
1389 :history 'org-tags-history
1390 :action 'counsel-org-tag-action
1391 :caller 'counsel-org-tag))))
1394 (defun counsel-org-tag-agenda ()
1395 "Set tags for the current agenda item."
1397 (let ((store (symbol-function 'org-set-tags)))
1401 (symbol-function 'counsel-org-tag))
1402 (org-agenda-set-tags nil nil))
1403 (fset 'org-set-tags store))))
1406 (defvar tmm-km-list nil)
1407 (declare-function tmm-get-keymap "tmm")
1408 (declare-function tmm--completion-table "tmm")
1409 (declare-function tmm-get-keybind "tmm")
1411 (defun counsel-tmm-prompt (menu)
1412 "Select and call an item from the MENU keymap."
1416 (setq tmm-km-list nil)
1417 (map-keymap (lambda (k v) (tmm-get-keymap (cons k v))) menu)
1418 (setq tmm-km-list (nreverse tmm-km-list))
1419 (setq out (ivy-read "Menu bar: " (tmm--completion-table tmm-km-list)
1422 (setq choice (cdr (assoc out tmm-km-list)))
1423 (setq chosen-string (car choice))
1424 (setq choice (cdr choice))
1425 (cond ((keymapp choice)
1426 (counsel-tmm-prompt choice))
1427 ((and choice chosen-string)
1428 (setq last-command-event chosen-string)
1429 (call-interactively choice)))))
1431 (defvar tmm-table-undef)
1434 (defun counsel-tmm ()
1435 "Text-mode emulation of looking and choosing from a menubar."
1438 (run-hooks 'menu-bar-update-hook)
1439 (setq tmm-table-undef nil)
1440 (counsel-tmm-prompt (tmm-get-keybind [menu-bar])))
1442 ;;** `counsel-yank-pop'
1443 (defcustom counsel-yank-pop-truncate-radius 2
1444 "When non-nil, truncate the display of long strings."
1448 (defun counsel--yank-pop-truncate (str)
1450 (let* ((lines (split-string str "\n" t))
1452 (first-match (cl-position-if
1453 (lambda (s) (string-match ivy--old-re s))
1455 (beg (max 0 (- first-match
1456 counsel-yank-pop-truncate-radius)))
1457 (end (min n (+ first-match
1458 counsel-yank-pop-truncate-radius
1460 (seq (cl-subseq lines beg end)))
1461 (if (null first-match)
1462 (error "Could not match %s" str)
1464 (setcar seq (concat "[...] " (car seq))))
1467 (concat (car (last seq)) " [...]")))
1468 (mapconcat #'identity seq "\n")))
1471 (defun counsel--yank-pop-format-function (cand-pairs)
1472 (ivy--format-function-generic
1473 (lambda (str _extra)
1476 (ivy--add-face s 'ivy-current-match))
1478 (counsel--yank-pop-truncate str) "\n" t)
1480 (lambda (str _extra)
1481 (counsel--yank-pop-truncate str))
1485 (defun counsel-yank-pop-action (s)
1486 "Insert S into the buffer, overwriting the previous yank."
1488 (delete-region ivy-completion-beg
1490 (insert (substring-no-properties s))
1491 (setq ivy-completion-end (point))))
1494 (defun counsel-yank-pop ()
1495 "Ivy replacement for `yank-pop'."
1497 (if (eq last-command 'yank)
1499 (setq ivy-completion-end (point))
1500 (setq ivy-completion-beg
1502 (search-backward (car kill-ring))
1504 (setq ivy-completion-beg (point))
1505 (setq ivy-completion-end (point)))
1506 (let ((candidates (cl-remove-if
1508 (or (< (length s) 3)
1509 (string-match "\\`[\n[:blank:]]+\\'" s)))
1510 (delete-dups kill-ring))))
1511 (let ((ivy-format-function #'counsel--yank-pop-format-function)
1513 (ivy-read "kill-ring: " candidates
1514 :action 'counsel-yank-pop-action
1515 :caller 'counsel-yank-pop))))
1517 ;;** `counsel-imenu'
1518 (defvar imenu-auto-rescan)
1519 (declare-function imenu--subalist-p "imenu")
1520 (declare-function imenu--make-index-alist "imenu")
1522 (defun counsel-imenu-get-candidates-from (alist &optional prefix)
1523 "Create a list of (key . value) from ALIST.
1524 PREFIX is used to create the key."
1525 (cl-mapcan (lambda (elm)
1526 (if (imenu--subalist-p elm)
1527 (counsel-imenu-get-candidates-from
1528 (cl-loop for (e . v) in (cdr elm) collect
1529 (cons e (if (integerp v) (copy-marker v) v)))
1530 ;; pass the prefix to next recursive call
1531 (concat prefix (if prefix ".") (car elm)))
1532 (let ((key (concat prefix (if prefix ".") (car elm))))
1534 ;; create a imenu candidate here
1535 (cons key (if (overlayp (cdr elm))
1536 (overlay-start (cdr elm))
1541 (defun counsel-imenu ()
1542 "Jump to a buffer position indexed by imenu."
1544 (unless (featurep 'imenu)
1545 (require 'imenu nil t))
1546 (let* ((imenu-auto-rescan t)
1547 (items (imenu--make-index-alist t))
1548 (items (delete (assoc "*Rescan*" items) items)))
1549 (ivy-read "imenu items:" (counsel-imenu-get-candidates-from items)
1550 :preselect (thing-at-point 'symbol)
1552 :action (lambda (candidate)
1554 ;; In org-mode, (imenu candidate) will expand child node
1555 ;; after jump to the candidate position
1557 :caller 'counsel-imenu)))
1559 ;;** `counsel-list-processes'
1560 (defun counsel-list-processes-action-delete (x)
1562 (setf (ivy-state-collection ivy-last)
1563 (setq ivy--all-candidates
1564 (delete x ivy--all-candidates))))
1566 (defun counsel-list-processes-action-switch (x)
1568 (switch-to-buffer x)
1569 (message "Process %s doesn't have a buffer" x)))
1572 (defun counsel-list-processes ()
1573 "Offer completion for `process-list'
1574 The default action deletes the selected process.
1575 An extra action allows to switch to the process buffer."
1577 (list-processes--refresh)
1578 (ivy-read "Process: " (mapcar #'process-name (process-list))
1582 ("o" counsel-list-processes-action-delete "kill")
1583 ("s" counsel-list-processes-action-switch "switch"))
1584 :caller 'counsel-list-processes))
1587 ;;** `counsel-rhythmbox'
1588 (defvar rhythmbox-library)
1589 (declare-function rhythmbox-load-library "ext:helm-rhythmbox")
1590 (declare-function dbus-call-method "dbus")
1591 (declare-function dbus-get-property "dbus")
1592 (declare-function rhythmbox-song-uri "ext:helm-rhythmbox")
1593 (declare-function helm-rhythmbox-candidates "ext:helm-rhythmbox")
1595 (defun counsel-rhythmbox-enqueue-song (song)
1596 "Let Rhythmbox enqueue SONG."
1597 (let ((service "org.gnome.Rhythmbox3")
1598 (path "/org/gnome/Rhythmbox3/PlayQueue")
1599 (interface "org.gnome.Rhythmbox3.PlayQueue"))
1600 (dbus-call-method :session service path interface
1601 "AddToQueue" (rhythmbox-song-uri song))))
1603 (defvar counsel-rhythmbox-history nil
1604 "History for `counsel-rhythmbox'.")
1606 (defun counsel-rhythmbox-current-song ()
1607 "Return the currently playing song title."
1609 (let* ((entry (dbus-get-property
1611 "org.mpris.MediaPlayer2.rhythmbox"
1612 "/org/mpris/MediaPlayer2"
1613 "org.mpris.MediaPlayer2.Player"
1615 (artist (caar (cadr (assoc "xesam:artist" entry))))
1616 (album (cl-caadr (assoc "xesam:album" entry)))
1617 (title (cl-caadr (assoc "xesam:title" entry))))
1618 (format "%s - %s - %s" artist album title))))
1621 (defun counsel-rhythmbox ()
1622 "Choose a song from the Rhythmbox library to play or enqueue."
1624 (unless (require 'helm-rhythmbox nil t)
1625 (error "Please install `helm-rhythmbox'"))
1626 (unless rhythmbox-library
1627 (rhythmbox-load-library)
1628 (while (null rhythmbox-library)
1630 (ivy-read "Rhythmbox: "
1631 (helm-rhythmbox-candidates)
1632 :history 'counsel-rhythmbox-history
1633 :preselect (counsel-rhythmbox-current-song)
1636 ("p" helm-rhythmbox-play-song "Play song")
1637 ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))
1638 :caller 'counsel-rhythmbox))
1642 ;;; counsel.el ends here