]> code.delx.au - gnu-emacs-elpa/blob - packages/vlf/vlf-follow.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / vlf / vlf-follow.el
1 ;;; vlf-follow.el --- VLF chunk follows point functionality -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Keywords: large files, follow, recenter
6 ;; Author: Andrey Kotlarski <m00naticus@gmail.com>
7 ;; URL: https://github.com/m00natic/vlfi
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25 ;; This package provides `vlf-toggle-follow' command which toggles
26 ;; continuous recenter of chunk around current point.
27
28 ;;; Code:
29
30 (require 'vlf)
31
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)
36
37 (defun vlf-recenter (vlf-buffer)
38 "Recenter chunk around current point in VLF-BUFFER."
39 (and vlf-follow-timer
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)
46 (progn
47 (let ((edit-end (+ (position-bytes (point-max))
48 vlf-start-pos)))
49 (vlf-move-to-chunk (min vlf-start-pos
50 (- current-pos half-batch))
51 (max edit-end
52 (+ current-pos half-batch))))
53 (goto-char (byte-to-position (- current-pos
54 vlf-start-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
59 vlf-start-pos))))))))
60
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)))
66
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
70 'vlf-recenter
71 (current-buffer)))
72 (add-hook 'kill-buffer-hook 'vlf-stop-follow nil t))
73
74 (defun vlf-toggle-follow ()
75 "Toggle continuous chunk recenter around current point."
76 (interactive)
77 (if vlf-mode
78 (if vlf-follow-timer
79 (progn (vlf-stop-follow)
80 (message "Following stopped"))
81 (vlf-start-follow (read-number "Number of seconds: " 1)))))
82
83 (provide 'vlf-follow)
84
85 ;;; vlf-follow.el ends here