diff options
| author | Titus von der Malsburg | 2015-03-21 12:31:29 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2015-03-21 12:31:29 +0200 |
| commit | 4a50af936e24b5f71df4079beb6dde82ed1955c2 (patch) | |
| tree | 820f0e83852d7bab9b050e4adaf38e8e928dacbc | |
| parent | 70565a21765ea5f9b163e1a0ef1331df03c7fc67 (diff) | |
| download | emacs-4a50af936e24b5f71df4079beb6dde82ed1955c2.tar.gz emacs-4a50af936e24b5f71df4079beb6dde82ed1955c2.zip | |
Add new functions for computing default font dimensions
lisp/window.el (window-font-width, window-font-height)
(window-max-chars-per-line): New functions.
lisp/simple.el (default-font-height): Doc fix.
(default-font-width): New function.
etc/NEWS: Mention `default-font-width', `window-font-height',
`window-font-width', and `window-max-chars-per-line'.
| -rw-r--r-- | etc/ChangeLog | 5 | ||||
| -rw-r--r-- | etc/NEWS | 18 | ||||
| -rw-r--r-- | lisp/ChangeLog | 8 | ||||
| -rw-r--r-- | lisp/simple.el | 24 | ||||
| -rw-r--r-- | lisp/window.el | 55 |
5 files changed, 109 insertions, 1 deletions
diff --git a/etc/ChangeLog b/etc/ChangeLog index cd5c54037c8..c94e12296ed 100644 --- a/etc/ChangeLog +++ b/etc/ChangeLog | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | 2015-03-21 Titus von der Malsburg <malsburg@posteo.de> | ||
| 2 | |||
| 3 | * NEWS: Mention `default-font-width', `window-font-height', | ||
| 4 | `window-font-width', and `window-max-chars-per-line'. | ||
| 5 | |||
| 1 | 2015-03-03 Kelvin White <kwhite@gnu.org> | 6 | 2015-03-03 Kelvin White <kwhite@gnu.org> |
| 2 | 7 | ||
| 3 | * NEWS.24: Add section to include ERC changes. | 8 | * NEWS.24: Add section to include ERC changes. |
| @@ -173,6 +173,24 @@ possible inaccuracies in the end position. | |||
| 173 | In particular, it now returns the average width of the font's | 173 | In particular, it now returns the average width of the font's |
| 174 | characters, which can be used for geometry-related calculations. | 174 | characters, which can be used for geometry-related calculations. |
| 175 | 175 | ||
| 176 | ** A new function `default-font-width' returns the average width of a | ||
| 177 | character in the current buffer's default font. If the default face | ||
| 178 | is remapped (see `face-remapping-alist'), the value for the remapped | ||
| 179 | face is returned. This function complements the existing function | ||
| 180 | `default-font-height'. | ||
| 181 | |||
| 182 | ** New functions `window-font-height' and `window-font-width' return | ||
| 183 | the height and average width of characters in a specified face and | ||
| 184 | window. If FACE is remapped (see `face-remapping-alist'), the | ||
| 185 | function returns the information for the remapped face. | ||
| 186 | |||
| 187 | ** A new function `window-max-chars-per-line' returns the maximal | ||
| 188 | number of characters that can be displayed on one line. If a face | ||
| 189 | and/or window are provided, these values are used for the | ||
| 190 | calculation. This function is different from `window-body-width' in | ||
| 191 | that it accounts for (i) continuation glyphs, (ii) the size of the | ||
| 192 | font, and (iii) the specified window. | ||
| 193 | |||
| 176 | 194 | ||
| 177 | * Editing Changes in Emacs 25.1 | 195 | * Editing Changes in Emacs 25.1 |
| 178 | 196 | ||
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 1cfefaa39df..2f9c430e45d 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,11 @@ | |||
| 1 | 2015-03-21 Titus von der Malsburg <malsburg@posteo.de> | ||
| 2 | |||
| 3 | * window.el (window-font-width, window-font-height) | ||
| 4 | (window-max-chars-per-line): New functions. | ||
| 5 | |||
| 6 | * simple.el (default-font-height): Doc fix. | ||
| 7 | (default-font-width): New function. | ||
| 8 | |||
| 1 | 2015-03-21 Tassilo Horn <tsdh@gnu.org> | 9 | 2015-03-21 Tassilo Horn <tsdh@gnu.org> |
| 2 | 10 | ||
| 3 | * emacs-lisp/lisp-mode.el (lisp-el-font-lock-keywords-1): Also | 11 | * emacs-lisp/lisp-mode.el (lisp-el-font-lock-keywords-1): Also |
diff --git a/lisp/simple.el b/lisp/simple.el index 98188a07b6f..f7f35564f35 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -5409,7 +5409,10 @@ lines." | |||
| 5409 | (declare-function font-info "font.c" (name &optional frame)) | 5409 | (declare-function font-info "font.c" (name &optional frame)) |
| 5410 | 5410 | ||
| 5411 | (defun default-font-height () | 5411 | (defun default-font-height () |
| 5412 | "Return the height in pixels of the current buffer's default face font." | 5412 | "Return the height in pixels of the current buffer's default face font. |
| 5413 | |||
| 5414 | If the default font is remapped (see `face-remapping-alist'), the | ||
| 5415 | function returns the height of the remapped face." | ||
| 5413 | (let ((default-font (face-font 'default))) | 5416 | (let ((default-font (face-font 'default))) |
| 5414 | (cond | 5417 | (cond |
| 5415 | ((and (display-multi-font-p) | 5418 | ((and (display-multi-font-p) |
| @@ -5420,6 +5423,25 @@ lines." | |||
| 5420 | (aref (font-info default-font) 3)) | 5423 | (aref (font-info default-font) 3)) |
| 5421 | (t (frame-char-height))))) | 5424 | (t (frame-char-height))))) |
| 5422 | 5425 | ||
| 5426 | (defun default-font-width () | ||
| 5427 | "Return the width in pixels of the current buffer's default face font. | ||
| 5428 | |||
| 5429 | If the default font is remapped (see `face-remapping-alist'), the | ||
| 5430 | function returns the width of the remapped face." | ||
| 5431 | (let ((default-font (face-font 'default))) | ||
| 5432 | (cond | ||
| 5433 | ((and (display-multi-font-p) | ||
| 5434 | ;; Avoid calling font-info if the frame's default font was | ||
| 5435 | ;; not changed since the frame was created. That's because | ||
| 5436 | ;; font-info is expensive for some fonts, see bug #14838. | ||
| 5437 | (not (string= (frame-parameter nil 'font) default-font))) | ||
| 5438 | (let* ((info (font-info (face-font 'default))) | ||
| 5439 | (width (aref info 11))) | ||
| 5440 | (if (> width 0) | ||
| 5441 | width | ||
| 5442 | (aref info 10)))) | ||
| 5443 | (t (frame-char-width))))) | ||
| 5444 | |||
| 5423 | (defun default-line-height () | 5445 | (defun default-line-height () |
| 5424 | "Return the pixel height of current buffer's default-face text line. | 5446 | "Return the pixel height of current buffer's default-face text line. |
| 5425 | 5447 | ||
diff --git a/lisp/window.el b/lisp/window.el index cc8a7fc402f..d17605099a0 100644 --- a/lisp/window.el +++ b/lisp/window.el | |||
| @@ -1835,6 +1835,61 @@ optional argument PIXELWISE is passed to the functions." | |||
| 1835 | (window-body-width window pixelwise) | 1835 | (window-body-width window pixelwise) |
| 1836 | (window-body-height window pixelwise))) | 1836 | (window-body-height window pixelwise))) |
| 1837 | 1837 | ||
| 1838 | (defun window-font-width (&optional window face) | ||
| 1839 | "Return average character width for the font of FACE used in WINDOW. | ||
| 1840 | WINDOW must be a live window and defaults to the selected one. | ||
| 1841 | |||
| 1842 | If FACE is nil or omitted, the default face is used. If FACE is | ||
| 1843 | remapped (see `face-remapping-alist'), the function returns the | ||
| 1844 | information for the remapped face." | ||
| 1845 | (with-selected-window (window-normalize-window window t) | ||
| 1846 | (if (display-multi-font-p) | ||
| 1847 | (let* ((face (if face face 'default)) | ||
| 1848 | (info (font-info (face-font face))) | ||
| 1849 | (width (aref info 11))) | ||
| 1850 | (if (> width 0) | ||
| 1851 | width | ||
| 1852 | (aref info 10))) | ||
| 1853 | (frame-char-width)))) | ||
| 1854 | |||
| 1855 | (defun window-font-height (&optional window face) | ||
| 1856 | "Return character height for the font of FACE used in WINDOW. | ||
| 1857 | WINDOW must be a live window and defaults to the selected one. | ||
| 1858 | |||
| 1859 | If FACE is nil or omitted, the default face is used. If FACE is | ||
| 1860 | remapped (see `face-remapping-alist'), the function returns the | ||
| 1861 | information for the remapped face." | ||
| 1862 | (with-selected-window (window-normalize-window window t) | ||
| 1863 | (if (display-multi-font-p) | ||
| 1864 | (let* ((face (if face face 'default)) | ||
| 1865 | (info (font-info (face-font face)))) | ||
| 1866 | (aref info 3)) | ||
| 1867 | (frame-char-height)))) | ||
| 1868 | |||
| 1869 | (defun window-max-chars-per-line (&optional window face) | ||
| 1870 | "Return the number of characters that can be displayed on one line in WINDOW. | ||
| 1871 | WINDOW must be a live window and defaults to the selected one. | ||
| 1872 | |||
| 1873 | The character width of FACE is used for the calculation. If FACE | ||
| 1874 | is nil or omitted, the default face is used. If FACE is | ||
| 1875 | remapped (see `face-remapping-alist'), the function uses the | ||
| 1876 | remapped face. | ||
| 1877 | |||
| 1878 | This function is different from `window-body-width' in two | ||
| 1879 | ways. First, it accounts for the portions of the line reserved | ||
| 1880 | for the continuation glyph. Second, it accounts for the size of | ||
| 1881 | the font." | ||
| 1882 | (with-selected-window (window-normalize-window window t) | ||
| 1883 | (let* ((window-width (window-body-width window t)) | ||
| 1884 | (font-width (window-font-width window face)) | ||
| 1885 | (ncols (/ window-width font-width))) | ||
| 1886 | (if (and (display-graphic-p) | ||
| 1887 | overflow-newline-into-fringe | ||
| 1888 | (/= (frame-parameter nil 'left-fringe) 0) | ||
| 1889 | (/= (frame-parameter nil 'right-fringe) 0)) | ||
| 1890 | ncols | ||
| 1891 | (1- ncols))))) | ||
| 1892 | |||
| 1838 | (defun window-current-scroll-bars (&optional window) | 1893 | (defun window-current-scroll-bars (&optional window) |
| 1839 | "Return the current scroll bar types for WINDOW. | 1894 | "Return the current scroll bar types for WINDOW. |
| 1840 | WINDOW must be a live window and defaults to the selected one. | 1895 | WINDOW must be a live window and defaults to the selected one. |