branch: elpa/markdown-mode
commit 92802fae9ebbc8c2e4c281c06dcdbd74b8bca80e
Merge: b524618c3ed 84276bda684
Author: Shohei YOSHIDA <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #923 from nobu/table-centerize
    
    Center center-aligned cells
---
 CHANGES.md             |  1 +
 markdown-mode.el       | 34 ++++++++++++++++++++++------------
 tests/markdown-test.el |  4 ++--
 3 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 707c25919b2..7ab0e180683 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -18,6 +18,7 @@
     - Fix `markdown-heading-at-point` at the end of line [GH-912][]
     - Catch an exception when `scan-sexp` fails [GH-917][]
     - `markdown-link-at-pos` should decode both control characters and spaces 
[GH-921][]
+    - `markdown-table-align` now aligns centered cells
 
 *   Improvements:
     - Support drag and drop features on Windows and multiple files' drag and 
drop
diff --git a/markdown-mode.el b/markdown-mode.el
index 0dbc5d177e2..8be1daafd13 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -9759,6 +9759,18 @@ This function assumes point is on a table."
        (cl-loop for c across line
                 always (member c '(?| ?- ?: ?\t ? )))))
 
+(defun markdown-table-align-raw (cells fmtspec widths)
+  (let (fmt width)
+    (mapconcat
+     (lambda (cell)
+       (setq fmt (car fmtspec) fmtspec (cdr fmtspec))
+       (setq width (car widths) widths (cdr widths))
+       (if (equal fmt 'c)
+           (setq cell (concat (make-string (/ (- width (length cell)) 2) ?\s) 
cell)))
+       (unless (equal fmt 'r) (setq width (- width)))
+       (format (format " %%%ds " width) cell))
+     cells "|")))
+
 (defun markdown-table-align ()
   "Align table at point.
 This function assumes point is on a table."
@@ -9783,8 +9795,6 @@ This function assumes point is on a table."
             (maxcells (if cells
                           (apply #'max (mapcar #'length cells))
                         (user-error "Empty table")))
-            ;; Empty cells to fill short lines
-            (emptycells (make-list maxcells ""))
             maxwidths)
        ;; Calculate maximum width for each column
        (dotimes (i maxcells)
@@ -9796,20 +9806,20 @@ This function assumes point is on a table."
        (setq fmtspec (markdown-table-colfmt fmtspec))
        ;; Compute formats needed for output of table lines
        (let ((hfmt (concat indent "|"))
-             (rfmt (concat indent "|"))
-             hfmt1 rfmt1 fmt)
-         (dolist (width maxwidths (setq hfmt (concat (substring hfmt 0 -1) 
"|")))
-           (setq fmt (pop fmtspec))
-           (cond ((equal fmt 'l) (setq hfmt1 ":%s-|" rfmt1 " %%-%ds |"))
-                 ((equal fmt 'r) (setq hfmt1 "-%s:|" rfmt1  " %%%ds |"))
-                 ((equal fmt 'c) (setq hfmt1 ":%s:|" rfmt1 " %%-%ds |"))
-                 (t              (setq hfmt1 "-%s-|" rfmt1 " %%-%ds |")))
-           (setq rfmt (concat rfmt (format rfmt1 width)))
+             hfmt1 fmt (fmts fmtspec))
+         (dolist (width maxwidths)
+           (setq fmt (car fmts) fmts (cdr fmts))
+           (cond ((equal fmt 'l) (setq hfmt1 ":%s-|"))
+                 ((equal fmt 'r) (setq hfmt1 "-%s:|"))
+                 ((equal fmt 'c) (setq hfmt1 ":%s:|"))
+                 (t              (setq hfmt1 "-%s-|")))
            (setq hfmt (concat hfmt (format hfmt1 (make-string width ?-)))))
          ;; Replace modified lines only
          (dolist (line lines)
            (let ((line (if line
-                           (apply #'format rfmt (append (pop cells) 
emptycells))
+                           (concat indent "|"
+                                   (markdown-table-align-raw (pop cells) 
fmtspec maxwidths)
+                                   "|")
                          hfmt))
                  (previous (buffer-substring (point) (line-end-position))))
              (if (equal previous line)
diff --git a/tests/markdown-test.el b/tests/markdown-test.el
index dc91a2d709c..ed2dfaba871 100644
--- a/tests/markdown-test.el
+++ b/tests/markdown-test.el
@@ -7750,7 +7750,7 @@ title: asdasdasd
     (search-forward "A")
     (markdown-table-align)
     (should (string= (buffer-string) "
-| A   | B    | C     |      D |
+| A   | B    |   C   |      D |
 |-----|:-----|:-----:|-------:|
 | aaa | bbbb | ccccc | dddddd |
 "))))
@@ -7765,7 +7765,7 @@ title: asdasdasd
     (search-forward "A")
     (markdown-table-align)
     (should (string= (buffer-string) "
-| A   | B    | C     |      D |
+| A   | B    |   C   |      D |
 |-----|:-----|:-----:|-------:|
 | aaa | bbbb | ccccc | dddddd |
 "))))

Reply via email to