On 2014-12-17 Wed 11:09, Eli Zaretskii wrote: >> From: Titus von der Malsburg >> Cc: 19395@debbugs.gnu.org >> Date: Wed, 17 Dec 2014 10:48:25 -0800 >> >> The problem is that there may not be a clean solution to that >> problem. A buffer can contain text is several font sizes and as far as >> I know there is no notion of a default size for a buffer, or is there? > > Yes, there is: the 'default' face. > >> In my special case, I have the font size under control and >> `term-window-width' would be good enough. > > But if we want this function to be more generally useful, it > shouldn't be limited to the frame's canonical character size, and > should at least take the face-remapping into account. Bonus points > for accepting a face as an argument and using that face's font > dimensions. This is more difficult than I thought. Below is a first sketch. Let me know if you think this is going in the right direction and I'll polish it and add the bonus feature. It appears that a font has to be rendered before Emacs can tell how wide a character is. That's why we need the temporary buffer. Not elegant, but I couldn't find a better way. `default-font-width' complements `default-font-height' in simple.el. The other function would go into window.el. Titus (defun default-font-width () "Return the width in pixels of the current buffer's default face font. More precisely, this returns the width of the letter ‘m’. If the font is mono-spaced, this will also be the width of all other printable characters." (let ((window (selected-window)) (remapping face-remapping-alist)) (with-temp-buffer (make-local-variable 'face-remapping-alist) (setq face-remapping-alist remapping) (set-window-buffer window (current-buffer)) (insert "m") (aref (aref (font-get-glyphs (font-at 1) 1 2) 0) 4)))) (defun window-available-columns () "Return the maximal number of characters that can be displayed on one line. This function is different from `window-body-width' in that it accounts for fringes (when at least one fringe has zero width, one column is reserved for continuation characters) and for the size of the default font (which may have been adjusted using, e.g., `text-scale-increase')." (let* ((window-width (window-body-width nil t)) (font-width (default-font-width)) (ncols (/ window-width font-width))) (if (and (not (featurep 'xemacs)) (display-graphic-p) overflow-newline-into-fringe (/= (frame-parameter nil 'left-fringe) 0) (/= (frame-parameter nil 'right-fringe) 0)) ncols (1- (ncols)))))