Replies: 5 comments 14 replies
-
| I'm definitely interested! I'll give it a try in following days, and then I'll add it to docs/readme. Thanks! | 
Beta Was this translation helpful? Give feedback.
-
| @michaelb I feel a little dumb, but how do I use this in orgmode? I wasn't able to find any documentation around it, and I tried with this org content: Tried running  | 
Beta Was this translation helpful? Give feedback.
-
| Works like a charm! Added it to readme to plugins section. Thanks! | 
Beta Was this translation helpful? Give feedback.
-
| @michaelb is it possible to add the org  | 
Beta Was this translation helpful? Give feedback.
-
| @michaelb I have managed to adapt the script that you shared with me such that it prints the output to the org file. Under the  Click to check the codelocal 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
  print(blk_beg_line_nr, blk_end_line_nr)
  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 MUnfortunately, the API listener is not called when there is a multi-line result. All my attempts were unsuccessful when fixing this problem. Any help to get this fixed would be appreciated. I think there might be something wrong with the backend because the following return: :lua require'sniprun.api'.run_string('print(1+2)\nprint(4+5)', 'python', {display = {'Classic'}})
3
9
Press ENTER or type command to continueWhereas, the following returns nothing :lua require'sniprun.api'.run_string('print(1+2)\nprint(4+5)', 'python', {display = {'Api'}})But, the following returns :lua require'sniprun.api'.run_string('print(1+2)', 'python', {display = {'Api'}})
Sniprun: No listener registered | 
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
I'm adding support to org-mode code bloc evaluation to my plugin sniprun. (Currently on the dev branch, so the binary has to be compiled locally)
I though you might find it interesting, and maybe, who knows, usable?
Beta Was this translation helpful? Give feedback.
All reactions