From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.devel Subject: Re: tabulated-list: extend truncation into next align-right col Date: Thu, 03 Nov 2016 00:08:26 +0900 Message-ID: <87vaw57wd1.fsf@gmail.com> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1478099344 28854 195.159.176.226 (2 Nov 2016 15:09:04 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 2 Nov 2016 15:09:04 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: tino.calancha@gmail.com, emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Nov 02 16:08:57 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c1x9q-0006FX-3P for ged-emacs-devel@m.gmane.org; Wed, 02 Nov 2016 16:08:54 +0100 Original-Received: from localhost ([::1]:55746 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c1x9s-0002hM-SV for ged-emacs-devel@m.gmane.org; Wed, 02 Nov 2016 11:08:56 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34019) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c1x9j-0002g2-Sv for emacs-devel@gnu.org; Wed, 02 Nov 2016 11:08:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c1x9f-0000eN-Cm for emacs-devel@gnu.org; Wed, 02 Nov 2016 11:08:47 -0400 Original-Received: from mail-pf0-x22d.google.com ([2607:f8b0:400e:c00::22d]:33545) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c1x9f-0000dv-0a for emacs-devel@gnu.org; Wed, 02 Nov 2016 11:08:43 -0400 Original-Received: by mail-pf0-x22d.google.com with SMTP id d2so13592193pfd.0 for ; Wed, 02 Nov 2016 08:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:references:cc:date:in-reply-to:message-id :user-agent:mime-version; bh=9RtoepJyAOllgcP/3idQ+MJl8DyTyuZbj5z/KacYlCk=; b=a3tB7zW7CPH8E8prNiO2VX9/vEH4e2MulKbCDjGGHWvIQcazP0/55CXkeSigfjhTBP ffFgJbVQKQJnT13PrZ+Xto1AV2iMeCMJpdRoRI5SAFcOqmhXR4P21SrBHWOnjP47ub6T RhcPAvZsIOtiDxCOTgP01pU70uPonJ2aSAQirp3E9v/fGiYEQiLLYHakjH+YKrqaJPDq 1xh0Z92qtQU0HCx7+o9TojvSANRTe0AMAuVMyQNKzTEddS0RFfjtW1nmvyTG+fCGc+sz whHS8js4RIMXgdTqvKKs5OdBGLO5LljnmNMbljO/zBmm9kKkne8bxikgB5SHZl/U6kIy MGpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:references:cc:date:in-reply-to :message-id:user-agent:mime-version; bh=9RtoepJyAOllgcP/3idQ+MJl8DyTyuZbj5z/KacYlCk=; b=ljz8/6Q5yjR25PowNX0fHsVx2Ix0xfyoE6paz9VSCsenMrbSUi9DL27m7BKcKocoMW fIQXvixL2XRddl8biMY/YRvra3XFZj9PbiHkLeD0Qv188h62AEIQmyB/oQ56f8GTfxY+ MFYNJrc4X2/4Yf/ql3n6E5ENcA7eyVjdMaZrVWOZFTX8CCfipKZRjoZruUaInGH5CVXp OZ/X4Xn7NeI/jz72eK5QtgPTv++F1tzqSYR/cgtog43+xk8tM+ZOdk4/uwsy2Mjnpko4 4Dq8eZsJUxLn0DbfKDOf/48vZTlymBLgxfPpUNAsmi48D2RoDeBgYL0dctS0g42bBaUn FEiQ== X-Gm-Message-State: ABUngvfB/AvHz9NP+0bkRS1IAq557iDEZXf0XkAytIG/F06jjwnutiY0/cNg94XEnkZHeg== X-Received: by 10.99.19.71 with SMTP id 7mr6485987pgt.38.1478099321381; Wed, 02 Nov 2016 08:08:41 -0700 (PDT) Original-Received: from calancha-pc (57.92.100.220.dy.bbexcite.jp. [220.100.92.57]) by smtp.gmail.com with ESMTPSA id t7sm5547147pfa.22.2016.11.02.08.08.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 Nov 2016 08:08:40 -0700 (PDT) In-Reply-To: (Stefan Monnier's message of "Wed, 02 Nov 2016 08:27:54 -0400") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::22d X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:209110 Archived-At: Stefan Monnier writes: >> + (min next-col-width >> + (tabulated-list--near-cols-max-widths >> + (1- (line-number-at-pos)) > > Hmm... this (line-number-at-pos) and the inverse `nth` inside > tabulated-list--near-cols-max-widths are both O(N), and since we go > through this loop N times, this gives us an O(N^2) complexity. > > Can't we just get "the next row" and "the previous row" more directly? We might introduce one variable `tabulated-list--current-row'. Please, let me know if there is a cleaner way. It might be convenient to define the predicate mentioned in Bug#24855. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; >From fdd805264d19b46c81a2abc42d47e0e403395a5e Mon Sep 17 00:00:00 2001 From: Tino Calancha Date: Thu, 3 Nov 2016 00:01:11 +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--current-row): New variable. (tabulated-list-print, tabulated-list-set-col): Use it. (tabulated-list--next-col-local-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 | 63 +++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/lisp/emacs-lisp/tabulated-list.el b/lisp/emacs-lisp/tabulated-list.el index 00b029d..cc91ae0 100644 --- a/lisp/emacs-lisp/tabulated-list.el +++ b/lisp/emacs-lisp/tabulated-list.el @@ -102,6 +102,8 @@ tabulated-list-printer object identifying the entry, and COLS is a vector of column descriptors, as documented in `tabulated-list-entries'.") +(defvar tabulated-list--current-row) + (defvar-local tabulated-list-sort-key nil "Sort key for the current Tabulated List mode buffer. If nil, no additional sorting is performed. @@ -298,6 +300,28 @@ tabulated-list--get-sorter (lambda (a b) (not (funcall sorter a b))) sorter)))) +(defun tabulated-list--next-col-local-max-widths (col) + (let ((entries (if (functionp tabulated-list-entries) + (funcall tabulated-list-entries) + tabulated-list-entries))) + (let* ((row tabulated-list--current-row) + (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 @@ -319,6 +343,7 @@ tabulated-list-print (funcall tabulated-list-entries) tabulated-list-entries)) (sorter (tabulated-list--get-sorter)) + (tabulated-list--current-row -1) entry-id saved-pt saved-col window-line) (and remember-pos (setq entry-id (tabulated-list-get-id)) @@ -341,6 +366,8 @@ tabulated-list-print (tabulated-list-print-fake-header))) ;; Finally, print the resulting list. (dolist (elt entries) + (setq tabulated-list--current-row + (1+ tabulated-list--current-row)) (let ((id (car elt))) (and entry-id (equal entry-id id) @@ -402,8 +429,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 +439,29 @@ 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--next-col-local-max-widths (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 +479,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))) @@ -494,7 +536,10 @@ tabulated-list-set-col (when (< pos eol) (delete-region pos (next-single-property-change pos prop nil eol)) (goto-char pos) - (tabulated-list-print-col col desc (current-column)) + (let ((tabulated-list--current-row + (- (line-number-at-pos) + (if (overlays-at (point-min)) 2 1)))) + (tabulated-list-print-col col desc (current-column))) (if change-entry-data (aset entry col desc)) (put-text-property pos (point) 'tabulated-list-id id) -- 2.10.1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; In GNU Emacs 26.0.50.5 (x86_64-pc-linux-gnu, GTK+ Version 3.22.2) of 2016-11-02 built on calancha-pc Repository revision: 126c879df42f741fe486236aea538290a8c2ed64