[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/vlf f23262e 057/310: Add shift back of file contents wh
From: |
Stefan Monnier |
Subject: |
[elpa] externals/vlf f23262e 057/310: Add shift back of file contents when edited chunk shrinks in size. |
Date: |
Sat, 28 Nov 2020 00:32:46 -0500 (EST) |
branch: externals/vlf
commit f23262e826ae4c4b316d4e474b027b21736b9e16
Author: Andrey Kotlarski <m00naticus@gmail.com>
Commit: Andrey Kotlarski <m00naticus@gmail.com>
Add shift back of file contents when edited chunk shrinks in size.
---
vlfi.el | 86 +++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 52 insertions(+), 34 deletions(-)
diff --git a/vlfi.el b/vlfi.el
index de550b5..7322744 100644
--- a/vlfi.el
+++ b/vlfi.el
@@ -413,47 +413,65 @@ Search is performed chunk by chunk in `vlfi-batch-size'
memory."
"Editing: Type \\[vlfi-write] to write chunk \
or \\[vlfi-discard-edit] to discard changes.")))
-(defun vlfi-write-1 ()
- "Append current buffer content to `vlfi-start-pos' position in file.
-Reopen last viewed chunk."
+(defun vlfi-file-shift-back (size-change)
+ "Shift file contents SIZE-CHANGE bytes back."
(write-region nil nil buffer-file-name vlfi-start-pos t)
- (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos)
- (vlfi-mode))
+ (let ((coding-system buffer-file-coding-system))
+ (setq buffer-file-coding-system nil)
+ (let ((read-start-pos vlfi-end-pos)
+ (reporter (make-progress-reporter "Adjusting file content"
+ vlfi-end-pos
+ vlfi-file-size)))
+ (while (vlfi-shift-batch read-start-pos (- read-start-pos
+ size-change))
+ (setq read-start-pos (+ read-start-pos vlfi-batch-size))
+ (progress-reporter-update reporter read-start-pos))
+ ;; pad end with space
+ (erase-buffer)
+ (insert-char 32 size-change)
+ (write-region nil nil buffer-file-name (- vlfi-file-size
+ size-change))
+ (progress-reporter-done reporter))
+ (setq buffer-file-coding-system coding-system)))
+
+(defun vlfi-shift-batch (read-pos write-pos)
+ "Read `vlfi-batch-size' bytes from READ-POS and write them \
+back at WRITE-POS. Return nil if EOF is reached, t otherwise."
+ (erase-buffer)
+ (setq vlfi-file-size (nth 7 (file-attributes buffer-file-name)))
+ (let ((read-end (+ read-pos vlfi-batch-size)))
+ (insert-file-contents-literally buffer-file-name nil
+ read-pos
+ (min vlfi-file-size read-end))
+ (write-region nil nil buffer-file-name write-pos t)
+ (< read-end vlfi-file-size)))
+
+(defun vlfi-file-shift-forward (size-change)
+ "Shift file contents SIZE-CHANGE bytes forward."
+ (ignore size-change))
(defun vlfi-write ()
"Write current chunk to file. Always return true to disable save.
-If changing size of chunk, may load the remaining part of file first."
+If changing size of chunk shift remaining file content."
(interactive)
(when (and (derived-mode-p 'vlfi-mode)
- (buffer-modified-p)
- (or (verify-visited-file-modtime)
- (y-or-n-p "File has changed since visited or \
-saved. Save anyway? ")))
+ (buffer-modified-p)
+ (or (verify-visited-file-modtime)
+ (y-or-n-p "File has changed since visited or saved. \
+Save anyway? ")))
(let ((size-change (- vlfi-end-pos vlfi-start-pos
- (length
- (encode-coding-region
- (point-min) (point-max)
- buffer-file-coding-system t)))))
- (if (zerop size-change)
- (vlfi-write-1)
- (setq vlfi-file-size (nth 7
- (file-attributes buffer-file-name)))
- (cond ((= vlfi-file-size vlfi-end-pos)
- (vlfi-write-1))
- ((y-or-n-p (concat "Changed size of original chunk. \
-Remaining part of the file ["
- (file-size-human-readable
- (- vlfi-file-size vlfi-end-pos))
- "] has to be loaded. Continue? "))
- (let ((pos (point)))
- (goto-char (point-max))
- (insert-file-contents buffer-file-name nil
- vlfi-end-pos vlfi-file-size)
- (when (< 0 size-change) ; pad with empty characters
- (goto-char (point-max))
- (insert-char 32 size-change))
- (vlfi-write-1)
- (goto-char pos))))))
+ (length (encode-coding-region
+ (point-min) (point-max)
+ buffer-file-coding-system t))))
+ (pos (point)))
+ (cond ((zerop size-change)
+ (write-region nil nil buffer-file-name vlfi-start-pos t))
+ ((< 0 size-change)
+ (vlfi-file-shift-back size-change))
+ (t (vlfi-file-shift-forward size-change)))
+ (goto-char pos))
+ (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos)
+ (vlfi-mode)
t))
(defun vlfi-discard-edit ()
- [elpa] externals/vlf 30e4959 019/310: Add direct jumps to first and last chunks., (continued)
- [elpa] externals/vlf 30e4959 019/310: Add direct jumps to first and last chunks., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 42693f6 026/310: Update README., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf ac382e9 032/310: Add temporary highlight of match., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 9abee04 038/310: Merge branch 'master' into edit, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 0ff2f3e 040/310: Merge branch 'master' into edit, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 1948f3e 041/310: Make VLFI edit mode actually working., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 5405a30 045/310: Intercept buffer saving with vlfi-write., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf f3132c0 044/310: Keep current position when moving around chunks., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 8787186 055/310: Show paging according to batch size instead of percentage., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 8f6299c 056/310: Simplify detection of buffer size change., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf f23262e 057/310: Add shift back of file contents when edited chunk shrinks in size.,
Stefan Monnier <=
- [elpa] externals/vlf 2f201c5 061/310: Add jump to chunk command., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf d5f2a36 060/310: Rearrange code in sections., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 705f9ce 064/310: Add issue section to the README., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 9fad430 066/310: Fix positioning after save., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf e545769 128/310: Fix adjusting of end chunk bytes., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf ae2237d 135/310: Minor adjustments:, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf cb47e19 139/310: Use temporary buffer for occur in case of modifications., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 7a14109 137/310: Fix search for GNU Emacs 23 (no assert)., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 452b7eb 141/310: Ensure there are no modifications when executing searches., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf a42247c 150/310: Automatically scroll to adjacent batch when start or end of chunk is, Stefan Monnier, 2020/11/28