1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-01-31 10:10:48 +00:00

Add shift back of file contents when edited chunk shrinks in size.

This commit is contained in:
Andrey Kotlarski 2013-04-13 20:47:29 +03:00
parent 8f6299c6c2
commit f23262e826

80
vlfi.el
View File

@ -413,47 +413,65 @@ Search is performed chunk by chunk in `vlfi-batch-size' memory."
"Editing: Type \\[vlfi-write] to write chunk \ "Editing: Type \\[vlfi-write] to write chunk \
or \\[vlfi-discard-edit] to discard changes."))) or \\[vlfi-discard-edit] to discard changes.")))
(defun vlfi-write-1 () (defun vlfi-file-shift-back (size-change)
"Append current buffer content to `vlfi-start-pos' position in file. "Shift file contents SIZE-CHANGE bytes back."
Reopen last viewed chunk."
(write-region nil nil buffer-file-name vlfi-start-pos t) (write-region nil nil buffer-file-name vlfi-start-pos t)
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos) (let ((coding-system buffer-file-coding-system))
(vlfi-mode)) (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 () (defun vlfi-write ()
"Write current chunk to file. Always return true to disable save. "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) (interactive)
(when (and (derived-mode-p 'vlfi-mode) (when (and (derived-mode-p 'vlfi-mode)
(buffer-modified-p) (buffer-modified-p)
(or (verify-visited-file-modtime) (or (verify-visited-file-modtime)
(y-or-n-p "File has changed since visited or \ (y-or-n-p "File has changed since visited or saved. \
saved. Save anyway? "))) Save anyway? ")))
(let ((size-change (- vlfi-end-pos vlfi-start-pos (let ((size-change (- vlfi-end-pos vlfi-start-pos
(length (length (encode-coding-region
(encode-coding-region
(point-min) (point-max) (point-min) (point-max)
buffer-file-coding-system t))))) buffer-file-coding-system t))))
(if (zerop size-change) (pos (point)))
(vlfi-write-1) (cond ((zerop size-change)
(setq vlfi-file-size (nth 7 (write-region nil nil buffer-file-name vlfi-start-pos t))
(file-attributes buffer-file-name))) ((< 0 size-change)
(cond ((= vlfi-file-size vlfi-end-pos) (vlfi-file-shift-back size-change))
(vlfi-write-1)) (t (vlfi-file-shift-forward size-change)))
((y-or-n-p (concat "Changed size of original chunk. \ (goto-char pos))
Remaining part of the file [" (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos)
(file-size-human-readable (vlfi-mode)
(- 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))))))
t)) t))
(defun vlfi-discard-edit () (defun vlfi-discard-edit ()