Skip to content
This repository has been archived by the owner on Aug 7, 2022. It is now read-only.

Remember PDF positions across sessions #18

Closed
oscarfv opened this issue Dec 5, 2014 · 31 comments
Closed

Remember PDF positions across sessions #18

oscarfv opened this issue Dec 5, 2014 · 31 comments

Comments

@oscarfv
Copy link

oscarfv commented Dec 5, 2014

For using pdf-tools as a book reader it would be useful to remember the visited files and the position, zoom level...

@politza
Copy link
Owner

politza commented Dec 6, 2014

Just use bookmark.el for that, maybe with a dedicated file for PDF
documents. What we could do, is store display attributes like
image-size, slice and image-origin in a bookmark. Which I just did.

@oscarfv
Copy link
Author

oscarfv commented Dec 6, 2014

Thank you.

It would be useful to automatically set a bookmark when the PDF buffer is killed. I can solve this locally with kill-buffer-hook, which is more convenient for my use case, because I don't want to set those bookmarks for certain PDFs (ephemeral ones.)

Thanks again.

@manuel-uberti
Copy link

@oscarfv could you share your solution? I would really like to save the position in the PDF I am reading.

@alezost
Copy link

alezost commented May 13, 2015

Just a related note: there is org-pdfview package. It also may be used to save a page.

@politza politza reopened this Jun 19, 2015
@politza politza changed the title Bookmarking/remembering files Remember PDF positions across sessions Oct 20, 2015
@Andre0991
Copy link

I second this suggestion, remembering the last page would be tremendously useful.

I mean, transparently, without bookmarking or doing something manually, although I'll try something like bookmarking with a hook for now.

@Andre0991
Copy link

Well, I tried this:

;; TODO: save bookmarks in separate file
(defun andre//pdf-tools--set-bookmark-pdf ()
    (when (eq major-mode 'pdf-view-mode)
      (bookmark-set (andre//pdf-tools--generate-bookmark-name-for-this-buffer))))

(defun andre//pdf-tools--jump-to-last-page-viewed-in-last-session ()
  (when (andre//pdf-tools--buffer-has-bookmark-from-previous-session)
    (bookmark-jump (andre//pdf-tools--generate-bookmark-name-for-this-buffer))))

(defun andre//pdf-tools--buffer-has-bookmark-from-previous-session ()
  (assoc (andre//pdf-tools--generate-bookmark-name-for-this-buffer) bookmark-alist))

(defun andre//pdf-tools--generate-bookmark-name-for-this-buffer ()
  (concat "PDF: "(buffer-file-name)))

;; using this spacemacs' function, but it's the same thing except that it takes a list of funtions
(spacemacs/add-to-hook 'kill-buffer-hook '(spacemacs//pdf-tools--set-bookmark-pdf))
(spacemacs/add-to-hook 'pdf-view-mode-hook '(spacemacs//pdf-tools--jump-to-last-page-viewed-in-last-session))

But it's always saving a bookmark on the first page. I wonder if it has to do with the hook. Maybe when it runs the position on the buffer is lost, I don't know (I don't know much elisp, still learning).

Anyone?

Edit: Actually, I suspect that it always sets the bookmark to the first page when it is not called interactively.
Edit: No, it doesn't have to do with this.

@Andre0991
Copy link

OK, this is rather ugly, but it works:

;; --- Save last page viewed in pdf-view-mode ---
;; Ideally we would set this as hook instead of an advice.
;; However, for some reason, if this is done, it sets the bookmark to the first page of the PDF.
;; TODO: Save those bookmarks in another file; don't show them on the list of bookmarks.

(defun andre//pdf-tools--generate-bookmark-name-for-this-buffer ()
  (concat "PDF: " (buffer-file-name)))

(defun andre//pdf-tools--set-bookmark-pdf (&optional arg1)
  (when (eq major-mode 'pdf-view-mode)
    (bookmark-set (andre//pdf-tools--generate-bookmark-name-for-this-buffer))))

(defun andre//pdf-tools--jump-to-last-page-viewed-in-last-session ()
  (bookmark-maybe-load-default-file)
  (condition-case err
      (bookmark-jump (andre//pdf-tools--generate-bookmark-name-for-this-buffer))
    (error
     (princ (format "Unable to get to last bookmark. Error: %s" err)))))

(defun andre//pdf-tools--set-bookmarks-before-quitting-emacs (&optional arg1)
  (cl-loop for buffer in (buffer-list) do
           (with-current-buffer buffer
               (when (eq major-mode 'pdf-view-mode)
                 (andre//pdf-tools--set-bookmark-pdf)))))

(add-hook 'pdf-view-mode-hook 'spacemacs//pdf-tools--jump-to-last-page-viewed-in-last-session)
(advice-add 'kill-buffer :before 'andre//pdf-tools--set-bookmark-pdf)
(advice-add 'kill-emacs :before 'andre//pdf-tools--set-bookmarks-before-quitting-emacs)
;; --- End save last page viewed in pdf-view-mode ---

Edit: Oh, actually this doesn't work properly across Emacs sessions (sometimes it does). I give up for now, haha.

Edit 2: OK, I got it. It doesn't work across emacs sessions because of #111.

@boyanpenkov
Copy link

boyanpenkov commented Aug 4, 2016

Can confirm Andre0991's works for me, on vanilla emacs (debian).

Edit 1: nope, never mind -- I do see similar erroneous behavior to what Andre was seeing.

@politza
Copy link
Owner

politza commented Dec 28, 2016

Andre's first solution seems to work/should work, that is after correcting the function names added to the hooks.

@boyanpenkov
Copy link

What function names need to be changed?

@braham-snyder
Copy link

braham-snyder commented Feb 7, 2017

The functions being added to the hooks just weren't consistent with those defined earlier in the snippet.

    ;; workaround for pdf-tools not reopening to last-viewed page of the pdf:
    ;; https://github.com/politza/pdf-tools/issues/18
    ;; TODO: save bookmarks in separate file

    (defun brds/pdf-set-last-viewed-bookmark ()
      (when (eq major-mode 'pdf-view-mode)
        (bookmark-set (brds/pdf-generate-bookmark-name))))

    (defun brds/pdf-jump-last-viewed-bookmark ()
      (when
          (brds/pdf-has-last-viewed-bookmark)
        (bookmark-jump (brds/pdf-generate-bookmark-name))))

    (defun brds/pdf-has-last-viewed-bookmark ()
      (assoc
       (brds/pdf-generate-bookmark-name) bookmark-alist))

    (defun brds/pdf-generate-bookmark-name ()
      (concat "PDF-LAST-VIEWED: " (buffer-file-name)))

    (add-hook 'kill-buffer-hook 'brds/pdf-set-last-viewed-bookmark)
    (add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark)

@boyanpenkov
Copy link

Am I missing something here? This does not touch my ~/.emacs.d/bookmarks after a few restarts -- while the same files under doc-view-mode work well.

@braham-snyder
Copy link

My bad--I hadn't tested the kill-emacs-hook bit well enough (I thought I had a weird persp-mode-related bug causing bookmark issues).

No one has mentioned save-place-mode? I've only looked at it briefly, but, e.g., it correctly handles the kill-emacs-hook case (see save-places-to-alist)--perhaps it just needs to be slightly modified to work with pdf-view buffers? At the least, it seems like the perfect reference for this issue. Unfortunately, I can't work on this in the near future.

@boyanpenkov
Copy link

Same-pace does sound good -- I had played with this before, but suspect that the failure mode here is that the pdf-view regnerates the file every time emacs opens.

FWIW, my short-term solution is to take PDFs I want to save and open then in doc-view (which converts each to a series of png's), on which save-place and the like work well. However, this is clearly a bad hack.

@politza
Copy link
Owner

politza commented Feb 8, 2017 via email

@boyanpenkov
Copy link

On a technical note, I'm not claiming that save-place has special code -- I do observe that my combination of save-place and bookmarks works for doc-view (but not pdf-view).

And I'm certainly under-educated here, so someone who knows should speak up.

@braham-snyder
Copy link

braham-snyder commented Mar 22, 2017

The kill-emacs-hook bit finally bothered me enough to attempt to fix it--surprisingly, this has worked perfectly for me the last few days (only changes are the last defun and add-hook calls):

    ;; workaround for pdf-tools not reopening to last-viewed page of the pdf:
    ;; https://github.com/politza/pdf-tools/issues/18#issuecomment-269515117
    (defun brds/pdf-set-last-viewed-bookmark ()
      (interactive)
      (when (eq major-mode 'pdf-view-mode)
        (bookmark-set (brds/pdf-generate-bookmark-name))))

    (defun brds/pdf-jump-last-viewed-bookmark ()
      (when
          (brds/pdf-has-last-viewed-bookmark)
        (bookmark-jump (brds/pdf-generate-bookmark-name))))

    (defun brds/pdf-has-last-viewed-bookmark ()
      (assoc
       (brds/pdf-generate-bookmark-name) bookmark-alist))

    (defun brds/pdf-generate-bookmark-name ()
      (concat "PDF-LAST-VIEWED: " (buffer-file-name)))

    (defun brds/pdf-set-all-last-viewed-bookmarks ()
      (dolist (buf (buffer-list))
        (with-current-buffer buf
            (brds/pdf-set-last-viewed-bookmark))))

    (add-hook 'kill-buffer-hook 'brds/pdf-set-last-viewed-bookmark)
    (add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark)
    (unless noninteractive  ; as `save-place-mode' does
      (add-hook 'kill-emacs-hook #'brds/pdf-set-all-last-viewed-bookmarks))

@boyanpenkov was something similar to this broken for you?

Tangentially, as noted in #199, my place is still occasionally lost intra-session (though that can be worked-around with a call to pdf-history-backward).

edit: Oh, this is barely different from the version in andre's second post--were you guys encountering something other than #199? I think that's the only related issue I'm still seeing.

@boyanpenkov
Copy link

After incorporating the change and restarting emacs a few times, no dice, I'm afraid....

@aijony
Copy link

aijony commented May 27, 2017

After restarting emacs, brds/pdf-has-last-viewed-bookmark returned nil unless I initialized the bookmark system so I put in (bokmark-set "fake"). This would only have to be done at the start up of emacs, but I was lazy. Edit: I have no clue how anything works

This is what I had to do to get it to work for me:

Thanks Braham!

 
    ;; workaround for pdf-tools not reopening to last-viewed page of the pdf:
    ;; https://github.com/politza/pdf-tools/issues/18#issuecomment-269515117
    (defun brds/pdf-set-last-viewed-bookmark ()
      (interactive)
      (when (eq major-mode 'pdf-view-mode)
        (bookmark-set (brds/pdf-generate-bookmark-name))))

    (defun brds/pdf-jump-last-viewed-bookmark ()
      (bookmark-set "fake") ; this is new
      (when
          (brds/pdf-has-last-viewed-bookmark)
        (bookmark-jump (brds/pdf-generate-bookmark-name))))

    (defun brds/pdf-has-last-viewed-bookmark ()
      (assoc
       (brds/pdf-generate-bookmark-name) bookmark-alist))

    (defun brds/pdf-generate-bookmark-name ()
      (concat "PDF-LAST-VIEWED: " (buffer-file-name)))

    (defun brds/pdf-set-all-last-viewed-bookmarks ()
      (dolist (buf (buffer-list))
        (with-current-buffer buf
            (brds/pdf-set-last-viewed-bookmark))))

    (add-hook 'kill-buffer-hook 'brds/pdf-set-last-viewed-bookmark)
    (add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark)
    (unless noninteractive  ; as `save-place-mode' does
      (add-hook 'kill-emacs-hook #'brds/pdf-set-all-last-viewed-bookmarks))`

@andrewSteer
Copy link

I would just like to subscribe to this request. Saving PDF positions (maybe also zoom?) between sessions would be amazing. It is a bit annoying to have to find exactly where I was last time I checked the pdf, or having to manually set a bookmark just for that purpose (besides re-zooming every single time I open a PDF).

@braham-snyder
Copy link

You can subscribe to any GitHub issue by clicking the "Subscribe" button at the top of the issue's page on the right hand side.

If you'd just like to express your support of an issue, I (and I suspect other subscribers) would greatly appreciate it if you gave the OP a thumbs up (by clicking the smiley face in its top right corner) instead of making a "+1" post.

(In case you haven't seen it, GitHub's issue tracker can sort issues by the numbers of reactions they've garnered -- ideally, IMO, we'd have explicit issue voting with votes displayed next to the number of comments as in isaacs/github#9 or, e.g., GitLab, but that doesn't look like it will happen in the near future.)

@caadar
Copy link

caadar commented Sep 20, 2017

(Unusable) variant with records stored in separate file:

(require 'bookmark+)

(setq bookmarks-pdf "~/.emacs.d/bookmarks-pdf")

(defun brds/pdf-set-last-viewed-bookmark ()
  (interactive)
  (when (eq major-mode 'pdf-view-mode)
	(bmkp-switch-bookmark-file-create bookmarks-pdf t)
	(bookmark-set (brds/pdf-generate-bookmark-name))
	(bmkp-switch-bookmark-file-create bookmark-default-file t)))

(defun brds/pdf-jump-last-viewed-bookmark ()
  (bmkp-switch-bookmark-file-create bookmarks-pdf t)
  (bookmark-set "PDF-LAST-VIEWED: fake") ; this is new
  (when
	  (brds/pdf-has-last-viewed-bookmark)
	(bookmark-jump (brds/pdf-generate-bookmark-name)))
  (bmkp-switch-bookmark-file-create bookmark-default-file t))

(defun brds/pdf-has-last-viewed-bookmark ()
  (assoc
   (brds/pdf-generate-bookmark-name) bmkp-latest-bookmark-alist))

(defun brds/pdf-generate-bookmark-name ()
  (concat "PDF-LAST-VIEWED: " (buffer-file-name)))

(defun brds/pdf-set-all-last-viewed-bookmarks ()
  (dolist (buf (buffer-list))
	(with-current-buffer buf
	  (brds/pdf-set-last-viewed-bookmark))))

(add-hook 'kill-buffer-hook 'brds/pdf-set-last-viewed-bookmark)
(add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark)
(unless noninteractive					; as `save-place-mode' does
  (add-hook 'kill-emacs-hook #'brds/pdf-set-all-last-viewed-bookmarks))

It's slow cuz bookmark files savings.

@agzam
Copy link

agzam commented Jul 9, 2019

What's the update on this issue? Did people give up? I mean it's been hanging around 2014. This and absence of continuous scrolling unfortunately makes pdf-tools quite inconvenient (almost unusable).

@politza
Copy link
Owner

politza commented Jul 9, 2019 via email

@agzam
Copy link

agzam commented Jul 9, 2019

My apologies if my comment makes it look demanding - wasn't my intention. I am genuinely interested, because I have recently re-discovered pdf-tools and even with all the downsides and issues it seems to be the best solution these days for handling pdf in Emacs, especially if you want to use Interleave or Org-noter.

@politza
Copy link
Owner

politza commented Jul 10, 2019 via email

@ghost
Copy link

ghost commented Sep 17, 2019

Here's an update to aijony's version without the fake bookmark kludge. Which works for me...

;; workaround for pdf-tools not reopening to last-viewed page of the pdf:
;; https://github.com/politza/pdf-tools/issues/18#issuecomment-269515117
(defun brds/pdf-set-last-viewed-bookmark ()
  (interactive)
  (when (eq major-mode 'pdf-view-mode)
    (bookmark-set (brds/pdf-generate-bookmark-name))))

(defun brds/pdf-jump-last-viewed-bookmark ()
  (when
      (brds/pdf-has-last-viewed-bookmark)
    (bookmark-jump (brds/pdf-generate-bookmark-name))))

(defun brds/pdf-has-last-viewed-bookmark ()
  (member (brds/pdf-generate-bookmark-name) (bookmark-all-names)))

(defun brds/pdf-generate-bookmark-name ()
  (concat "PDF-LAST-VIEWED: " (buffer-file-name)))

(defun brds/pdf-set-all-last-viewed-bookmarks ()
  (dolist (buf (buffer-list))
    (with-current-buffer buf
      (brds/pdf-set-last-viewed-bookmark))))

(add-hook 'kill-buffer-hook 'brds/pdf-set-last-viewed-bookmark)
(add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark)
(unless noninteractive  ; as `save-place-mode' does
  (add-hook 'kill-emacs-hook #'brds/pdf-set-all-last-viewed-bookmarks)))
`

@AloisJanicek
Copy link

I would just like to point out that if you decide to restore last position in mode hook like this
(add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark) and you will try to open org-pdfview or org-pdftools link pointing to specific page, those two things will collide and you will end up with two pdf buffers: one showing linked location and the other one showing restored location.

Best way to use both org-pdfview links to pages and brds/* functions is to make convenient key binding in pdf-view-mode-map for brds/pdf-jump-last-viewed-bookmark and restore last position only if you really need it.

@nhmacuk
Copy link

nhmacuk commented Apr 4, 2020

#18 (comment) sadly this didn't work for me. Does it still work for you?
I have succes using https://github.com/007kevin/pdf-view-restore. Maybe it would be nice to add that to pdf-view?

@ghost
Copy link

ghost commented Jun 22, 2020

I would just like to point out that if you decide to restore last position in mode hook like this
(add-hook 'pdf-view-mode-hook 'brds/pdf-jump-last-viewed-bookmark) and you will try to open org-pdfview or org-pdftools link pointing to specific page, those two things will collide and you will end up with two pdf buffers: one showing linked location and the other one showing restored location.

I'm trying out org-pdftools and that particular problem can be solved by wrapping the call to bookmark-jump like this: (save-window-excursion (bookmark-jump (brds/pdf-generate-bookmark-name))). I guess this also seems to make it work properly with commands such as find-file-other-window, though I don't know of any side-effects (the form comes with a usage warning).

@nicolaisingh
Copy link

nicolaisingh commented Jul 28, 2020

Hi, here's a package I wrote (saveplace-pdf-view) that implements this functionality with the help of save-place-mode. It's also now available in MELPA. I've been using it for quite a while, and so far it works for my setup.

@politza politza closed this as completed Aug 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests