summary refs log tree commit diff
path: root/src/main.janet
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.janet')
-rw-r--r--src/main.janet77
1 files changed, 63 insertions, 14 deletions
diff --git a/src/main.janet b/src/main.janet
index 8ca1215..4034dc9 100644
--- a/src/main.janet
+++ b/src/main.janet
@@ -2,24 +2,73 @@
 # SPDX-FileCopyrightText: 2025 Nguyễn Gia Phong
 # SPDX-License-Identifier: GPL-3.0-or-later
 
+(defn kay/select/move
+  [buf direction]
+  (let [data (buf :data)
+        selection (buf :selection)]
+    (struct ;(kvs buf)
+            :selection (match selection
+                         {:head head :tail tail}
+                         (struct ;(kvs selection)
+                                 :head (direction head data)
+                                 :tail (direction tail data))))))
+
+(defn kay/select/head
+  [buf direction]
+  (let [data (buf :data)
+        selection (buf :selection)]
+    (struct ;(kvs buf)
+            :selection (match selection
+                         {:head head :tail tail}
+                         (struct ;(kvs selection)
+                                 :head (direction head data)
+                                 :tail tail)))))
+
+(defn kay/select/flip
+  [buf]
+  (struct ;(kvs buf)
+          :selection (match (buf :selection)
+                       {:head head :tail tail} {:head tail :tail head})))
+
 (defn handle
-  [event]
+  [event buf]
   (match event
-    [:key-press key] (cond
-                       (:matches key (chr "c") {:ctrl true}) [:quit]
-                       (:matches key (chr "h") {}) (do (:go-up env) [])
-                       (:matches key (chr "j") {}) (do (:go-right env) [])
-                       (:matches key (chr "k") {}) (do (:go-left env) [])
-                       (:matches key (chr "l") {}) (do (:go-down env) [])
-                       [])))
+    {:winsize ws} (do (:resize kay/env ws)
+                      [[] buf])
+    {:key-press key}
+    (cond
+      (:matches key (chr "c") {:ctrl true}) [[:quit] buf]
+      (:matches key (chr "y") {}) (do (:yank kay/env buf)
+                                      [[] buf])
+      (:matches key (chr "i") {}) (do (:paste kay/env)
+                                      [[] buf])
+      (:matches key (chr "p") {}) [[] (struct ;(kvs buf)
+                                              :scroll-row (max 0 (dec (buf :scroll-row))))]
+      (:matches key (chr "n") {}) [[] (struct ;(kvs buf)
+                                              :scroll-row (inc (buf :scroll-row)))]
+      (:matches key (chr "h") {}) [[] (kay/select/move buf :up)]
+      (:matches key (chr "j") {}) [[] (kay/select/move buf :right)]
+      (:matches key (chr "k") {}) [[] (kay/select/move buf :left)]
+      (:matches key (chr "l") {}) [[] (kay/select/move buf :down)]
+      (:matches key (chr "h") {:shift true}) [[] (kay/select/head buf :up)]
+      (:matches key (chr "j") {:shift true}) [[] (kay/select/head buf :right)]
+      (:matches key (chr "k") {:shift true}) [[] (kay/select/head buf :left)]
+      (:matches key (chr "l") {:shift true}) [[] (kay/select/head buf :down)]
+      (:matches key (chr ";") {}) [[] (kay/select/flip buf)]
+      [[] buf])
+    {:paste paste} [[] (:paste kay/env buf paste)]))
 
 (defn run
-  [events]
+  [events buf]
   (if-let [event (array/pop events)]
     (unless (= :quit event)
-      (let [next-events (handle event)]
-        (:render env)
-        (run (array/join events next-events))))
-    (run @[(:next-event env)])))
+      (let [[next-events next-buf] (handle event buf)]
+        (:render kay/env next-buf)
+        (run (array/join events next-events)
+             next-buf)))
+    (run @[(:next-event kay/env)]
+         buf)))
 
-(run @[])
+(with [buf (kay/open (with [f (file/open (string kay/path))]
+                       (buffer (:read f :all))))]
+  (run @[] buf))