是否可以将除#+ BEGIN_#+ END_以外的模板添加到org-structure-template-alist?


9

我注意到org-structure-template-alist已更改(我正在使用org-mode版本9.2)自动扩展为#+BEGIN_<some block tag> #+END_<some block tag>。我想知道是否可以添加其他种类的模板。例如:PROPERTIES:<some properties>:END:模板。

有可能还是应该转向yasnippets等其他软件包?

Answers:


9

更新:

尚未注意到Org Mode 9.2更改了模板扩展机制,其中org-structure-template-alist仅适用于"#+BEGIN_"和定义的块"#+END_"。并且("p" ":PROPERTIES:?:END:")不再接受像这样的条目。

如上面的链接所述,其他“复杂”模板可以由function定义tempo-define-template,并且org-tempo必须被加载((require 'org-tempo))。实际的条目org-structure-template-alist 被转换为org-tempo-tags经由tempo-define-template通过org-tempo,并且org-tempo-tags默认为:

(("<i" . tempo-template-org-index)
 ("<A" . tempo-template-org-ascii)
 ("<H" . tempo-template-org-html)
 ("<L" . tempo-template-org-latex)
 ("<v" . tempo-template-org-verse)
 ("<s" . tempo-template-org-src)
 ("<q" . tempo-template-org-quote)
 ("<l" . tempo-template-org-export-latex)
 ("<h" . tempo-template-org-export-html)
 ("<E" . tempo-template-org-export)
 ("<e" . tempo-template-org-example)
 ("<C" . tempo-template-org-comment)
 ("<c" . tempo-template-org-center)
 ("<a" . tempo-template-org-export-ascii)
 ("<I" . tempo-template-org-include))

对于您的情况,可以通过以下方式定义模板:

(tempo-define-template "my-property"
               '(":PROPERTIES:" p ":END:" >)
               "<p"
               "Insert a property tempate")

以下答案仅适用于9.2之前的组织模式

是的,您可以这样添加一个条目:

(add-to-list 'org-structure-template-alist '("p" ":PROPERTIES:?:END:"))

然后在org文件中,键入<pTAB,它将扩展为属性,并将该点留在的位置?

您可以通过键入以下内容在变量的文档中找到更多详细信息 C-h v org-structure-template-alist RET


非常有帮助的答案,谢谢。顺便说一句,错字>上的符号tempo-define-template吗?如果不是...。它在定义中的作用是什么?
Dox

1
很高兴能有所帮助:)不是错别字,这意味着该行会缩进,tempo-define-template是内置的defun,有关详细信息,请参阅文档字符串
whatacold

2

他们在组织模式的自定义中引入不兼容的更改的频率确实很可惜。

以下代码为您提供了9.2版之前的org-mode的旧结构模板。该函数org-complete-expand-structure-template是9.1和更高版本的纯副本org-try-structure-completion是的稍微修改的版本。(我在此处添加了类型检查。)

安装该代码后,您可以
(add-to-list 'org-structure-template-alist '("p" ":PROPERTIES:?:END:"))
再次使用旧模板。

(defvar org-structure-template-alist)

(defun org+-avoid-old-structure-templates (fun &rest args)
  "Call FUN with ARGS with modified `org-structure-template-alist'.
Use a copy of `org-structure-template-alist' with all
old structure templates removed."
  (let ((org-structure-template-alist
     (cl-remove-if
      (lambda (template)
        (null (stringp (cdr template))))
      org-structure-template-alist)))
    (apply fun args)))

(eval-after-load "org"
  '(when (version<= "9.2" (org-version))
     (defun org-try-structure-completion ()
       "Try to complete a structure template before point.
This looks for strings like \"<e\" on an otherwise empty line and
expands them."
       (let ((l (buffer-substring (point-at-bol) (point)))
         a)
     (when (and (looking-at "[ \t]*$")
            (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
            (setq a (assoc (match-string 1 l) org-structure-template-alist))
            (null (stringp (cdr a))))
       (org-complete-expand-structure-template (+ -1 (point-at-bol)
                              (match-beginning 1)) a)
       t)))

     (defun org-complete-expand-structure-template (start cell)
       "Expand a structure template."
       (let ((rpl (nth 1 cell))
         (ind ""))
     (delete-region start (point))
     (when (string-match "\\`[ \t]*#\\+" rpl)
       (cond
        ((bolp))
        ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
         (setq ind (buffer-substring (point-at-bol) (point))))
        (t (newline))))
     (setq start (point))
     (when (string-match "%file" rpl)
       (setq rpl (replace-match
              (concat
               "\""
               (save-match-data
             (abbreviate-file-name (read-file-name "Include file: ")))
               "\"")
              t t rpl)))
     (setq rpl (mapconcat 'identity (split-string rpl "\n")
                  (concat "\n" ind)))
     (insert rpl)
     (when (re-search-backward "\\?" start t) (delete-char 1))))

     (advice-add 'org-tempo-add-templates :around #'org+-avoid-old-structure-templates)

     (add-hook 'org-tab-after-check-for-cycling-hook #'org-try-structure-completion)

     (require 'org-tempo)
     ))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.