以编程方式生成组织模式对象 [英] Generate Org-mode objects programmatically

查看:94
本文介绍了以编程方式生成组织模式对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成包含Org-mode文本的字符串,而不需要对语法进行硬编码。例如,我想运行一个函数,如(org-generate(org-generate-headingheading(org-generate-plain-list'(foobarbaz)) ),它将返回:

  *标题
- foo
- bar
- baz

换句话说,我想创建任意的组织模式文档复杂性,不需要微调管理功能,如星号和缩进,只能通过调用带有参数的函数返回一些Org对象。可以通过 org-element / p>

解决方案

INITIAL (2014年3月14日):第一稿草稿。



编辑(2014年3月15日):创建并修改名为的函数示例绑定变量 org-file 必须与现有的组织模式文件一致,允许变量 main-heading 子标题不是b由于在以'(开始的列表中使用变量)的明显限制,即在这种情况下,这两个变量不被识别,所以使用eing。 org-capture.el 中的 org-capture 已经被修改为包含 em>函数 org-capture-set-plist ,它们被修改为消除了前面的两个元素org-capture-entry (aka org-capture-templates ) - 两个条目(即:key 和用于从用户界面手动选择一个模板,而不是以编程方式生成组织缓冲区而不需要使用:description 示例。此外,与手动选择模板相关的功能组织抓取的部分已被删除。



EDIT (2014年3月16日):根据@sds和@lunaryorn提供的经验,修改的变量和列表处理如下:https://stackoverflow.com/a/22440518/2112489 添加了四个可选传入变量 - (1) 主标题; (2) sub-heading-headline ; (3) 子标题完成;和(4) plain-list 。该示例现在可以以交互方式工作,或通过使用以下格式评估函数:(org-generatePROJECT论文**论文:属性:\\\
:END: '(abc))



编辑(2014年3月19日) org-generate 现在是一个非交互式函数,需要传入变量 - 文档字符串已被更新。创建一个名为的函数示例,它使用新格式 org-generate






  ;;示例:
;; (org-generate'entry/Users/HOME/Desktop/myproject.orgPROJECT论文**论文:属性:\\\
:END:)
;; (org-generate'item/Users/HOME/Desktop/myproject.orgPROJECT论文nil'(abc))
/ pre>




 (defun example()
(interactive)
(let *(
(org-file/Users/HOME/Desktop/myproject.org)
(level-oneTASKS)
(level-two
Active [\#A]以编程方式生成组织模式对象)
(full-level-two(concat
** Active [\# A]以编程方式生成组织模式对象\
DEADLINE:<%<%Y-%m-%d%a>> \\\

:PROPERTIES: \\
:ToodledoFolder:TASKS\\\

:END:))
(plain-list'(foobarbaz)))
(org-generate'entry org-file level-one level-two full-level-two)
(org-generate'item org-file level-one level-two nil plain-list)) )

(defun org-generate(type org-file level-one
&可选level-two full-level-two plain-list)
org-capture-entry的形成选项与org-capture-templates类似。
但是,不使用前两个元素(即`:key`和`:description`)。
有关更多信息,请参阅变量org-capture-templates的doc-string。
(1)`type`:必需 - '条目| 'item
(2)`org-file`:必需 - 组织模式文件的路径。
(3)`level-one`:必需 - 主标题。
(4)`level-two`:可选 - 子标题标题(仅)。
(5)`full-level-two`:可选 - 完整的子标题。
(6)`plain-list`:可选 - 列表。
示例:
`(org-generate'entry org-file level-one level-two full-level-two)`
`(org-generate'item org-file level-one
(需要组织捕获)
(let(org-capture-entry)
(cond
((eq类型'条目) )
(setq org-capture-entry
`(entry
(file + headline,org-file,level-one)
,full-level-two:empty-lines
((eq类型'item)
(setq org-capture-entry
`(item
(file + olp,org-file,level-one,level-two)
nil:空行1:立即完成t))
(mapcar(lambda(x)
(progn
(setcar(nthcdr 2 org-capture-entry)x)
(lawlist-org-capture)))
plain-list)))))

(defun lawlist-org-capture()
(let *((ori​​g-buf(current-buffer))
(annotati on(if(and(boundp'org-capture-link-is-already-stored)
org-capture-link-is-already-stored)
(plist-get org-store-link- plist:annotation)
(ignore-errors(org-store-link nil))))
(条目org-capture-entry)
initial)
(setq initial org-capture-initial
(和(org-region-active-p)
(buffer-substring(point)(mark)))))
(when(stringp initial)
(remove-text-properties 0(length initial)'(只读t)initial))
(when(stringp annotation))
(remove-text-properties 0(length annotation)
$(只读t)注释))
(setq org-capture-plist(copy-sequence(nthcdr 3 entry)))
(org-capture-put:target(nth 1 entry )
(let((txt(nth 2 entry))(type(or(nth 0 entry)'entry)))
(when(或(not txt))(和(stringp txt)不是(string-match\\S-txt))))
(cond
((eq type'item)(setq txt - %?))
((eq type'checkitem)(setq txt - []%?))
((eq type'table-line)(setq txt %? | |))
((成员类型'(nil entry))(setq txt*%?\\\
%a))))
(org-capture-put:template txt:type类型))
(org-capture-get-template)
(org-capture-put:original-buffer orig-buf
:original-file(或(buffer-file-name orig -buf)
(和(featurep'dired)
(car(rassq orig-buf
dired-buffers))))
:original-file-nondirectory
(和(buffer-file-name orig-buf)
(file-name-nondirectory
(buffer-file-name orig-buf)))
:注释注释
:初始初始
:return-to-wconf(current-window-configuration)
:default-time
(或org-overriding-default-time
(org-current-time ))
(org-capture-set-target-location)
(条件案例错误
(org-capture-put:template(org-capture-fill-template))
((错误退出)
(if(get-buffer* Capture) *)(kill-buffer* Capture *))
(错误捕获中止:%s错误)))
(setq org-capture-clock-keep(org-capture-get :clock-keep))
(条件案例错误
(org-capture-place-template
(等于(car(org-capture-get:target))) b $ b((错误退出)
(if(和(buffer-base-buffer(current-buffer))
(string-match\\`CAPTURE-(buffer-name) )
(kill-buffer(current-buffer)))
(set-window-configuration(org-capture-get:return-to-wconf))
(错误 )))
(if(和(derived-mode-p'org-mode)
(org-capture-get:clock-in))
(condition-case nil
(progn
(if(org-clock-is-active)
(org-capture-put:interrupt-clock
(copy-marker org-clock-marker)))
(org-clock-in)
(org-set-local'org-capture-clock-was-started t))
(错误
无法启动clo ck在这个捕获缓冲区)))
(if(org-capture-get:immediate-finish)
(org-capture-finalize))))






示例http://www.lawlist.com/images/programmatic_org_file.png


I want to generate strings containing Org-mode text without actually hard-coding the syntax. For example i want to run a function such as (org-generate (org-generate-heading "heading" (org-generate-plain-list '("foo" "bar" "baz"))) and it will return:

* heading
  - foo
  - bar
  - baz

In other words, i want to create Org-mode documents of arbitrary complexity without micromanaging syntactic features like asterisks and indentation, only via calling functions with parameters, that return some Org objects. Is it possible to do that? Maybe via org-element?

解决方案

INITIAL (March 14, 2014):  First rough draft.

EDIT (March 15, 2014):  Created and revised the function named example. The path of the let-bound variable org-file must coincide with an existing org-mode file. The let-bound variables main-heading and sub-heading are not being used at this time due to an apparent limitation with using variables in a list that begins with '( -- i.e., those two variables are not recognized under this circumstance. The function org-capture from org-capture.el has been modified to include the contents of the function org-capture-set-plist, which in turn has been modified to eliminate the first two elements of org-capture-entry (aka org-capture-templates) -- both entries (i.e., :key and :description) are for manually selecting a template from the user-interface, and are not needed when generating an org buffer programmatically as is being done with this example. In addition, the portions of the function org-capture relating to manually selecting a template have been removed.

EDIT (March 16, 2014):  Revised variables and list handling based upon lessons provided by @sds and @lunaryorn in the following thread: https://stackoverflow.com/a/22440518/2112489  Added four optional incoming variables -- (1) main-heading; (2) sub-heading-headline; (3) sub-heading-complete; and (4) plain-list. The example now work either interactively, or by evaluating the function using the following format:  (org-generate "PROJECT" "Thesis" "** Thesis\n:PROPERTIES:\n:END:" '("a" "b" "c"))

EDIT (March 19, 2014):  org-generate is now a non-interactive function that requires incoming variables -- the doc-string has been updated. Created a function named example that utilizes the new format for org-generate.


;; EXAMPLES:
;; (org-generate 'entry "/Users/HOME/Desktop/myproject.org" "PROJECT" "Thesis" "** Thesis\n   :PROPERTIES:\n   :END:")
;; (org-generate 'item "/Users/HOME/Desktop/myproject.org" "PROJECT" "Thesis" nil '("a" "b" "c"))


(defun example ()
(interactive)
  (let* (
      (org-file "/Users/HOME/Desktop/myproject.org")
      (level-one "TASKS")
      (level-two
        "Active [\#A] Generate Org-mode objects programmatically.")
      (full-level-two (concat
        "** Active [\#A] Generate Org-mode objects programmatically.\n"
        "   DEADLINE: <%<%Y-%m-%d %a>>\n"
        "   :PROPERTIES:\n"
        "   :ToodledoFolder: TASKS\n"
        "   :END:"))
       (plain-list '("foo" "bar" "baz")) )
    (org-generate 'entry org-file level-one level-two full-level-two)
    (org-generate 'item org-file level-one level-two nil plain-list) ))

(defun org-generate (type org-file level-one
  &optional level-two full-level-two plain-list)
"Formating options for `org-capture-entry` are similar to `org-capture-templates`.
However, the first two elements (i.e., `:key` and `:description`) are NOT used.
Please see the doc-string of the variable `org-capture-templates` for more info.
  (1) `type`:  required -- 'entry | 'item
  (2) `org-file`:  required -- path to the org-mode file.
  (3) `level-one`:  required -- main heading.
  (4) `level-two`:  optional -- sub-heading headline (only).
  (5) `full-level-two`:  optional -- complete sub-heading.
  (6) `plain-list`:  optional -- a list.
EXAMPLES:
  `(org-generate 'entry org-file level-one level-two full-level-two)`
  `(org-generate 'item org-file level-one level-two nil plain-list)` "
  (require 'org-capture)
  (let (org-capture-entry)
    (cond
      ((eq type 'entry)
        (setq org-capture-entry
          `(entry
            (file+headline ,org-file ,level-one)
              ,full-level-two :empty-lines 1 :immediate-finish t))
        (lawlist-org-capture))
      ((eq type 'item)
        (setq org-capture-entry
          `(item
            (file+olp ,org-file ,level-one ,level-two)
              nil :empty-lines 1 :immediate-finish t))
        (mapcar (lambda (x)
          (progn
            (setcar (nthcdr 2 org-capture-entry) x)
            (lawlist-org-capture) ))
          plain-list)))))

(defun lawlist-org-capture ()
    (let* ((orig-buf (current-buffer))
     (annotation (if (and (boundp 'org-capture-link-is-already-stored)
        org-capture-link-is-already-stored)
         (plist-get org-store-link-plist :annotation)
       (ignore-errors (org-store-link nil))))
     (entry org-capture-entry)
     initial)
      (setq initial (or org-capture-initial
      (and (org-region-active-p)
           (buffer-substring (point) (mark)))))
      (when (stringp initial)
  (remove-text-properties 0 (length initial) '(read-only t) initial))
      (when (stringp annotation)
  (remove-text-properties 0 (length annotation)
        '(read-only t) annotation))
  (setq org-capture-plist (copy-sequence (nthcdr 3 entry)))
  (org-capture-put :target (nth 1 entry))
  (let ((txt (nth 2 entry)) (type (or (nth 0 entry) 'entry)))
    (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
      (cond
       ((eq type 'item) (setq txt "- %?"))
       ((eq type 'checkitem) (setq txt "- [ ] %?"))
       ((eq type 'table-line) (setq txt "| %? |"))
       ((member type '(nil entry)) (setq txt "* %?\n  %a"))))
    (org-capture-put :template txt :type type))
  (org-capture-get-template)
  (org-capture-put :original-buffer orig-buf
       :original-file (or (buffer-file-name orig-buf)
              (and (featurep 'dired)
             (car (rassq orig-buf
                   dired-buffers))))
       :original-file-nondirectory
       (and (buffer-file-name orig-buf)
            (file-name-nondirectory
             (buffer-file-name orig-buf)))
       :annotation annotation
       :initial initial
       :return-to-wconf (current-window-configuration)
       :default-time
       (or org-overriding-default-time
           (org-current-time)))
  (org-capture-set-target-location)
  (condition-case error
      (org-capture-put :template (org-capture-fill-template))
    ((error quit)
     (if (get-buffer "*Capture*") (kill-buffer "*Capture*"))
     (error "Capture abort: %s" error)))
  (setq org-capture-clock-keep (org-capture-get :clock-keep))
    (condition-case error
        (org-capture-place-template
         (equal (car (org-capture-get :target)) 'function))
      ((error quit)
       (if (and (buffer-base-buffer (current-buffer))
          (string-match "\\`CAPTURE-" (buffer-name)))
     (kill-buffer (current-buffer)))
       (set-window-configuration (org-capture-get :return-to-wconf))
       (error "Error.")))
    (if (and (derived-mode-p 'org-mode)
       (org-capture-get :clock-in))
        (condition-case nil
      (progn
        (if (org-clock-is-active)
      (org-capture-put :interrupted-clock
           (copy-marker org-clock-marker)))
        (org-clock-in)
        (org-set-local 'org-capture-clock-was-started t))
    (error
     "Could not start the clock in this capture buffer")))
    (if (org-capture-get :immediate-finish)
        (org-capture-finalize))))


Example http://www.lawlist.com/images/programmatic_org_file.png

这篇关于以编程方式生成组织模式对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆