Common Lisp: Add basic completion for toplevel symbols to the REPL
[jackhill/mal.git] / common-lisp / src / utils.lisp
index 8845f20..95eadf5 100644 (file)
@@ -5,7 +5,8 @@
            :getenv
            :read-file-string
            :raw-command-line-arguments
-           :listify))
+           :listify
+           :common-prefix))
 
 (in-package :utils)
 
@@ -27,3 +28,15 @@ is replaced with replacement."
 (defun listify (sequence)
   "Convert a sequence to a list"
   (map 'list #'identity sequence))
+
+(defun common-prefix (&rest strings)
+  (if (not strings)
+      ""
+      (let* ((char-lists (mapcar (lambda (string) (coerce string 'list)) strings))
+             (char-tuples (apply #'mapcar #'list char-lists))
+             (count 0))
+        (loop for char-tuple in char-tuples
+           while (every (lambda (char) (equal char (car char-tuple))) char-tuple)
+           do (incf count))
+
+        (subseq (car strings) 0 count))))