[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/vlf 8e65e13 029/310: Refactor and simplify search proce
From: |
Stefan Monnier |
Subject: |
[elpa] externals/vlf 8e65e13 029/310: Refactor and simplify search procedures. Always use batch sized |
Date: |
Sat, 28 Nov 2020 00:32:40 -0500 (EST) |
branch: externals/vlf
commit 8e65e13dd916d3d2f18bd2e99e4111e3a6f4cbf1
Author: Andrey Kotlarski <m00naticus@gmail.com>
Commit: Andrey Kotlarski <m00naticus@gmail.com>
Refactor and simplify search procedures. Always use batch sized
chunks.
---
vlfi.el | 183 ++++++++++++++++++++++++++--------------------------------------
1 file changed, 73 insertions(+), 110 deletions(-)
diff --git a/vlfi.el b/vlfi.el
index 8e24b38..a0a1a84 100644
--- a/vlfi.el
+++ b/vlfi.el
@@ -260,6 +260,77 @@ OP-TYPE specifies the file operation being performed over
FILENAME."
;;;###autoload
(fset 'abort-if-file-too-large 'vlfi-if-file-too-large)
+;;; search
+(defun vlfi-re-search (regexp count backward)
+ "Search for REGEXP COUNT number of times forward or BACKWARD."
+ (let* ((match-start-pos (+ vlfi-start-pos (point)))
+ (match-end-pos match-start-pos)
+ (to-find count)
+ (search-reporter (make-progress-reporter
+ (concat "Searching for " regexp)
+ (if backward
+ (- vlfi-file-size vlfi-end-pos)
+ vlfi-start-pos)
+ vlfi-file-size))
+ (half-batch (/ vlfi-batch-size 2)))
+ (unwind-protect
+ (catch 'end-of-file
+ (if backward
+ (while (not (zerop to-find))
+ (cond ((re-search-backward regexp nil t)
+ (setq match-start-pos (+ vlfi-start-pos
+ (match-beginning 0)))
+ (let ((new-match-end (+ vlfi-start-pos
+ (match-end 0))))
+ (if (< new-match-end match-end-pos)
+ (setq to-find (1- to-find)
+ match-end-pos new-match-end))))
+ ((zerop vlfi-start-pos)
+ (throw 'end-of-file nil))
+ (t (vlfi-move-to-batch (- vlfi-start-pos
+ half-batch))
+ (goto-char (if (< match-start-pos
+ vlfi-end-pos)
+ (- match-start-pos
+ vlfi-start-pos)
+ (point-max)))
+ (progress-reporter-update search-reporter
+ vlfi-start-pos))))
+ (while (not (zerop to-find))
+ (cond ((re-search-forward regexp nil t)
+ (setq match-end-pos (+ vlfi-start-pos
+ (match-end 0)))
+ (let ((new-match-start (+ vlfi-start-pos
+ (match-beginning 0))))
+ (if (< match-start-pos new-match-start)
+ (setq to-find (1- to-find)
+ match-start-pos new-match-start))))
+ ((= vlfi-end-pos vlfi-file-size)
+ (throw 'end-of-file nil))
+ (t (vlfi-move-to-batch (- vlfi-end-pos half-batch))
+ (goto-char (if (< vlfi-start-pos match-end-pos)
+ (- match-end-pos vlfi-start-pos)
+ (point-min)))
+ (progress-reporter-update search-reporter
+ vlfi-end-pos)))))
+ (progress-reporter-done search-reporter))
+ (vlfi-end-search (if backward match-start-pos match-end-pos)
+ count to-find))))
+
+(defun vlfi-end-search (match-pos count to-find)
+ "Move to chunk surrounding MATCH-POS.
+According to COUNT and left TO-FIND show if search has been
+successful. Return nil if nothing found."
+ (vlfi-move-to-batch (- match-pos (/ vlfi-batch-size 2)))
+ (goto-char (- match-pos vlfi-start-pos))
+ (cond ((zerop to-find) t)
+ ((< to-find count)
+ (message "Moved to the %d match which is last found"
+ (- count to-find))
+ t)
+ (t (message "Not found")
+ nil)))
+
(defun vlfi-re-search-forward (regexp count)
"Search forward for REGEXP prefix COUNT number of times."
(interactive (list (read-regexp "Search whole file"
@@ -267,54 +338,7 @@ OP-TYPE specifies the file operation being performed over
FILENAME."
(car regexp-history))
'regexp-history)
(or current-prefix-arg 1)))
- (let ((match-chunk-start vlfi-start-pos)
- (match-chunk-end 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 vlfi-start-pos
- match-chunk-end vlfi-end-pos)
- (if (and (< vlfi-batch-size match-start-pos)
- (> (- vlfi-end-pos vlfi-start-pos)
- vlfi-batch-size))
- (setq match-chunk-start
- (+ match-chunk-start 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 (if (< vlfi-start-pos match-chunk-end)
- match-start-pos
- (point-min)))
- (goto-char match-start-pos)
- (progress-reporter-update search-reporter
- vlfi-end-pos))))
- (progress-reporter-done search-reporter))
- (vlfi-end-search match-chunk-start match-chunk-end
- match-end-pos count to-find))))
+ (vlfi-re-search regexp count nil))
(defun vlfi-re-search-backward (regexp count)
"Search backward for REGEXP prefix COUNT number of times."
@@ -323,68 +347,7 @@ OP-TYPE specifies the file operation being performed over
FILENAME."
(car regexp-history))
'regexp-history)
(or current-prefix-arg 1)))
- (let ((match-chunk-start vlfi-start-pos)
- (match-chunk-end vlfi-end-pos)
- (match-start-pos (point))
- (match-end-pos (point))
- (to-find count)
- (search-reporter (make-progress-reporter
- (concat "Searching for " regexp)
- (- vlfi-file-size vlfi-end-pos)
- vlfi-file-size))
- (initial-chunk t))
- (unwind-protect
- (catch 'start-of-file
- (while (not (zerop to-find))
- (cond ((re-search-backward regexp nil t)
- (setq to-find (if (= match-end-pos
- (match-end 0))
- to-find
- (1- to-find))
- match-start-pos (match-beginning 0)
- match-end-pos (match-end 0)
- match-chunk-start vlfi-start-pos
- match-chunk-end vlfi-end-pos)
- (if (and (< match-end-pos vlfi-batch-size)
- (> (- vlfi-end-pos vlfi-start-pos)
- vlfi-batch-size))
- (setq match-chunk-end
- (- match-chunk-end
- vlfi-batch-size))))
- ((zerop vlfi-start-pos)
- (throw 'start-of-file nil))
- (t (if initial-chunk
- (progn (setq initial-chunk nil)
- (vlfi-prev-batch -1))
- (vlfi-move-to-chunk (- vlfi-start-pos
- vlfi-batch-size)
- (- vlfi-end-pos
- vlfi-batch-size)))
- (goto-char (if (< match-chunk-start vlfi-end-pos)
- match-end-pos
- (point-max)))
- (setq last-chunk-match nil)
- (progress-reporter-update search-reporter
- (- vlfi-file-size
- vlfi-start-pos)))))
- (progress-reporter-done search-reporter))
- (vlfi-end-search match-chunk-start match-chunk-end
- match-start-pos count to-find))))
-
-(defun vlfi-end-search (match-chunk-start match-chunk-end
- match-pos count to-find)
- "Move to chunk determined by MATCH-CHUNK-START and MATCH-CHUNK-END.
-Go to MATCH-POS and according to COUNT and left TO-FIND show if search
-has been successful. Return nil if nothing found."
- (vlfi-move-to-chunk match-chunk-start match-chunk-end)
- (goto-char match-pos)
- (cond ((zerop to-find) t)
- ((< to-find count)
- (message "Moved to the %d match which is last found"
- (- count to-find))
- t)
- (t (message "Not found")
- nil)))
+ (vlfi-re-search regexp count t))
(provide 'vlfi)
- [elpa] externals/vlf c4f87f9 048/310: Move save interception to vlfi-mode., (continued)
- [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
- [elpa] externals/vlf 2525ebf 049/310: Improvements to file saving., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf cedd0b4 059/310: Fix spaces and indent., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 6ea7a2a 065/310: Add macro for file size determination., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 91fecb7 023/310: Simplify key-bindings., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 8e65e13 029/310: Refactor and simplify search procedures. Always use batch sized,
Stefan Monnier <=
- [elpa] externals/vlf db3d2af 043/310: Update version, README and commentary section., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 467154a 047/310: Add documentation., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf a3c405f 051/310: Make detection of buffer size change on save more correct when dealing, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 7985f0f 052/310: Fix `vlfi-if-file-too-large' to be more tolerable on GNU Emacs 23., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 8637ce5 062/310: Update README and version., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 4cbefde 067/310: Optimize chunk save buffers for multiple insert/erase operations., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 473536e 073/310: Add adjust command for cases where chunk starts with part of multibyte symbol., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 2564c9a 006/310: Plug option for vlf when opening files on package load., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf c0d143b 011/310: Rename vlf to vlfi and update README., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 36411c3 022/310: Enhance search to cover in between chunk boundaries., Stefan Monnier, 2020/11/28