branch: master commit 7054901e35b28c08df86316f712f667467a6b504 Author: Oleh Krehel <ohwoeo...@gmail.com> Commit: Oleh Krehel <ohwoeo...@gmail.com>
ivy.el (ivy-occur-revert-buffer): Make more generic * ivy.el (ivy-occur): Call the occur function with ivy--old-cands. (ivy-occur-revert-buffer): Call the occur funciton with nil. (ivy-switch-buffer-occur): In case nil was given, rescan the buffer list for candidates. (ivy--occur-default): Extract. The current null-cands logic is a bit pointless, since if it was a list there's no way it changed; and if it was "obarray", it's the old version of it, also unchanged. Maybe rewrite functions that have the "obarray" as collection to receive "'obarray" instead. So that we can revert using the new version of "obarray". --- counsel.el | 12 ++++++------ ivy.el | 48 ++++++++++++++++++++++++++++-------------------- swiper.el | 34 +++++++++++++++++----------------- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/counsel.el b/counsel.el index 4164b05..7e12787 100644 --- a/counsel.el +++ b/counsel.el @@ -1269,7 +1269,7 @@ INITIAL-INPUT can be given as the initial minibuffer input." (let ((default-directory (ivy-state-directory ivy-last))) (find-file x)))) -(defun counsel-git-occur () +(defun counsel-git-occur (&optional _cands) "Occur function for `counsel-git' using `counsel-cmd-to-dired'." (cd (ivy-state-directory ivy-last)) (counsel-cmd-to-dired @@ -1556,7 +1556,7 @@ When CMD is non-nil, prompt for a specific \"git grep\" command." " ")))) (concat (format counsel-git-grep-cmd positive-pattern) negative-patterns))) -(defun counsel-git-grep-occur () +(defun counsel-git-grep-occur (&optional _cands) "Generate a custom occur buffer for `counsel-git-grep'." (counsel-grep-like-occur #'counsel--git-grep-occur-cmd)) @@ -2033,7 +2033,7 @@ If USE-IGNORE is non-nil, try to generate a command that respects " | grep" (concat " -type " type exclude-dots " | grep") cmd))) -(defun counsel-find-file-occur () +(defun counsel-find-file-occur (&optional _cands) (require 'find-dired) (cd ivy--directory) (if counsel-find-file-occur-use-find @@ -2558,7 +2558,7 @@ FZF-PROMPT, if non-nil, is passed as `ivy-read' prompt argument." (let ((default-directory counsel--fzf-dir)) (find-file x)))) -(defun counsel-fzf-occur () +(defun counsel-fzf-occur (&optional _cands) "Occur function for `counsel-fzf' using `counsel-cmd-to-dired'." (cd counsel--fzf-dir) (counsel-cmd-to-dired @@ -2864,7 +2864,7 @@ Works for `counsel-git-grep', `counsel-ag', etc." (cands (counsel--split-string (shell-command-to-string cmd)))) (swiper--occur-insert-lines (mapcar #'counsel--normalize-grep-match cands)))) -(defun counsel-ag-occur () +(defun counsel-ag-occur (&optional _cands) "Generate a custom occur buffer for `counsel-ag'." (counsel-grep-like-occur counsel-ag-command)) @@ -3035,7 +3035,7 @@ relative to the last position stored here.") (line-end-position)) (swiper--add-overlays (ivy--regex ivy-text)))))))) -(defun counsel-grep-occur () +(defun counsel-grep-occur (&optional _cands) "Generate a custom occur buffer for `counsel-grep'." (counsel-grep-like-occur (format diff --git a/ivy.el b/ivy.el index bdc3e2e..332b394 100644 --- a/ivy.el +++ b/ivy.el @@ -4330,15 +4330,18 @@ Skip buffers that match `ivy-ignore-buffers'." (t str)) str))) -(defun ivy-switch-buffer-occur () - "Occur function for `ivy-switch-buffer' using `ibuffer'." +(defun ivy-switch-buffer-occur (cands) + "Occur function for `ivy-switch-buffer' using `ibuffer'. +CANDS are the candidates to be displayed." + (unless cands + (setq cands (all-completions ivy-text #'internal-complete-buffer))) (ibuffer nil (buffer-name) `((or ,@(cl-mapcan (lambda (cand) (unless (eq (get-text-property 0 'face cand) 'ivy-virtual) `((name . ,(format "\\_<%s\\_>" (regexp-quote cand)))))) - ivy--old-cands))))) + cands))))) ;;;###autoload (defun ivy-switch-buffer () @@ -4759,6 +4762,18 @@ When `ivy-calling' isn't nil, call `ivy-occur-press'." (insert (if (string-match-p "\\`.[/\\]" cand) "" " ") cand ?\n))) +(defun ivy--occur-default (cands) + "Insert CANDS into the current occur buffer." + (unless cands + (let ((coll (ivy-state-collection ivy-last))) + (when (arrayp coll) + (setq coll (all-completions "" coll (ivy-state-predicate ivy-last)))) + (setq cands (ivy--filter (ivy-state-text ivy-last) coll)))) + (ivy-occur-mode) + (insert (format "%d candidates:\n" (length cands))) + (ivy--occur-insert-lines cands) + (read-only-mode)) + (defun ivy-occur () "Stop completion and put the current candidates into a new buffer. @@ -4772,7 +4787,8 @@ There is no limit on the number of *ivy-occur* buffers." (if (not (window-minibuffer-p)) (user-error "No completion session is active") (let* ((caller (ivy-state-caller ivy-last)) - (occur-fn (plist-get ivy--occurs-list caller)) + (occur-fn (or (plist-get ivy--occurs-list caller) + #'ivy--occur-default)) (buffer (generate-new-buffer (format "*ivy-occur%s \"%s\"*" @@ -4781,15 +4797,7 @@ There is no limit on the number of *ivy-occur* buffers." "") ivy-text)))) (with-current-buffer buffer - (let ((inhibit-read-only t)) - (erase-buffer) - (if occur-fn - (funcall occur-fn) - (ivy-occur-mode) - (insert (format "%d candidates:\n" (length ivy--old-cands))) - (read-only-mode) - (ivy--occur-insert-lines - ivy--old-cands))) + (funcall occur-fn ivy--old-cands) (setf (ivy-state-text ivy-last) ivy-text) (setq ivy-occur-last ivy-last)) (ivy-exit-with-action @@ -4810,13 +4818,13 @@ updated original buffer." (interactive) (let ((caller (ivy-state-caller ivy-occur-last)) (ivy-last ivy-occur-last)) - (when (memq caller (append '(swiper swiper-isearch) ivy-highlight-grep-commands)) - (let ((inhibit-read-only t) - (line (line-number-at-pos))) - (erase-buffer) - (funcall (plist-get ivy--occurs-list caller)) - (goto-char (point-min)) - (forward-line (1- line)))) + (let ((inhibit-read-only t) + (line (line-number-at-pos))) + (erase-buffer) + (funcall (or (plist-get ivy--occurs-list caller) + #'ivy--occur-default) nil) + (goto-char (point-min)) + (forward-line (1- line))) (setq ivy-occur-last ivy-last))) (declare-function wgrep-change-to-wgrep-mode "ext:wgrep") diff --git a/swiper.el b/swiper.el index a32eaa7..654dc31 100644 --- a/swiper.el +++ b/swiper.el @@ -631,13 +631,14 @@ When non-nil, INITIAL-INPUT is the initial search pattern." (nreverse res))) (defun swiper--occur-insert-lines (cands) - ;; Need precise number of header lines for `wgrep' to work. - (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n" - default-directory)) - (insert (format "%d candidates:\n" (length cands))) - (ivy--occur-insert-lines cands) - (goto-char (point-min)) - (forward-line 4)) + (let ((inhibit-read-only t)) + ;; Need precise number of header lines for `wgrep' to work. + (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n" + default-directory)) + (insert (format "%d candidates:\n" (length cands))) + (ivy--occur-insert-lines cands) + (goto-char (point-min)) + (forward-line 4))) (defun swiper--occur-buffer () (let ((buffer (ivy-state-buffer ivy-last))) @@ -651,7 +652,7 @@ When non-nil, INITIAL-INPUT is the initial search pattern." (setf (ivy-state-window ivy-last) (selected-window))) buffer)) -(defun swiper-occur () +(defun swiper-occur (&optional cands) "Generate a custom occur buffer for `swiper'. When capture groups are present in the input, print them instead of lines." (let* ((buffer (swiper--occur-buffer)) @@ -669,15 +670,14 @@ When capture groups are present in the input, print them instead of lines." (cands (swiper--occur-cands fname - (if (not (eq this-command 'ivy-occur-revert-buffer)) - ivy--old-cands - (setq ivy--old-re nil) - (save-window-excursion - (switch-to-buffer buffer) - (if (eq (ivy-state-caller ivy-last) 'swiper) - (let ((ivy--regex-function 'swiper--re-builder)) - (ivy--filter re (swiper--candidates))) - (swiper-isearch-function ivy-text))))))) + (or cands + (save-window-excursion + (setq ivy--old-re nil) + (switch-to-buffer buffer) + (if (eq (ivy-state-caller ivy-last) 'swiper) + (let ((ivy--regex-function 'swiper--re-builder)) + (ivy--filter re (swiper--candidates))) + (swiper-isearch-function ivy-text))))))) (if (string-match-p "\\\\(" re) (insert (mapconcat #'identity