Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append code block results to markdown document #152

Open
daephx opened this issue Apr 10, 2022 · 9 comments
Open

Append code block results to markdown document #152

daephx opened this issue Apr 10, 2022 · 9 comments

Comments

@daephx
Copy link

daephx commented Apr 10, 2022

As an extension to #50: Much like the the orgmode screenshot supplied,
is it possible to have a display option, specifically for source code blocks in markup where the result is printed to the document itself?

Example:

```python
print('Hello, World!')
```

_Result:_
```plain
Hello, World!
```

This way you can save the output for sharing/publishing, similar to Orgmode or Jupyter Notebooks.

You could keep virtual text on but have this as a compile step for your individual cells.
As I imagine such a feature should be exempt from REPL capabilities, otherwise it could cause some problems.

@daephx daephx changed the title Append code block results to document Append code block results to markdown document Apr 10, 2022
@michaelb
Copy link
Owner

Linking that here while I try to find some free time
#111 (comment)

@michaelb
Copy link
Owner

#153 adds supports for multiple code blocs in one selection, meaning that you can now do even :%SnipRun (in markdown or orgmode) and have sensible outputs. (one per code bloc)

There's no support for modifying the buffer in the meantime though, so it doesn't do what you want. With the VirtualText display option, it looks okay-ish, but nothing is inserted nor savable in the file..

@daephx
Copy link
Author

daephx commented Apr 14, 2022

Alright thanks for the consideration, that sounds interesting in the mean time.
I'll take a look when I get a moment!

@minhd-vu
Copy link

minhd-vu commented Mar 5, 2024

Hey! So I've been trying to get similar functionality by copying the output to the * buffer to be able to paste with p. Here's what I have so far:

    {
      "michaelb/sniprun",
      cmd = "SnipRun",
      branch = "master",
      build = "sh install.sh",
      config = function()
        require("sniprun").setup {
          -- your options
        }
      end,
      keys = {
        { "<leader>sr", "<cmd>:redir @* | :SnipRun | redir END<cr>", mode = { "n" }, desc = "SnipRun" },
        { "<leader>sr", "<cmd>:redir @* | :'<,'>SnipRun | redir END<cr>", mode = { "v" }, desc = "SnipRun" },
        { "<leader>sc", "<Plug>SnipClose", desc = "SnipClose" },
      },
    },

This works so far but I'd like to add ```plain\n{msg}\n``` around the message. Here's what I tried so far, but I'm having some trouble:

        {
          "<leader>sr",
          function()
            vim.cmd "redir @a | SnipRun | redir END"
            local a = vim.fn.getreg "a"
            vim.fn.setreg("b", a)
          end,
          mode = { "n" },
          desc = "SnipRun",
        },

This doesn't look like it's writing to the correct register, and when I check the a register with :reg it looks like it's empty. Wondering if y'all are experiencing similar issues. It also tried stuff with nvim_exec and nvim_command but had similar results.

It may be something with my neovim config itself. Pretty new to this deep configuration so let me know if I'm missing something obvious 😄 .

@michaelb
Copy link
Owner

michaelb commented Mar 6, 2024

Hi!
You're charting mostly unexplored terrain, but if your current solution works somehow, well I see no reason to change it

The 'clean' approach would be to use the API 's register_listener to create your own (lua) function to handle output however you want. It should also be relatively easy to customize the output to get wrapped with "plain\n{msg}\n" or whatever you want and continue using the rest of your scripts

@PlatyPew
Copy link

PlatyPew commented Jul 2, 2024

It's a bit late but here's my take on this

local api_listener = function(d)
    if vim.bo.filetype ~= "markdown" then
        return
    end

    if d.status ~= "ok" or d.message == "" then
        return
    end

    local output = string.format("```plain\n%s\n```\n", d.message)
    vim.fn.setreg('"', output)
end

require("sniprun.api").register_listener(api_listener)

@gerazov
Copy link

gerazov commented Jan 29, 2025

It might be slightly related, but it would be cool to add virtual text on each line that prints something in the REPL (if adding multiple virtual lines after the cell is not possible), like so:

Current image is obtained by running the whole cell selection and then rerunning on the print lines using the REPL.

@michaelb
Copy link
Owner

Unfortunately:

  • matching lines to their respective prints breaks down for non-trivial cases (print in a loop, print in function, or print of multiline string)
  • executing the code line-by-line (and record which part generated which output) also breaks down for every language that I can think of as soon as it's more complex than a list of variable evaluations

And those are the only way I can think of doing this..

I you want more lines in your output, you should use another display mode, such as FloatingWindow doc

@gerazov
Copy link

gerazov commented Jan 30, 2025

I see - your're right ...

The problem with the floating window (I use notify atm) is that it disappears.

For me I vote also for appending the result as a new code block below the cell that's being run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants