;; -*- lexical-binding: t -*-

;;;; open pdfs
(use-package saveplace-pdf-view
  :ensure t
  :demand t
  :after doc-view)

(use-package jao-pdf
  :demand t
  :config
  (setq jao-open-doc-fun 'jao-find-or-open)
  (setq jao-org-open-pdf-fun jao-open-doc-fun))

;;;; doc-view
(use-package doc-view
  :init
  (setq doc-view-cache-directory "~/.emacs.d/cache/docview"
        doc-view-resolution 110
        doc-view-continuous t
        doc-view-conversion-refresh-interval 1
        doc-view-mupdf-use-svg t)
  (defalias 'jao-doc-view-externally
    (jao-d-l 'jao-mac-open-in-skim 'jao-open-with-zathura))
  :hook ((doc-view-mode . jao-doc-session-mark))
  :bind (:map doc-view-mode-map
              ("j" . doc-view-next-line-or-next-page)
              ("J" . doc-view-search-next-match)
              ("k" . doc-view-previous-line-or-previous-page)
              ("K" . doc-view-search-previous-match)
              ("z" . jao-doc-view-externally)))

(use-package jao-doc-session :demand t)

(use-package jao-doc-view
  :demand t
  :bind (:map doc-view-mode-map
              ("b" . jao-doc-view-back)
              ("B" . jao-doc-view-forward)
              ("S" . jao-doc-session-save)
              ("u" . jao-doc-view-visit-url)))

;;;; transient
(defun jao-org-pdf-goto-org-linking ()
  (interactive)
  (jao-org-pdf-goto-org 4))

(jao-transient-major-mode doc-view
  ["Notes"
   ("o" "notes file" jao-org-pdf-goto-org)
   ("O" "notes file, linking" jao-org-pdf-goto-org-linking)]
  ["Navigation"
   ("b" "back jump" jao-doc-view-back)
   ("B" "forward jump" jao-doc-view-back)
   ("u" "visit URL" jao-doc-view-visit-url)]
  ["Slices"
   ("cb" "bounding box" doc-view-set-slice-from-bounding-box)
   ("cm" "using mouse" doc-view-set-slice-using-mouse)]
  ["Session"
   ("s" "load session" jao-afio-open-pdf-session)
   ("S" "save session" jao-doc-session-save)
   ("d" "visit cache directory" doc-view-dired-cache)]
  ["External viewers"
   ("z" "open with zathura" jao-open-with-zathura :if jao-is-linux)
   ("z" "open with skim" jao-mac-open-in-skim :if jao-is-darwin)])

(with-eval-after-load "pdf-view"
  (jao-transient-major-mode pdf-view
    ["Notes"
     ("o" "notes file" jao-org-pdf-goto-org)
     ("O" "notes file, linking" jao-org-pdf-goto-org-linking)]
    ["Navigation"
     ("b" "back jump" pdf-history-backward)
     ("f" "forward jump" pdf-history-forward)]
    ["Session"
     ("s" "load session" jao-afio-open-pdf-session)
     ("S" "save session" jao-doc-session-save)]
    ["External viewers"
     ("z" "open with zathura" jao-open-with-zathura)]))

;; (transient-get-suffix 'jao-transient-pdf-view '(0 -1))

(provide 'jao-custom-pdf)
