2019-04-21 19:59:44 -04:00
|
|
|
;;; editor/snippets/autoload/snippets.el -*- lexical-binding: t; -*-
|
2017-02-11 06:59:49 -05:00
|
|
|
|
2018-09-03 02:15:10 +02:00
|
|
|
(defun +snippets--remove-p (x y)
|
|
|
|
(and (equal (yas--template-key x) (yas--template-key y))
|
|
|
|
(file-in-directory-p (yas--template-get-file x) doom-emacs-dir)))
|
|
|
|
|
2018-08-11 16:48:31 +02:00
|
|
|
;;;###autoload
|
2018-09-17 08:53:13 -04:00
|
|
|
(defun +snippets-prompt-private (prompt choices &optional display-fn)
|
2018-09-03 02:15:10 +02:00
|
|
|
"Prioritize private snippets over built-in ones if there are multiple
|
|
|
|
choices.
|
|
|
|
|
|
|
|
There are two groups of snippets in Doom Emacs. The built in ones (under
|
|
|
|
`doom-emacs-dir'; provided by Doom or its plugins) or your private snippets
|
|
|
|
(outside of `doom-eamcs-dir').
|
|
|
|
|
|
|
|
If there are multiple snippets with the same key in either camp (but not both),
|
|
|
|
you will be prompted to select one.
|
|
|
|
|
|
|
|
If there are conflicting keys across the two camps, the built-in ones are
|
|
|
|
ignored. This makes it easy to override built-in snippets with private ones."
|
2019-07-10 21:27:12 +02:00
|
|
|
(when (eq this-command 'yas-expand)
|
2019-07-21 03:01:15 +02:00
|
|
|
(let* ((gc-cons-threshold most-positive-fixnum)
|
2018-09-03 02:15:10 +02:00
|
|
|
(choices (cl-remove-duplicates choices :test #'+snippets--remove-p)))
|
|
|
|
(if (cdr choices)
|
2018-09-07 22:08:11 -04:00
|
|
|
(cl-loop for fn in (cdr (memq '+snippets-prompt-private yas-prompt-functions))
|
2018-09-17 08:53:13 -04:00
|
|
|
if (funcall fn prompt choices display-fn)
|
2018-09-07 22:08:11 -04:00
|
|
|
return it)
|
2018-09-03 02:15:10 +02:00
|
|
|
(car choices)))))
|
2018-06-07 02:48:03 +02:00
|
|
|
|
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
|
|
|
(defun +snippet--ensure-dir (dir)
|
|
|
|
(unless (file-directory-p dir)
|
|
|
|
(if (y-or-n-p (format "%S doesn't exist. Create it?" (abbreviate-file-name dir)))
|
|
|
|
(make-directory dir t)
|
|
|
|
(error "%S doesn't exist" (abbreviate-file-name dir)))))
|
|
|
|
|
|
|
|
(defun +snippet--completing-read-uuid (prompt all-snippets &rest args)
|
|
|
|
(plist-get
|
|
|
|
(text-properties-at
|
|
|
|
0 (apply #'completing-read prompt
|
|
|
|
(cl-loop for (_ . tpl) in (mapcan #'yas--table-templates (if all-snippets
|
|
|
|
(hash-table-values yas--tables)
|
|
|
|
(yas--get-snippet-tables)))
|
|
|
|
|
|
|
|
for txt = (format "%-25s%-30s%s"
|
|
|
|
(yas--template-key tpl)
|
|
|
|
(yas--template-name tpl)
|
|
|
|
(abbreviate-file-name (yas--template-load-file tpl)))
|
|
|
|
collect
|
|
|
|
(progn
|
2020-11-14 14:16:01 -05:00
|
|
|
(set-text-properties 0 (length txt) `(uuid ,(yas--template-uuid tpl)
|
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
|
|
|
path ,(yas--template-load-file tpl))
|
|
|
|
txt)
|
|
|
|
txt))
|
|
|
|
args))
|
|
|
|
'uuid))
|
|
|
|
|
|
|
|
(defun +snippet--abort ()
|
|
|
|
(interactive)
|
|
|
|
(set-buffer-modified-p nil)
|
|
|
|
(kill-current-buffer))
|
|
|
|
|
|
|
|
(defvar +snippet--current-snippet-uuid nil)
|
|
|
|
(defun +snippet--edit ()
|
|
|
|
(interactive)
|
|
|
|
(when +snippet--current-snippet-uuid
|
|
|
|
(let ((buf (current-buffer)))
|
|
|
|
(+snippets/edit +snippet--current-snippet-uuid)
|
|
|
|
(kill-buffer buf))))
|
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;;; Commands
|
|
|
|
|
2017-02-11 06:59:49 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/goto-start-of-field ()
|
|
|
|
"Go to the beginning of the current field."
|
|
|
|
(interactive)
|
2017-04-17 02:19:20 -04:00
|
|
|
(let* ((snippet (car (yas-active-snippets)))
|
2018-06-16 12:22:50 +02:00
|
|
|
(active-field (yas--snippet-active-field snippet))
|
2019-06-25 21:38:16 +02:00
|
|
|
(position (if (yas--field-p active-field)
|
|
|
|
(yas--field-start active-field)
|
|
|
|
-1)))
|
2017-02-11 06:59:49 -05:00
|
|
|
(if (= (point) position)
|
|
|
|
(move-beginning-of-line 1)
|
|
|
|
(goto-char position))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/goto-end-of-field ()
|
|
|
|
"Go to the end of the current field."
|
|
|
|
(interactive)
|
2017-04-17 02:19:20 -04:00
|
|
|
(let* ((snippet (car (yas-active-snippets)))
|
2018-06-16 12:22:50 +02:00
|
|
|
(active-field (yas--snippet-active-field snippet))
|
2019-06-25 21:38:16 +02:00
|
|
|
(position (if (yas--field-p active-field)
|
|
|
|
(yas--field-end active-field)
|
|
|
|
-1)))
|
2017-02-11 06:59:49 -05:00
|
|
|
(if (= (point) position)
|
|
|
|
(move-end-of-line 1)
|
|
|
|
(goto-char position))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/delete-backward-char (&optional field)
|
|
|
|
"Prevents Yas from interfering with backspace deletion."
|
|
|
|
(interactive)
|
2018-06-16 12:22:50 +02:00
|
|
|
(let ((field (or field (and (overlayp yas--active-field-overlay)
|
2017-02-11 06:59:49 -05:00
|
|
|
(overlay-buffer yas--active-field-overlay)
|
|
|
|
(overlay-get yas--active-field-overlay 'yas--field)))))
|
2018-06-16 12:22:50 +02:00
|
|
|
(unless (and (yas--field-p field)
|
|
|
|
(eq (point) (marker-position (yas--field-start field))))
|
|
|
|
(call-interactively #'delete-backward-char))))
|
2017-02-11 06:59:49 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/delete-forward-char-or-field (&optional field)
|
|
|
|
"Delete forward, or skip the current field if it's empty. This is to prevent
|
|
|
|
buggy behavior when <delete> is pressed in an empty field."
|
|
|
|
(interactive)
|
|
|
|
(let ((field (or field (and yas--active-field-overlay
|
|
|
|
(overlay-buffer yas--active-field-overlay)
|
|
|
|
(overlay-get yas--active-field-overlay 'yas--field)))))
|
2018-06-16 12:22:50 +02:00
|
|
|
(cond ((not (yas--field-p field))
|
|
|
|
(delete-char 1))
|
|
|
|
((and (not (yas--field-modified-p field))
|
2017-02-11 06:59:49 -05:00
|
|
|
(eq (point) (marker-position (yas--field-start field))))
|
|
|
|
(yas--skip-and-clear field)
|
|
|
|
(yas-next-field 1))
|
|
|
|
((eq (point) (marker-position (yas--field-end field))) nil)
|
2018-06-16 12:22:50 +02:00
|
|
|
((delete-char 1)))))
|
2017-02-11 06:59:49 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/delete-to-start-of-field (&optional field)
|
|
|
|
"Delete to start-of-field."
|
|
|
|
(interactive)
|
2018-06-16 12:22:50 +02:00
|
|
|
(unless field
|
|
|
|
(setq field (and (overlayp yas--active-field-overlay)
|
|
|
|
(overlay-buffer yas--active-field-overlay)
|
|
|
|
(overlay-get yas--active-field-overlay 'yas--field))))
|
|
|
|
(when (yas--field-p field)
|
|
|
|
(let ((sof (marker-position (yas--field-start field))))
|
|
|
|
(when (and field (> (point) sof))
|
|
|
|
(delete-region sof (point))))))
|
2018-06-19 13:00:05 +02:00
|
|
|
|
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
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/find ()
|
|
|
|
"Open a snippet file (in all of `yas-snippet-dirs')."
|
|
|
|
(interactive)
|
|
|
|
(let* ((dirs (doom-files-in (cl-loop for dir in yas-snippet-dirs
|
|
|
|
if (symbolp dir)
|
|
|
|
collect (symbol-value dir)
|
|
|
|
else collect dir)
|
|
|
|
:depth 0 :type 'dirs))
|
|
|
|
(files (doom-files-in dirs :depth 0 :full t)))
|
|
|
|
(let ((template-path (completing-read "Find snippet: " (mapcar #'abbreviate-file-name files))))
|
|
|
|
(unless (file-readable-p template-path)
|
|
|
|
(user-error "Cannot read %S" template-path))
|
|
|
|
(find-file template-path)
|
|
|
|
(unless (file-in-directory-p template-path +snippets-dir)
|
|
|
|
(read-only-mode +1)
|
|
|
|
(setq header-line-format "This is a built-in snippet. Press C-c C-e to modify it"
|
|
|
|
+snippet--current-snippet-uuid template-uuid)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/find-private ()
|
|
|
|
"Open a private snippet file in `+snippets-dir'."
|
|
|
|
(interactive)
|
|
|
|
(doom-project-find-file +snippets-dir))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/find-for-current-mode (template-uuid)
|
|
|
|
"Open a snippet for this mode."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(+snippet--completing-read-uuid "Visit snippet: " current-prefix-arg)))
|
2020-11-14 14:16:01 -05:00
|
|
|
(if-let* ((template (yas--get-template-by-uuid major-mode template-uuid))
|
|
|
|
(template-path (yas--template-load-file template)))
|
|
|
|
(progn
|
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
|
|
|
(unless (file-readable-p template-path)
|
|
|
|
(user-error "Cannot read %S" template-path))
|
|
|
|
(find-file template-path)
|
|
|
|
(unless (file-in-directory-p template-path +snippets-dir)
|
|
|
|
(read-only-mode +1)
|
|
|
|
(setq header-line-format "This is a built-in snippet. Press C-c C-e to modify it"
|
|
|
|
+snippet--current-snippet-uuid template-uuid)))
|
|
|
|
(user-error "Cannot find template with UUID %S" template-uuid)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/new ()
|
|
|
|
"Create a new snippet in `+snippets-dir'."
|
|
|
|
(interactive)
|
|
|
|
(let ((default-directory
|
|
|
|
(expand-file-name (symbol-name major-mode)
|
|
|
|
+snippets-dir)))
|
|
|
|
(+snippet--ensure-dir default-directory)
|
|
|
|
(with-current-buffer (switch-to-buffer "untitled-snippet")
|
|
|
|
(snippet-mode)
|
|
|
|
(erase-buffer)
|
|
|
|
(yas-expand-snippet (concat "# -*- mode: snippet -*-\n"
|
|
|
|
"# name: $1\n"
|
|
|
|
"# uuid: $2\n"
|
|
|
|
"# key: ${3:trigger-key}${4:\n"
|
|
|
|
"# condition: t}\n"
|
|
|
|
"# --\n"
|
|
|
|
"$0"))
|
|
|
|
(when (bound-and-true-p evil-local-mode)
|
|
|
|
(evil-insert-state)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/new-alias (template-uuid)
|
|
|
|
"Create an alias for a snippet with uuid TEMPLATE-UUID.
|
|
|
|
|
|
|
|
You will be prompted for a snippet to alias."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(+snippet--completing-read-uuid "Select snippet to alias: "
|
|
|
|
current-prefix-arg)))
|
2019-07-14 14:43:45 +02:00
|
|
|
(unless (require 'doom-snippets nil t)
|
|
|
|
(user-error "This command requires the `doom-snippets' library bundled with Doom Emacs"))
|
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
|
|
|
(let ((default-directory (expand-file-name (symbol-name major-mode) +snippets-dir)))
|
|
|
|
(+snippet--ensure-dir default-directory)
|
|
|
|
(with-current-buffer (switch-to-buffer "untitled-snippet")
|
|
|
|
(snippet-mode)
|
|
|
|
(erase-buffer)
|
|
|
|
(yas-expand-snippet
|
|
|
|
(concat "# -*- mode: snippet -*-\n"
|
|
|
|
"# name: $1\n"
|
|
|
|
"# key: ${2:trigger-key}${3:\n"
|
|
|
|
"# condition: t}\n"
|
|
|
|
"# type: command\n"
|
|
|
|
"# --\n"
|
2019-09-20 23:10:53 -04:00
|
|
|
"(%alias \"${4:" (or template-uuid "uuid") "}\")"))
|
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
|
|
|
(when (bound-and-true-p evil-local-mode)
|
|
|
|
(evil-insert-state)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +snippets/edit (template-uuid)
|
|
|
|
"Edit a snippet with uuid TEMPLATE-UUID.
|
|
|
|
|
|
|
|
If the snippet isn't in `+snippets-dir', it will be copied there (where it will
|
|
|
|
shadow the default snippet)."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(+snippet--completing-read-uuid "Select snippet to alias: "
|
|
|
|
current-prefix-arg)))
|
2020-11-14 14:16:01 -05:00
|
|
|
(if-let* ((major-mode (if (eq major-mode 'snippet-mode)
|
|
|
|
(intern (file-name-base (directory-file-name default-directory)))
|
|
|
|
major-mode))
|
|
|
|
(template (yas--get-template-by-uuid major-mode template-uuid))
|
|
|
|
(template-path (yas--template-load-file template)))
|
|
|
|
(if (file-in-directory-p template-path +snippets-dir)
|
|
|
|
(find-file template-path)
|
|
|
|
(let ((buf (get-buffer-create (format "*%s*" (file-name-nondirectory template-path)))))
|
|
|
|
(with-current-buffer (switch-to-buffer buf)
|
|
|
|
(insert-file-contents template-path)
|
|
|
|
(snippet-mode)
|
|
|
|
(setq default-directory
|
|
|
|
(expand-file-name (file-name-nondirectory template-path)
|
|
|
|
(expand-file-name (symbol-name major-mode)
|
|
|
|
+snippets-dir))))))
|
|
|
|
(user-error "Couldn't find a snippet with uuid %S" template-uuid)))
|
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
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-26 19:57:13 +02:00
|
|
|
(defun +snippets-show-hints-in-header-line-h ()
|
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
|
|
|
(setq header-line-format
|
|
|
|
(substitute-command-keys
|
|
|
|
(concat "\\[yas-load-snippet-buffer-and-close] to finish, "
|
|
|
|
"\\[+snippet--abort] to abort, "
|
|
|
|
"\\[yas-tryout-snippet] to test it"))))
|
|
|
|
|
2018-06-19 13:00:05 +02:00
|
|
|
|
|
|
|
;;
|
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
|
|
|
;;; Hooks
|
2018-06-19 13:00:05 +02:00
|
|
|
|
2018-06-20 09:17:13 +02:00
|
|
|
;;;###autoload
|
2019-07-26 19:57:13 +02:00
|
|
|
(defun +snippets-enable-project-modes-h (mode &rest _)
|
2018-06-19 13:00:05 +02:00
|
|
|
"Automatically enable snippet libraries for project minor modes defined with
|
|
|
|
`def-project-mode!'."
|
|
|
|
(if (symbol-value mode)
|
|
|
|
(yas-activate-extra-mode mode)
|
|
|
|
(yas-deactivate-extra-mode mode)))
|
2018-11-02 11:29:58 -04:00
|
|
|
|
2019-04-22 19:39:50 -04:00
|
|
|
;;;###autoload
|
2019-07-26 19:57:13 +02:00
|
|
|
(defun +snippets-read-only-maybe-h ()
|
2019-04-22 19:39:50 -04:00
|
|
|
"Enable `read-only-mode' if snippet is built-in."
|
|
|
|
(when (file-in-directory-p default-directory doom-local-dir)
|
|
|
|
(read-only-mode 1)
|
|
|
|
(message "This is a built-in snippet, enabling read only mode. Use `yas-new-snippet' to redefine snippets")))
|
|
|
|
|
2018-11-02 11:29:58 -04:00
|
|
|
|
2019-05-31 20:11:06 -04:00
|
|
|
;;
|
|
|
|
;;; Advice
|
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-26 19:57:13 +02:00
|
|
|
(defun +snippets-expand-on-region-a (orig-fn &optional no-condition)
|
2020-05-24 13:31:29 -04:00
|
|
|
"Fix off-by-one when expanding snippets on an evil visual region.
|
2019-05-31 20:11:06 -04:00
|
|
|
|
2020-05-24 13:31:29 -04:00
|
|
|
Also strips whitespace out of selection. Also switches to insert mode. If
|
|
|
|
`evil-local-mode' isn't enabled, or we're not in visual mode, run ORIG-FN as
|
|
|
|
is."
|
2019-05-31 20:11:06 -04:00
|
|
|
(if (not (and (bound-and-true-p evil-local-mode)
|
|
|
|
(evil-visual-state-p)))
|
|
|
|
(funcall orig-fn no-condition)
|
2020-05-24 13:31:29 -04:00
|
|
|
;; Trim whitespace in selected region, so as not to introduce extra
|
|
|
|
;; whitespace into `yas-selected-text'.
|
|
|
|
(evil-visual-select (save-excursion
|
|
|
|
(goto-char evil-visual-beginning)
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
(point))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char evil-visual-end)
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
(point))
|
|
|
|
'inclusive)
|
|
|
|
(letf! ((defun region-beginning () evil-visual-beginning)
|
|
|
|
(defun region-end () evil-visual-end))
|
2019-07-10 12:08:39 +02:00
|
|
|
(funcall orig-fn no-condition)))
|
|
|
|
(when (and (bound-and-true-p evil-local-mode)
|
|
|
|
(yas-active-snippets))
|
|
|
|
(evil-insert-state +1)))
|