org-tempo.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. (setq org-tempo-tags
  80. (cl-remove-if (lambda (tag) (member (car tag) keys)) org-tempo-tags))
  81. (mapc #'org-tempo-add-block org-structure-template-alist)
  82. (mapc #'org-tempo-add-keyword org-tempo-keywords-alist)))
  83. (defun org-tempo-add-block (entry)
  84. "Add block entry from `org-structure-template-alist'."
  85. (let* ((key (format "<%c" (car entry)))
  86. (name (cdr entry)))
  87. (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
  88. `(,(format "#+begin_%s " name) p '> n n
  89. ,(format "#+end_%s" (car (split-string name " ")))
  90. >)
  91. key
  92. (format "Insert a %s block" name)
  93. 'org-tempo-tags)))
  94. (defun org-tempo-add-keyword (entry)
  95. "Add keyword entry from `org-tempo-keywords-alist'."
  96. (let* ((key (format "<%c" (car entry)))
  97. (name (cdr entry)))
  98. (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
  99. `(,(format "#+%s: " name) p '>)
  100. key
  101. (format "Insert a %s keyword" name)
  102. 'org-tempo-tags)))
  103. (defun org-tempo-complete-tag (&rest _)
  104. "Look for a tag and expand it silently.
  105. Unlike to `tempo-complete-tag', do not give a signal if a partial
  106. completion or no match at all is found. Return nil if expansion
  107. didn't succeed."
  108. (cl-letf (((symbol-function 'ding) #'ignore))
  109. (tempo-complete-tag t)))
  110. ;;; Additional keywords
  111. (defun org-tempo--include-file ()
  112. "Ask for file name and take care of quit"
  113. (let ((inhibit-quit t))
  114. (unless (with-local-quit
  115. (prog1 t
  116. (insert
  117. (format "#+include: %S "
  118. (file-relative-name
  119. (read-file-name "Include file: "))))))
  120. (insert "<I")
  121. (setq quit-flag nil))))
  122. (tempo-define-template "org-include"
  123. '((org-tempo--include-file)
  124. p >)
  125. "<I"
  126. "Include keyword"
  127. 'org-tempo-tags)
  128. ;;; Setup of Org Tempo
  129. ;;
  130. ;; Org Tempo is set up with each new Org buffer and potentially in the
  131. ;; current Org buffer.
  132. ;;
  133. ;; Tempo templates can only be added after Org is loaded as
  134. ;; `org-structure-template-alist' must be loaded.
  135. (add-hook 'org-mode-hook 'org-tempo-setup)
  136. (add-hook 'org-tab-before-tab-emulation-hook 'org-tempo-complete-tag)
  137. ;; Enable Org Tempo in all open Org buffers.
  138. (dolist (b (org-buffer-list 'files))
  139. (with-current-buffer b (org-tempo-setup)))
  140. (eval-after-load 'org
  141. '(org-tempo-add-templates))
  142. (provide 'org-tempo)
  143. ;;; org-tempo.el ends here