aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTassilo Horn2010-12-30 19:08:18 +0100
committerTassilo Horn2010-12-30 19:08:18 +0100
commit291cc04566397aa80f1d8c4ff92abbf656fc4512 (patch)
treeaea2d142023720c18cbe20e1bd068dcda81f9d5f
parent8ee2dc5cffd4b79007058f28dbf8fb10f02e462a (diff)
downloademacs-291cc04566397aa80f1d8c4ff92abbf656fc4512.tar.gz
emacs-291cc04566397aa80f1d8c4ff92abbf656fc4512.zip
* doc-view.el (doc-view-set-doc-type): New function refactored
from doc-view-mode. (doc-view-fallback-mode): New function. (doc-view-mode): Use it. (doc-view-mode-maybe): New function that checks if doc-view-mode can be used and falls back to the next best mode otherwise. * files.el (auto-mode-alist): Use doc-view-mode-maybe for PDF, DVI, OpenDocument, and MS Office files.
-rw-r--r--lisp/ChangeLog12
-rw-r--r--lisp/doc-view.el103
-rw-r--r--lisp/files.el2
3 files changed, 76 insertions, 41 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 7081edf4100..6a7bcf9745b 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,15 @@
12010-12-30 Tassilo Horn <tassilo@member.fsf.org>
2
3 * doc-view.el (doc-view-set-doc-type): New function refactored
4 from doc-view-mode.
5 (doc-view-fallback-mode): New function.
6 (doc-view-mode): Use it.
7 (doc-view-mode-maybe): New function that checks if doc-view-mode
8 can be used and falls back to the next best mode otherwise.
9
10 * files.el (auto-mode-alist): Use doc-view-mode-maybe for PDF,
11 DVI, OpenDocument, and MS Office files.
12
12010-12-30 Andreas Schwab <schwab@linux-m68k.org> 132010-12-30 Andreas Schwab <schwab@linux-m68k.org>
2 14
3 * emacs-lisp/rx.el (rx-syntax): Fix typo. 15 * emacs-lisp/rx.el (rx-syntax): Fix typo.
diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index a5dbff10564..801cddd1620 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -1059,11 +1059,7 @@ For now these keys are useful:
1059 (set (make-local-variable 'image-mode-winprops-alist) t) 1059 (set (make-local-variable 'image-mode-winprops-alist) t)
1060 ;; Switch to the previously used major mode or fall back to 1060 ;; Switch to the previously used major mode or fall back to
1061 ;; normal mode. 1061 ;; normal mode.
1062 (if doc-view-previous-major-mode 1062 (doc-view-fallback-mode)
1063 (funcall doc-view-previous-major-mode)
1064 (let ((auto-mode-alist (rassq-delete-all 'doc-view-mode
1065 (copy-alist auto-mode-alist))))
1066 (normal-mode)))
1067 (doc-view-minor-mode 1)) 1063 (doc-view-minor-mode 1))
1068 ;; Switch to doc-view-mode 1064 ;; Switch to doc-view-mode
1069 (when (and (buffer-modified-p) 1065 (when (and (buffer-modified-p)
@@ -1250,6 +1246,41 @@ If BACKWARD is non-nil, jump to the previous match."
1250 (dolist (x l1) (if (memq x l2) (push x l))) 1246 (dolist (x l1) (if (memq x l2) (push x l)))
1251 l)) 1247 l))
1252 1248
1249(defun doc-view-set-doc-type ()
1250 "Figure out the current document type (`doc-view-doc-type')."
1251 (let ((name-types
1252 (when buffer-file-name
1253 (cdr (assoc (file-name-extension buffer-file-name)
1254 '(
1255 ;; DVI
1256 ("dvi" dvi)
1257 ;; PDF
1258 ("pdf" pdf) ("epdf" pdf)
1259 ;; PostScript
1260 ("ps" ps) ("eps" ps)
1261 ;; OpenDocument formats
1262 ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
1263 ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
1264 ("ots" odf) ("otp" odf) ("otg" odf)
1265 ;; Microsoft Office formats (also handled
1266 ;; by the odf conversion chain)
1267 ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
1268 ("ppt" odf) ("pptx" odf))))))
1269 (content-types
1270 (save-excursion
1271 (goto-char (point-min))
1272 (cond
1273 ((looking-at "%!") '(ps))
1274 ((looking-at "%PDF") '(pdf))
1275 ((looking-at "\367\002") '(dvi))))))
1276 (set (make-local-variable 'doc-view-doc-type)
1277 (car (or (doc-view-intersection name-types content-types)
1278 (when (and name-types content-types)
1279 (error "Conflicting types: name says %s but content says %s"
1280 name-types content-types))
1281 name-types content-types
1282 (error "Cannot determine the document type"))))))
1283
1253;;;###autoload 1284;;;###autoload
1254(defun doc-view-mode () 1285(defun doc-view-mode ()
1255 "Major mode in DocView buffers. 1286 "Major mode in DocView buffers.
@@ -1266,49 +1297,19 @@ toggle between displaying the document or editing it as text.
1266 ;; The doc is empty or doesn't exist at all, so fallback to 1297 ;; The doc is empty or doesn't exist at all, so fallback to
1267 ;; another mode. We used to also check file-exists-p, but this 1298 ;; another mode. We used to also check file-exists-p, but this
1268 ;; returns nil for tar members. 1299 ;; returns nil for tar members.
1269 (let ((auto-mode-alist (remq (rassq 'doc-view-mode auto-mode-alist) 1300 (doc-view-fallback-mode)
1270 auto-mode-alist)))
1271 (normal-mode))
1272 1301
1273 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode) 1302 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode)
1274 doc-view-previous-major-mode 1303 doc-view-previous-major-mode
1275 (when (not (eq major-mode 'fundamental-mode)) 1304 (when (not (memq major-mode
1305 '(doc-view-mode fundamental-mode)))
1276 major-mode)))) 1306 major-mode))))
1277 (kill-all-local-variables) 1307 (kill-all-local-variables)
1278 (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode)) 1308 (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode))
1279 1309
1280 ;; Figure out the document type. 1310 ;; Figure out the document type.
1281 (let ((name-types 1311 (unless doc-view-doc-type
1282 (when buffer-file-name 1312 (doc-view-set-doc-type))
1283 (cdr (assoc (file-name-extension buffer-file-name)
1284 '(
1285 ;; DVI
1286 ("dvi" dvi)
1287 ;; PDF
1288 ("pdf" pdf) ("epdf" pdf)
1289 ;; PostScript
1290 ("ps" ps) ("eps" ps)
1291 ;; OpenDocument formats
1292 ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
1293 ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
1294 ("ots" odf) ("otp" odf) ("otg" odf)
1295 ;; Microsoft Office formats (also handled
1296 ;; by the odf conversion chain)
1297 ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf))))))
1298 (content-types
1299 (save-excursion
1300 (goto-char (point-min))
1301 (cond
1302 ((looking-at "%!") '(ps))
1303 ((looking-at "%PDF") '(pdf))
1304 ((looking-at "\367\002") '(dvi))))))
1305 (set (make-local-variable 'doc-view-doc-type)
1306 (car (or (doc-view-intersection name-types content-types)
1307 (when (and name-types content-types)
1308 (error "Conflicting types: name says %s but content says %s"
1309 name-types content-types))
1310 name-types content-types
1311 (error "Cannot determine the document type")))))
1312 1313
1313 (doc-view-make-safe-dir doc-view-cache-directory) 1314 (doc-view-make-safe-dir doc-view-cache-directory)
1314 ;; Handle compressed files, remote files, files inside archives 1315 ;; Handle compressed files, remote files, files inside archives
@@ -1376,6 +1377,28 @@ toggle between displaying the document or editing it as text.
1376 (set (make-local-variable 'view-read-only) nil) 1377 (set (make-local-variable 'view-read-only) nil)
1377 (run-mode-hooks 'doc-view-mode-hook))) 1378 (run-mode-hooks 'doc-view-mode-hook)))
1378 1379
1380(defun doc-view-fallback-mode ()
1381 "Fallback to the previous or next best major mode."
1382 (if doc-view-previous-major-mode
1383 (funcall doc-view-previous-major-mode)
1384 (let ((auto-mode-alist (rassq-delete-all
1385 'doc-view-mode-maybe
1386 (rassq-delete-all 'doc-view-mode
1387 (copy-alist auto-mode-alist)))))
1388 (normal-mode))))
1389
1390;;;###autoload
1391(defun doc-view-mode-maybe ()
1392 "Switch to `doc-view-mode' if possible.
1393If the required external tools are not available, then fallback
1394to the next best mode."
1395 (condition-case nil
1396 (doc-view-set-doc-type)
1397 (error (doc-view-fallback-mode)))
1398 (if (doc-view-mode-p doc-view-doc-type)
1399 (doc-view-mode)
1400 (doc-view-fallback-mode)))
1401
1379;;;###autoload 1402;;;###autoload
1380(define-minor-mode doc-view-minor-mode 1403(define-minor-mode doc-view-minor-mode
1381 "Toggle Doc view minor mode. 1404 "Toggle Doc view minor mode.
diff --git a/lisp/files.el b/lisp/files.el
index e89c3075397..76526de1c0a 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -2372,7 +2372,7 @@ ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\|RAR\\|7Z\\)\\'" . archive-mode)
2372 ("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode) 2372 ("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode)
2373 ("\\.\\(dif\\|pat\\)\\'" . diff-mode) ; for MSDOG 2373 ("\\.\\(dif\\|pat\\)\\'" . diff-mode) ; for MSDOG
2374 ("\\.[eE]?[pP][sS]\\'" . ps-mode) 2374 ("\\.[eE]?[pP][sS]\\'" . ps-mode)
2375 ("\\.\\(?:PDF\\|DVI\\|pdf\\|dvi\\)\\'" . doc-view-mode) 2375 ("\\.\\(?:PDF\\|DVI\\|OD[FGPST]\\|DOCX?\\|XLSX?\\|PPTX?\\|pdf\\|dvi\\|od[fgpst]\\|docx?\\|xlsx?\\|pptx?\\)\\'" . doc-view-mode-maybe)
2376 ("configure\\.\\(ac\\|in\\)\\'" . autoconf-mode) 2376 ("configure\\.\\(ac\\|in\\)\\'" . autoconf-mode)
2377 ("\\.s\\(v\\|iv\\|ieve\\)\\'" . sieve-mode) 2377 ("\\.s\\(v\\|iv\\|ieve\\)\\'" . sieve-mode)
2378 ("BROWSE\\'" . ebrowse-tree-mode) 2378 ("BROWSE\\'" . ebrowse-tree-mode)