unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#28246: display line number width != length of line number at eob
@ 2017-08-26 21:57 Keith David Bershatsky
  2017-08-27 14:23 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Keith David Bershatsky @ 2017-08-26 21:57 UTC (permalink / raw)
  To: 28246

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)





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-08-29 15:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-26 21:57 bug#28246: display line number width != length of line number at eob Keith David Bershatsky
2017-08-27 14:23 ` Eli Zaretskii
2017-08-28  1:46 ` Keith David Bershatsky
2017-08-28 17:27   ` Eli Zaretskii
2017-08-28 18:50 ` Keith David Bershatsky
2017-08-29 15:00   ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).