]> code.delx.au - gnu-emacs-elpa/commitdiff
New command ivy-occur-revert-buffer bound to "g"
authorOleh Krehel <ohwoeowho@gmail.com>
Fri, 4 Mar 2016 13:58:50 +0000 (14:58 +0100)
committerOleh Krehel <ohwoeowho@gmail.com>
Fri, 4 Mar 2016 14:11:28 +0000 (15:11 +0100)
* 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
swiper.el

diff --git a/ivy.el b/ivy.el
index 60c20a6cdb81687c162584ed5c520bb68f7c990f..cd9a7b593d5494bc677ad04a0ff5d5c1c1eff9ae 100644 (file)
--- 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)
index be73d6f74613383b5aadb9b0b0c2237532d6ca87..bdf38197f3e418a764e89624949ed3acbc175fcb 100644 (file)
--- 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")