latexenc.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ;;; latexenc.el --- guess correct coding system in LaTeX files -*-coding: iso-2022-7bit -*-
  2. ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
  3. ;; Author: Arne J,Ax(Brgensen <arne@arnested.dk>
  4. ;; Keywords: mule, coding system, latex
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This code tries to guess the correct coding system of a LaTeX file.
  18. ;; First it searches for a \inputencoding{...} or
  19. ;; \usepackage[...]{inputenc} line in the file and looks up the ... in
  20. ;; `latex-inputenc-coding-alist' to find the corresponding coding
  21. ;; system.
  22. ;; If this fails it will search for AUCTeX's TeX-master or tex-mode's
  23. ;; tex-main-file variable in the local variables section and visit
  24. ;; that file to get the coding system from the master file. This check
  25. ;; can be disabled by setting `latexenc-dont-use-TeX-master-flag' to
  26. ;; t.
  27. ;; If we have still not found a coding system we will try to use the
  28. ;; standard tex-mode's `tex-guess-main-file' and get the coding system
  29. ;; from the main file. This check can be disabled by setting
  30. ;; `latexenc-dont-use-tex-guess-main-file-flag' to t.
  31. ;; The functionality is enabled by adding the function
  32. ;; `latexenc-find-file-coding-system' to `file-coding-system-alist'
  33. ;; like this
  34. ;; (add-to-list 'file-coding-system-alist
  35. ;; '("\\.\\(tex\\|ltx\\|dtx\\|drv\\)\\'" . latexenc-find-file-coding-system))
  36. ;;; Code:
  37. ;;;###autoload
  38. (defcustom latex-inputenc-coding-alist
  39. (purecopy
  40. '(("ansinew" . windows-1252) ; MS Windows ANSI encoding, extension of Latin-1
  41. ("applemac" . mac-roman)
  42. ("ascii" . us-ascii)
  43. ("cp1250" . windows-1250) ; MS Windows encoding, codepage 1250
  44. ("cp1252" . windows-1252) ; synonym of ansinew
  45. ("cp1257" . cp1257)
  46. ("cp437de" . cp437) ; IBM code page 437 (German version): 225 is \ss
  47. ("cp437" . cp437) ; IBM code page 437: 225 is \beta
  48. ("cp850" . cp850) ; IBM code page 850
  49. ("cp852" . cp852) ; IBM code page 852
  50. ("cp858" . cp858) ; IBM code page 850 but with a euro symbol
  51. ("cp865" . cp865) ; IBM code page 865
  52. ("latin1" . iso-8859-1)
  53. ("latin2" . iso-8859-2)
  54. ("latin3" . iso-8859-3)
  55. ("latin4" . iso-8859-4)
  56. ("latin5" . iso-8859-5)
  57. ("latin9" . iso-8859-15)
  58. ;; ("latin10" . undecided)
  59. ;; ("macce" . undecided) ; Apple Central European
  60. ("next" . next) ; The Next encoding
  61. ("utf8" . utf-8)
  62. ("utf8x" . utf-8))) ; used by the Unicode LaTeX package
  63. "Mapping from LaTeX encodings in \"inputenc.sty\" to Emacs coding systems.
  64. LaTeX encodings are specified with \"\\usepackage[encoding]{inputenc}\".
  65. Used by the function `latexenc-find-file-coding-system'."
  66. :group 'files
  67. :group 'mule
  68. :type '(alist :key-type (string :tag "LaTeX input encoding")
  69. :value-type (coding-system :tag "Coding system")))
  70. ;;;###autoload
  71. (defun latexenc-inputenc-to-coding-system (inputenc)
  72. "Return the corresponding coding-system for the specified input encoding.
  73. Return nil if no matching coding system can be found."
  74. (cdr (assoc inputenc latex-inputenc-coding-alist)))
  75. ;;;###autoload
  76. (defun latexenc-coding-system-to-inputenc (cs)
  77. "Return the corresponding input encoding for the specified coding system.
  78. Return nil if no matching input encoding can be found."
  79. (let (result)
  80. (catch 'result
  81. (dolist (elem latex-inputenc-coding-alist result)
  82. (let ((elem-cs (cdr elem)))
  83. (when (and (coding-system-p elem-cs)
  84. (coding-system-p cs)
  85. (eq (coding-system-base cs) (coding-system-base elem-cs)))
  86. (setq result (car elem))
  87. (throw 'result result)))))))
  88. (defvar latexenc-dont-use-TeX-master-flag nil
  89. "Non-nil means don't follow TeX-master to find the coding system.")
  90. (defvar latexenc-dont-use-tex-guess-main-file-flag nil
  91. "Non-nil means don't use tex-guessmain-file to find the coding system.")
  92. ;;;###autoload
  93. (defun latexenc-find-file-coding-system (arg-list)
  94. "Determine the coding system of a LaTeX file if it uses \"inputenc.sty\".
  95. The mapping from LaTeX's \"inputenc.sty\" encoding names to Emacs
  96. coding system names is determined from `latex-inputenc-coding-alist'."
  97. (if (eq (car arg-list) 'insert-file-contents)
  98. (save-excursion
  99. ;; try to find the coding system in this file
  100. (goto-char (point-min))
  101. (if (catch 'cs
  102. (let ((case-fold-search nil))
  103. (while (search-forward "inputenc" nil t)
  104. (goto-char (match-beginning 0))
  105. (beginning-of-line)
  106. (if (or (looking-at "[^%\n]*\\\\usepackage\\[\\([^]]*\\)\\]{\\([^}]*,\\)?inputenc\\(,[^}]*\\)?}")
  107. (looking-at "[^%\n]*\\\\inputencoding{\\([^}]*\\)}"))
  108. (throw 'cs t)
  109. (goto-char (match-end 0))))))
  110. (let* ((match (match-string 1))
  111. (sym (or (latexenc-inputenc-to-coding-system match)
  112. (intern match))))
  113. (cond
  114. ((coding-system-p sym) sym)
  115. ((and (require 'code-pages nil t) (coding-system-p sym)) sym)
  116. (t 'undecided)))
  117. ;; else try to find it in the master/main file
  118. ;; Fixme: If the current file is in an archive (e.g. tar,
  119. ;; zip), we should find the master file in that archive.
  120. ;; But, that is not yet implemented. -- K.Handa
  121. (let ((default-directory (if (stringp (nth 1 arg-list))
  122. (file-name-directory (nth 1 arg-list))
  123. default-directory))
  124. latexenc-main-file)
  125. ;; Is there a TeX-master or tex-main-file in the local variables
  126. ;; section?
  127. (unless latexenc-dont-use-TeX-master-flag
  128. (goto-char (point-max))
  129. (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
  130. 'move)
  131. (search-forward "Local Variables:" nil t)
  132. (when (re-search-forward
  133. "^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\""
  134. nil t)
  135. (let ((file (match-string 2)))
  136. (dolist (ext `("" ,(if (boundp 'TeX-default-extension)
  137. (concat "." TeX-default-extension)
  138. "")
  139. ".tex" ".ltx" ".dtx" ".drv"))
  140. (if (and (null latexenc-main-file) ;Stop at first.
  141. (file-exists-p (concat file ext)))
  142. (setq latexenc-main-file (concat file ext)))))))
  143. ;; try tex-modes tex-guess-main-file
  144. (when (and (not latexenc-dont-use-tex-guess-main-file-flag)
  145. (not latexenc-main-file))
  146. ;; Use a separate `when' so the byte-compiler sees the fboundp.
  147. (when (fboundp 'tex-guess-main-file)
  148. (let ((tex-start-of-header "\\\\document\\(style\\|class\\)"))
  149. (setq latexenc-main-file (tex-guess-main-file)))))
  150. ;; if we found a master/main file get the coding system from it
  151. (if (and latexenc-main-file
  152. (file-regular-p latexenc-main-file)
  153. (file-readable-p latexenc-main-file))
  154. (let* ((latexenc-dont-use-tex-guess-main-file-flag t)
  155. (latexenc-dont-use-TeX-master-flag t)
  156. (latexenc-main-buffer
  157. (find-file-noselect latexenc-main-file t)))
  158. (coding-system-base ;Disregard the EOL part of the CS.
  159. (with-current-buffer latexenc-main-buffer
  160. (or coding-system-for-write buffer-file-coding-system
  161. 'undecided))))
  162. 'undecided))))
  163. 'undecided))
  164. (provide 'latexenc)
  165. ;;; latexenc.el ends here