fling adds kinetic scrolling to Emacs on touch screens. Swipe and lift your finger, and the text keeps moving, then slows to a stop. It was written for the Android port of Emacs, where the screen otherwise stops dead when the finger lifts.
Emacs’s own kinetic scrolling in pixel-scroll was built for touchpads. On a phone it feels slow and floaty: it averages the whole gesture, slows down linearly for a fixed 1.75 seconds, and moves a fixed distance per timer tick that takes longer than intended when redisplay is slow.
fling instead:
- takes the release speed from the last tenth of a second, so a pause before lifting gives no fling;
- times the swipe by when each touch happened, not when Emacs read it, so a swipe that queued up behind a stall does not fling too fast;
- slows down exponentially, so a hard flick travels further than a gentle one;
- glides sideways too when the swipe scrolled the window sideways, so a diagonal swipe glides diagonally, stopping at the start of lines or the end of the widest visible line;
- scrolls sideways by pixels rather than whole columns wherever touch scrolls sideways, by scrolling one column too far and pushing the text back with a blank
line-prefixnarrower than a column; - scrolls vertically line by line in a window scrolled sideways, where
pixel-scrollpicks the wrong window start and can jump to the end of the buffer; - moves by speed times the real time since the last frame, so a slow frame never slows the motion;
- stops on any input, and at either end of the buffer;
- treats a touch that stops a fling as a scroll, so catching the text never selects a word, drags the region, or moves point;
- delays a long press until Emacs has read any queued touch movement, so a stall cannot turn a swipe into a selection;
- defers garbage collection from the first touch until Emacs has been idle for
fling-gc-idle-delayseconds, since scrolling on a phone otherwise collects every few seconds and each collection stalls for a tenth of a second.
Requires Emacs 30.1 or newer. With Emacs’s built-in VC package manager:
(package-vc-install "https://github.com/suderman/fling")Alternatively, clone this repository and add its root to load-path. Then enable the mode:
(require 'fling)
(fling-mode 1)With use-package:
(use-package fling
:vc (:url "https://github.com/suderman/fling" :rev :newest)
:if (featurep 'android)
:config (fling-mode 1))Pixel-precise touch scrolling makes the drag and the fling match:
(setq touch-screen-precision-scroll t)fling-time-constant(default 0.35) is the number of seconds for a fling to lose about two thirds of its speed. A fling travels about its release speed times this value. Raise it for longer glides.fling-min-velocity(default 300) is the release speed, in pixels per second, below which a swipe does not fling.fling-gc-idle-delay(default 2) is the number of idle seconds after touch scrolling before Emacs collects garbage. Until then, scrolling may allocate up to 1 GB before Emacs collects anyway.
fling-scroll scrolls a window at a given speed and can be called directly.
While a window rests between columns, a sliver up to one column wide at the left edge shows blank instead of part of a character. Another command that scrolls the window sideways drops the prefix and leaves it on a whole column. Lines with their own line-prefix text property get a shifted copy of it instead of the blank prefix; an :align-to stretch, as Dashboard uses to center lines, has its target moved.
touch-screen.el has no public hook for touch scrolling. fling-mode advises its internal touch-screen-handle-scroll, touch-screen-handle-touch, and touch-screen-handle-timeout, as well as pixel-scroll-precision-scroll-down-page and pixel-scroll-precision-scroll-up-page, and reads and adjusts touch-screen-current-tool and touch-screen-current-timer. Check compatibility when upgrading Emacs. Disabling the mode removes the advice and any sub-column prefix.
Run the standalone ERT suite from this repository:
emacs --batch -L . -l test/fling-test.el -f ert-run-tests-batch-and-exitThe tests use a simulated clock and need no touch screen or personal configuration.
GPL-3.0-or-later. See LICENSE.