1 ;;; po.el --- basic support of PO translation files -*- coding: latin-1; -*-
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Authors: François Pinard <pinard@iro.umontreal.ca>,
7 ;; Greg McGary <gkm@magilla.cichlid.com>,
8 ;; Bruno Haible <bruno@clisp.org>.
9 ;; Keywords: i18n, files
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This package makes sure visiting PO files decodes them correctly,
31 ;; according to the Charset= header in the PO file. For more support
32 ;; for editing PO files, see po-mode.el.
36 (defconst po-content-type-charset-alist
37 '(("ASCII" . undecided
)
38 ("ANSI_X3.4-1968" . undecided
)
39 ("US-ASCII" . undecided
))
40 "Alist of coding system versus GNU libc/libiconv canonical charset name.
41 Contains canonical charset names that don't correspond to coding systems.")
43 (defun po-find-charset (filename)
44 "Return PO charset value for FILENAME."
46 "^\"Content-Type:[ \t]*text/plain;[ \t]*charset=\\(.*\\)\\\\n\"")
48 ;; Try the first 4096 bytes. In case we cannot find the charset value
49 ;; within the first 4096 bytes (the PO file might start with a long
50 ;; comment) try the next 4096 bytes repeatedly until we'll know for sure
51 ;; we've checked the empty header entry entirely.
52 (while (not (or short-read
(re-search-forward "^msgid" nil t
)))
54 (goto-char (point-max))
55 (let ((pair (insert-file-contents-literally filename nil
57 (1- (+ (point) 4096)))))
58 (setq short-read
(< (nth 1 pair
) 4096)))))
59 (cond ((re-search-forward charset-regexp nil t
) (match-string 1))
61 ;; We've found the first msgid; maybe, only a part of the msgstr
62 ;; value was loaded. Load the next 1024 bytes; if charset still
63 ;; isn't available, give up.
65 (goto-char (point-max))
66 (insert-file-contents-literally filename nil
68 (1- (+ (point) 1024))))
69 (if (re-search-forward charset-regexp nil t
)
72 (defun po-find-file-coding-system-guts (operation filename
)
73 "Return a (DECODING . ENCODING) pair for OPERATION on PO file FILENAME.
74 Do so according to FILENAME's declared charset."
76 (eq operation
'insert-file-contents
)
77 (file-exists-p filename
)
79 (let* ((coding-system-for-read 'no-conversion
)
80 (charset (or (po-find-charset filename
) "ascii"))
85 po-content-type-charset-alist
88 ((or (setq assoc
(assoc-string charset coding-system-alist t
))
90 (assoc-string (subst-char-in-string ?_ ?-
92 coding-system-alist t
)))
94 ;; In principle we should also check the `mime-charset'
95 ;; property of everything in the base coding system
96 ;; list, but there should always be a coding system
97 ;; corresponding to the MIME name.
98 ((featurep 'code-pages
)
102 ;; Try again with code-pages loaded. Maybe it's best
103 ;; to require it initially?
104 (require 'code-pages nil t
)
106 (setq assoc
(assoc-string charset coding-system-alist t
))
107 (setq assoc
(assoc-string (subst-char-in-string
109 coding-system-alist t
)))
114 (defun po-find-file-coding-system (arg-list)
115 "Return a (DECODING . ENCODING) pair, according to PO file's charset.
116 Called through `file-coding-system-alist', before the file is visited for real."
117 (po-find-file-coding-system-guts (car arg-list
) (car (cdr arg-list
))))
118 ;; This is for XEmacs.
119 ;(defun po-find-file-coding-system (operation filename)
121 ;Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
122 ;Called through file-coding-system-alist, before the file is visited for real."
123 ; (po-find-file-coding-system-guts operation filename))
127 ;;; arch-tag: 56748a57-d64c-4200-8f6b-c3a70496eb8c