1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-27 07:37:33 +00:00
emacs/lisp/pixel-scroll.el
Paul Eggert 476066e89d Avoid some double-rounding of Lisp timestamps
Also, simplify some time-related Lisp timestamp code
while we’re in the neighborhood.
* lisp/battery.el (battery-linux-proc-acpi)
(battery-linux-sysfs, battery-upower, battery-bsd-apm):
* lisp/calendar/timeclock.el (timeclock-seconds-to-string)
(timeclock-log, timeclock-last-period)
(timeclock-entry-length, timeclock-entry-list-span)
(timeclock-find-discrep, timeclock-generate-report):
* lisp/cedet/ede/detect.el (ede-detect-qtest):
* lisp/completion.el (cmpl-hours-since-origin):
* lisp/ecomplete.el (ecomplete-decay-1):
* lisp/emacs-lisp/ert.el (ert--results-update-stats-display)
(ert--results-update-stats-display-maybe):
* lisp/emacs-lisp/timer-list.el (list-timers):
* lisp/emacs-lisp/timer.el (timer-until)
(timer-event-handler):
* lisp/erc/erc-backend.el (erc-server-send-ping)
(erc-server-send-queue, erc-handle-parsed-server-response)
(erc-handle-unknown-server-response):
* lisp/erc/erc-track.el (erc-buffer-visible):
* lisp/erc/erc.el (erc-lurker-cleanup, erc-lurker-p)
(erc-cmd-PING, erc-send-current-line):
* lisp/eshell/em-pred.el (eshell-pred-file-time):
* lisp/eshell/em-unix.el (eshell-show-elapsed-time):
* lisp/gnus/gnus-icalendar.el (gnus-icalendar-event:org-timestamp):
* lisp/gnus/gnus-int.el (gnus-backend-trace):
* lisp/gnus/gnus-sum.el (gnus-user-date):
* lisp/gnus/mail-source.el (mail-source-delete-crash-box):
* lisp/gnus/nnmaildir.el (nnmaildir--scan):
* lisp/ibuf-ext.el (ibuffer-mark-old-buffers):
* lisp/gnus/nnmaildir.el (nnmaildir--scan):
* lisp/mouse.el (mouse--down-1-maybe-follows-link)
(mouse--click-1-maybe-follows-link):
* lisp/mpc.el (mpc--faster-toggle):
* lisp/net/rcirc.el (rcirc-handler-ctcp-KEEPALIVE)
(rcirc-sentinel):
* lisp/net/tramp-cache.el (tramp-get-file-property):
* lisp/net/tramp-sh.el (tramp-sh-handle-file-newer-than-file-p)
(tramp-maybe-open-connection):
* lisp/net/tramp-smb.el (tramp-smb-maybe-open-connection):
* lisp/org/org-clock.el (org-clock-resolve):
(org-resolve-clocks, org-clock-in, org-clock-out, org-clock-sum):
* lisp/org/org-timer.el (org-timer-start)
(org-timer-pause-or-continue, org-timer-seconds):
* lisp/org/org.el (org-evaluate-time-range):
* lisp/org/ox-publish.el (org-publish-cache-ctime-of-src):
* lisp/pixel-scroll.el (pixel-scroll-in-rush-p):
* lisp/play/hanoi.el (hanoi-move-ring):
* lisp/proced.el (proced-format-time):
* lisp/progmodes/cpp.el (cpp-progress-message):
* lisp/progmodes/flymake.el (flymake--handle-report):
* lisp/progmodes/js.el (js--wait-for-matching-output):
* lisp/subr.el (progress-reporter-do-update):
* lisp/term/xterm.el (xterm--read-event-for-query):
* lisp/time.el (display-time-update, emacs-uptime):
* lisp/tooltip.el (tooltip-delay):
* lisp/url/url-cookie.el (url-cookie-parse-file-netscape):
* lisp/url/url-queue.el (url-queue-prune-old-entries):
* lisp/url/url.el (url-retrieve-synchronously):
* lisp/xt-mouse.el (xterm-mouse-event):
Avoid double-rounding of time-related values.  Simplify.
* lisp/calendar/icalendar.el (icalendar--decode-isodatetime):
When hoping for the best (unlikely), use a better decoded time.
(icalendar--convert-sexp-to-ical): Avoid unnecessary encode-time.
* lisp/calendar/timeclock.el (timeclock-when-to-leave):
* lisp/cedet/ede/detect.el (ede-detect-qtest):
* lisp/desktop.el (desktop-create-buffer):
* lisp/emacs-lisp/benchmark.el (benchmark-elapse):
* lisp/gnus/gnus-art.el (article-lapsed-string):
* lisp/gnus/gnus-group.el (gnus-group-timestamp-delta):
* lisp/gnus/nnmail.el (nnmail-expired-article-p):
* lisp/gnus/nnmaildir.el (nnmaildir-request-expire-articles):
* lisp/nxml/rng-maint.el (rng-time-function):
* lisp/org/org-clock.el (org-clock-get-clocked-time)
(org-clock-resolve, org-resolve-clocks, org-resolve-clocks-if-idle):
* lisp/org/org-habit.el (org-habit-insert-consistency-graphs):
* lisp/progmodes/vhdl-mode.el (vhdl-update-progress-info)
(vhdl-fix-case-region-1):
Use time-since instead of open-coding most of it.
* lisp/erc/erc-dcc.el (erc-dcc-get-sentinel):
* lisp/erc/erc.el (erc-string-to-emacs-time, erc-time-gt):
Now obsolete.  All uses changed.
(erc-time-diff): Accept all Lisp time values.
All uses changed.
* lisp/gnus/gnus-demon.el (gnus-demon-idle-since):
* lisp/gnus/gnus-score.el (gnus-score-headers):
* lisp/gnus/nneething.el (nneething-make-head):
* lisp/gnus/nnheader.el (nnheader-message-maybe):
* lisp/gnus/nnimap.el (nnimap-keepalive):
* lisp/image.el (image-animate-timeout):
* lisp/mail/feedmail.el (feedmail-rfc822-date):
* lisp/net/imap.el (imap-wait-for-tag):
* lisp/net/newst-backend.el (newsticker--image-get):
* lisp/net/rcirc.el (rcirc-handler-317, rcirc-handler-333):
* lisp/obsolete/xesam.el (xesam-refresh-entry):
* lisp/org/org-agenda.el (org-agenda-show-clocking-issues)
(org-agenda-check-clock-gap, org-agenda-to-appt):
* lisp/org/org-capture.el (org-capture-set-target-location):
* lisp/org/org-clock.el (org-clock-resolve-clock)
(org-clocktable-steps):
* lisp/org/org-colview.el (org-columns-edit-value)
(org-columns, org-agenda-columns):
* lisp/org/org-duration.el (org-duration-from-minutes):
* lisp/org/org-element.el (org-element-cache-sync-duration)
(org-element-cache-sync-break)
(org-element--cache-interrupt-p, org-element--cache-sync):
* lisp/org/org-habit.el (org-habit-get-faces)
* lisp/org/org-indent.el (org-indent-add-properties):
* lisp/org/org-table.el (org-table-sum):
* lisp/org/org-timer.el (org-timer-show-remaining-time)
(org-timer-set-timer):
* lisp/org/org.el (org-babel-load-file, org-today)
(org-auto-repeat-maybe, org-2ft, org-time-stamp)
(org-read-date-analyze, org-time-stamp-to-now)
(org-small-year-to-year, org-goto-calendar):
* lisp/org/ox.el (org-export-insert-default-template):
* lisp/ses.el (ses--time-check):
* lisp/type-break.el (type-break-time-warning)
(type-break-statistics, type-break-demo-boring):
* lisp/url/url-cache.el (url-cache-expired)
(url-cache-prune-cache):
* lisp/vc/vc-git.el (vc-git-stash-snapshot):
* lisp/erc/erc-match.el (erc-log-matches-come-back):
Simplify.
2019-02-22 18:33:57 -08:00

356 lines
14 KiB
EmacsLisp

;;; pixel-scroll.el --- Scroll a line smoothly
;; Copyright (C) 2017-2019 Free Software Foundation, Inc.
;; Author: Tak Kunihiro <tkk@misasa.okayama-u.ac.jp>
;; Keywords: mouse
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;; Usage:
;;
;; To interactively toggle the mode:
;;
;; M-x pixel-scroll-mode RET
;;
;; To make the mode permanent, put these in your init file:
;;
;; (require 'pixel-scroll)
;; (pixel-scroll-mode 1)
;;; Commentary:
;; This package offers a global minor mode which makes mouse-wheel
;; scroll a line smoothly.
;;
;; Scrolling a line up by `set-window-vscroll' and that by `scroll-up'
;; give similar display as shown below.
;;
;; A: (scroll-up 1)
;; B: (set-window-vscroll nil (frame-char-height) t)
;;
;; Also scrolling a pixel up by `set-window-vscroll' and that by
;; `scroll-up' give similar display, when vscroll is the last pixel of
;; the line, as shown below.
;;
;; A: (scroll-up 1)
;; B: (set-window-vscroll nil (1- (frame-char-height) t)) (scroll-up 1)
;;
;; When point reaches to the top of a window on scroll by
;; `set-window-vscroll', vscroll is set to zero. To scroll a line
;; smoothly and continuously, this package scrolls a line by following
;; sequences.
;;
;; (vertical-motion 1)
;; (dolist (vs (number-sequence 1 (1- (frame-char-height))))
;; (set-window-vscroll nil vs t) (sit-for 0))
;; (scroll-up 1)
;;; Todo:
;;
;; Allowing pixel-level scrolling in Emacs requires a thorough review
;; of the related functionalities, to make sure none of them zeroes
;; out vscroll where users won't want that.
;;; Code:
(require 'mwheel)
(defvar pixel-wait 0
"Idle time on each step of pixel scroll specified in second.
More wait will result in slow and gentle scroll.")
(defvar pixel-resolution-fine-flag nil
"Set scrolling resolution to pixels instead of a line.
When it is t, scrolling resolution is number of pixels obtained
by `frame-char-height' instead of a line. When it is number,
scrolling resolution is set to number of pixels specified. In
case you need scrolling resolution of a pixel, set to 1. After a
pixel scroll, typing \\[next-line] or \\[previous-line] scrolls the window to make it
fully visible, and undoes the effect of the pixel-level scroll.")
(defvar pixel-dead-time 0.1
"Minimal interval in seconds before next smooth scrolling.
If another scrolling request arrives within this period, scrolling
will be carried out without pixel resolution. If zero, scrolling
is always with pixel resolution.")
(defvar pixel-last-scroll-time 0
"Time when the last scrolling was made, in second since the epoch.")
(defun pixel-scroll-in-rush-p ()
"Return non-nil if next scroll should be non-smooth.
When scrolling request is delivered soon after the previous one,
user is in hurry. When the time since last scroll is larger than
`pixel-dead-time', we are ready for another smooth scroll, and this
function returns nil."
(let* ((now (current-time))
(scroll-in-rush-p (time-less-p
(time-subtract now pixel-last-scroll-time)
pixel-dead-time)))
(setq pixel-last-scroll-time (float-time now))
scroll-in-rush-p))
;;;###autoload
(define-minor-mode pixel-scroll-mode
"A minor mode to scroll text pixel-by-pixel."
:init-value nil
:group 'scrolling
:global t
:version "26.1"
(if pixel-scroll-mode
(setq mwheel-scroll-up-function 'pixel-scroll-up
mwheel-scroll-down-function 'pixel-scroll-down)
(setq mwheel-scroll-up-function 'scroll-up
mwheel-scroll-down-function 'scroll-down)))
(defun pixel-scroll-up (&optional arg)
"Scroll text of selected window up ARG lines.
This is an alternative of `scroll-up'. Scope moves downward."
(interactive)
(or arg (setq arg 1))
(if (pixel-scroll-in-rush-p)
(scroll-up arg)
(dotimes (ii arg) ; move scope downward
(let ((amt (if pixel-resolution-fine-flag
(if (integerp pixel-resolution-fine-flag)
pixel-resolution-fine-flag
(frame-char-height))
(pixel-line-height))))
(if (pixel-eob-at-top-p) ; when end-of-the-buffer is close
(scroll-up 1) ; relay on robust method
(while (pixel-point-at-top-p amt) ; prevent too late (multi tries)
(vertical-motion 1)) ; move point downward
(pixel-scroll-pixel-up amt)))))) ; move scope downward
(defun pixel-scroll-down (&optional arg)
"Scroll text of selected window down ARG lines.
This is and alternative of `scroll-down'. Scope moves upward."
(interactive)
(or arg (setq arg 1))
(if (pixel-scroll-in-rush-p)
(scroll-down arg)
(dotimes (ii arg)
(let ((amt (if pixel-resolution-fine-flag
(if (integerp pixel-resolution-fine-flag)
pixel-resolution-fine-flag
(frame-char-height))
(pixel-line-height -1))))
(while (pixel-point-at-bottom-p amt) ; prevent too late (multi tries)
(vertical-motion -1)) ; move point upward
(if (or (pixel-bob-at-top-p amt) ; when beginning-of-the-buffer is seen
(pixel-eob-at-top-p)) ; for file with a long line
(scroll-down 1) ; relay on robust method
(pixel-scroll-pixel-down amt))))))
(defun pixel-bob-at-top-p (amt)
"Return non-nil if window-start is at beginning of the current buffer.
Window must be vertically scrolled by not more than AMT pixels."
(and (equal (window-start) (point-min))
(< (window-vscroll nil t) amt)))
(defun pixel-eob-at-top-p ()
"Return non-nil if end of buffer is at top of window."
(<= (count-lines (window-start) (window-end)) 2)) ; count-screen-lines
(defun pixel-posn-y-at-point ()
"Return y coordinates of point in pixels of current window.
This returns nil when horizontally scrolled."
(when (equal (window-hscroll) 0)
(save-excursion
;; When there's an overlay string on a line, move
;; point by (beginning-of-visual-line).
(beginning-of-visual-line)
;; (- (cadr (pos-visible-in-window-p (point) nil t))
;; (line-pixel-height))
(cdr (posn-x-y (posn-at-point))))))
(defun pixel-point-at-top-p (amt)
"Return if point is located at top of a window on coming scroll of AMT pixels.
When location of point was not obtained, this returns if point is at top
of window."
(let ((y (pixel-posn-y-at-point))
top-margin)
(cond
(y
(setq top-margin y)
(< top-margin amt))
(t
(<= (count-lines (window-start) (point)) 1)))))
(defun pixel-point-at-bottom-p (amt)
"Return if point is located at bottom of window on coming scroll of AMT pixels.
When location of point was not obtained, this returns nil."
(let* ((edges (window-inside-pixel-edges))
(height (- (nth 3 edges) (nth 1 edges))) ; (- bottom top)
(y (pixel-posn-y-at-point))
bottom-margin)
(when y
(setq bottom-margin (- height (+ y (pixel-visual-line-height))))
(< bottom-margin amt)))) ; coming unseen line
(defun pixel-scroll-pixel-up (amt)
"Scroll text of selected windows up AMT pixels.
Scope moves downward."
(while (>= (+ (window-vscroll nil t) amt)
(pixel-line-height))
(setq amt (- amt (pixel--whistlestop-line-up)))) ; major scroll
(pixel--whistlestop-pixel-up amt)) ; minor scroll
(defun pixel-scroll-pixel-down (amt)
"Scroll text of selected windows down AMT pixels.
Scope moves upward."
(while (> amt 0)
(let ((vs (window-vscroll nil t)))
(if (equal vs 0)
(progn
;; On horizontal scrolling, move cursor.
(when (> (window-hscroll) 0)
(vertical-motion -1))
(pixel-scroll-down-and-set-window-vscroll
(1- (pixel-line-height -1))))
(set-window-vscroll nil (1- vs) t))
(setq amt (1- amt))
(sit-for pixel-wait))))
(defun pixel--whistlestop-line-up ()
"Scroll text upward a line with each pixel whistlestopped.
When `vscroll' is non-zero, complete scrolling a line. When
`vscroll' is larger than height of multiple lines, for example
88, this flushes multiple lines. At the end, `vscroll' will be
zero. This assumes that the lines are with the same height.
Scope moves downward. This function returns number of pixels
that was scrolled."
(let* ((src (window-vscroll nil t)) ; EXAMPLE (initial) @0 @8 @88
(height (pixel-line-height)) ; 25 25 23
(line (1+ (/ src height))) ; catch up + one line 1 1 4
(dst (* line height)) ; goal @25 @25 @92
(delta (- dst src))) ; pixels to be scrolled 25 17 4
(pixel--whistlestop-pixel-up (1- delta)) ; until one less @24 @24 @91
(dotimes (ii line)
;; On horizontal scrolling, move cursor.
(when (> (window-hscroll) 0)
(vertical-motion 1))
(scroll-up 1))
(sit-for pixel-wait) ; scroll 1 pixel @0 @0 @0
delta))
(defun pixel--whistlestop-pixel-up (n)
"Scroll text upward by N pixels with each pixel whistlestopped.
Scope moves downward."
(when (> n 0)
(let ((vs0 (window-vscroll nil t)))
(dolist (vs (number-sequence (1+ vs0) (+ vs0 n)))
(set-window-vscroll nil vs t) (sit-for pixel-wait)))))
(defun pixel-line-height (&optional pos)
"Return height in pixels of text line at POS in the selected window.
When POS is nil or negative, height of the first line or the coming
unseen line above the first line, respectively, is provided."
(or pos (setq pos (window-start)))
(when (< pos 0)
(setq pos (pixel-point-at-unseen-line)))
(let ((vs1 (window-vscroll nil t))
height)
(set-window-vscroll nil 0 t)
(save-excursion
(goto-char pos)
(setq height (pixel-visual-line-height))) ; line-pixel-height, frame-char-height
(set-window-vscroll nil vs1 t)
height))
(defun pixel-visual-line-height ()
"Return height in pixels of text line where cursor is in the selected window."
(let ((pos (pixel-visible-pos-in-window)))
(cond
;; When a char of line is shown, obtain height by
;; (line-pixel-height).
(pos (save-excursion (goto-char pos) (line-pixel-height)))
;; When no char of line is shown but the line is at the top,
;; obtain height by (line-pixel-height). This is based on
;; expected response from display engine. See following
;; discussion.
;; https://lists.gnu.org/r/emacs-devel/2017-10/msg00621.html
((equal (count-lines (window-start) (point)) 1)
(line-pixel-height))
;; No char of line is shown and the line is not at the top,
;; obtain height by (frame-char-height).
(t (frame-char-height)))))
(defun pixel-visible-pos-in-window ()
"Return position shown on text line where cursor is in the selected window.
This will look for positions of point and end-of-visual-line,
then positions from beginning-of-visual-line to
end-of-visual-line. When no char in a line is shown, this
returns nil."
(let* ((beginning-of-visual-line-pos (save-excursion (beginning-of-visual-line) (point)))
(end-of-visual-line-pos (save-excursion (end-of-visual-line) (point)))
(pos-list (number-sequence beginning-of-visual-line-pos end-of-visual-line-pos))
(edges (window-inside-pixel-edges))
(width (- (nth 2 edges) (nth 0 edges)))
posn-x
visible-pos)
;; Optimize list of position to be surveyed.
(push end-of-visual-line-pos pos-list)
(push (point) pos-list)
(delete-dups pos-list)
;; Find out a char with position X that is more than zero and less
;; than width of screen.
(while (and (not visible-pos)
pos-list)
(setq posn-x (car (pos-visible-in-window-p (car pos-list) nil t)))
(if (and posn-x
(<= 0 posn-x)
(< posn-x width))
(setq visible-pos (car pos-list))
(setq pos-list (cdr pos-list))))
visible-pos))
(defun pixel-point-at-unseen-line ()
"Return the character position of line above the selected window.
The returned value is the position of the first character on the
unseen line just above the scope of current window."
(let* ((pos0 (window-start))
(vscroll0 (window-vscroll nil t))
(pos
(save-excursion
(goto-char pos0)
(if (bobp)
(point-min)
;; When there's an overlay string at window-start,
;; (beginning-of-visual-line 0) stays put.
(let ((ppos (point))
(tem (beginning-of-visual-line 0)))
(if (eq tem ppos)
(vertical-motion -1))
(point))))))
;; restore initial position
(set-window-start nil pos0 t)
(set-window-vscroll nil vscroll0 t)
pos))
(defun pixel-scroll-down-and-set-window-vscroll (vscroll)
"Scroll down a line and set VSCROLL in pixels.
It is important to call `set-window-start' to force the display
engine use that particular position as the window-start point.
Otherwise, redisplay will reset the window's vscroll."
(set-window-start nil (pixel-point-at-unseen-line) t)
(set-window-vscroll nil vscroll t))
(provide 'pixel-scroll)
;;; pixel-scroll.el ends here