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

PDF annotations disappear upon saving #28

Open
faaip opened this issue Jun 9, 2021 · 8 comments
Open

PDF annotations disappear upon saving #28

faaip opened this issue Jun 9, 2021 · 8 comments
Labels
needs investigation Something isn't working, need to understand what the actual problem is pending review Maintainer needs to review the changes ux Improving the User Experience of the library

Comments

@faaip
Copy link

faaip commented Jun 9, 2021

Hi!

I'm running into an error on macOS where the annotations disappear upon saving the pdf.

To reproduce:

  1. Create annotation C-c C-a h
  2. Save pdf (:w)
  3. Scroll back and forth in the PDF and the annotation has now disappeared.

Do you have any idea what might be the issue?

Thank you for taking this fork.

@vedang
Copy link
Owner

vedang commented Jun 20, 2021

Hey @faaip,

I am not able to reproduce your problem (I don't use evil-mode). My
steps are as follows:

  • Select a region of text with mouse-selection
  • Create an annotation with C-c C-a h
  • Save the buffer with C-x C-s
  • Scroll the buffer few pages up and down
  • Reload the file from disk using C-x C-v

In the above cases, the annotation remains and is not lost.

Perhaps it's possible that the PDF file you are reading is uncovering
some edge-case in the code. If it's possible for you to share the PDF,
I will try to add annotations on my own machine to check if this is
the case.

@fapdash
Copy link

fapdash commented Jul 25, 2021

I think this might be related to politza/pdf-tools#646, fuxialexander/org-pdftools#68 and https://org-roam.discourse.group/t/cannot-save-annotations-in-org-noter-pdf-and-see-when-i-open-up-again/915

I'm experiencing a similar issue but since there are so many different packages involved I'm not sure where ask for help.
So sorry if this is the wrong package.

Things that I have notices:

  • my pdftools buffer starts in read-only mode
  • when I try to exit it complains about the buffer not having been saved
  • the buffer can't be saved because it's in read-only mode

My workaround currently looks like this:

When reopening the pdf via org-noter I get the error:

(error "epdfinfo: No such annotation: annot-1-6")
  signal(error ("epdfinfo: No such annotation: annot-1-6"))
  error("epdfinfo: %s" "No such annotation: annot-1-6")
  pdf-info-query(getannot "/home/fap/repos/org/roam/bib_notes/pdf/foobar1970...." "annot-1-6")
  pdf-info-getannot("annot-1-6")
  ...

This can be solved by activating pdf-annot-minor-mode. [0]
After that I can navigate the annotatios as expected.

It seems quite complicated to reproduce it since several packages seem to interact in a way that breaks stuff:

  • pdf-tools
  • org-pdftools
  • org-noter-pdftools
  • org-noter

I think we need to figure out why the pdf buffer gets

  1. loaded in read-only mode?
  2. is not saved automatically after adding an annotation?

@vedang If this is not directly caused by pdf-tools. Do you have an idea how some of the other packages could mess with pdf annotation saving?

[0] I had to add (add-hook 'pdf-view-mode-hook #'pdf-annot-minor-mode) to my config.
Maybe this could be added to the README?

@fapdash
Copy link

fapdash commented Jul 26, 2021

I found a fix for this here:
https://github.com/stardiviner/emacs.d/blob/d149a4cb0f4520c92b4f3f9564db1e542d571d2c/init/Emacs/init-emacs-pdf.el#L58-L60

  (add-hook 'pdf-view-mode-hook #'pdf-annot-minor-mode)
  (add-hook 'pdf-view-mode-hook (lambda () (read-only-mode 0)))
  ;; save after adding annotation comment
  (advice-add 'pdf-annot-edit-contents-commit :after 'save-buffer)

@willis-richard
Copy link

willis-richard commented Jul 27, 2021

@fapdash. Thank you for linking to the other issue.

I am a novice (if it wasn't obvious), so take what I'm saying with a pinch of salt...
I have tried this fix that you posted, with my spacemacs + evil mode.

  • Calling ':wa' on the org-noter document now saves the pdf annotations too. Great! This is most of the problem solved.
  • Calling 'SPC f s' on the pdf also works, as it did before the fix.
  • ':w' on the pdf still fails to save it properly and results in dropping the annotations.
  • Closing emacs without saving the pdf, and then clicking 'yes save changes to buffer' also results in the annotations being dropped.

All in all, this is a good fix, I just need to get in the habit of calling ':wa'. However there are still some cases where the annotations get dropped.

@fapdash
Copy link

fapdash commented Jul 27, 2021

@Muff2n I have no experience witth Spacemacs and evil-mode.
You might want to find out what functions are actually called by :w and :wa and go from there with further debugging the issue.
In normal emacs you do this with describe-key (C-h k), don't know if this will work for you in evil-mode.
Otherwise you might want to ask for that on https://emacs.stackexchange.com

@willis-richard
Copy link

willis-richard commented Jul 28, 2021

I have determined that ':w' leads to the command 'evil-write' being called. This is a function defined in evil-commands.el, which is a spacemacs file. Unfortunately my elisp isn't great, so I am not able to understand more than that it will try to call save-buffer (which does work on the pdf) but there is more complicated logic in the file, which must be responsible for the failure. Where do you think would be the best place to report this? I've never experienced evil-write not working before.

@fapdash
Copy link

fapdash commented Jul 28, 2021

@Muff2n evil-write is defined here: https://github.com/emacs-evil/evil/blob/5c28294d830a5a79e9b9da2c32e7675d52d76720/evil-commands.el#L3027-L3065
So you might file a bug report to https://github.com/emacs-evil/evil
You can also learn how to use edebug to debug the code yourself and see at which point evil-write fails.

@vedang vedang added needs investigation Something isn't working, need to understand what the actual problem is pending review Maintainer needs to review the changes ux Improving the User Experience of the library labels Dec 31, 2021
@workcomplete
Copy link

@fapdash Thanks for sharing this fix! Combined with my autosave config, annotations are now saving without needing to explicitly call write/save from the pdf file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation Something isn't working, need to understand what the actual problem is pending review Maintainer needs to review the changes ux Improving the User Experience of the library
Projects
None yet
Development

No branches or pull requests

5 participants