Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 622
John J. Camilleri Avatar asked Jan 11 '13 09:01

John J. Camilleri


2 Answers

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.

like image 68
Alexander I.Grafov Avatar answered Oct 13 '22 00:10

Alexander I.Grafov


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))) 
like image 26
paul-g Avatar answered Oct 13 '22 01:10

paul-g