forked from joaotavora/yasnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yasnippet.el
5309 lines (4674 loc) · 215 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-2019 Free Software Foundation, Inc.
;; Authors: pluskid <[email protected]>,
;; João Távora <[email protected]>,
;; Noam Postavsky <[email protected]>
;; Maintainer: Noam Postavsky <[email protected]>
;; Version: 0.14.0
;; X-URL: http://github.com/joaotavora/yasnippet
;; Keywords: convenience, emulation
;; URL: http://github.com/joaotavora/yasnippet
;; Package-Requires: ((cl-lib "0.5"))
;; 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/joaotavora/yasnippet
;;; Code:
(require 'cl-lib)
(require 'eldoc) ; Needed for 24.
(declare-function cl-progv-after "cl-extra") ; Needed for 23.4.
(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)
(defconst yas--loaddir
(file-name-directory (or load-file-name buffer-file-name))
"Directory that yasnippet was loaded from.")
(defconst yas-installed-snippets-dir (expand-file-name "snippets" yas--loaddir))
(make-obsolete-variable 'yas-installed-snippets-dir "\
Yasnippet no longer comes with installed snippets" "0.14")
(defconst yas--default-user-snippets-dir
(expand-file-name "snippets" user-emacs-directory))
(defcustom yas-snippet-dirs (list yas--default-user-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 (directory :tag "Single directory")
(repeat :tag "List of directories"
(choice (directory) (variable))))
: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)))))
(defcustom yas-new-snippet-default "\
# -*- mode: snippet -*-
# name: $1
# key: ${2:${1:$(yas--key-from-desc yas-text)}}
# --
$0`(yas-escape-text yas-selected-text)`"
"Default snippet to use when creating a new snippet.
If nil, don't use any snippet."
:type 'string)
(defcustom yas-prompt-functions '(yas-dropdown-prompt
yas-completing-prompt
yas-maybe-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))
(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)))
(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)
(defcustom yas-also-indent-empty-lines nil
"Non-nil means also indent empty lines according to mode."
:type 'boolean)
(defcustom yas-snippet-revival t
"Non-nil means re-activate snippet fields after undo/redo."
:type 'boolean)
(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)
(defcustom yas-fallback-behavior 'return-nil
"This option is obsolete.
Now that the conditional keybinding `yas-maybe-expand' is
available, there's no more need for it."
:type '(choice (const :tag "Call previous command" call-other-command)
(const :tag "Do nothing" return-nil)))
(make-obsolete-variable
'yas-fallback-behavior
"For `call-other-command' behavior bind to the conditional
command value `yas-maybe-expand', for `return-nil' behavior bind
directly to `yas-expand'."
"0.12")
(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)
(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)
(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)))
(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)
(defcustom yas-wrap-around-region nil
"What to insert for snippet's $0 field.
If set to a character, insert contents of corresponding register.
If non-nil insert region contents. This can be overridden on a
per-snippet basis. A value of `cua' is considered equivalent to
`?0' for backwards compatibility."
:type '(choice (character :tag "Insert from register")
(const t :tag "Insert region contents")
(const nil :tag "Don't insert anything")
(const cua))) ; backwards compat
(defcustom yas-good-grace t
"If non-nil, don't raise errors in elisp evaluation.
This affects both the inline elisp in snippets and the hook
variables such as `yas-after-exit-snippet-hook'.
If this variable's value is `inline', an error string \"[yas]
error\" is returned instead of raising the error. If this
variable's value is `hooks', a message is output to according to
`yas-verbosity-level'. If this variable's value is t, both are
active."
:type 'boolean)
(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)
(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))
(defcustom yas-alias-to-yas/prefix-p t
"If non-nil make aliases for the old style yas/ prefixed symbols.
It must be set to nil before loading yasnippet to take effect."
:type 'boolean)
;; 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")
(defface yas--field-debug-face
'()
"The face used for debugging some overlays normally hidden")
;;; User-visible variables
(defconst yas-maybe-skip-and-clear-field
'(menu-item "" yas-skip-and-clear-field
:filter yas--maybe-clear-field-filter)
"A conditional key definition.
This can be used as a key definition in keymaps to bind a key to
`yas-skip-and-clear-field' only when at the beginning of an
unmodified snippet field.")
(defconst yas-maybe-clear-field
'(menu-item "" yas-clear-field
:filter yas--maybe-clear-field-filter)
"A conditional key definition.
This can be used as a key definition in keymaps to bind a key to
`yas-clear-field' only when at the beginning of an
unmodified snippet field.")
(defun yas-filtered-definition (def)
"Return a condition key definition.
The condition will respect the value of `yas-keymap-disable-hook'."
`(menu-item "" ,def
:filter ,(lambda (cmd) (unless (run-hook-with-args-until-success
'yas-keymap-disable-hook)
cmd))))
(defvar yas-keymap
(let ((map (make-sparse-keymap)))
(define-key map [(tab)] (yas-filtered-definition 'yas-next-field-or-maybe-expand))
(define-key map (kbd "TAB") (yas-filtered-definition 'yas-next-field-or-maybe-expand))
(define-key map [(shift tab)] (yas-filtered-definition 'yas-prev-field))
(define-key map [backtab] (yas-filtered-definition 'yas-prev-field))
(define-key map (kbd "C-g") (yas-filtered-definition 'yas-abort-snippet))
;; Yes, filters can be chained!
(define-key map (kbd "C-d") (yas-filtered-definition yas-maybe-skip-and-clear-field))
(define-key map (kbd "DEL") (yas-filtered-definition yas-maybe-clear-field))
map)
"The active keymap while a snippet expansion is in progress.")
(defvar yas-key-syntaxes (list #'yas-try-key-from-whitespace
"w_.()" "w_." "w_" "w")
"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' has the value (\"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-barbaz
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.")
(defconst yas-not-string-or-comment-condition
'(if (let ((ppss (syntax-ppss)))
(or (nth 3 ppss) (nth 4 ppss)))
'(require-snippet-condition . force-in-comment)
t)
"Disables snippet expansion in strings and comments.
To use, set `yas-buffer-local-condition' to this value.")
(defcustom yas-buffer-local-condition 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-syntax-comment-or-string-p)
\\='(require-snippet-condition . force-in-comment)
t))))"
:type
`(choice
(const :tag "Disable snippet expansion inside strings and comments"
,yas-not-string-or-comment-condition)
(const :tag "Expand all snippets regardless of conditions" always)
(const :tag "Expand snippets unless their condition is nil" t)
(const :tag "Disable all snippet expansion" nil)
sexp))
(defcustom yas-keymap-disable-hook nil
"The `yas-keymap' bindings are disabled if any function in this list returns non-nil.
This is useful to control whether snippet navigation bindings
override bindings from other packages (e.g., `company-mode')."
:type 'hook)
(defcustom yas-overlay-priority 100
"Priority to use for yasnippets overlays.
This is useful to control whether snippet navigation bindings
override `keymap' overlay property bindings from other packages."
:type 'integer)
(defcustom yas-inhibit-overlay-modification-protection nil
"If nil, changing text outside the active field aborts the snippet.
This protection is intended to prevent yasnippet from ending up
in an inconsistent state. However, some packages (e.g., the
company completion package) may trigger this protection when it
is not needed. In that case, setting this variable to non-nil
can be useful."
;; See also `yas--on-protection-overlay-modification'.
:type 'boolean)
;;; Internal variables
(defconst yas--version "0.14.0")
(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.")
(defvar yas--original-auto-fill-function nil
"The original value of `auto-fill-function'.")
(make-variable-buffer-local 'yas--original-auto-fill-function)
(defvar yas--watch-auto-fill-backtrace nil)
(defun yas--watch-auto-fill (sym newval op _where)
(when (and (or (and (eq sym 'yas--original-auto-fill-function)
(null newval)
(eq auto-fill-function 'yas--auto-fill))
(and (eq sym 'auto-fill-function)
(eq newval 'yas--auto-fill)
(null yas--original-auto-fill-function)))
(null yas--watch-auto-fill-backtrace)
(fboundp 'backtrace-frames) ; Suppress compiler warning.
;; If we're about to change `auto-fill-function' too,
;; it's okay (probably).
(not (and (eq op 'makunbound)
(not (eq (default-value 'auto-fill-function) 'yas--auto-fill))
(cl-member 'kill-all-local-variables
(backtrace-frames 'yas--watch-auto-fill)
:key (lambda (frame) (nth 1 frame))))))
(setq yas--watch-auto-fill-backtrace
(backtrace-frames 'yas--watch-auto-fill))))
;; Try to get more info on #873/919 (this only works for Emacs 26+).
(when (fboundp 'add-variable-watcher)
(add-variable-watcher 'yas--original-auto-fill-function
#'yas--watch-auto-fill)
(add-variable-watcher 'auto-fill-function
#'yas--watch-auto-fill))
(defun yas--snippet-next-id ()
(let ((id yas--snippet-id-seed))
(cl-incf yas--snippet-id-seed)
id))
;;; Minor mode stuff
(defvar yas--minor-mode-menu nil
"Holds the YASnippet menu.")
(defvar yas--condition-cache-timestamp nil)
(defun yas-maybe-expand-abbrev-key-filter (cmd)
"Return CMD if there is an expandable snippet at point.
This function is useful as a `:filter' to a conditional key
definition."
(when (let ((yas--condition-cache-timestamp (current-time)))
(yas--templates-for-key-at-point))
cmd))
(define-obsolete-function-alias 'yas--maybe-expand-key-filter
#'yas-maybe-expand-abbrev-key-filter "0.14")
(defconst yas-maybe-expand
'(menu-item "" yas-expand :filter yas-maybe-expand-abbrev-key-filter)
"A conditional key definition.
This can be used as a key definition in keymaps to bind a key to
`yas-expand' only when there is a snippet available to be
expanded.")
(defvar yas-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [(tab)] yas-maybe-expand)
(define-key map (kbd "TAB") yas-maybe-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"]))
(define-obsolete-variable-alias 'yas-extra-modes 'yas--extra-modes "0.9.1")
(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.")
(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 `yas-maybe-expand-from-keymap'
which decides on the 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 (&optional mode)
"Compute list of mode symbols that are active for `yas-expand' and friends."
(defvar yas--dfs) ;We rely on dynbind. We could use `letrec' instead!
(let* ((explored (if mode (list mode) ; Building up list in reverse.
(cons major-mode (reverse yas--extra-modes))))
(yas--dfs
(lambda (mode)
(cl-loop for neighbour
in (cl-list* (or (get mode 'derived-mode-parent)
;; Consider `fundamental-mode'
;; as ultimate ancestor.
'fundamental-mode)
;; NOTE: `fboundp' check is redundant
;; since Emacs 24.4.
(and (fboundp mode) (symbol-function mode))
(gethash mode yas--parents))
when (and neighbour
(not (memq neighbour explored))
(symbolp neighbour))
do (push neighbour explored)
(funcall yas--dfs neighbour)))))
(mapc yas--dfs explored)
(nreverse explored)))
(defvar yas-minor-mode-hook nil
"Hook run when `yas-minor-mode' is turned on.")
(defun yas--auto-fill-wrapper ()
(when (and auto-fill-function
(not (eq auto-fill-function #'yas--auto-fill)))
(setq yas--original-auto-fill-function auto-fill-function)
(setq auto-fill-function #'yas--auto-fill)))
;;;###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}"
:lighter " yas" ;; The indicator for the mode line.
(cond ((and yas-minor-mode (featurep 'yasnippet))
;; 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.
;;
(cl-pushnew 'yas--direct-keymaps emulation-mode-map-alists)
(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)
;; Install auto-fill handler.
(yas--auto-fill-wrapper) ; Now...
(add-hook 'auto-fill-mode-hook #'yas--auto-fill-wrapper)) ; or later.
(t
;; Uninstall the direct keymaps, post-command hook, and
;; auto-fill handler.
(remove-hook 'post-command-hook #'yas--post-command-handler t)
(remove-hook 'auto-fill-mode-hook #'yas--auto-fill-wrapper)
(when (local-variable-p 'yas--original-auto-fill-function)
(setq auto-fill-function yas--original-auto-fill-function))
(setq emulation-mode-map-alists
(remove 'yas--direct-keymaps emulation-mode-map-alists)))))
(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)))
(defun yas-temp-buffer-p (&optional buffer)
(eq (aref (buffer-name buffer) 0) ?\s))
(define-obsolete-variable-alias 'yas-dont-activate
'yas-dont-activate-functions "0.9.2")
(defvar yas-dont-activate-functions (list #'minibufferp #'yas-temp-buffer-p)
"Special hook to control which buffers `yas-global-mode' affects.
Functions are called with no argument, and should return non-nil to prevent
`yas-global-mode' from enabling yasnippet in this buffer.
In Emacsen < 24, 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 >= 24, 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-functions', which see."
(interactive)
(unless (or
;; The old behavior used for Emacs<24 was to set
;; `yas-dont-activate-functions' to t buffer-locally.
(not (or (listp yas-dont-activate-functions)
(functionp yas-dont-activate-functions)))
(run-hook-with-args-until-success 'yas-dont-activate-functions))
(yas-minor-mode 1)))
;;;###autoload
(define-globalized-minor-mode yas-global-mode yas-minor-mode yas-minor-mode-on)
(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))
(with-temp-buffer
(let ((prog-mode-hook nil)
(emacs-lisp-mode-hook nil))
(ignore-errors (emacs-lisp-mode)))
(font-lock-set-defaults)