mirror of
				https://github.com/m00natic/vlfi.git
				synced 2025-11-04 00:51:37 +00:00 
			
		
		
		
	Initial attempt at optimizing chunk access.
This commit is contained in:
		
							
								
								
									
										198
									
								
								vlfi.el
									
									
									
									
									
								
							
							
						
						
									
										198
									
								
								vlfi.el
									
									
									
									
									
								
							@@ -47,7 +47,7 @@
 | 
				
			|||||||
;;; Keep track of file position.
 | 
					;;; Keep track of file position.
 | 
				
			||||||
(defvar vlfi-start-pos 0
 | 
					(defvar vlfi-start-pos 0
 | 
				
			||||||
  "Absolute position of the visible chunk start.")
 | 
					  "Absolute position of the visible chunk start.")
 | 
				
			||||||
(defvar vlfi-end-pos vlfi-batch-size
 | 
					(defvar vlfi-end-pos 0
 | 
				
			||||||
  "Absolute position of the visible chunk end.")
 | 
					  "Absolute position of the visible chunk end.")
 | 
				
			||||||
(defvar vlfi-file-size 0 "Total size of presented file.")
 | 
					(defvar vlfi-file-size 0 "Total size of presented file.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -97,10 +97,10 @@ buffer.  You can customize number of bytes displayed by customizing
 | 
				
			|||||||
`vlfi-batch-size'."
 | 
					`vlfi-batch-size'."
 | 
				
			||||||
  (interactive "fFile to open: ")
 | 
					  (interactive "fFile to open: ")
 | 
				
			||||||
  (with-current-buffer (generate-new-buffer "*vlfi*")
 | 
					  (with-current-buffer (generate-new-buffer "*vlfi*")
 | 
				
			||||||
 | 
					    (vlfi-mode)
 | 
				
			||||||
    (setq buffer-file-name file
 | 
					    (setq buffer-file-name file
 | 
				
			||||||
          vlfi-file-size (vlfi-get-file-size file))
 | 
					          vlfi-file-size (vlfi-get-file-size file))
 | 
				
			||||||
    (vlfi-insert-file)
 | 
					    (vlfi-insert-file)
 | 
				
			||||||
    (vlfi-mode)
 | 
					 | 
				
			||||||
    (switch-to-buffer (current-buffer))))
 | 
					    (switch-to-buffer (current-buffer))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
@@ -208,12 +208,13 @@ with the prefix argument DECREASE it is halved."
 | 
				
			|||||||
(defun vlfi-insert-file (&optional from-end)
 | 
					(defun vlfi-insert-file (&optional from-end)
 | 
				
			||||||
  "Insert first chunk of current file contents in current buffer.
 | 
					  "Insert first chunk of current file contents in current buffer.
 | 
				
			||||||
With FROM-END prefix, start from the back."
 | 
					With FROM-END prefix, start from the back."
 | 
				
			||||||
  (if from-end
 | 
					  (let ((start 0)
 | 
				
			||||||
      (setq vlfi-start-pos (max 0 (- vlfi-file-size vlfi-batch-size))
 | 
					        (end vlfi-batch-size))
 | 
				
			||||||
            vlfi-end-pos vlfi-file-size)
 | 
					    (if from-end
 | 
				
			||||||
    (setq vlfi-start-pos 0
 | 
					        (setq start (- vlfi-file-size vlfi-batch-size)
 | 
				
			||||||
          vlfi-end-pos (min vlfi-batch-size vlfi-file-size)))
 | 
					              end vlfi-file-size)
 | 
				
			||||||
  (vlfi-move-to-chunk vlfi-start-pos vlfi-end-pos))
 | 
					      (setq end (min vlfi-batch-size vlfi-file-size)))
 | 
				
			||||||
 | 
					    (vlfi-move-to-chunk start end)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun vlfi-beginning-of-file ()
 | 
					(defun vlfi-beginning-of-file ()
 | 
				
			||||||
  "Jump to beginning of file content."
 | 
					  "Jump to beginning of file content."
 | 
				
			||||||
@@ -251,26 +252,13 @@ When prefix argument is negative
 | 
				
			|||||||
  (interactive "p")
 | 
					  (interactive "p")
 | 
				
			||||||
  (or (verify-visited-file-modtime (current-buffer))
 | 
					  (or (verify-visited-file-modtime (current-buffer))
 | 
				
			||||||
      (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
					      (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
				
			||||||
  (let ((end (min (+ vlfi-end-pos (* vlfi-batch-size
 | 
					  (let* ((end (min (+ vlfi-end-pos (* vlfi-batch-size
 | 
				
			||||||
                                     (abs append)))
 | 
					                                      (abs append)))
 | 
				
			||||||
                  vlfi-file-size)))
 | 
					                   vlfi-file-size))
 | 
				
			||||||
    (let ((inhibit-read-only t)
 | 
					         (start (if (< append 0)
 | 
				
			||||||
          (do-append (< append 0))
 | 
					                    vlfi-start-pos
 | 
				
			||||||
          (pos (position-bytes (point))))
 | 
					                  (- end vlfi-batch-size))))
 | 
				
			||||||
      (if do-append
 | 
					    (vlfi-move-to-chunk start 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)
 | 
					 | 
				
			||||||
      (setq vlfi-end-pos end)
 | 
					 | 
				
			||||||
      (goto-char (or (byte-to-position (+ pos (vlfi-adjust-chunk)))
 | 
					 | 
				
			||||||
                     (point-max)))))
 | 
					 | 
				
			||||||
  (set-visited-file-modtime)
 | 
					 | 
				
			||||||
  (set-buffer-modified-p nil)
 | 
					 | 
				
			||||||
  (vlfi-update-buffer-name))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun vlfi-prev-batch (prepend)
 | 
					(defun vlfi-prev-batch (prepend)
 | 
				
			||||||
  "Display the previous batch of file data.
 | 
					  "Display the previous batch of file data.
 | 
				
			||||||
@@ -281,31 +269,12 @@ When prefix argument is negative
 | 
				
			|||||||
  (interactive "p")
 | 
					  (interactive "p")
 | 
				
			||||||
  (if (zerop vlfi-start-pos)
 | 
					  (if (zerop vlfi-start-pos)
 | 
				
			||||||
      (error "Already at BOF"))
 | 
					      (error "Already at BOF"))
 | 
				
			||||||
  (or (verify-visited-file-modtime (current-buffer))
 | 
					  (let* ((start (max 0 (- vlfi-start-pos (* vlfi-batch-size
 | 
				
			||||||
      (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
					                                            (abs prepend)))))
 | 
				
			||||||
  (let ((inhibit-read-only t)
 | 
					         (end (if (< prepend 0)
 | 
				
			||||||
        (start (max 0 (- vlfi-start-pos (* vlfi-batch-size
 | 
					                  vlfi-end-pos
 | 
				
			||||||
                                           (abs prepend)))))
 | 
					                (+ start vlfi-batch-size))))
 | 
				
			||||||
        (do-prepend (< prepend 0))
 | 
					    (vlfi-move-to-chunk start end)))
 | 
				
			||||||
        (pos (- (position-bytes (point-max))
 | 
					 | 
				
			||||||
                (position-bytes (point)))))
 | 
					 | 
				
			||||||
    (if do-prepend
 | 
					 | 
				
			||||||
        (goto-char (point-min))
 | 
					 | 
				
			||||||
      (setq vlfi-end-pos (min (+ start vlfi-batch-size)
 | 
					 | 
				
			||||||
                              vlfi-file-size))
 | 
					 | 
				
			||||||
      (erase-buffer))
 | 
					 | 
				
			||||||
    (insert-file-contents buffer-file-name nil start
 | 
					 | 
				
			||||||
                          (if do-prepend
 | 
					 | 
				
			||||||
                              vlfi-start-pos
 | 
					 | 
				
			||||||
                            vlfi-end-pos))
 | 
					 | 
				
			||||||
    (setq vlfi-start-pos start)
 | 
					 | 
				
			||||||
    (setq pos (+ pos (vlfi-adjust-chunk)))
 | 
					 | 
				
			||||||
    (goto-char (or (byte-to-position (- (position-bytes (point-max))
 | 
					 | 
				
			||||||
                                        pos))
 | 
					 | 
				
			||||||
                   (point-max))))
 | 
					 | 
				
			||||||
  (set-visited-file-modtime)
 | 
					 | 
				
			||||||
  (set-buffer-modified-p nil)
 | 
					 | 
				
			||||||
  (vlfi-update-buffer-name))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun vlfi-move-to-batch (start &optional minimal)
 | 
					(defun vlfi-move-to-batch (start &optional minimal)
 | 
				
			||||||
  "Move to batch determined by START.
 | 
					  "Move to batch determined by START.
 | 
				
			||||||
@@ -313,39 +282,92 @@ Adjust according to file start/end and show `vlfi-batch-size' bytes.
 | 
				
			|||||||
When given MINIMAL flag, skip non important operations."
 | 
					When given MINIMAL flag, skip non important operations."
 | 
				
			||||||
  (or (verify-visited-file-modtime (current-buffer))
 | 
					  (or (verify-visited-file-modtime (current-buffer))
 | 
				
			||||||
      (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
					      (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
				
			||||||
  (setq vlfi-start-pos (max 0 start)
 | 
					  (let ((start (max 0 start))
 | 
				
			||||||
        vlfi-end-pos (min (+ vlfi-start-pos vlfi-batch-size)
 | 
					        (end (min (+ vlfi-start-pos vlfi-batch-size)
 | 
				
			||||||
                          vlfi-file-size))
 | 
					                  vlfi-file-size)))
 | 
				
			||||||
  (if (= vlfi-file-size vlfi-end-pos)   ; re-check file size
 | 
					    (if (= vlfi-file-size end)          ; re-adjust start
 | 
				
			||||||
      (setq vlfi-start-pos (max 0 (- vlfi-end-pos vlfi-batch-size))))
 | 
					        (setq start (max 0 (- end vlfi-batch-size))))
 | 
				
			||||||
  (let ((inhibit-read-only t)
 | 
					    (vlfi-move-to-chunk start end)))
 | 
				
			||||||
        (pos (position-bytes (point))))
 | 
					 | 
				
			||||||
    (erase-buffer)
 | 
					 | 
				
			||||||
    (insert-file-contents buffer-file-name nil
 | 
					 | 
				
			||||||
                          vlfi-start-pos vlfi-end-pos)
 | 
					 | 
				
			||||||
    (goto-char (or (byte-to-position (+ pos (vlfi-adjust-chunk)))
 | 
					 | 
				
			||||||
                   (point-max))))
 | 
					 | 
				
			||||||
  (set-buffer-modified-p nil)
 | 
					 | 
				
			||||||
  (set-visited-file-modtime)
 | 
					 | 
				
			||||||
  (or minimal(vlfi-update-buffer-name)))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun vlfi-move-to-chunk (start end &optional minimal)
 | 
					(defun vlfi-move-to-chunk (start end &optional minimal)
 | 
				
			||||||
  "Move to chunk determined by START END.
 | 
					  "Move to chunk determined by START END.
 | 
				
			||||||
When given MINIMAL flag, skip non important operations."
 | 
					When given MINIMAL flag, skip non important operations."
 | 
				
			||||||
  (or (verify-visited-file-modtime (current-buffer))
 | 
					  (catch 'abort
 | 
				
			||||||
      (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
					    (let ((changed (not (verify-visited-file-modtime
 | 
				
			||||||
  (setq vlfi-start-pos (max 0 start)
 | 
					                         (current-buffer))))
 | 
				
			||||||
        vlfi-end-pos (min end vlfi-file-size))
 | 
					          (modified (buffer-modified-p))
 | 
				
			||||||
  (let ((inhibit-read-only t)
 | 
					          (start (max 0 start))
 | 
				
			||||||
        (pos (position-bytes (point))))
 | 
					          (end (min end vlfi-file-size)))
 | 
				
			||||||
    (erase-buffer)
 | 
					      (if changed
 | 
				
			||||||
    (insert-file-contents buffer-file-name nil
 | 
					          (setq vlfi-file-size (vlfi-get-file-size buffer-file-name)))
 | 
				
			||||||
                          vlfi-start-pos vlfi-end-pos)
 | 
					      (if (or changed
 | 
				
			||||||
    (goto-char (or (byte-to-position (+ pos (vlfi-adjust-chunk)))
 | 
					              (<= vlfi-end-pos start)
 | 
				
			||||||
                   (point-max))))
 | 
					              (<= end vlfi-start-pos))
 | 
				
			||||||
  (set-buffer-modified-p nil)
 | 
					          (progn                        ; full chunk renewal
 | 
				
			||||||
  (set-visited-file-modtime)
 | 
					            (if (and modified
 | 
				
			||||||
  (or minimal (vlfi-update-buffer-name)))
 | 
					                     (not (y-or-n-p
 | 
				
			||||||
 | 
					                           "Buffer modified, are you sure? ")))
 | 
				
			||||||
 | 
					                (throw 'abort nil))
 | 
				
			||||||
 | 
					            (setq vlfi-start-pos start
 | 
				
			||||||
 | 
					                  vlfi-end-pos end)
 | 
				
			||||||
 | 
					            (let ((inhibit-read-only t)
 | 
				
			||||||
 | 
					                  (pos (position-bytes (point))))
 | 
				
			||||||
 | 
					              (erase-buffer)
 | 
				
			||||||
 | 
					              (insert-file-contents buffer-file-name nil
 | 
				
			||||||
 | 
					                                    vlfi-start-pos vlfi-end-pos)
 | 
				
			||||||
 | 
					              (goto-char (or (byte-to-position
 | 
				
			||||||
 | 
					                              (+ pos (vlfi-adjust-chunk)))
 | 
				
			||||||
 | 
					                             (point-max))))
 | 
				
			||||||
 | 
					            (set-buffer-modified-p nil))
 | 
				
			||||||
 | 
					        (if (and modified
 | 
				
			||||||
 | 
					                 (or (< end vlfi-end-pos)
 | 
				
			||||||
 | 
					                     (< 3 (- start vlfi-start-pos)))
 | 
				
			||||||
 | 
					                 (not (y-or-n-p "Buffer modified, are you sure? ")))
 | 
				
			||||||
 | 
					            (throw 'abort nil))
 | 
				
			||||||
 | 
					        (let ((pos (+ (position-bytes (point))
 | 
				
			||||||
 | 
					                      vlfi-start-pos))
 | 
				
			||||||
 | 
					              (adjust-encoding (or (< vlfi-end-pos end)
 | 
				
			||||||
 | 
					                                   (< start vlfi-start-pos))))
 | 
				
			||||||
 | 
					          (if adjust-encoding
 | 
				
			||||||
 | 
					              (let ((inhibit-read-only t))
 | 
				
			||||||
 | 
					                (encode-coding-region (point-min) (point-max)
 | 
				
			||||||
 | 
					                                      buffer-file-coding-system)))
 | 
				
			||||||
 | 
					          (cond ((< end vlfi-end-pos)       ; adjust ends
 | 
				
			||||||
 | 
					                 (let ((inhibit-read-only t))
 | 
				
			||||||
 | 
					                   (delete-region (1+ (- end vlfi-start-pos))
 | 
				
			||||||
 | 
					                                  (point-max))))
 | 
				
			||||||
 | 
					                ((< vlfi-end-pos end)
 | 
				
			||||||
 | 
					                 (goto-char (point-max))
 | 
				
			||||||
 | 
					                 (let ((inhibit-read-only t))
 | 
				
			||||||
 | 
					                   (insert-file-contents-literally
 | 
				
			||||||
 | 
					                    buffer-file-name nil vlfi-end-pos end))))
 | 
				
			||||||
 | 
					          (cond ((< start vlfi-start-pos)   ; adjust start
 | 
				
			||||||
 | 
					                 (goto-char (point-min))
 | 
				
			||||||
 | 
					                 (let ((inhibit-read-only t))
 | 
				
			||||||
 | 
					                   (insert-file-contents-literally
 | 
				
			||||||
 | 
					                    buffer-file-name nil start (1- vlfi-start-pos)))
 | 
				
			||||||
 | 
					                 (setq vlfi-start-pos start))
 | 
				
			||||||
 | 
					                ((< 3 (- start vlfi-start-pos))
 | 
				
			||||||
 | 
					                 (let ((inhibit-read-only t))
 | 
				
			||||||
 | 
					                   (delete-region (point-min)
 | 
				
			||||||
 | 
					                                  (byte-to-position
 | 
				
			||||||
 | 
					                                   (- start vlfi-start-pos 1))))
 | 
				
			||||||
 | 
					                 (setq vlfi-start-pos start)))
 | 
				
			||||||
 | 
					          (if adjust-encoding
 | 
				
			||||||
 | 
					              (let ((inhibit-read-only t))
 | 
				
			||||||
 | 
					                (decode-coding-region (point-min) (point-max)
 | 
				
			||||||
 | 
					                                      buffer-file-coding-system)))
 | 
				
			||||||
 | 
					          (or modified
 | 
				
			||||||
 | 
					              (set-buffer-modified-p nil))
 | 
				
			||||||
 | 
					          (setq vlfi-end-pos end)
 | 
				
			||||||
 | 
					          (goto-char
 | 
				
			||||||
 | 
					           (cond ((< pos vlfi-start-pos) (point-min))
 | 
				
			||||||
 | 
					                 ((< vlfi-end-pos pos) (point-max))
 | 
				
			||||||
 | 
					                 (t (or (byte-to-position (- pos vlfi-start-pos))
 | 
				
			||||||
 | 
					                        (point-min)))))))
 | 
				
			||||||
 | 
					      (if changed
 | 
				
			||||||
 | 
					          (set-visited-file-modtime)))
 | 
				
			||||||
 | 
					    (or minimal (vlfi-update-buffer-name))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun vlfi-adjust-chunk ()
 | 
					(defun vlfi-adjust-chunk ()
 | 
				
			||||||
  "Adjust chunk beginning until content can be properly decoded.
 | 
					  "Adjust chunk beginning until content can be properly decoded.
 | 
				
			||||||
@@ -590,16 +612,8 @@ EVENT may hold details of the invocation."
 | 
				
			|||||||
                (setq buffer (vlfi file))
 | 
					                (setq buffer (vlfi file))
 | 
				
			||||||
                (switch-to-buffer occur-buffer)))
 | 
					                (switch-to-buffer occur-buffer)))
 | 
				
			||||||
          (pop-to-buffer buffer)
 | 
					          (pop-to-buffer buffer)
 | 
				
			||||||
          (if (buffer-modified-p)
 | 
					          (vlfi-move-to-chunk chunk-start chunk-end)
 | 
				
			||||||
              (cond ((and (= vlfi-start-pos chunk-start)
 | 
					          (goto-char match-pos)))))
 | 
				
			||||||
                          (= vlfi-end-pos chunk-end))
 | 
					 | 
				
			||||||
                     (goto-char match-pos))
 | 
					 | 
				
			||||||
                    ((y-or-n-p "VLFI buffer has been modified.  \
 | 
					 | 
				
			||||||
Really jump to new chunk? ")
 | 
					 | 
				
			||||||
                     (vlfi-move-to-chunk chunk-start chunk-end)
 | 
					 | 
				
			||||||
                     (goto-char match-pos)))
 | 
					 | 
				
			||||||
            (vlfi-move-to-chunk chunk-start chunk-end)
 | 
					 | 
				
			||||||
            (goto-char match-pos))))))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun vlfi-occur (regexp)
 | 
					(defun vlfi-occur (regexp)
 | 
				
			||||||
  "Make whole file occur style index for REGEXP.
 | 
					  "Make whole file occur style index for REGEXP.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user