]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/vlf/vlf.el
[gnugo] Indicate buffer not modified after save.
[gnu-emacs-elpa] / packages / vlf / vlf.el
index 7092dc218300561ce018a70a95ddd95c87eeda13..2e3c9a1b8eef4ff515a33b3b1e5403cfda66c635 100644 (file)
@@ -1,12 +1,14 @@
 ;;; vlf.el --- View Large Files  -*- lexical-binding: t -*-
 
-;; Copyright (C) 2006, 2012, 2013  Free Software Foundation, Inc.
+;; Copyright (C) 2006, 2012-2014 Free Software Foundation, Inc.
 
-;; Version: 0.5
+;; Version: 1.5
 ;; Keywords: large files, utilities
+;; Maintainer: Andrey Kotlarski <m00naticus@gmail.com>
 ;; Authors: 2006 Mathias Dahl <mathias.dahl@gmail.com>
 ;;          2012 Sam Steingold <sds@gnu.org>
-;;          2013 Andrey Kotlarski <m00naticus@gmail.com>
+;;          2013-2014 Andrey Kotlarski <m00naticus@gmail.com>
+;; URL: https://github.com/m00natic/vlfi
 
 ;; This file is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
-
-;; This package provides the M-x vlf command, which visits part of a
-;; large file in a read-only buffer without visiting the entire file.
-;; The buffer uses VLF mode, which defines several commands for
-;; moving around, searching and editing selected chunk of file.
+;; This package provides the M-x vlf command, which visits part of
+;; large file without loading it entirely.  The buffer uses VLF mode,
+;; which provides several commands for moving around, searching,
+;; comparing and editing selected part of file.
+;; To have it offered when opening large files:
+;; (require 'vlf-integrate)
 
 ;; This package was inspired by a snippet posted by Kevin Rodgers,
 ;; showing how to use `insert-file-contents' to extract part of a
 
 ;;; Code:
 
-(defgroup vlf nil
-  "View Large Files in Emacs."
-  :prefix "vlf-"
-  :group 'files)
-
-(defcustom vlf-batch-size 1024
-  "Defines how large each batch of file data is (in bytes)."
-  :type 'integer
-  :group 'vlf)
-
-;;; Keep track of file position.
-(defvar vlf-start-pos 0
-  "Absolute position of the visible chunk start.")
-(defvar vlf-end-pos vlf-batch-size
-  "Absolute position of the visible chunk end.")
-(defvar vlf-file-size 0 "Total size of presented file.")
+(defgroup vlf nil "View Large Files in Emacs."
+  :prefix "vlf-" :group 'files)
+
+(defcustom vlf-before-batch-functions nil
+  "Hook that runs before multiple batch operations.
+One argument is supplied that specifies current action.  Possible
+values are: `write', `ediff', `occur', `search', `goto-line'."
+  :group 'vlf :type 'hook)
+
+(defcustom vlf-after-batch-functions nil
+  "Hook that runs after multiple batch operations.
+One argument is supplied that specifies current action.  Possible
+values are: `write', `ediff', `occur', `search', `goto-line'."
+  :group 'vlf :type 'hook)
+
+(require 'vlf-base)
+
+(autoload 'vlf-write "vlf-write" "Write current chunk to file." t)
+(autoload 'vlf-re-search-forward "vlf-search"
+  "Search forward for REGEXP prefix COUNT number of times." t)
+(autoload 'vlf-re-search-backward "vlf-search"
+  "Search backward for REGEXP prefix COUNT number of times." t)
+(autoload 'vlf-goto-line "vlf-search" "Go to line." t)
+(autoload 'vlf-occur "vlf-occur"
+  "Make whole file occur style index for REGEXP." t)
+(autoload 'vlf-toggle-follow "vlf-follow"
+  "Toggle continuous chunk recenter around current point." t)
+(autoload 'vlf-stop-follow "vlf-follow" "Stop continuous recenter." t)
+(autoload 'vlf-ediff-buffers "vlf-ediff"
+  "Run batch by batch ediff over VLF buffers." t)
 
 (defvar vlf-mode-map
   (let ((map (make-sparse-keymap)))
-    (define-key map [M-next] 'vlf-next-batch)
-    (define-key map [M-prior] 'vlf-prev-batch)
+    (define-key map "n" 'vlf-next-batch)
+    (define-key map "p" 'vlf-prev-batch)
+    (define-key map " " 'vlf-next-batch-from-point)
     (define-key map "+" 'vlf-change-batch-size)
     (define-key map "-"
       (lambda () "Decrease vlf batch size by factor of 2."
         (vlf-change-batch-size t)))
     (define-key map "s" 'vlf-re-search-forward)
     (define-key map "r" 'vlf-re-search-backward)
-    (define-key map "]" (lambda () "Jump to end of file content."
-                          (interactive)
-                          (vlf-insert-file buffer-file-name t)))
-    (define-key map "[" (lambda () "Jump to beginning of file content."
-                          (interactive)
-                          (vlf-insert-file buffer-file-name)))
-    (define-key map "e" 'vlf-edit-mode)
+    (define-key map "o" 'vlf-occur)
+    (define-key map "[" 'vlf-beginning-of-file)
+    (define-key map "]" 'vlf-end-of-file)
+    (define-key map "j" 'vlf-jump-to-chunk)
+    (define-key map "l" 'vlf-goto-line)
+    (define-key map "e" 'vlf-ediff-buffers)
+    (define-key map "f" 'vlf-toggle-follow)
+    (define-key map "g" 'vlf-revert)
     map)
   "Keymap for `vlf-mode'.")
 
-(define-derived-mode vlf-mode special-mode "VLF"
-  "Mode to browse large files in."
-  (setq buffer-read-only t)
-  (set-buffer-modified-p nil)
-  (buffer-disable-undo)
-  (make-local-variable 'vlf-batch-size)
-  (put 'vlf-batch-size 'permanent-local t)
-  (make-local-variable 'vlf-start-pos)
-  (put 'vlf-start-pos 'permanent-local t)
-  (make-local-variable 'vlf-end-pos)
-  (put 'vlf-end-pos 'permanent-local t)
-  (make-local-variable 'vlf-file-size)
-  (put 'vlf-file-size 'permanent-local t))
-
-(defun vlf-change-batch-size (decrease)
-  "Change the buffer-local value of `vlf-batch-size'.
-Normally, the value is doubled;
-with the prefix argument DECREASE it is halved."
-  (interactive "P")
-  (or (assq 'vlf-batch-size (buffer-local-variables))
-      (error "%s is not local in this buffer" 'vlf-batch-size))
-  (setq vlf-batch-size (if decrease
-                            (/ vlf-batch-size 2)
-                          (* vlf-batch-size 2)))
-  (vlf-move-to-batch vlf-start-pos))
+(defvar vlf-prefix-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-c\C-v" vlf-mode-map)
+    map)
+  "Prefixed keymap for `vlf-mode'.")
 
-(defun vlf-format-buffer-name ()
-  "Return format for vlf buffer name."
-  (format "%s(%s)[%.2f%%%%](%d)"
-          (file-name-nondirectory buffer-file-name)
-          (file-size-human-readable vlf-file-size)
-          (/ (* 100 vlf-end-pos) (float vlf-file-size))
-          vlf-batch-size))
+(define-minor-mode vlf-mode
+  "Mode to browse large files in."
+  :lighter " VLF" :group 'vlf :keymap vlf-prefix-map
+  (cond (vlf-mode
+         (set (make-local-variable 'require-final-newline) nil)
+         (add-hook 'write-file-functions 'vlf-write nil t)
+         (set (make-local-variable 'revert-buffer-function)
+              'vlf-revert)
+         (make-local-variable 'vlf-batch-size)
+         (setq vlf-file-size (vlf-get-file-size buffer-file-truename)
+               vlf-start-pos 0
+               vlf-end-pos 0)
+         (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))
+         (add-hook 'after-change-major-mode-hook 'vlf-keep-alive t t)
+         (vlf-keep-alive))
+        ((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))))
+         (kill-local-variable 'revert-buffer-function)
+         (vlf-stop-follow)
+         (kill-local-variable 'require-final-newline)
+         (remove-hook 'write-file-functions 'vlf-write t)
+         (remove-hook 'after-change-major-mode-hook
+                      'vlf-keep-alive t)
+         (let ((hexl (derived-mode-p 'hexl-mode)))
+           (if hexl (hexl-mode-exit))
+           (let ((pos (+ vlf-start-pos (position-bytes (point)))))
+             (vlf-with-undo-disabled
+              (insert-file-contents buffer-file-name t nil nil t))
+             (goto-char (byte-to-position pos)))
+           (if hexl (hexl-mode)))
+         (rename-buffer (file-name-nondirectory buffer-file-name) t))
+        (t (setq vlf-mode t))))
+
+(defun vlf-keep-alive ()
+  "Keep `vlf-mode' on major mode change."
+  (if (derived-mode-p 'hexl-mode)
+      (set (make-local-variable 'revert-buffer-function) 'vlf-revert))
+  (setq vlf-mode t))
 
-(defun vlf-update-buffer-name ()
-  "Update the current buffer name."
-  (rename-buffer (vlf-format-buffer-name) t))
+;;;###autoload
+(defun vlf (file)
+  "View Large FILE in batches.
+You can customize number of bytes displayed by customizing
+`vlf-batch-size'.
+Return newly created buffer."
+  (interactive "fFile to open: ")
+  (let ((vlf-buffer (generate-new-buffer "*vlf*")))
+    (set-buffer vlf-buffer)
+    (set-visited-file-name file)
+    (set-buffer-modified-p nil)
+    (vlf-mode 1)
+    (switch-to-buffer vlf-buffer)
+    vlf-buffer))
 
 (defun vlf-next-batch (append)
   "Display the next batch of file data.
@@ -119,30 +166,13 @@ When prefix argument is supplied and positive
 When prefix argument is negative
  append next APPEND number of batches to the existing buffer."
   (interactive "p")
-  (let ((end (+ vlf-end-pos (* vlf-batch-size
-                                (abs append)))))
-    (when (< vlf-file-size end)                ; re-check file size
-      (setq vlf-file-size (nth 7 (file-attributes buffer-file-name)))
-      (cond ((= vlf-end-pos vlf-file-size)
-             (error "Already at EOF"))
-            ((< vlf-file-size end)
-             (setq end vlf-file-size))))
-    (let ((inhibit-read-only t)
-          (do-append (< append 0))
-          (pos (point)))
-      (if do-append
-          (goto-char (point-max))
-        (setq vlf-start-pos (- end vlf-batch-size))
-        (erase-buffer))
-      (insert-file-contents buffer-file-name nil
-                            (if do-append
-                                vlf-end-pos
-                              vlf-start-pos)
-                            end)
-      (goto-char pos))
-    (setq vlf-end-pos end))
-  (set-buffer-modified-p nil)
-  (vlf-update-buffer-name))
+  (vlf-verify-size)
+  (let* ((end (min (+ vlf-end-pos (* vlf-batch-size (abs append)))
+                   vlf-file-size))
+         (start (if (< append 0)
+                    vlf-start-pos
+                  (- end vlf-batch-size))))
+    (vlf-move-to-chunk start end)))
 
 (defun vlf-prev-batch (prepend)
   "Display the previous batch of file data.
@@ -153,258 +183,140 @@ When prefix argument is negative
   (interactive "p")
   (if (zerop vlf-start-pos)
       (error "Already at BOF"))
-  (let ((inhibit-read-only t)
-        (start (max 0 (- vlf-start-pos (* vlf-batch-size
-                                           (abs prepend)))))
-        (do-prepend (< prepend 0))
-        (pos (- (point-max) (point))))
-    (if do-prepend
-        (goto-char (point-min))
-      (setq vlf-end-pos (+ start vlf-batch-size))
-      (erase-buffer))
-    (insert-file-contents buffer-file-name nil start
-                          (if do-prepend
-                              vlf-start-pos
-                            vlf-end-pos))
-    (goto-char (- (point-max) pos))
-    (setq vlf-start-pos start))
-  (set-buffer-modified-p nil)
-  (vlf-update-buffer-name))
-
-(defun vlf-move-to-batch (start)
-  "Move to batch determined by START.
-Adjust according to file start/end and show `vlf-batch-size' bytes."
-  (setq vlf-start-pos (max 0 start)
-        vlf-end-pos (+ vlf-start-pos vlf-batch-size))
-  (if (< vlf-file-size vlf-end-pos)   ; re-check file size
-      (setq vlf-file-size
-            (nth 7 (file-attributes buffer-file-name))
-            vlf-end-pos (min vlf-end-pos vlf-file-size)
-            vlf-start-pos (max 0 (- vlf-end-pos vlf-batch-size))))
-  (let ((inhibit-read-only t))
-    (erase-buffer)
-    (insert-file-contents buffer-file-name nil
-                          vlf-start-pos vlf-end-pos))
-  (set-buffer-modified-p nil)
-  (vlf-update-buffer-name))
-
-(defun vlf-move-to-chunk (start end)
-  "Move to chunk determined by START END."
-  (if (< vlf-file-size end)          ; re-check file size
-      (setq vlf-file-size (nth 7
-                                (file-attributes buffer-file-name))))
-  (setq vlf-start-pos (max 0 start)
-        vlf-end-pos (min end vlf-file-size))
-  (let ((inhibit-read-only t))
-    (erase-buffer)
-    (insert-file-contents buffer-file-name nil
-                          vlf-start-pos vlf-end-pos))
-  (set-buffer-modified-p nil)
-  (vlf-update-buffer-name))
-
-(defun vlf-insert-file (file &optional from-end)
-  "Insert first chunk of FILE contents in current buffer.
-With FROM-END prefix, start from the back."
-  (if from-end
-      (setq vlf-start-pos (max 0 (- vlf-file-size vlf-batch-size))
-            vlf-end-pos vlf-file-size)
-    (setq vlf-start-pos 0
-          vlf-end-pos (min vlf-batch-size vlf-file-size)))
-  (vlf-move-to-chunk vlf-start-pos vlf-end-pos))
-
-;;;###autoload
-(defun vlf (file &optional from-end)
-  "View Large FILE.  With FROM-END prefix, view from the back.
-Batches of the file data from FILE will be displayed in a read-only
-buffer.  You can customize number of bytes displayed by customizing
-`vlf-batch-size'."
-  (interactive "fFile to open: \nP")
-  (with-current-buffer (generate-new-buffer "*vlf*")
-    (setq buffer-file-name file
-          vlf-file-size (nth 7 (file-attributes file)))
-    (vlf-insert-file file from-end)
-    (vlf-mode)
-    (switch-to-buffer (current-buffer))))
+  (let* ((start (max 0 (- vlf-start-pos (* vlf-batch-size (abs prepend)))))
+         (end (if (< prepend 0)
+                  vlf-end-pos
+                (+ start vlf-batch-size))))
+    (vlf-move-to-chunk start end)))
+
+;; scroll auto batching
+(defadvice scroll-up (around vlf-scroll-up
+                             activate compile)
+  "Slide to next batch if at end of buffer in `vlf-mode'."
+  (if (and vlf-mode (pos-visible-in-window-p (point-max)))
+      (progn (vlf-next-batch 1)
+             (goto-char (point-min)))
+    ad-do-it))
+
+(defadvice scroll-down (around vlf-scroll-down
+                               activate compile)
+  "Slide to previous batch if at beginning of buffer in `vlf-mode'."
+  (if (and vlf-mode (pos-visible-in-window-p (point-min)))
+      (progn (vlf-prev-batch 1)
+             (goto-char (point-max)))
+    ad-do-it))
+
+;; hexl mode integration
+(defun vlf-hexl-before (&optional operation)
+  "Temporarily disable `hexl-mode' for OPERATION."
+  (when (derived-mode-p 'hexl-mode)
+    (hexl-mode-exit)
+    (set (make-local-variable 'vlf-restore-hexl-mode) operation)))
+
+(defun vlf-hexl-after (&optional operation)
+  "Re-enable `hexl-mode' if active before OPERATION."
+  (when (and (boundp 'vlf-restore-hexl-mode)
+             (eq vlf-restore-hexl-mode operation))
+    (hexl-mode)
+    (kill-local-variable 'vlf-restore-hexl-mode)))
+
+(add-hook 'vlf-before-batch-functions 'vlf-hexl-before)
+(add-hook 'vlf-after-batch-functions 'vlf-hexl-after)
+(add-hook 'vlf-before-chunk-update 'vlf-hexl-before)
+(add-hook 'vlf-after-chunk-update 'vlf-hexl-after)
+
+(eval-after-load "hexl"
+  '(progn
+     (defadvice hexl-save-buffer (around vlf-hexl-save
+                                         activate compile)
+       "Prevent hexl save if `vlf-mode' is active."
+       (if vlf-mode
+           (vlf-write)
+         ad-do-it))
+
+     (defadvice hexl-scroll-up (around vlf-hexl-scroll-up
+                                       activate compile)
+       "Slide to next batch if at end of buffer in `vlf-mode'."
+       (if (and vlf-mode (pos-visible-in-window-p (point-max))
+                (or (not (numberp arg)) (< 0 arg)))
+           (progn (vlf-next-batch 1)
+                  (goto-char (point-min)))
+         ad-do-it))
+
+     (defadvice hexl-scroll-down (around vlf-hexl-scroll-down
+                                         activate compile)
+       "Slide to previous batch if at beginning of buffer in `vlf-mode'."
+       (if (and vlf-mode (pos-visible-in-window-p (point-min)))
+           (progn (vlf-prev-batch 1)
+                  (goto-char (point-max)))
+         ad-do-it))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; utilities
 
-;;;###autoload
-(defun dired-vlf (from-end)
-  "In Dired, visit the file on this line in VLF mode.
-With FROM-END prefix, view from the back."
+(defun vlf-change-batch-size (decrease)
+  "Change the buffer-local value of `vlf-batch-size'.
+Normally, the value is doubled;
+with the prefix argument DECREASE it is halved."
   (interactive "P")
-  (vlf (dired-get-file-for-visit) from-end))
+  (vlf-set-batch-size (if decrease (/ vlf-batch-size 2)
+                        (* vlf-batch-size 2))))
 
-;;;###autoload
-(eval-after-load "dired"
-  '(define-key dired-mode-map "V" 'dired-vlf))
+(defun vlf-set-batch-size (size)
+  "Set batch to SIZE bytes and update chunk."
+  (interactive (list (read-number "Size in bytes: " vlf-batch-size)))
+  (setq vlf-batch-size size)
+  (vlf-move-to-batch vlf-start-pos))
 
-;;;###autoload
-(defun vlf-if-file-too-large (size op-type &optional filename)
-  "If file SIZE larger than `large-file-warning-threshold', \
-allow user to view file with `vlf', open it normally or abort.
-OP-TYPE specifies the file operation being performed over FILENAME."
-  (and large-file-warning-threshold size
-       (> size large-file-warning-threshold)
-       (let ((char nil))
-         (while (not (memq (setq char
-                                 (read-event
-                                  (propertize
-                                   (format "File %s is large (%s): \
-%s normally (o), %s with vlf (v) or abort (a)"
-                                           (file-name-nondirectory filename)
-                                           (file-size-human-readable size)
-                                           op-type op-type)
-                                   'face 'minibuffer-prompt)))
-                           '(?o ?O ?v ?V ?a ?A))))
-         (cond ((memq char '(?o ?O)))
-               ((memq char '(?v ?V))
-                (vlf filename nil)
-                (error ""))
-               ((memq char '(?a ?A))
-                (error "Aborted"))))))
-
-;;; hijack `abort-if-file-too-large'
-;;;###autoload
-(fset 'abort-if-file-too-large 'vlf-if-file-too-large)
-
-;;; search
-(defun vlf-re-search (regexp count backward)
-  "Search for REGEXP COUNT number of times forward or BACKWARD."
-  (let* ((match-start-pos (+ vlf-start-pos (point)))
-         (match-end-pos match-start-pos)
-         (to-find count)
-         (search-reporter (make-progress-reporter
-                           (concat "Searching for " regexp)
-                           (if backward
-                               (- vlf-file-size vlf-end-pos)
-                             vlf-start-pos)
-                           vlf-file-size))
-         (batch-step (/ vlf-batch-size 8))) ; amount of chunk overlap
-    (unwind-protect
-        (catch 'end-of-file
-          (if backward
-              (while (not (zerop to-find))
-                (cond ((re-search-backward regexp nil t)
-                       (setq to-find (1- to-find)
-                             match-start-pos (+ vlf-start-pos
-                                                (match-beginning 0))
-                             match-end-pos (+ vlf-start-pos
-                                              (match-end 0))))
-                      ((zerop vlf-start-pos)
-                       (throw 'end-of-file nil))
-                      (t (let ((batch-move (- vlf-start-pos
-                                              (- vlf-batch-size
-                                                 batch-step))))
-                           (vlf-move-to-batch
-                            (if (< match-start-pos batch-move)
-                                (- match-start-pos vlf-batch-size)
-                              batch-move)))
-                         (goto-char (if (< match-start-pos
-                                           vlf-end-pos)
-                                        (- match-start-pos
-                                           vlf-start-pos)
-                                      (point-max)))
-                         (progress-reporter-update search-reporter
-                                                   vlf-start-pos))))
-            (while (not (zerop to-find))
-              (cond ((re-search-forward regexp nil t)
-                     (setq to-find (1- to-find)
-                           match-start-pos (+ vlf-start-pos
-                                              (match-beginning 0))
-                           match-end-pos (+ vlf-start-pos
-                                            (match-end 0))))
-                    ((= vlf-end-pos vlf-file-size)
-                     (throw 'end-of-file nil))
-                    (t (let ((batch-move (- vlf-end-pos batch-step)))
-                         (vlf-move-to-batch
-                          (if (< batch-move match-end-pos)
-                              match-end-pos
-                            batch-move)))
-                       (goto-char (if (< vlf-start-pos match-end-pos)
-                                      (- match-end-pos vlf-start-pos)
-                                    (point-min)))
-                       (progress-reporter-update search-reporter
-                                                 vlf-end-pos)))))
-          (progress-reporter-done search-reporter))
-      (if backward
-          (vlf-goto-match match-end-pos match-start-pos
-                           count to-find)
-        (vlf-goto-match match-start-pos match-end-pos
-                         count to-find)))))
-
-(defun vlf-goto-match (match-pos-start match-pos-end count to-find)
-  "Move to chunk surrounding MATCH-POS-START and MATCH-POS-END.
-According to COUNT and left TO-FIND, show if search has been
-successful.  Return nil if nothing found."
-  (let ((success (zerop to-find)))
-    (or success
-        (vlf-move-to-batch (- match-pos-start
-                               (/ vlf-batch-size 2))))
-    (let* ((match-end (- match-pos-end vlf-start-pos))
-           (overlay (make-overlay (- match-pos-start vlf-start-pos)
-                                  match-end)))
-      (overlay-put overlay 'face 'region)
-      (or success (goto-char match-end))
-      (prog1 (cond (success t)
-                   ((< to-find count)
-                    (message "Moved to the %d match which is last"
-                             (- count to-find))
-                    t)
-                   (t (message "Not found")
-                      nil))
-        (sit-for 0.1)
-        (delete-overlay overlay)))))
-
-(defun vlf-re-search-forward (regexp count)
-  "Search forward for REGEXP prefix COUNT number of times."
-  (interactive (list (read-regexp "Search whole file"
-                                  (if regexp-history
-                                      (car regexp-history))
-                                  'regexp-history)
-                     (or current-prefix-arg 1)))
-  (vlf-re-search regexp count nil))
-
-(defun vlf-re-search-backward (regexp count)
-  "Search backward for REGEXP prefix COUNT number of times."
-  (interactive (list (read-regexp "Search whole file backward"
-                                  (if regexp-history
-                                      (car regexp-history))
-                                  'regexp-history)
-                     (or current-prefix-arg 1)))
-  (vlf-re-search regexp count t))
-
-;;; 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)
-    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-write ()
-  "Write current chunk to file.  May overwrite existing content."
+(defun vlf-beginning-of-file ()
+  "Jump to beginning of file content."
+  (interactive)
+  (vlf-move-to-batch 0))
+
+(defun vlf-end-of-file ()
+  "Jump to end of file content."
+  (interactive)
+  (vlf-verify-size)
+  (vlf-move-to-batch vlf-file-size))
+
+(defun vlf-revert (&optional _ignore-auto noconfirm)
+  "Revert current chunk.  Ignore _IGNORE-AUTO.
+Ask for confirmation if NOCONFIRM is nil."
   (interactive)
-  (when (or (= (buffer-size) (- vlf-end-pos vlf-start-pos))
-            (y-or-n-p "Changed size of original chunk.  \
-End of chunk will be garbled.  Continue? "))
-    (write-region nil nil buffer-file-name vlf-start-pos)
-    (vlf-move-to-chunk vlf-start-pos vlf-end-pos)
-    (vlf-mode)))
-
-(defun vlf-discard-edit ()
-  "Discard edit and refresh chunk from file."
+  (when (or noconfirm
+            (yes-or-no-p (format "Revert buffer from file %s? "
+                                 buffer-file-name)))
+    (set-buffer-modified-p nil)
+    (vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)))
+
+(defun vlf-jump-to-chunk (n)
+  "Go to to chunk N."
+  (interactive "nGoto to chunk: ")
+  (vlf-move-to-batch (* (1- n) vlf-batch-size)))
+
+(defun vlf-no-modifications ()
+  "Ensure there are no buffer modifications."
+  (if (buffer-modified-p)
+      (error "Save or discard your changes first")
+    t))
+
+(defun vlf-move-to-batch (start &optional minimal)
+  "Move to batch determined by START.
+Adjust according to file start/end and show `vlf-batch-size' bytes.
+When given MINIMAL flag, skip non important operations."
+  (vlf-verify-size)
+  (let* ((start (max 0 start))
+         (end (min (+ start vlf-batch-size) vlf-file-size)))
+    (if (= vlf-file-size end)          ; re-adjust start
+        (setq start (max 0 (- end vlf-batch-size))))
+    (vlf-move-to-chunk start end minimal)))
+
+(defun vlf-next-batch-from-point ()
+  "Display batch of file data starting from current point."
   (interactive)
-  (vlf-move-to-chunk vlf-start-pos vlf-end-pos)
-  (vlf-mode)
-  (message "Switched to VLF mode."))
+  (let ((start (+ vlf-start-pos (position-bytes (point)) -1)))
+    (vlf-move-to-chunk start (+ start vlf-batch-size)))
+  (goto-char (point-min)))
 
 (provide 'vlf)