]> git.eshelyaron.com Git - emacs.git/commitdiff
Add two new commands for centering doc-view images
authorYuan Fu <casouri@gmail.com>
Fri, 14 Aug 2020 15:43:03 +0000 (17:43 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Fri, 14 Aug 2020 15:43:03 +0000 (17:43 +0200)
* 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).

etc/NEWS
lisp/doc-view.el

index 91add027e4c19d89eac61bca9f6b18af35cf1c28..53e60cdb5ca4d46e360d5b43a37d102844875050 100644 (file)
--- 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-.'
index 3e2b2443260da087e2941c9818ea8be21341dcaa..77c06a8eaf9de2e804640b82e2a1ec0f56985aa9 100644 (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."