org-tempo.el 5.0 KB

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