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) ;; We've found the first msgid; maybe, only a part of the msgstr ;; value was loaded. Load the next 1024 bytes; if charset still ;; isn't available, give up. (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)))))) ;;;###autoload (autoload 'po-find-file-coding-system "po-compat") (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) (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 (not (string-lessp "23" emacs-version)) (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))))))) (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)))) (provide 'po-compat) ;;; Testing this file: ;; For each pofile in { ;; cs.po ; gettext/po/cs.el, charset=ISO-8859-2 ;; cs-modified.po ; gettext/po/cs.el, charset=ISO_8859-2 ;; de.po ; gettext/po/de.el, charset=UTF-8, if $emacsimpl = emacs ;; } do ;; Start $emacsimpl ;; M-x load-file po-compat.el RET ;; C-x C-f $pofile RET ;; Verify charset marker in status line ('2' = ISO-8859-2, 'u' = UTF-8). ;;; po-compat.el ends here