1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-04-18 16:50:19 +01:00

Ensure there are no modifications when executing searches.

This commit is contained in:
Andrey Kotlarski 2013-12-03 02:37:13 +02:00
parent 2dba838015
commit 452b7eb42d

162
vlf.el
View File

@ -277,6 +277,12 @@ Ask for confirmation if NOCONFIRM is nil."
(unwind-protect (progn ,@body) (unwind-protect (progn ,@body)
(buffer-enable-undo)))) (buffer-enable-undo))))
(defmacro vlf-no-modifications (&rest body)
"Ensure there are no modifications and execute BODY."
`(if (buffer-modified-p)
(error "Save or discard your changes first")
,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; batch movement ;;; batch movement
@ -342,63 +348,62 @@ If same as current chunk is requested, do nothing."
(defun vlf-move-to-chunk-1 (start end) (defun vlf-move-to-chunk-1 (start end)
"Move to chunk determined by START END keeping as much edits if any. "Move to chunk determined by START END keeping as much edits if any.
Return t if move hasn't been canceled." Return t if move hasn't been canceled."
(vlf-with-undo-disabled (let ((modified (buffer-modified-p))
(let ((modified (buffer-modified-p)) (start (max 0 start))
(start (max 0 start)) (end (min end vlf-file-size))
(end (min end vlf-file-size)) (edit-end (+ (position-bytes (point-max)) vlf-start-pos)))
(edit-end (+ (position-bytes (point-max)) vlf-start-pos))) (cond
(cond ((and (= start vlf-start-pos) (= end edit-end))
((and (= start vlf-start-pos) (= end edit-end)) (unless modified
(unless modified (vlf-move-to-chunk-2 start end)
(vlf-move-to-chunk-2 start end) t))
t)) ((or (<= edit-end start) (<= end vlf-start-pos))
((or (<= edit-end start) (<= end vlf-start-pos)) (when (or (not modified)
(when (or (not modified) (y-or-n-p "Chunk modified, are you sure? ")) ;full chunk renewal
(y-or-n-p "Buffer modified, are you sure? ")) ;full chunk renewal (vlf-move-to-chunk-2 start end)
(vlf-move-to-chunk-2 start end) t))
t)) ((or (and (<= start vlf-start-pos) (<= edit-end end))
((or (and (<= start vlf-start-pos) (<= edit-end end)) (not modified)
(not modified) (y-or-n-p "Chunk modified, are you sure? "))
(y-or-n-p "Buffer modified, are you sure? ")) (let ((pos (+ (position-bytes (point)) vlf-start-pos))
(let ((pos (+ (position-bytes (point)) vlf-start-pos)) (shift-start 0)
(shift-start 0) (shift-end 0)
(shift-end 0) (inhibit-read-only t))
(inhibit-read-only t)) (cond ((< end edit-end)
(cond ((< end edit-end) (delete-region (byte-to-position (1+
(delete-region (byte-to-position (1+ (- end
(- end vlf-start-pos)))
vlf-start-pos))) (point-max)))
(point-max))) ((< edit-end end)
((< edit-end end) (let ((edit-end-pos (point-max)))
(let ((edit-end-pos (point-max))) (goto-char edit-end-pos)
(goto-char edit-end-pos) (insert-file-contents buffer-file-name nil
(insert-file-contents buffer-file-name nil vlf-end-pos end)
vlf-end-pos end) (setq shift-end (cdr (vlf-adjust-chunk
(setq shift-end (cdr (vlf-adjust-chunk vlf-end-pos end nil t
vlf-end-pos end nil t edit-end-pos))))))
edit-end-pos)))))) (cond ((< vlf-start-pos start)
(cond ((< vlf-start-pos start) (delete-region (point-min) (byte-to-position
(delete-region (point-min) (byte-to-position (- start vlf-start-pos))))
(- start vlf-start-pos)))) ((< start vlf-start-pos)
((< start vlf-start-pos) (let ((edit-end-pos (point-max)))
(let ((edit-end-pos (point-max))) (goto-char edit-end-pos)
(goto-char edit-end-pos) (insert-file-contents buffer-file-name nil
(insert-file-contents buffer-file-name nil start vlf-start-pos)
start vlf-start-pos) (setq shift-start (car
(setq shift-start (car (vlf-adjust-chunk start
(vlf-adjust-chunk start vlf-start-pos
vlf-start-pos t nil
t nil edit-end-pos)))
edit-end-pos))) (goto-char (point-min))
(goto-char (point-min)) (insert (delete-and-extract-region edit-end-pos
(insert (delete-and-extract-region edit-end-pos (point-max))))))
(point-max)))))) (setq vlf-start-pos (- start shift-start)
(setq vlf-start-pos (- start shift-start) vlf-end-pos (+ end shift-end))
vlf-end-pos (+ end shift-end)) (goto-char (or (byte-to-position (- pos vlf-start-pos))
(goto-char (or (byte-to-position (- pos vlf-start-pos)) (point-max))))
(point-max)))) (set-buffer-modified-p modified)
(set-buffer-modified-p modified) t))))
t)))))
(defun vlf-move-to-chunk-2 (start end) (defun vlf-move-to-chunk-2 (start end)
"Unconditionally move to chunk determined by START END." "Unconditionally move to chunk determined by START END."
@ -484,7 +489,6 @@ BATCH-STEP is amount of overlap between successive chunks."
(- vlf-file-size vlf-end-pos) (- vlf-file-size vlf-end-pos)
vlf-start-pos) vlf-start-pos)
vlf-file-size))) vlf-file-size)))
(set-buffer-modified-p nil)
(vlf-with-undo-disabled (vlf-with-undo-disabled
(unwind-protect (unwind-protect
(catch 'end-of-file (catch 'end-of-file
@ -546,7 +550,6 @@ BATCH-STEP is amount of overlap between successive chunks."
(progress-reporter-update reporter (progress-reporter-update reporter
vlf-end-pos))))) vlf-end-pos)))))
(progress-reporter-done reporter)) (progress-reporter-done reporter))
(set-buffer-modified-p nil)
(if backward (if backward
(vlf-goto-match match-chunk-start match-chunk-end (vlf-goto-match match-chunk-start match-chunk-end
match-end-pos match-start-pos match-end-pos match-start-pos
@ -597,7 +600,8 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
(if regexp-history (if regexp-history
(car regexp-history))) (car regexp-history)))
(or current-prefix-arg 1))) (or current-prefix-arg 1)))
(vlf-re-search regexp count nil (/ vlf-batch-size 8))) (vlf-no-modifications
(vlf-re-search regexp count nil (/ vlf-batch-size 8))))
(defun vlf-re-search-backward (regexp count) (defun vlf-re-search-backward (regexp count)
"Search backward for REGEXP prefix COUNT number of times. "Search backward for REGEXP prefix COUNT number of times.
@ -606,28 +610,30 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
(if regexp-history (if regexp-history
(car regexp-history))) (car regexp-history)))
(or current-prefix-arg 1))) (or current-prefix-arg 1)))
(vlf-re-search regexp count t (/ vlf-batch-size 8))) (vlf-no-modifications
(vlf-re-search regexp count t (/ vlf-batch-size 8))))
(defun vlf-goto-line (n) (defun vlf-goto-line (n)
"Go to line N. If N is negative, count from the end of file." "Go to line N. If N is negative, count from the end of file."
(interactive "nGo to line: ") (interactive "nGo to line: ")
(let ((start-pos vlf-start-pos) (vlf-no-modifications
(end-pos vlf-end-pos) (let ((start-pos vlf-start-pos)
(pos (point)) (end-pos vlf-end-pos)
(success nil)) (pos (point))
(unwind-protect (success nil))
(if (< 0 n) (unwind-protect
(progn (vlf-beginning-of-file) (if (< 0 n)
(goto-char (point-min)) (progn (vlf-beginning-of-file)
(setq success (vlf-re-search "[\n\C-m]" (1- n) (goto-char (point-min))
nil 0))) (setq success (vlf-re-search "[\n\C-m]" (1- n)
(vlf-end-of-file) nil 0)))
(goto-char (point-max)) (vlf-end-of-file)
(setq success (vlf-re-search "[\n\C-m]" (- n) t 0))) (goto-char (point-max))
(if success (setq success (vlf-re-search "[\n\C-m]" (- n) t 0)))
(message "Onto line %s" n) (if success
(vlf-move-to-chunk start-pos end-pos) (message "Onto line %s" n)
(goto-char pos))))) (vlf-move-to-chunk start-pos end-pos)
(goto-char pos))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; occur ;;; occur