[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/vlf a9c14e2 268/310: Add regexp query replace over whol
From: |
Stefan Monnier |
Subject: |
[elpa] externals/vlf a9c14e2 268/310: Add regexp query replace over whole file command. |
Date: |
Sat, 28 Nov 2020 00:33:29 -0500 (EST) |
branch: externals/vlf
commit a9c14e2d4c51fcad0c8e66266f5797fc81eccfa6
Author: Andrey Kotlarski <m00naticus@gmail.com>
Commit: Andrey Kotlarski <m00naticus@gmail.com>
Add regexp query replace over whole file command.
---
vlf-search.el | 84 ++++++++++++++++++++++++++++++++++++++++++++++-------------
vlf.el | 3 +++
2 files changed, 69 insertions(+), 18 deletions(-)
diff --git a/vlf-search.el b/vlf-search.el
index ec668f2..df82cfd 100644
--- a/vlf-search.el
+++ b/vlf-search.el
@@ -64,15 +64,17 @@ Return t if search has been at least partially successful."
(if backward
(while (not (zerop to-find))
(cond ((re-search-backward regexp nil t)
- (setq to-find (1- to-find)
- match-chunk-start vlf-start-pos
- match-chunk-end vlf-end-pos
- match-start-pos (+ vlf-start-pos
- (position-bytes
- (match-beginning 0)))
- match-end-pos (+ vlf-start-pos
+ (setq match-end-pos (+ vlf-start-pos
(position-bytes
- (match-end 0)))))
+ (match-end 0))))
+ (if (/= match-start-pos match-end-pos)
+ (setq to-find (1- to-find)
+ match-chunk-start vlf-start-pos
+ match-chunk-end vlf-end-pos
+ match-start-pos
+ (+ vlf-start-pos
+ (position-bytes
+ (match-beginning 0))))))
((zerop vlf-start-pos)
(throw 'end-of-file nil))
(t (vlf-tune-batch tune-types)
@@ -97,15 +99,16 @@ Return t if search has been at least partially successful."
vlf-start-pos)))))
(while (not (zerop to-find))
(cond ((re-search-forward regexp nil t)
- (setq to-find (1- to-find)
- match-chunk-start vlf-start-pos
- match-chunk-end vlf-end-pos
- match-start-pos (+ vlf-start-pos
+ (setq match-start-pos (+ vlf-start-pos
(position-bytes
- (match-beginning 0)))
- match-end-pos (+ vlf-start-pos
- (position-bytes
- (match-end 0)))))
+ (match-beginning 0))))
+ (if (/= match-start-pos match-end-pos)
+ (setq to-find (1- to-find)
+ match-chunk-start vlf-start-pos
+ match-chunk-end vlf-end-pos
+ match-end-pos (+ vlf-start-pos
+ (position-bytes
+ (match-end 0))))))
((= vlf-end-pos vlf-file-size)
(throw 'end-of-file nil))
(t (vlf-tune-batch tune-types)
@@ -172,10 +175,12 @@ Return nil if nothing found."
(overlay-put overlay 'face 'match)
(if success
(message "Match found (%f secs)" (- (float-time) time))
- (goto-char match-end)
(message "Moved to the %d match which is last (%f secs)"
(- count to-find) (- (float-time) time)))
- (unwind-protect (sit-for 3)
+ (goto-char (or (byte-to-position (- match-pos-start
+ vlf-start-pos))
+ (point-max)))
+ (unwind-protect (sit-for 1)
(delete-overlay overlay))
t))))
@@ -311,6 +316,49 @@ Assume `hexl-mode' is active."
(goto-char (point-min))
(forward-line (- n 1 (/ hidden-bytes hexl-bits))))))
+(defun vlf-query-replace (regexp to-string &optional delimited backward)
+ "Query replace over whole file matching REGEXP with TO-STRING.
+Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
+only matches surrounded by word boundaries. A negative prefix arg means
+replace BACKWARD."
+ (interactive
+ (let ((common (query-replace-read-args
+ (concat "Query replace over whole file"
+ (if current-prefix-arg
+ (if (eq current-prefix-arg '-)
+ " backward"
+ " word")
+ "")
+ " regexp")
+ t)))
+ (list (nth 0 common) (nth 1 common) (nth 2 common) (nth 3 common))))
+ (query-replace-regexp regexp to-string delimited nil nil backward)
+ (if (buffer-modified-p)
+ (save-buffer))
+ (let ((match-found t)
+ (automatic (eq (lookup-key query-replace-map
+ (vector last-input-event))
+ 'automatic)))
+ (while (and match-found (if backward
+ (not (zerop vlf-start-pos))
+ (< vlf-end-pos vlf-file-size)))
+ (setq match-found (if backward
+ (vlf-re-search-backward regexp 1)
+ (vlf-re-search-forward regexp 1)))
+ (when match-found
+ (cond ((not automatic)
+ (query-replace-regexp regexp to-string delimited
+ nil nil backward)
+ (setq automatic (eq (lookup-key query-replace-map
+ (vector last-input-event))
+ 'automatic)))
+ (backward (while (re-search-backward regexp nil t)
+ (replace-match to-string)))
+ (t (while (re-search-forward regexp nil t)
+ (replace-match to-string))))
+ (if (buffer-modified-p)
+ (save-buffer))))))
+
(provide 'vlf-search)
;;; vlf-search.el ends here
diff --git a/vlf.el b/vlf.el
index 634e5cd..9dea3f7 100644
--- a/vlf.el
+++ b/vlf.el
@@ -59,6 +59,8 @@ values are: `write', `ediff', `occur', `search', `goto-line'."
(autoload 'vlf-re-search-backward "vlf-search"
"Search backward for REGEXP prefix COUNT number of times." t)
(autoload 'vlf-goto-line "vlf-search" "Go to line." t)
+(autoload 'vlf-query-replace "vlf-search"
+ "Replace some things after point matching REGEXP." t)
(autoload 'vlf-occur "vlf-occur"
"Make whole file occur style index for REGEXP." t)
(autoload 'vlf-toggle-follow "vlf-follow"
@@ -79,6 +81,7 @@ values are: `write', `ediff', `occur', `search', `goto-line'."
(vlf-change-batch-size t)))
(define-key map "s" 'vlf-re-search-forward)
(define-key map "r" 'vlf-re-search-backward)
+ (define-key map "%" 'vlf-query-replace)
(define-key map "o" 'vlf-occur)
(define-key map "[" 'vlf-beginning-of-file)
(define-key map "]" 'vlf-end-of-file)
- [elpa] externals/vlf 1f9ba7c 230/310: Use derived-mode-p., (continued)
- [elpa] externals/vlf 1f9ba7c 230/310: Use derived-mode-p., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 32ff2cb 239/310: Add bookkeeping module., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 5379943 242/310: Save times instead of speeds, compare on cumulative speed when tuning., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 0d9cc8e 245/310: Don't measure encoding of too small region when adjusting chunk start, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf e8bb4a9 248/310: Apply batch size tuning on adjacent moves, search, save adjusting and, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf d526ea8 257/310: Apply and restore batch size in more cases., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 9b6657b 260/310: Replace usage of conservative tune in single batch operations with load, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 2c660b0 269/310: Fix positive goto-line search and make highlighting of match optional., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 3cfa9b1 273/310: Refactor vlf-query-replace and don't adjust batch size if only, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 61599a0 274/310: Change linear tune to search only known measures and use it to, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf a9c14e2 268/310: Add regexp query replace over whole file command.,
Stefan Monnier <=
- [elpa] externals/vlf 673ae08 277/310: Lower tramp verbosity level., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf ac8d400 276/310: Fixes to hexl-mode activation., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf f63ea96 272/310: Document new query replace and save options., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf e4a2e80 266/310: Optimize goto-line for hexl-mode, no need to search., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 7b9befe 275/310: - don't load unnecessary data at the end of search or occur, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 842569a 270/310: Add ability to use temporary file when adjusting content on save., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf d579b7b 279/310: Workaround local maximum in batch tune., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf b14c912 290/310: Copy around buffer specific profile vectors when forking new buffers., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf b300137 292/310: Encode smaller region to detect cut point overall offset in vlf-occur, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 86be483 293/310: Improve search precision., Stefan Monnier, 2020/11/28