From 12c8840a7a5162a57ee9e1c151934451499a9cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chlo=C3=A9=20Vulquin?= Date: Thu, 25 Jul 2024 20:58:06 +0200 Subject: nvim: port markdown quote ftplugin It performs significantly better and I have no idea why. --- dot_config/nvim/ftplugin/markdown_quote.fnl | 57 +++++++++ dot_config/nvim/ftplugin/markdown_quote.lua | 185 +++++++++++++++++----------- 2 files changed, 170 insertions(+), 72 deletions(-) create mode 100644 dot_config/nvim/ftplugin/markdown_quote.fnl (limited to 'dot_config/nvim/ftplugin') diff --git a/dot_config/nvim/ftplugin/markdown_quote.fnl b/dot_config/nvim/ftplugin/markdown_quote.fnl new file mode 100644 index 0000000..a75c966 --- /dev/null +++ b/dot_config/nvim/ftplugin/markdown_quote.fnl @@ -0,0 +1,57 @@ +(local buf 0) ; operate on the current buffer on load + +(fn location [opts] + "Mutate or create ops. + Line1 is the start of the selection, and Line2 is the end." + (let [sl (or (?. opts :line1) (vim.fn.line :v)) + el (or (?. opts :line2) (vim.fn.line :.)) + swap (> sl el)] + (if swap + {:line1 (- 1 el) + :line2 sl} + {:line1 (- 1 sl) + :line2 el}))) + +(fn maplines [f] + "Returns a transducer that will map the selection's lines with f." + (fn [opts] + (let [opts (location opts) + argp #(partial $1 buf opts.line1 opts.line2 true) + setl (argp vim.api.nvim_buf_set_lines) + getl (argp vim.api.nvim_buf_get_lines)] + (setl (icollect [_ v (ipairs (getl))] + (f v)))))) + +(let [+qf #(case $ + ; empty lines don't get quoted + "" $ + + ; unless there's already whitespace or an existing quote in front, + ; we'll want to prepend ">" *and* a space + ; this is the "unless" case + (where s (s:match "^[%s>]")) + (.. :> s) + + s + (.. "> " s)) + -qf #(case $ + ; if the line starts with a "> " we remove both + ; since it's no longer a quote at all + (where s (s:match "^> ")) + (s:sub 3) + + ; else, we're just removing the ">" (a single quote level) + (where s (s:match :^>)) + (s:sub 2) + + _ $) + +quote (maplines +qf) + -quote (maplines -qf)] + (vim.api.nvim_buf_create_user_command buf :MdQuote +quote { :range true}) + (vim.api.nvim_buf_create_user_command buf :MdDeQuote -quote { :range true}) + (vim.keymap.set [:n :x] :> +quote + {:buffer true + :desc "increase quote level of selection"}) + (vim.keymap.set [:n :x] :< -quote + {:buffer true + :desc "decrease quote level of selection"})) diff --git a/dot_config/nvim/ftplugin/markdown_quote.lua b/dot_config/nvim/ftplugin/markdown_quote.lua index 3a57c20..b7c5324 100644 --- a/dot_config/nvim/ftplugin/markdown_quote.lua +++ b/dot_config/nvim/ftplugin/markdown_quote.lua @@ -1,76 +1,117 @@ -local api = vim.api -local fn = vim.fn - -local buffer = 0 -- operate on current buffer - +-- [nfnl] Compiled from ftplugin/markdown_quote.fnl by https://github.com/Olical/nfnl, do not edit. +local buf = 0 local function location(opts) - -- if called from a command, we don't have event info - if not opts then - opts = { - line1 = fn.line 'v', - line2 = fn.line '.', - } - end - - if opts.line1 > opts.line2 then - opts.line1, opts.line2 = opts.line2, opts.line1 - end - - -- nvim api lines are zero-based end-exclusive - opts.line1 = opts.line1 - 1 - return opts + local sl + local function _1_() + local t_2_ = opts + if (nil ~= t_2_) then + t_2_ = (t_2_).line1 + else + end + return t_2_ + end + sl = (_1_() or vim.fn.line("v")) + local el + local function _4_() + local t_5_ = opts + if (nil ~= t_5_) then + t_5_ = (t_5_).line2 + else + end + return t_5_ + end + el = (_4_() or vim.fn.line(".")) + local swap = (sl > el) + if swap then + return {line1 = (1 - el), line2 = sl} + else + return {line1 = (1 - sl), line2 = el} + end end - -local function mdquote(opts) - opts = location(opts) - - -- 1. get all of the matching lines - local lines = api.nvim_buf_get_lines(buffer, opts.line1, opts.line2, true) - - -- 2. edit them appropriately - for i, line in ipairs(lines) do - -- prepend a ">" and a " ", - -- unless the first character was already ">" or whitespace - -- so prepend " " first unless the above is true - if line:match '^$' then - elseif line:match '^[%s>]' then - lines[i] = '>' .. line - else - lines[i] = '> ' .. line - end - end - - -- 3. insert them back - api.nvim_buf_set_lines(buffer, opts.line1, opts.line2, true, lines) +local function maplines(f) + local function _8_(opts) + local opts0 = location(opts) + local argp + local function _9_(_241) + local _10_ = opts0.line1 + local _11_ = opts0.line2 + local function _12_(...) + return _241(buf, _10_, _11_, true, ...) + end + return _12_ + end + argp = _9_ + local setl = argp(vim.api.nvim_buf_set_lines) + local getl = argp(vim.api.nvim_buf_get_lines) + local function _13_() + local tbl_17_auto = {} + local i_18_auto = #tbl_17_auto + for _, v in ipairs(getl()) do + local val_19_auto = f(v) + if (nil ~= val_19_auto) then + i_18_auto = (i_18_auto + 1) + do end (tbl_17_auto)[i_18_auto] = val_19_auto + else + end + end + return tbl_17_auto + end + return setl(_13_()) + end + return _8_ end - -local function mddequote(opts) - opts = location(opts) - - -- see mdquote for the steps - local lines = api.nvim_buf_get_lines(buffer, opts.line1, opts.line2, true) - - for i, line in ipairs(lines) do - -- if the line starts with a "> ", remove both - -- elseif the line starts with a ">", remove that - -- TODO: I don't handle \t-based quoting - if line:match '^> ' then - lines[i] = line:sub(3) - elseif line:match '^>' then - lines[i] = line:sub(2) - end - end - - api.nvim_buf_set_lines(buffer, opts.line1, opts.line2, true, lines) +local _2bqf +local function _15_(_241) + local _16_ = _241 + if (_16_ == "") then + return _241 + else + local function _17_() + local s = _16_ + return s:match("^[%s>]") + end + if ((nil ~= _16_) and _17_()) then + local s = _16_ + return (">" .. s) + elseif (nil ~= _16_) then + local s = _16_ + return ("> " .. s) + else + return nil + end + end end - -api.nvim_buf_create_user_command(buffer, 'MdQuote', mdquote, { range = true }) -api.nvim_buf_create_user_command(buffer, 'MdDeQuote', mddequote, { range = true }) -vim.keymap.set({'n', 'x'}, '>', mdquote, { - buffer = true, - desc = 'increase quote level of selection', -}) -vim.keymap.set({'n', 'x'}, '<', mddequote, { - buffer = true, - desc = 'decrease quote level of selection', -}) +_2bqf = _15_ +local _qf +local function _19_(_241) + local _20_ = _241 + local function _21_() + local s = _20_ + return s:match("^> ") + end + if ((nil ~= _20_) and _21_()) then + local s = _20_ + return s:sub(3) + else + local function _22_() + local s = _20_ + return s:match("^>") + end + if ((nil ~= _20_) and _22_()) then + local s = _20_ + return s:sub(2) + elseif true then + local _ = _20_ + return _241 + else + return nil + end + end +end +_qf = _19_ +local _2bquote = maplines(_2bqf) +local _quote = maplines(_qf) +vim.api.nvim_buf_create_user_command(buf, "MdQuote", _2bquote, {range = true}) +vim.api.nvim_buf_create_user_command(buf, "MdDeQuote", _quote, {range = true}) +vim.keymap.set({"n", "x"}, ">", _2bquote, {buffer = true, desc = "increase quote level of selection"}) +return vim.keymap.set({"n", "x"}, "<", _quote, {buffer = true, desc = "decrease quote level of selection"}) -- cgit v1.2.3