Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update minibuffer height according to text height #154

Merged
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ The format is based on [Keep a Changelog].
merged lines ([#133]).

### Bugs fixed
* The mininbuffer height is now determined by the actual height of
displayed candidates. Previously the height could be off for
candidates containing unicode characters or other means which
changed the display height such as `line-spacing` ([#146], [#151],
[#154]).
* When passing a named function or compiled lambda as `candidates`
argument to `selectrum-read` an error would be thrown, which has
been fixed ([#163]).
Expand Down Expand Up @@ -138,7 +143,10 @@ The format is based on [Keep a Changelog].
[#133]: https://github.com/raxod502/selectrum/pull/133
[#138]: https://github.com/raxod502/selectrum/pull/138
[#140]: https://github.com/raxod502/selectrum/pull/140
[#146]: https://github.com/raxod502/selectrum/issues/146
[#151]: https://github.com/raxod502/selectrum/issues/151
[#152]: https://github.com/raxod502/selectrum/pull/152
[#154]: https://github.com/raxod502/selectrum/pull/154
[#157]: https://github.com/raxod502/selectrum/issues/157
[#159]: https://github.com/raxod502/selectrum/issues/159
[#160]: https://github.com/raxod502/selectrum/pull/160
Expand Down
44 changes: 36 additions & 8 deletions selectrum.el
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,6 @@ PRED defaults to `minibuffer-completion-predicate'."
(setq displayed-candidates
(seq-take displayed-candidates
selectrum-num-candidates-displayed))
(let ((n (1+ (if selectrum-fix-minibuffer-height
selectrum-num-candidates-displayed
(max (1- (window-height)) ; grow only
(length displayed-candidates))))))
(setf (window-height) n))
(let ((text (selectrum--candidates-display-string
displayed-candidates
input
Expand Down Expand Up @@ -791,13 +786,45 @@ PRED defaults to `minibuffer-completion-predicate'."
(point-max) (point-max) (current-buffer))
(setq text (concat (or default " ") text))
(put-text-property 0 1 'cursor t text)
(overlay-put selectrum--candidates-overlay 'after-string text)))
(overlay-put selectrum--candidates-overlay 'after-string text))
(selectrum--update-minibuffer-height first-index-displayed
highlighted-index
displayed-candidates))
(setq selectrum--end-of-input-marker (set-marker (make-marker) bound))
(set-marker-insertion-type selectrum--end-of-input-marker t)
(when keep-mark-active
(setq deactivate-mark nil))
(setq-local selectrum--init-p nil)))

(defun selectrum--update-minibuffer-height (first highlighted cands)
"Set minibuffer height for candidates display.
FIRST is the index of the first displayed candidate. HIGHLIGHTED
is the index if the highlighted candidate. CANDS are the
currently displayed candidates."
(when-let ((n (1+ selectrum-num-candidates-displayed))
(win (active-minibuffer-window)))
;; Don't attempt to resize a minibuffer frame.
(unless (frame-root-window-p win)
;; Set min initial height.
(when (and selectrum-fix-minibuffer-height
selectrum--init-p)
(with-selected-window win
(setf (window-height) n)))
;; Adjust if needed.
(when (or selectrum--init-p
(and selectrum--current-candidate-index
;; Allow size change when navigating, not while
;; typing.
(/= first highlighted)
;; Don't allow shrinking.
(= (length cands)
selectrum-num-candidates-displayed)))
(let ((dheight (cdr (window-text-pixel-size win)))
(wheight (window-pixel-height win)))
(when (/= dheight wheight)
(window-resize
win (- dheight wheight) nil nil 'pixelwise)))))))

(defun selectrum--first-lines (candidates)
"Return list of single line CANDIDATES.
Multiline canidates are merged into a single line."
Expand Down Expand Up @@ -964,8 +991,9 @@ into the user input area to start with."
(add-hook
'minibuffer-exit-hook #'selectrum--minibuffer-exit-hook nil 'local)
(setq-local selectrum--init-p t)
(setq selectrum--candidates-overlay
(make-overlay (point) (point) nil 'front-advance 'rear-advance))
(unless selectrum--candidates-overlay
(setq selectrum--candidates-overlay
(make-overlay (point) (point) nil 'front-advance 'rear-advance)))
(setq selectrum--start-of-input-marker (point-marker))
(if selectrum--repeat
(insert selectrum--previous-input-string)
Expand Down