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