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 authored and jrblevin committed Oct 29, 2018
1 parent 30ae222 commit 5252b71
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
tags around the output. ([GH-280][], [GH-281][])
- Add `markdown-unused-refs` command to list and clean up unused
references (available via `C-c C-c u`). ([GH-322][])
- Add `markdown-insert-table` (`C-c C-s t`) for interactive
table insertion. ([GH-369][])

* Improvements:

Expand Down Expand Up @@ -170,6 +172,7 @@
[gh-335]: https://github.com/jrblevin/markdown-mode/pull/335
[gh-340]: https://github.com/jrblevin/markdown-mode/issues/340
[gh-350]: https://github.com/jrblevin/markdown-mode/pull/350
[gh-369]: https://github.com/jrblevin/markdown-mode/pull/369

# Markdown Mode 2.3

Expand Down
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-s 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 (center is default).
- Specify header contents.

* Viewing Modes:

Read-only viewing modes, `markdown-view-mode` and `gfm-view-mode`
Expand Down
45 changes: 45 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -5283,6 +5283,7 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "P") 'markdown-pre-region)
(define-key map (kbd "q") 'markdown-insert-blockquote)
(define-key map (kbd "s") 'markdown-insert-strike-through)
(define-key map (kbd "t") 'markdown-insert-table)
(define-key map (kbd "Q") 'markdown-blockquote-region)
(define-key map (kbd "w") 'markdown-insert-wiki-link)
(define-key map (kbd "-") 'markdown-insert-hr)
Expand Down Expand Up @@ -5528,6 +5529,7 @@ 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]
"--"
["Convert Region to Table" markdown-table-convert-region]
["Sort Table Lines" markdown-table-sort-lines
Expand Down Expand Up @@ -9335,6 +9337,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 (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 (setq content "---")))

(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
47 changes: 47 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -5464,6 +5464,53 @@ 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 ""
(execute-kbd-macro (read-kbd-macro "M-x markdown-insert-table RET 2 RET 2 RET other RET id RET name RET"))
(should (string-equal (buffer-string) "|id|name|
|---|---|
| | |
| | |
"))))

;;; gfm-mode tests:

Expand Down

4 comments on commit 5252b71

@saf-dmitry
Copy link
Contributor

Choose a reason for hiding this comment

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

I think, the colon alignment options here are somehow misinterpreted.

A colon at the left of the separator line will make the column left-aligned; a colon on the right of the line will make the column right-aligned; colons at both sides means the column is center-aligned. If no colon is present (the default option), the default alignment of your system is selected (left in most cases).

@saf-dmitry
Copy link
Contributor

@saf-dmitry saf-dmitry commented on 5252b71 Nov 2, 2018

Choose a reason for hiding this comment

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

Here an alternative implementation:

(defun markdown-insert-table (&optional rows columns align)
  "Insert an empty pipe table.
Optional arguments ROWS, COLUMNS, and ALIGN specify number of
rows and columns and the column alignment."
  (interactive)
  (let* ((rows (or rows (string-to-number (read-string "Row size: "))))
         (columns (or columns (string-to-number (read-string "Column size: "))))
         (align (or align (read-string "Alignment ([l]eft, [r]ight, [c]enter): ")))
         (align (cond ((equal align "l") ":--")
                      ((equal align "r") "--:")
                      ((equal align "c") ":-:")
                      (t "---")))
         (pos (point))
         (indent (make-string (current-column) ?\ ))
         (line (concat
                (apply 'concat indent "|"
                       (make-list columns "  |")) "\n"))
         (hline (apply 'concat indent "|"
                       (make-list columns (concat align "|")))))
    (if (string-match
         "^[ \t]*$" (buffer-substring-no-properties
                     (point-at-bol) (point)))
        (beginning-of-line 1)
      (newline))
    (dotimes (_ rows) (insert line))
    (goto-char pos)
    (if (> rows 1)
        (progn
          (end-of-line 1) (insert (concat "\n" hline)) (goto-char pos)))
    (markdown-table-align)))

@jrblevin
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, Dmitry. You are right about the alignment.

Could you please submit this as PR? Perhaps the prompt for alignment could somehow indicate that simply pressing RET (or entering any other string), will give the default alignment?

@saf-dmitry
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please submit this as PR?

Done (PR #373)

Please sign in to comment.