เป็นไปได้ไหมที่จะเพิ่มเทมเพลตอื่นที่ไม่ใช่ # + BEGIN_ # + END_ ไปยัง org-structure-template-alist


9

ฉันพบว่าองค์กรโครงสร้างแม่แบบ-alist เปลี่ยนแปลง (ฉันใช้ org โหมดรุ่น 9.2) #+BEGIN_<some block tag> #+END_<some block tag>เพื่อขยายโดยอัตโนมัติ ฉันสงสัยว่าเป็นไปได้หรือไม่ที่จะเพิ่มเทมเพลตชนิดอื่น เช่น:PROPERTIES:<some properties>:END:แม่แบบ

เป็นไปได้หรือฉันควรเปลี่ยนเป็นแพ็คเกจอื่น ๆ เช่น yasnippets หรือไม่?

คำตอบ:


9

UPDATE:

ไม่ได้สังเกตเห็นว่าองค์กรโหมด 9.2การเปลี่ยนแปลงกลไกของการขยายตัวแม่แบบที่org-structure-template-alistเป็นเพียงบล็อกที่กำหนดโดยและ"#+BEGIN_" "#+END_"และไม่ชอบรายการที่ชอบ("p" ":PROPERTIES:?:END:")อีกต่อไป

ตามที่กล่าวไว้ในลิงก์ด้านบนเทมเพลต "ซับซ้อน" อื่น ๆ สามารถกำหนดได้โดยฟังก์ชัน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 คุณพิมพ์<pและก็จะขยายไปยังสถานที่ให้บริการและออกจากจุดที่ตำแหน่งของTAB?

C-h v org-structure-template-alist RETและคุณสามารถหารายละเอียดเพิ่มเติมในเอกสารประกอบของตัวแปรโดยการพิมพ์


คำตอบที่เป็นประโยชน์มากขอบคุณ Btw, เป็น>สัญลักษณ์ของtempo-define-templateการพิมพ์ผิดหรือเปล่า? ถ้าไม่ใช่ .... บทบาทของมันในนิยามคืออะไร?
Dox

1
ดีใจที่มันช่วย :) ไม่ใช่การพิมพ์ผิดมันหมายถึงบรรทัดจะเยื้องtempo-define-templateเป็นในตัว defun ดูdocstringสำหรับรายละเอียด
whatacold

2

ความถี่ที่พวกเขาแนะนำการเปลี่ยนแปลงที่เข้ากันไม่ได้ในการปรับแต่งโหมดองค์กรนั้นน่าเสียดายจริงๆ

รหัสต่อไปนี้ให้แม่แบบโครงสร้างแบบเก่าของโหมด org ก่อนเวอร์ชัน 9.2 กลับ ฟังก์ชั่นorg-complete-expand-structure-templateนี้เป็นของแท้คัดลอกมาจากเวอร์ชั่น 9.1 และorg-try-structure-completionเป็นเวอร์ชั่นที่ได้รับการแก้ไขเล็กน้อยของหนึ่งใน 9.1 (ฉันเพิ่มประเภทตรวจสอบที่นั่น)

หลังจากติดตั้งรหัสนั้นคุณสามารถใช้เทมเพลตเก่าของคุณ
(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)
     ))
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.