2019-04-21 19:59:44 -04:00
|
|
|
;;; editor/snippets/config.el -*- lexical-binding: t; -*-
|
2017-02-11 06:59:49 -05:00
|
|
|
|
2018-05-25 00:46:11 +02:00
|
|
|
(defvar +snippets-dir (expand-file-name "snippets/" doom-private-dir)
|
|
|
|
"Directory where `yasnippet' will search for your private snippets.")
|
|
|
|
|
|
|
|
|
|
|
|
;;
|
2018-09-07 19:36:16 -04:00
|
|
|
;; Packages
|
2018-05-25 00:46:11 +02:00
|
|
|
|
2017-02-23 00:06:12 -05:00
|
|
|
(def-package! yasnippet
|
2019-07-22 19:11:12 +02:00
|
|
|
:commands (yas-minor-mode-on
|
|
|
|
yas-expand
|
|
|
|
yas-expand-snippet
|
|
|
|
yas-lookup-snippet
|
|
|
|
yas-insert-snippet
|
|
|
|
yas-new-snippet
|
|
|
|
yas-visit-snippet-file)
|
2017-02-11 06:59:49 -05:00
|
|
|
:init
|
2017-02-28 17:58:52 -05:00
|
|
|
;; Ensure `yas-reload-all' is called as late as possible. Other modules could
|
|
|
|
;; have additional configuration for yasnippet. For example, file-templates.
|
2017-06-05 16:41:39 +02:00
|
|
|
(add-transient-hook! 'yas-minor-mode-hook (yas-reload-all))
|
2017-02-28 17:58:52 -05:00
|
|
|
|
2018-09-08 18:38:09 -04:00
|
|
|
(add-hook! (text-mode prog-mode conf-mode snippet-mode)
|
2017-04-17 02:17:10 -04:00
|
|
|
#'yas-minor-mode-on)
|
2017-02-19 18:32:09 -05:00
|
|
|
|
|
|
|
:config
|
2017-12-27 18:19:56 -05:00
|
|
|
(setq yas-verbosity (if doom-debug-mode 3 0)
|
2017-02-11 06:59:49 -05:00
|
|
|
yas-also-auto-indent-first-line t
|
2019-03-04 20:44:35 -05:00
|
|
|
;; Remove default ~/.emacs.d/snippets
|
2019-07-22 19:11:12 +02:00
|
|
|
yas-snippet-dirs (delete yas--default-user-snippets-dir
|
|
|
|
yas-snippet-dirs))
|
2018-07-09 15:33:31 +02:00
|
|
|
|
2019-07-14 22:46:16 +02:00
|
|
|
;; default snippets library, if available
|
|
|
|
(require 'doom-snippets nil t)
|
|
|
|
|
2018-08-11 16:48:31 +02:00
|
|
|
;; Allow private snippets in DOOMDIR/snippets
|
2018-05-25 00:46:11 +02:00
|
|
|
(add-to-list 'yas-snippet-dirs '+snippets-dir nil #'eq)
|
2018-07-09 15:33:31 +02:00
|
|
|
|
2018-08-11 16:48:31 +02:00
|
|
|
;; Remove GUI dropdown prompt (prefer ivy/helm)
|
2019-07-22 19:11:12 +02:00
|
|
|
(delq! 'yas-dropdown-prompt yas-prompt-functions)
|
2018-08-11 16:48:31 +02:00
|
|
|
;; Prioritize private snippets in `+snippets-dir' over built-in ones if there
|
|
|
|
;; are multiple choices.
|
|
|
|
(add-to-list 'yas-prompt-functions #'+snippets-prompt-private nil #'eq)
|
|
|
|
|
2018-06-19 13:00:05 +02:00
|
|
|
;; Register `def-project-mode!' modes with yasnippet. This enables project
|
|
|
|
;; specific snippet libraries (e.g. for Laravel, React or Jekyll projects).
|
2017-04-17 02:17:10 -04:00
|
|
|
(add-hook 'doom-project-hook #'+snippets|enable-project-modes)
|
2018-07-09 15:33:31 +02:00
|
|
|
|
2017-12-27 18:20:14 -05:00
|
|
|
;; Exit snippets on ESC from normal mode
|
2018-05-25 00:46:11 +02:00
|
|
|
(add-hook 'doom-escape-hook #'yas-abort-snippet)
|
2018-07-09 15:33:31 +02:00
|
|
|
|
2018-05-25 00:46:11 +02:00
|
|
|
(after! smartparens
|
2018-07-09 15:33:31 +02:00
|
|
|
;; tell smartparens overlays not to interfere with yasnippet keybinds
|
2018-06-19 13:00:05 +02:00
|
|
|
(advice-add #'yas-expand :before #'sp-remove-active-pair-overlay))
|
2018-07-09 15:33:31 +02:00
|
|
|
|
2019-04-22 19:39:50 -04:00
|
|
|
;; Enable `read-only-mode' for built-in snippets (in `doom-local-dir')
|
|
|
|
(add-hook 'snippet-mode-hook #'+snippets|read-only-maybe)
|
|
|
|
|
editor/snippets: expand on snippet commands & keybinds
- Introduces the +snippets/new (SPC s n) command for creating a new
private snippet
- Introduces the +snippets/new-lias (SPC s N) command for creating a new
private snippet alias, which will invoke another snippet (you will be
prompted to select one). This will only work with the emacs-snippets
library bundled with Doom Emacs, however, as it depends on its API.
- Introduces +snippets/edit (SPC s c) for modifying existing snippets.
How this differs from yas-visit-snippet-file is it will copy the
contents of built-in snippets into a buffer primed for your private
snippets (in DOOMDIR/snippets), while yas-visit-snippet-file will
simply open the originating snippet.
- Introduces the +snippets/find (SPC s ?),
+snippets/find-for-current-mode (SPC s /) and
+snippets/find-private (SPC s f) commands for, respectively, finding a
snippet file among *all* directories in yas-snippet-dirs, finding a
snippet for the current major mode (plus parents), and finding a
snippet from among your private library. This opens built-in snippets
in read-only mode, but you can press C-c C-e to open it in
+snippets/edit.
2019-07-12 20:41:50 +02:00
|
|
|
;; (Evil only) fix off-by-one issue with line-wise visual selections in
|
2019-05-31 20:11:06 -04:00
|
|
|
;; `yas-insert-snippet', and switches to insert mode afterwards.
|
editor/snippets: expand on snippet commands & keybinds
- Introduces the +snippets/new (SPC s n) command for creating a new
private snippet
- Introduces the +snippets/new-lias (SPC s N) command for creating a new
private snippet alias, which will invoke another snippet (you will be
prompted to select one). This will only work with the emacs-snippets
library bundled with Doom Emacs, however, as it depends on its API.
- Introduces +snippets/edit (SPC s c) for modifying existing snippets.
How this differs from yas-visit-snippet-file is it will copy the
contents of built-in snippets into a buffer primed for your private
snippets (in DOOMDIR/snippets), while yas-visit-snippet-file will
simply open the originating snippet.
- Introduces the +snippets/find (SPC s ?),
+snippets/find-for-current-mode (SPC s /) and
+snippets/find-private (SPC s f) commands for, respectively, finding a
snippet file among *all* directories in yas-snippet-dirs, finding a
snippet for the current major mode (plus parents), and finding a
snippet from among your private library. This opens built-in snippets
in read-only mode, but you can press C-c C-e to open it in
+snippets/edit.
2019-07-12 20:41:50 +02:00
|
|
|
(advice-add #'yas-insert-snippet :around #'+snippets*expand-on-region)
|
|
|
|
|
|
|
|
(define-key! snippet-mode-map
|
|
|
|
"C-c C-k" #'+snippet--abort
|
|
|
|
"C-c C-e" #'+snippet--edit)
|
|
|
|
(add-hook 'snippet-mode-hook #'+snippets|show-hints-in-header-line)
|
|
|
|
|
|
|
|
;; Replace commands with superior alternatives
|
|
|
|
(define-key! yas-minor-mode-map
|
|
|
|
[remap yas-new-snippet] #'+snippets/new
|
|
|
|
[remap yas-visit-snippet-file] #'+snippets/edit))
|
2017-02-11 06:59:49 -05:00
|
|
|
|
|
|
|
|
2019-07-22 00:35:24 +02:00
|
|
|
(def-package! auto-yasnippet
|
|
|
|
:defer t
|
|
|
|
:init (setq aya-persist-snippets-dir (concat doom-etc-dir "auto-snippets/"))
|
|
|
|
:config
|
|
|
|
(def-advice! +snippets-inhibit-yas-global-mode (orig-fn &rest args)
|
|
|
|
"auto-yasnippet enables `yas-global-mode'. This is obnoxious for folks like
|
|
|
|
us who use yas-minor-mode and enable yasnippet more selectively. This advice
|
|
|
|
swaps `yas-global-mode' with `yas-minor-mode'."
|
|
|
|
:around '(aya-expand aya-open-line)
|
|
|
|
(cl-letf (((symbol-function #'yas-global-mode) #'yas-minor-mode)
|
|
|
|
(yas-global-mode yas-minor-mode))
|
|
|
|
(apply orig-fn args))))
|