From 2a80d6bc9438212c8416b53017478aaec0697dde Mon Sep 17 00:00:00 2001
From: Phil Hagelberg <phil@hagelb.org>
Date: Fri, 27 Aug 2021 12:41:27 -0700
Subject: [PATCH fennel] Provide raw chars to repl commands.

This does not fix the underlying problem since the repl commands still
must read one form at a time rather than reading a line at a time, but
it at least gives access to the raw chars behind the form.

Fixes https://todo.sr.ht/~technomancy/fennel/76
---
 src/fennel/repl.fnl | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/fennel/repl.fnl b/src/fennel/repl.fnl
index 118194a..f228bfc 100644
--- a/src/fennel/repl.fnl
+++ b/src/fennel/repl.fnl
@@ -8,6 +8,7 @@
 (local parser (require :fennel.parser))
 (local compiler (require :fennel.compiler))
 (local specials (require :fennel.specials))
+(local unpack (or table.unpack _G.unpack))
 
 (fn default-read-chunk [parser-state]
   (io.write (if (< 0 parser-state.stack-size) ".." ">> "))
@@ -169,11 +170,14 @@ For more information about the language, see https://fennel-lang.org/reference")
 (compiler.metadata:set commands.reset :fnl/docstring
                        "Erase all repl-local scope.")
 
-(fn commands.complete [env read on-values on-error scope]
-  (run-command read on-error #(on-values (completer env scope (tostring $)))))
+(fn commands.complete [env read on-values on-error scope chars]
+  (run-command read on-error
+               #(on-values (completer env scope (-> (string.char (unpack chars))
+                                                    (: :gsub ",complete +" "")
+                                                    (: :sub 1 -2))))))
 
 (compiler.metadata:set commands.complete :fnl/docstring
-                       "Print all possible completions for a given input.")
+                       "Print all possible completions for a given input symbol.")
 
 (fn apropos* [pattern module prefix seen names]
   ;; package.loaded can contain modules with dots in the names.  Such
@@ -255,11 +259,11 @@ For more information about the language, see https://fennel-lang.org/reference")
         (match (name:match "^repl%-command%-(.*)")
           cmd-name (tset commands cmd-name (or (. commands cmd-name) f)))))))
 
-(fn run-command [input read loop env on-values on-error scope]
+(fn run-command-loop [input read loop env on-values on-error scope chars]
   (load-plugin-commands)
   (let [command-name (input:match ",([^%s/]+)")]
     (match (. commands command-name)
-      command (command env read on-values on-error scope)
+      command (command env read on-values on-error scope chars)
       _ (when (not= :exit command-name)
           (on-values ["Unknown command" command-name])))
     (when (not= :exit command-name)
@@ -306,7 +310,7 @@ For more information about the language, see https://fennel-lang.org/reference")
       (each [k (pairs chars)]
         (tset chars k nil))
       (let [(ok parse-ok? x) (pcall read)
-            src-string (string.char ((or table.unpack _G.unpack) chars))]
+            src-string (string.char (unpack chars))]
         (reset)
         (set utils.root.options opts)
         (if (not ok)
@@ -315,9 +319,11 @@ For more information about the language, see https://fennel-lang.org/reference")
               (clear-stream)
               (loop))
             (command? src-string)
-            (run-command src-string read loop env on-values on-error scope)
+            (run-command-loop src-string read loop env on-values on-error
+                              scope chars)
             (when parse-ok? ; if this is false, we got eof
               (match (pcall compiler.compile x (doto opts
+                                                 (tset :env env)
                                                  (tset :source src-string)
                                                  (tset :scope scope)))
                 (false msg) (do
-- 
2.33.0

Generated by Phil Hagelberg using scpaste at Fri Aug 27 12:41:41 2021. PDT. (original)