editor.l 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. (
  2. (def buffers (list
  3. (list (alloc-str 1024) "main")
  4. (list (alloc-str 1024) "mini")
  5. (list (alloc-str 1024) "scratch")
  6. ))
  7. ; buf-id x y w h cur type (0=text,1=cmd,2=image)
  8. (def views (list
  9. (list 0 0 0 800 800 0 0)
  10. (list 1 0 900 800 100 0 1)
  11. ))
  12. (def append-mode 1)
  13. (def focused-view-id 1)
  14. (def buf-str (fn id (item (item buffers id) 0)))
  15. (def set-buf-str (fn id new-str (def buffers (replace-item buffers id (replace-item (item buffers id) 0 new-str)))))
  16. (def view-buf-id (fn id (item (item views id) 0)))
  17. (def view-x (fn id (item (item views id) 1)))
  18. (def view-y (fn id (item (item views id) 2)))
  19. (def view-w (fn id (item (item views id) 3)))
  20. (def view-h (fn id (item (item views id) 4)))
  21. (def view-cursor (fn id (item (item views id) 5)))
  22. (def view-type (fn id (item (item views id) 6)))
  23. (def set-view-buf-id (fn id buf-id (def views (replace-item views id (replace-item (item views id) 0 buf-id)))))
  24. (def set-view-x (fn id x (def views (replace-item views id (replace-item (item views id) 1 x)))))
  25. (def set-view-y (fn id y (def views (replace-item views id (replace-item (item views id) 2 y)))))
  26. (def set-view-w (fn id w (def views (replace-item views id (replace-item (item views id) 3 w)))))
  27. (def set-view-h (fn id h (def views (replace-item views id (replace-item (item views id) 4 h)))))
  28. (def set-view-cursor (fn id new-cursor (def views (replace-item views id (replace-item (item views id) 5 new-cursor)))))
  29. (def set-view-type (fn id new-type (def views (replace-item views id (replace-item (item views id) 6 new-type)))))
  30. (def padding 20)
  31. (def new-buffer (fn (do
  32. (def buf (list (alloc-str 1024) "untitled"))
  33. (def buffers (reverse (cons buf buffers)))
  34. buf
  35. )))
  36. (def new-view (fn buf-id x y w h type (do
  37. (def view (list buf-id x y w h 0 type))
  38. (def views (reverse (cons view views)))
  39. view
  40. )))
  41. (def uinsert (fn buf-id pos kchr (do
  42. (set-buf-str buf-id (concat (buf-str buf-id) " "))
  43. (def buf (buf-str buf-id))
  44. (ucopy buf pos (+ pos 1) (- (usize buf) (+ pos 0)))
  45. (uput buf pos kchr)
  46. 0
  47. )))
  48. (def scroll-line (fn buf-id (do
  49. (def break-at (+ 1 (ufind-next str 0xa 0)))
  50. (set-buf-str id (substr str break-at (usize str)))
  51. )))
  52. (def text-view-render (fn view-id (do
  53. (def buf-id (view-buf-id view-id))
  54. (def rendered-upto
  55. (blit-string unifont
  56. (buf-str buf-id)
  57. (if (= focused-view-id view-id) (view-cursor view-id) -1)
  58. (+ padding (view-x view-id)) (+ padding (view-y view-id)) (view-w view-id) (view-h view-id) 0xffffff))
  59. (def str (buf-str buf-id))
  60. (def clipped (- (usize str) rendered-upto))
  61. (if (gt clipped 0) (scroll-line buf-id))
  62. )))
  63. (def image-view-render2 (fn view-id (do
  64. (def buf-id (view-buf-id view-id))
  65. (blit (buf-str buf-id) (+ padding (view-x view-id)) (+ padding (view-y view-id)) (view-w view-id) (view-h view-id))
  66. )))
  67. (def pixel-size 64)
  68. (def tbuf "")
  69. (def image-view-render (fn view-id (do
  70. (def buf-id (view-buf-id view-id))
  71. (def pixels (buf-str buf-id))
  72. (def vw (view-w view-id))
  73. (def rx 0)
  74. (def ry 0)
  75. (def vx (+ padding (view-x view-id)))
  76. (def vy (+ padding (view-y view-id)))
  77. (def cursor (view-cursor view-id)) ; FIXME clobbers i
  78. (def i 0)
  79. (while (lt i (size pixels)) (do
  80. (def color (+ (+ (* (get pixels (+ i 2)) 65536) (* (get pixels (+ i 1)) 256)) (get pixels i)))
  81. (rectfill (+ rx vx) (+ ry vy) pixel-size pixel-size color)
  82. (if (= cursor i) (do
  83. (rectfill (+ rx vx) (+ (* 3 (/ pixel-size 4)) (+ ry vy)) pixel-size (/ pixel-size 4) 0xffffff)
  84. (rectfill (+ rx vx) (+ (* 3 (/ pixel-size 4)) (+ ry vy)) pixel-size (/ pixel-size 5) 0x000000)
  85. ))
  86. ;(write tbuf (cons "i" i))
  87. ;(blit-string unifont tbuf -1 (+ rx vx) (+ ry vy) 100 16 0xffffff)
  88. (def rx (+ rx pixel-size))
  89. (if (gt rx vw) (do
  90. (def rx 0)
  91. (def ry (+ ry pixel-size))))
  92. (def i (+ i 3))
  93. ))
  94. 0
  95. )))
  96. (def view-render (fn view-id (do
  97. (def t (view-type view-id))
  98. (if (= t 2) (image-view-render view-id))
  99. (if (= t 3) (image-view-render2 view-id))
  100. (if (lt t 2) (text-view-render view-id))
  101. )))
  102. (def buf-append (fn id str (do
  103. (set-buf-str id (concat (buf-str id) (concat str [0a])))
  104. )))
  105. (def word-at (fn buf pos (do
  106. (def from (ufind-prev-ws buf pos))
  107. (def to (ufind-next-ws buf pos))
  108. (substr buf from (- to from))
  109. )))
  110. (def backspace (fn vid (if (gt (view-cursor vid) 0) (do
  111. (def cursor (view-cursor vid))
  112. (uremove (buf-str (view-buf-id vid)) cursor)
  113. (set-view-cursor vid (- cursor 1))
  114. ))))
  115. (def cursor-left (fn vid (do
  116. (def bufid (view-buf-id vid))
  117. (def cursor (view-cursor bufid))
  118. (set-view-cursor vid (- cursor 1))
  119. )))
  120. (def cursor-right (fn vid (do
  121. (def bufid (view-buf-id vid))
  122. (def cursor (view-cursor bufid))
  123. (set-view-cursor vid (+ cursor 1))
  124. )))
  125. (def cursor-up (fn vid (do
  126. (def bufid (view-buf-id vid))
  127. (def cursor (ufind-prev (buf-str bufid) 10 (- (view-cursor vid) 1)))
  128. (set-view-cursor vid cursor)
  129. )))
  130. (def cursor-down (fn vid (do
  131. (def bufid (view-buf-id vid))
  132. (def cursor (ufind-next (buf-str bufid) 10 (+ (view-cursor vid) 1)))
  133. (set-view-cursor vid cursor)
  134. )))
  135. (def eval-tmp-buf (alloc-str 1024))
  136. (def palette (quote (
  137. [000000]
  138. [ffffff]
  139. [68372b]
  140. [70a4b2]
  141. [6f3d86]
  142. [588d43]
  143. [352879]
  144. [b8c76f]
  145. [6f4f25]
  146. [433900]
  147. [9a6759]
  148. [444444]
  149. [6c6c6c]
  150. [9ad284]
  151. [6c5eb5]
  152. [959595]
  153. )))
  154. (def insert-char (fn k (do
  155. (def cursor (view-cursor focused-view-id))
  156. (def buf-id (view-buf-id focused-view-id))
  157. (def type (view-type focused-view-id))
  158. (print (cons "type" type))
  159. (if (= type 2) (do
  160. (print (cons "k" k))
  161. (def rgb (item palette (- k 48)))
  162. (put (buf-str buf-id) cursor (get rgb 0))
  163. (put (buf-str buf-id) (+ cursor 1) (get rgb 1))
  164. (put (buf-str buf-id) (+ cursor 2) (get rgb 2))
  165. (set-view-cursor focused-view-id (+ cursor 3))
  166. )
  167. (do
  168. (uinsert buf-id cursor k)
  169. (set-view-cursor focused-view-id (+ cursor 1))))
  170. )))
  171. (def focus-next-view (fn (do
  172. (def focused-view-id (% (+ focused-view-id 1) (length views)))
  173. (set-view-cursor focused-view-id (usize (buf-str (view-buf-id focused-view-id))))
  174. (print (list "focused view:" focused-view-id (view-cursor focused-view-id)))
  175. )))
  176. ; command handler ----------------------------------------------------------
  177. (def handle-command-key (fn k modif (do
  178. (print (list "key" k "modif" modif))
  179. (if (or (= k 13) (= k 10))
  180. (if (= (view-type focused-view-id) 1)
  181. (do
  182. (def buf-id (view-buf-id focused-view-id))
  183. (print (cons "evaling:" (buf-str buf-id)))
  184. (def eval-tmp (eval (buf-str buf-id)))
  185. (print (cons "eval-tmp:" eval-tmp))
  186. (if append-mode (do
  187. (write eval-tmp-buf eval-tmp)
  188. (buf-append 0 eval-tmp-buf)))
  189. 0)
  190. (insert-char k))
  191. )
  192. (if (or (= k 9) (= k 134))
  193. (if modif
  194. 0
  195. ; (plumb (word-at (buf-str (view-buf-id focused-view-id)) (view-cursor focused-view-id)))
  196. (focus-next-view)))
  197. (def j 2)
  198. (if (= (view-type focused-view-id) 2) (def j 4))
  199. (while (def j (- j 1)) (do
  200. (if (= k 127) (backspace focused-view-id))
  201. (if (= k 130) (cursor-left focused-view-id))
  202. (if (= k 132) (cursor-up focused-view-id))
  203. (if (= k 133) (cursor-down focused-view-id))
  204. (if (= k 131) (cursor-right focused-view-id))
  205. (print (cons "cursor" (view-cursor focused-view-id)))
  206. ))
  207. 0
  208. )))
  209. (def handle-editor-key (fn k modif (do
  210. (if (and (not (= k 27)) (and (gt k 13) (lt k 127)))
  211. (insert-char k)
  212. (handle-command-key k modif)
  213. )
  214. 0
  215. )))
  216. (def list-ids (fn lst (do
  217. (def i -1)
  218. (map (fn b (def i (+ i 1))) lst)
  219. )))
  220. (def render-all (fn (do
  221. (map view-render (list-ids views))
  222. )))
  223. (def gfx (fn (do
  224. (def append-mode 0)
  225. (set-buf-str 0 (alloc (* 3 (* 16 16))))
  226. (set-view-type 0 2)
  227. (set-view-cursor 0 0)
  228. (set-view-w 0 512)
  229. (set-view-h 0 512)
  230. (def pixel-size 32)
  231. )))
  232. ; welcome to bomber jacket OS (arm7/32bit) ☕ ☕ ☕ -------------------------
  233. (def loadimage (fn (do
  234. (set-view-type 0 3)
  235. (set-view-w 0 560)
  236. (def cat (load "grumpy.data"))
  237. (print "cat loaded!")
  238. (blit cat 20 20 560 720)
  239. (flip)
  240. )))
  241. (def main (fn (do
  242. (flip)
  243. (print (list "buffers: " buffers "views: " views))
  244. (print "render-all")
  245. (render-all)
  246. (print "entering while")
  247. (while 1 (do
  248. (def k (inkey 0))
  249. (def modif (inkey 1))
  250. (if k (do
  251. (if (and (lt k 127) keymap) (if modif (def k (get keymap (+ 128 k))) (def k (get keymap k))))
  252. ))
  253. (if (not (= k 0))
  254. (handle-editor-key k modif))
  255. (gc)
  256. (flip)
  257. (render-all)
  258. (udp-poll)
  259. ))
  260. )))
  261. (main)
  262. )