summaryrefslogtreecommitdiff
path: root/lisp/window.el
diff options
context:
space:
mode:
authorTitus von der Malsburg <malsburg@posteo.de>2022-04-29 15:14:09 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2022-04-29 15:14:09 +0200
commit91418d27e9c528fd9062c74a4ce58b657366a8c5 (patch)
treea3086a3164d75c103ac812c34dd1f7e114e09450 /lisp/window.el
parentfa52782f5c4eaef7138534766dfc8a29465785b2 (diff)
downloademacs-91418d27e9c528fd9062c74a4ce58b657366a8c5.tar.gz
emacs-91418d27e9c528fd9062c74a4ce58b657366a8c5.tar.bz2
emacs-91418d27e9c528fd9062c74a4ce58b657366a8c5.zip
Add new functions for computing character metrics for windows
* doc/lispref/display.texi (Size of Displayed Text): Document the char functions. * doc/lispref/windows.texi (Window Sizes): Document window-max-characters-per-line. * lisp/window.el (window-char-pixel-width) (window-char-pixel-height) (window-max-characters-per-line): New functions (bug#19395).
Diffstat (limited to 'lisp/window.el')
-rw-r--r--lisp/window.el52
1 files changed, 52 insertions, 0 deletions
diff --git a/lisp/window.el b/lisp/window.el
index dc33eb8a12a..bb4d51da5f5 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -10480,6 +10480,58 @@ displaying that processes's buffer."
(put 'shrink-window-horizontally 'repeat-map 'resize-window-repeat-map)
(put 'shrink-window 'repeat-map 'resize-window-repeat-map)
+(defun window-char-pixel-width (&optional window face)
+ "Return average character width for the font of FACE used in WINDOW.
+WINDOW must be a live window and defaults to the selected one.
+
+If FACE is nil or omitted, the default face is used. If FACE is
+remapped (see `face-remapping-alist'), the function returns the
+information for the remapped face."
+ (with-selected-window (window-normalize-window window t)
+ (let* ((face (if face face 'default))
+ (info (font-info (face-font face)))
+ (width (aref info 11)))
+ (if (> width 0)
+ width
+ (aref info 10)))))
+
+(defun window-char-pixel-height (&optional window face)
+ "Return character height for the font of FACE used in WINDOW.
+WINDOW must be a live window and defaults to the selected one.
+
+If FACE is nil or omitted, the default face is used. If FACE is
+remapped (see `face-remapping-alist'), the function returns the
+information for the remapped face."
+ (with-selected-window (window-normalize-window window t)
+ (let* ((face (if face face 'default))
+ (info (font-info (face-font face))))
+ (aref info 3))))
+
+(defun window-max-characters-per-line (&optional window face)
+ "Return the number of characters that can be displayed on one line in WINDOW.
+WINDOW must be a live window and defaults to the selected one.
+
+The character width 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-width' in two
+ways. First, it accounts for the portions of the line reserved
+for the continuation glyph. Second, it accounts for the size of
+the font, which may have been adjusted, e.g., using
+`text-scale-increase')."
+ (with-selected-window (window-normalize-window window t)
+ (let* ((window-width (window-body-width window t))
+ (font-width (window-char-pixel-width window face))
+ (ncols (/ window-width font-width)))
+ (if (and (display-graphic-p)
+ overflow-newline-into-fringe
+ (/= (frame-parameter nil 'left-fringe) 0)
+ (/= (frame-parameter nil 'right-fringe) 0))
+ ncols
+ (1- ncols)))))
+
(provide 'window)
;;; window.el ends here