Mercurial > emacs
annotate lisp/international/latexenc.el @ 96089:67f05963ef0d
(setup-default-fontset): Specify script for latin use of iso10646-1.
Fix use of lang tags.
author | Jason Rumney <jasonr@gnu.org> |
---|---|
date | Fri, 20 Jun 2008 08:00:56 +0000 |
parents | 889bc336b89b |
children | c3512b2085a0 |
rev | line source |
---|---|
68419 | 1 ;;; latexenc.el --- guess correct coding system in LaTeX files -*-coding: iso-2022-7bit -*- |
61909 | 2 |
79709 | 3 ;; Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
61909 | 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 | |
94664
889bc336b89b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79709
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
61909 | 11 ;; it under the terms of the GNU General Public License as published by |
94664
889bc336b89b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79709
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
889bc336b89b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79709
diff
changeset
|
13 ;; (at your option) any later version. |
61909 | 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 | |
94664
889bc336b89b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79709
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
61909 | 22 |
23 ;;; Commentary: | |
24 | |
25 ;; This code tries to guess the correct coding system of a LaTeX file. | |
26 | |
27 ;; First it searches for a \inputencoding{...} or | |
28 ;; \usepackage[...]{inputenc} line in the file and looks up the ... in | |
29 ;; `latex-inputenc-coding-alist' to find the corresponding coding | |
30 ;; system. | |
31 | |
32 ;; If this fails it will search for AUCTeX's TeX-master or tex-mode's | |
33 ;; tex-main-file variable in the local variables section and visit | |
34 ;; that file to get the coding system from the master file. This check | |
35 ;; can be disabled by setting `latexenc-dont-use-TeX-master-flag' to | |
36 ;; t. | |
37 | |
38 ;; If we have still not found a coding system we will try to use the | |
39 ;; standard tex-mode's `tex-guess-main-file' and get the coding system | |
40 ;; from the main file. This check can be disabled by setting | |
41 ;; `latexenc-dont-use-tex-guess-main-file-flag' to t. | |
42 | |
43 ;; The functionality is enabled by adding the function | |
44 ;; `latexenc-find-file-coding-system' to `file-coding-system-alist' | |
45 ;; like this | |
46 | |
47 ;; (add-to-list 'file-coding-system-alist | |
63091 | 48 ;; '("\\.\\(tex\\|ltx\\|dtx\\|drv\\)\\'" . latexenc-find-file-coding-system)) |
61909 | 49 |
50 ;;; Code: | |
51 | |
52 ;;;###autoload | |
53 (defcustom latex-inputenc-coding-alist | |
54 '(("ansinew" . windows-1252) ; MS Windows ANSI encoding, extension of Latin-1 | |
55 ("applemac" . mac-roman) | |
56 ("ascii" . us-ascii) | |
57 ("cp1250" . windows-1250) ; MS Windows encoding, codepage 1250 | |
58 ("cp1252" . windows-1252) ; synonym of ansinew | |
59 ("cp1257" . cp1257) | |
60 ("cp437de" . cp437) ; IBM code page 437 (German version): 225 is \ss | |
61 ("cp437" . cp437) ; IBM code page 437: 225 is \beta | |
62 ("cp850" . cp850) ; IBM code page 850 | |
63 ("cp852" . cp852) ; IBM code page 852 | |
72692
0b31ecee2604
* international/latexenc.el (latex-inputenc-coding-alist): Add cp858.
Reiner Steib <Reiner.Steib@gmx.de>
parents:
72322
diff
changeset
|
64 ("cp858" . cp858) ; IBM code page 850 but with a euro symbol |
61909 | 65 ("cp865" . cp865) ; IBM code page 865 |
66 ;; The DECMultinational charaterset used by the OpenVMS system | |
67 ;; ("decmulti" . undecided) | |
68 ("latin1" . iso-8859-1) | |
69 ("latin2" . iso-8859-2) | |
70 ("latin3" . iso-8859-3) | |
71 ("latin4" . iso-8859-4) | |
72 ("latin5" . iso-8859-5) | |
73 ("latin9" . iso-8859-15) | |
74 ;; ("latin10" . undecided) | |
75 ;; ("macce" . undecided) ; Apple Central European | |
76 ("next" . next) ; The Next encoding | |
77 ("utf8" . utf-8) | |
78 ("utf8x" . utf-8)) ; used by the Unicode LaTeX package | |
67256
47d2cf396650
(latex-inputenc-coding-alist): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
67205
diff
changeset
|
79 "Mapping from LaTeX encodings in \"inputenc.sty\" to Emacs coding systems. |
67205
06635bf17a9e
(latex-inputenc-coding-alist): Reword doc string.
Reiner Steib <Reiner.Steib@gmx.de>
parents:
64085
diff
changeset
|
80 LaTeX encodings are specified with \"\\usepackage[encoding]{inputenc}\". |
61909 | 81 Used by the function `latexenc-find-file-coding-system'." |
82 :group 'files | |
83 :group 'mule | |
84 :type '(alist :key-type (string :tag "LaTeX input encoding") | |
85 :value-type (coding-system :tag "Coding system"))) | |
86 | |
87 ;;;###autoload | |
88 (defun latexenc-inputenc-to-coding-system (inputenc) | |
89 "Return the corresponding coding-system for the specified input encoding. | |
90 Return nil if no matching coding system can be found." | |
91 (cdr (assoc inputenc latex-inputenc-coding-alist))) | |
92 | |
93 ;;;###autoload | |
94 (defun latexenc-coding-system-to-inputenc (cs) | |
95 "Return the corresponding input encoding for the specified coding system. | |
96 Return nil if no matching input encoding can be found." | |
97 (let (result) | |
98 (catch 'result | |
99 (dolist (elem latex-inputenc-coding-alist result) | |
100 (let ((elem-cs (cdr elem))) | |
101 (when (and (coding-system-p elem-cs) | |
102 (coding-system-p cs) | |
103 (eq (coding-system-base cs) (coding-system-base elem-cs))) | |
104 (setq result (car elem)) | |
105 (throw 'result result))))))) | |
106 | |
107 (defvar latexenc-dont-use-TeX-master-flag nil | |
108 "Non-nil means don't follow TeX-master to find the coding system.") | |
109 | |
110 (defvar latexenc-dont-use-tex-guess-main-file-flag nil | |
111 "Non-nil means don't use tex-guessmain-file to find the coding system.") | |
112 | |
113 ;;;###autoload | |
114 (defun latexenc-find-file-coding-system (arg-list) | |
115 "Determine the coding system of a LaTeX file if it uses \"inputenc.sty\". | |
116 The mapping from LaTeX's \"inputenc.sty\" encoding names to Emacs | |
117 coding system names is determined from `latex-inputenc-coding-alist'." | |
118 (if (eq (car arg-list) 'insert-file-contents) | |
119 (save-excursion | |
120 ;; try to find the coding system in this file | |
121 (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
|
122 (if (catch 'cs |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
123 (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
|
124 (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
|
125 (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
|
126 (beginning-of-line) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
127 (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
|
128 (looking-at "[^%\n]*\\\\inputencoding{\\([^}]*\\)}")) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
129 (throw 'cs t) |
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
130 (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
|
131 (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
|
132 (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
|
133 (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
|
134 (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
|
135 ((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
|
136 ((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
|
137 (t 'undecided))) |
61909 | 138 ;; else try to find it in the master/main file |
72322
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
139 |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
140 ;; Fixme: If the current file is in an archive (e.g. tar, |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
141 ;; zip), we should find the master file in that archive. |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
142 ;; But, that is not yet implemented. -- K.Handa |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
143 (let ((default-directory (if (stringp (nth 1 arg-list)) |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
144 (file-name-directory (nth 1 arg-list)) |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
145 default-directory)) |
2552d32034c7
(latexenc-find-file-coding-system):
Kenichi Handa <handa@m17n.org>
parents:
68421
diff
changeset
|
146 latexenc-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
|
147 ;; 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
|
148 ;; section? |
61909 | 149 (unless latexenc-dont-use-TeX-master-flag |
150 (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
|
151 (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
|
152 'move) |
62243
68b153fd6cd9
(latexenc-find-file-coding-system): Avoid `re-search-forward' when
Lute Kamstra <lute@gnu.org>
parents:
61958
diff
changeset
|
153 (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
|
154 (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
|
155 "^%+ *\\(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
|
156 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
|
157 (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
|
158 (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
|
159 (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
|
160 "") |
ecfed70e0c5c
(latexenc-find-file-coding-system): Don't inherit the EOL part of the
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62398
diff
changeset
|
161 ".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
|
162 (if (and (null latexenc-main-file) ;Stop at first. |
68421 | 163 (file-exists-p (concat file ext))) |
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
|
164 (setq latexenc-main-file (concat file ext))))))) |
61909 | 165 ;; try tex-modes tex-guess-main-file |
166 (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
|
167 (not latexenc-main-file)) |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
168 ;; 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
|
169 (when (fboundp 'tex-guess-main-file) |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
170 (let ((tex-start-of-header "\\\\document\\(style\\|class\\)")) |
33bc4355d988
(latexenc-find-file-coding-system):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
62974
diff
changeset
|
171 (setq latexenc-main-file (tex-guess-main-file))))) |
61909 | 172 ;; if we found a master/main file get the coding system from it |
173 (if (and latexenc-main-file | |
68418
0927d6785ca7
(latexenc-find-file-coding-system): Make sure latexenc-main-file is
Eli Zaretskii <eliz@gnu.org>
parents:
68356
diff
changeset
|
174 (file-regular-p latexenc-main-file) |
61909 | 175 (file-readable-p latexenc-main-file)) |
176 (let* ((latexenc-dont-use-tex-guess-main-file-flag t) | |
177 (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
|
178 (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
|
179 (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
|
180 (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
|
181 (with-current-buffer latexenc-main-buffer |
79543
5b9f6405b71a
(latexenc-find-file-coding-system): If both coding-system-for-write and
Eli Zaretskii <eliz@gnu.org>
parents:
78274
diff
changeset
|
182 (or coding-system-for-write buffer-file-coding-system |
5b9f6405b71a
(latexenc-find-file-coding-system): If both coding-system-for-write and
Eli Zaretskii <eliz@gnu.org>
parents:
78274
diff
changeset
|
183 'undecided)))) |
61909 | 184 'undecided)))) |
185 'undecided)) | |
186 | |
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
|
187 |
61909 | 188 (provide 'latexenc) |
189 | |
61920 | 190 ;; arch-tag: f971bc3e-1fec-4609-8f2f-73dd41ab22e1 |
61909 | 191 ;;; latexenc.el ends here |