Skip to content

Commit

Permalink
Add markdown-insert-table
Browse files Browse the repository at this point in the history
1. Define column size
2. Define row size
3. Define align type (left, right or center)
4. Define headers
  • Loading branch information
shigemk2 committed Oct 24, 2018
1 parent 30ae222 commit 1d278b8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@ can obtain a list of all keybindings by pressing <kbd>C-c C-h</kbd>.
correctly when calculating column widths, however, columns
containing hidden markup may not always be aligned properly.

<kbd>C-c C-t</kbd> (`markdown-insert-table`) is a general command for
inserting new table. Specify table size, table align and table header in
minibuffer when executing `markdown-insert-table`.
- Specify row size.
- Specify column size.
- Specify table align: right, left or center.
- Specify header contetnts.

* Viewing Modes:

Read-only viewing modes, `markdown-view-mode` and `gfm-view-mode`
Expand Down
46 changes: 46 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -5404,6 +5404,7 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "C-c C-t s") 'markdown-insert-header-setext-2)
(define-key map (kbd "C-c C-t t") 'markdown-insert-header-setext-1)
(define-key map (kbd "C-c C-i") 'markdown-insert-image)
(define-key map (kbd "C-c C-t") 'markdown-insert-table)
(define-key map (kbd "C-c C-x m") 'markdown-insert-list-item) ;; C-c C-j
(define-key map (kbd "C-c C-x C-x") 'markdown-toggle-gfm-checkbox) ;; C-c C-d
(define-key map (kbd "C-c -") 'markdown-insert-hr)
Expand Down Expand Up @@ -5528,6 +5529,8 @@ See also `markdown-mode-map'.")
:enable (markdown-table-at-point-p)]
["Insert Column" markdown-table-insert-column
:enable (markdown-table-at-point-p)]
["Insert Table" markdown-insert-table
:enable (markdown-table-at-point-p)]
"--"
["Convert Region to Table" markdown-table-convert-region]
["Sort Table Lines" markdown-table-sort-lines
Expand Down Expand Up @@ -9335,6 +9338,49 @@ spaces, or alternatively a TAB should be used as the separator."
(goto-char begin)
(markdown-table-align)))

(defun markdown-insert-table ()
"Insert a new table."
(interactive)
(let ((table-column (string-to-number (read-string "column size: ")))
(table-row (string-to-number (read-string "row size: ")))
(align-type (read-string "align type: "))
(content "")
(align-counter 1)
(align "|")
(header-counter 1)
(header "|")
(row-counter 1)
(column-counter 1))

(cond ((equal align-type "left") (setq content ":---"))
((equal align-type "right") (setq content "---:"))
((equal align-type "center") (setq content "---"))
(t (user-error "Speficy align-type: left, right or center")))

(while (<= align-counter table-column)
(setq align (concat align content "|"))
(setq align-counter (1+ align-counter)))
(setq align (concat align "\n"))

(while (<= header-counter table-column)
(setq header (concat header (read-string (concat "header " (number-to-string header-counter) ": ")) "|"))
(setq header-counter (1+ header-counter)))
(setq header (concat header "\n"))

(insert header)
(insert align)

(while (<= row-counter table-row)
(setq column-counter 1)
(while (<= column-counter (1+ table-column))
(insert "|")
(setq column-counter (1+ column-counter)))
(if (< row-counter table-row)
(insert "\n"))
(setq row-counter (1+ row-counter)))
(markdown-table-align)
))


;;; ElDoc Support

Expand Down
43 changes: 43 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -5464,6 +5464,48 @@ See GH-288."
(should (markdown-table-at-point-p))
(should (= (markdown-table-begin) 2))))

;;; Create table with left align

(ert-deftest test-markdown-insertion/create-table-with-left-align ()
"Insert table with left align."
(markdown-test-string ""
(execute-kbd-macro (read-kbd-macro "M-x markdown-insert-table RET 2 RET 2 RET left RET id RET name RET"))
(should (string-equal (buffer-string) "|id|name|
|:--|:--|
| | |
| | |
"))))

;;; Create table with right align

(ert-deftest test-markdown-insertion/create-table-with-right-align ()
"Insert table with right align."
(markdown-test-string ""
(execute-kbd-macro (read-kbd-macro "M-x markdown-insert-table RET 2 RET 2 RET right RET id RET name RET"))
(should (string-equal (buffer-string) "|id|name|
|--:|--:|
| | |
| | |
"))))

;;; Create table with center align

(ert-deftest test-markdown-insertion/create-table-with-center-align ()
"Insert table with center align."
(markdown-test-string ""
(execute-kbd-macro (read-kbd-macro "M-x markdown-insert-table RET 2 RET 2 RET center RET id RET name RET"))
(should (string-equal (buffer-string) "|id|name|
|---|---|
| | |
| | |
"))))

;;; Cannot create table

(ert-deftest test-markdown-insertion/cannot-create-table ()
"Cannot create table."
(markdown-test-string ""
(should-error (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-table RET 2 RET 2 RET other RET id RET name RET")) :type 'user-error)))

;;; gfm-mode tests:

Expand Down Expand Up @@ -5919,6 +5961,7 @@ https://github.com/jrblevin/markdown-mode/issues/235"
(should (string-prefix-p "false failed with exit code 1"
(error-message-string data))))))


(provide 'markdown-test)

;;; markdown-test.el ends here

0 comments on commit 1d278b8

Please sign in to comment.