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

Use buffer-file-truename for file size determination and remove

superfluous checks to vlf-verify-size when saving.  Fix vlf-revert not
to ask unnecessary.
This commit is contained in:
Andrey Kotlarski 2013-12-13 02:32:30 +02:00
parent 655805ce48
commit db1da304d5

70
vlf.el
View File

@ -122,7 +122,7 @@ Possible values are: nil to never use it;
'vlf-revert) 'vlf-revert)
(make-local-variable 'vlf-batch-size) (make-local-variable 'vlf-batch-size)
(set (make-local-variable 'vlf-file-size) (set (make-local-variable 'vlf-file-size)
(vlf-get-file-size buffer-file-name)) (vlf-get-file-size buffer-file-truename))
(set (make-local-variable 'vlf-start-pos) 0) (set (make-local-variable 'vlf-start-pos) 0)
(set (make-local-variable 'vlf-end-pos) 0) (set (make-local-variable 'vlf-end-pos) 0)
(let* ((pos (position-bytes (point))) (let* ((pos (position-bytes (point)))
@ -295,12 +295,12 @@ with the prefix argument DECREASE it is halved."
(defun vlf-get-file-size (file) (defun vlf-get-file-size (file)
"Get size in bytes of FILE." "Get size in bytes of FILE."
(or (nth 7 (file-attributes (file-truename file))) 0)) (or (nth 7 (file-attributes file)) 0))
(defun vlf-verify-size () (defun vlf-verify-size ()
"Update file size information if necessary and visited file time." "Update file size information if necessary and visited file time."
(unless (verify-visited-file-modtime (current-buffer)) (unless (verify-visited-file-modtime (current-buffer))
(setq vlf-file-size (vlf-get-file-size buffer-file-name)) (setq vlf-file-size (vlf-get-file-size buffer-file-truename))
(set-visited-file-modtime))) (set-visited-file-modtime)))
(defun vlf-insert-file (&optional from-end) (defun vlf-insert-file (&optional from-end)
@ -332,6 +332,7 @@ Ask for confirmation if NOCONFIRM is nil."
(yes-or-no-p (format "Revert buffer from file %s? " (yes-or-no-p (format "Revert buffer from file %s? "
buffer-file-name))) buffer-file-name)))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(set-visited-file-modtime)
(vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos))) (vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)))
(defun vlf-jump-to-chunk (n) (defun vlf-jump-to-chunk (n)
@ -955,35 +956,37 @@ in file: %s" total-matches line regexp file)
"Write current chunk to file. Always return true to disable save. "Write current chunk to file. Always return true to disable save.
If changing size of chunk, shift remaining file content." If changing size of chunk, shift remaining file content."
(interactive) (interactive)
(when (and (buffer-modified-p) (and (buffer-modified-p)
(or (verify-visited-file-modtime (current-buffer)) (or (verify-visited-file-modtime (current-buffer))
(y-or-n-p "File has changed since visited or saved. \ (y-or-n-p "File has changed since visited or saved. \
Save anyway? "))) Save anyway? "))
(if (zerop vlf-file-size) ;new file (if (zerop vlf-file-size) ;new file
(progn (progn
(write-region nil nil buffer-file-name vlf-start-pos t) (write-region nil nil buffer-file-name vlf-start-pos t)
(setq vlf-file-size (vlf-get-file-size buffer-file-name) (setq vlf-file-size (vlf-get-file-size
vlf-end-pos vlf-file-size) buffer-file-truename)
(vlf-update-buffer-name)) vlf-end-pos vlf-file-size)
(let* ((region-length (length (encode-coding-region (vlf-update-buffer-name))
(point-min) (point-max) (vlf-verify-size)
buffer-file-coding-system t))) (let* ((region-length (length (encode-coding-region
(size-change (- vlf-end-pos vlf-start-pos (point-min) (point-max)
region-length))) buffer-file-coding-system t)))
(if (zerop size-change) (size-change (- vlf-end-pos vlf-start-pos
(write-region nil nil buffer-file-name vlf-start-pos t) region-length)))
(let ((pos (point))) (if (zerop size-change)
(if (< 0 size-change) (write-region nil nil buffer-file-name vlf-start-pos t)
(vlf-file-shift-back size-change) (let ((pos (point)))
(vlf-file-shift-forward (- size-change))) (if (< 0 size-change)
(setq vlf-file-size (vlf-get-file-size buffer-file-name)) (vlf-file-shift-back size-change)
(vlf-move-to-chunk-2 vlf-start-pos (vlf-file-shift-forward (- size-change)))
(if (< (- vlf-end-pos vlf-start-pos) (vlf-verify-size)
vlf-batch-size) (vlf-move-to-chunk-2 vlf-start-pos
(+ vlf-start-pos vlf-batch-size) (if (< (- vlf-end-pos vlf-start-pos)
vlf-end-pos)) vlf-batch-size)
(vlf-update-buffer-name) (+ vlf-start-pos vlf-batch-size)
(goto-char pos)))))) vlf-end-pos))
(vlf-update-buffer-name)
(goto-char pos))))))
t) t)
(defun vlf-file-shift-back (size-change) (defun vlf-file-shift-back (size-change)
@ -1001,7 +1004,6 @@ Save anyway? ")))
(progress-reporter-update reporter read-start-pos)) (progress-reporter-update reporter read-start-pos))
;; pad end with space ;; pad end with space
(erase-buffer) (erase-buffer)
(vlf-verify-size)
(insert-char 32 size-change)) (insert-char 32 size-change))
(write-region nil nil buffer-file-name (- vlf-file-size (write-region nil nil buffer-file-name (- vlf-file-size
size-change) t) size-change) t)
@ -1011,7 +1013,6 @@ Save anyway? ")))
"Read `vlf-batch-size' bytes from READ-POS and write them \ "Read `vlf-batch-size' bytes from READ-POS and write them \
back at WRITE-POS. Return nil if EOF is reached, t otherwise." back at WRITE-POS. Return nil if EOF is reached, t otherwise."
(erase-buffer) (erase-buffer)
(vlf-verify-size)
(let ((read-end (+ read-pos vlf-batch-size))) (let ((read-end (+ read-pos vlf-batch-size)))
(insert-file-contents-literally buffer-file-name nil (insert-file-contents-literally buffer-file-name nil
read-pos read-pos
@ -1045,7 +1046,6 @@ Done by saving content up front and then writing previous batch."
Then write initial buffer content to file at WRITE-POS. Then write initial buffer content to file at WRITE-POS.
If HIDE-READ is non nil, temporarily hide literal read content. If HIDE-READ is non nil, temporarily hide literal read content.
Return nil if EOF is reached, t otherwise." Return nil if EOF is reached, t otherwise."
(vlf-verify-size)
(let ((read-more (< read-pos vlf-file-size)) (let ((read-more (< read-pos vlf-file-size))
(start-write-pos (point-min)) (start-write-pos (point-min))
(end-write-pos (point-max))) (end-write-pos (point-max)))