habamax / vim-asciidoctor

Asciidoctor plugin for Vim
MIT License
182 stars 16 forks source link

`gx` not working correctly to open URLs #73

Closed ctittel closed 4 years ago

ctittel commented 4 years ago

gx is the default command from Vim 7.4 to open URLs (see https://stackoverflow.com/questions/9458294/open-url-under-cursor-in-vim-with-browser).

I have the following line in a .adoc file opened with Vim:

https://www.brainlinks-braintools.uni-freiburg.de/rss-2020-workshop/[Interesting workshop from RSS 2020]

When doing gx on this line in normal mode, vim opens the clearly wrong URL https://www.brainlinks-braintools.uni-freiburg.de/rss-2020-workshop/[Interesting/ in the browser. Removing the trailing / in the vim link doesn't help.

habamax commented 4 years ago

gx is the default mapping of netrw builtin plugin and there is no clear way to pass it a correct URL unless I remap gx providing my own implementation.

Didn't plan but will look into it.

ctittel commented 4 years ago

I see! Is there currently an alternative command which opens adoc links correctly?

habamax commented 4 years ago

I see! Is there currently an alternative command which opens adoc links correctly?

I don't know. I for myself do not use netrw, so I was planning to implement it and even started some times ago:

"" Open URL under cursor using OS
"" http://ya.ru
"" ~/docs
"" $HOME/docs
"" C:/Users/maksim.kim/docs
"" .
func! os#open_url(word) abort
    " Windows only for now
    if !has("win32")
        return
    endif
    let word = a:word
    if word =~ '^[~.$].*'
        let word = expand(word)
    endif
    " TODO: check if barebone url
    " TODO: check if path or a filename
    " TODO: check and extract asciidoctor url
    " TODO: check and extract markdown url
    exe printf("silent !start %s", word)
    " nnoremap gx :call job_start('cmd.exe /c start '.expand("<cfile>"))<CR>
endfunc

But didn't have a time to do it.

ctittel commented 4 years ago

Thanks for the answer!

I don't know much vimscript (yet), but here is some additional information which might be useful:

Markdown plugins (probably) also have the same problem with gx and custom link formats. I have plasticboy/vim-markdown in my vimfile and it seems to be able to handle custom links correctly with ´gx´. I tried these two formats which both work correctly:

<https://whatever.com> and [file:zettelkasten.md](zettelkasten.md)

In the latter example gx opens the correct file no matter on which character in the string the cursor is - you can be on the first [ or last ) or anywhere inbetween.

habamax commented 4 years ago

Could you check latest master?

ctittel commented 4 years ago

Hi, I tested some links in my adoc files.

In my tries, links of the form https://example.com[Text] and https://example.com/whatever seemed to work correctly :)

But some problems I have found:

habamax commented 4 years ago

yep, it is not very robust at the moment.

habamax commented 4 years ago
ctittel commented 4 years ago

Cool :)

Here is the relevant section from the AsciiDoctor Reference: https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#links

For file links AsciiDoc uses two ways:

file:// works for me for absolute links.

A minor problem with the previous link: I have (https://groups.google.com/forum/#!topic/tiddlywiki/lIS6sFbrpwU) in my adoc file. When doing gx on it it now opens https://groups.google.com/forum/#!topic/tiddlywiki/lIS6sFbrpwU) (Parantese too much) But this probably doesn't matter as one can just add a space after the link.

habamax commented 4 years ago

I have (https://groups.google.com/forum/#!topic/tiddlywiki/lIS6sFbrpwU) in my adoc file. When doing gx on it it now opens https://groups.google.com/forum/#!topic/tiddlywiki/lIS6sFbrpwU) (Parantese too much)

This I wouldn't be able to fix. (simple problem --> quite unobvious solution)

habamax commented 4 years ago
  • link:index.html[Docs] for relative files
  • link:// for absolute links

Will have a look into this

habamax commented 4 years ago
  • link:index.html[Docs] for relative files
  • link:// for absolute links

Will have a look into this

should now also work.

Bear in mind that link:index.html[Docs] would only work if your vim working directory is the same.

habamax commented 4 years ago
* There are issues if there are spaces in the link description, e.g. with `https://noteself.org/[Note Self]`: If the cursor is on or between the first `h` and the `e` in `Note`, `gx` opens the correct website. But on the rest of the string it doesn't work.

I will try to implement it this week. Although I am not sure about implementation yet, and whether I would be able to do it :)

habamax commented 4 years ago

Could you check latest master?

I think I have fixed "cursor is on [description separated with spaces]"

ctittel commented 4 years ago

Great work! :)

The fix for spaces in the link title worked well for me. Also I think its very cool that file link's are always opened in the system default application.

But I have some issues with link::

habamax commented 4 years ago

Thx for input, will check it tomorrow

habamax commented 4 years ago
  • neither link:vim.adoc nor link:// are recognized as links when the [link title] is missing

This is actually not a valid asciidoctor syntax. link should be accompanied with []

  • directory paths: On my windows computer, only this path works for opening a file in a subdirectory: link:Keybinds\\2020-04-01_20-06-29_EmacsIcon.svg[Test]. When I use / or // for the subdirectory path instead, it doesn't work. This may create issues when switching between Linux and Windows.

forward slash:

Under the hood it just calls :!start misc/aseprite.adoc (or !open, !xdg-open for other oses) and for some reason it fails. I have tried to change current dir with lcd but it has no effect.

habamax commented 4 years ago

Hmm, looks like I did it :)

Pls, try latest master

ctittel commented 4 years ago

Very cool, works perfectly :) Great work

Here are some very advanced usecases from the quick reference that probably not many use (if the goal is to implement the specification perfectly):

The last two are probably not very important as Vim has strong navigation features inbuilt anyways, but I just want to mention them ;)

habamax commented 4 years ago

The last two are probably not very important as Vim has strong navigation features inbuilt anyways, but I just want to mention them ;)

Yep, do not plan to do anything like that. At least for now. Thx!