Emacs 将组织模式表转换为s表达式
我想从组织模式表导出到s表达式Emacs 将组织模式表转换为s表达式,emacs,org-mode,s-expression,Emacs,Org Mode,S Expression,我想从组织模式表导出到s表达式 | first | second | thrid | |--------+--------+--------| | value1 | value2 | value3 | | value4 | value5 | value6 | 将变成: ((:FIRST "value1" :SECOND "value2" :THIRD "value3") (:FIRST "value4" :SECOND "value5" :THIRD "value6")) 我计划写这样
| first | second | thrid |
|--------+--------+--------|
| value1 | value2 | value3 |
| value4 | value5 | value6 |
将变成:
((:FIRST "value1" :SECOND "value2" :THIRD "value3")
(:FIRST "value4" :SECOND "value5" :THIRD "value6"))
我计划写这样一个设置,如果它还不存在的话,但我想在我开始重新发明轮子之前,我会先打开stackoverflow。它有最小的错误检查 要使用的接口可以是编程接口:
(org-table-to-sexp <location-of-beginning-of-table> <location-of-end-of-table>)
这将在当前缓冲区中的表之后立即插入所需的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)))
(将组织表定义为sexp解析行()
Helper,以字符串列表的形式返回当前行
(省去远足
(保存匹配数据
(让((结果为零)
(行尾(保存偏移(行尾)(点)))
(行首)
(同时(向前搜索“\\([^ |]*\\)|”第t行末尾)
(let((匹配(mapcontat'identity(分割字符串(匹配字符串没有属性1))“”))
(如果(<0(长度匹配))
;真的想从前面和后面去掉空间吗
(推送匹配结果)
(反向结果((())))
(要求“cl”)
(将组织表改为sexp(b e)
“将组织模式表解析为sexp”
(省去远足
(保存匹配数据
(转到字符b)
(让)标题(地图车)
(兰姆达街)
(制造符号(concat):(上例str)))
(组织表到sexp解析行)))
(无性别)
(前进行1);跳过|--+--+--|行
(而(<(点)e)
(前线1)
(let((行结果为零))
(mapcar*(lambda(HE)
(按h线结果)
(按e线结果)
标题
(组织表到sexp解析行)
(如果行结果为
(推送(反向线路结果)
sexp)
sexp)
(defun将组织表插入sexp(b e)
“转换区域指定的表,并在表后插入sexp”
(交互式“r”)
(转到字符(最大b e))
(打印(组织表到sexp b e)(当前缓冲区)))
我不确定如何使用该功能。我将它添加到我的.emacs评估中,但我不知道如何使用它。如果您能详细介绍如何使用它,我将非常感激。@Chris我添加了一个交互式命令,您只需键入即可。我认为更大的问题是:你想如何使用它?
(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)))