From: Yuan Fu Date: Fri, 14 Aug 2020 15:43:03 +0000 (+0200) Subject: Add two new commands for centering doc-view images X-Git-Tag: emacs-28.0.90~6584^2~28 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=ce9e9881b32826cd278ca5a73f81bfc03f15a8d1;p=emacs.git 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). --- diff --git a/etc/NEWS b/etc/NEWS index 91add027e4c..53e60cdb5ca 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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-.' diff --git a/lisp/doc-view.el b/lisp/doc-view.el index 3e2b2443260..77c06a8eaf9 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -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 "") '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."