【问题标题】:Org-Mode table to s-expressions组织模式表到 s 表达式
【发布时间】:2010-01-18 13:13:20
【问题描述】:

我想从 Org-Mode 表导出到 s-表达式。

| first  | second | thrid  |
|--------+--------+--------|
| value1 | value2 | value3 |
| value4 | value5 | value6 |

会变成:

((:FIRST "value1" :SECOND "value2" :THIRD "value3")
 (:FIRST "value4" :SECOND "value5" :THIRD "value6"))

如果还没有这样的设置,我打算编写这样的设置,但我想在开始重新发明轮子之前先利用 stackoverflow。

【问题讨论】:

    标签: emacs org-mode s-expression


    【解决方案1】:

    这可以解决问题。它具有最少的错误检查。

    要使用的接口是编程接口:

    (org-table-to-sexp <location-of-beginning-of-table> <location-of-end-of-table>)
    

    在这种情况下,它会返回您请求的性别。

    如果想要交互使用,可以调用下面的命令对region中的表进行操作。所以,在表格的开头设置标记,移动到末尾,然后输入:

    M-x insert-org-table-to-sexp
    

    这将在当前缓冲区中的表之后立即插入所需的 sexp。

    代码如下:

    (defun org-table-to-sexp-parse-line ()
      "Helper, returns the current line as a list of strings"
      (save-excursion
        (save-match-data
          (let ((result nil)
                (end-of-line (save-excursion (end-of-line) (point))))
            (beginning-of-line)
            (while (re-search-forward "\\([^|]*\\)|" end-of-line t)
              (let ((match (mapconcat 'identity (split-string (match-string-no-properties 1)) " ")))
                (if (< 0 (length match))
                    ;; really want to strip spaces from front and back
                    (push match result))))
            (reverse result)))))
    
    (require 'cl)
    (defun org-table-to-sexp (b e)
      "Parse an org-mode table to sexp"
      (save-excursion
        (save-match-data
          (goto-char b)
          (let ((headers (mapcar
                          (lambda (str)
                            (make-symbol (concat ":" (upcase str))))
                          (org-table-to-sexp-parse-line)))
                (sexp nil))
            (forward-line 1)                ;skip |--+--+--| line
            (while (< (point) e)
              (forward-line 1)
              (let ((line-result nil))
                (mapcar* (lambda (h e)
                           (push h line-result)
                           (push e line-result))
                         headers
                         (org-table-to-sexp-parse-line))
                (if line-result
                    (push (reverse line-result)
                          sexp))))
            sexp))))
    
    (defun insert-org-table-to-sexp (b e)
      "Convert the table specified by the region and insert the sexp after the table"
      (interactive "r")
      (goto-char (max b e))
      (print (org-table-to-sexp b e) (current-buffer)))
    

    【讨论】:

    • 我不确定如何使用该功能。我将它添加到我的 .emacs eval'd 中,但我不知道如何使用它。如果您能详细说明如何使用它,我将不胜感激。
    • @Chris 我添加了一个交互式命令,您只需键入即可。我认为更大的问题是:你想如何使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2012-12-24
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多