]> code.delx.au - gnu-emacs-elpa/blob - counsel.el
counsel.el (counsel--async-sentinel): Re-display when no cands
[gnu-emacs-elpa] / counsel.el
1 ;;; counsel.el --- Various completion functions using Ivy -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; URL: https://github.com/abo-abo/swiper
7 ;; Version: 0.1.0
8 ;; Package-Requires: ((emacs "24.1") (swiper "0.4.0"))
9 ;; Keywords: completion, matching
10
11 ;; This file is part of GNU Emacs.
12
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; For a full copy of the GNU General Public License
24 ;; see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
28 ;; Just call one of the interactive functions in this file to complete
29 ;; the corresponding thing using `ivy'.
30 ;;
31 ;; Currently available: Elisp symbols, Clojure symbols, Git files.
32
33 ;;; Code:
34
35 (require 'swiper)
36 (require 'etags)
37
38 ;;;###autoload
39 (defun counsel-el ()
40 "Elisp completion at point."
41 (interactive)
42 (let* ((bnd (unless (and (looking-at ")")
43 (eq (char-before) ?\())
44 (bounds-of-thing-at-point
45 'symbol)))
46 (str (if bnd
47 (buffer-substring-no-properties
48 (car bnd)
49 (cdr bnd))
50 ""))
51 (ivy-height 7)
52 (funp (eq (char-before (car bnd)) ?\())
53 symbol-names)
54 (if bnd
55 (progn
56 (setq ivy-completion-beg
57 (move-marker (make-marker) (car bnd)))
58 (setq ivy-completion-end
59 (move-marker (make-marker) (cdr bnd))))
60 (setq ivy-completion-beg nil)
61 (setq ivy-completion-end nil))
62 (if (string= str "")
63 (mapatoms
64 (lambda (x)
65 (when (symbolp x)
66 (push (symbol-name x) symbol-names))))
67 (setq symbol-names
68 (all-completions str obarray
69 (and funp
70 (lambda (x)
71 (or (functionp x)
72 (macrop x)
73 (special-form-p x)))))))
74 (ivy-read "Symbol name: " symbol-names
75 :predicate (and funp #'functionp)
76 :initial-input str
77 :action #'ivy-completion-in-region-action)))
78
79 (declare-function slime-symbol-start-pos "ext:slime")
80 (declare-function slime-symbol-end-pos "ext:slime")
81 (declare-function slime-contextual-completions "ext:slime-c-p-c")
82
83 ;;;###autoload
84 (defun counsel-cl ()
85 "Common Lisp completion at point."
86 (interactive)
87 (setq ivy-completion-beg (slime-symbol-start-pos))
88 (setq ivy-completion-end (slime-symbol-end-pos))
89 (ivy-read "Symbol name: "
90 (car (slime-contextual-completions
91 ivy-completion-beg
92 ivy-completion-end))
93 :action #'ivy-completion-in-region-action))
94
95 (declare-function deferred:sync! "ext:deferred")
96 (declare-function jedi:complete-request "ext:jedi-core")
97 (declare-function jedi:ac-direct-matches "ext:jedi")
98
99 (defun counsel-jedi ()
100 "Python completion at point."
101 (interactive)
102 (let ((bnd (bounds-of-thing-at-point 'symbol)))
103 (if bnd
104 (progn
105 (setq ivy-completion-beg (car bnd))
106 (setq ivy-completion-end (cdr bnd)))
107 (setq ivy-completion-beg nil)
108 (setq ivy-completion-end nil)))
109 (deferred:sync!
110 (jedi:complete-request))
111 (ivy-read "Symbol name: " (jedi:ac-direct-matches)
112 :action #'counsel--py-action))
113
114 (defun counsel--py-action (symbol)
115 "Insert SYMBOL, erasing the previous one."
116 (when (stringp symbol)
117 (with-ivy-window
118 (when ivy-completion-beg
119 (delete-region
120 ivy-completion-beg
121 ivy-completion-end))
122 (setq ivy-completion-beg
123 (move-marker (make-marker) (point)))
124 (insert symbol)
125 (setq ivy-completion-end
126 (move-marker (make-marker) (point)))
127 (when (equal (get-text-property 0 'symbol symbol) "f")
128 (insert "()")
129 (setq ivy-completion-end
130 (move-marker (make-marker) (point)))
131 (backward-char 1)))))
132
133 (defvar counsel-describe-map
134 (let ((map (make-sparse-keymap)))
135 (define-key map (kbd "C-.") #'counsel-find-symbol)
136 (define-key map (kbd "C-,") #'counsel--info-lookup-symbol)
137 map))
138
139 (defun counsel-find-symbol ()
140 "Jump to the definition of the current symbol."
141 (interactive)
142 (ivy-exit-with-action #'counsel--find-symbol))
143
144 (defun counsel--info-lookup-symbol ()
145 "Lookup the current symbol in the info docs."
146 (interactive)
147 (ivy-exit-with-action #'counsel-info-lookup-symbol))
148
149 (defun counsel--find-symbol (x)
150 "Find symbol definition that corresponds to string X."
151 (with-no-warnings
152 (ring-insert find-tag-marker-ring (point-marker)))
153 (let ((full-name (get-text-property 0 'full-name x)))
154 (if full-name
155 (find-library full-name)
156 (let ((sym (read x)))
157 (cond ((and (eq (ivy-state-caller ivy-last)
158 'counsel-describe-variable)
159 (boundp sym))
160 (find-variable sym))
161 ((fboundp sym)
162 (find-function sym))
163 ((boundp sym)
164 (find-variable sym))
165 ((or (featurep sym)
166 (locate-library
167 (prin1-to-string sym)))
168 (find-library
169 (prin1-to-string sym)))
170 (t
171 (error "Couldn't fild definition of %s"
172 sym)))))))
173
174 (defvar counsel-describe-symbol-history nil
175 "History for `counsel-describe-variable' and `counsel-describe-function'.")
176
177 (defun counsel-symbol-at-point ()
178 "Return current symbol at point as a string."
179 (let ((s (thing-at-point 'symbol)))
180 (and (stringp s)
181 (if (string-match "\\`[`']?\\(.*?\\)'?\\'" s)
182 (match-string 1 s)
183 s))))
184
185 (defun counsel-variable-list ()
186 "Return the list of all currently bound variables."
187 (let (cands)
188 (mapatoms
189 (lambda (vv)
190 (when (or (get vv 'variable-documentation)
191 (and (boundp vv) (not (keywordp vv))))
192 (push (symbol-name vv) cands))))
193 cands))
194
195 ;;;###autoload
196 (defun counsel-describe-variable ()
197 "Forward to `describe-variable'."
198 (interactive)
199 (let ((enable-recursive-minibuffers t))
200 (ivy-read
201 "Describe variable: "
202 (counsel-variable-list)
203 :keymap counsel-describe-map
204 :preselect (counsel-symbol-at-point)
205 :history 'counsel-describe-symbol-history
206 :require-match t
207 :sort t
208 :action (lambda (x)
209 (describe-variable
210 (intern x)))
211 :caller 'counsel-describe-variable)))
212
213 (ivy-set-actions
214 'counsel-describe-variable
215 '(("i" counsel-info-lookup-symbol "info")
216 ("d" counsel--find-symbol "definition")))
217
218 (ivy-set-actions
219 'counsel-describe-function
220 '(("i" counsel-info-lookup-symbol "info")
221 ("d" counsel--find-symbol "definition")))
222
223 (ivy-set-actions
224 'counsel-M-x
225 '(("d" counsel--find-symbol "definition")))
226
227 ;;;###autoload
228 (defun counsel-describe-function ()
229 "Forward to `describe-function'."
230 (interactive)
231 (let ((enable-recursive-minibuffers t))
232 (ivy-read "Describe function: "
233 (let (cands)
234 (mapatoms
235 (lambda (x)
236 (when (fboundp x)
237 (push (symbol-name x) cands))))
238 cands)
239 :keymap counsel-describe-map
240 :preselect (counsel-symbol-at-point)
241 :history 'counsel-describe-symbol-history
242 :require-match t
243 :sort t
244 :action (lambda (x)
245 (describe-function
246 (intern x)))
247 :caller 'counsel-describe-function)))
248
249 (defvar info-lookup-mode)
250 (declare-function info-lookup->completions "info-look")
251 (declare-function info-lookup->mode-value "info-look")
252 (declare-function info-lookup-select-mode "info-look")
253 (declare-function info-lookup-change-mode "info-look")
254 (declare-function info-lookup "info-look")
255
256 ;;;###autoload
257 (defun counsel-info-lookup-symbol (symbol &optional mode)
258 "Forward to (`info-describe-symbol' SYMBOL MODE) with ivy completion."
259 (interactive
260 (progn
261 (require 'info-look)
262 (let* ((topic 'symbol)
263 (mode (cond (current-prefix-arg
264 (info-lookup-change-mode topic))
265 ((info-lookup->mode-value
266 topic (info-lookup-select-mode))
267 info-lookup-mode)
268 ((info-lookup-change-mode topic))))
269 (completions (info-lookup->completions topic mode))
270 (enable-recursive-minibuffers t)
271 (value (ivy-read
272 "Describe symbol: "
273 (mapcar #'car completions)
274 :sort t)))
275 (list value info-lookup-mode))))
276 (require 'info-look)
277 (info-lookup 'symbol symbol mode))
278
279 (defvar counsel-unicode-char-history nil
280 "History for `counsel-unicode-char'.")
281
282 ;;;###autoload
283 (defun counsel-unicode-char ()
284 "Insert a Unicode character at point."
285 (interactive)
286 (let ((minibuffer-allow-text-properties t))
287 (setq ivy-completion-beg (point))
288 (setq ivy-completion-end (point))
289 (ivy-read "Unicode name: "
290 (mapcar (lambda (x)
291 (propertize
292 (format "% -60s%c" (car x) (cdr x))
293 'result (cdr x)))
294 (ucs-names))
295 :action (lambda (char)
296 (with-ivy-window
297 (delete-region ivy-completion-beg ivy-completion-end)
298 (setq ivy-completion-beg (point))
299 (insert-char (get-text-property 0 'result char))
300 (setq ivy-completion-end (point))))
301 :history 'counsel-unicode-char-history)))
302
303 (declare-function cider-sync-request:complete "ext:cider-client")
304 ;;;###autoload
305 (defun counsel-clj ()
306 "Clojure completion at point."
307 (interactive)
308 (counsel--generic
309 (lambda (str)
310 (mapcar
311 #'cl-caddr
312 (cider-sync-request:complete str ":same")))))
313
314 (defvar counsel--git-dir nil
315 "Store the base git directory.")
316
317 ;;;###autoload
318 (defun counsel-git ()
319 "Find file in the current Git repository."
320 (interactive)
321 (setq counsel--git-dir (expand-file-name
322 (locate-dominating-file
323 default-directory ".git")))
324 (let* ((default-directory counsel--git-dir)
325 (cands (split-string
326 (shell-command-to-string
327 "git ls-files --full-name --")
328 "\n"
329 t)))
330 (ivy-read "Find file: " cands
331 :action #'counsel-git-action)))
332
333 (defun counsel-git-action (x)
334 (with-ivy-window
335 (let ((default-directory counsel--git-dir))
336 (find-file x))))
337
338 (defvar counsel--git-grep-dir nil
339 "Store the base git directory.")
340
341 (defvar counsel--git-grep-count nil
342 "Store the line count in current repository.")
343
344 (defun counsel-more-chars (n)
345 "Return two fake candidates prompting for at least N input."
346 (list ""
347 (format "%d chars more" (- n (length ivy-text)))))
348
349 (defvar counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S"
350 "Store the command for `counsel-git-grep'.")
351
352 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
353 "Grep in the current git repository for STRING."
354 (if (and (> counsel--git-grep-count 20000)
355 (< (length string) 3))
356 (counsel-more-chars 3)
357 (let* ((default-directory counsel--git-grep-dir)
358 (cmd (format counsel-git-grep-cmd
359 (setq ivy--old-re (ivy--regex string t)))))
360 (if (<= counsel--git-grep-count 20000)
361 (split-string (shell-command-to-string cmd) "\n" t)
362 (counsel--gg-candidates (ivy--regex string))
363 nil))))
364
365 (defvar counsel-git-grep-map
366 (let ((map (make-sparse-keymap)))
367 (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
368 (define-key map (kbd "M-q") 'counsel-git-grep-query-replace)
369 map))
370
371 (defun counsel-git-grep-query-replace ()
372 "Start `query-replace' with string to replace from last search string."
373 (interactive)
374 (if (null (window-minibuffer-p))
375 (user-error
376 "Should only be called in the minibuffer through `counsel-git-grep-map'")
377 (let* ((enable-recursive-minibuffers t)
378 (from (ivy--regex ivy-text))
379 (to (query-replace-read-to from "Query replace" t)))
380 (ivy-exit-with-action
381 (lambda (_)
382 (let (done-buffers)
383 (dolist (cand ivy--old-cands)
384 (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" cand)
385 (with-ivy-window
386 (let ((file-name (match-string-no-properties 1 cand)))
387 (setq file-name (expand-file-name file-name counsel--git-grep-dir))
388 (unless (member file-name done-buffers)
389 (push file-name done-buffers)
390 (find-file file-name)
391 (goto-char (point-min)))
392 (perform-replace from to t t nil)))))))))))
393
394 (defun counsel-git-grep-recenter ()
395 (interactive)
396 (with-ivy-window
397 (counsel-git-grep-action ivy--current)
398 (recenter-top-bottom)))
399
400 (defun counsel-git-grep-action (x)
401 (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
402 (with-ivy-window
403 (let ((file-name (match-string-no-properties 1 x))
404 (line-number (match-string-no-properties 2 x)))
405 (find-file (expand-file-name file-name counsel--git-grep-dir))
406 (goto-char (point-min))
407 (forward-line (1- (string-to-number line-number)))
408 (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
409 (unless (eq ivy-exit 'done)
410 (swiper--cleanup)
411 (swiper--add-overlays (ivy--regex ivy-text)))))))
412
413 (defvar counsel-git-grep-history nil
414 "History for `counsel-git-grep'.")
415
416 (defvar counsel-git-grep-cmd-history
417 '("git --no-pager grep --full-name -n --no-color -i -e %S")
418 "History for `counsel-git-grep' shell commands.")
419
420 ;;;###autoload
421 (defun counsel-git-grep (&optional cmd initial-input)
422 "Grep for a string in the current git repository.
423 When CMD is a string, use it as a \"git grep\" command.
424 When CMD is non-nil, prompt for a specific \"git grep\" command.
425 INITIAL-INPUT can be given as the initial minibuffer input."
426 (interactive "P")
427 (cond
428 ((stringp cmd)
429 (setq counsel-git-grep-cmd cmd))
430 (cmd
431 (setq counsel-git-grep-cmd
432 (ivy-read "cmd: " counsel-git-grep-cmd-history
433 :history 'counsel-git-grep-cmd-history))
434 (setq counsel-git-grep-cmd-history
435 (delete-dups counsel-git-grep-cmd-history)))
436 (t
437 (setq counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S")))
438 (setq counsel--git-grep-dir
439 (locate-dominating-file default-directory ".git"))
440 (if (null counsel--git-grep-dir)
441 (error "Not in a git repository")
442 (setq counsel--git-grep-count (counsel--gg-count "" t))
443 (ivy-read "git grep: " 'counsel-git-grep-function
444 :initial-input initial-input
445 :matcher #'counsel-git-grep-matcher
446 :dynamic-collection (> counsel--git-grep-count 20000)
447 :keymap counsel-git-grep-map
448 :action #'counsel-git-grep-action
449 :unwind #'swiper--cleanup
450 :history 'counsel-git-grep-history
451 :caller 'counsel-git-grep)))
452
453 (defcustom counsel-find-file-at-point nil
454 "When non-nil, add file-at-point to the list of candidates."
455 :type 'boolean
456 :group 'ivy)
457
458 (declare-function ffap-guesser "ffap")
459
460 (defvar counsel-find-file-map (make-sparse-keymap))
461
462 ;;;###autoload
463 (defun counsel-find-file (&optional initial-input)
464 "Forward to `find-file'.
465 When INITIAL-INPUT is non-nil, use it in the minibuffer during completion."
466 (interactive)
467 (ivy-read "Find file: " 'read-file-name-internal
468 :matcher #'counsel--find-file-matcher
469 :initial-input initial-input
470 :action
471 (lambda (x)
472 (with-ivy-window
473 (find-file (expand-file-name x ivy--directory))))
474 :preselect (when counsel-find-file-at-point
475 (require 'ffap)
476 (ffap-guesser))
477 :require-match 'confirm-after-completion
478 :history 'file-name-history
479 :keymap counsel-find-file-map))
480
481 (defcustom counsel-find-file-ignore-regexp nil
482 "A regexp of files to ignore while in `counsel-find-file'.
483 These files are un-ignored if `ivy-text' matches them.
484 The common way to show all files is to start `ivy-text' with a dot.
485 Possible value: \"\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)\"."
486 :group 'ivy)
487
488 (defun counsel--find-file-matcher (regexp candidates)
489 "Return REGEXP-matching CANDIDATES.
490 Skip some dotfiles unless `ivy-text' requires them."
491 (let ((res (ivy--re-filter regexp candidates)))
492 (if (or (null counsel-find-file-ignore-regexp)
493 (string-match counsel-find-file-ignore-regexp ivy-text))
494 res
495 (cl-remove-if
496 (lambda (x)
497 (string-match counsel-find-file-ignore-regexp x))
498 res))))
499
500 (defun counsel-git-grep-matcher (regexp candidates)
501 (or (and (equal regexp ivy--old-re)
502 ivy--old-cands)
503 (prog1
504 (setq ivy--old-cands
505 (cl-remove-if-not
506 (lambda (x)
507 (ignore-errors
508 (when (string-match "^[^:]+:[^:]+:" x)
509 (setq x (substring x (match-end 0)))
510 (if (stringp regexp)
511 (string-match regexp x)
512 (let ((res t))
513 (dolist (re regexp)
514 (setq res
515 (and res
516 (ignore-errors
517 (if (cdr re)
518 (string-match (car re) x)
519 (not (string-match (car re) x)))))))
520 res)))))
521 candidates))
522 (setq ivy--old-re regexp))))
523
524 (defvar counsel--async-time nil
525 "Store the time when a new process was started.
526 Or the time of the last minibuffer update.")
527
528 (defun counsel--async-command (cmd)
529 (let* ((counsel--process " *counsel*")
530 (proc (get-process counsel--process))
531 (buff (get-buffer counsel--process)))
532 (when proc
533 (delete-process proc))
534 (when buff
535 (kill-buffer buff))
536 (setq proc (start-process-shell-command
537 counsel--process
538 counsel--process
539 cmd))
540 (setq counsel--async-time (current-time))
541 (set-process-sentinel proc #'counsel--async-sentinel)
542 (set-process-filter proc #'counsel--async-filter)))
543
544 (defun counsel--async-sentinel (process event)
545 (if (string= event "finished\n")
546 (progn
547 (with-current-buffer (process-buffer process)
548 (setq ivy--all-candidates
549 (ivy--sort-maybe
550 (split-string (buffer-string) "\n" t)))
551 (if (null ivy--old-cands)
552 (setq ivy--index
553 (or (ivy--preselect-index
554 (ivy-state-preselect ivy-last)
555 ivy--all-candidates)
556 0))
557 (let ((re (funcall ivy--regex-function ivy-text)))
558 (unless (stringp re)
559 (setq re (caar re)))
560 (ivy--recompute-index
561 ivy-text re ivy--all-candidates)))
562 (setq ivy--old-cands ivy--all-candidates))
563 (if (null ivy--all-candidates)
564 (ivy--insert-minibuffer "")
565 (ivy--exhibit)))
566 (if (string= event "exited abnormally with code 1\n")
567 (progn
568 (setq ivy--all-candidates '("Error"))
569 (setq ivy--old-cands ivy--all-candidates)
570 (ivy--exhibit)))))
571
572 (defun counsel--async-filter (process str)
573 "Receive from PROCESS the output STR.
574 Update the minibuffer with the amount of lines collected every
575 0.5 seconds since the last update."
576 (with-current-buffer (process-buffer process)
577 (insert str))
578 (let (size)
579 (when (time-less-p
580 ;; 0.5s
581 '(0 0 500000 0)
582 (time-since counsel--async-time))
583 (with-current-buffer (process-buffer process)
584 (goto-char (point-min))
585 (setq size (- (buffer-size) (forward-line (buffer-size)))))
586 (ivy--insert-minibuffer
587 (format "\ncollected: %d" size))
588 (setq counsel--async-time (current-time)))))
589
590 (defun counsel-locate-action-extern (x)
591 "Use xdg-open shell command on X."
592 (call-process shell-file-name nil
593 nil nil
594 shell-command-switch
595 (format "%s %s"
596 (if (eq system-type 'darwin)
597 "open"
598 "xdg-open")
599 (shell-quote-argument x))))
600
601 (declare-function dired-jump "dired-x")
602 (defun counsel-locate-action-dired (x)
603 "Use `dired-jump' on X."
604 (dired-jump nil x))
605
606 (defvar counsel-locate-history nil
607 "History for `counsel-locate'.")
608
609 (defcustom counsel-locate-options (if (eq system-type 'darwin)
610 '("-i")
611 '("-i" "--regex"))
612 "Command line options for `locate`."
613 :group 'ivy
614 :type '(repeat string))
615
616 (ivy-set-actions
617 'counsel-locate
618 '(("x" counsel-locate-action-extern "xdg-open")
619 ("d" counsel-locate-action-dired "dired")))
620
621 (defun counsel-unquote-regex-parens (str)
622 (replace-regexp-in-string
623 "\\\\)" ")"
624 (replace-regexp-in-string
625 "\\\\(" "("
626 str)))
627
628 (defun counsel-locate-function (str &rest _u)
629 (if (< (length str) 3)
630 (counsel-more-chars 3)
631 (counsel--async-command
632 (format "locate %s '%s'"
633 (mapconcat #'identity counsel-locate-options " ")
634 (counsel-unquote-regex-parens
635 (ivy--regex str))))
636 '("" "working...")))
637
638 (defun counsel-delete-process ()
639 (let ((process (get-process " *counsel*")))
640 (when process
641 (delete-process process))))
642
643 ;;;###autoload
644 (defun counsel-locate (&optional initial-input)
645 "Call the \"locate\" shell command.
646 INITIAL-INPUT can be given as the initial minibuffer input."
647 (interactive)
648 (ivy-read "Locate: " #'counsel-locate-function
649 :initial-input initial-input
650 :dynamic-collection t
651 :history 'counsel-locate-history
652 :action (lambda (file)
653 (with-ivy-window
654 (when file
655 (find-file file))))
656 :unwind #'counsel-delete-process))
657
658 (defun counsel--generic (completion-fn)
659 "Complete thing at point with COMPLETION-FN."
660 (let* ((bnd (bounds-of-thing-at-point 'symbol))
661 (str (if bnd
662 (buffer-substring-no-properties
663 (car bnd) (cdr bnd))
664 ""))
665 (candidates (funcall completion-fn str))
666 (ivy-height 7)
667 (res (ivy-read (format "pattern (%s): " str)
668 candidates)))
669 (when (stringp res)
670 (when bnd
671 (delete-region (car bnd) (cdr bnd)))
672 (insert res))))
673
674 (defun counsel-directory-parent (dir)
675 "Return the directory parent of directory DIR."
676 (concat (file-name-nondirectory
677 (directory-file-name dir)) "/"))
678
679 (defun counsel-string-compose (prefix str)
680 "Make PREFIX the display prefix of STR though text properties."
681 (let ((str (copy-sequence str)))
682 (put-text-property
683 0 1 'display
684 (concat prefix (substring str 0 1))
685 str)
686 str))
687
688 ;;;###autoload
689 (defun counsel-load-library ()
690 "Load a selected the Emacs Lisp library.
691 The libraries are offered from `load-path'."
692 (interactive)
693 (let ((dirs load-path)
694 (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
695 (cands (make-hash-table :test #'equal))
696 short-name
697 old-val
698 dir-parent
699 res)
700 (dolist (dir dirs)
701 (when (file-directory-p dir)
702 (dolist (file (file-name-all-completions "" dir))
703 (when (string-match suffix file)
704 (unless (string-match "pkg.elc?$" file)
705 (setq short-name (substring file 0 (match-beginning 0)))
706 (if (setq old-val (gethash short-name cands))
707 (progn
708 ;; assume going up directory once will resolve name clash
709 (setq dir-parent (counsel-directory-parent (cdr old-val)))
710 (puthash short-name
711 (cons
712 (counsel-string-compose dir-parent (car old-val))
713 (cdr old-val))
714 cands)
715 (setq dir-parent (counsel-directory-parent dir))
716 (puthash (concat dir-parent short-name)
717 (cons
718 (propertize
719 (counsel-string-compose
720 dir-parent short-name)
721 'full-name (expand-file-name file dir))
722 dir)
723 cands))
724 (puthash short-name
725 (cons (propertize
726 short-name
727 'full-name (expand-file-name file dir))
728 dir) cands)))))))
729 (maphash (lambda (_k v) (push (car v) res)) cands)
730 (ivy-read "Load library: " (nreverse res)
731 :action (lambda (x)
732 (load-library
733 (get-text-property 0 'full-name x)))
734 :keymap counsel-describe-map)))
735
736 (defvar counsel-gg-state nil
737 "The current state of candidates / count sync.")
738
739 (defun counsel--gg-candidates (regex)
740 "Return git grep candidates for REGEX."
741 (setq counsel-gg-state -2)
742 (counsel--gg-count regex)
743 (let* ((default-directory counsel--git-grep-dir)
744 (counsel-gg-process " *counsel-gg*")
745 (proc (get-process counsel-gg-process))
746 (buff (get-buffer counsel-gg-process)))
747 (when proc
748 (delete-process proc))
749 (when buff
750 (kill-buffer buff))
751 (setq proc (start-process-shell-command
752 counsel-gg-process
753 counsel-gg-process
754 (concat
755 (format counsel-git-grep-cmd regex)
756 " | head -n 200")))
757 (set-process-sentinel
758 proc
759 #'counsel--gg-sentinel)))
760
761 (defun counsel--gg-sentinel (process event)
762 (if (string= event "finished\n")
763 (progn
764 (with-current-buffer (process-buffer process)
765 (setq ivy--all-candidates
766 (or (split-string (buffer-string) "\n" t)
767 '("")))
768 (setq ivy--old-cands ivy--all-candidates))
769 (when (= 0 (cl-incf counsel-gg-state))
770 (ivy--exhibit)))
771 (if (string= event "exited abnormally with code 1\n")
772 (progn
773 (setq ivy--all-candidates '("Error"))
774 (setq ivy--old-cands ivy--all-candidates)
775 (ivy--exhibit)))))
776
777 (defun counsel--gg-count (regex &optional no-async)
778 "Quickly and asynchronously count the amount of git grep REGEX matches.
779 When NO-ASYNC is non-nil, do it synchronously."
780 (let ((default-directory counsel--git-grep-dir)
781 (cmd
782 (concat
783 (format
784 (replace-regexp-in-string
785 "--full-name" "-c"
786 counsel-git-grep-cmd)
787 ;; "git grep -i -c '%s'"
788 (replace-regexp-in-string
789 "-" "\\\\-"
790 (replace-regexp-in-string "'" "''" regex)))
791 " | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"))
792 (counsel-ggc-process " *counsel-gg-count*"))
793 (if no-async
794 (string-to-number (shell-command-to-string cmd))
795 (let ((proc (get-process counsel-ggc-process))
796 (buff (get-buffer counsel-ggc-process)))
797 (when proc
798 (delete-process proc))
799 (when buff
800 (kill-buffer buff))
801 (setq proc (start-process-shell-command
802 counsel-ggc-process
803 counsel-ggc-process
804 cmd))
805 (set-process-sentinel
806 proc
807 #'(lambda (process event)
808 (when (string= event "finished\n")
809 (with-current-buffer (process-buffer process)
810 (setq ivy--full-length (string-to-number (buffer-string))))
811 (when (= 0 (cl-incf counsel-gg-state))
812 (ivy--exhibit)))))))))
813
814 (defun counsel--M-x-transformer (cand-pair)
815 "Add a binding to CAND-PAIR cdr if the car is bound in the current window.
816 CAND-PAIR is (command-name . extra-info)."
817 (let* ((command-name (car cand-pair))
818 (extra-info (cdr cand-pair))
819 (binding (substitute-command-keys (format "\\[%s]" command-name))))
820 (setq binding (replace-regexp-in-string "C-x 6" "<f2>" binding))
821 (if (string-match "^M-x" binding)
822 cand-pair
823 (cons command-name
824 (if extra-info
825 (format " %s (%s)" extra-info (propertize binding 'face 'font-lock-keyword-face))
826 (format " (%s)" (propertize binding 'face 'font-lock-keyword-face)))))))
827
828 (defvar smex-initialized-p)
829 (defvar smex-ido-cache)
830 (declare-function smex-initialize "ext:smex")
831 (declare-function smex-detect-new-commands "ext:smex")
832 (declare-function smex-update "ext:smex")
833 (declare-function smex-rank "ext:smex")
834
835 (defun counsel--M-x-prompt ()
836 "M-x plus the string representation of `current-prefix-arg'."
837 (if (not current-prefix-arg)
838 "M-x "
839 (concat
840 (if (eq current-prefix-arg '-)
841 "- "
842 (if (integerp current-prefix-arg)
843 (format "%d " current-prefix-arg)
844 (if (= (car current-prefix-arg) 4)
845 "C-u "
846 (format "%d " (car current-prefix-arg)))))
847 "M-x ")))
848
849 ;;;###autoload
850 (defun counsel-M-x (&optional initial-input)
851 "Ivy version of `execute-extended-command'.
852 Optional INITIAL-INPUT is the initial input in the minibuffer."
853 (interactive)
854 (unless initial-input
855 (setq initial-input (cdr (assoc this-command
856 ivy-initial-inputs-alist))))
857 (let* ((store ivy-format-function)
858 (ivy-format-function
859 (lambda (cand-pairs)
860 (funcall
861 store
862 (with-ivy-window
863 (mapcar #'counsel--M-x-transformer cand-pairs)))))
864 (cands obarray)
865 (pred 'commandp)
866 (sort t))
867 (when (require 'smex nil 'noerror)
868 (unless smex-initialized-p
869 (smex-initialize))
870 (smex-detect-new-commands)
871 (smex-update)
872 (setq cands smex-ido-cache)
873 (setq pred nil)
874 (setq sort nil))
875 (ivy-read (counsel--M-x-prompt) cands
876 :predicate pred
877 :require-match t
878 :history 'extended-command-history
879 :action
880 (lambda (cmd)
881 (when (featurep 'smex)
882 (smex-rank (intern cmd)))
883 (let ((prefix-arg current-prefix-arg)
884 (ivy-format-function store)
885 (this-command (intern cmd)))
886 (command-execute (intern cmd) 'record)))
887 :sort sort
888 :keymap counsel-describe-map
889 :initial-input initial-input
890 :caller 'counsel-M-x)))
891
892 (declare-function powerline-reset "ext:powerline")
893
894 (defun counsel--load-theme-action (x)
895 "Disable current themes and load theme X."
896 (condition-case nil
897 (progn
898 (mapc #'disable-theme custom-enabled-themes)
899 (load-theme (intern x))
900 (when (fboundp 'powerline-reset)
901 (powerline-reset)))
902 (error "Problem loading theme %s" x)))
903
904 ;;;###autoload
905 (defun counsel-load-theme ()
906 "Forward to `load-theme'.
907 Usable with `ivy-resume', `ivy-next-line-and-call' and
908 `ivy-previous-line-and-call'."
909 (interactive)
910 (ivy-read "Load custom theme: "
911 (mapcar 'symbol-name
912 (custom-available-themes))
913 :action #'counsel--load-theme-action))
914
915 (defvar rhythmbox-library)
916 (declare-function rhythmbox-load-library "ext:helm-rhythmbox")
917 (declare-function dbus-call-method "dbus")
918 (declare-function rhythmbox-song-uri "ext:helm-rhythmbox")
919 (declare-function helm-rhythmbox-candidates "ext:helm-rhythmbox")
920
921 (defun counsel-rhythmbox-enqueue-song (song)
922 "Let Rhythmbox enqueue SONG."
923 (let ((service "org.gnome.Rhythmbox3")
924 (path "/org/gnome/Rhythmbox3/PlayQueue")
925 (interface "org.gnome.Rhythmbox3.PlayQueue"))
926 (dbus-call-method :session service path interface
927 "AddToQueue" (rhythmbox-song-uri song))))
928
929 (defvar counsel-rhythmbox-history nil
930 "History for `counsel-rhythmbox'.")
931
932 ;;;###autoload
933 (defun counsel-rhythmbox ()
934 "Choose a song from the Rhythmbox library to play or enqueue."
935 (interactive)
936 (unless (require 'helm-rhythmbox nil t)
937 (error "Please install `helm-rhythmbox'"))
938 (unless rhythmbox-library
939 (rhythmbox-load-library)
940 (while (null rhythmbox-library)
941 (sit-for 0.1)))
942 (ivy-read "Rhythmbox: "
943 (helm-rhythmbox-candidates)
944 :history 'counsel-rhythmbox-history
945 :action
946 '(1
947 ("p" helm-rhythmbox-play-song "Play song")
948 ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))
949 :caller 'counsel-rhythmbox))
950
951 (defvar counsel-org-tags nil
952 "Store the current list of tags.")
953
954 (defvar org-outline-regexp)
955 (defvar org-indent-mode)
956 (defvar org-indent-indentation-per-level)
957 (defvar org-tags-column)
958 (declare-function org-get-tags-string "org")
959 (declare-function org-move-to-column "org-compat")
960
961 (defun counsel-org-change-tags (tags)
962 (let ((current (org-get-tags-string))
963 (col (current-column))
964 level)
965 ;; Insert new tags at the correct column
966 (beginning-of-line 1)
967 (setq level (or (and (looking-at org-outline-regexp)
968 (- (match-end 0) (point) 1))
969 1))
970 (cond
971 ((and (equal current "") (equal tags "")))
972 ((re-search-forward
973 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
974 (point-at-eol) t)
975 (if (equal tags "")
976 (delete-region
977 (match-beginning 0)
978 (match-end 0))
979 (goto-char (match-beginning 0))
980 (let* ((c0 (current-column))
981 ;; compute offset for the case of org-indent-mode active
982 (di (if (bound-and-true-p org-indent-mode)
983 (* (1- org-indent-indentation-per-level) (1- level))
984 0))
985 (p0 (if (equal (char-before) ?*) (1+ (point)) (point)))
986 (tc (+ org-tags-column (if (> org-tags-column 0) (- di) di)))
987 (c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags)))))
988 (rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
989 (replace-match rpl t t)
990 (and c0 indent-tabs-mode (tabify p0 (point)))
991 tags)))
992 (t (error "Tags alignment failed")))
993 (org-move-to-column col)))
994
995 (defun counsel-org--set-tags ()
996 (counsel-org-change-tags
997 (if counsel-org-tags
998 (format ":%s:"
999 (mapconcat #'identity counsel-org-tags ":"))
1000 "")))
1001
1002 (defvar org-agenda-bulk-marked-entries)
1003
1004 (declare-function org-get-at-bol "org")
1005 (declare-function org-agenda-error "org-agenda")
1006
1007 (defun counsel-org-tag-action (x)
1008 (if (member x counsel-org-tags)
1009 (progn
1010 (setq counsel-org-tags (delete x counsel-org-tags)))
1011 (unless (equal x "")
1012 (setq counsel-org-tags (append counsel-org-tags (list x)))
1013 (unless (member x ivy--all-candidates)
1014 (setq ivy--all-candidates (append ivy--all-candidates (list x))))))
1015 (let ((prompt (counsel-org-tag-prompt)))
1016 (setf (ivy-state-prompt ivy-last) prompt)
1017 (setq ivy--prompt (concat "%-4d " prompt)))
1018 (cond ((memq this-command '(ivy-done
1019 ivy-alt-done
1020 ivy-immediate-done))
1021 (if (eq major-mode 'org-agenda-mode)
1022 (if (null org-agenda-bulk-marked-entries)
1023 (let ((hdmarker (or (org-get-at-bol 'org-hd-marker)
1024 (org-agenda-error))))
1025 (with-current-buffer (marker-buffer hdmarker)
1026 (goto-char hdmarker)
1027 (counsel-org--set-tags)))
1028 (let ((add-tags (copy-sequence counsel-org-tags)))
1029 (dolist (m org-agenda-bulk-marked-entries)
1030 (with-current-buffer (marker-buffer m)
1031 (save-excursion
1032 (goto-char m)
1033 (setq counsel-org-tags
1034 (delete-dups
1035 (append (split-string (org-get-tags-string) ":" t)
1036 add-tags)))
1037 (counsel-org--set-tags))))))
1038 (counsel-org--set-tags)))
1039 ((eq this-command 'ivy-call)
1040 (delete-minibuffer-contents))))
1041
1042 (defun counsel-org-tag-prompt ()
1043 (format "Tags (%s): "
1044 (mapconcat #'identity counsel-org-tags ", ")))
1045
1046 (defvar org-setting-tags)
1047 (defvar org-last-tags-completion-table)
1048 (defvar org-tag-persistent-alist)
1049 (defvar org-tag-alist)
1050 (defvar org-complete-tags-always-offer-all-agenda-tags)
1051
1052 (declare-function org-at-heading-p "org")
1053 (declare-function org-back-to-heading "org")
1054 (declare-function org-get-buffer-tags "org")
1055 (declare-function org-global-tags-completion-table "org")
1056 (declare-function org-agenda-files "org")
1057 (declare-function org-agenda-set-tags "org-agenda")
1058
1059 ;;;###autoload
1060 (defun counsel-org-tag ()
1061 "Add or remove tags in org-mode."
1062 (interactive)
1063 (save-excursion
1064 (if (eq major-mode 'org-agenda-mode)
1065 (if org-agenda-bulk-marked-entries
1066 (setq counsel-org-tags nil)
1067 (let ((hdmarker (or (org-get-at-bol 'org-hd-marker)
1068 (org-agenda-error))))
1069 (with-current-buffer (marker-buffer hdmarker)
1070 (goto-char hdmarker)
1071 (setq counsel-org-tags
1072 (split-string (org-get-tags-string) ":" t)))))
1073 (unless (org-at-heading-p)
1074 (org-back-to-heading t))
1075 (setq counsel-org-tags (split-string (org-get-tags-string) ":" t)))
1076 (let ((org-setting-tags t)
1077 (org-last-tags-completion-table
1078 (append org-tag-persistent-alist
1079 (or org-tag-alist (org-get-buffer-tags))
1080 (and
1081 (or org-complete-tags-always-offer-all-agenda-tags
1082 (eq major-mode 'org-agenda-mode))
1083 (org-global-tags-completion-table
1084 (org-agenda-files))))))
1085 (ivy-read (counsel-org-tag-prompt)
1086 (lambda (str &rest _unused)
1087 (delete-dups
1088 (all-completions str 'org-tags-completion-function)))
1089 :history 'org-tags-history
1090 :action 'counsel-org-tag-action))))
1091
1092 ;;;###autoload
1093 (defun counsel-org-tag-agenda ()
1094 "Set tags for the current agenda item."
1095 (interactive)
1096 (let ((store (symbol-function 'org-set-tags)))
1097 (unwind-protect
1098 (progn
1099 (fset 'org-set-tags
1100 (symbol-function 'counsel-org-tag))
1101 (org-agenda-set-tags nil nil))
1102 (fset 'org-set-tags store))))
1103
1104 (defcustom counsel-ag-base-command "ag --vimgrep %S"
1105 "Format string to use in `cousel-ag-function' to construct the
1106 command. %S will be replaced by the regex string. The default is
1107 \"ag --vimgrep %S\"."
1108 :type 'stringp
1109 :group 'ivy)
1110
1111 (defun counsel-ag-function (string &optional _pred &rest _unused)
1112 "Grep in the current directory for STRING."
1113 (if (< (length string) 3)
1114 (counsel-more-chars 3)
1115 (let ((default-directory counsel--git-grep-dir)
1116 (regex (counsel-unquote-regex-parens
1117 (setq ivy--old-re
1118 (ivy--regex string)))))
1119 (counsel--async-command
1120 (format counsel-ag-base-command regex))
1121 nil)))
1122
1123 ;;;###autoload
1124 (defun counsel-ag (&optional initial-input initial-directory)
1125 "Grep for a string in the current directory using ag.
1126 INITIAL-INPUT can be given as the initial minibuffer input."
1127 (interactive)
1128 (setq counsel--git-grep-dir (or initial-directory default-directory))
1129 (ivy-read "ag: " 'counsel-ag-function
1130 :initial-input initial-input
1131 :dynamic-collection t
1132 :history 'counsel-git-grep-history
1133 :action #'counsel-git-grep-action
1134 :unwind (lambda ()
1135 (counsel-delete-process)
1136 (swiper--cleanup))))
1137
1138 ;;;###autoload
1139 (defun counsel-grep ()
1140 "Grep for a string in the current file."
1141 (interactive)
1142 (setq counsel--git-grep-dir (buffer-file-name))
1143 (ivy-read "grep: " 'counsel-grep-function
1144 :dynamic-collection t
1145 :preselect (format "%d:%s"
1146 (line-number-at-pos)
1147 (buffer-substring-no-properties
1148 (line-beginning-position)
1149 (line-end-position)))
1150 :history 'counsel-git-grep-history
1151 :update-fn (lambda ()
1152 (counsel-grep-action ivy--current))
1153 :action #'counsel-grep-action
1154 :unwind (lambda ()
1155 (counsel-delete-process)
1156 (swiper--cleanup))
1157 :caller 'counsel-grep))
1158
1159 (defun counsel-grep-function (string &optional _pred &rest _unused)
1160 "Grep in the current directory for STRING."
1161 (if (< (length string) 3)
1162 (counsel-more-chars 3)
1163 (let ((regex (counsel-unquote-regex-parens
1164 (setq ivy--old-re
1165 (ivy--regex string)))))
1166 (counsel--async-command
1167 (format "grep -nP --ignore-case '%s' %s" regex counsel--git-grep-dir))
1168 nil)))
1169
1170 (defun counsel-grep-action (x)
1171 (when (string-match "\\`\\([0-9]+\\):\\(.*\\)\\'" x)
1172 (with-ivy-window
1173 (let ((file-name counsel--git-grep-dir)
1174 (line-number (match-string-no-properties 1 x)))
1175 (find-file file-name)
1176 (goto-char (point-min))
1177 (forward-line (1- (string-to-number line-number)))
1178 (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
1179 (unless (eq ivy-exit 'done)
1180 (swiper--cleanup)
1181 (swiper--add-overlays (ivy--regex ivy-text)))))))
1182
1183 (defun counsel-recoll-function (string &optional _pred &rest _unused)
1184 "Grep in the current directory for STRING."
1185 (if (< (length string) 3)
1186 (counsel-more-chars 3)
1187 (counsel--async-command
1188 (format "recoll -t -b '%s'" string))
1189 nil))
1190
1191 ;; This command uses the recollq command line tool that comes together
1192 ;; with the recoll (the document indexing database) source:
1193 ;; http://www.lesbonscomptes.com/recoll/download.html
1194 ;; You need to build it yourself (together with recoll):
1195 ;; cd ./query && make && sudo cp recollq /usr/local/bin
1196 ;; You can try the GUI version of recoll with:
1197 ;; sudo apt-get install recoll
1198 ;; Unfortunately, that does not install recollq.
1199 (defun counsel-recoll (&optional initial-input)
1200 "Search for a string in the recoll database.
1201 You'll be given a list of files that match.
1202 Selecting a file will launch `swiper' for that file.
1203 INITIAL-INPUT can be given as the initial minibuffer input."
1204 (interactive)
1205 (ivy-read "recoll: " 'counsel-recoll-function
1206 :initial-input initial-input
1207 :dynamic-collection t
1208 :history 'counsel-git-grep-history
1209 :action (lambda (x)
1210 (when (string-match "file://\\(.*\\)\\'" x)
1211 (let ((file-name (match-string 1 x)))
1212 (find-file file-name)
1213 (unless (string-match "pdf$" x)
1214 (swiper ivy-text)))))))
1215
1216 (defvar tmm-km-list nil)
1217 (declare-function tmm-get-keymap "tmm")
1218 (declare-function tmm--completion-table "tmm")
1219 (declare-function tmm-get-keybind "tmm")
1220
1221 (defun counsel-tmm-prompt (menu)
1222 "Select and call an item from the MENU keymap."
1223 (let (out
1224 choice
1225 chosen-string)
1226 (setq tmm-km-list nil)
1227 (map-keymap (lambda (k v) (tmm-get-keymap (cons k v))) menu)
1228 (setq tmm-km-list (nreverse tmm-km-list))
1229 (setq out (ivy-read "Menu bar: " (tmm--completion-table tmm-km-list)
1230 :require-match t
1231 :sort nil))
1232 (setq choice (cdr (assoc out tmm-km-list)))
1233 (setq chosen-string (car choice))
1234 (setq choice (cdr choice))
1235 (cond ((keymapp choice)
1236 (counsel-tmm-prompt choice))
1237 ((and choice chosen-string)
1238 (setq last-command-event chosen-string)
1239 (call-interactively choice)))))
1240
1241 (defun counsel-tmm ()
1242 "Text-mode emulation of looking and choosing from a menubar."
1243 (interactive)
1244 (require 'tmm)
1245 (run-hooks 'menu-bar-update-hook)
1246 (counsel-tmm-prompt (tmm-get-keybind [menu-bar])))
1247
1248 (defcustom counsel-yank-pop-truncate nil
1249 "When non-nil, truncate the display of long strings."
1250 :group 'ivy)
1251
1252 ;;;###autoload
1253 (defun counsel-yank-pop ()
1254 "Ivy replacement for `yank-pop'."
1255 (interactive)
1256 (if (eq last-command 'yank)
1257 (progn
1258 (setq ivy-completion-end (point))
1259 (setq ivy-completion-beg
1260 (save-excursion
1261 (search-backward (car kill-ring))
1262 (point))))
1263 (setq ivy-completion-beg (point))
1264 (setq ivy-completion-end (point)))
1265 (let ((candidates (cl-remove-if
1266 (lambda (s)
1267 (or (< (length s) 3)
1268 (string-match "\\`[\n[:blank:]]+\\'" s)))
1269 (delete-dups kill-ring))))
1270 (when counsel-yank-pop-truncate
1271 (setq candidates
1272 (mapcar (lambda (s)
1273 (if (string-match "\\`\\(.*\n.*\n.*\n.*\\)\n" s)
1274 (progn
1275 (let ((s (copy-sequence s)))
1276 (put-text-property
1277 (match-end 1)
1278 (length s)
1279 'display
1280 " [...]"
1281 s)
1282 s))
1283 s))
1284 candidates)))
1285 (ivy-read "kill-ring: " candidates
1286 :action 'counsel-yank-pop-action)))
1287
1288 (defun counsel-yank-pop-action (s)
1289 "Insert S into the buffer, overwriting the previous yank."
1290 (with-ivy-window
1291 (delete-region ivy-completion-beg
1292 ivy-completion-end)
1293 (insert (substring-no-properties s))
1294 (setq ivy-completion-end (point))))
1295
1296 (defvar imenu-auto-rescan)
1297 (declare-function imenu--subalist-p "imenu")
1298 (declare-function imenu--make-index-alist "imenu")
1299
1300 (defun counsel-imenu-get-candidates-from (alist &optional prefix)
1301 "Create a list of (key . value) from ALIST.
1302 PREFIX is used to create the key."
1303 (cl-mapcan (lambda (elm)
1304 (if (imenu--subalist-p elm)
1305 (counsel-imenu-get-candidates-from
1306 (cl-loop for (e . v) in (cdr elm) collect
1307 (cons e (if (integerp v) (copy-marker v) v)))
1308 (concat prefix (if prefix ".") (car elm)))
1309 (list
1310 (cons (concat prefix (if prefix ".") (car elm))
1311 (if (overlayp (cdr elm))
1312 (overlay-start (cdr elm))
1313 (cdr elm))))))
1314 alist))
1315
1316 ;;;###autoload
1317 (defun counsel-imenu ()
1318 "Jump to a buffer position indexed by imenu."
1319 (interactive)
1320 (unless (featurep 'imenu)
1321 (require 'imenu nil t))
1322 (let* ((imenu-auto-rescan t)
1323 (items (imenu--make-index-alist t))
1324 (items (delete (assoc "*Rescan*" items) items)))
1325 (ivy-read "imenu items:" (counsel-imenu-get-candidates-from items)
1326 :action (lambda (pos)
1327 (with-ivy-window
1328 (goto-char pos))))))
1329
1330 (provide 'counsel)
1331
1332 ;;; counsel.el ends here