+;;; Low level window start and end.
+
+;; These routines are the Follow Mode versions of the low level
+;; functions described on page "Window Start and End" of the elisp
+;; manual, e.g. `window-group-start'. The aim is to be able to handle
+;; Follow Mode windows by replacing `window-start' by
+;; `window-group-start', etc.
+
+(defun follow-after-change (_beg _end _old-len)
+ "After change function: set `follow-start-end-invalid'."
+ (setq follow-start-end-invalid t))
+
+(defun follow-window-start (&optional window)
+ "Return position at which display currently starts in the
+Follow Mode group of windows which includes WINDOW.
+
+WINDOW must be a live window and defaults to the selected one.
+This is updated by redisplay or by calling
+`follow-set-window-start'."
+ (let ((windows (follow-all-followers window)))
+ (window-start (car windows))))
+
+(defun follow-window-end (&optional window update)
+ "Return position at which display currently ends in the Follow
+ Mode group of windows which includes WINDOW.
+
+ WINDOW must be a live window and defaults to the selected one.
+ This is updated by redisplay, when it runs to completion.
+ Simply changing the buffer text or setting `window-start' does
+ not update this value.
+
+ Return nil if there is no recorded value. (This can happen if
+ the last redisplay of WINDOW was preempted, and did not
+ finish.) If UPDATE is non-nil, compute the up-to-date position
+ if it isn't already recorded."
+ (let* ((windows (follow-all-followers window))
+ (last (car (last windows))))
+ (when (and update follow-start-end-invalid)
+ (follow-redisplay windows (car windows)))
+ (window-end last update)))
+
+(defun follow-set-window-start (window pos &optional noforce)
+ "Make display in the Follow Mode group of windows which includes
+WINDOW start at position POS in WINDOW's buffer.
+
+WINDOW must be a live window and defaults to the selected one. Return
+POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
+overriding motion of point in order to display at this exact start."
+ (let ((windows (follow-all-followers window)))
+ (setq follow-start-end-invalid t)
+ (set-window-start (car windows) pos noforce)))
+
+(defun follow-pos-visible-in-window-p (&optional pos window partially)
+ "Return non-nil if position POS is currently on the frame in one of
+ the windows in the Follow Mode group which includes WINDOW.
+
+WINDOW must be a live window and defaults to the selected one.
+
+Return nil if that position is scrolled vertically out of view. If a
+character is only partially visible, nil is returned, unless the
+optional argument PARTIALLY is non-nil. If POS is only out of view
+because of horizontal scrolling, return non-nil. If POS is t, it
+specifies the position of the last visible glyph in WINDOW. POS
+defaults to point in WINDOW; WINDOW defaults to the selected window.
+
+If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
+the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
+where X and Y are the pixel coordinates relative to the top left corner
+of the actual window containing it. The remaining elements are
+omitted if the character after POS is fully visible; otherwise, RTOP
+and RBOT are the number of pixels off-window at the top and bottom of
+the screen line (\"row\") containing POS, ROWH is the visible height
+of that row, and VPOS is the row number \(zero-based)."
+ (let* ((windows (follow-all-followers window))
+ (last (car (last windows))))
+ (when follow-start-end-invalid
+ (follow-redisplay windows (car windows)))
+ (let* ((cache (follow-windows-start-end windows))
+ (last-elt (car (last cache)))
+ our-pos pertinent-elt)
+ (setq pertinent-elt
+ (if (eq pos t)
+ last-elt
+ (setq our-pos (or pos (point)))
+ (catch 'element
+ (while cache
+ (when (< our-pos (nth 2 (car cache)))
+ (throw 'element (car cache)))
+ (setq cache (cdr cache)))
+ last-elt)))
+ (pos-visible-in-window-p our-pos (car pertinent-elt) partially))))
+
+(defun follow-move-to-window-line (arg)
+ "Position point relative to the Follow mode group containing the selected window.
+ARG nil means position point at center of the window group.
+Else, ARG specifies vertical position within the window group;
+zero means top of the first window in the group, negative means
+ relative to bottom of the last window in the group."
+ (let* ((windows (follow-all-followers))
+ (start-end (follow-windows-start-end windows))
+ (rev-start-end (reverse start-end))
+ (lines 0)
+ middle-window elt count)
+ (select-window
+ (cond
+ ((null arg)
+ (setq rev-start-end (nthcdr (/ (length windows) 2) rev-start-end))
+ (prog1 (car (car rev-start-end))
+ (while (setq rev-start-end (cdr rev-start-end))
+ (setq elt (car rev-start-end)
+ count (count-screen-lines (cadr elt) (nth 2 elt)
+ nil (car elt))
+ lines (+ lines count)))))
+ ((>= arg 0)
+ (while (and (cdr start-end)
+ (progn
+ (setq elt (car start-end)
+ count (count-screen-lines (cadr elt) (nth 2 elt)
+ nil (car elt)))
+ (>= arg count)))
+ (setq arg (- arg count)
+ lines (+ lines count)
+ start-end (cdr start-end)))
+ (car (car start-end)))
+ (t ; (< arg 0)
+ (while (and (cadr rev-start-end)
+ (progn
+ (setq elt (car rev-start-end)
+ count (count-lines (cadr elt) (nth 2 elt)))
+ (<= arg (- count))))
+ (setq arg (+ arg count)
+ rev-start-end (cdr rev-start-end)))
+ (prog1 (car (car rev-start-end))
+ (while (setq rev-start-end (cdr rev-start-end))
+ (setq elt (car rev-start-end)
+ count (count-screen-lines (cadr elt) (nth 2 elt)
+ nil (car elt))
+ lines (+ lines count)))))))
+ (+ lines (move-to-window-line arg))))
+