Add two new commands for centering doc-view images

* lisp/doc-view.el (doc-view-mode-map): Add binding for centering
commands.
(doc-view-center-page-horizontally, doc-view-center-page-vertically):
New functions (bug#42272).
This commit is contained in:
Yuan Fu 2020-08-14 17:43:03 +02:00 committed by Lars Ingebrigtsen
parent f3d93eb401
commit ce9e9881b3
2 changed files with 34 additions and 0 deletions

View file

@ -785,6 +785,11 @@ digits.
** Miscellaneous
---
*** Two new commands for centering in 'doc-view-mode'
'c h' will now center the page horizonally, and 'c v' will center the
page vertically.
---
*** 'icomplete-show-matches-on-no-input' behavior change
Previously, choosing a different completion with commands like 'C-.'

View file

@ -435,6 +435,9 @@ Typically \"page-%s.png\".")
(define-key map (kbd "c m") 'doc-view-set-slice-using-mouse)
(define-key map (kbd "c b") 'doc-view-set-slice-from-bounding-box)
(define-key map (kbd "c r") 'doc-view-reset-slice)
;; Centering the image
(define-key map (kbd "c h") 'doc-view-center-page-horizontally)
(define-key map (kbd "c v") 'doc-view-center-page-vertically)
;; Searching
(define-key map (kbd "C-s") 'doc-view-search)
(define-key map (kbd "<find>") 'doc-view-search)
@ -921,6 +924,32 @@ Resize the containing frame if needed."
(when new-frame-params
(modify-frame-parameters (selected-frame) new-frame-params))))
(defun doc-view-center-page-horizontally ()
"Center page horizontally when page is wider than window."
(interactive)
(let ((page-width (car (image-size (doc-view-current-image) 'pixel)))
(window-width (window-body-width nil 'pixel))
;; How much do we scroll in order to center the page?
(pixel-hscroll 0)
;; How many pixels are there in a column?
(col-in-pixel (/ (window-body-width nil 'pixel)
(window-body-width nil))))
(when (> page-width window-width)
(setq pixel-hscroll (/ (- page-width window-width) 2))
(set-window-hscroll (selected-window)
(/ pixel-hscroll col-in-pixel)))))
(defun doc-view-center-page-vertically ()
"Center page vertically when page is wider than window."
(interactive)
(let ((page-height (cdr (image-size (doc-view-current-image) 'pixel)))
(window-height (window-body-height nil 'pixel))
;; How much do we scroll in order to center the page?
(pixel-scroll 0))
(when (> page-height window-height)
(setq pixel-scroll (/ (- page-height window-height) 2))
(set-window-vscroll (selected-window) pixel-scroll 'pixel))))
(defun doc-view-reconvert-doc ()
"Reconvert the current document.
Should be invoked when the cached images aren't up-to-date."