emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Re: double-width characters in tables
Date: Wed, 13 Feb 2013 16:50:29 +0800	[thread overview]
Message-ID: <87k3qcbnsa.fsf_-_@ericabrahamsen.net> (raw)
In-Reply-To: 87a9r93s0o.fsf@ericabrahamsen.net

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

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Achim Gratz <Stromeko@nexgo.de> writes:
>
>> Eric Abrahamsen writes:
>>> Yes, org-string-width eventually calls string-width, so that behaves
>>> "correctly" as far as it goes
[...]

And more, this time to prevent errors when using column-narrowing
cookies on columns containing peculiar-width strings. It's still going
to look weird, often as not, but this will at least prevent blowups.

I'm providing two possible patches. One is a one-liner that does the
trick but produces ugly results. The other goes a wee bit overboard, but
looks a little better. I offer either, or neither, for consideration.

Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-table-column-narrowing-play-nice-with-variable-.patch --]
[-- Type: text/x-patch, Size: 1867 bytes --]

From ccf294f9dad0b81240b8f1a0051cecfddd47d1c4 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Wed, 13 Feb 2013 16:46:33 +0800
Subject: [PATCH] Make table column narrowing play nice with variable-width
 strings

---
 lisp/org-table.el | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index 3f276f8..d79b650 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -765,7 +765,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
 	 (hfmt1 (concat
 		 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
 	 emptystrings links dates emph raise narrow
-	 falign falign1 fmax f1 len c e space)
+	 falign falign1 fmax f1 f2 len c e space)
     (untabify beg end)
     (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
     ;; Check if we have links or dates
@@ -850,10 +850,19 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
 		  (unless (> f1 1)
 		    (error "Cannot narrow field starting with wide link \"%s\""
 			   (match-string 0 xx)))
-		  (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
-		  (add-text-properties (- f1 2) f1
-				       (list 'display org-narrow-column-arrow)
-				       xx)))))
+		  (setq f2 (length xx))
+		  (if (= (org-string-width xx)
+			 f2)
+		      (setq f2 f1)
+		    (setq f2 1)
+		    (while (< (org-string-width (substring xx 0 f2))
+			      f1)
+		      (setq f2 (1+ f2))))
+		  (add-text-properties f2 (length xx) (list 'org-cwidth t) xx)
+		  (add-text-properties (if (>= (string-width (substring xx (1- f2) f2)) 2)
+					   (1- f2) (- f2 2)) f2
+					   (list 'display org-narrow-column-arrow)
+					   xx)))))
       ;; Get the maximum width for each column
       (push (apply 'max (or fmax 1) 1 (mapcar 'org-string-width column))
 	    lengths)
-- 
1.8.1.3


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-Prevent-errors-with-table-column-narrowing-and-varia.patch --]
[-- Type: text/x-patch, Size: 877 bytes --]

From 319931ba76fc3ce5b8157f131d7c71c18f4c5d60 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Wed, 13 Feb 2013 15:07:59 +0800
Subject: [PATCH] Prevent errors with table column narrowing and variable-width
 strings

---
 lisp/org-table.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index 3f276f8..6fbff7a 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -850,6 +850,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
 		  (unless (> f1 1)
 		    (error "Cannot narrow field starting with wide link \"%s\""
 			   (match-string 0 xx)))
+		  (setq f1 (round (* (/ (float (length xx)) (org-string-width xx)) f1)))
 		  (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
 		  (add-text-properties (- f1 2) f1
 				       (list 'display org-narrow-column-arrow)
-- 
1.8.1.3


  parent reply	other threads:[~2013-02-13 12:10 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-12 10:56 double-width characters in tables Eric Abrahamsen
2013-02-12 11:02 ` Jambunathan K
2013-02-12 11:22   ` Bastien
2013-02-12 11:16 ` Bastien
2013-02-12 15:43   ` Eric Abrahamsen
2013-02-12 15:50     ` Nick Dokos
2013-02-12 15:55     ` Bastien
2013-02-12 17:55       ` Takaaki ISHIKAWA
2013-02-12 18:43     ` Achim Gratz
2013-02-13  1:46       ` Eric Abrahamsen
2013-02-13  2:23         ` [PATCH] " Eric Abrahamsen
2013-02-13 11:42           ` Bastien
2013-02-13 13:18             ` Eric Abrahamsen
2013-02-13  8:50         ` Eric Abrahamsen [this message]
2013-02-13 11:31         ` Bastien

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

  List information: https://www.orgmode.org/

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

  git send-email \
    --in-reply-to=87k3qcbnsa.fsf_-_@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-orgmode@gnu.org \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).