justbur / emacs-which-key

Emacs package that displays available keybindings in popup
GNU General Public License v3.0
1.74k stars 87 forks source link

With (which-key-setup-side-window-right), width is slightly too small #225

Closed sjbach closed 4 years ago

sjbach commented 4 years ago

See #113 for context and a screenshot.

I believe there's a further interaction here, as even with an ASCII separator, which-key consistently under-reserves side window width by one column in terminal Emacs (for me).

This results from the missing fringe in the terminal: "... when fringes are not available, Emacs uses the leftmost and rightmost character cells to indicate continuation and truncation ... . This reduces the width available for displaying text on each line, because the character cells used for truncation and continuation indicators are reserved for that purpose."

And then in the docstring for window-body-width, which underlies fit-window-to-buffer (which is called by which-key--show-buffer-side-window): "Note that the returned value includes the column reserved for the continuation glyph."

So it sounds like window-body-width is kind-of misleading in a terminal, as its count includes the reserved $ glyph column even though content can't be shown there.

It's pretty simple to demonstrate this:

$ emacs -nw -Q

(setq fit-window-to-buffer-horizontally t)

C-h v emacs-copyright  ;; (or whatever)
C-x o
M-x toggle-truncate-lines
M-x fit-window-to-buffer

And note that the "fitted" window truncates at least one of the lines in *Help* by one column. I'm supposing the mis-fit of the which-key buffer is the same phenomenon.

sjbach commented 4 years ago

Possible workaround (hacky): when updating the which-key buffer, unless (display-graphic-p) append a trailing space to each line of the content to be printed, fit the buffer as usual, and override the display of the $ truncation glyph:

(set-display-table-slot buffer-display-table 'truncation ?\ )

The lines will still be truncated, but invisibly, and the missing characters will just be the padding that we added.

justbur commented 4 years ago

Try (setq which-key-allow-imprecise-window-fit t)

Like you suggest it has something to do with how emacs shrinks the buffer using fit-window-to-buffer, but I can't see what I'm doing wrong on the which-key side. Even if I try to trick which-key into making a bigger buffer, the fit-window-to-buffer routine undoes it and yields the same result.

sjbach commented 4 years ago

Yep, which-key-allow-imprecise-window-fit appears to be a better option for terminal Emacs.

I agree which-key is blameless (in spirit) - this is assuredly an Emacs wart. Just one that is unlikely to go away.

Thanks for your work, by the way. This is an excellent package.

justbur commented 4 years ago

In that case, I think I will make it the default in the terminal, since it doesn't sacrifice much.

Thanks!