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

Fix adjusting of end chunk bytes.

This commit is contained in:
Andrey Kotlarski 2013-08-24 17:31:38 +03:00
parent 737c1e9979
commit e5457690fb

87
vlfi.el
View File

@ -299,12 +299,14 @@ When given MINIMAL flag, skip non important operations."
"Move to chunk determined by START END. "Move to chunk determined by START END.
When given MINIMAL flag, skip non important operations." When given MINIMAL flag, skip non important operations."
(vlfi-verify-size) (vlfi-verify-size)
(and (vlfi-move-to-chunk-1 start end) (if (buffer-modified-p)
(not minimal) (if (vlfi-move-to-chunk-1 start end)
(vlfi-update-buffer-name))) (or minimal (vlfi-update-buffer-name)))
(vlfi-move-to-chunk-2 start end)
(or minimal (vlfi-update-buffer-name))))
(defun vlfi-move-to-chunk-1 (start end) (defun vlfi-move-to-chunk-1 (start end)
"Move to chunk determined by START END keeping as much changes. "Move to chunk determined by START END keeping as much edits.
Return t if move hasn't been canceled." Return t if move hasn't been canceled."
(let ((modified (buffer-modified-p)) (let ((modified (buffer-modified-p))
(start (max 0 start)) (start (max 0 start))
@ -328,8 +330,9 @@ Return t if move hasn't been canceled."
(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 (- end (delete-region (byte-to-position (1+
vlfi-start-pos)) (- end
vlfi-start-pos)))
(point-max))) (point-max)))
((< edit-end end) ((< edit-end end)
(let ((edit-end-pos (point-max))) (let ((edit-end-pos (point-max)))
@ -337,7 +340,7 @@ Return t if move hasn't been canceled."
(insert-file-contents buffer-file-name nil (insert-file-contents buffer-file-name nil
vlfi-end-pos end) vlfi-end-pos end)
(setq shift-end (cdr (vlfi-adjust-chunk (setq shift-end (cdr (vlfi-adjust-chunk
vlfi-end-pos end vlfi-end-pos end nil t
edit-end-pos)))))) edit-end-pos))))))
(cond ((< vlfi-start-pos start) (cond ((< vlfi-start-pos start)
(delete-region (point-min) (byte-to-position (delete-region (point-min) (byte-to-position
@ -350,12 +353,13 @@ Return t if move hasn't been canceled."
(setq shift-start (car (setq shift-start (car
(vlfi-adjust-chunk start (vlfi-adjust-chunk start
vlfi-start-pos vlfi-start-pos
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 vlfi-start-pos (- start shift-start) (setq vlfi-start-pos (- start shift-start)
vlfi-end-pos (- end shift-end)) vlfi-end-pos (+ end shift-end))
(goto-char (or (byte-to-position (- pos vlfi-start-pos)) (goto-char (or (byte-to-position (- pos vlfi-start-pos))
(point-max)))) (point-max))))
(set-buffer-modified-p modified) (set-buffer-modified-p modified)
@ -370,41 +374,58 @@ Return t if move hasn't been canceled."
(erase-buffer) (erase-buffer)
(insert-file-contents buffer-file-name nil (insert-file-contents buffer-file-name nil
vlfi-start-pos vlfi-end-pos) vlfi-start-pos vlfi-end-pos)
(let ((shifts (vlfi-adjust-chunk vlfi-start-pos vlfi-end-pos))) (let ((shifts (vlfi-adjust-chunk vlfi-start-pos vlfi-end-pos t
t)))
(setq vlfi-start-pos (- vlfi-start-pos (car shifts)) (setq vlfi-start-pos (- vlfi-start-pos (car shifts))
vlfi-end-pos (- vlfi-end-pos (cdr shifts))) vlfi-end-pos (+ vlfi-end-pos (cdr shifts)))
(goto-char (or (byte-to-position (+ pos (car shifts))) (goto-char (or (byte-to-position (+ pos (car shifts)))
(point-max))))) (point-max)))))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(set-visited-file-modtime)) (set-visited-file-modtime))
(defun vlfi-adjust-chunk (start end &optional position) (defun vlfi-adjust-chunk (start end &optional adjust-start adjust-end
"Adjust chunk at absolute START till END until content can be \ position)
properly decoded. Use buffer POSITION as start if given. "Adjust chunk at absolute START to END till content can be \
properly decoded. ADJUST-START determines if trying to prepend bytes\
to the beginning, ADJUST-END - add to the end.
Use buffer POSITION as start if given.
Return number of bytes moved back for proper decoding and number of Return number of bytes moved back for proper decoding and number of
bytes striped from the end character." bytes added to the end."
(let ((position (or position (point-min))) (let ((position (or position (point-min)))
(shift-start 0) (shift-start 0)
(shift-end 0) (shift-end 0)
(chunk-size (- end start))) (chunk-size (- end start)))
(while (and (not (zerop start)) ;; adjust beginning
(< shift-start 4) (if adjust-start
(< 4 (abs (- chunk-size (while (and (not (zerop start))
(length (encode-coding-region (< shift-start 4)
position (point-max) (< 4 (abs (- chunk-size
buffer-file-coding-system (length (encode-coding-region
t)))))) position (point-max)
(setq shift-start (1+ shift-start) buffer-file-coding-system
start (1- start) t))))))
chunk-size (1+ chunk-size)) (setq shift-start (1+ shift-start)
(delete-region position (point-max)) start (1- start)
(goto-char position) chunk-size (1+ chunk-size))
(insert-file-contents buffer-file-name nil start end)) (delete-region position (point-max))
(if (< end vlfi-file-size) (goto-char position)
(let ((point-max (point-max))) (insert-file-contents buffer-file-name nil start end)))
(setq shift-end (- (position-bytes point-max) ;; adjust end
(position-bytes (1- point-max)))) (when (and adjust-end (< end vlfi-file-size))
(delete-region (1- point-max) point-max))) ;delete last character, may be incomplete (let ((expected-size (buffer-size)))
(while (and (= expected-size (buffer-size))
(< end vlfi-file-size))
(setq shift-end (1+ shift-end)
end (1+ end))
(delete-region position (point-max))
(goto-char position)
(insert-file-contents buffer-file-name nil start end)))
(when (< end vlfi-file-size)
(setq shift-end (1- shift-end)
end (1- end))
(delete-region position (point-max))
(goto-char position)
(insert-file-contents buffer-file-name nil start end)))
(cons shift-start shift-end))) (cons shift-start shift-end)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -805,7 +826,7 @@ Save anyway? ")))
((< 0 size-change) ((< 0 size-change)
(vlfi-file-shift-back size-change)) (vlfi-file-shift-back size-change))
(t (vlfi-file-shift-forward (- size-change)))) (t (vlfi-file-shift-forward (- size-change))))
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos) (vlfi-move-to-chunk-2 vlfi-start-pos vlfi-end-pos)
(goto-char pos)) (goto-char pos))
(vlfi-mode)) (vlfi-mode))
t) t)