branch: master
commit 09da54380f590b86309d81c64138e9619767a57a
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
swiper.el: Ease the creation of swiper-in-region functions
* swiper.el (swiper--point-min): New defvar.
(swiper--point-max): New defvar.
(swiper--init): Set the new vars.
(swiper--update-input-ivy): Use the new vars.
(swiper--action): Use the new var.
Example - swipe only in the current Org subtree:
(defun worf-occur ()
(interactive)
(let* ((bnd (worf--bounds-subtree))
(candidates
(save-restriction
(narrow-to-region (car bnd) (cdr bnd))
(swiper--init)
(swiper--candidates))))
(ivy-read "pattern: " candidates
:require-match t
:update-fn #'swiper--update-input-ivy
:action #'swiper--action
:unwind #'swiper--cleanup
:caller 'worf-occur)))
Here, `save-restriction' is used to select only the candidates in the
subtree. The actualy restriction is never shown, it's more seamless than
`org-narrow-to-subtree' followed by `swiper'.
---
swiper.el | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/swiper.el b/swiper.el
index 1e03445..b5ad76b 100644
--- a/swiper.el
+++ b/swiper.el
@@ -411,6 +411,8 @@ When REVERT is non-nil, regenerate the current *ivy-occur*
buffer."
(defvar swiper--current-line nil)
(defvar swiper--current-match-start nil)
+(defvar swiper--point-min nil)
+(defvar swiper--point-max nil)
(defun swiper--init ()
"Perform initialization common to both completion methods."
@@ -418,6 +420,8 @@ When REVERT is non-nil, regenerate the current *ivy-occur*
buffer."
(setq swiper--current-match-start nil)
(setq swiper--current-window-start nil)
(setq swiper--opoint (point))
+ (setq swiper--point-min (point-min))
+ (setq swiper--point-max (point-max))
(when (bound-and-true-p evil-mode)
(evil-set-jump)))
@@ -561,7 +565,7 @@ Matched candidates should have `swiper-invocation-face'."
(unless (if swiper--current-line
(eq swiper--current-line num)
(eq (line-number-at-pos) num))
- (goto-char (point-min))
+ (goto-char swiper--point-min)
(if swiper-use-visual-line
(line-move (1- num))
(forward-line (1- num))))
@@ -581,7 +585,10 @@ Matched candidates should have `swiper-invocation-face'."
(<= (point) (window-end (ivy-state-window ivy-last)
t)))
(recenter))
(setq swiper--current-window-start (window-start))))
- (swiper--add-overlays re)))))
+ (swiper--add-overlays
+ re
+ (max (window-start) swiper--point-min)
+ (min (window-end) swiper--point-max))))))
(defun swiper--add-overlays (re &optional beg end wnd)
"Add overlays for RE regexp in visible part of the current buffer.
@@ -658,7 +665,7 @@ WND, when specified is the window."
(unless (equal (current-buffer)
(ivy-state-buffer ivy-last))
(switch-to-buffer (ivy-state-buffer ivy-last)))
- (goto-char (point-min))
+ (goto-char swiper--point-min)
(funcall (if swiper-use-visual-line
#'line-move
#'forward-line)