在Emacs中创建文档视图适合页面宽度

时间:2014-04-23 06:32:12

标签: emacs document-view

我试图在Emacs中使用文档视图来阅读PDF,但我无法弄清楚如何让它的行为类似于宽度'命令许多PDF读者。是否有内部方法可以做到这一点?

4 个答案:

答案 0 :(得分:5)

以下代码段定义了一个新的辅助模式doc-view-autofit-mode,我在下面使用doc-view-mode-hook激活了该模式。它适用于Ubuntu 14.04上的Emacs 24.3,甚至在我调整窗口大小时调整缩放的大小!

(由于doc-view-autofit-timer-start,通常会有一段很短的调整延迟,但我很高兴与此同住。)

我不赞成这个解决方案;我找到了这段代码on the emacs-devel mailing list

(require 'cl)

;;;; 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)))

(add-hook 'doc-view-mode-hook 'doc-view-autofit-mode)

答案 1 :(得分:1)

它对我有用:

(add-hook 'doc-view-mode-hook 'doc-view-fit-width-to-window)

更新:如果转换(到png或其他内容)仍在进行中(首先打开文档),它无法正常工作。有另一种更可靠的方法来处理这种特殊情况(它根本不使用钩子但是使用建议):

(defadvice doc-view-display (after fit-width activate)
  (doc-view-fit-width-to-window))

答案 2 :(得分:1)

以下是Chris对答案的略微修改 - 它提供了与find-file-other-window等功能的兼容性 - 例如,selected-window与显示*.pdf的功能不同时文件。

  
(defvar last-displayed-doc-view-buffer nil)

(defun get-last-displayed-doc-view-buffer ()
  (setq last-displayed-doc-view-buffer (current-buffer)))

(add-hook 'doc-view-mode-hook 'get-last-displayed-doc-view-buffer)

(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."
  (if (null doc-view-autofit-timer)
    (setq doc-view-autofit-timer
      (run-with-timer doc-view-autofit-timer-start nil (lambda ()
        (let* (
            (selected-window
              (cond
                ((eq major-mode 'doc-view-mode)
                  (selected-window))
                (t
                  (get-buffer-window last-displayed-doc-view-buffer))))
            (current-buffer
              (cond
                ((eq major-mode 'doc-view-mode)
                  (current-buffer))
                (t
                  (get-buffer last-displayed-doc-view-buffer))))
            (selected-fit
              (when (buffer-live-p (get-buffer current-buffer))
                (with-current-buffer (get-buffer current-buffer)
                  doc-view-autofit-type))) )
          (when (window-live-p selected-window)
            (with-selected-window selected-window
              (when doc-view-autofit-timer (cancel-timer doc-view-autofit-timer))
              (setq doc-view-autofit-timer nil)
              (cond
                ((eq 'width selected-fit)
                  (doc-view-fit-width-to-window))
                ((eq 'height selected-fit)
                  (doc-view-fit-height-to-window))
                ((eq 'page selected-fit)
                  (doc-view-fit-page-to-window)))))))))
    (timer-inc-time doc-view-autofit-timer doc-view-autofit-timer-inc)))

并且,正如我先前对Chris'回答,以下变量需要定义:

  
(defvar doc-view-autofit-timer nil)

(defvar doc-view-autofit-type nil)

因为上面的修改为doc-view-mode-hook添加了一个新函数以获得函数current-buffer所需的doc-view-autofit-fit,所以必须确保后一个函数被追加到doc-view-mode-hook的末尾。所以更改看起来像这样 - 也就是说,我们为追加参数添加t

(add-hook 'doc-view-mode-hook 'doc-view-autofit-mode t)

克里斯的所有其他未被上述修改取代的答案仍然有效。


待办事项

  • 创建测试以在滚动时检查每个页面,以确保视图与autofit-type一致。目前,在处理长*.pdf文件时,页面大小会出错。

答案 3 :(得分:1)

只是注意:(要求'cl)已过期。因为emacs-24.3应该是

(require ‘cl-lib) 

请参阅http://www.emacswiki.org/emacs/CommonLispForEmacs