#+TITLE: Media and Books Configuration
#+PROPERTY: header-args :tangle ~/.emacs.d/config.el

* 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
  ;; Forcefully reset the variable after loading calibredb
  (defvar calibredb-root-dir (concat (getenv "HOME") "/library/books"))
  (defvar calibredb-db-dir (expand-file-name "metadata.db" calibredb-root-dir))
  					; (defvar calibredb-library-alist (concat (getenv "HOME") "/library/books"))
  ;;  (defvar calibredb-search-page-max-rows 1000)
  (defvar calibredb-id-width 6)
  (defvar calibredb-title-width 100)
  (defvar calibredb-format-width 0)
  (defvar calibredb-date-width 0)
  (defvar calibredb-author-width 20)
  (defvar calibredb-comment-width 0)
  (defvar calibredb-tag-width 0)
#+end_src

Some keybindings

#+begin_src elisp ~/.emacs.d/custom.el
  (defvar calibredb-show-mode-map
    (let ((map (make-sparse-keymap)))
      (define-key map "?" #'calibredb-entry-dispatch)
      (define-key map "o" #'calibredb-find-file)
      (define-key map "O" #'calibredb-find-file-other-frame)
      (define-key map "V" #'calibredb-open-file-with-default-tool)
      (define-key map "s" #'calibredb-set-metadata-dispatch)
      (define-key map "e" #'calibredb-export-dispatch)
      (define-key map "q" #'calibredb-entry-quit)
      (define-key map "y" #'calibredb-yank-dispatch)
      (define-key map "," #'calibredb-quick-look)
      (define-key map "." #'calibredb-dired-open)
      (define-key map "\M-/" #'calibredb-rga)
      (define-key map "\M-t" #'calibredb-set-metadata--tags)
      (define-key map "\M-a" #'calibredb-set-metadata--author_sort)
      (define-key map "\M-A" #'calibredb-set-metadata--authors)
      (define-key map "\M-T" #'calibredb-set-metadata--title)
      (define-key map "\M-c" #'calibredb-set-metadata--comments)
      map)
    "Keymap for `calibredb-show-mode'.")
#+end_src

#+begin_src elisp
  (defvar calibredb-search-mode-map
    (let ((map (make-sparse-keymap)))
      (define-key map [mouse-3] #'calibredb-search-mouse)
      (define-key map (kbd "<RET>") #'calibredb-find-file)
      (define-key map "?" #'calibredb-dispatch)
      (define-key map "a" #'calibredb-add)
      (define-key map "A" #'calibredb-add-dir)
      (define-key map "c" #'calibredb-clone)
      (define-key map "d" #'calibredb-remove)
      (define-key map "D" #'calibredb-remove-marked-items)
      (define-key map "j" #'calibredb-next-entry)
      (define-key map "k" #'calibredb-previous-entry)
      (define-key map "l" #'calibredb-virtual-library-list)
      (define-key map "L" #'calibredb-library-list)
      (define-key map "n" #'calibredb-virtual-library-next)
      (define-key map "N" #'calibredb-library-next)
      (define-key map "p" #'calibredb-virtual-library-previous)
      (define-key map "P" #'calibredb-library-previous)
      (define-key map "s" #'calibredb-set-metadata-dispatch)
      (define-key map "S" #'calibredb-switch-library)
      (define-key map "o" #'calibredb-find-file)
      (define-key map "O" #'calibredb-find-file-other-frame)
      (define-key map "v" #'calibredb-view)
      (define-key map "V" #'calibredb-open-file-with-default-tool)
      (define-key map "," #'calibredb-quick-look)
      (define-key map "." #'calibredb-dired-open)
      (define-key map "y" #'calibredb-yank-dispatch)
      (define-key map "b" #'calibredb-catalog-bib-dispatch)
      (define-key map "e" #'calibredb-export-dispatch)
      (define-key map "r" #'calibredb-search-refresh-and-clear-filter)
      (define-key map "R" #'calibredb-search-clear-filter)
      (define-key map "q" #'calibredb-search-quit)
      (define-key map "m" #'calibredb-mark-and-forward)
      (define-key map "f" #'calibredb-toggle-favorite-at-point)
      (define-key map "x" #'calibredb-toggle-archive-at-point)
      (define-key map "h" #'calibredb-toggle-highlight-at-point)
      (define-key map "u" #'calibredb-unmark-and-forward)
      (define-key map "i" #'calibredb-edit-annotation)
      (define-key map (kbd "<DEL>") #'calibredb-unmark-and-backward)
      (define-key map (kbd "<backtab>") #'calibredb-toggle-view)
      (define-key map (kbd "TAB") #'calibredb-toggle-view-at-point)
      (define-key map "\M-n" #'calibredb-show-next-entry)
      (define-key map "\M-p" #'calibredb-show-previous-entry)
      (define-key map "/" #'calibredb-search-live-filter)
      (define-key map "\M-t" #'calibredb-set-metadata--tags)
      (define-key map "\M-a" #'calibredb-set-metadata--author_sort)
      (define-key map "\M-A" #'calibredb-set-metadata--authors)
      (define-key map "\M-T" #'calibredb-set-metadata--title)
      (define-key map "\M-c" #'calibredb-set-metadata--comments)
      map)
    "Keymap for `calibredb-search-mode'.")
#+end_src

* Annotate PDFs and EPUBs (org-noter)

#+begin_src elisp :tangle no
  (use-package org-noter)
#+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 no
  (use-package nov
    :config
    (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode))
    )
#+end_src

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

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