X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/47d5d5b10c68e16e1f1cbf2b76b9a45e01118447..7b78b37f6ae1588e7c91c0edb4ae341f4bad868c:/packages/vlf/vlf.el diff --git a/packages/vlf/vlf.el b/packages/vlf/vlf.el index 2e3c9a1b8..e695c2c62 100644 --- a/packages/vlf/vlf.el +++ b/packages/vlf/vlf.el @@ -1,13 +1,13 @@ ;;; vlf.el --- View Large Files -*- lexical-binding: t -*- -;; Copyright (C) 2006, 2012-2014 Free Software Foundation, Inc. +;; Copyright (C) 2006, 2012-2015 Free Software Foundation, Inc. -;; Version: 1.5 +;; Version: 1.7 ;; Keywords: large files, utilities ;; Maintainer: Andrey Kotlarski ;; Authors: 2006 Mathias Dahl ;; 2012 Sam Steingold -;; 2013-2014 Andrey Kotlarski +;; 2013-2015 Andrey Kotlarski ;; URL: https://github.com/m00natic/vlfi ;; This file is free software; you can redistribute it and/or modify @@ -31,7 +31,7 @@ ;; which provides several commands for moving around, searching, ;; comparing and editing selected part of file. ;; To have it offered when opening large files: -;; (require 'vlf-integrate) +;; (require 'vlf-setup) ;; This package was inspired by a snippet posted by Kevin Rodgers, ;; showing how to use `insert-file-contents' to extract part of a @@ -39,8 +39,7 @@ ;;; Code: -(defgroup vlf nil "View Large Files in Emacs." - :prefix "vlf-" :group 'files) +(require 'vlf-base) (defcustom vlf-before-batch-functions nil "Hook that runs before multiple batch operations. @@ -54,7 +53,7 @@ One argument is supplied that specifies current action. Possible values are: `write', `ediff', `occur', `search', `goto-line'." :group 'vlf :type 'hook) -(require 'vlf-base) +(defvar hexl-bits) (autoload 'vlf-write "vlf-write" "Write current chunk to file." t) (autoload 'vlf-re-search-forward "vlf-search" @@ -62,6 +61,8 @@ values are: `write', `ediff', `occur', `search', `goto-line'." (autoload 'vlf-re-search-backward "vlf-search" "Search backward for REGEXP prefix COUNT number of times." t) (autoload 'vlf-goto-line "vlf-search" "Go to line." t) +(autoload 'vlf-query-replace "vlf-search" + "Query replace regexp over whole file." t) (autoload 'vlf-occur "vlf-occur" "Make whole file occur style index for REGEXP." t) (autoload 'vlf-toggle-follow "vlf-follow" @@ -82,6 +83,7 @@ values are: `write', `ediff', `occur', `search', `goto-line'." (vlf-change-batch-size t))) (define-key map "s" 'vlf-re-search-forward) (define-key map "r" 'vlf-re-search-backward) + (define-key map "%" 'vlf-query-replace) (define-key map "o" 'vlf-occur) (define-key map "[" 'vlf-beginning-of-file) (define-key map "]" 'vlf-end-of-file) @@ -101,7 +103,11 @@ values are: `write', `ediff', `occur', `search', `goto-line'." (define-minor-mode vlf-mode "Mode to browse large files in." - :lighter " VLF" :group 'vlf :keymap vlf-prefix-map + :group 'vlf :keymap vlf-prefix-map + :lighter (:eval (format " VLF[%d/%d](%s)" + (/ vlf-end-pos vlf-batch-size) + (/ vlf-file-size vlf-batch-size) + (file-size-human-readable vlf-file-size))) (cond (vlf-mode (set (make-local-variable 'require-final-newline) nil) (add-hook 'write-file-functions 'vlf-write nil t) @@ -128,14 +134,25 @@ values are: `write', `ediff', `occur', `search', `goto-line'." (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))))) + (if (derived-mode-p 'hexl-mode) + (let ((line (/ (1+ vlf-start-pos) hexl-bits)) + (pos (point))) + (if (consp buffer-undo-list) + (setq buffer-undo-list nil)) + (vlf-with-undo-disabled + (let ((inhibit-read-only t)) + (insert-file-contents-literally buffer-file-name + t nil nil t) + (hexlify-buffer))) + (set-buffer-modified-p nil) + (goto-char (point-min)) + (forward-line line) + (forward-char pos)) + (let ((pos (+ vlf-start-pos (position-bytes (point)))) + (inhibit-read-only t)) (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)) + (goto-char (byte-to-position pos))))) (t (setq vlf-mode t)))) (defun vlf-keep-alive () @@ -145,17 +162,22 @@ values are: `write', `ediff', `occur', `search', `goto-line'." (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)) @@ -167,6 +189,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 :raw) + '(:insert :encode))) (let* ((end (min (+ vlf-end-pos (* vlf-batch-size (abs append))) vlf-file-size)) (start (if (< append 0) @@ -183,6 +208,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 :raw) + '(:insert :encode))) (let* ((start (max 0 (- vlf-start-pos (* vlf-batch-size (abs prepend))))) (end (if (< prepend 0) vlf-end-pos @@ -206,24 +234,8 @@ When prefix argument is negative (goto-char (point-max))) ad-do-it)) -;; hexl mode integration -(defun vlf-hexl-before (&optional operation) - "Temporarily disable `hexl-mode' for OPERATION." - (when (derived-mode-p 'hexl-mode) - (hexl-mode-exit) - (set (make-local-variable 'vlf-restore-hexl-mode) operation))) - -(defun vlf-hexl-after (&optional operation) - "Re-enable `hexl-mode' if active before OPERATION." - (when (and (boundp 'vlf-restore-hexl-mode) - (eq vlf-restore-hexl-mode operation)) - (hexl-mode) - (kill-local-variable 'vlf-restore-hexl-mode))) - -(add-hook 'vlf-before-batch-functions 'vlf-hexl-before) -(add-hook 'vlf-after-batch-functions 'vlf-hexl-after) -(add-hook 'vlf-before-chunk-update 'vlf-hexl-before) -(add-hook 'vlf-after-chunk-update 'vlf-hexl-after) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; hexl mode integration (eval-after-load "hexl" '(progn @@ -249,6 +261,16 @@ When prefix argument is negative (if (and vlf-mode (pos-visible-in-window-p (point-min))) (progn (vlf-prev-batch 1) (goto-char (point-max))) + ad-do-it)) + + (defadvice hexl-mode-exit (around vlf-hexl-mode-exit + activate compile) + "Exit `hexl-mode' gracefully in case `vlf-mode' is active." + (if (and vlf-mode (not (buffer-modified-p))) + (vlf-with-undo-disabled + (erase-buffer) + ad-do-it + (vlf-move-to-chunk-2 vlf-start-pos vlf-end-pos)) ad-do-it)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -264,23 +286,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 :raw) + '(: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 :raw) + '(: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 :raw) + '(: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 @@ -292,6 +325,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 :raw) + '(:insert :encode))) (vlf-move-to-batch (* (1- n) vlf-batch-size))) (defun vlf-no-modifications () @@ -300,16 +336,15 @@ Ask for confirmation if NOCONFIRM is nil." (error "Save or discard your changes first") t)) -(defun vlf-move-to-batch (start &optional minimal) +(defun vlf-move-to-batch (start) "Move to batch determined by START. -Adjust according to file start/end and show `vlf-batch-size' bytes. -When given MINIMAL flag, skip non important operations." +Adjust according to file start/end and show `vlf-batch-size' bytes." (vlf-verify-size) (let* ((start (max 0 start)) (end (min (+ start vlf-batch-size) vlf-file-size))) (if (= vlf-file-size end) ; re-adjust start (setq start (max 0 (- end vlf-batch-size)))) - (vlf-move-to-chunk start end minimal))) + (vlf-move-to-chunk start end))) (defun vlf-next-batch-from-point () "Display batch of file data starting from current point."