-;;; yasnippet.el --- Yet another snippet extension for Emacs.\r
-\r
-;; Copyright (C) 2008-2012 Free Software Foundation, Inc.\r
-;; Authors: pluskid <pluskid@gmail.com>, João Távora <joaotavora@gmail.com>\r
-;; Version: 0.8.0\r
-;; Package-version: 0.8.0\r
-;; X-URL: http://github.com/capitaomorte/yasnippet\r
-;; Keywords: convenience, emulation\r
-;; URL: http://github.com/capitaomorte/yasnippet\r
-;; EmacsWiki: YaSnippetMode\r
-\r
-;; This program is free software: you can redistribute it and/or modify\r
-;; it under the terms of the GNU General Public License as published by\r
-;; the Free Software Foundation, either version 3 of the License, or\r
-;; (at your option) any later version.\r
-\r
-;; This program is distributed in the hope that it will be useful,\r
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-;; GNU General Public License for more details.\r
-\r
-;; You should have received a copy of the GNU General Public License\r
-;; along with this program. If not, see <http://www.gnu.org/licenses/>.\r
-\r
-;;; Commentary:\r
-;;\r
-;; Basic steps to setup:\r
-;;\r
-;; (add-to-list 'load-path\r
-;; "~/path-to-yasnippet")\r
-;; (require 'yasnippet)\r
-;; (yas-global-mode 1)\r
-;;\r
-;;\r
-;; Interesting variables are:\r
-;;\r
-;; `yas-snippet-dirs'\r
-;;\r
-;; The directory where user-created snippets are to be\r
-;; stored. Can also be a list of directories. In that case,\r
-;; when used for bulk (re)loading of snippets (at startup or\r
-;; via `yas-reload-all'), directories appearing earlier in\r
-;; the list shadow other dir's snippets. Also, the first\r
-;; directory is taken as the default for storing the user's\r
-;; new snippets.\r
-;;\r
-;; The deprecated `yas/root-directory' aliases this variable\r
-;; for backward-compatibility.\r
-;;\r
-;; `yas-extra-modes'\r
-;;\r
-;; A local variable that you can set in a hook to override\r
-;; snippet-lookup based on major mode. It is a a symbol (or\r
-;; list of symbols) that correspond to subdirectories of\r
-;; `yas-snippet-dirs' and is used for deciding which\r
-;; snippets to consider for the active buffer.\r
-;;\r
-;; Deprecated `yas/mode-symbol' aliases this variable for\r
-;; backward-compatibility.\r
-;;\r
-;; Major commands are:\r
-;;\r
-;; M-x yas-expand\r
-;;\r
-;; Try to expand snippets before point. In `yas-minor-mode',\r
-;; this is bound to `yas-trigger-key' which you can customize.\r
-;;\r
-;; M-x yas-load-directory\r
-;;\r
-;; Prompts you for a directory hierarchy of snippets to load.\r
-;;\r
-;; M-x yas-insert-snippet\r
-;;\r
-;; Prompts you for possible snippet expansion if that is\r
-;; possible according to buffer-local and snippet-local\r
-;; expansion conditions. With prefix argument, ignore these\r
-;; conditions.\r
-;;\r
-;; M-x yas-visit-snippet-file\r
-;;\r
-;; Prompts you for possible snippet expansions like\r
-;; `yas-insert-snippet', but instead of expanding it, takes\r
-;; you directly to the snippet definition's file, if it\r
-;; exists.\r
-;;\r
-;; M-x yas-new-snippet\r
-;;\r
-;; Lets you create a new snippet file in the correct\r
-;; subdirectory of `yas-snippet-dirs', according to the\r
-;; active major mode.\r
-;;\r
-;; M-x yas-load-snippet-buffer\r
-;;\r
-;; When editing a snippet, this loads the snippet. This is\r
-;; bound to "C-c C-c" while in the `snippet-mode' editing\r
-;; mode.\r
-;;\r
-;; M-x yas-tryout-snippet\r
-;;\r
-;; When editing a snippet, this opens a new empty buffer,\r
-;; sets it to the appropriate major mode and inserts the\r
-;; snippet there, so you can see what it looks like. This is\r
-;; bound to "C-c C-t" while in `snippet-mode'.\r
-;;\r
-;; M-x yas-describe-tables\r
-;;\r
-;; Lists known snippets in a separate buffer. User is\r
-;; prompted as to whether only the currently active tables\r
-;; are to be displayed, or all the tables for all major\r
-;; modes.\r
-;;\r
-;; The `dropdown-list.el' extension is bundled with YASnippet, you\r
-;; can optionally use it the preferred "prompting method", puting in\r
-;; your .emacs file, for example:\r
-;;\r
-;; (require 'dropdown-list)\r
-;; (setq yas-prompt-functions '(yas-dropdown-prompt\r
-;; yas-ido-prompt\r
-;; yas-completing-prompt))\r
-;;\r
-;; Also check out the customization group\r
-;;\r
-;; M-x customize-group RET yasnippet RET\r
-;;\r
-;; If you use the customization group to set variables\r
-;; `yas-snippet-dirs' or `yas-global-mode', make sure the path to\r
-;; "yasnippet.el" is present in the `load-path' *before* the\r
-;; `custom-set-variables' is executed in your .emacs file.\r
-;;\r
-;; For more information and detailed usage, refer to the project page:\r
-;; http://github.com/capitaomorte/yasnippet\r
-\r
-;;; Code:\r
-\r
-(require 'cl)\r
-(require 'easymenu)\r
-(require 'help-mode)\r
-\r
-\f\r
-;;; User customizable variables\r
-\r
-(defgroup yasnippet nil\r
- "Yet Another Snippet extension"\r
- :group 'editing)\r
-\r
-(defvar yas--load-file-name load-file-name\r
- "Store the filename that yasnippet.el was originally loaded from.")\r
-\r
-(defcustom yas-snippet-dirs (remove nil\r
- (list "~/.emacs.d/snippets"\r
- (when yas--load-file-name\r
- (concat (file-name-directory yas--load-file-name) "snippets"))))\r
- "Directory or list of snippet dirs for each major mode.\r
-\r
-The directory where user-created snippets are to be stored. Can\r
-also be a list of directories. In that case, when used for\r
-bulk (re)loading of snippets (at startup or via\r
-`yas-reload-all'), directories appearing earlier in the list\r
-shadow other dir's snippets. Also, the first directory is taken\r
-as the default for storing the user's new snippets."\r
- :type '(choice (string :tag "Single directory (string)")\r
- (repeat :args (string) :tag "List of directories (strings)"))\r
- :group 'yasnippet\r
- :require 'yasnippet\r
- :set #'(lambda (symbol new)\r
- (let ((old (and (boundp symbol)\r
- (symbol-value symbol))))\r
- (set-default symbol new)\r
- (unless (or (not (fboundp 'yas-reload-all))\r
- (equal old new))\r
- (yas-reload-all)))))\r
-\r
-(defun yas-snippet-dirs ()\r
- "Returns `yas-snippet-dirs' (which see) as a list."\r
- (if (listp yas-snippet-dirs) yas-snippet-dirs (list yas-snippet-dirs)))\r
-\r
-(defvaralias 'yas/root-directory 'yas-snippet-dirs)\r
-\r
-(defcustom yas-prompt-functions '(yas-x-prompt\r
- yas-dropdown-prompt\r
- yas-completing-prompt\r
- yas-ido-prompt\r
- yas-no-prompt)\r
- "Functions to prompt for keys, templates, etc interactively.\r
-\r
-These functions are called with the following arguments:\r
-\r
-- PROMPT: A string to prompt the user\r
-\r
-- CHOICES: a list of strings or objects.\r
-\r
-- optional DISPLAY-FN : A function that, when applied to each of\r
-the objects in CHOICES will return a string.\r
-\r
-The return value of any function you put here should be one of\r
-the objects in CHOICES, properly formatted with DISPLAY-FN (if\r
-that is passed).\r
-\r
-- To signal that your particular style of prompting is\r
-unavailable at the moment, you can also have the function return\r
-nil.\r
-\r
-- To signal that the user quit the prompting process, you can\r
-signal `quit' with\r
-\r
- (signal 'quit \"user quit!\")."\r
- :type '(repeat function)\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-indent-line 'auto\r
- "Controls indenting applied to a recent snippet expansion.\r
-\r
-The following values are possible:\r
-\r
-- `fixed' Indent the snippet to the current column;\r
-\r
-- `auto' Indent each line of the snippet with `indent-according-to-mode'\r
-\r
-Every other value means don't apply any snippet-side indendation\r
-after expansion (the manual per-line \"$>\" indentation still\r
-applies)."\r
- :type '(choice (const :tag "Nothing" nothing)\r
- (const :tag "Fixed" fixed)\r
- (const :tag "Auto" auto))\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-also-auto-indent-first-line nil\r
- "Non-nil means also auto indent first line according to mode.\r
-\r
-Naturally this is only valid when `yas-indent-line' is `auto'"\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-snippet-revival t\r
- "Non-nil means re-activate snippet fields after undo/redo."\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-trigger-key "<tab>"\r
- "The key bound to `yas-expand' when `yas-minor-mode' is active.\r
-\r
-Value is a string that is converted to the internal Emacs key\r
-representation using `read-kbd-macro'."\r
- :type 'string\r
- :group 'yasnippet\r
- :set #'(lambda (symbol key)\r
- (let ((old (and (boundp symbol)\r
- (symbol-value symbol))))\r
- (set-default symbol key)\r
- ;; On very first loading of this defcustom,\r
- ;; `yas-trigger-key' is *not* loaded.\r
- (if (fboundp 'yas--trigger-key-reload)\r
- (yas--trigger-key-reload old)))))\r
-\r
-(defcustom yas-next-field-key '("TAB" "<tab>")\r
- "The key to navigate to next field when a snippet is active.\r
-\r
-Value is a string that is converted to the internal Emacs key\r
-representation using `read-kbd-macro'.\r
-\r
-Can also be a list of strings."\r
- :type '(choice (string :tag "String")\r
- (repeat :args (string) :tag "List of strings"))\r
- :group 'yasnippet\r
- :set #'(lambda (symbol val)\r
- (set-default symbol val)\r
- (if (fboundp 'yas--init-yas-in-snippet-keymap)\r
- (yas--init-yas-in-snippet-keymap))))\r
-\r
-\r
-(defcustom yas-prev-field-key '("<backtab>" "<S-tab>")\r
- "The key to navigate to previous field when a snippet is active.\r
-\r
-Value is a string that is converted to the internal Emacs key\r
-representation using `read-kbd-macro'.\r
-\r
-Can also be a list of strings."\r
- :type '(choice (string :tag "String")\r
- (repeat :args (string) :tag "List of strings"))\r
- :group 'yasnippet\r
- :set #'(lambda (symbol val)\r
- (set-default symbol val)\r
- (if (fboundp 'yas--init-yas-in-snippet-keymap)\r
- (yas--init-yas-in-snippet-keymap))))\r
-\r
-(defcustom yas-skip-and-clear-key '("C-d" "<delete>" "<deletechar>")\r
- "The key to clear the currently active field.\r
-\r
-Value is a string that is converted to the internal Emacs key\r
-representation using `read-kbd-macro'.\r
-\r
-Can also be a list of strings."\r
- :type '(choice (string :tag "String")\r
- (repeat :args (string) :tag "List of strings"))\r
- :group 'yasnippet\r
- :set #'(lambda (symbol val)\r
- (set-default symbol val)\r
- (if (fboundp 'yas--init-yas-in-snippet-keymap)\r
- (yas--init-yas-in-snippet-keymap))))\r
-\r
-(defcustom yas-triggers-in-field nil\r
- "If non-nil, `yas-next-field-key' can trigger stacked expansions.\r
-\r
-Otherwise, `yas-next-field-key' just tries to move on to the next\r
-field"\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-fallback-behavior 'call-other-command\r
- "How to act when `yas-trigger-key' does *not* expand a snippet.\r
-\r
-- `call-other-command' means try to temporarily disable YASnippet\r
- and call the next command bound to `yas-trigger-key'.\r
-\r
-- nil or the symbol `return-nil' mean do nothing. (and\r
- `yas-expand' returns nil)\r
-\r
-- A lisp form (apply COMMAND . ARGS) means interactively call\r
- COMMAND, if ARGS is non-nil, call COMMAND non-interactively\r
- with ARGS as arguments."\r
- :type '(choice (const :tag "Call previous command" call-other-command)\r
- (const :tag "Do nothing" return-nil))\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-choose-keys-first nil\r
- "If non-nil, prompt for snippet key first, then for template.\r
-\r
-Otherwise prompts for all possible snippet names.\r
-\r
-This affects `yas-insert-snippet' and `yas-visit-snippet-file'."\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-choose-tables-first nil\r
- "If non-nil, and multiple eligible snippet tables, prompts user for tables first.\r
-\r
-Otherwise, user chooses between the merging together of all\r
-eligible tables.\r
-\r
-This affects `yas-insert-snippet', `yas-visit-snippet-file'"\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-use-menu 'abbreviate\r
- "Display a YASnippet menu in the menu bar.\r
-\r
-When non-nil, submenus for each snippet table will be listed\r
-under the menu \"Yasnippet\".\r
-\r
-- If set to `abbreviate', only the current major-mode\r
-menu and the modes set in `yas-extra-modes' are listed.\r
-\r
-- If set to `full', every submenu is listed\r
-\r
-- It set to nil, don't display a menu at all (this requires a\r
- `yas-reload-all' call if the menu is already visible).\r
-\r
-Any other non-nil value, every submenu is listed."\r
- :type '(choice (const :tag "Full" full)\r
- (const :tag "Abbreviate" abbreviate)\r
- (const :tag "No menu" nil))\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-trigger-symbol (if (eq window-system 'mac)\r
- (char-to-string ?\x21E5) ;; little ->| sign\r
- " =>")\r
- "The text that will be used in menu to represent the trigger."\r
- :type 'string\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-wrap-around-region nil\r
- "If non-nil, snippet expansion wraps around selected region.\r
-\r
-The wrapping occurs just before the snippet's exit marker. This\r
-can be overriden on a per-snippet basis."\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-good-grace t\r
- "If non-nil, don't raise errors in inline elisp evaluation.\r
-\r
-An error string \"[yas] error\" is returned instead."\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-visit-from-menu nil\r
- "If non-nil visit snippets's files from menu, instead of expanding them.\r
-\r
-This cafn only work when snippets are loaded from files."\r
- :type 'boolean\r
- :group 'yasnippet)\r
-\r
-(defcustom yas-expand-only-for-last-commands nil\r
- "List of `last-command' values to restrict tab-triggering to, or nil.\r
-\r
-Leave this set at nil (the default) to be able to trigger an\r
-expansion simply by placing the cursor after a valid tab trigger,\r
-using whichever commands.\r
-\r
-Optionallly, set this to something like '(self-insert-command) if\r
-you to wish restrict expansion to only happen when the last\r
-letter of the snippet tab trigger was typed immediately before\r
-the trigger key itself."\r
- :type '(repeat function)\r
- :group 'yasnippet)\r
-\r
-;; Only two faces, and one of them shouldn't even be used...\r
-;;\r
-(defface yas-field-highlight-face\r
- '((t (:inherit 'region)))\r
- "The face used to highlight the currently active field of a snippet"\r
- :group 'yasnippet)\r
-\r
-(defface yas--field-debug-face\r
- '()\r
- "The face used for debugging some overlays normally hidden"\r
- :group 'yasnippet)\r
-\r
-\f\r
-;;; User can also customize the next defvars\r
-\r
-(defun yas--define-some-keys (keys keymap definition)\r
- "Bind KEYS to DEFINITION in KEYMAP, read with `read-kbd-macro'."\r
- (let ((keys (or (and (listp keys) keys)\r
- (list keys))))\r
- (dolist (key keys)\r
- (define-key keymap (read-kbd-macro key) definition))))\r
-\r
-(defun yas--init-yas-in-snippet-keymap ()\r
- (setq yas-keymap\r
- (let ((map (make-sparse-keymap)))\r
- (mapc #'(lambda (binding)\r
- (yas--define-some-keys (car binding) map (cdr binding)))\r
- `((,yas-next-field-key . yas-next-field-or-maybe-expand)\r
- (,yas-prev-field-key . yas-prev-field)\r
- ("C-g" . yas-abort-snippet)\r
- (,yas-skip-and-clear-key . yas-skip-and-clear-or-delete-char)))\r
- map)))\r
-\r
-(defvar yas-keymap (yas--init-yas-in-snippet-keymap)\r
- "The keymap active while a snippet expansion is in progress.")\r
-\r
-(defvar yas-key-syntaxes (list "w" "w_" "w_." "w_.()" "^ ")\r
- "List of character syntaxes used to find a trigger key before point.\r
-The list is tried in the order while scanning characters\r
-backwards from point. For example, if the list is '(\"w\" \"w_\")\r
-first look for trigger keys which are composed exclusively of\r
-\"word\"-syntax characters, and then, if that fails, look for\r
-keys which are either of \"word\" or \"symbol\"\r
-syntax. Triggering after\r
-\r
-foo-bar\r
-\r
-will, according to the \"w\" element first try \"bar\". If that\r
-isn't a trigger key, \"foo-bar\" is tried, respecting a second\r
-\"w_\" element.")\r
-\r
-(defvar yas-after-exit-snippet-hook\r
- '()\r
- "Hooks to run after a snippet exited.\r
-\r
-The hooks will be run in an environment where some variables bound to\r
-proper values:\r
-\r
-`yas-snippet-beg' : The beginning of the region of the snippet.\r
-\r
-`yas-snippet-end' : Similar to beg.\r
-\r
-Attention: These hooks are not run when exiting nested/stackd snippet expansion!")\r
-\r
-(defvar yas-before-expand-snippet-hook\r
- '()\r
- "Hooks to run just before expanding a snippet.")\r
-\r
-(defvar yas-buffer-local-condition\r
- '(if (and (or (fourth (syntax-ppss))\r
- (fifth (syntax-ppss)))\r
- (eq (symbol-function this-command) 'yas-expand-from-trigger-key))\r
- '(require-snippet-condition . force-in-comment)\r
- t)\r
- "Snippet expanding condition.\r
-\r
-This variable is a lisp form which is evaluated everytime a\r
-snippet expansion is attemped:\r
-\r
- * If it evaluates to nil, no snippets can be expanded.\r
-\r
- * If it evaluates to the a cons (require-snippet-condition\r
- . REQUIREMENT)\r
-\r
- * Snippets bearing no \"# condition:\" directive are not\r
- considered\r
-\r
- * Snippets bearing conditions that evaluate to nil (or\r
- produce an error) won't be onsidered.\r
-\r
- * If the snippet has a condition that evaluates to non-nil\r
- RESULT:\r
-\r
- * If REQUIREMENT is t, the snippet is considered\r
-\r
- * If REQUIREMENT is `eq' RESULT, the snippet is\r
- considered\r
-\r
- * Otherwise, the snippet is not considered.\r
-\r
- * If it evaluates to the symbol 'always, all snippets are\r
- considered for expansion, regardless of any conditions.\r
-\r
- * If it evaluates to t or some other non-nil value\r
-\r
- * Snippet bearing no conditions, or conditions that\r
- evaluate to non-nil, are considered for expansion.\r
-\r
- * Otherwise, the snippet is not considered.\r
-\r
-Here's an example preventing snippets from being expanded from\r
-inside comments, in `python-mode' only, with the exception of\r
-snippets returning the symbol 'force-in-comment in their\r
-conditions.\r
-\r
- (add-hook 'python-mode-hook\r
- '(lambda ()\r
- (setq yas-buffer-local-condition\r
- '(if (python-in-string/comment)\r
- '(require-snippet-condition . force-in-comment)\r
- t))))\r
-\r
-The default value is similar, it filters out potential snippet\r
-expansions inside comments and string literals, unless the\r
-snippet itself contains a condition that returns the symbol\r
-`force-in-comment'.")\r
-\r
-\f\r
-;;; Internal variables\r
-\r
-(defvar yas--version "0.8.0 (beta)")\r
-\r
-(defvar yas--menu-table (make-hash-table)\r
- "A hash table of MAJOR-MODE symbols to menu keymaps.")\r
-\r
-(defvar yas--known-modes\r
- '(ruby-mode rst-mode markdown-mode)\r
- "A list of mode which is well known but not part of emacs.")\r
-\r
-(defvar yas--escaped-characters\r
- '(?\\ ?` ?\" ?' ?$ ?} ?{ ?\( ?\))\r
- "List of characters which *might* need to be escaped.")\r
-\r
-(defconst yas--field-regexp\r
- "${\\([0-9]+:\\)?\\([^}]*\\)}"\r
- "A regexp to *almost* recognize a field.")\r
-\r
-(defconst yas--multi-dollar-lisp-expression-regexp\r
- "$+[ \t\n]*\\(([^)]*)\\)"\r
- "A regexp to *almost* recognize a \"$(...)\" expression.")\r
-\r
-(defconst yas--backquote-lisp-expression-regexp\r
- "`\\([^`]*\\)`"\r
- "A regexp to recognize a \"`lisp-expression`\" expression." )\r
-\r
-(defconst yas--transform-mirror-regexp\r
- "${\\(?:\\([0-9]+\\):\\)?$\\([ \t\n]*([^}]*\\)"\r
- "A regexp to *almost* recognize a mirror with a transform.")\r
-\r
-(defconst yas--simple-mirror-regexp\r
- "$\\([0-9]+\\)"\r
- "A regexp to recognize a simple mirror.")\r
-\r
-(defvar yas--snippet-id-seed 0\r
- "Contains the next id for a snippet.")\r
-\r
-(defun yas--snippet-next-id ()\r
- (let ((id yas--snippet-id-seed))\r
- (incf yas--snippet-id-seed)\r
- id))\r
-\r
-\f\r
-;;; Minor mode stuff\r
-\r
-;; XXX: `last-buffer-undo-list' is somehow needed in Carbon Emacs for MacOSX\r
-(defvar last-buffer-undo-list nil)\r
-\r
-(defvar yas--minor-mode-menu nil\r
- "Holds the YASnippet menu")\r
-\r
-(defun yas--init-minor-keymap ()\r
- (let ((map (make-sparse-keymap)))\r
- (when yas-use-menu\r
- (easy-menu-define yas--minor-mode-menu\r
- map\r
- "Menu used when `yas-minor-mode' is active."\r
- '("YASnippet"\r
- "----"\r
- ["Expand trigger" yas-expand\r
- :help "Possibly expand tab trigger before point"]\r
- ["Insert at point..." yas-insert-snippet\r
- :help "Prompt for an expandable snippet and expand it at point"]\r
- ["New snippet..." yas-new-snippet\r
- :help "Create a new snippet in an appropriate directory"]\r
- ["Visit snippet file..." yas-visit-snippet-file\r
- :help "Prompt for an expandable snippet and find its file"]\r
- "----"\r
- ("Snippet menu behaviour"\r
- ["Visit snippets" (setq yas-visit-from-menu t)\r
- :help "Visit snippets from the menu"\r
- :active t :style radio :selected yas-visit-from-menu]\r
- ["Expand snippets" (setq yas-visit-from-menu nil)\r
- :help "Expand snippets from the menu"\r
- :active t :style radio :selected (not yas-visit-from-menu)]\r
- "----"\r
- ["Show all known modes" (setq yas-use-menu 'full)\r
- :help "Show one snippet submenu for each loaded table"\r
- :active t :style radio :selected (eq yas-use-menu 'full)]\r
- ["Abbreviate according to current mode" (setq yas-use-menu 'abbreviate)\r
- :help "Show only snippet submenus for the current active modes"\r
- :active t :style radio :selected (eq yas-use-menu 'abbreviate)])\r
- ("Indenting"\r
- ["Auto" (setq yas-indent-line 'auto)\r
- :help "Indent each line of the snippet with `indent-according-to-mode'"\r
- :active t :style radio :selected (eq yas-indent-line 'auto)]\r
- ["Fixed" (setq yas-indent-line 'fixed)\r
- :help "Indent the snippet to the current column"\r
- :active t :style radio :selected (eq yas-indent-line 'fixed)]\r
- ["None" (setq yas-indent-line 'none)\r
- :help "Don't apply any particular snippet indentation after expansion"\r
- :active t :style radio :selected (not (member yas-indent-line '(fixed auto)))]\r
- "----"\r
- ["Also auto indent first line" (setq yas-also-auto-indent-first-line\r
- (not yas-also-auto-indent-first-line))\r
- :help "When auto-indenting also, auto indent the first line menu"\r
- :active (eq yas-indent-line 'auto)\r
- :style toggle :selected yas-also-auto-indent-first-line]\r
- )\r
- ("Prompting method"\r
- ["System X-widget" (setq yas-prompt-functions\r
- (cons 'yas-x-prompt\r
- (remove 'yas-x-prompt\r
- yas-prompt-functions)))\r
- :help "Use your windowing system's (gtk, mac, windows, etc...) default menu"\r
- :active t :style radio :selected (eq (car yas-prompt-functions)\r
- 'yas-x-prompt)]\r
- ["Dropdown-list" (setq yas-prompt-functions\r
- (cons 'yas-dropdown-prompt\r
- (remove 'yas-dropdown-prompt\r
- yas-prompt-functions)))\r
- :help "Use a special dropdown list"\r
- :active t :style radio :selected (eq (car yas-prompt-functions)\r
- 'yas-dropdown-prompt)]\r
- ["Ido" (setq yas-prompt-functions\r
- (cons 'yas-ido-prompt\r
- (remove 'yas-ido-prompt\r
- yas-prompt-functions)))\r
- :help "Use an ido-style minibuffer prompt"\r
- :active t :style radio :selected (eq (car yas-prompt-functions)\r
- 'yas-ido-prompt)]\r
- ["Completing read" (setq yas-prompt-functions\r
- (cons 'yas-completing-prompt\r
- (remove 'yas-completing-prompt\r
- yas-prompt-functions)))\r
- :help "Use a normal minibuffer prompt"\r
- :active t :style radio :selected (eq (car yas-prompt-functions)\r
- 'yas-completing-prompt)]\r
- )\r
- ("Misc"\r
- ["Wrap region in exit marker"\r
- (setq yas-wrap-around-region\r
- (not yas-wrap-around-region))\r
- :help "If non-nil automatically wrap the selected text in the $0 snippet exit"\r
- :style toggle :selected yas-wrap-around-region]\r
- ["Allow stacked expansions "\r
- (setq yas-triggers-in-field\r
- (not yas-triggers-in-field))\r
- :help "If non-nil allow snippets to be triggered inside other snippet fields"\r
- :style toggle :selected yas-triggers-in-field]\r
- ["Revive snippets on undo "\r
- (setq yas-snippet-revival\r
- (not yas-snippet-revival))\r
- :help "If non-nil allow snippets to become active again after undo"\r
- :style toggle :selected yas-snippet-revival]\r
- ["Good grace "\r
- (setq yas-good-grace\r
- (not yas-good-grace))\r
- :help "If non-nil don't raise errors in bad embedded eslip in snippets"\r
- :style toggle :selected yas-good-grace]\r
- )\r
- "----"\r
- ["Load snippets..." yas-load-directory\r
- :help "Load snippets from a specific directory"]\r
- ["Reload everything" yas-reload-all\r
- :help "Cleanup stuff, reload snippets, rebuild menus"]\r
- ["About" yas-about\r
- :help "Display some information about YASsnippet"])))\r
-\r
- ;; Now for the stuff that has direct keybindings\r
- ;;\r
- (define-key map "\C-c&\C-s" 'yas-insert-snippet)\r
- (define-key map "\C-c&\C-n" 'yas-new-snippet)\r
- (define-key map "\C-c&\C-v" 'yas-visit-snippet-file)\r
- map))\r
-\r
-(defvar yas-minor-mode-map (yas--init-minor-keymap)\r
- "The keymap used when `yas-minor-mode' is active.")\r
-\r
-(defun yas--trigger-key-reload (&optional unbind-key)\r
- "Rebind `yas-expand' to the new value of `yas-trigger-key'.\r
-\r
-With optional UNBIND-KEY, try to unbind that key from\r
-`yas-minor-mode-map'."\r
- (when (and unbind-key\r
- (stringp unbind-key)\r
- (not (string= unbind-key "")))\r
- (define-key yas-minor-mode-map (read-kbd-macro unbind-key) nil))\r
- (when (and yas-trigger-key\r
- (stringp yas-trigger-key)\r
- (not (string= yas-trigger-key "")))\r
- (define-key yas-minor-mode-map (read-kbd-macro yas-trigger-key) 'yas-expand)))\r
-\r
-(defvar yas--tables (make-hash-table)\r
- "A hash table of mode symbols to `yas--table' objects.")\r
-\r
-(defvar yas--parents (make-hash-table)\r
- "A hash table of mode symbols do lists of direct parent mode symbols.\r
-\r
-This list is populated when reading the \".yas-parents\" files\r
-found when traversing snippet directories with\r
-`yas-load-directory'.\r
-\r
-There might be additionalal parenting information stored in the\r
-`derived-mode-parent' property of some mode symbols, but that is\r
-not recorded here.")\r
-\r
-(defvar yas--direct-keymaps (list)\r
- "Keymap alist supporting direct snippet keybindings.\r
-\r
-This variable is is placed in `emulation-mode-map-alists'.\r
-\r
-Its elements looks like (TABLE-NAME . KEYMAP). They're\r
-instantiated on `yas-reload-all' but KEYMAP is added to only when\r
-loading snippets. `yas--direct-TABLE-NAME' is then a variable set\r
-buffer-locally when entering `yas-minor-mode'. KEYMAP binds all\r
-defined direct keybindings to the command\r
-`yas-expand-from-keymap' which then which snippet to expand.")\r
-\r
-(defun yas-direct-keymaps-reload ()\r
- "Force reload the direct keybinding for active snippet tables."\r
- (interactive)\r
- (setq yas--direct-keymaps nil)\r
- (maphash #'(lambda (name table)\r
- (push (cons (intern (format "yas--direct-%s" name))\r
- (yas--table-direct-keymap table))\r
- yas--direct-keymaps))\r
- yas--tables))\r
-\r
-(defun yas--modes-to-activate ()\r
- "Compute list of mode symbols that are active for `yas-expand'\r
-and friends."\r
- (let ((modes-to-activate (list major-mode))\r
- (mode major-mode))\r
- (while (setq mode (get mode 'derived-mode-parent))\r
- (push mode modes-to-activate))\r
- (dolist (mode (yas-extra-modes))\r
- (push mode modes-to-activate))\r
- (remove-duplicates\r
- (append modes-to-activate\r
- (mapcan #'(lambda (mode)\r
- (yas--all-parents mode))\r
- modes-to-activate)))))\r
-\r
-(defvar yas-minor-mode-hook nil\r
- "Hook run when yas-minor-mode is turned on")\r
-\r
-;;;###autoload\r
-(define-minor-mode yas-minor-mode\r
- "Toggle YASnippet mode.\r
-\r
-When YASnippet mode is enabled, the `yas-trigger-key' key expands\r
-snippets of code depending on the major mode.\r
-\r
-With no argument, this command toggles the mode.\r
-positive prefix argument turns on the mode.\r
-Negative prefix argument turns off the mode.\r
-\r
-You can customize the key through `yas-trigger-key'.\r
-\r
-Key bindings:\r
-\\{yas-minor-mode-map}"\r
- nil\r
- ;; The indicator for the mode line.\r
- " yas"\r
- :group 'yasnippet\r
- (cond (yas-minor-mode\r
- ;; Reload the trigger key\r
- ;;\r
- (yas--trigger-key-reload)\r
- ;; Install the direct keymaps in `emulation-mode-map-alists'\r
- ;; (we use `add-hook' even though it's not technically a hook,\r
- ;; but it works). Then define variables named after modes to\r
- ;; index `yas--direct-keymaps'.\r
- ;;\r
- ;; Also install the post-command-hook.\r
- ;;\r
- (add-hook 'emulation-mode-map-alists 'yas--direct-keymaps)\r
- (add-hook 'post-command-hook 'yas--post-command-handler nil t)\r
- ;; Set the `yas--direct-%s' vars for direct keymap expansion\r
- ;;\r
- (dolist (mode (yas--modes-to-activate))\r
- (let ((name (intern (format "yas--direct-%s" mode))))\r
- (set-default name nil)\r
- (set (make-local-variable name) t)))\r
- ;; Perform JIT loads\r
- ;;\r
- (yas--load-pending-jits))\r
- (t\r
- ;; Uninstall the direct keymaps and the post-command hook\r
- ;;\r
- (remove-hook 'post-command-hook 'yas--post-command-handler t)\r
- (remove-hook 'emulation-mode-map-alists 'yas--direct-keymaps))))\r
-\r
-(defvar yas--dont-activate '(minibufferp)\r
- "If non-nil don't let `yas-minor-mode-on' active yas for this buffer.\r
-\r
-If a function, then its result is used.\r
-\r
-If a list of functions, then all functions must return nil to\r
-activate yas for this buffer.\r
-\r
-`yas-minor-mode-on' is usually called by `yas-global-mode' so\r
-this effectively lets you define exceptions to the \"global\"\r
-behaviour. Can also be a function of zero arguments.")\r
-(make-variable-buffer-local 'yas--dont-activate)\r
-\r
-(defun yas-minor-mode-on ()\r
- "Turn on YASnippet minor mode.\r
-\r
-Do this unless `yas--dont-activate' is truish "\r
- (interactive)\r
- (unless (cond ((functionp yas--dont-activate)\r
- (funcall yas--dont-activate))\r
- ((consp yas--dont-activate)\r
- (some #'funcall yas--dont-activate))\r
- (yas--dont-activate))\r
- ;; Load all snippets definitions unless we still don't have a\r
- ;; root-directory or some snippets have already been loaded.\r
- ;;\r
- (yas-minor-mode 1)))\r
-\r
-;;;###autoload\r
-(define-globalized-minor-mode yas-global-mode yas-minor-mode yas-minor-mode-on\r
- :group 'yasnippet\r
- :require 'yasnippet)\r
-\r
-(defun yas--global-mode-reload-with-jit-maybe ()\r
- (when yas-global-mode (yas-reload-all)))\r
-\r
-(add-hook 'yas-global-mode-hook 'yas--global-mode-reload-with-jit-maybe)\r
-\r
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
-;; Major mode stuff\r
-;;\r
-(defvar yas--font-lock-keywords\r
- (append '(("^#.*$" . font-lock-comment-face))\r
- lisp-font-lock-keywords\r
- lisp-font-lock-keywords-1\r
- lisp-font-lock-keywords-2\r
- '(("$\\([0-9]+\\)"\r
- (0 font-lock-keyword-face)\r
- (1 font-lock-string-face t))\r
- ("${\\([0-9]+\\):?"\r
- (0 font-lock-keyword-face)\r
- (1 font-lock-warning-face t))\r
- ("${" font-lock-keyword-face)\r
- ("$[0-9]+?" font-lock-preprocessor-face)\r
- ("\\(\\$(\\)" 1 font-lock-preprocessor-face)\r
- ("}"\r
- (0 font-lock-keyword-face)))))\r
-\r
-(defun yas--init-major-keymap ()\r
- (let ((map (make-sparse-keymap)))\r
- (easy-menu-define nil\r
- map\r
- "Menu used when snippet-mode is active."\r
- (cons "Snippet"\r
- (mapcar #'(lambda (ent)\r
- (when (third ent)\r
- (define-key map (third ent) (second ent)))\r
- (vector (first ent) (second ent) t))\r
- (list\r
- (list "Load this snippet" 'yas-load-snippet-buffer "\C-c\C-c")\r
- (list "Try out this snippet" 'yas-tryout-snippet "\C-c\C-t")))))\r
- map))\r
-\r
-(defvar snippet-mode-map\r
- (yas--init-major-keymap)\r
- "The keymap used when `snippet-mode' is active")\r
-\r
-\r
-(define-derived-mode snippet-mode text-mode "Snippet"\r
- "A mode for editing yasnippets"\r
- (setq font-lock-defaults '(yas--font-lock-keywords))\r
- (set (make-local-variable 'require-final-newline) nil)\r
- (set (make-local-variable 'comment-start) "#")\r
- (set (make-local-variable 'comment-start-skip) "#+[\t ]*"))\r
-\r
-\r
-\f\r
-;;; Internal structs for template management\r
-\r
-(defstruct (yas--template (:constructor yas--make-blank-template))\r
- "A template for a snippet."\r
- key\r
- content\r
- name\r
- condition\r
- expand-env\r
- file\r
- keybinding\r
- uuid\r
- menu-binding-pair\r
- group ;; as dictated by the #group: directive or .yas-make-groups\r
- perm-group ;; as dictated by `yas-define-menu'\r
- table\r
- )\r
-\r
-(defun yas--populate-template (template &rest args)\r
- "Helper function to populate a template with properties"\r
- (let (p v)\r
- (while args\r
- (aset template\r
- (position (intern (substring (symbol-name (car args)) 1))\r
- (mapcar #'car (get 'yas--template 'cl-struct-slots)))\r
- (second args))\r
- (setq args (cddr args)))\r
- template))\r
-\r
-(defstruct (yas--table (:constructor yas--make-snippet-table (name)))\r
- "A table to store snippets for a particular mode.\r
-\r
-Has the following fields:\r
-\r
-`yas--table-name'\r
-\r
- A symbol name normally corresponding to a major mode, but can\r
- also be a pseudo major-mode to be referenced in\r
- `yas-extra-modes', for example.\r
-\r
-`yas--table-hash'\r
-\r
- A hash table (KEY . NAMEHASH), known as the \"keyhash\". KEY is\r
- a string or a vector, where the former is the snippet's trigger\r
- and the latter means it's a direct keybinding. NAMEHASH is yet\r
- another hash of (NAME . TEMPLATE) where NAME is the snippet's\r
- name and TEMPLATE is a `yas--template' object.\r
-\r
-`yas--table-direct-keymap'\r
-\r
- A keymap for the snippets in this table that have direct\r
- keybindings. This is kept in sync with the keyhash, i.e., all\r
- the elements of the keyhash that are vectors appear here as\r
- bindings to `yas-expand-from-keymap'.\r
-\r
-`yas--table-uuidhash'\r
-\r
- A hash table mapping snippets uuid's to the same `yas--template'\r
- objects. A snippet uuid defaults to the snippet's name.\r
-"\r
- name\r
- (hash (make-hash-table :test 'equal))\r
- (uuidhash (make-hash-table :test 'equal))\r
- (parents nil)\r
- (direct-keymap (make-sparse-keymap)))\r
-\r
-(defun yas--get-template-by-uuid (mode uuid)\r
- "Find the snippet template in MODE by its UUID."\r
- (let* ((table (gethash mode yas--tables mode)))\r
- (when table\r
- (gethash uuid (yas--table-uuidhash table)))))\r
-\r
-;; Apropos storing/updating in TABLE, this works in two steps:\r
-;;\r
-;; 1. `yas--remove-template-by-uuid' removes any\r
-;; keyhash-namehash-template mappings from TABLE, grabing the\r
-;; snippet by its uuid. Also removes mappings from TABLE's\r
-;; `yas--table-direct-keymap' (FIXME: and should probably take care\r
-;; of potentially stale menu bindings right?.)\r
-;;\r
-;; 2. `yas--add-template' adds this all over again.\r
-;;\r
-;; Create a new or add to an existing keyhash-namehash mapping.\r
-;;\r
-;; For reference on understanding this, consider three snippet\r
-;; definitions:\r
-;;\r
-;; A: # name: The Foo\r
-;; # key: foo\r
-;; # binding: C-c M-l\r
-;;\r
-;; B: # name: Mrs Foo\r
-;; # key: foo\r
-;;\r
-;; C: # name: The Bar\r
-;; # binding: C-c M-l\r
-;;\r
-;; D: # name: Baz\r
-;; # key: baz\r
-;;\r
-;; keyhash namehashes(3) yas--template structs(4)\r
-;; -----------------------------------------------------\r
-;; __________\r
-;; / \\r
-;; "foo" ---> "The Foo" ---> [yas--template A] |\r
-;; "Mrs Foo" ---> [yas--template B] |\r
-;; |\r
-;; [C-c M-l] ---> "The Foo" -------------------------/\r
-;; "The Bar" ---> [yas--template C]\r
-;;\r
-;; "baz" ---> "Baz" ---> [yas--template D]\r
-;;\r
-;; Additionally, since uuid defaults to the name, we have a\r
-;; `yas--table-uuidhash' for TABLE\r
-;;\r
-;; uuidhash yas--template structs\r
-;; -------------------------------\r
-;; "The Foo" ---> [yas--template A]\r
-;; "Mrs Foo" ---> [yas--template B]\r
-;; "The Bar" ---> [yas--template C]\r
-;; "Baz" ---> [yas--template D]\r
-;;\r
-;; FIXME: the more I look at this data-structure the more I think I'm\r
-;; stupid. There has to be an easier way (but beware lots of code\r
-;; depends on this).\r
-;;\r
-(defun yas--remove-template-by-uuid (table uuid)\r
- "Remove from TABLE a template identified by UUID."\r
- (let ((template (gethash uuid (yas--table-uuidhash table))))\r
- (when template\r
- (let* ((name (yas--template-name template))\r
- (empty-keys nil))\r
- ;; Remove the name from each of the targeted namehashes\r
- ;;\r
- (maphash #'(lambda (k v)\r
- (let ((template (gethash name v)))\r
- (when (and template\r
- (eq uuid (yas--template-uuid template)))\r
- (remhash name v)\r
- (when (zerop (hash-table-count v))\r
- (push k empty-keys)))))\r
- (yas--table-hash table))\r
- ;; Remove the namehash themselves if they've become empty\r
- ;;\r
- (dolist (key empty-keys)\r
- (when (vectorp key)\r
- (define-key (yas--table-direct-keymap table) key nil))\r
- (remhash key (yas--table-hash table)))\r
-\r
- ;; Finally, remove the uuid from the uuidhash\r
- ;;\r
- (remhash uuid (yas--table-uuidhash table))))))\r
-\r
-(defun yas--add-template (table template)\r
- "Store in TABLE the snippet template TEMPLATE.\r
-\r
-KEY can be a string (trigger key) of a vector (direct\r
-keybinding)."\r
- (let ((name (yas--template-name template))\r
- (key (yas--template-key template))\r
- (keybinding (yas--template-keybinding template))\r
- (menu-binding-pair (yas--template-menu-binding-pair-get-create template)))\r
- (dolist (k (remove nil (list key keybinding)))\r
- (puthash name\r
- template\r
- (or (gethash k\r
- (yas--table-hash table))\r
- (puthash k\r
- (make-hash-table :test 'equal)\r
- (yas--table-hash table))))\r
- (when (vectorp k)\r
- (define-key (yas--table-direct-keymap table) k 'yas-expand-from-keymap)))\r
-\r
- ;; Update TABLE's `yas--table-uuidhash'\r
- (puthash (yas--template-uuid template)\r
- template\r
- (yas--table-uuidhash table))))\r
-\r
-(defun yas--update-template (table template)\r
- "Add or update TEMPLATE in TABLE.\r
-\r
-Also takes care of adding and updating to the associated menu."\r
- ;; Remove from table by uuid\r
- ;;\r
- (yas--remove-template-by-uuid table (yas--template-uuid template))\r
- ;; Add to table again\r
- ;;\r
- (yas--add-template table template)\r
- ;; Take care of the menu\r
- ;;\r
- (when yas-use-menu\r
- (yas--update-template-menu table template)))\r
-\r
-(defun yas--update-template-menu (table template)\r
- "Update every menu-related for TEMPLATE"\r
- (let ((menu-binding-pair (yas--template-menu-binding-pair-get-create template))\r
- (key (yas--template-key template))\r
- (keybinding (yas--template-keybinding template)))\r
- ;; The snippet might have changed name or keys, so update\r
- ;; user-visible strings\r
- ;;\r
- (unless (eq (cdr menu-binding-pair) :none)\r
- ;; the menu item name\r
- ;;\r
- (setf (cadar menu-binding-pair) (yas--template-name template))\r
- ;; the :keys information (also visible to the user)\r
- (setf (getf (cdr (car menu-binding-pair)) :keys)\r
- (or (and keybinding (key-description keybinding))\r
- (and key (concat key yas-trigger-symbol))))))\r
- (unless (yas--template-menu-managed-by-yas-define-menu template)\r
- (let ((menu-keymap\r
- (yas--menu-keymap-get-create (yas--table-mode table)\r
- (mapcar #'yas--table-mode\r
- (yas--table-parents table))))\r
- (group (yas--template-group template)))\r
- ;; Remove from menu keymap\r
- ;;\r
- (assert menu-keymap)\r
- (yas--delete-from-keymap menu-keymap (yas--template-uuid template))\r
-\r
- ;; Add necessary subgroups as necessary.\r
- ;;\r
- (dolist (subgroup group)\r
- (let ((subgroup-keymap (lookup-key menu-keymap (vector (make-symbol subgroup)))))\r
- (unless (and subgroup-keymap\r
- (keymapp subgroup-keymap))\r
- (setq subgroup-keymap (make-sparse-keymap))\r
- (define-key menu-keymap (vector (make-symbol subgroup))\r
- `(menu-item ,subgroup ,subgroup-keymap)))\r
- (setq menu-keymap subgroup-keymap)))\r
-\r
- ;; Add this entry to the keymap\r
- ;;\r
- (define-key menu-keymap\r
- (vector (make-symbol (yas--template-uuid template)))\r
- (car (yas--template-menu-binding-pair template))))))\r
-\r
-(defun yas--namehash-templates-alist (namehash)\r
- (let (alist)\r
- (maphash #'(lambda (k v)\r
- (push (cons k v) alist))\r
- namehash)\r
- alist))\r
-\r
-(defun yas--fetch (table key)\r
- "Fetch templates in TABLE by KEY.\r
-\r
-Return a list of cons (NAME . TEMPLATE) where NAME is a\r
-string and TEMPLATE is a `yas--template' structure."\r
- (let* ((keyhash (yas--table-hash table))\r
- (namehash (and keyhash (gethash key keyhash))))\r
- (when namehash\r
- (yas--filter-templates-by-condition (yas--namehash-templates-alist namehash)))))\r
-\r
-\f\r
-;;; Filtering/condition logic\r
-\r
-(defun yas--eval-condition (condition)\r
- (condition-case err\r
- (save-excursion\r
- (save-restriction\r
- (save-match-data\r
- (eval condition))))\r
- (error (progn\r
- (yas--message 1 "Error in condition evaluation: %s" (error-message-string err))\r
- nil))))\r
-\r
-\r
-(defun yas--filter-templates-by-condition (templates)\r
- "Filter the templates using the applicable condition.\r
-\r
-TEMPLATES is a list of cons (NAME . TEMPLATE) where NAME is a\r
-string and TEMPLATE is a `yas--template' structure.\r
-\r
-This function implements the rules described in\r
-`yas-buffer-local-condition'. See that variables documentation."\r
- (let ((requirement (yas--require-template-specific-condition-p)))\r
- (if (eq requirement 'always)\r
- templates\r
- (remove-if-not #'(lambda (pair)\r
- (yas--template-can-expand-p\r
- (yas--template-condition (cdr pair)) requirement))\r
- templates))))\r
-\r
-(defun yas--require-template-specific-condition-p ()\r
- "Decides if this buffer requests/requires snippet-specific\r
-conditions to filter out potential expansions."\r
- (if (eq 'always yas-buffer-local-condition)\r
- 'always\r
- (let ((local-condition (or (and (consp yas-buffer-local-condition)\r
- (yas--eval-condition yas-buffer-local-condition))\r
- yas-buffer-local-condition)))\r
- (when local-condition\r
- (if (eq local-condition t)\r
- t\r
- (and (consp local-condition)\r
- (eq 'require-snippet-condition (car local-condition))\r
- (symbolp (cdr local-condition))\r
- (cdr local-condition)))))))\r
-\r
-(defun yas--template-can-expand-p (condition requirement)\r
- "Evaluates CONDITION and REQUIREMENT and returns a boolean"\r
- (let* ((result (or (null condition)\r
- (yas--eval-condition condition))))\r
- (cond ((eq requirement t)\r
- result)\r
- (t\r
- (eq requirement result)))))\r
-\r
-(defun yas--all-parents (mode)\r
- "Returns a list of all parent modes of MODE"\r
- (let ((parents (gethash mode yas--parents)))\r
- (append parents\r
- (mapcan #'yas--all-parents parents))))\r
-\r
-(defun yas--table-templates (table)\r
- (when table\r
- (let ((acc (list)))\r
- (maphash #'(lambda (key namehash)\r
- (maphash #'(lambda (name template)\r
- (push (cons name template) acc))\r
- namehash))\r
- (yas--table-hash table))\r
- (yas--filter-templates-by-condition acc))))\r
-\r
-(defun yas--current-key ()\r
- "Get the key under current position. A key is used to find\r
-the template of a snippet in the current snippet-table."\r
- (let ((start (point))\r
- (end (point))\r
- (syntaxes yas-key-syntaxes)\r
- syntax\r
- done\r
- templates)\r
- (while (and (not done) syntaxes)\r
- (setq syntax (car syntaxes))\r
- (setq syntaxes (cdr syntaxes))\r
- (save-excursion\r
- (skip-syntax-backward syntax)\r
- (setq start (point)))\r
- (setq templates\r
- (mapcan #'(lambda (table)\r
- (yas--fetch table (buffer-substring-no-properties start end)))\r
- (yas--get-snippet-tables)))\r
- (if templates\r
- (setq done t)\r
- (setq start end)))\r
- (list templates\r
- start\r
- end)))\r
-\r
-\r
-(defun yas--table-all-keys (table)\r
- (when table\r
- (let ((acc))\r
- (maphash #'(lambda (key namehash)\r
- (when (yas--filter-templates-by-condition (yas--namehash-templates-alist namehash))\r
- (push key acc)))\r
- (yas--table-hash table))\r
- acc)))\r
-\r
-(defun yas--table-mode (table)\r
- (intern (yas--table-name table)))\r
-\r
-\f\r
-;;; Internal functions:\r
-\r
-(defun yas--real-mode? (mode)\r
- "Try to find out if MODE is a real mode. The MODE bound to\r
-a function (like `c-mode') is considered real mode. Other well\r
-known mode like `ruby-mode' which is not part of Emacs might\r
-not bound to a function until it is loaded. So yasnippet keeps\r
-a list of modes like this to help the judgement."\r
- (or (fboundp mode)\r
- (find mode yas--known-modes)))\r
-\r
-(defun yas--eval-lisp (form)\r
- "Evaluate FORM and convert the result to string."\r
- (let ((retval (catch 'yas--exception\r
- (condition-case err\r
- (save-excursion\r
- (save-restriction\r
- (save-match-data\r
- (widen)\r
- (let ((result (eval form)))\r
- (when result\r
- (format "%s" result))))))\r
- (error (if yas-good-grace\r
- (yas--format "elisp error! %s" (error-message-string err))\r
- (error (yas--format "elisp error: %s"\r
- (error-message-string err)))))))))\r
- (when (and (consp retval)\r
- (eq 'yas--exception (car retval)))\r
- (error (cdr retval)))\r
- retval))\r
-\r
-(defun yas--eval-lisp-no-saves (form)\r
- (condition-case err\r
- (eval form)\r
- (error (if yas-good-grace\r
- (yas--format "elisp error! %s" (error-message-string err))\r
- (error (yas--format "elisp error: %s"\r
- (error-message-string err)))))))\r
-\r
-(defun yas--read-lisp (string &optional nil-on-error)\r
- "Read STRING as a elisp expression and return it.\r
-\r
-In case STRING in an invalid expression and NIL-ON-ERROR is nil,\r
-return an expression that when evaluated will issue an error."\r
- (condition-case err\r
- (read string)\r
- (error (and (not nil-on-error)\r
- `(error (error-message-string ,err))))))\r
-\r
-(defun yas--read-keybinding (keybinding)\r
- "Read KEYBINDING as a snippet keybinding, return a vector."\r
- (when (and keybinding\r
- (not (string-match "keybinding" keybinding)))\r
- (condition-case err\r
- (let ((res (or (and (string-match "^\\[.*\\]$" keybinding)\r
- (read keybinding))\r
- (read-kbd-macro keybinding 'need-vector))))\r
- res)\r
- (error\r
- (yas--message 3 "warning: keybinding \"%s\" invalid since %s."\r
- keybinding (error-message-string err))\r
- nil))))\r
-\r
-(defvar yas-extra-modes nil\r
- "If non-nil, also lookup snippets for this/these modes.\r
-\r
-Can be a symbol or a list of symbols.\r
-\r
-This variable probably makes more sense as buffer-local, so\r
-ensure your use `make-local-variable' when you set it.")\r
-(defun yas-extra-modes ()\r
- (if (listp yas-extra-modes) yas-extra-modes (list yas-extra-modes)))\r
-(defvaralias 'yas/mode-symbol 'yas-extra-modes)\r
-\r
-(defun yas--table-get-create (mode)\r
- "Get or create the snippet table corresponding to MODE."\r
- (let ((table (gethash mode\r
- yas--tables)))\r
- (unless table\r
- (setq table (yas--make-snippet-table (symbol-name mode)))\r
- (puthash mode table yas--tables)\r
- (push (cons (intern (format "yas--direct-%s" mode))\r
- (yas--table-direct-keymap table))\r
- yas--direct-keymaps))\r
- table))\r
-\r
-(defun yas--get-snippet-tables ()\r
- "Get snippet tables for current buffer.\r
-\r
-Return a list of `yas--table' objects. The list of modes to\r
-consider is returned by `yas--modes-to-activate'"\r
- (remove nil\r
- (mapcar #'(lambda (mode-name)\r
- (gethash mode-name yas--tables))\r
- (yas--modes-to-activate))))\r
-\r
-(defun yas--menu-keymap-get-create (mode &optional parents)\r
- "Get or create the menu keymap for MODE and its PARENTS.\r
-\r
-This may very well create a plethora of menu keymaps and arrange\r
-them all in `yas--menu-table'"\r
- (let* ((menu-keymap (or (gethash mode yas--menu-table)\r
- (puthash mode (make-sparse-keymap) yas--menu-table))))\r
- (mapc #'yas--menu-keymap-get-create parents)\r
- (define-key yas--minor-mode-menu (vector mode)\r
- `(menu-item ,(symbol-name mode) ,menu-keymap\r
- :visible (yas--show-menu-p ',mode)))\r
- menu-keymap))\r
-\r
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
-;;; Template-related and snippet loading functions\r
-\r
-(defun yas--parse-template (&optional file)\r
- "Parse the template in the current buffer.\r
-\r
-Optional FILE is the absolute file name of the file being\r
-parsed.\r
-\r
-Optional GROUP is the group where the template is to go,\r
-otherwise we attempt to calculate it from FILE.\r
-\r
-Return a snippet-definition, i.e. a list\r
-\r
- (KEY TEMPLATE NAME CONDITION GROUP VARS FILE KEYBINDING UUID)\r
-\r
-If the buffer contains a line of \"# --\" then the contents above\r
-this line are ignored. Directives can set most of these with the syntax:\r
-\r
-# directive-name : directive-value\r
-\r
-Here's a list of currently recognized directives:\r
-\r
- * type\r
- * name\r
- * contributor\r
- * condition\r
- * group\r
- * key\r
- * expand-env\r
- * binding\r
- * uuid"\r
- (goto-char (point-min))\r
- (let* ((type 'snippet)\r
- (name (and file\r
- (file-name-nondirectory file)))\r
- (key nil)\r
- template\r
- bound\r
- condition\r
- (group (and file\r
- (yas--calculate-group file)))\r
- expand-env\r
- binding\r
- uuid)\r
- (if (re-search-forward "^# --\n" nil t)\r
- (progn (setq template\r
- (buffer-substring-no-properties (point)\r
- (point-max)))\r
- (setq bound (point))\r
- (goto-char (point-min))\r
- (while (re-search-forward "^# *\\([^ ]+?\\) *: *\\(.*\\)$" bound t)\r
- (when (string= "uuid" (match-string-no-properties 1))\r
- (setq uuid (match-string-no-properties 2)))\r
- (when (string= "type" (match-string-no-properties 1))\r
- (setq type (if (string= "command" (match-string-no-properties 2))\r
- 'command\r
- 'snippet)))\r
- (when (string= "key" (match-string-no-properties 1))\r
- (setq key (match-string-no-properties 2)))\r
- (when (string= "name" (match-string-no-properties 1))\r
- (setq name (match-string-no-properties 2)))\r
- (when (string= "condition" (match-string-no-properties 1))\r
- (setq condition (yas--read-lisp (match-string-no-properties 2))))\r
- (when (string= "group" (match-string-no-properties 1))\r
- (setq group (match-string-no-properties 2)))\r
- (when (string= "expand-env" (match-string-no-properties 1))\r
- (setq expand-env (yas--read-lisp (match-string-no-properties 2)\r
- 'nil-on-error)))\r
- (when (string= "binding" (match-string-no-properties 1))\r
- (setq binding (match-string-no-properties 2)))))\r
- (setq template\r
- (buffer-substring-no-properties (point-min) (point-max))))\r
- (unless (or key binding)\r
- (setq key (and file (file-name-nondirectory file))))\r
- (when (eq type 'command)\r
- (setq template (yas--read-lisp (concat "(progn" template ")"))))\r
- (when group\r
- (setq group (split-string group "\\.")))\r
- (list key template name condition group expand-env file binding uuid)))\r
-\r
-(defun yas--calculate-group (file)\r
- "Calculate the group for snippet file path FILE."\r
- (let* ((dominating-dir (locate-dominating-file file\r
- ".yas-make-groups"))\r
- (extra-path (and dominating-dir\r
- (replace-regexp-in-string (concat "^"\r
- (expand-file-name dominating-dir))\r
- ""\r
- (expand-file-name file))))\r
- (extra-dir (and extra-path\r
- (file-name-directory extra-path)))\r
- (group (and extra-dir\r
- (replace-regexp-in-string "/"\r
- "."\r
- (directory-file-name extra-dir)))))\r
- group))\r
-\r
-(defun yas--subdirs (directory &optional filep)\r
- "Return subdirs or files of DIRECTORY according to FILEP."\r
- (remove-if (lambda (file)\r
- (or (string-match "^\\."\r
- (file-name-nondirectory file))\r
- (string-match "^#.*#$"\r
- (file-name-nondirectory file))\r
- (string-match "~$"\r
- (file-name-nondirectory file))\r
- (if filep\r
- (file-directory-p file)\r
- (not (file-directory-p file)))))\r
- (directory-files directory t)))\r
-\r
-(defun yas--make-menu-binding (template)\r
- (let ((mode (yas--table-mode (yas--template-table template))))\r
- `(lambda () (interactive) (yas--expand-or-visit-from-menu ',mode ,(yas--template-uuid template)))))\r
-\r
-(defun yas--expand-or-visit-from-menu (mode uuid)\r
- (let* ((table (yas--table-get-create mode))\r
- (yas--current-template (and table\r
- (gethash uuid (yas--table-uuidhash table)))))\r
- (when yas--current-template\r
- (if yas-visit-from-menu\r
- (yas--visit-snippet-file-1 yas--current-template)\r
- (let ((where (if (region-active-p)\r
- (cons (region-beginning) (region-end))\r
- (cons (point) (point)))))\r
- (yas-expand-snippet (yas--template-content yas--current-template)\r
- (car where)\r
- (cdr where)\r
- (yas--template-expand-env yas--current-template)))))))\r
-\r
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
-;; Popping up for keys and templates\r
-;;\r
-(defvar yas--x-pretty-prompt-templates nil\r
- "If non-nil, attempt to prompt for templates like TextMate.")\r
-\r
-\r
-(defun yas--prompt-for-template (templates &optional prompt)\r
- "Interactively choose a template from the list TEMPLATES.\r
-\r
-TEMPLATES is a list of `yas--template'."\r
- (when templates\r
- (setq templates\r
- (sort templates #'(lambda (t1 t2)\r
- (< (length (yas--template-name t1))\r
- (length (yas--template-name t2))))))\r
- (if yas--x-pretty-prompt-templates\r
- (yas--x-pretty-prompt-templates "Choose a snippet" templates)\r
- (some #'(lambda (fn)\r
- (funcall fn (or prompt "Choose a snippet: ")\r
- templates\r
- #'yas--template-name))\r
- yas-prompt-functions))))\r
-\r
-(defun yas--prompt-for-keys (keys &optional prompt)\r
- "Interactively choose a template key from the list KEYS."\r
- (when keys\r
- (some #'(lambda (fn)\r
- (funcall fn (or prompt "Choose a snippet key: ") keys))\r
- yas-prompt-functions)))\r
-\r
-(defun yas--prompt-for-table (tables &optional prompt)\r
- (when tables\r
- (some #'(lambda (fn)\r
- (funcall fn (or prompt "Choose a snippet table: ")\r
- tables\r
- #'yas--table-name))\r
- yas-prompt-functions)))\r
-\r
-(defun yas-x-prompt (prompt choices &optional display-fn)\r
- "Display choices in a x-window prompt."\r
- ;; FIXME: HACK: if we notice that one of the objects in choices is\r
- ;; actually a `yas--template', defer to `yas--x-prompt-pretty-templates'\r
- ;;\r
- ;; This would be better implemented by passing CHOICES as a\r
- ;; strucutred tree rather than a list. Modifications would go as far\r
- ;; up as `yas--all-templates' I think.\r
- ;;\r
- (when (and window-system choices)\r
- (let ((chosen\r
- (let (menu d) ;; d for display\r
- (dolist (c choices)\r
- (setq d (or (and display-fn (funcall display-fn c))\r
- c))\r
- (cond ((stringp d)\r
- (push (cons (concat " " d) c) menu))\r
- ((listp d)\r
- (push (car d) menu))))\r
- (setq menu (list prompt (push "title" menu)))\r
- (x-popup-menu (if (fboundp 'posn-at-point)\r
- (let ((x-y (posn-x-y (posn-at-point (point)))))\r
- (list (list (+ (car x-y) 10)\r
- (+ (cdr x-y) 20))\r
- (selected-window)))\r
- t)\r
- menu))))\r
- (or chosen\r
- (keyboard-quit)))))\r
-\r
-(defun yas--x-pretty-prompt-templates (prompt templates)\r
- "Display TEMPLATES, grouping neatly by table name."\r
- (let ((organized (make-hash-table :test #'equal))\r
- menu\r
- more-than-one-table\r
- prefix)\r
- (dolist (tl templates)\r
- (puthash (yas--template-table tl)\r
- (cons tl\r
- (gethash (yas--template-table tl) organized))\r
- organized))\r
- (setq more-than-one-table (> (hash-table-count organized) 1))\r
- (setq prefix (if more-than-one-table\r
- " " ""))\r
- (if more-than-one-table\r
- (maphash #'(lambda (table templates)\r
- (push (yas--table-name table) menu)\r
- (dolist (tl templates)\r
- (push (cons (concat prefix (yas--template-name tl)) tl) menu))) organized)\r
- (setq menu (mapcar #'(lambda (tl) (cons (concat prefix (yas--template-name tl)) tl)) templates)))\r
-\r
- (setq menu (nreverse menu))\r
- (or (x-popup-menu (if (fboundp 'posn-at-point)\r
- (let ((x-y (posn-x-y (posn-at-point (point)))))\r
- (list (list (+ (car x-y) 10)\r
- (+ (cdr x-y) 20))\r
- (selected-window)))\r
- t)\r
- (list prompt (push "title" menu)))\r
- (keyboard-quit))))\r
-\r
-(defun yas-ido-prompt (prompt choices &optional display-fn)\r
- (when (and (fboundp 'ido-completing-read)\r
- (or (>= emacs-major-version 24)\r
- ido-mode))\r
- (yas-completing-prompt prompt choices display-fn #'ido-completing-read)))\r
-\r
-(eval-when-compile (require 'dropdown-list nil t))\r
-(defun yas-dropdown-prompt (prompt choices &optional display-fn)\r
- (when (featurep 'dropdown-list)\r
- (let (formatted-choices\r
- filtered-choices\r
- d\r
- n)\r
- (dolist (choice choices)\r
- (setq d (or (and display-fn (funcall display-fn choice))\r
- choice))\r
- (when (stringp d)\r
- (push d formatted-choices)\r
- (push choice filtered-choices)))\r
-\r
- (setq n (and formatted-choices (dropdown-list formatted-choices)))\r
- (if n\r
- (nth n filtered-choices)\r
- (keyboard-quit)))))\r
-\r
-(defun yas-completing-prompt (prompt choices &optional display-fn completion-fn)\r
- (let (formatted-choices\r
- filtered-choices\r
- chosen\r
- d\r
- (completion-fn (or completion-fn\r
- #'completing-read)))\r
- (dolist (choice choices)\r
- (setq d (or (and display-fn (funcall display-fn choice))\r
- choice))\r
- (when (stringp d)\r
- (push d formatted-choices)\r
- (push choice filtered-choices)))\r
- (setq chosen (and formatted-choices\r
- (funcall completion-fn prompt\r
- formatted-choices\r
- nil\r
- 'require-match\r
- nil\r
- nil)))\r
- (let ((position (or (and chosen\r
- (position chosen formatted-choices :test #'string=))\r
- 0)))\r
- (nth position filtered-choices))))\r
-\r
-(defun yas-no-prompt (prompt choices &optional display-fn)\r
- (first choices))\r
-\r
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
-;; Loading snippets from files\r
-;;\r
-(defun yas--load-yas-setup-file (file)\r
- (load file 'noerror))\r
-\r
-(defun yas-load-directory (top-level-dir &optional use-jit)\r
- "Load snippets in directory hierarchy TOP-LEVEL-DIR.\r
-\r
-Below TOP-LEVEL-DIR each directory should be a mode name.\r
-\r
-Optional USE-JIT use jit-loading of snippets."\r
- (interactive "DSelect the root directory: ")\r
- (unless yas-snippet-dirs\r
- (setq yas-snippet-dirs top-level-dir))\r
- (dolist (dir (yas--subdirs top-level-dir))\r
- (let* ((major-mode-and-parents (yas--compute-major-mode-and-parents\r
- (concat dir "/dummy")))\r
- (mode-sym (car major-mode-and-parents))\r
- (parents (cdr major-mode-and-parents)))\r
- ;; Attention: The parents and the menus are already defined\r
- ;; here, even if the snippets are later jit-loaded.\r
- ;;\r
- ;; * We need to know the parents at this point since entering a\r
- ;; given mode should jit load for its parents\r
- ;; immediately. This could be reviewed, the parents could be\r
- ;; discovered just-in-time-as well\r
- ;;\r
- ;; * We need to create the menus here to support the `full'\r
- ;; option to `yas-use-menu' (all known snippet menus are shown to the user)\r
- ;;\r
- (yas--define-parents mode-sym parents)\r
- (yas--menu-keymap-get-create mode-sym)\r
- (let ((form `(yas--load-directory-1 ,dir\r
- ',mode-sym\r
- ',parents)))\r
- (if use-jit\r
- (yas--schedule-jit mode-sym form)\r
- (eval form)))))\r
- (when (interactive-p)\r
- (yas--message 3 "Loaded snippets from %s." top-level-dir)))\r
-\r
-(defun yas--load-directory-1 (directory mode-sym parents &optional no-compiled-snippets)\r
- "Recursively load snippet templates from DIRECTORY."\r
- (unless (file-exists-p (concat directory "/" ".yas-skip"))\r
- (if (and (not no-compiled-snippets)\r
- (progn (yas--message 2 "Loading compiled snippets from %s" directory) t)\r
- (load (expand-file-name ".yas-compiled-snippets" directory) 'noerror (<= yas-verbosity 3)))\r
- (yas--message 2 "Loading snippet files from %s" directory)\r
- (yas--load-directory-2 directory mode-sym))))\r
-\r
-(defun yas--load-directory-2 (directory mode-sym)\r
- ;; Load .yas-setup.el files wherever we find them\r
- ;;\r
- (yas--load-yas-setup-file (expand-file-name ".yas-setup" directory))\r
- (let* ((default-directory directory)\r
- (snippet-defs nil))\r
- ;; load the snippet files\r
- ;;\r
- (with-temp-buffer\r
- (dolist (file (yas--subdirs directory 'no-subdirs-just-files))\r
- (when (file-readable-p file)\r
- (insert-file-contents file nil nil nil t)\r
- (push (yas--parse-template file)\r
- snippet-defs))))\r
- (when snippet-defs\r
- (yas-define-snippets mode-sym\r
- snippet-defs))\r
- ;; now recurse to a lower level\r
- ;;\r
- (dolist (subdir (yas--subdirs directory))\r
- (yas--load-directory-2 subdir\r
- mode-sym))))\r
-\r
-(defun yas--load-snippet-dirs (&optional nojit)\r
- "Reload the directories listed in `yas-snippet-dirs' or\r
- prompt the user to select one."\r
- (let (errors)\r
- (if yas-snippet-dirs\r
- (dolist (directory (reverse (yas-snippet-dirs)))\r
- (cond ((file-directory-p directory)\r
- (yas-load-directory directory (not nojit))\r
- (if nojit\r
- (yas--message 3 "Loaded %s" directory)\r
- (yas--message 3 "Prepared just-in-time loading for %s" directory)))\r
- (t\r
- (push (yas--message 0 "Check your `yas-snippet-dirs': %s is not a directory" directory) errors))))\r
- (call-interactively 'yas-load-directory))\r
- errors))\r
-\r
-(defun yas-reload-all (&optional interactive)\r
- "Reload all snippets and rebuild the YASnippet menu.\r
-\r
-When called interactively force immediate reload of all known\r
-snippets under `yas-snippet-dirs', otherwise use just-in-time\r
-loading."\r
- (interactive "p")\r
- (catch 'abort\r
- (let ((errors)\r
- (snippet-editing-buffers\r
- (remove-if-not #'(lambda (buffer)\r
- (with-current-buffer buffer yas--editing-template))\r
- (buffer-list))))\r
- ;; Warn if there are buffers visiting snippets, since reloading will break\r
- ;; any on-line editing of those buffers.\r
- ;;\r
- (when snippet-editing-buffers\r
- (if interactive\r
- (if (y-or-n-p "Some buffers editing live snippets, close them and proceed with reload?")\r
- (mapc #'kill-buffer snippet-editing-buffers)\r
- (yas--message 1 "Aborted reload...")\r
- (throw 'abort nil))\r
- ;; in a non-interactive use, at least set\r
- ;; `yas--editing-template' to nil, make it guess it next time around\r
- (mapc #'(lambda (buffer) (setq yas--editing-template nil)) (buffer-list))))\r
-\r
- ;; Empty all snippet tables, parenting info and all menu tables\r
- ;;\r
- (setq yas--tables (make-hash-table))\r
- (setq yas--parents (make-hash-table))\r
- (setq yas--menu-table (make-hash-table))\r
-\r
- ;; Cancel all pending 'yas--scheduled-jit-loads'\r
- ;;\r
- (setq yas--scheduled-jit-loads (make-hash-table))\r
-\r
- ;; Init the `yas-minor-mode-map', taking care not to break the\r
- ;; menu....\r
- ;;\r
- (setcdr yas-minor-mode-map (cdr (yas--init-minor-keymap)))\r
-\r
- ;; Reload the directories listed in `yas-snippet-dirs' or prompt\r
- ;; the user to select one.\r
- ;;\r
- (setq errors (yas--load-snippet-dirs interactive))\r
- ;; Reload the direct keybindings\r
- ;;\r
- (yas-direct-keymaps-reload)\r
- ;; Reload the trigger-key (shoudn't be needed, but see issue #237)\r
- ;;\r
- (yas--trigger-key-reload)\r
-\r
- (yas--message 3 "Reloaded everything%s...%s."\r
- (if interactive "" " (snippets will load just-in-time)")\r
- (if errors " (some errors, check *Messages*)" "")))))\r
-\r
-(defun yas--load-pending-jits ()\r
- (when yas-minor-mode\r
- (dolist (mode (yas--modes-to-activate))\r
- (let ((forms (reverse (gethash mode yas--scheduled-jit-loads))))\r
- ;; must reverse to maintain coherence with `yas-snippet-dirs'\r
- (dolist (form forms)\r
- (yas--message 3 "Loading for `%s', just-in-time: %s!" mode form)\r
- (eval form))\r
- (remhash mode yas--scheduled-jit-loads)))))\r
-\r
-;; (when (<= emacs-major-version 22)\r
-;; (add-hook 'after-change-major-mode-hook 'yas--load-pending-jits))\r
-\r
-(defun yas--quote-string (string)\r
- "Escape and quote STRING.\r
-foo\"bar\\! -> \"foo\\\"bar\\\\!\""\r
- (concat "\""\r
- (replace-regexp-in-string "[\\\"]"\r
- "\\\\\\&"\r
- string\r
- t)\r
- "\""))\r
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
-;;; Snippet compilation function\r
-\r
-(defun yas--initialize ()\r
- "For backward compatibility, enable `yas-minor-mode' globally"\r
- (yas-global-mode 1))\r
-\r
-(defun yas-compile-directory (top-level-dir)\r
- "Create .yas-compiled-snippets.el files under subdirs of TOP-LEVEL-DIR.\r
-\r
-This works by stubbing a few functions, then calling\r
-`yas-load-directory'."\r
- (interactive "DTop level snippet directory?")\r
- (flet ((yas--load-yas-setup-file\r
- (file)\r
- (let ((elfile (concat file ".el")))\r
- (when (file-exists-p elfile)\r
- (insert ";;; .yas-setup.el support file if any:\n;;;\n")\r
- (insert-file-contents elfile)\r
- (end-of-buffer)\r
- )))\r
- (yas-define-snippets\r
- (mode snippets)\r
- (insert ";;; Snippet definitions:\n;;;\n")\r
- (let ((literal-snippets (list))\r
- (print-length nil))\r
- (dolist (snippet snippets)\r
- (let ((key (first snippet))\r
- (template-content (second snippet))\r
- (name (third snippet))\r
- (condition (fourth snippet))\r
- (group (fifth snippet))\r
- (expand-env (sixth snippet))\r
- (file nil) ;; (seventh snippet)) ;; omit on purpose\r
- (binding (eighth snippet))\r
- (uuid (ninth snippet)))\r
- (push `(,key\r
- ,template-content\r
- ,name\r
- ,condition\r
- ,group\r
- ,expand-env\r
- ,file\r
- ,binding\r
- ,uuid)\r
- literal-snippets)))\r
- (insert (pp-to-string `(yas-define-snippets ',mode ',literal-snippets)))\r
- (insert "\n\n")))\r
- (yas--load-directory-1\r
- (dir mode parents &rest ignore)\r
- (let ((output-file (concat (file-name-as-directory dir) ".yas-compiled-snippets.el")))\r
- (with-temp-file output-file\r
- (insert (format ";;; Compiled snippets and support files for `%s'\n" mode))\r
- (yas--load-directory-2 dir mode)\r
- (insert (format ";;; Do not edit! File generated at %s\n" (current-time-string)))))))\r
- (yas-load-directory top-level-dir nil)))\r
-\r
-(defun yas-recompile-all ()\r
- "Compile every dir in `yas-snippet-dirs'."\r
- (interactive)\r
- (mapc #'yas-compile-directory (yas-snippet-dirs)))\r
-\r
-\r
-;;; JIT loading\r
-;;;\r
-\r
-(defvar yas--scheduled-jit-loads (make-hash-table)\r
- "Alist of mode-symbols to forms to be evaled when `yas-minor-mode' kicks in.")\r
-\r
-(defun yas--schedule-jit (mode form)\r
- (puthash mode\r
- (cons form\r
- (gethash mode yas--scheduled-jit-loads))\r
- yas--scheduled-jit-loads))\r
-\r
-\r
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
-;;; Some user level functions\r
-;;;\r
-\r
-(defun yas-about ()\r
- (interactive)\r
- (message (concat "yasnippet (version "\r
- yas--version\r
- ") -- pluskid <pluskid@gmail.com>/joaotavora <joaotavora@gmail.com>")))\r
-\r
-(defun yas--define-parents (mode parents)\r
- "Add PARENTS to the list of MODE's parents"\r
- (puthash mode (remove-duplicates\r
- (append parents\r
- (gethash mode yas--parents)))\r
- yas--parents))\r
-\r
-(defun yas-define-snippets (mode snippets)\r
- "Define SNIPPETS for MODE.\r
-\r
-SNIPPETS is a list of snippet definitions, each taking the\r
-following form\r
-\r
- (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING UUID)\r
-\r
-Within these, only KEY and TEMPLATE are actually mandatory.\r
-\r
-TEMPLATE might be a lisp form or a string, depending on whether\r
-this is a snippet or a snippet-command.\r
-\r
-CONDITION, EXPAND-ENV and KEYBINDING are lisp forms, they have\r
-been `yas--read-lisp'-ed and will eventually be\r
-`yas--eval-lisp'-ed.\r
-\r
-The remaining elements are strings.\r
-\r
-FILE is probably of very little use if you're programatically\r
-defining snippets.\r
-\r
-UUID is the snippets \"unique-id\". Loading a second snippet file\r
-with the same uuid replaced the previous snippet.\r
-\r
-You can use `yas--parse-template' to return such lists based on\r
-the current buffers contents."\r
- (let ((snippet-table (yas--table-get-create mode))\r
- (template nil))\r
- (dolist (snippet snippets)\r
- (setq template (yas-define-snippets-1 snippet\r
- snippet-table)))\r
- template))\r
-\r
-(defun yas-define-snippets-1 (snippet snippet-table)\r
- "Helper for `yas-define-snippets'."\r
- ;; X) Calculate some more defaults on the values returned by\r
- ;; `yas--parse-template'.\r
- ;;\r
- (let* ((file (seventh snippet))\r
- (key (car snippet))\r
- (name (or (third snippet)\r
- (and file\r
- (file-name-directory file))))\r
- (condition (fourth snippet))\r
- (group (fifth snippet))\r
- (keybinding (yas--read-keybinding (eighth snippet)))\r
- (uuid (or (ninth snippet)\r
- name))\r
- (template (or (gethash uuid (yas--table-uuidhash snippet-table))\r
- (yas--make-blank-template))))\r
- ;; X) populate the template object\r
- ;;\r
- (yas--populate-template template\r
- :table snippet-table\r
- :key key\r
- :content (second snippet)\r
- :name (or name key)\r
- :group group\r
- :condition condition\r
- :expand-env (sixth snippet)\r
- :file (seventh snippet)\r
- :keybinding keybinding\r
- :uuid uuid)\r
- ;; X) Update this template in the appropriate table. This step\r
- ;; also will take care of adding the key indicators in the\r
- ;; templates menu entry, if any\r
- ;;\r
- (yas--update-template snippet-table template)\r
- ;; X) Return the template\r
- ;;\r
- ;;\r
- template))\r
-\r
-\f\r
-;;; Apropos snippet menu:\r
-;;\r
-;; The snippet menu keymaps are store by mode in hash table called\r
-;; `yas--menu-table'. They are linked to the main menu in\r
-;; `yas--menu-keymap-get-create' and are initially created empty,\r
-;; reflecting the table hierarchy.\r
-;;\r
-;; They can be populated in two mutually exclusive ways: (1) by\r
-;; reading `yas--template-group', which in turn is populated by the "#\r
-;; group:" directives of the snippets or the ".yas-make-groups" file\r
-;; or (2) by using a separate `yas-define-menu' call, which declares a\r
-;; menu structure based on snippets uuids.\r
-;;\r
-;; Both situations are handled in `yas--update-template-menu', which\r
-;; uses the predicate `yas--template-menu-managed-by-yas-define-menu'\r
-;; that can tell between the two situations.\r
-;;\r
-;; Note:\r
-;;\r
-;; * if `yas-define-menu' is used it must run before\r
-;; `yas-define-snippets' and the UUIDS must match, otherwise we get\r
-;; duplicate entries. The `yas--template' objects are created in\r
-;; `yas-define-menu', holding nothing but the menu entry,\r
-;; represented by a pair of ((menu-item NAME :keys KEYS) TYPE) and\r
-;; stored in `yas--template-menu-binding-pair'. The (menu-item ...)\r
-;; part is then stored in the menu keymap itself which make the item\r
-;; appear to the user. These limitations could probably be revised.\r
-;;\r
-;; * The `yas--template-perm-group' slot is only used in\r
-;; `yas-describe-tables'.\r
-;;\r
-(defun yas--template-menu-binding-pair-get-create (template &optional type)\r
- "Get TEMPLATE's menu binding or assign it a new one.\r
-\r
-TYPE may be `:stay', signalling this menu binding should be\r
-static in the menu."\r
- (or (yas--template-menu-binding-pair template)\r
- (let ((key (yas--template-key template))\r
- (keybinding (yas--template-keybinding template)))\r
- (setf (yas--template-menu-binding-pair template)\r
- (cons `(menu-item ,(or (yas--template-name template)\r
- (yas--template-uuid template))\r
- ,(yas--make-menu-binding template)\r
- :keys ,nil)\r
- type)))))\r
-(defun yas--template-menu-managed-by-yas-define-menu (template)\r
- "Non-nil if TEMPLATE's menu entry was included in a `yas-define-menu' call."\r
- (cdr (yas--template-menu-binding-pair template)))\r
-\r
-\r
-(defun yas--show-menu-p (mode)\r
- (cond ((eq yas-use-menu 'abbreviate)\r
- (find mode\r
- (mapcar #'(lambda (table)\r
- (yas--table-mode table))\r
- (yas--get-snippet-tables))))\r
- ((eq yas-use-menu 'full)\r
- t)\r
- ((eq yas-use-menu t)\r
- t)))\r
-\r
-(defun yas--delete-from-keymap (keymap uuid)\r
- "Recursively delete items with UUID from KEYMAP and its submenus."\r
-\r
- ;; XXX: This used to skip any submenus named \"parent mode\"\r
- ;;\r
- ;; First of all, recursively enter submenus, i.e. the tree is\r
- ;; searched depth first so that stale submenus can be found in the\r
- ;; higher passes.\r
- ;;\r
- (mapc #'(lambda (item)\r
- (when (and (listp (cdr item))\r
- (keymapp (third (cdr item))))\r
- (yas--delete-from-keymap (third (cdr item)) uuid)))\r
- (rest keymap))\r
- ;; Set the uuid entry to nil\r
- ;;\r
- (define-key keymap (vector (make-symbol uuid)) nil)\r
- ;; Destructively modify keymap\r
- ;;\r
- (setcdr keymap (delete-if #'(lambda (item)\r
- (or (null (cdr item))\r
- (and (keymapp (third (cdr item)))\r
- (null (cdr (third (cdr item)))))))\r
- (rest keymap))))\r
-\r
-(defun yas-define-menu (mode menu &optional omit-items)\r
- "Define a snippet menu for MODE according to MENU, ommitting OMIT-ITEMS.\r
-\r
-MENU is a list, its elements can be:\r
-\r
-- (yas-item UUID) : Creates an entry the snippet identified with\r
- UUID. The menu entry for a snippet thus identified is\r
- permanent, i.e. it will never move (be reordered) in the menu.\r
-\r
-- (yas-separator) : Creates a separator\r
-\r
-- (yas-submenu NAME SUBMENU) : Creates a submenu with NAME,\r
- SUBMENU has the same form as MENU. NAME is also added to the\r
- list of groups of the snippets defined thereafter.\r
-\r
-OMIT-ITEMS is a list of snippet uuid's that will always be\r
-ommited from MODE's menu, even if they're manually loaded.\r
-\r
-This function does nothing if `yas-use-menu' is nil.\r
-"\r
- (when yas-use-menu\r
- (let* ((table (yas--table-get-create mode))\r
- (hash (yas--table-uuidhash table)))\r
- (yas--define-menu-1 table\r
- (yas--menu-keymap-get-create mode)\r
- menu\r
- hash)\r
- (dolist (uuid omit-items)\r
- (let ((template (or (gethash uuid hash)\r
- (yas--populate-template (puthash uuid\r
- (yas--make-blank-template)\r
- hash)\r
- :table table\r
- :uuid uuid))))\r
- (setf (yas--template-menu-binding-pair template) (cons nil :none)))))))\r
-\r
-(defun yas--define-menu-1 (table menu-keymap menu uuidhash &optional group-list)\r
- (dolist (e (reverse menu))\r
- (cond ((eq (first e) 'yas-item)\r
- (let ((template (or (gethash (second e) uuidhash)\r
- (yas--populate-template (puthash (second e)\r
- (yas--make-blank-template)\r
- uuidhash)\r
- :table table\r
- :perm-group group-list\r
- :uuid (second e)))))\r
- (define-key menu-keymap (vector (gensym))\r
- (car (yas--template-menu-binding-pair-get-create template :stay)))))\r
- ((eq (first e) 'yas-submenu)\r
- (let ((subkeymap (make-sparse-keymap)))\r
- (define-key menu-keymap (vector (gensym))\r
- `(menu-item ,(second e) ,subkeymap))\r
- (yas--define-menu-1 table\r
- subkeymap\r
- (third e)\r
- uuidhash\r
- (append group-list (list (second e))))))\r
- ((eq (first e) 'yas-separator)\r
- (define-key menu-keymap (vector (gensym))\r
- '(menu-item "----")))\r
- (t\r
- (yas--message 3 "Don't know anything about menu entry %s" (first e))))))\r
-\f\r
-(defun yas--define (mode key template &optional name condition group)\r
- "Define a snippet. Expanding KEY into TEMPLATE.\r
-\r
-NAME is a description to this template. Also update the menu if\r
-`yas-use-menu' is t. CONDITION is the condition attached to\r
-this snippet. If you attach a condition to a snippet, then it\r
-will only be expanded when the condition evaluated to non-nil."\r
- (yas-define-snippets mode\r
- (list (list key template name condition group))))\r
-\r
-(defun yas-hippie-try-expand (first-time?)\r
- "Integrate with hippie expand. Just put this function in\r
-`hippie-expand-try-functions-list'."\r
- (when yas-minor-mode\r
- (if (not first-time?)\r
- (let ((yas-fallback-behavior 'return-nil))\r
- (yas-expand))\r
- (undo 1)\r
- nil)))\r
-\r
-\r
-;;; Apropos condition-cache:\r
-;;;\r
-;;;\r
-;;;\r
-;;;\r
-(defvar yas--condition-cache-timestamp nil)\r
-(defmacro yas-define-condition-cache (func doc &rest body)\r
- "Define a function FUNC with doc DOC and body BODY, BODY is\r
-executed at most once every snippet expansion attempt, to check\r
-expansion conditions.\r
-\r
-It doesn't make any sense to call FUNC programatically."\r
- `(defun ,func () ,(if (and doc\r
- (stringp doc))\r
- (concat doc\r
-"\n\nFor use in snippets' conditions. Within each\r
-snippet-expansion routine like `yas-expand', computes actual\r
-value for the first time then always returns a cached value.")\r
- (setq body (cons doc body))\r
- nil)\r
- (let ((timestamp-and-value (get ',func 'yas--condition-cache)))\r
- (if (equal (car timestamp-and-value) yas--condition-cache-timestamp)\r
- (cdr timestamp-and-value)\r
- (let ((new-value (progn\r
- ,@body\r
- )))\r
- (put ',func 'yas--condition-cache (cons yas--condition-cache-timestamp new-value))\r
- new-value)))))\r
-\r
-(defalias 'yas-expand 'yas-expand-from-trigger-key)\r
-(defun yas-expand-from-trigger-key (&optional field)\r
- "Expand a snippet before point.\r
-\r
-If no snippet expansion is possible, fall back to the behaviour\r
-defined in `yas-fallback-behavior'.\r
-\r
-Optional argument FIELD is for non-interactive use and is an\r
-object satisfying `yas--field-p' to restrict the expansion to."\r
- (interactive)\r
- (setq yas--condition-cache-timestamp (current-time))\r
- (let (templates-and-pos)\r
- (unless (and yas-expand-only-for-last-commands\r
- (not (member last-command yas-expand-only-for-last-commands)))\r
- (setq templates-and-pos (if field\r
- (save-restriction\r
- (narrow-to-region (yas--field-start field)\r
- (yas--field-end field))\r
- (yas--current-key))\r
- (yas--current-key))))\r
- (if (and templates-and-pos\r
- (first templates-and-pos))\r
- (yas--expand-or-prompt-for-template (first templates-and-pos)\r
- (second templates-and-pos)\r
- (third templates-and-pos))\r
- (yas--fallback 'trigger-key))))\r
-\r
-(defun yas-expand-from-keymap ()\r
- "Directly expand some snippets, searching `yas--direct-keymaps'.\r
-\r
-If expansion fails, execute the previous binding for this key"\r
- (interactive)\r
- (setq yas--condition-cache-timestamp (current-time))\r
- (let* ((yas--prefix current-prefix-arg)\r
- (vec (subseq (this-command-keys-vector) (if current-prefix-arg\r
- universal-argument-num-events\r
- 0)))\r
- (templates (mapcan #'(lambda (table)\r
- (yas--fetch table vec))\r
- (yas--get-snippet-tables))))\r
- (if templates\r
- (yas--expand-or-prompt-for-template templates)\r
- (let ((yas-fallback-behavior 'call-other-command))\r
- (yas--fallback)))))\r
-\r
-(defun yas--expand-or-prompt-for-template (templates &optional start end)\r
- "Expand one of TEMPLATES from START to END.\r
-\r
-Prompt the user if TEMPLATES has more than one element, else\r
-expand immediately. Common gateway for\r
-`yas-expand-from-trigger-key' and `yas-expand-from-keymap'."\r
- (let ((yas--current-template (or (and (rest templates) ;; more than one\r
- (yas--prompt-for-template (mapcar #'cdr templates)))\r
- (cdar templates))))\r
- (when yas--current-template\r
- (yas-expand-snippet (yas--template-content yas--current-template)\r
- start\r
- end\r
- (yas--template-expand-env yas--current-template)))))\r
-\r
-(defun yas--trigger-key-for-fallback ()\r
- ;; When `yas-trigger-key' is <tab> it correctly overrides\r
- ;; org-mode's <tab>, for example and searching for fallbacks\r
- ;; correctly returns `org-cycle'. However, most other modes bind\r
- ;; "TAB" (which is translated from <tab>), and calling\r
- ;; (key-binding "TAB") does not place return that command into\r
- ;; our command-2 local. So we cheat.\r
- ;;\r
- (if (string= yas-trigger-key "<tab>")\r
- "TAB"\r
- yas-trigger-key))\r
-\r
-(defun yas--fallback (&optional from-trigger-key-p)\r
- "Fallback after expansion has failed.\r
-\r
-Common gateway for `yas-expand-from-trigger-key' and\r
-`yas-expand-from-keymap'."\r
- (cond ((eq yas-fallback-behavior 'return-nil)\r
- ;; return nil\r
- nil)\r
- ((eq yas-fallback-behavior 'call-other-command)\r
- (let* ((yas-minor-mode nil)\r
- (yas--direct-keymaps nil)\r
- (yas-trigger-key (yas--trigger-key-for-fallback))\r
- (keys-1 (this-command-keys-vector))\r
- (keys-2 (and yas-trigger-key\r
- from-trigger-key-p\r
- (stringp yas-trigger-key)\r
- (read-kbd-macro yas-trigger-key)))\r
- (command-1 (and keys-1 (key-binding keys-1)))\r
- (command-2 (and keys-2 (key-binding keys-2)))\r
- ;; An (ugly) safety: prevents infinite recursion of\r
- ;; yas-expand* calls.\r
- (command (or (and (symbolp command-1)\r
- (not (string-match "yas-expand" (symbol-name command-1)))\r
- command-1)\r
- (and (symbolp command-2)\r
- command-2))))\r
- (when (and (commandp command)\r
- (not (string-match "yas-expand" (symbol-name command))))\r
- (setq this-command command)\r
- (call-interactively command))))\r
- ((and (listp yas-fallback-behavior)\r
- (cdr yas-fallback-behavior)\r
- (eq 'apply (car yas-fallback-behavior)))\r
- (if (cddr yas-fallback-behavior)\r
- (apply (cadr yas-fallback-behavior)\r
- (cddr yas-fallback-behavior))\r
- (when (commandp (cadr yas-fallback-behavior))\r
- (setq this-command (cadr yas-fallback-behavior))\r
- (call-interactively (cadr yas-fallback-behavior)))))\r
- (t\r
- ;; also return nil if all the other fallbacks have failed\r
- nil)))\r
-\r
-\r
-\f\r
-;;; Utils for snippet development:\r
-\r
-(defun yas--all-templates (tables)\r
- "Return all snippet tables applicable for the current buffer.\r
-\r
-Honours `yas-choose-tables-first', `yas-choose-keys-first' and\r
-`yas-buffer-local-condition'"\r
- (when yas-choose-tables-first\r
- (setq tables (list (yas--prompt-for-table tables))))\r
- (mapcar #'cdr\r
- (if yas-choose-keys-first\r
- (let ((key (yas--prompt-for-keys\r
- (mapcan #'yas--table-all-keys tables))))\r
- (when key\r
- (mapcan #'(lambda (table)\r
- (yas--fetch table key))\r
- tables)))\r
- (remove-duplicates (mapcan #'yas--table-templates tables)\r
- :test #'equal))))\r
-\r
-(defun yas-insert-snippet (&optional no-condition)\r
- "Choose a snippet to expand, pop-up a list of choices according\r
-to `yas--prompt-function'.\r
-\r
-With prefix argument NO-CONDITION, bypass filtering of snippets\r
-by condition."\r
- (interactive "P")\r
- (setq yas--condition-cache-timestamp (current-time))\r
- (let* ((yas-buffer-local-condition (or (and no-condition\r
- 'always)\r
- yas-buffer-local-condition))\r
- (templates (yas--all-templates (yas--get-snippet-tables)))\r
- (yas--current-template (and templates\r
- (or (and (rest templates) ;; more than one template for same key\r
- (yas--prompt-for-template templates))\r
- (car templates))))\r
- (where (if (region-active-p)\r
- (cons (region-beginning) (region-end))\r
- (cons (point) (point)))))\r
- (if yas--current-template\r
- (yas-expand-snippet (yas--template-content yas--current-template)\r
- (car where)\r
- (cdr where)\r
- (yas--template-expand-env yas--current-template))\r
- (yas--message 3 "No snippets can be inserted here!"))))\r
-\r
-(defun yas-visit-snippet-file ()\r
- "Choose a snippet to edit, selection like `yas-insert-snippet'.\r
-\r
-Only success if selected snippet was loaded from a file. Put the\r
-visited file in `snippet-mode'."\r
- (interactive)\r
- (let* ((yas-buffer-local-condition 'always)\r
- (templates (yas--all-templates (yas--get-snippet-tables)))\r
- (yas-prompt-functions '(yas-ido-prompt yas-completing-prompt))\r
- (template (and templates\r
- (or (yas--prompt-for-template templates\r
- "Choose a snippet template to edit: ")\r
- (car templates)))))\r
-\r
- (if template\r
- (yas--visit-snippet-file-1 template)\r
- (message "No snippets tables active!"))))\r
-\r
-(defun yas--visit-snippet-file-1 (template)\r
- (let ((file (yas--template-file template)))\r
- (cond ((and file (file-readable-p file))\r
- (find-file-other-window file)\r
- (snippet-mode)\r
- (set (make-local-variable 'yas--editing-template) template))\r
- (file\r
- (message "Original file %s no longer exists!" file))\r
- (t\r
- (switch-to-buffer (format "*%s*"(yas--template-name template)))\r
- (let ((type 'snippet))\r
- (when (listp (yas--template-content template))\r
- (insert (format "# type: command\n"))\r
- (setq type 'command))\r
- (insert (format "# key: %s\n" (yas--template-key template)))\r
- (insert (format "# name: %s\n" (yas--template-name template)))\r
- (when (yas--template-keybinding template)\r
- (insert (format "# binding: %s\n" (yas--template-keybinding template))))\r
- (when (yas--template-expand-env template)\r
- (insert (format "# expand-env: %s\n" (yas--template-expand-env template))))\r
- (when (yas--template-condition template)\r
- (insert (format "# condition: %s\n" (yas--template-condition template))))\r
- (insert "# --\n")\r
- (insert (if (eq type 'command)\r
- (pp-to-string (yas--template-content template))\r
- (yas--template-content template))))\r
- (snippet-mode)\r
- (set (make-local-variable 'yas--editing-template) template)))))\r
-\r
-(defun yas--guess-snippet-directories-1 (table)\r
- "Guesses possible snippet subdirectories for TABLE."\r
- (cons (yas--table-name table)\r
- (mapcan #'(lambda (parent)\r
- (yas--guess-snippet-directories-1\r
- parent))\r
- (yas--table-parents table))))\r
-\r
-(defun yas--guess-snippet-directories (&optional table)\r
- "Try to guess suitable directories based on the current active\r
-tables (or optional TABLE).\r
-\r
-Returns a list of elemts (TABLE . DIRS) where TABLE is a\r
-`yas--table' object and DIRS is a list of all possible directories\r
-where snippets of table might exist."\r
- (let ((main-dir (replace-regexp-in-string\r
- "/+$" ""\r
- (or (first (or (yas-snippet-dirs)\r
- (setq yas-snippet-dirs '("~/.emacs.d/snippets")))))))\r
- (tables (or (and table\r
- (list table))\r
- (yas--get-snippet-tables))))\r
- ;; HACK! the snippet table created here is actually registered!\r
- ;;\r
- (unless (or table (gethash major-mode yas--tables))\r
- (push (yas--table-get-create major-mode)\r
- tables))\r
-\r
- (mapcar #'(lambda (table)\r
- (cons table\r
- (mapcar #'(lambda (subdir)\r
- (concat main-dir "/" subdir))\r
- (yas--guess-snippet-directories-1 table))))\r
- tables)))\r
-\r
-(defun yas--make-directory-maybe (table-and-dirs &optional main-table-string)\r
- "Returns a dir inside TABLE-AND-DIRS, prompts for creation if none exists."\r
- (or (some #'(lambda (dir) (when (file-directory-p dir) dir)) (cdr table-and-dirs))\r
- (let ((candidate (first (cdr table-and-dirs))))\r
- (unless (file-writable-p (file-name-directory candidate))\r
- (error (yas--format "%s is not writable." candidate)))\r
- (if (y-or-n-p (format "Guessed directory (%s) for%s%s table \"%s\" does not exist! Create? "\r
- candidate\r
- (if (gethash (yas--table-mode (car table-and-dirs))\r
- yas--tables)\r
- ""\r
- " brand new")\r
- (or main-table-string\r
- "")\r
- (yas--table-name (car table-and-dirs))))\r
- (progn\r
- (make-directory candidate 'also-make-parents)\r
- ;; create the .yas-parents file here...\r
- candidate)))))\r
-\r
-(defun yas-new-snippet (&optional no-template)\r
- "Pops a new buffer for writing a snippet.\r
-\r
-Expands a snippet-writing snippet, unless the optional prefix arg\r
-NO-TEMPLATE is non-nil."\r
- (interactive "P")\r
- (let ((guessed-directories (yas--guess-snippet-directories)))\r
-\r
- (switch-to-buffer "*new snippet*")\r
- (erase-buffer)\r
- (kill-all-local-variables)\r
- (snippet-mode)\r
- (yas-minor-mode 1)\r
- (set (make-local-variable 'yas--guessed-modes) (mapcar #'(lambda (d)\r
- (yas--table-mode (car d)))\r
- guessed-directories))\r
- (unless no-template (yas-expand-snippet "\\r
-# -*- mode: snippet -*-\r
-# name: $1\r
-# key: ${2:${1:$(replace-regexp-in-string \"\\\\\\\\(\\\\\\\\w+\\\\\\\\).*\" \"\\\\\\\\1\" yas-text)}}${3:\r
-# binding: ${4:direct-keybinding}}${5:\r
-# expand-env: ((${6:some-var} ${7:some-value}))}${8:\r
-# type: command}\r
-# --\r
-$0"))))\r
-\r
-(defun yas--compute-major-mode-and-parents (file)\r
- "Given FILE, find the nearest snippet directory for a given\r
-mode, then return a list (MODE-SYM PARENTS), the mode's symbol and a list\r
-representing one or more of the mode's parents.\r
-\r
-Note that MODE-SYM need not be the symbol of a real major mode,\r
-neither do the elements of PARENTS."\r
- (let* ((file-dir (and file\r
- (directory-file-name (or (some #'(lambda (special)\r
- (locate-dominating-file file special))\r
- '(".yas-setup.el"\r
- ".yas-make-groups"\r
- ".yas-parents"))\r
- (directory-file-name (file-name-directory file))))))\r
- (parents-file-name (concat file-dir "/.yas-parents"))\r
- (major-mode-name (and file-dir\r
- (file-name-nondirectory file-dir)))\r
- (major-mode-sym (or (and major-mode-name\r
- (intern major-mode-name))))\r
- (parents (when (file-readable-p parents-file-name)\r
- (mapcar #'intern\r
- (split-string\r
- (with-temp-buffer\r
- (insert-file-contents parents-file-name)\r
- (buffer-substring-no-properties (point-min)\r
- (point-max))))))))\r
- (when major-mode-sym\r
- (cons major-mode-sym parents))))\r
-\r
-(defvar yas--editing-template nil\r
- "Supporting variable for `yas-load-snippet-buffer' and `yas--visit-snippet'")\r
-\r
-(defvar yas--current-template nil\r
- "Holds the current template being expanded into a snippet.")\r
-\r
-(defvar yas--guessed-modes nil\r
- "List of guessed modes supporting `yas-load-snippet-buffer'.")\r
-\r
-(defun yas--read-table ()\r
- "Ask user for a snippet table, help with some guessing."\r
- (let ((prompt (if (and (featurep 'ido)\r
- ido-mode)\r
- 'ido-completing-read 'completing-read)))\r
- (unless yas--guessed-modes\r
- (set (make-local-variable 'yas--guessed-modes)\r
- (or (yas--compute-major-mode-and-parents buffer-file-name))))\r
- (intern\r
- (funcall prompt (format "Choose or enter a table (yas guesses %s): "\r
- (if yas--guessed-modes\r
- (first yas--guessed-modes)\r
- "nothing"))\r
- (mapcar #'symbol-name yas--guessed-modes)\r
- nil\r
- nil\r
- nil\r
- nil\r
- (if (first yas--guessed-modes)\r
- (symbol-name (first yas--guessed-modes)))))))\r
-\r
-(defun yas-load-snippet-buffer (table &optional interactive)\r
- "Parse and load current buffer's snippet definition into TABLE.\r
-\r
-TABLE is a symbol naming a passed to `yas--table-get-create'.\r
-\r
-When called interactively, prompt for the table name and\r
-whether (and where) to save the snippet, then quit the window."\r
- (interactive (list (yas--read-table) t))\r
- (cond\r
- ;; We have `yas--editing-template', this buffer's content comes from a\r
- ;; template which is already loaded and neatly positioned,...\r
- ;;\r
- (yas--editing-template\r
- (yas-define-snippets-1 (yas--parse-template (yas--template-file yas--editing-template))\r
- (yas--template-table yas--editing-template)))\r
- ;; Try to use `yas--guessed-modes'. If we don't have that use the\r
- ;; value from `yas--compute-major-mode-and-parents'\r
- ;;\r
- (t\r
- (unless yas--guessed-modes\r
- (set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))\r
- (let* ((table (yas--table-get-create table)))\r
- (set (make-local-variable 'yas--editing-template)\r
- (yas-define-snippets-1 (yas--parse-template buffer-file-name)\r
- table)))))\r
-\r
- (when (and interactive\r
- (or\r
- ;; Only offer to save this if it looks like a library or new\r
- ;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs'\r
- ;; which is not the first, or from an unwritable file)\r
- ;;\r
- (not (yas--template-file yas--editing-template))\r
- (not (file-writable-p (yas--template-file yas--editing-template)))\r
- (and (listp yas-snippet-dirs)\r
- (second yas-snippet-dirs)\r
- (not (string-match (expand-file-name (first yas-snippet-dirs))\r
- (yas--template-file yas--editing-template)))))\r
- (y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? ")))\r
- (let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))\r
- (chosen (and option\r
- (yas--make-directory-maybe option))))\r
- (when chosen\r
- (let ((default-file-name (or (and (yas--template-file yas--editing-template)\r
- (file-name-nondirectory (yas--template-file yas--editing-template)))\r
- (yas--template-name yas--editing-template))))\r
- (write-file (concat chosen "/"\r
- (read-from-minibuffer (format "File name to create in %s? " chosen)\r
- default-file-name)))\r
- (setf (yas--template-file yas--editing-template) buffer-file-name)))))\r
- (when interactive\r
- (quit-window interactive)\r
- (yas--message 3 "Snippet \"%s\" loaded for %s."\r
- (yas--template-name yas--editing-template)\r
- (yas--table-name (yas--template-table yas--editing-template)))))\r
-\r
-\r
-(defun yas-tryout-snippet (&optional debug)\r
- "Test current buffers's snippet template in other buffer."\r
- (interactive "P")\r
- (let* ((major-mode-and-parent (yas--compute-major-mode-and-parents buffer-file-name))\r
- (parsed (yas--parse-template))\r
- (test-mode (or (and (car major-mode-and-parent)\r
- (fboundp (car major-mode-and-parent))\r
- (car major-mode-and-parent))\r
- (first yas--guessed-modes)\r
- (intern (read-from-minibuffer (yas--format "Please input a mode: ")))))\r
- (yas--current-template\r
- (and parsed\r
- (fboundp test-mode)\r
- (yas--populate-template (yas--make-blank-template)\r
- :table nil ;; no tables for ephemeral snippets\r
- :key (first parsed)\r
- :content (second parsed)\r
- :name (third parsed)\r
- :expand-env (sixth parsed)))))\r
- (cond (yas--current-template\r
- (let ((buffer-name (format "*testing snippet: %s*" (yas--template-name yas--current-template))))\r
- (kill-buffer (get-buffer-create buffer-name))\r
- (switch-to-buffer (get-buffer-create buffer-name))\r
- (setq buffer-undo-list nil)\r
- (condition-case nil (funcall test-mode) (error nil))\r
- (yas-minor-mode 1)\r
- (setq buffer-read-only nil)\r
- (yas-expand-snippet (yas--template-content yas--current-template)\r
- (point-min)\r
- (point-max)\r
- (yas--template-expand-env yas--current-template))\r
- (when (and debug\r
- (require 'yasnippet-debug nil t))\r
- (add-hook 'post-command-hook 'yas-debug-snippet-vars nil t))))\r
- (t\r
- (yas--message 3 "Cannot test snippet for unknown major mode")))))\r
-\r
-(defun yas--template-fine-group (template)\r
- (car (last (or (yas--template-group template)\r
- (yas--template-perm-group template)))))\r
-\r
-(defun yas-describe-tables (&optional choose)\r
- "Display snippets for each table."\r
- (interactive "P")\r
- (let* ((by-name-hash (and choose\r
- (y-or-n-p "Show by namehash? ")))\r
- (buffer (get-buffer-create "*YASnippet tables*"))\r
- (active-tables (yas--get-snippet-tables))\r
- (remain-tables (let ((all))\r
- (maphash #'(lambda (k v)\r
- (unless (find v active-tables)\r
- (push v all)))\r
- yas--tables)\r
- all))\r
- (table-lists (list active-tables remain-tables))\r
- (original-buffer (current-buffer))\r
- (continue t)\r
- (yas--condition-cache-timestamp (current-time)))\r
- (with-current-buffer buffer\r
- (setq buffer-read-only nil)\r
- (erase-buffer)\r
- (cond ((not by-name-hash)\r
- (insert "YASnippet tables: \n")\r
- (while (and table-lists\r
- continue)\r
- (dolist (table (car table-lists))\r
- (yas--describe-pretty-table table original-buffer))\r
- (setq table-lists (cdr table-lists))\r
- (when table-lists\r
- (yas--create-snippet-xrefs)\r
- (display-buffer buffer)\r
- (setq continue (and choose (y-or-n-p "Show also non-active tables? ")))))\r
- (yas--create-snippet-xrefs)\r
- (help-mode)\r
- (goto-char 1))\r
- (t\r
- (insert "\n\nYASnippet tables by NAMEHASH: \n")\r
- (dolist (table (append active-tables remain-tables))\r
- (insert (format "\nSnippet table `%s':\n\n" (yas--table-name table)))\r
- (let ((keys))\r
- (maphash #'(lambda (k v)\r
- (push k keys))\r
- (yas--table-hash table))\r
- (dolist (key keys)\r
- (insert (format " key %s maps snippets: %s\n" key\r
- (let ((names))\r
- (maphash #'(lambda (k v)\r
- (push k names))\r
- (gethash key (yas--table-hash table)))\r
- names))))))))\r
- (goto-char 1)\r
- (setq buffer-read-only t))\r
- (display-buffer buffer)))\r
-\r
-(defun yas--describe-pretty-table (table &optional original-buffer)\r
- (insert (format "\nSnippet table `%s'"\r
- (yas--table-name table)))\r
- (if (yas--table-parents table)\r
- (insert (format " parents: %s\n"\r
- (mapcar #'yas--table-name\r
- (yas--table-parents table))))\r
- (insert "\n"))\r
- (insert (make-string 100 ?-) "\n")\r
- (insert "group state name key binding\n")\r
- (let ((groups-hash (make-hash-table :test #'equal)))\r
- (maphash #'(lambda (k v)\r
- (let ((group (or (yas--template-fine-group v)\r
- "(top level)")))\r
- (when (yas--template-name v)\r
- (puthash group\r
- (cons v (gethash group groups-hash))\r
- groups-hash))))\r
- (yas--table-uuidhash table))\r
- (maphash\r
- #'(lambda (group templates)\r
- (setq group (truncate-string-to-width group 25 0 ? "..."))\r
- (insert (make-string 100 ?-) "\n")\r
- (dolist (p templates)\r
- (let ((name (truncate-string-to-width (propertize (format "\\\\snippet `%s'" (yas--template-name p))\r
- 'yasnippet p)\r
- 50 0 ? "..."))\r
- (group (prog1 group\r
- (setq group (make-string (length group) ? ))))\r
- (condition-string (let ((condition (yas--template-condition p)))\r
- (if (and condition\r
- original-buffer)\r
- (with-current-buffer original-buffer\r
- (if (yas--eval-condition condition)\r
- "(y)"\r
- "(s)"))\r
- "(a)"))))\r
- (insert group " ")\r
- (insert condition-string " ")\r
- (insert name\r
- (if (string-match "\\.\\.\\.$" name)\r
- "'"\r
- " ")\r
- " ")\r
- (insert (truncate-string-to-width (or (yas--template-key p) "")\r
- 15 0 ? "...") " ")\r
- (insert (truncate-string-to-width (key-description (yas--template-keybinding p))\r
- 15 0 ? "...") " ")\r
- (insert "\n"))))\r
- groups-hash)))\r
-\r
-\r
-\f\r
-;;; User convenience functions, for using in snippet definitions\r
-\r
-(defvar yas-modified-p nil\r
- "Non-nil if field has been modified by user or transformation.")\r
-\r
-(defvar yas-moving-away-p nil\r
- "Non-nil if user is about to exit field.")\r
-\r
-(defvar yas-text nil\r
- "Contains current field text.")\r
-\r
-(defun yas-substr (str pattern &optional subexp)\r
- "Search PATTERN in STR and return SUBEXPth match.\r
-\r
-If found, the content of subexp group SUBEXP (default 0) is\r
- returned, or else the original STR will be returned."\r
- (let ((grp (or subexp 0)))\r
- (save-match-data\r
- (if (string-match pattern str)\r
- (match-string-no-properties grp str)\r
- str))))\r
-\r
-(defun yas-choose-value (&rest possibilities)\r
- "Prompt for a string in POSSIBILITIES and return it.\r
-\r
-The last element of POSSIBILITIES may be a list of strings."\r
- (unless (or yas-moving-away-p\r
- yas-modified-p)\r
- (setq possibilities (nreverse possibilities))\r
- (setq possibilities (if (listp (car possibilities))\r
- (append (reverse (car possibilities)) (rest possibilities))\r
- possibilities))\r
- (some #'(lambda (fn)\r
- (funcall fn "Choose: " possibilities))\r
- yas-prompt-functions)))\r
-\r
-(defun yas-key-to-value (alist)\r
- "Prompt for a string in the list POSSIBILITIES and return it."\r
- (unless (or yas-moving-away-p\r
- yas-modified-p)\r
- (let ((key (read-key-sequence "")))\r
- (when (stringp key)\r
- (or (cdr (find key alist :key #'car :test #'string=))\r
- key)))))\r
-\r
-(defun yas-throw (text)\r
- "Throw a yas--exception with TEXT as the reason."\r
- (throw 'yas--exception (cons 'yas--exception text)))\r
-\r
-(defun yas-verify-value (possibilities)\r
- "Verify that the current field value is in POSSIBILITIES\r
-\r
-Otherwise throw exception."\r
- (when (and yas-moving-away-p (notany #'(lambda (pos) (string= pos yas-text)) possibilities))\r
- (yas-throw (yas--format "Field only allows %s" possibilities))))\r
-\r
-(defun yas-field-value (number)\r
- "Get the string for field with NUMBER.\r
-\r
-Use this in primary and mirror transformations to tget."\r
- (let* ((snippet (car (yas--snippets-at-point)))\r
- (field (and snippet\r
- (yas--snippet-find-field snippet number))))\r
- (when field\r
- (yas--field-text-for-display field))))\r
-\r
-(defun yas-text ()\r
- "Return `yas-text' if that exists and is non-empty, else nil."\r
- (if (and yas-text\r
- (not (string= "" yas-text)))\r
- yas-text))\r
-\r
-(defun yas-selected-text ()\r
- "Return `yas-selected-text' if that exists and is non-empty, else nil."\r
- (if (and yas-selected-text\r
- (not (string= "" yas-selected-text)))\r
- yas-selected-text))\r
-\r
-(defun yas--get-field-once (number &optional transform-fn)\r
- (unless yas-modified-p\r
- (if transform-fn\r
- (funcall transform-fn (yas-field-value number))\r
- (yas-field-value number))))\r
-\r
-(defun yas-default-from-field (number)\r
- (unless yas-modified-p\r
- (yas-field-value number)))\r
-\r
-(defun yas-inside-string ()\r
- (equal 'font-lock-string-face (get-char-property (1- (point)) 'face)))\r
-\r
-(defun yas-unimplemented (&optional missing-feature)\r
- (if yas--current-template\r
- (if (y-or-n-p (format "This snippet is unimplemented (missing %s) Visit the snippet definition? "\r
- (or missing-feature\r
- "something")))\r
- (yas--visit-snippet-file-1 yas--current-template))\r
- (message "No implementation. Missing %s" (or missing-feature "something"))))\r
-\r
-\f\r
-;;; Snippet expansion and field management\r
-\r
-(defvar yas--active-field-overlay nil\r
- "Overlays the currently active field.")\r
-\r
-(defvar yas--field-protection-overlays nil\r
- "Two overlays protect the current active field ")\r
-\r
-(defconst yas--prefix nil\r
- "A prefix argument for expansion direct from keybindings")\r
-\r
-(defvar yas-selected-text nil\r
- "The selected region deleted on the last snippet expansion.")\r
-\r
-(defvar yas--start-column nil\r
- "The column where the snippet expansion started.")\r
-\r
-(make-variable-buffer-local 'yas--active-field-overlay)\r
-(make-variable-buffer-local 'yas--field-protection-overlays)\r
-(put 'yas--active-field-overlay 'permanent-local t)\r
-(put 'yas--field-protection-overlays 'permanent-local t)\r
-\r
-(defstruct (yas--snippet (:constructor yas--make-snippet ()))\r
- "A snippet.\r
-\r
-..."\r
- (fields '())\r
- (exit nil)\r
- (id (yas--snippet-next-id) :read-only t)\r
- (control-overlay nil)\r
- active-field\r
- ;; stacked expansion: the `previous-active-field' slot saves the\r
- ;; active field where the child expansion took place\r
- previous-active-field\r
- force-exit)\r
-\r
-(defstruct (yas--field (:constructor yas--make-field (number start end parent-field)))\r
- "A field."\r
- number\r
- start end\r
- parent-field\r
- (mirrors '())\r
- (transform nil)\r
- (modified-p nil)\r
- next)\r
-\r
-(defstruct (yas--mirror (:constructor yas--make-mirror (start end transform)))\r
- "A mirror."\r
- start end\r
- (transform nil)\r
- parent-field\r
- next)\r
-\r
-(defstruct (yas--exit (:constructor yas--make-exit (marker)))\r
- marker\r
- next)\r
-\r
-(defun yas--apply-transform (field-or-mirror field &optional empty-on-nil-p)\r
- "Calculate transformed string for FIELD-OR-MIRROR from FIELD.\r
-\r
-If there is no transform for ht field, return nil.\r
-\r
-If there is a transform but it returns nil, return the empty\r
-string iff EMPTY-ON-NIL-P is true."\r
- (let* ((yas-text (yas--field-text-for-display field))\r
- (text yas-text)\r
- (yas-modified-p (yas--field-modified-p field))\r
- (yas-moving-away-p nil)\r
- (transform (if (yas--mirror-p field-or-mirror)\r
- (yas--mirror-transform field-or-mirror)\r
- (yas--field-transform field-or-mirror)))\r
- (start-point (if (yas--mirror-p field-or-mirror)\r
- (yas--mirror-start field-or-mirror)\r
- (yas--field-start field-or-mirror)))\r
- (transformed (and transform\r
- (save-excursion\r
- (goto-char start-point)\r
- (let ((ret (yas--eval-lisp transform)))\r
- (or ret (and empty-on-nil-p "")))))))\r
- transformed))\r
-\r
-(defsubst yas--replace-all (from to &optional text)\r
- "Replace all occurance from FROM to TO.\r
-\r
-With optional string TEXT do it in that string."\r
- (if text\r
- (replace-regexp-in-string (regexp-quote from) to text t t)\r
- (goto-char (point-min))\r
- (while (search-forward from nil t)\r
- (replace-match to t t text))))\r
-\r
-(defun yas--snippet-find-field (snippet number)\r
- (find-if #'(lambda (field)\r
- (eq number (yas--field-number field)))\r
- (yas--snippet-fields snippet)))\r
-\r
-(defun yas--snippet-sort-fields (snippet)\r
- "Sort the fields of SNIPPET in navigation order."\r
- (setf (yas--snippet-fields snippet)\r
- (sort (yas--snippet-fields snippet)\r
- #'yas--snippet-field-compare)))\r
-\r
-(defun yas--snippet-field-compare (field1 field2)\r
- "Compare two fields. The field with a number is sorted first.\r
-If they both have a number, compare through the number. If neither\r
-have, compare through the field's start point"\r
- (let ((n1 (yas--field-number field1))\r
- (n2 (yas--field-number field2)))\r
- (if n1\r
- (if n2\r
- (or (zerop n2) (and (not (zerop n1))\r
- (< n1 n2)))\r
- (not (zerop n1)))\r
- (if n2\r
- (zerop n2)\r
- (< (yas--field-start field1)\r
- (yas--field-start field2))))))\r
-\r
-(defun yas--field-probably-deleted-p (snippet field)\r
- "Guess if SNIPPET's FIELD should be skipped."\r
- (and\r
- ;; field must be zero lentgh\r
- ;;\r
- (zerop (- (yas--field-start field) (yas--field-end field)))\r
- ;; skip if:\r
- (or\r
- ;; 1) is a nested field and it's been modified\r
- ;;\r
- (and (yas--field-parent-field field)\r
- (yas--field-modified-p field))\r
- ;; 2) ends just before the snippet end\r
- ;;\r
- (and (eq field (car (last (yas--snippet-fields snippet))))\r
- (= (yas--field-start field) (overlay-end (yas--snippet-control-overlay snippet)))))\r
- ;; the field numbered 0, just before the exit marker, should\r
- ;; never be skipped\r
- ;;\r
- (not (zerop (yas--field-number field)))))\r
-\r
-(defun yas--snippets-at-point (&optional all-snippets)\r
- "Return a sorted list of snippets at point, most recently\r
-inserted first."\r
- (sort\r
- (remove nil (remove-duplicates (mapcar #'(lambda (ov)\r
- (overlay-get ov 'yas--snippet))\r
- (if all-snippets\r
- (overlays-in (point-min) (point-max))\r
- (nconc (overlays-at (point)) (overlays-at (1- (point))))))))\r
- #'(lambda (s1 s2)\r
- (<= (yas--snippet-id s2) (yas--snippet-id s1)))))\r
-\r
-(defun yas-next-field-or-maybe-expand ()\r
- "Try to expand a snippet at a key before point, otherwise\r
-delegate to `yas-next-field'."\r
- (interactive)\r
- (if yas-triggers-in-field\r
- (let ((yas-fallback-behavior 'return-nil)\r
- (active-field (overlay-get yas--active-field-overlay 'yas--field)))\r
- (when active-field\r
- (unless (yas-expand-from-trigger-key active-field)\r
- (yas-next-field))))\r
- (yas-next-field)))\r
-\r
-(defun yas-next-field (&optional arg)\r
- "Navigate to next field. If there's none, exit the snippet."\r
- (interactive)\r
- (let* ((arg (or arg\r
- 1))\r
- (snippet (first (yas--snippets-at-point)))\r
- (active-field (overlay-get yas--active-field-overlay 'yas--field))\r
- (live-fields (remove-if #'(lambda (field)\r
- (and (not (eq field active-field))\r
- (yas--field-probably-deleted-p snippet field)))\r
- (yas--snippet-fields snippet)))\r
- (active-field-pos (position active-field live-fields))\r
- (target-pos (and active-field-pos (+ arg active-field-pos)))\r
- (target-field (and target-pos (nth target-pos live-fields))))\r
- ;; First check if we're moving out of a field with a transform\r
- ;;\r
- (when (and active-field\r
- (yas--field-transform active-field))\r
- (let* ((yas-moving-away-p t)\r
- (yas-text (yas--field-text-for-display active-field))\r
- (text yas-text)\r
- (yas-modified-p (yas--field-modified-p active-field)))\r
- ;; primary field transform: exit call to field-transform\r
- (yas--eval-lisp (yas--field-transform active-field))))\r
- ;; Now actually move...\r
- (cond ((and target-pos (>= target-pos (length live-fields)))\r
- (yas-exit-snippet snippet))\r
- (target-field\r
- (yas--move-to-field snippet target-field))\r
- (t\r
- nil))))\r
-\r
-(defun yas--place-overlays (snippet field)\r
- "Correctly place overlays for SNIPPET's FIELD"\r
- (yas--make-move-field-protection-overlays snippet field)\r
- (yas--make-move-active-field-overlay snippet field))\r
-\r
-(defun yas--move-to-field (snippet field)\r
- "Update SNIPPET to move to field FIELD.\r
-\r
-Also create some protection overlays"\r
- (goto-char (yas--field-start field))\r
- (yas--place-overlays snippet field)\r
- (overlay-put yas--active-field-overlay 'yas--field field)\r
- (let ((number (yas--field-number field)))\r
- ;; check for the special ${0: ...} field\r
- (if (and number (zerop number))\r
- (progn\r
- (set-mark (yas--field-end field))\r
- (setf (yas--snippet-force-exit snippet)\r
- (or (yas--field-transform field)\r
- t)))\r
- ;; make this field active\r
- (setf (yas--snippet-active-field snippet) field)\r
- ;; primary field transform: first call to snippet transform\r
- (unless (yas--field-modified-p field)\r
- (if (yas--field-update-display field snippet)\r
- (yas--update-mirrors snippet)\r
- (setf (yas--field-modified-p field) nil))))))\r
-\r
-(defun yas-prev-field ()\r
- "Navigate to prev field. If there's none, exit the snippet."\r
- (interactive)\r
- (yas-next-field -1))\r
-\r
-(defun yas-abort-snippet (&optional snippet)\r
- (interactive)\r
- (let ((snippet (or snippet\r
- (car (yas--snippets-at-point)))))\r
- (when snippet\r
- (setf (yas--snippet-force-exit snippet) t))))\r
-\r
-(defun yas-exit-snippet (snippet)\r
- "Goto exit-marker of SNIPPET."\r
- (interactive (list (first (yas--snippets-at-point))))\r
- (when snippet\r
- (setf (yas--snippet-force-exit snippet) t)\r
- (goto-char (if (yas--snippet-exit snippet)\r
- (yas--exit-marker (yas--snippet-exit snippet))\r
- (overlay-end (yas--snippet-control-overlay snippet))))))\r
-\r
-(defun yas-exit-all-snippets ()\r
- "Exit all snippets."\r
- (interactive)\r
- (mapc #'(lambda (snippet)\r
- (yas-exit-snippet snippet)\r
- (yas--check-commit-snippet))\r
- (yas--snippets-at-point 'all-snippets)))\r
-\r
-\f\r
-;;; Some low level snippet-routines:\r
-\r
-(defmacro yas--inhibit-overlay-hooks (&rest body)\r
- "Run BODY with `yas--inhibit-overlay-hooks' set to t."\r
- (declare (indent 0))\r
- `(let ((yas--inhibit-overlay-hooks t))\r
- (progn ,@body)))\r
-\r
-(defvar yas-snippet-beg nil "Beginning position of the last snippet commited.")\r
-(defvar yas-snippet-end nil "End position of the last snippet commited.")\r
-\r
-(defun yas--commit-snippet (snippet)\r
- "Commit SNIPPET, but leave point as it is. This renders the\r
-snippet as ordinary text."\r
-\r
- (let ((control-overlay (yas--snippet-control-overlay snippet)))\r
- ;;\r
- ;; Save the end of the moribund snippet in case we need to revive it\r
- ;; its original expansion.\r
- ;;\r
- (when (and control-overlay\r
- (overlay-buffer control-overlay))\r
- (setq yas-snippet-beg (overlay-start control-overlay))\r
- (setq yas-snippet-end (overlay-end control-overlay))\r
- (delete-overlay control-overlay))\r
-\r
- (yas--inhibit-overlay-hooks\r
- (when yas--active-field-overlay\r
- (delete-overlay yas--active-field-overlay))\r
- (when yas--field-protection-overlays\r
- (mapc #'delete-overlay yas--field-protection-overlays)))\r
-\r
- ;; stacked expansion: if the original expansion took place from a\r
- ;; field, make sure we advance it here at least to\r
- ;; `yas-snippet-end'...\r
- ;;\r
- (let ((previous-field (yas--snippet-previous-active-field snippet)))\r
- (when (and yas-snippet-end previous-field)\r
- (yas--advance-end-maybe previous-field yas-snippet-end)))\r
-\r
- ;; Convert all markers to points,\r
- ;;\r
- (yas--markers-to-points snippet)\r
-\r
- ;; Take care of snippet revival\r
- ;;\r
- (if yas-snippet-revival\r
- (push `(apply yas--snippet-revive ,yas-snippet-beg ,yas-snippet-end ,snippet)\r
- buffer-undo-list)\r
- ;; Dismember the snippet... this is useful if we get called\r
- ;; again from `yas--take-care-of-redo'....\r
- (setf (yas--snippet-fields snippet) nil)))\r
-\r
- (yas--message 3 "Snippet %s exited." (yas--snippet-id snippet)))\r
-\r
-(defun yas--safely-run-hooks (hook-var)\r
- (condition-case error\r
- (run-hooks hook-var)\r
- (error\r
- (yas--message 3 "%s error: %s" hook-var (error-message-string error)))))\r
-\r
-\r
-(defun yas--check-commit-snippet ()\r
- "Checks if point exited the currently active field of the\r
-snippet, if so cleans up the whole snippet up."\r
- (let* ((snippets (yas--snippets-at-point 'all-snippets))\r
- (snippets-left snippets)\r
- (snippet-exit-transform))\r
- (dolist (snippet snippets)\r
- (let ((active-field (yas--snippet-active-field snippet)))\r
- (setq snippet-exit-transform (yas--snippet-force-exit snippet))\r
- (cond ((or snippet-exit-transform\r
- (not (and active-field (yas--field-contains-point-p active-field))))\r
- (setq snippets-left (delete snippet snippets-left))\r
- (setf (yas--snippet-force-exit snippet) nil)\r
- (yas--commit-snippet snippet))\r
- ((and active-field\r
- (or (not yas--active-field-overlay)\r
- (not (overlay-buffer yas--active-field-overlay))))\r
- ;;\r
- ;; stacked expansion: this case is mainly for recent\r
- ;; snippet exits that place us back int the field of\r
- ;; another snippet\r
- ;;\r
- (save-excursion\r
- (yas--move-to-field snippet active-field)\r
- (yas--update-mirrors snippet)))\r
- (t\r
- nil))))\r
- (unless (or (null snippets) snippets-left)\r
- (if snippet-exit-transform\r
- (yas--eval-lisp-no-saves snippet-exit-transform))\r
- (yas--safely-run-hooks 'yas-after-exit-snippet-hook))))\r
-\r
-;; Apropos markers-to-points:\r
-;;\r
-;; This was found useful for performance reasons, so that an\r
-;; excessive number of live markers aren't kept around in the\r
-;; `buffer-undo-list'. However, in `markers-to-points', the\r
-;; set-to-nil markers can't simply be discarded and replaced with\r
-;; fresh ones in `points-to-markers'. The original marker that was\r
-;; just set to nil has to be reused.\r
-;;\r
-;; This shouldn't bring horrible problems with undo/redo, but it\r
-;; you never know\r
-;;\r
-(defun yas--markers-to-points (snippet)\r
- "Convert all markers in SNIPPET to a cons (POINT . MARKER)\r
-where POINT is the original position of the marker and MARKER is\r
-the original marker object with the position set to nil."\r
- (dolist (field (yas--snippet-fields snippet))\r
- (let ((start (marker-position (yas--field-start field)))\r
- (end (marker-position (yas--field-end field))))\r
- (set-marker (yas--field-start field) nil)\r
- (set-marker (yas--field-end field) nil)\r
- (setf (yas--field-start field) (cons start (yas--field-start field)))\r
- (setf (yas--field-end field) (cons end (yas--field-end field))))\r
- (dolist (mirror (yas--field-mirrors field))\r
- (let ((start (marker-position (yas--mirror-start mirror)))\r
- (end (marker-position (yas--mirror-end mirror))))\r
- (set-marker (yas--mirror-start mirror) nil)\r
- (set-marker (yas--mirror-end mirror) nil)\r
- (setf (yas--mirror-start mirror) (cons start (yas--mirror-start mirror)))\r
- (setf (yas--mirror-end mirror) (cons end (yas--mirror-end mirror))))))\r
- (let ((snippet-exit (yas--snippet-exit snippet)))\r
- (when snippet-exit\r
- (let ((exit (marker-position (yas--exit-marker snippet-exit))))\r
- (set-marker (yas--exit-marker snippet-exit) nil)\r
- (setf (yas--exit-marker snippet-exit) (cons exit (yas--exit-marker snippet-exit)))))))\r
-\r
-(defun yas--points-to-markers (snippet)\r
- "Convert all cons (POINT . MARKER) in SNIPPET to markers. This\r
-is done by setting MARKER to POINT with `set-marker'."\r
- (dolist (field (yas--snippet-fields snippet))\r
- (setf (yas--field-start field) (set-marker (cdr (yas--field-start field))\r
- (car (yas--field-start field))))\r
- (setf (yas--field-end field) (set-marker (cdr (yas--field-end field))\r
- (car (yas--field-end field))))\r
- (dolist (mirror (yas--field-mirrors field))\r
- (setf (yas--mirror-start mirror) (set-marker (cdr (yas--mirror-start mirror))\r
- (car (yas--mirror-start mirror))))\r
- (setf (yas--mirror-end mirror) (set-marker (cdr (yas--mirror-end mirror))\r
- (car (yas--mirror-end mirror))))))\r
- (let ((snippet-exit (yas--snippet-exit snippet)))\r
- (when snippet-exit\r
- (setf (yas--exit-marker snippet-exit) (set-marker (cdr (yas--exit-marker snippet-exit))\r
- (car (yas--exit-marker snippet-exit)))))))\r
-\r
-(defun yas--field-contains-point-p (field &optional point)\r
- (let ((point (or point\r
- (point))))\r
- (and (>= point (yas--field-start field))\r
- (<= point (yas--field-end field)))))\r
-\r
-(defun yas--field-text-for-display (field)\r
- "Return the propertized display text for field FIELD. "\r
- (buffer-substring (yas--field-start field) (yas--field-end field)))\r
-\r
-(defun yas--undo-in-progress ()\r
- "True if some kind of undo is in progress"\r
- (or undo-in-progress\r
- (eq this-command 'undo)\r
- (eq this-command 'redo)))\r
-\r
-(defun yas--make-control-overlay (snippet start end)\r
- "Creates the control overlay that surrounds the snippet and\r
-holds the keymap."\r
- (let ((overlay (make-overlay start\r
- end\r
- nil\r
- nil\r
- t)))\r
- (overlay-put overlay 'keymap yas-keymap)\r
- (overlay-put overlay 'priority 100)\r
- (overlay-put overlay 'yas--snippet snippet)\r
- overlay))\r
-\r
-(defun yas-skip-and-clear-or-delete-char (&optional field)\r
- "Clears unmodified field if at field start, skips to next tab.\r
-\r
-Otherwise deletes a character normally by calling `delete-char'."\r
- (interactive)\r
- (let ((field (or field\r
- (and yas--active-field-overlay\r
- (overlay-buffer yas--active-field-overlay)\r
- (overlay-get yas--active-field-overlay 'yas--field)))))\r
- (cond ((and field\r
- (not (yas--field-modified-p field))\r
- (eq (point) (marker-position (yas--field-start field))))\r
- (yas--skip-and-clear field)\r
- (yas-next-field 1))\r
- (t\r
- (call-interactively 'delete-char)))))\r
-\r
-(defun yas--skip-and-clear (field)\r
- "Deletes the region of FIELD and sets it modified state to t"\r
- ;; Just before skipping-and-clearing the field, mark its children\r
- ;; fields as modified, too. If the childen have mirrors-in-fields\r
- ;; this prevents them from updating erroneously (we're skipping and\r
- ;; deleting!).\r
- ;;\r
- (yas--mark-this-and-children-modified field)\r
- (delete-region (yas--field-start field) (yas--field-end field)))\r
-\r
-(defun yas--mark-this-and-children-modified (field)\r
- (setf (yas--field-modified-p field) t)\r
- (let ((fom (yas--field-next field)))\r
- (while (and fom\r
- (yas--fom-parent-field fom))\r
- (when (and (eq (yas--fom-parent-field fom) field)\r
- (yas--field-p fom))\r
- (yas--mark-this-and-children-modified fom))\r
- (setq fom (yas--fom-next fom)))))\r
-\r
-(defun yas--make-move-active-field-overlay (snippet field)\r
- "Place the active field overlay in SNIPPET's FIELD.\r
-\r
-Move the overlay, or create it if it does not exit."\r
- (if (and yas--active-field-overlay\r
- (overlay-buffer yas--active-field-overlay))\r
- (move-overlay yas--active-field-overlay\r
- (yas--field-start field)\r
- (yas--field-end field))\r
- (setq yas--active-field-overlay\r
- (make-overlay (yas--field-start field)\r
- (yas--field-end field)\r
- nil nil t))\r
- (overlay-put yas--active-field-overlay 'priority 100)\r
- (overlay-put yas--active-field-overlay 'face 'yas-field-highlight-face)\r
- (overlay-put yas--active-field-overlay 'yas--snippet snippet)\r
- (overlay-put yas--active-field-overlay 'modification-hooks '(yas--on-field-overlay-modification))\r
- (overlay-put yas--active-field-overlay 'insert-in-front-hooks\r
- '(yas--on-field-overlay-modification))\r
- (overlay-put yas--active-field-overlay 'insert-behind-hooks\r
- '(yas--on-field-overlay-modification))))\r
-\r
-(defvar yas--inhibit-overlay-hooks nil\r
- "Bind this temporarity to non-nil to prevent running `yas--on-*-modification'.")\r
-\r
-(defun yas--on-field-overlay-modification (overlay after? beg end &optional length)\r
- "Clears the field and updates mirrors, conditionally.\r
-\r
-Only clears the field if it hasn't been modified and it point it\r
-at field start. This hook doesn't do anything if an undo is in\r
-progress."\r
- (unless (or yas--inhibit-overlay-hooks\r
- (yas--undo-in-progress))\r
- (let* ((field (overlay-get overlay 'yas--field))\r
- (number (and field (yas--field-number field)))\r
- (snippet (overlay-get yas--active-field-overlay 'yas--snippet)))\r
- (cond (after?\r
- (yas--advance-end-maybe field (overlay-end overlay))\r
- (save-excursion\r
- (yas--field-update-display field snippet))\r
- (yas--update-mirrors snippet))\r
- (field\r
- (when (and (not after?)\r
- (not (yas--field-modified-p field))\r
- (eq (point) (if (markerp (yas--field-start field))\r
- (marker-position (yas--field-start field))\r
- (yas--field-start field))))\r
- (yas--skip-and-clear field))\r
- (setf (yas--field-modified-p field) t))))))\r
-\f\r
-;;; Apropos protection overlays:\r
-;;\r
-;; These exist for nasty users who will try to delete parts of the\r
-;; snippet outside the active field. Actual protection happens in\r
-;; `yas--on-protection-overlay-modification'.\r
-;;\r
-;; Currently this signals an error which inhibits the command. For\r
-;; commands that move point (like `kill-line'), point is restored in\r
-;; the `yas--post-command-handler' using a global\r
-;; `yas--protection-violation' variable.\r
-;;\r
-;; Alternatively, I've experimented with an implementation that\r
-;; commits the snippet before actually calling `this-command'\r
-;; interactively, and then signals an eror, which is ignored. but\r
-;; blocks all other million modification hooks. This presented some\r
-;; problems with stacked expansion.\r
-;;\r
-(defun yas--make-move-field-protection-overlays (snippet field)\r
- "Place protection overlays surrounding SNIPPET's FIELD.\r
-\r
-Move the overlays, or create them if they do not exit."\r
- (let ((start (yas--field-start field))\r
- (end (yas--field-end field)))\r
- ;; First check if the (1+ end) is contained in the buffer,\r
- ;; otherwise we'll have to do a bit of cheating and silently\r
- ;; insert a newline. the `(1+ (buffer-size))' should prevent this\r
- ;; when using stacked expansion\r
- ;;\r
- (when (< (buffer-size) end)\r
- (save-excursion\r
- (yas--inhibit-overlay-hooks\r
- (goto-char (point-max))\r
- (newline))))\r
- ;; go on to normal overlay creation/moving\r
- ;;\r
- (cond ((and yas--field-protection-overlays\r
- (every #'overlay-buffer yas--field-protection-overlays))\r
- (move-overlay (first yas--field-protection-overlays) (1- start) start)\r
- (move-overlay (second yas--field-protection-overlays) end (1+ end)))\r
- (t\r
- (setq yas--field-protection-overlays\r
- (list (make-overlay (1- start) start nil t nil)\r
- (make-overlay end (1+ end) nil t nil)))\r
- (dolist (ov yas--field-protection-overlays)\r
- (overlay-put ov 'face 'yas--field-debug-face)\r
- (overlay-put ov 'yas--snippet snippet)\r
- ;; (overlay-put ov 'evaporate t)\r
- (overlay-put ov 'modification-hooks '(yas--on-protection-overlay-modification)))))))\r
-\r
-(defvar yas--protection-violation nil\r
- "When non-nil, signals attempts to erronesly exit or modify the snippet.\r
-\r
-Functions in the `post-command-hook', for example\r
-`yas--post-command-handler' can check it and reset its value to\r
-nil. The variables value is the point where the violation\r
-originated")\r
-\r
-(defun yas--on-protection-overlay-modification (overlay after? beg end &optional length)\r
- "Signals a snippet violation, then issues error.\r
-\r
-The error should be ignored in `debug-ignored-errors'"\r
- (unless yas--inhibit-overlay-hooks\r
- (cond ((not (or after?\r
- (yas--undo-in-progress)))\r
- (setq yas--protection-violation (point))\r
- (error "Exit the snippet first!")))))\r
-\r
-(add-to-list 'debug-ignored-errors "^Exit the snippet first!$")\r
-\r
-\f\r
-;; Snippet expansion and "stacked" expansion:\r
-;;\r
-;; Stacked expansion is when you try to expand a snippet when already\r
-;; inside a snippet expansion.\r
-;;\r
-;; The parent snippet does not run its fields modification hooks\r
-;; (`yas--on-field-overlay-modification' and\r
-;; `yas--on-protection-overlay-modification') while the child snippet\r
-;; is active. This means, among other things, that the mirrors of the\r
-;; parent snippet are not updated, this only happening when one exits\r
-;; the child snippet.\r
-;;\r
-;; Unfortunately, this also puts some ugly (and not fully-tested)\r
-;; bits of code in `yas-expand-snippet' and\r
-;; `yas--commit-snippet'. I've tried to mark them with "stacked\r
-;; expansion:".\r
-;;\r
-;; This was thought to be safer in in an undo/redo perpective, but\r
-;; maybe the correct implementation is to make the globals\r
-;; `yas--active-field-overlay' and `yas--field-protection-overlays' be\r
-;; snippet-local and be active even while the child snippet is\r
-;; running. This would mean a lot of overlay modification hooks\r
-;; running, but if managed correctly (including overlay priorities)\r
-;; they should account for all situations...\r
-;;\r
-(defun yas-expand-snippet (content &optional start end expand-env)\r
- "Expand snippet CONTENT at current point.\r
-\r
-Text between START and END will be deleted before inserting\r
-template. EXPAND-ENV is are let-style variable to value bindings\r
-considered when expanding the snippet."\r
- (run-hooks 'yas-before-expand-snippet-hook)\r
-\r
- ;;\r
- (let* ((yas-selected-text (or yas-selected-text\r
- (and (region-active-p)\r
- (buffer-substring-no-properties (region-beginning)\r
- (region-end)))))\r
- (start (or start\r
- (and (region-active-p)\r
- (region-beginning))\r
- (point)))\r
- (end (or end\r
- (and (region-active-p)\r
- (region-end))\r
- (point)))\r
- (to-delete (and start\r
- end\r
- (buffer-substring-no-properties start end)))\r
- snippet)\r
- (goto-char start)\r
- (setq yas--indent-original-column (current-column))\r
- ;; Delete the region to delete, this *does* get undo-recorded.\r
- ;;\r
- (when (and to-delete\r
- (> end start))\r
- (delete-region start end))\r
-\r
- (cond ((listp content)\r
- ;; x) This is a snippet-command\r
- ;;\r
- (yas--eval-lisp-no-saves content))\r
- (t\r
- ;; x) This is a snippet-snippet :-)\r
- ;;\r
- ;; Narrow the region down to the content, shoosh the\r
- ;; `buffer-undo-list', and create the snippet, the new\r
- ;; snippet updates its mirrors once, so we are left with\r
- ;; some plain text. The undo action for deleting this\r
- ;; plain text will get recorded at the end.\r
- ;;\r
- ;; stacked expansion: also shoosh the overlay modification hooks\r
- (save-restriction\r
- (narrow-to-region start start)\r
- (let ((buffer-undo-list t))\r
- ;; snippet creation might evaluate users elisp, which\r
- ;; might generate errors, so we have to be ready to catch\r
- ;; them mostly to make the undo information\r
- ;;\r
- (setq yas--start-column (save-restriction (widen) (current-column)))\r
- (yas--inhibit-overlay-hooks\r
- (setq snippet\r
- (if expand-env\r
- (eval `(let* ,expand-env\r
- (insert content)\r
- (yas--snippet-create (point-min) (point-max))))\r
- (insert content)\r
- (yas--snippet-create (point-min) (point-max)))))))\r
-\r
- ;; stacked-expansion: This checks for stacked expansion, save the\r
- ;; `yas--previous-active-field' and advance its boudary.\r
- ;;\r
- (let ((existing-field (and yas--active-field-overlay\r
- (overlay-buffer yas--active-field-overlay)\r
- (overlay-get yas--active-field-overlay 'yas--field))))\r
- (when existing-field\r
- (setf (yas--snippet-previous-active-field snippet) existing-field)\r
- (yas--advance-end-maybe existing-field (overlay-end yas--active-field-overlay))))\r
-\r
- ;; Exit the snippet immediately if no fields\r
- ;;\r
- (unless (yas--snippet-fields snippet)\r
- (yas-exit-snippet snippet))\r
-\r
- ;; Push two undo actions: the deletion of the inserted contents of\r
- ;; the new snippet (without the "key") followed by an apply of\r
- ;; `yas--take-care-of-redo' on the newly inserted snippet boundaries\r
- ;;\r
- ;; A small exception, if `yas-also-auto-indent-first-line'\r
- ;; is t and `yas--indent' decides to indent the line to a\r
- ;; point before the actual expansion point, undo would be\r
- ;; messed up. We call the early point "newstart"". case,\r
- ;; and attempt to fix undo.\r
- ;;\r
- (let ((newstart (overlay-start (yas--snippet-control-overlay snippet)))\r
- (end (overlay-end (yas--snippet-control-overlay snippet))))\r
- (when (< newstart start)\r
- (push (cons (make-string (- start newstart) ? ) newstart) buffer-undo-list))\r
- (push (cons newstart end) buffer-undo-list)\r
- (push `(apply yas--take-care-of-redo ,start ,end ,snippet)\r
- buffer-undo-list))\r
- ;; Now, schedule a move to the first field\r
- ;;\r
- (let ((first-field (car (yas--snippet-fields snippet))))\r
- (when first-field\r
- (sit-for 0) ;; fix issue 125\r
- (yas--move-to-field snippet first-field)))\r
- (yas--message 3 "snippet expanded.")\r
- t))))\r
-\r
-(defun yas--take-care-of-redo (beg end snippet)\r
- "Commits SNIPPET, which in turn pushes an undo action for\r
-reviving it.\r
-\r
-Meant to exit in the `buffer-undo-list'."\r
- ;; slightly optimize: this action is only needed for snippets with\r
- ;; at least one field\r
- (when (yas--snippet-fields snippet)\r
- (yas--commit-snippet snippet)))\r
-\r
-(defun yas--snippet-revive (beg end snippet)\r
- "Revives the SNIPPET and creates a control overlay from BEG to\r
-END.\r
-\r
-BEG and END are, we hope, the original snippets boudaries. All\r
-the markers/points exiting existing inside SNIPPET should point\r
-to their correct locations *at the time the snippet is revived*.\r
-\r
-After revival, push the `yas--take-care-of-redo' in the\r
-`buffer-undo-list'"\r
- ;; Reconvert all the points to markers\r
- ;;\r
- (yas--points-to-markers snippet)\r
- ;; When at least one editable field existed in the zombie snippet,\r
- ;; try to revive the whole thing...\r
- ;;\r
- (let ((target-field (or (yas--snippet-active-field snippet)\r
- (car (yas--snippet-fields snippet)))))\r
- (when target-field\r
- (setf (yas--snippet-control-overlay snippet) (yas--make-control-overlay snippet beg end))\r
- (overlay-put (yas--snippet-control-overlay snippet) 'yas--snippet snippet)\r
-\r
- (yas--move-to-field snippet target-field)\r
-\r
- (push `(apply yas--take-care-of-redo ,beg ,end ,snippet)\r
- buffer-undo-list))))\r
-\r
-(defun yas--snippet-create (begin end)\r
- "Creates a snippet from an template inserted between BEGIN and END.\r
-\r
-Returns the newly created snippet."\r
- (let ((snippet (yas--make-snippet)))\r
- (goto-char begin)\r
- (yas--snippet-parse-create snippet)\r
-\r
- ;; Sort and link each field\r
- (yas--snippet-sort-fields snippet)\r
-\r
- ;; Create keymap overlay for snippet\r
- (setf (yas--snippet-control-overlay snippet)\r
- (yas--make-control-overlay snippet (point-min) (point-max)))\r
-\r
- ;; Move to end\r
- (goto-char (point-max))\r
-\r
- snippet))\r
-\r
-\f\r
-;;; Apropos adjacencies and "fom's":\r
-;;\r
-;; Once the $-constructs bits like "$n" and "${:n" are deleted in the\r
-;; recently expanded snippet, we might actually have many fields,\r
-;; mirrors (and the snippet exit) in the very same position in the\r
-;; buffer. Therefore we need to single-link the\r
-;; fields-or-mirrors-or-exit (which I have abbreviated to "fom")\r
-;; according to their original positions in the buffer.\r
-;;\r
-;; Then we have operation `yas--advance-end-maybe' and\r
-;; `yas--advance-start-maybe', which conditionally push the starts and\r
-;; ends of these foms down the chain.\r
-;;\r
-;; This allows for like the printf with the magic ",":\r
-;;\r
-;; printf ("${1:%s}\\n"${1:$(if (string-match "%" text) "," "\);")} \\r
-;; $2${1:$(if (string-match "%" text) "\);" "")}$0\r
-;;\r
-(defun yas--fom-start (fom)\r
- (cond ((yas--field-p fom)\r
- (yas--field-start fom))\r
- ((yas--mirror-p fom)\r
- (yas--mirror-start fom))\r
- (t\r
- (yas--exit-marker fom))))\r
-\r
-(defun yas--fom-end (fom)\r
- (cond ((yas--field-p fom)\r
- (yas--field-end fom))\r
- ((yas--mirror-p fom)\r
- (yas--mirror-end fom))\r
- (t\r
- (yas--exit-marker fom))))\r
-\r
-(defun yas--fom-next (fom)\r
- (cond ((yas--field-p fom)\r
- (yas--field-next fom))\r
- ((yas--mirror-p fom)\r
- (yas--mirror-next fom))\r
- (t\r
- (yas--exit-next fom))))\r
-\r
-(defun yas--fom-parent-field (fom)\r
- (cond ((yas--field-p fom)\r
- (yas--field-parent-field fom))\r
- ((yas--mirror-p fom)\r
- (yas--mirror-parent-field fom))\r
- (t\r
- nil)))\r
-\r
-(defun yas--calculate-adjacencies (snippet)\r
- "Calculate adjacencies for fields or mirrors of SNIPPET.\r
-\r
-This is according to their relative positions in the buffer, and\r
-has to be called before the $-constructs are deleted."\r
- (flet ((yas--fom-set-next-fom (fom nextfom)\r
- (cond ((yas--field-p fom)\r
- (setf (yas--field-next fom) nextfom))\r
- ((yas--mirror-p fom)\r
- (setf (yas--mirror-next fom) nextfom))\r
- (t\r
- (setf (yas--exit-next fom) nextfom))))\r
- (yas--compare-fom-begs (fom1 fom2)\r
- (if (= (yas--fom-start fom2) (yas--fom-start fom1))\r
- (yas--mirror-p fom2)\r
- (>= (yas--fom-start fom2) (yas--fom-start fom1))))\r
- (yas--link-foms (fom1 fom2)\r
- (yas--fom-set-next-fom fom1 fom2)))\r
- ;; make some yas--field, yas--mirror and yas--exit soup\r
- (let ((soup))\r
- (when (yas--snippet-exit snippet)\r
- (push (yas--snippet-exit snippet) soup))\r
- (dolist (field (yas--snippet-fields snippet))\r
- (push field soup)\r
- (dolist (mirror (yas--field-mirrors field))\r
- (push mirror soup)))\r
- (setq soup\r
- (sort soup\r
- #'yas--compare-fom-begs))\r
- (when soup\r
- (reduce #'yas--link-foms soup)))))\r
-\r
-(defun yas--calculate-mirrors-in-fields (snippet mirror)\r
- "Attempt to assign a parent field of SNIPPET to the mirror MIRROR.\r
-\r
-Use the tighest containing field if more than one field contains\r
-the mirror. Intended to be called *before* the dollar-regions are\r
-deleted."\r
- (let ((min (point-min))\r
- (max (point-max)))\r
- (dolist (field (yas--snippet-fields snippet))\r
- (when (and (<= (yas--field-start field) (yas--mirror-start mirror))\r
- (<= (yas--mirror-end mirror) (yas--field-end field))\r
- (< min (yas--field-start field))\r
- (< (yas--field-end field) max))\r
- (setq min (yas--field-start field)\r
- max (yas--field-end field))\r
- (setf (yas--mirror-parent-field mirror) field)))))\r
-\r
-(defun yas--advance-end-maybe (fom newend)\r
- "Maybe advance FOM's end to NEWEND if it needs it.\r
-\r
-If it does, also:\r
-\r
-* call `yas--advance-start-maybe' on FOM's next fom.\r
-\r
-* in case FOM is field call `yas--advance-end-maybe' on its parent\r
- field\r
-\r
-Also, if FOM is an exit-marker, always call\r
-`yas--advance-start-maybe' on its next fom. This is beacuse\r
-exit-marker have identical start and end markers.\r
-\r
-"\r
- (cond ((and fom (< (yas--fom-end fom) newend))\r
- (set-marker (yas--fom-end fom) newend)\r
- (yas--advance-start-maybe (yas--fom-next fom) newend)\r
- (yas--advance-end-of-parents-maybe (yas--fom-parent-field fom) newend))\r
- ((yas--exit-p fom)\r
- (yas--advance-start-maybe (yas--fom-next fom) newend))))\r
-\r
-(defun yas--advance-start-maybe (fom newstart)\r
- "Maybe advance FOM's start to NEWSTART if it needs it.\r
-\r
-If it does, also call `yas--advance-end-maybe' on FOM."\r
- (when (and fom (< (yas--fom-start fom) newstart))\r
- (set-marker (yas--fom-start fom) newstart)\r
- (yas--advance-end-maybe fom newstart)))\r
-\r
-(defun yas--advance-end-of-parents-maybe (field newend)\r
- "Like `yas--advance-end-maybe' but for parent fields.\r
-\r
-Only works for fields and doesn't care about the start of the\r
-next FOM. Works its way up recursively for parents of parents."\r
- (when (and field\r
- (< (yas--field-end field) newend))\r
- (set-marker (yas--field-end field) newend)\r
- (yas--advance-end-of-parents-maybe (yas--field-parent-field field) newend)))\r
-\r
-(defvar yas--dollar-regions nil\r
- "When expanding the snippet the \"parse-create\" functions add\r
- cons cells to this var")\r
-\r
-(defvar yas--backquote-markers-and-strings nil\r
- "List of (MARKER . STRING) marking where the the values\r
- from backquoted lisp expressions should be inserted at the end of\r
- expansion" )\r
-\r
-(defun yas--snippet-parse-create (snippet)\r
- "Parse a recently inserted snippet template, creating all\r
-necessary fields, mirrors and exit points.\r
-\r
-Meant to be called in a narrowed buffer, does various passes"\r
- (let ((parse-start (point)))\r
- ;; Reset the yas--dollar-regions\r
- ;;\r
- (setq yas--dollar-regions nil)\r
- ;; protect just the backquotes\r
- ;;\r
- (yas--protect-escapes nil '(?`))\r
- ;; replace all backquoted expressions\r
- ;;\r
- (goto-char parse-start)\r
- (yas--save-backquotes)\r
- ;; protect escaped characters\r
- ;;\r
- (yas--protect-escapes)\r
- ;; parse fields with {}\r
- ;;\r
- (goto-char parse-start)\r
- (yas--field-parse-create snippet)\r
- ;; parse simple mirrors and fields\r
- ;;\r
- (goto-char parse-start)\r
- (yas--simple-mirror-parse-create snippet)\r
- ;; parse mirror transforms\r
- ;;\r
- (goto-char parse-start)\r
- (yas--transform-mirror-parse-create snippet)\r
- ;; calculate adjacencies of fields and mirrors\r
- ;;\r
- (yas--calculate-adjacencies snippet)\r
- ;; Delete $-constructs\r
- ;;\r
- (yas--delete-regions yas--dollar-regions)\r
- ;; restore backquoted expression values\r
- ;;\r
- (yas--restore-backquotes)\r
- ;; restore escapes\r
- ;;\r
- (goto-char parse-start)\r
- (yas--restore-escapes)\r
- ;; update mirrors for the first time\r
- ;;\r
- (yas--update-mirrors snippet)\r
- ;; indent the best we can\r
- ;;\r
- (goto-char parse-start)\r
- (yas--indent snippet)))\r
-\r
-(defun yas--indent-according-to-mode (snippet-markers)\r
- "Indent current line according to mode, preserving\r
-SNIPPET-MARKERS."\r
- ;;; Apropos indenting problems....\r
- ;;\r
- ;; `indent-according-to-mode' uses whatever `indent-line-function'\r
- ;; is available. Some implementations of these functions delete text\r
- ;; before they insert. If there happens to be a marker just after\r
- ;; the text being deleted, the insertion actually happens after the\r
- ;; marker, which misplaces it.\r
- ;;\r
- ;; This would also happen if we had used overlays with the\r
- ;; `front-advance' property set to nil.\r
- ;;\r
- ;; This is why I have these `trouble-markers', they are the ones at\r
- ;; they are the ones at the first non-whitespace char at the line\r
- ;; (i.e. at `yas--real-line-beginning'. After indentation takes place\r
- ;; we should be at the correct to restore them to. All other\r
- ;; non-trouble-markers have been *pushed* and don't need special\r
- ;; attention.\r
- ;;\r
- (goto-char (yas--real-line-beginning))\r
- (let ((trouble-markers (remove-if-not #'(lambda (marker)\r
- (= marker (point)))\r
- snippet-markers)))\r
- (save-restriction\r
- (widen)\r
- (condition-case err\r
- (indent-according-to-mode)\r
- (error (yas--message 3 "Warning: `yas--indent-according-to-mode' having problems running %s" indent-line-function)\r
- nil)))\r
- (mapc #'(lambda (marker)\r
- (set-marker marker (point)))\r
- trouble-markers)))\r
-\r
-(defvar yas--indent-original-column nil)\r
-(defun yas--indent (snippet)\r
- (let ((snippet-markers (yas--collect-snippet-markers snippet)))\r
- ;; Look for those $>\r
- (save-excursion\r
- (while (re-search-forward "$>" nil t)\r
- (delete-region (match-beginning 0) (match-end 0))\r
- (when (not (eq yas-indent-line 'auto))\r
- (yas--indent-according-to-mode snippet-markers))))\r
- ;; Now do stuff for 'fixed and 'auto\r
- (save-excursion\r
- (cond ((eq yas-indent-line 'fixed)\r
- (while (and (zerop (forward-line))\r
- (zerop (current-column)))\r
- (indent-to-column yas--indent-original-column)))\r
- ((eq yas-indent-line 'auto)\r
- (let ((end (set-marker (make-marker) (point-max)))\r
- (indent-first-line-p yas-also-auto-indent-first-line))\r
- (while (and (zerop (if indent-first-line-p\r
- (prog1\r
- (forward-line 0)\r
- (setq indent-first-line-p nil))\r
- (forward-line 1)))\r
- (not (eobp))\r
- (<= (point) end))\r
- (yas--indent-according-to-mode snippet-markers))))\r
- (t\r
- nil)))))\r
-\r
-(defun yas--collect-snippet-markers (snippet)\r
- "Make a list of all the markers used by SNIPPET."\r
- (let (markers)\r
- (dolist (field (yas--snippet-fields snippet))\r
- (push (yas--field-start field) markers)\r
- (push (yas--field-end field) markers)\r
- (dolist (mirror (yas--field-mirrors field))\r
- (push (yas--mirror-start mirror) markers)\r
- (push (yas--mirror-end mirror) markers)))\r
- (let ((snippet-exit (yas--snippet-exit snippet)))\r
- (when (and snippet-exit\r
- (marker-buffer (yas--exit-marker snippet-exit)))\r
- (push (yas--exit-marker snippet-exit) markers)))\r
- markers))\r
-\r
-(defun yas--real-line-beginning ()\r
- (let ((c (char-after (line-beginning-position)))\r
- (n (line-beginning-position)))\r
- (while (or (eql c ?\ )\r
- (eql c ?\t))\r
- (incf n)\r
- (setq c (char-after n)))\r
- n))\r
-\r
-(defun yas--escape-string (escaped)\r
- (concat "YASESCAPE" (format "%d" escaped) "PROTECTGUARD"))\r
-\r
-(defun yas--protect-escapes (&optional text escaped)\r
- "Protect all escaped characters with their numeric ASCII value.\r
-\r
-With optional string TEXT do it in string instead of buffer."\r
- (let ((changed-text text)\r
- (text-provided-p text))\r
- (mapc #'(lambda (escaped)\r
- (setq changed-text\r
- (yas--replace-all (concat "\\" (char-to-string escaped))\r
- (yas--escape-string escaped)\r
- (when text-provided-p changed-text))))\r
- (or escaped yas--escaped-characters))\r
- changed-text))\r
-\r
-(defun yas--restore-escapes (&optional text escaped)\r
- "Restore all escaped characters from their numeric ASCII value.\r
-\r
-With optional string TEXT do it in string instead of the buffer."\r
- (let ((changed-text text)\r
- (text-provided-p text))\r
- (mapc #'(lambda (escaped)\r
- (setq changed-text\r
- (yas--replace-all (yas--escape-string escaped)\r
- (char-to-string escaped)\r
- (when text-provided-p changed-text))))\r
- (or escaped yas--escaped-characters))\r
- changed-text))\r
-\r
-(defun yas--save-backquotes ()\r
- "Save all the \"`(lisp-expression)`\"-style expression\r
-with their evaluated value into `yas--backquote-markers-and-strings'"\r
- (while (re-search-forward yas--backquote-lisp-expression-regexp nil t)\r
- (let ((current-string (match-string-no-properties 1)) transformed)\r
- (delete-region (match-beginning 0) (match-end 0))\r
- (setq transformed (yas--eval-lisp (yas--read-lisp (yas--restore-escapes current-string '(?`)))))\r
- (goto-char (match-beginning 0))\r
- (when transformed\r
- (let ((marker (make-marker)))\r
- (insert "Y") ;; quite horrendous, I love it :)\r
- (set-marker marker (point))\r
- (insert "Y")\r
- (push (cons marker transformed) yas--backquote-markers-and-strings))))))\r
-\r
-(defun yas--restore-backquotes ()\r
- "Replace all the markers in\r
-`yas--backquote-markers-and-strings' with their values"\r
- (while yas--backquote-markers-and-strings\r
- (let* ((marker-and-string (pop yas--backquote-markers-and-strings))\r
- (marker (car marker-and-string))\r
- (string (cdr marker-and-string)))\r
- (save-excursion\r
- (goto-char marker)\r
- (delete-char -1)\r
- (insert string)\r
- (delete-char 1)\r
- (set-marker marker nil)))))\r
-\r
-(defun yas--scan-sexps (from count)\r
- (condition-case err\r
- (with-syntax-table (standard-syntax-table)\r
- (scan-sexps from count))\r
- (error\r
- nil)))\r
-\r
-(defun yas--make-marker (pos)\r
- "Create a marker at POS with `nil' `marker-insertion-type'"\r
- (let ((marker (set-marker (make-marker) pos)))\r
- (set-marker-insertion-type marker nil)\r
- marker))\r
-\r
-(defun yas--field-parse-create (snippet &optional parent-field)\r
- "Parse most field expressions, except for the simple one \"$n\".\r
-\r
-The following count as a field:\r
-\r
-* \"${n: text}\", for a numbered field with default text, as long as N is not 0;\r
-\r
-* \"${n: text$(expression)}, the same with a lisp expression;\r
- this is caught with the curiously named `yas--multi-dollar-lisp-expression-regexp'\r
-\r
-* the same as above but unnumbered, (no N:) and number is calculated automatically.\r
-\r
-When multiple expressions are found, only the last one counts."\r
- ;;\r
- (save-excursion\r
- (while (re-search-forward yas--field-regexp nil t)\r
- (let* ((real-match-end-0 (yas--scan-sexps (1+ (match-beginning 0)) 1))\r
- (number (and (match-string-no-properties 1)\r
- (string-to-number (match-string-no-properties 1))))\r
- (brand-new-field (and real-match-end-0\r
- ;; break if on "$(" immediately\r
- ;; after the ":", this will be\r
- ;; caught as a mirror with\r
- ;; transform later.\r
- (not (save-match-data\r
- (eq (string-match "$[ \t\n]*("\r
- (match-string-no-properties 2)) 0)))\r
- ;; allow ${0: some exit text}\r
- ;; (not (and number (zerop number)))\r
- (yas--make-field number\r
- (yas--make-marker (match-beginning 2))\r
- (yas--make-marker (1- real-match-end-0))\r
- parent-field))))\r
- (when brand-new-field\r
- (goto-char real-match-end-0)\r
- (push (cons (1- real-match-end-0) real-match-end-0)\r
- yas--dollar-regions)\r
- (push (cons (match-beginning 0) (match-beginning 2))\r
- yas--dollar-regions)\r
- (push brand-new-field (yas--snippet-fields snippet))\r
- (save-excursion\r
- (save-restriction\r
- (narrow-to-region (yas--field-start brand-new-field) (yas--field-end brand-new-field))\r
- (goto-char (point-min))\r
- (yas--field-parse-create snippet brand-new-field)))))))\r
- ;; if we entered from a parent field, now search for the\r
- ;; `yas--multi-dollar-lisp-expression-regexp'. THis is used for\r
- ;; primary field transformations\r
- ;;\r
- (when parent-field\r
- (save-excursion\r
- (while (re-search-forward yas--multi-dollar-lisp-expression-regexp nil t)\r
- (let* ((real-match-end-1 (yas--scan-sexps (match-beginning 1) 1)))\r
- ;; commit the primary field transformation if:\r
- ;;\r
- ;; 1. we don't find it in yas--dollar-regions (a subnested\r
- ;; field) might have already caught it.\r
- ;;\r
- ;; 2. we really make sure we have either two '$' or some\r
- ;; text and a '$' after the colon ':'. This is a FIXME: work\r
- ;; my regular expressions and end these ugly hacks.\r
- ;;\r
- (when (and real-match-end-1\r
- (not (member (cons (match-beginning 0)\r
- real-match-end-1)\r
- yas--dollar-regions))\r
- (not (eq ?:\r
- (char-before (1- (match-beginning 1))))))\r
- (let ((lisp-expression-string (buffer-substring-no-properties (match-beginning 1)\r
- real-match-end-1)))\r
- (setf (yas--field-transform parent-field)\r
- (yas--read-lisp (yas--restore-escapes lisp-expression-string))))\r
- (push (cons (match-beginning 0) real-match-end-1)\r
- yas--dollar-regions)))))))\r
-\r
-(defun yas--transform-mirror-parse-create (snippet)\r
- "Parse the \"${n:$(lisp-expression)}\" mirror transformations."\r
- (while (re-search-forward yas--transform-mirror-regexp nil t)\r
- (let* ((real-match-end-0 (yas--scan-sexps (1+ (match-beginning 0)) 1))\r
- (number (string-to-number (match-string-no-properties 1)))\r
- (field (and number\r
- (not (zerop number))\r
- (yas--snippet-find-field snippet number)))\r
- (brand-new-mirror\r
- (and real-match-end-0\r
- field\r
- (yas--make-mirror (yas--make-marker (match-beginning 0))\r
- (yas--make-marker (match-beginning 0))\r
- (yas--read-lisp\r
- (yas--restore-escapes\r
- (buffer-substring-no-properties (match-beginning 2)\r
- (1- real-match-end-0))))))))\r
- (when brand-new-mirror\r
- (push brand-new-mirror\r
- (yas--field-mirrors field))\r
- (yas--calculate-mirrors-in-fields snippet brand-new-mirror)\r
- (push (cons (match-beginning 0) real-match-end-0) yas--dollar-regions)))))\r
-\r
-(defun yas--simple-mirror-parse-create (snippet)\r
- "Parse the simple \"$n\" fields/mirrors/exitmarkers."\r
- (while (re-search-forward yas--simple-mirror-regexp nil t)\r
- (let ((number (string-to-number (match-string-no-properties 1))))\r
- (cond ((zerop number)\r
-\r
- (setf (yas--snippet-exit snippet)\r
- (yas--make-exit (yas--make-marker (match-end 0))))\r
- (save-excursion\r
- (goto-char (match-beginning 0))\r
- (when yas-wrap-around-region\r
- (cond (yas-selected-text\r
- (insert yas-selected-text))\r
- ((and (eq yas-wrap-around-region 'cua)\r
- cua-mode\r
- (get-register ?0))\r
- (insert (prog1 (get-register ?0)\r
- (set-register ?0 nil))))))\r
- (push (cons (point) (yas--exit-marker (yas--snippet-exit snippet)))\r
- yas--dollar-regions)))\r
- (t\r
- (let ((field (yas--snippet-find-field snippet number)))\r
- (if field\r
- (let ((brand-new-mirror (yas--make-mirror\r
- (yas--make-marker (match-beginning 0))\r
- (yas--make-marker (match-beginning 0))\r
- nil)))\r
- (push brand-new-mirror\r
- (yas--field-mirrors field))\r
- (yas--calculate-mirrors-in-fields snippet brand-new-mirror))\r
- (push (yas--make-field number\r
- (yas--make-marker (match-beginning 0))\r
- (yas--make-marker (match-beginning 0))\r
- nil)\r
- (yas--snippet-fields snippet))))\r
- (push (cons (match-beginning 0) (match-end 0))\r
- yas--dollar-regions))))))\r
-\r
-(defun yas--delete-regions (regions)\r
- "Sort disjuct REGIONS by start point, then delete from the back."\r
- (mapc #'(lambda (reg)\r
- (delete-region (car reg) (cdr reg)))\r
- (sort regions\r
- #'(lambda (r1 r2)\r
- (>= (car r1) (car r2))))))\r
-\r
-(defun yas--update-mirrors (snippet)\r
- "Updates all the mirrors of SNIPPET."\r
- (save-excursion\r
- (let* ((fields (copy-list (yas--snippet-fields snippet)))\r
- (field (car fields)))\r
- (while field\r
- (dolist (mirror (yas--field-mirrors field))\r
- (let ((mirror-parent-field (yas--mirror-parent-field mirror)))\r
- ;; updatte this mirror\r
- ;;\r
- (yas--mirror-update-display mirror field)\r
- ;; for mirrors-in-fields: schedule a possible\r
- ;; parent field for reupdting later on\r
- ;;\r
- (when mirror-parent-field\r
- (add-to-list 'fields mirror-parent-field 'append #'eq))\r
- ;; `yas--place-overlays' is needed if the active field and\r
- ;; protected overlays have been changed because of insertions\r
- ;; in `yas--mirror-update-display'\r
- ;;\r
- (when (eq field (yas--snippet-active-field snippet))\r
- (yas--place-overlays snippet field))))\r
- (setq fields (cdr fields))\r
- (setq field (car fields))))))\r
-\r
-(defun yas--mirror-update-display (mirror field)\r
- "Update MIRROR according to FIELD (and mirror transform)."\r
-\r
- (let* ((mirror-parent-field (yas--mirror-parent-field mirror))\r
- (reflection (and (not (and mirror-parent-field\r
- (yas--field-modified-p mirror-parent-field)))\r
- (or (yas--apply-transform mirror field 'empty-on-nil)\r
- (yas--field-text-for-display field)))))\r
- (when (and reflection\r
- (not (string= reflection (buffer-substring-no-properties (yas--mirror-start mirror)\r
- (yas--mirror-end mirror)))))\r
- (goto-char (yas--mirror-start mirror))\r
- (yas--inhibit-overlay-hooks\r
- (insert reflection))\r
- (if (> (yas--mirror-end mirror) (point))\r
- (delete-region (point) (yas--mirror-end mirror))\r
- (set-marker (yas--mirror-end mirror) (point))\r
- (yas--advance-start-maybe (yas--mirror-next mirror) (point))\r
- ;; super-special advance\r
- (yas--advance-end-of-parents-maybe mirror-parent-field (point))))))\r
-\r
-(defun yas--field-update-display (field snippet)\r
- "Much like `yas--mirror-update-display', but for fields"\r
- (when (yas--field-transform field)\r
- (let ((transformed (and (not (eq (yas--field-number field) 0))\r
- (yas--apply-transform field field)))\r
- (point (point)))\r
- (when (and transformed\r
- (not (string= transformed (buffer-substring-no-properties (yas--field-start field)\r
- (yas--field-end field)))))\r
- (setf (yas--field-modified-p field) t)\r
- (goto-char (yas--field-start field))\r
- (yas--inhibit-overlay-hooks\r
- (insert transformed)\r
- (if (> (yas--field-end field) (point))\r
- (delete-region (point) (yas--field-end field))\r
- (set-marker (yas--field-end field) (point))\r
- (yas--advance-start-maybe (yas--field-next field) (point)))\r
- t)))))\r
-\r
-\f\r
-;;; Post-command hook:\r
-\r
-(defun yas--post-command-handler ()\r
- "Handles various yasnippet conditions after each command."\r
- (cond (yas--protection-violation\r
- (goto-char yas--protection-violation)\r
- (setq yas--protection-violation nil))\r
- ((eq 'undo this-command)\r
- ;;\r
- ;; After undo revival the correct field is sometimes not\r
- ;; restored correctly, this condition handles that\r
- ;;\r
- (let* ((snippet (car (yas--snippets-at-point)))\r
- (target-field (and snippet\r
- (find-if-not #'(lambda (field)\r
- (yas--field-probably-deleted-p snippet field))\r
- (remove nil\r
- (cons (yas--snippet-active-field snippet)\r
- (yas--snippet-fields snippet)))))))\r
- (when target-field\r
- (yas--move-to-field snippet target-field))))\r
- ((not (yas--undo-in-progress))\r
- ;; When not in an undo, check if we must commit the snippet\r
- ;; (user exited it).\r
- (yas--check-commit-snippet))))\r
-\f\r
-;;; Fancy docs:\r
-;;\r
-;; The docstrings for some functions are generated dynamically\r
-;; depending on the context.\r
-;;\r
-(put 'yas-expand 'function-documentation\r
- '(yas--expand-from-trigger-key-doc))\r
-(defun yas--expand-from-trigger-key-doc ()\r
- "A doc synthethizer for `yas--expand-from-trigger-key-doc'."\r
- (let ((fallback-description\r
- (cond ((eq yas-fallback-behavior 'call-other-command)\r
- (let* ((yas-minor-mode nil)\r
- (fallback (key-binding (read-kbd-macro (yas--trigger-key-for-fallback)))))\r
- (or (and fallback\r
- (format " call command `%s'." (pp-to-string fallback)))\r
- " do nothing.")))\r
- ((eq yas-fallback-behavior 'return-nil)\r
- ", do nothing.")\r
- (t\r
- ", defer to `yas--fallback-behaviour' :-)"))))\r
- (concat "Expand a snippet before point. If no snippet\r
-expansion is possible,"\r
- fallback-description\r
- "\n\nOptional argument FIELD is for non-interactive use and is an\r
-object satisfying `yas--field-p' to restrict the expansion to.")))\r
-\r
-(put 'yas-expand-from-keymap 'function-documentation '(yas--expand-from-keymap-doc))\r
-(defun yas--expand-from-keymap-doc ()\r
- "A doc synthethizer for `yas--expand-from-keymap-doc'."\r
- (add-hook 'temp-buffer-show-hook 'yas--snippet-description-finish-runonce)\r
- (concat "Expand/run snippets from keymaps, possibly falling back to original binding.\n"\r
- (when (eq this-command 'describe-key)\r
- (let* ((vec (this-single-command-keys))\r
- (templates (mapcan #'(lambda (table)\r
- (yas--fetch table vec))\r
- (yas--get-snippet-tables)))\r
- (yas--direct-keymaps nil)\r
- (fallback (key-binding vec)))\r
- (concat "In this case, "\r
- (when templates\r
- (concat "these snippets are bound to this key:\n"\r
- (yas--template-pretty-list templates)\r
- "\n\nIf none of these expands, "))\r
- (or (and fallback\r
- (format "fallback `%s' will be called." (pp-to-string fallback)))\r
- "no fallback keybinding is called."))))))\r
-\r
-(defun yas--template-pretty-list (templates)\r
- (let ((acc)\r
- (yas-buffer-local-condition 'always))\r
- (dolist (plate templates)\r
- (setq acc (concat acc "\n*) "\r
- (propertize (concat "\\\\snippet `" (car plate) "'")\r
- 'yasnippet (cdr plate)))))\r
- acc))\r
-\r
-(define-button-type 'help-snippet-def\r
- :supertype 'help-xref\r
- 'help-function (lambda (template) (yas--visit-snippet-file-1 template))\r
- 'help-echo (purecopy "mouse-2, RET: find snippets's definition"))\r
-\r
-(defun yas--snippet-description-finish-runonce ()\r
- "Final adjustments for the help buffer when snippets are concerned."\r
- (yas--create-snippet-xrefs)\r
- (remove-hook 'temp-buffer-show-hook 'yas--snippet-description-finish-runonce))\r
-\r
-(defun yas--create-snippet-xrefs ()\r
- (save-excursion\r
- (goto-char (point-min))\r
- (while (search-forward-regexp "\\\\\\\\snippet[ \s\t]+`\\([^']+\\)'" nil t)\r
- (let ((template (get-text-property (match-beginning 1)\r
- 'yasnippet)))\r
- (when template\r
- (help-xref-button 1 'help-snippet-def template)\r
- (kill-region (match-end 1) (match-end 0))\r
- (kill-region (match-beginning 0) (match-beginning 1)))))))\r
-\r
-(defun yas--expand-uuid (mode-symbol uuid &optional start end expand-env)\r
- "Expand a snippet registered in MODE-SYMBOL's table with UUID.\r
-\r
-Remaining args as in `yas-expand-snippet'."\r
- (let* ((table (gethash mode-symbol yas--tables))\r
- (yas--current-template (and table\r
- (gethash uuid (yas--table-uuidhash table)))))\r
- (when yas--current-template\r
- (yas-expand-snippet (yas--template-content yas--current-template)))))\r
-\f\r
-;;; Utils\r
-\r
-(defvar yas-verbosity 4\r
- "Log level for `yas--message' 4 means trace most anything, 0 means nothing.")\r
-\r
-(defun yas--message (level message &rest args)\r
- (when (> yas-verbosity level)\r
- (message (apply #'yas--format message args))))\r
-\r
-(defun yas--format (format-control &rest format-args)\r
- (apply #'format (concat "[yas] " format-control) format-args))\r
-\r
-\f\r
-;;; Some hacks:\r
-;;\r
-;; The functions\r
-;;\r
-;; `locate-dominating-file'\r
-;; `region-active-p'\r
-;;\r
-;; added for compatibility in emacsen < 23\r
-(unless (>= emacs-major-version 23)\r
- (unless (fboundp 'region-active-p)\r
- (defun region-active-p () (and transient-mark-mode mark-active)))\r
-\r
- (unless (fboundp 'locate-dominating-file)\r
- (defvar locate-dominating-stop-dir-regexp\r
- "\\`\\(?:[\\/][\\/][^\\/]+[\\/]\\|/\\(?:net\\|afs\\|\\.\\.\\.\\)/\\)\\'"\r
- "Regexp of directory names which stop the search in `locate-dominating-file'.\r
-Any directory whose name matches this regexp will be treated like\r
-a kind of root directory by `locate-dominating-file' which will stop its search\r
-when it bumps into it.\r
-The default regexp prevents fruitless and time-consuming attempts to find\r
-special files in directories in which filenames are interpreted as hostnames,\r
-or mount points potentially requiring authentication as a different user.")\r
-\r
- (defun locate-dominating-file (file name)\r
- "Look up the directory hierarchy from FILE for a file named NAME.\r
-Stop at the first parent directory containing a file NAME,\r
-and return the directory. Return nil if not found."\r
- ;; We used to use the above locate-dominating-files code, but the\r
- ;; directory-files call is very costly, so we're much better off doing\r
- ;; multiple calls using the code in here.\r
- ;;\r
- ;; Represent /home/luser/foo as ~/foo so that we don't try to look for\r
- ;; `name' in /home or in /.\r
- (setq file (abbreviate-file-name file))\r
- (let ((root nil)\r
- (prev-file file)\r
- ;; `user' is not initialized outside the loop because\r
- ;; `file' may not exist, so we may have to walk up part of the\r
- ;; hierarchy before we find the "initial UUID".\r
- (user nil)\r
- try)\r
- (while (not (or root\r
- (null file)\r
- ;; FIXME: Disabled this heuristic because it is sometimes\r
- ;; inappropriate.\r
- ;; As a heuristic, we stop looking up the hierarchy of\r
- ;; directories as soon as we find a directory belonging\r
- ;; to another user. This should save us from looking in\r
- ;; things like /net and /afs. This assumes that all the\r
- ;; files inside a project belong to the same user.\r
- ;; (let ((prev-user user))\r
- ;; (setq user (nth 2 (file-attributes file)))\r
- ;; (and prev-user (not (equal user prev-user))))\r
- (string-match locate-dominating-stop-dir-regexp file)))\r
- (setq try (file-exists-p (expand-file-name name file)))\r
- (cond (try (setq root file))\r
- ((equal file (setq prev-file file\r
- file (file-name-directory\r
- (directory-file-name file))))\r
- (setq file nil))))\r
- root))))\r
-\r
-;; `c-neutralize-syntax-in-CPP` sometimes fires "End of Buffer" error\r
-;; (when it execute forward-char) and interrupt the after change\r
-;; hook. Thus prevent the insert-behind hook of yasnippet to be\r
-;; invoked. Here's a way to reproduce it:\r
-\r
-;; # open a *new* Emacs.\r
-;; # load yasnippet.\r
-;; # open a *new* .cpp file.\r
-;; # input "inc" and press TAB to expand the snippet.\r
-;; # select the `#include <...>` snippet.\r
-;; # type inside `<>`\r
-\r
-(defadvice c-neutralize-syntax-in-CPP\r
- (around yas--mp/c-neutralize-syntax-in-CPP activate)\r
- "Adviced `c-neutralize-syntax-in-CPP' to properly\r
-handle the end-of-buffer error fired in it by calling\r
-`forward-char' at the end of buffer."\r
- (condition-case err\r
- ad-do-it\r
- (error (message (error-message-string err)))))\r
-\r
-;; disable c-electric-* serial command in YAS fields\r
-(add-hook 'c-mode-common-hook\r
- '(lambda ()\r
- (dolist (k '(":" ">" ";" "<" "{" "}"))\r
- (define-key (symbol-value (make-local-variable 'yas-keymap))\r
- k 'self-insert-command))))\r
-\f\r
-;;; Backward compatibility to to yasnippet <= 0.7\r
-(defvar yas--exported-syms '(;; `defcustom's\r
- ;;\r
- yas-snippet-dirs\r
- yas-prompt-functions\r
- yas-indent-line\r
- yas-also-auto-indent-first-line\r
- yas-snippet-revival\r
- yas-trigger-key\r
- yas-next-field-key\r
- yas-prev-field-key\r
- yas-skip-and-clear-key\r
- yas-triggers-in-field\r
- yas-fallback-behavior\r
- yas-choose-keys-first\r
- yas-choose-tables-first\r
- yas-use-menu\r
- yas-trigger-symbol\r
- yas-wrap-around-region\r
- yas-good-grace\r
- yas-visit-from-menu\r
- yas-expand-only-for-last-commands\r
- yas-field-highlight-face\r
-\r
- ;; these vars can be customized as well\r
- ;;\r
- yas-keymap\r
- yas-verbosity\r
- yas-extra-modes\r
- yas-key-syntaxes\r
- yas-after-exit-snippet-hook\r
- yas-before-expand-snippet-hook\r
- yas-buffer-local-condition\r
-\r
- ;; prompting functions\r
- ;;\r
- yas-x-prompt\r
- yas-ido-prompt\r
- yas-no-prompt\r
- yas-completing-prompt\r
- yas-dropdown-prompt\r
-\r
- ;; interactive functions\r
- ;;\r
- yas-expand\r
- yas-minor-mode\r
- yas-global-mode\r
- yas-direct-keymaps-reload\r
- yas-minor-mode-on\r
- yas-load-directory\r
- yas-reload-all\r
- yas-compile-directory\r
- yas-recompile-all\r
- yas-about\r
- yas-expand-from-trigger-key\r
- yas-expand-from-keymap\r
- yas-insert-snippet\r
- yas-visit-snippet-file\r
- yas-new-snippet\r
- yas-load-snippet-buffer\r
- yas-tryout-snippet\r
- yas-describe-tables\r
- yas-next-field-or-maybe-expand\r
- yas-next-field\r
- yas-prev-field\r
- yas-abort-snippet\r
- yas-exit-snippet\r
- yas-exit-all-snippets\r
- yas-skip-and-clear-or-delete-char\r
-\r
- ;; symbols that I "exported" for use\r
- ;; in snippets and hookage\r
- ;;\r
- yas-expand-snippet\r
- yas-define-snippets\r
- yas-define-menu\r
- yas-snippet-beg\r
- yas-snippet-end\r
- yas-modified-p\r
- yas-moving-away-p\r
- yas-text\r
- yas-substr\r
- yas-choose-value\r
- yas-key-to-value\r
- yas-throw\r
- yas-verify-value\r
- yas-field-value\r
- yas-text\r
- yas-selected-text\r
- yas-default-from-field\r
- yas-inside-string\r
- yas-unimplemented\r
- yas-define-condition-cache\r
- yas-hippie-try-expand\r
-\r
- ;; debug definitions\r
- ;; yas-debug-snippet-vars\r
- ;; yas-exterminate-package\r
- ;; yas-debug-test\r
-\r
- ;; testing definitions\r
- ;; yas-should-expand\r
- ;; yas-should-not-expand\r
- ;; yas-mock-insert\r
- ;; yas-make-file-or-dirs\r
- ;; yas-variables\r
- ;; yas-saving-variables\r
- ;; yas-call-with-snippet-dirs\r
- ;; yas-with-snippet-dirs\r
-))\r
-\r
-(dolist (sym yas--exported-syms)\r
- (let ((backported (intern (replace-regexp-in-string "^yas-" "yas/" (symbol-name sym)))))\r
- (when (boundp sym)\r
- (make-obsolete-variable backported sym "yasnippet 0.8")\r
- (defvaralias backported sym))\r
- (when (fboundp sym)\r
- (make-obsolete backported sym "yasnippet 0.8")\r
- (defalias backported sym))))\r
-\r
-\f\r
-(provide 'yasnippet)\r
-\r
-;;; yasnippet.el ends here\r
-;; Local Variables:\r
-;; coding: utf-8\r
-;; End:\r
+;;; 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)
+
+\f
+;;; 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")))
+
+(defconst yas--default-user-snippets-dir
+ (concat user-emacs-directory "snippets"))
+
+(defcustom yas-snippet-dirs (remove nil
+ (list yas--default-user-snippets-dir
+ '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)
+
+\f
+;;; 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'.")
+
+\f
+;;; 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))
+
+\f
+;;; 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 (&optional mode)
+ "Compute list of mode symbols that are active for `yas-expand'
+and friends."
+ (let* ((explored (if mode (list mode) ; Building up list in reverse.
+ (cons major-mode (reverse yas--extra-modes))))
+ (dfs
+ (lambda (mode)
+ (cl-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))
+ do (push neighbour explored)
+ (funcall dfs neighbour)))))
+ (mapcar dfs explored)
+ (nreverse explored)))
+
+(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)
+
+\f
+;;; 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 ]*"))
+
+
+\f
+;;; Internal structs for template management
+
+(cl-defstruct (yas--template
+ (:constructor yas--make-template)
+ ;; Handles `yas-define-snippets' format, plus the
+ ;; initial TABLE argument.
+ (:constructor
+ yas--define-snippets-2
+ (table
+ key content
+ &optional xname condition group
+ expand-env load-file xkeybinding xuuid save-file
+ &aux
+ (name (or xname
+ ;; A little redundant: we always get a name
+ ;; from `yas--parse-template' except when
+ ;; there isn't a file.
+ (and load-file (file-name-nondirectory load-file))
+ (and save-file (file-name-nondirectory save-file))
+ key))
+ (keybinding (yas--read-keybinding xkeybinding))
+ (uuid (or xuuid name))
+ (old (gethash uuid (yas--table-uuidhash table)))
+ (menu-binding-pair
+ (and old (yas--template-menu-binding-pair old)))
+ (perm-group
+ (and old (yas--template-perm-group old))))))
+ "A template for a snippet."
+ key
+ content
+ name
+ condition
+ expand-env
+ load-file
+ save-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
+;;
+;; keyhash namehashes(3) yas--template structs(4)
+;; -----------------------------------------------------
+;; __________
+;; / \
+;; "foo" ---> "The Foo" ---> [yas--template A] |
+;; "Mrs Foo" ---> [yas--template B] |
+;; |
+;; [C-c M-l] ---> "The Foo" -------------------------/
+;; "The Bar" ---> [yas--template C]
+;;
+;; "baz" ---> "Baz" ---> [yas--template D]
+;;
+;; Additionally, since uuid defaults to the name, we have a
+;; `yas--table-uuidhash' for TABLE
+;;
+;; uuidhash yas--template structs
+;; -------------------------------
+;; "The Foo" ---> [yas--template A]
+;; "Mrs Foo" ---> [yas--template B]
+;; "The Bar" ---> [yas--template C]
+;; "Baz" ---> [yas--template D]
+;;
+;; FIXME: the more I look at this data-structure the more I think I'm
+;; stupid. There has to be an easier way (but beware lots of code
+;; depends on this).
+;;
+(defun yas--remove-template-by-uuid (table uuid)
+ "Remove from TABLE a template identified by UUID."
+ (let ((template (gethash uuid (yas--table-uuidhash table))))
+ (when template
+ (let* ((name (yas--template-name template))
+ (empty-keys nil))
+ ;; Remove the name from each of the targeted namehashes
+ ;;
+ (maphash #'(lambda (k v)
+ (let ((template (gethash name v)))
+ (when (and template
+ (eq uuid (yas--template-uuid template)))
+ (remhash name v)
+ (when (zerop (hash-table-count v))
+ (push k empty-keys)))))
+ (yas--table-hash table))
+ ;; Remove the namehash themselves if they've become empty
+ ;;
+ (dolist (key empty-keys)
+ (when (vectorp key)
+ (define-key (yas--table-direct-keymap table) key nil))
+ (remhash key (yas--table-hash table)))
+
+ ;; Finally, remove the uuid from the uuidhash
+ ;;
+ (remhash uuid (yas--table-uuidhash table))))))
+
+(defun yas--add-template (table template)
+ "Store in TABLE the snippet template TEMPLATE.
+
+KEY can be a string (trigger key) of a vector (direct
+keybinding)."
+ (let ((name (yas--template-name template))
+ (key (yas--template-key template))
+ (keybinding (yas--template-keybinding template))
+ (_menu-binding-pair (yas--template-menu-binding-pair-get-create template)))
+ (dolist (k (remove nil (list key keybinding)))
+ (puthash name
+ template
+ (or (gethash k
+ (yas--table-hash table))
+ (puthash k
+ (make-hash-table :test 'equal)
+ (yas--table-hash table))))
+ (when (vectorp k)
+ (define-key (yas--table-direct-keymap table) k 'yas-expand-from-keymap)))
+
+ ;; Update TABLE's `yas--table-uuidhash'
+ (puthash (yas--template-uuid template)
+ template
+ (yas--table-uuidhash table))))
+
+(defun yas--update-template (table template)
+ "Add or update TEMPLATE in TABLE.
+
+Also takes care of adding and updating to the associated menu.
+Return TEMPLATE."
+ ;; Remove from table by uuid
+ ;;
+ (yas--remove-template-by-uuid table (yas--template-uuid template))
+ ;; Add to table again
+ ;;
+ (yas--add-template table template)
+ ;; Take care of the menu
+ ;;
+ (yas--update-template-menu table template)
+ template)
+
+(defun yas--update-template-menu (table template)
+ "Update every menu-related for TEMPLATE."
+ (let ((menu-binding-pair (yas--template-menu-binding-pair-get-create template))
+ (key (yas--template-key template))
+ (keybinding (yas--template-keybinding template)))
+ ;; The snippet might have changed name or keys, so update
+ ;; user-visible strings
+ ;;
+ (unless (eq (cdr menu-binding-pair) :none)
+ ;; the menu item name
+ ;;
+ (setf (cadar menu-binding-pair) (yas--template-name template))
+ ;; the :keys information (also visible to the user)
+ (setf (getf (cdr (car menu-binding-pair)) :keys)
+ (or (and keybinding (key-description keybinding))
+ (and key (concat key yas-trigger-symbol))))))
+ (unless (yas--template-menu-managed-by-yas-define-menu template)
+ (let ((menu-keymap
+ (yas--menu-keymap-get-create (yas--table-mode table)
+ (mapcar #'yas--table-mode
+ (yas--table-parents table))))
+ (group (yas--template-group template)))
+ ;; Remove from menu keymap
+ ;;
+ (assert menu-keymap)
+ (yas--delete-from-keymap menu-keymap (yas--template-uuid template))
+
+ ;; Add necessary subgroups as necessary.
+ ;;
+ (dolist (subgroup group)
+ (let ((subgroup-keymap (lookup-key menu-keymap (vector (make-symbol subgroup)))))
+ (unless (and subgroup-keymap
+ (keymapp subgroup-keymap))
+ (setq subgroup-keymap (make-sparse-keymap))
+ (define-key menu-keymap (vector (make-symbol subgroup))
+ `(menu-item ,subgroup ,subgroup-keymap)))
+ (setq menu-keymap subgroup-keymap)))
+
+ ;; Add this entry to the keymap
+ ;;
+ (define-key menu-keymap
+ (vector (make-symbol (yas--template-uuid template)))
+ (car (yas--template-menu-binding-pair template))))))
+
+(defun yas--namehash-templates-alist (namehash)
+ "Return NAMEHASH as an alist."
+ (let (alist)
+ (maphash #'(lambda (k v)
+ (push (cons k v) alist))
+ namehash)
+ alist))
+
+(defun yas--fetch (table key)
+ "Fetch templates in TABLE by KEY.
+
+Return a list of cons (NAME . TEMPLATE) where NAME is a
+string and TEMPLATE is a `yas--template' structure."
+ (let* ((keyhash (yas--table-hash table))
+ (namehash (and keyhash (gethash key keyhash))))
+ (when namehash
+ (yas--filter-templates-by-condition (yas--namehash-templates-alist namehash)))))
+
+\f
+;;; Filtering/condition logic
+
+(defun yas--eval-condition (condition)
+ (condition-case err
+ (save-excursion
+ (save-restriction
+ (save-match-data
+ (eval condition))))
+ (error (progn
+ (yas--message 1 "Error in condition evaluation: %s" (error-message-string err))
+ nil))))
+
+
+(defun yas--filter-templates-by-condition (templates)
+ "Filter the templates using the applicable condition.
+
+TEMPLATES is a list of cons (NAME . TEMPLATE) where NAME is a
+string and TEMPLATE is a `yas--template' structure.
+
+This function implements the rules described in
+`yas-buffer-local-condition'. See that variables documentation."
+ (let ((requirement (yas--require-template-specific-condition-p)))
+ (if (eq requirement 'always)
+ templates
+ (remove-if-not #'(lambda (pair)
+ (yas--template-can-expand-p
+ (yas--template-condition (cdr pair)) requirement))
+ templates))))
+
+(defun yas--require-template-specific-condition-p ()
+ "Decide if this buffer requests/requires snippet-specific
+conditions to filter out potential expansions."
+ (if (eq 'always yas-buffer-local-condition)
+ 'always
+ (let ((local-condition (or (and (consp yas-buffer-local-condition)
+ (yas--eval-condition yas-buffer-local-condition))
+ yas-buffer-local-condition)))
+ (when local-condition
+ (if (eq local-condition t)
+ t
+ (and (consp local-condition)
+ (eq 'require-snippet-condition (car local-condition))
+ (symbolp (cdr local-condition))
+ (cdr local-condition)))))))
+
+(defun yas--template-can-expand-p (condition requirement)
+ "Evaluate CONDITION and REQUIREMENT and return a boolean."
+ (let* ((result (or (null condition)
+ (yas--eval-condition condition))))
+ (cond ((eq requirement t)
+ result)
+ (t
+ (eq requirement result)))))
+
+(defun yas--table-templates (table)
+ (when table
+ (let ((acc (list)))
+ (maphash #'(lambda (_key namehash)
+ (maphash #'(lambda (name template)
+ (push (cons name template) acc))
+ namehash))
+ (yas--table-hash table))
+ (yas--filter-templates-by-condition acc))))
+
+(defun yas--templates-for-key-at-point ()
+ "Find `yas--template' objects for any trigger keys preceding point.
+Returns (TEMPLATES START END). This function respects
+`yas-key-syntaxes', which see."
+ (save-excursion
+ (let ((original (point))
+ (methods yas-key-syntaxes)
+ (templates)
+ (method))
+ (while (and methods
+ (not templates))
+ (unless (eq method (car methods))
+ ;; TRICKY: `eq'-ness test means we can only be here if
+ ;; `method' is a function that returned `again', and hence
+ ;; don't revert back to original position as per
+ ;; `yas-key-syntaxes'.
+ (goto-char original))
+ (setq method (car methods))
+ (cond ((stringp method)
+ (skip-syntax-backward method)
+ (setq methods (cdr methods)))
+ ((functionp method)
+ (unless (eq (funcall method original)
+ 'again)
+ (setq methods (cdr methods))))
+ (t
+ (setq methods (cdr methods))
+ (yas--warning "Invalid element `%s' in `yas-key-syntaxes'" method)))
+ (let ((possible-key (buffer-substring-no-properties (point) original)))
+ (save-excursion
+ (goto-char original)
+ (setq templates
+ (mapcan #'(lambda (table)
+ (yas--fetch table possible-key))
+ (yas--get-snippet-tables))))))
+ (when templates
+ (list templates (point) original)))))
+
+(defun yas--table-all-keys (table)
+ "Get trigger keys of all active snippets in TABLE."
+ (let ((acc))
+ (maphash #'(lambda (key namehash)
+ (when (yas--filter-templates-by-condition (yas--namehash-templates-alist namehash))
+ (push key acc)))
+ (yas--table-hash table))
+ acc))
+
+(defun yas--table-mode (table)
+ (intern (yas--table-name table)))
+
+\f
+;;; Internal functions and macros:
+
+(defun yas--handle-error (err)
+ "Handle error depending on value of `yas-good-grace'."
+ (let ((msg (yas--format "elisp error: %s" (error-message-string err))))
+ (if yas-good-grace msg
+ (error "%s" msg))))
+
+(defun yas--eval-lisp (form)
+ "Evaluate FORM and convert the result to string."
+ (let ((retval (catch 'yas--exception
+ (condition-case err
+ (save-excursion
+ (save-restriction
+ (save-match-data
+ (widen)
+ (let ((result (eval form)))
+ (when result
+ (format "%s" result))))))
+ (error (yas--handle-error err))))))
+ (when (and (consp retval)
+ (eq 'yas--exception (car retval)))
+ (error (cdr retval)))
+ retval))
+
+(defun yas--eval-lisp-no-saves (form)
+ (condition-case err
+ (eval form)
+ (error (message "%s" (yas--handle-error err)))))
+
+(defun yas--read-lisp (string &optional nil-on-error)
+ "Read STRING as a elisp expression and return it.
+
+In case STRING in an invalid expression and NIL-ON-ERROR is nil,
+return an expression that when evaluated will issue an error."
+ (condition-case err
+ (read string)
+ (error (and (not nil-on-error)
+ `(error (error-message-string ,err))))))
+
+(defun yas--read-keybinding (keybinding)
+ "Read KEYBINDING as a snippet keybinding, return a vector."
+ (when (and keybinding
+ (not (string-match "keybinding" keybinding)))
+ (condition-case err
+ (let ((res (or (and (string-match "^\\[.*\\]$" keybinding)
+ (read keybinding))
+ (read-kbd-macro keybinding 'need-vector))))
+ res)
+ (error
+ (yas--message 3 "warning: keybinding \"%s\" invalid since %s."
+ keybinding (error-message-string err))
+ nil))))
+
+(defun yas--table-get-create (mode)
+ "Get or create the snippet table corresponding to MODE."
+ (let ((table (gethash mode
+ yas--tables)))
+ (unless table
+ (setq table (yas--make-snippet-table (symbol-name mode)))
+ (puthash mode table yas--tables)
+ (push (cons (intern (format "yas--direct-%s" mode))
+ (yas--table-direct-keymap table))
+ yas--direct-keymaps))
+ table))
+
+(defun yas--get-snippet-tables (&optional mode)
+ "Get snippet tables for MODE.
+
+MODE defaults to the current buffer's `major-mode'.
+
+Return a list of `yas--table' objects. The list of modes to
+consider is returned by `yas--modes-to-activate'"
+ (remove nil
+ (mapcar #'(lambda (name)
+ (gethash name yas--tables))
+ (yas--modes-to-activate mode))))
+
+(defun yas--menu-keymap-get-create (mode &optional parents)
+ "Get or create the menu keymap for MODE and its PARENTS.
+
+This may very well create a plethora of menu keymaps and arrange
+them all in `yas--menu-table'"
+ (let* ((menu-keymap (or (gethash mode yas--menu-table)
+ (puthash mode (make-sparse-keymap) yas--menu-table))))
+ (mapc #'yas--menu-keymap-get-create parents)
+ (define-key yas--minor-mode-menu (vector mode)
+ `(menu-item ,(symbol-name mode) ,menu-keymap
+ :visible (yas--show-menu-p ',mode)))
+ menu-keymap))
+
+\f
+;;; Template-related and snippet loading functions
+
+(defun yas--parse-template (&optional file)
+ "Parse the template in the current buffer.
+
+Optional FILE is the absolute file name of the file being
+parsed.
+
+Optional GROUP is the group where the template is to go,
+otherwise we attempt to calculate it from FILE.
+
+Return a snippet-definition, i.e. a list
+
+ (KEY TEMPLATE NAME CONDITION GROUP VARS LOAD-FILE KEYBINDING UUID)
+
+If the buffer contains a line of \"# --\" then the contents above
+this line are ignored. Directives can set most of these with the syntax:
+
+# directive-name : directive-value
+
+Here's a list of currently recognized directives:
+
+ * type
+ * name
+ * contributor
+ * condition
+ * group
+ * key
+ * expand-env
+ * binding
+ * uuid"
+ (goto-char (point-min))
+ (let* ((type 'snippet)
+ (name (and file
+ (file-name-nondirectory file)))
+ (key nil)
+ template
+ bound
+ condition
+ (group (and file
+ (yas--calculate-group file)))
+ expand-env
+ binding
+ uuid)
+ (if (re-search-forward "^# --\n" nil t)
+ (progn (setq template
+ (buffer-substring-no-properties (point)
+ (point-max)))
+ (setq bound (point))
+ (goto-char (point-min))
+ (while (re-search-forward "^# *\\([^ ]+?\\) *: *\\(.*?\\)[[:space:]]*$" bound t)
+ (when (string= "uuid" (match-string-no-properties 1))
+ (setq uuid (match-string-no-properties 2)))
+ (when (string= "type" (match-string-no-properties 1))
+ (setq type (if (string= "command" (match-string-no-properties 2))
+ 'command
+ 'snippet)))
+ (when (string= "key" (match-string-no-properties 1))
+ (setq key (match-string-no-properties 2)))
+ (when (string= "name" (match-string-no-properties 1))
+ (setq name (match-string-no-properties 2)))
+ (when (string= "condition" (match-string-no-properties 1))
+ (setq condition (yas--read-lisp (match-string-no-properties 2))))
+ (when (string= "group" (match-string-no-properties 1))
+ (setq group (match-string-no-properties 2)))
+ (when (string= "expand-env" (match-string-no-properties 1))
+ (setq expand-env (yas--read-lisp (match-string-no-properties 2)
+ 'nil-on-error)))
+ (when (string= "binding" (match-string-no-properties 1))
+ (setq binding (match-string-no-properties 2)))))
+ (setq template
+ (buffer-substring-no-properties (point-min) (point-max))))
+ (unless (or key binding)
+ (setq key (and file (file-name-nondirectory file))))
+ (when (eq type 'command)
+ (setq template (yas--read-lisp (concat "(progn" template ")"))))
+ (when group
+ (setq group (split-string group "\\.")))
+ (list key template name condition group expand-env file binding uuid)))
+
+(defun yas--calculate-group (file)
+ "Calculate the group for snippet file path FILE."
+ (let* ((dominating-dir (locate-dominating-file file
+ ".yas-make-groups"))
+ (extra-path (and dominating-dir
+ (replace-regexp-in-string (concat "^"
+ (expand-file-name dominating-dir))
+ ""
+ (expand-file-name file))))
+ (extra-dir (and extra-path
+ (file-name-directory extra-path)))
+ (group (and extra-dir
+ (replace-regexp-in-string "/"
+ "."
+ (directory-file-name extra-dir)))))
+ group))
+
+(defun yas--subdirs (directory &optional filep)
+ "Return subdirs or files of DIRECTORY according to FILEP."
+ (remove-if (lambda (file)
+ (or (string-match "^\\."
+ (file-name-nondirectory file))
+ (string-match "^#.*#$"
+ (file-name-nondirectory file))
+ (string-match "~$"
+ (file-name-nondirectory file))
+ (if filep
+ (file-directory-p file)
+ (not (file-directory-p file)))))
+ (directory-files directory t)))
+
+(defun yas--make-menu-binding (template)
+ (let ((mode (yas--table-mode (yas--template-table template))))
+ `(lambda () (interactive) (yas--expand-or-visit-from-menu ',mode ,(yas--template-uuid template)))))
+
+(defun yas--expand-or-visit-from-menu (mode uuid)
+ (let* ((table (yas--table-get-create mode))
+ (yas--current-template (and table
+ (gethash uuid (yas--table-uuidhash table)))))
+ (when yas--current-template
+ (if yas-visit-from-menu
+ (yas--visit-snippet-file-1 yas--current-template)
+ (let ((where (if (region-active-p)
+ (cons (region-beginning) (region-end))
+ (cons (point) (point)))))
+ (yas-expand-snippet (yas--template-content yas--current-template)
+ (car where)
+ (cdr where)
+ (yas--template-expand-env yas--current-template)))))))
+
+(defun yas--key-from-desc (text)
+ "Return a yasnippet key from a description string TEXT."
+ (replace-regexp-in-string "\\(\\w+\\).*" "\\1" text))
+
+\f
+;;; Popping up for keys and templates
+
+(defun yas--prompt-for-template (templates &optional prompt)
+ "Interactively choose a template from the list TEMPLATES.
+
+TEMPLATES is a list of `yas--template'.
+
+Optional PROMPT sets the prompt to use."
+ (when templates
+ (setq templates
+ (sort templates #'(lambda (t1 t2)
+ (< (length (yas--template-name t1))
+ (length (yas--template-name t2))))))
+ (some #'(lambda (fn)
+ (funcall fn (or prompt "Choose a snippet: ")
+ templates
+ #'yas--template-name))
+ yas-prompt-functions)))
+
+(defun yas--prompt-for-keys (keys &optional prompt)
+ "Interactively choose a template key from the list KEYS.
+
+Optional PROMPT sets the prompt to use."
+ (when keys
+ (some #'(lambda (fn)
+ (funcall fn (or prompt "Choose a snippet key: ") keys))
+ yas-prompt-functions)))
+
+(defun yas--prompt-for-table (tables &optional prompt)
+ "Interactively choose a table from the list TABLES.
+
+Optional PROMPT sets the prompt to use."
+ (when tables
+ (some #'(lambda (fn)
+ (funcall fn (or prompt "Choose a snippet table: ")
+ tables
+ #'yas--table-name))
+ yas-prompt-functions)))
+
+(defun yas-x-prompt (prompt choices &optional display-fn)
+ "Display choices in a x-window prompt."
+ (when (and window-system choices)
+ ;; Let window position be recalculated to ensure that
+ ;; `posn-at-point' returns non-nil.
+ (redisplay)
+ (or
+ (x-popup-menu
+ (if (fboundp 'posn-at-point)
+ (let ((x-y (posn-x-y (posn-at-point (point)))))
+ (list (list (+ (car x-y) 10)
+ (+ (cdr x-y) 20))
+ (selected-window)))
+ t)
+ `(,prompt ("title"
+ ,@(mapcar* (lambda (c d) `(,(concat " " d) . ,c))
+ choices
+ (if display-fn (mapcar display-fn choices) choices)))))
+ (keyboard-quit))))
+
+(defun yas-ido-prompt (prompt choices &optional display-fn)
+ (when (and (fboundp 'ido-completing-read)
+ (or (>= emacs-major-version 24)
+ ido-mode))
+ (yas-completing-prompt prompt choices display-fn #'ido-completing-read)))
+
+(defun yas-dropdown-prompt (_prompt choices &optional display-fn)
+ (when (fboundp 'dropdown-list)
+ (let* ((formatted-choices
+ (if display-fn (mapcar display-fn choices) choices))
+ (n (dropdown-list formatted-choices)))
+ (if n (nth n choices)
+ (keyboard-quit)))))
+
+(defun yas-completing-prompt (prompt choices &optional display-fn completion-fn)
+ (let* ((formatted-choices
+ (if display-fn (mapcar display-fn choices) choices))
+ (chosen (funcall (or completion-fn #'completing-read)
+ prompt formatted-choices
+ nil 'require-match nil nil)))
+ (if (eq choices formatted-choices)
+ chosen
+ (nth (or (position chosen formatted-choices :test #'string=) 0)
+ choices))))
+
+(defun yas-no-prompt (_prompt choices &optional _display-fn)
+ (first choices))
+
+\f
+;;; Defining snippets
+;; This consists of creating and registering `yas--template' objects in the
+;; correct tables.
+;;
+
+(defvar yas--creating-compiled-snippets nil)
+
+(defun yas--define-snippets-1 (snippet snippet-table)
+ "Helper for `yas-define-snippets'."
+ ;; Update the appropriate table. Also takes care of adding the
+ ;; key indicators in the templates menu entry, if any.
+ (yas--update-template
+ snippet-table (apply #'yas--define-snippets-2 snippet-table snippet)))
+
+(defun yas-define-snippets (mode snippets)
+ "Define SNIPPETS for MODE.
+
+SNIPPETS is a list of snippet definitions, each taking the
+following form
+
+ (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV LOAD-FILE KEYBINDING UUID SAVE-FILE)
+
+Within these, only KEY and TEMPLATE are actually mandatory.
+
+TEMPLATE might be a Lisp form or a string, depending on whether
+this is a snippet or a snippet-command.
+
+CONDITION, EXPAND-ENV and KEYBINDING are Lisp forms, they have
+been `yas--read-lisp'-ed and will eventually be
+`yas--eval-lisp'-ed.
+
+The remaining elements are strings.
+
+FILE is probably of very little use if you're programatically
+defining snippets.
+
+UUID is the snippet's \"unique-id\". Loading a second snippet
+file with the same uuid would replace the previous snippet.
+
+You can use `yas--parse-template' to return such lists based on
+the current buffers contents."
+ (if yas--creating-compiled-snippets
+ (let ((print-length nil))
+ (insert ";;; Snippet definitions:\n;;;\n")
+ (dolist (snippet snippets)
+ ;; Fill in missing elements with nil.
+ (setq snippet (append snippet (make-list (- 10 (length snippet)) nil)))
+ ;; Move LOAD-FILE to SAVE-FILE because we will load from the
+ ;; compiled file, not LOAD-FILE.
+ (let ((load-file (nth 6 snippet)))
+ (setcar (nthcdr 6 snippet) nil)
+ (setcar (nthcdr 9 snippet) load-file)))
+ (insert (pp-to-string
+ `(yas-define-snippets ',mode ',snippets)))
+ (insert "\n\n"))
+ ;; Normal case.
+ (let ((snippet-table (yas--table-get-create mode))
+ (template nil))
+ (dolist (snippet snippets)
+ (setq template (yas--define-snippets-1 snippet
+ snippet-table)))
+ template)))
+
+\f
+;;; Loading snippets from files
+
+(defun yas--template-get-file (template)
+ "Return TEMPLATE's LOAD-FILE or SAVE-FILE."
+ (or (yas--template-load-file template)
+ (let ((file (yas--template-save-file template)))
+ (when file
+ (yas--message 2 "%s has no load file, use save file, %s, instead."
+ (yas--template-name template) file))
+ file)))
+
+(defun yas--load-yas-setup-file (file)
+ (if (not yas--creating-compiled-snippets)
+ ;; Normal case.
+ (load file 'noerror (<= yas-verbosity 2))
+ (let ((elfile (concat file ".el")))
+ (when (file-exists-p elfile)
+ (insert ";;; contents of the .yas-setup.el support file:\n;;;\n")
+ (insert-file-contents elfile)
+ (goto-char (point-max))))))
+
+(defun yas--define-parents (mode parents)
+ "Add PARENTS to the list of MODE's parents."
+ (puthash mode (remove-duplicates
+ (append parents
+ (gethash mode yas--parents)))
+ yas--parents))
+
+(defun yas-load-directory (top-level-dir &optional use-jit interactive)
+ "Load snippets in directory hierarchy TOP-LEVEL-DIR.
+
+Below TOP-LEVEL-DIR each directory should be a mode name.
+
+With prefix argument USE-JIT do jit-loading of snippets."
+ (interactive
+ (list (read-directory-name "Select the root directory: " nil nil t)
+ current-prefix-arg t))
+ (unless yas-snippet-dirs
+ (setq yas-snippet-dirs top-level-dir))
+ (let ((impatient-buffers))
+ (dolist (dir (yas--subdirs top-level-dir))
+ (let* ((major-mode-and-parents (yas--compute-major-mode-and-parents
+ (concat dir "/dummy")))
+ (mode-sym (car major-mode-and-parents))
+ (parents (cdr major-mode-and-parents)))
+ ;; Attention: The parents and the menus are already defined
+ ;; here, even if the snippets are later jit-loaded.
+ ;;
+ ;; * We need to know the parents at this point since entering a
+ ;; given mode should jit load for its parents
+ ;; immediately. This could be reviewed, the parents could be
+ ;; discovered just-in-time-as well
+ ;;
+ ;; * We need to create the menus here to support the `full'
+ ;; option to `yas-use-menu' (all known snippet menus are shown to the user)
+ ;;
+ (yas--define-parents mode-sym parents)
+ (yas--menu-keymap-get-create mode-sym)
+ (let ((fun `(lambda () ;; FIXME: Simulating lexical-binding.
+ (yas--load-directory-1 ',dir ',mode-sym))))
+ (if use-jit
+ (yas--schedule-jit mode-sym fun)
+ (funcall fun)))
+ ;; Look for buffers that are already in `mode-sym', and so
+ ;; need the new snippets immediately...
+ ;;
+ (when use-jit
+ (cl-loop for buffer in (buffer-list)
+ do (with-current-buffer buffer
+ (when (eq major-mode mode-sym)
+ (yas--message 3 "Discovered there was already %s in %s" buffer mode-sym)
+ (push buffer impatient-buffers)))))))
+ ;; ...after TOP-LEVEL-DIR has been completely loaded, call
+ ;; `yas--load-pending-jits' in these impatient buffers.
+ ;;
+ (cl-loop for buffer in impatient-buffers
+ do (with-current-buffer buffer (yas--load-pending-jits))))
+ (when interactive
+ (yas--message 3 "Loaded snippets from %s." top-level-dir)))
+
+(defun yas--load-directory-1 (directory mode-sym)
+ "Recursively load snippet templates from DIRECTORY."
+ (if yas--creating-compiled-snippets
+ (let ((output-file (expand-file-name ".yas-compiled-snippets.el"
+ directory)))
+ (with-temp-file output-file
+ (insert (format ";;; Compiled snippets and support files for `%s'\n"
+ mode-sym))
+ (yas--load-directory-2 directory mode-sym)
+ (insert (format ";;; Do not edit! File generated at %s\n"
+ (current-time-string)))))
+ ;; Normal case.
+ (unless (file-exists-p (concat directory "/" ".yas-skip"))
+ (unless (and (load (expand-file-name ".yas-compiled-snippets" directory) 'noerror (<= yas-verbosity 3))
+ (progn (yas--message 2 "Loaded compiled snippets from %s" directory) t))
+ (yas--message 2 "Loading snippet files from %s" directory)
+ (yas--load-directory-2 directory mode-sym)))))
+
+(defun yas--load-directory-2 (directory mode-sym)
+ ;; Load .yas-setup.el files wherever we find them
+ ;;
+ (yas--load-yas-setup-file (expand-file-name ".yas-setup" directory))
+ (let* ((default-directory directory)
+ (snippet-defs nil))
+ ;; load the snippet files
+ ;;
+ (with-temp-buffer
+ (dolist (file (yas--subdirs directory 'no-subdirs-just-files))
+ (when (file-readable-p file)
+ (insert-file-contents file nil nil nil t)
+ (push (yas--parse-template file)
+ snippet-defs))))
+ (when snippet-defs
+ (yas-define-snippets mode-sym
+ snippet-defs))
+ ;; now recurse to a lower level
+ ;;
+ (dolist (subdir (yas--subdirs directory))
+ (yas--load-directory-2 subdir
+ mode-sym))))
+
+(defun yas--load-snippet-dirs (&optional nojit)
+ "Reload the directories listed in `yas-snippet-dirs' or
+prompt the user to select one."
+ (let (errors)
+ (if yas-snippet-dirs
+ (dolist (directory (reverse (yas-snippet-dirs)))
+ (cond ((file-directory-p directory)
+ (yas-load-directory directory (not nojit))
+ (if nojit
+ (yas--message 3 "Loaded %s" directory)
+ (yas--message 3 "Prepared just-in-time loading for %s" directory)))
+ (t
+ (push (yas--message 0 "Check your `yas-snippet-dirs': %s is not a directory" directory) errors))))
+ (call-interactively 'yas-load-directory))
+ errors))
+
+(defun yas-reload-all (&optional no-jit interactive)
+ "Reload all snippets and rebuild the YASnippet menu.
+
+When NO-JIT is non-nil force immediate reload of all known
+snippets under `yas-snippet-dirs', otherwise use just-in-time
+loading.
+
+When called interactively, use just-in-time loading when given a
+prefix argument."
+ (interactive (list (not current-prefix-arg) t))
+ (catch 'abort
+ (let ((errors)
+ (snippet-editing-buffers
+ (remove-if-not #'(lambda (buffer)
+ (with-current-buffer buffer yas--editing-template))
+ (buffer-list))))
+ ;; Warn if there are buffers visiting snippets, since reloading will break
+ ;; any on-line editing of those buffers.
+ ;;
+ (when snippet-editing-buffers
+ (if interactive
+ (if (y-or-n-p "Some buffers editing live snippets, close them and proceed with reload? ")
+ (mapc #'kill-buffer snippet-editing-buffers)
+ (yas--message 1 "Aborted reload...")
+ (throw 'abort nil))
+ ;; in a non-interactive use, at least set
+ ;; `yas--editing-template' to nil, make it guess it next time around
+ (mapc #'(lambda (buffer)
+ (with-current-buffer buffer
+ (kill-local-variable 'yas--editing-template)))
+ (buffer-list))))
+
+ ;; Empty all snippet tables and parenting info
+ ;;
+ (setq yas--tables (make-hash-table))
+ (setq yas--parents (make-hash-table))
+
+ ;; Before killing `yas--menu-table' use its keys to cleanup the
+ ;; mode menu parts of `yas--minor-mode-menu' (thus also cleaning
+ ;; up `yas-minor-mode-map', which points to it)
+ ;;
+ (maphash #'(lambda (menu-symbol _keymap)
+ (define-key yas--minor-mode-menu (vector menu-symbol) nil))
+ yas--menu-table)
+ ;; Now empty `yas--menu-table' as well
+ (setq yas--menu-table (make-hash-table))
+
+ ;; Cancel all pending 'yas--scheduled-jit-loads'
+ ;;
+ (setq yas--scheduled-jit-loads (make-hash-table))
+
+ ;; Reload the directories listed in `yas-snippet-dirs' or prompt
+ ;; the user to select one.
+ ;;
+ (setq errors (yas--load-snippet-dirs no-jit))
+ ;; Reload the direct keybindings
+ ;;
+ (yas-direct-keymaps-reload)
+
+ (run-hooks 'yas-after-reload-hook)
+ (yas--message 3 "Reloaded everything%s...%s."
+ (if no-jit "" " (snippets will load just-in-time)")
+ (if errors " (some errors, check *Messages*)" "")))))
+
+(defvar yas-after-reload-hook nil
+ "Hooks run after `yas-reload-all'.")
+
+(defun yas--load-pending-jits ()
+ (dolist (mode (yas--modes-to-activate))
+ (let ((funs (reverse (gethash mode yas--scheduled-jit-loads))))
+ ;; must reverse to maintain coherence with `yas-snippet-dirs'
+ (dolist (fun funs)
+ (yas--message 3 "Loading for `%s', just-in-time: %s!" mode fun)
+ (funcall fun))
+ (remhash mode yas--scheduled-jit-loads))))
+
+;; (when (<= emacs-major-version 22)
+;; (add-hook 'after-change-major-mode-hook 'yas--load-pending-jits))
+
+(defun yas--quote-string (string)
+ "Escape and quote STRING.
+foo\"bar\\! -> \"foo\\\"bar\\\\!\""
+ (concat "\""
+ (replace-regexp-in-string "[\\\"]"
+ "\\\\\\&"
+ string
+ t)
+ "\""))
+\f
+;;; Snippet compilation function
+
+(defun yas-compile-directory (top-level-dir)
+ "Create .yas-compiled-snippets.el files under subdirs of TOP-LEVEL-DIR.
+
+This works by stubbing a few functions, then calling
+`yas-load-directory'."
+ (interactive "DTop level snippet directory?")
+ (let ((yas--creating-compiled-snippets t))
+ (yas-load-directory top-level-dir nil)))
+
+(defun yas-recompile-all ()
+ "Compile every dir in `yas-snippet-dirs'."
+ (interactive)
+ (mapc #'yas-compile-directory (yas-snippet-dirs)))
+
+
+;;; JIT loading
+;;;
+
+(defvar yas--scheduled-jit-loads (make-hash-table)
+ "Alist of mode-symbols to forms to be evaled when `yas-minor-mode' kicks in.")
+
+(defun yas--schedule-jit (mode fun)
+ (push fun (gethash mode yas--scheduled-jit-loads)))
+
+
+\f
+;;; Some user level functions
+
+(defun yas-about ()
+ (interactive)
+ (message (concat "yasnippet (version "
+ yas--version
+ ") -- pluskid <pluskid@gmail.com>/joaotavora <joaotavora@gmail.com>")))
+
+\f
+;;; Apropos snippet menu:
+;;
+;; The snippet menu keymaps are store by mode in hash table called
+;; `yas--menu-table'. They are linked to the main menu in
+;; `yas--menu-keymap-get-create' and are initially created empty,
+;; reflecting the table hierarchy.
+;;
+;; They can be populated in two mutually exclusive ways: (1) by
+;; reading `yas--template-group', which in turn is populated by the "#
+;; group:" directives of the snippets or the ".yas-make-groups" file
+;; or (2) by using a separate `yas-define-menu' call, which declares a
+;; menu structure based on snippets uuids.
+;;
+;; Both situations are handled in `yas--update-template-menu', which
+;; uses the predicate `yas--template-menu-managed-by-yas-define-menu'
+;; that can tell between the two situations.
+;;
+;; Note:
+;;
+;; * if `yas-define-menu' is used it must run before
+;; `yas-define-snippets' and the UUIDS must match, otherwise we get
+;; duplicate entries. The `yas--template' objects are created in
+;; `yas-define-menu', holding nothing but the menu entry,
+;; represented by a pair of ((menu-item NAME :keys KEYS) TYPE) and
+;; stored in `yas--template-menu-binding-pair'. The (menu-item ...)
+;; part is then stored in the menu keymap itself which make the item
+;; appear to the user. These limitations could probably be revised.
+;;
+;; * The `yas--template-perm-group' slot is only used in
+;; `yas-describe-tables'.
+;;
+(defun yas--template-menu-binding-pair-get-create (template &optional type)
+ "Get TEMPLATE's menu binding or assign it a new one.
+
+TYPE may be `:stay', signaling this menu binding should be
+static in the menu."
+ (or (yas--template-menu-binding-pair template)
+ (let (;; (key (yas--template-key template))
+ ;; (keybinding (yas--template-keybinding template))
+ )
+ (setf (yas--template-menu-binding-pair template)
+ (cons `(menu-item ,(or (yas--template-name template)
+ (yas--template-uuid template))
+ ,(yas--make-menu-binding template)
+ :keys ,nil)
+ type)))))
+(defun yas--template-menu-managed-by-yas-define-menu (template)
+ "Non-nil if TEMPLATE's menu entry was included in a `yas-define-menu' call."
+ (cdr (yas--template-menu-binding-pair template)))
+
+
+(defun yas--show-menu-p (mode)
+ (cond ((eq yas-use-menu 'abbreviate)
+ (find mode
+ (mapcar #'(lambda (table)
+ (yas--table-mode table))
+ (yas--get-snippet-tables))))
+ (yas-use-menu t)))
+
+(defun yas--delete-from-keymap (keymap uuid)
+ "Recursively delete items with UUID from KEYMAP and its submenus."
+
+ ;; XXX: This used to skip any submenus named \"parent mode\"
+ ;;
+ ;; First of all, recursively enter submenus, i.e. the tree is
+ ;; searched depth first so that stale submenus can be found in the
+ ;; higher passes.
+ ;;
+ (mapc #'(lambda (item)
+ (when (and (listp (cdr item))
+ (keymapp (third (cdr item))))
+ (yas--delete-from-keymap (third (cdr item)) uuid)))
+ (rest keymap))
+ ;; Set the uuid entry to nil
+ ;;
+ (define-key keymap (vector (make-symbol uuid)) nil)
+ ;; Destructively modify keymap
+ ;;
+ (setcdr keymap (delete-if #'(lambda (item)
+ (or (null (cdr item))
+ (and (keymapp (third (cdr item)))
+ (null (cdr (third (cdr item)))))))
+ (rest keymap))))
+
+(defun yas-define-menu (mode menu &optional omit-items)
+ "Define a snippet menu for MODE according to MENU, omitting OMIT-ITEMS.
+
+MENU is a list, its elements can be:
+
+- (yas-item UUID) : Creates an entry the snippet identified with
+ UUID. The menu entry for a snippet thus identified is
+ permanent, i.e. it will never move (be reordered) in the menu.
+
+- (yas-separator) : Creates a separator
+
+- (yas-submenu NAME SUBMENU) : Creates a submenu with NAME,
+ SUBMENU has the same form as MENU. NAME is also added to the
+ list of groups of the snippets defined thereafter.
+
+OMIT-ITEMS is a list of snippet uuid's that will always be
+omitted from MODE's menu, even if they're manually loaded."
+ (let* ((table (yas--table-get-create mode))
+ (hash (yas--table-uuidhash table)))
+ (yas--define-menu-1 table
+ (yas--menu-keymap-get-create mode)
+ menu
+ hash)
+ (dolist (uuid omit-items)
+ (let ((template (or (gethash uuid hash)
+ (puthash uuid
+ (yas--make-template :table table
+ :uuid uuid)
+ hash))))
+ (setf (yas--template-menu-binding-pair template) (cons nil :none))))))
+
+(defun yas--define-menu-1 (table menu-keymap menu uuidhash &optional group-list)
+ "Helper for `yas-define-menu'."
+ (dolist (e (reverse menu))
+ (cond ((eq (first e) 'yas-item)
+ (let ((template (or (gethash (second e) uuidhash)
+ (puthash (second e)
+ (yas--make-template
+ :table table
+ :perm-group group-list
+ :uuid (second e))
+ uuidhash))))
+ (define-key menu-keymap (vector (gensym))
+ (car (yas--template-menu-binding-pair-get-create template :stay)))))
+ ((eq (first e) 'yas-submenu)
+ (let ((subkeymap (make-sparse-keymap)))
+ (define-key menu-keymap (vector (gensym))
+ `(menu-item ,(second e) ,subkeymap))
+ (yas--define-menu-1 table
+ subkeymap
+ (third e)
+ uuidhash
+ (append group-list (list (second e))))))
+ ((eq (first e) 'yas-separator)
+ (define-key menu-keymap (vector (gensym))
+ '(menu-item "----")))
+ (t
+ (yas--message 3 "Don't know anything about menu entry %s" (first e))))))
+\f
+(defun yas--define (mode key template &optional name condition group)
+ "Define a snippet. Expanding KEY into TEMPLATE.
+
+NAME is a description to this template. Also update the menu if
+`yas-use-menu' is t. CONDITION is the condition attached to
+this snippet. If you attach a condition to a snippet, then it
+will only be expanded when the condition evaluated to non-nil."
+ (yas-define-snippets mode
+ (list (list key template name condition group))))
+
+(defun yas-hippie-try-expand (first-time?)
+ "Integrate with hippie expand.
+
+Just put this function in `hippie-expand-try-functions-list'."
+ (when yas-minor-mode
+ (if (not first-time?)
+ (let ((yas-fallback-behavior 'return-nil))
+ (yas-expand))
+ (undo 1)
+ nil)))
+
+
+;;; Apropos condition-cache:
+;;;
+;;;
+;;;
+;;;
+(defvar yas--condition-cache-timestamp nil)
+(defmacro yas-define-condition-cache (func doc &rest body)
+ "Define a function FUNC with doc DOC and body BODY.
+BODY is executed at most once every snippet expansion attempt, to check
+expansion conditions.
+
+It doesn't make any sense to call FUNC programatically."
+ `(defun ,func () ,(if (and doc
+ (stringp doc))
+ (concat doc
+"\n\nFor use in snippets' conditions. Within each
+snippet-expansion routine like `yas-expand', computes actual
+value for the first time then always returns a cached value.")
+ (setq body (cons doc body))
+ nil)
+ (let ((timestamp-and-value (get ',func 'yas--condition-cache)))
+ (if (equal (car timestamp-and-value) yas--condition-cache-timestamp)
+ (cdr timestamp-and-value)
+ (let ((new-value (progn
+ ,@body
+ )))
+ (put ',func 'yas--condition-cache (cons yas--condition-cache-timestamp new-value))
+ new-value)))))
+
+(defalias 'yas-expand 'yas-expand-from-trigger-key)
+(defun yas-expand-from-trigger-key (&optional field)
+ "Expand a snippet before point.
+
+If no snippet expansion is possible, fall back to the behaviour
+defined in `yas-fallback-behavior'.
+
+Optional argument FIELD is for non-interactive use and is an
+object satisfying `yas--field-p' to restrict the expansion to."
+ (interactive)
+ (setq yas--condition-cache-timestamp (current-time))
+ (let (templates-and-pos)
+ (unless (and yas-expand-only-for-last-commands
+ (not (member last-command yas-expand-only-for-last-commands)))
+ (setq templates-and-pos (if field
+ (save-restriction
+ (narrow-to-region (yas--field-start field)
+ (yas--field-end field))
+ (yas--templates-for-key-at-point))
+ (yas--templates-for-key-at-point))))
+ (if templates-and-pos
+ (yas--expand-or-prompt-for-template (first templates-and-pos)
+ (second templates-and-pos)
+ (third templates-and-pos))
+ (yas--fallback))))
+
+(defun yas-expand-from-keymap ()
+ "Directly expand some snippets, searching `yas--direct-keymaps'.
+
+If expansion fails, execute the previous binding for this key"
+ (interactive)
+ (setq yas--condition-cache-timestamp (current-time))
+ (let* ((vec (subseq (this-command-keys-vector) (if current-prefix-arg
+ (length (this-command-keys))
+ 0)))
+ (templates (mapcan #'(lambda (table)
+ (yas--fetch table vec))
+ (yas--get-snippet-tables))))
+ (if templates
+ (yas--expand-or-prompt-for-template templates)
+ (let ((yas-fallback-behavior 'call-other-command))
+ (yas--fallback)))))
+
+(defun yas--expand-or-prompt-for-template (templates &optional start end)
+ "Expand one of TEMPLATES from START to END.
+
+Prompt the user if TEMPLATES has more than one element, else
+expand immediately. Common gateway for
+`yas-expand-from-trigger-key' and `yas-expand-from-keymap'."
+ (let ((yas--current-template (or (and (rest templates) ;; more than one
+ (yas--prompt-for-template (mapcar #'cdr templates)))
+ (cdar templates))))
+ (when yas--current-template
+ (yas-expand-snippet (yas--template-content yas--current-template)
+ start
+ end
+ (yas--template-expand-env yas--current-template)))))
+
+;; Apropos the trigger key and the fallback binding:
+;;
+;; When `yas-minor-mode-map' binds <tab>, that correctly overrides
+;; org-mode's <tab>, for example and searching for fallbacks correctly
+;; returns `org-cycle'. However, most other modes bind "TAB". TODO,
+;; improve this explanation.
+;;
+(defun yas--fallback ()
+ "Fallback after expansion has failed.
+
+Common gateway for `yas-expand-from-trigger-key' and
+`yas-expand-from-keymap'."
+ (cond ((eq yas-fallback-behavior 'return-nil)
+ ;; return nil
+ nil)
+ ((eq yas-fallback-behavior 'yas--fallback)
+ (error (concat "yasnippet fallback loop!\n"
+ "This can happen when you bind `yas-expand' "
+ "outside of the `yas-minor-mode-map'.")))
+ ((eq yas-fallback-behavior 'call-other-command)
+ (let* ((yas-fallback-behavior 'yas--fallback)
+ ;; Also bind `yas-minor-mode' to prevent fallback
+ ;; loops when other extensions use mechanisms similar
+ ;; to `yas--keybinding-beyond-yasnippet'. (github #525
+ ;; and #526)
+ ;;
+ (yas-minor-mode nil)
+ (beyond-yasnippet (yas--keybinding-beyond-yasnippet)))
+ (yas--message 4 "Falling back to %s" beyond-yasnippet)
+ (assert (or (null beyond-yasnippet) (commandp beyond-yasnippet)))
+ (setq this-command beyond-yasnippet)
+ (when beyond-yasnippet
+ (call-interactively beyond-yasnippet))))
+ ((and (listp yas-fallback-behavior)
+ (cdr yas-fallback-behavior)
+ (eq 'apply (car yas-fallback-behavior)))
+ (let ((command-or-fn (cadr yas-fallback-behavior))
+ (args (cddr yas-fallback-behavior))
+ (yas-fallback-behavior 'yas--fallback)
+ (yas-minor-mode nil))
+ (if args
+ (apply command-or-fn args)
+ (when (commandp command-or-fn)
+ (setq this-command command-or-fn)
+ (call-interactively command-or-fn)))))
+ (t
+ ;; also return nil if all the other fallbacks have failed
+ nil)))
+
+(defun yas--keybinding-beyond-yasnippet ()
+ "Get current keys's binding as if YASsnippet didn't exist."
+ (let* ((yas-minor-mode nil)
+ (yas--direct-keymaps nil)
+ (keys (this-single-command-keys)))
+ (or (key-binding keys t)
+ (key-binding (yas--fallback-translate-input keys) t))))
+
+(defun yas--fallback-translate-input (keys)
+ "Emulate `read-key-sequence', at least what I think it does.
+
+Keys should be an untranslated key vector. Returns a translated
+vector of keys. FIXME not thoroughly tested."
+ (let ((retval [])
+ (i 0))
+ (while (< i (length keys))
+ (let ((j i)
+ (translated local-function-key-map))
+ (while (and (< j (length keys))
+ translated
+ (keymapp translated))
+ (setq translated (cdr (assoc (aref keys j) (remove 'keymap translated)))
+ j (1+ j)))
+ (setq retval (vconcat retval (cond ((symbolp translated)
+ `[,translated])
+ ((vectorp translated)
+ translated)
+ (t
+ (substring keys i j)))))
+ (setq i j)))
+ retval))
+
+\f
+;;; Utils for snippet development:
+
+(defun yas--all-templates (tables)
+ "Get `yas--template' objects in TABLES, applicable for buffer and point.
+
+Honours `yas-choose-tables-first', `yas-choose-keys-first' and
+`yas-buffer-local-condition'"
+ (when yas-choose-tables-first
+ (setq tables (list (yas--prompt-for-table tables))))
+ (mapcar #'cdr
+ (if yas-choose-keys-first
+ (let ((key (yas--prompt-for-keys
+ (mapcan #'yas--table-all-keys tables))))
+ (when key
+ (mapcan #'(lambda (table)
+ (yas--fetch table key))
+ tables)))
+ (remove-duplicates (mapcan #'yas--table-templates tables)
+ :test #'equal))))
+
+(defun yas--lookup-snippet-1 (name mode)
+ "Get the snippet called NAME in MODE's tables."
+ (let ((yas-choose-tables-first nil) ; avoid prompts
+ (yas-choose-keys-first nil))
+ (cl-find name (yas--all-templates
+ (yas--get-snippet-tables mode))
+ :key #'yas--template-name :test #'string=)))
+
+(defun yas-lookup-snippet (name &optional mode noerror)
+ "Get the snippet content for the snippet NAME in MODE's tables.
+
+MODE defaults to the current buffer's `major-mode'. If NOERROR
+is non-nil, then don't signal an error if there isn't any snippet
+called NAME.
+
+Honours `yas-buffer-local-condition'."
+ (let ((snippet (yas--lookup-snippet-1 name mode)))
+ (cond
+ (snippet (yas--template-content snippet))
+ (noerror nil)
+ (t (error "No snippet named: %s" name)))))
+
+(defun yas-insert-snippet (&optional no-condition)
+ "Choose a snippet to expand, pop-up a list of choices according
+to `yas-prompt-functions'.
+
+With prefix argument NO-CONDITION, bypass filtering of snippets
+by condition."
+ (interactive "P")
+ (setq yas--condition-cache-timestamp (current-time))
+ (let* ((yas-buffer-local-condition (or (and no-condition
+ 'always)
+ yas-buffer-local-condition))
+ (templates (yas--all-templates (yas--get-snippet-tables)))
+ (yas--current-template (and templates
+ (or (and (rest templates) ;; more than one template for same key
+ (yas--prompt-for-template templates))
+ (car templates))))
+ (where (if (region-active-p)
+ (cons (region-beginning) (region-end))
+ (cons (point) (point)))))
+ (if yas--current-template
+ (yas-expand-snippet (yas--template-content yas--current-template)
+ (car where)
+ (cdr where)
+ (yas--template-expand-env yas--current-template))
+ (yas--message 3 "No snippets can be inserted here!"))))
+
+(defun yas-visit-snippet-file ()
+ "Choose a snippet to edit, selection like `yas-insert-snippet'.
+
+Only success if selected snippet was loaded from a file. Put the
+visited file in `snippet-mode'."
+ (interactive)
+ (let* ((yas-buffer-local-condition 'always)
+ (templates (yas--all-templates (yas--get-snippet-tables)))
+ (yas-prompt-functions '(yas-ido-prompt yas-completing-prompt))
+ (template (and templates
+ (or (yas--prompt-for-template templates
+ "Choose a snippet template to edit: ")
+ (car templates)))))
+
+ (if template
+ (yas--visit-snippet-file-1 template)
+ (message "No snippets tables active!"))))
+
+(defun yas--visit-snippet-file-1 (template)
+ "Helper for `yas-visit-snippet-file'."
+ (let ((file (yas--template-get-file template)))
+ (cond ((and file (file-readable-p file))
+ (find-file-other-window file)
+ (snippet-mode)
+ (set (make-local-variable 'yas--editing-template) template))
+ (file
+ (message "Original file %s no longer exists!" file))
+ (t
+ (switch-to-buffer (format "*%s*"(yas--template-name template)))
+ (let ((type 'snippet))
+ (when (listp (yas--template-content template))
+ (insert (format "# type: command\n"))
+ (setq type 'command))
+ (insert (format "# key: %s\n" (yas--template-key template)))
+ (insert (format "# name: %s\n" (yas--template-name template)))
+ (when (yas--template-keybinding template)
+ (insert (format "# binding: %s\n" (yas--template-keybinding template))))
+ (when (yas--template-expand-env template)
+ (insert (format "# expand-env: %s\n" (yas--template-expand-env template))))
+ (when (yas--template-condition template)
+ (insert (format "# condition: %s\n" (yas--template-condition template))))
+ (insert "# --\n")
+ (insert (if (eq type 'command)
+ (pp-to-string (yas--template-content template))
+ (yas--template-content template))))
+ (snippet-mode)
+ (set (make-local-variable 'yas--editing-template) template)))))
+
+(defun yas--guess-snippet-directories-1 (table)
+ "Guess possible snippet subdirectories for TABLE."
+ (cons (yas--table-name table)
+ (mapcan #'(lambda (parent)
+ (yas--guess-snippet-directories-1
+ parent))
+ (yas--table-parents table))))
+
+(defun yas--guess-snippet-directories (&optional table)
+ "Try to guess suitable directories based on the current active
+tables (or optional TABLE).
+
+Returns a list of elements (TABLE . DIRS) where TABLE is a
+`yas--table' object and DIRS is a list of all possible directories
+where snippets of table might exist."
+ (let ((main-dir (replace-regexp-in-string
+ "/+$" ""
+ (or (first (or (yas-snippet-dirs)
+ (setq yas-snippet-dirs (list yas--default-user-snippets-dir)))))))
+ (tables (or (and table
+ (list table))
+ (yas--get-snippet-tables))))
+ ;; HACK! the snippet table created here is actually registered!
+ ;;
+ (unless (or table (gethash major-mode yas--tables))
+ (push (yas--table-get-create major-mode)
+ tables))
+
+ (mapcar #'(lambda (table)
+ (cons table
+ (mapcar #'(lambda (subdir)
+ (concat main-dir "/" subdir))
+ (yas--guess-snippet-directories-1 table))))
+ tables)))
+
+(defun yas--make-directory-maybe (table-and-dirs &optional main-table-string)
+ "Return a dir inside TABLE-AND-DIRS, prompts for creation if none exists."
+ (or (some #'(lambda (dir) (when (file-directory-p dir) dir)) (cdr table-and-dirs))
+ (let ((candidate (first (cdr table-and-dirs))))
+ (unless (file-writable-p (file-name-directory candidate))
+ (error (yas--format "%s is not writable." candidate)))
+ (if (y-or-n-p (format "Guessed directory (%s) for%s%s table \"%s\" does not exist! Create? "
+ candidate
+ (if (gethash (yas--table-mode (car table-and-dirs))
+ yas--tables)
+ ""
+ " brand new")
+ (or main-table-string
+ "")
+ (yas--table-name (car table-and-dirs))))
+ (progn
+ (make-directory candidate 'also-make-parents)
+ ;; create the .yas-parents file here...
+ candidate)))))
+
+(defun yas-new-snippet (&optional no-template)
+ "Pops a new buffer for writing a snippet.
+
+Expands a snippet-writing snippet, unless the optional prefix arg
+NO-TEMPLATE is non-nil."
+ (interactive "P")
+ (let ((guessed-directories (yas--guess-snippet-directories)))
+
+ (switch-to-buffer "*new snippet*")
+ (erase-buffer)
+ (kill-all-local-variables)
+ (snippet-mode)
+ (yas-minor-mode 1)
+ (set (make-local-variable 'yas--guessed-modes) (mapcar #'(lambda (d)
+ (yas--table-mode (car d)))
+ guessed-directories))
+ (if (and (not no-template) yas-new-snippet-default)
+ (yas-expand-snippet yas-new-snippet-default))))
+
+(defun yas--compute-major-mode-and-parents (file)
+ "Given FILE, find the nearest snippet directory for a given mode.
+
+Returns a list (MODE-SYM PARENTS), the mode's symbol and a list
+representing one or more of the mode's parents.
+
+Note that MODE-SYM need not be the symbol of a real major mode,
+neither do the elements of PARENTS."
+ (let* ((file-dir (and file
+ (directory-file-name (or (some #'(lambda (special)
+ (locate-dominating-file file special))
+ '(".yas-setup.el"
+ ".yas-make-groups"
+ ".yas-parents"))
+ (directory-file-name (file-name-directory file))))))
+ (parents-file-name (concat file-dir "/.yas-parents"))
+ (major-mode-name (and file-dir
+ (file-name-nondirectory file-dir)))
+ (major-mode-sym (or (and major-mode-name
+ (intern major-mode-name))))
+ (parents (when (file-readable-p parents-file-name)
+ (mapcar #'intern
+ (split-string
+ (with-temp-buffer
+ (insert-file-contents parents-file-name)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))))
+ (when major-mode-sym
+ (cons major-mode-sym (remove major-mode-sym parents)))))
+
+(defvar yas--editing-template nil
+ "Supporting variable for `yas-load-snippet-buffer' and `yas--visit-snippet'.")
+
+(defvar yas--current-template nil
+ "Holds the current template being expanded into a snippet.")
+
+(defvar yas--guessed-modes nil
+ "List of guessed modes supporting `yas-load-snippet-buffer'.")
+
+(defun yas--read-table ()
+ "Ask user for a snippet table, help with some guessing."
+ (let ((prompt (if (and (featurep 'ido)
+ ido-mode)
+ 'ido-completing-read 'completing-read)))
+ (unless yas--guessed-modes
+ (set (make-local-variable 'yas--guessed-modes)
+ (or (yas--compute-major-mode-and-parents buffer-file-name))))
+ (intern
+ (funcall prompt (format "Choose or enter a table (yas guesses %s): "
+ (if yas--guessed-modes
+ (first yas--guessed-modes)
+ "nothing"))
+ (mapcar #'symbol-name yas--guessed-modes)
+ nil
+ nil
+ nil
+ nil
+ (if (first yas--guessed-modes)
+ (symbol-name (first yas--guessed-modes)))))))
+
+(defun yas-load-snippet-buffer (table &optional interactive)
+ "Parse and load current buffer's snippet definition into TABLE.
+
+TABLE is a symbol naming a passed to `yas--table-get-create'.
+
+When called interactively, prompt for the table name."
+ (interactive (list (yas--read-table) t))
+ (cond
+ ;; We have `yas--editing-template', this buffer's content comes from a
+ ;; template which is already loaded and neatly positioned,...
+ ;;
+ (yas--editing-template
+ (yas--define-snippets-1 (yas--parse-template (yas--template-load-file yas--editing-template))
+ (yas--template-table yas--editing-template)))
+ ;; Try to use `yas--guessed-modes'. If we don't have that use the
+ ;; value from `yas--compute-major-mode-and-parents'
+ ;;
+ (t
+ (unless yas--guessed-modes
+ (set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))
+ (let* ((table (yas--table-get-create table)))
+ (set (make-local-variable 'yas--editing-template)
+ (yas--define-snippets-1 (yas--parse-template buffer-file-name)
+ table)))))
+ (when interactive
+ (yas--message 3 "Snippet \"%s\" loaded for %s."
+ (yas--template-name yas--editing-template)
+ (yas--table-name (yas--template-table yas--editing-template)))))
+
+(defun yas-load-snippet-buffer-and-close (table &optional kill)
+ "Load the snippet with `yas-load-snippet-buffer', possibly
+ save, then `quit-window' if saved.
+
+If the snippet is new, ask the user whether (and where) to save
+it. If the snippet already has a file, just save it.
+
+The prefix argument KILL is passed to `quit-window'.
+
+Don't use this from a Lisp program, call `yas-load-snippet-buffer'
+and `kill-buffer' instead."
+ (interactive (list (yas--read-table) current-prefix-arg))
+ (yas-load-snippet-buffer table t)
+ (let ((file (yas--template-get-file yas--editing-template)))
+ (when (and (or
+ ;; Only offer to save this if it looks like a library or new
+ ;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs'
+ ;; which is not the first, or from an unwritable file)
+ ;;
+ (not file)
+ (not (file-writable-p file))
+ (and (cdr-safe yas-snippet-dirs)
+ (not (string-prefix-p (expand-file-name (car yas-snippet-dirs)) file))))
+ (y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? ")))
+ (let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
+ (chosen (and option
+ (yas--make-directory-maybe option))))
+ (when chosen
+ (let ((default-file-name (or (and file (file-name-nondirectory file))
+ (yas--template-name yas--editing-template))))
+ (write-file (concat chosen "/"
+ (read-from-minibuffer (format "File name to create in %s? " chosen)
+ default-file-name)))
+ (setf (yas--template-load-file yas--editing-template) buffer-file-name))))))
+ (when buffer-file-name
+ (save-buffer)
+ (quit-window kill)))
+
+(defun yas-tryout-snippet (&optional debug)
+ "Test current buffer's snippet template in other buffer."
+ (interactive "P")
+ (let* ((major-mode-and-parent (yas--compute-major-mode-and-parents buffer-file-name))
+ (parsed (yas--parse-template))
+ (test-mode (or (and (car major-mode-and-parent)
+ (fboundp (car major-mode-and-parent))
+ (car major-mode-and-parent))
+ (first yas--guessed-modes)
+ (intern (read-from-minibuffer (yas--format "Please input a mode: ")))))
+ (yas--current-template
+ (and parsed
+ (fboundp test-mode)
+ (yas--make-template :table nil ;; no tables for ephemeral snippets
+ :key (first parsed)
+ :content (second parsed)
+ :name (third parsed)
+ :expand-env (sixth parsed)))))
+ (cond (yas--current-template
+ (let ((buffer-name (format "*testing snippet: %s*" (yas--template-name yas--current-template))))
+ (kill-buffer (get-buffer-create buffer-name))
+ (switch-to-buffer (get-buffer-create buffer-name))
+ (setq buffer-undo-list nil)
+ (condition-case nil (funcall test-mode) (error nil))
+ (yas-minor-mode 1)
+ (setq buffer-read-only nil)
+ (yas-expand-snippet (yas--template-content yas--current-template)
+ (point-min)
+ (point-max)
+ (yas--template-expand-env yas--current-template))
+ (when (and debug
+ (require 'yasnippet-debug nil t))
+ (add-hook 'post-command-hook 'yas-debug-snippet-vars nil t))))
+ (t
+ (yas--message 3 "Cannot test snippet for unknown major mode")))))
+
+(defun yas-active-keys ()
+ "Return all active trigger keys for current buffer and point."
+ (remove-duplicates
+ (remove-if-not #'stringp (mapcan #'yas--table-all-keys (yas--get-snippet-tables)))
+ :test #'string=))
+
+(defun yas--template-fine-group (template)
+ (car (last (or (yas--template-group template)
+ (yas--template-perm-group template)))))
+
+(defun yas-describe-tables (&optional choose)
+ "Display snippets for each table."
+ (interactive "P")
+ (let* ((by-name-hash (and choose
+ (y-or-n-p "Show by namehash? ")))
+ (buffer (get-buffer-create "*YASnippet tables*"))
+ (active-tables (yas--get-snippet-tables))
+ (remain-tables (let ((all))
+ (maphash #'(lambda (_k v)
+ (unless (find v active-tables)
+ (push v all)))
+ yas--tables)
+ all))
+ (table-lists (list active-tables remain-tables))
+ (original-buffer (current-buffer))
+ (continue t)
+ (yas--condition-cache-timestamp (current-time)))
+ (with-current-buffer buffer
+ (setq buffer-read-only nil)
+ (erase-buffer)
+ (cond ((not by-name-hash)
+ (insert "YASnippet tables: \n")
+ (while (and table-lists
+ continue)
+ (dolist (table (car table-lists))
+ (yas--describe-pretty-table table original-buffer))
+ (setq table-lists (cdr table-lists))
+ (when table-lists
+ (yas--create-snippet-xrefs)
+ (display-buffer buffer)
+ (setq continue (and choose (y-or-n-p "Show also non-active tables? ")))))
+ (yas--create-snippet-xrefs)
+ (help-mode)
+ (goto-char 1))
+ (t
+ (insert "\n\nYASnippet tables by NAMEHASH: \n")
+ (dolist (table (append active-tables remain-tables))
+ (insert (format "\nSnippet table `%s':\n\n" (yas--table-name table)))
+ (let ((keys))
+ (maphash #'(lambda (k _v)
+ (push k keys))
+ (yas--table-hash table))
+ (dolist (key keys)
+ (insert (format " key %s maps snippets: %s\n" key
+ (let ((names))
+ (maphash #'(lambda (k _v)
+ (push k names))
+ (gethash key (yas--table-hash table)))
+ names))))))))
+ (goto-char 1)
+ (setq buffer-read-only t))
+ (display-buffer buffer)))
+
+(defun yas--describe-pretty-table (table &optional original-buffer)
+ (insert (format "\nSnippet table `%s'"
+ (yas--table-name table)))
+ (if (yas--table-parents table)
+ (insert (format " parents: %s\n"
+ (mapcar #'yas--table-name
+ (yas--table-parents table))))
+ (insert "\n"))
+ (insert (make-string 100 ?-) "\n")
+ (insert "group state name key binding\n")
+ (let ((groups-hash (make-hash-table :test #'equal)))
+ (maphash #'(lambda (_k v)
+ (let ((group (or (yas--template-fine-group v)
+ "(top level)")))
+ (when (yas--template-name v)
+ (puthash group
+ (cons v (gethash group groups-hash))
+ groups-hash))))
+ (yas--table-uuidhash table))
+ (maphash
+ #'(lambda (group templates)
+ (setq group (truncate-string-to-width group 25 0 ? "..."))
+ (insert (make-string 100 ?-) "\n")
+ (dolist (p templates)
+ (let ((name (truncate-string-to-width (propertize (format "\\\\snippet `%s'" (yas--template-name p))
+ 'yasnippet p)
+ 50 0 ? "..."))
+ (group (prog1 group
+ (setq group (make-string (length group) ? ))))
+ (condition-string (let ((condition (yas--template-condition p)))
+ (if (and condition
+ original-buffer)
+ (with-current-buffer original-buffer
+ (if (yas--eval-condition condition)
+ "(y)"
+ "(s)"))
+ "(a)"))))
+ (insert group " ")
+ (insert condition-string " ")
+ (insert name
+ (if (string-match "\\.\\.\\.$" name)
+ "'"
+ " ")
+ " ")
+ (insert (truncate-string-to-width (or (yas--template-key p) "")
+ 15 0 ? "...") " ")
+ (insert (truncate-string-to-width (key-description (yas--template-keybinding p))
+ 15 0 ? "...") " ")
+ (insert "\n"))))
+ groups-hash)))
+
+
+\f
+;;; User convenience functions, for using in `yas-key-syntaxes'
+
+(defun yas-try-key-from-whitespace (_start-point)
+ "As `yas-key-syntaxes' element, look for whitespace delimited key.
+
+A newline will be considered whitespace even if the mode syntax
+marks it as something else (typically comment ender)."
+ (skip-chars-backward "^[:space:]\n"))
+
+(defun yas-shortest-key-until-whitespace (_start-point)
+ "Like `yas-longest-key-from-whitespace' but take the shortest key."
+ (when (/= (skip-chars-backward "^[:space:]\n" (1- (point))) 0)
+ 'again))
+
+(defun yas-longest-key-from-whitespace (start-point)
+ "As `yas-key-syntaxes' element, look for longest key between point and whitespace.
+
+A newline will be considered whitespace even if the mode syntax
+marks it as something else (typically comment ender)."
+ (if (= (point) start-point)
+ (yas-try-key-from-whitespace start-point)
+ (forward-char))
+ (unless (<= start-point (1+ (point)))
+ 'again))
+
+
+\f
+;;; User convenience functions, for using in snippet definitions
+
+(defvar yas-modified-p nil
+ "Non-nil if field has been modified by user or transformation.")
+
+(defvar yas-moving-away-p nil
+ "Non-nil if user is about to exit field.")
+
+(defvar yas-text nil
+ "Contains current field text.")
+
+(defun yas-substr (str pattern &optional subexp)
+ "Search PATTERN in STR and return SUBEXPth match.
+
+If found, the content of subexp group SUBEXP (default 0) is
+ returned, or else the original STR will be returned."
+ (let ((grp (or subexp 0)))
+ (save-match-data
+ (if (string-match pattern str)
+ (match-string-no-properties grp str)
+ str))))
+
+(defun yas-choose-value (&rest possibilities)
+ "Prompt for a string in POSSIBILITIES and return it.
+
+The last element of POSSIBILITIES may be a list of strings."
+ (unless (or yas-moving-away-p
+ yas-modified-p)
+ (let* ((last-link (last possibilities))
+ (last-elem (car last-link)))
+ (when (listp last-elem)
+ (setcar last-link (car last-elem))
+ (setcdr last-link (cdr last-elem))))
+ (some #'(lambda (fn)
+ (funcall fn "Choose: " possibilities))
+ yas-prompt-functions)))
+
+(defun yas-key-to-value (alist)
+ (unless (or yas-moving-away-p
+ yas-modified-p)
+ (let ((key (read-key-sequence "")))
+ (when (stringp key)
+ (or (cdr (find key alist :key #'car :test #'string=))
+ key)))))
+
+(defun yas-throw (text)
+ "Throw a yas--exception with TEXT as the reason."
+ (throw 'yas--exception (cons 'yas--exception text)))
+
+(defun yas-verify-value (possibilities)
+ "Verify that the current field value is in POSSIBILITIES.
+
+Otherwise throw exception."
+ (when (and yas-moving-away-p (notany #'(lambda (pos) (string= pos yas-text)) possibilities))
+ (yas-throw (yas--format "Field only allows %s" possibilities))))
+
+(defun yas-field-value (number)
+ "Get the string for field with NUMBER.
+
+Use this in primary and mirror transformations to tget."
+ (let* ((snippet (car (yas--snippets-at-point)))
+ (field (and snippet
+ (yas--snippet-find-field snippet number))))
+ (when field
+ (yas--field-text-for-display field))))
+
+(defun yas-text ()
+ "Return `yas-text' if that exists and is non-empty, else nil."
+ (if (and yas-text
+ (not (string= "" yas-text)))
+ yas-text))
+
+(defun yas-selected-text ()
+ "Return `yas-selected-text' if that exists and is non-empty, else nil."
+ (if (and yas-selected-text
+ (not (string= "" yas-selected-text)))
+ yas-selected-text))
+
+(defun yas--get-field-once (number &optional transform-fn)
+ (unless yas-modified-p
+ (if transform-fn
+ (funcall transform-fn (yas-field-value number))
+ (yas-field-value number))))
+
+(defun yas-default-from-field (number)
+ (unless yas-modified-p
+ (yas-field-value number)))
+
+(defun yas-inside-string ()
+ "Return non-nil if the point is inside a string according to font-lock."
+ (equal 'font-lock-string-face (get-char-property (1- (point)) 'face)))
+
+(defun yas-unimplemented (&optional missing-feature)
+ (if yas--current-template
+ (if (y-or-n-p (format "This snippet is unimplemented (missing %s) Visit the snippet definition? "
+ (or missing-feature
+ "something")))
+ (yas--visit-snippet-file-1 yas--current-template))
+ (message "No implementation. Missing %s" (or missing-feature "something"))))
+
+\f
+;;; Snippet expansion and field management
+
+(defvar yas--active-field-overlay nil
+ "Overlays the currently active field.")
+
+(defvar yas--field-protection-overlays nil
+ "Two overlays protect the current active field.")
+
+(defvar yas-selected-text nil
+ "The selected region deleted on the last snippet expansion.")
+
+(defvar yas--start-column nil
+ "The column where the snippet expansion started.")
+
+(make-variable-buffer-local 'yas--active-field-overlay)
+(make-variable-buffer-local 'yas--field-protection-overlays)
+(put 'yas--active-field-overlay 'permanent-local t)
+(put 'yas--field-protection-overlays 'permanent-local t)
+
+(defstruct (yas--snippet (:constructor yas--make-snippet ()))
+ "A snippet.
+
+..."
+ (fields '())
+ (exit nil)
+ (id (yas--snippet-next-id) :read-only t)
+ (control-overlay nil)
+ active-field
+ ;; stacked expansion: the `previous-active-field' slot saves the
+ ;; active field where the child expansion took place
+ previous-active-field
+ force-exit)
+
+(defstruct (yas--field (:constructor yas--make-field (number start end parent-field)))
+ "A field.
+
+NUMBER is the field number.
+START and END are mostly buffer markers, but see \"apropos markers-to-points\".
+PARENT-FIELD is a `yas--field' this field is nested under, or nil.
+MIRRORS is a list of `yas--mirror's
+TRANSFORM is a lisp form.
+MODIFIED-P is a boolean set to true once user inputs text.
+NEXT is another `yas--field' or `yas--mirror' or `yas--exit'.
+"
+ number
+ start end
+ parent-field
+ (mirrors '())
+ (transform nil)
+ (modified-p nil)
+ next)
+
+
+(defstruct (yas--mirror (:constructor yas--make-mirror (start end transform)))
+ "A mirror.
+
+START and END are mostly buffer markers, but see \"apropos markers-to-points\".
+TRANSFORM is a lisp form.
+PARENT-FIELD is a `yas--field' this mirror is nested under, or nil.
+NEXT is another `yas--field' or `yas--mirror' or `yas--exit'
+DEPTH is a count of how many nested mirrors can affect this mirror"
+ start end
+ (transform nil)
+ parent-field
+ next
+ depth)
+
+(defstruct (yas--exit (:constructor yas--make-exit (marker)))
+ marker
+ next)
+
+(defun yas--apply-transform (field-or-mirror field &optional empty-on-nil-p)
+ "Calculate transformed string for FIELD-OR-MIRROR from FIELD.
+
+If there is no transform for ht field, return nil.
+
+If there is a transform but it returns nil, return the empty
+string iff EMPTY-ON-NIL-P is true."
+ (let* ((yas-text (yas--field-text-for-display field))
+ (yas-modified-p (yas--field-modified-p field))
+ (yas-moving-away-p nil)
+ (transform (if (yas--mirror-p field-or-mirror)
+ (yas--mirror-transform field-or-mirror)
+ (yas--field-transform field-or-mirror)))
+ (start-point (if (yas--mirror-p field-or-mirror)
+ (yas--mirror-start field-or-mirror)
+ (yas--field-start field-or-mirror)))
+ (transformed (and transform
+ (save-excursion
+ (goto-char start-point)
+ (let ((ret (yas--eval-lisp transform)))
+ (or ret (and empty-on-nil-p "")))))))
+ transformed))
+
+(defsubst yas--replace-all (from to &optional text)
+ "Replace all occurrences from FROM to TO.
+
+With optional string TEXT do it in that string."
+ (if text
+ (replace-regexp-in-string (regexp-quote from) to text t t)
+ (goto-char (point-min))
+ (while (search-forward from nil t)
+ (replace-match to t t text))))
+
+(defun yas--snippet-find-field (snippet number)
+ (find-if #'(lambda (field)
+ (eq number (yas--field-number field)))
+ (yas--snippet-fields snippet)))
+
+(defun yas--snippet-sort-fields (snippet)
+ "Sort the fields of SNIPPET in navigation order."
+ (setf (yas--snippet-fields snippet)
+ (sort (yas--snippet-fields snippet)
+ #'yas--snippet-field-compare)))
+
+(defun yas--snippet-field-compare (field1 field2)
+ "Compare FIELD1 and FIELD2.
+
+The field with a number is sorted first. If they both have a
+number, compare through the number. If neither have, compare
+through the field's start point"
+ (let ((n1 (yas--field-number field1))
+ (n2 (yas--field-number field2)))
+ (if n1
+ (if n2
+ (or (zerop n2) (and (not (zerop n1))
+ (< n1 n2)))
+ (not (zerop n1)))
+ (if n2
+ (zerop n2)
+ (< (yas--field-start field1)
+ (yas--field-start field2))))))
+
+(defun yas--field-probably-deleted-p (snippet field)
+ "Guess if SNIPPET's FIELD should be skipped."
+ (and
+ ;; field must be zero length
+ ;;
+ (zerop (- (yas--field-start field) (yas--field-end field)))
+ ;; field must have been modified
+ ;;
+ (yas--field-modified-p field)
+ ;; either:
+ (or
+ ;; 1) it's a nested field
+ ;;
+ (yas--field-parent-field field)
+ ;; 2) ends just before the snippet end
+ ;;
+ (and (eq field (car (last (yas--snippet-fields snippet))))
+ (= (yas--field-start field) (overlay-end (yas--snippet-control-overlay snippet)))))
+ ;; the field numbered 0, just before the exit marker, should
+ ;; never be skipped
+ ;;
+ (not (and (yas--field-number field)
+ (zerop (yas--field-number field))))))
+
+(defun yas--snippets-at-point (&optional all-snippets)
+ "Return a sorted list of snippets at point.
+
+The most recently-inserted snippets are returned first."
+ (sort
+ (delq nil (delete-dups
+ (mapcar (lambda (ov) (overlay-get ov 'yas--snippet))
+ (if all-snippets (overlays-in (point-min) (point-max))
+ (nconc (overlays-at (point))
+ (overlays-at (1- (point))))))))
+ #'(lambda (s1 s2)
+ (<= (yas--snippet-id s2) (yas--snippet-id s1)))))
+
+(defun yas-next-field-or-maybe-expand ()
+ "Try to expand a snippet at a key before point.
+
+Otherwise delegate to `yas-next-field'."
+ (interactive)
+ (if yas-triggers-in-field
+ (let ((yas-fallback-behavior 'return-nil)
+ (active-field (overlay-get yas--active-field-overlay 'yas--field)))
+ (when active-field
+ (unless (yas-expand-from-trigger-key active-field)
+ (yas-next-field))))
+ (yas-next-field)))
+
+(defun yas-next-field (&optional arg)
+ "Navigate to the ARGth next field.
+
+If there's none, exit the snippet."
+ (interactive)
+ (let* ((arg (or arg
+ 1))
+ (snippet (first (yas--snippets-at-point)))
+ (active-field (overlay-get yas--active-field-overlay 'yas--field))
+ (live-fields (remove-if #'(lambda (field)
+ (and (not (eq field active-field))
+ (yas--field-probably-deleted-p snippet field)))
+ (yas--snippet-fields snippet)))
+ (active-field-pos (position active-field live-fields))
+ (target-pos (and active-field-pos (+ arg active-field-pos)))
+ (target-field (and target-pos (nth target-pos live-fields))))
+ ;; First check if we're moving out of a field with a transform
+ ;;
+ (when (and active-field
+ (yas--field-transform active-field))
+ (let* ((yas-moving-away-p t)
+ (yas-text (yas--field-text-for-display active-field))
+ (yas-modified-p (yas--field-modified-p active-field)))
+ ;; primary field transform: exit call to field-transform
+ (yas--eval-lisp (yas--field-transform active-field))))
+ ;; Now actually move...
+ (cond ((and target-pos (>= target-pos (length live-fields)))
+ (yas-exit-snippet snippet))
+ (target-field
+ (yas--move-to-field snippet target-field))
+ (t
+ nil))))
+
+(defun yas--place-overlays (snippet field)
+ "Correctly place overlays for SNIPPET's FIELD."
+ (yas--make-move-field-protection-overlays snippet field)
+ (yas--make-move-active-field-overlay snippet field))
+
+(defun yas--move-to-field (snippet field)
+ "Update SNIPPET to move to field FIELD.
+
+Also create some protection overlays"
+ (goto-char (yas--field-start field))
+ (yas--place-overlays snippet field)
+ (overlay-put yas--active-field-overlay 'yas--field field)
+ (let ((number (yas--field-number field)))
+ ;; check for the special ${0: ...} field
+ (if (and number (zerop number))
+ (progn
+ (set-mark (yas--field-end field))
+ (setf (yas--snippet-force-exit snippet)
+ (or (yas--field-transform field)
+ t)))
+ ;; make this field active
+ (setf (yas--snippet-active-field snippet) field)
+ ;; primary field transform: first call to snippet transform
+ (unless (yas--field-modified-p field)
+ (if (yas--field-update-display field)
+ (yas--update-mirrors snippet)
+ (setf (yas--field-modified-p field) nil))))))
+
+(defun yas-prev-field ()
+ "Navigate to prev field. If there's none, exit the snippet."
+ (interactive)
+ (yas-next-field -1))
+
+(defun yas-abort-snippet (&optional snippet)
+ (interactive)
+ (let ((snippet (or snippet
+ (car (yas--snippets-at-point)))))
+ (when snippet
+ (setf (yas--snippet-force-exit snippet) t))))
+
+(defun yas-exit-snippet (snippet)
+ "Goto exit-marker of SNIPPET."
+ (interactive (list (first (yas--snippets-at-point))))
+ (when snippet
+ (setf (yas--snippet-force-exit snippet) t)
+ (goto-char (if (yas--snippet-exit snippet)
+ (yas--exit-marker (yas--snippet-exit snippet))
+ (overlay-end (yas--snippet-control-overlay snippet))))))
+
+(defun yas-exit-all-snippets ()
+ "Exit all snippets."
+ (interactive)
+ (mapc #'(lambda (snippet)
+ (yas-exit-snippet snippet)
+ (yas--check-commit-snippet))
+ (yas--snippets-at-point 'all-snippets)))
+
+\f
+;;; Some low level snippet-routines:
+
+(defvar yas--inhibit-overlay-hooks nil
+ "Bind this temporarily to non-nil to prevent running `yas--on-*-modification'.")
+
+(defvar yas-snippet-beg nil "Beginning position of the last snippet committed.")
+(defvar yas-snippet-end nil "End position of the last snippet committed.")
+
+(defun yas--commit-snippet (snippet)
+ "Commit SNIPPET, but leave point as it is.
+
+This renders the snippet as ordinary text."
+
+ (let ((control-overlay (yas--snippet-control-overlay snippet)))
+ ;;
+ ;; Save the end of the moribund snippet in case we need to revive it
+ ;; its original expansion.
+ ;;
+ (when (and control-overlay
+ (overlay-buffer control-overlay))
+ (setq yas-snippet-beg (overlay-start control-overlay))
+ (setq yas-snippet-end (overlay-end control-overlay))
+ (delete-overlay control-overlay))
+
+ (let ((yas--inhibit-overlay-hooks t))
+ (when yas--active-field-overlay
+ (delete-overlay yas--active-field-overlay))
+ (when yas--field-protection-overlays
+ (mapc #'delete-overlay yas--field-protection-overlays)))
+
+ ;; stacked expansion: if the original expansion took place from a
+ ;; field, make sure we advance it here at least to
+ ;; `yas-snippet-end'...
+ ;;
+ (let ((previous-field (yas--snippet-previous-active-field snippet)))
+ (when (and yas-snippet-end previous-field)
+ (yas--advance-end-maybe previous-field yas-snippet-end)))
+
+ ;; Convert all markers to points,
+ ;;
+ (yas--markers-to-points snippet)
+
+ ;; Take care of snippet revival
+ ;;
+ (if yas-snippet-revival
+ (push `(apply yas--snippet-revive ,yas-snippet-beg ,yas-snippet-end ,snippet)
+ buffer-undo-list)
+ ;; Dismember the snippet... this is useful if we get called
+ ;; again from `yas--take-care-of-redo'....
+ (setf (yas--snippet-fields snippet) nil)))
+
+ (yas--message 3 "Snippet %s exited." (yas--snippet-id snippet)))
+
+(defun yas--safely-run-hooks (hook-var)
+ (condition-case error
+ (run-hooks hook-var)
+ (error
+ (yas--message 3 "%s error: %s" hook-var (error-message-string error)))))
+
+
+(defun yas--check-commit-snippet ()
+ "Check if point exited the currently active field of the snippet.
+
+If so cleans up the whole snippet up."
+ (let* ((snippets (yas--snippets-at-point 'all-snippets))
+ (snippets-left snippets)
+ (snippet-exit-transform))
+ (dolist (snippet snippets)
+ (let ((active-field (yas--snippet-active-field snippet)))
+ (setq snippet-exit-transform (yas--snippet-force-exit snippet))
+ (cond ((or snippet-exit-transform
+ (not (and active-field (yas--field-contains-point-p active-field))))
+ (setq snippets-left (delete snippet snippets-left))
+ (setf (yas--snippet-force-exit snippet) nil)
+ (yas--commit-snippet snippet))
+ ((and active-field
+ (or (not yas--active-field-overlay)
+ (not (overlay-buffer yas--active-field-overlay))))
+ ;;
+ ;; stacked expansion: this case is mainly for recent
+ ;; snippet exits that place us back int the field of
+ ;; another snippet
+ ;;
+ (save-excursion
+ (yas--move-to-field snippet active-field)
+ (yas--update-mirrors snippet)))
+ (t
+ nil))))
+ (unless (or (null snippets) snippets-left)
+ (if snippet-exit-transform
+ (yas--eval-lisp-no-saves snippet-exit-transform))
+ (yas--safely-run-hooks 'yas-after-exit-snippet-hook))))
+
+;; Apropos markers-to-points:
+;;
+;; This was found useful for performance reasons, so that an
+;; excessive number of live markers aren't kept around in the
+;; `buffer-undo-list'. However, in `markers-to-points', the
+;; set-to-nil markers can't simply be discarded and replaced with
+;; fresh ones in `points-to-markers'. The original marker that was
+;; just set to nil has to be reused.
+;;
+;; This shouldn't bring horrible problems with undo/redo, but it
+;; you never know
+;;
+(defun yas--markers-to-points (snippet)
+ "Convert all markers in SNIPPET to a cons (POINT . MARKER)
+where POINT is the original position of the marker and MARKER is
+the original marker object with the position set to nil."
+ (dolist (field (yas--snippet-fields snippet))
+ (let ((start (marker-position (yas--field-start field)))
+ (end (marker-position (yas--field-end field))))
+ (set-marker (yas--field-start field) nil)
+ (set-marker (yas--field-end field) nil)
+ (setf (yas--field-start field) (cons start (yas--field-start field)))
+ (setf (yas--field-end field) (cons end (yas--field-end field))))
+ (dolist (mirror (yas--field-mirrors field))
+ (let ((start (marker-position (yas--mirror-start mirror)))
+ (end (marker-position (yas--mirror-end mirror))))
+ (set-marker (yas--mirror-start mirror) nil)
+ (set-marker (yas--mirror-end mirror) nil)
+ (setf (yas--mirror-start mirror) (cons start (yas--mirror-start mirror)))
+ (setf (yas--mirror-end mirror) (cons end (yas--mirror-end mirror))))))
+ (let ((snippet-exit (yas--snippet-exit snippet)))
+ (when snippet-exit
+ (let ((exit (marker-position (yas--exit-marker snippet-exit))))
+ (set-marker (yas--exit-marker snippet-exit) nil)
+ (setf (yas--exit-marker snippet-exit) (cons exit (yas--exit-marker snippet-exit)))))))
+
+(defun yas--points-to-markers (snippet)
+ "Convert all cons (POINT . MARKER) in SNIPPET to markers.
+
+This is done by setting MARKER to POINT with `set-marker'."
+ (dolist (field (yas--snippet-fields snippet))
+ (setf (yas--field-start field) (set-marker (cdr (yas--field-start field))
+ (car (yas--field-start field))))
+ (setf (yas--field-end field) (set-marker (cdr (yas--field-end field))
+ (car (yas--field-end field))))
+ (dolist (mirror (yas--field-mirrors field))
+ (setf (yas--mirror-start mirror) (set-marker (cdr (yas--mirror-start mirror))
+ (car (yas--mirror-start mirror))))
+ (setf (yas--mirror-end mirror) (set-marker (cdr (yas--mirror-end mirror))
+ (car (yas--mirror-end mirror))))))
+ (let ((snippet-exit (yas--snippet-exit snippet)))
+ (when snippet-exit
+ (setf (yas--exit-marker snippet-exit) (set-marker (cdr (yas--exit-marker snippet-exit))
+ (car (yas--exit-marker snippet-exit)))))))
+
+(defun yas--field-contains-point-p (field &optional point)
+ (let ((point (or point
+ (point))))
+ (and (>= point (yas--field-start field))
+ (<= point (yas--field-end field)))))
+
+(defun yas--field-text-for-display (field)
+ "Return the propertized display text for field FIELD."
+ (buffer-substring (yas--field-start field) (yas--field-end field)))
+
+(defun yas--undo-in-progress ()
+ "True if some kind of undo is in progress."
+ (or undo-in-progress
+ (eq this-command 'undo)
+ (eq this-command 'redo)))
+
+(defun yas--make-control-overlay (snippet start end)
+ "Create the control overlay that surrounds the snippet and
+holds the keymap."
+ (let ((overlay (make-overlay start
+ end
+ nil
+ nil
+ t)))
+ (overlay-put overlay 'keymap yas-keymap)
+ (overlay-put overlay 'priority 100)
+ (overlay-put overlay 'yas--snippet snippet)
+ overlay))
+
+(defun yas-skip-and-clear-or-delete-char (&optional field)
+ "Clears unmodified field if at field start, skips to next tab.
+
+Otherwise deletes a character normally by calling `delete-char'."
+ (interactive)
+ (let ((field (or field
+ (and yas--active-field-overlay
+ (overlay-buffer yas--active-field-overlay)
+ (overlay-get yas--active-field-overlay 'yas--field)))))
+ (cond ((and field
+ (not (yas--field-modified-p field))
+ (eq (point) (marker-position (yas--field-start field))))
+ (yas--skip-and-clear field)
+ (yas-next-field 1))
+ (t
+ (call-interactively 'delete-char)))))
+
+(defun yas--skip-and-clear (field)
+ "Deletes the region of FIELD and sets it's modified state to t."
+ ;; Just before skipping-and-clearing the field, mark its children
+ ;; fields as modified, too. If the children have mirrors-in-fields
+ ;; this prevents them from updating erroneously (we're skipping and
+ ;; deleting!).
+ ;;
+ (yas--mark-this-and-children-modified field)
+ (delete-region (yas--field-start field) (yas--field-end field)))
+
+(defun yas--mark-this-and-children-modified (field)
+ (setf (yas--field-modified-p field) t)
+ (let ((fom (yas--field-next field)))
+ (while (and fom
+ (yas--fom-parent-field fom))
+ (when (and (eq (yas--fom-parent-field fom) field)
+ (yas--field-p fom))
+ (yas--mark-this-and-children-modified fom))
+ (setq fom (yas--fom-next fom)))))
+
+(defun yas--make-move-active-field-overlay (snippet field)
+ "Place the active field overlay in SNIPPET's FIELD.
+
+Move the overlay, or create it if it does not exit."
+ (if (and yas--active-field-overlay
+ (overlay-buffer yas--active-field-overlay))
+ (move-overlay yas--active-field-overlay
+ (yas--field-start field)
+ (yas--field-end field))
+ (setq yas--active-field-overlay
+ (make-overlay (yas--field-start field)
+ (yas--field-end field)
+ nil nil t))
+ (overlay-put yas--active-field-overlay 'priority 100)
+ (overlay-put yas--active-field-overlay 'face 'yas-field-highlight-face)
+ (overlay-put yas--active-field-overlay 'yas--snippet snippet)
+ (overlay-put yas--active-field-overlay 'modification-hooks '(yas--on-field-overlay-modification))
+ (overlay-put yas--active-field-overlay 'insert-in-front-hooks
+ '(yas--on-field-overlay-modification))
+ (overlay-put yas--active-field-overlay 'insert-behind-hooks
+ '(yas--on-field-overlay-modification))))
+
+(defun yas--on-field-overlay-modification (overlay after? _beg _end &optional _length)
+ "Clears the field and updates mirrors, conditionally.
+
+Only clears the field if it hasn't been modified and point is at
+field start. This hook does nothing if an undo is in progress."
+ (unless (or yas--inhibit-overlay-hooks
+ (not (overlayp yas--active-field-overlay)) ; Avoid Emacs bug #21824.
+ (yas--undo-in-progress))
+ (let* ((field (overlay-get overlay 'yas--field))
+ (snippet (overlay-get yas--active-field-overlay 'yas--snippet)))
+ (cond (after?
+ (yas--advance-end-maybe field (overlay-end overlay))
+ (save-excursion
+ (yas--field-update-display field))
+ (yas--update-mirrors snippet))
+ (field
+ (when (and (eq this-command 'self-insert-command)
+ (not (yas--field-modified-p field))
+ (= (point) (yas--field-start field)))
+ (yas--skip-and-clear field))
+ (setf (yas--field-modified-p field) t))))))
+\f
+;;; Apropos protection overlays:
+;;
+;; These exist for nasty users who will try to delete parts of the
+;; snippet outside the active field. Actual protection happens in
+;; `yas--on-protection-overlay-modification'.
+;;
+;; As of github #537 this no longer inhibits the command by issuing an
+;; error: all the snippets at point, including nested snippets, are
+;; automatically commited and the current command can proceed.
+;;
+(defun yas--make-move-field-protection-overlays (snippet field)
+ "Place protection overlays surrounding SNIPPET's FIELD.
+
+Move the overlays, or create them if they do not exit."
+ (let ((start (yas--field-start field))
+ (end (yas--field-end field)))
+ ;; First check if the (1+ end) is contained in the buffer,
+ ;; otherwise we'll have to do a bit of cheating and silently
+ ;; insert a newline. the `(1+ (buffer-size))' should prevent this
+ ;; when using stacked expansion
+ ;;
+ (when (< (buffer-size) end)
+ (save-excursion
+ (let ((yas--inhibit-overlay-hooks t))
+ (goto-char (point-max))
+ (newline))))
+ ;; go on to normal overlay creation/moving
+ ;;
+ (cond ((and yas--field-protection-overlays
+ (every #'overlay-buffer yas--field-protection-overlays))
+ (move-overlay (first yas--field-protection-overlays) (1- start) start)
+ (move-overlay (second yas--field-protection-overlays) end (1+ end)))
+ (t
+ (setq yas--field-protection-overlays
+ (list (make-overlay (1- start) start nil t nil)
+ (make-overlay end (1+ end) nil t nil)))
+ (dolist (ov yas--field-protection-overlays)
+ (overlay-put ov 'face 'yas--field-debug-face)
+ (overlay-put ov 'yas--snippet snippet)
+ ;; (overlay-put ov 'evaporate t)
+ (overlay-put ov 'modification-hooks '(yas--on-protection-overlay-modification)))))))
+
+(defun yas--on-protection-overlay-modification (_overlay after? _beg _end &optional _length)
+ "Signals a snippet violation, then issues error.
+
+The error should be ignored in `debug-ignored-errors'"
+ (unless (or yas--inhibit-overlay-hooks
+ after?
+ (yas--undo-in-progress))
+ (let ((snippets (yas--snippets-at-point)))
+ (yas--message 3 "Comitting snippets. Action would destroy a protection overlay.")
+ (cl-loop for snippet in snippets
+ do (yas--commit-snippet snippet)))))
+
+(add-to-list 'debug-ignored-errors "^Exit the snippet first!$")
+
+\f
+;;; Snippet expansion and "stacked" expansion:
+;;
+;; Stacked expansion is when you try to expand a snippet when already
+;; inside a snippet expansion.
+;;
+;; The parent snippet does not run its fields modification hooks
+;; (`yas--on-field-overlay-modification' and
+;; `yas--on-protection-overlay-modification') while the child snippet
+;; is active. This means, among other things, that the mirrors of the
+;; parent snippet are not updated, this only happening when one exits
+;; the child snippet.
+;;
+;; Unfortunately, this also puts some ugly (and not fully-tested)
+;; bits of code in `yas-expand-snippet' and
+;; `yas--commit-snippet'. I've tried to mark them with "stacked
+;; expansion:".
+;;
+;; This was thought to be safer in an undo/redo perspective, but
+;; maybe the correct implementation is to make the globals
+;; `yas--active-field-overlay' and `yas--field-protection-overlays' be
+;; snippet-local and be active even while the child snippet is
+;; running. This would mean a lot of overlay modification hooks
+;; running, but if managed correctly (including overlay priorities)
+;; they should account for all situations...
+;;
+(defun yas-expand-snippet (content &optional start end expand-env)
+ "Expand snippet CONTENT at current point.
+
+Text between START and END will be deleted before inserting
+template. EXPAND-ENV is a list of (SYM VALUE) let-style dynamic bindings
+considered when expanding the snippet."
+ (cl-assert (and yas-minor-mode
+ (memq 'yas--post-command-handler post-command-hook))
+ nil
+ "[yas] `yas-expand-snippet' needs properly setup `yas-minor-mode'")
+ (run-hooks 'yas-before-expand-snippet-hook)
+
+ ;;
+ (let* ((yas-selected-text (or yas-selected-text
+ (and (region-active-p)
+ (buffer-substring-no-properties (region-beginning)
+ (region-end)))))
+ (start (or start
+ (and (region-active-p)
+ (region-beginning))
+ (point)))
+ (end (or end
+ (and (region-active-p)
+ (region-end))
+ (point)))
+ (to-delete (and start
+ end
+ (buffer-substring-no-properties start end)))
+ snippet)
+ (goto-char start)
+ (setq yas--indent-original-column (current-column))
+ ;; Delete the region to delete, this *does* get undo-recorded.
+ ;;
+ (when (and to-delete
+ (> end start))
+ (delete-region start end))
+
+ (cond ((listp content)
+ ;; x) This is a snippet-command
+ ;;
+ (yas--eval-lisp-no-saves content))
+ (t
+ ;; x) This is a snippet-snippet :-)
+ ;;
+ ;; Narrow the region down to the content, shoosh the
+ ;; `buffer-undo-list', and create the snippet, the new
+ ;; snippet updates its mirrors once, so we are left with
+ ;; some plain text. The undo action for deleting this
+ ;; plain text will get recorded at the end.
+ ;;
+ ;; stacked expansion: also shoosh the overlay modification hooks
+ (let ((buffer-undo-list t))
+ ;; snippet creation might evaluate users elisp, which
+ ;; might generate errors, so we have to be ready to catch
+ ;; them mostly to make the undo information
+ ;;
+ (setq yas--start-column (current-column))
+ (let ((yas--inhibit-overlay-hooks t))
+ (setq snippet
+ (if expand-env
+ (eval `(let* ,expand-env
+ (insert content)
+ (yas--snippet-create start (point))))
+ (insert content)
+ (yas--snippet-create start (point))))))
+
+ ;; stacked-expansion: This checks for stacked expansion, save the
+ ;; `yas--previous-active-field' and advance its boundary.
+ ;;
+ (let ((existing-field (and yas--active-field-overlay
+ (overlay-buffer yas--active-field-overlay)
+ (overlay-get yas--active-field-overlay 'yas--field))))
+ (when existing-field
+ (setf (yas--snippet-previous-active-field snippet) existing-field)
+ (yas--advance-end-maybe existing-field (overlay-end yas--active-field-overlay))))
+
+ ;; Exit the snippet immediately if no fields
+ ;;
+ (unless (yas--snippet-fields snippet)
+ (yas-exit-snippet snippet))
+
+ ;; Push two undo actions: the deletion of the inserted contents of
+ ;; the new snippet (without the "key") followed by an apply of
+ ;; `yas--take-care-of-redo' on the newly inserted snippet boundaries
+ ;;
+ ;; A small exception, if `yas-also-auto-indent-first-line'
+ ;; is t and `yas--indent' decides to indent the line to a
+ ;; point before the actual expansion point, undo would be
+ ;; messed up. We call the early point "newstart"". case,
+ ;; and attempt to fix undo.
+ ;;
+ (let ((newstart (overlay-start (yas--snippet-control-overlay snippet)))
+ (end (overlay-end (yas--snippet-control-overlay snippet))))
+ (when (< newstart start)
+ (push (cons (make-string (- start newstart) ? ) newstart) buffer-undo-list))
+ (push (cons newstart end) buffer-undo-list)
+ (push `(apply yas--take-care-of-redo ,start ,end ,snippet)
+ buffer-undo-list))
+ ;; Now, schedule a move to the first field
+ ;;
+ (let ((first-field (car (yas--snippet-fields snippet))))
+ (when first-field
+ (sit-for 0) ;; fix issue 125
+ (yas--move-to-field snippet first-field)))
+ (yas--message 3 "snippet expanded.")
+ t))))
+
+(defun yas--take-care-of-redo (_beg _end snippet)
+ "Commits SNIPPET, which in turn pushes an undo action for reviving it.
+
+Meant to exit in the `buffer-undo-list'."
+ ;; slightly optimize: this action is only needed for snippets with
+ ;; at least one field
+ (when (yas--snippet-fields snippet)
+ (yas--commit-snippet snippet)))
+
+(defun yas--snippet-revive (beg end snippet)
+ "Revives SNIPPET and creates a control overlay from BEG to END.
+
+BEG and END are, we hope, the original snippets boundaries.
+All the markers/points exiting existing inside SNIPPET should point
+to their correct locations *at the time the snippet is revived*.
+
+After revival, push the `yas--take-care-of-redo' in the
+`buffer-undo-list'"
+ ;; Reconvert all the points to markers
+ ;;
+ (yas--points-to-markers snippet)
+ ;; When at least one editable field existed in the zombie snippet,
+ ;; try to revive the whole thing...
+ ;;
+ (let ((target-field (or (yas--snippet-active-field snippet)
+ (car (yas--snippet-fields snippet)))))
+ (when target-field
+ (setf (yas--snippet-control-overlay snippet) (yas--make-control-overlay snippet beg end))
+ (overlay-put (yas--snippet-control-overlay snippet) 'yas--snippet snippet)
+
+ (yas--move-to-field snippet target-field)
+
+ (push `(apply yas--take-care-of-redo ,beg ,end ,snippet)
+ buffer-undo-list))))
+
+(defun yas--snippet-create (begin end)
+ "Create a snippet from a template inserted at BEGIN to END.
+
+Returns the newly created snippet."
+ (save-restriction
+ (narrow-to-region begin end)
+ (let ((snippet (yas--make-snippet)))
+ (goto-char begin)
+ (yas--snippet-parse-create snippet)
+
+ ;; Sort and link each field
+ (yas--snippet-sort-fields snippet)
+
+ ;; Create keymap overlay for snippet
+ (setf (yas--snippet-control-overlay snippet)
+ (yas--make-control-overlay snippet (point-min) (point-max)))
+
+ ;; Move to end
+ (goto-char (point-max))
+
+ snippet)))
+
+\f
+;;; Apropos adjacencies and "fom's":
+;;
+;; Once the $-constructs bits like "$n" and "${:n" are deleted in the
+;; recently expanded snippet, we might actually have many fields,
+;; mirrors (and the snippet exit) in the very same position in the
+;; buffer. Therefore we need to single-link the
+;; fields-or-mirrors-or-exit (which I have abbreviated to "fom")
+;; according to their original positions in the buffer.
+;;
+;; Then we have operation `yas--advance-end-maybe' and
+;; `yas--advance-start-maybe', which conditionally push the starts and
+;; ends of these foms down the chain.
+;;
+;; This allows for like the printf with the magic ",":
+;;
+;; printf ("${1:%s}\\n"${1:$(if (string-match "%" text) "," "\);")} \
+;; $2${1:$(if (string-match "%" text) "\);" "")}$0
+;;
+(defun yas--fom-start (fom)
+ (cond ((yas--field-p fom)
+ (yas--field-start fom))
+ ((yas--mirror-p fom)
+ (yas--mirror-start fom))
+ (t
+ (yas--exit-marker fom))))
+
+(defun yas--fom-end (fom)
+ (cond ((yas--field-p fom)
+ (yas--field-end fom))
+ ((yas--mirror-p fom)
+ (yas--mirror-end fom))
+ (t
+ (yas--exit-marker fom))))
+
+(defun yas--fom-next (fom)
+ (cond ((yas--field-p fom)
+ (yas--field-next fom))
+ ((yas--mirror-p fom)
+ (yas--mirror-next fom))
+ (t
+ (yas--exit-next fom))))
+
+(defun yas--fom-parent-field (fom)
+ (cond ((yas--field-p fom)
+ (yas--field-parent-field fom))
+ ((yas--mirror-p fom)
+ (yas--mirror-parent-field fom))
+ (t
+ nil)))
+
+(defun yas--calculate-adjacencies (snippet)
+ "Calculate adjacencies for fields or mirrors of SNIPPET.
+
+This is according to their relative positions in the buffer, and
+has to be called before the $-constructs are deleted."
+ (let* ((fom-set-next-fom
+ (lambda (fom nextfom)
+ (cond ((yas--field-p fom)
+ (setf (yas--field-next fom) nextfom))
+ ((yas--mirror-p fom)
+ (setf (yas--mirror-next fom) nextfom))
+ (t
+ (setf (yas--exit-next fom) nextfom)))))
+ (compare-fom-begs
+ (lambda (fom1 fom2)
+ (if (= (yas--fom-start fom2) (yas--fom-start fom1))
+ (yas--mirror-p fom2)
+ (>= (yas--fom-start fom2) (yas--fom-start fom1)))))
+ (link-foms fom-set-next-fom))
+ ;; make some yas--field, yas--mirror and yas--exit soup
+ (let ((soup))
+ (when (yas--snippet-exit snippet)
+ (push (yas--snippet-exit snippet) soup))
+ (dolist (field (yas--snippet-fields snippet))
+ (push field soup)
+ (dolist (mirror (yas--field-mirrors field))
+ (push mirror soup)))
+ (setq soup
+ (sort soup compare-fom-begs))
+ (when soup
+ (reduce link-foms soup)))))
+
+(defun yas--calculate-mirrors-in-fields (snippet mirror)
+ "Attempt to assign a parent field of SNIPPET to the mirror MIRROR.
+
+Use the tightest containing field if more than one field contains
+the mirror. Intended to be called *before* the dollar-regions are
+deleted."
+ (let ((min (point-min))
+ (max (point-max)))
+ (dolist (field (yas--snippet-fields snippet))
+ (when (and (<= (yas--field-start field) (yas--mirror-start mirror))
+ (<= (yas--mirror-end mirror) (yas--field-end field))
+ (< min (yas--field-start field))
+ (< (yas--field-end field) max))
+ (setq min (yas--field-start field)
+ max (yas--field-end field))
+ (setf (yas--mirror-parent-field mirror) field)))))
+
+(defun yas--advance-end-maybe (fom newend)
+ "Maybe advance FOM's end to NEWEND if it needs it.
+
+If it does, also:
+
+* call `yas--advance-start-maybe' on FOM's next fom.
+
+* in case FOM is field call `yas--advance-end-maybe' on its parent
+ field
+
+Also, if FOM is an exit-marker, always call
+`yas--advance-start-maybe' on its next fom. This is because
+exit-marker have identical start and end markers."
+ (cond ((and fom (< (yas--fom-end fom) newend))
+ (set-marker (yas--fom-end fom) newend)
+ (yas--advance-start-maybe (yas--fom-next fom) newend)
+ (yas--advance-end-of-parents-maybe (yas--fom-parent-field fom) newend))
+ ((yas--exit-p fom)
+ (yas--advance-start-maybe (yas--fom-next fom) newend))))
+
+(defun yas--advance-start-maybe (fom newstart)
+ "Maybe advance FOM's start to NEWSTART if it needs it.
+
+If it does, also call `yas--advance-end-maybe' on FOM."
+ (when (and fom (< (yas--fom-start fom) newstart))
+ (set-marker (yas--fom-start fom) newstart)
+ (yas--advance-end-maybe fom newstart)))
+
+(defun yas--advance-end-of-parents-maybe (field newend)
+ "Like `yas--advance-end-maybe' but for parent fields.
+
+Only works for fields and doesn't care about the start of the
+next FOM. Works its way up recursively for parents of parents."
+ (when (and field
+ (< (yas--field-end field) newend))
+ (set-marker (yas--field-end field) newend)
+ (yas--advance-end-of-parents-maybe (yas--field-parent-field field) newend)))
+
+(defvar yas--dollar-regions nil
+ "When expanding the snippet the \"parse-create\" functions add
+cons cells to this var.")
+
+(defvar yas--backquote-markers-and-strings nil
+ "List of (MARKER . STRING) marking where the values from
+backquoted Lisp expressions should be inserted at the end of
+expansion.")
+
+(defun yas--snippet-parse-create (snippet)
+ "Parse a recently inserted snippet template, creating all
+necessary fields, mirrors and exit points.
+
+Meant to be called in a narrowed buffer, does various passes"
+ (let ((parse-start (point)))
+ ;; Reset the yas--dollar-regions
+ ;;
+ (setq yas--dollar-regions nil)
+ ;; protect just the backquotes
+ ;;
+ (yas--protect-escapes nil '(?`))
+ ;; replace all backquoted expressions
+ ;;
+ (goto-char parse-start)
+ (yas--save-backquotes)
+ ;; protect escaped characters
+ ;;
+ (yas--protect-escapes)
+ ;; parse fields with {}
+ ;;
+ (goto-char parse-start)
+ (yas--field-parse-create snippet)
+ ;; parse simple mirrors and fields
+ ;;
+ (goto-char parse-start)
+ (yas--simple-mirror-parse-create snippet)
+ ;; parse mirror transforms
+ ;;
+ (goto-char parse-start)
+ (yas--transform-mirror-parse-create snippet)
+ ;; calculate adjacencies of fields and mirrors
+ ;;
+ (yas--calculate-adjacencies snippet)
+ ;; Delete $-constructs
+ ;;
+ (save-restriction (widen) (yas--delete-regions yas--dollar-regions))
+ ;; restore backquoted expression values
+ ;;
+ (yas--restore-backquotes)
+ ;; restore escapes
+ ;;
+ (goto-char parse-start)
+ (yas--restore-escapes)
+ ;; update mirrors for the first time
+ ;;
+ (yas--update-mirrors snippet)
+ ;; indent the best we can
+ ;;
+ (goto-char parse-start)
+ (yas--indent snippet)))
+
+(defun yas--indent-according-to-mode (snippet-markers)
+ "Indent current line according to mode, preserving SNIPPET-MARKERS."
+ ;;; Apropos indenting problems....
+ ;;
+ ;; `indent-according-to-mode' uses whatever `indent-line-function'
+ ;; is available. Some implementations of these functions delete text
+ ;; before they insert. If there happens to be a marker just after
+ ;; the text being deleted, the insertion actually happens after the
+ ;; marker, which misplaces it.
+ ;;
+ ;; This would also happen if we had used overlays with the
+ ;; `front-advance' property set to nil.
+ ;;
+ ;; This is why I have these `trouble-markers', they are the ones at
+ ;; they are the ones at the first non-whitespace char at the line
+ ;; (i.e. at `yas--real-line-beginning'. After indentation takes place
+ ;; we should be at the correct to restore them to. All other
+ ;; non-trouble-markers have been *pushed* and don't need special
+ ;; attention.
+ ;;
+ (goto-char (yas--real-line-beginning))
+ (let ((trouble-markers (remove-if-not #'(lambda (marker)
+ (= marker (point)))
+ snippet-markers)))
+ (save-restriction
+ (widen)
+ (condition-case _
+ (indent-according-to-mode)
+ (error (yas--message 3 "Warning: `yas--indent-according-to-mode' having problems running %s" indent-line-function)
+ nil)))
+ (mapc #'(lambda (marker)
+ (set-marker marker (point)))
+ trouble-markers)))
+
+(defvar yas--indent-original-column nil)
+(defun yas--indent (snippet)
+ (let ((snippet-markers (yas--collect-snippet-markers snippet)))
+ ;; Look for those $>
+ (save-excursion
+ (while (re-search-forward "$>" nil t)
+ (delete-region (match-beginning 0) (match-end 0))
+ (when (not (eq yas-indent-line 'auto))
+ (yas--indent-according-to-mode snippet-markers))))
+ ;; Now do stuff for 'fixed and 'auto
+ (save-excursion
+ (cond ((eq yas-indent-line 'fixed)
+ (while (and (zerop (forward-line))
+ (zerop (current-column)))
+ (indent-to-column yas--indent-original-column)))
+ ((eq yas-indent-line 'auto)
+ (let ((end (set-marker (make-marker) (point-max)))
+ (indent-first-line-p yas-also-auto-indent-first-line))
+ (while (and (zerop (if indent-first-line-p
+ (prog1
+ (forward-line 0)
+ (setq indent-first-line-p nil))
+ (forward-line 1)))
+ (not (eobp))
+ (<= (point) end))
+ (yas--indent-according-to-mode snippet-markers))))
+ (t
+ nil)))))
+
+(defun yas--collect-snippet-markers (snippet)
+ "Make a list of all the markers used by SNIPPET."
+ (let (markers)
+ (dolist (field (yas--snippet-fields snippet))
+ (push (yas--field-start field) markers)
+ (push (yas--field-end field) markers)
+ (dolist (mirror (yas--field-mirrors field))
+ (push (yas--mirror-start mirror) markers)
+ (push (yas--mirror-end mirror) markers)))
+ (let ((snippet-exit (yas--snippet-exit snippet)))
+ (when (and snippet-exit
+ (marker-buffer (yas--exit-marker snippet-exit)))
+ (push (yas--exit-marker snippet-exit) markers)))
+ markers))
+
+(defun yas--real-line-beginning ()
+ (let ((c (char-after (line-beginning-position)))
+ (n (line-beginning-position)))
+ (while (or (eql c ?\ )
+ (eql c ?\t))
+ (cl-incf n)
+ (setq c (char-after n)))
+ n))
+
+(defun yas--escape-string (escaped)
+ (concat "YASESCAPE" (format "%d" escaped) "PROTECTGUARD"))
+
+(defun yas--protect-escapes (&optional text escaped)
+ "Protect all escaped characters with their numeric ASCII value.
+
+With optional string TEXT do it in string instead of buffer."
+ (let ((changed-text text)
+ (text-provided-p text))
+ (mapc #'(lambda (escaped)
+ (setq changed-text
+ (yas--replace-all (concat "\\" (char-to-string escaped))
+ (yas--escape-string escaped)
+ (when text-provided-p changed-text))))
+ (or escaped yas--escaped-characters))
+ changed-text))
+
+(defun yas--restore-escapes (&optional text escaped)
+ "Restore all escaped characters from their numeric ASCII value.
+
+With optional string TEXT do it in string instead of the buffer."
+ (let ((changed-text text)
+ (text-provided-p text))
+ (mapc #'(lambda (escaped)
+ (setq changed-text
+ (yas--replace-all (yas--escape-string escaped)
+ (char-to-string escaped)
+ (when text-provided-p changed-text))))
+ (or escaped yas--escaped-characters))
+ changed-text))
+
+(defun yas--save-backquotes ()
+ "Save all the \"`(lisp-expression)`\"-style expressions
+with their evaluated value into `yas--backquote-markers-and-strings'."
+ (while (re-search-forward yas--backquote-lisp-expression-regexp nil t)
+ (let ((current-string (match-string-no-properties 1)) transformed)
+ (save-restriction (widen)
+ (delete-region (match-beginning 0) (match-end 0)))
+ (setq transformed (yas--eval-lisp (yas--read-lisp (yas--restore-escapes current-string '(?`)))))
+ (goto-char (match-beginning 0))
+ (when transformed
+ (let ((marker (make-marker)))
+ (save-restriction
+ (widen)
+ (insert "Y") ;; quite horrendous, I love it :)
+ (set-marker marker (point))
+ (insert "Y"))
+ (push (cons marker transformed) yas--backquote-markers-and-strings))))))
+
+(defun yas--restore-backquotes ()
+ "Replace markers in `yas--backquote-markers-and-strings' with their values."
+ (while yas--backquote-markers-and-strings
+ (let* ((marker-and-string (pop yas--backquote-markers-and-strings))
+ (marker (car marker-and-string))
+ (string (cdr marker-and-string)))
+ (save-excursion
+ (goto-char marker)
+ (save-restriction
+ (widen)
+ (delete-char -1)
+ (insert string)
+ (delete-char 1))
+ (set-marker marker nil)))))
+
+(defun yas--scan-sexps (from count)
+ (ignore-errors
+ (save-match-data ; `scan-sexps' may modify match data.
+ (with-syntax-table (standard-syntax-table)
+ (scan-sexps from count)))))
+
+(defun yas--make-marker (pos)
+ "Create a marker at POS with nil `marker-insertion-type'."
+ (let ((marker (set-marker (make-marker) pos)))
+ (set-marker-insertion-type marker nil)
+ marker))
+
+(defun yas--field-parse-create (snippet &optional parent-field)
+ "Parse most field expressions in SNIPPET, except for the simple one \"$n\".
+
+The following count as a field:
+
+* \"${n: text}\", for a numbered field with default text, as long as N is not 0;
+
+* \"${n: text$(expression)}, the same with a Lisp expression;
+ this is caught with the curiously named `yas--multi-dollar-lisp-expression-regexp'
+
+* the same as above but unnumbered, (no N:) and number is calculated automatically.
+
+When multiple expressions are found, only the last one counts."
+ ;;
+ (save-excursion
+ (while (re-search-forward yas--field-regexp nil t)
+ (let* ((real-match-end-0 (yas--scan-sexps (1+ (match-beginning 0)) 1))
+ (number (and (match-string-no-properties 1)
+ (string-to-number (match-string-no-properties 1))))
+ (brand-new-field (and real-match-end-0
+ ;; break if on "$(" immediately
+ ;; after the ":", this will be
+ ;; caught as a mirror with
+ ;; transform later.
+ (not (string-match-p "\\`\\$[ \t\n]*("
+ (match-string-no-properties 2)))
+ ;; allow ${0: some exit text}
+ ;; (not (and number (zerop number)))
+ (yas--make-field number
+ (yas--make-marker (match-beginning 2))
+ (yas--make-marker (1- real-match-end-0))
+ parent-field))))
+ (when brand-new-field
+ (goto-char real-match-end-0)
+ (push (cons (1- real-match-end-0) real-match-end-0)
+ yas--dollar-regions)
+ (push (cons (match-beginning 0) (match-beginning 2))
+ yas--dollar-regions)
+ (push brand-new-field (yas--snippet-fields snippet))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region (yas--field-start brand-new-field) (yas--field-end brand-new-field))
+ (goto-char (point-min))
+ (yas--field-parse-create snippet brand-new-field)))))))
+ ;; if we entered from a parent field, now search for the
+ ;; `yas--multi-dollar-lisp-expression-regexp'. This is used for
+ ;; primary field transformations
+ ;;
+ (when parent-field
+ (save-excursion
+ (while (re-search-forward yas--multi-dollar-lisp-expression-regexp nil t)
+ (let* ((real-match-end-1 (yas--scan-sexps (match-beginning 1) 1)))
+ ;; commit the primary field transformation if:
+ ;;
+ ;; 1. we don't find it in yas--dollar-regions (a subnested
+ ;; field) might have already caught it.
+ ;;
+ ;; 2. we really make sure we have either two '$' or some
+ ;; text and a '$' after the colon ':'. This is a FIXME: work
+ ;; my regular expressions and end these ugly hacks.
+ ;;
+ (when (and real-match-end-1
+ (not (member (cons (match-beginning 0)
+ real-match-end-1)
+ yas--dollar-regions))
+ (not (eq ?:
+ (char-before (1- (match-beginning 1))))))
+ (let ((lisp-expression-string (buffer-substring-no-properties (match-beginning 1)
+ real-match-end-1)))
+ (setf (yas--field-transform parent-field)
+ (yas--read-lisp (yas--restore-escapes lisp-expression-string))))
+ (push (cons (match-beginning 0) real-match-end-1)
+ yas--dollar-regions)))))))
+
+(defun yas--transform-mirror-parse-create (snippet)
+ "Parse the \"${n:$(lisp-expression)}\" mirror transformations in SNIPPET."
+ (while (re-search-forward yas--transform-mirror-regexp nil t)
+ (let* ((real-match-end-0 (yas--scan-sexps (1+ (match-beginning 0)) 1))
+ (number (string-to-number (match-string-no-properties 1)))
+ (field (and number
+ (not (zerop number))
+ (yas--snippet-find-field snippet number)))
+ (brand-new-mirror
+ (and real-match-end-0
+ field
+ (yas--make-mirror (yas--make-marker (match-beginning 0))
+ (yas--make-marker (match-beginning 0))
+ (yas--read-lisp
+ (yas--restore-escapes
+ (buffer-substring-no-properties (match-beginning 2)
+ (1- real-match-end-0))))))))
+ (when brand-new-mirror
+ (push brand-new-mirror
+ (yas--field-mirrors field))
+ (yas--calculate-mirrors-in-fields snippet brand-new-mirror)
+ (push (cons (match-beginning 0) real-match-end-0) yas--dollar-regions)))))
+
+(defun yas--simple-mirror-parse-create (snippet)
+ "Parse the simple \"$n\" fields/mirrors/exitmarkers in SNIPPET."
+ (while (re-search-forward yas--simple-mirror-regexp nil t)
+ (let ((number (string-to-number (match-string-no-properties 1))))
+ (cond ((zerop number)
+
+ (setf (yas--snippet-exit snippet)
+ (yas--make-exit (yas--make-marker (match-end 0))))
+ (save-excursion
+ (goto-char (match-beginning 0))
+ (when yas-wrap-around-region
+ (cond (yas-selected-text
+ (insert yas-selected-text))
+ ((and (eq yas-wrap-around-region 'cua)
+ cua-mode
+ (get-register ?0))
+ (insert (prog1 (get-register ?0)
+ (set-register ?0 nil))))))
+ (push (cons (point) (yas--exit-marker (yas--snippet-exit snippet)))
+ yas--dollar-regions)))
+ (t
+ (let ((field (yas--snippet-find-field snippet number)))
+ (if field
+ (let ((brand-new-mirror (yas--make-mirror
+ (yas--make-marker (match-beginning 0))
+ (yas--make-marker (match-beginning 0))
+ nil)))
+ (push brand-new-mirror
+ (yas--field-mirrors field))
+ (yas--calculate-mirrors-in-fields snippet brand-new-mirror))
+ (push (yas--make-field number
+ (yas--make-marker (match-beginning 0))
+ (yas--make-marker (match-beginning 0))
+ nil)
+ (yas--snippet-fields snippet))))
+ (push (cons (match-beginning 0) (match-end 0))
+ yas--dollar-regions))))))
+
+(defun yas--delete-regions (regions)
+ "Sort disjuct REGIONS by start point, then delete from the back."
+ (mapc #'(lambda (reg)
+ (delete-region (car reg) (cdr reg)))
+ (sort regions
+ #'(lambda (r1 r2)
+ (>= (car r1) (car r2))))))
+
+(defun yas--calculate-mirror-depth (mirror &optional traversed)
+ (let* ((parent (yas--mirror-parent-field mirror))
+ (parents-mirrors (and parent
+ (yas--field-mirrors parent))))
+ (or (yas--mirror-depth mirror)
+ (setf (yas--mirror-depth mirror)
+ (cond ((memq mirror traversed)
+ 0)
+ ((and parent parents-mirrors)
+ (1+ (reduce #'max
+ (mapcar #'(lambda (m)
+ (yas--calculate-mirror-depth m
+ (cons mirror
+ traversed)))
+ parents-mirrors))))
+ (parent
+ 1)
+ (t
+ 0))))))
+
+(defun yas--update-mirrors (snippet)
+ "Update all the mirrors of SNIPPET."
+ (save-excursion
+ (dolist (field-and-mirror (sort
+ ;; make a list of ((F1 . M1) (F1 . M2) (F2 . M3) (F2 . M4) ...)
+ ;; where F is the field that M is mirroring
+ ;;
+ (mapcan #'(lambda (field)
+ (mapcar #'(lambda (mirror)
+ (cons field mirror))
+ (yas--field-mirrors field)))
+ (yas--snippet-fields snippet))
+ ;; then sort this list so that entries with mirrors with parent
+ ;; fields appear before. This was important for fixing #290, and
+ ;; luckily also handles the case where a mirror in a field causes
+ ;; another mirror to need reupdating
+ ;;
+ #'(lambda (field-and-mirror1 field-and-mirror2)
+ (> (yas--calculate-mirror-depth (cdr field-and-mirror1))
+ (yas--calculate-mirror-depth (cdr field-and-mirror2))))))
+ (let* ((field (car field-and-mirror))
+ (mirror (cdr field-and-mirror))
+ (parent-field (yas--mirror-parent-field mirror)))
+ ;; before updating a mirror with a parent-field, maybe advance
+ ;; its start (#290)
+ ;;
+ (when parent-field
+ (yas--advance-start-maybe mirror (yas--fom-start parent-field)))
+ ;; update this mirror
+ ;;
+ (yas--mirror-update-display mirror field)
+ ;; `yas--place-overlays' is needed if the active field and
+ ;; protected overlays have been changed because of insertions
+ ;; in `yas--mirror-update-display'
+ ;;
+ (when (eq field (yas--snippet-active-field snippet))
+ (yas--place-overlays snippet field))))))
+
+(defun yas--mirror-update-display (mirror field)
+ "Update MIRROR according to FIELD (and mirror transform)."
+
+ (let* ((mirror-parent-field (yas--mirror-parent-field mirror))
+ (reflection (and (not (and mirror-parent-field
+ (yas--field-modified-p mirror-parent-field)))
+ (or (yas--apply-transform mirror field 'empty-on-nil)
+ (yas--field-text-for-display field)))))
+ (when (and reflection
+ (not (string= reflection (buffer-substring-no-properties (yas--mirror-start mirror)
+ (yas--mirror-end mirror)))))
+ (goto-char (yas--mirror-start mirror))
+ (let ((yas--inhibit-overlay-hooks t))
+ (insert reflection))
+ (if (> (yas--mirror-end mirror) (point))
+ (delete-region (point) (yas--mirror-end mirror))
+ (set-marker (yas--mirror-end mirror) (point))
+ (yas--advance-start-maybe (yas--mirror-next mirror) (point))
+ ;; super-special advance
+ (yas--advance-end-of-parents-maybe mirror-parent-field (point))))))
+
+(defun yas--field-update-display (field)
+ "Much like `yas--mirror-update-display', but for fields."
+ (when (yas--field-transform field)
+ (let ((transformed (and (not (eq (yas--field-number field) 0))
+ (yas--apply-transform field field))))
+ (when (and transformed
+ (not (string= transformed (buffer-substring-no-properties (yas--field-start field)
+ (yas--field-end field)))))
+ (setf (yas--field-modified-p field) t)
+ (goto-char (yas--field-start field))
+ (let ((yas--inhibit-overlay-hooks t))
+ (insert transformed)
+ (if (> (yas--field-end field) (point))
+ (delete-region (point) (yas--field-end field))
+ (set-marker (yas--field-end field) (point))
+ (yas--advance-start-maybe (yas--field-next field) (point)))
+ t)))))
+
+\f
+;;; Post-command hook:
+;;
+(defun yas--post-command-handler ()
+ "Handles various yasnippet conditions after each command."
+ (cond ((eq 'undo this-command)
+ ;;
+ ;; After undo revival the correct field is sometimes not
+ ;; restored correctly, this condition handles that
+ ;;
+ (let* ((snippet (car (yas--snippets-at-point)))
+ (target-field (and snippet
+ (find-if-not #'(lambda (field)
+ (yas--field-probably-deleted-p snippet field))
+ (remove nil
+ (cons (yas--snippet-active-field snippet)
+ (yas--snippet-fields snippet)))))))
+ (when target-field
+ (yas--move-to-field snippet target-field))))
+ ((not (yas--undo-in-progress))
+ ;; When not in an undo, check if we must commit the snippet
+ ;; (user exited it).
+ (yas--check-commit-snippet))))
+\f
+;;; Fancy docs:
+;;
+;; The docstrings for some functions are generated dynamically
+;; depending on the context.
+;;
+(put 'yas-expand 'function-documentation
+ '(yas--expand-from-trigger-key-doc t))
+(defun yas--expand-from-trigger-key-doc (context)
+ "A doc synthesizer for `yas--expand-from-trigger-key-doc'."
+ (let* ((yas-fallback-behavior (and context yas-fallback-behavior))
+ (fallback-description
+ (cond ((eq yas-fallback-behavior 'call-other-command)
+ (let* ((fallback (yas--keybinding-beyond-yasnippet)))
+ (or (and fallback
+ (format "call command `%s'."
+ (pp-to-string fallback)))
+ "do nothing (`yas-expand' doesn't override\nanything).")))
+ ((eq yas-fallback-behavior 'return-nil)
+ "do nothing.")
+ (t "defer to `yas-fallback-behavior' (which see)."))))
+ (concat "Expand a snippet before point. If no snippet
+expansion is possible, "
+ fallback-description
+ "\n\nOptional argument FIELD is for non-interactive use and is an
+object satisfying `yas--field-p' to restrict the expansion to.")))
+
+(put 'yas-expand-from-keymap 'function-documentation
+ '(yas--expand-from-keymap-doc t))
+(defun yas--expand-from-keymap-doc (context)
+ "A doc synthesizer for `yas--expand-from-keymap-doc'."
+ (add-hook 'temp-buffer-show-hook 'yas--snippet-description-finish-runonce)
+ (concat "Expand/run snippets from keymaps, possibly falling back to original binding.\n"
+ (when (and context (eq this-command 'describe-key))
+ (let* ((vec (this-single-command-keys))
+ (templates (mapcan #'(lambda (table)
+ (yas--fetch table vec))
+ (yas--get-snippet-tables)))
+ (yas--direct-keymaps nil)
+ (fallback (key-binding vec)))
+ (concat "In this case, "
+ (when templates
+ (concat "these snippets are bound to this key:\n"
+ (yas--template-pretty-list templates)
+ "\n\nIf none of these expands, "))
+ (or (and fallback
+ (format "fallback `%s' will be called." (pp-to-string fallback)))
+ "no fallback keybinding is called."))))))
+
+(defun yas--template-pretty-list (templates)
+ (let ((acc)
+ (yas-buffer-local-condition 'always))
+ (dolist (plate templates)
+ (setq acc (concat acc "\n*) "
+ (propertize (concat "\\\\snippet `" (car plate) "'")
+ 'yasnippet (cdr plate)))))
+ acc))
+
+(define-button-type 'help-snippet-def
+ :supertype 'help-xref
+ 'help-function (lambda (template) (yas--visit-snippet-file-1 template))
+ 'help-echo (purecopy "mouse-2, RET: find snippets's definition"))
+
+(defun yas--snippet-description-finish-runonce ()
+ "Final adjustments for the help buffer when snippets are concerned."
+ (yas--create-snippet-xrefs)
+ (remove-hook 'temp-buffer-show-hook 'yas--snippet-description-finish-runonce))
+
+(defun yas--create-snippet-xrefs ()
+ (save-excursion
+ (goto-char (point-min))
+ (while (search-forward-regexp "\\\\\\\\snippet[ \s\t]+`\\([^']+\\)'" nil t)
+ (let ((template (get-text-property (match-beginning 1)
+ 'yasnippet)))
+ (when template
+ (help-xref-button 1 'help-snippet-def template)
+ (kill-region (match-end 1) (match-end 0))
+ (kill-region (match-beginning 0) (match-beginning 1)))))))
+\f
+;;; Utils
+
+(defvar yas-verbosity 4
+ "Log level for `yas--message' 4 means trace most anything, 0 means nothing.")
+
+(defun yas--message (level message &rest args)
+ "When LEVEL is above `yas-verbosity-level', log MESSAGE and ARGS."
+ (when (> yas-verbosity level)
+ (message "%s" (apply #'yas--format message args))))
+
+(defun yas--warning (format-control &rest format-args)
+ (let ((msg (apply #'format format-control format-args)))
+ (display-warning 'yasnippet msg :warning)
+ (yas--message 1 msg)))
+
+(defun yas--format (format-control &rest format-args)
+ (apply #'format (concat "[yas] " format-control) format-args))
+
+\f
+;;; Some hacks:
+;;
+;; The functions
+;;
+;; `locate-dominating-file'
+;; `region-active-p'
+;;
+;; added for compatibility in emacsen < 23
+(unless (>= emacs-major-version 23)
+ (unless (fboundp 'region-active-p)
+ (defun region-active-p () (and transient-mark-mode mark-active)))
+
+ (unless (fboundp 'locate-dominating-file)
+ (defvar locate-dominating-stop-dir-regexp
+ "\\`\\(?:[\\/][\\/][^\\/]+[\\/]\\|/\\(?:net\\|afs\\|\\.\\.\\.\\)/\\)\\'"
+ "Regexp of directory names which stop the search in `locate-dominating-file'.
+Any directory whose name matches this regexp will be treated like
+a kind of root directory by `locate-dominating-file' which will stop its search
+when it bumps into it.
+The default regexp prevents fruitless and time-consuming attempts to find
+special files in directories in which filenames are interpreted as hostnames,
+or mount points potentially requiring authentication as a different user.")
+
+ (defun locate-dominating-file (file name)
+ "Look up the directory hierarchy from FILE for a file named NAME.
+Stop at the first parent directory containing a file NAME,
+and return the directory. Return nil if not found."
+ ;; We used to use the above locate-dominating-files code, but the
+ ;; directory-files call is very costly, so we're much better off doing
+ ;; multiple calls using the code in here.
+ ;;
+ ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
+ ;; `name' in /home or in /.
+ (setq file (abbreviate-file-name file))
+ (let ((root nil)
+ try)
+ (while (not (or root
+ (null file)
+ ;; FIXME: Disabled this heuristic because it is sometimes
+ ;; inappropriate.
+ ;; As a heuristic, we stop looking up the hierarchy of
+ ;; directories as soon as we find a directory belonging
+ ;; to another user. This should save us from looking in
+ ;; things like /net and /afs. This assumes that all the
+ ;; files inside a project belong to the same user.
+ ;; (let ((prev-user user))
+ ;; (setq user (nth 2 (file-attributes file)))
+ ;; (and prev-user (not (equal user prev-user))))
+ (string-match locate-dominating-stop-dir-regexp file)))
+ (setq try (file-exists-p (expand-file-name name file)))
+ (cond (try (setq root file))
+ ((equal file (setq file (file-name-directory
+ (directory-file-name file))))
+ (setq file nil))))
+ root))))
+
+\f
+;;; Backward compatibility to yasnippet <= 0.7
+
+(defun yas-initialize ()
+ "For backward compatibility, enable `yas-minor-mode' globally."
+ (declare (obsolete "Use (yas-global-mode 1) instead." "0.8"))
+ (yas-global-mode 1))
+
+(defvar yas--backported-syms '(;; `defcustom's
+ ;;
+ yas-snippet-dirs
+ yas-prompt-functions
+ yas-indent-line
+ yas-also-auto-indent-first-line
+ yas-snippet-revival
+ yas-triggers-in-field
+ yas-fallback-behavior
+ yas-choose-keys-first
+ yas-choose-tables-first
+ yas-use-menu
+ yas-trigger-symbol
+ yas-wrap-around-region
+ yas-good-grace
+ yas-visit-from-menu
+ yas-expand-only-for-last-commands
+ yas-field-highlight-face
+
+ ;; these vars can be customized as well
+ ;;
+ yas-keymap
+ yas-verbosity
+ yas-extra-modes
+ yas-key-syntaxes
+ yas-after-exit-snippet-hook
+ yas-before-expand-snippet-hook
+ yas-buffer-local-condition
+ yas-dont-activate
+
+ ;; prompting functions
+ ;;
+ yas-x-prompt
+ yas-ido-prompt
+ yas-no-prompt
+ yas-completing-prompt
+ yas-dropdown-prompt
+
+ ;; interactive functions
+ ;;
+ yas-expand
+ yas-minor-mode
+ yas-global-mode
+ yas-direct-keymaps-reload
+ yas-minor-mode-on
+ yas-load-directory
+ yas-reload-all
+ yas-compile-directory
+ yas-recompile-all
+ yas-about
+ yas-expand-from-trigger-key
+ yas-expand-from-keymap
+ yas-insert-snippet
+ yas-visit-snippet-file
+ yas-new-snippet
+ yas-load-snippet-buffer
+ yas-tryout-snippet
+ yas-describe-tables
+ yas-next-field-or-maybe-expand
+ yas-next-field
+ yas-prev-field
+ yas-abort-snippet
+ yas-exit-snippet
+ yas-exit-all-snippets
+ yas-skip-and-clear-or-delete-char
+ yas-initialize
+
+ ;; symbols that I "exported" for use
+ ;; in snippets and hookage
+ ;;
+ yas-expand-snippet
+ yas-define-snippets
+ yas-define-menu
+ yas-snippet-beg
+ yas-snippet-end
+ yas-modified-p
+ yas-moving-away-p
+ yas-substr
+ yas-choose-value
+ yas-key-to-value
+ yas-throw
+ yas-verify-value
+ yas-field-value
+ yas-text
+ yas-selected-text
+ yas-default-from-field
+ yas-inside-string
+ yas-unimplemented
+ yas-define-condition-cache
+ yas-hippie-try-expand
+
+ ;; debug definitions
+ ;; yas-debug-snippet-vars
+ ;; yas-exterminate-package
+ ;; yas-debug-test
+
+ ;; testing definitions
+ ;; yas-should-expand
+ ;; yas-should-not-expand
+ ;; yas-mock-insert
+ ;; yas-make-file-or-dirs
+ ;; yas-variables
+ ;; yas-saving-variables
+ ;; yas-call-with-snippet-dirs
+ ;; yas-with-snippet-dirs
+)
+ "Backported yasnippet symbols.
+
+They are mapped to \"yas/*\" variants.")
+
+(dolist (sym yas--backported-syms)
+ (let ((backported (intern (replace-regexp-in-string "^yas-" "yas/" (symbol-name sym)))))
+ (when (boundp sym)
+ (make-obsolete-variable backported sym "yasnippet 0.8")
+ (defvaralias backported sym))
+ (when (fboundp sym)
+ (make-obsolete backported sym "yasnippet 0.8")
+ (defalias backported sym))))
+
+(defvar yas--exported-syms
+ (let (exported)
+ (mapatoms (lambda (atom)
+ (if (and (or (and (boundp atom)
+ (not (get atom 'byte-obsolete-variable)))
+ (and (fboundp atom)
+ (not (get atom 'byte-obsolete-info))))
+ (string-match-p "^yas-[^-]" (symbol-name atom)))
+ (push atom exported))))
+ exported)
+ "Exported yasnippet symbols.
+
+i.e. the ones with \"yas-\" single dash prefix. I will try to
+keep them in future yasnippet versions and other elisp libraries
+can more or less safely rely upon them.")
+
+\f
+(provide 'yasnippet)
+;; Local Variables:
+;; coding: utf-8
+;; indent-tabs-mode: nil
+;; byte-compile-warnings: (not cl-functions)
+;; End:
+;;; yasnippet.el ends here