[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/vlf ca13bea 101/310: Try utf-8 and if not successful, a
From: |
Stefan Monnier |
Subject: |
[elpa] externals/vlf ca13bea 101/310: Try utf-8 and if not successful, auto detect when adjusting chunk for |
Date: |
Sat, 28 Nov 2020 00:32:55 -0500 (EST) |
branch: externals/vlf
commit ca13beac241036d60ff0bd1bc9ae84ac39a76012
Author: Andrey Kotlarski <m00naticus@gmail.com>
Commit: Andrey Kotlarski <m00naticus@gmail.com>
Try utf-8 and if not successful, auto detect when adjusting chunk for
proper decoding.
---
vlfi.el | 122 ++++++++++++++++++++++++++++++++++------------------------------
1 file changed, 64 insertions(+), 58 deletions(-)
diff --git a/vlfi.el b/vlfi.el
index 5e10c60..0d82fcc 100644
--- a/vlfi.el
+++ b/vlfi.el
@@ -296,6 +296,7 @@ When given MINIMAL flag, skip non important operations."
(let ((changed (not (verify-visited-file-modtime
(current-buffer))))
(modified (buffer-modified-p))
+ (inhibit-read-only t)
(start (max 0 start))
(end (min end vlfi-file-size)))
(if changed
@@ -310,13 +311,14 @@ When given MINIMAL flag, skip non important operations."
(throw 'abort nil))
(setq vlfi-start-pos start
vlfi-end-pos end)
- (let ((inhibit-read-only t)
- (pos (position-bytes (point))))
+ (let ((pos (position-bytes (point))))
(erase-buffer)
(insert-file-contents-literally
buffer-file-name nil vlfi-start-pos vlfi-end-pos)
- (goto-char (or (byte-to-position
- (+ pos (vlfi-adjust-chunk)))
+ (setq pos (+ pos (vlfi-prepare-chunk)))
+ (decode-coding-region (point-min) (point-max)
+ buffer-file-coding-system)
+ (goto-char (or (byte-to-position pos)
(point-max))))
(set-buffer-modified-p nil))
(if (and modified
@@ -324,19 +326,16 @@ When given MINIMAL flag, skip non important operations."
(< 3 (- start vlfi-start-pos)))
(not (y-or-n-p "Buffer modified, are you sure? ")))
(throw 'abort nil))
- (let ((pos (+ (position-bytes (point))
+ (let* ((pos (+ (position-bytes (point))
vlfi-start-pos))
- (adjust-encoding (or (< vlfi-end-pos end)
- (< start vlfi-start-pos)))
- (adjust-chunk (< start vlfi-start-pos)))
+ (adjust-chunk (< start vlfi-start-pos))
+ (adjust-encoding (or adjust-chunk
+ (< vlfi-end-pos end))))
(if adjust-encoding
- (let ((inhibit-read-only t))
- (encode-coding-region (point-min) (point-max)
- buffer-file-coding-system)))
- (vlfi-print-offset "require goto:" start end)
+ (encode-coding-region (point-min) (point-max)
+ buffer-file-coding-system))
(cond ((< end vlfi-end-pos) ; adjust ends
- (let ((inhibit-read-only t)
- (offset (- end vlfi-start-pos)))
+ (let ((offset (- end vlfi-start-pos)))
(if adjust-encoding
(progn (delete-region offset (point-max))
(setq vlfi-end-pos end)))
@@ -347,19 +346,16 @@ When given MINIMAL flag, skip non important operations."
(1- offset))))))
((< vlfi-end-pos end)
(goto-char (point-max))
- (let ((inhibit-read-only t))
- (insert-file-contents-literally
- buffer-file-name nil vlfi-end-pos end))
+ (insert-file-contents-literally
+ buffer-file-name nil vlfi-end-pos end)
(setq vlfi-end-pos end)))
(cond ((< start vlfi-start-pos) ; adjust start
(goto-char (point-min))
- (let ((inhibit-read-only t))
- (insert-file-contents-literally
- buffer-file-name nil start vlfi-start-pos))
+ (insert-file-contents-literally
+ buffer-file-name nil start vlfi-start-pos)
(setq vlfi-start-pos start))
((< 3 (- start vlfi-start-pos))
- (let ((inhibit-read-only t)
- (offset (- start vlfi-start-pos)))
+ (let ((offset (- start vlfi-start-pos)))
(if adjust-encoding
(progn (delete-region (point-min) offset)
(setq vlfi-start-pos start))
@@ -369,12 +365,11 @@ When given MINIMAL flag, skip non important operations."
(1+ offset))))
(delete-region (point-min) offset)))
(setq vlfi-start-pos start)))
- (vlfi-print-offset "actual goto:" vlfi-start-pos vlfi-end-pos)
- (if adjust-chunk (vlfi-adjust-chunk))
- (if adjust-encoding
- (let ((inhibit-read-only t))
- (decode-coding-region (point-min) (point-max)
- buffer-file-coding-system)))
+ (when adjust-encoding
+ (if adjust-chunk
+ (vlfi-prepare-chunk))
+ (decode-coding-region (point-min) (point-max)
+ buffer-file-coding-system))
(or modified (set-buffer-modified-p nil))
(goto-char
(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)))
(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 ()
- "Adjust chunk beginning until content can be properly decoded.
+(defun vlfi-prepare-chunk ()
+ "Apply proper decoding and adjust chunk start if needed.
Return number of bytes moved back for this to happen."
- (with-coding-priority '(utf-8)
- (setq buffer-file-coding-system 'utf-8;; (detect-coding-region (point-min)
- ;; (point-max)
- ;; t)
- )
- (let ((shift 0)
- (inhibit-read-only t))
- (while (and (not (zerop vlfi-start-pos))
- (< shift 3)
- (/= (- vlfi-end-pos vlfi-start-pos)
- (length (encode-coding-string
- (decode-coding-region
- (point-min) (point-max)
- buffer-file-coding-system t)
- buffer-file-coding-system t))))
- (setq shift (1+ shift)
- vlfi-start-pos (1- vlfi-start-pos))
- (goto-char (point-min))
- (insert-file-contents-literally
- buffer-file-name nil vlfi-start-pos (1+ vlfi-start-pos))
- (setq buffer-file-coding-system 'utf-8
- ;; (detect-coding-region (point-min) (point-max) t)
- ))
- (decode-coding-region (point-min) (point-max)
- buffer-file-coding-system)
- shift)))
+ (let ((status (vlfi-adjust-chunk 'utf-8)))
+ (unless (car status) ; no success with utf-8, auto-detect
+ (delete-region (point-min) (+ (point-min) (cdr status)))
+ (setq vlfi-start-pos (+ vlfi-start-pos (cdr status))
+ status (vlfi-adjust-chunk)))
+ (cdr status)))
+
+(defun vlfi-adjust-chunk (&optional encoding)
+ "Adjust chunk beginning until content can be properly decoded.
+Try with explicit ENCODING if given, otherwise auto-detect.
+Return cons \(success-status . number-of-bytes-moved-back\)."
+ (setq buffer-file-coding-system
+ (or encoding
+ (detect-coding-region (point-min) (point-max) t)))
+ (let ((shift 0)
+ (success nil)
+ (chunk-size (- vlfi-end-pos vlfi-start-pos)))
+ (while (and (< shift 4)
+ (not (setq success (vlfi-decode-status chunk-size)))
+ (not (zerop vlfi-start-pos)))
+ (goto-char (point-min))
+ (insert-file-contents-literally ; insert 1 byte
+ buffer-file-name nil (1- vlfi-start-pos) vlfi-start-pos)
+ (setq shift (1+ shift)
+ chunk-size (1+ chunk-size)
+ 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
- [elpa] externals/vlf ca8ba42 077/310: Fix position handling to correctly deal with multibyte characters., (continued)
- [elpa] externals/vlf ca8ba42 077/310: Fix position handling to correctly deal with multibyte characters., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 5aef3e1 078/310: Update file size changes more lazily., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf ba439de 083/310: Initial generation of vlfi-occur results., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 83eb8a5 080/310: Fixes for GNU Emacs 23., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 70719b0 089/310: Add vlfi-occur-show command for showing match but still staying in, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf a5fec57 090/310: Mostly documentation and commentary added., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf fbc3a37 094/310: Add tip for ability to change major mode and more section hierarchy., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf fd9c258 096/310: Use permanent buffer local write hook and update README., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf fbe0814 097/310: Fix occur indexing not to skip last chunk., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 28255a2 098/310: Correctly print MB (for older emacsen)., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf ca13bea 101/310: Try utf-8 and if not successful, auto detect when adjusting chunk for,
Stefan Monnier <=
- [elpa] externals/vlf ae775f8 107/310: Extend vlfi-goto-line to count lines from the end with negative, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 36d2ed4 109/310: Update README., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 3c2fb6b 111/310: Merge branch 'master' into chunk-opt2, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf a0b8d5f 115/310: Merge pull request #3 from danlamanna/master, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 8ed9192 117/310: Review fixes:, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 298d8f5 118/310: Keep track of current batch encoded size and allow fluctuation when, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 563d9ca 119/310: Fix vlfi-revert to revert on user confirmation., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 34dfbd2 121/310: Use set-visited-file-name and abstract getting current file size and, Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 6a1919d 001/310: Initial commit - original state of GNU ELPA vlf.el., Stefan Monnier, 2020/11/28
- [elpa] externals/vlf 2d0709b 003/310: Improvements to the existing functionality:, Stefan Monnier, 2020/11/28