1 ;;; vlf.el --- View Large Files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006, 2012-2014 Free Software Foundation, Inc.
6 ;; Keywords: large files, utilities
7 ;; Maintainer: Andrey Kotlarski <m00naticus@gmail.com>
8 ;; Authors: 2006 Mathias Dahl <mathias.dahl@gmail.com>
9 ;; 2012 Sam Steingold <sds@gnu.org>
10 ;; 2013-2014 Andrey Kotlarski <m00naticus@gmail.com>
11 ;; URL: https://github.com/m00natic/vlfi
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; This file is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
29 ;; This package provides the M-x vlf command, which visits part of
30 ;; large file without loading it entirely. The buffer uses VLF mode,
31 ;; which provides several commands for moving around, searching,
32 ;; comparing and editing selected part of file.
33 ;; To have it offered when opening large files:
34 ;; (require 'vlf-integrate)
36 ;; This package was inspired by a snippet posted by Kevin Rodgers,
37 ;; showing how to use `insert-file-contents' to extract part of a
42 (defgroup vlf nil "View Large Files in Emacs."
43 :prefix "vlf-" :group 'files)
45 (defcustom vlf-before-batch-functions nil
46 "Hook that runs before multiple batch operations.
47 One argument is supplied that specifies current action. Possible
48 values are: `write', `ediff', `occur', `search', `goto-line'."
49 :group 'vlf :type 'hook)
51 (defcustom vlf-after-batch-functions nil
52 "Hook that runs after multiple batch operations.
53 One argument is supplied that specifies current action. Possible
54 values are: `write', `ediff', `occur', `search', `goto-line'."
55 :group 'vlf :type 'hook)
59 (autoload 'vlf-write "vlf-write" "Write current chunk to file." t)
60 (autoload 'vlf-re-search-forward "vlf-search"
61 "Search forward for REGEXP prefix COUNT number of times." t)
62 (autoload 'vlf-re-search-backward "vlf-search"
63 "Search backward for REGEXP prefix COUNT number of times." t)
64 (autoload 'vlf-goto-line "vlf-search" "Go to line." t)
65 (autoload 'vlf-occur "vlf-occur"
66 "Make whole file occur style index for REGEXP." t)
67 (autoload 'vlf-toggle-follow "vlf-follow"
68 "Toggle continuous chunk recenter around current point." t)
69 (autoload 'vlf-stop-follow "vlf-follow" "Stop continuous recenter." t)
70 (autoload 'vlf-ediff-buffers "vlf-ediff"
71 "Run batch by batch ediff over VLF buffers." t)
74 (let ((map (make-sparse-keymap)))
75 (define-key map "n" 'vlf-next-batch)
76 (define-key map "p" 'vlf-prev-batch)
77 (define-key map " " 'vlf-next-batch-from-point)
78 (define-key map "+" 'vlf-change-batch-size)
80 (lambda () "Decrease vlf batch size by factor of 2."
82 (vlf-change-batch-size t)))
83 (define-key map "s" 'vlf-re-search-forward)
84 (define-key map "r" 'vlf-re-search-backward)
85 (define-key map "o" 'vlf-occur)
86 (define-key map "[" 'vlf-beginning-of-file)
87 (define-key map "]" 'vlf-end-of-file)
88 (define-key map "j" 'vlf-jump-to-chunk)
89 (define-key map "l" 'vlf-goto-line)
90 (define-key map "e" 'vlf-ediff-buffers)
91 (define-key map "f" 'vlf-toggle-follow)
92 (define-key map "g" 'vlf-revert)
94 "Keymap for `vlf-mode'.")
96 (defvar vlf-prefix-map
97 (let ((map (make-sparse-keymap)))
98 (define-key map "\C-c\C-v" vlf-mode-map)
100 "Prefixed keymap for `vlf-mode'.")
102 (define-minor-mode vlf-mode
103 "Mode to browse large files in."
104 :lighter " VLF" :group 'vlf :keymap vlf-prefix-map
106 (set (make-local-variable 'require-final-newline) nil)
107 (add-hook 'write-file-functions 'vlf-write nil t)
108 (set (make-local-variable 'revert-buffer-function)
110 (make-local-variable 'vlf-batch-size)
111 (setq vlf-file-size (vlf-get-file-size buffer-file-truename)
114 (let* ((pos (position-bytes (point)))
115 (start (* (/ pos vlf-batch-size) vlf-batch-size)))
116 (goto-char (byte-to-position (- pos start)))
117 (vlf-move-to-batch start))
118 (add-hook 'after-change-major-mode-hook 'vlf-keep-alive t t)
120 ((or (not large-file-warning-threshold)
121 (< vlf-file-size large-file-warning-threshold)
122 (y-or-n-p (format "Load whole file (%s)? "
123 (file-size-human-readable
125 (kill-local-variable 'revert-buffer-function)
127 (kill-local-variable 'require-final-newline)
128 (remove-hook 'write-file-functions 'vlf-write t)
129 (remove-hook 'after-change-major-mode-hook
131 (let ((hexl (derived-mode-p 'hexl-mode)))
132 (if hexl (hexl-mode-exit))
133 (let ((pos (+ vlf-start-pos (position-bytes (point)))))
134 (vlf-with-undo-disabled
135 (insert-file-contents buffer-file-name t nil nil t))
136 (goto-char (byte-to-position pos)))
137 (if hexl (hexl-mode)))
138 (rename-buffer (file-name-nondirectory buffer-file-name) t))
139 (t (setq vlf-mode t))))
141 (defun vlf-keep-alive ()
142 "Keep `vlf-mode' on major mode change."
143 (if (derived-mode-p 'hexl-mode)
144 (set (make-local-variable 'revert-buffer-function) 'vlf-revert))
149 "View Large FILE in batches.
150 You can customize number of bytes displayed by customizing
152 Return newly created buffer."
153 (interactive "fFile to open: ")
154 (let ((vlf-buffer (generate-new-buffer "*vlf*")))
155 (set-buffer vlf-buffer)
156 (set-visited-file-name file)
157 (set-buffer-modified-p nil)
159 (switch-to-buffer vlf-buffer)
162 (defun vlf-next-batch (append)
163 "Display the next batch of file data.
164 When prefix argument is supplied and positive
165 jump over APPEND number of batches.
166 When prefix argument is negative
167 append next APPEND number of batches to the existing buffer."
170 (let* ((end (min (+ vlf-end-pos (* vlf-batch-size (abs append)))
172 (start (if (< append 0)
174 (- end vlf-batch-size))))
175 (vlf-move-to-chunk start end)))
177 (defun vlf-prev-batch (prepend)
178 "Display the previous batch of file data.
179 When prefix argument is supplied and positive
180 jump over PREPEND number of batches.
181 When prefix argument is negative
182 append previous PREPEND number of batches to the existing buffer."
184 (if (zerop vlf-start-pos)
185 (error "Already at BOF"))
186 (let* ((start (max 0 (- vlf-start-pos (* vlf-batch-size (abs prepend)))))
187 (end (if (< prepend 0)
189 (+ start vlf-batch-size))))
190 (vlf-move-to-chunk start end)))
192 ;; scroll auto batching
193 (defadvice scroll-up (around vlf-scroll-up
195 "Slide to next batch if at end of buffer in `vlf-mode'."
196 (if (and vlf-mode (pos-visible-in-window-p (point-max)))
197 (progn (vlf-next-batch 1)
198 (goto-char (point-min)))
201 (defadvice scroll-down (around vlf-scroll-down
203 "Slide to previous batch if at beginning of buffer in `vlf-mode'."
204 (if (and vlf-mode (pos-visible-in-window-p (point-min)))
205 (progn (vlf-prev-batch 1)
206 (goto-char (point-max)))
209 ;; hexl mode integration
210 (defun vlf-hexl-before (&optional operation)
211 "Temporarily disable `hexl-mode' for OPERATION."
212 (when (derived-mode-p 'hexl-mode)
214 (set (make-local-variable 'vlf-restore-hexl-mode) operation)))
216 (defun vlf-hexl-after (&optional operation)
217 "Re-enable `hexl-mode' if active before OPERATION."
218 (when (and (boundp 'vlf-restore-hexl-mode)
219 (eq vlf-restore-hexl-mode operation))
221 (kill-local-variable 'vlf-restore-hexl-mode)))
223 (add-hook 'vlf-before-batch-functions 'vlf-hexl-before)
224 (add-hook 'vlf-after-batch-functions 'vlf-hexl-after)
225 (add-hook 'vlf-before-chunk-update 'vlf-hexl-before)
226 (add-hook 'vlf-after-chunk-update 'vlf-hexl-after)
228 (eval-after-load "hexl"
230 (defadvice hexl-save-buffer (around vlf-hexl-save
232 "Prevent hexl save if `vlf-mode' is active."
237 (defadvice hexl-scroll-up (around vlf-hexl-scroll-up
239 "Slide to next batch if at end of buffer in `vlf-mode'."
240 (if (and vlf-mode (pos-visible-in-window-p (point-max))
241 (or (not (numberp arg)) (< 0 arg)))
242 (progn (vlf-next-batch 1)
243 (goto-char (point-min)))
246 (defadvice hexl-scroll-down (around vlf-hexl-scroll-down
248 "Slide to previous batch if at beginning of buffer in `vlf-mode'."
249 (if (and vlf-mode (pos-visible-in-window-p (point-min)))
250 (progn (vlf-prev-batch 1)
251 (goto-char (point-max)))
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 (defun vlf-change-batch-size (decrease)
258 "Change the buffer-local value of `vlf-batch-size'.
259 Normally, the value is doubled;
260 with the prefix argument DECREASE it is halved."
262 (vlf-set-batch-size (if decrease (/ vlf-batch-size 2)
263 (* vlf-batch-size 2))))
265 (defun vlf-set-batch-size (size)
266 "Set batch to SIZE bytes and update chunk."
267 (interactive (list (read-number "Size in bytes: " vlf-batch-size)))
268 (setq vlf-batch-size size)
269 (vlf-move-to-batch vlf-start-pos))
271 (defun vlf-beginning-of-file ()
272 "Jump to beginning of file content."
274 (vlf-move-to-batch 0))
276 (defun vlf-end-of-file ()
277 "Jump to end of file content."
280 (vlf-move-to-batch vlf-file-size))
282 (defun vlf-revert (&optional _ignore-auto noconfirm)
283 "Revert current chunk. Ignore _IGNORE-AUTO.
284 Ask for confirmation if NOCONFIRM is nil."
287 (yes-or-no-p (format "Revert buffer from file %s? "
289 (set-buffer-modified-p nil)
290 (vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)))
292 (defun vlf-jump-to-chunk (n)
294 (interactive "nGoto to chunk: ")
295 (vlf-move-to-batch (* (1- n) vlf-batch-size)))
297 (defun vlf-no-modifications ()
298 "Ensure there are no buffer modifications."
299 (if (buffer-modified-p)
300 (error "Save or discard your changes first")
303 (defun vlf-move-to-batch (start &optional minimal)
304 "Move to batch determined by START.
305 Adjust according to file start/end and show `vlf-batch-size' bytes.
306 When given MINIMAL flag, skip non important operations."
308 (let* ((start (max 0 start))
309 (end (min (+ start vlf-batch-size) vlf-file-size)))
310 (if (= vlf-file-size end) ; re-adjust start
311 (setq start (max 0 (- end vlf-batch-size))))
312 (vlf-move-to-chunk start end minimal)))
314 (defun vlf-next-batch-from-point ()
315 "Display batch of file data starting from current point."
317 (let ((start (+ vlf-start-pos (position-bytes (point)) -1)))
318 (vlf-move-to-chunk start (+ start vlf-batch-size)))
319 (goto-char (point-min)))