[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/vlf 2a7625a 024/310: Improve correctness of search forw
From: |
Stefan Monnier |
Subject: |
[elpa] externals/vlf 2a7625a 024/310: Improve correctness of search forward. |
Date: |
Sat, 28 Nov 2020 00:32:39 -0500 (EST) |
branch: externals/vlf
commit 2a7625afe61d8478408600d7c9a03970f7bade28
Author: Andrey Kotlarski <m00naticus@gmail.com>
Commit: Andrey Kotlarski <m00naticus@gmail.com>
Improve correctness of search forward.
---
vlfi.el | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 100 insertions(+), 14 deletions(-)
diff --git a/vlfi.el b/vlfi.el
index a5bda50..83ffaa5 100644
--- a/vlfi.el
+++ b/vlfi.el
@@ -164,6 +164,19 @@ When prefix argument is negative
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
+(defun vlfi-move-to-chunk (start end)
+ "Move to chunk determined by START END."
+ (when (< vlfi-file-size end) ; re-check file size
+ (setq vlfi-file-size (nth 7 (file-attributes buffer-file-name))))
+ (setq vlfi-start-pos start
+ vlfi-end-pos (min end vlfi-file-size))
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert-file-contents buffer-file-name nil
+ vlfi-start-pos vlfi-end-pos))
+ (set-buffer-modified-p nil)
+ (vlfi-update-buffer-name))
+
(defun vlfi-insert-file (file &optional from-end)
"Insert first chunk of FILE contents in current buffer.
With FROM-END prefix, start from the back."
@@ -172,12 +185,7 @@ With FROM-END prefix, start from the back."
vlfi-end-pos vlfi-file-size)
(setq vlfi-start-pos 0
vlfi-end-pos (min vlfi-batch-size vlfi-file-size)))
- (let ((inhibit-read-only t))
- (erase-buffer)
- (insert-file-contents buffer-file-name nil
- vlfi-start-pos vlfi-end-pos))
- (set-buffer-modified-p nil)
- (vlfi-update-buffer-name))
+ (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos))
;;;###autoload
(defun vlfi (file &optional from-end)
@@ -248,37 +256,63 @@ OP-TYPE specifies the file operation being performed over
FILENAME."
vlfi-start-pos)
vlfi-file-size))
(initial-chunk t)
- (prev 0))
+ (prev 0)
+ match-chunk-start-pos match-chunk-end-pos)
(unwind-protect
(catch 'end-of-file
(if backward
(while (not (zerop to-find))
(cond ((re-search-backward regexp nil t)
- (setq to-find (1- to-find)))
+ (setq to-find (1- to-find)
+ match-chunk-start-pos vlfi-start-pos
+ match-chunk-end-pos
+ (if (< (match-end 0) prev)
+ vlfi-end-pos
+ (- vlfi-end-pos vlfi-batch-size))))
((zerop vlfi-start-pos)
- (throw 'end-of-file nil))
+ (throw 'end-of-file t))
(t (if initial-chunk
(setq initial-chunk nil)
(let ((inhibit-read-only t))
- (delete-region prev (point-max))))
+ (delete-region prev (point-max)))
+ (setq vlfi-end-pos (- vlfi-end-pos
+ vlfi-batch-size)))
(setq prev (point-min))
(vlfi-prev-batch -1)
+ (goto-char (point-max))
(progress-reporter-update
search-reporter (- vlfi-file-size
vlfi-end-pos)))))
(while (not (zerop to-find))
(cond ((re-search-forward regexp nil t)
- (setq to-find (1- to-find)))
+ (setq to-find (1- to-find)
+ match-chunk-end-pos vlfi-end-pos
+ match-chunk-start-pos
+ (if (< prev (match-beginning 0))
+ vlfi-start-pos
+ (+ vlfi-start-pos vlfi-batch-size))))
((= vlfi-end-pos vlfi-file-size)
- (throw 'end-of-file nil))
+ (throw 'end-of-file t))
(t (if initial-chunk
(setq initial-chunk nil)
(let ((inhibit-read-only t))
- (delete-region (point-min) prev)))
+ (delete-region (point-min) prev))
+ (setq vlfi-start-pos (+ vlfi-start-pos
+ vlfi-batch-size)))
(setq prev (point-max))
(vlfi-next-batch -1)
+ (goto-char (point-min))
(progress-reporter-update search-reporter
vlfi-end-pos))))))
+ (and (not initial-chunk)
+ (< to-find count)
+ (if backward ; remove additional chunk
+ (if (< (match-end 0) prev)
+ (let ((inhibit-read-only t))
+ (delete-region prev (point-max))))
+ (if (< prev (match-beginning 0))
+ (let ((inhibit-read-only t))
+ (delete-region (point-min) prev)))))
(progress-reporter-done search-reporter)
(or (zerop to-find)
(if (< to-find count)
@@ -297,7 +331,59 @@ OP-TYPE specifies the file operation being performed over
FILENAME."
(defun vlfi-re-search-forward (regexp count)
"Search forward for REGEXP COUNT number of times."
(interactive "sSearch whole file: \np")
- (vlfi-re-search regexp nil count))
+ (let ((match-chunk-start-pos vlfi-start-pos)
+ (match-chunk-end-pos vlfi-end-pos)
+ (match-start-pos (point))
+ (match-end-pos (point))
+ (to-find count)
+ (search-reporter (make-progress-reporter
+ (concat "Searching for " regexp)
+ vlfi-start-pos vlfi-file-size))
+ (initial-chunk t))
+ (unwind-protect
+ (catch 'end-of-file
+ (while (not (zerop to-find))
+ (cond ((re-search-forward regexp nil t)
+ (setq to-find (if (= match-start-pos
+ (match-beginning 0))
+ to-find
+ (1- to-find))
+ match-start-pos (match-beginning 0)
+ match-end-pos (match-end 0)
+ match-chunk-start-pos vlfi-start-pos
+ match-chunk-end-pos vlfi-end-pos)
+ (if (< vlfi-batch-size match-start-pos)
+ (setq match-chunk-start-pos
+ (+ match-chunk-start-pos vlfi-batch-size)
+ match-start-pos (- match-start-pos
+ vlfi-batch-size)
+ match-end-pos (- match-end-pos
+ vlfi-batch-size))))
+ ((= vlfi-end-pos vlfi-file-size)
+ (throw 'end-of-file nil))
+ (t (if initial-chunk
+ (progn (setq initial-chunk nil)
+ (vlfi-next-batch -1))
+ (vlfi-move-to-chunk (+ vlfi-start-pos
+ vlfi-batch-size)
+ (+ vlfi-end-pos
+ vlfi-batch-size)))
+ (goto-char match-start-pos)
+ (progress-reporter-update search-reporter
+ vlfi-end-pos))))
+ (progress-reporter-done search-reporter))
+ (if (zerop to-find)
+ (when (< vlfi-start-pos match-chunk-start-pos)
+ (let ((inhibit-read-only t))
+ (delete-region (point-min) (1+ vlfi-batch-size)))
+ (setq vlfi-start-pos match-chunk-start-pos))
+ (vlfi-move-to-chunk match-chunk-start-pos
+ match-chunk-end-pos)
+ (goto-char match-end-pos)
+ (if (< to-find count)
+ (message "Moved to the %d match which is last found"
+ (- count to-find))
+ (message "Not found"))))))
(defun vlfi-re-search-backward (regexp count)
"Search backward for REGEXP COUNT number of times."
- [elpa] externals/vlf b40bb60 025/310: Tweaks and fixes to searching., (continued)
- [elpa] externals/vlf b40bb60 025/310: Tweaks and fixes to searching., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 748fd14 027/310: More key-binding simplification., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 670561e 031/310: Optimize search a bit., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf ab20671 033/310: Abstract batch overlapping during search and optimize a bit., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf e387ed5 035/310: Add forgotten local variable statement and documentation., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 0dd4ba8 014/310: Add regex search forward functionality., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 9e27aeb 021/310: Keep stable cursor position when moving through chunks., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 81e4fe1 016/310: Merge branch 'search' of https://github.com/m00natic/vlfi into search, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf e43725e 007/310: Add autoload cookies to fix package installation., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 853386f 015/310: Add regex search forward functionality., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 2a7625a 024/310: Improve correctness of search forward.,
Stefan Monnier <=
- [elpa] externals/vlf c36db11 034/310: Return correct search success status., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 8a33dbf 036/310: Add something like edit minor mode., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 56134a8 050/310: Make vlfi usable under GNU Emacs 23., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 24675d8 054/310: Manage modification times when attempting to save., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf c4f87f9 048/310: Move save interception to vlfi-mode., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf c14c7f0 058/310: Add shift forward of file contents when edited chunk grows in size., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf e115e7e 053/310: Add custom revert function., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 2edbb6b 004/310: Add vlf as an option when opening large files., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 80ca6b6 037/310: Fix vlfi invocation when prompted for large files., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 03f3233 039/310: Don't reinsert content and move on successful search., Stefan Monnier, 2020/11/28