Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown-insert-table #369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the docs. Please update this line with the new keybinding (see below).

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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: "contents"


* 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C-c C-t is actually a prefix to heading (titling) commands (which can be seen a couple of lines above). We'd need to find a new keybinding. Perhaps C-c C-s t, which would be under markdown-mode-style-map.

(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)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the predicate should be(not (markdown-table-at-point-p)), which means this menu item is only enabled when there is not a table at the point.

"--"
["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: "))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a helpful tip in the prompt such as "(left, right, center (default)):"

(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")))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps centered should be the pass-through default?


(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
42 changes: 42 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