1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-11-08 19:11:38 +00:00

21 Commits
0.5 ... 0.6

Author SHA1 Message Date
Andrey Kotlarski
705f9ce0eb Add issue section to the README. 2013-04-13 23:45:30 +03:00
Andrey Kotlarski
b52ca6c044 Don't change encoding for edited chunk, leave it to the user in case
of problem characters.
2013-04-13 23:45:06 +03:00
Andrey Kotlarski
8637ce5ceb Update README and version. 2013-04-13 23:10:47 +03:00
Andrey Kotlarski
2f201c56d5 Add jump to chunk command. 2013-04-13 22:57:09 +03:00
Andrey Kotlarski
d5f2a36086 Rearrange code in sections. 2013-04-13 22:49:08 +03:00
Andrey Kotlarski
cedd0b4e82 Fix spaces and indent. 2013-04-13 22:36:33 +03:00
Andrey Kotlarski
c14c7f00ce Add shift forward of file contents when edited chunk grows in size. 2013-04-13 22:34:54 +03:00
Andrey Kotlarski
f23262e826 Add shift back of file contents when edited chunk shrinks in size. 2013-04-13 20:47:29 +03:00
Andrey Kotlarski
8f6299c6c2 Simplify detection of buffer size change. 2013-04-13 01:29:12 +03:00
Andrey Kotlarski
8787186619 Show paging according to batch size instead of percentage. 2013-04-12 01:01:42 +03:00
Andrey Kotlarski
24675d8741 Manage modification times when attempting to save. 2013-04-12 00:21:14 +03:00
Andrey Kotlarski
e115e7e56a Add custom revert function. 2013-04-11 23:00:34 +03:00
Andrey Kotlarski
7985f0f453 Fix `vlfi-if-file-too-large' to be more tolerable on GNU Emacs 23. 2013-04-11 18:36:39 +03:00
Andrey Kotlarski
a3c405fd37 Make detection of buffer size change on save more correct when dealing
with Unicode.
2013-04-11 18:08:34 +03:00
Andrey Kotlarski
56134a8281 Make vlfi usable under GNU Emacs 23. 2013-04-11 17:45:35 +03:00
Andrey Kotlarski
2525ebf069 Improvements to file saving. 2013-04-11 17:44:32 +03:00
Andrey Kotlarski
c4f87f9ec7 Move save interception to vlfi-mode. 2013-04-11 16:53:38 +03:00
Andrey Kotlarski
467154ad06 Add documentation. 2013-04-10 02:10:00 +03:00
Andrey Kotlarski
06108220e2 Make saving with change of size correct. 2013-04-10 02:08:54 +03:00
Andrey Kotlarski
5405a30de4 Intercept buffer saving with vlfi-write. 2013-04-08 23:24:48 +03:00
Andrey Kotlarski
f3132c0970 Keep current position when moving around chunks. 2013-04-08 12:49:17 +03:00
2 changed files with 265 additions and 103 deletions

View File

@@ -1,13 +1,22 @@
* View Large File Improved
* View Large Files Improved
An Emacs mode that allows viewing files in chunks. This is a fork
that builds on the GNU ELPA vlf.el. It adds the following
that builds on the bare bones GNU ELPA vlf.el. It adds the following
improvements:
- by chunk search
- chunk editing
- options to jump to end or beginning of file
- regular expression search on whole file (in constant memory
determined by current batch size)
- chunk editing (if size has changed, saving is done in constant
memory determined by current batch size)
- options to jump to beginning, end or arbitrary file chunk
- ability to jump/insert given number of batches at once
- ability to view newly added content if the file has grown meanwhile
- newly added content is acknowledged if file has changed size
meanwhile
- vlfi is added as an option when opening large files
* Known issues
- When chunk starts with only part of multi-byte character, encoding
for this chunk is clobbered visibly, don't edit/save in such case.
Workaround is to move back to the first non-clobbered chunk and
enlarge batch size until this chunk comes into range.

347
vlfi.el
View File

@@ -1,9 +1,8 @@
;;; vlfi.el --- View Large Files Improved
;;; -*- lexical-bind: t -*-
;;; vlfi.el --- View Large Files Improved -*- lexical-binding: t -*-
;; Copyright (C) 2006, 2012, 2013 Free Software Foundation, Inc.
;; Version: 0.5
;; Version: 0.6
;; Keywords: large files, utilities
;; Authors: 2006 Mathias Dahl <mathias.dahl@gmail.com>
;; 2012 Sam Steingold <sds@gnu.org>
@@ -63,13 +62,10 @@
(vlfi-change-batch-size t)))
(define-key map "s" 'vlfi-re-search-forward)
(define-key map "r" 'vlfi-re-search-backward)
(define-key map "]" (lambda () "Jump to end of file content."
(interactive)
(vlfi-insert-file buffer-file-name t)))
(define-key map "[" (lambda () "Jump to beginning of file content."
(interactive)
(vlfi-insert-file buffer-file-name)))
(define-key map "[" 'vlfi-beginning-of-file)
(define-key map "]" 'vlfi-end-of-file)
(define-key map "e" 'vlfi-edit-mode)
(define-key map "j" 'vlfi-jump-to-chunk)
map)
"Keymap for `vlfi-mode'.")
@@ -78,6 +74,8 @@
(setq buffer-read-only t)
(set-buffer-modified-p nil)
(buffer-disable-undo)
(add-hook 'write-contents-functions 'vlfi-write)
(setq revert-buffer-function 'vlfi-revert)
(make-local-variable 'vlfi-batch-size)
(put 'vlfi-batch-size 'permanent-local t)
(make-local-variable 'vlfi-start-pos)
@@ -87,6 +85,75 @@
(make-local-variable 'vlfi-file-size)
(put 'vlfi-file-size 'permanent-local t))
;;;###autoload
(defun vlfi (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
`vlfi-batch-size'."
(interactive "fFile to open: \nP")
(with-current-buffer (generate-new-buffer "*vlfi*")
(setq buffer-file-name file
vlfi-file-size (nth 7 (file-attributes file)))
(vlfi-insert-file from-end)
(vlfi-mode)
(switch-to-buffer (current-buffer))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; integration with other packages
;;;###autoload
(defun dired-vlfi (from-end)
"In Dired, visit the file on this line in VLFI mode.
With FROM-END prefix, view from the back."
(interactive "P")
(vlfi (dired-get-file-for-visit) from-end))
;;;###autoload
(eval-after-load "dired"
'(define-key dired-mode-map "V" 'dired-vlfi))
;;;###autoload
(defun vlfi-if-file-too-large (size op-type &optional filename)
"If file SIZE larger than `large-file-warning-threshold', \
allow user to view file with `vlfi', 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 vlfi (v) or abort (a)"
(if filename
(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))
(vlfi filename nil)
(error ""))
((memq char '(?a ?A))
(error "Aborted"))))))
;; hijack `abort-if-file-too-large'
;;;###autoload
(fset 'abort-if-file-too-large 'vlfi-if-file-too-large)
;; non recent Emacs
(unless (fboundp 'file-size-human-readable)
(defun file-size-human-readable (file-size)
"Print FILE-SIZE in MB."
(format "%.1fMB" (/ file-size 1024.0))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; utilities
(defun vlfi-change-batch-size (decrease)
"Change the buffer-local value of `vlfi-batch-size'.
Normally, the value is doubled;
@@ -101,16 +168,50 @@ with the prefix argument DECREASE it is halved."
(defun vlfi-format-buffer-name ()
"Return format for vlfi buffer name."
(format "%s(%s)[%.2f%%%%](%d)"
(format "%s(%s)[%d/%d](%d)"
(file-name-nondirectory buffer-file-name)
(file-size-human-readable vlfi-file-size)
(/ (* 100 vlfi-end-pos) (float vlfi-file-size))
(/ vlfi-end-pos vlfi-batch-size)
(/ vlfi-file-size vlfi-batch-size)
vlfi-batch-size))
(defun vlfi-update-buffer-name ()
"Update the current buffer name."
(rename-buffer (vlfi-format-buffer-name) t))
(defun vlfi-insert-file (&optional from-end)
"Insert first chunk of current file contents in current buffer.
With FROM-END prefix, start from the back."
(if from-end
(setq vlfi-start-pos (max 0 (- vlfi-file-size vlfi-batch-size))
vlfi-end-pos vlfi-file-size)
(setq vlfi-start-pos 0
vlfi-end-pos (min vlfi-batch-size vlfi-file-size)))
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos))
(defun vlfi-beginning-of-file ()
"Jump to beginning of file content."
(interactive)
(vlfi-insert-file))
(defun vlfi-end-of-file ()
"Jump to end of file content."
(interactive)
(vlfi-insert-file t))
(defun vlfi-revert (&rest args)
"Revert current chunk. Ignore ARGS."
(ignore args)
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos))
(defun vlfi-jump-to-chunk (n)
"Go to to chunk N."
(interactive "nGoto to chunk: ")
(vlfi-move-to-batch (* (1- n) vlfi-batch-size)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; batch movement
(defun vlfi-next-batch (append)
"Display the next batch of file data.
When prefix argument is supplied and positive
@@ -120,7 +221,7 @@ When prefix argument is negative
(interactive "p")
(let ((end (+ vlfi-end-pos (* vlfi-batch-size
(abs append)))))
(when (< vlfi-file-size end) ; re-check file size
(when (< vlfi-file-size end) ; re-check file size
(setq vlfi-file-size (nth 7 (file-attributes buffer-file-name)))
(cond ((= vlfi-end-pos vlfi-file-size)
(error "Already at EOF"))
@@ -133,13 +234,13 @@ When prefix argument is negative
(goto-char (point-max))
(setq vlfi-start-pos (- end vlfi-batch-size))
(erase-buffer))
(insert-file-contents buffer-file-name nil
(if do-append
vlfi-end-pos
vlfi-start-pos)
(insert-file-contents buffer-file-name nil (if do-append
vlfi-end-pos
vlfi-start-pos)
end)
(goto-char pos))
(setq vlfi-end-pos end))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
@@ -167,6 +268,7 @@ When prefix argument is negative
vlfi-end-pos))
(goto-char (- (point-max) pos))
(setq vlfi-start-pos start))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
@@ -180,92 +282,36 @@ Adjust according to file start/end and show `vlfi-batch-size' bytes."
(nth 7 (file-attributes buffer-file-name))
vlfi-end-pos (min vlfi-end-pos vlfi-file-size)
vlfi-start-pos (max 0 (- vlfi-end-pos vlfi-batch-size))))
(let ((inhibit-read-only t))
(let ((inhibit-read-only t)
(pos (point)))
(erase-buffer)
(insert-file-contents buffer-file-name nil
vlfi-start-pos vlfi-end-pos))
vlfi-start-pos vlfi-end-pos)
(goto-char pos))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
(defun vlfi-move-to-chunk (start end)
"Move to chunk determined by START END."
(if (< vlfi-file-size end) ; re-check file size
(if (< vlfi-file-size end) ; re-check file size
(setq vlfi-file-size (nth 7
(file-attributes buffer-file-name))))
(setq vlfi-start-pos (max 0 start)
vlfi-end-pos (min end vlfi-file-size))
(let ((inhibit-read-only t))
(let ((inhibit-read-only t)
(pos (point)))
(erase-buffer)
(insert-file-contents buffer-file-name nil
vlfi-start-pos vlfi-end-pos))
vlfi-start-pos vlfi-end-pos)
(goto-char pos))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
(defun vlfi-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 vlfi-start-pos (max 0 (- vlfi-file-size vlfi-batch-size))
vlfi-end-pos vlfi-file-size)
(setq vlfi-start-pos 0
vlfi-end-pos (min vlfi-batch-size vlfi-file-size)))
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos))
;;;###autoload
(defun vlfi (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
`vlfi-batch-size'."
(interactive "fFile to open: \nP")
(with-current-buffer (generate-new-buffer "*vlfi*")
(setq buffer-file-name file
vlfi-file-size (nth 7 (file-attributes file)))
(vlfi-insert-file file from-end)
(vlfi-mode)
(switch-to-buffer (current-buffer))))
;;;###autoload
(defun dired-vlfi (from-end)
"In Dired, visit the file on this line in VLFI mode.
With FROM-END prefix, view from the back."
(interactive "P")
(vlfi (dired-get-file-for-visit) from-end))
;;;###autoload
(eval-after-load "dired"
'(define-key dired-mode-map "V" 'dired-vlfi))
;;;###autoload
(defun vlfi-if-file-too-large (size op-type &optional filename)
"If file SIZE larger than `large-file-warning-threshold', \
allow user to view file with `vlfi', 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 vlfi (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))
(vlfi filename nil)
(error ""))
((memq char '(?a ?A))
(error "Aborted"))))))
;;; hijack `abort-if-file-too-large'
;;;###autoload
(fset 'abort-if-file-too-large 'vlfi-if-file-too-large)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; search
(defun vlfi-re-search (regexp count backward)
"Search for REGEXP COUNT number of times forward or BACKWARD."
(let* ((match-start-pos (+ vlfi-start-pos (point)))
@@ -354,7 +400,8 @@ successful. Return nil if nothing found."
(delete-overlay overlay)))))
(defun vlfi-re-search-forward (regexp count)
"Search forward for REGEXP prefix COUNT number of times."
"Search forward for REGEXP prefix COUNT number of times.
Search is performed chunk by chunk in `vlfi-batch-size' memory."
(interactive (list (read-regexp "Search whole file"
(if regexp-history
(car regexp-history))
@@ -363,7 +410,8 @@ successful. Return nil if nothing found."
(vlfi-re-search regexp count nil))
(defun vlfi-re-search-backward (regexp count)
"Search backward for REGEXP prefix COUNT number of times."
"Search backward for REGEXP prefix COUNT number of times.
Search is performed chunk by chunk in `vlfi-batch-size' memory."
(interactive (list (read-regexp "Search whole file backward"
(if regexp-history
(car regexp-history))
@@ -371,7 +419,9 @@ successful. Return nil if nothing found."
(or current-prefix-arg 1)))
(vlfi-re-search regexp count t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; editing
(defvar vlfi-edit-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map text-mode-map)
@@ -388,16 +438,6 @@ successful. Return nil if nothing found."
"Editing: Type \\[vlfi-write] to write chunk \
or \\[vlfi-discard-edit] to discard changes.")))
(defun vlfi-write ()
"Write current chunk to file. May overwrite existing content."
(interactive)
(when (or (= (buffer-size) (- vlfi-end-pos vlfi-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 vlfi-start-pos)
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos)
(vlfi-mode)))
(defun vlfi-discard-edit ()
"Discard edit and refresh chunk from file."
(interactive)
@@ -405,6 +445,119 @@ End of chunk will be garbled. Continue? "))
(vlfi-mode)
(message "Switched to VLFI mode."))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; saving
(defun vlfi-write ()
"Write current chunk to file. Always return true to disable save.
If changing size of chunk shift remaining file content."
(interactive)
(when (and (derived-mode-p 'vlfi-mode)
(buffer-modified-p)
(or (verify-visited-file-modtime)
(y-or-n-p "File has changed since visited or saved. \
Save anyway? ")))
(let ((size-change (- vlfi-end-pos vlfi-start-pos
(length (encode-coding-region
(point-min) (point-max)
buffer-file-coding-system t))))
(pos (point)))
(cond ((zerop size-change)
(write-region nil nil buffer-file-name vlfi-start-pos t))
((< 0 size-change)
(vlfi-file-shift-back size-change))
(t (vlfi-file-shift-forward (- size-change))))
(goto-char pos))
(vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos)
(vlfi-mode)
t))
(defun vlfi-file-shift-back (size-change)
"Shift file contents SIZE-CHANGE bytes back."
(let ((coding-system buffer-file-coding-system))
(write-region nil nil buffer-file-name vlfi-start-pos t)
(setq buffer-file-coding-system nil)
(let ((read-start-pos vlfi-end-pos)
(reporter (make-progress-reporter "Adjusting file content"
vlfi-end-pos
vlfi-file-size)))
(while (vlfi-shift-batch read-start-pos (- read-start-pos
size-change))
(setq read-start-pos (+ read-start-pos vlfi-batch-size))
(progress-reporter-update reporter read-start-pos))
;; pad end with space
(erase-buffer)
(insert-char 32 size-change)
(write-region nil nil buffer-file-name (- vlfi-file-size
size-change) t)
(progress-reporter-done reporter))
(setq buffer-file-coding-system coding-system)))
(defun vlfi-shift-batch (read-pos write-pos)
"Read `vlfi-batch-size' bytes from READ-POS and write them \
back at WRITE-POS. Return nil if EOF is reached, t otherwise."
(erase-buffer)
(setq vlfi-file-size (nth 7 (file-attributes buffer-file-name)))
(let ((read-end (+ read-pos vlfi-batch-size)))
(insert-file-contents-literally buffer-file-name nil
read-pos
(min vlfi-file-size read-end))
(write-region nil nil buffer-file-name write-pos t)
(< read-end vlfi-file-size)))
(defun vlfi-file-shift-forward (size-change)
"Shift file contents SIZE-CHANGE bytes forward.
Done by saving content up front and then writing previous batch."
(let ((vlfi-buffer (current-buffer))
(temp-buffer (generate-new-buffer (concat " "
(buffer-name))))
(coding-system buffer-file-coding-system))
(let ((file buffer-file-name))
(set-buffer temp-buffer)
(setq buffer-file-name file))
(set-buffer vlfi-buffer)
(let ((read-buffer temp-buffer)
(write-buffer vlfi-buffer)
(size (+ vlfi-batch-size size-change))
(read-pos vlfi-end-pos)
(write-pos vlfi-start-pos)
swap-buffer
(reporter (make-progress-reporter "Adjusting file content"
vlfi-start-pos
vlfi-file-size)))
(while (vlfi-shift-batches size read-buffer read-pos
write-buffer write-pos)
(setq swap-buffer read-buffer
read-buffer write-buffer
write-buffer swap-buffer
write-pos (+ read-pos size-change)
read-pos (+ read-pos size))
(progress-reporter-update reporter write-pos))
(progress-reporter-done reporter))
(kill-buffer temp-buffer)
(set-buffer vlfi-buffer)
(setq buffer-file-coding-system coding-system)))
(defun vlfi-shift-batches (size read-buffer read-pos
write-buffer write-pos)
"Read SIZE bytes in READ-BUFFER starting from READ-POS.
Then write contents of WRITE-BUFFER to buffer file at WRITE-POS.
Return nil if EOF is reached, t otherwise."
(let* ((file-size (nth 7 (file-attributes buffer-file-name)))
(read-more (< read-pos file-size)))
(when read-more
;; read
(set-buffer read-buffer)
(erase-buffer)
(setq buffer-file-coding-system nil)
(insert-file-contents-literally buffer-file-name nil read-pos
(min file-size (+ read-pos
size))))
;; write
(set-buffer write-buffer)
(write-region nil nil buffer-file-name write-pos t)
read-more))
(provide 'vlfi)
;;; vlfi.el ends here