mirror of
https://github.com/m00natic/vlfi.git
synced 2025-01-31 02:00:47 +00:00
Try utf-8 and if not successful, auto detect when adjusting chunk for
proper decoding.
This commit is contained in:
parent
d2bd47c2b4
commit
ca13beac24
122
vlfi.el
122
vlfi.el
@ -296,6 +296,7 @@ When given MINIMAL flag, skip non important operations."
|
|||||||
(let ((changed (not (verify-visited-file-modtime
|
(let ((changed (not (verify-visited-file-modtime
|
||||||
(current-buffer))))
|
(current-buffer))))
|
||||||
(modified (buffer-modified-p))
|
(modified (buffer-modified-p))
|
||||||
|
(inhibit-read-only t)
|
||||||
(start (max 0 start))
|
(start (max 0 start))
|
||||||
(end (min end vlfi-file-size)))
|
(end (min end vlfi-file-size)))
|
||||||
(if changed
|
(if changed
|
||||||
@ -310,13 +311,14 @@ When given MINIMAL flag, skip non important operations."
|
|||||||
(throw 'abort nil))
|
(throw 'abort nil))
|
||||||
(setq vlfi-start-pos start
|
(setq vlfi-start-pos start
|
||||||
vlfi-end-pos end)
|
vlfi-end-pos end)
|
||||||
(let ((inhibit-read-only t)
|
(let ((pos (position-bytes (point))))
|
||||||
(pos (position-bytes (point))))
|
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(insert-file-contents-literally
|
(insert-file-contents-literally
|
||||||
buffer-file-name nil vlfi-start-pos vlfi-end-pos)
|
buffer-file-name nil vlfi-start-pos vlfi-end-pos)
|
||||||
(goto-char (or (byte-to-position
|
(setq pos (+ pos (vlfi-prepare-chunk)))
|
||||||
(+ pos (vlfi-adjust-chunk)))
|
(decode-coding-region (point-min) (point-max)
|
||||||
|
buffer-file-coding-system)
|
||||||
|
(goto-char (or (byte-to-position pos)
|
||||||
(point-max))))
|
(point-max))))
|
||||||
(set-buffer-modified-p nil))
|
(set-buffer-modified-p nil))
|
||||||
(if (and modified
|
(if (and modified
|
||||||
@ -324,19 +326,16 @@ When given MINIMAL flag, skip non important operations."
|
|||||||
(< 3 (- start vlfi-start-pos)))
|
(< 3 (- start vlfi-start-pos)))
|
||||||
(not (y-or-n-p "Buffer modified, are you sure? ")))
|
(not (y-or-n-p "Buffer modified, are you sure? ")))
|
||||||
(throw 'abort nil))
|
(throw 'abort nil))
|
||||||
(let ((pos (+ (position-bytes (point))
|
(let* ((pos (+ (position-bytes (point))
|
||||||
vlfi-start-pos))
|
vlfi-start-pos))
|
||||||
(adjust-encoding (or (< vlfi-end-pos end)
|
(adjust-chunk (< start vlfi-start-pos))
|
||||||
(< start vlfi-start-pos)))
|
(adjust-encoding (or adjust-chunk
|
||||||
(adjust-chunk (< start vlfi-start-pos)))
|
(< vlfi-end-pos end))))
|
||||||
(if adjust-encoding
|
(if adjust-encoding
|
||||||
(let ((inhibit-read-only t))
|
(encode-coding-region (point-min) (point-max)
|
||||||
(encode-coding-region (point-min) (point-max)
|
buffer-file-coding-system))
|
||||||
buffer-file-coding-system)))
|
|
||||||
(vlfi-print-offset "require goto:" start end)
|
|
||||||
(cond ((< end vlfi-end-pos) ; adjust ends
|
(cond ((< end vlfi-end-pos) ; adjust ends
|
||||||
(let ((inhibit-read-only t)
|
(let ((offset (- end vlfi-start-pos)))
|
||||||
(offset (- end vlfi-start-pos)))
|
|
||||||
(if adjust-encoding
|
(if adjust-encoding
|
||||||
(progn (delete-region offset (point-max))
|
(progn (delete-region offset (point-max))
|
||||||
(setq vlfi-end-pos end)))
|
(setq vlfi-end-pos end)))
|
||||||
@ -347,19 +346,16 @@ When given MINIMAL flag, skip non important operations."
|
|||||||
(1- offset))))))
|
(1- offset))))))
|
||||||
((< vlfi-end-pos end)
|
((< vlfi-end-pos end)
|
||||||
(goto-char (point-max))
|
(goto-char (point-max))
|
||||||
(let ((inhibit-read-only t))
|
(insert-file-contents-literally
|
||||||
(insert-file-contents-literally
|
buffer-file-name nil vlfi-end-pos end)
|
||||||
buffer-file-name nil vlfi-end-pos end))
|
|
||||||
(setq vlfi-end-pos end)))
|
(setq vlfi-end-pos end)))
|
||||||
(cond ((< start vlfi-start-pos) ; adjust start
|
(cond ((< start vlfi-start-pos) ; adjust start
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(let ((inhibit-read-only t))
|
(insert-file-contents-literally
|
||||||
(insert-file-contents-literally
|
buffer-file-name nil start vlfi-start-pos)
|
||||||
buffer-file-name nil start vlfi-start-pos))
|
|
||||||
(setq vlfi-start-pos start))
|
(setq vlfi-start-pos start))
|
||||||
((< 3 (- start vlfi-start-pos))
|
((< 3 (- start vlfi-start-pos))
|
||||||
(let ((inhibit-read-only t)
|
(let ((offset (- start vlfi-start-pos)))
|
||||||
(offset (- start vlfi-start-pos)))
|
|
||||||
(if adjust-encoding
|
(if adjust-encoding
|
||||||
(progn (delete-region (point-min) offset)
|
(progn (delete-region (point-min) offset)
|
||||||
(setq vlfi-start-pos start))
|
(setq vlfi-start-pos start))
|
||||||
@ -369,12 +365,11 @@ When given MINIMAL flag, skip non important operations."
|
|||||||
(1+ offset))))
|
(1+ offset))))
|
||||||
(delete-region (point-min) offset)))
|
(delete-region (point-min) offset)))
|
||||||
(setq vlfi-start-pos start)))
|
(setq vlfi-start-pos start)))
|
||||||
(vlfi-print-offset "actual goto:" vlfi-start-pos vlfi-end-pos)
|
(when adjust-encoding
|
||||||
(if adjust-chunk (vlfi-adjust-chunk))
|
(if adjust-chunk
|
||||||
(if adjust-encoding
|
(vlfi-prepare-chunk))
|
||||||
(let ((inhibit-read-only t))
|
(decode-coding-region (point-min) (point-max)
|
||||||
(decode-coding-region (point-min) (point-max)
|
buffer-file-coding-system))
|
||||||
buffer-file-coding-system)))
|
|
||||||
(or modified (set-buffer-modified-p nil))
|
(or modified (set-buffer-modified-p nil))
|
||||||
(goto-char
|
(goto-char
|
||||||
(cond ((< pos vlfi-start-pos) (point-min))
|
(cond ((< pos vlfi-start-pos) (point-min))
|
||||||
@ -384,38 +379,49 @@ When given MINIMAL flag, skip non important operations."
|
|||||||
(if changed (set-visited-file-modtime)))
|
(if changed (set-visited-file-modtime)))
|
||||||
(or minimal (vlfi-update-buffer-name))))
|
(or minimal (vlfi-update-buffer-name))))
|
||||||
|
|
||||||
(defun vlfi-print-offset (text start end)
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
(message "%s: %d %d" text start end))
|
;;; encoding
|
||||||
|
|
||||||
(defun vlfi-adjust-chunk ()
|
(defun vlfi-prepare-chunk ()
|
||||||
"Adjust chunk beginning until content can be properly decoded.
|
"Apply proper decoding and adjust chunk start if needed.
|
||||||
Return number of bytes moved back for this to happen."
|
Return number of bytes moved back for this to happen."
|
||||||
(with-coding-priority '(utf-8)
|
(let ((status (vlfi-adjust-chunk 'utf-8)))
|
||||||
(setq buffer-file-coding-system 'utf-8;; (detect-coding-region (point-min)
|
(unless (car status) ; no success with utf-8, auto-detect
|
||||||
;; (point-max)
|
(delete-region (point-min) (+ (point-min) (cdr status)))
|
||||||
;; t)
|
(setq vlfi-start-pos (+ vlfi-start-pos (cdr status))
|
||||||
)
|
status (vlfi-adjust-chunk)))
|
||||||
(let ((shift 0)
|
(cdr status)))
|
||||||
(inhibit-read-only t))
|
|
||||||
(while (and (not (zerop vlfi-start-pos))
|
(defun vlfi-adjust-chunk (&optional encoding)
|
||||||
(< shift 3)
|
"Adjust chunk beginning until content can be properly decoded.
|
||||||
(/= (- vlfi-end-pos vlfi-start-pos)
|
Try with explicit ENCODING if given, otherwise auto-detect.
|
||||||
(length (encode-coding-string
|
Return cons \(success-status . number-of-bytes-moved-back\)."
|
||||||
(decode-coding-region
|
(setq buffer-file-coding-system
|
||||||
(point-min) (point-max)
|
(or encoding
|
||||||
buffer-file-coding-system t)
|
(detect-coding-region (point-min) (point-max) t)))
|
||||||
buffer-file-coding-system t))))
|
(let ((shift 0)
|
||||||
(setq shift (1+ shift)
|
(success nil)
|
||||||
vlfi-start-pos (1- vlfi-start-pos))
|
(chunk-size (- vlfi-end-pos vlfi-start-pos)))
|
||||||
(goto-char (point-min))
|
(while (and (< shift 4)
|
||||||
(insert-file-contents-literally
|
(not (setq success (vlfi-decode-status chunk-size)))
|
||||||
buffer-file-name nil vlfi-start-pos (1+ vlfi-start-pos))
|
(not (zerop vlfi-start-pos)))
|
||||||
(setq buffer-file-coding-system 'utf-8
|
(goto-char (point-min))
|
||||||
;; (detect-coding-region (point-min) (point-max) t)
|
(insert-file-contents-literally ; insert 1 byte
|
||||||
))
|
buffer-file-name nil (1- vlfi-start-pos) vlfi-start-pos)
|
||||||
(decode-coding-region (point-min) (point-max)
|
(setq shift (1+ shift)
|
||||||
buffer-file-coding-system)
|
chunk-size (1+ chunk-size)
|
||||||
shift)))
|
vlfi-start-pos (1- vlfi-start-pos)
|
||||||
|
buffer-file-coding-system
|
||||||
|
(or encoding
|
||||||
|
(detect-coding-region (point-min) (point-max) t))))
|
||||||
|
(cons success shift)))
|
||||||
|
|
||||||
|
(defun vlfi-decode-status (size)
|
||||||
|
"Check if decoding followed by encoding results in SIZE bytes."
|
||||||
|
(= size (length (encode-coding-string
|
||||||
|
(decode-coding-region (point-min) (point-max)
|
||||||
|
buffer-file-coding-system t)
|
||||||
|
buffer-file-coding-system t))))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;;; search
|
;;; search
|
||||||
|
Loading…
x
Reference in New Issue
Block a user