1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-02-23 21:38:05 +00:00

Turn vlf into minor mode.

This commit is contained in:
Andrey Kotlarski 2013-12-02 02:08:24 +02:00
parent 7a141091c8
commit efae918a83

495
vlf.el
View File

@ -61,9 +61,10 @@
(put 'vlf-file-size 'permanent-local t) (put 'vlf-file-size 'permanent-local t)
(defvar vlf-mode-map (defvar vlf-mode-map
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap))
(define-key map [M-next] 'vlf-next-batch) (map-prefix (make-sparse-keymap)))
(define-key map [M-prior] 'vlf-prev-batch) (define-key map [next] 'vlf-next-batch)
(define-key map [prior] 'vlf-prev-batch)
(define-key map "+" 'vlf-change-batch-size) (define-key map "+" 'vlf-change-batch-size)
(define-key map "-" (define-key map "-"
(lambda () "Decrease vlf batch size by factor of 2." (lambda () "Decrease vlf batch size by factor of 2."
@ -74,37 +75,56 @@
(define-key map "o" 'vlf-occur) (define-key map "o" 'vlf-occur)
(define-key map "[" 'vlf-beginning-of-file) (define-key map "[" 'vlf-beginning-of-file)
(define-key map "]" 'vlf-end-of-file) (define-key map "]" 'vlf-end-of-file)
(define-key map "e" 'vlf-edit-mode)
(define-key map "j" 'vlf-jump-to-chunk) (define-key map "j" 'vlf-jump-to-chunk)
(define-key map "l" 'vlf-goto-line) (define-key map "l" 'vlf-goto-line)
map) (define-key map "q" 'vlf-discard-edit)
(define-key map-prefix "\C-c\C-v" map)
map-prefix)
"Keymap for `vlf-mode'.") "Keymap for `vlf-mode'.")
(define-derived-mode vlf-mode special-mode "VLF" (define-minor-mode vlf-mode
"Mode to browse large files in." "Mode to browse large files in."
(setq buffer-read-only t) :lighter " VLF"
(set-buffer-modified-p nil) :group 'vlf-mode
(buffer-disable-undo) :keymap vlf-mode-map
(add-hook 'write-file-functions 'vlf-write nil t) (if vlf-mode
(make-local-variable 'revert-buffer-function) (progn
(setq revert-buffer-function 'vlf-revert) (add-hook 'write-file-functions 'vlf-write nil t)
(make-local-variable 'vlf-batch-size) (set (make-local-variable 'revert-buffer-function)
(make-local-variable 'vlf-start-pos) 'vlf-revert)
(make-local-variable 'vlf-end-pos) (make-local-variable 'vlf-batch-size)
(make-local-variable 'vlf-file-size)) (set (make-local-variable 'vlf-start-pos) -1)
(make-local-variable 'vlf-end-pos)
(set (make-local-variable 'vlf-file-size)
(vlf-get-file-size buffer-file-name))
(let* ((pos (position-bytes (point)))
(start (* (/ pos vlf-batch-size) vlf-batch-size)))
(goto-char (byte-to-position (- pos start)))
(vlf-move-to-batch start)))
(when (or (not large-file-warning-threshold)
(< vlf-file-size large-file-warning-threshold)
(y-or-n-p (format "Load whole file? (%s) "
(file-size-human-readable
vlf-file-size))))
(remove-hook 'write-file-functions 'vlf-write t)
(kill-local-variable 'revert-buffer-function)
(let ((pos (+ vlf-start-pos (position-bytes (point)))))
(erase-buffer)
(insert-file-contents buffer-file-name)
(set-buffer-modified-p nil)
(goto-char (byte-to-position pos)))
(rename-buffer (file-name-nondirectory buffer-file-name) t))))
;;;###autoload ;;;###autoload
(defun vlf (file) (defun vlf (file)
"View Large FILE. "View Large FILE in batches.
Batches of the file data from FILE will be displayed in a read-only You can customize number of bytes displayed by customizing
buffer. You can customize number of bytes displayed by customizing
`vlf-batch-size'." `vlf-batch-size'."
(interactive "fFile to open: ") (interactive "fFile to open: ")
(with-current-buffer (generate-new-buffer "*vlf*") (with-current-buffer (generate-new-buffer "*vlf*")
(set-visited-file-name file) (set-visited-file-name file)
(vlf-mode) (set-buffer-modified-p nil)
(setq vlf-file-size (vlf-get-file-size buffer-file-name)) (vlf-mode 1)
(vlf-insert-file)
(switch-to-buffer (current-buffer)))) (switch-to-buffer (current-buffer))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -156,8 +176,7 @@ OP-TYPE specifies the file operation being performed over FILENAME."
(defadvice scroll-up (around vlf-scroll-up (defadvice scroll-up (around vlf-scroll-up
activate compile) activate compile)
"Slide to next batch if at end of buffer in `vlf-mode'." "Slide to next batch if at end of buffer in `vlf-mode'."
(if (and (derived-mode-p 'vlf-mode) (if (and vlf-mode (eobp))
(eobp))
(progn (vlf-next-batch 1) (progn (vlf-next-batch 1)
(goto-char (point-min))) (goto-char (point-min)))
ad-do-it)) ad-do-it))
@ -165,8 +184,7 @@ OP-TYPE specifies the file operation being performed over FILENAME."
(defadvice scroll-down (around vlf-scroll-down (defadvice scroll-down (around vlf-scroll-down
activate compile) activate compile)
"Slide to previous batch if at beginning of buffer in `vlf-mode'." "Slide to previous batch if at beginning of buffer in `vlf-mode'."
(if (and (derived-mode-p 'vlf-mode) (if (and vlf-mode (bobp))
(bobp))
(progn (vlf-prev-batch 1) (progn (vlf-prev-batch 1)
(goto-char (point-max))) (goto-char (point-max)))
ad-do-it)) ad-do-it))
@ -187,22 +205,26 @@ Normally, the value is doubled;
with the prefix argument DECREASE it is halved." with the prefix argument DECREASE it is halved."
(interactive "P") (interactive "P")
(setq vlf-batch-size (if decrease (setq vlf-batch-size (if decrease
(/ vlf-batch-size 2) (/ vlf-batch-size 2)
(* vlf-batch-size 2))) (* vlf-batch-size 2)))
(vlf-move-to-batch vlf-start-pos)) (vlf-move-to-batch vlf-start-pos))
(defun vlf-format-buffer-name () (defun vlf-format-buffer-name ()
"Return format for vlf buffer name." "Return format for vlf buffer name."
(format "%s(%s)[%d/%d](%d)" (format "%s[%d/%d]"
(file-name-nondirectory buffer-file-name) (file-name-nondirectory buffer-file-name)
(file-size-human-readable vlf-file-size)
(/ vlf-end-pos vlf-batch-size) (/ vlf-end-pos vlf-batch-size)
(/ vlf-file-size vlf-batch-size) (/ vlf-file-size vlf-batch-size)))
vlf-batch-size))
(defun vlf-update-buffer-name () (defun vlf-update-buffer-name ()
"Update the current buffer name." "Update the current buffer name and modeline."
(rename-buffer (vlf-format-buffer-name) t)) (rename-buffer (vlf-format-buffer-name) t)
(setq minor-mode-alist
(mapcar (lambda (x)
(if (eq 'vlf-mode (car x))
`(vlf-mode ,(format " VLF[%d]" vlf-batch-size))
x))
minor-mode-alist)))
(defun vlf-get-file-size (file) (defun vlf-get-file-size (file)
"Get size in bytes of FILE." "Get size in bytes of FILE."
@ -248,6 +270,13 @@ Ask for confirmation if NOCONFIRM is nil."
(interactive "nGoto to chunk: ") (interactive "nGoto to chunk: ")
(vlf-move-to-batch (* (1- n) vlf-batch-size))) (vlf-move-to-batch (* (1- n) vlf-batch-size)))
(defmacro vlf-with-undo-disabled (&rest body)
"Execute BODY with temporarily disabled undo."
(declare (indent defun))
`(progn (buffer-disable-undo)
(unwind-protect (progn ,@body)
(buffer-enable-undo))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; batch movement ;;; batch movement
@ -260,7 +289,7 @@ When prefix argument is negative
(interactive "p") (interactive "p")
(vlf-verify-size) (vlf-verify-size)
(let* ((end (min (+ vlf-end-pos (* vlf-batch-size (let* ((end (min (+ vlf-end-pos (* vlf-batch-size
(abs append))) (abs append)))
vlf-file-size)) vlf-file-size))
(start (if (< append 0) (start (if (< append 0)
vlf-start-pos vlf-start-pos
@ -277,7 +306,7 @@ When prefix argument is negative
(if (zerop vlf-start-pos) (if (zerop vlf-start-pos)
(error "Already at BOF")) (error "Already at BOF"))
(let* ((start (max 0 (- vlf-start-pos (* vlf-batch-size (let* ((start (max 0 (- vlf-start-pos (* vlf-batch-size
(abs prepend))))) (abs prepend)))))
(end (if (< prepend 0) (end (if (< prepend 0)
vlf-end-pos vlf-end-pos
(+ start vlf-batch-size)))) (+ start vlf-batch-size))))
@ -313,83 +342,85 @@ If same as current chunk is requested, do nothing."
(defun vlf-move-to-chunk-1 (start end) (defun vlf-move-to-chunk-1 (start end)
"Move to chunk determined by START END keeping as much edits if any. "Move to chunk determined by START END keeping as much edits if any.
Return t if move hasn't been canceled." Return t if move hasn't been canceled."
(let ((modified (buffer-modified-p)) (vlf-with-undo-disabled
(start (max 0 start)) (let ((modified (buffer-modified-p))
(end (min end vlf-file-size)) (start (max 0 start))
(edit-end (+ (position-bytes (point-max)) vlf-start-pos))) (end (min end vlf-file-size))
(cond (edit-end (+ (position-bytes (point-max)) vlf-start-pos)))
((and (= start vlf-start-pos) (= end edit-end)) (cond
(unless modified ((and (= start vlf-start-pos) (= end edit-end))
(vlf-move-to-chunk-2 start end) (unless modified
t)) (vlf-move-to-chunk-2 start end)
((or (<= edit-end start) (<= end vlf-start-pos)) t))
(when (or (not modified) ((or (<= edit-end start) (<= end vlf-start-pos))
(y-or-n-p "Buffer modified, are you sure? ")) ; full chunk renewal (when (or (not modified)
(vlf-move-to-chunk-2 start end) (y-or-n-p "Buffer modified, are you sure? ")) ;full chunk renewal
t)) (vlf-move-to-chunk-2 start end)
((or (and (<= start vlf-start-pos) (<= edit-end end)) t))
(not modified) ((or (and (<= start vlf-start-pos) (<= edit-end end))
(y-or-n-p "Buffer modified, are you sure? ")) (not modified)
(let ((pos (+ (position-bytes (point)) vlf-start-pos)) (y-or-n-p "Buffer modified, are you sure? "))
(shift-start 0) (let ((pos (+ (position-bytes (point)) vlf-start-pos))
(shift-end 0) (shift-start 0)
(inhibit-read-only t)) (shift-end 0)
(cond ((< end edit-end) (inhibit-read-only t))
(delete-region (byte-to-position (1+ (cond ((< end edit-end)
(- end (delete-region (byte-to-position (1+
vlf-start-pos))) (- end
(point-max))) vlf-start-pos)))
((< edit-end end) (point-max)))
(let ((edit-end-pos (point-max))) ((< edit-end end)
(goto-char edit-end-pos) (let ((edit-end-pos (point-max)))
(insert-file-contents buffer-file-name nil (goto-char edit-end-pos)
vlf-end-pos end) (insert-file-contents buffer-file-name nil
(setq shift-end (cdr (vlf-adjust-chunk vlf-end-pos end)
vlf-end-pos end nil t (setq shift-end (cdr (vlf-adjust-chunk
edit-end-pos)))))) vlf-end-pos end nil t
(cond ((< vlf-start-pos start) edit-end-pos))))))
(delete-region (point-min) (byte-to-position (cond ((< vlf-start-pos start)
(- start vlf-start-pos)))) (delete-region (point-min) (byte-to-position
((< start vlf-start-pos) (- start vlf-start-pos))))
(let ((edit-end-pos (point-max))) ((< start vlf-start-pos)
(goto-char edit-end-pos) (let ((edit-end-pos (point-max)))
(insert-file-contents buffer-file-name nil (goto-char edit-end-pos)
start vlf-start-pos) (insert-file-contents buffer-file-name nil
(setq shift-start (car start vlf-start-pos)
(vlf-adjust-chunk start (setq shift-start (car
(vlf-adjust-chunk start
vlf-start-pos vlf-start-pos
t nil t nil
edit-end-pos))) edit-end-pos)))
(goto-char (point-min)) (goto-char (point-min))
(insert (delete-and-extract-region edit-end-pos (insert (delete-and-extract-region edit-end-pos
(point-max)))))) (point-max))))))
(setq vlf-start-pos (- start shift-start) (setq vlf-start-pos (- start shift-start)
vlf-end-pos (+ end shift-end)) vlf-end-pos (+ end shift-end))
(goto-char (or (byte-to-position (- pos vlf-start-pos)) (goto-char (or (byte-to-position (- pos vlf-start-pos))
(point-max)))) (point-max))))
(set-buffer-modified-p modified) (set-buffer-modified-p modified)
t)))) t)))))
(defun vlf-move-to-chunk-2 (start end) (defun vlf-move-to-chunk-2 (start end)
"Unconditionally move to chunk determined by START END." "Unconditionally move to chunk determined by START END."
(setq vlf-start-pos (max 0 start) (vlf-with-undo-disabled
vlf-end-pos (min end vlf-file-size)) (setq vlf-start-pos (max 0 start)
(let ((inhibit-read-only t) vlf-end-pos (min end vlf-file-size))
(pos (position-bytes (point)))) (let ((inhibit-read-only t)
(erase-buffer) (pos (position-bytes (point))))
(insert-file-contents buffer-file-name nil (erase-buffer)
vlf-start-pos vlf-end-pos) (insert-file-contents buffer-file-name nil
(let ((shifts (vlf-adjust-chunk vlf-start-pos vlf-end-pos t vlf-start-pos vlf-end-pos)
t))) (let ((shifts (vlf-adjust-chunk vlf-start-pos vlf-end-pos t
(setq vlf-start-pos (- vlf-start-pos (car shifts)) t)))
vlf-end-pos (+ vlf-end-pos (cdr shifts))) (setq vlf-start-pos (- vlf-start-pos (car shifts))
(goto-char (or (byte-to-position (+ pos (car shifts))) vlf-end-pos (+ vlf-end-pos (cdr shifts)))
(point-max))))) (goto-char (or (byte-to-position (+ pos (car shifts)))
(set-buffer-modified-p nil) (point-max)))))
(set-visited-file-modtime)) (set-buffer-modified-p nil)
(set-visited-file-modtime)))
(defun vlf-adjust-chunk (start end &optional adjust-start adjust-end (defun vlf-adjust-chunk (start end &optional adjust-start adjust-end
position) position)
"Adjust chunk at absolute START to END till content can be \ "Adjust chunk at absolute START to END till content can be \
properly decoded. ADJUST-START determines if trying to prepend bytes\ properly decoded. ADJUST-START determines if trying to prepend bytes\
to the beginning, ADJUST-END - add to the end. to the beginning, ADJUST-END - add to the end.
@ -453,80 +484,80 @@ BATCH-STEP is amount of overlap between successive chunks."
vlf-start-pos) vlf-start-pos)
vlf-file-size))) vlf-file-size)))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(buffer-disable-undo) (vlf-with-undo-disabled
(unwind-protect (unwind-protect
(catch 'end-of-file (catch 'end-of-file
(if backward (if backward
(while (not (zerop to-find)) (while (not (zerop to-find))
(cond ((re-search-backward regexp nil t) (cond ((re-search-backward regexp nil t)
(setq to-find (1- to-find) (setq to-find (1- to-find)
match-chunk-start vlf-start-pos match-chunk-start vlf-start-pos
match-chunk-end vlf-end-pos match-chunk-end vlf-end-pos
match-start-pos (+ vlf-start-pos match-start-pos (+ vlf-start-pos
(position-bytes (position-bytes
(match-beginning 0))) (match-beginning 0)))
match-end-pos (+ vlf-start-pos match-end-pos (+ vlf-start-pos
(position-bytes (position-bytes
(match-end 0))))) (match-end 0)))))
((zerop vlf-start-pos) ((zerop vlf-start-pos)
(throw 'end-of-file nil)) (throw 'end-of-file nil))
(t (let ((batch-move (- vlf-start-pos (t (let ((batch-move (- vlf-start-pos
(- vlf-batch-size (- vlf-batch-size
batch-step)))) batch-step))))
(vlf-move-to-batch (vlf-move-to-batch
(if (< match-start-pos batch-move) (if (< match-start-pos batch-move)
(- match-start-pos vlf-batch-size) (- match-start-pos vlf-batch-size)
batch-move) t)) batch-move) t))
(goto-char (if (< match-start-pos (goto-char (if (< match-start-pos
vlf-end-pos) vlf-end-pos)
(or (byte-to-position (or (byte-to-position
(- match-start-pos (- match-start-pos
vlf-start-pos)) vlf-start-pos))
(point-max)) (point-max))
(point-max))) (point-max)))
(progress-reporter-update (progress-reporter-update
reporter (- vlf-file-size reporter (- vlf-file-size
vlf-start-pos))))) vlf-start-pos)))))
(while (not (zerop to-find)) (while (not (zerop to-find))
(cond ((re-search-forward regexp nil t) (cond ((re-search-forward regexp nil t)
(setq to-find (1- to-find) (setq to-find (1- to-find)
match-chunk-start vlf-start-pos match-chunk-start vlf-start-pos
match-chunk-end vlf-end-pos match-chunk-end vlf-end-pos
match-start-pos (+ vlf-start-pos match-start-pos (+ vlf-start-pos
(position-bytes (position-bytes
(match-beginning 0))) (match-beginning 0)))
match-end-pos (+ vlf-start-pos match-end-pos (+ vlf-start-pos
(position-bytes (position-bytes
(match-end 0))))) (match-end 0)))))
((= vlf-end-pos vlf-file-size) ((= vlf-end-pos vlf-file-size)
(throw 'end-of-file nil)) (throw 'end-of-file nil))
(t (let ((batch-move (- vlf-end-pos batch-step))) (t (let ((batch-move (- vlf-end-pos batch-step)))
(vlf-move-to-batch (vlf-move-to-batch
(if (< batch-move match-end-pos) (if (< batch-move match-end-pos)
match-end-pos match-end-pos
batch-move) t)) batch-move) t))
(goto-char (if (< vlf-start-pos match-end-pos) (goto-char (if (< vlf-start-pos match-end-pos)
(or (byte-to-position (or (byte-to-position
(- match-end-pos (- match-end-pos
vlf-start-pos)) vlf-start-pos))
(point-min)) (point-min))
(point-min))) (point-min)))
(progress-reporter-update reporter (progress-reporter-update reporter
vlf-end-pos))))) vlf-end-pos)))))
(progress-reporter-done reporter)) (progress-reporter-done reporter))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(if backward (if backward
(vlf-goto-match match-chunk-start match-chunk-end (vlf-goto-match match-chunk-start match-chunk-end
match-end-pos match-start-pos match-end-pos match-start-pos
count to-find) count to-find)
(vlf-goto-match match-chunk-start match-chunk-end (vlf-goto-match match-chunk-start match-chunk-end
match-start-pos match-end-pos match-start-pos match-end-pos
count to-find))))) count to-find))))))
(defun vlf-goto-match (match-chunk-start match-chunk-end (defun vlf-goto-match (match-chunk-start match-chunk-end
match-pos-start match-pos-start
match-pos-end match-pos-end
count to-find) count to-find)
"Move to MATCH-CHUNK-START MATCH-CHUNK-END surrounding \ "Move to MATCH-CHUNK-START MATCH-CHUNK-END surrounding \
MATCH-POS-START and MATCH-POS-END. MATCH-POS-START and MATCH-POS-END.
According to COUNT and left TO-FIND, show if search has been According to COUNT and left TO-FIND, show if search has been
@ -588,7 +619,7 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
(progn (vlf-beginning-of-file) (progn (vlf-beginning-of-file)
(goto-char (point-min)) (goto-char (point-min))
(setq success (vlf-re-search "[\n\C-m]" (1- n) (setq success (vlf-re-search "[\n\C-m]" (1- n)
nil 0))) nil 0)))
(vlf-end-of-file) (vlf-end-of-file)
(goto-char (point-max)) (goto-char (point-max))
(setq success (vlf-re-search "[\n\C-m]" (- n) t 0))) (setq success (vlf-re-search "[\n\C-m]" (- n) t 0)))
@ -682,11 +713,11 @@ Prematurely ending indexing will still show what's found so far."
(vlf-beginning-of-file) (vlf-beginning-of-file)
(goto-char (point-min)) (goto-char (point-min))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(buffer-disable-undo) (vlf-with-undo-disabled
(unwind-protect (vlf-build-occur regexp) (unwind-protect (vlf-build-occur regexp)
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(vlf-move-to-chunk start-pos end-pos) (vlf-move-to-chunk start-pos end-pos)
(goto-char pos)))) (goto-char pos)))))
(defun vlf-build-occur (regexp) (defun vlf-build-occur (regexp)
"Build occur style index for REGEXP." "Build occur style index for REGEXP."
@ -698,7 +729,7 @@ Prematurely ending indexing will still show what's found so far."
(match-end-pos (+ vlf-start-pos (position-bytes (point)))) (match-end-pos (+ vlf-start-pos (position-bytes (point))))
(occur-buffer (generate-new-buffer (occur-buffer (generate-new-buffer
(concat "*VLF-occur " (file-name-nondirectory (concat "*VLF-occur " (file-name-nondirectory
buffer-file-name) buffer-file-name)
"*"))) "*")))
(line-regexp (concat "\\(?5:[\n\C-m]\\)\\|\\(?10:" (line-regexp (concat "\\(?5:[\n\C-m]\\)\\|\\(?10:"
regexp "\\)")) regexp "\\)"))
@ -762,8 +793,8 @@ Prematurely ending indexing will still show what's found so far."
(unless end-of-file (unless end-of-file
(let ((batch-move (- vlf-end-pos batch-step))) (let ((batch-move (- vlf-end-pos batch-step)))
(vlf-move-to-batch (if (< batch-move match-end-pos) (vlf-move-to-batch (if (< batch-move match-end-pos)
match-end-pos match-end-pos
batch-move) t)) batch-move) t))
(goto-char (if (< vlf-start-pos match-end-pos) (goto-char (if (< vlf-start-pos match-end-pos)
(or (byte-to-position (- match-end-pos (or (byte-to-position (- match-end-pos
vlf-start-pos)) vlf-start-pos))
@ -792,30 +823,11 @@ in file: %s" total-matches line regexp file)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; editing ;;; editing
(defvar vlf-edit-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map text-mode-map)
(define-key map "\C-c\C-c" 'vlf-write)
(define-key map "\C-c\C-q" 'vlf-discard-edit)
(define-key map "\C-v" vlf-mode-map)
map)
"Keymap for command `vlf-edit-mode'.")
(define-derived-mode vlf-edit-mode vlf-mode "VLF[edit]"
"Major mode for editing large file chunks."
(setq buffer-read-only nil)
(buffer-enable-undo)
(message (substitute-command-keys
"Editing: Type \\[vlf-write] to write chunk \
or \\[vlf-discard-edit] to discard changes.")))
(defun vlf-discard-edit () (defun vlf-discard-edit ()
"Discard edit and refresh chunk from file." "Discard edit and refresh chunk from file."
(interactive) (interactive)
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(vlf-move-to-chunk vlf-start-pos vlf-end-pos) (vlf-move-to-chunk vlf-start-pos vlf-end-pos))
(vlf-mode)
(message "Switched to VLF mode."))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; saving ;;; saving
@ -839,30 +851,29 @@ Save anyway? ")))
(vlf-file-shift-back size-change)) (vlf-file-shift-back size-change))
(t (vlf-file-shift-forward (- size-change)))) (t (vlf-file-shift-forward (- size-change))))
(vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos) (vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)
(goto-char pos)) (goto-char pos)))
(vlf-mode))
t) t)
(defun vlf-file-shift-back (size-change) (defun vlf-file-shift-back (size-change)
"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-region nil nil buffer-file-name vlf-start-pos t)
(buffer-disable-undo) (vlf-with-undo-disabled
(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..."
vlf-end-pos vlf-end-pos
vlf-file-size))) vlf-file-size)))
(while (vlf-shift-batch read-start-pos (- read-start-pos (while (vlf-shift-batch read-start-pos (- read-start-pos
size-change)) size-change))
(setq read-start-pos (+ read-start-pos vlf-batch-size)) (setq read-start-pos (+ read-start-pos vlf-batch-size))
(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) (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)
(progress-reporter-done reporter))) (progress-reporter-done reporter))))
(defun vlf-shift-batch (read-pos write-pos) (defun vlf-shift-batch (read-pos write-pos)
"Read `vlf-batch-size' bytes from READ-POS and write them \ "Read `vlf-batch-size' bytes from READ-POS and write them \
@ -879,26 +890,26 @@ back at WRITE-POS. Return nil if EOF is reached, t otherwise."
(defun vlf-file-shift-forward (size-change) (defun vlf-file-shift-forward (size-change)
"Shift file contents SIZE-CHANGE bytes forward. "Shift file contents SIZE-CHANGE bytes forward.
Done by saving content up front and then writing previous batch." Done by saving content up front and then writing previous batch."
(buffer-disable-undo) (vlf-with-undo-disabled
(let ((size (+ vlf-batch-size size-change)) (let ((read-size (max (/ vlf-batch-size 2) size-change))
(read-pos vlf-end-pos) (read-pos vlf-end-pos)
(write-pos vlf-start-pos) (write-pos vlf-start-pos)
(reporter (make-progress-reporter "Adjusting file content..." (reporter (make-progress-reporter "Adjusting file content..."
vlf-start-pos vlf-start-pos
vlf-file-size))) vlf-file-size)))
(when (vlf-shift-batches size read-pos write-pos t) (when (vlf-shift-batches read-size read-pos write-pos t)
(setq write-pos (+ read-pos size-change) (setq write-pos (+ read-pos size-change)
read-pos (+ read-pos size)) read-pos (+ read-pos read-size))
(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 size read-pos write-pos nil) (while (vlf-shift-batches read-size read-pos write-pos nil)
(setq write-pos (+ read-pos size-change) (setq write-pos (+ read-pos size-change)
read-pos (+ read-pos size)) read-pos (+ read-pos read-size))
(progress-reporter-update reporter write-pos)))) (progress-reporter-update reporter write-pos))))
(progress-reporter-done reporter))) (progress-reporter-done reporter))))
(defun vlf-shift-batches (size read-pos write-pos hide-read) (defun vlf-shift-batches (read-size read-pos write-pos hide-read)
"Append 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.
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."
@ -909,8 +920,8 @@ Return nil if EOF is reached, t otherwise."
(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 (insert-file-contents-literally buffer-file-name nil read-pos
(min vlf-file-size (+ read-pos (min vlf-file-size
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))