branch: elpa/markdown-mode
commit a7b3e12598269ce43d2887c4cce78bb98a5949d4
Author: Nobuyoshi Nakada <[email protected]>
Commit: Nobuyoshi Nakada <[email protected]>
Center center-aligned cells
---
markdown-mode.el | 34 ++++++++++++++++++++++------------
tests/markdown-test.el | 4 ++--
2 files changed, 24 insertions(+), 14 deletions(-)
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 |
"))))