From ff71d6284e51769d84e2cbb589249a2df6e4d9c9 Mon Sep 17 00:00:00 2001 From: Achim Gratz Date: Sun, 12 May 2019 15:06:56 +0200 Subject: [PATCH] lisp/org-table.el: fix table alignment --- lisp/org-table.el | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index ffcf97d2c..9e57305c3 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -4240,13 +4240,19 @@ FIELD is a string. WIDTH is a number. ALIGN is either \"c\", ;; by nil. Trailing spaces are removed. (fields (mapcar (lambda (l) - (and (not (string-match-p org-table-hline-regexp l)) - (org-split-string l "[ \t]*|[ \t]*"))) + (cond + ((string-match-p "^[ \t]*|-" l) 'sep) + ((string-match-p "^[ \t]*|~" l) 'hsep) + ((string-match-p "^[ \t]*|:" l) 'csep) + (t (org-split-string l "[ \t]*|[ \t]*")))) (split-string (buffer-substring beg end) "\n" t))) ;; Compute number of columns. If the table contains no ;; field, create a default table and bail out. (columns-number - (if fields (apply #'max (mapcar #'length fields)) + (if fields + (apply #'max (mapcar (lambda (f) (if (symbolp f) + 0 + (length f))) fields)) (kill-region beg end) (org-table-create org-table-default-size) (user-error "Empty table - created default table"))) @@ -4259,7 +4265,9 @@ FIELD is a string. WIDTH is a number. ALIGN is either \"c\", (numbers 0) (non-empty 0)) (dolist (row fields) - (let ((cell (or (nth i row) ""))) + (let ((cell (cond + ((symbolp row) "") + (t (or (nth i row) ""))))) (setq max-width (max max-width (org-string-width cell))) (cond (fixed-align? nil) ((equal cell "") nil) @@ -4288,10 +4296,20 @@ FIELD is a string. WIDTH is a number. ALIGN is either \"c\", (new (format "%s|%s|" indent - (if (null row) ;horizontal rule + (cond + ((eq 'sep row) ;horizontal rule (mapconcat (lambda (w) (make-string (+ 2 w) ?-)) widths - "+") + "+")) + ((eq 'hsep row) ;horizontal separator + (mapconcat (lambda (w) (make-string (+ 2 w) ?~)) + widths + "+")) + ((eq 'csep row) ;calculation separator + (mapconcat (lambda (w) (make-string (+ 2 w) ?:)) + widths + "+")) + (t ;data line (let ((cells ;add missing fields (append row (make-list (- columns-number @@ -4302,7 +4320,7 @@ FIELD is a string. WIDTH is a number. ALIGN is either \"c\", cells widths alignments) - "|")))))) + "|"))))))) (if (equal new previous) (forward-line) (insert new "\n") -- 2.11.4.GIT