1
0
mirror of https://github.com/m00natic/vlfi.git synced 2024-10-05 18:30:51 +01:00

Simplify detection of buffer size change.

This commit is contained in:
Andrey Kotlarski 2013-04-13 01:29:12 +03:00
parent 8787186619
commit 8f6299c6c2

342
vlfi.el
View File

@ -50,7 +50,6 @@
(defvar vlfi-end-pos vlfi-batch-size
"Absolute position of the visible chunk end.")
(defvar vlfi-file-size 0 "Total size of presented file.")
(defvar vlfi-buffer-size 0 "Buffer size of current chunk.")
(defvar vlfi-mode-map
(let ((map (make-sparse-keymap)))
@ -59,8 +58,8 @@
(define-key map "+" 'vlfi-change-batch-size)
(define-key map "-"
(lambda () "Decrease vlfi batch size by factor of 2."
(interactive)
(vlfi-change-batch-size t)))
(interactive)
(vlfi-change-batch-size t)))
(define-key map "s" 'vlfi-re-search-forward)
(define-key map "r" 'vlfi-re-search-backward)
(define-key map "[" 'vlfi-beginning-of-file)
@ -83,9 +82,7 @@
(make-local-variable 'vlfi-end-pos)
(put 'vlfi-end-pos 'permanent-local t)
(make-local-variable 'vlfi-file-size)
(put 'vlfi-file-size 'permanent-local t)
(make-local-variable 'vlfi-buffer-size)
(put 'vlfi-buffer-size 'permanent-local t))
(put 'vlfi-file-size 'permanent-local t))
(defun vlfi-change-batch-size (decrease)
"Change the buffer-local value of `vlfi-batch-size'.
@ -95,18 +92,18 @@ with the prefix argument DECREASE it is halved."
(or (assq 'vlfi-batch-size (buffer-local-variables))
(error "%s is not local in this buffer" 'vlfi-batch-size))
(setq vlfi-batch-size (if decrease
(/ vlfi-batch-size 2)
(* vlfi-batch-size 2)))
(/ vlfi-batch-size 2)
(* vlfi-batch-size 2)))
(vlfi-move-to-batch vlfi-start-pos))
(defun vlfi-format-buffer-name ()
"Return format for vlfi buffer name."
(format "%s(%s)[%d/%d](%d)"
(file-name-nondirectory buffer-file-name)
(file-size-human-readable vlfi-file-size)
(/ vlfi-end-pos vlfi-batch-size)
(/ vlfi-file-size vlfi-batch-size)
vlfi-batch-size))
(file-name-nondirectory buffer-file-name)
(file-size-human-readable 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."
@ -120,28 +117,26 @@ When prefix argument is negative
append next APPEND number of batches to the existing buffer."
(interactive "p")
(let ((end (+ vlfi-end-pos (* vlfi-batch-size
(abs append)))))
(abs append)))))
(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"))
((< vlfi-file-size end)
(setq end vlfi-file-size))))
(error "Already at EOF"))
((< vlfi-file-size end)
(setq end vlfi-file-size))))
(let ((inhibit-read-only t)
(do-append (< append 0))
(pos (point)))
(do-append (< append 0))
(pos (point)))
(if do-append
(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)
end)
(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)
end)
(goto-char pos))
(setq vlfi-end-pos end
vlfi-buffer-size (buffer-size)))
(setq vlfi-end-pos end))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
@ -156,21 +151,20 @@ When prefix argument is negative
(if (zerop vlfi-start-pos)
(error "Already at BOF"))
(let ((inhibit-read-only t)
(start (max 0 (- vlfi-start-pos (* vlfi-batch-size
(abs prepend)))))
(do-prepend (< prepend 0))
(pos (- (point-max) (point))))
(start (max 0 (- vlfi-start-pos (* vlfi-batch-size
(abs prepend)))))
(do-prepend (< prepend 0))
(pos (- (point-max) (point))))
(if do-prepend
(goto-char (point-min))
(goto-char (point-min))
(setq vlfi-end-pos (+ start vlfi-batch-size))
(erase-buffer))
(insert-file-contents buffer-file-name nil start
(if do-prepend
vlfi-start-pos
vlfi-end-pos))
(if do-prepend
vlfi-start-pos
vlfi-end-pos))
(goto-char (- (point-max) pos))
(setq vlfi-start-pos start
vlfi-buffer-size (buffer-size)))
(setq vlfi-start-pos start))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
@ -179,19 +173,18 @@ When prefix argument is negative
"Move to batch determined by START.
Adjust according to file start/end and show `vlfi-batch-size' bytes."
(setq vlfi-start-pos (max 0 start)
vlfi-end-pos (+ vlfi-start-pos vlfi-batch-size))
vlfi-end-pos (+ vlfi-start-pos vlfi-batch-size))
(if (< vlfi-file-size vlfi-end-pos) ; re-check file size
(setq vlfi-file-size
(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))))
(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)
(pos (point)))
(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))
(setq vlfi-buffer-size (buffer-size))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
@ -200,16 +193,15 @@ Adjust according to file start/end and show `vlfi-batch-size' bytes."
"Move to chunk determined by START END."
(if (< vlfi-file-size end) ; re-check file size
(setq vlfi-file-size (nth 7
(file-attributes buffer-file-name))))
(file-attributes buffer-file-name))))
(setq vlfi-start-pos (max 0 start)
vlfi-end-pos (min end vlfi-file-size))
vlfi-end-pos (min end vlfi-file-size))
(let ((inhibit-read-only t)
(pos (point)))
(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))
(setq vlfi-buffer-size (buffer-size))
(set-visited-file-modtime)
(set-buffer-modified-p nil)
(vlfi-update-buffer-name))
@ -219,9 +211,9 @@ Adjust according to file start/end and show `vlfi-batch-size' bytes."
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)
vlfi-end-pos vlfi-file-size)
(setq vlfi-start-pos 0
vlfi-end-pos (min vlfi-batch-size vlfi-file-size)))
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 ()
@ -243,7 +235,7 @@ buffer. You can customize number of bytes displayed by customizing
(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-file-size (nth 7 (file-attributes file)))
(vlfi-insert-file from-end)
(vlfi-mode)
(switch-to-buffer (current-buffer))))
@ -272,25 +264,25 @@ 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): \
(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"))))))
(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
@ -300,66 +292,66 @@ OP-TYPE specifies the file operation being performed over FILENAME."
(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)))
(match-end-pos match-start-pos)
(to-find count)
(search-reporter (make-progress-reporter
(concat "Searching for " regexp)
(if backward
(- vlfi-file-size vlfi-end-pos)
vlfi-start-pos)
vlfi-file-size))
(batch-step (/ vlfi-batch-size 8))) ; amount of chunk overlap
(match-end-pos match-start-pos)
(to-find count)
(search-reporter (make-progress-reporter
(concat "Searching for " regexp)
(if backward
(- vlfi-file-size vlfi-end-pos)
vlfi-start-pos)
vlfi-file-size))
(batch-step (/ vlfi-batch-size 8))) ; amount of chunk overlap
(unwind-protect
(catch 'end-of-file
(if backward
(while (not (zerop to-find))
(cond ((re-search-backward regexp nil t)
(setq to-find (1- to-find)
match-start-pos (+ vlfi-start-pos
(match-beginning 0))
match-end-pos (+ vlfi-start-pos
(match-end 0))))
((zerop vlfi-start-pos)
(throw 'end-of-file nil))
(t (let ((batch-move (- vlfi-start-pos
(- vlfi-batch-size
batch-step))))
(vlfi-move-to-batch
(if (< match-start-pos batch-move)
(- match-start-pos vlfi-batch-size)
batch-move)))
(goto-char (if (< match-start-pos
vlfi-end-pos)
(- match-start-pos
vlfi-start-pos)
(point-max)))
(progress-reporter-update search-reporter
vlfi-start-pos))))
(while (not (zerop to-find))
(cond ((re-search-forward regexp nil t)
(setq to-find (1- to-find)
match-start-pos (+ vlfi-start-pos
(match-beginning 0))
match-end-pos (+ vlfi-start-pos
(match-end 0))))
((= vlfi-end-pos vlfi-file-size)
(throw 'end-of-file nil))
(t (let ((batch-move (- vlfi-end-pos batch-step)))
(vlfi-move-to-batch
(if (< batch-move match-end-pos)
match-end-pos
batch-move)))
(goto-char (if (< vlfi-start-pos match-end-pos)
(- match-end-pos vlfi-start-pos)
(point-min)))
(progress-reporter-update search-reporter
vlfi-end-pos)))))
(progress-reporter-done search-reporter))
(catch 'end-of-file
(if backward
(while (not (zerop to-find))
(cond ((re-search-backward regexp nil t)
(setq to-find (1- to-find)
match-start-pos (+ vlfi-start-pos
(match-beginning 0))
match-end-pos (+ vlfi-start-pos
(match-end 0))))
((zerop vlfi-start-pos)
(throw 'end-of-file nil))
(t (let ((batch-move (- vlfi-start-pos
(- vlfi-batch-size
batch-step))))
(vlfi-move-to-batch
(if (< match-start-pos batch-move)
(- match-start-pos vlfi-batch-size)
batch-move)))
(goto-char (if (< match-start-pos
vlfi-end-pos)
(- match-start-pos
vlfi-start-pos)
(point-max)))
(progress-reporter-update search-reporter
vlfi-start-pos))))
(while (not (zerop to-find))
(cond ((re-search-forward regexp nil t)
(setq to-find (1- to-find)
match-start-pos (+ vlfi-start-pos
(match-beginning 0))
match-end-pos (+ vlfi-start-pos
(match-end 0))))
((= vlfi-end-pos vlfi-file-size)
(throw 'end-of-file nil))
(t (let ((batch-move (- vlfi-end-pos batch-step)))
(vlfi-move-to-batch
(if (< batch-move match-end-pos)
match-end-pos
batch-move)))
(goto-char (if (< vlfi-start-pos match-end-pos)
(- match-end-pos vlfi-start-pos)
(point-min)))
(progress-reporter-update search-reporter
vlfi-end-pos)))))
(progress-reporter-done search-reporter))
(if backward
(vlfi-goto-match match-end-pos match-start-pos
count to-find)
(vlfi-goto-match match-start-pos match-end-pos
count to-find)))))
(vlfi-goto-match match-end-pos match-start-pos
count to-find)
(vlfi-goto-match match-start-pos match-end-pos
count to-find)))))
(defun vlfi-goto-match (match-pos-start match-pos-end count to-find)
"Move to chunk surrounding MATCH-POS-START and MATCH-POS-END.
@ -367,41 +359,41 @@ According to COUNT and left TO-FIND, show if search has been
successful. Return nil if nothing found."
(let ((success (zerop to-find)))
(or success
(vlfi-move-to-batch (- match-pos-start
(/ vlfi-batch-size 2))))
(vlfi-move-to-batch (- match-pos-start
(/ vlfi-batch-size 2))))
(let* ((match-end (- match-pos-end vlfi-start-pos))
(overlay (make-overlay (- match-pos-start vlfi-start-pos)
match-end)))
(overlay (make-overlay (- match-pos-start vlfi-start-pos)
match-end)))
(overlay-put overlay 'face 'region)
(or success (goto-char match-end))
(prog1 (cond (success t)
((< to-find count)
(message "Moved to the %d match which is last"
(- count to-find))
t)
(t (message "Not found")
nil))
(sit-for 0.1)
(delete-overlay overlay)))))
((< to-find count)
(message "Moved to the %d match which is last"
(- count to-find))
t)
(t (message "Not found")
nil))
(sit-for 0.1)
(delete-overlay overlay)))))
(defun vlfi-re-search-forward (regexp count)
"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))
'regexp-history)
(or current-prefix-arg 1)))
(if regexp-history
(car regexp-history))
'regexp-history)
(or current-prefix-arg 1)))
(vlfi-re-search regexp count nil))
(defun vlfi-re-search-backward (regexp count)
"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))
'regexp-history)
(or current-prefix-arg 1)))
(if regexp-history
(car regexp-history))
'regexp-history)
(or current-prefix-arg 1)))
(vlfi-re-search regexp count t))
;;; editing
@ -418,7 +410,7 @@ Search is performed chunk by chunk in `vlfi-batch-size' memory."
(setq buffer-read-only nil)
(buffer-enable-undo)
(message (substitute-command-keys
"Editing: Type \\[vlfi-write] to write chunk \
"Editing: Type \\[vlfi-write] to write chunk \
or \\[vlfi-discard-edit] to discard changes.")))
(defun vlfi-write-1 ()
@ -433,31 +425,35 @@ Reopen last viewed chunk."
If changing size of chunk, may load the remaining part of file first."
(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 \
(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-buffer-size (buffer-size))))
(let ((size-change (- vlfi-end-pos vlfi-start-pos
(length
(encode-coding-region
(point-min) (point-max)
buffer-file-coding-system t)))))
(if (zerop size-change)
(vlfi-write-1)
(setq vlfi-file-size (nth 7
(file-attributes buffer-file-name)))
(cond ((= vlfi-file-size vlfi-end-pos)
(vlfi-write-1))
((y-or-n-p (concat "Changed size of original chunk. \
(vlfi-write-1)
(setq vlfi-file-size (nth 7
(file-attributes buffer-file-name)))
(cond ((= vlfi-file-size vlfi-end-pos)
(vlfi-write-1))
((y-or-n-p (concat "Changed size of original chunk. \
Remaining part of the file ["
(file-size-human-readable
(- vlfi-file-size vlfi-end-pos))
"] has to be loaded. Continue? "))
(let ((pos (point)))
(goto-char (point-max))
(insert-file-contents buffer-file-name nil
vlfi-end-pos vlfi-file-size)
(when (< 0 size-change) ; pad with empty characters
(goto-char (point-max))
(insert-char 32 size-change))
(vlfi-write-1)
(goto-char pos))))))
(file-size-human-readable
(- vlfi-file-size vlfi-end-pos))
"] has to be loaded. Continue? "))
(let ((pos (point)))
(goto-char (point-max))
(insert-file-contents buffer-file-name nil
vlfi-end-pos vlfi-file-size)
(when (< 0 size-change) ; pad with empty characters
(goto-char (point-max))
(insert-char 32 size-change))
(vlfi-write-1)
(goto-char pos))))))
t))
(defun vlfi-discard-edit ()