| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
(defcustom latex-inputenc-coding-alist |
|---|
| 56 |
'(("ansinew" . windows-1252) |
|---|
| 57 |
("applemac" . mac-roman) |
|---|
| 58 |
("ascii" . us-ascii) |
|---|
| 59 |
("cp1250" . windows-1250) |
|---|
| 60 |
("cp1252" . windows-1252) |
|---|
| 61 |
("cp1257" . cp1257) |
|---|
| 62 |
("cp437de" . cp437) |
|---|
| 63 |
("cp437" . cp437) |
|---|
| 64 |
("cp850" . cp850) |
|---|
| 65 |
("cp852" . cp852) |
|---|
| 66 |
("cp858" . cp858) |
|---|
| 67 |
("cp865" . cp865) |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 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 |
|
|---|
| 77 |
|
|---|
| 78 |
("next" . next) |
|---|
| 79 |
("utf8" . utf-8) |
|---|
| 80 |
("utf8x" . utf-8)) |
|---|
| 81 |
"Mapping from LaTeX encodings in \"inputenc.sty\" to Emacs coding systems. |
|---|
| 82 |
LaTeX encodings are specified with \"\\usepackage[encoding]{inputenc}\". |
|---|
| 83 |
Used by the function `latexenc-find-file-coding-system'." |
|---|
| 84 |
:group 'files |
|---|
| 85 |
:group 'mule |
|---|
| 86 |
:type '(alist :key-type (string :tag "LaTeX input encoding") |
|---|
| 87 |
:value-type (coding-system :tag "Coding system"))) |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
(defun latexenc-inputenc-to-coding-system (inputenc) |
|---|
| 91 |
"Return the corresponding coding-system for the specified input encoding. |
|---|
| 92 |
Return nil if no matching coding system can be found." |
|---|
| 93 |
(cdr (assoc inputenc latex-inputenc-coding-alist))) |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
(defun latexenc-coding-system-to-inputenc (cs) |
|---|
| 97 |
"Return the corresponding input encoding for the specified coding system. |
|---|
| 98 |
Return nil if no matching input encoding can be found." |
|---|
| 99 |
(let (result) |
|---|
| 100 |
(catch 'result |
|---|
| 101 |
(dolist (elem latex-inputenc-coding-alist result) |
|---|
| 102 |
(let ((elem-cs (cdr elem))) |
|---|
| 103 |
(when (and (coding-system-p elem-cs) |
|---|
| 104 |
(coding-system-p cs) |
|---|
| 105 |
(eq (coding-system-base cs) (coding-system-base elem-cs))) |
|---|
| 106 |
(setq result (car elem)) |
|---|
| 107 |
(throw 'result result))))))) |
|---|
| 108 |
|
|---|
| 109 |
(defvar latexenc-dont-use-TeX-master-flag nil |
|---|
| 110 |
"Non-nil means don't follow TeX-master to find the coding system.") |
|---|
| 111 |
|
|---|
| 112 |
(defvar latexenc-dont-use-tex-guess-main-file-flag nil |
|---|
| 113 |
"Non-nil means don't use tex-guessmain-file to find the coding system.") |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
(defun latexenc-find-file-coding-system (arg-list) |
|---|
| 117 |
"Determine the coding system of a LaTeX file if it uses \"inputenc.sty\". |
|---|
| 118 |
The mapping from LaTeX's \"inputenc.sty\" encoding names to Emacs |
|---|
| 119 |
coding system names is determined from `latex-inputenc-coding-alist'." |
|---|
| 120 |
(if (eq (car arg-list) 'insert-file-contents) |
|---|
| 121 |
(save-excursion |
|---|
| 122 |
|
|---|
| 123 |
(goto-char (point-min)) |
|---|
| 124 |
(if (catch 'cs |
|---|
| 125 |
(let ((case-fold-search nil)) |
|---|
| 126 |
(while (search-forward "inputenc" nil t) |
|---|
| 127 |
(goto-char (match-beginning 0)) |
|---|
| 128 |
(beginning-of-line) |
|---|
| 129 |
(if (or (looking-at "[^%\n]*\\\\usepackage\\[\\([^]]*\\)\\]{\\([^}]*,\\)?inputenc\\(,[^}]*\\)?}") |
|---|
| 130 |
(looking-at "[^%\n]*\\\\inputencoding{\\([^}]*\\)}")) |
|---|
| 131 |
(throw 'cs t) |
|---|
| 132 |
(goto-char (match-end 0)))))) |
|---|
| 133 |
(let* ((match (match-string 1)) |
|---|
| 134 |
(sym (or (latexenc-inputenc-to-coding-system match) |
|---|
| 135 |
(intern match)))) |
|---|
| 136 |
(cond |
|---|
| 137 |
((coding-system-p sym) sym) |
|---|
| 138 |
((and (require 'code-pages nil t) (coding-system-p sym)) sym) |
|---|
| 139 |
(t 'undecided))) |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
(let ((default-directory (if (stringp (nth 1 arg-list)) |
|---|
| 146 |
(file-name-directory (nth 1 arg-list)) |
|---|
| 147 |
default-directory)) |
|---|
| 148 |
latexenc-main-file) |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
(unless latexenc-dont-use-TeX-master-flag |
|---|
| 152 |
(goto-char (point-max)) |
|---|
| 153 |
(search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) |
|---|
| 154 |
'move) |
|---|
| 155 |
(search-forward "Local Variables:" nil t) |
|---|
| 156 |
(when (re-search-forward |
|---|
| 157 |
"^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\"" |
|---|
| 158 |
nil t) |
|---|
| 159 |
(let ((file (match-string 2))) |
|---|
| 160 |
(dolist (ext `("" ,(if (boundp 'TeX-default-extension) |
|---|
| 161 |
(concat "." TeX-default-extension) |
|---|
| 162 |
"") |
|---|
| 163 |
".tex" ".ltx" ".dtx" ".drv")) |
|---|
| 164 |
(if (and (null latexenc-main-file) |
|---|
| 165 |
(file-exists-p (concat file ext))) |
|---|
| 166 |
(setq latexenc-main-file (concat file ext))))))) |
|---|
| 167 |
|
|---|
| 168 |
(when (and (not latexenc-dont-use-tex-guess-main-file-flag) |
|---|
| 169 |
(not latexenc-main-file)) |
|---|
| 170 |
|
|---|
| 171 |
(when (fboundp 'tex-guess-main-file) |
|---|
| 172 |
(let ((tex-start-of-header "\\\\document\\(style\\|class\\)")) |
|---|
| 173 |
(setq latexenc-main-file (tex-guess-main-file))))) |
|---|
| 174 |
|
|---|
| 175 |
(if (and latexenc-main-file |
|---|
| 176 |
(file-regular-p latexenc-main-file) |
|---|
| 177 |
(file-readable-p latexenc-main-file)) |
|---|
| 178 |
(let* ((latexenc-dont-use-tex-guess-main-file-flag t) |
|---|
| 179 |
(latexenc-dont-use-TeX-master-flag t) |
|---|
| 180 |
(latexenc-main-buffer |
|---|
| 181 |
(find-file-noselect latexenc-main-file t))) |
|---|
| 182 |
(coding-system-base |
|---|
| 183 |
(with-current-buffer latexenc-main-buffer |
|---|
| 184 |
(or coding-system-for-write buffer-file-coding-system |
|---|
| 185 |
'undecided)))) |
|---|
| 186 |
'undecided)))) |
|---|
| 187 |
'undecided)) |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
(provide 'latexenc) |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|