Skip to content

Instantly share code, notes, and snippets.

@MattRoelle
Last active June 2, 2022 17:00
Show Gist options
  • Save MattRoelle/dab3cca3cb5ed4e8cffe3c92fe44c11c to your computer and use it in GitHub Desktop.
Save MattRoelle/dab3cca3cb5ed4e8cffe3c92fe44c11c to your computer and use it in GitHub Desktop.
(local editor (require :polywell))
(local fennel (require :polywell.lib.fennel))
(local fmt (require :polywell.lib.fnlfmt))
(local completion (require :polywell.completion))
;; from here -- https://github.com/oakmac/parinfer-lua
(local parinfer (require :lib.parinfer))
(local inspect (require :lib.inspect))
(local lume (require :lib.lume))
;(print "p" parinfer)
(fn switch-to-insert []
(editor.block-next-textinput)
(editor.activate-mode "vi-insert"))
(fn switch-to-normal []
(editor.cmd.no-mark)
(editor.activate-mode "vi-normal"))
(fn switch-to-visual []
(editor.cmd.mark)
(editor.activate-mode "vi-visual"))
(local keywords ["let" "set" "tset" "fn" "lambda" "λ" "require" "if" "when"
"do" "block" "true" "false" "nil" "values" "pack" "for" "each"
"collect" "icollect" "accumulate" "macros"
"local" "partial" "and" "or" "not" "special" "macro"])
(set keywords.comment-pattern ";")
(fn completer [input]
(completion.for (completion.add-prefixes _G input []) input))
(fn indentation [lines prev-line-num]
(let [prev-line (. lines prev-line-num)]
(- (length prev-line) (length (lume.ltrim prev-line)))))
(fn apply-parinfer []
(let [input (editor.get-buffer-content)
output (parinfer.smartMode input)]
(if output.error
(editor.echo (.. "Parinfer Error: " output.error))
(editor.set-buffer-content output.text))))
(fn handle-vim-command [cmd]
(match cmd
"w" (do (editor.cmd.save) (editor.echo "Saved"))
(editor.echo "Invalid Command")))
(fn vim-command-prompt []
(editor.read-line ":" handle-vim-command))
(editor.add-mode
{:name "vi-insert"
:parent "edit"
:props {:on-change
#(do
(editor.colorize keywords))
;(apply-parinfer))
: indentation
: completer}
:map {:escape switch-to-normal
"tab" editor.cmd.complete}
:ctrl { "j" switch-to-normal}})
(fn repleval [input]
(fennel.eval input {:onValues editor.print
:pp (fn [x] (fennel.view x { :depth 3}))
:onError editor.print
:env (setmetatable {:print editor.print } { :__index _G})
:moduleName "polywell.lib.fennel"}))
(fn eval-buffer []
(repleval (editor.get-buffer-content)))
(editor.add-mode
{:name "vi-normal"
:activate-patterns [".*fnl$"]
:props {:on-change (partial editor.colorize keywords)
:activate (partial editor.colorize keywords)}
:parent "base"
:block-input? true
:ctrl {"f" apply-parinfer
"e" {:map {"b" eval-buffer}}}
:shift {"a" #(do (editor.cmd.end-of-line) (switch-to-insert))
"i" #(do (editor.cmd.beginning-of-line) (switch-to-insert))
"4" editor.cmd.end-of-line
"6" editor.cmd.beginning-of-line
";" vim-command-prompt
"v" #(do (editor.cmd.end-of-line)
(switch-to-visual)
(editor.cmd.beginning-of-line))
"p" #(do (editor.cmd.end-of-line)
(editor.cmd.next-line)
(editor.cmd.yank)
(editor.cmd.end-of-line)
(editor.cmd.newline))
"g" editor.cmd.beginning-of-buffer}
:map {"h" editor.cmd.backward-char
"l" editor.cmd.forward-char
"j" editor.cmd.next-line
"k" editor.cmd.prev-line
"w" editor.cmd.forward-word
"d" {:map {"d" #(do (editor.cmd.beginning-of-line) (editor.cmd.kill-line) (editor.cmd.delete-forwards))}}
"b" editor.cmd.backward-word
"p" editor.cmd.yank
"u" editor.cmd.undo
"r" editor.cmd.replace
"x" editor.cmd.delete-forwards
"v" switch-to-visual
"/" editor.cmd.search
"backspace" editor.cmd.delete-backwards
"i" switch-to-insert
"g" { :map { "g" editor.cmd.end-of-buffer}}}})
(editor.add-mode
{:name "vi-visual"
:parent "vi-normal"
:block-input? true
:map {"d" #(do (editor.cmd.kill-region) (switch-to-normal))
"escape" switch-to-normal}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment