neorg icon indicating copy to clipboard operation
neorg copied to clipboard

Support relative paths for xdg-opening

Open mrossinek opened this issue 3 years ago • 17 comments

Discussed in https://github.com/nvim-neorg/neorg/discussions/456

Originally posted by esquires May 14, 2022 From the documentation here I can form links using absolute paths to images to be opened with xdg-open but I can't seem to figure out the syntax for opening files relative to the current file. Is that possible?

I'd like to do something like this but neither seem to work:

{file:./mypicture.png}[mypicture]
{file:///.mypicture.png}[mypicture]

Can you point me to the right syntax for doing relative paths so that they open in an external program? It seems that the @path syntax works fine (e.g. for text files): {@ myfile.txt}[myfile]

The problem appears to be, that the File URI scheme does not (officially) support relative paths. However, we should be able to support some sensible custom solution for this within Neorg.

mrossinek avatar May 22 '22 15:05 mrossinek

bump

It's useful if you rely on images and other non-norg files and you need to open them using xdg-open.

{file:///abosulute/path} 
{file://:path:} # this can use the capabilities of the :path: target 

BoManev avatar Apr 02 '23 23:04 BoManev

Hope to see this feature.

brg1ng avatar Apr 18 '24 02:04 brg1ng

Links to non norg files are written as {/ path/to/file}. You see the / at the front which means a normal file path.

Please read {# File Location} section in the specification for more detials. https://github.com/nvim-neorg/norg-specs/blob/51aab3dfd3bdf1d7ebdd2648d2e418ddd8d9ce2c/1.0-specification.norg#L1343

Absolute paths also works exactly the same with {/ /etc/passwd} or {/ C:/Users/...}.

pysan3 avatar Apr 18 '24 04:04 pysan3

The {/ path/to/file} link only works for text files.

brg1ng avatar Apr 18 '24 05:04 brg1ng

@brglng what are you trying to open b/c this works perfectly fine for me

https://github.com/nvim-neorg/neorg/assets/56943754/4b8ba668-977a-4128-89e7-0febbbcc0a68

benlubas avatar Apr 22 '24 16:04 benlubas

@benlubas For binary file, {/ } works fine, but sometimes explicit open with xdg-open may be required, e.g., to open a .html file, however it opens inside Neovim instead of a browser.

brg1ng avatar Apr 23 '24 05:04 brg1ng

So it doesn't work the way you want for text files?

There's the inbuilt gx command if you want to 'force' open a file externally, I think that it makes sense for neorg to open files that nvim can handle inside nvim, and only defer to xdg-open when it sees a file that it can't handle.

benlubas avatar Apr 23 '24 13:04 benlubas

FYI, the gx command does not work for either {/ path/to/file} or {file:path/to/file}.

brg1ng avatar Apr 23 '24 13:04 brg1ng

One issue is that gx doesn't seem to handle relative paths when the working directory is not the same as the directory containing the file. Let's say there is a file in ~/mydir/myfile.norg and in that file we have

{/ ./index.html}

If the working directory is ~/mydir, then gx works fine. Otherwise though, if the working directory is some other directory than ~/mydir, gx will not open it, returning the error message vim.ui.open: command failed (2): { "xdg-open", "./index.html" } .

It seems like there are a couple of approaches so that these links open in an external file:

  • Add relative paths to {file:path/to/file} links. There is a pull request (https://github.com/nvim-neorg/neorg/pull/1382) that I closed since {/ ./mypic.jpg} works correctly. However, {/ path} doesn't work for things that could be opened in either vim or an external file, the pull request would solve this issue.
  • Override gx so that it handles relative paths even when the working directory is not the same as the file.

Personally it seems like the 2nd approach may be better since it would mean there is less link syntax to remember ({file:path} wouldn't be needed) and users could decide themselves how to open links at read time (rather than it being forced at write time). In other words, opening in vim would be <cr> and opening externally would be gx.

esquires avatar May 01 '24 17:05 esquires

Override gx so that it handles relative paths even when the working directory is not the same as the file.

I like this solution as well. Though I know that some people have custom implementations of this keybind already, so we have to make sure that we fallback to what's already there if it's used on something we don't handle. (for example many people add logic to make things that look like: nvim-neorg/neorg open as a github link)

@pysan3 @vhyrro thoughts? I can handle the implementation (or @esquires if you would like to do it I'm not gonna stop you)

benlubas avatar May 01 '24 20:05 benlubas

I just want mention, also, when the file name contains space, even for gx I have to have the file name selected, which is very not ergonomic.

glyh avatar Jun 13 '24 06:06 glyh

@glyh with the addition of pathlib.nvim, you should be able to jump to a space-containing filepath with a simple <CR> no problem. (Obviously neorg >= 0.8)

Could you describe your situation with more details?

pysan3 avatar Jun 13 '24 10:06 pysan3

When I want to open an HTML file with space in its name in an external browser. I have to select the whole file name and then use gx.

Even so, there's no way to open it in a new browser window. Chrome usually opens inside the same old browser window.

I kinda need this because I've experinced a lot when the web page is gone after some year and I would like to have a copy of it offline.

EDIT:

The second issue seems to be workaroundable with some wrapper on the external browser, but for the selection part there has to be some change in Neorg

glyh avatar Jun 13 '24 10:06 glyh

Even so, there's no way to open it in a new browser window. Chrome usually opens inside the same old browser window.

this is not controlled by neorg

I kinda need this because I've experinced a lot when the web page is gone after some year and I would like to have a copy of it offline.

I don't see how this is related to neorg or anything else in this issue?

perhaps open a new issue if your problems aren't related to this issue (relative paths)

max397574 avatar Jun 13 '24 10:06 max397574

Yes I update that comment indicating that this might be workaroundable with wrappers.

Well it is related. I'm just explaining why I need this feature in case someone wonders.

Absolute path using {file:///abc} can contain a space inside but relative path {/ abc} can't if you just use gx(you have to select it).

glyh avatar Jun 13 '24 10:06 glyh

yes but what has the part about you wanting to use webpages offline todo with neorg?

max397574 avatar Jun 13 '24 10:06 max397574

I've already explained above this has nothing to do with this feature specifically, but why I need to have relative access to webpage (which is a use case). You are free to ignore them, I don't know why are you arguing with me on that point.

glyh avatar Jun 13 '24 10:06 glyh