R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
169 stars 18 forks source link

open_html doesn't work if getOption("browser") is a function #232

Closed idavydov closed 1 month ago

idavydov commented 1 month ago

I noticed that if I set options(browser=function(url) {...}) then open_html setting is ignored.

I think it's a valid option and is correctly used e.g. by browserURL().

Would it maybe be possible to allow browser which is a function? I am using this to view html's from a remote host using a combination of ssh port forwarding, servr and opener.

Not sure if my solution is optimal, by happy to document it in a wiki.

jalvesaq commented 1 month ago

When the browser option isn't a string, R.nvim receives an empty string as the browser's name, and, then, it calls either open (Windows or macOS) or xdg-open (Linux) to open the HTML.

jalvesaq commented 1 month ago

Not sure if my solution is optimal, by happy to document it in a wiki.

Yes, please.

Perhaps, if the option "browser" is a function R.nvim should use R to open the HTML document. Or, maybe, it should always use R to open HTML documents.

idavydov commented 1 month ago

Here the first draft; at the end I mention the problem I currently have.

Yes, for the remote setup it would be great if R.nvim could call browseURL() via R. I do not see any downsides of this approach.

jalvesaq commented 1 month ago

Here the first draft; at the end I mention the problem I currently have.

Thanks!

Could you try the branch "browseURL", please?

idavydov commented 1 month ago

Great, thanks a lot.

browserURL seem to work great with .Rmd. Quarto render doesn't open preview automatically, as far as I understand. For quarto preview one could set:

        quarto_preview_args = ", port=.free_port()",

I will update the wiki once this is in main.

So all seem to work great.

jalvesaq commented 1 month ago

Merged now. Thanks for reporting the issue and for writing the page on the Wiki!

idavydov commented 1 month ago

sorry for reopening this discussion. I am now getting suspicious messages after rendering an R Markdown file.

^I[C]: in function 'jobstart'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:128: in function 'start'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/doc.lua:223: in function 'load_html'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/doc.lua:237: in function 'open'
^I[string ":lua"]:1: in main chunk
^I[C]: in function 'execute'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:48: in function 'exec_stdout_cmd'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:69: in function <<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:58>
stack traceback:
^I[C]: in function 'execute'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:48: in function 'exec_stdout_cmd'
^I<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:69: in function <<...>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:58>
jalvesaq commented 1 month ago

This isn't easy to debug without replicating locally. It's necessary to put nvimcom.verbose=6 and/or enable logging in rnvimserver to know what message is causing the problem.

idavydov commented 1 month ago

So let's say I have a minimal .Rmd:

```{r}
cat("hi")

Every time I press `<localleader>kh` I get this output with `nvimcom.verbose=6`:

nvim.interlace.rmd("a.Rmd", outform = "html_document", rmddir = "", envir = .GlobalEnv)

processing file: a.Rmd

output file: a.knit.md

<...>/bin/pandoc +RTS -K512m -RTS a.knit.md --to html4 --from markdown+au tolink_bare_uris+tex_math_single_backslash --output a.html --lua-filter /usr/l ocal/lib/R/site-library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /us r/local/lib/R/site-library/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resou rces --standalone --variable bs3=TRUE --section-divs --template /usr/local/lib /R/site-library/rmarkdown/rmd/h/default.html --no-highlight --variable highlig htjs=1 --variable theme=bootstrap --mathjax --variable 'mathjax-url=https://ma thjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --include-in -header /tmp/Rtmp1xCtyo/rmarkdown-str1f8fb64200f6f.html [WARNING] This document format requires a nonempty element. Defaulting to 'a.knit' as the title. To specify a title, use 'title' in metadata or --metadata title="...".</p> <p>Output created: a.html send_to_nvim [6] {3659219060}: lua require('r.doc').open('<hidden>/a.html', 'RbrowseURLfun') nvimcom_task() nvimcom_globalenv_list() nvimcom received: E3657596637browseURL('<hidden>/a.html') nvimcom_fire() globalenv_list(0) len1 = 70144, len2 = 70144</p> <pre><code> and I get this in messages:</code></pre> <p>Vim(lua):E5108: Error executing lua Vim:E475: Invalid argument: expected String or List stack traceback: ^I[C]: in function 'jobstart' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:128: in function 'start' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/doc.lua:223: in function 'load_html' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/doc.lua:237: in function 'open' ^I[string ":lua"]:1: in main chunk ^I[C]: in function 'execute' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:48: in function 'exec_stdout_cmd' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:69: in function <<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:58> stack traceback: ^I[C]: in function 'execute' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:48: in function 'exec_stdout_cmd' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:69: in function <<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:58> function: builtin#18 Vim(lua):E5108: Error executing lua Vim:E475: Invalid argument: expected String or List stack traceback: ^I[C]: in function 'jobstart' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:128: in function 'start' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/doc.lua:223: in function 'load_html' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/doc.lua:237: in function 'open' ^I[string ":lua"]:1: in main chunk ^I[C]: in function 'execute' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:48: in function 'exec_stdout_cmd' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:69: in function <<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:58> stack traceback: ^I[C]: in function 'execute' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:48: in function 'exec_stdout_cmd' ^I<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:69: in function <<hidden>/.local/share/nvim/lazy/R.nvim/lua/r/job.lua:58></p> <pre><code> <details> <summary>R.nvim config</summary> ```lua config = function() -- Create a table with the options to be passed to setup() local opts = { nvimpager = "tab", close_term = false, open_html = "open", open_pdf = "no", pdfviewer = "", csv_app = "tmux new-window vd --theme=light", quarto_preview_args = ", port=.free_port()", R_args = { "--quiet", "--no-save", "--no-restore" }, hook = { on_filetype = function() -- This function will be called at the FileType event -- of files supported by R.nvim. This is an -- opportunity to create mappings local to buffers. vim.api.nvim_buf_set_keymap(0, "n", "<Enter>", "<Plug>RDSendLine", {}) vim.api.nvim_buf_set_keymap(0, "v", "<Enter>", "<Plug>RSendSelection", {}) end, }, min_editor_width = 72, rconsole_width = 78, disable_cmds = { "RCustomStart", "RSaveClose", "RPackages", }, } if vim.env.R_AUTO_START == "1" then opts.auto_start = "on startup" end require("r").setup(opts) end,</code></pre> <p></details></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/idavydov"><img src="https://avatars.githubusercontent.com/u/671660?v=4" />idavydov</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <p>I can reliably reproduce this error by calling: <code>:lua require('r.doc').open('a.html', 'RbrowseURLfun')</code></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/jalvesaq"><img src="https://avatars.githubusercontent.com/u/891655?v=4" />jalvesaq</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <blockquote> <p>I can reliably reproduce this error by calling: :lua require('r.doc').open('a.html', 'RbrowseURLfun')</p> </blockquote> <p>This sends the command <code>browseURL("a.html")</code> to R. Your setup is complex and requires R to run remotely, which I can't replicate because I currently don't have a spare computer to use as remote machine.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/idavydov"><img src="https://avatars.githubusercontent.com/u/671660?v=4" />idavydov</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <p>I'm looking at the code, and I think the problem might be <a href="https://github.com/R-nvim/R.nvim/blob/93efe8a9c1819a32f92b7df1202ae26001fc5bf5/lua/r/doc.lua#L218">here</a>.</p> <pre><code class="language-lua"> if browser == "" then if config.is_windows or config.is_darwin then cmd = { "open", fullpath } else cmd = { "xdg-open", fullpath } end elseif browser == "RbrowseURLfun" then send_to_nvimcom("E", "browseURL('" .. fullpath .. "')") else cmd = vim.split(browser, " ") table.insert(cmd, fullpath) end job.start(fullpath, cmd, { detach = true, on_exit = job.on_exit })</code></pre> <p>From my uninformed perspective the <code>job.start</code> shouldn't happen if <code>browser == "RbrowseURLfun"</code>.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/idavydov"><img src="https://avatars.githubusercontent.com/u/671660?v=4" />idavydov</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <p>It <em>could</em> be something like this (I'm do not know lua well):</p> <pre><code class="language-lua"> if browser == "RbrowseURLfun" then send_to_nvimcom("E", "browseURL('" .. fullpath .. "')") else if browser == "" then local cmd if config.is_windows or config.is_darwin then cmd = { "open", fullpath } else cmd = { "xdg-open", fullpath } end else cmd = vim.split(browser, " ") table.insert(cmd, fullpath) end job.start(fullpath, cmd, { detach = true, on_exit = job.on_exit }) end</code></pre> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/idavydov"><img src="https://avatars.githubusercontent.com/u/671660?v=4" />idavydov</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <p>I tested this fix locally and it seems to work fine. I can make a PR if that helps, @jalvesaq .</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/jalvesaq"><img src="https://avatars.githubusercontent.com/u/891655?v=4" />jalvesaq</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <p>Yes, please. It's better because your contribution is registered in Git history.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/idavydov"><img src="https://avatars.githubusercontent.com/u/671660?v=4" />idavydov</a> commented <strong> 1 month ago</strong> </div> <div class="markdown-body"> <h1>245 seems to work fine for me</h1> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>