diff options
author | Tassilo Horn <tsdh@gnu.org> | 2014-10-22 15:30:16 +0200 |
---|---|---|
committer | Tassilo Horn <tsdh@gnu.org> | 2014-10-22 15:30:16 +0200 |
commit | 36749d80256f49ac10860405b95fe319012c3b91 (patch) | |
tree | b69838ad4de84b8bf001a38451a449da6e917072 /lisp/doc-view.el | |
parent | 8ae9147a6c6b2e8ebe04610aee4d4aea04d89b2d (diff) | |
download | emacs-36749d80256f49ac10860405b95fe319012c3b91.tar.gz |
Improve viewing plain text contents of document
* doc/emacs/misc.texi (Document View): Adapt to latest doc-view changes wrt
viewing the document's plain text contents.
* lisp/doc-view.el (doc-view-open-text): View the document's plain text
in the current buffer instead of a new one.
(doc-view-toggle-display): Handle the case where the current
buffer contains the plain text contents of the document.
(doc-view-initiate-display): Don't switch to fallback mode if the
user wants to view the doc's plain text.
(doc-view-set-doc-type): Use assoc-string instead of
assoc-ignore-case.
Diffstat (limited to 'lisp/doc-view.el')
-rw-r--r-- | lisp/doc-view.el | 72 |
1 files changed, 46 insertions, 26 deletions
diff --git a/lisp/doc-view.el b/lisp/doc-view.el index 9458cace74e..37cb2787cca 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -1392,19 +1392,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 +1425,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 +1604,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 +1635,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 +1653,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)) |