Subject: 29.0.50; [PATCH] Mode-line elements too wide in some VC buffers
From: Gabriel <[email-masked]>
To: [email-masked]
Date: Tue, 22 Nov 2022 17:58:25 -0300
List-Id: <debbugs-submit.debbugs.gnu.org>

Severity: wishlist

Description:

Some mode-line elements are too wide in some VC buffers, which affects
the information visibility.

Steps:

1) emacs -Q

2) Open some file controlled by Git VC, e.g.: C-x f ~/git/emacs/README

The buffer named "README" will be shown, the buffer name is displayed on
the left side of the mode-line.

(length (buffer-name)) => 6

3) Annotate the buffer: C-x v g

The VC-Annotate buffer named "*Annotate README (rev 1f39da3098a2a4cec9985e6db934ed14b7b522b7)*"
will be shown, the buffer name is displayed on the left side of the
mode-line.  The revision number might differ.

(length (buffer-name)) => 64

4) Show log revision at line: l

The Git-Log-View buffer will be shown.  The value of
`vc-parent-buffername' is " from *Annotate README (rev 1f39da3098a2a4cec9985e6db934ed14b7b522b7)*",
displayed on the right side of the mode-line.  The revision number might
differ.

(length vc-parent-buffer-name) => 70

Analysis:

A short named buffer "README" with length of 6 had information displayed
in the mode-line with lengths of 64 (with `vc-annotate') and 70 (with
`vc-annotate-show-log-revision-at-line').  This excessive length affects
the visibility of information in mode-line, which was supposed to ease
the access of information to the user.  A long buffer name can also
affect the display and usability in other interfaces, e.g.:
`list-buffers', `ibuffer' etc.

The problem, in this case, is the long revision string used by Git.  I
am not sure how other VCs behaves in this matter (bzr, cvs, dav, hg,
rcs, sccs, src, svc etc).

Solutions:

For Git, a better option would be to use a "short revision" (by default
the first 7 characters of the "long revision"), which would reduce, for
the example above, the length from 64 to 39 (and `vc-parent-buffer-name'
from 70 to 45):

diff --git a/lisp/vc/vc-annotate.el b/lisp/vc/vc-annotate.el
index a15cf417de..63485af52e 100644
--- a/lisp/vc/vc-annotate.el
+++ b/lisp/vc/vc-annotate.el
@@ -409,7 +409,9 @@ vc-annotate
 				  nil nil "20")))))))
   (vc-ensure-vc-buffer)
   (setq vc-annotate-display-mode display-mode) ;Not sure why.  --Stef
-  (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
+  (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*"
+                                   (buffer-name)
+                                   (string-limit rev 7)))
          (temp-buffer-show-function 'vc-annotate-display-select)
          ;; If BUF is specified, we presume the caller maintains current line,
          ;; so we don't need to do it here.  This implementation may give

Another option would be to display additional information in the
header-line.

Another option would be to allow the user to format how the VC-Annotate
buffer name is displayed (and optionally also the
`vc-parent-buffer-name').  See attached patches.

From 4ff9db05b20ac48b222b06f6c11a12fd35fdcdec Mon Sep 17 00:00:00 2001
From: Gabriel do Nascimento Ribeiro <[email-masked]>
Date: Tue, 22 Nov 2022 16:45:30 -0300
Subject: [PATCH 1/1] New option vc-annotate-buffer-name-function

* lisp/vc/vc-annotate.el (vc-annotate-buffer-name-function): New user
option.
(vc-annotate-buffer-name-default): New function to compute the default
name of VC-Annotate buffers.
(vc-annotate): Use `vc-annotate-buffer-name-function'.

* etc/NEWS: Announce the new user option.
---
 etc/NEWS               |  5 +++++
 lisp/vc/vc-annotate.el | 20 +++++++++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5a65896d69..01dab3387b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1973,6 +1973,11 @@ your mail user agent.  The behavior of 'vc-prepare-patch' can be
 modified by the user options 'vc-prepare-patches-separately' and
 'vc-default-patch-addressee'.
 
++++
+*** New user option 'vc-annotate-buffer-name-function'.
+It allows setting a custom function to format the name of VC-Annotate
+buffers.
+
 ** Message
 
 ---
diff --git a/lisp/vc/vc-annotate.el b/lisp/vc/vc-annotate.el
index a15cf417de..ff3c9905d1 100644
--- a/lisp/vc/vc-annotate.el
+++ b/lisp/vc/vc-annotate.el
@@ -162,6 +162,22 @@ vc-annotate-menu-elements
   :type '(repeat number)
   :group 'vc)
 
+(defcustom vc-annotate-buffer-name-function #'vc-annotate-buffer-name-default
+  "Function to compute the name of a VC-Annotate buffer.
+The function receives two arguments, the buffer being annotated
+and the revision as string.  It should return the VC-Annotate
+buffer name as string."
+  :type 'function
+  :group 'vc
+  :version "29.1")
+
+(defun vc-annotate-buffer-name-default (buffer revision)
+  "Function to compute the default name of a VC-Annotate buffer.
+See `vc-annotate-buffer-name-function'."
+  (format "*Annotate %s (rev %s)*"
+          (buffer-name buffer)
+          revision))
+
 (defvar-keymap vc-annotate-mode-map
   :doc "Local keymap used for VC-Annotate mode."
   "a"   #'vc-annotate-revision-previous-to-line
@@ -409,7 +425,9 @@ vc-annotate
 				  nil nil "20")))))))
   (vc-ensure-vc-buffer)
   (setq vc-annotate-display-mode display-mode) ;Not sure why.  --Stef
-  (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
+  (let* ((temp-buffer-name (funcall vc-annotate-buffer-name-function
+                                    (current-buffer)
+                                    rev))
          (temp-buffer-show-function 'vc-annotate-display-select)
          ;; If BUF is specified, we presume the caller maintains current line,
          ;; so we don't need to do it here.  This implementation may give
-- 
2.34.1

From bdfa9d64850e4b7de6b77dd16280a840ed0735fc Mon Sep 17 00:00:00 2001
From: Gabriel do Nascimento Ribeiro <[email-masked]>
Date: Tue, 22 Nov 2022 16:50:35 -0300
Subject: [PATCH 1/1] New option vc-parent-buffer-name-format-function'

* lisp/vc/vc-dispatcher.el (vc-parent-buffer-name-format-function):
New user option to compute the parent buffer name identitication in
mode-line of VC buffers.
(vc-parent-buffer-name-format-default): New function to compute the
default parent buffer name identitication in mode-line of VC buffers.
(vc-setup-buffer, vc-start-logentry): Use
`vc-parent-buffer-name-format-function'.

* etc/NEWS: Announce the new user option.
---
 etc/NEWS                 |  5 +++++
 lisp/vc/vc-dispatcher.el | 22 ++++++++++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5a65896d69..31bc22ab9e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1973,6 +1973,11 @@ your mail user agent.  The behavior of 'vc-prepare-patch' can be
 modified by the user options 'vc-prepare-patches-separately' and
 'vc-default-patch-addressee'.
 
++++
+*** New user option 'vc-parent-buffer-name-format-function'.
+It allows setting a custom function to format the parent buffer name
+identitication in mode-line of VC buffers.
+
 ** Message
 
 ---
diff --git a/lisp/vc/vc-dispatcher.el b/lisp/vc/vc-dispatcher.el
index dc3ed52650..e45d4d16ef 100644
--- a/lisp/vc/vc-dispatcher.el
+++ b/lisp/vc/vc-dispatcher.el
@@ -142,6 +142,22 @@ vc-suppress-confirm
   :type 'boolean
   :group 'vc)
 
+(defcustom vc-parent-buffer-name-format-function #'vc-parent-buffer-name-format-default
+  "Function to compute the parent buffer name identitication in
+mode-line of VC buffers.
+The function receives one argument, the parent buffer.  It should
+return the parent buffer name identitication as string."
+  :type 'function
+  :group 'vc
+  :version "29.1")
+
+(defun vc-parent-buffer-name-format-default (buffer)
+  "Function to compute the default parent buffer name identitication
+in mode-line of VC buffers.
+See `vc-parent-buffer-name-format-function'."
+  (format " from %s"
+          (buffer-name buffer)))
+
 ;; Variables the user doesn't need to know about.
 
 (defvar vc-log-operation nil)
@@ -188,7 +204,8 @@ vc-setup-buffer
     (kill-all-local-variables)
     (setq-local vc-parent-buffer camefrom)
     (setq-local vc-parent-buffer-name
-                (concat " from " (buffer-name camefrom)))
+                (funcall vc-parent-buffer-name-format-function
+                         camefrom))
     (setq default-directory olddir)
     (let ((buffer-undo-list t)
           (inhibit-read-only t))
@@ -748,7 +765,8 @@ vc-start-logentry
       (pop-to-buffer (get-buffer-create logbuf)))
     (setq-local vc-parent-buffer parent)
     (setq-local vc-parent-buffer-name
-                (concat " from " (buffer-name vc-parent-buffer)))
+                (funcall vc-parent-buffer-name-format-function
+                         vc-parent-buffer))
     (when patch-string
       (setq-local vc-patch-string patch-string))
     (vc-log-edit files mode backend)
-- 
2.34.1

Please share your suggestions.

---
Gabriel