org-mode 表の桁幅指定での日本語幅ずれ回避

org-modeの表で <NN> と桁幅指定しても、 日本語がある桁では幅の計算がうまく行かない問題。 ELFで話題に出たので、 パッチの形にしてみた。7.8.06用。

org-table-7.8.06.diff

diff -r 4db0de28f0ba lisp/org-table.el
--- a/lisp/org-table.el	Sat Mar 24 15:29:00 2012 +0900
+++ b/lisp/org-table.el	Sat Mar 24 15:41:58 2012 +0900
@@ -762,12 +762,19 @@
 		    'help-echo
 		    (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
 		  (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
+
+		  ;; Calculate c and f1 again to fit to display-width
+		  (let*((xs (org-no-properties xx))
+			(s0 (format (format "%%.%ds" (- f1 2)) xs))
+			(s1 (format (format "%%.%ds" f1) xs)))
+		    (setq c (length s0) f1 (length s1)))
 		  (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)
+		  (setq space (make-string (max 0 (- fmax (string-width (substring xx 0 c)) 2)) ? ))
+		  (add-text-properties c f1
+				       (list 'display (concat org-narrow-column-arrow space))
 				       xx)))))
       ;; Get the maximum width for each column
       (push (apply 'max (or fmax 1) 1 (mapcar 'org-string-width column))