Typically I have two windows, left and right, with the left window (call it the "editing window") showing a buffer in a prog-mode or text-mode and the right window (call it the "auxiliary window") showing something else: documentation, Magit,
make output, or just the start-up screen.
My font size and frame width are such that, if these windows' widths are balanced, the editing window is (by a small amount) not wide enough to show all its content out to its fill-column, while the auxiliary window has more width than it typically needs. I would like Emacs to keep the windows sized so that the editing window (or each window, for simplicity) has enough width to show its fill-column.
I have the following Elisp running in the
prog-mode-hook and the
(setq-local fit-window-to-buffer-horizontally t) (let (w (get-buffer-window)) (fit-window-to-buffer w nil nil nil (+ fill-column 8)) (window-preserve-size w t t)))
(The added 8 columns are to account for the fringes, line numbers, etc.)
If I start Emacs as
emacs <FILE>, such that it opens with a left window showing FILE and a right window showing the start-up screen, the above code does make the left window larger, such that its fill-column is visible. However, if I then open Magit or a help window, or run
make with Projectile, such that Magit or help or
make takes over the right window, or even if I open another file with
C-x C-f in the left window, the windows' widths become balanced, such that the left window may no longer be wide enough to show all of FILE (depending on its fill-column) and Magit or whatever has more width than it needs.
How can I make Emacs respect windows' buffers' fill-columns when resizing windows?
I have seen the window hooks, but those seemed promising only until I saw "Window change functions should not [...] change the [...] size [...] of any window".
window-min-width, but they, as I understand it, apply only to all windows, which is unsatisfactory, as the width I want varies by buffer. I tried
window-size-fixed, but, by fixing the height of the editing window, it prevented completions from appearing at the bottom of the frame.
split-windowparameter for windows you wish to have special behaviour? You could then use a window change hook to add or remove that parameter, in accordance with the buffer it was currently displaying. (Suggestion is entirely untested by me.)
*Messages*, which shows that it doesn't get called when I (for example) open Magit, which is as I expected: I didn't think any window was getting split; rather, the existing left window gets repurposed for Magit/help/
window-min-widthcan be helpful to control the minimum size vertically and horizontally. See also the variable
window-size-fixed. The doc-strings describe how those variables may be used.
window-size-fixeddefinitely looks useful here. Perhaps
display-buffer-alistcould be used to set the window width when displaying the buffer (see
C-h i g (elisp)Buffer Display Action Alists) and
window-size-fixedcould prevent other things from changing it?