Replies: 1 comment
-
@ gzagatti from neovim-orgmode has been kind enough to create a nice working solution for orgmode. local ok, sa = pcall(require, "sniprun.api")
if not ok then error("Snipun not installed") end
local M = {}
local code_block = {}
M.setup = {
parse_pattern = {
code_opening = [[\v^#\+(BEGIN_SRC|begin_src)]],
code_closing = [[\v^#\+(END_SRC|end_src)]],
-- #+RESULTS is an affiliated keyword
-- it can precede any element, except those listed in the manual
-- for now I use a simple regex that should detect the end of a fixed
-- delimited block, a table or an unnested block
-- https://orgmode.org/worg/dev/org-syntax.html#Affiliated_keywords
result_opening = [[\v^#\+(RESULTS|results):]],
result_closing = [[^\(\([:|]\|#+END_\).*\)\?\n\([:|]\s\?\)\@!]],
},
result_block_name = "#+RESULTS:",
}
function parse_block (opening, closing)
local save_cursor = vim.fn.getpos('.')
-- get line number of beginning of code block
-- we want to go to the beginning of the block to avoid matching between two blocks
local blk_beg_line_nr = vim.fn.search(opening, "bcW")
if blk_beg_line_nr == 0 then
print("Not in a block")
return
end
-- get line number of closing of code block
local blk_end_line_nr = vim.fn.search(closing, "nW")
if blk_end_line_nr == 0 then
print("Not in a block")
return
end
vim.fn.setpos(".", save_cursor)
if blk_end_line_nr < save_cursor[2] then
print("Not in a block")
return
end
return {
blk_beg_line_nr = blk_beg_line_nr,
blk_end_line_nr = blk_end_line_nr
}
end
function insert_result (result)
result = vim.fn.split(result, "\n")
for i = 1, #result do
result[i] = ": " .. result[i]
end
local save_cursor = vim.fn.getpos('.')
-- remove existing results if present
vim.fn.setpos(".", { 0, code_block.blk_end_line_nr + 1, 1, 0 })
non_empty_line_nr = vim.fn.search([[\S]], "W")
result_block = parse_block(M.setup.parse_pattern.result_opening,
M.setup.parse_pattern.result_closing)
if result_block then
vim.fn.deletebufline(vim.fn.bufname(), result_block.blk_beg_line_nr,
result_block.blk_end_line_nr)
end
if vim.fn.getline(code_block.blk_end_line_nr + 1) ~= '' then
vim.fn.append(code_block.blk_end_line_nr, '')
end
vim.fn.append(code_block.blk_end_line_nr + 1, M.setup.result_block_name)
if #result > 0 then
vim.fn.append(code_block.blk_end_line_nr + 2, result)
end
if vim.fn.getline(code_block.blk_end_line_nr + 2 + #result + 1) ~= '' then
vim.fn.append(code_block.blk_end_line_nr + 2 + #result, '')
end
vim.fn.setpos(".", save_cursor)
code_block = {}
end
function M.snip_org_run ()
code_block = parse_block(M.setup.parse_pattern.code_opening, M.setup.parse_pattern.code_closing)
if code_block then
config_values = require"sniprun".config_values
original_display = config_values["display"]
original_display[#original_display + 1] = "Api"
config_values["display"] = { "Api" }
sa.run_range(code_block.blk_beg_line_nr, code_block.blk_end_line_nr, nil, config_values)
config_values["display"] = original_display
end
end
function api_listener (d)
if d.status == "ok" then
insert_result(d.message)
elseif d.status == "error" then
print("Error: ", d.message)
end
end
sa.register_listener(api_listener)
return M sourcing this snippet as a file in your config, then running
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
medwat has done some nice work here for output below markdown code blocs
#111 (comment)
Beta Was this translation helpful? Give feedback.
All reactions