]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/vlf/vlf.el
[vlf] bump version
[gnu-emacs-elpa] / packages / vlf / vlf.el
index 18f274bf4d2a9cba0098c934819ce3152dc0625d..07554faf82a649e8224412a83e121a37692bfe5e 100644 (file)
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 2006, 2012-2014 Free Software Foundation, Inc.
 
-;; Version: 1.4
+;; Version: 1.6
 ;; Keywords: large files, utilities
 ;; Maintainer: Andrey Kotlarski <m00naticus@gmail.com>
 ;; Authors: 2006 Mathias Dahl <mathias.dahl@gmail.com>
 
 (require 'vlf-base)
 
+(defcustom vlf-before-batch-functions nil
+  "Hook that runs before multiple batch operations.
+One argument is supplied that specifies current action.  Possible
+values are: `write', `ediff', `occur', `search', `goto-line'."
+  :group 'vlf :type 'hook)
+
+(defcustom vlf-after-batch-functions nil
+  "Hook that runs after multiple batch operations.
+One argument is supplied that specifies current action.  Possible
+values are: `write', `ediff', `occur', `search', `goto-line'."
+  :group 'vlf :type 'hook)
+
 (autoload 'vlf-write "vlf-write" "Write current chunk to file." t)
 (autoload 'vlf-re-search-forward "vlf-search"
   "Search forward for REGEXP prefix COUNT number of times." t)
 
 (define-minor-mode vlf-mode
   "Mode to browse large files in."
-  :lighter " VLF"
-  :group 'vlf
-  :keymap vlf-prefix-map
-  (if vlf-mode
-      (progn
-        (set (make-local-variable 'require-final-newline) nil)
-        (add-hook 'write-file-functions 'vlf-write nil t)
-        (set (make-local-variable 'revert-buffer-function)
-             'vlf-revert)
-        (make-local-variable 'vlf-batch-size)
-        (setq vlf-file-size (vlf-get-file-size buffer-file-truename)
-              vlf-start-pos 0
-              vlf-end-pos 0)
-        (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)))
-    (kill-local-variable 'revert-buffer-function)
-    (vlf-stop-follow)
-    (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))))
-      (kill-local-variable 'require-final-newline)
-      (remove-hook 'write-file-functions 'vlf-write t)
-      (let ((pos (+ vlf-start-pos (position-bytes (point)))))
-        (vlf-with-undo-disabled
-         (insert-file-contents buffer-file-name t nil nil t))
-        (goto-char (byte-to-position pos)))
-      (rename-buffer (file-name-nondirectory buffer-file-name) t))))
+  :lighter " VLF" :group 'vlf :keymap vlf-prefix-map
+  (cond (vlf-mode
+         (set (make-local-variable 'require-final-newline) nil)
+         (add-hook 'write-file-functions 'vlf-write nil t)
+         (set (make-local-variable 'revert-buffer-function)
+              'vlf-revert)
+         (make-local-variable 'vlf-batch-size)
+         (setq vlf-file-size (vlf-get-file-size buffer-file-truename)
+               vlf-start-pos 0
+               vlf-end-pos 0)
+         (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))
+         (add-hook 'after-change-major-mode-hook 'vlf-keep-alive t t)
+         (vlf-keep-alive))
+        ((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))))
+         (kill-local-variable 'revert-buffer-function)
+         (vlf-stop-follow)
+         (kill-local-variable 'require-final-newline)
+         (remove-hook 'write-file-functions 'vlf-write t)
+         (remove-hook 'after-change-major-mode-hook
+                      'vlf-keep-alive t)
+         (let ((hexl (derived-mode-p 'hexl-mode)))
+           (if hexl (hexl-mode-exit))
+           (let ((pos (+ vlf-start-pos (position-bytes (point)))))
+             (vlf-with-undo-disabled
+              (insert-file-contents buffer-file-name t nil nil t))
+             (goto-char (byte-to-position pos)))
+           (if hexl (hexl-mode)))
+         (rename-buffer (file-name-nondirectory buffer-file-name) t))
+        (t (setq vlf-mode t))))
+
+(defun vlf-keep-alive ()
+  "Keep `vlf-mode' on major mode change."
+  (if (derived-mode-p 'hexl-mode)
+      (set (make-local-variable 'revert-buffer-function) 'vlf-revert))
+  (setq vlf-mode t))
 
 ;;;###autoload
-(defun vlf (file)
-  "View Large FILE in batches.
+(defun vlf (file &optional minimal)
+  "View Large FILE in batches.  When MINIMAL load just a few bytes.
 You can customize number of bytes displayed by customizing
 `vlf-batch-size'.
 Return newly created buffer."
-  (interactive "fFile to open: ")
+  (interactive (list (read-file-name "File to open: ") nil))
   (let ((vlf-buffer (generate-new-buffer "*vlf*")))
     (set-buffer vlf-buffer)
     (set-visited-file-name file)
     (set-buffer-modified-p nil)
+    (if (or minimal (file-remote-p file))
+        (set (make-local-variable 'vlf-batch-size) 1024))
     (vlf-mode 1)
+    (when minimal                 ;restore batch size to default value
+      (kill-local-variable 'vlf-batch-size)
+      (make-local-variable 'vlf-batch-size))
     (switch-to-buffer vlf-buffer)
     vlf-buffer))
 
@@ -141,6 +169,9 @@ When prefix argument is negative
  append next APPEND number of batches to the existing buffer."
   (interactive "p")
   (vlf-verify-size)
+  (vlf-tune-load (if (derived-mode-p 'hexl-mode)
+                     '(:hexl :dehexlify :insert :encode)
+                   '(:insert :encode)))
   (let* ((end (min (+ vlf-end-pos (* vlf-batch-size (abs append)))
                    vlf-file-size))
          (start (if (< append 0)
@@ -157,6 +188,9 @@ When prefix argument is negative
   (interactive "p")
   (if (zerop vlf-start-pos)
       (error "Already at BOF"))
+  (vlf-tune-load (if (derived-mode-p 'hexl-mode)
+                     '(:hexl :dehexlify :insert :encode)
+                   '(:insert :encode)))
   (let* ((start (max 0 (- vlf-start-pos (* vlf-batch-size (abs prepend)))))
          (end (if (< prepend 0)
                   vlf-end-pos
@@ -180,6 +214,35 @@ When prefix argument is negative
              (goto-char (point-max)))
     ad-do-it))
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; hexl mode integration
+
+(eval-after-load "hexl"
+  '(progn
+     (defadvice hexl-save-buffer (around vlf-hexl-save
+                                         activate compile)
+       "Prevent hexl save if `vlf-mode' is active."
+       (if vlf-mode
+           (vlf-write)
+         ad-do-it))
+
+     (defadvice hexl-scroll-up (around vlf-hexl-scroll-up
+                                       activate compile)
+       "Slide to next batch if at end of buffer in `vlf-mode'."
+       (if (and vlf-mode (pos-visible-in-window-p (point-max))
+                (or (not (numberp arg)) (< 0 arg)))
+           (progn (vlf-next-batch 1)
+                  (goto-char (point-min)))
+         ad-do-it))
+
+     (defadvice hexl-scroll-down (around vlf-hexl-scroll-down
+                                         activate compile)
+       "Slide to previous batch if at beginning of buffer in `vlf-mode'."
+       (if (and vlf-mode (pos-visible-in-window-p (point-min)))
+           (progn (vlf-prev-batch 1)
+                  (goto-char (point-max)))
+         ad-do-it))))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;; utilities
 
@@ -193,23 +256,34 @@ with the prefix argument DECREASE it is halved."
 
 (defun vlf-set-batch-size (size)
   "Set batch to SIZE bytes and update chunk."
-  (interactive (list (read-number "Size in bytes: " vlf-batch-size)))
+  (interactive
+   (list (read-number "Size in bytes: "
+                      (vlf-tune-optimal-load
+                       (if (derived-mode-p 'hexl-mode)
+                           '(:hexl :dehexlify :insert :encode)
+                         '(:insert :encode))))))
   (setq vlf-batch-size size)
   (vlf-move-to-batch vlf-start-pos))
 
 (defun vlf-beginning-of-file ()
   "Jump to beginning of file content."
   (interactive)
+  (vlf-tune-load (if (derived-mode-p 'hexl-mode)
+                     '(:hexl :dehexlify :insert :encode)
+                   '(:insert :encode)))
   (vlf-move-to-batch 0))
 
 (defun vlf-end-of-file ()
   "Jump to end of file content."
   (interactive)
   (vlf-verify-size)
+  (vlf-tune-load (if (derived-mode-p 'hexl-mode)
+                     '(:hexl :dehexlify :insert :encode)
+                   '(:insert :encode)))
   (vlf-move-to-batch vlf-file-size))
 
-(defun vlf-revert (&optional _ignore-auto noconfirm)
-  "Revert current chunk.  Ignore _IGNORE-AUTO.
+(defun vlf-revert (&optional _auto noconfirm)
+  "Revert current chunk.  Ignore _AUTO.
 Ask for confirmation if NOCONFIRM is nil."
   (interactive)
   (when (or noconfirm
@@ -221,6 +295,9 @@ Ask for confirmation if NOCONFIRM is nil."
 (defun vlf-jump-to-chunk (n)
   "Go to to chunk N."
   (interactive "nGoto to chunk: ")
+  (vlf-tune-load (if (derived-mode-p 'hexl-mode)
+                     '(:hexl :dehexlify :insert :encode)
+                   '(:insert :encode)))
   (vlf-move-to-batch (* (1- n) vlf-batch-size)))
 
 (defun vlf-no-modifications ()