Skip to content

Commit

Permalink
pdf: add goto prev/next title page
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Ye committed Sep 24, 2024
1 parent 598c700 commit 20a8e0f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
5 changes: 4 additions & 1 deletion phye-lisp/phye-init-key.el
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@
:keymaps 'pdf-view-mode-map
"C-s" 'pdf-occur
"C-x o" 'ace-window
",xo" 'ace-window)
",xo" 'ace-window
",rR" 'phye/open-recent-file-in-other-frame
"n" 'phye/pdf-goto-next-title-page
"p" 'phye/pdf-goto-prev-title-page)

(provide 'phye-init-key)
45 changes: 42 additions & 3 deletions phye-lisp/phye-init-pdf.el
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
(use-package pdf-tools
:ensure t
:defer 5
:bind (:map pdf-view-mode-map
(";" . ace-pinyin-jump-char-2)
("C-x o" . other-window))
:config
(pdf-tools-install)
(blink-cursor-mode nil)
Expand All @@ -26,4 +23,46 @@
(pdf-view-resize-factor 1.1))
;; }}

(defun phye/pdf--goto-page-by-cond (condfunc)
"Go to title page by CONDFUNC in pdf."
(let* ((buffer (current-buffer))
(window (get-buffer-window buffer))
(toc (pdf-info-outline buffer))
(cur-page (pdf-view-current-page window))
(found nil))
(message "Cur-page: %d" cur-page)
(cl-loop while (not found) do
(let* ((title-item-0 (nth 0 toc))
(title-item-1 (nth 1 toc))
(title-page-0 (cdr (nth 3 title-item-0)))
(title-page-1 (cdr (nth 3 title-item-1))))
(setq target-page (funcall condfunc cur-page title-page-0 title-page-1))
(if (>= target-page 0)
(progn
(setq found t)
(pdf-view-goto-page target-page window))
(setq toc (cdr toc)))))))

(defun phye/pdf-goto-next-title-page ()
"Go to next title page."
(interactive)
(phye/pdf--goto-page-by-cond
(lambda (cur-page title-page-0 title-page-1)
(if (and (<= title-page-0 cur-page) (< cur-page title-page-1))
(progn
;; (message "found")
title-page-1)
-1))))

(defun phye/pdf-goto-prev-title-page ()
"Go to next title page."
(interactive)
(phye/pdf--goto-page-by-cond
(lambda (cur-page title-page-0 title-page-1)
(if (and (< title-page-0 cur-page) (<= cur-page title-page-1))
(progn
;; (message "found")
title-page-0)
-1))))

(provide 'phye-init-pdf)

0 comments on commit 20a8e0f

Please sign in to comment.