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

Opening OneNote links from Vimwiki #818

Closed
llinfeng opened this issue Feb 3, 2020 · 7 comments
Closed

Opening OneNote links from Vimwiki #818

llinfeng opened this issue Feb 3, 2020 · 7 comments

Comments

@llinfeng
Copy link

@llinfeng llinfeng commented Feb 3, 2020

Thanks to severoraz's post in Issue #71, I added one more condition to help with opening OneNote links on Windows 10 machines.

    " Update things for Windows: to open OneNote links
    " Credits 1: https://github.com/vimwiki/vimwiki/issues/71#issuecomment-494395082
    " Credits 2: https://github.com/vimwiki/vimwiki/issues/71#issuecomment-66627514
    " Other refernces: https://superuser.com/q/1522422/361600
    function! VimwikiLinkHandler(link)
        let link = a:link
        let islink = 0
        if link =~ '^local:.*'
            let islink = 1
            let local_dir = matchstr(link, '^local:\zs.*')
            let abs_dir = expand('%:p:h').'/'.local_dir
        elseif link =~ '^file:.*'
            let islink = 1
            let abs_dir = matchstr(link, '^file:\zs.*')
        elseif link =~ '^onenote:.*'
            let islink = 2
            let onenote_link = a:link
        endif
        if islink == 1
            exe "!explorer '" . substitute(abs_dir,"/","\\\\",'g') . "'"
            return 1
        " If it is an OneNote link
        elseif islink == 2
            silent! exe '!start /MIN cmd /c "start "" "' . onenote_link . '" "'
            return 1
        else
            return 0
        endif
    endfunction

The function above helps Vimwiki to recognize links with onenote: prefix, produced by OneNote (or OneNote 2016) when one right-click a page for its link.

One caveat is that the following warning message will raise every time when I "followed" the onenote: link. I did not find a good way to deal wit it. The command, start "" "onenote:https://d.docs.live.net/...", runs flawlessly when supplied to Command Prompt.
image

Please help if you know how to suppress such a warning message for OneNote 2016. (I opted to use it as the default for Windows 10, URL:OneNote Protocol. The other OneNote App does not respond to perfectly to this onenote: file link per my testing.)

@llinfeng
Copy link
Author

@llinfeng llinfeng commented Feb 10, 2020

Setting the newer OneNote app as default for onenote: protocol gets rid of this warning message.
image

@llinfeng llinfeng closed this Feb 10, 2020
@bousch
Copy link

@bousch bousch commented Feb 24, 2020

Thanks, when running vimwiki in the WSL bash terminal the command above does not work because then you have to escape the ampersands. I use the following command in this case:

!cmd.exe /c start "" "' . substitute(onenote_link,"&","^&",'g') . '" 2> /dev/null

When running vimwiki in termux on Android you can use termux-open:

!termux-open '" . onenote_link . "'

@llinfeng
Copy link
Author

@llinfeng llinfeng commented Feb 24, 2020

@bousch Looks good! Would you mind sharing the full function as defined for WSL bash and/or termux? I tried for 10 minutes and did not get it to work.

@bousch
Copy link

@bousch bousch commented Feb 24, 2020

This is the complete function I use atm. Note I currently only use vim in WSL or Termux so I removed the original command. For WSL I had to add the error redirection to /dev/null to suppress some output in vim.

function! VimwikiLinkHandler(link)
    let link = a:link
    let islink = 0
    if link =~ '^local:.*'
        let islink = 1
        let local_dir = matchstr(link, '^local:\zs.*')
        let abs_dir = expand('%:p:h').'/'.local_dir
    elseif link =~ '^file:.*'
        let islink = 1
        let abs_dir = matchstr(link, '^file:\zs.*')
    elseif link =~ '^onenote:.*'
        let islink = 2
        let onenote_link = a:link
    endif
    if islink == 1
        exe "!explorer '" . substitute(abs_dir,"/","\\\\",'g') . "'"
        return 1
    " If it is an OneNote link
    elseif islink == 2
        if g:uname == 'Android'
            let onenote_command = "!termux-open '" . onenote_link . "'"
        else
            " From WSL the ampersands must be escaped like this ^&
            let onenote_command = '!cmd.exe /c start "" "' . substitute(onenote_link,"&","^&",'g') . '" 2> /dev/null'
        endif
        silent! exe onenote_command
        return 1
    else
        return 0
    endif
endfunction

(edit: And I did not implement anything for the local links on termux or wsl. I have no need for that yet.)

@bousch
Copy link

@bousch bousch commented Feb 24, 2020

BTW I define uname in the beginning of my vimrc (looks a little messy, this could be improved I guess):

let OS=substitute(system('uname -s'),"\n","","")
let uname=substitute(system('uname -o'),"\n","","")
let unamea=substitute(system('uname -a'),"\n","","")

function! RunsInWSL ()
  return g:unamea =~ "Linux" && g:unamea =~ "Microsoft"
endfunction

@bousch
Copy link

@bousch bousch commented Feb 24, 2020

Links need to be surrounded by square brackets but I just noticed that not all links work. Not sure why yet.

@llinfeng
Copy link
Author

@llinfeng llinfeng commented Feb 24, 2020

@bousch Thanks a lot! Which emulator do you use for WSL? I use wsltty as it permits mouse-clicks. However, when triggering a [link-tag-text](onenote:xxx) link from WSL-Vim, the terminal Vim is rendered poorly: whatever that did not change (including background color) will be fully black. Moving lines up and down will reveal each line at a time, and resizing the entire window for the emulator shall redraw the full pane.
WSLVim-OneNote

Also, I added one line to your conditionals to make sure gvim.exe on Windows does open OneNote links.

    " If it is an OneNote link
    elseif islink == 2
        if g:uname == 'Android'
            let onenote_command = "!termux-open '" . onenote_link . "'"
        elseif !has('dnd') && (has('windows') || has('win32'))
            silent! exe '!start /MIN cmd /c "start "" "' . onenote_link . '" "'
            return 1
        else
            " From WSL the ampersands must be escaped like this ^&
            let onenote_command = '!cmd.exe /c start "" "' . substitute(onenote_link,"&","^&",'g') . '" 2> /dev/null'
        endif
        silent! exe onenote_command
        return 1
    " End of the if-OneNote piece


Lastly, I cannot reproduce your quote as below:

Links need to be surrounded by square brackets but I just noticed that not all links work. Not sure why yet.

Per my Vimwiki setup, I used Markdown syntax and the following links in Vimwiki are all running smoothly:

[Test `[` page ](https://onedrive.live.com/view.aspx?XXX)
[Test `[` page ](onenote:https://d.docs.live.net/XXX)

Both are pointing to a OneNote page named Test [ page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants