forked from barak/yasnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yasnippet.el
4582 lines (4012 loc) · 181 KB
/
yasnippet.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; yasnippet.el --- Yet another snippet extension for Emacs.
;; Copyright (C) 2008-2013, 2015 Free Software Foundation, Inc.
;; Authors: pluskid <pluskid@gmail.com>, João Távora <joaotavora@gmail.com>
;; Maintainer: João Távora <joaotavora@gmail.com>
;; Version: 0.8.1
;; Package-version: 0.8.0
;; X-URL: http://github.com/capitaomorte/yasnippet
;; Keywords: convenience, emulation
;; URL: http://github.com/capitaomorte/yasnippet
;; EmacsWiki: YaSnippetMode
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Basic steps to setup:
;;
;; (add-to-list 'load-path
;; "~/path-to-yasnippet")
;; (require 'yasnippet)
;; (yas-global-mode 1)
;;
;;
;; Interesting variables are:
;;
;; `yas-snippet-dirs'
;;
;; The directory where user-created snippets are to be
;; stored. Can also be a list of directories. In that case,
;; when used for bulk (re)loading of snippets (at startup or
;; via `yas-reload-all'), directories appearing earlier in
;; the list override other dir's snippets. Also, the first
;; directory is taken as the default for storing the user's
;; new snippets.
;;
;; The deprecated `yas/root-directory' aliases this variable
;; for backward-compatibility.
;;
;;
;; Major commands are:
;;
;; M-x yas-expand
;;
;; Try to expand snippets before point. In `yas-minor-mode',
;; this is normally bound to TAB, but you can customize it in
;; `yas-minor-mode-map'.
;;
;; M-x yas-load-directory
;;
;; Prompts you for a directory hierarchy of snippets to load.
;;
;; M-x yas-activate-extra-mode
;;
;; Prompts you for an extra mode to add snippets for in the
;; current buffer.
;;
;; M-x yas-insert-snippet
;;
;; Prompts you for possible snippet expansion if that is
;; possible according to buffer-local and snippet-local
;; expansion conditions. With prefix argument, ignore these
;; conditions.
;;
;; M-x yas-visit-snippet-file
;;
;; Prompts you for possible snippet expansions like
;; `yas-insert-snippet', but instead of expanding it, takes
;; you directly to the snippet definition's file, if it
;; exists.
;;
;; M-x yas-new-snippet
;;
;; Lets you create a new snippet file in the correct
;; subdirectory of `yas-snippet-dirs', according to the
;; active major mode.
;;
;; M-x yas-load-snippet-buffer
;;
;; When editing a snippet, this loads the snippet. This is
;; bound to "C-c C-c" while in the `snippet-mode' editing
;; mode.
;;
;; M-x yas-tryout-snippet
;;
;; When editing a snippet, this opens a new empty buffer,
;; sets it to the appropriate major mode and inserts the
;; snippet there, so you can see what it looks like. This is
;; bound to "C-c C-t" while in `snippet-mode'.
;;
;; M-x yas-describe-tables
;;
;; Lists known snippets in a separate buffer. User is
;; prompted as to whether only the currently active tables
;; are to be displayed, or all the tables for all major
;; modes.
;;
;; If you have `dropdown-list' installed, you can optionally use it
;; as the preferred "prompting method", putting in your .emacs file,
;; for example:
;;
;; (require 'dropdown-list)
;; (setq yas-prompt-functions '(yas-dropdown-prompt
;; yas-ido-prompt
;; yas-completing-prompt))
;;
;; Also check out the customization group
;;
;; M-x customize-group RET yasnippet RET
;;
;; If you use the customization group to set variables
;; `yas-snippet-dirs' or `yas-global-mode', make sure the path to
;; "yasnippet.el" is present in the `load-path' *before* the
;; `custom-set-variables' is executed in your .emacs file.
;;
;; For more information and detailed usage, refer to the project page:
;; http://github.com/capitaomorte/yasnippet
;;; Code:
(require 'cl)
(require 'cl-lib)
(require 'easymenu)
(require 'help-mode)
(defvar yas--editing-template)
(defvar yas--guessed-modes)
(defvar yas--indent-original-column)
(defvar yas--scheduled-jit-loads)
(defvar yas-keymap)
(defvar yas-selected-text)
(defvar yas-verbosity)
(defvar yas--current-template)
;;; User customizable variables
(defgroup yasnippet nil
"Yet Another Snippet extension"
:prefix "yas-"
:group 'editing)
(defvar yas-installed-snippets-dir nil)
(setq yas-installed-snippets-dir
(when load-file-name
(concat (file-name-directory load-file-name) "snippets")))
(defcustom yas-snippet-dirs (remove nil
(list "~/.emacs.d/snippets"
'yas-installed-snippets-dir))
"List of top-level snippet directories.
Each element, a string or a symbol whose value is a string,
designates a top-level directory where per-mode snippet
directories can be found.
Elements appearing earlier in the list override later elements'
snippets.
The first directory is taken as the default for storing snippet's
created with `yas-new-snippet'. "
:type '(choice (string :tag "Single directory (string)")
(repeat :args (string) :tag "List of directories (strings)"))
:group 'yasnippet
:require 'yasnippet
:set #'(lambda (symbol new)
(let ((old (and (boundp symbol)
(symbol-value symbol))))
(set-default symbol new)
(unless (or (not (fboundp 'yas-reload-all))
(equal old new))
(yas-reload-all)))))
(defun yas-snippet-dirs ()
"Return variable `yas-snippet-dirs' as list of strings."
(cl-loop for e in (if (listp yas-snippet-dirs)
yas-snippet-dirs
(list yas-snippet-dirs))
collect
(cond ((stringp e) e)
((and (symbolp e)
(boundp e)
(stringp (symbol-value e)))
(symbol-value e))
(t
(error "[yas] invalid element %s in `yas-snippet-dirs'" e)))))
(defvaralias 'yas/root-directory 'yas-snippet-dirs)
(defcustom yas-new-snippet-default "\
# -*- mode: snippet; require-final-newline: nil -*-
# name: $1
# key: ${2:${1:$(yas--key-from-desc yas-text)}}${3:
# binding: ${4:direct-keybinding}}
# --
$0"
"Default snippet to use when creating a new snippet.
If nil, don't use any snippet."
:type 'string
:group 'yasnippet)
(defcustom yas-prompt-functions '(yas-x-prompt
yas-dropdown-prompt
yas-completing-prompt
yas-ido-prompt
yas-no-prompt)
"Functions to prompt for keys, templates, etc interactively.
These functions are called with the following arguments:
- PROMPT: A string to prompt the user
- CHOICES: a list of strings or objects.
- optional DISPLAY-FN : A function that, when applied to each of
the objects in CHOICES will return a string.
The return value of any function you put here should be one of
the objects in CHOICES, properly formatted with DISPLAY-FN (if
that is passed).
- To signal that your particular style of prompting is
unavailable at the moment, you can also have the function return
nil.
- To signal that the user quit the prompting process, you can
signal `quit' with
(signal 'quit \"user quit!\")."
:type '(repeat function)
:group 'yasnippet)
(defcustom yas-indent-line 'auto
"Controls indenting applied to a recent snippet expansion.
The following values are possible:
- `fixed' Indent the snippet to the current column;
- `auto' Indent each line of the snippet with `indent-according-to-mode'
Every other value means don't apply any snippet-side indentation
after expansion (the manual per-line \"$>\" indentation still
applies)."
:type '(choice (const :tag "Nothing" nothing)
(const :tag "Fixed" fixed)
(const :tag "Auto" auto))
:group 'yasnippet)
(defcustom yas-also-auto-indent-first-line nil
"Non-nil means also auto indent first line according to mode.
Naturally this is only valid when `yas-indent-line' is `auto'"
:type 'boolean
:group 'yasnippet)
(defcustom yas-snippet-revival t
"Non-nil means re-activate snippet fields after undo/redo."
:type 'boolean
:group 'yasnippet)
(defcustom yas-triggers-in-field nil
"If non-nil, allow stacked expansions (snippets inside snippets).
Otherwise `yas-next-field-or-maybe-expand' just moves on to the
next field"
:type 'boolean
:group 'yasnippet)
(defcustom yas-fallback-behavior 'call-other-command
"How to act when `yas-expand' does *not* expand a snippet.
- `call-other-command' means try to temporarily disable YASnippet
and call the next command bound to whatever key was used to
invoke `yas-expand'.
- nil or the symbol `return-nil' mean do nothing. (and
`yas-expand' returns nil)
- A Lisp form (apply COMMAND . ARGS) means interactively call
COMMAND. If ARGS is non-nil, call COMMAND non-interactively
with ARGS as arguments."
:type '(choice (const :tag "Call previous command" call-other-command)
(const :tag "Do nothing" return-nil))
:group 'yasnippet)
(defcustom yas-choose-keys-first nil
"If non-nil, prompt for snippet key first, then for template.
Otherwise prompts for all possible snippet names.
This affects `yas-insert-snippet' and `yas-visit-snippet-file'."
:type 'boolean
:group 'yasnippet)
(defcustom yas-choose-tables-first nil
"If non-nil, and multiple eligible snippet tables, prompts user for tables first.
Otherwise, user chooses between the merging together of all
eligible tables.
This affects `yas-insert-snippet', `yas-visit-snippet-file'"
:type 'boolean
:group 'yasnippet)
(defcustom yas-use-menu 'abbreviate
"Display a YASnippet menu in the menu bar.
When non-nil, submenus for each snippet table will be listed
under the menu \"Yasnippet\".
- If set to `abbreviate', only the current major-mode
menu and the modes set in `yas--extra-modes' are listed.
- If set to `full', every submenu is listed
- If set to `nil', hide the menu.
Any other non-nil value, every submenu is listed."
:type '(choice (const :tag "Full" full)
(const :tag "Abbreviate" abbreviate)
(const :tag "No menu" nil))
:group 'yasnippet)
(defcustom yas-trigger-symbol (or (and (eq window-system 'mac)
(ignore-errors
(char-to-string ?\x21E5))) ;; little ->| sign
" =>")
"The text that will be used in menu to represent the trigger."
:type 'string
:group 'yasnippet)
(defcustom yas-wrap-around-region nil
"If non-nil, snippet expansion wraps around selected region.
The wrapping occurs just before the snippet's exit marker. This
can be overridden on a per-snippet basis."
:type 'boolean
:group 'yasnippet)
(defcustom yas-good-grace t
"If non-nil, don't raise errors in inline elisp evaluation.
An error string \"[yas] error\" is returned instead."
:type 'boolean
:group 'yasnippet)
(defcustom yas-visit-from-menu nil
"If non-nil visit snippets's files from menu, instead of expanding them.
This can only work when snippets are loaded from files."
:type 'boolean
:group 'yasnippet)
(defcustom yas-expand-only-for-last-commands nil
"List of `last-command' values to restrict tab-triggering to, or nil.
Leave this set at nil (the default) to be able to trigger an
expansion simply by placing the cursor after a valid tab trigger,
using whichever commands.
Optionally, set this to something like '(self-insert-command) if
you to wish restrict expansion to only happen when the last
letter of the snippet tab trigger was typed immediately before
the trigger key itself."
:type '(repeat function)
:group 'yasnippet)
;; Only two faces, and one of them shouldn't even be used...
;;
(defface yas-field-highlight-face
'((t (:inherit 'region)))
"The face used to highlight the currently active field of a snippet"
:group 'yasnippet)
(defface yas--field-debug-face
'()
"The face used for debugging some overlays normally hidden"
:group 'yasnippet)
;;; User-visible variables
(defvar yas-keymap (let ((map (make-sparse-keymap)))
(define-key map [(tab)] 'yas-next-field-or-maybe-expand)
(define-key map (kbd "TAB") 'yas-next-field-or-maybe-expand)
(define-key map [(shift tab)] 'yas-prev-field)
(define-key map [backtab] 'yas-prev-field)
(define-key map (kbd "C-g") 'yas-abort-snippet)
(define-key map (kbd "C-d") 'yas-skip-and-clear-or-delete-char)
map)
"The active keymap while a snippet expansion is in progress.")
(defvar yas-key-syntaxes (list "w" "w_" "w_." "w_.()"
#'yas-try-key-from-whitespace)
"Syntaxes and functions to help look for trigger keys before point.
Each element in this list specifies how to skip buffer positions
backwards and look for the start of a trigger key.
Each element can be either a string or a function receiving the
original point as an argument. A string element is simply passed
to `skip-syntax-backward' whereas a function element is called
with no arguments and should also place point before the original
position.
The string between the resulting buffer position and the original
point is matched against the trigger keys in the active snippet
tables.
If no expandable snippets are found, the next element is the list
is tried, unless a function element returned the symbol `again',
in which case it is called again from the previous position and
may once more reposition point.
For example, if `yas-key-syntaxes'' value is '(\"w\" \"w_\"),
trigger keys composed exclusively of \"word\"-syntax characters
are looked for first. Failing that, longer keys composed of
\"word\" or \"symbol\" syntax are looked for. Therefore,
triggering after
foo-bar
will, according to the \"w\" element first try \"barbaz\". If
that isn't a trigger key, \"foo-barbaz\" is tried, respecting the
second \"w_\" element. Notice that even if \"baz\" is a trigger
key for an active snippet, it won't be expanded, unless a
function is added to `yas-key-syntaxes' that eventually places
point between \"bar\" and \"baz\".
See also Info node `(elisp) Syntax Descriptors'.")
(defvar yas-after-exit-snippet-hook
'()
"Hooks to run after a snippet exited.
The hooks will be run in an environment where some variables bound to
proper values:
`yas-snippet-beg' : The beginning of the region of the snippet.
`yas-snippet-end' : Similar to beg.
Attention: These hooks are not run when exiting nested/stacked snippet expansion!")
(defvar yas-before-expand-snippet-hook
'()
"Hooks to run just before expanding a snippet.")
(defvar yas-buffer-local-condition
'(if (and (or (fourth (syntax-ppss))
(fifth (syntax-ppss)))
this-command
(eq this-command 'yas-expand-from-trigger-key))
'(require-snippet-condition . force-in-comment)
t)
"Snippet expanding condition.
This variable is a Lisp form which is evaluated every time a
snippet expansion is attempted:
* If it evaluates to nil, no snippets can be expanded.
* If it evaluates to the a cons (require-snippet-condition
. REQUIREMENT)
* Snippets bearing no \"# condition:\" directive are not
considered
* Snippets bearing conditions that evaluate to nil (or
produce an error) won't be considered.
* If the snippet has a condition that evaluates to non-nil
RESULT:
* If REQUIREMENT is t, the snippet is considered
* If REQUIREMENT is `eq' RESULT, the snippet is
considered
* Otherwise, the snippet is not considered.
* If it evaluates to the symbol 'always, all snippets are
considered for expansion, regardless of any conditions.
* If it evaluates to t or some other non-nil value
* Snippet bearing no conditions, or conditions that
evaluate to non-nil, are considered for expansion.
* Otherwise, the snippet is not considered.
Here's an example preventing snippets from being expanded from
inside comments, in `python-mode' only, with the exception of
snippets returning the symbol 'force-in-comment in their
conditions.
(add-hook 'python-mode-hook
'(lambda ()
(setq yas-buffer-local-condition
'(if (python-in-string/comment)
'(require-snippet-condition . force-in-comment)
t))))
The default value is similar, it filters out potential snippet
expansions inside comments and string literals, unless the
snippet itself contains a condition that returns the symbol
`force-in-comment'.")
;;; Internal variables
(defvar yas--version "0.8.0beta")
(defvar yas--menu-table (make-hash-table)
"A hash table of MAJOR-MODE symbols to menu keymaps.")
(defvar yas--escaped-characters
'(?\\ ?` ?\" ?' ?$ ?} ?{ ?\( ?\))
"List of characters which *might* need to be escaped.")
(defconst yas--field-regexp
"${\\([0-9]+:\\)?\\([^}]*\\)}"
"A regexp to *almost* recognize a field.")
(defconst yas--multi-dollar-lisp-expression-regexp
"$+[ \t\n]*\\(([^)]*)\\)"
"A regexp to *almost* recognize a \"$(...)\" expression.")
(defconst yas--backquote-lisp-expression-regexp
"`\\([^`]*\\)`"
"A regexp to recognize a \"`lisp-expression`\" expression." )
(defconst yas--transform-mirror-regexp
"${\\(?:\\([0-9]+\\):\\)?$\\([ \t\n]*([^}]*\\)"
"A regexp to *almost* recognize a mirror with a transform.")
(defconst yas--simple-mirror-regexp
"$\\([0-9]+\\)"
"A regexp to recognize a simple mirror.")
(defvar yas--snippet-id-seed 0
"Contains the next id for a snippet.")
(defun yas--snippet-next-id ()
(let ((id yas--snippet-id-seed))
(cl-incf yas--snippet-id-seed)
id))
;;; Minor mode stuff
;; XXX: `last-buffer-undo-list' is somehow needed in Carbon Emacs for MacOSX
(defvar last-buffer-undo-list nil)
(defvar yas--minor-mode-menu nil
"Holds the YASnippet menu.")
(defvar yas-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [(tab)] 'yas-expand)
(define-key map (kbd "TAB") 'yas-expand)
(define-key map "\C-c&\C-s" 'yas-insert-snippet)
(define-key map "\C-c&\C-n" 'yas-new-snippet)
(define-key map "\C-c&\C-v" 'yas-visit-snippet-file)
map)
"The keymap used when `yas-minor-mode' is active.")
(easy-menu-define yas--minor-mode-menu
yas-minor-mode-map
"Menu used when `yas-minor-mode' is active."
'("YASnippet" :visible yas-use-menu
"----"
["Expand trigger" yas-expand
:help "Possibly expand tab trigger before point"]
["Insert at point..." yas-insert-snippet
:help "Prompt for an expandable snippet and expand it at point"]
["New snippet..." yas-new-snippet
:help "Create a new snippet in an appropriate directory"]
["Visit snippet file..." yas-visit-snippet-file
:help "Prompt for an expandable snippet and find its file"]
"----"
("Snippet menu behaviour"
["Visit snippets" (setq yas-visit-from-menu t)
:help "Visit snippets from the menu"
:active t :style radio :selected yas-visit-from-menu]
["Expand snippets" (setq yas-visit-from-menu nil)
:help "Expand snippets from the menu"
:active t :style radio :selected (not yas-visit-from-menu)]
"----"
["Show all known modes" (setq yas-use-menu 'full)
:help "Show one snippet submenu for each loaded table"
:active t :style radio :selected (eq yas-use-menu 'full)]
["Abbreviate according to current mode" (setq yas-use-menu 'abbreviate)
:help "Show only snippet submenus for the current active modes"
:active t :style radio :selected (eq yas-use-menu 'abbreviate)])
("Indenting"
["Auto" (setq yas-indent-line 'auto)
:help "Indent each line of the snippet with `indent-according-to-mode'"
:active t :style radio :selected (eq yas-indent-line 'auto)]
["Fixed" (setq yas-indent-line 'fixed)
:help "Indent the snippet to the current column"
:active t :style radio :selected (eq yas-indent-line 'fixed)]
["None" (setq yas-indent-line 'none)
:help "Don't apply any particular snippet indentation after expansion"
:active t :style radio :selected (not (member yas-indent-line '(fixed auto)))]
"----"
["Also auto indent first line" (setq yas-also-auto-indent-first-line
(not yas-also-auto-indent-first-line))
:help "When auto-indenting also, auto indent the first line menu"
:active (eq yas-indent-line 'auto)
:style toggle :selected yas-also-auto-indent-first-line]
)
("Prompting method"
["System X-widget" (setq yas-prompt-functions
(cons 'yas-x-prompt
(remove 'yas-x-prompt
yas-prompt-functions)))
:help "Use your windowing system's (gtk, mac, windows, etc...) default menu"
:active t :style radio :selected (eq (car yas-prompt-functions)
'yas-x-prompt)]
["Dropdown-list" (setq yas-prompt-functions
(cons 'yas-dropdown-prompt
(remove 'yas-dropdown-prompt
yas-prompt-functions)))
:help "Use a special dropdown list"
:active t :style radio :selected (eq (car yas-prompt-functions)
'yas-dropdown-prompt)]
["Ido" (setq yas-prompt-functions
(cons 'yas-ido-prompt
(remove 'yas-ido-prompt
yas-prompt-functions)))
:help "Use an ido-style minibuffer prompt"
:active t :style radio :selected (eq (car yas-prompt-functions)
'yas-ido-prompt)]
["Completing read" (setq yas-prompt-functions
(cons 'yas-completing-prompt
(remove 'yas-completing-prompt
yas-prompt-functions)))
:help "Use a normal minibuffer prompt"
:active t :style radio :selected (eq (car yas-prompt-functions)
'yas-completing-prompt)]
)
("Misc"
["Wrap region in exit marker"
(setq yas-wrap-around-region
(not yas-wrap-around-region))
:help "If non-nil automatically wrap the selected text in the $0 snippet exit"
:style toggle :selected yas-wrap-around-region]
["Allow stacked expansions "
(setq yas-triggers-in-field
(not yas-triggers-in-field))
:help "If non-nil allow snippets to be triggered inside other snippet fields"
:style toggle :selected yas-triggers-in-field]
["Revive snippets on undo "
(setq yas-snippet-revival
(not yas-snippet-revival))
:help "If non-nil allow snippets to become active again after undo"
:style toggle :selected yas-snippet-revival]
["Good grace "
(setq yas-good-grace
(not yas-good-grace))
:help "If non-nil don't raise errors in bad embedded elisp in snippets"
:style toggle :selected yas-good-grace]
)
"----"
["Load snippets..." yas-load-directory
:help "Load snippets from a specific directory"]
["Reload everything" yas-reload-all
:help "Cleanup stuff, reload snippets, rebuild menus"]
["About" yas-about
:help "Display some information about YASnippet"]))
(defvar yas--extra-modes nil
"An internal list of modes for which to also lookup snippets.
This variable probably makes more sense as buffer-local, so
ensure your use `make-local-variable' when you set it.")
(define-obsolete-variable-alias 'yas-extra-modes 'yas--extra-modes "0.8.1")
(defvar yas--tables (make-hash-table)
"A hash table of mode symbols to `yas--table' objects.")
(defvar yas--parents (make-hash-table)
"A hash table of mode symbols do lists of direct parent mode symbols.
This list is populated when reading the \".yas-parents\" files
found when traversing snippet directories with
`yas-load-directory'.
There might be additional parenting information stored in the
`derived-mode-parent' property of some mode symbols, but that is
not recorded here.")
(defvar yas--direct-keymaps (list)
"Keymap alist supporting direct snippet keybindings.
This variable is placed in `emulation-mode-map-alists'.
Its elements looks like (TABLE-NAME . KEYMAP). They're
instantiated on `yas-reload-all' but KEYMAP is added to only when
loading snippets. `yas--direct-TABLE-NAME' is then a variable set
buffer-locally when entering `yas-minor-mode'. KEYMAP binds all
defined direct keybindings to the command
`yas-expand-from-keymap' which then which snippet to expand.")
(defun yas-direct-keymaps-reload ()
"Force reload the direct keybinding for active snippet tables."
(interactive)
(setq yas--direct-keymaps nil)
(maphash #'(lambda (name table)
(push (cons (intern (format "yas--direct-%s" name))
(yas--table-direct-keymap table))
yas--direct-keymaps))
yas--tables))
(defun yas--modes-to-activate ()
"Compute list of mode symbols that are active for `yas-expand'
and friends."
(let (dfs)
(setq dfs (lambda (mode &optional explored)
(push mode explored)
(cons mode
(loop for neighbour
in (cl-list* (get mode 'derived-mode-parent)
(ignore-errors (symbol-function mode))
(gethash mode yas--parents))
when (and neighbour
(not (memq neighbour explored))
(symbolp neighbour))
append (funcall dfs neighbour explored)))))
(remove-duplicates (append yas--extra-modes
(funcall dfs major-mode)))))
(defvar yas-minor-mode-hook nil
"Hook run when `yas-minor-mode' is turned on.")
;;;###autoload
(define-minor-mode yas-minor-mode
"Toggle YASnippet mode.
When YASnippet mode is enabled, `yas-expand', normally bound to
the TAB key, expands snippets of code depending on the major
mode.
With no argument, this command toggles the mode.
positive prefix argument turns on the mode.
Negative prefix argument turns off the mode.
Key bindings:
\\{yas-minor-mode-map}"
nil
;; The indicator for the mode line.
" yas"
:group 'yasnippet
(cond (yas-minor-mode
;; Install the direct keymaps in `emulation-mode-map-alists'
;; (we use `add-hook' even though it's not technically a hook,
;; but it works). Then define variables named after modes to
;; index `yas--direct-keymaps'.
;;
;; Also install the post-command-hook.
;;
(add-hook 'emulation-mode-map-alists 'yas--direct-keymaps)
(add-hook 'post-command-hook 'yas--post-command-handler nil t)
;; Set the `yas--direct-%s' vars for direct keymap expansion
;;
(dolist (mode (yas--modes-to-activate))
(let ((name (intern (format "yas--direct-%s" mode))))
(set-default name nil)
(set (make-local-variable name) t)))
;; Perform JIT loads
;;
(yas--load-pending-jits))
(t
;; Uninstall the direct keymaps and the post-command hook
;;
(remove-hook 'post-command-hook 'yas--post-command-handler t)
(remove-hook 'emulation-mode-map-alists 'yas--direct-keymaps))))
(defun yas-activate-extra-mode (mode)
"Activates the snippets for the given `mode' in the buffer.
The function can be called in the hook of a minor mode to
activate snippets associated with that mode."
(interactive
(let (modes
symbol)
(maphash (lambda (k _)
(setq modes (cons (list k) modes)))
yas--parents)
(setq symbol (completing-read
"Activate mode: " modes nil t))
(list
(when (not (string= "" symbol))
(intern symbol)))))
(when mode
(add-to-list (make-local-variable 'yas--extra-modes) mode)
(yas--load-pending-jits)))
(defun yas-deactivate-extra-mode (mode)
"Deactivates the snippets for the given `mode' in the buffer."
(interactive
(list (intern
(completing-read
"Deactivate mode: " (mapcar #'list yas--extra-modes) nil t))))
(set (make-local-variable 'yas--extra-modes)
(remove mode
yas--extra-modes)))
(defvar yas-dont-activate '(minibufferp)
"If non-nil don't let `yas-global-mode' affect some buffers.
If a function of zero arguments, then its result is used.
If a list of functions, then all functions must return nil to
activate yas for this buffer.
In Emacsen <= 23, this variable is buffer-local. Because
`yas-minor-mode-on' is called by `yas-global-mode' after
executing the buffer's major mode hook, setting this variable
there is an effective way to define exceptions to the \"global\"
activation behaviour.
In Emacsen > 23, only the global value is used. To define
per-mode exceptions to the \"global\" activation behaviour, call
`yas-minor-mode' with a negative argument directily in the major
mode's hook.")
(unless (> emacs-major-version 23)
(with-no-warnings
(make-variable-buffer-local 'yas-dont-activate)))
(defun yas-minor-mode-on ()
"Turn on YASnippet minor mode.
Honour `yas-dont-activate', which see."
(interactive)
;; Check `yas-dont-activate'
(unless (cond ((functionp yas-dont-activate)
(funcall yas-dont-activate))
((consp yas-dont-activate)
(some #'funcall yas-dont-activate))
(yas-dont-activate))
(yas-minor-mode 1)))
;;;###autoload
(define-globalized-minor-mode yas-global-mode yas-minor-mode yas-minor-mode-on
:group 'yasnippet
:require 'yasnippet)
(defun yas--global-mode-reload-with-jit-maybe ()
"Run `yas-reload-all' when `yas-global-mode' is on."
(when yas-global-mode (yas-reload-all)))
(add-hook 'yas-global-mode-hook 'yas--global-mode-reload-with-jit-maybe)
;;; Major mode stuff
(defvar yas--font-lock-keywords
(append '(("^#.*$" . font-lock-comment-face))
lisp-font-lock-keywords-2
'(("$\\([0-9]+\\)"
(0 font-lock-keyword-face)
(1 font-lock-string-face t))
("${\\([0-9]+\\):?"
(0 font-lock-keyword-face)
(1 font-lock-warning-face t))
("${" . font-lock-keyword-face)
("$[0-9]+?" . font-lock-preprocessor-face)
("\\(\\$(\\)" 1 font-lock-preprocessor-face)
("}"
(0 font-lock-keyword-face)))))
(defvar snippet-mode-map
(let ((map (make-sparse-keymap)))
(easy-menu-define nil
map
"Menu used when snippet-mode is active."
(cons "Snippet"
(mapcar #'(lambda (ent)
(when (third ent)
(define-key map (third ent) (second ent)))
(vector (first ent) (second ent) t))
'(("Load this snippet" yas-load-snippet-buffer "\C-c\C-l")
("Load and quit window" yas-load-snippet-buffer-and-close "\C-c\C-c")
("Try out this snippet" yas-tryout-snippet "\C-c\C-t")))))
map)
"The keymap used when `snippet-mode' is active.")
(define-derived-mode snippet-mode text-mode "Snippet"
"A mode for editing yasnippets"
(setq font-lock-defaults '(yas--font-lock-keywords))
(set (make-local-variable 'require-final-newline) nil)
(set (make-local-variable 'comment-start) "#")
(set (make-local-variable 'comment-start-skip) "#+[\t ]*"))
;;; Internal structs for template management
(defstruct (yas--template (:constructor yas--make-template))
"A template for a snippet."
key
content
name
condition
expand-env
file
keybinding
uuid
menu-binding-pair
group ;; as dictated by the #group: directive or .yas-make-groups
perm-group ;; as dictated by `yas-define-menu'
table
)
(defstruct (yas--table (:constructor yas--make-snippet-table (name)))
"A table to store snippets for a particular mode.
Has the following fields:
`yas--table-name'
A symbol name normally corresponding to a major mode, but can
also be a pseudo major-mode to be used in
`yas-activate-extra-mode', for example.
`yas--table-hash'
A hash table (KEY . NAMEHASH), known as the \"keyhash\". KEY is
a string or a vector, where the former is the snippet's trigger
and the latter means it's a direct keybinding. NAMEHASH is yet
another hash of (NAME . TEMPLATE) where NAME is the snippet's
name and TEMPLATE is a `yas--template' object.
`yas--table-direct-keymap'
A keymap for the snippets in this table that have direct
keybindings. This is kept in sync with the keyhash, i.e., all
the elements of the keyhash that are vectors appear here as
bindings to `yas-expand-from-keymap'.
`yas--table-uuidhash'
A hash table mapping snippets uuid's to the same `yas--template'
objects. A snippet uuid defaults to the snippet's name."
name
(hash (make-hash-table :test 'equal))
(uuidhash (make-hash-table :test 'equal))
(parents nil)
(direct-keymap (make-sparse-keymap)))
(defun yas--get-template-by-uuid (mode uuid)
"Find the snippet template in MODE by its UUID."
(let* ((table (gethash mode yas--tables mode)))
(when table
(gethash uuid (yas--table-uuidhash table)))))
;; Apropos storing/updating in TABLE, this works in two steps:
;;
;; 1. `yas--remove-template-by-uuid' removes any
;; keyhash-namehash-template mappings from TABLE, grabbing the
;; snippet by its uuid. Also removes mappings from TABLE's
;; `yas--table-direct-keymap' (FIXME: and should probably take care
;; of potentially stale menu bindings right?.)
;;
;; 2. `yas--add-template' adds this all over again.
;;
;; Create a new or add to an existing keyhash-namehash mapping.
;;
;; For reference on understanding this, consider three snippet
;; definitions:
;;
;; A: # name: The Foo
;; # key: foo
;; # binding: C-c M-l
;;
;; B: # name: Mrs Foo
;; # key: foo
;;
;; C: # name: The Bar
;; # binding: C-c M-l
;;
;; D: # name: Baz
;; # key: baz
;;