#+TITLE: Media and Books Configuration
#+PROPERTY: header-args :tangle yes

* Read ebooks (calibredb)

#+begin_src elisp
  (use-package calibredb
    :defer t
    :config
    (setq calibredb-format-all-the-icons t)
    (setq calibredb-format-icons-in-terminal t))
#+end_src

#+begin_src elisp
  (defvar calibredb-root-dir (expand-file-name "~/memex/library/books"))
  (defvar calibredb-db-dir (expand-file-name "metadata.db" calibredb-root-dir))
  (defvar calibredb-id-width 6)
  (defvar calibredb-title-width 100)
#+end_src

* PDF Tools

#+begin_src elisp
  (use-package pdf-tools
    :mode ("\\.pdf\\'" . pdf-view-mode)
    :config
    (pdf-tools-install :no-query)
    (setq-default pdf-view-display-size 'fit-page)
    (setq pdf-annot-activate-created-annotations t))
#+end_src

* Annotate PDFs and EPUBs (org-noter)

#+begin_src elisp
  (use-package org-noter
    :config
    (setq org-noter-notes-search-path (list (expand-file-name "~/memex/library/books")))
    (setq org-noter-default-notes-file-names '("books.org")))
#+end_src

#+begin_src elisp :tangle ~/.emacs.d/custom.el
  (defvar org-noter-notes-search-path (list (concat org-directory "/library/books")))
  (defvar org-noter-default-notes-file-names '("books.org"))  
#+end_src

* Link PDFs (org-noter-pdftools)

#+begin_src elisp
  (use-package org-noter-pdftools
    :after org-noter
    :config
    ;; Add a function to ensure precise note is inserted
    (defun org-noter-pdftools-insert-precise-note (&optional toggle-no-questions)
      (interactive "P")
      (org-noter--with-valid-session
       (let ((org-noter-insert-note-no-questions (if toggle-no-questions
  						   (not org-noter-insert-note-no-questions)
                                                   org-noter-insert-note-no-questions))
             (org-pdftools-use-isearch-link t)
             (org-pdftools-use-freepointer-annot t))
         (org-noter-insert-note (org-noter--get-precise-info)))))

    ;; fix https://github.com/weirdNox/org-noter/pull/93/commits/f8349ae7575e599f375de1be6be2d0d5de4e6cbf
    (defun org-noter-set-start-location (&optional arg)
      "When opening a session with this document, go to the current location.
  With a prefix ARG, remove start location."
      (interactive "P")
      (org-noter--with-valid-session
       (let ((inhibit-read-only t)
             (ast (org-noter--parse-root))
             (location (org-noter--doc-approx-location (when (called-interactively-p 'any) 'interactive))))
         (with-current-buffer (org-noter--session-notes-buffer session)
           (org-with-wide-buffer
            (goto-char (org-element-property :begin ast))
            (if arg
                (org-entry-delete nil org-noter-property-note-location)
              (org-entry-put nil org-noter-property-note-location
                             (org-noter--pretty-print-location location))))))))
    (with-eval-after-load 'pdf-annot
      (add-hook 'pdf-annot-activate-handler-functions #'org-noter-pdftools-jump-to-note)
      )
    )
#+end_src

* View EPUBs (nov.el)

#+begin_src elisp :tangle yes
  (use-package nov
    :config
    (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode))
    )
#+end_src

* Zotero (helm-bibtex)
#+begin_src elisp :tangle yes
  (use-package helm-bibtex)
#+end_src

#+begin_src elisp :tangle ~/.emacs.d/custom.el
  (defvar bibtex-completion-bibliography '("~/bibliography/zotero.bib"))
#+end_src