org-tempo: Silence byte-compiler
[org-mode/org-tableheadings.git] / lisp / org-tempo.el
blob5dd665f6bb21be4b336793e762705cab57b96389
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)
44 (defvar org-structure-template-alist)
47 (defgroup org-tempo nil
48 "Options for template expansion of Org structures"
49 :tag "Org structure"
50 :group 'org)
52 (defvar org-tempo-tags nil
53 "Tempo tags for Org mode")
55 (defcustom org-tempo-keywords-alist
56 '((?L . "latex")
57 (?H . "html")
58 (?A . "ascii")
59 (?i . "index"))
60 "Keyword completion elements.
62 Like `org-structure-template-alist' this alist of KEY characters
63 and KEYWORD. The tempo snippet \"<KEY\" is expand to the KEYWORD
64 value.
66 For example \"<l\" at the beginning of a line is expanded to
67 #+latex:"
68 :group 'org-tempo
69 :type '(repeat (cons (character :tag "Key")
70 (string :tag "Keyword")))
71 :package-version '(Org . "9.2"))
75 ;;; Org Tempo functions and setup.
77 (defun org-tempo-setup ()
78 (org-tempo-add-templates)
79 (tempo-use-tag-list 'org-tempo-tags)
80 (setq-local tempo-match-finder "^ *\\(<[[:word:]]\\)\\="))
82 (defun org-tempo-add-templates ()
83 "Update all Org Tempo templates.
85 Goes through `org-structure-template-alist' and
86 `org-tempo-keywords-alist'."
87 (let ((keys (mapcar (lambda (pair) (format "<%c" (car pair)))
88 (append org-structure-template-alist
89 org-tempo-keywords-alist))))
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'"))
95 ;; Remove any keys already defined in case they have been updated.
96 (mapc (lambda (key)
97 (if (assoc-string key org-tempo-tags)
98 (setq org-tempo-tags
99 (delete (assoc-string key org-tempo-tags)
100 org-tempo-tags))))
101 keys)
102 (mapc #'org-tempo-add-block org-structure-template-alist)
103 (mapc #'org-tempo-add-keyword org-tempo-keywords-alist)))
105 (defun org-tempo-add-block (entry)
106 "Add block entry from `org-structure-template-alist'."
107 (let* ((key (format "<%c" (car entry)))
108 (name (cdr entry)))
109 (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
110 `(,(format "#+begin_%s " name) p '> n n
111 ,(format "#+end_%s" (car (split-string name " ")))
114 (format "Insert a %s block" name)
115 'org-tempo-tags)))
117 (defun org-tempo-add-keyword (entry)
118 "Add keyword entry from `org-tempo-keywords-alist'."
119 (let* ((key (format "<%c" (car entry)))
120 (name (cdr entry)))
121 (tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
122 `(,(format "#+%s: " name) p '>)
124 (format "Insert a %s keyword" name)
125 'org-tempo-tags)))
127 ;;; Additional keywords
129 (defun org-tempo--include-file ()
130 "Ask for file name and take care of quit"
131 (let ((inhibit-quit t))
132 (unless (with-local-quit
133 (prog1 t
134 (insert
135 (format "#+include: %S "
136 (file-relative-name
137 (read-file-name "Include file: "))))))
138 (insert "<I")
139 (setq quit-flag nil))))
141 (tempo-define-template "org-include"
142 '((org-tempo--include-file)
143 p >)
144 "<I"
145 "Include keyword"
146 'org-tempo-tags)
149 ;;; Setup of Org Tempo
151 ;; Org Tempo is set up with each new Org buffer and potentially in the
152 ;; current Org buffer.
154 ;; Tempo templates can only be added after Org is loaded as
155 ;; `org-structure-template-alist' must be loaded.
157 (add-hook 'org-mode-hook 'org-tempo-setup)
158 (add-hook 'org-tab-before-tab-emulation-hook
159 'tempo-complete-tag)
161 ;; Enable Org Tempo in all open Org buffers.
162 (dolist (b (org-buffer-list))
163 (with-current-buffer b (org-tempo-setup)))
165 (eval-after-load 'org
166 '(org-tempo-add-templates))
168 (provide 'org-tempo)
170 ;;; org-tempo.el ends here