1

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 2

1

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)
3
  • 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)
    – Timm
    Jun 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.
    – Timm
    Jun 12, 2017 at 19:34
  • You can either call company-capf after < interactively or set company-minimum-prefix-length to 0 to get the completion immediately.
    – mutbuerger
    Jun 13, 2017 at 9:38
0

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.

  1. M-x yas-new-snippet
  2. Insert name, key, and content like this:
    # -*- mode: snippet -*-
    # name: latex export
    # key: l_
    # --
    #+BEGIN_EXPORT latex
    $0
    #+END_EXPORT
  1. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.