From 4610711eaa7e1fd145fb6ab7ce85f005e5d821be Mon Sep 17 00:00:00 2001 From: Walter Leibbrandt Date: Sat, 5 Jun 2021 21:34:40 +0200 Subject: [PATCH] Initial implementation --- README.md | 38 +++++++++++++++ UNLICENSE | 25 ++++++++++ fnl/conjure-macroexpand/main.fnl | 49 ++++++++++++++++++- lua/conjure-macroexpand/main.lua | 80 ++++++++++++++++++++++++++++++-- 4 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 README.md create mode 100644 UNLICENSE diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b6f5b8 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# ConjureMacroexpand + +Adds commands `ConjureMacroexpand`, `ConjureMacroexpand0` and +`ConjureMacroexpand1` that will macro-expand the form under the cursor, using +[Conjure](https://github.com/Olical/conjure)'s REPL connection. + +This is the one bit of functionality missing from Conjure that kept +[vim-fireplace](https://github.com/tpope/vim-fireplace/) in my Neovim config. + +## Installation + +Using [vim-plug](https://github.com/junegunn/vim-plug): + +```viml +Plug 'walterl/conjure-macroexpand' +``` + +## Mappings + +* `cm` - Calls `clojure.walk/macroexpand-all` on the form under the cursor. +* `c0` - Calls `clojure.core/macroexpand` on the form under the cursor. +* `c1` - Calls `clojure.core/macroexpand-1` on the form under the cursor. + +## Configuration + +Disable mappings: + +```viml +set g:conjure_macroexpand_disable_mappings = 1 +``` + +## Unlicenced (just like Conjure) + +Find the full [unlicense](http://unlicense.org/) in the `UNLICENSE` file, but here's a snippet. + +> This is free and unencumbered software released into the public domain. +> +> Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..a84c395 --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,25 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to + diff --git a/fnl/conjure-macroexpand/main.fnl b/fnl/conjure-macroexpand/main.fnl index bebf500..d248b4d 100644 --- a/fnl/conjure-macroexpand/main.fnl +++ b/fnl/conjure-macroexpand/main.fnl @@ -1,4 +1,49 @@ -(module conjure-macroexpand.main) +(module conjure-macroexpand.main + {require {a aniseed.core + nvim aniseed.nvim + bridge conjure.bridge + client conjure.client + eval conjure.eval + extract conjure.extract + log conjure.log + mapping conjure.mapping}}) + +;; Adapted from conjure.eval/current-form +(defn- current-form [] + (let [form (extract.form {})] + (when form + (let [{: content} form] + content)))) + +(defn- clj-client [f args] + (client.with-filetype "clojure" f args)) + +(defn- output-expanded [orig] + (fn [r] + (log.append [(.. "; " orig) r] {:break? true}))) + +(defn conjure-macroexpand [expand-cmd] + (let [form (current-form) + me-form (.. "(" (or expand-cmd "clojure.walk/macroexpand-all") " '" form ")")] + (clj-client eval.eval-str + {:origin :conjure-macroexpand + :code me-form + :passive? true + :on-result (output-expanded me-form)}))) (defn init [] - (print "Hello, World!")) + (nvim.ex.command_ + "ConjureMacroexpand" + (bridge.viml->lua :conjure-macroexpand.main :conjure-macroexpand)) + (nvim.ex.command_ + "ConjureMacroexpand0" + (bridge.viml->lua :conjure-macroexpand.main :conjure-macroexpand {:args "\"clojure.core/macroexpand\""})) + (nvim.ex.command_ + "ConjureMacroexpand1" + (bridge.viml->lua :conjure-macroexpand.main :conjure-macroexpand {:args "\"clojure.core/macroexpand-1\""})) + + (when (or (not nvim.g.conjure_macroexpand_disable_mappings) + (= 0 nvim.g.conjure_macroexpand_disable_mappings)) + (mapping.buf :n nil "cm" ":ConjureMacroexpand") + (mapping.buf :n nil "c0" ":ConjureMacroexpand0") + (mapping.buf :n nil "c1" ":ConjureMacroexpand1"))) diff --git a/lua/conjure-macroexpand/main.lua b/lua/conjure-macroexpand/main.lua index 3a1c2b5..e85a328 100644 --- a/lua/conjure-macroexpand/main.lua +++ b/lua/conjure-macroexpand/main.lua @@ -25,27 +25,101 @@ autoload = _1_ local function _2_(...) local ok_3f_0_, val_0_ = nil, nil local function _2_() - return {} + return {require("conjure-macroexpand.aniseed.core"), require("conjure.bridge"), require("conjure.client"), require("conjure.eval"), require("conjure.extract"), require("conjure.log"), require("conjure.mapping"), require("conjure-macroexpand.aniseed.nvim")} end ok_3f_0_, val_0_ = pcall(_2_) if ok_3f_0_ then - _0_["aniseed/local-fns"] = {} + _0_["aniseed/local-fns"] = {require = {a = "conjure-macroexpand.aniseed.core", bridge = "conjure.bridge", client = "conjure.client", eval = "conjure.eval", extract = "conjure.extract", log = "conjure.log", mapping = "conjure.mapping", nvim = "conjure-macroexpand.aniseed.nvim"}} return val_0_ else return print(val_0_) end end local _local_0_ = _2_(...) +local a = _local_0_[1] +local bridge = _local_0_[2] +local client = _local_0_[3] +local eval = _local_0_[4] +local extract = _local_0_[5] +local log = _local_0_[6] +local mapping = _local_0_[7] +local nvim = _local_0_[8] local _2amodule_2a = _0_ local _2amodule_name_2a = "conjure-macroexpand.main" do local _ = ({nil, _0_, nil, {{}, nil, nil, nil}})[2] end +local current_form +do + local v_0_ + local function current_form0() + local form = extract.form({}) + if form then + local _let_0_ = form + local content = _let_0_["content"] + return content + end + end + v_0_ = current_form0 + local t_0_ = (_0_)["aniseed/locals"] + t_0_["current-form"] = v_0_ + current_form = v_0_ +end +local clj_client +do + local v_0_ + local function clj_client0(f, args) + return client["with-filetype"]("clojure", f, args) + end + v_0_ = clj_client0 + local t_0_ = (_0_)["aniseed/locals"] + t_0_["clj-client"] = v_0_ + clj_client = v_0_ +end +local output_expanded +do + local v_0_ + local function output_expanded0(orig) + local function _3_(r) + return log.append({("; " .. orig), r}, {["break?"] = true}) + end + return _3_ + end + v_0_ = output_expanded0 + local t_0_ = (_0_)["aniseed/locals"] + t_0_["output-expanded"] = v_0_ + output_expanded = v_0_ +end +local conjure_macroexpand +do + local v_0_ + do + local v_0_0 + local function conjure_macroexpand0(expand_cmd) + local form = current_form() + local me_form = ("(" .. (expand_cmd or "clojure.walk/macroexpand-all") .. " '" .. form .. ")") + return clj_client(eval["eval-str"], {["on-result"] = output_expanded(me_form), ["passive?"] = true, code = me_form, origin = "conjure-macroexpand"}) + end + v_0_0 = conjure_macroexpand0 + _0_["conjure-macroexpand"] = v_0_0 + v_0_ = v_0_0 + end + local t_0_ = (_0_)["aniseed/locals"] + t_0_["conjure-macroexpand"] = v_0_ + conjure_macroexpand = v_0_ +end local init do local v_0_ do local v_0_0 local function init0() - return print("Hello, World!") + nvim.ex.command_("ConjureMacroexpand", bridge["viml->lua"]("conjure-macroexpand.main", "conjure-macroexpand")) + nvim.ex.command_("ConjureMacroexpand0", bridge["viml->lua"]("conjure-macroexpand.main", "conjure-macroexpand", {args = "\"clojure.core/macroexpand\""})) + nvim.ex.command_("ConjureMacroexpand1", bridge["viml->lua"]("conjure-macroexpand.main", "conjure-macroexpand", {args = "\"clojure.core/macroexpand-1\""})) + if (not nvim.g.conjure_macroexpand_disable_mappings or (0 == nvim.g.conjure_macroexpand_disable_mappings)) then + mapping.buf("n", nil, "cm", ":ConjureMacroexpand") + mapping.buf("n", nil, "c0", ":ConjureMacroexpand0") + return mapping.buf("n", nil, "c1", ":ConjureMacroexpand1") + end end v_0_0 = init0 _0_["init"] = v_0_0