From 23804c403b5a4ec7ba810a2fa7c1b084682d2cba Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Thu, 2 Jun 2022 21:12:04 -0700 Subject: [PATCH] Account for remapped faces in $COLUMNS and $LINES in Eshell * lisp/window.el (window-max-lines): New function. * lisp/eshell/esh-var.el (eshell-variable-aliases-list): Use it (and 'window-max-chars-per-line'). * lisp/eshell/em-ls.el (eshell-ls-find-column-lengths): Use 'window-max-chars-per-line'. * test/lisp/eshell/esh-var-tests.el (esh-var-test/window-height) (esh-var-test/window-width): Rename to... (esh-var-test/lines-var, esh-var-test/columns-var): ... and update expected value. * doc/lispref/windows.texi (Window Sizes): Document 'window-max-lines'. * etc/NEWS: Announce addition of 'window-max-lines' (bug#55696). --- doc/lispref/windows.texi | 29 ++++++++++++++++++++++------- etc/NEWS | 7 +++++++ lisp/eshell/em-ls.el | 2 +- lisp/eshell/esh-var.el | 4 ++-- lisp/window.el | 16 ++++++++++++++++ test/lisp/eshell/esh-var-tests.el | 16 +++++++++------- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index 0d285b2ad4..4d9515c029 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi @@ -907,15 +907,15 @@ Window Sizes described in the corresponding sections. If your Lisp program needs to make layout decisions, you will find the -following function useful: +following functions useful: @defun window-max-chars-per-line &optional window face -This function returns the number of characters displayed in the -specified face @var{face} in the specified window @var{window} (which -must be a live window). If @var{face} was remapped (@pxref{Face -Remapping}), the information is returned for the remapped face. If -omitted or @code{nil}, @var{face} defaults to the default face, and -@var{window} defaults to the selected window. +This function returns the number of characters can be displayed in the +specified face @var{face} in a single line in the specified window +@var{window} (which must be a live window). If @var{face} was +remapped (@pxref{Face Remapping}), the information is returned for the +remapped face. If omitted or @code{nil}, @var{face} defaults to the +default face, and @var{window} defaults to the selected window. Unlike @code{window-body-width}, this function accounts for the actual size of @var{face}'s font, instead of working in units of the canonical @@ -924,6 +924,21 @@ Window Sizes one or both of its fringes. @end defun +@defun window-max-lines &optional window face +This function returns the number of lines that can be displayed at +once in the specified face @var{face} in the specified window +@var{window} (which must be a live window). As with +@code{window-max-chars-per-line}, if @var{face} was remapped +(@pxref{Face Remapping}), the information is returned for the remapped +face. If omitted or @code{nil}, @var{face} defaults to the default +face, and @var{window} defaults to the selected window. + +Unlike @code{window-body-height}, this function accounts for the +actual size of @var{face}'s font, instead of working in units of the +canonical character width of @var{window}'s frame (@pxref{Frame +Font}). +@end defun + @cindex fixed-size window @vindex window-min-height @vindex window-min-width diff --git a/etc/NEWS b/etc/NEWS index 3870e937df..36fc33fd1a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2028,6 +2028,13 @@ The new 'x-show-tooltip-timeout' variable allows the user to alter this for packages that don't use 'tooltip-show', but instead call the lower level function directly. ++++ +** New function 'window-max-lines'. +This function returns the number of lines that can be displayed at +once in the specified face and window. Unlike 'window-body-height', +it accounts for the actual size of the text, and respect face +remapping. + --- ** New function 'current-cpu-time'. It gives access to the CPU time used by the Emacs process, for diff --git a/lisp/eshell/em-ls.el b/lisp/eshell/em-ls.el index 874591d250..fcd5f46c18 100644 --- a/lisp/eshell/em-ls.el +++ b/lisp/eshell/em-ls.el @@ -845,7 +845,7 @@ eshell-ls-find-column-lengths (lambda (file) (+ 2 (length (car file)))) files)) - (max-width (+ (window-width) 2)) + (max-width (+ (window-max-chars-per-line) 2)) col-widths colw) diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el index 186f6358bc..57db2249fb 100644 --- a/lisp/eshell/esh-var.el +++ b/lisp/eshell/esh-var.el @@ -149,8 +149,8 @@ eshell-variable-name-regexp (defcustom eshell-variable-aliases-list `(;; for eshell.el - ("COLUMNS" ,(lambda (_indices) (window-width)) t) - ("LINES" ,(lambda (_indices) (window-height)) t) + ("COLUMNS" ,(lambda (_indices) (window-max-chars-per-line)) t) + ("LINES" ,(lambda (_indices) (window-max-lines)) t) ;; for eshell-cmd.el ("_" ,(lambda (indices) diff --git a/lisp/window.el b/lisp/window.el index 5da867715f..f62640f209 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -2181,6 +2181,22 @@ window-max-chars-per-line ;; truncation glyphs, not one. (1- ncols))))) +(defun window-max-lines (&optional window face) + "Return the number of lines that can be displayed at once in WINDOW. +WINDOW must be a live window and defaults to the selected one. + +The character height of FACE is used for the calculation. If FACE +is nil or omitted, the default face is used. If FACE is +remapped (see `face-remapping-alist'), the function uses the +remapped face. + +This function is different from `window-body-height' in that it +accounts for the size of the font." + (with-selected-window (window-normalize-window window t) + (let ((window-height (window-body-height window t)) + (font-height (window-font-height window face))) + (/ window-height font-height)))) + (defun window-current-scroll-bars (&optional window) "Return the current scroll bar types for WINDOW. WINDOW must be a live window and defaults to the selected one. diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el index 4e2a18861e..a99624de03 100644 --- a/test/lisp/eshell/esh-var-tests.el +++ b/test/lisp/eshell/esh-var-tests.el @@ -469,13 +469,15 @@ esh-var-test/quoted-interp-convert-cmd-split-indices ;; Built-in variables -(ert-deftest esh-var-test/window-height () - "$LINES should equal (window-height)" - (should (eshell-test-command-result "= $LINES (window-height)"))) - -(ert-deftest esh-var-test/window-width () - "$COLUMNS should equal (window-width)" - (should (eshell-test-command-result "= $COLUMNS (window-width)"))) +(ert-deftest esh-var-test/lines-var () + "$LINES should equal (window-max-lines)" + (should (equal (eshell-test-command-result "echo $LINES") + (window-max-lines)))) + +(ert-deftest esh-var-test/columns-var () + "$COLUMNS should equal (window-max-chars-per-line)" + (should (equal (eshell-test-command-result "echo $COLUMNS") + (window-max-chars-per-line)))) (ert-deftest esh-var-test/last-result-var () "Test using the \"last result\" ($$) variable" -- 2.25.1