emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master ce9e988: Add two new commands for centering doc-view images


From: Lars Ingebrigtsen
Subject: master ce9e988: Add two new commands for centering doc-view images
Date: Fri, 14 Aug 2020 11:43:12 -0400 (EDT)

branch: master
commit ce9e9881b32826cd278ca5a73f81bfc03f15a8d1
Author: Yuan Fu <casouri@gmail.com>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    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).
---
 etc/NEWS         |  5 +++++
 lisp/doc-view.el | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 91add02..53e60cd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -786,6 +786,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-.'
 and then hitting enter would choose the default completion.  Doung
diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index 3e2b244..77c06a8 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 "<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."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]