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

Fix chunk end adjustment and save for current and older Emacsen.

This commit is contained in:
Andrey Kotlarski 2013-12-08 21:04:26 +02:00
parent 959bbc7661
commit f4ee23c07f

78
vlf.el
View File

@ -373,9 +373,8 @@ Return t if move hasn't been canceled."
(inhibit-read-only t)) (inhibit-read-only t))
(cond ((< end edit-end) (cond ((< end edit-end)
(vlf-with-undo-disabled (vlf-with-undo-disabled
(delete-region (byte-to-position (1+ (delete-region (byte-to-position
(- end (1+ (- end vlf-start-pos)))
vlf-start-pos)))
(point-max)))) (point-max))))
((< edit-end end) ((< edit-end end)
(let ((edit-end-pos (point-max))) (let ((edit-end-pos (point-max)))
@ -432,43 +431,58 @@ Return t if move hasn't been canceled."
(defun vlf-adjust-chunk (start end &optional adjust-start adjust-end (defun vlf-adjust-chunk (start end &optional adjust-start adjust-end
position) position)
"Adjust chunk at absolute START to END till content can be \ "Adjust chunk at absolute START to END till content can be\
properly decoded. ADJUST-START determines if trying to prepend bytes\ properly decoded. ADJUST-START determines if trying to prepend bytes\
to the beginning, ADJUST-END - append to the end. to the beginning, ADJUST-END - append to the end.
Use buffer POSITION as start if given. 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 added to the end." bytes added to the end."
(let ((position (or position (point-min))) (let ((shift-start 0)
(shift-start 0) (shift-end 0))
(shift-end 0)
(chunk-size (- end start)))
;; adjust beginning
(if adjust-start (if adjust-start
(while (and (not (zerop start)) (let ((position (or position (point-min)))
(< shift-start 4) (chunk-size (- end 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))
;; adjust end (goto-char position)
(when (and adjust-end (< end vlf-file-size)) (insert-file-contents buffer-file-name nil start end))))
(let ((expected-size (buffer-size))) ; in case partial symbol is not displayed (if adjust-end
(while (and (= expected-size (buffer-size)) (cond ((vlf-partial-decode-shown-p) ;remove raw bytes from end
(< end vlf-file-size)) (goto-char (point-max))
(setq shift-end (1+ shift-end) (while (eq (char-charset (preceding-char)) 'eight-bit)
end (1+ end)) (setq shift-end (1- shift-end))
(delete-region position (point-max)) (delete-char -1)))
(goto-char position) ((< end vlf-file-size) ;add bytes until new character is displayed
(insert-file-contents buffer-file-name nil start end)))) (let ((position (or position (point-min)))
(expected-size (buffer-size)))
(while (and (progn
(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)
(< end vlf-file-size))
(= expected-size (buffer-size))))))))
(cons shift-start shift-end))) (cons shift-start shift-end)))
(defun vlf-partial-decode-shown-p ()
"Determine if partial decode codes are displayed.
This seems to be the case with GNU/Emacs before 24.4."
(cond ((< emacs-major-version 24) t)
((< 24 emacs-major-version) nil)
(t ;; TODO: use (< emacs-minor-version 4) after 24.4 release
(string-lessp emacs-version "24.3.5"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; search ;;; search