;; Copyright (C) 2006, 2012-2014 Free Software Foundation, Inc.
-;; Version: 1.2
+;; Version: 1.3
;; Keywords: large files, utilities
;; Maintainer: Andrey Kotlarski <m00naticus@gmail.com>
;; Authors: 2006 Mathias Dahl <mathias.dahl@gmail.com>
;; Boston, MA 02111-1307, USA.
;;; Commentary:
-;; This package provides the M-x vlf command, which visits part of a
-;; large file without loading the entire file.
-;; The buffer uses VLF mode, which defines several commands for
+;; 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 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:
-;;;###autoload
-(require 'vlf-integrate)
-
(require 'vlf-base)
-(defcustom vlf-batch-size 1024
- "Defines how large each batch of file data is (in bytes)."
- :group 'vlf
- :type 'integer)
-(put 'vlf-batch-size 'permanent-local t)
-
-;;; Keep track of file position.
-(defvar vlf-start-pos 0
- "Absolute position of the visible chunk start.")
-(put 'vlf-start-pos 'permanent-local t)
-
-(defvar vlf-end-pos 0 "Absolute position of the visible chunk end.")
-(put 'vlf-end-pos 'permanent-local t)
-
-(defvar vlf-file-size 0 "Total size of presented file.")
-(put 'vlf-file-size 'permanent-local t)
-
(autoload 'vlf-write "vlf-write" "Write current chunk to file.")
(autoload 'vlf-re-search-forward "vlf-search"
"Search forward for REGEXP prefix COUNT number of times.")
"Make whole file occur style index for REGEXP.")
(autoload 'vlf-toggle-follow "vlf-follow"
"Toggle continuous chunk recenter around current point.")
+(autoload 'vlf-stop-follow "vlf-follow"
+ "Stop continuous recenter.")
(defvar vlf-mode-map
(let ((map (make-sparse-keymap)))
map)
"Prefixed keymap for `vlf-mode'.")
-(defmacro vlf-with-undo-disabled (&rest body)
- "Execute BODY with temporarily disabled undo."
- `(let ((undo-list buffer-undo-list))
- (setq buffer-undo-list t)
- (unwind-protect (progn ,@body)
- (setq buffer-undo-list undo-list))))
-
(define-minor-mode vlf-mode
"Mode to browse large files in."
:lighter " VLF"
(set (make-local-variable 'revert-buffer-function)
'vlf-revert)
(make-local-variable 'vlf-batch-size)
- (set (make-local-variable 'vlf-file-size)
- (vlf-get-file-size buffer-file-truename))
- (set (make-local-variable 'vlf-start-pos) 0)
- (set (make-local-variable 'vlf-end-pos) 0)
- (set (make-local-variable 'vlf-follow-timer) nil)
+ (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)))
(kill-local-variable 'revert-buffer-function)
- (vlf-stop-following)
+ (vlf-stop-follow)
(when (or (not large-file-warning-threshold)
(< vlf-file-size large-file-warning-threshold)
(y-or-n-p (format "Load whole file (%s)? "
You can customize number of bytes displayed by customizing
`vlf-batch-size'."
(interactive "fFile to open: ")
- (with-current-buffer (generate-new-buffer "*vlf*")
+ (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 (current-buffer))))
+ (switch-to-buffer vlf-buffer)
+ vlf-buffer))
+
+(defun vlf-next-batch (append)
+ "Display the next batch of file data.
+When prefix argument is supplied and positive
+ jump over APPEND number of batches.
+When prefix argument is negative
+ append next APPEND number of batches to the existing buffer."
+ (interactive "p")
+ (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.
+When prefix argument is supplied and positive
+ jump over PREPEND number of batches.
+When prefix argument is negative
+ append previous PREPEND number of batches to the existing buffer."
+ (interactive "p")
+ (if (zerop vlf-start-pos)
+ (error "Already at BOF"))
+ (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
(* vlf-batch-size 2)))
(vlf-move-to-batch vlf-start-pos))
-(defun vlf-update-buffer-name ()
- "Update the current buffer name."
- (rename-buffer (format "%s(%d/%d)[%s]"
- (file-name-nondirectory buffer-file-name)
- (/ vlf-end-pos vlf-batch-size)
- (/ vlf-file-size vlf-batch-size)
- (file-size-human-readable vlf-batch-size))
- t))
-
-(defun vlf-get-file-size (file)
- "Get size in bytes of FILE."
- (or (nth 7 (file-attributes file)) 0))
-
-(defun vlf-verify-size ()
- "Update file size information if necessary and visited file time."
- (unless (verify-visited-file-modtime (current-buffer))
- (setq vlf-file-size (vlf-get-file-size buffer-file-truename))
- (set-visited-file-modtime)))
-
-(defun vlf-insert-file (&optional from-end)
- "Insert first chunk of current file contents in current buffer.
-With FROM-END prefix, start from the back."
- (let ((start 0)
- (end vlf-batch-size))
- (if from-end
- (setq start (- vlf-file-size vlf-batch-size)
- end vlf-file-size)
- (setq end (min vlf-batch-size vlf-file-size)))
- (vlf-move-to-chunk start end)))
-
(defun vlf-beginning-of-file ()
"Jump to beginning of file content."
(interactive)
- (vlf-insert-file))
+ (vlf-move-to-batch 0))
(defun vlf-end-of-file ()
"Jump to end of file content."
(interactive)
- (vlf-insert-file t))
+ (vlf-move-to-batch vlf-file-size))
(defun vlf-revert (&optional _ignore-auto noconfirm)
"Revert current chunk. Ignore _IGNORE-AUTO.
(yes-or-no-p (format "Revert buffer from file %s? "
buffer-file-name)))
(set-buffer-modified-p nil)
- (set-visited-file-modtime)
(vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)))
(defun vlf-jump-to-chunk (n)
(error "Save or discard your changes first")
t))
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;; batch movement
-
-(defun vlf-next-batch (append)
- "Display the next batch of file data.
-When prefix argument is supplied and positive
- jump over APPEND number of batches.
-When prefix argument is negative
- append next APPEND number of batches to the existing buffer."
- (interactive "p")
- (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.
-When prefix argument is supplied and positive
- jump over PREPEND number of batches.
-When prefix argument is negative
- append previous PREPEND number of batches to the existing buffer."
- (interactive "p")
- (if (zerop vlf-start-pos)
- (error "Already at BOF"))
- (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)))
-
(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.