emacs-devel
[Top][All Lists]
Advanced

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

Re: tabulated-list: extend truncation into next align-right col


From: Tino Calancha
Subject: Re: tabulated-list: extend truncation into next align-right col
Date: Wed, 2 Nov 2016 03:25:49 +0900 (JST)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)



On Tue, 1 Nov 2016, Stefan Monnier wrote:

;; Without computing maximum widths:
C1  C2                                       Numbers
abc **************************************... 123456
abc *******************************************... 1
abc ************************************         123

That's exactly what used to happen in *Buffer List* (and the source for
this TODO item).  It wasn't that bad (I can't remember any user
complaints about it).  Admittedly, we could do better: compute the max
width not of the whole column but of the current line, and the two
surrounding ones.  This way we'd avoid
We have now also a patch for that approach.

Following is a comparison of:
I) The classical: The current behaviour in master branch.
II) The shy one: Using previous patch, i.e., computing the maximum width in the
    whole column.  Note how, for every row, he feel shy to enter in Score 
column :-)
III) The brother-in-law: Using new patch, i.e., computing the local maximum 
width in the
    column around the target row.  Note how he enter in your score column, open 
your
    fridge without asking perm. and collect one of your favourite beers :-S

;; I) current behaviour: truncate until column width (50 in Comment column):
M Name            Date             Comment                                      
              Score ▼
  Foo Tree        2016-11-02 02:51 
******************************************line ...               5
D Bar Soup        2016-11-02 02:51 line with 23 characters                      
                   15
* Baz Silver      2016-11-02 02:51 ***************************line with 50 
characters          210000
  Qux Spec        2016-11-02 02:51 ********************************line with 55 
ch...          380000

;; II) Previous patch; compute maximum in column:
M Name            Date             Comment                                      
              Score ▼
  Foo Tree        2016-11-02 02:30 
******************************************line with 65 c...      5
D Bar Soup        2016-11-02 02:30 line with 23 characters                      
                   15
* Baz Silver      2016-11-02 02:30 ***************************line with 50 
characters          210000
  Qux Spec        2016-11-02 02:30 ********************************line with 55 
characters     380000

;; III) New patch; compute local maximum in column around target row:
M Name            Date             Comment                                      
              Score ▼
  Foo Tree        2016-11-02 02:30 
******************************************line with 65 chara...  5
D Bar Soup        2016-11-02 02:30 line with 23 characters                      
                   15
* Baz Silver      2016-11-02 02:30 ***************************line with 50 
characters          210000
  Qux Spec        2016-11-02 02:30 ********************************line with 55 
characters     380000

So, from previous e-mail exchange i guess you prefer III) right?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
From 6512184ccb8127d94410d56f147de9ecadc932c6 Mon Sep 17 00:00:00 2001
From: Tino Calancha <address@hidden>
Date: Wed, 2 Nov 2016 02:20:14 +0900
Subject: [PATCH] tabulated-list: extend truncation into next align-right
 column

See discussion on:
https://lists.gnu.org/archive/html/emacs-devel/2016-10/msg01101.html
* lisp/emacs-lisp/tabulated-list.el
(tabulated-list--near-cols-max-widths): New defun.
(tabulated-list-print-col): Use it.  If the next column is
align-right, and has some space left then don't truncate to width,
use some of the available space from the next column.
---
 lisp/emacs-lisp/tabulated-list.el | 54 +++++++++++++++++++++++++++++++++------
 1 file changed, 46 insertions(+), 8 deletions(-)

diff --git a/lisp/emacs-lisp/tabulated-list.el 
b/lisp/emacs-lisp/tabulated-list.el
index 00b029d..085fe6f 100644
--- a/lisp/emacs-lisp/tabulated-list.el
+++ b/lisp/emacs-lisp/tabulated-list.el
@@ -298,6 +298,27 @@ tabulated-list--get-sorter
           (lambda (a b) (not (funcall sorter a b)))
         sorter))))

+(defun tabulated-list--near-cols-max-widths (row col)
+  (let ((entries (if (functionp tabulated-list-entries)
+                     (funcall tabulated-list-entries)
+                   tabulated-list-entries)))
+    (let* ((entry (nth row entries))
+           (entry-up (if (> row 0)
+                         (nth (1- row) entries)
+                       entry))
+           (entry-down (if (< row (1- (length entries)))
+                           (nth (1+ row) entries)
+                         entry))
+           (desc (elt (cadr entry) col))
+           (desc-up (elt (cadr entry-up) col))
+           (desc-down (elt (cadr entry-down) col)))
+      (apply #'max (mapcar (lambda (x)
+                             (string-width
+                              (if (stringp x)
+                                  x
+                                (car x))))
+                           (list desc desc-up desc-down))))))
+
 (defun tabulated-list-print (&optional remember-pos update)
   "Populate the current Tabulated List mode buffer.
 This sorts the `tabulated-list-entries' list if sorting is
@@ -402,8 +423,6 @@ tabulated-list-print-col
 N is the column number, COL-DESC is a column descriptor (see
 `tabulated-list-entries'), and X is the column number at point.
 Return the column number after insertion."
-  ;; TODO: don't truncate to `width' if the next column is align-right
-  ;; and has some space left.
   (let* ((format    (aref tabulated-list-format n))
         (name      (nth 0 format))
         (width     (nth 1 format))
@@ -414,12 +433,31 @@ tabulated-list-print-col
          (label-width (string-width label))
         (help-echo (concat (car format) ": " label))
         (opoint (point))
-        (not-last-col (< (1+ n) (length tabulated-list-format))))
+        (not-last-col (< (1+ n) (length tabulated-list-format)))
+         available-space)
+    (when not-last-col
+      (let* ((next-col-format (aref tabulated-list-format (1+ n)))
+             (next-col-right-align (plist-get (nthcdr 3 next-col-format)
+                                              :right-align))
+             (next-col-width (nth 1 next-col-format)))
+        (setq available-space
+              (if (and (not right-align)
+                       next-col-right-align)
+                  (-
+                   (+ width next-col-width)
+                   (min next-col-width
+                        (tabulated-list--near-cols-max-widths
+                         (1- (line-number-at-pos))
+                         (1+ n))))
+                width))))
     ;; Truncate labels if necessary (except last column).
-    (and not-last-col
-        (> label-width width)
-        (setq label (truncate-string-to-width label width nil nil t)
-               label-width width))
+    ;; Don't truncate to `width' if the next column is align-right
+    ;; and has some space left, truncate to `available-space' instead.
+    (when (and not-last-col
+               (> label-width available-space)
+               (setq label (truncate-string-to-width
+                            label available-space nil nil t)
+                     label-width available-space)))
     (setq label (bidi-string-mark-left-to-right label))
     (when (and right-align (> width label-width))
       (let ((shift (- width label-width)))
@@ -437,7 +475,7 @@ tabulated-list-print-col
       (when not-last-col
         (when (> pad-right 0) (insert (make-string pad-right ?\s)))
         (insert (propertize
-                 (make-string (- next-x x label-width pad-right) ?\s)
+                 (make-string (- width (min width label-width)) ?\s)
                  'display `(space :align-to ,next-x))))
       (put-text-property opoint (point) 'tabulated-list-column-name name)
       next-x)))
--
2.10.1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 26.0.50.3 (x86_64-pc-linux-gnu, GTK+ Version 3.22.2)
 of 2016-11-02 built on calancha-pc
Repository revision: c3640fcc96ed80368209c73d7ac9a0f0d1833d93

reply via email to

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