Allow file template :trigger to be a function

This commit is contained in:
Henrik Lissner 2018-07-14 21:33:13 +02:00
parent 6efd9d4573
commit e97fd7558c
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -18,9 +18,13 @@ these properties:
Provides a secondary predicate. This function takes no arguments and is Provides a secondary predicate. This function takes no arguments and is
executed from within the target buffer. If it returns nil, this rule will be executed from within the target buffer. If it returns nil, this rule will be
skipped over. skipped over.
:trigger :trigger STRING|FUNCTION
The yasnippet trigger keyword used to trigger the target snippet. If If a string, this is the yasnippet trigger keyword used to trigger the
omitted, `+file-templates-default-trigger' is used. target snippet.
If a function, this function will be run in the context of the buffer to
insert a file template into. It is given no arguments and must insert text
into the current buffer manually.
If omitted, `+file-templates-default-trigger' is used.
:mode SYMBOL :mode SYMBOL
What mode to get the yasnippet snippet from. If omitted, either PRED (if What mode to get the yasnippet snippet from. If omitted, either PRED (if
it's a major-mode symbol) or the mode of the buffer is used. it's a major-mode symbol) or the mode of the buffer is used.
@ -69,19 +73,21 @@ evil is loaded and enabled)."
(user-error "Couldn't determine mode for %s file template" pred)) (user-error "Couldn't determine mode for %s file template" pred))
(unless trigger (unless trigger
(setq trigger +file-templates-default-trigger)) (setq trigger +file-templates-default-trigger))
(require 'yasnippet) (if (functionp trigger)
(unless yas-minor-mode (funcall trigger)
(yas-minor-mode-on)) (require 'yasnippet)
(when (and yas-minor-mode (unless yas-minor-mode
(yas-expand-snippet (yas-minor-mode-on))
(yas--template-content (when (and yas-minor-mode
(cl-find trigger (yas--all-templates (yas--get-snippet-tables mode)) (yas-expand-snippet
:key #'yas--template-key :test #'equal))) (yas--template-content
(and (featurep 'evil) evil-mode) (cl-find trigger (yas--all-templates (yas--get-snippet-tables mode))
(and yas--active-field-overlay :key #'yas--template-key :test #'equal)))
(overlay-buffer yas--active-field-overlay) (and (featurep 'evil) evil-mode)
(overlay-get yas--active-field-overlay 'yas--field))) (and yas--active-field-overlay
(evil-initialize-state 'insert))))) (overlay-buffer yas--active-field-overlay)
(overlay-get yas--active-field-overlay 'yas--field)))
(evil-initialize-state 'insert))))))
;;;###autoload ;;;###autoload
(defun +file-templates-get-short-path () (defun +file-templates-get-short-path ()