2018-02-06 01:07:36 +01:00
|
|
|
;;; tools/pdf/config.el -*- lexical-binding: t; -*-
|
|
|
|
|
2020-11-30 19:01:47 -05:00
|
|
|
(use-package! pdf-tools
|
|
|
|
:mode ("\\.pdf\\'" . pdf-view-mode)
|
2019-09-26 15:03:18 -04:00
|
|
|
:magic ("%PDF" . pdf-view-mode)
|
2019-12-17 18:10:16 -05:00
|
|
|
:init
|
2019-09-26 13:15:44 -04:00
|
|
|
(after! pdf-annot
|
|
|
|
(defun +pdf-cleanup-windows-h ()
|
|
|
|
"Kill left-over annotation buffers when the document is killed."
|
|
|
|
(when (buffer-live-p pdf-annot-list-document-buffer)
|
|
|
|
(pdf-info-close pdf-annot-list-document-buffer))
|
|
|
|
(when (buffer-live-p pdf-annot-list-buffer)
|
|
|
|
(kill-buffer pdf-annot-list-buffer))
|
|
|
|
(let ((contents-buffer (get-buffer "*Contents*")))
|
|
|
|
(when (and contents-buffer (buffer-live-p contents-buffer))
|
|
|
|
(kill-buffer contents-buffer))))
|
|
|
|
(add-hook! 'pdf-view-mode-hook
|
|
|
|
(add-hook 'kill-buffer-hook #'+pdf-cleanup-windows-h nil t)))
|
2018-06-15 03:32:46 +02:00
|
|
|
|
2019-12-17 18:10:16 -05:00
|
|
|
:config
|
2021-08-04 01:18:06 -04:00
|
|
|
(defadvice! +pdf--install-epdfinfo-a (fn &rest args)
|
2021-05-25 00:10:45 -04:00
|
|
|
"Install epdfinfo after the first PDF file, if needed."
|
|
|
|
:around #'pdf-view-mode
|
2022-07-24 12:54:57 +02:00
|
|
|
(if (and (require 'pdf-info nil t)
|
|
|
|
(or (pdf-info-running-p)
|
|
|
|
(ignore-errors (pdf-info-check-epdfinfo) t)))
|
2021-08-04 01:18:06 -04:00
|
|
|
(apply fn args)
|
2021-05-25 00:10:45 -04:00
|
|
|
;; If we remain in pdf-view-mode, it'll spit out cryptic errors. This
|
|
|
|
;; graceful failure is better UX.
|
|
|
|
(fundamental-mode)
|
|
|
|
(message "Viewing PDFs in Emacs requires epdfinfo. Use `M-x pdf-tools-install' to build it")))
|
2020-11-30 19:01:47 -05:00
|
|
|
|
2022-07-24 12:54:57 +02:00
|
|
|
;; Despite its namesake, this does not call `pdf-tools-install', it only sets
|
|
|
|
;; up hooks, auto-mode-alist/magic-mode-alist entries, global modes, and
|
|
|
|
;; refreshes pdf-view-mode buffers, if any.
|
|
|
|
;;
|
|
|
|
;; I avoid calling `pdf-tools-install' directly because `pdf-tools' is easy to
|
|
|
|
;; prematurely load in the background (e.g. when exporting an org file or by
|
|
|
|
;; packages like org-pdftools). And I don't want pdf-tools to suddenly block
|
|
|
|
;; Emacs and spew out compiler output for a few minutes in those cases. It's
|
|
|
|
;; abysmal UX. The `pdf-view-mode' advice above works around this with a less
|
|
|
|
;; cryptic failure message, at least.
|
2021-05-25 10:35:08 -04:00
|
|
|
(pdf-tools-install-noverify)
|
2020-12-09 17:16:34 -05:00
|
|
|
|
2020-11-30 19:01:47 -05:00
|
|
|
;; For consistency with other special modes
|
2019-12-17 18:10:16 -05:00
|
|
|
(map! :map pdf-view-mode-map :gn "q" #'kill-current-buffer)
|
|
|
|
|
2020-04-22 15:50:17 -04:00
|
|
|
(setq-default pdf-view-display-size 'fit-page)
|
|
|
|
;; Enable hiDPI support, but at the cost of memory! See politza/pdf-tools#51
|
|
|
|
(setq pdf-view-use-scaling t
|
|
|
|
pdf-view-use-imagemagick nil)
|
2019-08-27 12:36:20 +08:00
|
|
|
|
2020-11-25 12:29:53 -05:00
|
|
|
;; Handle PDF-tools related popups better
|
|
|
|
(set-popup-rules!
|
|
|
|
'(("^\\*Outline*" :side right :size 40 :select nil)
|
2021-07-08 21:42:13 -04:00
|
|
|
("^\\*Edit Annotation " :quit nil)
|
2020-11-25 12:29:53 -05:00
|
|
|
("\\(?:^\\*Contents\\|'s annots\\*$\\)" :ignore t)))
|
|
|
|
|
|
|
|
;; The mode-line does serve any useful purpose is annotation windows
|
|
|
|
(add-hook 'pdf-annot-list-mode-hook #'hide-mode-line-mode)
|
|
|
|
|
|
|
|
;; HACK Fix #1107: flickering pdfs when evil-mode is enabled
|
|
|
|
(setq-hook! 'pdf-view-mode-hook evil-normal-state-cursor (list nil))
|
|
|
|
|
2021-03-22 15:42:38 -04:00
|
|
|
;; HACK Refresh FG/BG for pdfs when `pdf-view-midnight-colors' is changed by a
|
|
|
|
;; theme or with `setq!'.
|
|
|
|
;; TODO PR this upstream?
|
|
|
|
(defun +pdf-reload-midnight-minor-mode-h ()
|
|
|
|
(when pdf-view-midnight-minor-mode
|
|
|
|
(pdf-info-setoptions
|
|
|
|
:render/foreground (car pdf-view-midnight-colors)
|
|
|
|
:render/background (cdr pdf-view-midnight-colors)
|
|
|
|
:render/usecolors t)
|
|
|
|
(pdf-cache-clear-images)
|
|
|
|
(pdf-view-redisplay t)))
|
|
|
|
(put 'pdf-view-midnight-colors 'custom-set
|
|
|
|
(lambda (sym value)
|
|
|
|
(set-default sym value)
|
|
|
|
(dolist (buffer (doom-buffers-in-mode 'pdf-view-mode))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(if (get-buffer-window buffer)
|
|
|
|
(+pdf-reload-midnight-minor-mode-h)
|
|
|
|
;; Defer refresh for buffers that aren't visible, to avoid
|
|
|
|
;; blocking Emacs for too long while changing themes.
|
|
|
|
(add-hook 'doom-switch-buffer-hook #'+pdf-reload-midnight-minor-mode-h
|
|
|
|
nil 'local))))))
|
|
|
|
|
2021-04-15 13:28:58 -04:00
|
|
|
;; Silence "File *.pdf is large (X MiB), really open?" prompts for pdfs
|
2021-08-04 01:18:06 -04:00
|
|
|
(defadvice! +pdf-suppress-large-file-prompts-a (fn size op-type filename &optional offer-raw)
|
2021-04-15 13:28:58 -04:00
|
|
|
:around #'abort-if-file-too-large
|
|
|
|
(unless (string-match-p "\\.pdf\\'" filename)
|
2021-08-04 01:18:06 -04:00
|
|
|
(funcall fn size op-type filename offer-raw))))
|
2020-11-27 15:52:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
(use-package! saveplace-pdf-view
|
|
|
|
:after pdf-view)
|