From 06108220e20e381dbcab4dda507d37dfb2b9abf1 Mon Sep 17 00:00:00 2001 From: Andrey Kotlarski Date: Wed, 10 Apr 2013 02:08:54 +0300 Subject: [PATCH] Make saving with change of size correct. --- vlfi.el | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/vlfi.el b/vlfi.el index ffa5263..db15580 100644 --- a/vlfi.el +++ b/vlfi.el @@ -399,14 +399,32 @@ successful. Return nil if nothing found." or \\[vlfi-discard-edit] to discard changes."))) (defun vlfi-write () - "Write current chunk to file. May overwrite existing content." + "Write current chunk to file. +If changing size of chunk, may load the remaining part of file first." (interactive) - (when (or (= (buffer-size) (- vlfi-end-pos vlfi-start-pos)) - (y-or-n-p "Changed size of original chunk. \ -End of chunk will be garbled. Continue? ")) - (write-region nil nil buffer-file-name vlfi-start-pos) - (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos) - (vlfi-mode)) + (let ((size-change (- vlfi-end-pos vlfi-start-pos (buffer-size)))) + (cond ((zerop size-change) + (write-region nil nil buffer-file-name vlfi-start-pos t) + (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos) + (vlfi-mode)) + ((y-or-n-p "Changed size of original chunk. \ +Remaining part of the file has to be loaded. Continue? ") + (let ((pos (point))) + (goto-char (point-max)) + (setq vlfi-file-size + (nth 7 (file-attributes buffer-file-name))) + (let ((load-reporter (make-progress-reporter + "Loading rest of the file"))) + (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)) + (progress-reporter-done load-reporter)) + (write-region nil nil buffer-file-name vlfi-start-pos t) + (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos) + (goto-char pos)) + (vlfi-mode)))) t) (defun vlfi-discard-edit ()