org-tempo.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ;;; org-tempo.el --- Template expansion for Org structures -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Rasmus Pank Roulund <emacs at pank dot eu>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; URL: https://orgmode.org
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  20. ;;
  21. ;;; Commentary:
  22. ;;
  23. ;; Org Tempo reimplements completions of structure template before
  24. ;; point like `org-try-structure-completion' in Org v9.1 and earlier.
  25. ;; For example, strings like "<e" at the beginning of the line will be
  26. ;; expanded to an example block.
  27. ;;
  28. ;; All blocks defined in `org-structure-template-alist' are added as
  29. ;; Org Tempo shortcuts, in addition to keywords defined in
  30. ;; `org-tempo-keywords-alist'.
  31. ;;
  32. ;; `tempo' can also be used to define more sophisticated keywords
  33. ;; completions. See the section "Additional keywords" below for
  34. ;; examples.
  35. ;;
  36. ;;; Code:
  37. (require 'org-macs)
  38. (org-assert-version)
  39. (require 'tempo)
  40. (require 'cl-lib)
  41. (require 'org)
  42. (defvar org-structure-template-alist)
  43. (defgroup org-tempo nil
  44. "Template expansion of Org structures."
  45. :tag "Org structure"
  46. :group 'org)
  47. (defvar org-tempo-tags nil
  48. "Tempo tags for Org mode.")
  49. (defcustom org-tempo-keywords-alist
  50. '(("L" . "latex")
  51. ("H" . "html")
  52. ("A" . "ascii")
  53. ("i" . "index"))
  54. "Keyword completion elements.
  55. This is an alist of KEY characters and corresponding KEYWORDS,
  56. just like `org-structure-template-alist'. The tempo snippet
  57. \"<KEY\" will be expanded using the KEYWORD value. For example
  58. \"<L\" at the beginning of a line is expanded to \"#+latex:\".
  59. Do not use \"I\" as a KEY, as it is reserved for expanding
  60. \"#+include\"."
  61. :type '(repeat (cons (string :tag "Key")
  62. (string :tag "Keyword")))
  63. :package-version '(Org . "9.2"))
  64. ;;; Org Tempo functions and setup.
  65. (defun org-tempo-setup ()
  66. "Setup tempo tags and match finder for the current buffer."
  67. (org-tempo--update-maybe)
  68. (tempo-use-tag-list 'org-tempo-tags)
  69. (setq-local tempo-match-finder "^ *\\(<[[:word:]]+\\)\\="))
  70. (defun org-tempo--keys ()
  71. "Return a list of all Org Tempo expansion strings, like \"<s\"."
  72. (mapcar (lambda (pair) (format "<%s" (car pair)))
  73. (append org-structure-template-alist
  74. org-tempo-keywords-alist)))
  75. (defun org-tempo--update-maybe ()
  76. "Check and add new Org Tempo templates if necessary.
  77. In particular, if new entries were added to
  78. `org-structure-template-alist' or `org-tempo-keywords-alist', new
  79. Tempo templates will be added."
  80. (unless (cl-every (lambda (key) (assoc key org-tempo-tags))
  81. (org-tempo--keys))
  82. (org-tempo-add-templates)))
  83. (defun org-tempo-add-templates ()
  84. "Update all Org Tempo templates.
  85. Go through `org-structure-template-alist' and
  86. `org-tempo-keywords-alist' and update tempo templates."
  87. (mapc #'org--check-org-structure-template-alist '(org-structure-template-alist
  88. org-tempo-keywords-alist))
  89. (let ((keys (org-tempo--keys)))
  90. ;; Check for duplicated snippet keys and warn if any are found.
  91. (when (> (length keys) (length (delete-dups keys)))
  92. (warn
  93. "Duplicated keys in `org-structure-template-alist' and `org-tempo-keywords-alist'"))
  94. ;; Remove any keys already defined in case they have been updated.
  95. (setq org-tempo-tags
  96. (cl-remove-if (lambda (tag) (member (car tag) keys)) org-tempo-tags))
  97. (mapc #'org-tempo-add-block org-structure-template-alist)
  98. (mapc #'org-tempo-add-keyword org-tempo-keywords-alist)))
  99. (defun org-tempo-add-block (entry)
  100. "Add block entry from `org-structure-template-alist'."
  101. (let* ((key (format "<%s" (car entry)))
  102. (name (cdr entry))
  103. (special (member name '("src" "export")))
  104. (upcase? (string= (car (split-string name))
  105. (upcase (car (split-string name))))))
  106. (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
  107. `(,(format "#+%s_%s%s"
  108. (if upcase? "BEGIN" "begin")
  109. name
  110. (if special " " ""))
  111. ,(when special 'p) '> n ,(unless special 'p) n
  112. ,(format "#+%s_%s"
  113. (if upcase? "END" "end")
  114. (car (split-string name " ")))
  115. >)
  116. key
  117. (format "Insert a %s block" name)
  118. 'org-tempo-tags)))
  119. (defun org-tempo-add-keyword (entry)
  120. "Add keyword entry from `org-tempo-keywords-alist'."
  121. (let* ((key (format "<%s" (car entry)))
  122. (name (cdr entry)))
  123. (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
  124. `(,(format "#+%s: " name) p '>)
  125. key
  126. (format "Insert a %s keyword" name)
  127. 'org-tempo-tags)))
  128. (defun org-tempo-complete-tag (&rest _)
  129. "Look for a tag and expand it silently.
  130. Unlike to `tempo-complete-tag', do not give a signal if a partial
  131. completion or no match at all is found. Return nil if expansion
  132. didn't succeed."
  133. (org-tempo--update-maybe)
  134. ;; `tempo-complete-tag' returns its SILENT argument when there is no
  135. ;; completion available at all.
  136. (not (eq 'fail (tempo-complete-tag 'fail))))
  137. ;;; Additional keywords
  138. (defun org-tempo--include-file ()
  139. "Add #+include: and a file name."
  140. (let ((inhibit-quit t))
  141. (unless (with-local-quit
  142. (prog1 t
  143. (insert
  144. (format "#+include: %S "
  145. (file-relative-name
  146. (read-file-name "Include file: "))))))
  147. (insert "<I")
  148. (setq quit-flag nil))))
  149. (tempo-define-template "org-include"
  150. '((org-tempo--include-file)
  151. p >)
  152. "<I"
  153. "Include keyword"
  154. 'org-tempo-tags)
  155. ;;; Setup of Org Tempo
  156. ;;
  157. ;; Org Tempo is set up with each new Org buffer and potentially in the
  158. ;; current Org buffer.
  159. (add-hook 'org-mode-hook #'org-tempo-setup)
  160. (add-hook 'org-tab-before-tab-emulation-hook #'org-tempo-complete-tag)
  161. ;; Enable Org Tempo in all open Org buffers.
  162. (dolist (b (org-buffer-list 'files))
  163. (with-current-buffer b (org-tempo-setup)))
  164. (provide 'org-tempo)
  165. ;;; org-tempo.el ends here