Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / misc / po-compat.el
1 ;;; po-compat.el --- basic support of PO translation files -*- coding: latin-1; -*-
2
3 ;; Copyright (C) 1995-2002, 2010, 2015 Free Software Foundation, Inc.
4
5 ;; Authors: François Pinard <pinard@iro.umontreal.ca>,
6 ;;          Greg McGary <gkm@magilla.cichlid.com>,
7 ;;          Bruno Haible <bruno@clisp.org>.
8 ;; Keywords: i18n, files
9
10 ;; This file is part of GNU gettext.
11
12 ;; GNU gettext is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU gettext is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, see
24 ;; <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Emacs 21.2 and newer already contain this file, under the name po.el,
29 ;; and without portability hassles.
30
31 ;; This package makes sure visiting PO files decodes them correctly,
32 ;; according to the Charset= header in the PO file.  For more support
33 ;; for editing PO files, see po-mode.el.
34
35 ;;; Code:
36 \f
37 ;;; Emacs portability matters.
38
39 ;; Identify which Emacs variety is being used.
40 ;; This file supports:
41 ;;   - XEmacs (version 19 and above) -> po-XEMACS = t,
42 ;;   - GNU Emacs (version 20 and above) -> po-EMACS20 = t,
43 ;;   - GNU Emacs (version 19) -> no flag.
44 (eval-and-compile
45   (cond ((string-match "XEmacs\\|Lucid" emacs-version)
46          (setq po-EMACS20 nil po-XEMACS t))
47         ((and (string-lessp "19" emacs-version) (featurep 'faces))
48          (setq po-EMACS20 t po-XEMACS nil))
49         (t (setq po-EMACS20 nil po-XEMACS nil))))
50
51 ;; Handle missing 'with-temp-buffer' function.
52 (eval-and-compile
53   (if (fboundp 'with-temp-buffer)
54       (fset 'po-with-temp-buffer (symbol-function 'with-temp-buffer))
55
56     (defmacro po-with-temp-buffer (&rest forms)
57       "Create a temporary buffer, and evaluate FORMS there like 'progn'."
58       (let ((curr-buffer (make-symbol "curr-buffer"))
59             (temp-buffer (make-symbol "temp-buffer")))
60         `(let ((,curr-buffer (current-buffer))
61                (,temp-buffer (get-buffer-create
62                               (generate-new-buffer-name " *po-temp*"))))
63            (unwind-protect
64                (progn
65                  (set-buffer ,temp-buffer)
66                  ,@forms)
67              (set-buffer ,curr-buffer)
68              (and (buffer-name ,temp-buffer)
69                   (kill-buffer ,temp-buffer))))))))
70
71 (defconst po-content-type-charset-alist
72   '(; Note: Emacs 21 doesn't support all encodings, thus the missing entries.
73     ("ASCII" . undecided)
74     ("ANSI_X3.4-1968" . undecided)
75     ("US-ASCII" . undecided)
76     ("ISO-8859-1" . iso-8859-1)
77     ("ISO_8859-1" . iso-8859-1)
78     ("ISO-8859-2" . iso-8859-2)
79     ("ISO_8859-2" . iso-8859-2)
80     ("ISO-8859-3" . iso-8859-3)
81     ("ISO_8859-3" . iso-8859-3)
82     ("ISO-8859-4" . iso-8859-4)
83     ("ISO_8859-4" . iso-8859-4)
84     ("ISO-8859-5" . iso-8859-5)
85     ("ISO_8859-5" . iso-8859-5)
86     ;("ISO-8859-6" . ??)
87     ;("ISO_8859-6" . ??)
88     ("ISO-8859-7" . iso-8859-7)
89     ("ISO_8859-7" . iso-8859-7)
90     ("ISO-8859-8" . iso-8859-8)
91     ("ISO_8859-8" . iso-8859-8)
92     ("ISO-8859-9" . iso-8859-9)
93     ("ISO_8859-9" . iso-8859-9)
94     ;("ISO-8859-13" . ??)
95     ;("ISO_8859-13" . ??)
96     ;("ISO-8859-14" . ??)
97     ;("ISO_8859-14" . ??)
98     ("ISO-8859-15" . iso-8859-15) ; requires Emacs 21
99     ("ISO_8859-15" . iso-8859-15) ; requires Emacs 21
100     ("KOI8-R" . koi8-r)
101     ;("KOI8-U" . ??)
102     ;("KOI8-T" . ??)
103     ("CP437" . cp437) ; requires Emacs 20
104     ("CP775" . cp775) ; requires Emacs 20
105     ("CP850" . cp850) ; requires Emacs 20
106     ("CP852" . cp852) ; requires Emacs 20
107     ("CP855" . cp855) ; requires Emacs 20
108     ;("CP856" . ??)
109     ("CP857" . cp857) ; requires Emacs 20
110     ("CP861" . cp861) ; requires Emacs 20
111     ("CP862" . cp862) ; requires Emacs 20
112     ("CP864" . cp864) ; requires Emacs 20
113     ("CP865" . cp865) ; requires Emacs 20
114     ("CP866" . cp866) ; requires Emacs 21
115     ("CP869" . cp869) ; requires Emacs 20
116     ;("CP874" . ??)
117     ;("CP922" . ??)
118     ;("CP932" . ??)
119     ;("CP943" . ??)
120     ;("CP949" . ??)
121     ;("CP950" . ??)
122     ;("CP1046" . ??)
123     ;("CP1124" . ??)
124     ;("CP1129" . ??)
125     ("CP1250" . cp1250) ; requires Emacs 20
126     ("CP1251" . cp1251) ; requires Emacs 20
127     ("CP1252" . iso-8859-1) ; approximation
128     ("CP1253" . cp1253) ; requires Emacs 20
129     ("CP1254" . iso-8859-9) ; approximation
130     ("CP1255" . iso-8859-8) ; approximation
131     ;("CP1256" . ??)
132     ("CP1257" . cp1257) ; requires Emacs 20
133     ("GB2312" . cn-gb-2312)  ; also named 'gb2312' in XEmacs 21 or Emacs 21
134                              ; also named 'euc-cn' in Emacs 20 or Emacs 21
135     ("EUC-JP" . euc-jp)
136     ("EUC-KR" . euc-kr)
137     ;("EUC-TW" . ??)
138     ("BIG5" . big5)
139     ;("BIG5-HKSCS" . ??)
140     ;("GBK" . ??)
141     ;("GB18030" . ??)
142     ("SHIFT_JIS" . shift_jis)
143     ;("JOHAB" . ??)
144     ("TIS-620" . tis-620)    ; requires Emacs 20 or Emacs 21
145     ("VISCII" . viscii)      ; requires Emacs 20 or Emacs 21
146     ;("GEORGIAN-PS" . ??)
147     ("UTF-8" . utf-8)        ; requires Mule-UCS in Emacs 20, or Emacs 21
148     )
149   "How to convert a GNU libc/libiconv canonical charset name as seen in
150 Content-Type into a Mule coding system.")
151
152 (defun po-find-charset (filename)
153   "Return PO file charset value."
154   (interactive)
155   (let ((charset-regexp
156          "^\"Content-Type: text/plain;[ \t]*charset=\\(.*\\)\\\\n\"")
157         (short-read nil))
158     ;; Try the first 4096 bytes.  In case we cannot find the charset value
159     ;; within the first 4096 bytes (the PO file might start with a long
160     ;; comment) try the next 4096 bytes repeatedly until we'll know for sure
161     ;; we've checked the empty header entry entirely.
162     (while (not (or short-read (re-search-forward "^msgid" nil t)))
163       (save-excursion
164         (goto-char (point-max))
165         (let ((pair (insert-file-contents-literally filename nil
166                                                     (1- (point))
167                                                     (1- (+ (point) 4096)))))
168           (setq short-read (< (nth 1 pair) 4096)))))
169     (cond ((re-search-forward charset-regexp nil t) (match-string 1))
170           (short-read nil)
171           ;; We've found the first msgid; maybe, only a part of the msgstr
172           ;; value was loaded.  Load the next 1024 bytes; if charset still
173           ;; isn't available, give up.
174           (t (save-excursion
175                (goto-char (point-max))
176                (insert-file-contents-literally filename nil
177                                                (1- (point))
178                                                (1- (+ (point) 1024))))
179              (if (re-search-forward charset-regexp nil t)
180                  (match-string 1))))))
181
182 (eval-and-compile
183   (if po-EMACS20
184       (defun po-find-file-coding-system-guts (operation filename)
185         "\
186 Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
187 Called through file-coding-system-alist, before the file is visited for real."
188         (and (eq operation 'insert-file-contents)
189              (file-exists-p filename)
190              (po-with-temp-buffer
191               (let* ((coding-system-for-read 'no-conversion)
192                      (charset (or (po-find-charset filename) "ascii"))
193                      (charset-upper (upcase charset))
194                      (charset-lower (downcase charset))
195                      (candidate
196                       (cdr (assoc charset-upper po-content-type-charset-alist)))
197                      (try-symbol (or candidate (intern-soft charset-lower)))
198                      (try-string
199                       (if try-symbol (symbol-name try-symbol) charset-lower)))
200                 (list (cond ((and try-symbol (coding-system-p try-symbol))
201                              try-symbol)
202                             ((and po-EMACS20
203                                   (not (string-lessp "23" emacs-version))
204                                   (string-match "\\`cp[1-9][0-9][0-9]?\\'"
205                                                 try-string)
206                                   (assoc (substring try-string 2)
207                                          (cp-supported-codepages)))
208                              (codepage-setup (substring try-string 2))
209                              (intern try-string))
210                             (t
211                              'no-conversion))))))))
212
213   (if po-XEMACS
214       (defun po-find-file-coding-system-guts (operation filename)
215         "\
216 Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
217 Called through file-coding-system-alist, before the file is visited for real."
218         (and (eq operation 'insert-file-contents)
219              (file-exists-p filename)
220              (po-with-temp-buffer
221                (let ((coding-system-for-read 'no-conversion))
222                  (let* ((charset (or (po-find-charset filename)
223                                      "ascii"))
224                         (charset-upper (upcase charset))
225                         (charset-lower (intern (downcase charset))))
226                    (list (or (cdr (assoc charset-upper
227                                          po-content-type-charset-alist))
228                              (if (memq charset-lower (coding-system-list))
229                                  charset-lower
230                                'no-conversion)))))))))
231
232   (if po-EMACS20
233       (defun po-find-file-coding-system (arg-list)
234         "\
235 Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
236 Called through file-coding-system-alist, before the file is visited for real."
237         (po-find-file-coding-system-guts (car arg-list) (car (cdr arg-list)))))
238
239   (if po-XEMACS
240       (defun po-find-file-coding-system (operation filename)
241         "\
242 Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
243 Called through file-coding-system-alist, before the file is visited for real."
244         (po-find-file-coding-system-guts operation filename)))
245
246   )
247
248 (provide 'po-compat)
249
250 ;;; Testing this file:
251
252 ;; For each emacsimpl in { emacs, xemacs } do
253 ;;   For each pofile in {
254 ;;     cs.po           ; gettext/po/cs.el, charset=ISO-8859-2
255 ;;     cs-modified.po  ; gettext/po/cs.el, charset=ISO_8859-2
256 ;;     de.po           ; gettext/po/de.el, charset=UTF-8, if $emacsimpl = emacs
257 ;;   } do
258 ;;     Start $emacsimpl
259 ;;     M-x load-file  po-compat.el RET
260 ;;     C-x C-f  $pofile RET
261 ;;     Verify charset marker in status line ('2' = ISO-8859-2, 'u' = UTF-8).
262
263 ;;; po-compat.el ends here