unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* DocView AutoFitting via "doc-view-autofit-mode"
@ 2012-03-31  0:24 Moritz Maxeiner
  2012-04-01  9:34 ` Tassilo Horn
  0 siblings, 1 reply; 12+ messages in thread
From: Moritz Maxeiner @ 2012-03-31  0:24 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 5298 bytes --]

Hi,
   I wrote this for myself, thought it might be useful for others as well
   and believed this to be the correct mailing list. The patch is against
   the bzr version of somewhere in the last couple of hours.
If anything is missing please tell me,

cal

[Description]

This is a minor mode allowing documents viewed with DocView to be automaticly
refitted to the window they are shown in (no need to hit W/H/P every
time the window size changes).

It is implemented by adding a local hook to window-configuration-change-hook
for the buffer the document is in, which in turn either starts or increases
- if already started - a buffer local timer every time the hook is called.
When the timer runs out, it calls doc-view-fit-[width/height/page]-to-window
according to a buffer local variable.

The timer is used, so that when the hook triggers a lot in a short period
of time it doesn't cause too much overhead (Useful for slower computers).

[ChangeLog]

* doc-view.el (doc-view-autofit-timer-start doc-view-autofit-timer-inc)
   (doc-view-autofit-default-fit doc-view-autofit-mode-map)
   (doc-view-autofit-set doc-view-autofit-width)
   (doc-view-autofit-height doc-view-autofit-page)
   (doc-view-autofit-fit doc-view-autofit-mode): New minor mode
   with customs and functions for automatic fitting in DocView buffers.

[Patch]

*** doc-view.el    2012-03-31 01:55:44.046165270 +0200
--- doc-view.el    2012-03-31 02:02:03.710204323 +0200
*************** See the command `doc-view-mode' for more
*** 1557,1562 ****
--- 1557,1654 ----
          (doc-view-goto-page page)))))


+ ;;;; Automatic fitting minor mode
+
+ (defcustom doc-view-autofit-timer-start 1.0
+   "Initial value (seconds) for the timer that delays the fitting when
+ `doc-view-autofit-fit' is called (Which is when a window
+ configuration change occurs and a document needs to be fitted)."
+   :type 'number
+   :group 'doc-view)
+
+ (defcustom doc-view-autofit-timer-inc 0.02
+   "Value to increase (seconds) the timer (see `doc-view-autofit-timer-start')
+ by, if there is another window configuration change occuring, before
+ it runs out."
+   :type 'number
+   :group 'doc-view)
+
+ (defcustom doc-view-autofit-default-fit 'width
+   "The fitting type initially used when mode is enabled.
+ Valid values are: width, height, page."
+   :type 'symbol
+   :group 'doc-view)
+
+ (defvar doc-view-autofit-mode-map
+   (let ((map (make-sparse-keymap)))
+     (define-key map (kbd "C-c W") 'doc-view-autofit-width)
+     (define-key map (kbd "C-c H") 'doc-view-autofit-height)
+     (define-key map (kbd "C-c P") 'doc-view-autofit-page)
+     map)
+   "Keymap used by `doc-view-autofit-mode'.")
+
+ (defun doc-view-autofit-set (type)
+   "Set autofitting to TYPE for current buffer."
+   (when doc-view-autofit-mode
+     (setq doc-view-autofit-type type)
+     (doc-view-autofit-fit)))
+
+ (defun doc-view-autofit-width ()
+   "Set autofitting to width for current buffer."
+   (interactive) (doc-view-autofit-set 'width))
+
+ (defun doc-view-autofit-height ()
+   "Set autofitting to height for current buffer."
+   (interactive) (doc-view-autofit-set 'height))
+
+ (defun doc-view-autofit-page ()
+   "Set autofitting to page for current buffer."
+   (interactive) (doc-view-autofit-set 'page))
+
+ (defun doc-view-autofit-fit ()
+   "Fits the document in the selected window's buffer
+ delayed with a timer, so multiple calls in succession
+ don't cause as much overhead."
+   (lexical-let
+       ((window (selected-window)))
+     (if (equal doc-view-autofit-timer nil)
+         (setq doc-view-autofit-timer
+               (run-with-timer
+                doc-view-autofit-timer-start nil
+                (lambda ()
+                  (if (window-live-p window)
+                      (save-selected-window
+                        (select-window window)
+                        (cancel-timer doc-view-autofit-timer)
+                        (setq doc-view-autofit-timer nil)
+                        (cond
+                         ((equal 'width doc-view-autofit-type)
+                          (doc-view-fit-width-to-window))
+                         ((equal 'height doc-view-autofit-type)
+                          (doc-view-fit-height-to-window))
+                         ((equal 'page doc-view-autofit-type)
+                          (doc-view-fit-page-to-window))))))))
+       (timer-inc-time doc-view-autofit-timer doc-view-autofit-timer-inc))))
+
+ (define-minor-mode doc-view-autofit-mode
+   "Minor mode for automatic (timer based) fitting in DocView."
+   :lighter " AFit" :keymap doc-view-autofit-mode-map :group 'doc-view
+   (when doc-view-autofit-mode
+     (set (make-local-variable 'doc-view-autofit-type)
+          doc-view-autofit-default-fit)
+     (set (make-local-variable 'doc-view-autofit-timer) nil)
+     (add-hook 'window-configuration-change-hook
+               'doc-view-autofit-fit nil t)
+     (doc-view-autofit-fit))
+   (when (not doc-view-autofit-mode)
+     (remove-hook 'window-configuration-change-hook
+                  'doc-view-autofit-fit t)
+     (when doc-view-autofit-timer
+         (cancel-timer doc-view-autofit-timer)
+         (setq doc-view-autofit-timer nil))
+     (setq doc-view-autofit-type nil)))
+
+
   (provide 'doc-view)

   ;; Local Variables:



[-- Attachment #2: doc-view-autofit.patch --]
[-- Type: text/x-patch, Size: 3654 bytes --]

*** doc-view.el	2012-03-31 01:55:44.046165270 +0200
--- doc-view.el	2012-03-31 02:02:03.710204323 +0200
*************** See the command `doc-view-mode' for more
*** 1557,1562 ****
--- 1557,1654 ----
         (doc-view-goto-page page)))))
  
  
+ ;;;; Automatic fitting minor mode
+ 
+ (defcustom doc-view-autofit-timer-start 1.0
+   "Initial value (seconds) for the timer that delays the fitting when
+ `doc-view-autofit-fit' is called (Which is when a window
+ configuration change occurs and a document needs to be fitted)."
+   :type 'number
+   :group 'doc-view)
+ 
+ (defcustom doc-view-autofit-timer-inc 0.02
+   "Value to increase (seconds) the timer (see `doc-view-autofit-timer-start')
+ by, if there is another window configuration change occuring, before
+ it runs out."
+   :type 'number
+   :group 'doc-view)
+ 
+ (defcustom doc-view-autofit-default-fit 'width
+   "The fitting type initially used when mode is enabled.
+ Valid values are: width, height, page."
+   :type 'symbol
+   :group 'doc-view)
+ 
+ (defvar doc-view-autofit-mode-map
+   (let ((map (make-sparse-keymap)))
+ 	(define-key map (kbd "C-c W") 'doc-view-autofit-width)
+ 	(define-key map (kbd "C-c H") 'doc-view-autofit-height)
+ 	(define-key map (kbd "C-c P") 'doc-view-autofit-page)
+ 	map)
+   "Keymap used by `doc-view-autofit-mode'.")
+ 
+ (defun doc-view-autofit-set (type)
+   "Set autofitting to TYPE for current buffer."
+   (when doc-view-autofit-mode
+ 	(setq doc-view-autofit-type type)
+ 	(doc-view-autofit-fit)))
+ 
+ (defun doc-view-autofit-width ()
+   "Set autofitting to width for current buffer."
+   (interactive) (doc-view-autofit-set 'width))
+ 
+ (defun doc-view-autofit-height ()
+   "Set autofitting to height for current buffer."
+   (interactive) (doc-view-autofit-set 'height))
+ 
+ (defun doc-view-autofit-page ()
+   "Set autofitting to page for current buffer."
+   (interactive) (doc-view-autofit-set 'page))
+ 
+ (defun doc-view-autofit-fit ()
+   "Fits the document in the selected window's buffer
+ delayed with a timer, so multiple calls in succession
+ don't cause as much overhead."
+   (lexical-let
+ 	  ((window (selected-window)))
+ 	(if (equal doc-view-autofit-timer nil)
+ 		(setq doc-view-autofit-timer
+ 			  (run-with-timer
+ 			   doc-view-autofit-timer-start nil
+ 			   (lambda ()
+ 				 (if (window-live-p window)
+ 					 (save-selected-window
+ 					   (select-window window)
+ 					   (cancel-timer doc-view-autofit-timer)
+ 					   (setq doc-view-autofit-timer nil)
+ 					   (cond
+ 						((equal 'width doc-view-autofit-type)
+ 						 (doc-view-fit-width-to-window))
+ 						((equal 'height doc-view-autofit-type)
+ 						 (doc-view-fit-height-to-window))
+ 						((equal 'page doc-view-autofit-type)
+ 						 (doc-view-fit-page-to-window))))))))
+ 	  (timer-inc-time doc-view-autofit-timer doc-view-autofit-timer-inc))))
+ 
+ (define-minor-mode doc-view-autofit-mode
+   "Minor mode for automatic (timer based) fitting in DocView."
+   :lighter " AFit" :keymap doc-view-autofit-mode-map :group 'doc-view
+   (when doc-view-autofit-mode
+ 	(set (make-local-variable 'doc-view-autofit-type)
+ 		 doc-view-autofit-default-fit)
+ 	(set (make-local-variable 'doc-view-autofit-timer) nil)
+ 	(add-hook 'window-configuration-change-hook
+ 			  'doc-view-autofit-fit nil t)
+ 	(doc-view-autofit-fit))
+   (when (not doc-view-autofit-mode)
+ 	(remove-hook 'window-configuration-change-hook
+ 				 'doc-view-autofit-fit t)
+ 	(when doc-view-autofit-timer
+ 		(cancel-timer doc-view-autofit-timer)
+ 		(setq doc-view-autofit-timer nil))
+ 	(setq doc-view-autofit-type nil)))
+ 
+ 
  (provide 'doc-view)
  
  ;; Local Variables:

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-04-26  3:19 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-03-31  0:24 DocView AutoFitting via "doc-view-autofit-mode" Moritz Maxeiner
2012-04-01  9:34 ` Tassilo Horn
2012-04-02 17:58   ` Moritz Maxeiner
2012-04-02 18:37     ` Drew Adams
2012-04-02 18:41     ` Tassilo Horn
2012-04-03 13:34     ` Stefan Monnier
2012-04-03 13:38       ` Moritz Maxeiner
2012-04-03 14:51         ` Stefan Monnier
2012-04-03 15:11           ` Moritz Maxeiner
2012-04-03 16:14             ` Stefan Monnier
2012-04-26  3:19               ` Stefan Monnier
2012-04-03 15:18           ` Tassilo Horn

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).