;;; Code:
(require 'swiper)
+(require 'etags)
(defvar counsel-completion-beg nil
"Completion bounds start.")
(defun counsel--find-symbol (x)
"Find symbol definition that corresponds to string X."
+ (with-no-warnings
+ (ring-insert find-tag-marker-ring (point-marker)))
(let ((full-name (get-text-property 0 'full-name x)))
(if full-name
(find-library full-name)
(let ((sym (read x)))
- (cond ((boundp sym)
+ (cond ((and (eq (ivy-state-caller ivy-last)
+ 'counsel-describe-variable)
+ (boundp sym))
(find-variable sym))
((fboundp sym)
(find-function sym))
+ ((boundp sym)
+ (find-variable sym))
((or (featurep sym)
(locate-library
(prin1-to-string sym)))
:sort t
:action (lambda (x)
(describe-variable
- (intern x))))))
+ (intern x)))
+ :caller 'counsel-describe-variable)))
(ivy-set-actions
'counsel-describe-variable
'(("i" counsel-info-lookup-symbol "info")
("d" counsel--find-symbol "definition")))
+(ivy-set-actions
+ 'counsel-M-x
+ '(("d" counsel--find-symbol "definition")))
+
;;;###autoload
(defun counsel-describe-function ()
"Forward to `describe-function'."
:sort t
:action (lambda (x)
(describe-function
- (intern x))))))
+ (intern x)))
+ :caller 'counsel-describe-function)))
(defvar info-lookup-mode)
(declare-function info-lookup->completions "info-look")
(require 'info-look)
(info-lookup 'symbol symbol mode))
+(defvar counsel-unicode-char-history nil
+ "History for `counsel-unicode-char'.")
+
;;;###autoload
(defun counsel-unicode-char ()
"Insert a Unicode character at point."
(interactive)
(let ((minibuffer-allow-text-properties t))
+ (setq counsel-completion-beg (point))
+ (setq counsel-completion-end (point))
(ivy-read "Unicode name: "
(mapcar (lambda (x)
(propertize
'result (cdr x)))
(ucs-names))
:action (lambda (char)
- (insert-char (get-text-property 0 'result char))))))
+ (with-ivy-window
+ (delete-region counsel-completion-beg counsel-completion-end)
+ (setq counsel-completion-beg (point))
+ (insert-char (get-text-property 0 'result char))
+ (setq counsel-completion-end (point))))
+ :history 'counsel-unicode-char-history)))
(declare-function cider-sync-request:complete "ext:cider-client")
;;;###autoload
"git ls-files --full-name --")
"\n"
t))
- (action (lambda (x) (find-file x))))
+ (action `(lambda (x)
+ (let ((default-directory ,default-directory))
+ (find-file x)))))
(ivy-read "Find file: " cands
:action action)))
(list ""
(format "%d chars more" (- n (length ivy-text)))))
+(defvar counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S"
+ "Store the command for `counsel-git-grep'.")
+
(defun counsel-git-grep-function (string &optional _pred &rest _unused)
"Grep in the current git repository for STRING."
(if (and (> counsel--git-grep-count 20000)
(< (length string) 3))
(counsel-more-chars 3)
(let* ((default-directory counsel--git-grep-dir)
- (cmd (format "git --no-pager grep --full-name -n --no-color -i -e %S"
+ (cmd (format counsel-git-grep-cmd
(setq ivy--old-re (ivy--regex string t)))))
(if (<= counsel--git-grep-count 20000)
(split-string (shell-command-to-string cmd) "\n" t)
(defvar counsel-git-grep-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-l") 'counsel-git-grep-recenter)
+ (define-key map (kbd "M-q") 'counsel-git-grep-query-replace)
map))
+(defun counsel-git-grep-query-replace ()
+ "Start `query-replace' with string to replace from last search string."
+ (interactive)
+ (if (null (window-minibuffer-p))
+ (user-error
+ "Should only be called in the minibuffer through `counsel-git-grep-map'")
+ (let* ((enable-recursive-minibuffers t)
+ (from (ivy--regex ivy-text))
+ (to (query-replace-read-to from "Query replace" t)))
+ (ivy-set-action
+ (lambda (_)
+ (let (done-buffers)
+ (dolist (cand ivy--old-cands)
+ (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" cand)
+ (with-ivy-window
+ (let ((file-name (match-string-no-properties 1 cand)))
+ (setq file-name (expand-file-name file-name counsel--git-grep-dir))
+ (unless (member file-name done-buffers)
+ (push file-name done-buffers)
+ (find-file file-name)
+ (goto-char (point-min)))
+ (perform-replace from to t t nil))))))))
+ (setq ivy-exit 'done)
+ (exit-minibuffer))))
+
(defun counsel-git-grep-recenter ()
(interactive)
(with-ivy-window
(defvar counsel-git-grep-history nil
"History for `counsel-git-grep'.")
+(defvar counsel-git-grep-cmd-history
+ '("git --no-pager grep --full-name -n --no-color -i -e %S")
+ "History for `counsel-git-grep' shell commands.")
+
;;;###autoload
-(defun counsel-git-grep (&optional initial-input)
+(defun counsel-git-grep (&optional cmd initial-input)
"Grep for a string in the current git repository.
+When CMD is a string, use it as a \"git grep\" command.
+When CMD is non-nil, prompt for a specific \"git grep\" command.
INITIAL-INPUT can be given as the initial minibuffer input."
- (interactive)
+ (interactive "P")
+ (cond
+ ((stringp cmd)
+ (setq counsel-git-grep-cmd cmd))
+ (cmd
+ (setq counsel-git-grep-cmd
+ (ivy-read "cmd: " counsel-git-grep-cmd-history
+ :history 'counsel-git-grep-cmd-history))
+ (setq counsel-git-grep-cmd-history
+ (delete-dups counsel-git-grep-cmd-history)))
+ (t
+ (setq counsel-git-grep-cmd "git --no-pager grep --full-name -n --no-color -i -e %S")))
(setq counsel--git-grep-dir
(locate-dominating-file default-directory ".git"))
(if (null counsel--git-grep-dir)
:keymap counsel-git-grep-map
:action #'counsel-git-grep-action
:unwind #'swiper--cleanup
- :history 'counsel-git-grep-history)))
+ :history 'counsel-git-grep-history
+ :caller 'counsel-git-grep)))
(defcustom counsel-find-file-at-point nil
"When non-nil, add file-at-point to the list of candidates."
candidates))
(setq ivy--old-re regexp))))
+(defvar counsel--async-time nil
+ "Store the time when a new process was started.
+Or the time of the last minibuffer update.")
+
(defun counsel--async-command (cmd)
(let* ((counsel--process " *counsel*")
(proc (get-process counsel--process))
counsel--process
counsel--process
cmd))
- (set-process-sentinel proc #'counsel--async-sentinel)))
+ (setq counsel--async-time (current-time))
+ (set-process-sentinel proc #'counsel--async-sentinel)
+ (set-process-filter proc #'counsel--async-filter)))
(defun counsel--async-sentinel (process event)
(if (string= event "finished\n")
(setq ivy--old-cands ivy--all-candidates)
(ivy--exhibit)))))
+(defun counsel--async-filter (process str)
+ "Receive from PROCESS the output STR.
+Update the minibuffer with the amount of lines collected every
+0.5 seconds since the last update."
+ (with-current-buffer (process-buffer process)
+ (insert str))
+ (let (size)
+ (when (time-less-p
+ ;; 0.5s
+ '(0 0 500000 0)
+ (time-since counsel--async-time))
+ (with-current-buffer (process-buffer process)
+ (goto-char (point-min))
+ (setq size (- (buffer-size) (forward-line (buffer-size)))))
+ (ivy--insert-minibuffer
+ (format "\ncollected: %d" size))
+ (setq counsel--async-time (current-time)))))
+
(defun counsel-locate-action-extern (x)
"Use xdg-open shell command on X."
(call-process shell-file-name nil
(ivy--regex str))))
'("" "working...")))
+(defun counsel-delete-process ()
+ (let ((process (get-process " *counsel*")))
+ (when process
+ (delete-process process))))
+
;;;###autoload
(defun counsel-locate ()
"Call locate shell command."
:dynamic-collection t
:history 'counsel-locate-history
:action (lambda (file)
- (when file
- (find-file file)))))
+ (with-ivy-window
+ (when file
+ (find-file file))))
+ :unwind #'counsel-delete-process))
(defun counsel--generic (completion-fn)
"Complete thing at point with COMPLETION-FN."
(setq proc (start-process-shell-command
counsel-gg-process
counsel-gg-process
- (format "git --no-pager grep --full-name -n --no-color -i -e %S | head -n 200"
- regex)))
+ (concat
+ (format counsel-git-grep-cmd regex)
+ " | head -n 200")))
(set-process-sentinel
proc
#'counsel--gg-sentinel)))
(if (string= event "finished\n")
(progn
(with-current-buffer (process-buffer process)
- (setq ivy--all-candidates (split-string (buffer-string) "\n" t))
+ (setq ivy--all-candidates
+ (or (split-string (buffer-string) "\n" t)
+ '("")))
(setq ivy--old-cands ivy--all-candidates))
(when (= 0 (cl-incf counsel-gg-state))
(ivy--exhibit)))
"Quickly and asynchronously count the amount of git grep REGEX matches.
When NO-ASYNC is non-nil, do it synchronously."
(let ((default-directory counsel--git-grep-dir)
- (cmd (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
- regex))
+ (cmd
+ (concat
+ (format
+ (replace-regexp-in-string
+ "--full-name" "-c"
+ counsel-git-grep-cmd)
+ ;; "git grep -i -c '%s'"
+ (replace-regexp-in-string
+ "-" "\\\\-"
+ (replace-regexp-in-string "'" "''" regex)))
+ " | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"))
(counsel-ggc-process " *counsel-gg-count*"))
(if no-async
(string-to-number (shell-command-to-string cmd))
(declare-function smex-detect-new-commands "ext:smex")
(declare-function smex-update "ext:smex")
(declare-function smex-rank "ext:smex")
-(declare-function package-installed-p "package")
+
+(defun counsel--M-x-prompt ()
+ "M-x plus the string representation of `current-prefix-arg'."
+ (if (not current-prefix-arg)
+ "M-x "
+ (concat
+ (if (eq current-prefix-arg '-)
+ "- "
+ (if (integerp current-prefix-arg)
+ (format "%d " current-prefix-arg)
+ (if (= (car current-prefix-arg) 4)
+ "C-u "
+ (format "%d " (car current-prefix-arg)))))
+ "M-x ")))
;;;###autoload
(defun counsel-M-x (&optional initial-input)
(cands obarray)
(pred 'commandp)
(sort t))
- (when (or (featurep 'smex)
- (package-installed-p 'smex))
- (require 'smex)
+ (when (require 'smex nil 'noerror)
(unless smex-initialized-p
(smex-initialize))
(smex-detect-new-commands)
(setq cands smex-ido-cache)
(setq pred nil)
(setq sort nil))
- (ivy-read "M-x " cands
+ (ivy-read (counsel--M-x-prompt) cands
:predicate pred
:require-match t
:history 'extended-command-history
:action
'(1
("p" helm-rhythmbox-play-song "Play song")
- ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))))
+ ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))
+ :caller 'counsel-rhythmbox))
(defvar counsel-org-tags nil
"Store the current list of tags.")
"Grep in the current directory for STRING."
(if (< (length string) 3)
(counsel-more-chars 3)
- (let ((regex (counsel-unquote-regex-parens (ivy--regex string))))
+ (let ((default-directory counsel--git-grep-dir)
+ (regex (counsel-unquote-regex-parens
+ (setq ivy--old-re
+ (ivy--regex string)))))
(counsel--async-command
(format "ag --noheading --nocolor %S" regex))
nil)))
-(defun counsel-ag (&optional initial-input)
+(defun counsel-ag (&optional initial-input initial-directory)
"Grep for a string in the current directory using ag.
INITIAL-INPUT can be given as the initial minibuffer input."
(interactive)
- (setq counsel--git-grep-dir default-directory)
+ (setq counsel--git-grep-dir (or initial-directory default-directory))
(ivy-read "ag: " 'counsel-ag-function
:initial-input initial-input
:dynamic-collection t
:history 'counsel-git-grep-history
:action #'counsel-git-grep-action
- :unwind #'swiper--cleanup))
+ :unwind (lambda ()
+ (counsel-delete-process)
+ (swiper--cleanup))))
(defun counsel-recoll-function (string &optional _pred &rest _unused)
"Grep in the current directory for STRING."
(unless (string-match "pdf$" x)
(swiper ivy-text)))))))
+(defcustom counsel-yank-pop-truncate nil
+ "When non-nil, truncate the display of long strings."
+ :group 'ivy)
+
+;;;###autoload
+(defun counsel-yank-pop ()
+ "Ivy replacement for `yank-pop'."
+ (interactive)
+ (if (eq last-command 'yank)
+ (progn
+ (setq counsel-completion-end (point))
+ (setq counsel-completion-beg
+ (save-excursion
+ (search-backward (car kill-ring))
+ (point))))
+ (setq counsel-completion-beg (point))
+ (setq counsel-completion-end (point)))
+ (let ((candidates (cl-remove-if
+ (lambda (s)
+ (or (< (length s) 3)
+ (string-match "\\`[\n[:blank:]]+\\'" s)))
+ (delete-dups kill-ring))))
+ (when counsel-yank-pop-truncate
+ (setq candidates
+ (mapcar (lambda (s)
+ (if (string-match "\\`\\(.*\n.*\n.*\n.*\\)\n" s)
+ (progn
+ (let ((s (copy-sequence s)))
+ (put-text-property
+ (match-end 1)
+ (length s)
+ 'display
+ " [...]"
+ s)
+ s))
+ s))
+ candidates)))
+ (ivy-read "kill-ring: " candidates
+ :action 'counsel-yank-pop-action)))
+
+(defun counsel-yank-pop-action (s)
+ "Insert S into the buffer, overwriting the previous yank."
+ (with-ivy-window
+ (delete-region counsel-completion-beg
+ counsel-completion-end)
+ (insert (substring-no-properties s))
+ (setq counsel-completion-end (point))))
+
(provide 'counsel)
;;; counsel.el ends here