org-tempo.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; org-tempo.el --- Template expansion for Org structures -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2018 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Rasmus Pank Roulund <emacs at pank dot eu>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://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. ;; additional details.
  35. ;;
  36. ;;; Code:
  37. (require 'tempo)
  38. (require 'cl-lib)
  39. (declare-function org-buffer-list "org" (&optional predicate exclude-tmp))
  40. (defvar org-structure-template-alist)
  41. (defgroup org-tempo nil
  42. "Options for template expansion of Org structures"
  43. :tag "Org structure"
  44. :group 'org)
  45. (defvar org-tempo-tags nil
  46. "Tempo tags for Org mode")
  47. (defcustom org-tempo-keywords-alist
  48. '((?L . "latex")
  49. (?H . "html")
  50. (?A . "ascii")
  51. (?i . "index"))
  52. "Keyword completion elements.
  53. Like `org-structure-template-alist' this alist of KEY characters
  54. and KEYWORD. The tempo snippet \"<KEY\" is expand to the KEYWORD
  55. value.
  56. For example \"<l\" at the beginning of a line is expanded to
  57. #+latex:"
  58. :group 'org-tempo
  59. :type '(repeat (cons (character :tag "Key")
  60. (string :tag "Keyword")))
  61. :package-version '(Org . "9.2"))
  62. ;;; Org Tempo functions and setup.
  63. (defun org-tempo-setup ()
  64. (org-tempo-add-templates)
  65. (tempo-use-tag-list 'org-tempo-tags)
  66. (setq-local tempo-match-finder "^ *\\(<[[:word:]]\\)\\="))
  67. (defun org-tempo-add-templates ()
  68. "Update all Org Tempo templates.
  69. Goes through `org-structure-template-alist' and
  70. `org-tempo-keywords-alist'."
  71. (let ((keys (mapcar (lambda (pair) (format "<%c" (car pair)))
  72. (append org-structure-template-alist
  73. org-tempo-keywords-alist))))
  74. ;; Check for duplicated snippet keys and warn if any are found.
  75. (when (> (length keys) (length (delete-dups keys)))
  76. (warn
  77. "Duplicated keys in `org-structure-template-alist' and `org-tempo-keywords-alist'"))
  78. ;; Remove any keys already defined in case they have been updated.
  79. (mapc (lambda (key)
  80. (if (assoc-string key org-tempo-tags)
  81. (setq org-tempo-tags
  82. (delete (assoc-string key org-tempo-tags)
  83. org-tempo-tags))))
  84. keys)
  85. (mapc #'org-tempo-add-block org-structure-template-alist)
  86. (mapc #'org-tempo-add-keyword org-tempo-keywords-alist)))
  87. (defun org-tempo-add-block (entry)
  88. "Add block entry from `org-structure-template-alist'."
  89. (let* ((key (format "<%c" (car entry)))
  90. (name (cdr entry)))
  91. (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
  92. `(,(format "#+begin_%s " name) p '> n n
  93. ,(format "#+end_%s" (car (split-string name " ")))
  94. >)
  95. key
  96. (format "Insert a %s block" name)
  97. 'org-tempo-tags)))
  98. (defun org-tempo-add-keyword (entry)
  99. "Add keyword entry from `org-tempo-keywords-alist'."
  100. (let* ((key (format "<%c" (car entry)))
  101. (name (cdr entry)))
  102. (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
  103. `(,(format "#+%s: " name) p '>)
  104. key
  105. (format "Insert a %s keyword" name)
  106. 'org-tempo-tags)))
  107. ;;; Additional keywords
  108. (defun org-tempo--include-file ()
  109. "Ask for file name and take care of quit"
  110. (let ((inhibit-quit t))
  111. (unless (with-local-quit
  112. (prog1 t
  113. (insert
  114. (format "#+include: %S "
  115. (file-relative-name
  116. (read-file-name "Include file: "))))))
  117. (insert "<I")
  118. (setq quit-flag nil))))
  119. (tempo-define-template "org-include"
  120. '((org-tempo--include-file)
  121. p >)
  122. "<I"
  123. "Include keyword"
  124. 'org-tempo-tags)
  125. ;;; Setup of Org Tempo
  126. ;;
  127. ;; Org Tempo is set up with each new Org buffer and potentially in the
  128. ;; current Org buffer.
  129. ;;
  130. ;; Tempo templates can only be added after Org is loaded as
  131. ;; `org-structure-template-alist' must be loaded.
  132. (add-hook 'org-mode-hook 'org-tempo-setup)
  133. (add-hook 'org-tab-before-tab-emulation-hook
  134. 'tempo-complete-tag)
  135. ;; Enable Org Tempo in all open Org buffers.
  136. (dolist (b (org-buffer-list))
  137. (with-current-buffer b (org-tempo-setup)))
  138. (eval-after-load 'org
  139. '(org-tempo-add-templates))
  140. (provide 'org-tempo)
  141. ;;; org-tempo.el ends here