[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master ac26024 20/63: Rework error handling
From: |
Noam Postavsky |
Subject: |
[elpa] master ac26024 20/63: Rework error handling |
Date: |
Mon, 17 Jul 2017 22:54:13 -0400 (EDT) |
branch: master
commit ac260248371437ba930b15fcad23fd6c2ada9f6c
Author: João Távora <address@hidden>
Commit: Noam Postavsky <address@hidden>
Rework error handling
* yasnippet.el (yas-good-grace): Add support for `inline' and `hooks'
options.
(yas--eval-for-string): Renamed from `yas--eval-lisp'. Errors are
passed straight through (no rethrow) when `yas-good-grace' is nil.
(yas--handle-error): Removed.
(yas--eval-for-effect): Renamed from `yas--eval-lisp-no-saves'. Use
`yas--safely-run-hook'.
(yas-define-snippets): Update docstring.
(yas-throw): Don't throw, just signal.
(yas--save-backquotes, yas--apply-transform): Use
`yas--eval-for-string'.
(yas--safely-run-hook): Renamed from `yas--safely-run-hooks'. Takes 1
hook function, instead of a hook var; check `yas-good-grace'.
(yas-expand-snippet): Use `yas--eval-for-effect'.
---
yasnippet.el | 91 ++++++++++++++++++++++++++++++++----------------------------
1 file changed, 49 insertions(+), 42 deletions(-)
diff --git a/yasnippet.el b/yasnippet.el
index 644aa90..44545c0 100644
--- a/yasnippet.el
+++ b/yasnippet.el
@@ -340,9 +340,16 @@ per-snippet basis. A value of `cua' is considered
equivalent to
(const cua))) ; backwards compat
(defcustom yas-good-grace t
- "If non-nil, don't raise errors in inline elisp evaluation.
+ "If non-nil, don't raise errors in elisp evaluation.
-An error string \"[yas] error\" is returned instead."
+This affects both the inline elisp in snippets and the hook
+variables such as `yas-after-exit-snippet-hook'.
+
+If this variable's value is `inline', an error string \"[yas]
+error\" is returned instead of raising the error. If this
+variable's value is `hooks', a message is output to according to
+`yas-verbosity-level'. If this variable's value is t, both are
+active."
:type 'boolean)
(defcustom yas-visit-from-menu nil
@@ -1323,33 +1330,27 @@ Returns (TEMPLATES START END). This function respects
;;; Internal functions and macros:
-(defun yas--handle-error (err)
- "Handle error depending on value of `yas-good-grace'."
- (let ((msg (yas--format "elisp error: %s" (error-message-string err))))
- (if yas-good-grace msg
- (error "%s" msg))))
-
-(defun yas--eval-lisp (form)
+(defun yas--eval-for-string (form)
"Evaluate FORM and convert the result to string."
- (let ((retval (catch 'yas--exception
- (condition-case err
- (save-excursion
- (save-restriction
- (save-match-data
- (widen)
- (let ((result (eval form)))
- (when result
- (format "%s" result))))))
- (error (yas--handle-error err))))))
- (when (and (consp retval)
- (eq 'yas--exception (car retval)))
- (error (cdr retval)))
- retval))
-
-(defun yas--eval-lisp-no-saves (form)
- (condition-case err
- (eval form)
- (error (message "%s" (yas--handle-error err)))))
+ (let ((eval-saving-stuff
+ (lambda (form)
+ (save-excursion
+ (save-restriction
+ (save-match-data
+ (widen)
+ (let ((result (eval form)))
+ (when result
+ (format "%s" result)))))))))
+ (if (memq yas-good-grace '(t inline))
+ (condition-case oops
+ (funcall eval-saving-stuff form)
+ (yas--exception (signal 'yas-exception (cdr oops)))
+ (error (cdr oops)))
+ (funcall eval-saving-stuff form))))
+
+(defun yas--eval-for-effect (form)
+ ;; FIXME: simulating lexical-binding.
+ (yas--safely-run-hook `(lambda () ,form)))
(defun yas--read-lisp (string &optional nil-on-error)
"Read STRING as a elisp expression and return it.
@@ -1665,7 +1666,7 @@ this is a snippet or a snippet-command.
CONDITION, EXPAND-ENV and KEYBINDING are Lisp forms, they have
been `yas--read-lisp'-ed and will eventually be
-`yas--eval-lisp'-ed.
+`yas--eval-for-string'-ed.
The remaining elements are strings.
@@ -2855,7 +2856,8 @@ The last element of POSSIBILITIES may be a list of
strings."
(defun yas-throw (text)
"Throw a yas--exception with TEXT as the reason."
- (throw 'yas--exception (cons 'yas--exception text)))
+ (signal 'yas--exception text))
+(put 'yas--exception 'error-conditions '(error yas--exception))
(defun yas-verify-value (possibilities)
"Verify that the current field value is in POSSIBILITIES.
@@ -3020,7 +3022,7 @@ string iff EMPTY-ON-NIL-P is true."
(transformed (and transform
(save-excursion
(goto-char start-point)
- (let ((ret (yas--eval-lisp transform)))
+ (let ((ret (yas--eval-for-string transform)))
(or ret (and empty-on-nil-p "")))))))
transformed))
@@ -3333,11 +3335,16 @@ This renders the snippet as ordinary text."
(yas--maybe-move-to-active-field snippet))
(setq yas--snippets-to-move nil))
-(defun yas--safely-run-hooks (hook-var)
- (condition-case error
- (run-hooks hook-var)
- (error
- (yas--message 2 "%s error: %s" hook-var (error-message-string error)))))
+(defun yas--safely-run-hook (hook)
+ (let ((run-the-hook (lambda (hook) (funcall hook))))
+ (if (memq yas-good-grace '(t hooks))
+ (funcall run-the-hook hook)
+ (condition-case error
+ (funcall run-the-hook hook)
+ (error
+ (yas--message 2 "Error running %s: %s"
+ (if (symbolp hook) hook "a hook")
+ (error-message-string error)))))))
(defun yas--check-commit-snippet ()
@@ -3371,8 +3378,8 @@ If so cleans up the whole snippet up."
nil)))))
(unless (or (null snippets) snippets-left)
(if snippet-exit-transform
- (yas--eval-lisp-no-saves snippet-exit-transform))
- (yas--safely-run-hooks 'yas-after-exit-snippet-hook))))
+ (yas--eval-for-effect snippet-exit-transform))
+ (mapcar #'yas--safely-run-hook yas-after-exit-snippet-hook))))
;; Apropos markers-to-points:
;;
@@ -3648,7 +3655,7 @@ considered when expanding the snippet."
(cond ((listp content)
;; x) This is a snippet-command
;;
- (yas--eval-lisp-no-saves content))
+ (yas--eval-for-effect content))
(t
;; x) This is a snippet-snippet :-)
;;
@@ -4169,9 +4176,9 @@ with their evaluated value into
`yas--backquote-markers-and-strings'."
(delete-region (match-beginning 0) (match-end 0)))
(let ((before-change-functions
(cons detect-change before-change-functions)))
- (setq transformed (yas--eval-lisp (yas--read-lisp
- (yas--restore-escapes
- current-string '(?`))))))
+ (setq transformed (yas--eval-for-string (yas--read-lisp
+ (yas--restore-escapes
+ current-string '(?`))))))
(goto-char (match-beginning 0))
(when transformed
(let ((marker (make-marker))
- [elpa] master cddb826 29/63: Merge: let snippets expand in strings/comments by default, (continued)
- [elpa] master cddb826 29/63: Merge: let snippets expand in strings/comments by default, Noam Postavsky, 2017/07/17
- [elpa] master 462f566 44/63: Don't leave unreadable objects in the undo list, Noam Postavsky, 2017/07/17
- [elpa] master 3949421 41/63: Fix problems with auto-fill-mode interaction, Noam Postavsky, 2017/07/17
- [elpa] master 0d9afb2 31/63: Fix mirror transformation error with expand-env, Noam Postavsky, 2017/07/17
- [elpa] master b4d2f9c 37/63: Update snippets submodule, Noam Postavsky, 2017/07/17
- [elpa] master c5fddf8 34/63: Restore syntax-propertize-function before indenting snippet, Noam Postavsky, 2017/07/17
- [elpa] master 5043a96 38/63: Correctly wrap auto-fill-function when it's toggled on later, Noam Postavsky, 2017/07/17
- [elpa] master 30913fd 35/63: Only update live snippets, Noam Postavsky, 2017/07/17
- [elpa] master 0f6d82f 46/63: * .travis.yml: Use Emacs 25.2, Noam Postavsky, 2017/07/17
- [elpa] master 6102ff5 50/63: Handle undo of snippet 1st line indent properly, Noam Postavsky, 2017/07/17
- [elpa] master ac26024 20/63: Rework error handling,
Noam Postavsky <=
- [elpa] master c3a9447 43/63: Fix condition timestamps in key filter functions, Noam Postavsky, 2017/07/17
- [elpa] master 82818e5 48/63: Don't delete snippets more than once., Noam Postavsky, 2017/07/17
- [elpa] master 63f44b4 33/63: Ensure inhibit-modification-hooks is nil while modifying buffer, Noam Postavsky, 2017/07/17
- [elpa] master 0041efe 17/63: Make snippets work in org source blocks, Noam Postavsky, 2017/07/17
- [elpa] master 47f37f7 42/63: Fix ${0:soon-to-be-deleted} with no other fields, Noam Postavsky, 2017/07/17
- [elpa] master e8ed276 52/63: Make sure tests don't rely on running order, Noam Postavsky, 2017/07/17
- [elpa] master 2a3a0cd 61/63: Update CONTRIBUTING.md information, Noam Postavsky, 2017/07/17
- [elpa] master b0e729b 58/63: Optionally add undo list to debug info, Noam Postavsky, 2017/07/17
- [elpa] master 274406e 55/63: Provide command line interface from yasnippet-debug.el, Noam Postavsky, 2017/07/17
- [elpa] master 6e9b7f9 30/63: Fix running hook variable set as buffer-local, Noam Postavsky, 2017/07/17