1

I'm using pdftools when displaying pdfs and it would be nice if emacs could bookmark the pdf with the default filename when I kill that buffer. I've got something like this, but it doesn't work.

;; Set a bookmark on kill
(add-hook 'kill-buffer-hook `bookmark-set)

UPDATE

I've remarked out all my configuration apart from the add-hook line above. When I open up a pdf file which I want to test with bookmarking on exit I get the following error;

bookmark-buffer-file-name: Buffer not visiting a file or directory
Making completion list... [2 times]
File Linux-Voice-Issue-027.pdf is large (73.0M), really open? (y or n) y
File mode specification error: (error "Buffer not visiting a file or directory")
bookmark-buffer-file-name: Buffer not visiting a file or directory

UPDATE

After putting the code 'chen bin' has suggested I'm getting a lot further. However now when I commit a change using Magit I get the following error and the commit buffer doesn't close, although the commit happens.

Type C-c C-c to finish, or C-c C-k to cancel
Saving file /home/map7/testgit/.git/COMMIT_EDITMSG...
Wrote /home/map7/testgit/.git/COMMIT_EDITMSG
string=: Wrong type argument: char-or-string-p, nil
Git finished
(No files need saving)

UPDATE

I now understand that I also need to test if my buffer file I'm closing has an extension.

(defun kill-buffer-hook-setup ()
  (if (and buffer-file-name
           (file-name-extension buffer-file-name)
           (string= (downcase (file-name-extension buffer-file-name)) "pdf")
           (yes-or-no-p "Set bookmark with current file name?"))
      (bookmark-set (file-name-nondirectory buffer-file-name) nil)))
(add-hook 'kill-buffer-hook 'kill-buffer-hook-setup)
  • 2
    Could you please give details on "it doesn't work"? – JeanPierre May 7 '16 at 11:44
2

You got typo and didn't cover corner cases.

(defun kill-buffer-hook-setup ()
  (if (and buffer-file-name
           (file-name-extension buffer-file-name)
           (string= (downcase (file-name-extension buffer-file-name)) "pdf")
           (yes-or-no-p "Set bookmark with current file name?"))
    (bookmark-set (file-name-nondirectory buffer-file-name) nil)))
(add-hook 'kill-buffer-hook 'kill-buffer-hook-setup)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.