From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Moritz Maxeiner Newsgroups: gmane.emacs.devel Subject: DocView AutoFitting via "doc-view-autofit-mode" Date: Sat, 31 Mar 2012 02:24:27 +0200 Message-ID: <4F764EBB.5030102@googlemail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------020503020505050707020809" X-Trace: dough.gmane.org 1333153484 21048 80.91.229.3 (31 Mar 2012 00:24:44 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 31 Mar 2012 00:24:44 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Mar 31 02:24:43 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SDm7W-0005nj-Qn for ged-emacs-devel@m.gmane.org; Sat, 31 Mar 2012 02:24:43 +0200 Original-Received: from localhost ([::1]:43865 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SDm7V-0001Kh-VK for ged-emacs-devel@m.gmane.org; Fri, 30 Mar 2012 20:24:41 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:41766) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SDm7S-0001KZ-GE for emacs-devel@gnu.org; Fri, 30 Mar 2012 20:24:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SDm7P-0006Mz-Uv for emacs-devel@gnu.org; Fri, 30 Mar 2012 20:24:38 -0400 Original-Received: from mail-bk0-f41.google.com ([209.85.214.41]:58491) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SDm7P-0006MQ-H4 for emacs-devel@gnu.org; Fri, 30 Mar 2012 20:24:35 -0400 Original-Received: by bkwq16 with SMTP id q16so1201809bkw.0 for ; Fri, 30 Mar 2012 17:24:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type; bh=BLBnav/BDu3xvp1qbhgpymVoerGg47yaFimnyCewW/c=; b=eWzjW+84LrfqKMa+5lrJk2oFQWFyv5dsfYEhf33damCVJ5ILkHyIZ9oNHNzpnw1U11 XFe23l1pIZiyDqRahthaF0MQOZXgoWkpx402/y7BO51WN42QXP0DoJ51uiKyuLNZqWrJ hMLklmGSzNVm+lTD+cky8YORMC7w8qPN0mYkMVHcq8CwKPgu+1Txs2JjXI1WhqDX52cN +1SvNv7r8esBcdBVS28rWy+Fvrc55KmKE0mEZaSjBCyZPzapTDlhVt0Ahk5JpmDDU4zL Xp09aWVVSP8O4u9mseWwtBNfF9OtZUWBT2fS84ZGVo5/6HSH2noIm+tSw0yj9Uoh9AOe jzcQ== Original-Received: by 10.205.136.12 with SMTP id ii12mr130849bkc.98.1333153470066; Fri, 30 Mar 2012 17:24:30 -0700 (PDT) Original-Received: from [192.168.2.117] (p5DDB3B11.dip.t-dialin.net. [93.219.59.17]) by mx.google.com with ESMTPS id r14sm23616609bkv.11.2012.03.30.17.24.28 (version=SSLv3 cipher=OTHER); Fri, 30 Mar 2012 17:24:29 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120314 Thunderbird/11.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.41 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:149255 Archived-At: This is a multi-part message in MIME format. --------------020503020505050707020809 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: --------------020503020505050707020809 Content-Type: text/x-patch; name="doc-view-autofit.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="doc-view-autofit.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: --------------020503020505050707020809--