Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / international / latexenc.el
index 88da8ff..79786fb 100644 (file)
@@ -1,16 +1,16 @@
-;;; latexenc.el --- guess correct coding system in LaTeX files
+;;; latexenc.el --- guess correct coding system in LaTeX files -*-coding: iso-2022-7bit -*-
 
-;; Copyright (C) 2005 Free Software Foundation, Inc.
+;; Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Arne J\e,Ax\e(Brgensen <arne@arnested.dk>
 ;; Keywords: mule, coding system, latex
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,9 +18,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -47,7 +45,7 @@
 ;; like this
 
 ;; (add-to-list 'file-coding-system-alist
-;;          '("\\.tex\\|\\.ltx\\|\\.dtx\\|\\.drv\\'" . latexenc-find-file-coding-system))
+;;          '("\\.\\(tex\\|ltx\\|dtx\\|drv\\)\\'" . latexenc-find-file-coding-system))
 
 ;;; Code:
 
@@ -63,7 +61,7 @@
     ("cp437" . cp437) ; IBM code page 437: 225 is \beta
     ("cp850" . cp850) ; IBM code page 850
     ("cp852" . cp852) ; IBM code page 852
-    ;; ("cp858" . undecided) ; IBM code page 850 but with a euro symbol
+    ("cp858" . cp858) ; IBM code page 850 but with a euro symbol
     ("cp865" . cp865) ; IBM code page 865
     ;; The DECMultinational charaterset used by the OpenVMS system
     ;; ("decmulti" . undecided)
@@ -78,7 +76,8 @@
     ("next" . next) ; The Next encoding
     ("utf8" . utf-8)
     ("utf8x" . utf-8)) ; used by the Unicode LaTeX package
-  "Mapping from encoding names used by LaTeX's \"inputenc.sty\" to Emacs coding systems.
+  "Mapping from LaTeX encodings in \"inputenc.sty\" to Emacs coding systems.
+LaTeX encodings are specified with \"\\usepackage[encoding]{inputenc}\".
 Used by the function `latexenc-find-file-coding-system'."
   :group 'files
   :group 'mule
@@ -130,46 +129,58 @@ coding system names is determined from `latex-inputenc-coding-alist'."
                      (throw 'cs t)
                    (goto-char (match-end 0))))))
            (let* ((match (match-string 1))
-                  (sym (intern match)))
-             (when (latexenc-inputenc-to-coding-system match)
-               (setq sym (latexenc-inputenc-to-coding-system match)))
-             (when (coding-system-p sym)
-               sym
-               (if (and (require 'code-pages nil t) (coding-system-p sym))
-                   sym
-                 'undecided)))
+                  (sym (or (latexenc-inputenc-to-coding-system match)
+                            (intern match))))
+             (cond
+               ((coding-system-p sym) sym)
+               ((and (require 'code-pages nil t) (coding-system-p sym)) sym)
+               (t 'undecided)))
           ;; else try to find it in the master/main file
-          (let (latexenc-main-file)
-            ;; is there a TeX-master or tex-main-file in the local variable section
+
+         ;; Fixme: If the current file is in an archive (e.g. tar,
+         ;; zip), we should find the master file in that archive.
+         ;; But, that is not yet implemented.   -- K.Handa
+          (let ((default-directory (if (stringp (nth 1 arg-list))
+                                      (file-name-directory (nth 1 arg-list))
+                                    default-directory))
+               latexenc-main-file)
+            ;; Is there a TeX-master or tex-main-file in the local variables
+            ;; section?
             (unless latexenc-dont-use-TeX-master-flag
               (goto-char (point-max))
-             (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
+             (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
+                               'move)
              (search-forward "Local Variables:" nil t)
-              (when (re-search-forward "^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\"" nil t)
-                (let ((file (concat (file-name-directory (nth 1 arg-list)) (match-string 2))))
-                  (if (file-exists-p file)
-                      (setq latexenc-main-file file)
-                    (if (boundp 'TeX-default-extension)
-                        (when (file-exists-p (concat file "." TeX-default-extension))
-                          (setq latexenc-main-file (concat file "." TeX-default-extension)))
-                      (dolist (ext '("drv" "dtx" "ltx" "tex"))
-                        (if (file-exists-p (concat file "." ext))
-                            (setq latexenc-main-file (concat file "." ext)))))))))
+              (when (re-search-forward
+                     "^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\""
+                     nil t)
+                (let ((file (match-string 2)))
+                  (dolist (ext `("" ,(if (boundp 'TeX-default-extension)
+                                         (concat "." TeX-default-extension)
+                                       "")
+                                 ".tex" ".ltx" ".dtx" ".drv"))
+                    (if (and (null latexenc-main-file) ;Stop at first.
+                             (file-exists-p (concat file ext)))
+                        (setq latexenc-main-file (concat file ext)))))))
             ;; try tex-modes tex-guess-main-file
             (when (and (not latexenc-dont-use-tex-guess-main-file-flag)
                        (not latexenc-main-file))
+              ;; Use a separate `when' so the byte-compiler sees the fboundp.
               (when (fboundp 'tex-guess-main-file)
-                (let ((tex-start-of-header "\\\\document\\(style\\|class\\)")
-                      (default-directory (file-name-directory (nth 1 arg-list))))
+                (let ((tex-start-of-header "\\\\document\\(style\\|class\\)"))
                   (setq latexenc-main-file (tex-guess-main-file)))))
             ;; if we found a master/main file get the coding system from it
             (if (and latexenc-main-file
+                     (file-regular-p latexenc-main-file)
                      (file-readable-p latexenc-main-file))
                 (let* ((latexenc-dont-use-tex-guess-main-file-flag t)
                        (latexenc-dont-use-TeX-master-flag t)
-                       (latexenc-main-buffer (find-file-noselect latexenc-main-file t)))
-                  (or (buffer-local-value 'coding-system-for-write latexenc-main-buffer)
-                      (buffer-local-value 'buffer-file-coding-system latexenc-main-buffer)))
+                       (latexenc-main-buffer
+                        (find-file-noselect latexenc-main-file t)))
+                  (coding-system-base   ;Disregard the EOL part of the CS.
+                   (with-current-buffer latexenc-main-buffer
+                     (or coding-system-for-write buffer-file-coding-system
+                        'undecided))))
               'undecided))))
     'undecided))