nvim-neorg / neorg

Modernity meets insane extensibility. The future of organizing your life in Neovim.
GNU General Public License v3.0
6.46k stars 215 forks source link

Support relative paths for xdg-opening #465

Open mrossinek opened 2 years ago

mrossinek commented 2 years ago

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

Originally posted by **esquires** May 14, 2022 From the documentation [here](https://github.com/nvim-neorg/neorg/blob/main/doc/neorg.norg#L166) 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.

BoManev commented 1 year ago

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 
brg1ng commented 6 months ago

Hope to see this feature.

pysan3 commented 6 months ago

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/...}.

brg1ng commented 6 months ago

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

benlubas commented 6 months ago

@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

brg1ng commented 6 months ago

@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.

benlubas commented 6 months ago

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.

brg1ng commented 6 months ago

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

esquires commented 6 months ago

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:

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.

benlubas commented 6 months ago

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)

glyh commented 4 months ago

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.

pysan3 commented 4 months ago

@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?

glyh commented 4 months ago

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

max397574 commented 4 months ago

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)

glyh commented 4 months ago

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).

max397574 commented 4 months ago

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

glyh commented 4 months ago

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.