lervag / wiki.vim

A wiki plugin for Vim
MIT License
652 stars 69 forks source link

citekey: link to BibDesk (in wiki.vim too) [feature request] #341

Closed alexandreroberts closed 6 months ago

alexandreroberts commented 6 months ago

This is a feature request just like the one I just opened for vimtex. It too is based on an old thread from a year ago about how doing so might be feasible.

What I would like is to be able to enter a single command (in the case of wiki.vim, simply <CR>) in order to open the citekey under the cursor in BibDesk (on MacOS). Unlike in vimtex, I don't think wiki.vim "knows" which .bib file to use, so perhaps it would make most sense for it to simply be a reference to whatever .bib file is open in BibDesk at the time. I think this might be what open x-bdsk://citekey (see description here) does by default anyway.

lervag commented 6 months ago

Just for completeness, could you give one or more examples of wiki text with citekeys?

As a very simple, first step, I've added a handler for bdsk: schemes. Thus, if you write a link like bdsk:citekey, then I believe it should work as expected already.

alexandreroberts commented 6 months ago

Just for completeness, could you give one or more examples of wiki text with citekeys?

Here is an example: See @ei2 and @BS. (The cite keys are ei2 and BS, respectively. Note that final dots do not count as part of the citekey, as we determined here.)

As a very simple, first step, I've added a handler for bdsk: schemes. Thus, if you write a link like bdsk:citekey, then I believe it should work as expected already.

I've just tested it and can confirm that it works: when I type <CR> while the cursor is over the text bdsk:ei2 in a wiki file, BibDesk opens and displays the ei2 entry.

lervag commented 6 months ago

Ok, so what is missing now is to connect @... with bdsk:.... Or, is it? In fact, it is not. You can change it with:

let g:wiki_link_default_schemes = { 'cite': 'bdsk' }

Can you test this and see if it is actually a sufficient solution here?

alexandreroberts commented 6 months ago

That did it! Once I added this line to my nvim configuration, it worked perfectly: with cursor over the @, e, i, or 2 in @ei2., pressing <CR> brought opened the ei2 entry in BibDesk.

As I tested it further, I noticed a "bug" related to that wiki-ft.vim query of mine from a year ago: if the citekey contains :, then wiki-ft.vim and wiki.vim treat it as if the citekey ended before the :, i.e., both in how it is highlighted and what citekey string wiki.vim passes to BibDesk. Many of my citekeys use the character :.

I think the solution to that "bug" would be the same as for .: count : as part of the citekey unless it is the very last permissible character of a citekey.

alexandreroberts commented 6 months ago

Ditto for +, which I also use in citekeys!

lervag commented 6 months ago

That did it! Once I added this line to my nvim configuration, it worked perfectly: with cursor over the @, e, i, or 2 in @ei2., pressing <CR> brought opened the ei2 entry in BibDesk.

Great, glad to hear it!

As I tested it further, I noticed a "bug" related to that wiki-ft.vim query of mine from a year ago: if the citekey contains :, then wiki-ft.vim and wiki.vim treat it as if the citekey ended before the :, i.e., both in how it is highlighted and what citekey string wiki.vim passes to BibDesk. Many of my citekeys use the character :.

I think the solution to that "bug" would be the same as for .: count : as part of the citekey unless it is the very last permissible character of a citekey.

Could you give a couple of examples of this? And, just for the sake of clarity, is this a bug in wiki.vim or in wiki-ft.vim? Or both?

lervag commented 6 months ago

Nevermind, I think I figured out what you want and I think my latest commit fixes it. Please update and test.

alexandreroberts commented 6 months ago

Thanks for the fix! I've just tested it now, and in the case of + it works perfectly (both wiki-ft.vim, i.e., the highlighting and wiki.vim, i.e., the link to BibDesk).

In the case of : the highlighting is right, but when I type <CR> while the cursor is on a citekey that includes :, nothing happens at all.

lervag commented 6 months ago

Huh, strange. Can you type <leader>wl on that link and post the information you get here?

lervag commented 6 months ago

Also, can you give me an example of how that link looks like?

alexandreroberts commented 6 months ago

The citekey is @john.skylitzes:histories:thurn1973. This is what wiki.vim returns:

 Type:          url
 Match:         skylitzes:histories:thurn1973
 URL:           skylitzes:histories:thurn1973
 Description:   N/A

 Resolved URL object: 
   url:         skylitzes:histories:thurn1973
   stripped:    histories:thurn1973
   origin:      /Users/robe941/Documents/wikiNotepad/index.wiki
   scheme:      skylitzes

So it looks like . and : don't work together in the same citekey.

Here is a screenshot showing that this citekey is nevertheless correctly highlighted:

Screenshot 2024-04-04 at 9 00 51 PM
alexandreroberts commented 6 months ago

Ah, but curiously when I put my cursor over the john part, this is what I get instead:

 Type:          cite
 Match:         @john.skylitzes:histories:thurn1973
 URL:           bdsk:john.skylitzes:histories:thurn1973
 URL (raw):     john.skylitzes:histories:thurn1973
 Description:   N/A

 Resolved URL object: 
   url:         bdsk:john.skylitzes:histories:thurn1973
   stripped:    john.skylitzes:histories:thurn1973
   origin:      /Users/robe941/Documents/wikiNotepad/index.wiki
   scheme:      bdsk

But even then, <CR> does nothing.

alexandreroberts commented 6 months ago

This makes me wonder whether there are two problems:

  1. wiki.vim isn't parsing it quite right
  2. even when it does parse it right, maybe bdsk: links that include : don't work. Maybe the colons need to be somehow escaped in the URL?
lervag commented 6 months ago

I pushed another commit that at least fixes one of these issues. That is, the type should now be cite.

However, the next problem is probably a bit harder. One thing we can try is to see if url encoding works here. You could try something like this:

function! ResolverBdsk(url) abort
  let a:url.stripped = wiki#url#utils#url_encode(a:url.stripped)
  return a:url
endfunction

" Change the wiki scheme resolver
let g:wiki_link_schemes = {
      \ 'bdsk': {
      \   'resolver': function('MyWikiResolver'),
      \ }
      \}

Or, simply just try this to use this link: <bdsk:john.skylitzes%3Ahistories%3Athurn1973>. The %3A is the url encoding for a colon.

alexandreroberts commented 6 months ago

The simple link works!

The function to generalize this solution returns an error message for me:

Screenshot 2024-04-04 at 9 47 27 PM

I also get an error when simply opening my wiki in nvim. Line 18 is the line in the relevant config file that reads let g:wiki_link_schemes = {:

Screenshot 2024-04-04 at 9 47 45 PM

This is the beginning of that config file (plugin/wiki.vim):

" Wiki.vim setup
let g:wiki_root = '~/Documents/wikiNotepad/' 
let g:wiki_filetypes = ['wiki']
let g:wiki_link_target_type = 'wiki'
let g:wiki_link_extension = ''
"let g:wiki_select_method = 'fzf'

" citekeys link to BibDesk
let g:wiki_link_default_schemes = { 'cite': 'bdsk' }

" deal with colons in citekeys
function! ResolverBdsk(url) abort
  let a:url.stripped = wiki#url#utils#url_encode(a:url.stripped)
  return a:url
endfunction

" tell wiki.vim to create links using function to deal with colons
let g:wiki_link_schemes = {
      \ 'bdsk': {
      \   'resolver': function('MyWikiResolver'),
      \ }
      \}
lervag commented 6 months ago

Sorry, my mistake: Notice the name mismatch: MyWikiResolver should be ResolverBdsk.

lervag commented 6 months ago

I could add this url encoding as a default built-in behaviour here, but if so, I should first check if it works with Zotero.

alexandreroberts commented 6 months ago

That fixed it! Now the last link (the one with the colon) works perfectly.

I do think it would make sense to make this the default built-in behavior, assuming it doesn't cause any problems for Zotero. Maybe it could be used only in the case that the BibDesk option is set?

lervag commented 6 months ago

Yes. However, I believe it does make sense to do this at the url level. That is, if I did url encoding only for the cite links, then it won't apply the url encoding for e.g. [[bdsk:LINK]] or <bdsk:...> or similar, only for @my:citation.

And as such, I think the pragmatic choice for now is to just add this as the default behaviour for bdsk url's, and then consider this for Zotero if anyone should want it/ask for it.

alexandreroberts commented 6 months ago

That makes sense to me. Thank you for this significant improvement to wiki.vim!

On Apr 4, 2024, at 10:04 PM, Karl Yngve Lervåg @.***> wrote:

Yes. However, I believe it does make sense to do this at the url level. That is, if I did url encoding only for the cite links, then it won't apply the url encoding for e.g. [[bdsk:LINK]] or or similar, only for @my:citation.

And as such, I think the pragmatic choice for now is to just add this as the default behaviour for bdsk url's, and then consider this for Zotero if anyone should want it/ask for it.

— Reply to this email directly, view it on GitHub https://github.com/lervag/wiki.vim/issues/341#issuecomment-2038110872, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABURL6TUW6BZCBP4JF5JVRDY3WW6DAVCNFSM6AAAAABFSENOSGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZYGEYTAOBXGI. You are receiving this because you authored the thread.

lervag commented 6 months ago

No problem; I pushed the change now, so you can remove the latest update to your config. Let me know if it works :)

alexandreroberts commented 6 months ago

I just updated it. This is now the error that I get:

Screenshot 2024-04-04 at 10 10 45 PM
alexandreroberts commented 6 months ago

(I also removed the additional code from my config.)

lervag commented 6 months ago

Sorry! I'll fix that asap.

lervag commented 6 months ago

There...

alexandreroberts commented 6 months ago

It works now, thank you!

lervag commented 6 months ago

Great, glad to hear it!