]> code.delx.au - gnu-emacs/blobdiff - lisp/follow.el
Add a new function `svg-embed'
[gnu-emacs] / lisp / follow.el
index 37de923e6a5d2c73dd0bd94d87778a395d838c8c..c510e5a848beeed12a9004cc135ebea7c4bd2d26 100644 (file)
@@ -1,6 +1,6 @@
 ;;; follow.el --- synchronize windows showing the same buffer
 
-;; Copyright (C) 1995-1997, 1999, 2001-2015 Free Software Foundation,
+;; Copyright (C) 1995-1997, 1999, 2001-2016 Free Software Foundation,
 ;; Inc.
 
 ;; Author: Anders Lindgren
@@ -399,11 +399,11 @@ virtual window.  This is accomplished by two main techniques:
   makes it possible to walk between windows using normal cursor
   movement commands.
 
-Follow mode comes to its prime when used on a large screen and two
-side-by-side windows are used.  The user can, with the help of Follow
-mode, use two full-height windows as though they would have been
-one.  Imagine yourself editing a large function, or section of text,
-and being able to use 144 lines instead of the normal 72... (your
+Follow mode comes to its prime when used on a large screen and two or
+more side-by-side windows are used.  The user can, with the help of
+Follow mode, use these full-height windows as though they were one.
+Imagine yourself editing a large function, or section of text, and
+being able to use 144 or 216 lines instead of the normal 72... (your
 mileage may vary).
 
 To split one large window into two side-by-side windows, the commands
@@ -421,7 +421,21 @@ Keys specific to Follow mode:
       (progn
        (add-hook 'compilation-filter-hook 'follow-align-compilation-windows t t)
        (add-hook 'post-command-hook 'follow-post-command-hook t)
-       (add-hook 'window-size-change-functions 'follow-window-size-change t))
+       (add-hook 'window-size-change-functions 'follow-window-size-change t)
+        (add-hook 'after-change-functions 'follow-after-change nil t)
+        (add-hook 'isearch-update-post-hook 'follow-post-command-hook nil t)
+        (add-hook 'replace-update-post-hook 'follow-post-command-hook nil t)
+        (add-hook 'ispell-update-post-hook 'follow-post-command-hook nil t)
+
+        (setq window-group-start-function 'follow-window-start)
+        (setq window-group-end-function 'follow-window-end)
+        (setq set-window-group-start-function 'follow-set-window-start)
+        (setq recenter-window-group-function 'follow-recenter)
+        (setq pos-visible-in-window-group-p-function
+              'follow-pos-visible-in-window-p)
+        (setq selected-window-group-function 'follow-all-followers)
+        (setq move-to-window-group-line-function 'follow-move-to-window-line))
+
     ;; Remove globally-installed hook functions only if there is no
     ;; other Follow mode buffer.
     (let ((buffers (buffer-list))
@@ -432,6 +446,19 @@ Keys specific to Follow mode:
       (unless following
        (remove-hook 'post-command-hook 'follow-post-command-hook)
        (remove-hook 'window-size-change-functions 'follow-window-size-change)))
+
+    (kill-local-variable 'move-to-window-group-line-function)
+    (kill-local-variable 'selected-window-group-function)
+    (kill-local-variable 'pos-visible-in-window-group-p-function)
+    (kill-local-variable 'recenter-window-group-function)
+    (kill-local-variable 'set-window-group-start-function)
+    (kill-local-variable 'window-group-end-function)
+    (kill-local-variable 'window-group-start-function)
+
+    (remove-hook 'ispell-update-post-hook 'follow-post-command-hook t)
+    (remove-hook 'replace-update-post-hook 'follow-post-command-hook t)
+    (remove-hook 'isearch-update-post-hook 'follow-post-command-hook t)
+    (remove-hook 'after-change-functions 'follow-after-change t)
     (remove-hook 'compilation-filter-hook 'follow-align-compilation-windows t)))
 
 (defun follow-find-file-hook ()
@@ -505,6 +532,80 @@ Return the new position."
 ;; position...  (This would also be corrected if we would have had a
 ;; good redisplay abstraction.)
 
+(defun follow-scroll-up-arg (arg)
+  "Scroll the text in a follow mode window chain up by ARG lines.
+If ARG is nil, scroll the size of the current window.
+
+This is an internal function for `follow-scroll-up' and
+`follow-scroll-up-window'."
+  (let ((opoint (point))  (owin (selected-window)))
+    (while
+        ;; If we are too near EOB, try scrolling the previous window.
+        (condition-case nil (progn (scroll-up arg) nil)
+          (end-of-buffer
+           (condition-case nil (progn (follow-previous-window) t)
+             (error
+              (select-window owin)
+              (goto-char opoint)
+              (signal 'end-of-buffer nil))))))
+    (unless (and scroll-preserve-screen-position
+                 (get this-command 'scroll-command))
+      (goto-char opoint))
+    (setq follow-fixed-window t)))
+
+(defun follow-scroll-down-arg (arg)
+  "Scroll the text in a follow mode window chain down by ARG lines.
+If ARG is nil, scroll the size of the current window.
+
+This is an internal function for `follow-scroll-down' and
+`follow-scroll-down-window'."
+  (let ((opoint (point)))
+    (scroll-down arg)
+    (unless (and scroll-preserve-screen-position
+                 (get this-command 'scroll-command))
+      (goto-char opoint))
+    (setq follow-fixed-window t)))
+
+;;;###autoload
+(defun follow-scroll-up-window (&optional arg)
+  "Scroll text in a Follow mode window up by that window's size.
+The other windows in the window chain will scroll synchronously.
+
+If called with no ARG, the `next-screen-context-lines' last lines of
+the window will be visible after the scroll.
+
+If called with an argument, scroll ARG lines up.
+Negative ARG means scroll downward.
+
+Works like `scroll-up' when not in Follow mode."
+  (interactive "P")
+  (cond ((not follow-mode)
+        (scroll-up arg))
+       ((eq arg '-)
+        (follow-scroll-down-window))
+       (t (follow-scroll-up-arg arg))))
+(put 'follow-scroll-up-window 'scroll-command t)
+
+;;;###autoload
+(defun follow-scroll-down-window (&optional arg)
+  "Scroll text in a Follow mode window down by that window's size.
+The other windows in the window chain will scroll synchronously.
+
+If called with no ARG, the `next-screen-context-lines' top lines of
+the window in the chain will be visible after the scroll.
+
+If called with an argument, scroll ARG lines down.
+Negative ARG means scroll upward.
+
+Works like `scroll-down' when not in Follow mode."
+  (interactive "P")
+  (cond ((not follow-mode)
+        (scroll-down arg))
+       ((eq arg '-)
+        (follow-scroll-up-window))
+       (t (follow-scroll-down-arg arg))))
+(put 'follow-scroll-down-window 'scroll-command t)
+
 ;;;###autoload
 (defun follow-scroll-up (&optional arg)
   "Scroll text in a Follow mode window chain up.
@@ -519,23 +620,18 @@ Works like `scroll-up' when not in Follow mode."
   (interactive "P")
   (cond ((not follow-mode)
         (scroll-up arg))
-       ((eq arg '-)
-        (follow-scroll-down))
-       (t
-        (let ((opoint (point))  (owin (selected-window)))
-          (while
-              ;; If we are too near EOB, try scrolling the previous window.
-              (condition-case nil (progn (scroll-up arg) nil)
-                (end-of-buffer
-                 (condition-case nil (progn (follow-previous-window) t)
-                   (error
-                    (select-window owin)
-                    (goto-char opoint)
-                    (signal 'end-of-buffer nil))))))
-          (unless (and scroll-preserve-screen-position
-                       (get this-command 'scroll-command))
-            (goto-char opoint))
-          (setq follow-fixed-window t)))))
+       (arg (follow-scroll-up-arg arg))
+        (t
+        (let* ((windows (follow-all-followers))
+               (end (window-end (car (reverse windows)))))
+          (if (eq end (point-max))
+              (signal 'end-of-buffer nil)
+            (select-window (car windows))
+            ;; `window-end' might return nil.
+            (if end
+                (goto-char end))
+            (vertical-motion (- next-screen-context-lines))
+            (set-window-start (car windows) (point)))))))
 (put 'follow-scroll-up 'scroll-command t)
 
 ;;;###autoload
@@ -552,15 +648,22 @@ Works like `scroll-down' when not in Follow mode."
   (interactive "P")
   (cond ((not follow-mode)
         (scroll-down arg))
-       ((eq arg '-)
-        (follow-scroll-up))
-       (t
-        (let ((opoint (point)))
-          (scroll-down arg)
-          (unless (and scroll-preserve-screen-position
-                       (get this-command 'scroll-command))
-            (goto-char opoint))
-          (setq follow-fixed-window t)))))
+       (arg (follow-scroll-down-arg arg))
+        (t
+        (let* ((windows (follow-all-followers))
+               (win (car (reverse windows)))
+               (start (window-start (car windows))))
+          (if (eq start (point-min))
+              (signal 'beginning-of-buffer nil)
+            (select-window win)
+            (goto-char start)
+            (vertical-motion (- (- (window-height win)
+                                   (if header-line-format 2 1)
+                                   next-screen-context-lines)))
+            (set-window-start win (point))
+            (goto-char start)
+            (vertical-motion (- next-screen-context-lines 1))
+            (setq follow-internal-force-redisplay t))))))
 (put 'follow-scroll-down 'scroll-command t)
 
 (declare-function comint-adjust-point "comint" (window))
@@ -1015,6 +1118,10 @@ Otherwise, return nil."
 ;; is nil.  Start every window directly after the end of the previous
 ;; window, to make sure long lines are displayed correctly.
 
+(defvar follow-start-end-invalid t
+  "When non-nil, indicates `follow-windows-start-end-cache' is invalid.")
+(make-variable-buffer-local 'follow-start-end-invalid)
+
 (defun follow-redisplay (&optional windows win preserve-win)
   "Reposition the WINDOWS around WIN.
 Should point be too close to the roof we redisplay everything
@@ -1047,7 +1154,8 @@ repositioning the other windows."
     (dolist (w windows)
       (unless (and preserve-win (eq w win))
        (set-window-start w start))
-      (setq start (car (follow-calc-win-end w))))))
+      (setq start (car (follow-calc-win-end w))))
+    (setq follow-start-end-invalid nil)))
 
 (defun follow-estimate-first-window-start (windows win start)
   "Estimate the position of the first window.
@@ -1443,6 +1551,146 @@ non-first windows in Follow mode."
 
 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t)
 
+;;; 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))))
+
 ;;; Profile support
 
 ;; The following (non-evaluated) section can be used to