unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Keith David Bershatsky <esq@lawlist.com>
To: 28246@debbugs.gnu.org
Subject: bug#28246: display line number width != length of line number at eob
Date: Sat, 26 Aug 2017 14:57:56 -0700	[thread overview]
Message-ID: <m27exq56vv.wl%esq@lawlist.com> (raw)

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	other threads:[~2017-08-26 21:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-26 21:57 Keith David Bershatsky [this message]
2017-08-27 14:23 ` bug#28246: display line number width != length of line number at eob 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m27exq56vv.wl%esq@lawlist.com \
    --to=esq@lawlist.com \
    --cc=28246@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).