]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/swiper/swiper.el
Merge commit '6abd4f4fe740054d433d928d90fb1671cce6719c'
[gnu-emacs-elpa] / packages / swiper / swiper.el
index 2f95a312c43cd7dc629be2579dd08425a813c4c8..1032f04869eb77423f69b4145254f37c7d1dddd6 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
 ;; URL: https://github.com/abo-abo/swiper
-;; Version: 0.4.1
+;; Version: 0.5.1
 ;; Package-Requires: ((emacs "24.1"))
 ;; Keywords: matching
 
@@ -83,9 +83,6 @@
     map)
   "Keymap for swiper.")
 
-(defvar swiper--window nil
-  "Store the current window.")
-
 (defun swiper-query-replace ()
   "Start `query-replace' with string to replace from last search string."
   (interactive)
            (to (query-replace-read-to from "Query replace" t)))
       (delete-minibuffer-contents)
       (ivy-set-action (lambda (_)
-                        (with-selected-window swiper--window
+                        (with-ivy-window
+                          (move-beginning-of-line 1)
                           (perform-replace from to
                                            t t nil))))
       (swiper--cleanup)
       (exit-minibuffer))))
 
 (defvar avy-background)
+(defvar avy-all-windows)
 (declare-function avy--regex-candidates "ext:avy")
 (declare-function avy--process "ext:avy")
 (declare-function avy--overlay-post "ext:avy")
-(declare-function avy--goto "ext:avy")
+(declare-function avy-action-goto "ext:avy")
 
 ;;;###autoload
 (defun swiper-avy ()
   "Jump to one of the current swiper candidates."
   (interactive)
-  (with-selected-window (ivy-state-window ivy-last)
-    (let* ((candidates
-            (avy--regex-candidates
-             (ivy--regex ivy-text)))
-           (avy-background nil)
-           (candidate
-            (avy--process candidates #'avy--overlay-post)))
-      (ivy-quit-and-run
-       (avy--goto candidate)))))
+  (unless (string= ivy-text "")
+    (with-ivy-window
+      (let* ((avy-all-windows nil)
+             (candidates
+              (avy--regex-candidates
+               (ivy--regex ivy-text)))
+             (avy-background nil)
+             (candidate
+              (avy--process candidates #'avy--overlay-post)))
+        (ivy-quit-and-run
+         (avy-action-goto candidate))))))
 
 (defun swiper-recenter-top-bottom (&optional arg)
-  "Call (`recenter-top-bottom' ARG) in `swiper--window'."
+  "Call (`recenter-top-bottom' ARG)."
   (interactive "P")
-  (with-selected-window swiper--window
+  (with-ivy-window
     (recenter-top-bottom arg)))
 
 (defun swiper-font-lock-ensure ()
   "Ensure the entired buffer is highlighted."
   (unless (or (derived-mode-p 'magit-mode)
+              (bound-and-true-p magit-blame-mode)
               (memq major-mode '(package-menu-mode
                                  gnus-summary-mode
                                  gnus-article-mode
                                  dired-mode
                                  jabber-chat-mode
                                  elfeed-search-mode
-                                 fundamental-mode)))
+                                 fundamental-mode
+                                 Man-mode
+                                 woman-mode)))
     (unless (> (buffer-size) 100000)
       (if (fboundp 'font-lock-ensure)
           (font-lock-ensure)
@@ -192,11 +196,9 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
 
 (defun swiper--init ()
   "Perform initialization common to both completion methods."
-  (deactivate-mark)
   (setq swiper--opoint (point))
   (setq swiper--len 0)
-  (setq swiper--anchor (line-number-at-pos))
-  (setq swiper--window (selected-window)))
+  (setq swiper--anchor (line-number-at-pos)))
 
 (defun swiper--re-builder (str)
   "Transform STR into a swiper regex.
@@ -219,6 +221,9 @@ there have line numbers. In the buffer, `ivy--regex' should be used."
     (t
      (ivy--regex-plus str))))
 
+(defvar swiper-history nil
+  "History for `swiper'.")
+
 (defun swiper--ivy (&optional initial-input)
   "`isearch' with an overview using `ivy'.
 When non-nil, INITIAL-INPUT is the initial search pattern."
@@ -231,15 +236,13 @@ Please remove it and update the \"swiper\" package."))
         (preselect (format
                     swiper--format-spec
                     (line-number-at-pos)
-                    (regexp-quote
-                     (buffer-substring-no-properties
-                      (line-beginning-position)
-                      (line-end-position)))))
+                    (buffer-substring-no-properties
+                     (line-beginning-position)
+                     (line-end-position))))
         res)
     (unwind-protect
          (setq res (ivy-read
-                    (replace-regexp-in-string
-                     "%s" "pattern: " swiper--format-spec)
+                    "Swiper: "
                     candidates
                     :initial-input initial-input
                     :keymap swiper-map
@@ -247,7 +250,8 @@ Please remove it and update the \"swiper\" package."))
                     :require-match t
                     :update-fn #'swiper--update-input-ivy
                     :unwind #'swiper--cleanup
-                    :re-builder #'swiper--re-builder))
+                    :re-builder #'swiper--re-builder
+                    :history 'swiper-history))
       (if (null ivy-exit)
           (goto-char swiper--opoint)
         (swiper--action res ivy-text)))))
@@ -274,21 +278,26 @@ Please remove it and update the \"swiper\" package."))
 
 (defun swiper--update-input-ivy ()
   "Called when `ivy' input is updated."
-  (swiper--cleanup)
-  (let* ((re (ivy--regex ivy-text))
-         (str ivy--current)
-         (num (if (string-match "^[0-9]+" str)
-                  (string-to-number (match-string 0 str))
-                0)))
-    (with-selected-window swiper--window
+  (with-ivy-window
+    (swiper--cleanup)
+    (let* ((re (ivy--regex ivy-text))
+           (str ivy--current)
+           (num (if (string-match "^[0-9]+" str)
+                    (string-to-number (match-string 0 str))
+                  0)))
       (goto-char (point-min))
       (when (cl-plusp num)
         (goto-char (point-min))
         (forward-line (1- num))
+        (if (and (equal ivy-text "")
+                 (>= swiper--opoint (line-beginning-position))
+                 (<= swiper--opoint (line-end-position)))
+            (goto-char swiper--opoint)
+          (re-search-forward re (line-end-position) t))
         (isearch-range-invisible (line-beginning-position)
                                  (line-end-position))
         (unless (and (>= (point) (window-start))
-                     (<= (point) (window-end swiper--window t)))
+                     (<= (point) (window-end (ivy-state-window ivy-last) t)))
           (recenter)))
       (swiper--add-overlays re))))
 
@@ -299,39 +308,39 @@ BEG and END, when specified, are the point bounds."
              (line-beginning-position)
              (1+ (line-end-position)))))
     (overlay-put ov 'face 'swiper-line-face)
-    (overlay-put ov 'window swiper--window)
-    (push ov swiper--overlays))
-  (let* ((wh (window-height))
-         (beg (or beg (save-excursion
-                        (forward-line (- wh))
-                        (point))))
-         (end (or end (save-excursion
-                        (forward-line wh)
-                        (point)))))
-    (when (>= (length re) swiper-min-highlight)
-      (save-excursion
-        (goto-char beg)
-        ;; RE can become an invalid regexp
-        (while (and (ignore-errors (re-search-forward re end t))
-                    (> (- (match-end 0) (match-beginning 0)) 0))
-          (let ((i 0))
-            (while (<= i ivy--subexps)
-              (when (match-beginning i)
-                (let ((overlay (make-overlay (match-beginning i)
-                                             (match-end i)))
-                      (face
-                       (cond ((zerop ivy--subexps)
-                              (cadr swiper-faces))
-                             ((zerop i)
-                              (car swiper-faces))
-                             (t
-                              (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
-                                   swiper-faces)))))
-                  (push overlay swiper--overlays)
-                  (overlay-put overlay 'face face)
-                  (overlay-put overlay 'window swiper--window)
-                  (overlay-put overlay 'priority i)))
-              (cl-incf i))))))))
+    (overlay-put ov 'window (ivy-state-window ivy-last))
+    (push ov swiper--overlays)
+    (let* ((wh (window-height))
+           (beg (or beg (save-excursion
+                          (forward-line (- wh))
+                          (point))))
+           (end (or end (save-excursion
+                          (forward-line wh)
+                          (point)))))
+      (when (>= (length re) swiper-min-highlight)
+        (save-excursion
+          (goto-char beg)
+          ;; RE can become an invalid regexp
+          (while (and (ignore-errors (re-search-forward re end t))
+                      (> (- (match-end 0) (match-beginning 0)) 0))
+            (let ((i 0))
+              (while (<= i ivy--subexps)
+                (when (match-beginning i)
+                  (let ((overlay (make-overlay (match-beginning i)
+                                               (match-end i)))
+                        (face
+                         (cond ((zerop ivy--subexps)
+                                (cadr swiper-faces))
+                               ((zerop i)
+                                (car swiper-faces))
+                               (t
+                                (nth (1+ (mod (+ i 2) (1- (length swiper-faces))))
+                                     swiper-faces)))))
+                    (push overlay swiper--overlays)
+                    (overlay-put overlay 'face face)
+                    (overlay-put overlay 'window (ivy-state-window ivy-last))
+                    (overlay-put overlay 'priority i)))
+                (cl-incf i)))))))))
 
 (defun swiper--action (x input)
   "Goto line X and search for INPUT."
@@ -347,6 +356,16 @@ BEG and END, when specified, are the point bounds."
         (push-mark swiper--opoint t)
         (message "Mark saved where search started")))))
 
+;; (define-key isearch-mode-map (kbd "C-o") 'swiper-from-isearch)
+(defun swiper-from-isearch ()
+  "Invoke `swiper' from isearch."
+  (interactive)
+  (let ((query (if isearch-regexp
+                   isearch-string
+                 (regexp-quote isearch-string))))
+    (isearch-exit)
+    (swiper query)))
+
 (provide 'swiper)
 
 ;;; swiper.el ends here