在Emacs中编辑Markdown管道表

Joh*_*eri 24 emacs markdown org-mode

我喜欢写Markdown,经常发现自己需要桌子.有没有什么好的方法可以在Emacs 中编辑Markdown的管道表?我指的是这种语法:

| Header | Header | Right |
|--------|--------|------:|
|  Cell  |  Cell  |  $10  |
|  Cell  |  Cell  |  $20  |
Run Code Online (Sandbox Code Playgroud)

我首先尝试了Emacs的表模式,这很好,但是设计用于Markdown不支持的"网格表"(让我们说在Github的Markdown中).

还有org-mode的表格模式,可以用作次要模式.这非常接近; 但交叉点现在被+字符替换,并且不支持对齐冒号.所以org-tblmode首先给我这样的东西:

| Header | Header | Right |
|--------+--------+-------|
| Cell   | Cell   | $10   |
| Cell   | Cell   | $20   |
Run Code Online (Sandbox Code Playgroud)

然后我需要手动编辑到以下内容(编辑交集字符并添加对齐冒号):

| Header | Header | Right |
|--------|--------|------:|
| Cell   | Cell   | $10   |
| Cell   | Cell   | $20   |
Run Code Online (Sandbox Code Playgroud)

是否有一些可以org-tblmode处理这个?您还有什么用于/建议在Emacs中编辑Markdown的管道表?

Ale*_*fov 11

OrgMode具有很好的功能orgtbl-to-generic,可以将orgmode'表转换为外来格式.我找到了基于以下内容定义自定义转换器的简单示例orgtbl-to-generic:https://gist.github.com/yryozo/5807243.另请参阅此处有关ORGTBL RECEIVE/SEND功能的说明:http://dynamic-thinking.blogspot.ru/2009/11/orgtbl-mode.html .您需要orgtbl-to-gfm在Emacs中定义自定义功能并将其放入自动加载,然后您可以使用次模式orgtbl-mode编辑表格.

小限制:仅支持左右列对齐,因为内部的Emacs OrgMode不支持中心对齐.

请参阅上面的示例组织模式源:

<!---
#+ORGTBL: SEND sample orgtbl-to-gfm
| Column 1      | Column 2 |
|---------------+----------|
|               | <l>      |
| Sample line 1 | 100      |
| Sample line 2 | 200      |
-->
Run Code Online (Sandbox Code Playgroud)

并且从组织模式源转换为markdown的结果:

<!--- BEGIN RECEIVE ORGTBL sample -->
| Column 1 | Column 2 |
|---|---|
| Sample line 1 | 100 |
| Sample line 2 | 200 |
<!--- END RECEIVE ORGTBL sample -->
Run Code Online (Sandbox Code Playgroud)

它们都放在同一个文件中.


Wil*_*hes 10

markdown-modemarkdown-do命令(绑定到C-c C-d)。当点位于表格中时,它会填充所有列,以便它们对齐。

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


pau*_*l-g 7

这是我用来处理降价模式下的表格.可能是一个黑客,但对我来说效果很好.它为markdown缓冲区添加了一个本地钩子,在保存时,在整个缓冲区中用" - | - "替换" - + - ".

(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)))
Run Code Online (Sandbox Code Playgroud)


Pas*_*ten 4

将函数绑定到一个键以实现区域的右对齐转换:

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

这将在正确对齐的情况下替换-+-为和-|-替换。-|:|

请注意,包含 \n ,因为这可以确保另一个-|-不会更改为-:|,但仅-|当它后面跟着new-line.