|
1 | 1 | [](./LICENSE)
|
2 | 2 |
|
3 |
| -# Emacs Yasnippet Library |
4 |
| - |
5 |
| -The [yasnippet](https://github.com/capitaomorte/yasnippet) snippets bundled with |
6 |
| -[Doom Emacs]. |
7 |
| - |
8 |
| -## Notes to snippet authors |
9 |
| - |
10 |
| -+ `%` is aliased to `yas-selected-text` for convenience. |
11 |
| -+ [Doom Emacs][emacs.d] has project-specific minor modes. A `+` prefix denotes |
12 |
| - these minor modes, e.g. `+php-laravel-mode`. |
13 |
| -+ This library uses custom helper functions, like `(!%!)` and `(%1)`. These are |
14 |
| - defined in `fundamental-mode/.yas-setup.el`. See their docstrings to see what |
15 |
| - they do. |
16 |
| -+ The `(%alias TRIGGER &optional MODE)` function is available for setting up |
17 |
| - multiple triggers for one snippet. e.g. |
18 |
| - |
19 |
| - ```emacs-lisp |
20 |
| - ;; js-mode/class |
21 |
| - # name: class |
22 |
| - # -- |
23 |
| - class ${1:Name} { |
24 |
| - $0 |
25 |
| - } |
26 |
| -
|
27 |
| - ;; js-mode/cl |
28 |
| - # name: class |
29 |
| - # uuid: cl |
30 |
| - # type: command |
31 |
| - # -- |
32 |
| - (%alias "class") |
33 |
| - ``` |
| 3 | +# Doom Emacs' Snippet Library |
| 4 | + |
| 5 | +This repository contains the official [yasnippet] snippets library for [Doom |
| 6 | +Emacs]. |
| 7 | + |
| 8 | +It exposes a small API to assist you in writing your own snippets (such as |
| 9 | +`doom-snippets-expand` for easily creating snippet aliases), and provides |
| 10 | +snippets for the project/framework-specific minor modes that [Doom Emacs] |
| 11 | +defines (e.g. `+php-laravel-mode`). |
34 | 12 |
|
35 |
| -Note: alias snippets with the same `name` must have a unique `uuid`. |
36 | 13 |
|
37 | 14 | ## Install
|
38 | 15 |
|
39 |
| -Clone this repo and: |
| 16 | ++ [Doom Emacs] users need only enable the `:editor snippets` module. |
| 17 | ++ This package isn't available on MELPA yet. |
| 18 | ++ Otherwise, clone this repo somewhere local and use: |
| 19 | + |
| 20 | + ``` emacs-lisp |
| 21 | + (use-package doom-snippets |
| 22 | + :load-path "path/to/emacs-snippets" |
| 23 | + :after yasnippet) |
| 24 | + ``` |
| 25 | + |
40 | 26 |
|
41 |
| -``` emacs-lisp |
42 |
| -(add-to-list 'load-path "path/to/emacs-snippets") |
43 |
| -(require 'emacs-snippets) |
| 27 | +## Snippets API |
| 28 | +This library exposes a small API to assist you in writing your own snippets. |
| 29 | +This is not an exhaustive list, but are the most useful. |
44 | 30 |
|
45 |
| -;; OR |
| 31 | +### `doom-snippets-expand PROPERTY VALUE &optional MODE` |
46 | 32 |
|
47 |
| -(use-package emacs-snippets |
48 |
| - :load-path "path/to/emacs-snippets" |
49 |
| - :after yasnippet) |
| 33 | +This is primarily used for create snippet aliases. A snippet alias if a snippet |
| 34 | +that will expand another snippet when used. e.g. |
| 35 | + |
| 36 | +```emacs-lisp |
| 37 | +;;; in js-mode/class |
| 38 | +# name: class |
| 39 | +# -- |
| 40 | +class ${1:Name} { |
| 41 | + $0 |
| 42 | +} |
| 43 | +
|
| 44 | +;;; in js-mode/cl |
| 45 | +# name: class |
| 46 | +# key: cl |
| 47 | +# type: command |
| 48 | +# -- |
| 49 | +(doom-snippets-expand :name "class") |
50 | 50 | ```
|
51 | 51 |
|
52 |
| -`emacs-snippets` sets itself up when `yasnippet` first loads. |
| 52 | +### `doom-snippets-format FORMAT &optional DEFAULT TRIM` |
53 | 53 |
|
54 |
| -### Doom Emacs |
| 54 | +Returns `FORMAT`, which is a format string with a custom spec: |
55 | 55 |
|
56 |
| -No need to install this on Doom, it is included in the `:feature snippets` |
57 |
| -module. That said, if you were to install it manually, for whatever reason, |
58 |
| -place this in a module's (or your private) packages.el |
59 |
| -(`~/.doom.d/packages.el`): |
| 56 | +| spec | description | |
| 57 | +|------|--------------------------------------------------------------------| |
| 58 | +| %s | The contents of your current selection (`yas-selected-text`) | |
| 59 | +| %! | A newline, if your current selection spans more than a single line | |
| 60 | +| %> | A newline, unless the point is at EOL | |
60 | 61 |
|
61 |
| -```emacs-lisp |
62 |
| -(package! emacs-snippets |
63 |
| - :recipe (:fetcher github |
64 |
| - :repo "hlissner/emacs-snippets" |
65 |
| - :files ("*"))) |
| 62 | ++ If `yas-selected-text` is empty, `DEFAULT` is used. |
| 63 | ++ If `TRIM` is non-nil, whitespace on the ends of `yas-selected-text` is |
| 64 | + trimmed. |
| 65 | + |
| 66 | +An example of its use: |
| 67 | + |
| 68 | +```text |
| 69 | +# -*- mode: snippet -*- |
| 70 | +# name: while ... { ... } |
| 71 | +# key: while |
| 72 | +# -- |
| 73 | +while ${1:true} { `(doom-snippets-format "%!%s%!")`$0 } |
| 74 | +``` |
| 75 | + |
| 76 | +If the selection consists of a single line, this will expand to: |
| 77 | + |
| 78 | +``` javascript |
| 79 | +while true { console.log("Hello world")| } |
| 80 | +``` |
| 81 | + |
| 82 | +If it consists of multiple lines, it will expand to: |
| 83 | + |
| 84 | +``` javascript |
| 85 | +while true { |
| 86 | + console.log("Hello") |
| 87 | + console.log("world")| |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +`PROPERTY` can be `:uuid`, `:name`, `:key` or `:file`, and `MODE` restricts the |
| 92 | +snippet search to a certain snippet table (by major mode). It isn't wise to use |
| 93 | +`MODE` to reference snippets for oher major modes, because it will only see |
| 94 | +snippets that yasnippet have already loaded (and it lazy loads each table). |
| 95 | + |
| 96 | +### `doom-snippets-without-trigger &rest BODY` |
| 97 | + |
| 98 | +Preforms `BODY` after moving the point to the start of the trigger keyword. |
| 99 | + |
| 100 | +Without this, tests like `bolp` would meaninglessly fail because the cursor is |
| 101 | +always in front of the word that triggered this snippet. |
| 102 | + |
| 103 | +``` text |
| 104 | +# -*- mode: snippet -*- |
| 105 | +# name: .to_string() |
| 106 | +# key: ts |
| 107 | +# condition: (doom-snippets-without-trigger (eq (char-before) ?.)) |
| 108 | +# -- |
| 109 | +to_string() |
66 | 110 | ```
|
67 | 111 |
|
68 | 112 |
|
69 |
| -[emacs.d]: https://github.com/hlissner/doom-emacs |
| 113 | +[yasnippet]: https://github.com/capitaomorte/yasnippet |
| 114 | +[Doom Emacs]: https://github.com/hlissner/doom-emacs |
0 commit comments