Next: , Previous: , Up: Display   [Contents][Index]


37.10 Size of Displayed Text

Since not all characters have the same width, these functions let you check the width of a character. See Primitive Indent, and Screen Lines, for related functions.

Function: char-width char

This function returns the width in columns of the character char, if it were displayed in the current buffer (i.e., taking into account the buffer’s display table, if any; see Display Tables). The width of a tab character is usually tab-width (see Usual Display).

Function: string-width string

This function returns the width in columns of the string string, if it were displayed in the current buffer and the selected window.

Function: truncate-string-to-width string width &optional start-column padding ellipsis

This function returns the part of string that fits within width columns, as a new string.

If string does not reach width, then the result ends where string ends. If one multi-column character in string extends across the column width, that character is not included in the result. Thus, the result can fall short of width but cannot go beyond it.

The optional argument start-column specifies the starting column. If this is non-nil, then the first start-column columns of the string are omitted from the value. If one multi-column character in string extends across the column start-column, that character is not included.

The optional argument padding, if non-nil, is a padding character added at the beginning and end of the result string, to extend it to exactly width columns. The padding character is used at the end of the result if it falls short of width. It is also used at the beginning of the result if one multi-column character in string extends across the column start-column.

If ellipsis is non-nil, it should be a string which will replace the end of string (including any padding) if it extends beyond width, unless the display width of string is equal to or less than the display width of ellipsis. If ellipsis is non-nil and not a string, it stands for "...".

(truncate-string-to-width "\tab\t" 12 4)
     ⇒ "ab"
(truncate-string-to-width "\tab\t" 12 4 ?\s)
     ⇒ "    ab  "

The following function returns the size in pixels of text as if it were displayed in a given window. This function is used by fit-window-to-buffer (see Resizing Windows) and fit-frame-to-buffer (see Size and Position) to make a window exactly as large as the text it contains.

Function: window-text-pixel-size &optional window from to x-limit y-limit mode-and-header-line

This function returns the size of the text of window’s buffer in pixels. window must be a live window and defaults to the selected one. The return value is a cons of the maximum pixel-width of any text line and the maximum pixel-height of all text lines.

The optional argument from, if non-nil, specifies the first text position to consider and defaults to the minimum accessible position of the buffer. If from is t, it uses the minimum accessible position that is not a newline character. The optional argument to, if non-nil, specifies the last text position to consider and defaults to the maximum accessible position of the buffer. If to is t, it uses the maximum accessible position that is not a newline character.

The optional argument x-limit, if non-nil, specifies the maximum pixel-width that can be returned. x-limit nil or omitted, means to use the pixel-width of window’s body (see Window Sizes); this is useful when the caller does not intend to change the width of window. Otherwise, the caller should specify here the maximum width window’s body may assume. Text whose x-coordinate is beyond x-limit is ignored. Since calculating the width of long lines can take some time, it’s always a good idea to make this argument as small as needed; in particular, if the buffer might contain long lines that will be truncated anyway.

The optional argument y-limit, if non-nil, specifies the maximum pixel-height that can be returned. Text lines whose y-coordinate is beyond y-limit are ignored. Since calculating the pixel-height of a large buffer can take some time, it makes sense to specify this argument; in particular, if the caller does not know the size of the buffer.

The optional argument mode-and-header-line nil or omitted means to not include the height of the mode- or header-line of window in the return value. If it is either the symbol mode-line or header-line, include only the height of that line, if present, in the return value. If it is t, include the height of both, if present, in the return value.


Next: , Previous: , Up: Display   [Contents][Index]