Mercurial > emacs
annotate lisp/international/latexenc.el @ 90228:fa0da9b57058
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-82
Merge from emacs--cvs-trunk--0
Patches applied:
* emacs--cvs-trunk--0 (patch 542-553)
- Update from CVS
- Merge from gnus--rel--5.10
* gnus--rel--5.10 (patch 116-121)
- Merge from emacs--cvs-trunk--0
- Update from CVS
author | Miles Bader <miles@gnu.org> |
---|---|
date | Mon, 19 Sep 2005 10:20:33 +0000 |
parents | 18a818a2ee7c |
children | 06635bf17a9e |
rev | line source |
---|---|
61909 | 1 ;;; latexenc.el --- guess correct coding system in LaTeX files |
2 | |
3 ;; Copyright (C) 2005 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Arne J,Ax(Brgensen <arne@arnested.dk> | |
6 ;; Keywords: mule, coding system, latex | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
61909 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; This code tries to guess the correct coding system of a LaTeX file. | |
28 | |
29 ;; First it searches for a \inputencoding{...} or | |
30 ;; \usepackage[...]{inputenc} line in the file and looks up the ... in | |
31 ;; `latex-inputenc-coding-alist' to find the corresponding coding | |
32 ;; system. | |
33 | |
34 ;; If this fails it will search for AUCTeX's TeX-master or tex-mode's | |
35 ;; tex-main-file variable in the local variables section and visit | |
36 ;; that file to get the coding system from the master file. This check | |
37 ;; can be disabled by setting `latexenc-dont-use-TeX-master-flag' to | |
38 ;; t. | |
39 | |
40 ;; If we have still not found a coding system we will try to use the | |
41 ;; standard tex-mode's `tex-guess-main-file' and get the coding system | |
42 ;; from the main file. This check can be disabled by setting | |
43 ;; `latexenc-dont-use-tex-guess-main-file-flag' to t. | |
44 | |
45 ;; The functionality is enabled by adding the function | |
46 ;; `latexenc-find-file-coding-system' to `file-coding-system-alist' | |
47 ;; like this | |
48 | |
49 ;; (add-to-list 'file-coding-system-alist | |
63091 | 50 ;; '("\\.\\(tex\\|ltx\\|dtx\\|drv\\)\\'" . latexenc-find-file-coding-system)) |
61909 | 51 |
52 ;;; Code: | |
53 | |
54 ;;;###autoload | |
55 (defcustom latex-inputenc-coding-alist | |
56 '(("ansinew" . windows-1252) ; MS Windows ANSI encoding, extension of Latin-1 | |
57 ("applemac" . mac-roman) | |
58 ("ascii" . us-ascii) | |
59 ("cp1250" . windows-1250) ; MS Windows encoding, codepage 1250 | |
60 ("cp1252" . windows-1252) ; synonym of ansinew | |
61 ("cp1257" . cp1257) | |
62 ("cp437de" . cp437) ; IBM code page 437 (German version): 225 is \ss | |
63 ("cp437" . cp437) ; IBM code page 437: 225 is \beta | |
64 ("cp850" . cp850) ; IBM code page 850 | |
65 ("cp852" . cp852) ; IBM code page 852 | |
66 ;; ("cp858" . undecided) ; IBM code page 850 but with a euro symbol | |
67 ("cp865" . cp865) ; IBM code page 865 | |
68 ;; The DECMultinational charaterset used by the OpenVMS system | |
69 ;; ("decmulti" . undecided) | |
70 ("latin1" . iso-8859-1) | |
71 ("latin2" . iso-8859-2) | |
72 ("latin3" . iso-8859-3) | |
73 ("latin4" . iso-8859-4) | |
74 ("latin5" . iso-8859-5) | |
75 ("latin9" . iso-8859-15) | |
76 ;; ("latin10" . undecided) | |
77 ;; ("macce" . undecided) ; Apple Central European | |
78 ("next" . next) ; The Next encoding | |
79 ("utf8" . utf-8) | |
80 ("utf8x" . utf-8)) ; used by the Unicode LaTeX package | |
81 "Mapping from encoding names used by LaTeX's \"inputenc.sty\" to Emacs coding systems. | |
82 Used by the function `latexenc-find-file-coding-system'." | |
83 :group 'files | |
84 :group 'mule | |
85 :type '(alist :key-type (string :tag "LaTeX input encoding") | |
86 :value-type (coding-system :tag "Coding system"))) | |
87 | |
88 ;;;###autoload | |
89 (defun latexenc-inputenc-to-coding-system (inputenc) | |
90 "Return the corresponding coding-system for the specified input encoding. | |
91 Return nil if no matching coding system can be found." | |
92 (cdr (assoc inputenc latex-inputenc-coding-alist))) | |
93 | |
94 ;;;###autoload | |
95 (defun latexenc-coding-system-to-inputenc (cs) | |
96 "Return the corresponding input encoding for the specified coding system. | |
97 Return nil if no matching input encoding can be found." | |
98 (let (result) | |
99 (catch 'result | |
100 (dolist (elem latex-inputenc-coding-alist result) | |
101 (let ((elem-cs (cdr elem))) | |
102 (when (and (coding-system-p elem-cs) | |
103 (coding-system-p cs) | |
104 (eq (coding-system-base cs) (coding-system-base elem-cs))) | |
105 (setq result (car elem)) | |
106 (throw 'result result))))))) | |
107 | |
108 (defvar latexenc-dont-use-TeX-master-flag nil | |
109 "Non-nil means don't follow TeX-master to find the coding system.") | |
110 | |
111 (defvar latexenc-dont-use-tex-guess-main-file-flag nil | |
112 "Non-nil means don't use tex-guessmain-file to find the coding system.") | |
113 | |
114 ;;;###autoload | |
115 (defun latexenc-find-file-coding-system (arg-list) | |
116 "Determine the coding system of a LaTeX file if it uses \"inputenc.sty\". | |
117 The mapping from LaTeX's \"inputenc.sty\" encoding names to Emacs | |
118 coding system names is determined from `latex-inputenc-coding-alist'." | |
119 (if (eq (car arg-list) 'insert-file-contents) | |
120 (save-excursion | |
121 ;; try to find the coding system in this file | |
122 (goto-char (point-min)) | |
62243
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
123 (if (catch 'cs |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
124 (let ((case-fold-search nil)) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
125 (while (search-forward "inputenc" nil t) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
126 (goto-char (match-beginning 0)) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
127 (beginning-of-line) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
128 (if (or (looking-at "[^%\n]*\\\\usepackage\\[\\([^]]*\\)\\]{\\([^}]*,\\)?inputenc\\(,[^}]*\\)?}") |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
129 (looking-at "[^%\n]*\\\\inputencoding{\\([^}]*\\)}")) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
130 (throw 'cs t) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
131 (goto-char (match-end 0)))))) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
132 (let* ((match (match-string 1)) |
62974
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
133 (sym (or (latexenc-inputenc-to-coding-system match) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
134 (intern match)))) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
135 (cond |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
136 ((coding-system-p sym) sym) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
137 ((and (require 'code-pages nil t) (coding-system-p sym)) sym) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
138 (t 'undecided))) |
61909 | 139 ;; else try to find it in the master/main file |
62974
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
140 (let ((default-directory (file-name-directory (nth 1 arg-list))) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
141 latexenc-main-file) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
142 ;; Is there a TeX-master or tex-main-file in the local variables |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
143 ;; section? |
61909 | 144 (unless latexenc-dont-use-TeX-master-flag |
145 (goto-char (point-max)) | |
62974
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
146 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
147 'move) |
62243
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
148 (search-forward "Local Variables:" nil t) |
62974
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
149 (when (re-search-forward |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
150 "^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\"" |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
151 nil t) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
152 (let ((file (match-string 2))) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
153 (dolist (ext `("" ,(if (boundp 'TeX-default-extension) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
154 (concat "." TeX-default-extension) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
155 "") |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
156 ".tex" ".ltx" ".dtx" ".drv")) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
157 (if (and (null latexenc-main-file) ;Stop at first. |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
158 (file-exists-p (concat file ext))) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
159 (setq latexenc-main-file (concat file ext))))))) |
61909 | 160 ;; try tex-modes tex-guess-main-file |
161 (when (and (not latexenc-dont-use-tex-guess-main-file-flag) | |
63084
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
162 (not latexenc-main-file)) |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
163 ;; Use a separate `when' so the byte-compiler sees the fboundp. |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
164 (when (fboundp 'tex-guess-main-file) |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
165 (let ((tex-start-of-header "\\\\document\\(style\\|class\\)")) |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
166 (setq latexenc-main-file (tex-guess-main-file))))) |
61909 | 167 ;; if we found a master/main file get the coding system from it |
168 (if (and latexenc-main-file | |
169 (file-readable-p latexenc-main-file)) | |
170 (let* ((latexenc-dont-use-tex-guess-main-file-flag t) | |
171 (latexenc-dont-use-TeX-master-flag t) | |
62974
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
172 (latexenc-main-buffer |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
173 (find-file-noselect latexenc-main-file t))) |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
174 (coding-system-base ;Disregard the EOL part of the CS. |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
175 (with-current-buffer latexenc-main-buffer |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
176 (or coding-system-for-write buffer-file-coding-system)))) |
61909 | 177 'undecided)))) |
178 'undecided)) | |
179 | |
62398
d0d78600444a
Add page marker to force the "Local Variables:" string out of the last page.
Juanma Barranquero <lekktu@gmail.com>
parents:
62243
diff
changeset
|
180 |
61909 | 181 (provide 'latexenc) |
182 | |
61920 | 183 ;; arch-tag: f971bc3e-1fec-4609-8f2f-73dd41ab22e1 |
61909 | 184 ;;; latexenc.el ends here |