From 4134de068fe97e308dff2c684adce9fd2af468ca Mon Sep 17 00:00:00 2001 From: Andrey Kotlarski Date: Fri, 13 Dec 2013 17:22:10 +0200 Subject: [PATCH] Add intelligent recenter chunk around point functionality. --- README.org | 13 +++++++++++++ vlf.el | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/README.org b/README.org index 045ab0c..9acd286 100644 --- a/README.org +++ b/README.org @@ -79,6 +79,9 @@ refresh with *C-c C-v g*. ** Move around +Scrolling automatically triggers moving to previous or next chunk at +the beginning or end respectively of the current one. + *C-c C-v n* and *C-c C-v p* move batch by batch. With positive prefix argument they move prefix number of batches. With negative - append prefix number of batches. @@ -93,6 +96,16 @@ how many chunks there are (using the current batch size), look at the parenthesized part of the buffer name, batch size is also indicated at the end. +** Follow point + +Intelligent continuous recenter around point in current buffer can be +enabled with: + +*M-x vlf-start-following* + +You are asked for number of idle seconds interval for automatic +update. To cancel following invoke *M-x vlf-stop-following*. + ** Search whole file *C-c C-v s* and *C-c C-v r* search forward and backward respectively diff --git a/vlf.el b/vlf.el index 73b133f..d38aa35 100644 --- a/vlf.el +++ b/vlf.el @@ -73,6 +73,11 @@ Possible values are: nil to never use it; (defvar vlf-file-size 0 "Total size of presented file.") (put 'vlf-file-size 'permanent-local t) +(defvar vlf-follow-timer nil + "Contains timer and it's repeat interval if vlf buffer is set to\ +continuously recenter.") +(put 'vlf-follow-timer 'permanent-local t) + (defvar vlf-mode-map (let ((map (make-sparse-keymap))) (define-key map "n" 'vlf-next-batch) @@ -125,11 +130,13 @@ Possible values are: nil to never use it; (vlf-get-file-size buffer-file-truename)) (set (make-local-variable 'vlf-start-pos) 0) (set (make-local-variable 'vlf-end-pos) 0) + (set (make-local-variable 'vlf-follow-timer) nil) (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-following) (when (or (not large-file-warning-threshold) (< vlf-file-size large-file-warning-threshold) (y-or-n-p (format "Load whole file (%s)? " @@ -563,6 +570,50 @@ This seems to be the case with GNU/Emacs before 24.4." (t ;; TODO: use (< emacs-minor-version 4) after 24.4 release (string-lessp emacs-version "24.3.5")))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; follow point + +(defun vlf-recenter (vlf-buffer) + "Recenter chunk around current point in VLF-BUFFER." + (and vlf-follow-timer + (eq (current-buffer) vlf-buffer) + (or (pos-visible-in-window-p (point-min)) + (pos-visible-in-window-p (point-max))) + (let ((current-pos (+ vlf-start-pos (position-bytes (point)))) + (half-batch (/ vlf-batch-size 2))) + (if (buffer-modified-p) + (progn + (let ((edit-end (+ (position-bytes (point-max)) + vlf-start-pos))) + (vlf-move-to-chunk (min vlf-start-pos + (- current-pos half-batch)) + (max edit-end + (+ current-pos half-batch)))) + (goto-char (byte-to-position (- current-pos + vlf-start-pos)))) + (vlf-move-to-batch (- current-pos half-batch)) + (and (< half-batch current-pos) + (< half-batch (- vlf-file-size current-pos)) + (goto-char (byte-to-position half-batch))))))) + +(defun vlf-stop-following () + "Stop continuous recenter." + (interactive) + (when vlf-follow-timer + (cancel-timer (car vlf-follow-timer)) + (setq vlf-follow-timer nil))) + +(defun vlf-start-following (interval) + "Continuously recenter chunk around point every INTERVAL seconds." + (interactive "nNumber of seconds: ") + (when vlf-mode + (vlf-stop-following) + (setq vlf-follow-timer (cons (run-with-idle-timer interval interval + 'vlf-recenter + (current-buffer)) + interval)) + (add-hook 'kill-buffer-hook 'vlf-stop-following nil t))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; search