1 ;;; vlf-follow.el --- VLF chunk follows point functionality -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Keywords: large files, follow, recenter
6 ;; Author: Andrey Kotlarski <m00naticus@gmail.com>
7 ;; URL: https://github.com/m00natic/vlfi
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
25 ;; This package provides `vlf-toggle-follow' command which toggles
26 ;; continuous recenter of chunk around current point.
32 (defvar vlf-follow-timer nil
33 "Contains timer if vlf buffer is set to continuously recenter.")
34 (make-variable-buffer-local 'vlf-follow-timer)
35 (put 'vlf-follow-timer 'permanent-local t)
37 (defun vlf-recenter (vlf-buffer)
38 "Recenter chunk around current point in VLF-BUFFER."
40 (eq (current-buffer) vlf-buffer)
41 (or (pos-visible-in-window-p (point-min))
42 (pos-visible-in-window-p (point-max)))
43 (let ((current-pos (+ vlf-start-pos (position-bytes (point))))
44 (half-batch (/ vlf-batch-size 2)))
45 (if (buffer-modified-p)
47 (let ((edit-end (+ (position-bytes (point-max))
49 (vlf-move-to-chunk (min vlf-start-pos
50 (- current-pos half-batch))
52 (+ current-pos half-batch))))
53 (goto-char (byte-to-position (- current-pos
55 (vlf-move-to-batch (- current-pos half-batch))
56 (and (< half-batch current-pos)
57 (< half-batch (- vlf-file-size current-pos))
58 (goto-char (byte-to-position (- current-pos
61 (defun vlf-stop-follow ()
62 "Stop continuous recenter."
63 (when vlf-follow-timer
64 (cancel-timer vlf-follow-timer)
65 (setq vlf-follow-timer nil)))
67 (defun vlf-start-follow (interval)
68 "Continuously recenter chunk around point every INTERVAL seconds."
69 (setq vlf-follow-timer (run-with-idle-timer interval interval
72 (add-hook 'kill-buffer-hook 'vlf-stop-follow nil t))
74 (defun vlf-toggle-follow ()
75 "Toggle continuous chunk recenter around current point."
79 (progn (vlf-stop-follow)
80 (message "Following stopped"))
81 (vlf-start-follow (read-number "Number of seconds: " 1)))))
85 ;;; vlf-follow.el ends here