]> code.delx.au - gnu-emacs-elpa/blob - packages/vlf/vlf.el
Merge commit '469cd3bc117bfb8da0c03a2a2fb185e80c81d068'
[gnu-emacs-elpa] / packages / vlf / vlf.el
1 ;;; vlf.el --- View Large Files -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2006, 2012-2014 Free Software Foundation, Inc.
4
5 ;; Version: 1.5
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
12
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)
16 ;; any later version.
17
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.
22
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.
27
28 ;;; Commentary:
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)
35
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
38 ;; file.
39
40 ;;; Code:
41
42 (defgroup vlf nil "View Large Files in Emacs."
43 :prefix "vlf-" :group 'files)
44
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)
50
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)
56
57 (require 'vlf-base)
58
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)
72
73 (defvar vlf-mode-map
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)
79 (define-key map "-"
80 (lambda () "Decrease vlf batch size by factor of 2."
81 (interactive)
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)
93 map)
94 "Keymap for `vlf-mode'.")
95
96 (defvar vlf-prefix-map
97 (let ((map (make-sparse-keymap)))
98 (define-key map "\C-c\C-v" vlf-mode-map)
99 map)
100 "Prefixed keymap for `vlf-mode'.")
101
102 (define-minor-mode vlf-mode
103 "Mode to browse large files in."
104 :lighter " VLF" :group 'vlf :keymap vlf-prefix-map
105 (cond (vlf-mode
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)
109 'vlf-revert)
110 (make-local-variable 'vlf-batch-size)
111 (setq vlf-file-size (vlf-get-file-size buffer-file-truename)
112 vlf-start-pos 0
113 vlf-end-pos 0)
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)
119 (vlf-keep-alive))
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
124 vlf-file-size))))
125 (kill-local-variable 'revert-buffer-function)
126 (vlf-stop-follow)
127 (kill-local-variable 'require-final-newline)
128 (remove-hook 'write-file-functions 'vlf-write t)
129 (remove-hook 'after-change-major-mode-hook
130 'vlf-keep-alive t)
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))))
140
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))
145 (setq vlf-mode t))
146
147 ;;;###autoload
148 (defun vlf (file)
149 "View Large FILE in batches.
150 You can customize number of bytes displayed by customizing
151 `vlf-batch-size'.
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)
158 (vlf-mode 1)
159 (switch-to-buffer vlf-buffer)
160 vlf-buffer))
161
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."
168 (interactive "p")
169 (vlf-verify-size)
170 (let* ((end (min (+ vlf-end-pos (* vlf-batch-size (abs append)))
171 vlf-file-size))
172 (start (if (< append 0)
173 vlf-start-pos
174 (- end vlf-batch-size))))
175 (vlf-move-to-chunk start end)))
176
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."
183 (interactive "p")
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)
188 vlf-end-pos
189 (+ start vlf-batch-size))))
190 (vlf-move-to-chunk start end)))
191
192 ;; scroll auto batching
193 (defadvice scroll-up (around vlf-scroll-up
194 activate compile)
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)))
199 ad-do-it))
200
201 (defadvice scroll-down (around vlf-scroll-down
202 activate compile)
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)))
207 ad-do-it))
208
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)
213 (hexl-mode-exit)
214 (set (make-local-variable 'vlf-restore-hexl-mode) operation)))
215
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))
220 (hexl-mode)
221 (kill-local-variable 'vlf-restore-hexl-mode)))
222
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)
227
228 (eval-after-load "hexl"
229 '(progn
230 (defadvice hexl-save-buffer (around vlf-hexl-save
231 activate compile)
232 "Prevent hexl save if `vlf-mode' is active."
233 (if vlf-mode
234 (vlf-write)
235 ad-do-it))
236
237 (defadvice hexl-scroll-up (around vlf-hexl-scroll-up
238 activate compile)
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)))
244 ad-do-it))
245
246 (defadvice hexl-scroll-down (around vlf-hexl-scroll-down
247 activate compile)
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)))
252 ad-do-it))))
253
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;;; utilities
256
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."
261 (interactive "P")
262 (vlf-set-batch-size (if decrease (/ vlf-batch-size 2)
263 (* vlf-batch-size 2))))
264
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))
270
271 (defun vlf-beginning-of-file ()
272 "Jump to beginning of file content."
273 (interactive)
274 (vlf-move-to-batch 0))
275
276 (defun vlf-end-of-file ()
277 "Jump to end of file content."
278 (interactive)
279 (vlf-verify-size)
280 (vlf-move-to-batch vlf-file-size))
281
282 (defun vlf-revert (&optional _ignore-auto noconfirm)
283 "Revert current chunk. Ignore _IGNORE-AUTO.
284 Ask for confirmation if NOCONFIRM is nil."
285 (interactive)
286 (when (or noconfirm
287 (yes-or-no-p (format "Revert buffer from file %s? "
288 buffer-file-name)))
289 (set-buffer-modified-p nil)
290 (vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)))
291
292 (defun vlf-jump-to-chunk (n)
293 "Go to to chunk N."
294 (interactive "nGoto to chunk: ")
295 (vlf-move-to-batch (* (1- n) vlf-batch-size)))
296
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")
301 t))
302
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."
307 (vlf-verify-size)
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)))
313
314 (defun vlf-next-batch-from-point ()
315 "Display batch of file data starting from current point."
316 (interactive)
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)))
320
321 (provide 'vlf)
322
323 ;;; vlf.el ends here