branch: master commit 20f11b7af72a4e09f5abffa189ac1398ac2a5429 Author: Oleh Krehel <ohwoeo...@gmail.com> Commit: Oleh Krehel <ohwoeo...@gmail.com>
New command ivy-occur-revert-buffer bound to "g" * ivy.el (ivy-occur-mode-map): Move `ivy-occur-press' from "g" to "f". Bind "g" to `ivy-occur-revert-buffer'. (ivy-occur-revert-buffer): New command. Does what e.g. `revert-buffer' does for *Help* buffers. * swiper.el (swiper-faces): Add :group and :type. (swiper-occur): Try to avoid calling `ivy-occur-grep-mode' as it will kill all local variables. (swiper--action): Update so that `ivy-occur-toggle-calling' works properly. --- ivy.el | 30 +++++++++++++++++++++++++++++- swiper.el | 19 +++++++++++-------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/ivy.el b/ivy.el index 60c20a6..cd9a7b5 100644 --- a/ivy.el +++ b/ivy.el @@ -2695,7 +2695,8 @@ buffer would modify `ivy-last'.") (define-key map (kbd "k") 'ivy-occur-previous-line) (define-key map (kbd "h") 'backward-char) (define-key map (kbd "l") 'forward-char) - (define-key map (kbd "g") 'ivy-occur-press) + (define-key map (kbd "f") 'ivy-occur-press) + (define-key map (kbd "g") 'ivy-occur-revert-buffer) (define-key map (kbd "a") 'ivy-occur-read-action) (define-key map (kbd "o") 'ivy-occur-dispatch) (define-key map (kbd "c") 'ivy-occur-toggle-calling) @@ -2703,6 +2704,33 @@ buffer would modify `ivy-last'.") map) "Keymap for Ivy Occur mode.") +(defun ivy-occur-revert-buffer () + "Refresh the buffer making it up-to date with the collection. + +Currently only works for `swiper'. In that specific case, the +*ivy-occur* buffer becomes nearly useless as the orignal buffer +is updated, since the line numbers no longer match. + +Calling this function is as if you called `ivy-occur' on the +updated original buffer." + (interactive) + (let ((caller (ivy-state-caller ivy-occur-last)) + (text (progn (string-match "\"\\(.*\\)\"" (buffer-name)) + (match-string 1 (buffer-name)))) + (ivy-last ivy-occur-last)) + (when (eq caller 'swiper) + (let ((buffer (ivy-state-buffer ivy-occur-last))) + (unless (buffer-live-p buffer) + (error "buffer was killed")) + (with-current-buffer buffer + (setq ivy--old-re nil) + (setq ivy--old-cands (ivy--filter text (swiper--candidates)))) + (let ((inhibit-read-only t)) + (erase-buffer) + (swiper-occur) + (goto-char (point-min)) + (forward-line 4)))))) + (defun ivy-occur-toggle-calling () "Toggle `ivy-calling'." (interactive) diff --git a/swiper.el b/swiper.el index be73d6f..bdf3819 100644 --- a/swiper.el +++ b/swiper.el @@ -69,7 +69,9 @@ swiper-match-face-2 swiper-match-face-3 swiper-match-face-4) - "List of `swiper' faces for group matches.") + "List of `swiper' faces for group matches." + :group 'ivy-faces + :type 'list) (defcustom swiper-min-highlight 2 "Only highlight matches for regexps at least this long." @@ -316,12 +318,11 @@ When non-nil, INITIAL-INPUT is the initial search pattern." (defun swiper-occur () "Generate a custom occur buffer for `swiper'." - (ivy-occur-grep-mode) - (font-lock-mode -1) (let* ((fname (propertize (with-ivy-window (file-name-nondirectory - (buffer-file-name))) + (buffer-file-name + (ivy-state-buffer ivy-last)))) 'face 'compilation-info)) (cands (mapcar @@ -334,6 +335,9 @@ When non-nil, INITIAL-INPUT is the initial search pattern." 'face 'compilation-line-number) (substring s 1))) ivy--old-cands))) + (unless (eq major-mode 'ivy-occur-grep-mode) + (ivy-occur-grep-mode) + (font-lock-mode -1)) (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n" default-directory)) (insert (format "%d candidates:\n" (length cands))) @@ -559,10 +563,9 @@ WND, when specified is the window." (defun swiper--action (x) "Goto line X." - (let ((ln (1- (read (if (memq this-command '(ivy-occur-press)) - (when (string-match ":\\([0-9]+\\):.*\\'" x) - (match-string-no-properties 1 x)) - (get-text-property 0 'display x))))) + (let ((ln (1- (read (or (get-text-property 0 'display x) + (and (string-match ":\\([0-9]+\\):.*\\'" x) + (match-string-no-properties 1 x)))))) (re (ivy--regex ivy-text))) (if (null x) (user-error "No candidates")