X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/eed18bf1fd1162872e196091ab795a773f277e8e..81deba3d7a2b187d58fe26bd8b4eafb5687095e1:/lisp/doc-view.el diff --git a/lisp/doc-view.el b/lisp/doc-view.el index 9458cace74..5f1c94a012 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -1,6 +1,6 @@ ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs -*- lexical-binding: t -*- -;; Copyright (C) 2007-2014 Free Software Foundation, Inc. +;; Copyright (C) 2007-2015 Free Software Foundation, Inc. ;; ;; Author: Tassilo Horn ;; Maintainer: Tassilo Horn @@ -415,7 +415,6 @@ Typically \"page-%s.png\".") (define-key map "H" 'doc-view-fit-height-to-window) (define-key map "P" 'doc-view-fit-page-to-window) ;; Killing the buffer (and the process) - (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer) (define-key map (kbd "K") 'doc-view-kill-proc) ;; Slicing the image (define-key map (kbd "s s") 'doc-view-set-slice) @@ -645,12 +644,8 @@ at the top edge of the page moves to the previous page." (setq doc-view--current-timer nil)) (setq mode-line-process nil)) -(defun doc-view-kill-proc-and-buffer () - "Kill the current converter process and buffer." - (interactive) - (doc-view-kill-proc) - (when (eq major-mode 'doc-view-mode) - (kill-buffer (current-buffer)))) +(define-obsolete-function-alias 'doc-view-kill-proc-and-buffer + #'image-kill-buffer "25.1") (defun doc-view-make-safe-dir (dir) (condition-case nil @@ -1392,19 +1387,28 @@ For now these keys are useful: (tooltip-show (doc-view-current-info))) (defun doc-view-open-text () - "Open a buffer with the current doc's contents as text." + "Display the current doc's contents as text." (interactive) (if doc-view--current-converter-processes (message "DocView: please wait till conversion finished.") - (let ((txt (expand-file-name "doc.txt" (doc-view--current-cache-dir))) - (bname (or buffer-file-name (buffer-name)))) + (let ((txt (expand-file-name "doc.txt" (doc-view--current-cache-dir)))) (if (file-readable-p txt) - (let ((name (concat "Text contents of " - (file-name-nondirectory bname))) - (dir (or (file-name-directory bname) default-directory))) - (with-current-buffer (find-file txt) - (rename-buffer name) - (setq default-directory dir))) + (let ((inhibit-read-only t) + (buffer-undo-list t) + (dv-bfn doc-view--buffer-file-name)) + (erase-buffer) + (set-buffer-multibyte t) + (insert-file-contents txt) + (text-mode) + (setq-local doc-view--buffer-file-name dv-bfn) + (set-buffer-modified-p nil) + (doc-view-minor-mode) + (add-hook 'write-file-functions + (lambda () + (when (eq major-mode 'text-mode) + (error "Cannot save text contents of document %s" + buffer-file-name))) + nil t)) (doc-view-doc->txt txt 'doc-view-open-text))))) ;;;;; Toggle between editing and viewing @@ -1416,20 +1420,30 @@ For now these keys are useful: (defun doc-view-toggle-display () "Toggle between editing a document as text or viewing it." (interactive) - (if (eq major-mode 'doc-view-mode) - ;; Switch to editing mode - (progn - (doc-view-kill-proc) - (setq buffer-read-only nil) - ;; Switch to the previously used major mode or fall back to - ;; normal mode. - (doc-view-fallback-mode) - (doc-view-minor-mode 1)) + (cond + ((eq major-mode 'doc-view-mode) + ;; Switch to editing mode + (doc-view-kill-proc) + (setq buffer-read-only nil) + ;; Switch to the previously used major mode or fall back to + ;; normal mode. + (doc-view-fallback-mode) + (doc-view-minor-mode 1)) + ((eq major-mode 'text-mode) + (let ((buffer-undo-list t)) + ;; We're currently viewing the document's text contents, so switch + ;; back to . + (setq buffer-read-only nil) + (insert-file-contents doc-view--buffer-file-name nil nil nil t) + (doc-view-fallback-mode) + (doc-view-minor-mode 1) + (set-buffer-modified-p nil))) + (t ;; Switch to doc-view-mode (when (and (buffer-modified-p) (y-or-n-p "The buffer has been modified. Save the changes? ")) (save-buffer)) - (doc-view-mode))) + (doc-view-mode)))) ;;;; Searching @@ -1585,11 +1599,11 @@ If BACKWARD is non-nil, jump to the previous match." (concat "No PNG support is available, or some conversion utility for " (file-name-extension doc-view--buffer-file-name) " files is missing.")) - (when (and (executable-find doc-view-pdftotext-program) - (y-or-n-p - "Unable to render file. View extracted text instead? ")) - (doc-view-open-text)) - (doc-view-toggle-display))) + (if (and (executable-find doc-view-pdftotext-program) + (y-or-n-p + "Unable to render file. View extracted text instead? ")) + (doc-view-open-text) + (doc-view-toggle-display)))) (defvar bookmark-make-record-function) @@ -1616,7 +1630,7 @@ If BACKWARD is non-nil, jump to the previous match." "Figure out the current document type (`doc-view-doc-type')." (let ((name-types (when buffer-file-name - (cdr (assoc-ignore-case + (cdr (assoc-string (file-name-extension buffer-file-name) '( ;; DVI @@ -1634,7 +1648,8 @@ If BACKWARD is non-nil, jump to the previous match." ;; Microsoft Office formats (also handled by the odf ;; conversion chain). ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf) - ("ppt" odf) ("pps" odf) ("pptx" odf)))))) + ("ppt" odf) ("pps" odf) ("pptx" odf)) + t)))) (content-types (save-excursion (goto-char (point-min)) @@ -1665,6 +1680,9 @@ If BACKWARD is non-nil, jump to the previous match." ;; desktop.el integration (defun doc-view-desktop-save-buffer (_desktop-dirname) + ;; FIXME: This is wrong, since this info is per-window but we only do it once + ;; here for the buffer. IOW it should be saved via something like + ;; `window-persistent-parameters'. `((page . ,(doc-view-current-page)) (slice . ,(doc-view-current-slice)))) @@ -1675,8 +1693,13 @@ If BACKWARD is non-nil, jump to the previous match." (let ((page (cdr (assq 'page misc))) (slice (cdr (assq 'slice misc)))) (desktop-restore-file-buffer file name misc) + ;; FIXME: We need to run this code after displaying the buffer. (with-selected-window (or (get-buffer-window (current-buffer) 0) (selected-window)) + ;; FIXME: This should be done for all windows restored that show + ;; this buffer. Basically, the page/slice should be saved as + ;; window-parameters in the window-state(s) and then restoring this + ;; window-state should call us back (to interpret/use those parameters). (doc-view-goto-page page) (when slice (apply 'doc-view-set-slice slice)))))