使 Emacs 中的文档视图适合页面宽度



我正在尝试在Emacs中使用文档视图来阅读PDF,但是我不知道如何使其行为类似于许多PDF阅读器具有的"适合宽度"命令。有没有内部方法可以做到这一点?

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

(由于doc-view-autofit-timer-start,通常会有短暂的大小调整延迟,但我很高兴接受这一点。

我不相信解决方案;我在 emacs-devel 邮件列表中找到了这段代码。

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

它对我有用:

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

以下是 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)

Chris的回答中尚未被上述修改取代的其他所有内容仍然有效。


要做

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

只是注意:(需要'cl(已经过时了。从 emacs-24.3 开始,它应该是

(require ‘cl-lib) 

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

相关内容

  • 没有找到相关文章

最新更新