Skip to content
This repository
Browse code

Merge branch '0.8-fsf-changes'

  • Loading branch information...
commit f28a3df702c62f1b045ea5b57d1344c90ebc9731 2 parents f96bd13 + 100beaf
João Távora authored

Showing 3 changed files with 84 additions and 63 deletions. Show diff stats Hide diff stats

  1. +2 0  .gitignore
  2. +4 4 yasnippet-tests.el
  3. +78 59 yasnippet.el
2  .gitignore
@@ -6,3 +6,5 @@ extras/imported/**
6 6 !extras/imported/*/.yas-setup.el
7 7 .yas-compiled-snippets.el
8 8 *.elc
  9 +ert-x.*
  10 +ert.*
8 yasnippet-tests.el
@@ -424,9 +424,9 @@ TODO: be meaner"
424 424 (ert-deftest test-yas-tab-binding ()
425 425 (with-temp-buffer
426 426 (yas-minor-mode -1)
427   - (should (not (eq (key-binding (yas--read-keybinding yas-trigger-key)) 'yas-expand)))
  427 + (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
428 428 (yas-minor-mode 1)
429   - (should (eq (key-binding (yas--read-keybinding yas-trigger-key)) 'yas-expand))
  429 + (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
430 430 (yas-expand-snippet "$1 $2 $3")
431 431 (dolist (k (if (listp yas-next-field-key)
432 432 yas-next-field-key
@@ -441,7 +441,7 @@ TODO: be meaner"
441 441 (with-temp-buffer
442 442 (org-mode)
443 443 (yas-minor-mode 1)
444   - (should (eq (key-binding (yas--read-keybinding yas-trigger-key)) 'yas-expand))))
  444 + (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))))
445 445
446 446 ;;; Helpers
447 447 ;;;
@@ -515,7 +515,7 @@ TODO: be meaner"
515 515 (progn
516 516 (mapc #'yas-make-file-or-dirs dirs)
517 517 (funcall fn))
518   - (when (>= emacs-major-version 23)
  518 + (when (>= emacs-major-version 24)
519 519 (delete-directory default-directory 'recursive))))))
520 520
521 521 (defmacro yas-with-snippet-dirs (dirs &rest body)
137 yasnippet.el
@@ -236,8 +236,8 @@ Naturally this is only valid when `yas-indent-line' is `auto'"
236 236 :type 'boolean
237 237 :group 'yasnippet)
238 238
239   -(defcustom yas-trigger-key "TAB"
240   - "The key bound to `yas-expand' when function `yas-minor-mode' is active.
  239 +(defcustom yas-trigger-key "<tab>"
  240 + "The key bound to `yas-expand' when `yas-minor-mode' is active.
241 241
242 242 Value is a string that is converted to the internal Emacs key
243 243 representation using `read-kbd-macro'."
@@ -2257,6 +2257,18 @@ expand immediately. Common gateway for
2257 2257 end
2258 2258 (yas--template-expand-env yas--current-template)))))
2259 2259
  2260 +(defun yas--trigger-key-for-fallback ()
  2261 + ;; When `yas-trigger-key' is <tab> it correctly overrides
  2262 + ;; org-mode's <tab>, for example and searching for fallbacks
  2263 + ;; correctly returns `org-cycle'. However, most other modes bind
  2264 + ;; "TAB" (which is translated from <tab>), and calling
  2265 + ;; (key-binding "TAB") does not place return that command into
  2266 + ;; our command-2 local. So we cheat.
  2267 + ;;
  2268 + (if (string= yas-trigger-key "<tab>")
  2269 + "TAB"
  2270 + yas-trigger-key))
  2271 +
2260 2272 (defun yas--fallback (&optional from-trigger-key-p)
2261 2273 "Fallback after expansion has failed.
2262 2274
@@ -2268,6 +2280,7 @@ Common gateway for `yas-expand-from-trigger-key' and
2268 2280 ((eq yas-fallback-behavior 'call-other-command)
2269 2281 (let* ((yas-minor-mode nil)
2270 2282 (yas--direct-keymaps nil)
  2283 + (yas-trigger-key (yas--trigger-key-for-fallback))
2271 2284 (keys-1 (this-command-keys-vector))
2272 2285 (keys-2 (and yas-trigger-key
2273 2286 from-trigger-key-p
@@ -2513,15 +2526,38 @@ neither do the elements of PARENTS."
2513 2526 (defvar yas--guessed-modes nil
2514 2527 "List of guessed modes supporting `yas-load-snippet-buffer'.")
2515 2528
2516   -(defun yas-load-snippet-buffer (&optional kill)
2517   - "Parse and load current buffer's snippet definition.
2518   -
2519   -With optional prefix argument KILL quit the window and buffer."
2520   - (interactive "P")
  2529 +(defun yas--read-table ()
  2530 + "Ask user for a snippet table, help with some guessing."
  2531 + (let ((prompt (if (and (featurep 'ido)
  2532 + ido-mode)
  2533 + 'ido-completing-read 'completing-read)))
  2534 + (unless yas--guessed-modes
  2535 + (set (make-local-variable 'yas--guessed-modes)
  2536 + (or (yas--compute-major-mode-and-parents buffer-file-name))))
  2537 + (intern
  2538 + (funcall prompt (format "Choose or enter a table (yas guesses %s): "
  2539 + (if yas--guessed-modes
  2540 + (first yas--guessed-modes)
  2541 + "nothing"))
  2542 + (mapcar #'symbol-name yas--guessed-modes)
  2543 + nil
  2544 + nil
  2545 + nil
  2546 + nil
  2547 + (if (first yas--guessed-modes)
  2548 + (symbol-name (first yas--guessed-modes)))))))
  2549 +
  2550 +(defun yas-load-snippet-buffer (table &optional interactive)
  2551 + "Parse and load current buffer's snippet definition into TABLE.
  2552 +
  2553 +TABLE is a symbol naming a passed to `yas--table-get-create'.
  2554 +
  2555 +When called interactively, prompt for the table name and
  2556 +whether (and where) to save the snippet, then quit the window."
  2557 + (interactive (list (yas--read-table) t))
2521 2558 (cond
2522   - ;; We have `yas--editing-template', this buffer's
2523   - ;; content comes from a template which is already loaded and
2524   - ;; neatly positioned,...
  2559 + ;; We have `yas--editing-template', this buffer's content comes from a
  2560 + ;; template which is already loaded and neatly positioned,...
2525 2561 ;;
2526 2562 (yas--editing-template
2527 2563 (yas-define-snippets-1 (yas--parse-template (yas--template-file yas--editing-template))
@@ -2532,57 +2568,40 @@ With optional prefix argument KILL quit the window and buffer."
2532 2568 (t
2533 2569 (unless yas--guessed-modes
2534 2570 (set (make-local-variable 'yas--guessed-modes) (or (yas--compute-major-mode-and-parents buffer-file-name))))
2535   - (let* ((prompt (if (and (featurep 'ido)
2536   - ido-mode)
2537   - 'ido-completing-read 'completing-read))
2538   - (table (yas--table-get-create
2539   - (intern
2540   - (funcall prompt (format "Choose or enter a table (yas guesses %s): "
2541   - (if yas--guessed-modes
2542   - (first yas--guessed-modes)
2543   - "nothing"))
2544   - (mapcar #'symbol-name yas--guessed-modes)
2545   - nil
2546   - nil
2547   - nil
2548   - nil
2549   - (if (first yas--guessed-modes)
2550   - (symbol-name (first yas--guessed-modes))))))))
  2571 + (let* ((table (yas--table-get-create table)))
2551 2572 (set (make-local-variable 'yas--editing-template)
2552 2573 (yas-define-snippets-1 (yas--parse-template buffer-file-name)
2553 2574 table)))))
2554   - ;; Now, offer to save this iff:
2555   - ;;
2556   - ;; 1) `yas-snippet-dirs' is a list and its first element does not
2557   - ;; match this template's file (i.e. this is a library snippet, not
2558   - ;; a user snippet) OR
2559   - ;;
2560   - ;; 2) yas--editing-template comes from a file that we cannot write to...
2561   - ;;
2562   - (when (or (not (yas--template-file yas--editing-template))
2563   - (not (file-writable-p (yas--template-file yas--editing-template)))
2564   - (and (listp yas-snippet-dirs)
2565   - (second yas-snippet-dirs)
2566   - (not (string-match (expand-file-name (first yas-snippet-dirs))
2567   - (yas--template-file yas--editing-template)))))
2568   -
2569   - (when (y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? "))
2570   - (let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
2571   - (chosen (and option
2572   - (yas--make-directory-maybe option))))
2573   - (when chosen
2574   - (let ((default-file-name (or (and (yas--template-file yas--editing-template)
2575   - (file-name-nondirectory (yas--template-file yas--editing-template)))
2576   - (yas--template-name yas--editing-template))))
2577   - (write-file (concat chosen "/"
2578   - (read-from-minibuffer (format "File name to create in %s? " chosen)
2579   - default-file-name)))
2580   - (setf (yas--template-file yas--editing-template) buffer-file-name))))))
2581   - (when kill
2582   - (quit-window kill))
2583   - (yas--message 3 "Snippet \"%s\" loaded for %s."
2584   - (yas--template-name yas--editing-template)
2585   - (yas--table-name (yas--template-table yas--editing-template))))
  2575 +
  2576 + (when (and interactive
  2577 + (or
  2578 + ;; Only offer to save this if it looks like a library or new
  2579 + ;; snippet (loaded from elisp, from a dir in `yas-snippet-dirs'
  2580 + ;; which is not the first, or from an unwritable file)
  2581 + ;;
  2582 + (not (yas--template-file yas--editing-template))
  2583 + (not (file-writable-p (yas--template-file yas--editing-template)))
  2584 + (and (listp yas-snippet-dirs)
  2585 + (second yas-snippet-dirs)
  2586 + (not (string-match (expand-file-name (first yas-snippet-dirs))
  2587 + (yas--template-file yas--editing-template)))))
  2588 + (y-or-n-p (yas--format "Looks like a library or new snippet. Save to new file? ")))
  2589 + (let* ((option (first (yas--guess-snippet-directories (yas--template-table yas--editing-template))))
  2590 + (chosen (and option
  2591 + (yas--make-directory-maybe option))))
  2592 + (when chosen
  2593 + (let ((default-file-name (or (and (yas--template-file yas--editing-template)
  2594 + (file-name-nondirectory (yas--template-file yas--editing-template)))
  2595 + (yas--template-name yas--editing-template))))
  2596 + (write-file (concat chosen "/"
  2597 + (read-from-minibuffer (format "File name to create in %s? " chosen)
  2598 + default-file-name)))
  2599 + (setf (yas--template-file yas--editing-template) buffer-file-name)))))
  2600 + (when interactive
  2601 + (quit-window interactive)
  2602 + (yas--message 3 "Snippet \"%s\" loaded for %s."
  2603 + (yas--template-name yas--editing-template)
  2604 + (yas--table-name (yas--template-table yas--editing-template)))))
2586 2605
2587 2606
2588 2607 (defun yas-tryout-snippet (&optional debug)
@@ -4216,7 +4235,7 @@ When multiple expressions are found, only the last one counts."
4216 4235 (let ((fallback-description
4217 4236 (cond ((eq yas-fallback-behavior 'call-other-command)
4218 4237 (let* ((yas-minor-mode nil)
4219   - (fallback (key-binding (read-kbd-macro yas-trigger-key))))
  4238 + (fallback (key-binding (read-kbd-macro (yas--trigger-key-for-fallback)))))
4220 4239 (or (and fallback
4221 4240 (format " call command `%s'." (pp-to-string fallback)))
4222 4241 " do nothing.")))

0 comments on commit f28a3df

Please sign in to comment.
Something went wrong with that request. Please try again.