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

Add end character shift when adjusting chunk. Apply partial chunk

update when buffer is not modified too.
This commit is contained in:
Andrey Kotlarski 2013-08-09 01:59:32 +03:00
parent 0ecb40a5ee
commit 2d71996d9e

120
vlfi.el
View File

@ -197,7 +197,7 @@ with the prefix argument DECREASE it is halved."
(format "%s(%s)[%d/%d](%d)" (format "%s(%s)[%d/%d](%d)"
(file-name-nondirectory buffer-file-name) (file-name-nondirectory buffer-file-name)
(file-size-human-readable vlfi-file-size) (file-size-human-readable vlfi-file-size)
(/ vlfi-end-pos vlfi-batch-size) (/ (+ vlfi-end-pos 4) vlfi-batch-size)
(/ vlfi-file-size vlfi-batch-size) (/ vlfi-file-size vlfi-batch-size)
vlfi-batch-size)) vlfi-batch-size))
@ -302,46 +302,33 @@ 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)
(if (buffer-modified-p) (and (vlfi-move-to-chunk-1 start end)
(and (vlfi-move-to-chunk-2 start end) (not minimal)
(not minimal) (vlfi-update-buffer-name)))
(vlfi-update-buffer-name))
(vlfi-move-to-chunk-1 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." "Move to chunk determined by START END keeping as much changes.
(setq vlfi-start-pos (max 0 start)
vlfi-end-pos (min end vlfi-file-size))
(let ((inhibit-read-only t)
(pos (position-bytes (point))))
(erase-buffer)
(insert-file-contents buffer-file-name nil
vlfi-start-pos vlfi-end-pos)
(let ((shift (vlfi-adjust-chunk vlfi-start-pos vlfi-end-pos)))
(setq vlfi-start-pos (- vlfi-start-pos shift))
(goto-char (or (byte-to-position (+ pos shift))
(point-max)))))
(set-buffer-modified-p nil)
(set-visited-file-modtime))
(defun vlfi-move-to-chunk-2 (start end)
"Move to chunk determined by START END.
Return t if move hasn't been canceled." Return t if move hasn't been canceled."
(let ((start (max 0 start)) (let ((modified (buffer-modified-p))
(start (max 0 start))
(end (min end vlfi-file-size)) (end (min end vlfi-file-size))
(edit-end (+ (position-bytes (point-max)) vlfi-start-pos))) (edit-end (+ (position-bytes (point-max)) vlfi-start-pos)))
(cond (cond
((and (= start vlfi-start-pos) (= end edit-end)) ((and (= start vlfi-start-pos) (= end edit-end))
nil) (unless modified
(vlfi-move-to-chunk-2 start end)
t))
((or (<= edit-end start) (<= end vlfi-start-pos)) ((or (<= edit-end start) (<= end vlfi-start-pos))
(when (y-or-n-p "Buffer modified, are you sure? ") ; full chunk renewal (when (or (not modified)
(vlfi-move-to-chunk-1 start end) (y-or-n-p "Buffer modified, are you sure? ")) ; full chunk renewal
(vlfi-move-to-chunk-2 start end)
t)) t))
((or (and (<= start vlfi-start-pos) (<= edit-end end)) ((or (and (<= start vlfi-start-pos) (<= edit-end end))
(not modified)
(y-or-n-p "Buffer modified, are you sure? ")) (y-or-n-p "Buffer modified, are you sure? "))
(let ((pos (+ (position-bytes (point)) vlfi-start-pos)) (let ((pos (+ (position-bytes (point)) vlfi-start-pos))
(shift 0) (shift-start 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 (- end
@ -352,7 +339,9 @@ Return t if move hasn't been canceled."
(goto-char edit-end-pos) (goto-char edit-end-pos)
(insert-file-contents buffer-file-name nil (insert-file-contents buffer-file-name nil
vlfi-end-pos end) vlfi-end-pos end)
(vlfi-adjust-chunk vlfi-end-pos end edit-end-pos)))) (setq shift-end (cdr (vlfi-adjust-chunk
vlfi-end-pos end
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
(- start vlfi-start-pos)))) (- start vlfi-start-pos))))
@ -361,46 +350,65 @@ Return t if move hasn't been canceled."
(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 vlfi-start-pos) start vlfi-start-pos)
(setq shift (vlfi-adjust-chunk start vlfi-start-pos (setq shift-start (car
edit-end-pos)) (vlfi-adjust-chunk start
vlfi-start-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) (setq vlfi-start-pos (- start shift-start)
vlfi-end-pos 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-visited-file-modtime) (set-buffer-modified-p modified)
t)))) t))))
(defun vlfi-move-to-chunk-2 (start end)
"Move to chunk determined by START END."
(setq vlfi-start-pos (max 0 start)
vlfi-end-pos (min end vlfi-file-size))
(let ((inhibit-read-only t)
(pos (position-bytes (point))))
(erase-buffer)
(insert-file-contents buffer-file-name nil
vlfi-start-pos vlfi-end-pos)
(let ((shifts (vlfi-adjust-chunk vlfi-start-pos vlfi-end-pos)))
(setq vlfi-start-pos (- vlfi-start-pos (car shifts))
vlfi-end-pos (- vlfi-end-pos (cdr shifts)))
(goto-char (or (byte-to-position (+ pos (car shifts)))
(point-max)))))
(set-buffer-modified-p nil)
(set-visited-file-modtime))
(defun vlfi-adjust-chunk (start end &optional position) (defun vlfi-adjust-chunk (start end &optional position)
"Adjust chunk at absolute START till END until content can be \ "Adjust chunk at absolute START till END until content can be \
properly decoded. Use buffer POSITION as start if given. properly decoded. Use buffer POSITION as start if given.
Set `vlfi-encode-size' to size of buffer when encoded. Return number of bytes moved back for proper decoding and number of
Return number of bytes moved back for this to happen." bytes striped from the end character."
(let ((update-encode-size (not position)) (let ((position (or position (point-min)))
(encode-size 0) (shift-start 0)
(position (or position (point-min))) (shift-end 0)
(shift 0)
(chunk-size (- end start))) (chunk-size (- end start)))
(while (and (< shift 4) (while (and (not (zerop start))
(< shift-start 4)
(< 4 (abs (- chunk-size (< 4 (abs (- chunk-size
(setq encode-size (length (encode-coding-region
(length (encode-coding-region position (point-max)
position (point-max) buffer-file-coding-system
buffer-file-coding-system t))))))
t)))))) (setq shift-start (1+ shift-start)
(not (zerop start)))
(setq shift (1+ shift)
start (1- start) start (1- start)
chunk-size (1+ chunk-size)) chunk-size (1+ chunk-size))
(let ((inhibit-read-only t)) (delete-region position (point-max))
(delete-region position (point-max)) (goto-char position)
(goto-char position) (insert-file-contents buffer-file-name nil start end))
(insert-file-contents buffer-file-name nil start end))) (if (< end vlfi-file-size)
(if update-encode-size (let ((point-max (point-max)))
(setq vlfi-encode-size encode-size)) (setq shift-end (- (position-bytes point-max)
shift)) (position-bytes (1- point-max))))
(delete-region (1- point-max) point-max))) ;delete last character, may be incomplete
(cons shift-start shift-end)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; search ;;; search