Skip to content

Commit 782958e

Browse files
committed
Improve table insertion
1 parent a1e1ecc commit 782958e

File tree

2 files changed

+31
-48
lines changed

2 files changed

+31
-48
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,8 @@ can obtain a list of all keybindings by pressing <kbd>C-c C-h</kbd>.
579579
correctly when calculating column widths, however, columns
580580
containing hidden markup may not always be aligned properly.
581581

582-
<kbd>C-c C-s t</kbd> (`markdown-insert-table`) is a general command for
583-
inserting new table. Specify table size, table align and table header in
584-
minibuffer when executing `markdown-insert-table`.
585-
- Specify row size.
586-
- Specify column size.
587-
- Specify table align: right, left or center (center is default).
588-
- Specify header contents.
582+
<kbd>C-c C-s t</kbd> (`markdown-insert-table`) is a general command for inserting new table.
583+
The command prompts for table size and column alignment and inserts an empty pipe table at point.
589584

590585
* Viewing Modes:
591586

markdown-mode.el

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9351,48 +9351,36 @@ spaces, or alternatively a TAB should be used as the separator."
93519351
(goto-char begin)
93529352
(markdown-table-align)))
93539353

9354-
(defun markdown-insert-table ()
9355-
"Insert a new table."
9354+
(defun markdown-insert-table (&optional rows columns align)
9355+
"Insert an empty pipe table.
9356+
Optional arguments ROWS, COLUMNS, and ALIGN specify number of
9357+
rows and columns and the column alignment."
93569358
(interactive)
9357-
(let ((table-column (string-to-number (read-string "column size: ")))
9358-
(table-row (string-to-number (read-string "row size: ")))
9359-
(align-type (read-string "align type (left, right, center (default)): "))
9360-
(content "")
9361-
(align-counter 1)
9362-
(align "|")
9363-
(header-counter 1)
9364-
(header "|")
9365-
(row-counter 1)
9366-
(column-counter 1))
9367-
9368-
(cond ((equal align-type "left") (setq content ":---"))
9369-
((equal align-type "right") (setq content "---:"))
9370-
((equal align-type "center") (setq content "---"))
9371-
(t (setq content "---")))
9372-
9373-
(while (<= align-counter table-column)
9374-
(setq align (concat align content "|"))
9375-
(setq align-counter (1+ align-counter)))
9376-
(setq align (concat align "\n"))
9377-
9378-
(while (<= header-counter table-column)
9379-
(setq header (concat header (read-string (concat "header " (number-to-string header-counter) ": ")) "|"))
9380-
(setq header-counter (1+ header-counter)))
9381-
(setq header (concat header "\n"))
9382-
9383-
(insert header)
9384-
(insert align)
9385-
9386-
(while (<= row-counter table-row)
9387-
(setq column-counter 1)
9388-
(while (<= column-counter (1+ table-column))
9389-
(insert "|")
9390-
(setq column-counter (1+ column-counter)))
9391-
(if (< row-counter table-row)
9392-
(insert "\n"))
9393-
(setq row-counter (1+ row-counter)))
9394-
(markdown-table-align)
9395-
))
9359+
(let* ((rows (or rows (string-to-number (read-string "Row size: "))))
9360+
(columns (or columns (string-to-number (read-string "Column size: "))))
9361+
(align (or align (read-string "Alignment ([l]eft, [r]ight, [c]enter): ")))
9362+
(align (cond ((equal align "l") ":--")
9363+
((equal align "r") "--:")
9364+
((equal align "c") ":-:")
9365+
(t "---")))
9366+
(pos (point))
9367+
(indent (make-string (current-column) ?\ ))
9368+
(line (concat
9369+
(apply 'concat indent "|"
9370+
(make-list columns " |")) "\n"))
9371+
(hline (apply 'concat indent "|"
9372+
(make-list columns (concat align "|")))))
9373+
(if (string-match
9374+
"^[ \t]*$" (buffer-substring-no-properties
9375+
(point-at-bol) (point)))
9376+
(beginning-of-line 1)
9377+
(newline))
9378+
(dotimes (_ rows) (insert line))
9379+
(goto-char pos)
9380+
(if (> rows 1)
9381+
(progn
9382+
(end-of-line 1) (insert (concat "\n" hline)) (goto-char pos)))
9383+
(markdown-table-align)))
93969384

93979385

93989386
;;; ElDoc Support

0 commit comments

Comments
 (0)