1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-04-19 01:00:19 +01:00

Unify and optimize deletion.

This commit is contained in:
Andrey Kotlarski 2014-01-20 02:37:51 +02:00
parent 82fd5b943b
commit e747de4495

View File

@ -147,36 +147,28 @@ bytes added to the end."
(let ((pos (+ (position-bytes (point)) vlf-start-pos)) (let ((pos (+ (position-bytes (point)) vlf-start-pos))
(inhibit-read-only t)) (inhibit-read-only t))
(cond ((< end edit-end) (cond ((< end edit-end)
(let* ((del-pos (or (byte-to-position (setq end (car (vlf-delete-region
(1+ (- end vlf-start-pos))) (point-min) vlf-start-pos edit-end
(point-min))) end (or (byte-to-position
(del-len (length (encode-coding-region (- end vlf-start-pos))
del-pos (point-max) (point-min))
buffer-file-coding-system nil))))
t))))
(setq end (- (if (zerop vlf-end-pos)
vlf-file-size
vlf-end-pos)
del-len))
(vlf-with-undo-disabled
(delete-region del-pos (point-max)))))
((< edit-end end) ((< edit-end end)
(vlf-with-undo-disabled (vlf-with-undo-disabled
(setq shift-end (cdr (vlf-insert-file-contents (setq shift-end (cdr (vlf-insert-file-contents
vlf-end-pos end vlf-end-pos end
(/= start vlf-end-pos) t (/= start vlf-end-pos) t
(point-max))))))) (point-max)))))))
(setq vlf-end-pos (+ end shift-end))
(cond ((< vlf-start-pos start) (cond ((< vlf-start-pos start)
(let* ((del-pos (byte-to-position (let ((del-info (vlf-delete-region
(1+ (- start vlf-start-pos)))) (point-min) vlf-start-pos
(del-len (length (encode-coding-region vlf-end-pos start
(point-min) del-pos (byte-to-position
buffer-file-coding-system (- start vlf-start-pos)) t)))
t)))) (setq start (car del-info))
(setq start (+ vlf-start-pos del-len)) (vlf-shift-undo-list (- (point-min)
(vlf-with-undo-disabled (cdr del-info)))))
(delete-region (point-min) del-pos))
(vlf-shift-undo-list (- (point-min) del-pos))))
((< start vlf-start-pos) ((< start vlf-start-pos)
(let ((edit-end-pos (point-max))) (let ((edit-end-pos (point-max)))
(vlf-with-undo-disabled (vlf-with-undo-disabled
@ -187,13 +179,13 @@ bytes added to the end."
(goto-char (point-min)) (goto-char (point-min))
(insert (delete-and-extract-region (insert (delete-and-extract-region
edit-end-pos (point-max)))) edit-end-pos (point-max))))
(vlf-shift-undo-list (- (point-max) edit-end-pos))))) (vlf-shift-undo-list (- (point-max)
edit-end-pos)))))
(setq start (- start shift-start)) (setq start (- start shift-start))
(goto-char (or (byte-to-position (- pos start)) (goto-char (or (byte-to-position (- pos start))
(byte-to-position (- pos vlf-start-pos)) (byte-to-position (- pos vlf-start-pos))
(point-max))) (point-max)))
(setq vlf-start-pos start (setq vlf-start-pos start))
vlf-end-pos (+ end shift-end)))
(set-buffer-modified-p modified) (set-buffer-modified-p modified)
(set-visited-file-modtime) (set-visited-file-modtime)
(cons shift-start shift-end)))))) (cons shift-start shift-end))))))
@ -241,11 +233,24 @@ bytes added to the end."
(setq shift-start (vlf-adjust-start start safe-end position (setq shift-start (vlf-adjust-start start safe-end position
adjust-end) adjust-end)
start (- start shift-start)) start (- start shift-start))
(vlf-insert-file-contents-safe start safe-end position)) (vlf-insert-file-contents-1 start safe-end position))
(if adjust-end (if adjust-end
(setq shift-end (vlf-adjust-end start end position))) (setq shift-end (- (car (vlf-delete-region position start
safe-end end
(point-max)
nil 'start))
end)))
(cons shift-start shift-end))) (cons shift-start shift-end)))
(defun vlf-insert-file-contents-1 (start end position)
"Extract decoded file bytes START to END at POSITION."
(let ((coding buffer-file-coding-system))
(insert-file-contents-literally buffer-file-name nil start end)
(let ((coding-system-for-read coding))
(decode-coding-inserted-region position (point-max)
buffer-file-name nil start end)))
(setq buffer-file-coding-system last-coding-system-used))
(defun vlf-adjust-start (start end position adjust-end) (defun vlf-adjust-start (start end position adjust-end)
"Adjust chunk beginning at absolute START to END till content can\ "Adjust chunk beginning at absolute START to END till content can\
be properly decoded. Use buffer POSITION as start. be properly decoded. Use buffer POSITION as start.
@ -257,7 +262,7 @@ Return number of bytes moved back for proper decoding."
(strict (or (= sample-end vlf-file-size) (strict (or (= sample-end vlf-file-size)
(and (not adjust-end) (= sample-end end)))) (and (not adjust-end) (= sample-end end))))
(shift 0)) (shift 0))
(while (and (progn (vlf-insert-file-contents-safe (while (and (progn (vlf-insert-file-contents-1
safe-start sample-end position) safe-start sample-end position)
(not (zerop safe-start))) (not (zerop safe-start)))
(< shift 3) (< shift 3)
@ -273,53 +278,59 @@ Return number of bytes moved back for proper decoding."
safe-start (1- safe-start) safe-start (1- safe-start)
chunk-size (1+ chunk-size)) chunk-size (1+ chunk-size))
(delete-region position (point-max))) (delete-region position (point-max)))
(let ((cut-pos position) (setq safe-start (car (vlf-delete-region position safe-start
(cut-len 0)) sample-end start
(while (< safe-start start) position t 'start)))
(setq cut-len (length (encode-coding-region (unless (= sample-end end)
cut-pos (1+ cut-pos)
buffer-file-coding-system t))
cut-pos (1+ cut-pos)
safe-start (+ safe-start cut-len)))
(if (< start safe-start)
(setq safe-start (- safe-start cut-len)
cut-pos (1- cut-pos)))
(if (= sample-end end)
(delete-region position cut-pos)
(delete-region position (point-max)) (delete-region position (point-max))
(vlf-insert-file-contents-safe safe-start end position))) (vlf-insert-file-contents-1 safe-start end position))
(- start safe-start))) (- start safe-start)))
(defun vlf-adjust-end (start end position) (defun vlf-delete-region (position start end border cut-point from-start
"Adjust chunk end at absolute START to END starting at POSITION. &optional encode-direction)
Remove characters from the end until length is closest to expected. "Delete from chunk starting at POSITION enclosing absolute file\
Return number of bytes added over expected." positions START to END at absolute position BORDER. Start search for
(let ((expected-size (- end start)) best cut at CUT-POINT. Delete from buffer beginning if FROM-START is
(current-size (length (encode-coding-region non nil or up to buffer end otherwise. ENCODE-DIRECTION determines
position (point-max) which side of the region to use to calculate cut position's absolute
file position. Possible values are: `start' - from the beginning;
`end' - from end; nil - the shorter side.
Return actual absolute position of new border and buffer point at
which deletion was performed."
(let* ((encode-from-end (if encode-direction
(eq encode-direction 'end)
(< (- end border) (- border start))))
(dist (if encode-from-end
(- end (length (encode-coding-region
cut-point (point-max)
buffer-file-coding-system t))) buffer-file-coding-system t)))
(cut-point (point-max)) (+ start (length (encode-coding-region
(cut-len 0)) position cut-point
(while (< expected-size current-size) buffer-file-coding-system t)))))
(setq cut-len (length (encode-coding-region (len 0))
(1- cut-point) cut-point (if (< border dist)
(while (< border dist)
(setq len (length (encode-coding-region
cut-point (1- cut-point)
buffer-file-coding-system t)) buffer-file-coding-system t))
cut-point (1- cut-point) cut-point (1- cut-point)
current-size (- current-size cut-len))) dist (- dist len)))
(if (< current-size expected-size) (while (< dist border)
(setq len (length (encode-coding-region
cut-point (1+ cut-point)
buffer-file-coding-system t))
cut-point (1+ cut-point)
dist (+ dist len)))
(or (= dist border)
(setq cut-point (1- cut-point)
dist (- dist len))))
(and (not from-start) (/= dist border)
(setq cut-point (1+ cut-point) (setq cut-point (1+ cut-point)
current-size (+ current-size cut-len))) dist (+ dist len)))
(delete-region cut-point (point-max)) (vlf-with-undo-disabled
(- current-size expected-size))) (if from-start (delete-region position cut-point)
(delete-region cut-point (point-max))))
(defun vlf-insert-file-contents-safe (start end position) (cons dist (1+ cut-point))))
"Extract decoded file bytes START to END at POSITION."
(let ((coding buffer-file-coding-system))
(insert-file-contents-literally buffer-file-name nil start end)
(let ((coding-system-for-read coding))
(decode-coding-inserted-region position (point-max)
buffer-file-name nil start end)))
(setq buffer-file-coding-system last-coding-system-used))
(defun vlf-shift-undo-list (n) (defun vlf-shift-undo-list (n)
"Shift undo list element regions by N." "Shift undo list element regions by N."