1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-01-18 20:10:47 +00:00

Add hooks to run around chunk moves and batch operations. Don't err

when tramp hasn't been loaded yet.
This commit is contained in:
Andrey Kotlarski 2014-02-14 02:46:52 +02:00
parent 924d6b18fa
commit b05255b225
7 changed files with 66 additions and 29 deletions

View File

@ -35,6 +35,14 @@
:group 'vlf :type 'integer) :group 'vlf :type 'integer)
(put 'vlf-batch-size 'permanent-local t) (put 'vlf-batch-size 'permanent-local t)
(defcustom vlf-before-chunk-update nil
"Hook that runs before chunk update."
:group 'vlf :type 'hook)
(defcustom vlf-after-chunk-update nil
"Hook that runs after chunk update."
:group 'vlf :type 'hook)
;;; Keep track of file position. ;;; Keep track of file position.
(defvar vlf-start-pos 0 (defvar vlf-start-pos 0
"Absolute position of the visible chunk start.") "Absolute position of the visible chunk start.")
@ -139,6 +147,7 @@ bytes added to the end."
((or (and (<= start vlf-start-pos) (<= edit-end end)) ((or (and (<= start vlf-start-pos) (<= edit-end end))
(not modified) (not modified)
(y-or-n-p "Chunk modified, are you sure? ")) (y-or-n-p "Chunk modified, are you sure? "))
(run-hooks 'vlf-before-chunk-update)
(let ((shift-start 0) (let ((shift-start 0)
(shift-end 0)) (shift-end 0))
(let ((pos (+ (position-bytes (point)) vlf-start-pos)) (let ((pos (+ (position-bytes (point)) vlf-start-pos))
@ -197,12 +206,14 @@ bytes added to the end."
(setq vlf-start-pos start)) (setq vlf-start-pos start))
(set-buffer-modified-p modified) (set-buffer-modified-p modified)
(set-visited-file-modtime) (set-visited-file-modtime)
(run-hooks 'vlf-after-chunk-update)
(cons shift-start shift-end)))))) (cons shift-start shift-end))))))
(defun vlf-move-to-chunk-2 (start end) (defun vlf-move-to-chunk-2 (start end)
"Unconditionally move to chunk enclosed by START END bytes. "Unconditionally move to chunk enclosed by START END bytes.
Return number of bytes moved back for proper decoding and number of Return number of bytes moved back for proper decoding and number of
bytes added to the end." bytes added to the end."
(run-hooks 'vlf-before-chunk-update)
(vlf-verify-size t) (vlf-verify-size t)
(setq vlf-start-pos (max 0 start) (setq vlf-start-pos (max 0 start)
vlf-end-pos (min end vlf-file-size)) vlf-end-pos (min end vlf-file-size))
@ -220,6 +231,7 @@ bytes added to the end."
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(or (eq buffer-undo-list t) (or (eq buffer-undo-list t)
(setq buffer-undo-list nil)) (setq buffer-undo-list nil))
(run-hooks 'vlf-after-chunk-update)
shifts)) shifts))
(defun vlf-insert-file-contents (start end adjust-start adjust-end (defun vlf-insert-file-contents (start end adjust-start adjust-end

View File

@ -1,4 +1,4 @@
;;; vlf-ediff.el --- VLF ediff functionality ;;; vlf-ediff.el --- VLF ediff functionality -*- lexical-binding: t -*-
;; Copyright (C) 2014 Free Software Foundation, Inc. ;; Copyright (C) 2014 Free Software Foundation, Inc.
@ -154,6 +154,7 @@ beginning of difference list."
governed by EDIFF-BUFFER. NEXT-FUNC is used to jump to the next governed by EDIFF-BUFFER. NEXT-FUNC is used to jump to the next
logical chunks in case there is no difference at the current ones." logical chunks in case there is no difference at the current ones."
(set-buffer buffer-A) (set-buffer buffer-A)
(run-hook-with-args 'vlf-before-batch-functions 'ediff)
(setq buffer-A (current-buffer)) ;names change, so reference by buffer object (setq buffer-A (current-buffer)) ;names change, so reference by buffer object
(let ((end-A (= vlf-start-pos vlf-end-pos)) (let ((end-A (= vlf-start-pos vlf-end-pos))
(chunk-A (cons vlf-start-pos vlf-end-pos)) (chunk-A (cons vlf-start-pos vlf-end-pos))
@ -163,9 +164,11 @@ logical chunks in case there is no difference at the current ones."
(forward-p (eq next-func 'vlf-next-chunk))) (forward-p (eq next-func 'vlf-next-chunk)))
(font-lock-mode 0) (font-lock-mode 0)
(set-buffer buffer-B) (set-buffer buffer-B)
(run-hook-with-args 'vlf-before-batch-functions 'ediff)
(setq buffer-B (current-buffer) (setq buffer-B (current-buffer)
min-file-size (min min-file-size vlf-file-size)) min-file-size (min min-file-size vlf-file-size))
(let ((tramp-verbose (min 2 tramp-verbose)) (let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(end-B (= vlf-start-pos vlf-end-pos)) (end-B (= vlf-start-pos vlf-end-pos))
(chunk-B (cons vlf-start-pos vlf-end-pos)) (chunk-B (cons vlf-start-pos vlf-end-pos))
(font-lock-B font-lock-mode) (font-lock-B font-lock-mode)
@ -222,12 +225,6 @@ logical chunks in case there is no difference at the current ones."
(and (not end-A) (not end-B))) (and (not end-A) (not end-B)))
(vlf-ediff-refine buffer-A buffer-B))) (vlf-ediff-refine buffer-A buffer-B)))
(setq done t)) (setq done t))
(when font-lock-A
(set-buffer buffer-A)
(font-lock-mode 1))
(when font-lock-B
(set-buffer buffer-B)
(font-lock-mode 1))
(unless done (unless done
(set-buffer buffer-A) (set-buffer buffer-A)
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
@ -237,7 +234,13 @@ logical chunks in case there is no difference at the current ones."
(vlf-move-to-chunk (car chunk-B) (cdr chunk-B)) (vlf-move-to-chunk (car chunk-B) (cdr chunk-B))
(set-buffer ediff-buffer) (set-buffer ediff-buffer)
(ediff-update-diffs) (ediff-update-diffs)
(vlf-ediff-refine buffer-A buffer-B)))))) (vlf-ediff-refine buffer-A buffer-B))
(set-buffer buffer-A)
(if font-lock-A (font-lock-mode 1))
(run-hook-with-args 'vlf-after-batch-functions 'ediff)
(set-buffer buffer-B)
(if font-lock-B (font-lock-mode 1))
(run-hook-with-args 'vlf-after-batch-functions 'ediff)))))
(defun vlf-ediff-refine (buffer-A buffer-B) (defun vlf-ediff-refine (buffer-A buffer-B)
"Try to minimize differences between BUFFER-A and BUFFER-B. "Try to minimize differences between BUFFER-A and BUFFER-B.

View File

@ -26,10 +26,8 @@
;;; Code: ;;; Code:
(defgroup vlf nil (defgroup vlf nil "View Large Files in Emacs."
"View Large Files in Emacs." :prefix "vlf-" :group 'files)
:prefix "vlf-"
:group 'files)
(defcustom vlf-application 'ask (defcustom vlf-application 'ask
"Determines when `vlf' will be offered on opening files. "Determines when `vlf' will be offered on opening files.
@ -37,18 +35,16 @@ Possible values are: nil to never use it;
`ask' offer `vlf' when file size is beyond `large-file-warning-threshold'; `ask' offer `vlf' when file size is beyond `large-file-warning-threshold';
`dont-ask' automatically use `vlf' for large files; `dont-ask' automatically use `vlf' for large files;
`always' use `vlf' for all files." `always' use `vlf' for all files."
:group 'vlf :group 'vlf :type '(radio (const :format "%v " nil)
:type '(radio (const :format "%v " nil) (const :format "%v " ask)
(const :format "%v " ask) (const :format "%v " dont-ask)
(const :format "%v " dont-ask) (const :format "%v" always)))
(const :format "%v" always)))
(defcustom vlf-forbidden-modes-list (defcustom vlf-forbidden-modes-list
'(archive-mode tar-mode jka-compr git-commit-mode image-mode '(archive-mode tar-mode jka-compr git-commit-mode image-mode
doc-view-mode doc-view-mode-maybe ebrowse-tree-mode) doc-view-mode doc-view-mode-maybe ebrowse-tree-mode)
"Major modes which VLF will not be automatically applied to." "Major modes which VLF will not be automatically applied to."
:group 'vlf :group 'vlf :type '(list symbol))
:type '(list symbol))
(unless (fboundp 'file-size-human-readable) (unless (fboundp 'file-size-human-readable)
(defun file-size-human-readable (file-size) (defun file-size-human-readable (file-size)

View File

@ -131,8 +131,10 @@ Prematurely ending indexing will still show what's found so far."
(set (make-local-variable 'vlf-batch-size) batch-size) (set (make-local-variable 'vlf-batch-size) batch-size)
(vlf-mode 1) (vlf-mode 1)
(goto-char (point-min)) (goto-char (point-min))
(run-hook-with-args 'vlf-before-batch-functions 'occur)
(vlf-with-undo-disabled (vlf-with-undo-disabled
(vlf-build-occur regexp vlf-buffer)))) (vlf-build-occur regexp vlf-buffer))))
(run-hook-with-args 'vlf-before-batch-functions 'occur)
(let ((start-pos vlf-start-pos) (let ((start-pos vlf-start-pos)
(end-pos vlf-end-pos) (end-pos vlf-end-pos)
(pos (point))) (pos (point)))
@ -141,11 +143,13 @@ Prematurely ending indexing will still show what's found so far."
(goto-char (point-min)) (goto-char (point-min))
(unwind-protect (vlf-build-occur regexp (current-buffer)) (unwind-protect (vlf-build-occur regexp (current-buffer))
(vlf-move-to-chunk start-pos end-pos) (vlf-move-to-chunk start-pos end-pos)
(goto-char pos)))))) (goto-char pos)))))
(run-hook-with-args 'vlf-before-after-functions 'occur))
(defun vlf-build-occur (regexp vlf-buffer) (defun vlf-build-occur (regexp vlf-buffer)
"Build occur style index for REGEXP over VLF-BUFFER." "Build occur style index for REGEXP over VLF-BUFFER."
(let ((tramp-verbose (min 2 tramp-verbose)) (let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(case-fold-search t) (case-fold-search t)
(line 1) (line 1)
(last-match-line 0) (last-match-line 0)
@ -244,7 +248,8 @@ in file: %s" total-matches line regexp file)
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(forward-char 2) (forward-char 2)
(vlf-occur-mode)) (vlf-occur-mode))
(display-buffer occur-buffer))))) (display-buffer occur-buffer))
(run-hook-with-args 'vlf-after-batch-functions 'occur))))
(provide 'vlf-occur) (provide 'vlf-occur)

View File

@ -34,7 +34,9 @@
BATCH-STEP is amount of overlap between successive chunks." BATCH-STEP is amount of overlap between successive chunks."
(if (<= count 0) (if (<= count 0)
(error "Count must be positive")) (error "Count must be positive"))
(let* ((tramp-verbose (min 2 tramp-verbose)) (run-hook-with-args 'vlf-before-batch-functions 'search)
(let* ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(case-fold-search t) (case-fold-search t)
(match-chunk-start vlf-start-pos) (match-chunk-start vlf-start-pos)
(match-chunk-end vlf-end-pos) (match-chunk-end vlf-end-pos)
@ -118,7 +120,8 @@ BATCH-STEP is amount of overlap between successive chunks."
count to-find) count to-find)
(vlf-goto-match match-chunk-start match-chunk-end (vlf-goto-match match-chunk-start match-chunk-end
match-start-pos match-end-pos match-start-pos match-end-pos
count to-find)))))) count to-find)))))
(run-hook-with-args 'vlf-after-batch-functions 'search))
(defun vlf-goto-match (match-chunk-start match-chunk-end (defun vlf-goto-match (match-chunk-start match-chunk-end
match-pos-start match-pos-start
@ -179,8 +182,10 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
"Go to line N. If N is negative, count from the end of file." "Go to line N. If N is negative, count from the end of file."
(interactive (if (vlf-no-modifications) (interactive (if (vlf-no-modifications)
(list (read-number "Go to line: ")))) (list (read-number "Go to line: "))))
(run-hook-with-args 'vlf-before-batch-functions 'goto-line)
(vlf-verify-size) (vlf-verify-size)
(let ((tramp-verbose (min 2 tramp-verbose)) (let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(start-pos vlf-start-pos) (start-pos vlf-start-pos)
(end-pos vlf-end-pos) (end-pos vlf-end-pos)
(pos (point)) (pos (point))
@ -244,7 +249,8 @@ Search is performed chunk by chunk in `vlf-batch-size' memory."
(vlf-with-undo-disabled (vlf-with-undo-disabled
(vlf-move-to-chunk-2 start-pos end-pos)) (vlf-move-to-chunk-2 start-pos end-pos))
(goto-char pos) (goto-char pos)
(message "Unable to find line"))))) (message "Unable to find line"))
(run-hook-with-args 'vlf-after-batch-functions 'goto-line))))
(provide 'vlf-search) (provide 'vlf-search)

View File

@ -52,7 +52,9 @@ Save anyway? "))
region-length))) region-length)))
(if (zerop size-change) (if (zerop size-change)
(write-region nil nil buffer-file-name vlf-start-pos t) (write-region nil nil buffer-file-name vlf-start-pos t)
(let ((tramp-verbose (min 2 tramp-verbose)) (run-hook-with-args 'vlf-before-batch-functions 'write)
(let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(pos (point)) (pos (point))
(font-lock font-lock-mode)) (font-lock font-lock-mode))
(font-lock-mode 0) (font-lock-mode 0)
@ -66,7 +68,8 @@ Save anyway? "))
(+ vlf-start-pos vlf-batch-size) (+ vlf-start-pos vlf-batch-size)
vlf-end-pos)) vlf-end-pos))
(vlf-update-buffer-name) (vlf-update-buffer-name)
(goto-char pos)))))) (goto-char pos))
(run-hook-with-args 'vlf-after-batch-functions 'write)))))
t) t)
(defun vlf-file-shift-back (size-change) (defun vlf-file-shift-back (size-change)

12
vlf.el
View File

@ -39,6 +39,18 @@
;;; Code: ;;; Code:
(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)
(require 'vlf-base) (require 'vlf-base)
(autoload 'vlf-write "vlf-write" "Write current chunk to file." t) (autoload 'vlf-write "vlf-write" "Write current chunk to file." t)