mirror of
https://github.com/m00natic/vlfi.git
synced 2025-02-23 21:38:05 +00:00
Turn vlf into minor mode.
This commit is contained in:
parent
7a141091c8
commit
efae918a83
151
vlf.el
151
vlf.el
@ -61,9 +61,10 @@
|
||||
(put 'vlf-file-size 'permanent-local 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)
|
||||
(let ((map (make-sparse-keymap))
|
||||
(map-prefix (make-sparse-keymap)))
|
||||
(define-key map [next] 'vlf-next-batch)
|
||||
(define-key map [prior] 'vlf-prev-batch)
|
||||
(define-key map "+" 'vlf-change-batch-size)
|
||||
(define-key map "-"
|
||||
(lambda () "Decrease vlf batch size by factor of 2."
|
||||
@ -74,37 +75,56 @@
|
||||
(define-key map "o" 'vlf-occur)
|
||||
(define-key map "[" 'vlf-beginning-of-file)
|
||||
(define-key map "]" 'vlf-end-of-file)
|
||||
(define-key map "e" 'vlf-edit-mode)
|
||||
(define-key map "j" 'vlf-jump-to-chunk)
|
||||
(define-key map "l" 'vlf-goto-line)
|
||||
map)
|
||||
(define-key map "q" 'vlf-discard-edit)
|
||||
(define-key map-prefix "\C-c\C-v" map)
|
||||
map-prefix)
|
||||
"Keymap for `vlf-mode'.")
|
||||
|
||||
(define-derived-mode vlf-mode special-mode "VLF"
|
||||
(define-minor-mode vlf-mode
|
||||
"Mode to browse large files in."
|
||||
(setq buffer-read-only t)
|
||||
(set-buffer-modified-p nil)
|
||||
(buffer-disable-undo)
|
||||
:lighter " VLF"
|
||||
:group 'vlf-mode
|
||||
:keymap vlf-mode-map
|
||||
(if vlf-mode
|
||||
(progn
|
||||
(add-hook 'write-file-functions 'vlf-write nil t)
|
||||
(make-local-variable 'revert-buffer-function)
|
||||
(setq revert-buffer-function 'vlf-revert)
|
||||
(set (make-local-variable 'revert-buffer-function)
|
||||
'vlf-revert)
|
||||
(make-local-variable 'vlf-batch-size)
|
||||
(make-local-variable 'vlf-start-pos)
|
||||
(set (make-local-variable 'vlf-start-pos) -1)
|
||||
(make-local-variable 'vlf-end-pos)
|
||||
(make-local-variable 'vlf-file-size))
|
||||
(set (make-local-variable 'vlf-file-size)
|
||||
(vlf-get-file-size buffer-file-name))
|
||||
(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)))
|
||||
(when (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))))
|
||||
(remove-hook 'write-file-functions 'vlf-write t)
|
||||
(kill-local-variable 'revert-buffer-function)
|
||||
(let ((pos (+ vlf-start-pos (position-bytes (point)))))
|
||||
(erase-buffer)
|
||||
(insert-file-contents buffer-file-name)
|
||||
(set-buffer-modified-p nil)
|
||||
(goto-char (byte-to-position pos)))
|
||||
(rename-buffer (file-name-nondirectory buffer-file-name) t))))
|
||||
|
||||
;;;###autoload
|
||||
(defun vlf (file)
|
||||
"View Large FILE.
|
||||
Batches of the file data from FILE will be displayed in a read-only
|
||||
buffer. You can customize number of bytes displayed by customizing
|
||||
"View Large FILE in batches.
|
||||
You can customize number of bytes displayed by customizing
|
||||
`vlf-batch-size'."
|
||||
(interactive "fFile to open: ")
|
||||
(with-current-buffer (generate-new-buffer "*vlf*")
|
||||
(set-visited-file-name file)
|
||||
(vlf-mode)
|
||||
(setq vlf-file-size (vlf-get-file-size buffer-file-name))
|
||||
(vlf-insert-file)
|
||||
(set-buffer-modified-p nil)
|
||||
(vlf-mode 1)
|
||||
(switch-to-buffer (current-buffer))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
@ -156,8 +176,7 @@ OP-TYPE specifies the file operation being performed over FILENAME."
|
||||
(defadvice scroll-up (around vlf-scroll-up
|
||||
activate compile)
|
||||
"Slide to next batch if at end of buffer in `vlf-mode'."
|
||||
(if (and (derived-mode-p 'vlf-mode)
|
||||
(eobp))
|
||||
(if (and vlf-mode (eobp))
|
||||
(progn (vlf-next-batch 1)
|
||||
(goto-char (point-min)))
|
||||
ad-do-it))
|
||||
@ -165,8 +184,7 @@ OP-TYPE specifies the file operation being performed over FILENAME."
|
||||
(defadvice scroll-down (around vlf-scroll-down
|
||||
activate compile)
|
||||
"Slide to previous batch if at beginning of buffer in `vlf-mode'."
|
||||
(if (and (derived-mode-p 'vlf-mode)
|
||||
(bobp))
|
||||
(if (and vlf-mode (bobp))
|
||||
(progn (vlf-prev-batch 1)
|
||||
(goto-char (point-max)))
|
||||
ad-do-it))
|
||||
@ -193,16 +211,20 @@ with the prefix argument DECREASE it is halved."
|
||||
|
||||
(defun vlf-format-buffer-name ()
|
||||
"Return format for vlf buffer name."
|
||||
(format "%s(%s)[%d/%d](%d)"
|
||||
(format "%s[%d/%d]"
|
||||
(file-name-nondirectory buffer-file-name)
|
||||
(file-size-human-readable vlf-file-size)
|
||||
(/ vlf-end-pos vlf-batch-size)
|
||||
(/ vlf-file-size vlf-batch-size)
|
||||
vlf-batch-size))
|
||||
(/ vlf-file-size vlf-batch-size)))
|
||||
|
||||
(defun vlf-update-buffer-name ()
|
||||
"Update the current buffer name."
|
||||
(rename-buffer (vlf-format-buffer-name) t))
|
||||
"Update the current buffer name and modeline."
|
||||
(rename-buffer (vlf-format-buffer-name) t)
|
||||
(setq minor-mode-alist
|
||||
(mapcar (lambda (x)
|
||||
(if (eq 'vlf-mode (car x))
|
||||
`(vlf-mode ,(format " VLF[%d]" vlf-batch-size))
|
||||
x))
|
||||
minor-mode-alist)))
|
||||
|
||||
(defun vlf-get-file-size (file)
|
||||
"Get size in bytes of FILE."
|
||||
@ -248,6 +270,13 @@ Ask for confirmation if NOCONFIRM is nil."
|
||||
(interactive "nGoto to chunk: ")
|
||||
(vlf-move-to-batch (* (1- n) vlf-batch-size)))
|
||||
|
||||
(defmacro vlf-with-undo-disabled (&rest body)
|
||||
"Execute BODY with temporarily disabled undo."
|
||||
(declare (indent defun))
|
||||
`(progn (buffer-disable-undo)
|
||||
(unwind-protect (progn ,@body)
|
||||
(buffer-enable-undo))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; batch movement
|
||||
|
||||
@ -313,6 +342,7 @@ If same as current chunk is requested, do nothing."
|
||||
(defun vlf-move-to-chunk-1 (start end)
|
||||
"Move to chunk determined by START END keeping as much edits if any.
|
||||
Return t if move hasn't been canceled."
|
||||
(vlf-with-undo-disabled
|
||||
(let ((modified (buffer-modified-p))
|
||||
(start (max 0 start))
|
||||
(end (min end vlf-file-size))
|
||||
@ -368,10 +398,11 @@ Return t if move hasn't been canceled."
|
||||
(goto-char (or (byte-to-position (- pos vlf-start-pos))
|
||||
(point-max))))
|
||||
(set-buffer-modified-p modified)
|
||||
t))))
|
||||
t)))))
|
||||
|
||||
(defun vlf-move-to-chunk-2 (start end)
|
||||
"Unconditionally move to chunk determined by START END."
|
||||
(vlf-with-undo-disabled
|
||||
(setq vlf-start-pos (max 0 start)
|
||||
vlf-end-pos (min end vlf-file-size))
|
||||
(let ((inhibit-read-only t)
|
||||
@ -386,7 +417,7 @@ Return t if move hasn't been canceled."
|
||||
(goto-char (or (byte-to-position (+ pos (car shifts)))
|
||||
(point-max)))))
|
||||
(set-buffer-modified-p nil)
|
||||
(set-visited-file-modtime))
|
||||
(set-visited-file-modtime)))
|
||||
|
||||
(defun vlf-adjust-chunk (start end &optional adjust-start adjust-end
|
||||
position)
|
||||
@ -453,7 +484,7 @@ BATCH-STEP is amount of overlap between successive chunks."
|
||||
vlf-start-pos)
|
||||
vlf-file-size)))
|
||||
(set-buffer-modified-p nil)
|
||||
(buffer-disable-undo)
|
||||
(vlf-with-undo-disabled
|
||||
(unwind-protect
|
||||
(catch 'end-of-file
|
||||
(if backward
|
||||
@ -521,7 +552,7 @@ BATCH-STEP is amount of overlap between successive chunks."
|
||||
count to-find)
|
||||
(vlf-goto-match match-chunk-start match-chunk-end
|
||||
match-start-pos match-end-pos
|
||||
count to-find)))))
|
||||
count to-find))))))
|
||||
|
||||
(defun vlf-goto-match (match-chunk-start match-chunk-end
|
||||
match-pos-start
|
||||
@ -682,11 +713,11 @@ Prematurely ending indexing will still show what's found so far."
|
||||
(vlf-beginning-of-file)
|
||||
(goto-char (point-min))
|
||||
(set-buffer-modified-p nil)
|
||||
(buffer-disable-undo)
|
||||
(vlf-with-undo-disabled
|
||||
(unwind-protect (vlf-build-occur regexp)
|
||||
(set-buffer-modified-p nil)
|
||||
(vlf-move-to-chunk start-pos end-pos)
|
||||
(goto-char pos))))
|
||||
(goto-char pos)))))
|
||||
|
||||
(defun vlf-build-occur (regexp)
|
||||
"Build occur style index for REGEXP."
|
||||
@ -792,30 +823,11 @@ in file: %s" total-matches line regexp file)
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; 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)
|
||||
(define-key map "\C-v" vlf-mode-map)
|
||||
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-discard-edit ()
|
||||
"Discard edit and refresh chunk from file."
|
||||
(interactive)
|
||||
(set-buffer-modified-p nil)
|
||||
(vlf-move-to-chunk vlf-start-pos vlf-end-pos)
|
||||
(vlf-mode)
|
||||
(message "Switched to VLF mode."))
|
||||
(vlf-move-to-chunk vlf-start-pos vlf-end-pos))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; saving
|
||||
@ -839,14 +851,13 @@ Save anyway? ")))
|
||||
(vlf-file-shift-back size-change))
|
||||
(t (vlf-file-shift-forward (- size-change))))
|
||||
(vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)
|
||||
(goto-char pos))
|
||||
(vlf-mode))
|
||||
(goto-char pos)))
|
||||
t)
|
||||
|
||||
(defun vlf-file-shift-back (size-change)
|
||||
"Shift file contents SIZE-CHANGE bytes back."
|
||||
(write-region nil nil buffer-file-name vlf-start-pos t)
|
||||
(buffer-disable-undo)
|
||||
(vlf-with-undo-disabled
|
||||
(let ((read-start-pos vlf-end-pos)
|
||||
(coding-system-for-write 'no-conversion)
|
||||
(reporter (make-progress-reporter "Adjusting file content..."
|
||||
@ -862,7 +873,7 @@ Save anyway? ")))
|
||||
(insert-char 32 size-change)
|
||||
(write-region nil nil buffer-file-name (- vlf-file-size
|
||||
size-change) t)
|
||||
(progress-reporter-done reporter)))
|
||||
(progress-reporter-done reporter))))
|
||||
|
||||
(defun vlf-shift-batch (read-pos write-pos)
|
||||
"Read `vlf-batch-size' bytes from READ-POS and write them \
|
||||
@ -879,26 +890,26 @@ back at WRITE-POS. Return nil if EOF is reached, t otherwise."
|
||||
(defun vlf-file-shift-forward (size-change)
|
||||
"Shift file contents SIZE-CHANGE bytes forward.
|
||||
Done by saving content up front and then writing previous batch."
|
||||
(buffer-disable-undo)
|
||||
(let ((size (+ vlf-batch-size size-change))
|
||||
(vlf-with-undo-disabled
|
||||
(let ((read-size (max (/ vlf-batch-size 2) size-change))
|
||||
(read-pos vlf-end-pos)
|
||||
(write-pos vlf-start-pos)
|
||||
(reporter (make-progress-reporter "Adjusting file content..."
|
||||
vlf-start-pos
|
||||
vlf-file-size)))
|
||||
(when (vlf-shift-batches size read-pos write-pos t)
|
||||
(when (vlf-shift-batches read-size read-pos write-pos t)
|
||||
(setq write-pos (+ read-pos size-change)
|
||||
read-pos (+ read-pos size))
|
||||
read-pos (+ read-pos read-size))
|
||||
(progress-reporter-update reporter write-pos)
|
||||
(let ((coding-system-for-write 'no-conversion))
|
||||
(while (vlf-shift-batches size read-pos write-pos nil)
|
||||
(while (vlf-shift-batches read-size read-pos write-pos nil)
|
||||
(setq write-pos (+ read-pos size-change)
|
||||
read-pos (+ read-pos size))
|
||||
read-pos (+ read-pos read-size))
|
||||
(progress-reporter-update reporter write-pos))))
|
||||
(progress-reporter-done reporter)))
|
||||
(progress-reporter-done reporter))))
|
||||
|
||||
(defun vlf-shift-batches (size read-pos write-pos hide-read)
|
||||
"Append SIZE bytes of file starting at READ-POS.
|
||||
(defun vlf-shift-batches (read-size read-pos write-pos hide-read)
|
||||
"Append READ-SIZE bytes of file starting at READ-POS.
|
||||
Then write initial buffer content to file at WRITE-POS.
|
||||
If HIDE-READ is non nil, temporarily hide literal read content.
|
||||
Return nil if EOF is reached, t otherwise."
|
||||
@ -909,8 +920,8 @@ Return nil if EOF is reached, t otherwise."
|
||||
(when read-more
|
||||
(goto-char end-write-pos)
|
||||
(insert-file-contents-literally buffer-file-name nil read-pos
|
||||
(min vlf-file-size (+ read-pos
|
||||
size))))
|
||||
(min vlf-file-size
|
||||
(+ read-pos read-size))))
|
||||
;; write
|
||||
(if hide-read ; hide literal region if user has to choose encoding
|
||||
(narrow-to-region start-write-pos end-write-pos))
|
||||
|
Loading…
x
Reference in New Issue
Block a user