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

Replace operations with respective vlf-tune wrappers.

This commit is contained in:
Andrey Kotlarski 2014-08-31 02:38:14 +03:00
parent 32ff2cb067
commit 069b2f55d4
4 changed files with 61 additions and 58 deletions

View File

@ -27,6 +27,8 @@
;;; Code: ;;; Code:
(require 'vlf-tune)
(defcustom vlf-batch-size 1000000 (defcustom vlf-batch-size 1000000
"Defines how large each batch of file data initially is (in bytes)." "Defines how large each batch of file data initially is (in bytes)."
:group 'vlf :type 'integer) :group 'vlf :type 'integer)
@ -133,11 +135,10 @@ bytes added to the end."
(setq restore-hexl t (setq restore-hexl t
hexl-undo-list buffer-undo-list hexl-undo-list buffer-undo-list
buffer-undo-list t) buffer-undo-list t)
(hexl-mode-exit)) (vlf-tune-dehexlify))
(+ vlf-start-pos (+ vlf-start-pos
(length (encode-coding-region (vlf-tune-encode-length (point-min)
(point-min) (point-max) (point-max))))
buffer-file-coding-system t))))
vlf-end-pos)) vlf-end-pos))
(shifts (shifts
(cond (cond
@ -161,7 +162,7 @@ bytes added to the end."
(when (and hexl (not restore-hexl)) (when (and hexl (not restore-hexl))
(if (consp buffer-undo-list) (if (consp buffer-undo-list)
(setq buffer-undo-list nil)) (setq buffer-undo-list nil))
(hexl-mode-exit)) (vlf-tune-dehexlify))
(let ((shift-start 0) (let ((shift-start 0)
(shift-end 0)) (shift-end 0))
(let ((pos (+ (position-bytes (point)) vlf-start-pos)) (let ((pos (+ (position-bytes (point)) vlf-start-pos))
@ -224,12 +225,12 @@ bytes added to the end."
(set-buffer-modified-p modified) (set-buffer-modified-p modified)
(set-visited-file-modtime) (set-visited-file-modtime)
(when hexl (when hexl
(hexl-mode) (vlf-tune-hexlify)
(setq restore-hexl nil)) (setq restore-hexl nil))
(run-hooks 'vlf-after-chunk-update) (run-hooks 'vlf-after-chunk-update)
(cons shift-start shift-end)))))) (cons shift-start shift-end))))))
(when restore-hexl (when restore-hexl
(hexl-mode) (vlf-tune-hexlify)
(setq buffer-undo-list hexl-undo-list)) (setq buffer-undo-list hexl-undo-list))
shifts)) shifts))
@ -252,7 +253,7 @@ bytes added to the end."
vlf-end-pos t t) vlf-end-pos t t)
vlf-start-pos (- vlf-start-pos (car shifts)) vlf-start-pos (- vlf-start-pos (car shifts))
vlf-end-pos (+ vlf-end-pos (cdr 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))) (goto-char (or (byte-to-position (+ pos (car shifts)))
(point-max))))) (point-max)))))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
@ -293,7 +294,7 @@ bytes added to the end."
(defun vlf-insert-file-contents-1 (start end) (defun vlf-insert-file-contents-1 (start end)
"Extract decoded file bytes START to 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) (defun vlf-adjust-start (start end position adjust-end)
"Adjust chunk beginning at absolute START to END till content can\ "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))) (not (zerop safe-start)))
(< shift 3) (< shift 3)
(let ((diff (- chunk-size (let ((diff (- chunk-size
(length (vlf-tune-encode-length position
(encode-coding-region (point-max)))))
position (point-max)
buffer-file-coding-system t)))))
(if strict (if strict
(not (zerop diff)) (not (zerop diff))
(or (< diff -3) (< 0 diff))))) (or (< diff -3) (< 0 diff)))))
@ -345,12 +344,10 @@ which deletion was performed."
(eq encode-direction 'end) (eq encode-direction 'end)
(< (- end border) (- border start)))) (< (- end border) (- border start))))
(dist (if encode-from-end (dist (if encode-from-end
(- end (length (encode-coding-region (- end (vlf-tune-encode-length cut-point
cut-point (point-max) (point-max)))
buffer-file-coding-system t))) (+ start (vlf-tune-encode-length position
(+ start (length (encode-coding-region cut-point))))
position cut-point
buffer-file-coding-system t)))))
(len 0)) (len 0))
(if (< border dist) (if (< border dist)
(while (< border dist) (while (< border dist)
@ -378,7 +375,7 @@ which deletion was performed."
(defun vlf-shift-undo-list (n) (defun vlf-shift-undo-list (n)
"Shift undo list element regions by 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 (setq buffer-undo-list
(nreverse (nreverse
(let ((min (point-min)) (let ((min (point-min))

View File

@ -124,7 +124,7 @@ EVENT may hold details of the invocation."
pos-relative))) pos-relative)))
(cond (current-prefix-arg (cond (current-prefix-arg
(setq vlf-buffer (vlf file t)) (setq vlf-buffer (vlf file t))
(or not-hexl (hexl-mode)) (or not-hexl (vlf-tune-hexlify))
(switch-to-buffer occur-buffer)) (switch-to-buffer occur-buffer))
((not (buffer-live-p vlf-buffer)) ((not (buffer-live-p vlf-buffer))
(unless (catch 'found (unless (catch 'found
@ -137,7 +137,7 @@ EVENT may hold details of the invocation."
(setq vlf-buffer buf) (setq vlf-buffer buf)
(throw 'found t)))) (throw 'found t))))
(setq vlf-buffer (vlf file t)) (setq vlf-buffer (vlf file t))
(or not-hexl (hexl-mode))) (or not-hexl (vlf-tune-hexlify)))
(switch-to-buffer occur-buffer) (switch-to-buffer occur-buffer)
(setq vlf-occur-vlf-buffer vlf-buffer))) (setq vlf-occur-vlf-buffer vlf-buffer)))
(pop-to-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-buffer-modified-p nil)
(set (make-local-variable 'vlf-batch-size) batch-size) (set (make-local-variable 'vlf-batch-size) batch-size)
(vlf-mode 1) (vlf-mode 1)
(if is-hexl (if is-hexl (vlf-tune-hexlify))
(hexl-mode))
(run-hook-with-args 'vlf-before-batch-functions 'occur) (run-hook-with-args 'vlf-before-batch-functions 'occur)
(goto-char (point-min)) (goto-char (point-min))
(vlf-with-undo-disabled (vlf-with-undo-disabled

View File

@ -213,8 +213,7 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
(while (and (< (- end start) n) (while (and (< (- end start) n)
(< n (- vlf-file-size start))) (< n (- vlf-file-size start)))
(erase-buffer) (erase-buffer)
(insert-file-contents-literally buffer-file-name (vlf-tune-insert-file-contents-literally start end)
nil start end)
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward "[\n\C-m]" nil t) (while (re-search-forward "[\n\C-m]" nil t)
(setq n (1- n))) (setq n (1- n)))
@ -239,8 +238,7 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
(or is-hexl (or is-hexl
(while (and (< (- end start) n) (< n end)) (while (and (< (- end start) n) (< n end))
(erase-buffer) (erase-buffer)
(insert-file-contents-literally buffer-file-name (vlf-tune-insert-file-contents-literally start end)
nil start end)
(goto-char (point-max)) (goto-char (point-max))
(while (re-search-backward "[\n\C-m]" nil t) (while (re-search-backward "[\n\C-m]" nil t)
(setq n (1- n))) (setq n (1- n)))

View File

@ -43,28 +43,30 @@ If changing size of chunk, shift remaining file content."
(when hexl (when hexl
(if (consp buffer-undo-list) (if (consp buffer-undo-list)
(setq buffer-undo-list nil)) (setq buffer-undo-list nil))
(hexl-mode-exit)) (vlf-tune-dehexlify))
(if (zerop vlf-file-size) ;new file (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 (setq vlf-file-size (vlf-get-file-size
buffer-file-truename) buffer-file-truename)
vlf-end-pos vlf-file-size) vlf-end-pos vlf-file-size)
(vlf-update-buffer-name)) (vlf-update-buffer-name))
(let* ((region-length (length (encode-coding-region (let* ((region-length (vlf-tune-encode-length (point-min)
(point-min) (point-max) (point-max)))
buffer-file-coding-system t)))
(size-change (- vlf-end-pos vlf-start-pos (size-change (- vlf-end-pos vlf-start-pos
region-length))) region-length)))
(if (zerop size-change) (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) (let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2))) (min tramp-verbose 2)))
(pos (point)) (pos (point))
(font-lock font-lock-mode)) (font-lock font-lock-mode))
(font-lock-mode 0) (font-lock-mode 0)
(if (< 0 size-change) (if (< 0 size-change)
(vlf-file-shift-back size-change) (vlf-file-shift-back size-change region-length)
(vlf-file-shift-forward (- size-change))) (vlf-file-shift-forward (- size-change) region-length))
(if font-lock (font-lock-mode 1)) (if font-lock (font-lock-mode 1))
(vlf-move-to-chunk-2 vlf-start-pos (vlf-move-to-chunk-2 vlf-start-pos
(if (< (- vlf-end-pos 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-end-pos))
(vlf-update-buffer-name) (vlf-update-buffer-name)
(goto-char pos))))) (goto-char pos)))))
(if hexl (hexl-mode))) (if hexl (vlf-tune-hexlify)))
(run-hook-with-args 'vlf-after-batch-functions 'write)) (run-hook-with-args 'vlf-after-batch-functions 'write))
t) t)
(defun vlf-file-shift-back (size-change) (defun vlf-file-shift-back (size-change write-size)
"Shift file contents SIZE-CHANGE bytes back." "Shift file contents SIZE-CHANGE bytes back.
(write-region nil nil buffer-file-name vlf-start-pos t) 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) (let ((read-start-pos vlf-end-pos)
(coding-system-for-write 'no-conversion) (coding-system-for-write 'no-conversion)
(reporter (make-progress-reporter "Adjusting file content..." (reporter (make-progress-reporter "Adjusting file content..."
@ -94,8 +97,8 @@ If changing size of chunk, shift remaining file content."
(erase-buffer) (erase-buffer)
(vlf-verify-size t) (vlf-verify-size t)
(insert-char 32 size-change)) (insert-char 32 size-change))
(write-region nil nil buffer-file-name (- vlf-file-size (vlf-tune-write nil nil (- vlf-file-size size-change)
size-change) t) t size-change)
(progress-reporter-done reporter))) (progress-reporter-done reporter)))
(defun vlf-shift-batch (read-pos write-pos) (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." back at WRITE-POS. Return nil if EOF is reached, t otherwise."
(erase-buffer) (erase-buffer)
(vlf-verify-size t) (vlf-verify-size t)
(let ((read-end (+ read-pos vlf-batch-size))) (let ((read-end (min (+ read-pos vlf-batch-size) vlf-file-size)))
(insert-file-contents-literally buffer-file-name nil (vlf-tune-insert-file-contents-literally read-pos read-end)
read-pos (vlf-tune-write nil nil write-pos 0 (- read-end read-pos))
(min vlf-file-size read-end))
(write-region nil nil buffer-file-name write-pos 0)
(< read-end vlf-file-size))) (< 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. "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." Done by saving content up front and then writing previous batch."
(let ((read-size (max (/ vlf-batch-size 2) size-change)) (let ((read-size (max (/ vlf-batch-size 2) size-change))
(read-pos vlf-end-pos) (read-pos vlf-end-pos)
@ -120,20 +122,28 @@ Done by saving content up front and then writing previous batch."
vlf-start-pos vlf-start-pos
vlf-file-size))) vlf-file-size)))
(vlf-with-undo-disabled (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) (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-update reporter write-pos)
(let ((coding-system-for-write 'no-conversion)) (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) (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-update reporter write-pos)))))
(progress-reporter-done reporter))) (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. "Append READ-SIZE bytes of file starting at READ-POS.
Then write initial buffer content to file at WRITE-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. 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 t) (vlf-verify-size t)
@ -142,14 +152,13 @@ Return nil if EOF is reached, t otherwise."
(end-write-pos (point-max))) (end-write-pos (point-max)))
(when read-more (when read-more
(goto-char end-write-pos) (goto-char end-write-pos)
(insert-file-contents-literally buffer-file-name nil read-pos (vlf-tune-insert-file-contents-literally
(min vlf-file-size read-pos (min vlf-file-size (+ read-pos read-size))))
(+ read-pos read-size))))
;; write ;; write
(if hide-read ; hide literal region if user has to choose encoding (if hide-read ; hide literal region if user has to choose encoding
(narrow-to-region start-write-pos end-write-pos)) (narrow-to-region start-write-pos end-write-pos))
(write-region start-write-pos end-write-pos (vlf-tune-write start-write-pos end-write-pos write-pos
buffer-file-name write-pos 0) (or (not read-more) 0) write-size)
(delete-region start-write-pos end-write-pos) (delete-region start-write-pos end-write-pos)
(if hide-read (widen)) (if hide-read (widen))
read-more)) read-more))