all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Emacs developers <emacs-devel@gnu.org>,
	Tino Calancha <tino.calancha@gmail.com>
Subject: Re: tabulated-list: extend truncation into next align-right col
Date: Wed, 2 Nov 2016 03:25:49 +0900 (JST)	[thread overview]
Message-ID: <alpine.DEB.2.20.1611020323350.3586@calancha-pc> (raw)
In-Reply-To: <jwv4m3rgz47.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 8129 bytes --]



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 <tino.calancha@gmail.com>
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

  parent reply	other threads:[~2016-11-01 18:25 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-31 17:41 tabulated-list: extend truncation into next align-right col Tino Calancha
2016-10-31 18:40 ` Stefan Monnier
2016-11-01  3:31   ` Tino Calancha
2016-11-01 12:41     ` Stefan Monnier
2016-11-01 13:25       ` Tino Calancha
2016-11-01 18:25       ` Tino Calancha [this message]
2016-11-02  0:38         ` Stefan Monnier
2016-11-02  5:06           ` Tino Calancha
2016-11-02  8:20             ` Tino Calancha
2016-11-02 12:27         ` Stefan Monnier
2016-11-02 15:08           ` Tino Calancha
2016-11-02 15:16             ` Tino Calancha
2016-11-04 14:35           ` Tino Calancha
2016-11-04 16:53             ` Tino Calancha
2016-11-04 17:29             ` Stefan Monnier
2016-11-07  1:59               ` Tino Calancha
2016-11-14  8:43                 ` Tino Calancha
2016-10-31 20:15 ` Drew Adams
2016-10-31 20:47   ` Mark Oteiza

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

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

  git send-email \
    --in-reply-to=alpine.DEB.2.20.1611020323350.3586@calancha-pc \
    --to=tino.calancha@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.