3

The result of window-body-width doesn't change when display-line-number-mode is enabled / disabled.

How can I get the window width but excluding the fringe?

3
  • 1
    Edited - I assume you meant display-line-number-mode, and not line-number-mode. The latter just puts the line number in the mode-line.
    – Drew
    Commented Feb 6, 2022 at 4:48
  • The way Eli Z. designed the built-in line numbers mode was to prepend the line numbers to the display line, and all of that happens to the right of the fringe. There is only a hard-coded space (not a fringe) between the line numbers and the text to the right. The deprecated lisp version of linum-mode and nlinum-mode used the left of the fringe, which is different than the current built-in version.
    – lawlist
    Commented Feb 6, 2022 at 7:32
  • The hard-coded space between the built-in line numbers and the text to the right is located in xdisp.c, at the line that reads: strcat (lnum_buf, " "); I haven't played with the internals for a few years now, but my recollection is that there were some geometry functions written for users to call from lisp to obtain additional values that were not previously available in earlier versions of Emacs.
    – lawlist
    Commented Feb 6, 2022 at 7:51

2 Answers 2

2

The function window-max-chars-per-line does exactly this.

Found in the emacs manual: the effective width of the window in characters.

For e.g., its return value does change based on whether or not line numbers are enabled in the current buffer. It can do this for any face via an optional argument.

1

If I understand you right, this might do it (size in pixels):

(defun window-pixel-width-minus-fringe ()
  (let ((width    (window-pixel-width))
        (fringes  (window-fringes)))
    (- width (+ (car fringes) (cadr fringes)))))

See the Elisp manual, nodes Window Sizes and Fringe Size and Position.

3
  • The result of the function (window-fringes) doesn't change when displaying line number is toggled: (global-display-line-numbers-mode)
    – ideasman42
    Commented Feb 14, 2022 at 5:40
  • Line numbers are not part of the window width. See Window Total Width at Window Sizes. And that's exactly what you asked for: "the width of the window without line numbers".
    – Drew
    Commented Feb 14, 2022 at 18:54
  • Of course window-fringes doesn't change according to whether line numbers are displayed. Fringes have nothing to do with line numbers.
    – Drew
    Commented Feb 14, 2022 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.