bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#19395: 25.0.50; Setting left fringe to 0 messes up window-width


From: Titus von der Malsburg
Subject: bug#19395: 25.0.50; Setting left fringe to 0 messes up window-width
Date: Wed, 17 Dec 2014 19:36:04 -0800

On 2014-12-17 Wed 11:09, Eli Zaretskii wrote:
>> From: Titus von der Malsburg <malsburg@posteo.de>
>> 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)))))

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]