Within org-mode
, I use easy templates a lot. But I also use company-mode
and easy templates are not listed there, even though I've managed to add support for some of the org-mode
keywords (see here). Does anyone know how to achieve this?
2 Answers
You could add your own completion function to completion-at-point-functions
and add company-capf
to company-backends
:
(defun org-easy-template--completion-table (str pred action)
(pcase action
(`nil (try-completion str org-structure-template-alist pred))
(`t (all-completions str org-structure-template-alist pred))))
(defun org-easy-template--annotation-function (s)
(format " -> %s" (cadr (assoc s org-structure-template-alist))))
(defun org-easy-template-completion-function ()
(when (looking-back "^[ \t]*<\\([^ \t]*\\)" (point-at-bol))
(list
(match-beginning 1) (point)
'org-easy-template--completion-table
:annotation-function 'org-easy-template--annotation-function
:exclusive 'no)))
(defun add-easy-templates-to-capf ()
(add-hook 'completion-at-point-functions
'org-easy-template-completion-function nil t))
(add-hook 'org-mode-hook #'add-easy-templates-to-capf)
-
Thanks! Maybe you should also say that it should be added to the
org-mode-hook
:(defun add-easy-templates-to-capf () (add-hook 'completion-at-point-functions 'org-easy-template-completion-function nil t)) (add-hook 'org-mode-hook #'add-easy-templates-to-capf)
– TimmJun 12, 2017 at 19:28 -
Another minor thing: easy templates are still not listed after
<
. It doesn't bother me that much though, because I was more concerned with the actual completion.– TimmJun 12, 2017 at 19:34 -
You can either call
company-capf
after < interactively or setcompany-minimum-prefix-length
to 0 to get the completion immediately. Jun 13, 2017 at 9:38
Since I'haven't seen a direct answer to my question yet, here's a workaround using yasnippet
: simply define a snippet for every easy template.
M-x yas-new-snippet
- Insert name, key, and content like this:
# -*- mode: snippet -*- # name: latex export # key: l_ # -- #+BEGIN_EXPORT latex $0 #+END_EXPORT
- Store it in
your_snippet_folder/org-mode
.
One could also use the key <l
instead of l_
, but this doesn't play well together with company
.