From 1adfb1d130c27b4d0f12d956446604a3714c1bc9 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Tue, 8 Jan 2019 10:41:09 +0100 Subject: Add lua-mode.el, update php-mode and yasnippet, add some new C and Python snippets --- .emacs.d/lisp/yasnippet.el | 82 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 14 deletions(-) (limited to '.emacs.d/lisp/yasnippet.el') 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 @@ ;;; 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) @@ -402,7 +403,15 @@ It must be set to nil before loading yasnippet to take effect." "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 snippey field.") +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.") (defvar yas-keymap (let ((map (make-sparse-keymap))) (define-key map [(tab)] 'yas-next-field-or-maybe-expand) @@ -411,6 +420,7 @@ unmodified snippey field.") (define-key map [backtab] 'yas-prev-field) (define-key map (kbd "C-g") 'yas-abort-snippet) (define-key map (kbd "C-d") yas-maybe-skip-and-clear-field) + (define-key map (kbd "DEL") yas-maybe-clear-field) map) "The active keymap while a snippet expansion is in progress.") @@ -442,7 +452,7 @@ are looked for first. Failing that, longer keys composed of \"word\" or \"symbol\" syntax are looked for. Therefore, triggering after -foo-bar +foo-barbaz will, according to the \"w\" element first try \"barbaz\". If that isn't a trigger key, \"foo-barbaz\" is tried, respecting the @@ -623,13 +633,19 @@ override bindings from other packages (e.g., `company-mode')." (defvar yas--condition-cache-timestamp nil) -(defun yas--maybe-expand-key-filter (cmd) +(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-key-filter) + '(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 @@ -801,7 +817,10 @@ which decides on the snippet to expand.") (yas--dfs (lambda (mode) (cl-loop for neighbour - in (cl-list* (get mode 'derived-mode-parent) + 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)) @@ -2984,6 +3003,16 @@ The last element of POSSIBILITIES may be a list of strings." (funcall fn "Choose: " possibilities)) yas-prompt-functions))) +(defun yas-completing-read (&rest args) + "A snippet-aware version of `completing-read'. +This can be used to query the user for the initial value of a +snippet field. The arguments are the same as `completing-read'. + +\(fn PROMPT COLLECTION &optional PREDICATE REQUIRE-MATCH INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)" + (unless (or yas-moving-away-p + yas-modified-p) + (apply #'completing-read args))) + (defun yas--auto-next () "Helper for `yas-auto-next'." (remove-hook 'post-command-hook #'yas--auto-next t) @@ -3013,7 +3042,7 @@ The last element of POSSIBILITIES may be a list of strings." (defun yas-verify-value (possibilities) "Verify that the current field value is in POSSIBILITIES. Otherwise signal `yas-exception'." - (when (and yas-moving-away-p (cl-notany (lambda (pos) (string= pos yas-text)) possibilities)) + (when (and yas-moving-away-p (not (member yas-text possibilities))) (yas-throw (format "Field only allows %s" possibilities)))) (defun yas-field-value (number) @@ -3326,8 +3355,9 @@ Otherwise delegate to `yas-next-field'." If there's none, exit the snippet." (interactive) (unless arg (setq arg 1)) - (let* ((snippet (car (yas-active-snippets))) - (active-field (overlay-get yas--active-field-overlay 'yas--field)) + (let* ((active-field (overlay-get yas--active-field-overlay 'yas--field)) + (snippet (car (yas-active-snippets (yas--field-start active-field) + (yas--field-end active-field)))) (target-field (yas--find-next-field arg snippet active-field))) (yas--letenv (yas--snippet-expand-env snippet) ;; Apply transform to active field. @@ -3658,6 +3688,11 @@ Use as a `:filter' argument for a conditional keybinding." (yas--skip-and-clear (or field (yas-current-field))) (yas-next-field 1)) +(defun yas-clear-field (&optional field) + "Clears unmodified FIELD if at field start." + (interactive) + (yas--skip-and-clear (or field (yas-current-field)))) + (defun yas-skip-and-clear-or-delete-char (&optional field) "Clears unmodified field if at field start, skips to next tab. @@ -3745,7 +3780,15 @@ field start. This hook does nothing if an undo is in progress." ;; We delete text starting from the END of insertion. (yas--skip-and-clear field end)) (setf (yas--field-modified-p field) t) - (yas--advance-end-maybe field (overlay-end overlay)) + ;; Adjust any pending active fields in case of stacked + ;; expansion. + (let ((pfield field) + (psnippets (yas-active-snippets beg end))) + (while (and pfield psnippets) + (let ((psnippet (pop psnippets))) + (cl-assert (memq pfield (yas--snippet-fields psnippet))) + (yas--advance-end-maybe pfield (overlay-end overlay)) + (setq pfield (yas--snippet-previous-active-field psnippet))))) (save-excursion (yas--field-update-display field)) (yas--update-mirrors snippet))) @@ -4033,15 +4076,21 @@ Returns the newly created snippet." (yas--letenv expand-env ;; Put a single undo action for the expanded snippet's ;; content. - (let ((buffer-undo-list t)) + (let ((buffer-undo-list t) + (inhibit-modification-hooks t)) ;; Some versions of cc-mode fail when inserting snippet - ;; content in a narrowed buffer. + ;; content in a narrowed buffer, so make sure to insert + ;; before narrowing. Furthermore, call before and after + ;; change functions manually, otherwise cc-mode's cache can + ;; get messed up. (goto-char begin) + (run-hook-with-args 'before-change-functions begin begin) (insert content) (setq end (+ end (length content))) (narrow-to-region begin end) (goto-char (point-min)) - (yas--snippet-parse-create snippet)) + (yas--snippet-parse-create snippet) + (run-hook-with-args 'after-change-functions (point-min) (point-max) 0)) (when (listp buffer-undo-list) (push (cons (point-min) (point-max)) buffer-undo-list)) @@ -4505,8 +4554,7 @@ Lisp expression." current-string '(?`)))))) (goto-char (match-beginning 0)) (when transformed - (let ((marker (make-marker)) - (before-change-functions (cdr before-change-functions))) + (let ((marker (make-marker))) (yas--save-restriction-and-widen (insert "Y") ;; quite horrendous, I love it :) (set-marker marker (point)) @@ -4905,6 +4953,12 @@ object satisfying `yas--field-p' to restrict the expansion to."))) (help-xref-button 1 'help-snippet-def template) (delete-region (match-end 1) (match-end 0)) (delete-region (match-beginning 0) (match-beginning 1))))))) + +;;; Eldoc configuration. +(eldoc-add-command 'yas-next-field-or-maybe-expand + 'yas-next-field 'yas-prev-field + 'yas-expand 'yas-expand-from-keymap + 'yas-expand-from-trigger-key) ;;; Utils -- cgit v1.3