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

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

bug#28246: display line number width != length of line number at eob


From: Keith David Bershatsky
Subject: bug#28246: display line number width != length of line number at eob
Date: Sat, 26 Aug 2017 14:57:56 -0700

I would like to configure native line numbers to dynamically adjust the width 
(smaller/larger) so that it is equal to the length of the last line in the 
buffer.

Examples:

* Buffer has 1 to 9 lines, the width should be 1 (irrespective of where point 
is).

* Buffer has 10 to 99 lines, the width should 2 (irrespective of where point 
is).

* Buffer has 100 to 999 lines, the width should be 3 (irrespective of where 
point is).

Emacs is erroneously increasing the line number width before there are 
sufficient lines in the buffer to merit such an increase in width.

Emacs fails to decrease the line number width when lines are removed from the 
buffer that merit a decrease in the width.

The erroneous behavior can be demonstrated by evaluating the following code in 
a *scratch* buffer, and holding down the return key, and by holding down the 
backspace key.

The desired behavior can be achieved with the Lisp code below AND by adding the 
following lines of code to maybe_produce_line_number just above the comment /* 
Compute the required width if needed.  */

  /* example modification to achieve desired behavior */
  if (NATNUMP (Vdisplay_line_numbers_width)
      && !EQ (Vdisplay_line_numbers, Qrelative)
      && !EQ (Vdisplay_line_numbers, Qvisual))
    it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);

I was unable to achieve the desired behavior by customizing Lisp variables such 
as display-line-numbers-grow-only and/or display-line-numbers-width-start.

Here is the Lisp code that I am using:


(require 'display-line-numbers)

(setq display-line-numbers-grow-only nil)

(setq display-line-numbers-width-start nil)

(defvar display-line-numbers--update-width-var t
"When non-nil, update the line number width.")

(defun display-line-numbers--update-width-fn ()
"Update the line number width based upon the last line in the buffer.
This function should be attached to the `post-command-hook'"
  (let ((display-width (line-number-display-width))
        (desired-width
          (save-excursion
            (goto-char (point-max))
            (length (format-mode-line "%l")))))
  (setq display-line-numbers-width desired-width)
  (message "display-width (%s) | target-width (%s)" display-width 
desired-width)))

(define-minor-mode display-line-numbers-mode
  "Toggle display of line numbers in the buffer.
This uses `display-line-numbers' internally.
-  To change the type of line numbers displayed by default,
customize `display-line-numbers-type'.  To change the type while
the mode is on, set `display-line-numbers' directly."
  :lighter nil
  (cond
    (display-line-numbers-mode
      (cond
        ((null display-line-numbers--update-width-var)
          (when display-line-numbers-width-start
            (setq display-line-numbers-width
                  (length (number-to-string
                           (count-lines (point-min) (point-max))))))
          (when display-line-numbers-grow-only
            (add-hook 'pre-command-hook #'display-line-numbers-update-width nil 
t)))
        (display-line-numbers--update-width-var
          (add-hook 'post-command-hook #'display-line-numbers--update-width-fn 
nil 'local)))
      (setq display-line-numbers display-line-numbers-type))
    (t
    (remove-hook 'pre-command-hook #'display-line-numbers-update-width 'local)
    (remove-hook 'post-command-hook #'display-line-numbers--update-width-fn 
'local)
    (setq display-line-numbers nil))))

(display-line-numbers-mode t)





reply via email to

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