Subject: bug#44068: 28.0.50; Faulty uses of tabulated-list-format

From: Stephen Berman <[email-masked]>
Date: Thu, 29 Oct 2020 23:48:38 +0100

On Thu, 29 Oct 2020 16:58:52 +0000 Stefan Kangas <[email-masked]> wrote:

> Stephen Berman <[email-masked]> writes:
>
>> I was wrong that adjusting column widths in tabulated-list-format is
>> necessary: with the attached patch, when a non-final column whose label
>> is at least 3 characters long is selected, the label will get truncated
>> and a sort indicator added; but the final column label will never get
>> truncated and always get a sort indicator when selected.
>
> The behavior with your patch looks like an improvement to me.  How about
> the case when we narrow the column but there is no sort indicator,
> should we use the ellipsis then?  See for example `list-buffers' when
> using the narrowing commands without sorting on any of the headers.

Try the attached patch; does it do what you're suggesting well enough?

diff --git a/lisp/emacs-lisp/tabulated-list.el b/lisp/emacs-lisp/tabulated-list.el
index b13f609f88..4341ad4f92 100644
--- a/lisp/emacs-lisp/tabulated-list.el
+++ b/lisp/emacs-lisp/tabulated-list.el
@@ -269,42 +269,50 @@ tabulated-list-init-header
   ;; FIXME: Should share code with tabulated-list-print-col!
   (let ((x (max tabulated-list-padding 0))
 	(button-props `(help-echo "Click to sort by column"
-			mouse-face header-line-highlight
-			keymap ,tabulated-list-sort-button-map))
+			          mouse-face header-line-highlight
+			          keymap ,tabulated-list-sort-button-map))
+        (len (length tabulated-list-format))
 	(cols nil))
     (if display-line-numbers
         (setq x (+ x (tabulated-list-line-number-width))))
     (push (propertize " " 'display `(space :align-to ,x)) cols)
-    (dotimes (n (length tabulated-list-format))
+    (dotimes (n len)
       (let* ((col (aref tabulated-list-format n))
+             (not-last-col (< n (1- len)))
 	     (label (nth 0 col))
+             (lablen (length label))
+             (pname label)
 	     (width (nth 1 col))
 	     (props (nthcdr 3 col))
 	     (pad-right (or (plist-get props :pad-right) 1))
              (right-align (plist-get props :right-align))
              (next-x (+ x pad-right width)))
+        (when (and (>= lablen 3)
+                   (> (+ 2 lablen) width) not-last-col)
+          (setq label (truncate-string-to-width label (- lablen 2)
+                                                nil nil t)))
 	(push
 	 (cond
 	  ;; An unsortable column
 	  ((not (nth 2 col))
-	   (propertize label 'tabulated-list-column-name label))
+	   (propertize label 'tabulated-list-column-name pname))
 	  ;; The selected sort column
 	  ((equal (car col) (car tabulated-list-sort-key))
 	   (apply 'propertize
-		  (concat label
-			  (cond
-			   ((> (+ 2 (length label)) width) "")
-			   ((cdr tabulated-list-sort-key)
+                  (concat label
+                          (cond
+                           ((and (< lablen 3) not-last-col) "")
+                           ((cdr tabulated-list-sort-key)
                             (format " %c"
                                     tabulated-list-gui-sort-indicator-desc))
-			   (t (format " %c"
+                           (t (format " %c"
                                       tabulated-list-gui-sort-indicator-asc))))
-		  'face 'bold
-		  'tabulated-list-column-name label
-		  button-props))
+                  'face 'bold
+                  'tabulated-list-column-name pname
+                  button-props))
 	  ;; Unselected sortable column.
 	  (t (apply 'propertize label
-		    'tabulated-list-column-name label
+		    'tabulated-list-column-name pname
 		    button-props)))
 	 cols)
         (when right-align

>> The question remains whether the truncated display in current
>> (non-adjusted) uses of tabulated-list-mode is acceptable: if so, the
>> patch below is all that's needed, if not, then the adjustments in my
>> first patch can be made as well.
>
> I'm not sure I understand what you mean here, could you elaborate?

I meant that in some uses of tabulated-list-mode there are columns whose
default width is so narrow that, when selected for sorting, they are
immediately truncated (using the patch in my previous post), e.g. the
"Status" column in list-processes.  And the issue is exacerbated with
the current patch implementing your new suggestion: with its default
width "Status" is now truncated whether selected or not.  In such cases
it may be better to make the default width of such columns wider, which
is what my first patch did.  Then with the current patch, it will still
be truncated when the column is sufficiently narrowed with `{'.

Steve Berman