jalvesaq / zotcite

Neovim plugin for integration with Zotero
GNU General Public License v3.0
174 stars 15 forks source link

Open item in Zotero from vim buffer #79

Open artkpv opened 4 months ago

artkpv commented 4 months ago

Hi @jalvesaq! Thank you for the tool. How to open an item (book, article, etc.) in Zotero from a zotero link in my vim buffer? I place my cursor at the @<somelinkhere> and press Enter. It opens Zotero but doesn't focus on the item I expect. While other features are working like open attachment, get biblio info etc.

:Zinfo:

Information from the Python module:
  time to load data : 1e-05 (check) + 0.00954 (read) + 0.03217 (write) + 0.01726 (sql)
  zotero.sqlite     : /home/**/mydir/ref/Zotero/zotero.sqlite
  tmpdir            : /home/***/.cache/zotcite
  data dir          : /home/***/mydir/ref/Zotero
  references found  : 392
  citation template : {Authors}_{Year}
  excluded fields   : []
  attachments dir   : /home/***/mydir/books
  docs              : {}
  zotero.py         : /home/****/mydir/ref/dotfiles/nvim/pack/minpac/start/zotcite/python3/zotero.py
  banned words      : a an the some from on in to of do with
~/.config/nvim/pack/minpac/start/zotcite master
❯ g log -1
commit 9753c2629cefa3bc388e3bac4ac65dfb9d5a3610 (grafted, HEAD -> master, origin/master, origin/HEAD)
Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
Date:   Sat Mar 16 12:20:47 2024 -0300

    Make :Zinfo show the time to update the database
jalvesaq commented 4 months ago

It doesn't work on my side either. Please, update the plugin and put let zotcite_wait_attachment = 1 in your vimrc.

After that, I can see the following error message:

Error running `xdg-open "zotero://open-pdf/library/items/GWCTFLHS"`: $(realpath: -c: line 2: unexpected EOF while looking for matching `)'

I installed Zotero manually. This should be the reason why xdg-open doesn't know what to do with zotero://.

jalvesaq commented 4 months ago

I edited my zotero.desktop file and replaced

Exec=bash -c "$(dirname $(realpath $(echo %k | sed -e 's/^file:\/\///')))/zotero -url %U"

with

Exec=/path_to/zotero -url %U"

and now I can replicate the issue as you reported. Maybe the ZotFile extension is required (see https://forums.zotero.org/discussion/73776/create-external-link-to-open-pdf-within-zotero), but it's installed here...

This feature was introduced by @jasonccox

artkpv commented 4 months ago

@jalvesaq I can open attachments by pressing <leader>zo for pdfs, htmls. The issue is about openning an item inside the Zotero program which means a record there that contains biblio info, links to PDFs, or PDFs themselves, etc. When I place a cursor inside my vim buffer on a @some_id_author_year and press Enter key then the zotero window appears but it does not focus the item inside Zotero. Hope it is clear now what's the issue.

I've tried let zotcite_wait_attachment = 1 it doesn't show anything

jalvesaq commented 4 months ago

Same bug here. I don't know how to fix it.

jasonccox commented 4 months ago

Chiming in as the original author of this feature. I don't have Zotero installed anymore, so I can't do much debugging right now, but I'd guess that the issue is related to the logic to build the zotero:// URL: https://github.com/jalvesaq/zotcite/blob/4f1d669e704900ced262a03ea4325dc94bc72b04/autoload/zotcite.vim#L251. I don't recall how I figured out how to construct that URL, and I'm not finding any documentation on those URLs from a quick search either.

jalvesaq commented 4 months ago

Thanks, @jasonccox!

When we can't find anything on the web, we have to look at the source code. I downloaded Zotero from Git Hub and used grep to search the string "open.*pdf". Part of the output was:

chrome/content/zotero/ZoteroProtocolHandler.jsm:         * zotero://open-pdf/library/items/[itemKey]?page=[page]
chrome/content/zotero/ZoteroProtocolHandler.jsm:         * zotero://open-pdf/groups/[groupID]/items/[itemKey]?page=[page]
chrome/content/zotero/ZoteroProtocolHandler.jsm:         * zotero://open-pdf/[libraryID]_[key]/[page]

The first line has the command that we are already using, but it doesn't work.

Looking at the code, I found:

image

whose part of the diff is:

-           uriPath = uriPath.substr('//open-pdf/'.length);
-           var mimeType, content = '';
+           uriPath = uriPath.replace(/^\/\/open(-pdf)?\//, '');

And in Zotero, clicking on "Help / Debug Output Logging / View Output", I find:

[JavaScript Error: "URIError: malformed URI sequence" {file: "resource://gre/modules/osfile/ospath_unix.jsm" line: 188}]

This is the end of my research, and I still don't know how to fix the bug.

Perhaps, someone will find a solution. I will wait a few days before removing the option zotcite_open_in_zotero.

adam-coates commented 4 months ago

I also experience the issues pointed out here. I notice though that the @ZoteroKey# 's are different for a parent and child Item. Using the ZotFile plugin to get the keys for each (ie directly in zotero itself right clicking and selecting "copy select item links" https://forums.zotero.org/discussion/comment/317284/#Comment_317284, I notice that I get two different ones for the citation and the pdf itself.

So when I do: xdg-open "zotero://open-pdf/library/items/NMPZXK65" generated from the original @ZoteroKey# zotero opens but no pdf opens but when I do xdg-open "zotero://open-pdf/library/items/C892Q6IM" generated from the pdf itself then the pdf opens.

How are the URI's made? Based on the original @ZoteroKey# or by the key associated with the pdf.

Here's a snippet from the sqlite database

{"key":"MSNST3S9","version":1571,"parentItem":"C892Q6IM","itemType"

In the above snippet both MSNST3S9 and C892Q6IM work to open the pdf.

jalvesaq commented 4 months ago

Would you be able to make a pull request fixing this issue?

adam-coates commented 4 months ago

I can, I just noticed though that if I set in config open_in_zotero = true and then press <leader>zo I can get it to work and the ID used for the URI is correct, this is referring to the lines here:

https://github.com/jalvesaq/zotcite/blob/71e495875238a1186e66472307e810112b7f3404/lua/zotcite/get.lua#L13-L16

Here's an example:

@9IQRHJF2#Remedios_Logothetis_Kayser-2014 IP2MW2MU

Then I'm a bit confused about where the logic is failing in this, since enter for me is not doing anything anymore since moving the merge from lua to master.

jalvesaq commented 4 months ago

My guess is that this commit caused the problem:

https://github.com/zotero/zotero/commit/a7d59a90d37695e4eaa0e42d9e2301b049d0a0e7