Subject: bug#19576: write-file writes the wrong buffer

From: Anders Lindgren <[email-masked]>
Date: Tue, 17 Nov 2015 22:15:07 +0100

Hi Everybody!

(I took the liberty to add Eli to this as well, as this involves the
display engine.)

Meanwhile, I'm taking an opportunity to ask you about your intriguing
> comment
> in follow.el:
>
> ;; Almost like the real thing, except when the cursor ends up outside
> ;; the top or bottom...  In our case however, we end up outside the
> ;; window and hence we are recentered.  Should we let `recenter' handle
> ;; the point position we would never leave the selected window.  To do
> ;; it ourselves we would need to do our own redisplay, which is easier
> ;; said than done.  (Why didn't I do a real display abstraction from
> ;; the beginning?)
>
> What a real display abstraction would you create if you designed
> follow-mode today?
>

Unfortunately, I haven't got a clue what my twenty-years younger me was
thinking when writing this...

However, here are some thoughts on follow-mode I have accumulated over the
years:

* The core of follow-mode call `redisplay' and does all sorts of tricks to
keep up the illusion that a number of windows form a very tall windows. I
would like to do some investigations if this can be done more efficiently.
Keith David Bershatsky (a.k.a. lawlist) has analyzed under which
circumstances Emacs calls the different hooks and functions using a tool he
named `test-mode'. Maybe this information can be used to write a better
implementation of follow-mode. (Of course, one alternative would be to
implement this in the Emacs display engine, but I don't see that happening
anytime soon.)

* The Emacs display engine tries to recenter windows when window-start ==
buffer-end, to ensure that no window would appear empty. When follow-mode
is enabled, this is not what we want (except for the leftmost window). To
counter the recentering efforts of Emacs, follow-mode sets window-start
whenever it gets a chance. I would prefer if it was possible to tell the
display engine to leave some windows alone, preferably with some kind of
buffer-local `recenter-window-functions' variable. It could be made generic
so that the return value would be an integer telling Emacs to show this
many lines (if positive) or place the last line X lines from the bottom
(when negative). If such hook existed, follow-mode could return 0 for all
(except the leftmost) windows. Note that the user can change the content of
any window at any time, so this property isn't static.

* Make it easier for other packages to use it. By this I mean packages that
display information, like "grep", should be able to use follow mode to
display a buffer using side by side windows. Concretely, in
font-lock-studio (a debugger for font-lock keywords I wrote a while ago) I
do this, but I had to call `follow-post-command-hook' explicitly. Follow
mode should need a better interface for this, alternatively this should be
built into `display-buffer' so that it happens automatically.

* When follow-mode is enabled, there is a noticeable lag when updating the
region. You can see this by pressing C-c SPACE and holding the up or down
key. When follow-mode is disabled, the region is updated as fast as the
cursor moves, when enabled, it is updated in chunks.

* When written in the Emacs old era, the region was always highlighted in
all windows. Follow-mode went to great lengths to place the cursor in the
non-selected windows so that the region would look good. Today, one could
get the same effect by setting `highlight-nonselected-windows' (except that
the region is visible on the last line of windows to the left of the
selected window.)  I would love to give the user the option to 1)
automatically set this variable and move the cursor in the non-selected
windows or 2) don't move the cursors at all. (Alternatively, invent a new
way to highlight the region of the selected windows in the other windows,
without the cursor in the non-selected windows affecting it.)

* isearch: If I recall correctly, isearch used to worked across follow-mode
windows. Of course, this was before isearch highlighted matches etc. It
would be great if this could be restored once again!

* Process output: In early versions of follow-mode, it could be used with
any process. This was accomplished using `defadvice' on a handful of
process-related functions. At some point in time, this system was replaced
with a system specific to comint and compilation buffers -- as part of the
great defadvice sweep. Personally, I would like to Emacs to provide
`pre-process-output-functions' and `post-process-output-functions',
allowing packages like follow-mode to perform whatever action they would
like to the output of any process.

* If a user have columns with different widths, follow-mode can't correctly
display long lines stretching from one window to the next. The reason for
this is that the start position can't be placed at an arbitrary location on
a long line, only on positions that are a multiple of the column width. I
don't see any way to solve this without modifying the display engine.

* Automatic tests: These should verify the basic functionality, that the
windows are aligned properly. That moving the point down in one window
would move it to the next. All follow-mode-specific commands etc.

Anyway, this is a dire wish-list and I feel that I, unfortunately,
presently can't contribute to it. When it comes to Emacs responsibilities,
I have taken over the NextStep port after Jan Djärvs resignation, and on a
personal level I have a full time job and I have two small children and a
third due in about a week (if all goes well). Anyway, I'm glad that Alan
has taken this bull by the horn. However, feel free to ask questions or
discuss implementation ideas regarding this.

Sincerely,
    Anders Lindgren