| 1 |
;;; mew-mule.el --- Environment of Mule common for Mew |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Jul 15, 1998 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(eval-when-compile |
| 9 |
(require 'mew-env0)) |
| 10 |
|
| 11 |
;; |
| 12 |
;; Charset |
| 13 |
;; |
| 14 |
|
| 15 |
(defun mew-charset-m17n () |
| 16 |
(if (string= mew-charset-m17n mew-utf-8) |
| 17 |
(if mew-internal-utf-8p |
| 18 |
mew-utf-8 |
| 19 |
(if (condition-case nil (require 'un-define) (file-error nil)) |
| 20 |
mew-utf-8 |
| 21 |
mew-iso-2022-jp-2)) |
| 22 |
mew-iso-2022-jp-2)) |
| 23 |
|
| 24 |
(defun mew-charset-guess-string (str) |
| 25 |
(let ((ecsdb (mew-ecsdb-guess-string str))) |
| 26 |
(if ecsdb |
| 27 |
(mew-cs-to-charset (mew-ecsdb-get-cs ecsdb)) ;; not hcs, take care |
| 28 |
(mew-charset-m17n)))) |
| 29 |
|
| 30 |
(defun mew-ecsdb-guess-string (str) |
| 31 |
(with-temp-buffer |
| 32 |
(insert str) |
| 33 |
(mew-ecsdb-guess-region (point-min) (point-max)))) |
| 34 |
|
| 35 |
(defun mew-charset-guess-region (beg end) |
| 36 |
"Guess charset for the region." |
| 37 |
(interactive "r") |
| 38 |
(let ((ecsdb (mew-ecsdb-guess-region beg end)) |
| 39 |
ret) |
| 40 |
(if (null ecsdb) |
| 41 |
(setq ret (mew-charset-m17n)) |
| 42 |
(setq ret (mew-cs-to-charset (mew-ecsdb-get-cs ecsdb)))) ;; not hcs |
| 43 |
(if (mew-called-interactively-p) (message "%s" ret)) ;; for debug |
| 44 |
ret)) |
| 45 |
|
| 46 |
(defun mew-ecsdb-guess-region (beg end) |
| 47 |
(let* ((tcsl (mew-find-cs-region beg end)) |
| 48 |
(N (length tcsl)) |
| 49 |
(alst mew-cs-database-for-encoding) |
| 50 |
acsl csl ret) |
| 51 |
(dolist (ecsdb alst) |
| 52 |
(setq acsl (mew-ecsdb-get-lcs ecsdb)) |
| 53 |
(catch 'loop |
| 54 |
(dotimes (i N) |
| 55 |
(unless (member (nth i tcsl) acsl) |
| 56 |
(setq ecsdb nil) |
| 57 |
(setq acsl nil) |
| 58 |
(throw 'loop nil)))) |
| 59 |
(if (null ret) |
| 60 |
(setq ret ecsdb) |
| 61 |
(if (and acsl (< (length acsl) (length csl))) |
| 62 |
(setq ret ecsdb csl acsl)))) |
| 63 |
ret)) |
| 64 |
|
| 65 |
(autoload 'mew-zenkaku-katakana-region "mew-lang-jp" nil) |
| 66 |
(autoload 'mew-latin0-region "mew-lang-latin" nil) |
| 67 |
|
| 68 |
(defun mew-charset-sanity-check (beg end) |
| 69 |
"Eliminate invalid characters" |
| 70 |
(interactive "r") |
| 71 |
(let ((lcs (mew-find-cs-region beg end))) |
| 72 |
(cond |
| 73 |
((member mew-lc-kana lcs) |
| 74 |
(mew-zenkaku-katakana-region beg end)) |
| 75 |
((and (not (fboundp 'set-charset-priority)) |
| 76 |
(memq 'latin-iso8859-1 lcs) (memq 'latin-iso8859-15 lcs)) ;; xxx |
| 77 |
(mew-latin0-region beg end))))) |
| 78 |
|
| 79 |
;;; |
| 80 |
;;; |
| 81 |
;;; |
| 82 |
|
| 83 |
(defun mew-file-guess-coding-system (file) |
| 84 |
(with-temp-buffer |
| 85 |
(kill-local-variable 'find-file-literally) |
| 86 |
(set-buffer-multibyte t) |
| 87 |
(insert-file-contents file nil 0 1024) |
| 88 |
buffer-file-coding-system)) |
| 89 |
|
| 90 |
(defun mew-cs-strip-lineinfo (cs) |
| 91 |
(let ((str (symbol-name cs))) |
| 92 |
(if (string-match "-\\(unix\\|mac\\|dos\\)$" str) |
| 93 |
(intern (substring str 0 (match-beginning 0))) |
| 94 |
cs))) |
| 95 |
|
| 96 |
;;; |
| 97 |
;;; |
| 98 |
;;; |
| 99 |
|
| 100 |
(defun mew-coding-system-equal (cs1 cs2) |
| 101 |
;; Emacs 22 causes an error if cs is not defined. |
| 102 |
(condition-case nil |
| 103 |
(coding-system-equal cs1 cs2) |
| 104 |
(error nil))) |
| 105 |
|
| 106 |
(defun mew-cs-to-charset (cs) |
| 107 |
(let ((dcsdb (mew-assoc-equal cs mew-cs-database-for-decoding 1 'mew-coding-system-equal))) |
| 108 |
(if (null dcsdb) |
| 109 |
(mew-charset-m17n) |
| 110 |
(mew-dcsdb-get-charset dcsdb)))) |
| 111 |
|
| 112 |
(defun mew-charset-to-cs (charset) |
| 113 |
(when charset |
| 114 |
(let ((dcsdb (assoc (downcase charset) mew-cs-database-for-decoding))) |
| 115 |
(if (null dcsdb) |
| 116 |
mew-cs-unknown |
| 117 |
(mew-dcsdb-get-cs dcsdb))))) |
| 118 |
|
| 119 |
(defun mew-charset-to-ecsdb (charset) |
| 120 |
(mew-assoc-equal (mew-charset-to-cs charset) mew-cs-database-for-encoding 1)) |
| 121 |
|
| 122 |
(defun mew-charset-to-cte (charset) |
| 123 |
(mew-ecsdb-get-cte (mew-charset-to-ecsdb charset))) |
| 124 |
|
| 125 |
(defun mew-charset-to-delsp (charset) |
| 126 |
(mew-ecsdb-get-delsp (mew-charset-to-ecsdb charset))) |
| 127 |
|
| 128 |
;; |
| 129 |
|
| 130 |
(defvar mew-charset-list |
| 131 |
(mapcar 'mew-dcsdb-get-charset mew-cs-database-for-decoding)) |
| 132 |
|
| 133 |
;; |
| 134 |
|
| 135 |
(defun mew-cs-encode-arg (arg) |
| 136 |
(let ((cs (mew-ecsdb-cs-for-arg (mew-ecsdb-guess-string arg)))) |
| 137 |
(if (mew-coding-system-p cs) |
| 138 |
(mew-cs-encode-string arg cs) |
| 139 |
arg))) |
| 140 |
|
| 141 |
(provide 'mew-mule) |
| 142 |
|
| 143 |
;;; Copyright Notice: |
| 144 |
|
| 145 |
;; Copyright (C) 1998-2015 Mew developing team. |
| 146 |
;; All rights reserved. |
| 147 |
|
| 148 |
;; Redistribution and use in source and binary forms, with or without |
| 149 |
;; modification, are permitted provided that the following conditions |
| 150 |
;; are met: |
| 151 |
;; |
| 152 |
;; 1. Redistributions of source code must retain the above copyright |
| 153 |
;; notice, this list of conditions and the following disclaimer. |
| 154 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 155 |
;; notice, this list of conditions and the following disclaimer in the |
| 156 |
;; documentation and/or other materials provided with the distribution. |
| 157 |
;; 3. Neither the name of the team nor the names of its contributors |
| 158 |
;; may be used to endorse or promote products derived from this software |
| 159 |
;; without specific prior written permission. |
| 160 |
;; |
| 161 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 162 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 163 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 164 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 165 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 166 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 167 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 168 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 169 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 170 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 171 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 172 |
|
| 173 |
;;; mew-mule.el ends here |