From 069b2f55d4c1ebe0d99b44500b8bce27915356cd Mon Sep 17 00:00:00 2001 From: Andrey Kotlarski Date: Sun, 31 Aug 2014 02:38:14 +0300 Subject: [PATCH] Replace operations with respective vlf-tune wrappers. --- vlf-base.el | 37 +++++++++++++-------------- vlf-occur.el | 7 +++--- vlf-search.el | 6 ++--- vlf-write.el | 69 +++++++++++++++++++++++++++++---------------------- 4 files changed, 61 insertions(+), 58 deletions(-) diff --git a/vlf-base.el b/vlf-base.el index 30e9b27..d3aaee4 100644 --- a/vlf-base.el +++ b/vlf-base.el @@ -27,6 +27,8 @@ ;;; Code: +(require 'vlf-tune) + (defcustom vlf-batch-size 1000000 "Defines how large each batch of file data initially is (in bytes)." :group 'vlf :type 'integer) @@ -133,11 +135,10 @@ bytes added to the end." (setq restore-hexl t hexl-undo-list buffer-undo-list buffer-undo-list t) - (hexl-mode-exit)) + (vlf-tune-dehexlify)) (+ vlf-start-pos - (length (encode-coding-region - (point-min) (point-max) - buffer-file-coding-system t)))) + (vlf-tune-encode-length (point-min) + (point-max)))) vlf-end-pos)) (shifts (cond @@ -161,7 +162,7 @@ bytes added to the end." (when (and hexl (not restore-hexl)) (if (consp buffer-undo-list) (setq buffer-undo-list nil)) - (hexl-mode-exit)) + (vlf-tune-dehexlify)) (let ((shift-start 0) (shift-end 0)) (let ((pos (+ (position-bytes (point)) vlf-start-pos)) @@ -224,12 +225,12 @@ bytes added to the end." (set-buffer-modified-p modified) (set-visited-file-modtime) (when hexl - (hexl-mode) + (vlf-tune-hexlify) (setq restore-hexl nil)) (run-hooks 'vlf-after-chunk-update) (cons shift-start shift-end)))))) (when restore-hexl - (hexl-mode) + (vlf-tune-hexlify) (setq buffer-undo-list hexl-undo-list)) shifts)) @@ -252,7 +253,7 @@ bytes added to the end." vlf-end-pos t t) vlf-start-pos (- vlf-start-pos (car shifts)) vlf-end-pos (+ vlf-end-pos (cdr shifts))) - (if hexl (hexl-mode))) + (if hexl (vlf-tune-hexlify))) (goto-char (or (byte-to-position (+ pos (car shifts))) (point-max))))) (set-buffer-modified-p nil) @@ -293,7 +294,7 @@ bytes added to the end." (defun vlf-insert-file-contents-1 (start end) "Extract decoded file bytes START to END." - (insert-file-contents buffer-file-name nil start end)) + (vlf-tune-insert-file-contents start end)) (defun vlf-adjust-start (start end position adjust-end) "Adjust chunk beginning at absolute START to END till content can\ @@ -311,10 +312,8 @@ Return number of bytes moved back for proper decoding." (not (zerop safe-start))) (< shift 3) (let ((diff (- chunk-size - (length - (encode-coding-region - position (point-max) - buffer-file-coding-system t))))) + (vlf-tune-encode-length position + (point-max))))) (if strict (not (zerop diff)) (or (< diff -3) (< 0 diff))))) @@ -345,12 +344,10 @@ which deletion was performed." (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))) - (+ start (length (encode-coding-region - position cut-point - buffer-file-coding-system t))))) + (- end (vlf-tune-encode-length cut-point + (point-max))) + (+ start (vlf-tune-encode-length position + cut-point)))) (len 0)) (if (< border dist) (while (< border dist) @@ -378,7 +375,7 @@ which deletion was performed." (defun vlf-shift-undo-list (n) "Shift undo list element regions by N." - (or (eq buffer-undo-list t) + (or (null buffer-undo-list) (eq buffer-undo-list t) (setq buffer-undo-list (nreverse (let ((min (point-min)) diff --git a/vlf-occur.el b/vlf-occur.el index 2dac4a4..5ce1643 100644 --- a/vlf-occur.el +++ b/vlf-occur.el @@ -124,7 +124,7 @@ EVENT may hold details of the invocation." pos-relative))) (cond (current-prefix-arg (setq vlf-buffer (vlf file t)) - (or not-hexl (hexl-mode)) + (or not-hexl (vlf-tune-hexlify)) (switch-to-buffer occur-buffer)) ((not (buffer-live-p vlf-buffer)) (unless (catch 'found @@ -137,7 +137,7 @@ EVENT may hold details of the invocation." (setq vlf-buffer buf) (throw 'found t)))) (setq vlf-buffer (vlf file t)) - (or not-hexl (hexl-mode))) + (or not-hexl (vlf-tune-hexlify))) (switch-to-buffer occur-buffer) (setq vlf-occur-vlf-buffer vlf-buffer))) (pop-to-buffer vlf-buffer) @@ -162,8 +162,7 @@ Prematurely ending indexing will still show what's found so far." (set-buffer-modified-p nil) (set (make-local-variable 'vlf-batch-size) batch-size) (vlf-mode 1) - (if is-hexl - (hexl-mode)) + (if is-hexl (vlf-tune-hexlify)) (run-hook-with-args 'vlf-before-batch-functions 'occur) (goto-char (point-min)) (vlf-with-undo-disabled diff --git a/vlf-search.el b/vlf-search.el index 3b81d57..0fe88fc 100644 --- a/vlf-search.el +++ b/vlf-search.el @@ -213,8 +213,7 @@ Search is performed chunk by chunk in `vlf-batch-size' memory." (while (and (< (- end start) n) (< n (- vlf-file-size start))) (erase-buffer) - (insert-file-contents-literally buffer-file-name - nil start end) + (vlf-tune-insert-file-contents-literally start end) (goto-char (point-min)) (while (re-search-forward "[\n\C-m]" nil t) (setq n (1- n))) @@ -239,8 +238,7 @@ Search is performed chunk by chunk in `vlf-batch-size' memory." (or is-hexl (while (and (< (- end start) n) (< n end)) (erase-buffer) - (insert-file-contents-literally buffer-file-name - nil start end) + (vlf-tune-insert-file-contents-literally start end) (goto-char (point-max)) (while (re-search-backward "[\n\C-m]" nil t) (setq n (1- n))) diff --git a/vlf-write.el b/vlf-write.el index 5c94113..969203e 100644 --- a/vlf-write.el +++ b/vlf-write.el @@ -43,28 +43,30 @@ If changing size of chunk, shift remaining file content." (when hexl (if (consp buffer-undo-list) (setq buffer-undo-list nil)) - (hexl-mode-exit)) + (vlf-tune-dehexlify)) (if (zerop vlf-file-size) ;new file - (progn (write-region nil nil buffer-file-name vlf-start-pos t) + (progn (vlf-tune-write nil nil vlf-start-pos t + (vlf-tune-encode-length (point-min) + (point-max))) (setq vlf-file-size (vlf-get-file-size buffer-file-truename) vlf-end-pos vlf-file-size) (vlf-update-buffer-name)) - (let* ((region-length (length (encode-coding-region - (point-min) (point-max) - buffer-file-coding-system t))) + (let* ((region-length (vlf-tune-encode-length (point-min) + (point-max))) (size-change (- vlf-end-pos vlf-start-pos region-length))) (if (zerop size-change) - (write-region nil nil buffer-file-name vlf-start-pos t) + (vlf-tune-write nil nil vlf-start-pos t + (- vlf-end-pos vlf-start-pos)) (let ((tramp-verbose (if (boundp 'tramp-verbose) (min tramp-verbose 2))) (pos (point)) (font-lock font-lock-mode)) (font-lock-mode 0) (if (< 0 size-change) - (vlf-file-shift-back size-change) - (vlf-file-shift-forward (- size-change))) + (vlf-file-shift-back size-change region-length) + (vlf-file-shift-forward (- size-change) region-length)) (if font-lock (font-lock-mode 1)) (vlf-move-to-chunk-2 vlf-start-pos (if (< (- vlf-end-pos vlf-start-pos) @@ -73,13 +75,14 @@ If changing size of chunk, shift remaining file content." vlf-end-pos)) (vlf-update-buffer-name) (goto-char pos))))) - (if hexl (hexl-mode))) + (if hexl (vlf-tune-hexlify))) (run-hook-with-args 'vlf-after-batch-functions 'write)) t) -(defun vlf-file-shift-back (size-change) - "Shift file contents SIZE-CHANGE bytes back." - (write-region nil nil buffer-file-name vlf-start-pos t) +(defun vlf-file-shift-back (size-change write-size) + "Shift file contents SIZE-CHANGE bytes back. +WRITE-SIZE is byte length of saved chunk." + (vlf-tune-write nil nil vlf-start-pos t write-size) (let ((read-start-pos vlf-end-pos) (coding-system-for-write 'no-conversion) (reporter (make-progress-reporter "Adjusting file content..." @@ -94,8 +97,8 @@ If changing size of chunk, shift remaining file content." (erase-buffer) (vlf-verify-size t) (insert-char 32 size-change)) - (write-region nil nil buffer-file-name (- vlf-file-size - size-change) t) + (vlf-tune-write nil nil (- vlf-file-size size-change) + t size-change) (progress-reporter-done reporter))) (defun vlf-shift-batch (read-pos write-pos) @@ -103,15 +106,14 @@ If changing size of chunk, shift remaining file content." back at WRITE-POS. Return nil if EOF is reached, t otherwise." (erase-buffer) (vlf-verify-size t) - (let ((read-end (+ read-pos vlf-batch-size))) - (insert-file-contents-literally buffer-file-name nil - read-pos - (min vlf-file-size read-end)) - (write-region nil nil buffer-file-name write-pos 0) + (let ((read-end (min (+ read-pos vlf-batch-size) vlf-file-size))) + (vlf-tune-insert-file-contents-literally read-pos read-end) + (vlf-tune-write nil nil write-pos 0 (- read-end read-pos)) (< read-end vlf-file-size))) -(defun vlf-file-shift-forward (size-change) +(defun vlf-file-shift-forward (size-change write-size) "Shift file contents SIZE-CHANGE bytes forward. +WRITE-SIZE is byte length of saved chunk. Done by saving content up front and then writing previous batch." (let ((read-size (max (/ vlf-batch-size 2) size-change)) (read-pos vlf-end-pos) @@ -120,20 +122,28 @@ Done by saving content up front and then writing previous batch." vlf-start-pos vlf-file-size))) (vlf-with-undo-disabled - (when (vlf-shift-batches read-size read-pos write-pos t) + (when (vlf-shift-batches read-size read-pos write-pos + write-size t) (setq write-pos (+ read-pos size-change) - read-pos (+ read-pos read-size)) + read-pos (+ read-pos read-size) + write-size read-size + read-size (max (/ vlf-batch-size 2) size-change)) (progress-reporter-update reporter write-pos) (let ((coding-system-for-write 'no-conversion)) - (while (vlf-shift-batches read-size read-pos write-pos nil) + (while (vlf-shift-batches read-size read-pos write-pos + write-size nil) (setq write-pos (+ read-pos size-change) - read-pos (+ read-pos read-size)) + read-pos (+ read-pos read-size) + write-size read-size + read-size (max (/ vlf-batch-size 2) size-change)) (progress-reporter-update reporter write-pos))))) (progress-reporter-done reporter))) -(defun vlf-shift-batches (read-size read-pos write-pos hide-read) +(defun vlf-shift-batches (read-size read-pos write-pos write-size + hide-read) "Append READ-SIZE bytes of file starting at READ-POS. Then write initial buffer content to file at WRITE-POS. +WRITE-SIZE is byte length of saved chunk. If HIDE-READ is non nil, temporarily hide literal read content. Return nil if EOF is reached, t otherwise." (vlf-verify-size t) @@ -142,14 +152,13 @@ Return nil if EOF is reached, t otherwise." (end-write-pos (point-max))) (when read-more (goto-char end-write-pos) - (insert-file-contents-literally buffer-file-name nil read-pos - (min vlf-file-size - (+ read-pos read-size)))) + (vlf-tune-insert-file-contents-literally + read-pos (min vlf-file-size (+ read-pos read-size)))) ;; write (if hide-read ; hide literal region if user has to choose encoding (narrow-to-region start-write-pos end-write-pos)) - (write-region start-write-pos end-write-pos - buffer-file-name write-pos 0) + (vlf-tune-write start-write-pos end-write-pos write-pos + (or (not read-more) 0) write-size) (delete-region start-write-pos end-write-pos) (if hide-read (widen)) read-more))