branch: elpa/with-simulated-input
commit 662055c0a5f17819b992bd8d08c731501a7d20d0
Author: Ryan C. Thompson <[email protected]>
Commit: Ryan C. Thompson <[email protected]>

    Handle non-lexical contexts as well as lexical
    
    Previously, using "with-simulated-input" with "lexical-binding" set to
    nil would throw an error.
---
 tests/test-with-simulated-input.el | 12 ++++++++++++
 with-simulated-input.el            | 10 +++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/tests/test-with-simulated-input.el 
b/tests/test-with-simulated-input.el
index a2d9794e7e..63a854db7d 100644
--- a/tests/test-with-simulated-input.el
+++ b/tests/test-with-simulated-input.el
@@ -6,6 +6,7 @@
 
 ;; Needs to be dynamically bound
 (defvar mycollection)
+(defvar my-non-lexical-var)
 
 (describe "`with-simulated-input'"
 
@@ -114,6 +115,17 @@
         (expect my-lexical-var
                 :to-be-truthy)))
 
+    (it "should work in a non-lexical environment"
+      (let ((my-non-lexical-var nil))
+        (eval
+         '(with-simulated-input '("hello"
+                                  (setq my-non-lexical-var t)
+                                  "RET")
+            (read-string "Enter a string: "))
+         nil)
+        (expect my-non-lexical-var
+                :to-be-truthy)))
+
     (it "should allow interpolation of variables into KEYS"
       (let ((my-key-sequence "hello")
             (my-lisp-form '(insert " world")))
diff --git a/with-simulated-input.el b/with-simulated-input.el
index 92e692452a..097e968fec 100644
--- a/with-simulated-input.el
+++ b/with-simulated-input.el
@@ -104,6 +104,8 @@ to check.
 (defmacro wsi-current-lexical-environment ()
   "Return the current lexical environment.
 
+If `lexical-binding' is not enabled, return nil.
+
 This macro expands to a lisp form that evaluates to the current
 lexical environment. It works by creating a closure and then
 extracting and returning its lexical environment.
@@ -111,11 +113,13 @@ extracting and returning its lexical environment.
 This can be used to manually construct closures in that
 environment."
   `(let ((temp-closure (lambda () t)))
-     (cl-assert (eq (car temp-closure) 'closure) t)
-     (cadr temp-closure)))
+     (when (eq (car temp-closure) 'closure)
+       (cadr temp-closure))))
 
 (defun wsi-make-closure (expr env)
-  `(closure ,env () ,expr))
+  (if env
+      `(closure ,env () ,expr)
+    `(lambda () ,expr)))
 
 ;;;###autoload
 (defmacro with-simulated-input (keys &rest body)

Reply via email to