John J. Camilleri
John J. Camilleri

Reputation: 4440

Editing Markdown pipe tables in Emacs

I like to write in Markdown and often find myself needing tables. Are there any good ways of editing Markdown's pipe tables in Emacs? I am referring to this kind of syntax:

| Header | Header | Right |
|--------|--------|------:|
|  Cell  |  Cell  |  $10  |
|  Cell  |  Cell  |  $20  |

I first tried Emacs' table mode which is nice, but is designed for "grid tables" which are not supported by Markdown (let's say in Github's Markdown).

There is also org-mode's table mode which can be used as a minor mode. This comes pretty close; but the intersections are now replaced by + characters and there is no support the alignment colon. So org-tblmode first gives me something like this:

| Header | Header | Right |
|--------+--------+-------|
| Cell   | Cell   | $10   |
| Cell   | Cell   | $20   |

which I then need to manually edit to the following (editing intersection characters and adding alignment colon):

| Header | Header | Right |
|--------|--------|------:|
| Cell   | Cell   | $10   |
| Cell   | Cell   | $20   |

Is there some may that org-tblmode can also handle this? What else do you use/suggest for editing Markdown's pipe tables in Emacs?

Upvotes: 31

Views: 9835

Answers (6)

practicalli-johnny
practicalli-johnny

Reputation: 1916

Markdown-mode package provides table support for GitHub flavored markdown.

Markdown-mode is part of Spacemacs (and probably other community configurations). Note that if using markdown and org layers in Spacemacs, orgtbl will hijack some key bindings, i.e. TAB and format tables using org syntax. Disabling orgtbl-mode removed the orgtbl highjack.

Upvotes: 0

Wilfred Hughes
Wilfred Hughes

Reputation: 31171

markdown-mode has the markdown-do command (bound to C-c C-d). When point is in a table, it pads all the columns so they're aligned.

https://github.com/jrblevin/markdown-mode#usage

Upvotes: 14

paul-g
paul-g

Reputation: 3877

This is what I use to deal with tables in markdown mode. May be a bit of a hack but works well enough for me. It adds a local hook to a markdown buffer which, on save, replaces "-+-" with "-|-" in the entire buffer.

(require 'org-table)

(defun cleanup-org-tables ()
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "-+-" nil t) (replace-match "-|-"))
    ))

(add-hook 'markdown-mode-hook 'orgtbl-mode)
(add-hook 'markdown-mode-hook
          (lambda()
            (add-hook 'after-save-hook 'cleanup-org-tables  nil 'make-it-local)))

Upvotes: 10

Alexander I.Grafov
Alexander I.Grafov

Reputation: 1408

OrgMode has nice function orgtbl-to-generic for converting orgmode' tables to alien formats. I found simple example defining custom convertor based on orgtbl-to-generic: https://gist.github.com/yryozo/5807243. Also see explanation of ORGTBL RECEIVE/SEND features here: http://dynamic-thinking.blogspot.ru/2009/11/orgtbl-mode.html. You need to define custom orgtbl-to-gfm function in Emacs and place it to your autoload then you may use minor mode orgtbl-mode for editing tables.

Small restriction: only right and left column alignments supported because Emacs OrgMode inside self doesn't supports central alignment.

See above sample org-mode source:

<!---
#+ORGTBL: SEND sample orgtbl-to-gfm
| Column 1      | Column 2 |
|---------------+----------|
|               | <l>      |
| Sample line 1 | 100      |
| Sample line 2 | 200      |
-->

And the result in markdown converted from the org-mode source:

<!--- BEGIN RECEIVE ORGTBL sample -->
| Column 1 | Column 2 |
|---|---|
| Sample line 1 | 100 |
| Sample line 2 | 200 |
<!--- END RECEIVE ORGTBL sample -->

Both them placed in the same file.

Upvotes: 11

Yann Vernier
Yann Vernier

Reputation: 15887

Org-mode is so enticingly close that I believe this is a case for its minor mode orgtbl with arbitrary syntax to convert. A line containing :-, -: or :-: would thus have its colons removed, and a second line with <l>, <c> and <r> added to inform org-mode of the alignment. The translation back shouldn't be too hard then. Sadly I'm not good enough at Emacs to write it right now. Incidentally, pandoc's markdown parser does accept + as generated by org-mode.

Having looked a bit more at it, I believe the existing hooks in org-mode require a bit much cruft around the text, and some editing functions are in order. Since this is my first attempt at emacs lisp, my emacs lisp to convert between orgtbl and markdown is nothing short of horrid, but I have managed to convert a test table.

Upvotes: 3

PascalVKooten
PascalVKooten

Reputation: 21451

Bind the function to a key for a right-aligned conversion of a region:

(defun markdown-regexp-right (beg end)
  (interactive "r")
  (replace-regexp "-\|[^-]" "-:|\n" nil beg end)    
  (replace-regexp "-\\+-" "-|-" nil beg end)
)

This will replace -+- with -|- and replace -| with :| in the right alignment case.

Note that \n is included, because this makes sure the other -|- don't get changed to -:|, but only -| when it is followed by a new-line.

Upvotes: 4

Related Questions