(eval-and-compile
(cond ((string-match "XEmacs\\|Lucid" emacs-version)
(setq po-EMACS20 nil po-XEMACS t))
((and (string-lessp "19" emacs-version) (featurep 'faces))
(setq po-EMACS20 t po-XEMACS nil))
(t (setq po-EMACS20 nil po-XEMACS nil))))
(eval-and-compile
(if (fboundp 'with-temp-buffer)
(fset 'po-with-temp-buffer (symbol-function 'with-temp-buffer))
(defmacro po-with-temp-buffer (&rest forms)
"Create a temporary buffer, and evaluate FORMS there like 'progn'."
(let ((curr-buffer (make-symbol "curr-buffer"))
(temp-buffer (make-symbol "temp-buffer")))
`(let ((,curr-buffer (current-buffer))
(,temp-buffer (get-buffer-create
(generate-new-buffer-name " *po-temp*"))))
(unwind-protect
(progn
(set-buffer ,temp-buffer)
,@forms)
(set-buffer ,curr-buffer)
(and (buffer-name ,temp-buffer)
(kill-buffer ,temp-buffer))))))))
(defconst po-content-type-charset-alist
'( ("ASCII" . undecided)
("ANSI_X3.4-1968" . undecided)
("US-ASCII" . undecided)
("ISO-8859-1" . iso-8859-1)
("ISO_8859-1" . iso-8859-1)
("ISO-8859-2" . iso-8859-2)
("ISO_8859-2" . iso-8859-2)
("ISO-8859-3" . iso-8859-3)
("ISO_8859-3" . iso-8859-3)
("ISO-8859-4" . iso-8859-4)
("ISO_8859-4" . iso-8859-4)
("ISO-8859-5" . iso-8859-5)
("ISO_8859-5" . iso-8859-5)
("ISO-8859-7" . iso-8859-7)
("ISO_8859-7" . iso-8859-7)
("ISO-8859-8" . iso-8859-8)
("ISO_8859-8" . iso-8859-8)
("ISO-8859-9" . iso-8859-9)
("ISO_8859-9" . iso-8859-9)
("ISO-8859-15" . iso-8859-15) ("ISO_8859-15" . iso-8859-15) ("KOI8-R" . koi8-r)
("CP437" . cp437) ("CP775" . cp775) ("CP850" . cp850) ("CP852" . cp852) ("CP855" . cp855) ("CP857" . cp857) ("CP861" . cp861) ("CP862" . cp862) ("CP864" . cp864) ("CP865" . cp865) ("CP866" . cp866) ("CP869" . cp869) ("CP1250" . cp1250) ("CP1251" . cp1251) ("CP1252" . iso-8859-1) ("CP1253" . cp1253) ("CP1254" . iso-8859-9) ("CP1255" . iso-8859-8) ("CP1257" . cp1257) ("GB2312" . cn-gb-2312) ("EUC-JP" . euc-jp)
("EUC-KR" . euc-kr)
("BIG5" . big5)
("SHIFT_JIS" . shift_jis)
("TIS-620" . tis-620) ("VISCII" . viscii) ("UTF-8" . utf-8) )
"How to convert a GNU libc/libiconv canonical charset name as seen in
Content-Type into a Mule coding system.")
(defun po-find-charset (filename)
"Return PO file charset value."
(interactive)
(let ((charset-regexp
"^\"Content-Type: text/plain;[ \t]*charset=\\(.*\\)\\\\n\"")
(short-read nil))
(while (not (or short-read (re-search-forward "^msgid" nil t)))
(save-excursion
(goto-char (point-max))
(let ((pair (insert-file-contents-literally filename nil
(1- (point))
(1- (+ (point) 4096)))))
(setq short-read (< (nth 1 pair) 4096)))))
(cond ((re-search-forward charset-regexp nil t) (match-string 1))
(short-read nil)
(t (save-excursion
(goto-char (point-max))
(insert-file-contents-literally filename nil
(1- (point))
(1- (+ (point) 1024))))
(if (re-search-forward charset-regexp nil t)
(match-string 1))))))
(eval-and-compile
(if po-EMACS20
(defun po-find-file-coding-system-guts (operation filename)
"\
Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
Called through file-coding-system-alist, before the file is visited for real."
(and (eq operation 'insert-file-contents)
(file-exists-p filename)
(po-with-temp-buffer
(let* ((coding-system-for-read 'no-conversion)
(charset (or (po-find-charset filename) "ascii"))
(charset-upper (upcase charset))
(charset-lower (downcase charset))
(candidate
(cdr (assoc charset-upper po-content-type-charset-alist)))
(try-symbol (or candidate (intern-soft charset-lower)))
(try-string
(if try-symbol (symbol-name try-symbol) charset-lower)))
(list (cond ((and try-symbol (coding-system-p try-symbol))
try-symbol)
((and po-EMACS20
(string-match "\\`cp[1-9][0-9][0-9]?\\'"
try-string)
(assoc (substring try-string 2)
(cp-supported-codepages)))
(codepage-setup (substring try-string 2))
(intern try-string))
(t
'no-conversion))))))))
(if po-XEMACS
(defun po-find-file-coding-system-guts (operation filename)
"\
Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
Called through file-coding-system-alist, before the file is visited for real."
(and (eq operation 'insert-file-contents)
(file-exists-p filename)
(po-with-temp-buffer
(let ((coding-system-for-read 'no-conversion))
(let* ((charset (or (po-find-charset filename)
"ascii"))
(charset-upper (upcase charset))
(charset-lower (intern (downcase charset))))
(list (or (cdr (assoc charset-upper
po-content-type-charset-alist))
(if (memq charset-lower (coding-system-list))
charset-lower
'no-conversion)))))))))
(if po-EMACS20
(defun po-find-file-coding-system (arg-list)
"\
Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
Called through file-coding-system-alist, before the file is visited for real."
(po-find-file-coding-system-guts (car arg-list) (car (cdr arg-list)))))
(if po-XEMACS
(defun po-find-file-coding-system (operation filename)
"\
Return a Mule (DECODING . ENCODING) pair, according to PO file charset.
Called through file-coding-system-alist, before the file is visited for real."
(po-find-file-coding-system-guts operation filename)))
)
(provide 'po-compat)