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

fails to open external links #116

Closed
kraxli opened this issue Aug 23, 2020 · 11 comments
Closed

fails to open external links #116

kraxli opened this issue Aug 23, 2020 · 11 comments
Assignees
Labels
bug:report Something does not work as intended / expected.

Comments

@kraxli
Copy link

kraxli commented Aug 23, 2020

Hi

I cannot open an external link with gx (mapped to <Plug>(mkdx-gx)) or gf in e.g. Firefox, Libreoffice, ...

OS: Linux Mint 20 Ulyana
Vim: NVIM v0.4.4
Default browser: Mozilla Firefox 79.0

My mkdx-settings

    let g:mkdx#settings     = { 'highlight': { 'enable': 1 },
                            \ 'enter': { 'shift': 0, 'enable': 1},
                            \ 'links': { 'external': { 'enable': 1, 'relative': 1 }, 'fragment': {'complete': 1, 'jumplist': 1 } },
                            \ 'toc': { 'text': 'Table of Contents', 'update_on_write': 1 },
                            \ 'fold': { 'enable': 0 },
                            \ 'map': { 'enable': 0 },
                            \ 'gf_on_steroids': 1,
                            \ 'checkbox': { 'toggles': [' ', '-', 'x'] },
                            \ }

Example

  1. create a link in a markdown file: [link to google](https://www.google.com/)
  2. move on it
  3. select normal mode
  4. press gx

Expected
Google page opens in Firefox

Actual
nothing happens

What do I miss in terms of settings?

Thanks
Dave

@SidOfc
Copy link
Owner

SidOfc commented Aug 23, 2020

Hi Dave,

Behind the scenes mkdx uses the open command which opens files and urls using your default applications. For this feature to work, you must be able to run the open command from the terminal. On Linux, I believe the package you'd need is called xdg-open.

Do you have open (test using: which xdg-open) installed? If not can you install it and try again?

EDIT:

I noticed the instructions on this part are not very clear about open being required for this to work, I'll improve the docs on my part in the meantime!

@SidOfc SidOfc self-assigned this Aug 23, 2020
SidOfc added a commit that referenced this issue Aug 23, 2020
@kraxli
Copy link
Author

kraxli commented Aug 25, 2020

many thanks for coming back on this and for the clarification in README!

on linux / ubuntu I need to call xdg-open https://github.com. I've tried to set alias open='xdg-open' in my .bashrc and open https://github.com works perfectly in the terminal but without success on a markdown file neither with the mapping

map gx <Plug>(mkdx-gx)
map gf <Plug>(mkdx-gf)

nor

nmap gx <Plug>(mkdx-gx)
nmap gf <Plug>(mkdx-gf)

What works for me is

function! config#open_url()
  let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;]*')
  echo s:uri
  if s:uri != ""
    silent exec "!xdg-open ".s:uri
  else
    echo "No URI found in line."
  endif
endfunction

nmap gx :call config#open_url()<cr>

or using the Open comand from https://github.com/xolox/vim-shell in nmap gx :Open<cr>

@SidOfc
Copy link
Owner

SidOfc commented Aug 25, 2020

Hey @kraxli, hmmn that is quite odd, perhaps it has to do with the way system works but I'm not sure yet.
I pushed a commit which uses your method of silent! exec and also checks if xdg-open can be executed instead of just open.

Can you give this a shot?

@SidOfc SidOfc added the bug:report Something does not work as intended / expected. label Aug 25, 2020
@kraxli
Copy link
Author

kraxli commented Aug 29, 2020

Hi @SidOfc

many thanks for coming back and the update! I pulled the newest master branch and it works better :-)

Below a few checks on opening stuff with gx and gf

# Test Section
- [Diary](00_Diary.md) -> works with gf / gx :-)
- https://google.com -> works  with gf / gx :-)
- [Google](https://google.com) -> works  with gf / gx :-)
- [peewee](http://docs.peewee-orm.com/en/2.10.2/index.html) -> works with gf / gx  :-)
- peewee database: e.g. [generating-models-from-existing-databases](http://docs.peewee-orm.com/en/2.10.2/peewee/database.html#generating-models-from-existing-databases) -> not working
- http://docs.peewee-orm.com/en/2.10.2/index.html -> not working
- other file format [Doc](./test.doc) -> gf / gx opens the file in Neovim but not in word/libre-office

@SidOfc
Copy link
Owner

SidOfc commented Aug 29, 2020

Ah nice, glad to hear it's mostly working now.

I'll look into the other scenario's as well, cheers for providing examples 👍

@SidOfc
Copy link
Owner

SidOfc commented Aug 30, 2020

Hi again @kraxli, I've fixed the issue with the links not working, the reason it didn't work was because they had a subdomain and my regex didn't take that into account.

I still have to find a way to make "doc" links open in libreoffice though, I'm going to look into that next. In the meantime you can update mkdx so that at least the links will work :)

Will report back when I've found a fix for opening doc / sheet / pdf files using open.

@SidOfc
Copy link
Owner

SidOfc commented Aug 30, 2020

Alright @kraxli other "non plain" files should now also open in an external application. Note that the file command is required for this to work. I've updated the documentation to reflect this.

Let me know if everything is working for you 🤞

@kraxli
Copy link
Author

kraxli commented Aug 30, 2020

nice - thanks a lot! works pretty nice :-)

the only thing which isn't working for me is

- peewee database: e.g. [generating-models-from-existing-databases](http://docs.peewee-orm.com/en/2.10.2/peewee/database.html#generating-models-from-existing-databases) -> not working

but the web-address may be a bit complicated and hard to parse. For such cases would it be possible to allow for vmap with gx? Such that one could mark / select the whole address and open it? This would simplify the detection / parsing of the address

@SidOfc
Copy link
Owner

SidOfc commented Aug 30, 2020

Alright, that issue should now also be fixed, this happened because I used this method:

exec '!xdg-open' . ' ' . shellescape(destination)

But if the destination includes hashtags, they get resolved to the current working directory. The fix was simple, to just prefix any # with a \ so that vim would treat them like regular characters:

exec '!xdg-open' . ' ' . shellescape(substitute(destination, '#', '\\#', 'g'))

About adding a visual mode mapping, it can be done of course but it would not actually simplify anything as the normal mode mapping is expected to work just as well. That said though it could be a nice fallback to have when all else fails, and could also be used in non-link references to files and URLs.

I've opened a new issue visual mode support since it's not related to this bug but rather a new feature, feel free to comment there if you have suggestions about how it could work!

Also, can you confirm once again if the issue with the last non working link is resolved? If so I'll close this issue even though It's not unlikely more issues could pop up with this, but I'll fix them as they come along.

@kraxli
Copy link
Author

kraxli commented Aug 31, 2020

Thanks @SidOfc - now it is working like a charm 👍
Thanks a lot for your great plugin!! I am closing this issue.

@kraxli kraxli closed this as completed Aug 31, 2020
@SidOfc
Copy link
Owner

SidOfc commented Aug 31, 2020

Cheers for the compliment @kraxli, always nice to hear :)

Let me know if you find more issues / have other ideas, thanks for closing and have a good remainder of the day 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:report Something does not work as intended / expected.
Projects
None yet
Development

No branches or pull requests

2 participants