summaryrefslogtreecommitdiff
path: root/.emacs.d/lisp/yasnippet.el
diff options
context:
space:
mode:
authorSimeon Simeonov2019-01-08 10:41:09 +0100
committerSimeon Simeonov2019-01-08 10:41:09 +0100
commit1adfb1d130c27b4d0f12d956446604a3714c1bc9 (patch)
tree4a5acbabe5c01af7fa0e2fbcca183d634df16c78 /.emacs.d/lisp/yasnippet.el
parent91a787a67152d5c62a311a0e49a5242d8a6fb2b8 (diff)
Add lua-mode.el, update php-mode and yasnippet, add some new C and Python snippets
Diffstat (limited to '.emacs.d/lisp/yasnippet.el')
-rw-r--r--.emacs.d/lisp/yasnippet.el82
1 files changed, 68 insertions, 14 deletions
diff --git a/.emacs.d/lisp/yasnippet.el b/.emacs.d/lisp/yasnippet.el
index 46624ed..065e709 100644
--- a/.emacs.d/lisp/yasnippet.el
+++ b/.emacs.d/lisp/yasnippet.el
@@ -132,6 +132,7 @@
132;;; Code: 132;;; Code:
133 133
134(require 'cl-lib) 134(require 'cl-lib)
135(require 'eldoc) ; Needed for 24.
135(declare-function cl-progv-after "cl-extra") ; Needed for 23.4. 136(declare-function cl-progv-after "cl-extra") ; Needed for 23.4.
136(require 'easymenu) 137(require 'easymenu)
137(require 'help-mode) 138(require 'help-mode)
@@ -402,7 +403,15 @@ It must be set to nil before loading yasnippet to take effect."
402 "A conditional key definition. 403 "A conditional key definition.
403This can be used as a key definition in keymaps to bind a key to 404This can be used as a key definition in keymaps to bind a key to
404`yas-skip-and-clear-field' only when at the beginning of an 405`yas-skip-and-clear-field' only when at the beginning of an
405unmodified snippey field.") 406unmodified snippet field.")
407
408(defconst yas-maybe-clear-field
409 '(menu-item "" yas-clear-field
410 :filter yas--maybe-clear-field-filter)
411 "A conditional key definition.
412This can be used as a key definition in keymaps to bind a key to
413`yas-clear-field' only when at the beginning of an
414unmodified snippet field.")
406 415
407(defvar yas-keymap (let ((map (make-sparse-keymap))) 416(defvar yas-keymap (let ((map (make-sparse-keymap)))
408 (define-key map [(tab)] 'yas-next-field-or-maybe-expand) 417 (define-key map [(tab)] 'yas-next-field-or-maybe-expand)
@@ -411,6 +420,7 @@ unmodified snippey field.")
411 (define-key map [backtab] 'yas-prev-field) 420 (define-key map [backtab] 'yas-prev-field)
412 (define-key map (kbd "C-g") 'yas-abort-snippet) 421 (define-key map (kbd "C-g") 'yas-abort-snippet)
413 (define-key map (kbd "C-d") yas-maybe-skip-and-clear-field) 422 (define-key map (kbd "C-d") yas-maybe-skip-and-clear-field)
423 (define-key map (kbd "DEL") yas-maybe-clear-field)
414 map) 424 map)
415 "The active keymap while a snippet expansion is in progress.") 425 "The active keymap while a snippet expansion is in progress.")
416 426
@@ -442,7 +452,7 @@ are looked for first. Failing that, longer keys composed of
442\"word\" or \"symbol\" syntax are looked for. Therefore, 452\"word\" or \"symbol\" syntax are looked for. Therefore,
443triggering after 453triggering after
444 454
445foo-bar 455foo-barbaz
446 456
447will, according to the \"w\" element first try \"barbaz\". If 457will, according to the \"w\" element first try \"barbaz\". If
448that isn't a trigger key, \"foo-barbaz\" is tried, respecting the 458that isn't a trigger key, \"foo-barbaz\" is tried, respecting the
@@ -623,13 +633,19 @@ override bindings from other packages (e.g., `company-mode')."
623 633
624(defvar yas--condition-cache-timestamp nil) 634(defvar yas--condition-cache-timestamp nil)
625 635
626(defun yas--maybe-expand-key-filter (cmd) 636(defun yas-maybe-expand-abbrev-key-filter (cmd)
637 "Return CMD if there is an expandable snippet at point.
638This function is useful as a `:filter' to a conditional key
639definition."
627 (when (let ((yas--condition-cache-timestamp (current-time))) 640 (when (let ((yas--condition-cache-timestamp (current-time)))
628 (yas--templates-for-key-at-point)) 641 (yas--templates-for-key-at-point))
629 cmd)) 642 cmd))
630 643
644(define-obsolete-function-alias 'yas--maybe-expand-key-filter
645 #'yas-maybe-expand-abbrev-key-filter "0.14")
646
631(defconst yas-maybe-expand 647(defconst yas-maybe-expand
632 '(menu-item "" yas-expand :filter yas--maybe-expand-key-filter) 648 '(menu-item "" yas-expand :filter yas-maybe-expand-abbrev-key-filter)
633 "A conditional key definition. 649 "A conditional key definition.
634This can be used as a key definition in keymaps to bind a key to 650This can be used as a key definition in keymaps to bind a key to
635`yas-expand' only when there is a snippet available to be 651`yas-expand' only when there is a snippet available to be
@@ -801,7 +817,10 @@ which decides on the snippet to expand.")
801 (yas--dfs 817 (yas--dfs
802 (lambda (mode) 818 (lambda (mode)
803 (cl-loop for neighbour 819 (cl-loop for neighbour
804 in (cl-list* (get mode 'derived-mode-parent) 820 in (cl-list* (or (get mode 'derived-mode-parent)
821 ;; Consider `fundamental-mode'
822 ;; as ultimate ancestor.
823 'fundamental-mode)
805 ;; NOTE: `fboundp' check is redundant 824 ;; NOTE: `fboundp' check is redundant
806 ;; since Emacs 24.4. 825 ;; since Emacs 24.4.
807 (and (fboundp mode) (symbol-function mode)) 826 (and (fboundp mode) (symbol-function mode))
@@ -2984,6 +3003,16 @@ The last element of POSSIBILITIES may be a list of strings."
2984 (funcall fn "Choose: " possibilities)) 3003 (funcall fn "Choose: " possibilities))
2985 yas-prompt-functions))) 3004 yas-prompt-functions)))
2986 3005
3006(defun yas-completing-read (&rest args)
3007 "A snippet-aware version of `completing-read'.
3008This can be used to query the user for the initial value of a
3009snippet field. The arguments are the same as `completing-read'.
3010
3011\(fn PROMPT COLLECTION &optional PREDICATE REQUIRE-MATCH INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)"
3012 (unless (or yas-moving-away-p
3013 yas-modified-p)
3014 (apply #'completing-read args)))
3015
2987(defun yas--auto-next () 3016(defun yas--auto-next ()
2988 "Helper for `yas-auto-next'." 3017 "Helper for `yas-auto-next'."
2989 (remove-hook 'post-command-hook #'yas--auto-next t) 3018 (remove-hook 'post-command-hook #'yas--auto-next t)
@@ -3013,7 +3042,7 @@ The last element of POSSIBILITIES may be a list of strings."
3013(defun yas-verify-value (possibilities) 3042(defun yas-verify-value (possibilities)
3014 "Verify that the current field value is in POSSIBILITIES. 3043 "Verify that the current field value is in POSSIBILITIES.
3015Otherwise signal `yas-exception'." 3044Otherwise signal `yas-exception'."
3016 (when (and yas-moving-away-p (cl-notany (lambda (pos) (string= pos yas-text)) possibilities)) 3045 (when (and yas-moving-away-p (not (member yas-text possibilities)))
3017 (yas-throw (format "Field only allows %s" possibilities)))) 3046 (yas-throw (format "Field only allows %s" possibilities))))
3018 3047
3019(defun yas-field-value (number) 3048(defun yas-field-value (number)
@@ -3326,8 +3355,9 @@ Otherwise delegate to `yas-next-field'."
3326If there's none, exit the snippet." 3355If there's none, exit the snippet."
3327 (interactive) 3356 (interactive)
3328 (unless arg (setq arg 1)) 3357 (unless arg (setq arg 1))
3329 (let* ((snippet (car (yas-active-snippets))) 3358 (let* ((active-field (overlay-get yas--active-field-overlay 'yas--field))
3330 (active-field (overlay-get yas--active-field-overlay 'yas--field)) 3359 (snippet (car (yas-active-snippets (yas--field-start active-field)
3360 (yas--field-end active-field))))
3331 (target-field (yas--find-next-field arg snippet active-field))) 3361 (target-field (yas--find-next-field arg snippet active-field)))
3332 (yas--letenv (yas--snippet-expand-env snippet) 3362 (yas--letenv (yas--snippet-expand-env snippet)
3333 ;; Apply transform to active field. 3363 ;; Apply transform to active field.
@@ -3658,6 +3688,11 @@ Use as a `:filter' argument for a conditional keybinding."
3658 (yas--skip-and-clear (or field (yas-current-field))) 3688 (yas--skip-and-clear (or field (yas-current-field)))
3659 (yas-next-field 1)) 3689 (yas-next-field 1))
3660 3690
3691(defun yas-clear-field (&optional field)
3692 "Clears unmodified FIELD if at field start."
3693 (interactive)
3694 (yas--skip-and-clear (or field (yas-current-field))))
3695
3661(defun yas-skip-and-clear-or-delete-char (&optional field) 3696(defun yas-skip-and-clear-or-delete-char (&optional field)
3662 "Clears unmodified field if at field start, skips to next tab. 3697 "Clears unmodified field if at field start, skips to next tab.
3663 3698
@@ -3745,7 +3780,15 @@ field start. This hook does nothing if an undo is in progress."
3745 ;; We delete text starting from the END of insertion. 3780 ;; We delete text starting from the END of insertion.
3746 (yas--skip-and-clear field end)) 3781 (yas--skip-and-clear field end))
3747 (setf (yas--field-modified-p field) t) 3782 (setf (yas--field-modified-p field) t)
3748 (yas--advance-end-maybe field (overlay-end overlay)) 3783 ;; Adjust any pending active fields in case of stacked
3784 ;; expansion.
3785 (let ((pfield field)
3786 (psnippets (yas-active-snippets beg end)))
3787 (while (and pfield psnippets)
3788 (let ((psnippet (pop psnippets)))
3789 (cl-assert (memq pfield (yas--snippet-fields psnippet)))
3790 (yas--advance-end-maybe pfield (overlay-end overlay))
3791 (setq pfield (yas--snippet-previous-active-field psnippet)))))
3749 (save-excursion 3792 (save-excursion
3750 (yas--field-update-display field)) 3793 (yas--field-update-display field))
3751 (yas--update-mirrors snippet))) 3794 (yas--update-mirrors snippet)))
@@ -4033,15 +4076,21 @@ Returns the newly created snippet."
4033 (yas--letenv expand-env 4076 (yas--letenv expand-env
4034 ;; Put a single undo action for the expanded snippet's 4077 ;; Put a single undo action for the expanded snippet's
4035 ;; content. 4078 ;; content.
4036 (let ((buffer-undo-list t)) 4079 (let ((buffer-undo-list t)
4080 (inhibit-modification-hooks t))
4037 ;; Some versions of cc-mode fail when inserting snippet 4081 ;; Some versions of cc-mode fail when inserting snippet
4038 ;; content in a narrowed buffer. 4082 ;; content in a narrowed buffer, so make sure to insert
4083 ;; before narrowing. Furthermore, call before and after
4084 ;; change functions manually, otherwise cc-mode's cache can
4085 ;; get messed up.
4039 (goto-char begin) 4086 (goto-char begin)
4087 (run-hook-with-args 'before-change-functions begin begin)
4040 (insert content) 4088 (insert content)
4041 (setq end (+ end (length content))) 4089 (setq end (+ end (length content)))
4042 (narrow-to-region begin end) 4090 (narrow-to-region begin end)
4043 (goto-char (point-min)) 4091 (goto-char (point-min))
4044 (yas--snippet-parse-create snippet)) 4092 (yas--snippet-parse-create snippet)
4093 (run-hook-with-args 'after-change-functions (point-min) (point-max) 0))
4045 (when (listp buffer-undo-list) 4094 (when (listp buffer-undo-list)
4046 (push (cons (point-min) (point-max)) 4095 (push (cons (point-min) (point-max))
4047 buffer-undo-list)) 4096 buffer-undo-list))
@@ -4505,8 +4554,7 @@ Lisp expression."
4505 current-string '(?`)))))) 4554 current-string '(?`))))))
4506 (goto-char (match-beginning 0)) 4555 (goto-char (match-beginning 0))
4507 (when transformed 4556 (when transformed
4508 (let ((marker (make-marker)) 4557 (let ((marker (make-marker)))
4509 (before-change-functions (cdr before-change-functions)))
4510 (yas--save-restriction-and-widen 4558 (yas--save-restriction-and-widen
4511 (insert "Y") ;; quite horrendous, I love it :) 4559 (insert "Y") ;; quite horrendous, I love it :)
4512 (set-marker marker (point)) 4560 (set-marker marker (point))
@@ -4905,6 +4953,12 @@ object satisfying `yas--field-p' to restrict the expansion to.")))
4905 (help-xref-button 1 'help-snippet-def template) 4953 (help-xref-button 1 'help-snippet-def template)
4906 (delete-region (match-end 1) (match-end 0)) 4954 (delete-region (match-end 1) (match-end 0))
4907 (delete-region (match-beginning 0) (match-beginning 1))))))) 4955 (delete-region (match-beginning 0) (match-beginning 1)))))))
4956
4957;;; Eldoc configuration.
4958(eldoc-add-command 'yas-next-field-or-maybe-expand
4959 'yas-next-field 'yas-prev-field
4960 'yas-expand 'yas-expand-from-keymap
4961 'yas-expand-from-trigger-key)
4908 4962
4909;;; Utils 4963;;; Utils
4910 4964