diff options
Diffstat (limited to '.emacs.d/lisp')
| -rw-r--r-- | .emacs.d/lisp/yasnippet.el | 3676 |
1 files changed, 3676 insertions, 0 deletions
diff --git a/.emacs.d/lisp/yasnippet.el b/.emacs.d/lisp/yasnippet.el new file mode 100644 index 0000000..fcbce9e --- /dev/null +++ b/.emacs.d/lisp/yasnippet.el | |||
| @@ -0,0 +1,3676 @@ | |||
| 1 | ;;; Yasnippet.el --- Yet another snippet extension for Emacs. | ||
| 2 | |||
| 3 | ;; Copyright 2008 pluskid | ||
| 4 | ;; 2009 pluskid, joaotavora | ||
| 5 | |||
| 6 | ;; Authors: pluskid <pluskid@gmail.com>, joaotavora <joaotavora@gmail.com> | ||
| 7 | ;; Version: 0.6.1 | ||
| 8 | ;; Package-version: 0.6.1c | ||
| 9 | ;; X-URL: http://code.google.com/p/yasnippet/ | ||
| 10 | ;; Keywords: convenience, emulation | ||
| 11 | ;; URL: http://code.google.com/p/yasnippet/ | ||
| 12 | ;; EmacsWiki: YaSnippetMode | ||
| 13 | |||
| 14 | ;; This file is free software; you can redistribute it and/or modify | ||
| 15 | ;; it under the terms of the GNU General Public License as published by | ||
| 16 | ;; the Free Software Foundation; either version 2, or (at your option) | ||
| 17 | ;; any later version. | ||
| 18 | |||
| 19 | ;; This file is distributed in the hope that it will be useful, | ||
| 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 22 | ;; GNU General Public License for more details. | ||
| 23 | |||
| 24 | ;; You should have received a copy of the GNU General Public License | ||
| 25 | ;; along with GNU Emacs; see the file COPYING. If not, write to | ||
| 26 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 27 | ;; Boston, MA 02111-1307, USA. | ||
| 28 | |||
| 29 | ;;; Commentary: | ||
| 30 | |||
| 31 | ;; Basic steps to setup: | ||
| 32 | ;; | ||
| 33 | ;; 1. In your .emacs file: | ||
| 34 | ;; (add-to-list 'load-path "/dir/to/yasnippet.el") | ||
| 35 | ;; (require 'yasnippet) | ||
| 36 | ;; 2. Place the `snippets' directory somewhere. E.g: ~/.emacs.d/snippets | ||
| 37 | ;; 3. In your .emacs file | ||
| 38 | ;; (setq yas/root-directory "~/.emacs/snippets") | ||
| 39 | ;; (yas/load-directory yas/root-directory) | ||
| 40 | ;; 4. To enable the YASnippet menu and tab-trigger expansion | ||
| 41 | ;; M-x yas/minor-mode | ||
| 42 | ;; 5. To globally enable the minor mode in *all* buffers | ||
| 43 | ;; M-x yas/global-mode | ||
| 44 | ;; | ||
| 45 | ;; Steps 4. and 5. are optional, you don't have to use the minor | ||
| 46 | ;; mode to use YASnippet. | ||
| 47 | ;; | ||
| 48 | ;; Interesting variables are: | ||
| 49 | ;; | ||
| 50 | ;; `yas/root-directory' | ||
| 51 | ;; | ||
| 52 | ;; The directory where user-created snippets are to be | ||
| 53 | ;; stored. Can also be a list of directories that | ||
| 54 | ;; `yas/reload-all' will use for bulk-reloading snippets. In | ||
| 55 | ;; that case the first directory the default for storing new | ||
| 56 | ;; snippets. | ||
| 57 | ;; | ||
| 58 | ;; `yas/mode-symbol' | ||
| 59 | ;; | ||
| 60 | ;; A local variable that you can set in a hook to override | ||
| 61 | ;; snippet-lookup based on major mode. It is a a symbol (or | ||
| 62 | ;; list of symbols) that correspond to subdirectories of | ||
| 63 | ;; `yas/root-directory' and is used for deciding which | ||
| 64 | ;; snippets to consider for the active buffer. | ||
| 65 | ;; | ||
| 66 | ;; Major commands are: | ||
| 67 | ;; | ||
| 68 | ;; M-x yas/expand | ||
| 69 | ;; | ||
| 70 | ;; Try to expand snippets before point. In `yas/minor-mode', | ||
| 71 | ;; this is bound to `yas/trigger-key' which you can customize. | ||
| 72 | ;; | ||
| 73 | ;; M-x yas/load-directory | ||
| 74 | ;; | ||
| 75 | ;; Prompts you for a directory hierarchy of snippets to load. | ||
| 76 | ;; | ||
| 77 | ;; M-x yas/insert-snippet | ||
| 78 | ;; | ||
| 79 | ;; Prompts you for possible snippet expansion if that is | ||
| 80 | ;; possible according to buffer-local and snippet-local | ||
| 81 | ;; expansion conditions. With prefix argument, ignore these | ||
| 82 | ;; conditions. | ||
| 83 | ;; | ||
| 84 | ;; M-x yas/find-snippets | ||
| 85 | ;; | ||
| 86 | ;; Lets you find the snippet files in the correct | ||
| 87 | ;; subdirectory of `yas/root-directory', according to the | ||
| 88 | ;; active major mode (if it exists) like | ||
| 89 | ;; `find-file-other-window'. | ||
| 90 | ;; | ||
| 91 | ;; M-x yas/visit-snippet-file | ||
| 92 | ;; | ||
| 93 | ;; Prompts you for possible snippet expansions like | ||
| 94 | ;; `yas/insert-snippet', but instead of expanding it, takes | ||
| 95 | ;; you directly to the snippet definition's file, if it | ||
| 96 | ;; exists. | ||
| 97 | ;; | ||
| 98 | ;; M-x yas/new-snippet | ||
| 99 | ;; | ||
| 100 | ;; Lets you create a new snippet file in the correct | ||
| 101 | ;; subdirectory of `yas/root-directory', according to the | ||
| 102 | ;; active major mode. | ||
| 103 | ;; | ||
| 104 | ;; M-x yas/load-snippet-buffer | ||
| 105 | ;; | ||
| 106 | ;; When editing a snippet, this loads the snippet. This is | ||
| 107 | ;; bound to "C-c C-c" while in the `snippet-mode' editing | ||
| 108 | ;; mode. | ||
| 109 | ;; | ||
| 110 | ;; M-x yas/tryout-snippet | ||
| 111 | ;; | ||
| 112 | ;; When editing a snippet, this opens a new empty buffer, | ||
| 113 | ;; sets it to the appropriate major mode and inserts the | ||
| 114 | ;; snippet there, so you can see what it looks like. This is | ||
| 115 | ;; bound to "C-c C-t" while in `snippet-mode'. | ||
| 116 | ;; | ||
| 117 | ;; The `dropdown-list.el' extension is bundled with YASnippet, you | ||
| 118 | ;; can optionally use it the preferred "prompting method", puting in | ||
| 119 | ;; your .emacs file, for example: | ||
| 120 | ;; | ||
| 121 | ;; (require 'dropdown-list) | ||
| 122 | ;; (setq yas/prompt-functions '(yas/dropdown-prompt | ||
| 123 | ;; yas/ido-prompt | ||
| 124 | ;; yas/completing-prompt)) | ||
| 125 | ;; | ||
| 126 | ;; Also check out the customization group | ||
| 127 | ;; | ||
| 128 | ;; M-x customize-group RET yasnippet RET | ||
| 129 | ;; | ||
| 130 | ;; If you use the customization group to set variables | ||
| 131 | ;; `yas/root-directory' or `yas/global-mode', make sure the path to | ||
| 132 | ;; "yasnippet.el" is present in the `load-path' *before* the | ||
| 133 | ;; `custom-set-variables' is executed in your .emacs file. | ||
| 134 | ;; | ||
| 135 | ;; For more information and detailed usage, refer to the project page: | ||
| 136 | ;; http://code.google.com/p/yasnippet/ | ||
| 137 | |||
| 138 | ;;; Code: | ||
| 139 | |||
| 140 | (require 'cl) | ||
| 141 | (require 'assoc) | ||
| 142 | (require 'easymenu) | ||
| 143 | |||
| 144 | |||
| 145 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 146 | ;; User customizable variables | ||
| 147 | |||
| 148 | |||
| 149 | (defgroup yasnippet nil | ||
| 150 | "Yet Another Snippet extension" | ||
| 151 | :group 'editing) | ||
| 152 | |||
| 153 | ;;;###autoload | ||
| 154 | (defcustom yas/root-directory nil | ||
| 155 | "Root directory that stores the snippets for each major mode. | ||
| 156 | |||
| 157 | If you set this from your .emacs, can also be a list of strings, | ||
| 158 | for multiple root directories. If you make this a list, the first | ||
| 159 | element is always the user-created snippets directory. Other | ||
| 160 | directories are used for bulk reloading of all snippets using | ||
| 161 | `yas/reload-all'" | ||
| 162 | :type '(choice (string :tag "Single directory (string)") | ||
| 163 | (repeat :args (string) :tag "List of directories (strings)")) | ||
| 164 | :group 'yasnippet | ||
| 165 | :require 'yasnippet | ||
| 166 | :set #'(lambda (symbol new) | ||
| 167 | (let ((old (and (boundp symbol) | ||
| 168 | (symbol-value symbol)))) | ||
| 169 | (set-default symbol new) | ||
| 170 | (unless (or (not (fboundp 'yas/reload-all)) | ||
| 171 | (equal old new)) | ||
| 172 | (yas/reload-all))))) | ||
| 173 | |||
| 174 | (defcustom yas/prompt-functions '(yas/x-prompt | ||
| 175 | yas/dropdown-prompt | ||
| 176 | yas/completing-prompt | ||
| 177 | yas/ido-prompt | ||
| 178 | yas/no-prompt) | ||
| 179 | "Functions to prompt for keys, templates, etc interactively. | ||
| 180 | |||
| 181 | These functions are called with the following arguments: | ||
| 182 | |||
| 183 | - PROMPT: A string to prompt the user | ||
| 184 | |||
| 185 | - CHOICES: a list of strings or objects. | ||
| 186 | |||
| 187 | - optional DISPLAY-FN : A function that, when applied to each of | ||
| 188 | the objects in CHOICES will return a string. | ||
| 189 | |||
| 190 | The return value of any function you put here should be one of | ||
| 191 | the objects in CHOICES, properly formatted with DISPLAY-FN (if | ||
| 192 | that is passed). | ||
| 193 | |||
| 194 | - To signal that your particular style of prompting is | ||
| 195 | unavailable at the moment, you can also have the function return | ||
| 196 | nil. | ||
| 197 | |||
| 198 | - To signal that the user quit the prompting process, you can | ||
| 199 | signal `quit' with | ||
| 200 | |||
| 201 | (signal 'quit \"user quit!\")." | ||
| 202 | :type '(repeat function) | ||
| 203 | :group 'yasnippet) | ||
| 204 | |||
| 205 | (defcustom yas/indent-line 'auto | ||
| 206 | "Controls indenting applied to a recent snippet expansion. | ||
| 207 | |||
| 208 | The following values are possible: | ||
| 209 | |||
| 210 | - `fixed' Indent the snippet to the current column; | ||
| 211 | |||
| 212 | - `auto' Indent each line of the snippet with `indent-according-to-mode' | ||
| 213 | |||
| 214 | Every other value means don't apply any snippet-side indendation | ||
| 215 | after expansion (the manual per-line \"$>\" indentation still | ||
| 216 | applies)." | ||
| 217 | :type '(choice (const :tag "Nothing" nothing) | ||
| 218 | (const :tag "Fixed" fixed) | ||
| 219 | (const :tag "Auto" auto)) | ||
| 220 | :group 'yasnippet) | ||
| 221 | |||
| 222 | (defcustom yas/also-auto-indent-first-line nil | ||
| 223 | "Non-nil means also auto indent first line according to mode. | ||
| 224 | |||
| 225 | Naturally this is only valid when `yas/indent-line' is `auto'" | ||
| 226 | :type 'boolean | ||
| 227 | :group 'yasnippet) | ||
| 228 | |||
| 229 | (defcustom yas/snippet-revival t | ||
| 230 | "Non-nil means re-activate snippet fields after undo/redo." | ||
| 231 | :type 'boolean | ||
| 232 | :group 'yasnippet) | ||
| 233 | |||
| 234 | (defcustom yas/trigger-key "TAB" | ||
| 235 | "The key bound to `yas/expand' when function `yas/minor-mode' is active. | ||
| 236 | |||
| 237 | Value is a string that is converted to the internal Emacs key | ||
| 238 | representation using `read-kbd-macro'." | ||
| 239 | :type 'string | ||
| 240 | :group 'yasnippet | ||
| 241 | :set #'(lambda (symbol key) | ||
| 242 | (let ((old (and (boundp symbol) | ||
| 243 | (symbol-value symbol)))) | ||
| 244 | (set-default symbol key) | ||
| 245 | ;; On very first loading of this defcustom, | ||
| 246 | ;; `yas/trigger-key' is *not* loaded. | ||
| 247 | (if (fboundp 'yas/trigger-key-reload) | ||
| 248 | (yas/trigger-key-reload old))))) | ||
| 249 | |||
| 250 | (defcustom yas/next-field-key '("TAB" "<tab>") | ||
| 251 | "The key to navigate to next field when a snippet is active. | ||
| 252 | |||
| 253 | Value is a string that is converted to the internal Emacs key | ||
| 254 | representation using `read-kbd-macro'. | ||
| 255 | |||
| 256 | Can also be a list of strings." | ||
| 257 | :type '(choice (string :tag "String") | ||
| 258 | (repeat :args (string) :tag "List of strings")) | ||
| 259 | :group 'yasnippet | ||
| 260 | :set #'(lambda (symbol val) | ||
| 261 | (set-default symbol val) | ||
| 262 | (if (fboundp 'yas/init-yas-in-snippet-keymap) | ||
| 263 | (yas/init-yas-in-snippet-keymap)))) | ||
| 264 | |||
| 265 | |||
| 266 | (defcustom yas/prev-field-key '("<backtab>" "<S-tab>") | ||
| 267 | "The key to navigate to previous field when a snippet is active. | ||
| 268 | |||
| 269 | Value is a string that is converted to the internal Emacs key | ||
| 270 | representation using `read-kbd-macro'. | ||
| 271 | |||
| 272 | Can also be a list of strings." | ||
| 273 | :type '(choice (string :tag "String") | ||
| 274 | (repeat :args (string) :tag "List of strings")) | ||
| 275 | :group 'yasnippet | ||
| 276 | :set #'(lambda (symbol val) | ||
| 277 | (set-default symbol val) | ||
| 278 | (if (fboundp 'yas/init-yas-in-snippet-keymap) | ||
| 279 | (yas/init-yas-in-snippet-keymap)))) | ||
| 280 | |||
| 281 | (defcustom yas/skip-and-clear-key "C-d" | ||
| 282 | "The key to clear the currently active field. | ||
| 283 | |||
| 284 | Value is a string that is converted to the internal Emacs key | ||
| 285 | representation using `read-kbd-macro'. | ||
| 286 | |||
| 287 | Can also be a list of strings." | ||
| 288 | :type '(choice (string :tag "String") | ||
| 289 | (repeat :args (string) :tag "List of strings")) | ||
| 290 | :group 'yasnippet | ||
| 291 | :set #'(lambda (symbol val) | ||
| 292 | (set-default symbol val) | ||
| 293 | (if (fboundp 'yas/init-yas-in-snippet-keymap) | ||
| 294 | (yas/init-yas-in-snippet-keymap)))) | ||
| 295 | |||
| 296 | (defcustom yas/triggers-in-field nil | ||
| 297 | "If non-nil, `yas/next-field-key' can trigger stacked expansions. | ||
| 298 | |||
| 299 | Otherwise, `yas/next-field-key' just tries to move on to the next | ||
| 300 | field" | ||
| 301 | :type 'boolean | ||
| 302 | :group 'yasnippet) | ||
| 303 | |||
| 304 | (defcustom yas/fallback-behavior 'call-other-command | ||
| 305 | "How to act when `yas/trigger-key' does *not* expand a snippet. | ||
| 306 | |||
| 307 | - `call-other-command' means try to temporarily disable YASnippet | ||
| 308 | and call the next command bound to `yas/trigger-key'. | ||
| 309 | |||
| 310 | - nil or the symbol `return-nil' mean do nothing. (and | ||
| 311 | `yas/expand-returns' nil) | ||
| 312 | |||
| 313 | - A lisp form (apply COMMAND . ARGS) means interactively call | ||
| 314 | COMMAND, if ARGS is non-nil, call COMMAND non-interactively | ||
| 315 | with ARGS as arguments." | ||
| 316 | :type '(choice (const :tag "Call previous command" call-other-command) | ||
| 317 | (const :tag "Do nothing" return-nil)) | ||
| 318 | :group 'yasnippet) | ||
| 319 | (make-variable-buffer-local 'yas/fallback-behavior) | ||
| 320 | |||
| 321 | (defcustom yas/choose-keys-first nil | ||
| 322 | "If non-nil, prompt for snippet key first, then for template. | ||
| 323 | |||
| 324 | Otherwise prompts for all possible snippet names. | ||
| 325 | |||
| 326 | This affects `yas/insert-snippet' and `yas/visit-snippet-file'." | ||
| 327 | :type 'boolean | ||
| 328 | :group 'yasnippet) | ||
| 329 | |||
| 330 | (defcustom yas/choose-tables-first nil | ||
| 331 | "If non-nil, and multiple eligible snippet tables, prompts user for tables first. | ||
| 332 | |||
| 333 | Otherwise, user chooses between the merging together of all | ||
| 334 | eligible tables. | ||
| 335 | |||
| 336 | This affects `yas/insert-snippet', `yas/visit-snippet-file'" | ||
| 337 | :type 'boolean | ||
| 338 | :group 'yasnippet) | ||
| 339 | |||
| 340 | (defcustom yas/use-menu 'real-modes | ||
| 341 | "Display a YASnippet menu in the menu bar. | ||
| 342 | |||
| 343 | When non-nil, submenus for each snippet table will be listed | ||
| 344 | under the menu \"Yasnippet\". | ||
| 345 | |||
| 346 | - If set to `real-modes' only submenus whose name more or less | ||
| 347 | corresponds to a major mode are listed. | ||
| 348 | |||
| 349 | - If set to `abbreviate', only the current major-mode | ||
| 350 | menu and the modes set in `yas/mode-symbol' are listed. | ||
| 351 | |||
| 352 | Any other non-nil value, every submenu is listed." | ||
| 353 | :type '(choice (const :tag "Full" t) | ||
| 354 | (const :tag "Real modes only" real-modes) | ||
| 355 | (const :tag "Abbreviate" abbreviate)) | ||
| 356 | :group 'yasnippet) | ||
| 357 | |||
| 358 | (defcustom yas/trigger-symbol " =>" | ||
| 359 | "The text that will be used in menu to represent the trigger." | ||
| 360 | :type 'string | ||
| 361 | :group 'yasnippet) | ||
| 362 | |||
| 363 | (defcustom yas/wrap-around-region nil | ||
| 364 | "If non-nil, snippet expansion wraps around selected region. | ||
| 365 | |||
| 366 | The wrapping occurs just before the snippet's exit marker. This | ||
| 367 | can be overriden on a per-snippet basis." | ||
| 368 | :type 'boolean | ||
| 369 | :group 'yasnippet) | ||
| 370 | |||
| 371 | (defcustom yas/good-grace t | ||
| 372 | "If non-nil, don't raise errors in inline elisp evaluation. | ||
| 373 | |||
| 374 | An error string \"[yas] error\" is returned instead." | ||
| 375 | :type 'boolean | ||
| 376 | :group 'yasnippet) | ||
| 377 | |||
| 378 | (defcustom yas/ignore-filenames-as-triggers nil | ||
| 379 | "If non-nil, don't derive tab triggers from filenames. | ||
| 380 | |||
| 381 | This means a snippet without a \"# key:'\ directive wont have a | ||
| 382 | tab trigger." | ||
| 383 | :type 'boolean | ||
| 384 | :group 'yasnippet) | ||
| 385 | |||
| 386 | (defcustom yas/visit-from-menu nil | ||
| 387 | "If non-nil visit snippets's files from menu, instead of expanding them. | ||
| 388 | |||
| 389 | This cafn only work when snippets are loaded from files." | ||
| 390 | :type 'boolean | ||
| 391 | :group 'yasnippet) | ||
| 392 | |||
| 393 | (defface yas/field-highlight-face | ||
| 394 | '((((class color) (background light)) (:background "DarkSeaGreen1")) | ||
| 395 | (t (:background "DimGrey"))) | ||
| 396 | "The face used to highlight the currently active field of a snippet" | ||
| 397 | :group 'yasnippet) | ||
| 398 | |||
| 399 | (defface yas/field-debug-face | ||
| 400 | '() | ||
| 401 | "The face used for debugging some overlays normally hidden" | ||
| 402 | :group 'yasnippet) | ||
| 403 | |||
| 404 | |||
| 405 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 406 | ;; User can also customize the next defvars | ||
| 407 | (defun yas/define-some-keys (keys keymap definition) | ||
| 408 | "Bind KEYS to DEFINITION in KEYMAP, read with `read-kbd-macro'." | ||
| 409 | (let ((keys (or (and (listp keys) keys) | ||
| 410 | (list keys)))) | ||
| 411 | (dolist (key keys) | ||
| 412 | (define-key keymap (read-kbd-macro key) definition)))) | ||
| 413 | |||
| 414 | (defvar yas/keymap | ||
| 415 | (let ((map (make-sparse-keymap))) | ||
| 416 | (mapc #'(lambda (binding) | ||
| 417 | (yas/define-some-keys (car binding) map (cdr binding))) | ||
| 418 | `((,yas/next-field-key . yas/next-field-or-maybe-expand) | ||
| 419 | (,yas/prev-field-key . yas/prev-field) | ||
| 420 | ("C-g" . yas/abort-snippet) | ||
| 421 | (,yas/skip-and-clear-key . yas/skip-and-clear-or-delete-char))) | ||
| 422 | map) | ||
| 423 | "The keymap active while a snippet expansion is in progress.") | ||
| 424 | |||
| 425 | (defvar yas/key-syntaxes (list "w" "w_" "w_." "^ ") | ||
| 426 | "A list of syntax of a key. This list is tried in the order | ||
| 427 | to try to find a key. For example, if the list is '(\"w\" \"w_\"). | ||
| 428 | And in emacs-lisp-mode, where \"-\" has the syntax of \"_\": | ||
| 429 | |||
| 430 | foo-bar | ||
| 431 | |||
| 432 | will first try \"bar\", if not found, then \"foo-bar\" is tried.") | ||
| 433 | |||
| 434 | (defvar yas/after-exit-snippet-hook | ||
| 435 | '() | ||
| 436 | "Hooks to run after a snippet exited. | ||
| 437 | |||
| 438 | The hooks will be run in an environment where some variables bound to | ||
| 439 | proper values: | ||
| 440 | |||
| 441 | `yas/snippet-beg' : The beginning of the region of the snippet. | ||
| 442 | |||
| 443 | `yas/snippet-end' : Similar to beg. | ||
| 444 | |||
| 445 | Attention: These hooks are not run when exiting nested/stackd snippet expansion!") | ||
| 446 | |||
| 447 | (defvar yas/before-expand-snippet-hook | ||
| 448 | '() | ||
| 449 | "Hooks to run just before expanding a snippet.") | ||
| 450 | |||
| 451 | (defvar yas/buffer-local-condition | ||
| 452 | '(if (and (not (bobp)) | ||
| 453 | (or (equal 'font-lock-comment-face | ||
| 454 | (get-char-property (1- (point)) | ||
| 455 | 'face)) | ||
| 456 | (equal 'font-lock-string-face | ||
| 457 | (get-char-property (1- (point)) | ||
| 458 | 'face)))) | ||
| 459 | '(require-snippet-condition . force-in-comment) | ||
| 460 | t) | ||
| 461 | "Snippet expanding condition. | ||
| 462 | |||
| 463 | This variable is a lisp form: | ||
| 464 | |||
| 465 | * If it evaluates to nil, no snippets can be expanded. | ||
| 466 | |||
| 467 | * If it evaluates to the a cons (require-snippet-condition | ||
| 468 | . REQUIREMENT) | ||
| 469 | |||
| 470 | * Snippets bearing no \"# condition:\" directive are not | ||
| 471 | considered | ||
| 472 | |||
| 473 | * Snippets bearing conditions that evaluate to nil (or | ||
| 474 | produce an error) won't be onsidered. | ||
| 475 | |||
| 476 | * If the snippet has a condition that evaluates to non-nil | ||
| 477 | RESULT: | ||
| 478 | |||
| 479 | * If REQUIREMENT is t, the snippet is considered | ||
| 480 | |||
| 481 | * If REQUIREMENT is `eq' RESULT, the snippet is | ||
| 482 | considered | ||
| 483 | |||
| 484 | * Otherwise, the snippet is not considered. | ||
| 485 | |||
| 486 | * If it evaluates to the symbol 'always, all snippets are | ||
| 487 | considered for expansion, regardless of any conditions. | ||
| 488 | |||
| 489 | * If it evaluates to t or some other non-nil value | ||
| 490 | |||
| 491 | * Snippet bearing no conditions, or conditions that | ||
| 492 | evaluate to non-nil, are considered for expansion. | ||
| 493 | |||
| 494 | * Otherwise, the snippet is not considered. | ||
| 495 | |||
| 496 | Here's an example preventing snippets from being expanded from | ||
| 497 | inside comments, in `python-mode' only, with the exception of | ||
| 498 | snippets returning the symbol 'force-in-comment in their | ||
| 499 | conditions. | ||
| 500 | |||
| 501 | (add-hook 'python-mode-hook | ||
| 502 | '(lambda () | ||
| 503 | (setq yas/buffer-local-condition | ||
| 504 | '(if (python-in-string/comment) | ||
| 505 | '(require-snippet-condition . force-in-comment) | ||
| 506 | t)))) | ||
| 507 | |||
| 508 | The default value is similar, it filters out potential snippet | ||
| 509 | expansions inside comments and string literals, unless the | ||
| 510 | snippet itself contains a condition that returns the symbol | ||
| 511 | `force-in-comment'.") | ||
| 512 | (make-variable-buffer-local 'yas/buffer-local-condition) | ||
| 513 | |||
| 514 | |||
| 515 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 516 | ;; Internal variables | ||
| 517 | |||
| 518 | (defvar yas/version "0.6.1b") | ||
| 519 | |||
| 520 | (defvar yas/menu-table (make-hash-table) | ||
| 521 | "A hash table of MAJOR-MODE symbols to menu keymaps.") | ||
| 522 | |||
| 523 | (defvar yas/active-keybindings nil | ||
| 524 | "A list of cons (KEYMAP . KEY) setup from defining snippets.") | ||
| 525 | |||
| 526 | (defvar yas/known-modes | ||
| 527 | '(ruby-mode rst-mode markdown-mode) | ||
| 528 | "A list of mode which is well known but not part of emacs.") | ||
| 529 | |||
| 530 | (defvar yas/escaped-characters | ||
| 531 | '(?\\ ?` ?' ?$ ?} ) | ||
| 532 | "List of characters which *might* need to be escaped.") | ||
| 533 | |||
| 534 | (defconst yas/field-regexp | ||
| 535 | "${\\([0-9]+:\\)?\\([^}]*\\)}" | ||
| 536 | "A regexp to *almost* recognize a field.") | ||
| 537 | |||
| 538 | (defconst yas/multi-dollar-lisp-expression-regexp | ||
| 539 | "$+[ \t\n]*\\(([^)]*)\\)" | ||
| 540 | "A regexp to *almost* recognize a \"$(...)\" expression.") | ||
| 541 | |||
| 542 | (defconst yas/backquote-lisp-expression-regexp | ||
| 543 | "`\\([^`]*\\)`" | ||
| 544 | "A regexp to recognize a \"`lisp-expression`\" expression." ) | ||
| 545 | |||
| 546 | (defconst yas/transform-mirror-regexp | ||
| 547 | "${\\(?:\\([0-9]+\\):\\)?$\\([ \t\n]*([^}]*\\)" | ||
| 548 | "A regexp to *almost* recognize a mirror with a transform.") | ||
| 549 | |||
| 550 | (defconst yas/simple-mirror-regexp | ||
| 551 | "$\\([0-9]+\\)" | ||
| 552 | "A regexp to recognize a simple mirror.") | ||
| 553 | |||
| 554 | (defvar yas/snippet-id-seed 0 | ||
| 555 | "Contains the next id for a snippet.") | ||
| 556 | |||
| 557 | (defun yas/snippet-next-id () | ||
| 558 | (let ((id yas/snippet-id-seed)) | ||
| 559 | (incf yas/snippet-id-seed) | ||
| 560 | id)) | ||
| 561 | |||
| 562 | |||
| 563 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 564 | ;; Minor mode stuff | ||
| 565 | |||
| 566 | ;; XXX: `last-buffer-undo-list' is somehow needed in Carbon Emacs for MacOSX | ||
| 567 | (defvar last-buffer-undo-list nil) | ||
| 568 | |||
| 569 | (defvar yas/minor-mode-menu nil | ||
| 570 | "Holds the YASnippet menu") | ||
| 571 | |||
| 572 | (defun yas/init-minor-keymap () | ||
| 573 | (let ((map (make-sparse-keymap))) | ||
| 574 | (easy-menu-define yas/minor-mode-menu | ||
| 575 | map | ||
| 576 | "Menu used when YAS/minor-mode is active." | ||
| 577 | '("YASnippet" | ||
| 578 | "----" | ||
| 579 | ["Expand trigger" yas/expand | ||
| 580 | :help "Possibly expand tab trigger before point"] | ||
| 581 | ["Insert at point..." yas/insert-snippet | ||
| 582 | :help "Prompt for an expandable snippet and expand it at point"] | ||
| 583 | ["New snippet..." yas/new-snippet | ||
| 584 | :help "Create a new snippet in an appropriate directory"] | ||
| 585 | ["Visit snippet file..." yas/visit-snippet-file | ||
| 586 | :help "Prompt for an expandable snippet and find its file"] | ||
| 587 | ["Find snippets..." yas/find-snippets | ||
| 588 | :help "Invoke `find-file' in the appropriate snippet directory"] | ||
| 589 | "----" | ||
| 590 | ("Snippet menu behaviour" | ||
| 591 | ["Visit snippets" (setq yas/visit-from-menu t) | ||
| 592 | :help "Visit snippets from the menu" | ||
| 593 | :active t :style radio :selected yas/visit-from-menu] | ||
| 594 | ["Expand snippets" (setq yas/visit-from-menu nil) | ||
| 595 | :help "Expand snippets from the menu" | ||
| 596 | :active t :style radio :selected (not yas/visit-from-menu)] | ||
| 597 | "----" | ||
| 598 | ["Show \"Real\" modes only" (setq yas/use-menu 'real-modes) | ||
| 599 | :help "Show snippet submenus for modes that appear to be real major modes" | ||
| 600 | :active t :style radio :selected (eq yas/use-menu 'real-modes)] | ||
| 601 | ["Show all modes" (setq yas/use-menu 't) | ||
| 602 | :help "Show one snippet submenu for each loaded table" | ||
| 603 | :active t :style radio :selected (eq yas/use-menu 't)] | ||
| 604 | ["Abbreviate according to current mode" (setq yas/use-menu 'abbreviate) | ||
| 605 | :help "Show only snippet submenus for the current active modes" | ||
| 606 | :active t :style radio :selected (eq yas/use-menu 'abbreviate)]) | ||
| 607 | ("Indenting" | ||
| 608 | ["Auto" (setq yas/indent-line 'auto) | ||
| 609 | :help "Indent each line of the snippet with `indent-according-to-mode'" | ||
| 610 | :active t :style radio :selected (eq yas/indent-line 'auto)] | ||
| 611 | ["Fixed" (setq yas/indent-line 'fixed) | ||
| 612 | :help "Indent the snippet to the current column" | ||
| 613 | :active t :style radio :selected (eq yas/indent-line 'fixed)] | ||
| 614 | ["None" (setq yas/indent-line 'none) | ||
| 615 | :help "Don't apply any particular snippet indentation after expansion" | ||
| 616 | :active t :style radio :selected (not (member yas/indent-line '(fixed auto)))] | ||
| 617 | "----" | ||
| 618 | ["Also auto indent first line" (setq yas/also-auto-indent-first-line | ||
| 619 | (not yas/also-auto-indent-first-line)) | ||
| 620 | :help "When auto-indenting also, auto indent the first line menu" | ||
| 621 | :active (eq yas/indent-line 'auto) | ||
| 622 | :style toggle :selected yas/also-auto-indent-first-line] | ||
| 623 | ) | ||
| 624 | ("Prompting method" | ||
| 625 | ["System X-widget" (setq yas/prompt-functions | ||
| 626 | (cons 'yas/x-prompt | ||
| 627 | (remove 'yas/x-prompt | ||
| 628 | yas/prompt-functions))) | ||
| 629 | :help "Use your windowing system's (gtk, mac, windows, etc...) default menu" | ||
| 630 | :active t :style radio :selected (eq (car yas/prompt-functions) | ||
| 631 | 'yas/x-prompt)] | ||
| 632 | ["Dropdown-list" (setq yas/prompt-functions | ||
| 633 | (cons 'yas/dropdown-prompt | ||
| 634 | (remove 'yas/dropdown-prompt | ||
| 635 | yas/prompt-functions))) | ||
| 636 | :help "Use a special dropdown list" | ||
| 637 | :active t :style radio :selected (eq (car yas/prompt-functions) | ||
| 638 | 'yas/dropdown-prompt)] | ||
| 639 | ["Ido" (setq yas/prompt-functions | ||
| 640 | (cons 'yas/ido-prompt | ||
| 641 | (remove 'yas/ido-prompt | ||
| 642 | yas/prompt-functions))) | ||
| 643 | :help "Use an ido-style minibuffer prompt" | ||
| 644 | :active t :style radio :selected (eq (car yas/prompt-functions) | ||
| 645 | 'yas/ido-prompt)] | ||
| 646 | ["Completing read" (setq yas/prompt-functions | ||
| 647 | (cons 'yas/completing-prompt | ||
| 648 | (remove 'yas/completing-prompt-prompt | ||
| 649 | yas/prompt-functions))) | ||
| 650 | :help "Use a normal minibuffer prompt" | ||
| 651 | :active t :style radio :selected (eq (car yas/prompt-functions) | ||
| 652 | 'yas/completing-prompt-prompt)] | ||
| 653 | ) | ||
| 654 | ("Misc" | ||
| 655 | ["Wrap region in exit marker" | ||
| 656 | (setq yas/wrap-around-region | ||
| 657 | (not yas/wrap-around-region)) | ||
| 658 | :help "If non-nil automatically wrap the selected text in the $0 snippet exit" | ||
| 659 | :style toggle :selected yas/wrap-around-region] | ||
| 660 | ["Allow stacked expansions " | ||
| 661 | (setq yas/triggers-in-field | ||
| 662 | (not yas/triggers-in-field)) | ||
| 663 | :help "If non-nil allow snippets to be triggered inside other snippet fields" | ||
| 664 | :style toggle :selected yas/triggers-in-field] | ||
| 665 | ["Revive snippets on undo " | ||
| 666 | (setq yas/snippet-revival | ||
| 667 | (not yas/snippet-revival)) | ||
| 668 | :help "If non-nil allow snippets to become active again after undo" | ||
| 669 | :style toggle :selected yas/snippet-revival] | ||
| 670 | ["Good grace " | ||
| 671 | (setq yas/good-grace | ||
| 672 | (not yas/good-grace)) | ||
| 673 | :help "If non-nil don't raise errors in bad embedded eslip in snippets" | ||
| 674 | :style toggle :selected yas/good-grace] | ||
| 675 | ["Ignore filenames as triggers" | ||
| 676 | (setq yas/ignore-filenames-as-triggers | ||
| 677 | (not yas/ignore-filenames-as-triggers)) | ||
| 678 | :help "If non-nil don't derive tab triggers from filenames" | ||
| 679 | :style toggle :selected yas/ignore-filenames-as-triggers] | ||
| 680 | ) | ||
| 681 | "----" | ||
| 682 | ["Load snippets..." yas/load-directory | ||
| 683 | :help "Load snippets from a specific directory"] | ||
| 684 | ["Reload everything" yas/reload-all | ||
| 685 | :help "Cleanup stuff, reload snippets, rebuild menus"] | ||
| 686 | ["About" yas/about | ||
| 687 | :help "Display some information about YASsnippet"])) | ||
| 688 | ;; Now for the stuff that has direct keybindings | ||
| 689 | ;; | ||
| 690 | (define-key map "\C-c&\C-s" 'yas/insert-snippet) | ||
| 691 | (define-key map "\C-c&\C-n" 'yas/new-snippet) | ||
| 692 | (define-key map "\C-c&\C-v" 'yas/visit-snippet-file) | ||
| 693 | (define-key map "\C-c&\C-f" 'yas/find-snippets) | ||
| 694 | map)) | ||
| 695 | |||
| 696 | (defvar yas/minor-mode-map (yas/init-minor-keymap) | ||
| 697 | "The keymap used when `yas/minor-mode' is active.") | ||
| 698 | |||
| 699 | (defun yas/trigger-key-reload (&optional unbind-key) | ||
| 700 | "Rebind `yas/expand' to the new value of `yas/trigger-key'. | ||
| 701 | |||
| 702 | With optional UNBIND-KEY, try to unbind that key from | ||
| 703 | `yas/minor-mode-map'." | ||
| 704 | (when (and unbind-key | ||
| 705 | (stringp unbind-key) | ||
| 706 | (not (string= unbind-key ""))) | ||
| 707 | (define-key yas/minor-mode-map (read-kbd-macro unbind-key) nil)) | ||
| 708 | (when (and yas/trigger-key | ||
| 709 | (stringp yas/trigger-key) | ||
| 710 | (not (string= yas/trigger-key ""))) | ||
| 711 | (define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand))) | ||
| 712 | |||
| 713 | ;;;###autoload | ||
| 714 | (define-minor-mode yas/minor-mode | ||
| 715 | "Toggle YASnippet mode. | ||
| 716 | |||
| 717 | When YASnippet mode is enabled, the `tas/trigger-key' key expands | ||
| 718 | snippets of code depending on the mode. | ||
| 719 | |||
| 720 | With no argument, this command toggles the mode. | ||
| 721 | positive prefix argument turns on the mode. | ||
| 722 | Negative prefix argument turns off the mode. | ||
| 723 | |||
| 724 | You can customize the key through `yas/trigger-key'. | ||
| 725 | |||
| 726 | Key bindings: | ||
| 727 | \\{yas/minor-mode-map}" | ||
| 728 | nil | ||
| 729 | ;; The indicator for the mode line. | ||
| 730 | " yas" | ||
| 731 | :group 'yasnippet | ||
| 732 | (when yas/minor-mode | ||
| 733 | (yas/trigger-key-reload) | ||
| 734 | ;; load all snippets definitions unless we still don't have a | ||
| 735 | ;; root-directory or some snippets have already been loaded. | ||
| 736 | (unless (or (null yas/root-directory) | ||
| 737 | (> (hash-table-count yas/snippet-tables) 0)) | ||
| 738 | (yas/reload-all)))) | ||
| 739 | |||
| 740 | (defvar yas/dont-activate #'(lambda () | ||
| 741 | (and yas/root-directory | ||
| 742 | (null (yas/get-snippet-tables)))) | ||
| 743 | "If non-nil don't let `yas/minor-mode-on' active yas for this buffer. | ||
| 744 | |||
| 745 | `yas/minor-mode-on' is usually called by `yas/global-mode' so | ||
| 746 | this effectively lets you define exceptions to the \"global\" | ||
| 747 | behaviour.") | ||
| 748 | (make-variable-buffer-local 'yas/dont-activate) | ||
| 749 | |||
| 750 | |||
| 751 | (defun yas/minor-mode-on () | ||
| 752 | "Turn on YASnippet minor mode. | ||
| 753 | |||
| 754 | Do this unless `yas/dont-activate' is t or the function | ||
| 755 | `yas/get-snippet-tables' (which see), returns an empty list." | ||
| 756 | (interactive) | ||
| 757 | (unless (or (and (functionp yas/dont-activate) | ||
| 758 | (funcall yas/dont-activate)) | ||
| 759 | (and (not (functionp yas/dont-activate)) | ||
| 760 | yas/dont-activate)) | ||
| 761 | (yas/minor-mode 1))) | ||
| 762 | |||
| 763 | (defun yas/minor-mode-off () | ||
| 764 | "Turn off YASnippet minor mode." | ||
| 765 | (interactive) | ||
| 766 | (yas/minor-mode -1)) | ||
| 767 | |||
| 768 | (define-globalized-minor-mode yas/global-mode yas/minor-mode yas/minor-mode-on | ||
| 769 | :group 'yasnippet | ||
| 770 | :require 'yasnippet) | ||
| 771 | |||
| 772 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 773 | ;; Major mode stuff | ||
| 774 | ;; | ||
| 775 | (defvar yas/font-lock-keywords | ||
| 776 | (append '(("^#.*$" . font-lock-comment-face)) | ||
| 777 | lisp-font-lock-keywords | ||
| 778 | lisp-font-lock-keywords-1 | ||
| 779 | lisp-font-lock-keywords-2 | ||
| 780 | '(("$\\([0-9]+\\)" | ||
| 781 | (0 font-lock-keyword-face) | ||
| 782 | (1 font-lock-string-face t)) | ||
| 783 | ("${\\([0-9]+\\):?" | ||
| 784 | (0 font-lock-keyword-face) | ||
| 785 | (1 font-lock-warning-face t)) | ||
| 786 | ("${" font-lock-keyword-face) | ||
| 787 | ("$[0-9]+?" font-lock-preprocessor-face) | ||
| 788 | ("\\(\\$(\\)" 1 font-lock-preprocessor-face) | ||
| 789 | ("}" | ||
| 790 | (0 font-lock-keyword-face))))) | ||
| 791 | |||
| 792 | (defun yas/init-major-keymap () | ||
| 793 | (let ((map (make-sparse-keymap))) | ||
| 794 | (easy-menu-define nil | ||
| 795 | map | ||
| 796 | "Menu used when snippet-mode is active." | ||
| 797 | (cons "Snippet" | ||
| 798 | (mapcar #'(lambda (ent) | ||
| 799 | (when (third ent) | ||
| 800 | (define-key map (third ent) (second ent))) | ||
| 801 | (vector (first ent) (second ent) t)) | ||
| 802 | (list | ||
| 803 | (list "Load this snippet" 'yas/load-snippet-buffer "\C-c\C-c") | ||
| 804 | (list "Try out this snippet" 'yas/tryout-snippet "\C-c\C-t"))))) | ||
| 805 | map)) | ||
| 806 | |||
| 807 | (defvar snippet-mode-map | ||
| 808 | (yas/init-major-keymap) | ||
| 809 | "The keymap used when `snippet-mode' is active") | ||
| 810 | |||
| 811 | |||
| 812 | (define-derived-mode snippet-mode text-mode "Snippet" | ||
| 813 | "A mode for editing yasnippets" | ||
| 814 | (set-syntax-table (standard-syntax-table)) | ||
| 815 | (setq font-lock-defaults '(yas/font-lock-keywords)) | ||
| 816 | (set (make-local-variable 'require-final-newline) nil) | ||
| 817 | (use-local-map snippet-mode-map)) | ||
| 818 | |||
| 819 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 820 | ;; Internal structs for template management | ||
| 821 | |||
| 822 | (defstruct (yas/template (:constructor yas/make-template | ||
| 823 | (content name condition expand-env file keybinding))) | ||
| 824 | "A template for a snippet." | ||
| 825 | content | ||
| 826 | name | ||
| 827 | condition | ||
| 828 | expand-env | ||
| 829 | file | ||
| 830 | keybinding) | ||
| 831 | |||
| 832 | (defvar yas/snippet-tables (make-hash-table) | ||
| 833 | "A hash table of MAJOR-MODE symbols to `yas/snippet-table' objects.") | ||
| 834 | |||
| 835 | (defstruct (yas/snippet-table (:constructor yas/make-snippet-table (name))) | ||
| 836 | "A table to store snippets for a particular mode. | ||
| 837 | |||
| 838 | Has the following fields: | ||
| 839 | |||
| 840 | `yas/snippet-table-name' | ||
| 841 | |||
| 842 | A symbol normally corresponding to a major mode, but can also be | ||
| 843 | a pseudo major-mode to be referenced in `yas/mode-symbol', for | ||
| 844 | example. | ||
| 845 | |||
| 846 | `yas/snippet-table-hash' | ||
| 847 | |||
| 848 | A hash table the key is a string (the snippet key) and the | ||
| 849 | value is yet another hash of (NAME TEMPLATE), where NAME is the | ||
| 850 | snippet name and TEMPLATE is a `yas/template' object name. | ||
| 851 | |||
| 852 | `yas/snippet-table-parents' | ||
| 853 | |||
| 854 | A list of tables considered parents of this table: i.e. when | ||
| 855 | searching for expansions they are searched as well." | ||
| 856 | name | ||
| 857 | (hash (make-hash-table :test 'equal)) | ||
| 858 | (parents nil)) | ||
| 859 | |||
| 860 | (defvar yas/better-guess-for-replacements nil | ||
| 861 | "If non-nil `yas/store' better guess snippet replacements.") | ||
| 862 | |||
| 863 | (defun yas/store (table name key template) | ||
| 864 | "Store a snippet template in the TABLE." | ||
| 865 | |||
| 866 | ;; This is dones by searching twice: | ||
| 867 | ;; | ||
| 868 | ;; * Try to get the existing namehash from TABLE using key. | ||
| 869 | ;; | ||
| 870 | ;; * Try to get the existing namehash from by searching the *whole* | ||
| 871 | ;; snippet table for NAME. This is becuase they user might have | ||
| 872 | ;; changed the key and that can no longer be used to locate the | ||
| 873 | ;; previous `yas/template-structure'. | ||
| 874 | ;; | ||
| 875 | ;; * If that returns nothing, oh well... | ||
| 876 | ;; | ||
| 877 | (dolist (existing-namehash (remove nil (list (gethash key (yas/snippet-table-hash table)) | ||
| 878 | (when yas/better-guess-for-replacements | ||
| 879 | (let (a) | ||
| 880 | (maphash #'(lambda (key namehash) | ||
| 881 | (when (gethash name namehash) | ||
| 882 | (setq a namehash))) | ||
| 883 | (yas/snippet-table-hash table)) | ||
| 884 | a))))) | ||
| 885 | (let ((existing-template (gethash name existing-namehash))) | ||
| 886 | (when existing-template | ||
| 887 | ;; Remove the existing keybinding | ||
| 888 | (when (yas/template-keybinding existing-template) | ||
| 889 | (define-key | ||
| 890 | (symbol-value (first (yas/template-keybinding existing-template))) | ||
| 891 | (second (yas/template-keybinding existing-template)) | ||
| 892 | nil) | ||
| 893 | (setq yas/active-keybindings | ||
| 894 | (delete (yas/template-keybinding existing-template) | ||
| 895 | yas/active-keybindings))) | ||
| 896 | ;; Remove the (name . template) mapping from existing-namehash. | ||
| 897 | (remhash name existing-namehash)))) | ||
| 898 | ;; Now store the new template independent of the previous steps. | ||
| 899 | ;; | ||
| 900 | (puthash name | ||
| 901 | template | ||
| 902 | (or (gethash key | ||
| 903 | (yas/snippet-table-hash table)) | ||
| 904 | (puthash key | ||
| 905 | (make-hash-table :test 'equal) | ||
| 906 | (yas/snippet-table-hash table))))) | ||
| 907 | |||
| 908 | (defun yas/fetch (table key) | ||
| 909 | "Fetch a snippet binding to KEY from TABLE." | ||
| 910 | (let* ((keyhash (yas/snippet-table-hash table)) | ||
| 911 | (namehash (and keyhash (gethash key keyhash)))) | ||
| 912 | (when namehash | ||
| 913 | (yas/filter-templates-by-condition | ||
| 914 | (let (alist) | ||
| 915 | (maphash #'(lambda (k v) | ||
| 916 | (push (cons k v) alist)) | ||
| 917 | namehash) | ||
| 918 | alist))))) | ||
| 919 | |||
| 920 | |||
| 921 | ;; Filtering/condition logic | ||
| 922 | |||
| 923 | (defun yas/eval-condition (condition) | ||
| 924 | (condition-case err | ||
| 925 | (save-excursion | ||
| 926 | (save-restriction | ||
| 927 | (save-match-data | ||
| 928 | (eval condition)))) | ||
| 929 | (error (progn | ||
| 930 | (message (format "[yas] error in condition evaluation: %s" | ||
| 931 | (error-message-string err))) | ||
| 932 | nil)))) | ||
| 933 | |||
| 934 | |||
| 935 | (defun yas/filter-templates-by-condition (templates) | ||
| 936 | "Filter the templates using the applicable condition. | ||
| 937 | |||
| 938 | TEMPLATES is a list of cons (NAME . TEMPLATE) where NAME is a | ||
| 939 | string and TEMPLATE is a `yas/template' structure. | ||
| 940 | |||
| 941 | This function implements the rules described in | ||
| 942 | `yas/buffer-local-condition'. See that variables documentation." | ||
| 943 | (let ((requirement (yas/require-template-specific-condition-p))) | ||
| 944 | (if (eq requirement 'always) | ||
| 945 | templates | ||
| 946 | (remove-if-not #'(lambda (pair) | ||
| 947 | (yas/template-can-expand-p (yas/template-condition (cdr pair)) requirement)) | ||
| 948 | templates)))) | ||
| 949 | |||
| 950 | (defun yas/require-template-specific-condition-p () | ||
| 951 | "Decides if this buffer requests/requires snippet-specific | ||
| 952 | conditions to filter out potential expansions." | ||
| 953 | (if (eq 'always yas/buffer-local-condition) | ||
| 954 | 'always | ||
| 955 | (let ((local-condition (or (and (consp yas/buffer-local-condition) | ||
| 956 | (yas/eval-condition yas/buffer-local-condition)) | ||
| 957 | yas/buffer-local-condition))) | ||
| 958 | (when local-condition | ||
| 959 | (if (eq local-condition t) | ||
| 960 | t | ||
| 961 | (and (consp local-condition) | ||
| 962 | (eq 'require-snippet-condition (car local-condition)) | ||
| 963 | (symbolp (cdr local-condition)) | ||
| 964 | (cdr local-condition))))))) | ||
| 965 | |||
| 966 | (defun yas/template-can-expand-p (condition &optional requirement) | ||
| 967 | "Evaluates CONDITION and REQUIREMENT and returns a boolean" | ||
| 968 | (let* ((requirement (or requirement | ||
| 969 | (yas/require-template-specific-condition-p))) | ||
| 970 | (result (or (null condition) | ||
| 971 | (yas/eval-condition | ||
| 972 | (condition-case err | ||
| 973 | (read condition) | ||
| 974 | (error (progn | ||
| 975 | (message (format "[yas] error reading condition: %s" | ||
| 976 | (error-message-string err)))) | ||
| 977 | nil)))))) | ||
| 978 | (cond ((eq requirement t) | ||
| 979 | result) | ||
| 980 | (t | ||
| 981 | (eq requirement result))))) | ||
| 982 | |||
| 983 | (defun yas/snippet-table-get-all-parents (table) | ||
| 984 | (let ((parents (yas/snippet-table-parents table))) | ||
| 985 | (when parents | ||
| 986 | (append (copy-list parents) | ||
| 987 | (mapcan #'yas/snippet-table-get-all-parents parents))))) | ||
| 988 | |||
| 989 | (defun yas/snippet-table-templates (table) | ||
| 990 | (when table | ||
| 991 | (let ((acc (list))) | ||
| 992 | (maphash #'(lambda (key namehash) | ||
| 993 | (maphash #'(lambda (name template) | ||
| 994 | (push (cons name template) acc)) | ||
| 995 | namehash)) | ||
| 996 | (yas/snippet-table-hash table)) | ||
| 997 | (yas/filter-templates-by-condition acc)))) | ||
| 998 | |||
| 999 | (defun yas/current-key () | ||
| 1000 | "Get the key under current position. A key is used to find | ||
| 1001 | the template of a snippet in the current snippet-table." | ||
| 1002 | (let ((start (point)) | ||
| 1003 | (end (point)) | ||
| 1004 | (syntaxes yas/key-syntaxes) | ||
| 1005 | syntax | ||
| 1006 | done | ||
| 1007 | templates) | ||
| 1008 | (while (and (not done) syntaxes) | ||
| 1009 | (setq syntax (car syntaxes)) | ||
| 1010 | (setq syntaxes (cdr syntaxes)) | ||
| 1011 | (save-excursion | ||
| 1012 | (skip-syntax-backward syntax) | ||
| 1013 | (setq start (point))) | ||
| 1014 | (setq templates | ||
| 1015 | (mapcan #'(lambda (table) | ||
| 1016 | (yas/fetch table (buffer-substring-no-properties start end))) | ||
| 1017 | (yas/get-snippet-tables))) | ||
| 1018 | (if templates | ||
| 1019 | (setq done t) | ||
| 1020 | (setq start end))) | ||
| 1021 | (list templates | ||
| 1022 | start | ||
| 1023 | end))) | ||
| 1024 | |||
| 1025 | |||
| 1026 | (defun yas/snippet-table-all-keys (table) | ||
| 1027 | (when table | ||
| 1028 | (let ((acc)) | ||
| 1029 | (maphash #'(lambda (key templates) | ||
| 1030 | (when (yas/filter-templates-by-condition templates) | ||
| 1031 | (push key acc))) | ||
| 1032 | (yas/snippet-table-hash table)) | ||
| 1033 | acc))) | ||
| 1034 | |||
| 1035 | |||
| 1036 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1037 | ;; Internal functions | ||
| 1038 | |||
| 1039 | (defun yas/real-mode? (mode) | ||
| 1040 | "Try to find out if MODE is a real mode. The MODE bound to | ||
| 1041 | a function (like `c-mode') is considered real mode. Other well | ||
| 1042 | known mode like `ruby-mode' which is not part of Emacs might | ||
| 1043 | not bound to a function until it is loaded. So yasnippet keeps | ||
| 1044 | a list of modes like this to help the judgement." | ||
| 1045 | (or (fboundp mode) | ||
| 1046 | (find mode yas/known-modes))) | ||
| 1047 | |||
| 1048 | (defun yas/read-and-eval-string (string) | ||
| 1049 | ;; TODO: This is a possible optimization point, the expression could | ||
| 1050 | ;; be stored in cons format instead of string, | ||
| 1051 | "Evaluate STRING and convert the result to string." | ||
| 1052 | (let ((retval (catch 'yas/exception | ||
| 1053 | (condition-case err | ||
| 1054 | (save-excursion | ||
| 1055 | (save-restriction | ||
| 1056 | (save-match-data | ||
| 1057 | (widen) | ||
| 1058 | (let ((result (eval (read string)))) | ||
| 1059 | (when result | ||
| 1060 | (format "%s" result)))))) | ||
| 1061 | (error (if yas/good-grace | ||
| 1062 | "[yas] elisp error!" | ||
| 1063 | (error (format "[yas] elisp error: %s" | ||
| 1064 | (error-message-string err))))))))) | ||
| 1065 | (when (and (consp retval) | ||
| 1066 | (eq 'yas/exception (car retval))) | ||
| 1067 | (error (cdr retval))) | ||
| 1068 | retval)) | ||
| 1069 | |||
| 1070 | (defvar yas/mode-symbol nil | ||
| 1071 | "If non-nil, lookup snippets using this instead of `major-mode'.") | ||
| 1072 | (make-variable-buffer-local 'yas/mode-symbol) | ||
| 1073 | |||
| 1074 | (defun yas/snippet-table-get-create (mode) | ||
| 1075 | "Get the snippet table corresponding to MODE. | ||
| 1076 | |||
| 1077 | Optional DIRECTORY gets recorded as the default directory to | ||
| 1078 | search for snippet files if the retrieved/created table didn't | ||
| 1079 | already have such a property." | ||
| 1080 | (let ((table (gethash mode | ||
| 1081 | yas/snippet-tables))) | ||
| 1082 | (unless table | ||
| 1083 | (setq table (yas/make-snippet-table (symbol-name mode))) | ||
| 1084 | (puthash mode table yas/snippet-tables)) | ||
| 1085 | table)) | ||
| 1086 | |||
| 1087 | (defun yas/get-snippet-tables (&optional mode-symbol dont-search-parents) | ||
| 1088 | "Get snippet tables for current buffer. | ||
| 1089 | |||
| 1090 | Return a list of 'yas/snippet-table' objects indexed by mode. | ||
| 1091 | |||
| 1092 | The modes are tried in this order: optional MODE-SYMBOL, then | ||
| 1093 | `yas/mode-symbol', then `major-mode' then, unless | ||
| 1094 | DONT-SEARCH-PARENTS is non-nil, the guessed parent mode of either | ||
| 1095 | MODE-SYMBOL or `major-mode'. | ||
| 1096 | |||
| 1097 | Guessing is done by looking up the MODE-SYMBOL's | ||
| 1098 | `derived-mode-parent' property, see also `derived-mode-p'." | ||
| 1099 | (let ((mode-tables | ||
| 1100 | (mapcar #'(lambda (mode) | ||
| 1101 | (gethash mode yas/snippet-tables)) | ||
| 1102 | (append (list mode-symbol) | ||
| 1103 | (if (listp yas/mode-symbol) | ||
| 1104 | yas/mode-symbol | ||
| 1105 | (list yas/mode-symbol)) | ||
| 1106 | (list major-mode | ||
| 1107 | (and (not dont-search-parents) | ||
| 1108 | (get (or mode-symbol major-mode) | ||
| 1109 | 'derived-mode-parent)))))) | ||
| 1110 | (all-tables)) | ||
| 1111 | (dolist (table (remove nil mode-tables)) | ||
| 1112 | (push table all-tables) | ||
| 1113 | (nconc all-tables (yas/snippet-table-get-all-parents table))) | ||
| 1114 | (remove-duplicates all-tables))) | ||
| 1115 | |||
| 1116 | (defun yas/menu-keymap-get-create (mode) | ||
| 1117 | "Get the menu keymap correspondong to MODE." | ||
| 1118 | (or (gethash mode yas/menu-table) | ||
| 1119 | (puthash mode (make-sparse-keymap) yas/menu-table))) | ||
| 1120 | |||
| 1121 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1122 | ;;; Template-related and snippet loading functions | ||
| 1123 | |||
| 1124 | (defun yas/parse-template (&optional file) | ||
| 1125 | "Parse the template in the current buffer. | ||
| 1126 | |||
| 1127 | Optional FILE is the absolute file name of the file being | ||
| 1128 | parsed. | ||
| 1129 | |||
| 1130 | Return a snippet-definition, i.e. a list | ||
| 1131 | |||
| 1132 | (KEY TEMPLATE NAME CONDITION GROUP VARS FILE KEYBINDING) | ||
| 1133 | |||
| 1134 | If the buffer contains a line of \"# --\" then the contents | ||
| 1135 | above this line are ignored. Variables can be set above this | ||
| 1136 | line through the syntax: | ||
| 1137 | |||
| 1138 | #name : value | ||
| 1139 | |||
| 1140 | Here's a list of currently recognized variables: | ||
| 1141 | |||
| 1142 | * name | ||
| 1143 | * contributor | ||
| 1144 | * condition | ||
| 1145 | * key | ||
| 1146 | * group | ||
| 1147 | * expand-env | ||
| 1148 | |||
| 1149 | #name: #include \"...\" | ||
| 1150 | # -- | ||
| 1151 | #include \"$1\"" | ||
| 1152 | ;; | ||
| 1153 | ;; | ||
| 1154 | (goto-char (point-min)) | ||
| 1155 | (let* ((name (and file | ||
| 1156 | (file-name-nondirectory file))) | ||
| 1157 | (key (unless yas/ignore-filenames-as-triggers | ||
| 1158 | (and name | ||
| 1159 | (file-name-sans-extension name)))) | ||
| 1160 | template | ||
| 1161 | bound | ||
| 1162 | condition | ||
| 1163 | (group (and file | ||
| 1164 | (yas/calculate-group file))) | ||
| 1165 | expand-env | ||
| 1166 | binding) | ||
| 1167 | (if (re-search-forward "^# --\n" nil t) | ||
| 1168 | (progn (setq template | ||
| 1169 | (buffer-substring-no-properties (point) | ||
| 1170 | (point-max))) | ||
| 1171 | (setq bound (point)) | ||
| 1172 | (goto-char (point-min)) | ||
| 1173 | (while (re-search-forward "^# *\\([^ ]+?\\) *: *\\(.*\\)$" bound t) | ||
| 1174 | (when (string= "name" (match-string-no-properties 1)) | ||
| 1175 | (setq name (match-string-no-properties 2))) | ||
| 1176 | (when (string= "condition" (match-string-no-properties 1)) | ||
| 1177 | (setq condition (match-string-no-properties 2))) | ||
| 1178 | (when (string= "group" (match-string-no-properties 1)) | ||
| 1179 | (setq group (match-string-no-properties 2))) | ||
| 1180 | (when (string= "expand-env" (match-string-no-properties 1)) | ||
| 1181 | (setq expand-env (match-string-no-properties 2))) | ||
| 1182 | (when (string= "key" (match-string-no-properties 1)) | ||
| 1183 | (setq key (match-string-no-properties 2))) | ||
| 1184 | (when (string= "binding" (match-string-no-properties 1)) | ||
| 1185 | (setq binding (match-string-no-properties 2))))) | ||
| 1186 | (setq template | ||
| 1187 | (buffer-substring-no-properties (point-min) (point-max)))) | ||
| 1188 | (list key template name condition group expand-env file binding))) | ||
| 1189 | |||
| 1190 | (defun yas/calculate-group (file) | ||
| 1191 | "Calculate the group for snippet file path FILE." | ||
| 1192 | (let* ((dominating-dir (locate-dominating-file file | ||
| 1193 | ".yas-make-groups")) | ||
| 1194 | (extra-path (and dominating-dir | ||
| 1195 | (replace-regexp-in-string (concat "^" | ||
| 1196 | (expand-file-name dominating-dir)) | ||
| 1197 | "" | ||
| 1198 | (expand-file-name file)))) | ||
| 1199 | (extra-dir (and extra-path | ||
| 1200 | (file-name-directory extra-path))) | ||
| 1201 | (group (and extra-dir | ||
| 1202 | (replace-regexp-in-string "/" | ||
| 1203 | "." | ||
| 1204 | (directory-file-name extra-dir))))) | ||
| 1205 | group)) | ||
| 1206 | |||
| 1207 | ;; (defun yas/glob-files (directory &optional recurse-p append) | ||
| 1208 | ;; "Returns files under DIRECTORY ignoring dirs and hidden files. | ||
| 1209 | |||
| 1210 | ;; If RECURSE in non-nil, do that recursively." | ||
| 1211 | ;; (let (ret | ||
| 1212 | ;; (default-directory directory)) | ||
| 1213 | ;; (dolist (entry (directory-files ".")) | ||
| 1214 | ;; (cond ((or (string-match "^\\." | ||
| 1215 | ;; (file-name-nondirectory entry)) | ||
| 1216 | ;; (string-match "~$" | ||
| 1217 | ;; (file-name-nondirectory entry))) | ||
| 1218 | ;; nil) | ||
| 1219 | ;; ((and recurse-p | ||
| 1220 | ;; (file-directory-p entry)) | ||
| 1221 | ;; (setq ret (nconc ret | ||
| 1222 | ;; (yas/glob-files (expand-file-name entry) | ||
| 1223 | ;; recurse-p | ||
| 1224 | ;; (if append | ||
| 1225 | ;; (concat append "/" entry) | ||
| 1226 | ;; entry))))) | ||
| 1227 | ;; ((file-directory-p entry) | ||
| 1228 | ;; nil) | ||
| 1229 | ;; (t | ||
| 1230 | ;; (push (if append | ||
| 1231 | ;; (concat append "/" entry) | ||
| 1232 | ;; entry) ret)))) | ||
| 1233 | ;; ret)) | ||
| 1234 | |||
| 1235 | (defun yas/subdirs (directory &optional file?) | ||
| 1236 | "Return subdirs or files of DIRECTORY according to FILE?." | ||
| 1237 | (remove-if (lambda (file) | ||
| 1238 | (or (string-match "^\\." | ||
| 1239 | (file-name-nondirectory file)) | ||
| 1240 | (string-match "~$" | ||
| 1241 | (file-name-nondirectory file)) | ||
| 1242 | (if file? | ||
| 1243 | (file-directory-p file) | ||
| 1244 | (not (file-directory-p file))))) | ||
| 1245 | (directory-files directory t))) | ||
| 1246 | |||
| 1247 | (defun yas/make-menu-binding (template) | ||
| 1248 | `(lambda () (interactive) (yas/expand-or-visit-from-menu ,template))) | ||
| 1249 | |||
| 1250 | (defun yas/expand-or-visit-from-menu (template) | ||
| 1251 | (if yas/visit-from-menu | ||
| 1252 | (yas/visit-snippet-file-1 template) | ||
| 1253 | (let ((where (if mark-active | ||
| 1254 | (cons (region-beginning) (region-end)) | ||
| 1255 | (cons (point) (point))))) | ||
| 1256 | (yas/expand-snippet (yas/template-content template) | ||
| 1257 | (car where) | ||
| 1258 | (cdr where))))) | ||
| 1259 | |||
| 1260 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1261 | ;; Popping up for keys and templates | ||
| 1262 | ;; | ||
| 1263 | (defun yas/prompt-for-template (templates &optional prompt) | ||
| 1264 | "Interactively choose a template from the list TEMPLATES. | ||
| 1265 | |||
| 1266 | TEMPLATES is a list of `yas/template'." | ||
| 1267 | (when templates | ||
| 1268 | (some #'(lambda (fn) | ||
| 1269 | (funcall fn (or prompt "Choose a snippet: ") | ||
| 1270 | templates | ||
| 1271 | #'yas/template-name)) | ||
| 1272 | yas/prompt-functions))) | ||
| 1273 | |||
| 1274 | (defun yas/prompt-for-keys (keys &optional prompt) | ||
| 1275 | "Interactively choose a template key from the list KEYS." | ||
| 1276 | (when keys | ||
| 1277 | (some #'(lambda (fn) | ||
| 1278 | (funcall fn (or prompt "Choose a snippet key: ") keys)) | ||
| 1279 | yas/prompt-functions))) | ||
| 1280 | |||
| 1281 | (defun yas/prompt-for-table (tables &optional prompt) | ||
| 1282 | (when tables | ||
| 1283 | (some #'(lambda (fn) | ||
| 1284 | (funcall fn (or prompt "Choose a snippet table: ") | ||
| 1285 | tables | ||
| 1286 | #'yas/snippet-table-name)) | ||
| 1287 | yas/prompt-functions))) | ||
| 1288 | |||
| 1289 | (defun yas/x-prompt (prompt choices &optional display-fn) | ||
| 1290 | (when (and window-system choices) | ||
| 1291 | (let ((keymap (cons 'keymap | ||
| 1292 | (cons | ||
| 1293 | prompt | ||
| 1294 | (mapcar (lambda (choice) | ||
| 1295 | (list choice | ||
| 1296 | 'menu-item | ||
| 1297 | (if display-fn | ||
| 1298 | (funcall display-fn choice) | ||
| 1299 | choice) | ||
| 1300 | t)) | ||
| 1301 | choices))))) | ||
| 1302 | (when (cdr keymap) | ||
| 1303 | (car (x-popup-menu (if (fboundp 'posn-at-point) | ||
| 1304 | (let ((x-y (posn-x-y (posn-at-point (point))))) | ||
| 1305 | (list (list (+ (car x-y) 10) | ||
| 1306 | (+ (cdr x-y) 20)) | ||
| 1307 | (selected-window))) | ||
| 1308 | t) | ||
| 1309 | keymap)))))) | ||
| 1310 | |||
| 1311 | (defun yas/ido-prompt (prompt choices &optional display-fn) | ||
| 1312 | (when (and (featurep 'ido) | ||
| 1313 | ido-mode) | ||
| 1314 | (let* ((formatted-choices (or (and display-fn | ||
| 1315 | (mapcar display-fn choices)) | ||
| 1316 | choices)) | ||
| 1317 | (chosen (and formatted-choices | ||
| 1318 | (ido-completing-read prompt | ||
| 1319 | formatted-choices | ||
| 1320 | nil | ||
| 1321 | 'require-match | ||
| 1322 | nil | ||
| 1323 | nil)))) | ||
| 1324 | (when chosen | ||
| 1325 | (nth (position chosen formatted-choices :test #'string=) choices))))) | ||
| 1326 | |||
| 1327 | (eval-when-compile (require 'dropdown-list nil t)) | ||
| 1328 | (defun yas/dropdown-prompt (prompt choices &optional display-fn) | ||
| 1329 | (when (featurep 'dropdown-list) | ||
| 1330 | (let* ((formatted-choices (or (and display-fn | ||
| 1331 | (mapcar display-fn choices)) | ||
| 1332 | choices)) | ||
| 1333 | (chosen (and formatted-choices | ||
| 1334 | (nth (dropdown-list formatted-choices) | ||
| 1335 | choices)))) | ||
| 1336 | chosen))) | ||
| 1337 | |||
| 1338 | (defun yas/completing-prompt (prompt choices &optional display-fn) | ||
| 1339 | (let* ((formatted-choices (or (and display-fn | ||
| 1340 | (mapcar display-fn choices)) | ||
| 1341 | choices)) | ||
| 1342 | (chosen (and formatted-choices | ||
| 1343 | (completing-read prompt | ||
| 1344 | formatted-choices | ||
| 1345 | nil | ||
| 1346 | 'require-match | ||
| 1347 | nil | ||
| 1348 | nil)))) | ||
| 1349 | (when chosen | ||
| 1350 | (nth (position chosen formatted-choices :test #'string=) choices)))) | ||
| 1351 | |||
| 1352 | (defun yas/no-prompt (prompt choices &optional display-fn) | ||
| 1353 | (first choices)) | ||
| 1354 | |||
| 1355 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1356 | ;; Loading snippets from files | ||
| 1357 | ;; | ||
| 1358 | (defun yas/load-directory-1 (directory &optional parents no-hierarchy-parents making-groups-sym) | ||
| 1359 | "Recursively load snippet templates from DIRECTORY." | ||
| 1360 | ;; TODO: Rewrite this horrible, horrible monster I created | ||
| 1361 | (unless (file-exists-p (concat directory "/" ".yas-skip")) | ||
| 1362 | (let* ((major-mode-and-parents (unless making-groups-sym | ||
| 1363 | (yas/compute-major-mode-and-parents (concat directory "/dummy") | ||
| 1364 | nil | ||
| 1365 | no-hierarchy-parents))) | ||
| 1366 | (yas/ignore-filenames-as-triggers (or yas/ignore-filenames-as-triggers | ||
| 1367 | (file-exists-p (concat directory "/" ".yas-ignore-filenames-as-triggers")))) | ||
| 1368 | (mode-sym (and major-mode-and-parents | ||
| 1369 | (car major-mode-and-parents))) | ||
| 1370 | (parents (if making-groups-sym | ||
| 1371 | parents | ||
| 1372 | (rest major-mode-and-parents))) | ||
| 1373 | (snippet-defs nil) | ||
| 1374 | (make-groups-p (or making-groups-sym | ||
| 1375 | (file-exists-p (concat directory "/" ".yas-make-groups"))))) | ||
| 1376 | (with-temp-buffer | ||
| 1377 | (dolist (file (yas/subdirs directory 'no-subdirs-just-files)) | ||
| 1378 | (when (file-readable-p file) | ||
| 1379 | (insert-file-contents file nil nil nil t) | ||
| 1380 | (push (yas/parse-template file) | ||
| 1381 | snippet-defs)))) | ||
| 1382 | (yas/define-snippets (or mode-sym | ||
| 1383 | making-groups-sym) | ||
| 1384 | snippet-defs | ||
| 1385 | parents) | ||
| 1386 | (dolist (subdir (yas/subdirs directory)) | ||
| 1387 | (if make-groups-p | ||
| 1388 | (yas/load-directory-1 subdir parents 't (or mode-sym | ||
| 1389 | making-groups-sym)) | ||
| 1390 | (yas/load-directory-1 subdir (list mode-sym))))))) | ||
| 1391 | |||
| 1392 | (defun yas/load-directory (directory) | ||
| 1393 | "Load snippet definition from a directory hierarchy. | ||
| 1394 | |||
| 1395 | Below the top-level directory, each directory is a mode | ||
| 1396 | name. And under each subdirectory, each file is a definition | ||
| 1397 | of a snippet. The file name is the trigger key and the | ||
| 1398 | content of the file is the template." | ||
| 1399 | (interactive "DSelect the root directory: ") | ||
| 1400 | (unless (file-directory-p directory) | ||
| 1401 | (error "Error %s not a directory" directory)) | ||
| 1402 | (unless yas/root-directory | ||
| 1403 | (setq yas/root-directory directory)) | ||
| 1404 | (dolist (dir (yas/subdirs directory)) | ||
| 1405 | (yas/load-directory-1 dir nil 'no-hierarchy-parents)) | ||
| 1406 | (when (interactive-p) | ||
| 1407 | (message "done."))) | ||
| 1408 | |||
| 1409 | (defun yas/kill-snippet-keybindings () | ||
| 1410 | "Remove the all active snippet keybindings." | ||
| 1411 | (interactive) | ||
| 1412 | (dolist (keybinding yas/active-keybindings) | ||
| 1413 | (define-key (symbol-value (first keybinding)) (second keybinding) nil)) | ||
| 1414 | (setq yas/active-keybindings nil)) | ||
| 1415 | |||
| 1416 | (defun yas/reload-all (&optional reset-root-directory) | ||
| 1417 | "Reload all snippets and rebuild the YASnippet menu. " | ||
| 1418 | (interactive "P") | ||
| 1419 | ;; Turn off global modes and minor modes, save their state though | ||
| 1420 | ;; | ||
| 1421 | (let ((restore-global-mode (prog1 yas/global-mode | ||
| 1422 | (yas/global-mode -1))) | ||
| 1423 | (restore-minor-mode (prog1 yas/minor-mode | ||
| 1424 | (yas/minor-mode -1)))) | ||
| 1425 | ;; Empty all snippet tables and all menu tables | ||
| 1426 | ;; | ||
| 1427 | (setq yas/snippet-tables (make-hash-table)) | ||
| 1428 | (setq yas/menu-table (make-hash-table)) | ||
| 1429 | |||
| 1430 | ;; Init the `yas/minor-mode-map', taking care not to break the | ||
| 1431 | ;; menu.... | ||
| 1432 | ;; | ||
| 1433 | (setf (cdr yas/minor-mode-map) | ||
| 1434 | (cdr (yas/init-minor-keymap))) | ||
| 1435 | |||
| 1436 | ;; Now, clean up the other keymaps we might have cluttered up. | ||
| 1437 | (yas/kill-snippet-keybindings) | ||
| 1438 | |||
| 1439 | (when reset-root-directory | ||
| 1440 | (setq yas/root-directory nil)) | ||
| 1441 | |||
| 1442 | ;; Reload the directories listed in `yas/root-directory' or prompt | ||
| 1443 | ;; the user to select one. | ||
| 1444 | ;; | ||
| 1445 | (if yas/root-directory | ||
| 1446 | (if (listp yas/root-directory) | ||
| 1447 | (dolist (directory yas/root-directory) | ||
| 1448 | (yas/load-directory directory)) | ||
| 1449 | (yas/load-directory yas/root-directory)) | ||
| 1450 | (call-interactively 'yas/load-directory)) | ||
| 1451 | |||
| 1452 | ;; Restore the mode configuration | ||
| 1453 | ;; | ||
| 1454 | (when restore-minor-mode | ||
| 1455 | (yas/minor-mode 1)) | ||
| 1456 | (when restore-global-mode | ||
| 1457 | (yas/global-mode 1)) | ||
| 1458 | |||
| 1459 | (message "[yas] Reloading everything... Done."))) | ||
| 1460 | |||
| 1461 | (defun yas/quote-string (string) | ||
| 1462 | "Escape and quote STRING. | ||
| 1463 | foo\"bar\\! -> \"foo\\\"bar\\\\!\"" | ||
| 1464 | (concat "\"" | ||
| 1465 | (replace-regexp-in-string "[\\\"]" | ||
| 1466 | "\\\\\\&" | ||
| 1467 | string | ||
| 1468 | t) | ||
| 1469 | "\"")) | ||
| 1470 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1471 | ;;; Yasnippet Bundle | ||
| 1472 | |||
| 1473 | (defun yas/initialize () | ||
| 1474 | "For backward compatibility, enable `yas/minor-mode' globally" | ||
| 1475 | (yas/global-mode 1)) | ||
| 1476 | |||
| 1477 | (defun yas/compile-bundle | ||
| 1478 | (&optional yasnippet yasnippet-bundle snippet-roots code dropdown) | ||
| 1479 | "Compile snippets in SNIPPET-ROOTS to a single bundle file. | ||
| 1480 | |||
| 1481 | YASNIPPET is the yasnippet.el file path. | ||
| 1482 | |||
| 1483 | YASNIPPET-BUNDLE is the output file of the compile result. | ||
| 1484 | |||
| 1485 | SNIPPET-ROOTS is a list of root directories that contains the | ||
| 1486 | snippets definition. | ||
| 1487 | |||
| 1488 | CODE is the code to be placed at the end of the generated file | ||
| 1489 | and that can initialize the YASnippet bundle. | ||
| 1490 | |||
| 1491 | Last optional argument DROPDOWN is the filename of the | ||
| 1492 | dropdown-list.el library. | ||
| 1493 | |||
| 1494 | Here's the default value for all the parameters: | ||
| 1495 | |||
| 1496 | (yas/compile-bundle \"yasnippet.el\" | ||
| 1497 | \"yasnippet-bundle.el\" | ||
| 1498 | \"snippets\") | ||
| 1499 | \"(yas/initialize-bundle) | ||
| 1500 | ### autoload | ||
| 1501 | (require 'yasnippet-bundle)`\" | ||
| 1502 | \"dropdown-list.el\") | ||
| 1503 | " | ||
| 1504 | (interactive "ffind the yasnippet.el file: \nFTarget bundle file: \nDSnippet directory to bundle: \nMExtra code? \nfdropdown-library: ") | ||
| 1505 | |||
| 1506 | (let* ((yasnippet (or yasnippet | ||
| 1507 | "yasnippet.el")) | ||
| 1508 | (yasnippet-bundle (or yasnippet-bundle | ||
| 1509 | "./yasnippet-bundle.el")) | ||
| 1510 | (snippet-roots (or snippet-roots | ||
| 1511 | "snippets")) | ||
| 1512 | (dropdown (or dropdown | ||
| 1513 | "dropdown-list.el")) | ||
| 1514 | (code (or (and code | ||
| 1515 | (condition-case err (read code) (error nil)) | ||
| 1516 | code) | ||
| 1517 | (concat "(yas/initialize-bundle)" | ||
| 1518 | "\n;;;###autoload" ; break through so that won't | ||
| 1519 | "(require 'yasnippet-bundle)"))) | ||
| 1520 | (dirs (or (and (listp snippet-roots) snippet-roots) | ||
| 1521 | (list snippet-roots))) | ||
| 1522 | (bundle-buffer nil)) | ||
| 1523 | (with-temp-file yasnippet-bundle | ||
| 1524 | (insert ";;; yasnippet-bundle.el --- " | ||
| 1525 | "Yet another snippet extension (Auto compiled bundle)\n") | ||
| 1526 | (insert-file-contents yasnippet) | ||
| 1527 | (goto-char (point-max)) | ||
| 1528 | (insert "\n") | ||
| 1529 | (when dropdown | ||
| 1530 | (insert-file-contents dropdown)) | ||
| 1531 | (goto-char (point-max)) | ||
| 1532 | (insert ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n") | ||
| 1533 | (insert ";;;; Auto-generated code ;;;;\n") | ||
| 1534 | (insert ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n") | ||
| 1535 | (insert "(defun yas/initialize-bundle ()\n" | ||
| 1536 | " \"Initialize YASnippet and load snippets in the bundle.\"") | ||
| 1537 | (flet ((yas/define-snippets | ||
| 1538 | (mode snippets &optional parent-or-parents) | ||
| 1539 | (insert ";;; snippets for " (symbol-name mode) "\n") | ||
| 1540 | (let ((literal-snippets (list))) | ||
| 1541 | (dolist (snippet snippets) | ||
| 1542 | (let ((key (first snippet)) | ||
| 1543 | (template-content (second snippet)) | ||
| 1544 | (name (third snippet)) | ||
| 1545 | (condition (fourth snippet)) | ||
| 1546 | (group (fifth snippet)) | ||
| 1547 | (expand-env (sixth snippet)) | ||
| 1548 | ;; Omit the file on purpose | ||
| 1549 | (file nil) ;; (seventh snippet)) | ||
| 1550 | (binding (eighth snippet))) | ||
| 1551 | (push `(,key | ||
| 1552 | ,template-content | ||
| 1553 | ,name | ||
| 1554 | ,condition | ||
| 1555 | ,group | ||
| 1556 | ,expand-env | ||
| 1557 | ,file | ||
| 1558 | ,binding) | ||
| 1559 | literal-snippets))) | ||
| 1560 | (insert (pp-to-string `(yas/define-snippets ',mode ',literal-snippets ',parent-or-parents))) | ||
| 1561 | (insert "\n\n")))) | ||
| 1562 | (dolist (dir dirs) | ||
| 1563 | (dolist (subdir (yas/subdirs dir)) | ||
| 1564 | (yas/load-directory-1 subdir nil 'no-hierarchy-parents)))) | ||
| 1565 | |||
| 1566 | (insert (pp-to-string `(yas/global-mode 1))) | ||
| 1567 | (insert ")\n\n" code "\n") | ||
| 1568 | |||
| 1569 | ;; bundle-specific provide and value for yas/dont-activate | ||
| 1570 | (let ((bundle-feature-name (file-name-nondirectory | ||
| 1571 | (file-name-sans-extension | ||
| 1572 | yasnippet-bundle)))) | ||
| 1573 | (insert (pp-to-string `(set-default 'yas/dont-activate | ||
| 1574 | #'(lambda () | ||
| 1575 | (and (or yas/root-directory | ||
| 1576 | (featurep ',(make-symbol bundle-feature-name))) | ||
| 1577 | (null (yas/get-snippet-tables))))))) | ||
| 1578 | (insert (pp-to-string `(provide ',(make-symbol bundle-feature-name))))) | ||
| 1579 | |||
| 1580 | (insert ";;; " | ||
| 1581 | (file-name-nondirectory yasnippet-bundle) | ||
| 1582 | " ends here\n")))) | ||
| 1583 | |||
| 1584 | (defun yas/compile-textmate-bundle () | ||
| 1585 | (interactive) | ||
| 1586 | (yas/compile-bundle "yasnippet.el" | ||
| 1587 | "./yasnippet-textmate-bundle.el" | ||
| 1588 | "extras/imported/" | ||
| 1589 | (concat "(yas/initialize-bundle)" | ||
| 1590 | "\n;;;###autoload" ; break through so that won't | ||
| 1591 | "(require 'yasnippet-textmate-bundle)") | ||
| 1592 | "dropdown-list.el")) | ||
| 1593 | |||
| 1594 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1595 | ;;; Some user level functions | ||
| 1596 | ;;; | ||
| 1597 | |||
| 1598 | (defun yas/about () | ||
| 1599 | (interactive) | ||
| 1600 | (message (concat "yasnippet (version " | ||
| 1601 | yas/version | ||
| 1602 | ") -- pluskid <pluskid@gmail.com>/joaotavora <joaotavora@gmail.com>"))) | ||
| 1603 | |||
| 1604 | (defun yas/define-snippets (mode snippets &optional parent-mode) | ||
| 1605 | "Define SNIPPETS for MODE. | ||
| 1606 | |||
| 1607 | SNIPPETS is a list of snippet definitions, each taking the | ||
| 1608 | following form: | ||
| 1609 | |||
| 1610 | (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING) | ||
| 1611 | |||
| 1612 | Within these, only TEMPLATE is actually mandatory. | ||
| 1613 | |||
| 1614 | All the elelements are strings, including CONDITION, EXPAND-ENV | ||
| 1615 | and KEYBINDING which will be `read' and eventually `eval'-ed. | ||
| 1616 | |||
| 1617 | FILE is probably of very little use if you're programatically | ||
| 1618 | defining snippets. | ||
| 1619 | |||
| 1620 | You can use `yas/parse-template' to return such lists based on | ||
| 1621 | the current buffers contents. | ||
| 1622 | |||
| 1623 | Optional PARENT-MODE can be used to specify the parent tables of | ||
| 1624 | MODE. It can be a mode symbol of a list of mode symbols. It does | ||
| 1625 | not need to be a real mode." | ||
| 1626 | (let ((snippet-table (yas/snippet-table-get-create mode)) | ||
| 1627 | (parent-tables (mapcar #'yas/snippet-table-get-create | ||
| 1628 | (if (listp parent-mode) | ||
| 1629 | parent-mode | ||
| 1630 | (list parent-mode)))) | ||
| 1631 | (keymap (if yas/use-menu | ||
| 1632 | (yas/menu-keymap-get-create mode) | ||
| 1633 | nil))) | ||
| 1634 | ;; Setup the menu | ||
| 1635 | ;; | ||
| 1636 | (when parent-tables | ||
| 1637 | (setf (yas/snippet-table-parents snippet-table) | ||
| 1638 | parent-tables) | ||
| 1639 | (when yas/use-menu | ||
| 1640 | (let ((parent-menu-syms-and-names | ||
| 1641 | (if (listp parent-mode) | ||
| 1642 | (mapcar #'(lambda (sym) | ||
| 1643 | (cons sym (concat "parent mode - " (symbol-name sym)))) | ||
| 1644 | parent-mode) | ||
| 1645 | '((parent-mode . "parent mode"))))) | ||
| 1646 | (mapc #'(lambda (sym-and-name) | ||
| 1647 | (define-key keymap | ||
| 1648 | (vector (intern (replace-regexp-in-string " " "_" (cdr sym-and-name)))) | ||
| 1649 | (list 'menu-item (cdr sym-and-name) | ||
| 1650 | (yas/menu-keymap-get-create (car sym-and-name))))) | ||
| 1651 | (reverse parent-menu-syms-and-names))))) | ||
| 1652 | (when yas/use-menu | ||
| 1653 | (define-key yas/minor-mode-menu (vector mode) | ||
| 1654 | `(menu-item ,(symbol-name mode) ,keymap | ||
| 1655 | :visible (yas/show-menu-p ',mode)))) | ||
| 1656 | ;; Iterate the recently parsed snippets definition | ||
| 1657 | ;; | ||
| 1658 | (dolist (snippet snippets) | ||
| 1659 | (let* ((file (seventh snippet)) | ||
| 1660 | (key (or (car snippet) | ||
| 1661 | (unless yas/ignore-filenames-as-triggers | ||
| 1662 | (and file | ||
| 1663 | (file-name-sans-extension (file-name-nondirectory file)))))) | ||
| 1664 | (name (or (third snippet) | ||
| 1665 | (and file | ||
| 1666 | (file-name-directory file)))) | ||
| 1667 | (condition (fourth snippet)) | ||
| 1668 | (group (fifth snippet)) | ||
| 1669 | (keybinding (eighth snippet)) | ||
| 1670 | (template nil)) | ||
| 1671 | ;; Read the snippet's "binding :" expression | ||
| 1672 | ;; | ||
| 1673 | (condition-case err | ||
| 1674 | (when keybinding | ||
| 1675 | (setq keybinding (read (eighth snippet))) | ||
| 1676 | (let* ((this-mode-map-symbol (intern (concat (symbol-name mode) "-map"))) | ||
| 1677 | (keys (or (and (consp keybinding) | ||
| 1678 | (read-kbd-macro (cdr keybinding))) | ||
| 1679 | (read-kbd-macro keybinding))) | ||
| 1680 | (keymap-symbol (or (and (consp keybinding) | ||
| 1681 | (car keybinding)) | ||
| 1682 | this-mode-map-symbol))) | ||
| 1683 | (if (and (boundp keymap-symbol) | ||
| 1684 | (keymapp (symbol-value keymap-symbol))) | ||
| 1685 | (setq keybinding (list keymap-symbol | ||
| 1686 | keys | ||
| 1687 | name)) | ||
| 1688 | (error (format "keymap \"%s\" does not (yet?) exist" keymap-symbol))))) | ||
| 1689 | (error | ||
| 1690 | (message "[yas] warning: keybinding \"%s\" invalid for snippet \"%s\" since %s." | ||
| 1691 | keybinding name (error-message-string err)) | ||
| 1692 | (setf keybinding nil))) | ||
| 1693 | |||
| 1694 | ;; Create the `yas/template' object and store in the | ||
| 1695 | ;; appropriate snippet table. This only done if we have found | ||
| 1696 | ;; a key and a name for the snippet, because that is what | ||
| 1697 | ;; indexes the snippet tables | ||
| 1698 | ;; | ||
| 1699 | (setq template (yas/make-template (second snippet) | ||
| 1700 | (or name key) | ||
| 1701 | condition | ||
| 1702 | (sixth snippet) | ||
| 1703 | (seventh snippet) | ||
| 1704 | keybinding)) | ||
| 1705 | (when (and key | ||
| 1706 | name) | ||
| 1707 | (yas/store snippet-table | ||
| 1708 | name | ||
| 1709 | key | ||
| 1710 | template)) | ||
| 1711 | ;; If we have a keybinding, register it if it does not | ||
| 1712 | ;; conflict! | ||
| 1713 | ;; | ||
| 1714 | (when keybinding | ||
| 1715 | (let ((lookup (lookup-key (symbol-value (first keybinding)) (second keybinding)))) | ||
| 1716 | (if (and lookup | ||
| 1717 | (not (numberp lookup))) | ||
| 1718 | (message "[yas] warning: won't overwrite keybinding \"%s\" for snippet \"%s\" in `%s'" | ||
| 1719 | (key-description (second keybinding)) name (first keybinding)) | ||
| 1720 | (define-key | ||
| 1721 | (symbol-value (first keybinding)) | ||
| 1722 | (second keybinding) | ||
| 1723 | `(lambda (&optional yas/prefix) | ||
| 1724 | (interactive "P") | ||
| 1725 | (when (yas/template-can-expand-p ,(yas/template-condition template)) | ||
| 1726 | (yas/expand-snippet ,(yas/template-content template) | ||
| 1727 | nil | ||
| 1728 | nil | ||
| 1729 | ,(yas/template-expand-env template))))) | ||
| 1730 | (add-to-list 'yas/active-keybindings keybinding)))) | ||
| 1731 | |||
| 1732 | ;; Setup the menu groups, reorganizing from group to group if | ||
| 1733 | ;; necessary | ||
| 1734 | ;; | ||
| 1735 | (when yas/use-menu | ||
| 1736 | (let ((group-keymap keymap)) | ||
| 1737 | ;; Delete this entry from another group if already exists | ||
| 1738 | ;; in some other group. An entry is considered as existing | ||
| 1739 | ;; in another group if its name string-matches. | ||
| 1740 | ;; | ||
| 1741 | (yas/delete-from-keymap group-keymap name) | ||
| 1742 | |||
| 1743 | ;; ... then add this entry to the correct group | ||
| 1744 | (when (and (not (null group)) | ||
| 1745 | (not (string= "" group))) | ||
| 1746 | (dolist (subgroup (mapcar #'make-symbol | ||
| 1747 | (split-string group "\\."))) | ||
| 1748 | (let ((subgroup-keymap (lookup-key group-keymap | ||
| 1749 | (vector subgroup)))) | ||
| 1750 | (when (null subgroup-keymap) | ||
| 1751 | (setq subgroup-keymap (make-sparse-keymap)) | ||
| 1752 | (define-key group-keymap (vector subgroup) | ||
| 1753 | `(menu-item ,(symbol-name subgroup) | ||
| 1754 | ,subgroup-keymap))) | ||
| 1755 | (setq group-keymap subgroup-keymap)))) | ||
| 1756 | (define-key group-keymap (vector (gensym)) | ||
| 1757 | `(menu-item ,(yas/template-name template) | ||
| 1758 | ,(yas/make-menu-binding template) | ||
| 1759 | :help ,name | ||
| 1760 | :keys ,(when (and key name) | ||
| 1761 | (concat key yas/trigger-symbol)))))))))) | ||
| 1762 | |||
| 1763 | (defun yas/show-menu-p (mode) | ||
| 1764 | (cond ((eq yas/use-menu 'abbreviate) | ||
| 1765 | (find mode | ||
| 1766 | (mapcar #'(lambda (table) | ||
| 1767 | (intern (yas/snippet-table-name table))) | ||
| 1768 | (yas/get-snippet-tables)))) | ||
| 1769 | ((eq yas/use-menu 'real-modes) | ||
| 1770 | (yas/real-mode? mode)) | ||
| 1771 | (t | ||
| 1772 | t))) | ||
| 1773 | |||
| 1774 | (defun yas/delete-from-keymap (keymap name) | ||
| 1775 | "Recursively delete items name NAME from KEYMAP and its submenus. | ||
| 1776 | |||
| 1777 | Skip any submenus named \"parent mode\"" | ||
| 1778 | ;; First of all, recursively enter submenus, i.e. the tree is | ||
| 1779 | ;; searched depth first so that stale submenus can be found in the | ||
| 1780 | ;; higher passes. | ||
| 1781 | ;; | ||
| 1782 | (mapc #'(lambda (item) | ||
| 1783 | (when (and (keymapp (fourth item)) | ||
| 1784 | (stringp (third item)) | ||
| 1785 | (not (string-match "parent mode" (third item)))) | ||
| 1786 | (yas/delete-from-keymap (fourth item) name))) | ||
| 1787 | (rest keymap)) | ||
| 1788 | ;; | ||
| 1789 | (when (keymapp keymap) | ||
| 1790 | (let ((pos-in-keymap)) | ||
| 1791 | (while (setq pos-in-keymap | ||
| 1792 | (position-if #'(lambda (item) | ||
| 1793 | (and (listp item) | ||
| 1794 | (or | ||
| 1795 | ;; the menu item we want to delete | ||
| 1796 | (and (eq 'menu-item (second item)) | ||
| 1797 | (third item) | ||
| 1798 | (and (string= (third item) name))) | ||
| 1799 | ;; a stale subgroup | ||
| 1800 | (and (keymapp (fourth item)) | ||
| 1801 | (not (and (stringp (third item)) | ||
| 1802 | (string-match "parent mode" | ||
| 1803 | (third item)))) | ||
| 1804 | (null (rest (fourth item))))))) | ||
| 1805 | keymap)) | ||
| 1806 | (setf (nthcdr pos-in-keymap keymap) | ||
| 1807 | (nthcdr (+ 1 pos-in-keymap) keymap)))))) | ||
| 1808 | |||
| 1809 | (defun yas/define (mode key template &optional name condition group) | ||
| 1810 | "Define a snippet. Expanding KEY into TEMPLATE. | ||
| 1811 | |||
| 1812 | NAME is a description to this template. Also update the menu if | ||
| 1813 | `yas/use-menu' is `t'. CONDITION is the condition attached to | ||
| 1814 | this snippet. If you attach a condition to a snippet, then it | ||
| 1815 | will only be expanded when the condition evaluated to non-nil." | ||
| 1816 | (yas/define-snippets mode | ||
| 1817 | (list (list key template name condition group)))) | ||
| 1818 | |||
| 1819 | (defun yas/hippie-try-expand (first-time?) | ||
| 1820 | "Integrate with hippie expand. Just put this function in | ||
| 1821 | `hippie-expand-try-functions-list'." | ||
| 1822 | (if (not first-time?) | ||
| 1823 | (let ((yas/fallback-behavior 'return-nil)) | ||
| 1824 | (yas/expand)) | ||
| 1825 | (undo 1) | ||
| 1826 | nil)) | ||
| 1827 | |||
| 1828 | (defun yas/expand () | ||
| 1829 | "Expand a snippet before point. | ||
| 1830 | |||
| 1831 | If no snippet expansion is possible, fall back to the behaviour | ||
| 1832 | defined in `yas/fallback-behavior'" | ||
| 1833 | (interactive) | ||
| 1834 | (yas/expand-1)) | ||
| 1835 | |||
| 1836 | (defun yas/expand-1 (&optional field) | ||
| 1837 | "Actually fo the work for `yas/expand'" | ||
| 1838 | (multiple-value-bind (templates start end) (if field | ||
| 1839 | (save-restriction | ||
| 1840 | (narrow-to-region (yas/field-start field) (yas/field-end field)) | ||
| 1841 | (yas/current-key)) | ||
| 1842 | (yas/current-key)) | ||
| 1843 | (if templates | ||
| 1844 | (let ((template (or (and (rest templates) ;; more than one | ||
| 1845 | (yas/prompt-for-template (mapcar #'cdr templates))) | ||
| 1846 | (cdar templates)))) | ||
| 1847 | (when template | ||
| 1848 | (yas/expand-snippet (yas/template-content template) | ||
| 1849 | start | ||
| 1850 | end | ||
| 1851 | (yas/template-expand-env template)))) | ||
| 1852 | (cond ((eq yas/fallback-behavior 'return-nil) | ||
| 1853 | ;; return nil | ||
| 1854 | nil) | ||
| 1855 | ((eq yas/fallback-behavior 'call-other-command) | ||
| 1856 | (let* ((yas/minor-mode nil) | ||
| 1857 | (keys-1 (this-command-keys-vector)) | ||
| 1858 | (keys-2 (and yas/trigger-key | ||
| 1859 | (stringp yas/trigger-key) | ||
| 1860 | (read-kbd-macro yas/trigger-key))) | ||
| 1861 | (command-1 (and keys-1 (key-binding keys-1))) | ||
| 1862 | (command-2 (and keys-2 (key-binding keys-2))) | ||
| 1863 | (command (or (and (not (eq command-1 'yas/expand)) | ||
| 1864 | command-1) | ||
| 1865 | command-2))) | ||
| 1866 | (when (and (commandp command) | ||
| 1867 | (not (eq 'yas/expand command))) | ||
| 1868 | (setq this-command command) | ||
| 1869 | (call-interactively command)))) | ||
| 1870 | ((and (listp yas/fallback-behavior) | ||
| 1871 | (cdr yas/fallback-behavior) | ||
| 1872 | (eq 'apply (car yas/fallback-behavior))) | ||
| 1873 | (if (cddr yas/fallback-behavior) | ||
| 1874 | (apply (cadr yas/fallback-behavior) | ||
| 1875 | (cddr yas/fallback-behavior)) | ||
| 1876 | (when (commandp (cadr yas/fallback-behavior)) | ||
| 1877 | (setq this-command (cadr yas/fallback-behavior)) | ||
| 1878 | (call-interactively (cadr yas/fallback-behavior))))) | ||
| 1879 | (t | ||
| 1880 | ;; also return nil if all the other fallbacks have failed | ||
| 1881 | nil))))) | ||
| 1882 | |||
| 1883 | |||
| 1884 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 1885 | ;;; Snippet development | ||
| 1886 | |||
| 1887 | (defun yas/all-templates (tables) | ||
| 1888 | "Return all snippet tables applicable for the current buffer. | ||
| 1889 | |||
| 1890 | Honours `yas/choose-tables-first', `yas/choose-keys-first' and | ||
| 1891 | `yas/buffer-local-condition'" | ||
| 1892 | (when yas/choose-tables-first | ||
| 1893 | (setq tables (list (yas/prompt-for-table tables)))) | ||
| 1894 | (mapcar #'cdr | ||
| 1895 | (if yas/choose-keys-first | ||
| 1896 | (let ((key (yas/prompt-for-keys | ||
| 1897 | (mapcan #'yas/snippet-table-all-keys tables)))) | ||
| 1898 | (when key | ||
| 1899 | (mapcan #'(lambda (table) | ||
| 1900 | (yas/fetch table key)) | ||
| 1901 | tables))) | ||
| 1902 | (mapcan #'yas/snippet-table-templates tables)))) | ||
| 1903 | |||
| 1904 | (defun yas/insert-snippet (&optional no-condition) | ||
| 1905 | "Choose a snippet to expand, pop-up a list of choices according | ||
| 1906 | to `yas/prompt-function'. | ||
| 1907 | |||
| 1908 | With prefix argument NO-CONDITION, bypass filtering of snippets | ||
| 1909 | by condition." | ||
| 1910 | (interactive "P") | ||
| 1911 | (let* ((yas/buffer-local-condition (or (and no-condition | ||
| 1912 | 'always) | ||
| 1913 | yas/buffer-local-condition)) | ||
| 1914 | (templates (yas/all-templates (yas/get-snippet-tables))) | ||
| 1915 | (template (and templates | ||
| 1916 | (or (and (rest templates) ;; more than one template for same key | ||
| 1917 | (yas/prompt-for-template templates)) | ||
| 1918 | (car templates)))) | ||
| 1919 | (where (if mark-active | ||
| 1920 | (cons (region-beginning) (region-end)) | ||
| 1921 | (cons (point) (point))))) | ||
| 1922 | (if template | ||
| 1923 | (yas/expand-snippet (yas/template-content template) | ||
| 1924 | (car where) | ||
| 1925 | (cdr where) | ||
| 1926 | (yas/template-expand-env template)) | ||
| 1927 | (message "[yas] No snippets can be inserted here!")))) | ||
| 1928 | |||
| 1929 | (defun yas/visit-snippet-file () | ||
| 1930 | "Choose a snippet to edit, selection like `yas/insert-snippet'. | ||
| 1931 | |||
| 1932 | Only success if selected snippet was loaded from a file. Put the | ||
| 1933 | visited file in `snippet-mode'." | ||
| 1934 | (interactive) | ||
| 1935 | (let* ((yas/buffer-local-condition 'always) | ||
| 1936 | (templates (yas/all-templates (yas/get-snippet-tables))) | ||
| 1937 | (template (and templates | ||
| 1938 | (or (and (rest templates) ;; more than one template for same key | ||
| 1939 | (yas/prompt-for-template templates | ||
| 1940 | "Choose a snippet template to edit: ")) | ||
| 1941 | (car templates))))) | ||
| 1942 | |||
| 1943 | (when template | ||
| 1944 | (yas/visit-snippet-file-1 template)))) | ||
| 1945 | |||
| 1946 | (defun yas/visit-snippet-file-1 (template) | ||
| 1947 | (let ((file (yas/template-file template))) | ||
| 1948 | (cond ((and file (file-exists-p file)) | ||
| 1949 | (find-file-other-window file) | ||
| 1950 | (snippet-mode)) | ||
| 1951 | (file | ||
| 1952 | (message "Original file %s no longer exists!" file)) | ||
| 1953 | (t | ||
| 1954 | (message "This snippet was not loaded from a file!"))))) | ||
| 1955 | |||
| 1956 | (defun yas/guess-snippet-directories-1 (table &optional suffix) | ||
| 1957 | "Guesses possible snippet subdirsdirectories for TABLE." | ||
| 1958 | (unless suffix | ||
| 1959 | (setq suffix (yas/snippet-table-name table))) | ||
| 1960 | (cons suffix | ||
| 1961 | (mapcan #'(lambda (parent) | ||
| 1962 | (yas/guess-snippet-directories-1 | ||
| 1963 | parent | ||
| 1964 | (concat (yas/snippet-table-name parent) "/" suffix))) | ||
| 1965 | (yas/snippet-table-parents table)))) | ||
| 1966 | |||
| 1967 | (defun yas/guess-snippet-directories () | ||
| 1968 | "Try to guess suitable directories based on the current active | ||
| 1969 | tables. | ||
| 1970 | |||
| 1971 | Returns a a list of options alist TABLE -> DIRS where DIRS are | ||
| 1972 | all the possibly directories where snippets of table might be | ||
| 1973 | lurking." | ||
| 1974 | (let ((main-dir (or (and (listp yas/root-directory) | ||
| 1975 | (first yas/root-directory)) | ||
| 1976 | yas/root-directory | ||
| 1977 | (setq yas/root-directory "~/.emacs.d/snippets"))) | ||
| 1978 | (tables (yas/get-snippet-tables))) | ||
| 1979 | ;; HACK! the snippet table created here is a dummy table that | ||
| 1980 | ;; holds the correct name so that `yas/make-directory-maybe' can | ||
| 1981 | ;; work. The real table, if it does not exist in | ||
| 1982 | ;; yas/snippet-tables will be created when the first snippet for | ||
| 1983 | ;; that mode is loaded. | ||
| 1984 | ;; | ||
| 1985 | (unless (gethash major-mode yas/snippet-tables) | ||
| 1986 | (setq tables (cons (yas/make-snippet-table (symbol-name major-mode)) | ||
| 1987 | tables))) | ||
| 1988 | |||
| 1989 | (mapcar #'(lambda (table) | ||
| 1990 | (cons table | ||
| 1991 | (mapcar #'(lambda (subdir) | ||
| 1992 | (concat main-dir "/" subdir)) | ||
| 1993 | (yas/guess-snippet-directories-1 table)))) | ||
| 1994 | tables))) | ||
| 1995 | |||
| 1996 | (defun yas/make-directory-maybe (table-and-dirs &optional main-table-string) | ||
| 1997 | "Returns a dir inside TABLE-AND-DIRS, prompts for creation if none exists." | ||
| 1998 | (or (some #'(lambda (dir) (when (file-directory-p dir) dir)) (cdr table-and-dirs)) | ||
| 1999 | (let ((candidate (first (cdr table-and-dirs)))) | ||
| 2000 | (if (y-or-n-p (format "Guessed directory (%s) for%s%s table \"%s\" does not exist! Create? " | ||
| 2001 | candidate | ||
| 2002 | (if (gethash (intern (yas/snippet-table-name (car table-and-dirs))) | ||
| 2003 | yas/snippet-tables) | ||
| 2004 | "" | ||
| 2005 | " brand new") | ||
| 2006 | (or main-table-string | ||
| 2007 | "") | ||
| 2008 | (yas/snippet-table-name (car table-and-dirs)))) | ||
| 2009 | (progn | ||
| 2010 | (make-directory candidate 'also-make-parents) | ||
| 2011 | ;; create the .yas-parents file here... | ||
| 2012 | candidate))))) | ||
| 2013 | |||
| 2014 | (defun yas/new-snippet (&optional choose-instead-of-guess) | ||
| 2015 | "" | ||
| 2016 | (interactive "P") | ||
| 2017 | (let* ((guessed-directories (yas/guess-snippet-directories)) | ||
| 2018 | (option (or (and choose-instead-of-guess | ||
| 2019 | (some #'(lambda (fn) | ||
| 2020 | (funcall fn "Choose a snippet table: " | ||
| 2021 | guessed-directories | ||
| 2022 | #'(lambda (option) | ||
| 2023 | (yas/snippet-table-name (car option))))) | ||
| 2024 | yas/prompt-functions)) | ||
| 2025 | (first guessed-directories))) | ||
| 2026 | (chosen)) | ||
| 2027 | (setq chosen (yas/make-directory-maybe option (unless choose-instead-of-guess | ||
| 2028 | " main"))) | ||
| 2029 | (unless (or chosen | ||
| 2030 | choose-instead-of-guess) | ||
| 2031 | (if (y-or-n-p (format "Continue guessing for other active tables %s? " | ||
| 2032 | (mapcar #'(lambda (table-and-dirs) | ||
| 2033 | (yas/snippet-table-name (car table-and-dirs))) | ||
| 2034 | (rest guessed-directories)))) | ||
| 2035 | (setq chosen (some #'yas/make-directory-maybe | ||
| 2036 | (rest guessed-directories))))) | ||
| 2037 | (unless (or chosen | ||
| 2038 | choose-instead-of-guess) | ||
| 2039 | (when (y-or-n-p "Having trouble... use snippet root dir? ") | ||
| 2040 | (setq chosen (if (listp yas/root-directory) | ||
| 2041 | (first yas/root-directory) | ||
| 2042 | yas/root-directory)))) | ||
| 2043 | (if chosen | ||
| 2044 | (let ((default-directory chosen) | ||
| 2045 | (name (read-from-minibuffer "Enter a snippet name: "))) | ||
| 2046 | (find-file-other-window (concat name | ||
| 2047 | ".yasnippet")) | ||
| 2048 | (snippet-mode) | ||
| 2049 | (unless (and choose-instead-of-guess | ||
| 2050 | (not (y-or-n-p "Insert a snippet with useful headers? "))) | ||
| 2051 | (yas/expand-snippet (format | ||
| 2052 | "\ | ||
| 2053 | # -*- mode: snippet -*- | ||
| 2054 | # name: %s | ||
| 2055 | # key: $1${2: | ||
| 2056 | # binding: \"${3:keybinding}\"}${4: | ||
| 2057 | # expand-env: ((${5:some-var} ${6:some-value}))} | ||
| 2058 | # -- | ||
| 2059 | $0" name)))) | ||
| 2060 | (message "[yas] aborted snippet creation.")))) | ||
| 2061 | |||
| 2062 | (defun yas/find-snippets (&optional same-window ) | ||
| 2063 | "Look for user snippets in guessed current mode's directory. | ||
| 2064 | |||
| 2065 | Calls `find-file' interactively in the guessed directory. | ||
| 2066 | |||
| 2067 | With prefix arg SAME-WINDOW opens the buffer in the same window. | ||
| 2068 | |||
| 2069 | Because snippets can be loaded from many different locations, | ||
| 2070 | this has to guess the correct directory using | ||
| 2071 | `yas/guess-snippet-directories', which returns a list of | ||
| 2072 | options. | ||
| 2073 | |||
| 2074 | If any one of these exists, it is taken and `find-file' is called | ||
| 2075 | there, otherwise, proposes to create the first option returned by | ||
| 2076 | `yas/guess-snippet-directories'." | ||
| 2077 | (interactive "P") | ||
| 2078 | (let* ((guessed-directories (yas/guess-snippet-directories)) | ||
| 2079 | (chosen) | ||
| 2080 | (buffer)) | ||
| 2081 | (setq chosen (yas/make-directory-maybe (first guessed-directories) " main")) | ||
| 2082 | (unless chosen | ||
| 2083 | (if (y-or-n-p (format "Continue guessing for other active tables %s? " | ||
| 2084 | (mapcar #'(lambda (table-and-dirs) | ||
| 2085 | (yas/snippet-table-name (car table-and-dirs))) | ||
| 2086 | (rest guessed-directories)))) | ||
| 2087 | (setq chosen (some #'yas/make-directory-maybe | ||
| 2088 | (rest guessed-directories))))) | ||
| 2089 | (unless chosen | ||
| 2090 | (when (y-or-n-p "Having trouble... go to snippet root dir? ") | ||
| 2091 | (setq chosen (if (listp yas/root-directory) | ||
| 2092 | (first yas/root-directory) | ||
| 2093 | yas/root-directory)))) | ||
| 2094 | (if chosen | ||
| 2095 | (let ((default-directory chosen)) | ||
| 2096 | (setq buffer (call-interactively (if same-window | ||
| 2097 | 'find-file | ||
| 2098 | 'find-file-other-window))) | ||
| 2099 | (when buffer | ||
| 2100 | (save-excursion | ||
| 2101 | (set-buffer buffer) | ||
| 2102 | (when (eq major-mode 'fundamental-mode) | ||
| 2103 | (snippet-mode))))) | ||
| 2104 | (message "Could not guess snippet dir!")))) | ||
| 2105 | |||
| 2106 | (defun yas/compute-major-mode-and-parents (file &optional prompt-if-failed no-hierarchy-parents) | ||
| 2107 | (let* ((file-dir (and file | ||
| 2108 | (directory-file-name (or (locate-dominating-file file ".yas-make-groups") | ||
| 2109 | (directory-file-name (file-name-directory file)))))) | ||
| 2110 | (major-mode-name (and file-dir | ||
| 2111 | (file-name-nondirectory file-dir))) | ||
| 2112 | (parent-file-dir (and file-dir | ||
| 2113 | (directory-file-name (file-name-directory file-dir)))) | ||
| 2114 | (parent-mode-name (and parent-file-dir | ||
| 2115 | (not no-hierarchy-parents) | ||
| 2116 | (file-name-nondirectory parent-file-dir))) | ||
| 2117 | (major-mode-sym (or (and major-mode-name | ||
| 2118 | (intern major-mode-name)) | ||
| 2119 | (when prompt-if-failed | ||
| 2120 | (read-from-minibuffer | ||
| 2121 | "[yas] Cannot auto-detect major mode! Enter a major mode: ")))) | ||
| 2122 | (parent-mode-sym (and parent-mode-name | ||
| 2123 | (intern parent-mode-name))) | ||
| 2124 | (extra-parents-file-name (concat file-dir "/.yas-parents")) | ||
| 2125 | (more-parents (when (file-readable-p extra-parents-file-name) | ||
| 2126 | (mapcar #'intern | ||
| 2127 | (split-string | ||
| 2128 | (with-temp-buffer | ||
| 2129 | (insert-file-contents extra-parents-file-name) | ||
| 2130 | (buffer-substring-no-properties (point-min) | ||
| 2131 | (point-max)))))))) | ||
| 2132 | (when major-mode-sym | ||
| 2133 | (remove nil (append (list major-mode-sym parent-mode-sym) | ||
| 2134 | more-parents))))) | ||
| 2135 | |||
| 2136 | (defun yas/load-snippet-buffer (&optional kill) | ||
| 2137 | "Parse and load current buffer's snippet definition. | ||
| 2138 | |||
| 2139 | With optional prefix argument KILL quit the window and buffer." | ||
| 2140 | (interactive "P") | ||
| 2141 | (if buffer-file-name | ||
| 2142 | (let ((major-mode-and-parent (yas/compute-major-mode-and-parents buffer-file-name))) | ||
| 2143 | (if major-mode-and-parent | ||
| 2144 | (let* ((parsed (yas/parse-template buffer-file-name)) | ||
| 2145 | (name (and parsed | ||
| 2146 | (third parsed)))) | ||
| 2147 | (when name | ||
| 2148 | (let ((yas/better-guess-for-replacements t)) | ||
| 2149 | (yas/define-snippets (car major-mode-and-parent) | ||
| 2150 | (list parsed) | ||
| 2151 | (cdr major-mode-and-parent))) | ||
| 2152 | (when (and (buffer-modified-p) | ||
| 2153 | (y-or-n-p "Save snippet? ")) | ||
| 2154 | (save-buffer)) | ||
| 2155 | (if kill | ||
| 2156 | (quit-window kill) | ||
| 2157 | (message "[yas] Snippet \"%s\" loaded for %s." | ||
| 2158 | name | ||
| 2159 | (car major-mode-and-parent))))) | ||
| 2160 | (message "[yas] Cannot load snippet for unknown major mode"))) | ||
| 2161 | (message "Save the buffer as a file first!"))) | ||
| 2162 | |||
| 2163 | (defun yas/tryout-snippet (&optional debug) | ||
| 2164 | "Test current buffers's snippet template in other buffer." | ||
| 2165 | (interactive "P") | ||
| 2166 | (let* ((major-mode-and-parent (yas/compute-major-mode-and-parents buffer-file-name)) | ||
| 2167 | (parsed (yas/parse-template)) | ||
| 2168 | (test-mode (or (and (car major-mode-and-parent) | ||
| 2169 | (fboundp (car major-mode-and-parent)) | ||
| 2170 | (car major-mode-and-parent)) | ||
| 2171 | (intern (read-from-minibuffer "[yas] please input a mode: ")))) | ||
| 2172 | (template (and parsed | ||
| 2173 | (fboundp test-mode) | ||
| 2174 | (yas/make-template (second parsed) | ||
| 2175 | (third parsed) | ||
| 2176 | nil | ||
| 2177 | (sixth parsed) | ||
| 2178 | nil | ||
| 2179 | nil)))) | ||
| 2180 | (cond (template | ||
| 2181 | (let ((buffer-name (format "*YAS TEST: %s*" (yas/template-name template)))) | ||
| 2182 | (set-buffer (switch-to-buffer buffer-name)) | ||
| 2183 | (erase-buffer) | ||
| 2184 | (setq buffer-undo-list nil) | ||
| 2185 | (funcall test-mode) | ||
| 2186 | (yas/expand-snippet (yas/template-content template) | ||
| 2187 | (point-min) | ||
| 2188 | (point-max) | ||
| 2189 | (yas/template-expand-env template)) | ||
| 2190 | (when debug | ||
| 2191 | (add-hook 'post-command-hook 'yas/debug-snippet-vars 't 'local)))) | ||
| 2192 | (t | ||
| 2193 | (message "[yas] Cannot test snippet for unknown major mode"))))) | ||
| 2194 | |||
| 2195 | |||
| 2196 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 2197 | ;;; User convenience functions, for using in snippet definitions | ||
| 2198 | |||
| 2199 | (defvar yas/modified-p nil | ||
| 2200 | "Non-nil if field has been modified by user or transformation.") | ||
| 2201 | |||
| 2202 | (defvar yas/moving-away-p nil | ||
| 2203 | "Non-nil if user is about to exit field.") | ||
| 2204 | |||
| 2205 | (defvar yas/text nil | ||
| 2206 | "Contains current field text.") | ||
| 2207 | |||
| 2208 | (defun yas/substr (str pattern &optional subexp) | ||
| 2209 | "Search PATTERN in STR and return SUBEXPth match. | ||
| 2210 | |||
| 2211 | If found, the content of subexp group SUBEXP (default 0) is | ||
| 2212 | returned, or else the original STR will be returned." | ||
| 2213 | (let ((grp (or subexp 0))) | ||
| 2214 | (save-match-data | ||
| 2215 | (if (string-match pattern str) | ||
| 2216 | (match-string-no-properties grp str) | ||
| 2217 | str)))) | ||
| 2218 | |||
| 2219 | (defun yas/choose-value (possibilities) | ||
| 2220 | "Prompt for a string in the list POSSIBILITIES and return it." | ||
| 2221 | (unless (or yas/moving-away-p | ||
| 2222 | yas/modified-p) | ||
| 2223 | (some #'(lambda (fn) | ||
| 2224 | (funcall fn "Choose: " possibilities)) | ||
| 2225 | yas/prompt-functions))) | ||
| 2226 | |||
| 2227 | (defun yas/key-to-value (alist) | ||
| 2228 | "Prompt for a string in the list POSSIBILITIES and return it." | ||
| 2229 | (unless (or yas/moving-away-p | ||
| 2230 | yas/modified-p) | ||
| 2231 | (let ((key (read-key-sequence ""))) | ||
| 2232 | (when (stringp key) | ||
| 2233 | (or (cdr (find key alist :key #'car :test #'string=)) | ||
| 2234 | key))))) | ||
| 2235 | |||
| 2236 | (defun yas/throw (text) | ||
| 2237 | "Throw a yas/exception with TEXT as the reason." | ||
| 2238 | (throw 'yas/exception (cons 'yas/exception text))) | ||
| 2239 | |||
| 2240 | (defun yas/verify-value (possibilities) | ||
| 2241 | "Verify that the current field value is in POSSIBILITIES | ||
| 2242 | |||
| 2243 | Otherwise throw exception." | ||
| 2244 | (when (and yas/moving-away-p (notany #'(lambda (pos) (string= pos yas/text)) possibilities)) | ||
| 2245 | (yas/throw (format "[yas] field only allows %s" possibilities)))) | ||
| 2246 | |||
| 2247 | (defun yas/field-value (number) | ||
| 2248 | (let* ((snippet (car (yas/snippets-at-point))) | ||
| 2249 | (field (and snippet | ||
| 2250 | (yas/snippet-find-field snippet number)))) | ||
| 2251 | (when field | ||
| 2252 | (yas/field-text-for-display field)))) | ||
| 2253 | |||
| 2254 | (defun yas/default-from-field (number) | ||
| 2255 | (unless yas/modified-p | ||
| 2256 | (yas/field-value number))) | ||
| 2257 | |||
| 2258 | (defun yas/inside-string () | ||
| 2259 | (equal 'font-lock-string-face (get-char-property (1- (point)) 'face))) | ||
| 2260 | |||
| 2261 | |||
| 2262 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 2263 | ;;; Snippet expansion and field management | ||
| 2264 | |||
| 2265 | (defvar yas/active-field-overlay nil | ||
| 2266 | "Overlays the currently active field.") | ||
| 2267 | |||
| 2268 | (defvar yas/field-protection-overlays nil | ||
| 2269 | "Two overlays protect the current active field ") | ||
| 2270 | |||
| 2271 | (defconst yas/prefix nil | ||
| 2272 | "A prefix argument for expansion direct from keybindings") | ||
| 2273 | |||
| 2274 | (defvar yas/deleted-text nil | ||
| 2275 | "The text deleted in the last snippet expansion.") | ||
| 2276 | |||
| 2277 | (defvar yas/selected-text nil | ||
| 2278 | "The selected region deleted on the last snippet expansion.") | ||
| 2279 | |||
| 2280 | (defvar yas/start-column nil | ||
| 2281 | "The column where the snippet expansion started.") | ||
| 2282 | |||
| 2283 | (make-variable-buffer-local 'yas/active-field-overlay) | ||
| 2284 | (make-variable-buffer-local 'yas/field-protection-overlays) | ||
| 2285 | (make-variable-buffer-local 'yas/deleted-text) | ||
| 2286 | |||
| 2287 | (defstruct (yas/snippet (:constructor yas/make-snippet ())) | ||
| 2288 | "A snippet. | ||
| 2289 | |||
| 2290 | ..." | ||
| 2291 | (fields '()) | ||
| 2292 | (exit nil) | ||
| 2293 | (id (yas/snippet-next-id) :read-only t) | ||
| 2294 | (control-overlay nil) | ||
| 2295 | active-field | ||
| 2296 | ;; stacked expansion: the `previous-active-field' slot saves the | ||
| 2297 | ;; active field where the child expansion took place | ||
| 2298 | previous-active-field | ||
| 2299 | force-exit) | ||
| 2300 | |||
| 2301 | (defstruct (yas/field (:constructor yas/make-field (number start end parent-field))) | ||
| 2302 | "A field." | ||
| 2303 | number | ||
| 2304 | start end | ||
| 2305 | parent-field | ||
| 2306 | (mirrors '()) | ||
| 2307 | (transform nil) | ||
| 2308 | (modified-p nil) | ||
| 2309 | next) | ||
| 2310 | |||
| 2311 | (defstruct (yas/mirror (:constructor yas/make-mirror (start end transform))) | ||
| 2312 | "A mirror." | ||
| 2313 | start end | ||
| 2314 | (transform nil) | ||
| 2315 | next) | ||
| 2316 | |||
| 2317 | (defstruct (yas/exit (:constructor yas/make-exit (marker))) | ||
| 2318 | marker | ||
| 2319 | next) | ||
| 2320 | |||
| 2321 | (defun yas/apply-transform (field-or-mirror field) | ||
| 2322 | "Calculate the value of the field/mirror. If there's a transform | ||
| 2323 | for this field, apply it. Otherwise, returned nil." | ||
| 2324 | (let* ((yas/text (yas/field-text-for-display field)) | ||
| 2325 | (text yas/text) | ||
| 2326 | (yas/modified-p (yas/field-modified-p field)) | ||
| 2327 | (yas/moving-away-p nil) | ||
| 2328 | (transform (if (yas/mirror-p field-or-mirror) | ||
| 2329 | (yas/mirror-transform field-or-mirror) | ||
| 2330 | (yas/field-transform field-or-mirror))) | ||
| 2331 | (start-point (if (yas/mirror-p field-or-mirror) | ||
| 2332 | (yas/mirror-start field-or-mirror) | ||
| 2333 | (yas/field-start field-or-mirror))) | ||
| 2334 | (transformed (and transform | ||
| 2335 | (save-excursion | ||
| 2336 | (goto-char start-point) | ||
| 2337 | (yas/read-and-eval-string transform))))) | ||
| 2338 | transformed)) | ||
| 2339 | |||
| 2340 | (defsubst yas/replace-all (from to &optional text) | ||
| 2341 | "Replace all occurance from FROM to TO. | ||
| 2342 | |||
| 2343 | With optional string TEXT do it in that string." | ||
| 2344 | (if text | ||
| 2345 | (replace-regexp-in-string (regexp-quote from) to text t t) | ||
| 2346 | (goto-char (point-min)) | ||
| 2347 | (while (search-forward from nil t) | ||
| 2348 | (replace-match to t t text)))) | ||
| 2349 | |||
| 2350 | (defun yas/snippet-find-field (snippet number) | ||
| 2351 | (find-if #'(lambda (field) | ||
| 2352 | (eq number (yas/field-number field))) | ||
| 2353 | (yas/snippet-fields snippet))) | ||
| 2354 | |||
| 2355 | (defun yas/snippet-sort-fields (snippet) | ||
| 2356 | "Sort the fields of SNIPPET in navigation order." | ||
| 2357 | (setf (yas/snippet-fields snippet) | ||
| 2358 | (sort (yas/snippet-fields snippet) | ||
| 2359 | '(lambda (field1 field2) | ||
| 2360 | (yas/snippet-field-compare field1 field2))))) | ||
| 2361 | |||
| 2362 | (defun yas/snippet-field-compare (field1 field2) | ||
| 2363 | "Compare two fields. The field with a number is sorted first. | ||
| 2364 | If they both have a number, compare through the number. If neither | ||
| 2365 | have, compare through the field's start point" | ||
| 2366 | (let ((n1 (yas/field-number field1)) | ||
| 2367 | (n2 (yas/field-number field2))) | ||
| 2368 | (if n1 | ||
| 2369 | (if n2 | ||
| 2370 | (< n1 n2) | ||
| 2371 | t) | ||
| 2372 | (if n2 | ||
| 2373 | nil | ||
| 2374 | (< (yas/field-start field1) | ||
| 2375 | (yas/field-start field2)))))) | ||
| 2376 | |||
| 2377 | (defun yas/field-probably-deleted-p (snippet field) | ||
| 2378 | "Guess if SNIPPET's FIELD should be skipped." | ||
| 2379 | (and (zerop (- (yas/field-start field) (yas/field-end field))) | ||
| 2380 | (or (yas/field-parent-field field) | ||
| 2381 | (and (eq field (car (last (yas/snippet-fields snippet)))) | ||
| 2382 | (= (yas/field-start field) (overlay-end (yas/snippet-control-overlay snippet))))))) | ||
| 2383 | |||
| 2384 | (defun yas/snippets-at-point (&optional all-snippets) | ||
| 2385 | "Return a sorted list of snippets at point, most recently | ||
| 2386 | inserted first." | ||
| 2387 | (sort | ||
| 2388 | (remove nil (remove-duplicates (mapcar #'(lambda (ov) | ||
| 2389 | (overlay-get ov 'yas/snippet)) | ||
| 2390 | (if all-snippets | ||
| 2391 | (overlays-in (point-min) (point-max)) | ||
| 2392 | (overlays-at (point)))))) | ||
| 2393 | #'(lambda (s1 s2) | ||
| 2394 | (<= (yas/snippet-id s2) (yas/snippet-id s1))))) | ||
| 2395 | |||
| 2396 | (defun yas/next-field-or-maybe-expand () | ||
| 2397 | "Try to expand a snippet at a key before point, otherwise | ||
| 2398 | delegate to `yas/next-field'." | ||
| 2399 | (interactive) | ||
| 2400 | (if yas/triggers-in-field | ||
| 2401 | (let ((yas/fallback-behavior 'return-nil) | ||
| 2402 | (active-field (overlay-get yas/active-field-overlay 'yas/field))) | ||
| 2403 | (when active-field | ||
| 2404 | (unless (yas/expand-1 active-field) | ||
| 2405 | (yas/next-field)))) | ||
| 2406 | (yas/next-field))) | ||
| 2407 | |||
| 2408 | (defun yas/next-field (&optional arg) | ||
| 2409 | "Navigate to next field. If there's none, exit the snippet." | ||
| 2410 | (interactive) | ||
| 2411 | (let* ((arg (or arg | ||
| 2412 | 1)) | ||
| 2413 | (snippet (first (yas/snippets-at-point))) | ||
| 2414 | (active-field (overlay-get yas/active-field-overlay 'yas/field)) | ||
| 2415 | (live-fields (remove-if #'(lambda (field) | ||
| 2416 | (and (not (eq field active-field)) | ||
| 2417 | (yas/field-probably-deleted-p snippet field))) | ||
| 2418 | (yas/snippet-fields snippet))) | ||
| 2419 | (active-field-pos (position active-field live-fields)) | ||
| 2420 | (target-pos (and active-field-pos (+ arg active-field-pos))) | ||
| 2421 | (target-field (nth target-pos live-fields))) | ||
| 2422 | ;; First check if we're moving out of a field with a transform | ||
| 2423 | ;; | ||
| 2424 | (when (and active-field | ||
| 2425 | (yas/field-transform active-field)) | ||
| 2426 | (let* ((yas/moving-away-p t) | ||
| 2427 | (yas/text (yas/field-text-for-display active-field)) | ||
| 2428 | (text yas/text) | ||
| 2429 | (yas/modified-p (yas/field-modified-p active-field))) | ||
| 2430 | ;; primary field transform: exit call to field-transform | ||
| 2431 | (yas/read-and-eval-string (yas/field-transform active-field)))) | ||
| 2432 | ;; Now actually move... | ||
| 2433 | (cond ((>= target-pos (length live-fields)) | ||
| 2434 | (yas/exit-snippet snippet)) | ||
| 2435 | (target-field | ||
| 2436 | (yas/move-to-field snippet target-field)) | ||
| 2437 | (t | ||
| 2438 | nil)))) | ||
| 2439 | |||
| 2440 | (defun yas/place-overlays (snippet field) | ||
| 2441 | "Correctly place overlays for SNIPPET's FIELD" | ||
| 2442 | (yas/make-move-field-protection-overlays snippet field) | ||
| 2443 | (yas/make-move-active-field-overlay snippet field)) | ||
| 2444 | |||
| 2445 | (defun yas/move-to-field (snippet field) | ||
| 2446 | "Update SNIPPET to move to field FIELD. | ||
| 2447 | |||
| 2448 | Also create some protection overlays" | ||
| 2449 | (goto-char (yas/field-start field)) | ||
| 2450 | (setf (yas/snippet-active-field snippet) field) | ||
| 2451 | (yas/place-overlays snippet field) | ||
| 2452 | (overlay-put yas/active-field-overlay 'yas/field field) | ||
| 2453 | ;; primary field transform: first call to snippet transform | ||
| 2454 | (unless (yas/field-modified-p field) | ||
| 2455 | (if (yas/field-update-display field snippet) | ||
| 2456 | (let ((inhibit-modification-hooks t)) | ||
| 2457 | (yas/update-mirrors snippet)) | ||
| 2458 | (setf (yas/field-modified-p field) nil)))) | ||
| 2459 | |||
| 2460 | (defun yas/prev-field () | ||
| 2461 | "Navigate to prev field. If there's none, exit the snippet." | ||
| 2462 | (interactive) | ||
| 2463 | (yas/next-field -1)) | ||
| 2464 | |||
| 2465 | (defun yas/abort-snippet (&optional snippet) | ||
| 2466 | (interactive) | ||
| 2467 | (let ((snippet (or snippet | ||
| 2468 | (car (yas/snippets-at-point))))) | ||
| 2469 | (when snippet | ||
| 2470 | (setf (yas/snippet-force-exit snippet) t)))) | ||
| 2471 | |||
| 2472 | (defun yas/exit-snippet (snippet) | ||
| 2473 | "Goto exit-marker of SNIPPET." | ||
| 2474 | (interactive) | ||
| 2475 | (setf (yas/snippet-force-exit snippet) t) | ||
| 2476 | (goto-char (if (yas/snippet-exit snippet) | ||
| 2477 | (yas/exit-marker (yas/snippet-exit snippet)) | ||
| 2478 | (overlay-end (yas/snippet-control-overlay snippet))))) | ||
| 2479 | |||
| 2480 | (defun yas/exit-all-snippets () | ||
| 2481 | "Exit all snippets." | ||
| 2482 | (interactive) | ||
| 2483 | (mapc #'(lambda (snippet) | ||
| 2484 | (yas/exit-snippet snippet) | ||
| 2485 | (yas/check-commit-snippet)) | ||
| 2486 | (yas/snippets-at-point))) | ||
| 2487 | |||
| 2488 | |||
| 2489 | ;;; Apropos markers-to-points: | ||
| 2490 | ;;; | ||
| 2491 | ;;; This was found useful for performance reasons, so that an | ||
| 2492 | ;;; excessive number of live markers aren't kept around in the | ||
| 2493 | ;;; `buffer-undo-list'. However, in `markers-to-points', the | ||
| 2494 | ;;; set-to-nil markers can't simply be discarded and replaced with | ||
| 2495 | ;;; fresh ones in `points-to-markers'. The original marker that was | ||
| 2496 | ;;; just set to nil has to be reused. | ||
| 2497 | ;;; | ||
| 2498 | ;;; This shouldn't bring horrible problems with undo/redo, but it | ||
| 2499 | ;;; you never know | ||
| 2500 | ;;; | ||
| 2501 | |||
| 2502 | (defun yas/markers-to-points (snippet) | ||
| 2503 | "Convert all markers in SNIPPET to a cons (POINT . MARKER) | ||
| 2504 | where POINT is the original position of the marker and MARKER is | ||
| 2505 | the original marker object with the position set to nil." | ||
| 2506 | (dolist (field (yas/snippet-fields snippet)) | ||
| 2507 | (let ((start (marker-position (yas/field-start field))) | ||
| 2508 | (end (marker-position (yas/field-end field)))) | ||
| 2509 | (set-marker (yas/field-start field) nil) | ||
| 2510 | (set-marker (yas/field-end field) nil) | ||
| 2511 | (setf (yas/field-start field) (cons start (yas/field-start field))) | ||
| 2512 | (setf (yas/field-end field) (cons end (yas/field-end field)))) | ||
| 2513 | (dolist (mirror (yas/field-mirrors field)) | ||
| 2514 | (let ((start (marker-position (yas/mirror-start mirror))) | ||
| 2515 | (end (marker-position (yas/mirror-end mirror)))) | ||
| 2516 | (set-marker (yas/mirror-start mirror) nil) | ||
| 2517 | (set-marker (yas/mirror-end mirror) nil) | ||
| 2518 | (setf (yas/mirror-start mirror) (cons start (yas/mirror-start mirror))) | ||
| 2519 | (setf (yas/mirror-end mirror) (cons end (yas/mirror-end mirror)))))) | ||
| 2520 | (let ((snippet-exit (yas/snippet-exit snippet))) | ||
| 2521 | (when snippet-exit | ||
| 2522 | (let ((exit (marker-position (yas/exit-marker snippet-exit)))) | ||
| 2523 | (set-marker (yas/exit-marker snippet-exit) nil) | ||
| 2524 | (setf (yas/exit-marker snippet-exit) (cons exit (yas/exit-marker snippet-exit))))))) | ||
| 2525 | |||
| 2526 | (defun yas/points-to-markers (snippet) | ||
| 2527 | "Convert all cons (POINT . MARKER) in SNIPPET to markers. This | ||
| 2528 | is done by setting MARKER to POINT with `set-marker'." | ||
| 2529 | (dolist (field (yas/snippet-fields snippet)) | ||
| 2530 | (setf (yas/field-start field) (set-marker (cdr (yas/field-start field)) | ||
| 2531 | (car (yas/field-start field)))) | ||
| 2532 | (setf (yas/field-end field) (set-marker (cdr (yas/field-end field)) | ||
| 2533 | (car (yas/field-end field)))) | ||
| 2534 | (dolist (mirror (yas/field-mirrors field)) | ||
| 2535 | (setf (yas/mirror-start mirror) (set-marker (cdr (yas/mirror-start mirror)) | ||
| 2536 | (car (yas/mirror-start mirror)))) | ||
| 2537 | (setf (yas/mirror-end mirror) (set-marker (cdr (yas/mirror-end mirror)) | ||
| 2538 | (car (yas/mirror-end mirror)))))) | ||
| 2539 | (let ((snippet-exit (yas/snippet-exit snippet))) | ||
| 2540 | (when snippet-exit | ||
| 2541 | (setf (yas/exit-marker snippet-exit) (set-marker (cdr (yas/exit-marker snippet-exit)) | ||
| 2542 | (car (yas/exit-marker snippet-exit))))))) | ||
| 2543 | |||
| 2544 | (defun yas/commit-snippet (snippet &optional no-hooks) | ||
| 2545 | "Commit SNIPPET, but leave point as it is. This renders the | ||
| 2546 | snippet as ordinary text. | ||
| 2547 | |||
| 2548 | Return a buffer position where the point should be placed if | ||
| 2549 | exiting the snippet. | ||
| 2550 | |||
| 2551 | NO-HOOKS means don't run the `yas/after-exit-snippet-hook' hooks." | ||
| 2552 | |||
| 2553 | (let ((control-overlay (yas/snippet-control-overlay snippet)) | ||
| 2554 | yas/snippet-beg | ||
| 2555 | yas/snippet-end) | ||
| 2556 | ;; | ||
| 2557 | ;; Save the end of the moribund snippet in case we need to revive it | ||
| 2558 | ;; its original expansion. | ||
| 2559 | ;; | ||
| 2560 | (when (and control-overlay | ||
| 2561 | (overlay-buffer control-overlay)) | ||
| 2562 | (setq yas/snippet-beg (overlay-start control-overlay)) | ||
| 2563 | (setq yas/snippet-end (overlay-end control-overlay)) | ||
| 2564 | (delete-overlay control-overlay)) | ||
| 2565 | |||
| 2566 | (let ((inhibit-modification-hooks t)) | ||
| 2567 | (when yas/active-field-overlay | ||
| 2568 | (delete-overlay yas/active-field-overlay)) | ||
| 2569 | (when yas/field-protection-overlays | ||
| 2570 | (mapc #'delete-overlay yas/field-protection-overlays))) | ||
| 2571 | |||
| 2572 | ;; stacked expansion: if the original expansion took place from a | ||
| 2573 | ;; field, make sure we advance it here at least to | ||
| 2574 | ;; `yas/snippet-end'... | ||
| 2575 | ;; | ||
| 2576 | (let ((previous-field (yas/snippet-previous-active-field snippet))) | ||
| 2577 | (when (and yas/snippet-end previous-field) | ||
| 2578 | (yas/advance-end-maybe previous-field yas/snippet-end))) | ||
| 2579 | |||
| 2580 | ;; Convert all markers to points, | ||
| 2581 | ;; | ||
| 2582 | (yas/markers-to-points snippet) | ||
| 2583 | |||
| 2584 | ;; Take care of snippet revival | ||
| 2585 | ;; | ||
| 2586 | (if yas/snippet-revival | ||
| 2587 | (push `(apply yas/snippet-revive ,yas/snippet-beg ,yas/snippet-end ,snippet) | ||
| 2588 | buffer-undo-list) | ||
| 2589 | ;; Dismember the snippet... this is useful if we get called | ||
| 2590 | ;; again from `yas/take-care-of-redo'.... | ||
| 2591 | (setf (yas/snippet-fields snippet) nil)) | ||
| 2592 | |||
| 2593 | ;; XXX: `yas/after-exit-snippet-hook' should be run with | ||
| 2594 | ;; `yas/snippet-beg' and `yas/snippet-end' bound. That might not | ||
| 2595 | ;; be the case if the main overlay had somehow already | ||
| 2596 | ;; disappeared, which sometimes happens when the snippet's messed | ||
| 2597 | ;; up... | ||
| 2598 | ;; | ||
| 2599 | (unless no-hooks (run-hooks 'yas/after-exit-snippet-hook))) | ||
| 2600 | |||
| 2601 | (message "[yas] snippet exited.")) | ||
| 2602 | |||
| 2603 | (defun yas/check-commit-snippet () | ||
| 2604 | "Checks if point exited the currently active field of the | ||
| 2605 | snippet, if so cleans up the whole snippet up." | ||
| 2606 | (let* ((snippets (yas/snippets-at-point 'all-snippets)) | ||
| 2607 | (snippets-left snippets)) | ||
| 2608 | (dolist (snippet snippets) | ||
| 2609 | (let ((active-field (yas/snippet-active-field snippet))) | ||
| 2610 | (cond ((or (prog1 (yas/snippet-force-exit snippet) | ||
| 2611 | (setf (yas/snippet-force-exit snippet) nil)) | ||
| 2612 | (not (and active-field (yas/field-contains-point-p active-field)))) | ||
| 2613 | (setq snippets-left (delete snippet snippets-left)) | ||
| 2614 | (yas/commit-snippet snippet snippets-left)) | ||
| 2615 | ((and active-field | ||
| 2616 | (or (not yas/active-field-overlay) | ||
| 2617 | (not (overlay-buffer yas/active-field-overlay)))) | ||
| 2618 | ;; | ||
| 2619 | ;; stacked expansion: this case is mainly for recent | ||
| 2620 | ;; snippet exits that place us back int the field of | ||
| 2621 | ;; another snippet | ||
| 2622 | ;; | ||
| 2623 | (save-excursion | ||
| 2624 | (yas/move-to-field snippet active-field) | ||
| 2625 | (yas/update-mirrors snippet))) | ||
| 2626 | (t | ||
| 2627 | nil)))) | ||
| 2628 | (unless snippets-left | ||
| 2629 | (remove-hook 'post-command-hook 'yas/post-command-handler 'local) | ||
| 2630 | (remove-hook 'pre-command-hook 'yas/pre-command-handler 'local)))) | ||
| 2631 | |||
| 2632 | (defun yas/field-contains-point-p (field &optional point) | ||
| 2633 | (let ((point (or point | ||
| 2634 | (point)))) | ||
| 2635 | (and (>= point (yas/field-start field)) | ||
| 2636 | (<= point (yas/field-end field))))) | ||
| 2637 | |||
| 2638 | (defun yas/field-text-for-display (field) | ||
| 2639 | "Return the propertized display text for field FIELD. " | ||
| 2640 | (buffer-substring (yas/field-start field) (yas/field-end field))) | ||
| 2641 | |||
| 2642 | (defun yas/undo-in-progress () | ||
| 2643 | "True if some kind of undo is in progress" | ||
| 2644 | (or undo-in-progress | ||
| 2645 | (eq this-command 'undo) | ||
| 2646 | (eq this-command 'redo))) | ||
| 2647 | |||
| 2648 | (defun yas/make-control-overlay (snippet start end) | ||
| 2649 | "Creates the control overlay that surrounds the snippet and | ||
| 2650 | holds the keymap." | ||
| 2651 | (let ((overlay (make-overlay start | ||
| 2652 | end | ||
| 2653 | nil | ||
| 2654 | nil | ||
| 2655 | t))) | ||
| 2656 | (overlay-put overlay 'keymap yas/keymap) | ||
| 2657 | (overlay-put overlay 'yas/snippet snippet) | ||
| 2658 | overlay)) | ||
| 2659 | |||
| 2660 | (defun yas/skip-and-clear-or-delete-char (&optional field) | ||
| 2661 | "Clears unmodified field if at field start, skips to next tab. | ||
| 2662 | |||
| 2663 | Otherwise deletes a character normally by calling `delete-char'." | ||
| 2664 | (interactive) | ||
| 2665 | (let ((field (or field | ||
| 2666 | (and yas/active-field-overlay | ||
| 2667 | (overlay-buffer yas/active-field-overlay) | ||
| 2668 | (overlay-get yas/active-field-overlay 'yas/field))))) | ||
| 2669 | (cond ((and field | ||
| 2670 | (not (yas/field-modified-p field)) | ||
| 2671 | (eq (point) (marker-position (yas/field-start field)))) | ||
| 2672 | (yas/skip-and-clear field) | ||
| 2673 | (yas/next-field 1)) | ||
| 2674 | (t | ||
| 2675 | (call-interactively 'delete-char))))) | ||
| 2676 | |||
| 2677 | (defun yas/skip-and-clear (field) | ||
| 2678 | "Deletes the region of FIELD and sets it modified state to t" | ||
| 2679 | (setf (yas/field-modified-p field) t) | ||
| 2680 | (delete-region (yas/field-start field) (yas/field-end field))) | ||
| 2681 | |||
| 2682 | (defun yas/make-move-active-field-overlay (snippet field) | ||
| 2683 | "Place the active field overlay in SNIPPET's FIELD. | ||
| 2684 | |||
| 2685 | Move the overlay, or create it if it does not exit." | ||
| 2686 | (if (and yas/active-field-overlay | ||
| 2687 | (overlay-buffer yas/active-field-overlay)) | ||
| 2688 | (move-overlay yas/active-field-overlay | ||
| 2689 | (yas/field-start field) | ||
| 2690 | (yas/field-end field)) | ||
| 2691 | (setq yas/active-field-overlay | ||
| 2692 | (make-overlay (yas/field-start field) | ||
| 2693 | (yas/field-end field) | ||
| 2694 | nil nil t)) | ||
| 2695 | (overlay-put yas/active-field-overlay 'priority 100) | ||
| 2696 | (overlay-put yas/active-field-overlay 'face 'yas/field-highlight-face) | ||
| 2697 | (overlay-put yas/active-field-overlay 'yas/snippet snippet) | ||
| 2698 | (overlay-put yas/active-field-overlay 'modification-hooks '(yas/on-field-overlay-modification)) | ||
| 2699 | (overlay-put yas/active-field-overlay 'insert-in-front-hooks | ||
| 2700 | '(yas/on-field-overlay-modification)) | ||
| 2701 | (overlay-put yas/active-field-overlay 'insert-behind-hooks | ||
| 2702 | '(yas/on-field-overlay-modification)))) | ||
| 2703 | |||
| 2704 | (defun yas/on-field-overlay-modification (overlay after? beg end &optional length) | ||
| 2705 | "Clears the field and updates mirrors, conditionally. | ||
| 2706 | |||
| 2707 | Only clears the field if it hasn't been modified and it point it | ||
| 2708 | at field start. This hook doesn't do anything if an undo is in | ||
| 2709 | progress." | ||
| 2710 | (unless (yas/undo-in-progress) | ||
| 2711 | (let ((field (overlay-get yas/active-field-overlay 'yas/field))) | ||
| 2712 | (cond (after? | ||
| 2713 | (yas/advance-end-maybe field (overlay-end overlay)) | ||
| 2714 | ;;; primary field transform: normal calls to expression | ||
| 2715 | (let ((saved-point (point))) | ||
| 2716 | (yas/field-update-display field (car (yas/snippets-at-point))) | ||
| 2717 | (goto-char saved-point)) | ||
| 2718 | (yas/update-mirrors (car (yas/snippets-at-point)))) | ||
| 2719 | (field | ||
| 2720 | (when (and (not after?) | ||
| 2721 | (not (yas/field-modified-p field)) | ||
| 2722 | (eq (point) (if (markerp (yas/field-start field)) | ||
| 2723 | (marker-position (yas/field-start field)) | ||
| 2724 | (yas/field-start field)))) | ||
| 2725 | (yas/skip-and-clear field)) | ||
| 2726 | (setf (yas/field-modified-p field) t)))))) | ||
| 2727 | |||
| 2728 | ;;; Apropos protection overlays: | ||
| 2729 | ;;; | ||
| 2730 | ;;; These exist for nasty users who will try to delete parts of the | ||
| 2731 | ;;; snippet outside the active field. Actual protection happens in | ||
| 2732 | ;;; `yas/on-protection-overlay-modification'. | ||
| 2733 | ;;; | ||
| 2734 | ;;; Currently this signals an error which inhibits the command. For | ||
| 2735 | ;;; commands that move point (like `kill-line'), point is restored in | ||
| 2736 | ;;; the `yas/post-command-handler' using a global | ||
| 2737 | ;;; `yas/protection-violation' variable. | ||
| 2738 | ;;; | ||
| 2739 | ;;; Alternatively, I've experimented with an implementation that | ||
| 2740 | ;;; commits the snippet before actually calling `this-command' | ||
| 2741 | ;;; interactively, and then signals an eror, which is ignored. but | ||
| 2742 | ;;; blocks all other million modification hooks. This presented some | ||
| 2743 | ;;; problems with stacked expansion. | ||
| 2744 | ;;; | ||
| 2745 | |||
| 2746 | (defun yas/make-move-field-protection-overlays (snippet field) | ||
| 2747 | "Place protection overlays surrounding SNIPPET's FIELD. | ||
| 2748 | |||
| 2749 | Move the overlays, or create them if they do not exit." | ||
| 2750 | (let ((start (yas/field-start field)) | ||
| 2751 | (end (yas/field-end field))) | ||
| 2752 | ;; First check if the (1+ end) is contained in the buffer, | ||
| 2753 | ;; otherwise we'll have to do a bit of cheating and silently | ||
| 2754 | ;; insert a newline. the `(1+ (buffer-size))' should prevent this | ||
| 2755 | ;; when using stacked expansion | ||
| 2756 | ;; | ||
| 2757 | (when (< (buffer-size) end) | ||
| 2758 | (save-excursion | ||
| 2759 | (let ((inhibit-modification-hooks t)) | ||
| 2760 | (goto-char (point-max)) | ||
| 2761 | (newline)))) | ||
| 2762 | ;; go on to normal overlay creation/moving | ||
| 2763 | ;; | ||
| 2764 | (cond ((and yas/field-protection-overlays | ||
| 2765 | (every #'overlay-buffer yas/field-protection-overlays)) | ||
| 2766 | (move-overlay (first yas/field-protection-overlays) (1- start) start) | ||
| 2767 | (move-overlay (second yas/field-protection-overlays) end (1+ end))) | ||
| 2768 | (t | ||
| 2769 | (setq yas/field-protection-overlays | ||
| 2770 | (list (make-overlay (1- start) start nil t nil) | ||
| 2771 | (make-overlay end (1+ end) nil t nil))) | ||
| 2772 | (dolist (ov yas/field-protection-overlays) | ||
| 2773 | (overlay-put ov 'face 'yas/field-debug-face) | ||
| 2774 | (overlay-put ov 'yas/snippet snippet) | ||
| 2775 | ;; (overlay-put ov 'evaporate t) | ||
| 2776 | (overlay-put ov 'modification-hooks '(yas/on-protection-overlay-modification))))))) | ||
| 2777 | |||
| 2778 | (defvar yas/protection-violation nil | ||
| 2779 | "When non-nil, signals attempts to erronesly exit or modify the snippet. | ||
| 2780 | |||
| 2781 | Functions in the `post-command-hook', for example | ||
| 2782 | `yas/post-command-handler' can check it and reset its value to | ||
| 2783 | nil. The variables value is the point where the violation | ||
| 2784 | originated") | ||
| 2785 | |||
| 2786 | (defun yas/on-protection-overlay-modification (overlay after? beg end &optional length) | ||
| 2787 | "Signals a snippet violation, then issues error. | ||
| 2788 | |||
| 2789 | The error should be ignored in `debug-ignored-errors'" | ||
| 2790 | (cond ((not (or after? | ||
| 2791 | (yas/undo-in-progress))) | ||
| 2792 | (setq yas/protection-violation (point)) | ||
| 2793 | (error "Exit the snippet first!")))) | ||
| 2794 | |||
| 2795 | (add-to-list 'debug-ignored-errors "^Exit the snippet first!$") | ||
| 2796 | |||
| 2797 | |||
| 2798 | ;;; Apropos stacked expansion: | ||
| 2799 | ;;; | ||
| 2800 | ;;; the parent snippet does not run its fields modification hooks | ||
| 2801 | ;;; (`yas/on-field-overlay-modification' and | ||
| 2802 | ;;; `yas/on-protection-overlay-modification') while the child snippet | ||
| 2803 | ;;; is active. This means, among other things, that the mirrors of the | ||
| 2804 | ;;; parent snippet are not updated, this only happening when one exits | ||
| 2805 | ;;; the child snippet. | ||
| 2806 | ;;; | ||
| 2807 | ;;; Unfortunately, this also puts some ugly (and not fully-tested) | ||
| 2808 | ;;; bits of code in `yas/expand-snippet' and | ||
| 2809 | ;;; `yas/commit-snippet'. I've tried to mark them with "stacked | ||
| 2810 | ;;; expansion:". | ||
| 2811 | ;;; | ||
| 2812 | ;;; This was thought to be safer in in an undo/redo perpective, but | ||
| 2813 | ;;; maybe the correct implementation is to make the globals | ||
| 2814 | ;;; `yas/active-field-overlay' and `yas/field-protection-overlays' be | ||
| 2815 | ;;; snippet-local and be active even while the child snippet is | ||
| 2816 | ;;; running. This would mean a lot of overlay modification hooks | ||
| 2817 | ;;; running, but if managed correctly (including overlay priorities) | ||
| 2818 | ;;; they should account for all situations... | ||
| 2819 | ;;; | ||
| 2820 | |||
| 2821 | (defun yas/expand-snippet (template &optional start end expand-env) | ||
| 2822 | "Expand snippet at current point. Text between START and END | ||
| 2823 | will be deleted before inserting template." | ||
| 2824 | (run-hooks 'yas/before-expand-snippet-hook) | ||
| 2825 | |||
| 2826 | ;; If a region is active, set `yas/selected-text' | ||
| 2827 | (setq yas/selected-text | ||
| 2828 | (when mark-active | ||
| 2829 | (prog1 (buffer-substring-no-properties (region-beginning) | ||
| 2830 | (region-end)) | ||
| 2831 | (unless start (setq start (region-beginning)) | ||
| 2832 | (unless end (setq end (region-end))))))) | ||
| 2833 | |||
| 2834 | (when start | ||
| 2835 | (goto-char start)) | ||
| 2836 | |||
| 2837 | ;; stacked expansion: shoosh the overlay modification hooks | ||
| 2838 | ;; | ||
| 2839 | (let ((to-delete (and start end (buffer-substring-no-properties start end))) | ||
| 2840 | (start (or start (point))) | ||
| 2841 | (end (or end (point))) | ||
| 2842 | (inhibit-modification-hooks t) | ||
| 2843 | (column (current-column)) | ||
| 2844 | snippet) | ||
| 2845 | |||
| 2846 | ;; Delete the region to delete, this *does* get undo-recorded. | ||
| 2847 | ;; | ||
| 2848 | (when (and to-delete | ||
| 2849 | (> end start)) | ||
| 2850 | (delete-region start end) | ||
| 2851 | (setq yas/deleted-text to-delete)) | ||
| 2852 | |||
| 2853 | ;; Narrow the region down to the template, shoosh the | ||
| 2854 | ;; `buffer-undo-list', and create the snippet, the new snippet | ||
| 2855 | ;; updates its mirrors once, so we are left with some plain text. | ||
| 2856 | ;; The undo action for deleting this plain text will get recorded | ||
| 2857 | ;; at the end of this function. | ||
| 2858 | (save-restriction | ||
| 2859 | (narrow-to-region start start) | ||
| 2860 | (let ((buffer-undo-list t)) | ||
| 2861 | ;; snippet creation might evaluate users elisp, which | ||
| 2862 | ;; might generate errors, so we have to be ready to catch | ||
| 2863 | ;; them mostly to make the undo information | ||
| 2864 | ;; | ||
| 2865 | (setq yas/start-column (save-restriction (widen) (current-column))) | ||
| 2866 | (insert template) | ||
| 2867 | |||
| 2868 | (setq snippet | ||
| 2869 | (if expand-env | ||
| 2870 | (let ((read-vars (condition-case err | ||
| 2871 | (read expand-env) | ||
| 2872 | (error nil)))) | ||
| 2873 | (eval `(let ,read-vars | ||
| 2874 | (yas/snippet-create (point-min) (point-max))))) | ||
| 2875 | (yas/snippet-create (point-min) (point-max)))))) | ||
| 2876 | |||
| 2877 | ;; stacked-expansion: This checks for stacked expansion, save the | ||
| 2878 | ;; `yas/previous-active-field' and advance its boudary. | ||
| 2879 | ;; | ||
| 2880 | (let ((existing-field (and yas/active-field-overlay | ||
| 2881 | (overlay-buffer yas/active-field-overlay) | ||
| 2882 | (overlay-get yas/active-field-overlay 'yas/field)))) | ||
| 2883 | (when existing-field | ||
| 2884 | (setf (yas/snippet-previous-active-field snippet) existing-field) | ||
| 2885 | (yas/advance-end-maybe existing-field (overlay-end yas/active-field-overlay)))) | ||
| 2886 | |||
| 2887 | ;; Exit the snippet immediately if no fields | ||
| 2888 | ;; | ||
| 2889 | (unless (yas/snippet-fields snippet) | ||
| 2890 | (yas/exit-snippet snippet)) | ||
| 2891 | |||
| 2892 | ;; Push two undo actions: the deletion of the inserted contents of | ||
| 2893 | ;; the new snippet (without the "key") followed by an apply of | ||
| 2894 | ;; `yas/take-care-of-redo' on the newly inserted snippet boundaries | ||
| 2895 | ;; | ||
| 2896 | (let ((start (overlay-start (yas/snippet-control-overlay snippet))) | ||
| 2897 | (end (overlay-end (yas/snippet-control-overlay snippet)))) | ||
| 2898 | (push (cons start end) buffer-undo-list) | ||
| 2899 | (push `(apply yas/take-care-of-redo ,start ,end ,snippet) | ||
| 2900 | buffer-undo-list)) | ||
| 2901 | ;; Now, move to the first field | ||
| 2902 | ;; | ||
| 2903 | (let ((first-field (car (yas/snippet-fields snippet)))) | ||
| 2904 | (when first-field | ||
| 2905 | (yas/move-to-field snippet first-field)))) | ||
| 2906 | (message "[yas] snippet expanded.")) | ||
| 2907 | |||
| 2908 | (defun yas/take-care-of-redo (beg end snippet) | ||
| 2909 | "Commits SNIPPET, which in turn pushes an undo action for | ||
| 2910 | reviving it. | ||
| 2911 | |||
| 2912 | Meant to exit in the `buffer-undo-list'." | ||
| 2913 | ;; slightly optimize: this action is only needed for snippets with | ||
| 2914 | ;; at least one field | ||
| 2915 | (when (yas/snippet-fields snippet) | ||
| 2916 | (yas/commit-snippet snippet 'no-hooks))) | ||
| 2917 | |||
| 2918 | (defun yas/snippet-revive (beg end snippet) | ||
| 2919 | "Revives the SNIPPET and creates a control overlay from BEG to | ||
| 2920 | END. | ||
| 2921 | |||
| 2922 | BEG and END are, we hope, the original snippets boudaries. All | ||
| 2923 | the markers/points exiting existing inside SNIPPET should point | ||
| 2924 | to their correct locations *at the time the snippet is revived*. | ||
| 2925 | |||
| 2926 | After revival, push the `yas/take-care-of-redo' in the | ||
| 2927 | `buffer-undo-list'" | ||
| 2928 | ;; Reconvert all the points to markers | ||
| 2929 | ;; | ||
| 2930 | (yas/points-to-markers snippet) | ||
| 2931 | ;; When at least one editable field existed in the zombie snippet, | ||
| 2932 | ;; try to revive the whole thing... | ||
| 2933 | ;; | ||
| 2934 | (let ((target-field (or (yas/snippet-active-field snippet) | ||
| 2935 | (car (yas/snippet-fields snippet))))) | ||
| 2936 | (when target-field | ||
| 2937 | (setf (yas/snippet-control-overlay snippet) (yas/make-control-overlay snippet beg end)) | ||
| 2938 | (overlay-put (yas/snippet-control-overlay snippet) 'yas/snippet snippet) | ||
| 2939 | |||
| 2940 | (yas/move-to-field snippet target-field) | ||
| 2941 | |||
| 2942 | (add-hook 'post-command-hook 'yas/post-command-handler nil t) | ||
| 2943 | (add-hook 'pre-command-hook 'yas/pre-command-handler t t) | ||
| 2944 | |||
| 2945 | (push `(apply yas/take-care-of-redo ,beg ,end ,snippet) | ||
| 2946 | buffer-undo-list)))) | ||
| 2947 | |||
| 2948 | (defun yas/snippet-create (begin end) | ||
| 2949 | "Creates a snippet from an template inserted between BEGIN and END. | ||
| 2950 | |||
| 2951 | Returns the newly created snippet." | ||
| 2952 | (let ((snippet (yas/make-snippet))) | ||
| 2953 | (goto-char begin) | ||
| 2954 | (yas/snippet-parse-create snippet) | ||
| 2955 | |||
| 2956 | ;; Sort and link each field | ||
| 2957 | (yas/snippet-sort-fields snippet) | ||
| 2958 | |||
| 2959 | ;; Create keymap overlay for snippet | ||
| 2960 | (setf (yas/snippet-control-overlay snippet) | ||
| 2961 | (yas/make-control-overlay snippet (point-min) (point-max))) | ||
| 2962 | |||
| 2963 | ;; Move to end | ||
| 2964 | (goto-char (point-max)) | ||
| 2965 | |||
| 2966 | ;; Setup hooks | ||
| 2967 | (add-hook 'post-command-hook 'yas/post-command-handler nil t) | ||
| 2968 | (add-hook 'pre-command-hook 'yas/pre-command-handler t t) | ||
| 2969 | |||
| 2970 | snippet)) | ||
| 2971 | |||
| 2972 | |||
| 2973 | ;;; Apropos adjacencies: Once the $-constructs bits like "$n" and | ||
| 2974 | ;;; "${:n" are deleted in the recently expanded snippet, we might | ||
| 2975 | ;;; actually have many fields, mirrors (and the snippet exit) in the | ||
| 2976 | ;;; very same position in the buffer. Therefore we need to single-link | ||
| 2977 | ;;; the fields-or-mirrors-or-exit, which I have called "fom", | ||
| 2978 | ;;; according to their original positions in the buffer. | ||
| 2979 | ;;; | ||
| 2980 | ;;; Then we have operation `yas/advance-end-maybe' and | ||
| 2981 | ;;; `yas/advance-start-maybe', which conditionally push the starts and | ||
| 2982 | ;;; ends of these foms down the chain. | ||
| 2983 | ;;; | ||
| 2984 | ;;; This allows for like the printf with the magic ",": | ||
| 2985 | ;;; | ||
| 2986 | ;;; printf ("${1:%s}\\n"${1:$(if (string-match "%" text) "," "\);")} \ | ||
| 2987 | ;;; $2${1:$(if (string-match "%" text) "\);" "")}$0 | ||
| 2988 | ;;; | ||
| 2989 | |||
| 2990 | (defun yas/fom-start (fom) | ||
| 2991 | (cond ((yas/field-p fom) | ||
| 2992 | (yas/field-start fom)) | ||
| 2993 | ((yas/mirror-p fom) | ||
| 2994 | (yas/mirror-start fom)) | ||
| 2995 | (t | ||
| 2996 | (yas/exit-marker fom)))) | ||
| 2997 | |||
| 2998 | (defun yas/fom-end (fom) | ||
| 2999 | (cond ((yas/field-p fom) | ||
| 3000 | (yas/field-end fom)) | ||
| 3001 | ((yas/mirror-p fom) | ||
| 3002 | (yas/mirror-end fom)) | ||
| 3003 | (t | ||
| 3004 | (yas/exit-marker fom)))) | ||
| 3005 | |||
| 3006 | (defun yas/fom-next (fom) | ||
| 3007 | (cond ((yas/field-p fom) | ||
| 3008 | (yas/field-next fom)) | ||
| 3009 | ((yas/mirror-p fom) | ||
| 3010 | (yas/mirror-next fom)) | ||
| 3011 | (t | ||
| 3012 | (yas/exit-next fom)))) | ||
| 3013 | |||
| 3014 | (defun yas/calculate-adjacencies (snippet) | ||
| 3015 | "Calculate adjacencies for fields or mirrors of SNIPPET. | ||
| 3016 | |||
| 3017 | This is according to their relative positions in the buffer, and | ||
| 3018 | has to be called before the $-constructs are deleted." | ||
| 3019 | (flet ((yas/fom-set-next-fom (fom nextfom) | ||
| 3020 | (cond ((yas/field-p fom) | ||
| 3021 | (setf (yas/field-next fom) nextfom)) | ||
| 3022 | ((yas/mirror-p fom) | ||
| 3023 | (setf (yas/mirror-next fom) nextfom)) | ||
| 3024 | (t | ||
| 3025 | (setf (yas/exit-next fom) nextfom)))) | ||
| 3026 | (yas/compare-fom-begs (fom1 fom2) | ||
| 3027 | (> (yas/fom-start fom2) (yas/fom-start fom1))) | ||
| 3028 | (yas/link-foms (fom1 fom2) | ||
| 3029 | (yas/fom-set-next-fom fom1 fom2))) | ||
| 3030 | ;; make some yas/field, yas/mirror and yas/exit soup | ||
| 3031 | (let ((soup)) | ||
| 3032 | (when (yas/snippet-exit snippet) | ||
| 3033 | (push (yas/snippet-exit snippet) soup)) | ||
| 3034 | (dolist (field (yas/snippet-fields snippet)) | ||
| 3035 | (push field soup) | ||
| 3036 | (dolist (mirror (yas/field-mirrors field)) | ||
| 3037 | (push mirror soup))) | ||
| 3038 | (setq soup | ||
| 3039 | (sort soup | ||
| 3040 | #'yas/compare-fom-begs)) | ||
| 3041 | (when soup | ||
| 3042 | (reduce #'yas/link-foms soup))))) | ||
| 3043 | |||
| 3044 | (defun yas/advance-end-maybe (fom newend) | ||
| 3045 | "Maybe advance FOM's end to NEWEND if it needs it. | ||
| 3046 | |||
| 3047 | If it does, also: | ||
| 3048 | |||
| 3049 | * call `yas/advance-start-maybe' on FOM's next fom. | ||
| 3050 | |||
| 3051 | * in case FOM is field call `yas/advance-end-maybe' on its parent | ||
| 3052 | field" | ||
| 3053 | (when (and fom (< (yas/fom-end fom) newend)) | ||
| 3054 | (set-marker (yas/fom-end fom) newend) | ||
| 3055 | (yas/advance-start-maybe (yas/fom-next fom) newend) | ||
| 3056 | (if (and (yas/field-p fom) | ||
| 3057 | (yas/field-parent-field fom)) | ||
| 3058 | (yas/advance-end-maybe (yas/field-parent-field fom) newend)))) | ||
| 3059 | |||
| 3060 | (defun yas/advance-start-maybe (fom newstart) | ||
| 3061 | "Maybe advance FOM's start to NEWSTART if it needs it. | ||
| 3062 | |||
| 3063 | If it does, also call `yas/advance-end-maybe' on FOM." | ||
| 3064 | (when (and fom (< (yas/fom-start fom) newstart)) | ||
| 3065 | (set-marker (yas/fom-start fom) newstart) | ||
| 3066 | (yas/advance-end-maybe fom newstart))) | ||
| 3067 | |||
| 3068 | (defvar yas/dollar-regions nil | ||
| 3069 | "When expanding the snippet the \"parse-create\" functions add | ||
| 3070 | cons cells to this var") | ||
| 3071 | |||
| 3072 | (defun yas/snippet-parse-create (snippet) | ||
| 3073 | "Parse a recently inserted snippet template, creating all | ||
| 3074 | necessary fields, mirrors and exit points. | ||
| 3075 | |||
| 3076 | Meant to be called in a narrowed buffer, does various passes" | ||
| 3077 | (let ((parse-start (point))) | ||
| 3078 | ;; Reset the yas/dollar-regions | ||
| 3079 | ;; | ||
| 3080 | (setq yas/dollar-regions nil) | ||
| 3081 | ;; protect escaped quote, backquotes and backslashes | ||
| 3082 | ;; | ||
| 3083 | (yas/protect-escapes nil '(?\\ ?` ?')) | ||
| 3084 | ;; replace all backquoted expressions | ||
| 3085 | ;; | ||
| 3086 | (goto-char parse-start) | ||
| 3087 | (yas/replace-backquotes) | ||
| 3088 | ;; protect escapes again since previous steps might have generated | ||
| 3089 | ;; more characters needing escaping | ||
| 3090 | ;; | ||
| 3091 | (goto-char parse-start) | ||
| 3092 | (yas/protect-escapes) | ||
| 3093 | ;; parse fields with {} | ||
| 3094 | ;; | ||
| 3095 | (goto-char parse-start) | ||
| 3096 | (yas/field-parse-create snippet) | ||
| 3097 | ;; parse simple mirrors and fields | ||
| 3098 | ;; | ||
| 3099 | (goto-char parse-start) | ||
| 3100 | (yas/simple-mirror-parse-create snippet) | ||
| 3101 | ;; parse mirror transforms | ||
| 3102 | ;; | ||
| 3103 | (goto-char parse-start) | ||
| 3104 | (yas/transform-mirror-parse-create snippet) | ||
| 3105 | ;; calculate adjacencies of fields and mirrors | ||
| 3106 | ;; | ||
| 3107 | (yas/calculate-adjacencies snippet) | ||
| 3108 | ;; Delete $-constructs | ||
| 3109 | ;; | ||
| 3110 | (yas/delete-regions yas/dollar-regions) | ||
| 3111 | ;; restore escapes | ||
| 3112 | ;; | ||
| 3113 | (goto-char parse-start) | ||
| 3114 | (yas/restore-escapes) | ||
| 3115 | ;; update mirrors for the first time | ||
| 3116 | ;; | ||
| 3117 | (yas/update-mirrors snippet) | ||
| 3118 | ;; indent the best we can | ||
| 3119 | ;; | ||
| 3120 | (goto-char parse-start) | ||
| 3121 | (yas/indent snippet))) | ||
| 3122 | |||
| 3123 | (defun yas/indent-according-to-mode (snippet-markers) | ||
| 3124 | "Indent current line according to mode, preserving | ||
| 3125 | SNIPPET-MARKERS." | ||
| 3126 | ;; XXX: Here seems to be the indent problem: | ||
| 3127 | ;; | ||
| 3128 | ;; `indent-according-to-mode' uses whatever | ||
| 3129 | ;; `indent-line-function' is available. Some | ||
| 3130 | ;; implementations of these functions delete text | ||
| 3131 | ;; before they insert. If there happens to be a marker | ||
| 3132 | ;; just after the text being deleted, the insertion | ||
| 3133 | ;; actually happens after the marker, which misplaces | ||
| 3134 | ;; it. | ||
| 3135 | ;; | ||
| 3136 | ;; This would also happen if we had used overlays with | ||
| 3137 | ;; the `front-advance' property set to nil. | ||
| 3138 | ;; | ||
| 3139 | ;; This is why I have these `trouble-markers', they are the ones at | ||
| 3140 | ;; they are the ones at the first non-whitespace char at the line | ||
| 3141 | ;; (i.e. at `yas/real-line-beginning'. After indentation takes place | ||
| 3142 | ;; we should be at the correct to restore them to. All other | ||
| 3143 | ;; non-trouble-markers have been *pushed* and don't need special | ||
| 3144 | ;; attention. | ||
| 3145 | ;; | ||
| 3146 | (goto-char (yas/real-line-beginning)) | ||
| 3147 | (let ((trouble-markers (remove-if-not #'(lambda (marker) | ||
| 3148 | (= marker (point))) | ||
| 3149 | snippet-markers))) | ||
| 3150 | (save-restriction | ||
| 3151 | (widen) | ||
| 3152 | (condition-case err | ||
| 3153 | (indent-according-to-mode) | ||
| 3154 | (error (message "[yas] warning: yas/indent-according-to-mode habing problems running %s" indent-line-function) | ||
| 3155 | nil))) | ||
| 3156 | (mapc #'(lambda (marker) | ||
| 3157 | (set-marker marker (point))) | ||
| 3158 | trouble-markers))) | ||
| 3159 | |||
| 3160 | (defun yas/indent (snippet) | ||
| 3161 | (let ((snippet-markers (yas/collect-snippet-markers snippet))) | ||
| 3162 | ;; Look for those $> | ||
| 3163 | (save-excursion | ||
| 3164 | (while (re-search-forward "$>" nil t) | ||
| 3165 | (delete-region (match-beginning 0) (match-end 0)) | ||
| 3166 | (when (not (eq yas/indent-line 'auto)) | ||
| 3167 | (yas/indent-according-to-mode snippet-markers)))) | ||
| 3168 | ;; Now do stuff for 'fixed and 'auto | ||
| 3169 | (save-excursion | ||
| 3170 | (cond ((eq yas/indent-line 'fixed) | ||
| 3171 | (while (and (zerop (forward-line)) | ||
| 3172 | (zerop (current-column))) | ||
| 3173 | (indent-to-column column))) | ||
| 3174 | ((eq yas/indent-line 'auto) | ||
| 3175 | (let ((end (set-marker (make-marker) (point-max))) | ||
| 3176 | (indent-first-line-p yas/also-auto-indent-first-line)) | ||
| 3177 | (while (and (zerop (if indent-first-line-p | ||
| 3178 | (prog1 | ||
| 3179 | (forward-line 0) | ||
| 3180 | (setq indent-first-line-p nil)) | ||
| 3181 | (forward-line 1))) | ||
| 3182 | (not (eobp)) | ||
| 3183 | (<= (point) end)) | ||
| 3184 | (yas/indent-according-to-mode snippet-markers)))) | ||
| 3185 | (t | ||
| 3186 | nil))))) | ||
| 3187 | |||
| 3188 | (defun yas/collect-snippet-markers (snippet) | ||
| 3189 | "Make a list of all the markers used by SNIPPET." | ||
| 3190 | (let (markers) | ||
| 3191 | (dolist (field (yas/snippet-fields snippet)) | ||
| 3192 | (push (yas/field-start field) markers) | ||
| 3193 | (push (yas/field-end field) markers) | ||
| 3194 | (dolist (mirror (yas/field-mirrors field)) | ||
| 3195 | (push (yas/mirror-start mirror) markers) | ||
| 3196 | (push (yas/mirror-end mirror) markers))) | ||
| 3197 | (let ((snippet-exit (yas/snippet-exit snippet))) | ||
| 3198 | (when (and snippet-exit | ||
| 3199 | (marker-buffer (yas/exit-marker snippet-exit))) | ||
| 3200 | (push (yas/exit-marker snippet-exit) markers))) | ||
| 3201 | markers)) | ||
| 3202 | |||
| 3203 | (defun yas/real-line-beginning () | ||
| 3204 | (let ((c (char-after (line-beginning-position))) | ||
| 3205 | (n (line-beginning-position))) | ||
| 3206 | (while (or (eql c ?\ ) | ||
| 3207 | (eql c ?\t)) | ||
| 3208 | (incf n) | ||
| 3209 | (setq c (char-after n))) | ||
| 3210 | n)) | ||
| 3211 | |||
| 3212 | (defun yas/escape-string (escaped) | ||
| 3213 | (concat "YASESCAPE" (format "%d" escaped) "PROTECTGUARD")) | ||
| 3214 | |||
| 3215 | (defun yas/protect-escapes (&optional text escaped) | ||
| 3216 | "Protect all escaped characters with their numeric ASCII value. | ||
| 3217 | |||
| 3218 | With optional string TEXT do it in string instead of buffer." | ||
| 3219 | (let ((changed-text text) | ||
| 3220 | (text-provided-p text)) | ||
| 3221 | (mapc #'(lambda (escaped) | ||
| 3222 | (setq changed-text | ||
| 3223 | (yas/replace-all (concat "\\" (char-to-string escaped)) | ||
| 3224 | (yas/escape-string escaped) | ||
| 3225 | (when text-provided-p changed-text)))) | ||
| 3226 | (or escaped yas/escaped-characters)) | ||
| 3227 | changed-text)) | ||
| 3228 | |||
| 3229 | (defun yas/restore-escapes (&optional text escaped) | ||
| 3230 | "Restore all escaped characters from their numeric ASCII value. | ||
| 3231 | |||
| 3232 | With optional string TEXT do it in string instead of the buffer." | ||
| 3233 | (let ((changed-text text) | ||
| 3234 | (text-provided-p text)) | ||
| 3235 | (mapc #'(lambda (escaped) | ||
| 3236 | (setq changed-text | ||
| 3237 | (yas/replace-all (yas/escape-string escaped) | ||
| 3238 | (char-to-string escaped) | ||
| 3239 | (when text-provided-p changed-text)))) | ||
| 3240 | (or escaped yas/escaped-characters)) | ||
| 3241 | changed-text)) | ||
| 3242 | |||
| 3243 | (defun yas/replace-backquotes () | ||
| 3244 | "Replace all the \"`(lisp-expression)`\"-style expression | ||
| 3245 | with their evaluated value" | ||
| 3246 | (while (re-search-forward yas/backquote-lisp-expression-regexp nil t) | ||
| 3247 | (let ((transformed (yas/read-and-eval-string (yas/restore-escapes (match-string 1))))) | ||
| 3248 | (goto-char (match-end 0)) | ||
| 3249 | (when transformed (insert transformed)) | ||
| 3250 | (delete-region (match-beginning 0) (match-end 0))))) | ||
| 3251 | |||
| 3252 | (defun yas/scan-sexps (from count) | ||
| 3253 | (condition-case err | ||
| 3254 | (with-syntax-table (standard-syntax-table) | ||
| 3255 | (scan-sexps from count)) | ||
| 3256 | (error | ||
| 3257 | nil))) | ||
| 3258 | |||
| 3259 | (defun yas/make-marker (pos) | ||
| 3260 | "Create a marker at POS with `nil' `marker-insertion-type'" | ||
| 3261 | (let ((marker (set-marker (make-marker) pos))) | ||
| 3262 | (set-marker-insertion-type marker nil) | ||
| 3263 | marker)) | ||
| 3264 | |||
| 3265 | (defun yas/field-parse-create (snippet &optional parent-field) | ||
| 3266 | "Parse most field expressions, except for the simple one \"$n\". | ||
| 3267 | |||
| 3268 | The following count as a field: | ||
| 3269 | |||
| 3270 | * \"${n: text}\", for a numbered field with default text, as long as N is not 0; | ||
| 3271 | |||
| 3272 | * \"${n: text$(expression)}, the same with a lisp expression; | ||
| 3273 | this is caught with the curiously named `yas/multi-dollar-lisp-expression-regexp' | ||
| 3274 | |||
| 3275 | * the same as above but unnumbered, (no N:) and number is calculated automatically. | ||
| 3276 | |||
| 3277 | When multiple expressions are found, only the last one counts." | ||
| 3278 | ;; | ||
| 3279 | (save-excursion | ||
| 3280 | (while (re-search-forward yas/field-regexp nil t) | ||
| 3281 | (let* ((real-match-end-0 (yas/scan-sexps (1+ (match-beginning 0)) 1)) | ||
| 3282 | (number (and (match-string-no-properties 1) | ||
| 3283 | (string-to-number (match-string-no-properties 1)))) | ||
| 3284 | (brand-new-field (and real-match-end-0 | ||
| 3285 | ;; break if on "$(" immediately | ||
| 3286 | ;; after the ":", this will be | ||
| 3287 | ;; caught as a mirror with | ||
| 3288 | ;; transform later. | ||
| 3289 | (not (save-match-data | ||
| 3290 | (eq (string-match "$[ \t\n]*(" | ||
| 3291 | (match-string-no-properties 2)) 0))) | ||
| 3292 | (not (and number (zerop number))) | ||
| 3293 | (yas/make-field number | ||
| 3294 | (yas/make-marker (match-beginning 2)) | ||
| 3295 | (yas/make-marker (1- real-match-end-0)) | ||
| 3296 | parent-field)))) | ||
| 3297 | (when brand-new-field | ||
| 3298 | (goto-char real-match-end-0) | ||
| 3299 | (push (cons (1- real-match-end-0) real-match-end-0) | ||
| 3300 | yas/dollar-regions) | ||
| 3301 | (push (cons (match-beginning 0) (match-beginning 2)) | ||
| 3302 | yas/dollar-regions) | ||
| 3303 | (push brand-new-field (yas/snippet-fields snippet)) | ||
| 3304 | (save-excursion | ||
| 3305 | (save-restriction | ||
| 3306 | (narrow-to-region (yas/field-start brand-new-field) (yas/field-end brand-new-field)) | ||
| 3307 | (goto-char (point-min)) | ||
| 3308 | (yas/field-parse-create snippet brand-new-field))))))) | ||
| 3309 | ;; if we entered from a parent field, now search for the | ||
| 3310 | ;; `yas/multi-dollar-lisp-expression-regexp'. THis is used for | ||
| 3311 | ;; primary field transformations | ||
| 3312 | ;; | ||
| 3313 | (when parent-field | ||
| 3314 | (save-excursion | ||
| 3315 | (while (re-search-forward yas/multi-dollar-lisp-expression-regexp nil t) | ||
| 3316 | (let* ((real-match-end-1 (yas/scan-sexps (match-beginning 1) 1))) | ||
| 3317 | ;; commit the primary field transformation if we don't find | ||
| 3318 | ;; it in yas/dollar-regions (a subnested field) might have | ||
| 3319 | ;; already caught it. | ||
| 3320 | (when (and real-match-end-1 | ||
| 3321 | (not (member (cons (match-beginning 0) | ||
| 3322 | real-match-end-1) | ||
| 3323 | yas/dollar-regions))) | ||
| 3324 | (let ((lisp-expression-string (buffer-substring-no-properties (match-beginning 1) | ||
| 3325 | real-match-end-1))) | ||
| 3326 | (setf (yas/field-transform parent-field) (yas/restore-escapes lisp-expression-string))) | ||
| 3327 | (push (cons (match-beginning 0) real-match-end-1) | ||
| 3328 | yas/dollar-regions))))))) | ||
| 3329 | |||
| 3330 | (defun yas/transform-mirror-parse-create (snippet) | ||
| 3331 | "Parse the \"${n:$(lisp-expression)}\" mirror transformations." | ||
| 3332 | (while (re-search-forward yas/transform-mirror-regexp nil t) | ||
| 3333 | (let* ((real-match-end-0 (yas/scan-sexps (1+ (match-beginning 0)) 1)) | ||
| 3334 | (number (string-to-number (match-string-no-properties 1))) | ||
| 3335 | (field (and number | ||
| 3336 | (not (zerop number)) | ||
| 3337 | (yas/snippet-find-field snippet number)))) | ||
| 3338 | (when (and real-match-end-0 | ||
| 3339 | field) | ||
| 3340 | (push (yas/make-mirror (yas/make-marker (match-beginning 0)) | ||
| 3341 | (yas/make-marker (match-beginning 0)) | ||
| 3342 | (yas/restore-escapes | ||
| 3343 | (buffer-substring-no-properties (match-beginning 2) | ||
| 3344 | (1- real-match-end-0)))) | ||
| 3345 | (yas/field-mirrors field)) | ||
| 3346 | (push (cons (match-beginning 0) real-match-end-0) yas/dollar-regions))))) | ||
| 3347 | |||
| 3348 | (defun yas/simple-mirror-parse-create (snippet) | ||
| 3349 | "Parse the simple \"$n\" mirrors and the exit-marker." | ||
| 3350 | (while (re-search-forward yas/simple-mirror-regexp nil t) | ||
| 3351 | (let ((number (string-to-number (match-string-no-properties 1)))) | ||
| 3352 | (cond ((zerop number) | ||
| 3353 | |||
| 3354 | (setf (yas/snippet-exit snippet) | ||
| 3355 | (yas/make-exit (yas/make-marker (match-end 0)))) | ||
| 3356 | (save-excursion | ||
| 3357 | (goto-char (match-beginning 0)) | ||
| 3358 | (when yas/wrap-around-region | ||
| 3359 | (cond (yas/selected-text | ||
| 3360 | (insert yas/selected-text)) | ||
| 3361 | ((and (eq yas/wrap-around-region 'cua) | ||
| 3362 | cua-mode | ||
| 3363 | (get-register ?0)) | ||
| 3364 | (insert (prog1 (get-register ?0) | ||
| 3365 | (set-register ?0 nil)))))) | ||
| 3366 | (push (cons (point) (yas/exit-marker (yas/snippet-exit snippet))) | ||
| 3367 | yas/dollar-regions))) | ||
| 3368 | (t | ||
| 3369 | (let ((field (yas/snippet-find-field snippet number))) | ||
| 3370 | (if field | ||
| 3371 | (push (yas/make-mirror (yas/make-marker (match-beginning 0)) | ||
| 3372 | (yas/make-marker (match-beginning 0)) | ||
| 3373 | nil) | ||
| 3374 | (yas/field-mirrors field)) | ||
| 3375 | (push (yas/make-field number | ||
| 3376 | (yas/make-marker (match-beginning 0)) | ||
| 3377 | (yas/make-marker (match-beginning 0)) | ||
| 3378 | nil) | ||
| 3379 | (yas/snippet-fields snippet)))) | ||
| 3380 | (push (cons (match-beginning 0) (match-end 0)) | ||
| 3381 | yas/dollar-regions)))))) | ||
| 3382 | |||
| 3383 | (defun yas/delete-regions (regions) | ||
| 3384 | "Sort disjuct REGIONS by start point, then delete from the back." | ||
| 3385 | (mapc #'(lambda (reg) | ||
| 3386 | (delete-region (car reg) (cdr reg))) | ||
| 3387 | (sort regions | ||
| 3388 | #'(lambda (r1 r2) | ||
| 3389 | (>= (car r1) (car r2)))))) | ||
| 3390 | |||
| 3391 | (defun yas/update-mirrors (snippet) | ||
| 3392 | "Updates all the mirrors of SNIPPET." | ||
| 3393 | (save-excursion | ||
| 3394 | (dolist (field (yas/snippet-fields snippet)) | ||
| 3395 | (dolist (mirror (yas/field-mirrors field)) | ||
| 3396 | ;; stacked expansion: I added an `inhibit-modification-hooks' | ||
| 3397 | ;; here, for safety, may need to remove if we the mechanism is | ||
| 3398 | ;; altered. | ||
| 3399 | ;; | ||
| 3400 | (let ((inhibit-modification-hooks t)) | ||
| 3401 | (yas/mirror-update-display mirror field) | ||
| 3402 | ;; `yas/place-overlays' is needed if the active field and | ||
| 3403 | ;; protected overlays have been changed because of insertions | ||
| 3404 | ;; in `yas/mirror-update-display' | ||
| 3405 | ;; | ||
| 3406 | (when (eq field (yas/snippet-active-field snippet)) | ||
| 3407 | (yas/place-overlays snippet field))))))) | ||
| 3408 | |||
| 3409 | (defun yas/mirror-update-display (mirror field) | ||
| 3410 | "Update MIRROR according to FIELD (and mirror transform)." | ||
| 3411 | (let ((reflection (or (yas/apply-transform mirror field) | ||
| 3412 | (yas/field-text-for-display field)))) | ||
| 3413 | (when (and reflection | ||
| 3414 | (not (string= reflection (buffer-substring-no-properties (yas/mirror-start mirror) | ||
| 3415 | (yas/mirror-end mirror))))) | ||
| 3416 | (goto-char (yas/mirror-start mirror)) | ||
| 3417 | (insert reflection) | ||
| 3418 | (if (> (yas/mirror-end mirror) (point)) | ||
| 3419 | (delete-region (point) (yas/mirror-end mirror)) | ||
| 3420 | (set-marker (yas/mirror-end mirror) (point)) | ||
| 3421 | (yas/advance-start-maybe (yas/mirror-next mirror) (point)))))) | ||
| 3422 | |||
| 3423 | (defun yas/field-update-display (field snippet) | ||
| 3424 | "Much like `yas/mirror-update-display', but for fields" | ||
| 3425 | (when (yas/field-transform field) | ||
| 3426 | (let ((inhibit-modification-hooks t) | ||
| 3427 | (transformed (yas/apply-transform field field)) | ||
| 3428 | (point (point))) | ||
| 3429 | (when (and transformed | ||
| 3430 | (not (string= transformed (buffer-substring-no-properties (yas/field-start field) | ||
| 3431 | (yas/field-end field))))) | ||
| 3432 | (setf (yas/field-modified-p field) t) | ||
| 3433 | (goto-char (yas/field-start field)) | ||
| 3434 | (insert transformed) | ||
| 3435 | (if (> (yas/field-end field) (point)) | ||
| 3436 | (delete-region (point) (yas/field-end field)) | ||
| 3437 | (set-marker (yas/field-end field) (point)) | ||
| 3438 | (yas/advance-start-maybe (yas/field-next field) (point))) | ||
| 3439 | t)))) | ||
| 3440 | |||
| 3441 | |||
| 3442 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 3443 | ;; Pre- and post-command hooks | ||
| 3444 | ;; | ||
| 3445 | (defun yas/pre-command-handler () ) | ||
| 3446 | |||
| 3447 | (defun yas/post-command-handler () | ||
| 3448 | "Handles various yasnippet conditions after each command." | ||
| 3449 | (cond (yas/protection-violation | ||
| 3450 | (goto-char yas/protection-violation) | ||
| 3451 | (setq yas/protection-violation nil)) | ||
| 3452 | ((eq 'undo this-command) | ||
| 3453 | ;; | ||
| 3454 | ;; After undo revival the correct field is sometimes not | ||
| 3455 | ;; restored correctly, this condition handles that | ||
| 3456 | ;; | ||
| 3457 | (let* ((snippet (car (yas/snippets-at-point))) | ||
| 3458 | (target-field (and snippet | ||
| 3459 | (find-if-not #'(lambda (field) | ||
| 3460 | (yas/field-probably-deleted-p snippet field)) | ||
| 3461 | (remove nil | ||
| 3462 | (cons (yas/snippet-active-field snippet) | ||
| 3463 | (yas/snippet-fields snippet))))))) | ||
| 3464 | (when target-field | ||
| 3465 | (yas/move-to-field snippet target-field)))) | ||
| 3466 | ((not (yas/undo-in-progress)) | ||
| 3467 | ;; When not in an undo, check if we must commit the snippet (use exited it). | ||
| 3468 | (yas/check-commit-snippet)))) | ||
| 3469 | |||
| 3470 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 3471 | ;; Debug functions. Use (or change) at will whenever needed. | ||
| 3472 | ;; | ||
| 3473 | ;; some useful debug code for looking up snippet tables | ||
| 3474 | ;; | ||
| 3475 | ;; (insert (pp | ||
| 3476 | ;; (let ((shit)) | ||
| 3477 | ;; (maphash #'(lambda (k v) | ||
| 3478 | ;; (push k shit)) | ||
| 3479 | ;; (yas/snippet-table-hash (gethash 'ruby-mode yas/snippet-tables))) | ||
| 3480 | ;; shit))) | ||
| 3481 | ;; | ||
| 3482 | |||
| 3483 | (defun yas/debug-tables () | ||
| 3484 | (interactive) | ||
| 3485 | (with-output-to-temp-buffer "*YASnippet tables*" | ||
| 3486 | (dolist (symbol (remove nil (append (list major-mode) | ||
| 3487 | (if (listp yas/mode-symbol) | ||
| 3488 | yas/mode-symbol | ||
| 3489 | (list yas/mode-symbol))))) | ||
| 3490 | (princ (format "Snippet table hash keys for %s:\n\n" symbol)) | ||
| 3491 | (let ((keys)) | ||
| 3492 | (maphash #'(lambda (k v) | ||
| 3493 | (push k keys)) | ||
| 3494 | (yas/snippet-table-hash (gethash symbol yas/snippet-tables))) | ||
| 3495 | (princ keys)) | ||
| 3496 | |||
| 3497 | (princ (format "Keymap for %s:\n\n" symbol)) | ||
| 3498 | (princ (gethash symbol yas/menu-table))))) | ||
| 3499 | |||
| 3500 | (defun yas/debug-snippet-vars () | ||
| 3501 | "Debug snippets, fields, mirrors and the `buffer-undo-list'." | ||
| 3502 | (interactive) | ||
| 3503 | (with-output-to-temp-buffer "*YASnippet trace*" | ||
| 3504 | (princ "Interesting YASnippet vars: \n\n") | ||
| 3505 | |||
| 3506 | (princ (format "\nPost command hook: %s\n" post-command-hook)) | ||
| 3507 | (princ (format "\nPre command hook: %s\n" pre-command-hook)) | ||
| 3508 | |||
| 3509 | (princ (format "%s live snippets in total\n" (length (yas/snippets-at-point (quote all-snippets))))) | ||
| 3510 | (princ (format "%s overlays in buffer:\n\n" (length (overlays-in (point-min) (point-max))))) | ||
| 3511 | (princ (format "%s live snippets at point:\n\n" (length (yas/snippets-at-point)))) | ||
| 3512 | |||
| 3513 | |||
| 3514 | (dolist (snippet (yas/snippets-at-point)) | ||
| 3515 | (princ (format "\tsid: %d control overlay from %d to %d\n" | ||
| 3516 | (yas/snippet-id snippet) | ||
| 3517 | (overlay-start (yas/snippet-control-overlay snippet)) | ||
| 3518 | (overlay-end (yas/snippet-control-overlay snippet)))) | ||
| 3519 | (princ (format "\tactive field: %d from %s to %s covering \"%s\"\n" | ||
| 3520 | (yas/field-number (yas/snippet-active-field snippet)) | ||
| 3521 | (marker-position (yas/field-start (yas/snippet-active-field snippet))) | ||
| 3522 | (marker-position (yas/field-end (yas/snippet-active-field snippet))) | ||
| 3523 | (buffer-substring-no-properties (yas/field-start (yas/snippet-active-field snippet)) (yas/field-end (yas/snippet-active-field snippet))))) | ||
| 3524 | (when (yas/snippet-exit snippet) | ||
| 3525 | (princ (format "\tsnippet-exit: at %s next: %s\n" | ||
| 3526 | (yas/exit-marker (yas/snippet-exit snippet)) | ||
| 3527 | (yas/exit-next (yas/snippet-exit snippet))))) | ||
| 3528 | (dolist (field (yas/snippet-fields snippet)) | ||
| 3529 | (princ (format "\tfield: %d from %s to %s covering \"%s\" next: %s\n" | ||
| 3530 | (yas/field-number field) | ||
| 3531 | (marker-position (yas/field-start field)) | ||
| 3532 | (marker-position (yas/field-end field)) | ||
| 3533 | (buffer-substring-no-properties (yas/field-start field) (yas/field-end field)) | ||
| 3534 | (yas/debug-format-fom-concise (yas/field-next field)))) | ||
| 3535 | (dolist (mirror (yas/field-mirrors field)) | ||
| 3536 | (princ (format "\t\tmirror: from %s to %s covering \"%s\" next: %s\n" | ||
| 3537 | (marker-position (yas/mirror-start mirror)) | ||
| 3538 | (marker-position (yas/mirror-end mirror)) | ||
| 3539 | (buffer-substring-no-properties (yas/mirror-start mirror) (yas/mirror-end mirror)) | ||
| 3540 | (yas/debug-format-fom-concise (yas/mirror-next mirror))))))) | ||
| 3541 | |||
| 3542 | (princ (format "\nUndo is %s and point-max is %s.\n" | ||
| 3543 | (if (eq buffer-undo-list t) | ||
| 3544 | "DISABLED" | ||
| 3545 | "ENABLED") | ||
| 3546 | (point-max))) | ||
| 3547 | (unless (eq buffer-undo-list t) | ||
| 3548 | (princ (format "Undpolist has %s elements. First 10 elements follow:\n" (length buffer-undo-list))) | ||
| 3549 | (let ((first-ten (subseq buffer-undo-list 0 19))) | ||
| 3550 | (dolist (undo-elem first-ten) | ||
| 3551 | (princ (format "%2s: %s\n" (position undo-elem first-ten) (truncate-string-to-width (format "%s" undo-elem) 70)))))))) | ||
| 3552 | |||
| 3553 | (defun yas/debug-format-fom-concise (fom) | ||
| 3554 | (when fom | ||
| 3555 | (cond ((yas/field-p fom) | ||
| 3556 | (format "field %d from %d to %d" | ||
| 3557 | (yas/field-number fom) | ||
| 3558 | (marker-position (yas/field-start fom)) | ||
| 3559 | (marker-position (yas/field-end fom)))) | ||
| 3560 | ((yas/mirror-p fom) | ||
| 3561 | (format "mirror from %d to %d" | ||
| 3562 | (marker-position (yas/mirror-start fom)) | ||
| 3563 | (marker-position (yas/mirror-end fom)))) | ||
| 3564 | (t | ||
| 3565 | (format "snippet exit at %d" | ||
| 3566 | (marker-position (yas/fom-start fom))))))) | ||
| 3567 | |||
| 3568 | |||
| 3569 | (defun yas/exterminate-package () | ||
| 3570 | (interactive) | ||
| 3571 | (yas/global-mode -1) | ||
| 3572 | (yas/minor-mode -1) | ||
| 3573 | (yas/kill-snippet-keybindings) | ||
| 3574 | (mapatoms #'(lambda (atom) | ||
| 3575 | (when (string-match "yas/" (symbol-name atom)) | ||
| 3576 | (unintern atom))))) | ||
| 3577 | |||
| 3578 | (defun yas/debug-test (&optional quiet) | ||
| 3579 | (interactive "P") | ||
| 3580 | (yas/load-directory (or (and (listp yas/root-directory) | ||
| 3581 | (first yas/root-directory)) | ||
| 3582 | yas/root-directory | ||
| 3583 | "~/Source/yasnippet/snippets/")) | ||
| 3584 | (set-buffer (switch-to-buffer "*YAS TEST*")) | ||
| 3585 | (mapc #'yas/commit-snippet (yas/snippets-at-point 'all-snippets)) | ||
| 3586 | (erase-buffer) | ||
| 3587 | (setq buffer-undo-list nil) | ||
| 3588 | (setq undo-in-progress nil) | ||
| 3589 | (snippet-mode) | ||
| 3590 | (yas/minor-mode 1) | ||
| 3591 | (let ((abbrev)) | ||
| 3592 | (setq abbrev "$f") | ||
| 3593 | (insert abbrev)) | ||
| 3594 | (unless quiet | ||
| 3595 | (add-hook 'post-command-hook 'yas/debug-snippet-vars 't 'local))) | ||
| 3596 | |||
| 3597 | |||
| 3598 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 3599 | ;;; `locate-dominating-file' is added for compatibility in emacs < 23 | ||
| 3600 | (unless (or (eq emacs-major-version 23) | ||
| 3601 | (fboundp 'locate-dominating-file)) | ||
| 3602 | (defvar locate-dominating-stop-dir-regexp | ||
| 3603 | "\\`\\(?:[\\/][\\/][^\\/]+[\\/]\\|/\\(?:net\\|afs\\|\\.\\.\\.\\)/\\)\\'" | ||
| 3604 | "Regexp of directory names which stop the search in `locate-dominating-file'. | ||
| 3605 | Any directory whose name matches this regexp will be treated like | ||
| 3606 | a kind of root directory by `locate-dominating-file' which will stop its search | ||
| 3607 | when it bumps into it. | ||
| 3608 | The default regexp prevents fruitless and time-consuming attempts to find | ||
| 3609 | special files in directories in which filenames are interpreted as hostnames, | ||
| 3610 | or mount points potentially requiring authentication as a different user.") | ||
| 3611 | |||
| 3612 | (defun locate-dominating-file (file name) | ||
| 3613 | "Look up the directory hierarchy from FILE for a file named NAME. | ||
| 3614 | Stop at the first parent directory containing a file NAME, | ||
| 3615 | and return the directory. Return nil if not found." | ||
| 3616 | ;; We used to use the above locate-dominating-files code, but the | ||
| 3617 | ;; directory-files call is very costly, so we're much better off doing | ||
| 3618 | ;; multiple calls using the code in here. | ||
| 3619 | ;; | ||
| 3620 | ;; Represent /home/luser/foo as ~/foo so that we don't try to look for | ||
| 3621 | ;; `name' in /home or in /. | ||
| 3622 | (setq file (abbreviate-file-name file)) | ||
| 3623 | (let ((root nil) | ||
| 3624 | (prev-file file) | ||
| 3625 | ;; `user' is not initialized outside the loop because | ||
| 3626 | ;; `file' may not exist, so we may have to walk up part of the | ||
| 3627 | ;; hierarchy before we find the "initial UID". | ||
| 3628 | (user nil) | ||
| 3629 | try) | ||
| 3630 | (while (not (or root | ||
| 3631 | (null file) | ||
| 3632 | ;; FIXME: Disabled this heuristic because it is sometimes | ||
| 3633 | ;; inappropriate. | ||
| 3634 | ;; As a heuristic, we stop looking up the hierarchy of | ||
| 3635 | ;; directories as soon as we find a directory belonging | ||
| 3636 | ;; to another user. This should save us from looking in | ||
| 3637 | ;; things like /net and /afs. This assumes that all the | ||
| 3638 | ;; files inside a project belong to the same user. | ||
| 3639 | ;; (let ((prev-user user)) | ||
| 3640 | ;; (setq user (nth 2 (file-attributes file))) | ||
| 3641 | ;; (and prev-user (not (equal user prev-user)))) | ||
| 3642 | (string-match locate-dominating-stop-dir-regexp file))) | ||
| 3643 | (setq try (file-exists-p (expand-file-name name file))) | ||
| 3644 | (cond (try (setq root file)) | ||
| 3645 | ((equal file (setq prev-file file | ||
| 3646 | file (file-name-directory | ||
| 3647 | (directory-file-name file)))) | ||
| 3648 | (setq file nil)))) | ||
| 3649 | root))) | ||
| 3650 | |||
| 3651 | (provide 'yasnippet) | ||
| 3652 | |||
| 3653 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 3654 | ;; Monkey patching for other functions that's causing | ||
| 3655 | ;; problems to yasnippet. For details on why I patch | ||
| 3656 | ;; those functions, refer to | ||
| 3657 | ;; http://code.google.com/p/yasnippet/wiki/MonkeyPatching | ||
| 3658 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 3659 | (defadvice c-neutralize-syntax-in-CPP | ||
| 3660 | (around yas-mp/c-neutralize-syntax-in-CPP activate) | ||
| 3661 | "Adviced `c-neutralize-syntax-in-CPP' to properly | ||
| 3662 | handle the end-of-buffer error fired in it by calling | ||
| 3663 | `forward-char' at the end of buffer." | ||
| 3664 | (condition-case err | ||
| 3665 | ad-do-it | ||
| 3666 | (error (message (error-message-string err))))) | ||
| 3667 | |||
| 3668 | ;; disable c-electric-* serial command in YAS fields | ||
| 3669 | (add-hook 'c-mode-common-hook | ||
| 3670 | '(lambda () | ||
| 3671 | (dolist (k '(":" ">" ";" "<" "{" "}")) | ||
| 3672 | (define-key (symbol-value (make-local-variable 'yas/keymap)) | ||
| 3673 | k 'self-insert-command)))) | ||
| 3674 | |||
| 3675 | |||
| 3676 | ;;; yasnippet.el ends here | ||
