mm-util.el 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. ;;; mm-util.el --- Utility functions for Mule and low level things
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; For Emacs <22.2 and XEmacs.
  19. (eval-and-compile
  20. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  21. (eval-when-compile (require 'cl))
  22. (require 'mail-prsvr)
  23. (eval-and-compile
  24. (if (featurep 'xemacs)
  25. (unless (ignore-errors
  26. (require 'timer-funcs))
  27. (require 'timer))
  28. (require 'timer)))
  29. (defvar mm-mime-mule-charset-alist )
  30. ;; Note this is not presently used on Emacs >= 23, which is good,
  31. ;; since it means standalone message-mode (which requires mml and
  32. ;; hence mml-util) does not load gnus-util.
  33. (autoload 'gnus-completing-read "gnus-util")
  34. ;; Emulate functions that are not available in every (X)Emacs version.
  35. ;; The name of a function is prefixed with mm-, like `mm-char-int' for
  36. ;; `char-int' that is a native XEmacs function, not available in Emacs.
  37. ;; Gnus programs all should use mm- functions, not the original ones.
  38. (eval-and-compile
  39. (mapc
  40. (lambda (elem)
  41. (let ((nfunc (intern (format "mm-%s" (car elem)))))
  42. (if (fboundp (car elem))
  43. (defalias nfunc (car elem))
  44. (defalias nfunc (cdr elem)))))
  45. `(;; `coding-system-list' is not available in XEmacs 21.4 built
  46. ;; without the `file-coding' feature.
  47. (coding-system-list . ignore)
  48. ;; `char-int' is an XEmacs function, not available in Emacs.
  49. (char-int . identity)
  50. ;; `coding-system-equal' is an Emacs function, not available in XEmacs.
  51. (coding-system-equal . equal)
  52. ;; `annotationp' is an XEmacs function, not available in Emacs.
  53. (annotationp . ignore)
  54. ;; `set-buffer-file-coding-system' is not available in XEmacs 21.4
  55. ;; built without the `file-coding' feature.
  56. (set-buffer-file-coding-system . ignore)
  57. ;; `read-charset' is an Emacs function, not available in XEmacs.
  58. (read-charset
  59. . ,(lambda (prompt)
  60. "Return a charset."
  61. (intern
  62. (gnus-completing-read
  63. prompt
  64. (mapcar (lambda (e) (symbol-name (car e)))
  65. mm-mime-mule-charset-alist)
  66. t))))
  67. ;; `subst-char-in-string' is not available in XEmacs 21.4.
  68. (subst-char-in-string
  69. . ,(lambda (from to string &optional inplace)
  70. ;; stolen (and renamed) from nnheader.el
  71. "Replace characters in STRING from FROM to TO.
  72. Unless optional argument INPLACE is non-nil, return a new string."
  73. (let ((string (if inplace string (copy-sequence string)))
  74. (len (length string))
  75. (idx 0))
  76. ;; Replace all occurrences of FROM with TO.
  77. (while (< idx len)
  78. (when (= (aref string idx) from)
  79. (aset string idx to))
  80. (setq idx (1+ idx)))
  81. string)))
  82. ;; `replace-in-string' is an XEmacs function, not available in Emacs.
  83. (replace-in-string
  84. . ,(lambda (string regexp rep &optional literal)
  85. "See `replace-regexp-in-string', only the order of args differs."
  86. (replace-regexp-in-string regexp rep string nil literal)))
  87. ;; `string-as-unibyte' is an Emacs function, not available in XEmacs.
  88. (string-as-unibyte . identity)
  89. ;; `string-make-unibyte' is an Emacs function, not available in XEmacs.
  90. (string-make-unibyte . identity)
  91. ;; string-as-multibyte often doesn't really do what you think it does.
  92. ;; Example:
  93. ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
  94. ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
  95. ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
  96. ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
  97. ;; but
  98. ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
  99. ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
  100. ;; Better use string-to-multibyte or encode-coding-string.
  101. ;; If you really need string-as-multibyte somewhere it's usually
  102. ;; because you're using the internal emacs-mule representation (maybe
  103. ;; because you're using string-as-unibyte somewhere), which is
  104. ;; generally a problem in itself.
  105. ;; Here is an approximate equivalence table to help think about it:
  106. ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
  107. ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
  108. ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
  109. ;; `string-as-multibyte' is an Emacs function, not available in XEmacs.
  110. (string-as-multibyte . identity)
  111. ;; `multibyte-string-p' is an Emacs function, not available in XEmacs.
  112. (multibyte-string-p . ignore)
  113. ;; `insert-byte' is available only in Emacs 23.1 or greater.
  114. (insert-byte . insert-char)
  115. ;; `multibyte-char-to-unibyte' is an Emacs function, not available
  116. ;; in XEmacs.
  117. (multibyte-char-to-unibyte . identity)
  118. ;; `set-buffer-multibyte' is an Emacs function, not available in XEmacs.
  119. (set-buffer-multibyte . ignore)
  120. ;; `special-display-p' is an Emacs function, not available in XEmacs.
  121. (special-display-p
  122. . ,(lambda (buffer-name)
  123. "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
  124. (and special-display-function
  125. (or (and (member buffer-name special-display-buffer-names) t)
  126. (cdr (assoc buffer-name special-display-buffer-names))
  127. (catch 'return
  128. (dolist (elem special-display-regexps)
  129. (and (stringp elem)
  130. (string-match elem buffer-name)
  131. (throw 'return t))
  132. (and (consp elem)
  133. (stringp (car elem))
  134. (string-match (car elem) buffer-name)
  135. (throw 'return (cdr elem)))))))))
  136. ;; `substring-no-properties' is available only in Emacs 22.1 or greater.
  137. (substring-no-properties
  138. . ,(lambda (string &optional from to)
  139. "Return a substring of STRING, without text properties.
  140. It starts at index FROM and ending before TO.
  141. TO may be nil or omitted; then the substring runs to the end of STRING.
  142. If FROM is nil or omitted, the substring starts at the beginning of STRING.
  143. If FROM or TO is negative, it counts from the end.
  144. With one argument, just copy STRING without its properties."
  145. (setq string (substring string (or from 0) to))
  146. (set-text-properties 0 (length string) nil string)
  147. string))
  148. ;; `line-number-at-pos' is available only in Emacs 22.1 or greater
  149. ;; and XEmacs 21.5.
  150. (line-number-at-pos
  151. . ,(lambda (&optional pos)
  152. "Return (narrowed) buffer line number at position POS.
  153. If POS is nil, use current buffer location.
  154. Counting starts at (point-min), so the value refers
  155. to the contents of the accessible portion of the buffer."
  156. (let ((opoint (or pos (point))) start)
  157. (save-excursion
  158. (goto-char (point-min))
  159. (setq start (point))
  160. (goto-char opoint)
  161. (forward-line 0)
  162. (1+ (count-lines start (point))))))))))
  163. ;; `decode-coding-string', `encode-coding-string', `decode-coding-region'
  164. ;; and `encode-coding-region' are available in Emacs and XEmacs built with
  165. ;; the `file-coding' feature, but the XEmacs versions treat nil, that is
  166. ;; given as the `coding-system' argument, as the `binary' coding system.
  167. (eval-and-compile
  168. (if (featurep 'xemacs)
  169. (if (featurep 'file-coding)
  170. (progn
  171. (defun mm-decode-coding-string (str coding-system)
  172. (if coding-system
  173. (decode-coding-string str coding-system)
  174. str))
  175. (defun mm-encode-coding-string (str coding-system)
  176. (if coding-system
  177. (encode-coding-string str coding-system)
  178. str))
  179. (defun mm-decode-coding-region (start end coding-system)
  180. (if coding-system
  181. (decode-coding-region start end coding-system)))
  182. (defun mm-encode-coding-region (start end coding-system)
  183. (if coding-system
  184. (encode-coding-region start end coding-system))))
  185. (defun mm-decode-coding-string (str coding-system) str)
  186. (defun mm-encode-coding-string (str coding-system) str)
  187. (defalias 'mm-decode-coding-region 'ignore)
  188. (defalias 'mm-encode-coding-region 'ignore))
  189. (defalias 'mm-decode-coding-string 'decode-coding-string)
  190. (defalias 'mm-encode-coding-string 'encode-coding-string)
  191. (defalias 'mm-decode-coding-region 'decode-coding-region)
  192. (defalias 'mm-encode-coding-region 'encode-coding-region)))
  193. ;; `string-to-multibyte' is available only in Emacs.
  194. (defalias 'mm-string-to-multibyte (if (featurep 'xemacs)
  195. 'identity
  196. 'string-to-multibyte))
  197. ;; `char-or-char-int-p' is an XEmacs function, not available in Emacs.
  198. (eval-and-compile
  199. (defalias 'mm-char-or-char-int-p
  200. (cond
  201. ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
  202. ((fboundp 'char-valid-p) 'char-valid-p)
  203. (t 'identity))))
  204. ;; `ucs-to-char' is a function that Mule-UCS provides.
  205. (eval-and-compile
  206. (if (featurep 'xemacs)
  207. (cond ((and (fboundp 'unicode-to-char) ;; XEmacs 21.5.
  208. (subrp (symbol-function 'unicode-to-char)))
  209. (if (featurep 'mule)
  210. (defalias 'mm-ucs-to-char 'unicode-to-char)
  211. (defun mm-ucs-to-char (codepoint)
  212. "Convert Unicode codepoint to character."
  213. (or (unicode-to-char codepoint) ?#))))
  214. ((featurep 'mule)
  215. (defun mm-ucs-to-char (codepoint)
  216. "Convert Unicode codepoint to character."
  217. (if (fboundp 'ucs-to-char) ;; Mule-UCS is loaded.
  218. (progn
  219. (defalias 'mm-ucs-to-char
  220. (lambda (codepoint)
  221. "Convert Unicode codepoint to character."
  222. (condition-case nil
  223. (or (ucs-to-char codepoint) ?#)
  224. (error ?#))))
  225. (mm-ucs-to-char codepoint))
  226. (condition-case nil
  227. (or (int-to-char codepoint) ?#)
  228. (error ?#)))))
  229. (t
  230. (defun mm-ucs-to-char (codepoint)
  231. "Convert Unicode codepoint to character."
  232. (condition-case nil
  233. (or (int-to-char codepoint) ?#)
  234. (error ?#)))))
  235. (if (let ((char (make-char 'japanese-jisx0208 36 34)))
  236. (eq char (decode-char 'ucs char)))
  237. ;; Emacs 23.
  238. (defalias 'mm-ucs-to-char 'identity)
  239. (defun mm-ucs-to-char (codepoint)
  240. "Convert Unicode codepoint to character."
  241. (or (decode-char 'ucs codepoint) ?#)))))
  242. ;; Fixme: This seems always to be used to read a MIME charset, so it
  243. ;; should be re-named and fixed (in Emacs) to offer completion only on
  244. ;; proper charset names (base coding systems which have a
  245. ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
  246. ;; test with
  247. ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
  248. ;; (coding-system-get 'iso-8859-1 :mime-charset))'
  249. ;; Actually, there should be an `mm-coding-system-mime-charset'.
  250. (eval-and-compile
  251. (defalias 'mm-read-coding-system
  252. (if (featurep 'emacs) 'read-coding-system
  253. (cond
  254. ((fboundp 'read-coding-system)
  255. (if (and (featurep 'xemacs)
  256. (<= (string-to-number emacs-version) 21.1))
  257. (lambda (prompt &optional default-coding-system)
  258. (read-coding-system prompt))
  259. 'read-coding-system))
  260. (t (lambda (prompt &optional default-coding-system)
  261. "Prompt the user for a coding system."
  262. (gnus-completing-read
  263. prompt (mapcar (lambda (s) (symbol-name (car s)))
  264. mm-mime-mule-charset-alist))))))))
  265. (defvar mm-coding-system-list nil)
  266. (defun mm-get-coding-system-list ()
  267. "Get the coding system list."
  268. (or mm-coding-system-list
  269. (setq mm-coding-system-list (mm-coding-system-list))))
  270. (defun mm-coding-system-p (cs)
  271. "Return non-nil if CS is a symbol naming a coding system.
  272. In XEmacs, also return non-nil if CS is a coding system object.
  273. If CS is available, return CS itself in Emacs, and return a coding
  274. system object in XEmacs."
  275. (if (fboundp 'find-coding-system)
  276. (and cs (find-coding-system cs))
  277. (if (fboundp 'coding-system-p)
  278. (when (coding-system-p cs)
  279. cs)
  280. ;; no-MULE XEmacs:
  281. (car (memq cs (mm-get-coding-system-list))))))
  282. (defvar mm-charset-synonym-alist
  283. `(
  284. ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
  285. ,@(unless (mm-coding-system-p 'x-ctext)
  286. '((x-ctext . ctext)))
  287. ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
  288. ;; positions!
  289. ,@(unless (mm-coding-system-p 'iso-8859-15)
  290. '((iso-8859-15 . iso-8859-1)))
  291. ;; BIG-5HKSCS is similar to, but different than, BIG-5.
  292. ,@(unless (mm-coding-system-p 'big5-hkscs)
  293. '((big5-hkscs . big5)))
  294. ;; A Microsoft misunderstanding.
  295. ,@(when (and (not (mm-coding-system-p 'unicode))
  296. (mm-coding-system-p 'utf-16-le))
  297. '((unicode . utf-16-le)))
  298. ;; A Microsoft misunderstanding.
  299. ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
  300. (if (mm-coding-system-p 'cp949)
  301. '((ks_c_5601-1987 . cp949))
  302. '((ks_c_5601-1987 . euc-kr))))
  303. ;; Windows-31J is Windows Codepage 932.
  304. ,@(when (and (not (mm-coding-system-p 'windows-31j))
  305. (mm-coding-system-p 'cp932))
  306. '((windows-31j . cp932)))
  307. ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
  308. ;; http://www.iana.org/assignments/charset-reg/GBK
  309. ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
  310. ,@(when (and (not (mm-coding-system-p 'gbk))
  311. (mm-coding-system-p 'cp936))
  312. '((gbk . cp936)))
  313. ;; UTF8 is a bogus name for UTF-8
  314. ,@(when (and (not (mm-coding-system-p 'utf8))
  315. (mm-coding-system-p 'utf-8))
  316. '((utf8 . utf-8)))
  317. ;; ISO8859-1 is a bogus name for ISO-8859-1
  318. ,@(when (and (not (mm-coding-system-p 'iso8859-1))
  319. (mm-coding-system-p 'iso-8859-1))
  320. '((iso8859-1 . iso-8859-1)))
  321. ;; ISO_8859-1 is a bogus name for ISO-8859-1
  322. ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
  323. (mm-coding-system-p 'iso-8859-1))
  324. '((iso_8859-1 . iso-8859-1)))
  325. )
  326. "A mapping from unknown or invalid charset names to the real charset names.
  327. See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
  328. (defun mm-codepage-setup (number &optional alias)
  329. "Create a coding system cpNUMBER.
  330. The coding system is created using `codepage-setup'. If ALIAS is
  331. non-nil, an alias is created and added to
  332. `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
  333. the alias. Else windows-NUMBER is used."
  334. (interactive
  335. (let ((completion-ignore-case t)
  336. (candidates (if (fboundp 'cp-supported-codepages)
  337. (cp-supported-codepages)
  338. ;; Removed in Emacs 23 (unicode), so signal an error:
  339. (error "`codepage-setup' not present in this Emacs version"))))
  340. (list (gnus-completing-read "Setup DOS Codepage" candidates
  341. t nil nil "437"))))
  342. (when alias
  343. (setq alias (if (stringp alias)
  344. (intern alias)
  345. (intern (format "windows-%s" number)))))
  346. (let* ((cp (intern (format "cp%s" number))))
  347. (unless (mm-coding-system-p cp)
  348. (if (fboundp 'codepage-setup) ; silence compiler
  349. (codepage-setup number)
  350. (error "`codepage-setup' not present in this Emacs version")))
  351. (when (and alias
  352. ;; Don't add alias if setup of cp failed.
  353. (mm-coding-system-p cp))
  354. (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
  355. (defcustom mm-codepage-iso-8859-list
  356. (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
  357. ;; Outlook users in Czech republic. Use this to allow reading of
  358. ;; their e-mails.
  359. '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
  360. ;; Europe). See also `gnus-article-dumbquotes-map'.
  361. '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
  362. '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
  363. "A list of Windows codepage numbers and iso-8859 charset numbers.
  364. If an element is a number corresponding to a supported windows
  365. codepage, appropriate entries to `mm-charset-synonym-alist' are
  366. added by `mm-setup-codepage-iso-8859'. An element may also be a
  367. cons cell where the car is a codepage number and the cdr is the
  368. corresponding number of an iso-8859 charset."
  369. :type '(list (set :inline t
  370. (const 1250 :tag "Central and East European")
  371. (const (1252 . 1) :tag "West European")
  372. (const (1254 . 9) :tag "Turkish")
  373. (const (1255 . 8) :tag "Hebrew"))
  374. (repeat :inline t
  375. :tag "Other options"
  376. (choice
  377. (integer :tag "Windows codepage number")
  378. (cons (integer :tag "Windows codepage number")
  379. (integer :tag "iso-8859 charset number")))))
  380. :version "22.1" ;; Gnus 5.10.9
  381. :group 'mime)
  382. (defcustom mm-codepage-ibm-list
  383. (list 437 ;; (US etc.)
  384. 860 ;; (Portugal)
  385. 861 ;; (Iceland)
  386. 862 ;; (Israel)
  387. 863 ;; (Canadian French)
  388. 865 ;; (Nordic)
  389. 852 ;;
  390. 850 ;; (Latin 1)
  391. 855 ;; (Cyrillic)
  392. 866 ;; (Cyrillic - Russian)
  393. 857 ;; (Turkish)
  394. 864 ;; (Arabic)
  395. 869 ;; (Greek)
  396. 874);; (Thai)
  397. ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
  398. ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
  399. "List of IBM codepage numbers.
  400. The codepage mappings slightly differ between IBM and other vendors.
  401. See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
  402. If an element is a number corresponding to a supported windows
  403. codepage, appropriate entries to `mm-charset-synonym-alist' are
  404. added by `mm-setup-codepage-ibm'."
  405. :type '(list (set :inline t
  406. (const 437 :tag "US etc.")
  407. (const 860 :tag "Portugal")
  408. (const 861 :tag "Iceland")
  409. (const 862 :tag "Israel")
  410. (const 863 :tag "Canadian French")
  411. (const 865 :tag "Nordic")
  412. (const 852)
  413. (const 850 :tag "Latin 1")
  414. (const 855 :tag "Cyrillic")
  415. (const 866 :tag "Cyrillic - Russian")
  416. (const 857 :tag "Turkish")
  417. (const 864 :tag "Arabic")
  418. (const 869 :tag "Greek")
  419. (const 874 :tag "Thai"))
  420. (repeat :inline t
  421. :tag "Other options"
  422. (integer :tag "Codepage number")))
  423. :version "22.1" ;; Gnus 5.10.9
  424. :group 'mime)
  425. (defun mm-setup-codepage-iso-8859 (&optional list)
  426. "Add appropriate entries to `mm-charset-synonym-alist'.
  427. Unless LIST is given, `mm-codepage-iso-8859-list' is used."
  428. (unless list
  429. (setq list mm-codepage-iso-8859-list))
  430. (dolist (i list)
  431. (let (cp windows iso)
  432. (if (consp i)
  433. (setq cp (intern (format "cp%d" (car i)))
  434. windows (intern (format "windows-%d" (car i)))
  435. iso (intern (format "iso-8859-%d" (cdr i))))
  436. (setq cp (intern (format "cp%d" i))
  437. windows (intern (format "windows-%d" i))))
  438. (unless (mm-coding-system-p windows)
  439. (if (mm-coding-system-p cp)
  440. (add-to-list 'mm-charset-synonym-alist (cons windows cp))
  441. (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
  442. (defun mm-setup-codepage-ibm (&optional list)
  443. "Add appropriate entries to `mm-charset-synonym-alist'.
  444. Unless LIST is given, `mm-codepage-ibm-list' is used."
  445. (unless list
  446. (setq list mm-codepage-ibm-list))
  447. (dolist (number list)
  448. (let ((ibm (intern (format "ibm%d" number)))
  449. (cp (intern (format "cp%d" number))))
  450. (when (and (not (mm-coding-system-p ibm))
  451. (mm-coding-system-p cp))
  452. (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
  453. ;; Initialize:
  454. (mm-setup-codepage-iso-8859)
  455. (mm-setup-codepage-ibm)
  456. ;; Note: this has to be defined before `mm-charset-to-coding-system'.
  457. (defcustom mm-charset-eval-alist
  458. (if (featurep 'xemacs)
  459. nil ;; I don't know what would be useful for XEmacs.
  460. '(;; Emacs 22 provides autoloads for 1250-1258
  461. ;; (i.e. `mm-codepage-setup' does nothing).
  462. (windows-1250 . (mm-codepage-setup 1250 t))
  463. (windows-1251 . (mm-codepage-setup 1251 t))
  464. (windows-1253 . (mm-codepage-setup 1253 t))
  465. (windows-1257 . (mm-codepage-setup 1257 t))))
  466. "An alist of (CHARSET . FORM) pairs.
  467. If an article is encoded in an unknown CHARSET, FORM is
  468. evaluated. This allows to load additional libraries providing
  469. charsets on demand. If supported by your Emacs version, you
  470. could use `autoload-coding-system' here."
  471. :version "22.1" ;; Gnus 5.10.9
  472. :type '(list (set :inline t
  473. (const (windows-1250 . (mm-codepage-setup 1250 t)))
  474. (const (windows-1251 . (mm-codepage-setup 1251 t)))
  475. (const (windows-1253 . (mm-codepage-setup 1253 t)))
  476. (const (windows-1257 . (mm-codepage-setup 1257 t)))
  477. (const (cp850 . (mm-codepage-setup 850 nil))))
  478. (repeat :inline t
  479. :tag "Other options"
  480. (cons (symbol :tag "charset")
  481. (symbol :tag "form"))))
  482. :group 'mime)
  483. (put 'mm-charset-eval-alist 'risky-local-variable t)
  484. (defvar mm-charset-override-alist)
  485. ;; Note: this function has to be defined before `mm-charset-override-alist'
  486. ;; since it will use this function in order to determine its default value
  487. ;; when loading mm-util.elc.
  488. (defun mm-charset-to-coding-system (charset &optional lbt
  489. allow-override silent)
  490. "Return coding-system corresponding to CHARSET.
  491. CHARSET is a symbol naming a MIME charset.
  492. If optional argument LBT (`unix', `dos' or `mac') is specified, it is
  493. used as the line break code type of the coding system.
  494. If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
  495. map undesired charset names to their replacement. This should
  496. only be used for decoding, not for encoding.
  497. A non-nil value of SILENT means don't issue a warning even if CHARSET
  498. is not available."
  499. ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
  500. (when (stringp charset)
  501. (setq charset (intern (downcase charset))))
  502. (when lbt
  503. (setq charset (intern (format "%s-%s" charset lbt))))
  504. (cond
  505. ((null charset)
  506. charset)
  507. ;; Running in a non-MULE environment.
  508. ((or (null (mm-get-coding-system-list))
  509. (not (fboundp 'coding-system-get)))
  510. charset)
  511. ;; Check override list quite early. Should only used for decoding, not for
  512. ;; encoding!
  513. ((and allow-override
  514. (let ((cs (cdr (assq charset mm-charset-override-alist))))
  515. (and cs (mm-coding-system-p cs) cs))))
  516. ;; ascii
  517. ((or (eq charset 'us-ascii)
  518. (string-match "ansi.x3.4" (symbol-name charset)))
  519. 'ascii)
  520. ;; Check to see whether we can handle this charset. (This depends
  521. ;; on there being some coding system matching each `mime-charset'
  522. ;; property defined, as there should be.)
  523. ((and (mm-coding-system-p charset)
  524. ;;; Doing this would potentially weed out incorrect charsets.
  525. ;;; charset
  526. ;;; (eq charset (coding-system-get charset 'mime-charset))
  527. )
  528. charset)
  529. ;; Use coding system Emacs knows.
  530. ((and (fboundp 'coding-system-from-name)
  531. (coding-system-from-name charset)))
  532. ;; Eval expressions from `mm-charset-eval-alist'
  533. ((let* ((el (assq charset mm-charset-eval-alist))
  534. (cs (car el))
  535. (form (cdr el)))
  536. (and cs
  537. form
  538. (prog2
  539. ;; Avoid errors...
  540. (condition-case nil (eval form) (error nil))
  541. ;; (message "Failed to eval `%s'" form))
  542. (mm-coding-system-p cs)
  543. (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
  544. cs)))
  545. ;; Translate invalid charsets.
  546. ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
  547. (and cs
  548. (mm-coding-system-p cs)
  549. ;; (message
  550. ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
  551. ;; cs charset)
  552. cs)))
  553. ;; Last resort: search the coding system list for entries which
  554. ;; have the right mime-charset in case the canonical name isn't
  555. ;; defined (though it should be).
  556. ((let (cs)
  557. ;; mm-get-coding-system-list returns a list of cs without lbt.
  558. ;; Do we need -lbt?
  559. (dolist (c (mm-get-coding-system-list))
  560. (if (and (null cs)
  561. (eq charset (or (coding-system-get c :mime-charset)
  562. (coding-system-get c 'mime-charset))))
  563. (setq cs c)))
  564. (unless (or silent cs)
  565. ;; Warn the user about unknown charset:
  566. (if (fboundp 'gnus-message)
  567. (gnus-message 7 "Unknown charset: %s" charset)
  568. (message "Unknown charset: %s" charset)))
  569. cs))))
  570. ;; Note: `mm-charset-to-coding-system' has to be defined before this.
  571. (defcustom mm-charset-override-alist
  572. ;; Note: pairs that cannot be used in the Emacs version currently running
  573. ;; will be removed.
  574. '((gb2312 . gbk)
  575. (iso-8859-1 . windows-1252)
  576. (iso-8859-8 . windows-1255)
  577. (iso-8859-9 . windows-1254))
  578. "A mapping from undesired charset names to their replacement.
  579. You may add pairs like (iso-8859-1 . windows-1252) here,
  580. i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
  581. superset of iso-8859-1."
  582. :type
  583. '(list
  584. :convert-widget
  585. (lambda (widget)
  586. (let ((defaults
  587. (delq nil
  588. (mapcar (lambda (pair)
  589. (if (mm-charset-to-coding-system (cdr pair)
  590. nil nil t)
  591. pair))
  592. '((gb2312 . gbk)
  593. (iso-8859-1 . windows-1252)
  594. (iso-8859-8 . windows-1255)
  595. (iso-8859-9 . windows-1254)
  596. (undecided . windows-1252)))))
  597. (val (copy-sequence (default-value 'mm-charset-override-alist)))
  598. pair rest)
  599. (while val
  600. (push (if (and (prog1
  601. (setq pair (assq (caar val) defaults))
  602. (setq defaults (delq pair defaults)))
  603. (equal (car val) pair))
  604. `(const ,pair)
  605. `(cons :format "%v"
  606. (const :format "(%v" ,(caar val))
  607. (symbol :size 3 :format " . %v)\n" ,(cdar val))))
  608. rest)
  609. (setq val (cdr val)))
  610. (while defaults
  611. (push `(const ,(pop defaults)) rest))
  612. (widget-convert
  613. 'list
  614. `(set :inline t :format "%v" ,@(nreverse rest))
  615. `(repeat :inline t :tag "Other options"
  616. (cons :format "%v"
  617. (symbol :size 3 :format "(%v")
  618. (symbol :size 3 :format " . %v)\n")))))))
  619. ;; Remove pairs that cannot be used in the Emacs version currently
  620. ;; running. Note that this section will be evaluated when loading
  621. ;; mm-util.elc.
  622. :set (lambda (symbol value)
  623. (custom-set-default
  624. symbol (delq nil
  625. (mapcar (lambda (pair)
  626. (if (mm-charset-to-coding-system (cdr pair)
  627. nil nil t)
  628. pair))
  629. value))))
  630. :version "22.1" ;; Gnus 5.10.9
  631. :group 'mime)
  632. (defvar mm-binary-coding-system
  633. (cond
  634. ((mm-coding-system-p 'binary) 'binary)
  635. ((mm-coding-system-p 'no-conversion) 'no-conversion)
  636. (t nil))
  637. "100% binary coding system.")
  638. (defvar mm-text-coding-system
  639. (or (if (memq system-type '(windows-nt ms-dos))
  640. (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
  641. (and (mm-coding-system-p 'raw-text) 'raw-text))
  642. mm-binary-coding-system)
  643. "Text-safe coding system (For removing ^M).")
  644. (defvar mm-text-coding-system-for-write nil
  645. "Text coding system for write.")
  646. (defvar mm-auto-save-coding-system
  647. (cond
  648. ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
  649. (if (memq system-type '(windows-nt ms-dos))
  650. (if (mm-coding-system-p 'utf-8-emacs-dos)
  651. 'utf-8-emacs-dos mm-binary-coding-system)
  652. 'utf-8-emacs))
  653. ((mm-coding-system-p 'emacs-mule)
  654. (if (memq system-type '(windows-nt ms-dos))
  655. (if (mm-coding-system-p 'emacs-mule-dos)
  656. 'emacs-mule-dos mm-binary-coding-system)
  657. 'emacs-mule))
  658. ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
  659. (t mm-binary-coding-system))
  660. "Coding system of auto save file.")
  661. (defvar mm-universal-coding-system mm-auto-save-coding-system
  662. "The universal coding system.")
  663. ;; Fixme: some of the cars here aren't valid MIME charsets. That
  664. ;; should only matter with XEmacs, though.
  665. (defvar mm-mime-mule-charset-alist
  666. `((us-ascii ascii)
  667. (iso-8859-1 latin-iso8859-1)
  668. (iso-8859-2 latin-iso8859-2)
  669. (iso-8859-3 latin-iso8859-3)
  670. (iso-8859-4 latin-iso8859-4)
  671. (iso-8859-5 cyrillic-iso8859-5)
  672. ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
  673. ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
  674. ;; charset is koi8-r, not iso-8859-5.
  675. (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
  676. (iso-8859-6 arabic-iso8859-6)
  677. (iso-8859-7 greek-iso8859-7)
  678. (iso-8859-8 hebrew-iso8859-8)
  679. (iso-8859-9 latin-iso8859-9)
  680. (iso-8859-14 latin-iso8859-14)
  681. (iso-8859-15 latin-iso8859-15)
  682. (viscii vietnamese-viscii-lower)
  683. (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
  684. (euc-kr korean-ksc5601)
  685. (gb2312 chinese-gb2312)
  686. (gbk chinese-gbk)
  687. (gb18030 gb18030-2-byte
  688. gb18030-4-byte-bmp gb18030-4-byte-smp
  689. gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
  690. (big5 chinese-big5-1 chinese-big5-2)
  691. (tibetan tibetan)
  692. (thai-tis620 thai-tis620)
  693. (windows-1251 cyrillic-iso8859-5)
  694. (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
  695. (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
  696. latin-jisx0201 japanese-jisx0208-1978
  697. chinese-gb2312 japanese-jisx0208
  698. korean-ksc5601 japanese-jisx0212)
  699. (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
  700. latin-jisx0201 japanese-jisx0208-1978
  701. chinese-gb2312 japanese-jisx0208
  702. korean-ksc5601 japanese-jisx0212
  703. chinese-cns11643-1 chinese-cns11643-2)
  704. (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
  705. cyrillic-iso8859-5 greek-iso8859-7
  706. latin-jisx0201 japanese-jisx0208-1978
  707. chinese-gb2312 japanese-jisx0208
  708. korean-ksc5601 japanese-jisx0212
  709. chinese-cns11643-1 chinese-cns11643-2
  710. chinese-cns11643-3 chinese-cns11643-4
  711. chinese-cns11643-5 chinese-cns11643-6
  712. chinese-cns11643-7)
  713. (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
  714. japanese-jisx0213-1 japanese-jisx0213-2)
  715. (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
  716. ,(cond ((fboundp 'unicode-precedence-list)
  717. (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
  718. (unicode-precedence-list)))))
  719. ((or (not (fboundp 'charsetp)) ;; non-Mule case
  720. (charsetp 'unicode-a)
  721. (not (mm-coding-system-p 'mule-utf-8)))
  722. '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
  723. (t ;; If we have utf-8 we're in Mule 5+.
  724. (append '(utf-8)
  725. (delete 'ascii
  726. (coding-system-get 'mule-utf-8 'safe-charsets))))))
  727. "Alist of MIME-charset/MULE-charsets.")
  728. (defun mm-enrich-utf-8-by-mule-ucs ()
  729. "Make the `utf-8' MIME charset usable by the Mule-UCS package.
  730. This function will run when the `un-define' module is loaded under
  731. XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
  732. with Mule charsets. It is completely useless for Emacs."
  733. (when (boundp 'unicode-basic-translation-charset-order-list)
  734. (condition-case nil
  735. (let ((val (delq
  736. 'ascii
  737. (copy-sequence
  738. (symbol-value
  739. 'unicode-basic-translation-charset-order-list))))
  740. (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
  741. (if elem
  742. (setcdr elem val)
  743. (setq mm-mime-mule-charset-alist
  744. (nconc mm-mime-mule-charset-alist
  745. (list (cons 'utf-8 val))))))
  746. (error))))
  747. ;; Correct by construction, but should be unnecessary for Emacs:
  748. (if (featurep 'xemacs)
  749. (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
  750. (when (and (fboundp 'coding-system-list)
  751. (fboundp 'sort-coding-systems))
  752. (let ((css (sort-coding-systems (coding-system-list 'base-only)))
  753. cs mime mule alist)
  754. (while css
  755. (setq cs (pop css)
  756. mime (or (coding-system-get cs :mime-charset); Emacs 23 (unicode)
  757. (coding-system-get cs 'mime-charset)))
  758. (when (and mime
  759. (not (eq t (setq mule
  760. (coding-system-get cs 'safe-charsets))))
  761. (not (assq mime alist)))
  762. (push (cons mime (delq 'ascii mule)) alist)))
  763. (setq mm-mime-mule-charset-alist (nreverse alist)))))
  764. (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
  765. "A list of special charsets.
  766. Valid elements include:
  767. `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
  768. `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
  769. )
  770. (defvar mm-iso-8859-15-compatible
  771. '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
  772. (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
  773. "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
  774. (defvar mm-iso-8859-x-to-15-table
  775. (and (fboundp 'coding-system-p)
  776. (mm-coding-system-p 'iso-8859-15)
  777. (mapcar
  778. (lambda (cs)
  779. (if (mm-coding-system-p (car cs))
  780. (let ((c (string-to-char
  781. (decode-coding-string "\341" (car cs)))))
  782. (cons (char-charset c)
  783. (cons
  784. (- (string-to-char
  785. (decode-coding-string "\341" 'iso-8859-15)) c)
  786. (string-to-list (decode-coding-string (car (cdr cs))
  787. (car cs))))))
  788. '(gnus-charset 0)))
  789. mm-iso-8859-15-compatible))
  790. "A table of the difference character between ISO-8859-X and ISO-8859-15.")
  791. (defcustom mm-coding-system-priorities
  792. (let ((lang (if (boundp 'current-language-environment)
  793. (symbol-value 'current-language-environment))))
  794. (cond (;; XEmacs without Mule but with `file-coding'.
  795. (not lang) nil)
  796. ;; In XEmacs 21.5 it may be the one like "Japanese (UTF-8)".
  797. ((string-match "\\`Japanese" lang)
  798. ;; Japanese users prefer iso-2022-jp to euc-japan or
  799. ;; shift_jis, however iso-8859-1 should be used when
  800. ;; there are only ASCII text and Latin-1 characters.
  801. '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8))))
  802. "Preferred coding systems for encoding outgoing messages.
  803. More than one suitable coding system may be found for some text.
  804. By default, the coding system with the highest priority is used
  805. to encode outgoing messages (see `sort-coding-systems'). If this
  806. variable is set, it overrides the default priority."
  807. :version "21.2"
  808. :type '(repeat (symbol :tag "Coding system"))
  809. :group 'mime)
  810. ;; ??
  811. (defvar mm-use-find-coding-systems-region
  812. (fboundp 'find-coding-systems-region)
  813. "Use `find-coding-systems-region' to find proper coding systems.
  814. Setting it to nil is useful on Emacsen supporting Unicode if sending
  815. mail with multiple parts is preferred to sending a Unicode one.")
  816. (defvar mm-extra-numeric-entities
  817. (mapcar
  818. (lambda (item)
  819. (cons (car item) (mm-ucs-to-char (cdr item))))
  820. '((#x80 . #x20AC) (#x82 . #x201A) (#x83 . #x0192) (#x84 . #x201E)
  821. (#x85 . #x2026) (#x86 . #x2020) (#x87 . #x2021) (#x88 . #x02C6)
  822. (#x89 . #x2030) (#x8A . #x0160) (#x8B . #x2039) (#x8C . #x0152)
  823. (#x8E . #x017D) (#x91 . #x2018) (#x92 . #x2019) (#x93 . #x201C)
  824. (#x94 . #x201D) (#x95 . #x2022) (#x96 . #x2013) (#x97 . #x2014)
  825. (#x98 . #x02DC) (#x99 . #x2122) (#x9A . #x0161) (#x9B . #x203A)
  826. (#x9C . #x0153) (#x9E . #x017E) (#x9F . #x0178)))
  827. "*Alist of extra numeric entities and characters other than ISO 10646.
  828. This table is used for decoding extra numeric entities to characters,
  829. like \"&#128;\" to the euro sign, mainly in html messages.")
  830. ;;; Internal variables:
  831. ;;; Functions:
  832. (defun mm-mule-charset-to-mime-charset (charset)
  833. "Return the MIME charset corresponding to the given Mule CHARSET."
  834. (if (and (fboundp 'find-coding-systems-for-charsets)
  835. (fboundp 'sort-coding-systems))
  836. (let ((css (sort (sort-coding-systems
  837. (find-coding-systems-for-charsets (list charset)))
  838. 'mm-sort-coding-systems-predicate))
  839. cs mime)
  840. (while (and (not mime)
  841. css)
  842. (when (setq cs (pop css))
  843. (setq mime (or (coding-system-get cs :mime-charset)
  844. (coding-system-get cs 'mime-charset)))))
  845. mime)
  846. (let ((alist (mapcar (lambda (cs)
  847. (assq cs mm-mime-mule-charset-alist))
  848. (sort (mapcar 'car mm-mime-mule-charset-alist)
  849. 'mm-sort-coding-systems-predicate)))
  850. out)
  851. (while alist
  852. (when (memq charset (cdar alist))
  853. (setq out (caar alist)
  854. alist nil))
  855. (pop alist))
  856. out)))
  857. (eval-and-compile
  858. (if (featurep 'xemacs)
  859. (defalias 'mm-enable-multibyte 'ignore)
  860. (defun mm-enable-multibyte ()
  861. "Set the multibyte flag of the current buffer.
  862. Only do this if the default value of `enable-multibyte-characters' is
  863. non-nil. This is a no-op in XEmacs."
  864. (set-buffer-multibyte 'to)))
  865. (if (featurep 'xemacs)
  866. (defalias 'mm-disable-multibyte 'ignore)
  867. (defun mm-disable-multibyte ()
  868. "Unset the multibyte flag of in the current buffer.
  869. This is a no-op in XEmacs."
  870. (set-buffer-multibyte nil))))
  871. (defun mm-preferred-coding-system (charset)
  872. ;; A typo in some Emacs versions.
  873. (or (get-charset-property charset 'preferred-coding-system)
  874. (get-charset-property charset 'prefered-coding-system)))
  875. ;; Mule charsets shouldn't be used.
  876. (defsubst mm-guess-charset ()
  877. "Guess Mule charset from the language environment."
  878. (or
  879. mail-parse-mule-charset ;; cached mule-charset
  880. (progn
  881. (setq mail-parse-mule-charset
  882. (and (boundp 'current-language-environment)
  883. (car (last
  884. (assq 'charset
  885. (assoc current-language-environment
  886. language-info-alist))))))
  887. (if (or (not mail-parse-mule-charset)
  888. (eq mail-parse-mule-charset 'ascii))
  889. (setq mail-parse-mule-charset
  890. (or (car (last (assq mail-parse-charset
  891. mm-mime-mule-charset-alist)))
  892. ;; default
  893. 'latin-iso8859-1)))
  894. mail-parse-mule-charset)))
  895. (defun mm-charset-after (&optional pos)
  896. "Return charset of a character in current buffer at position POS.
  897. If POS is nil, it defaults to the current point.
  898. If POS is out of range, the value is nil.
  899. If the charset is `composition', return the actual one."
  900. (let ((char (char-after pos)) charset)
  901. (if (< (mm-char-int char) 128)
  902. (setq charset 'ascii)
  903. ;; charset-after is fake in some Emacsen.
  904. (setq charset (and (fboundp 'char-charset) (char-charset char)))
  905. (if (eq charset 'composition) ; Mule 4
  906. (let ((p (or pos (point))))
  907. (cadr (find-charset-region p (1+ p))))
  908. (if (and charset (not (memq charset '(ascii eight-bit-control
  909. eight-bit-graphic))))
  910. charset
  911. (mm-guess-charset))))))
  912. (defun mm-mime-charset (charset)
  913. "Return the MIME charset corresponding to the given Mule CHARSET."
  914. (if (eq charset 'unknown)
  915. (error "The message contains non-printable characters, please use attachment"))
  916. (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
  917. (or
  918. (and (mm-preferred-coding-system charset)
  919. (or (coding-system-get
  920. (mm-preferred-coding-system charset) :mime-charset)
  921. (coding-system-get
  922. (mm-preferred-coding-system charset) 'mime-charset)))
  923. (and (eq charset 'ascii)
  924. 'us-ascii)
  925. (mm-preferred-coding-system charset)
  926. (mm-mule-charset-to-mime-charset charset))
  927. ;; This is for XEmacs.
  928. (mm-mule-charset-to-mime-charset charset)))
  929. ;; `delete-dups' is not available in XEmacs 21.4.
  930. (if (fboundp 'delete-dups)
  931. (defalias 'mm-delete-duplicates 'delete-dups)
  932. (defun mm-delete-duplicates (list)
  933. "Destructively remove `equal' duplicates from LIST.
  934. Store the result in LIST and return it. LIST must be a proper list.
  935. Of several `equal' occurrences of an element in LIST, the first
  936. one is kept.
  937. This is a compatibility function for Emacsen without `delete-dups'."
  938. ;; Code from `subr.el' in Emacs 22:
  939. (let ((tail list))
  940. (while tail
  941. (setcdr tail (delete (car tail) (cdr tail)))
  942. (setq tail (cdr tail))))
  943. list))
  944. ;; Fixme: This is used in places when it should be testing the
  945. ;; default multibyteness. See mm-default-multibyte-p.
  946. (eval-and-compile
  947. (if (and (not (featurep 'xemacs))
  948. (boundp 'enable-multibyte-characters))
  949. (defun mm-multibyte-p ()
  950. "Non-nil if multibyte is enabled in the current buffer."
  951. enable-multibyte-characters)
  952. (defun mm-multibyte-p () (featurep 'mule))))
  953. (defun mm-default-multibyte-p ()
  954. "Return non-nil if the session is multibyte.
  955. This affects whether coding conversion should be attempted generally."
  956. (if (featurep 'mule)
  957. (if (boundp 'enable-multibyte-characters)
  958. (default-value 'enable-multibyte-characters)
  959. t)))
  960. (defun mm-iso-8859-x-to-15-region (&optional b e)
  961. (if (fboundp 'char-charset)
  962. (let (charset item c inconvertible)
  963. (save-restriction
  964. (if e (narrow-to-region b e))
  965. (goto-char (point-min))
  966. (skip-chars-forward "\0-\177")
  967. (while (not (eobp))
  968. (cond
  969. ((not (setq item (assq (char-charset (setq c (char-after)))
  970. mm-iso-8859-x-to-15-table)))
  971. (forward-char))
  972. ((memq c (cdr (cdr item)))
  973. (setq inconvertible t)
  974. (forward-char))
  975. (t
  976. (insert-before-markers (prog1 (+ c (car (cdr item)))
  977. (delete-char 1)))))
  978. (skip-chars-forward "\0-\177")))
  979. (not inconvertible))))
  980. (defun mm-sort-coding-systems-predicate (a b)
  981. (let ((priorities
  982. (mapcar (lambda (cs)
  983. ;; Note: invalid entries are dropped silently
  984. (and (setq cs (mm-coding-system-p cs))
  985. (coding-system-base cs)))
  986. mm-coding-system-priorities)))
  987. (and (setq a (mm-coding-system-p a))
  988. (if (setq b (mm-coding-system-p b))
  989. (> (length (memq (coding-system-base a) priorities))
  990. (length (memq (coding-system-base b) priorities)))
  991. t))))
  992. (eval-when-compile
  993. (autoload 'latin-unity-massage-name "latin-unity")
  994. (autoload 'latin-unity-maybe-remap "latin-unity")
  995. (autoload 'latin-unity-representations-feasible-region "latin-unity")
  996. (autoload 'latin-unity-representations-present-region "latin-unity"))
  997. (defvar latin-unity-coding-systems)
  998. (defvar latin-unity-ucs-list)
  999. (defun mm-xemacs-find-mime-charset-1 (begin end)
  1000. "Determine which MIME charset to use to send region as message.
  1001. This uses the XEmacs-specific latin-unity package to better handle the
  1002. case where identical characters from diverse ISO-8859-? character sets
  1003. can be encoded using a single one of the corresponding coding systems.
  1004. It treats `mm-coding-system-priorities' as the list of preferred
  1005. coding systems; a useful example setting for this list in Western
  1006. Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
  1007. to the very standard Latin 1 coding system, and only move to coding
  1008. systems that are less supported as is necessary to encode the
  1009. characters that exist in the buffer.
  1010. Latin Unity doesn't know about those non-ASCII Roman characters that
  1011. are available in various East Asian character sets. As such, its
  1012. behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
  1013. buffer and it can otherwise be encoded as Latin 1, won't be ideal.
  1014. But this is very much a corner case, so don't worry about it."
  1015. (let ((systems mm-coding-system-priorities) csets psets curset)
  1016. ;; Load the Latin Unity library, if available.
  1017. (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
  1018. (require 'latin-unity))
  1019. ;; Now, can we use it?
  1020. (if (featurep 'latin-unity)
  1021. (progn
  1022. (setq csets (latin-unity-representations-feasible-region begin end)
  1023. psets (latin-unity-representations-present-region begin end))
  1024. (catch 'done
  1025. ;; Pass back the first coding system in the preferred list
  1026. ;; that can encode the whole region.
  1027. (dolist (curset systems)
  1028. (setq curset (latin-unity-massage-name 'buffer-default curset))
  1029. ;; If the coding system is a universal coding system, then
  1030. ;; it can certainly encode all the characters in the region.
  1031. (if (memq curset latin-unity-ucs-list)
  1032. (throw 'done (list curset)))
  1033. ;; If a coding system isn't universal, and isn't in
  1034. ;; the list that latin unity knows about, we can't
  1035. ;; decide whether to use it here. Leave that until later
  1036. ;; in `mm-find-mime-charset-region' function, whence we
  1037. ;; have been called.
  1038. (unless (memq curset latin-unity-coding-systems)
  1039. (throw 'done nil))
  1040. ;; Right, we know about this coding system, and it may
  1041. ;; conceivably be able to encode all the characters in
  1042. ;; the region.
  1043. (if (latin-unity-maybe-remap begin end curset csets psets t)
  1044. (throw 'done (list curset))))
  1045. ;; Can't encode using anything from the
  1046. ;; `mm-coding-system-priorities' list.
  1047. ;; Leave `mm-find-mime-charset' to do most of the work.
  1048. nil))
  1049. ;; Right, latin unity isn't available; let `mm-find-charset-region'
  1050. ;; take its default action, which equally applies to GNU Emacs.
  1051. nil)))
  1052. (defmacro mm-xemacs-find-mime-charset (begin end)
  1053. (when (featurep 'xemacs)
  1054. `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
  1055. (declare-function mm-delete-duplicates "mm-util" (list))
  1056. (defun mm-find-mime-charset-region (b e &optional hack-charsets)
  1057. "Return the MIME charsets needed to encode the region between B and E.
  1058. nil means ASCII, a single-element list represents an appropriate MIME
  1059. charset, and a longer list means no appropriate charset."
  1060. (let (charsets)
  1061. ;; The return possibilities of this function are a mess...
  1062. (or (and (mm-multibyte-p)
  1063. mm-use-find-coding-systems-region
  1064. ;; Find the mime-charset of the most preferred coding
  1065. ;; system that has one.
  1066. (let ((systems (find-coding-systems-region b e)))
  1067. (when mm-coding-system-priorities
  1068. (setq systems
  1069. (sort systems 'mm-sort-coding-systems-predicate)))
  1070. (setq systems (delq 'compound-text systems))
  1071. (unless (equal systems '(undecided))
  1072. (while systems
  1073. (let* ((head (pop systems))
  1074. (cs (or (coding-system-get head :mime-charset)
  1075. (coding-system-get head 'mime-charset))))
  1076. ;; The mime-charset (`x-ctext') of
  1077. ;; `compound-text' is not in the IANA list. We
  1078. ;; shouldn't normally use anything here with a
  1079. ;; mime-charset having an `x-' prefix.
  1080. ;; Fixme: Allow this to be overridden, since
  1081. ;; there is existing use of x-ctext.
  1082. ;; Also people apparently need the coding system
  1083. ;; `iso-2022-jp-3' (which Mule-UCS defines with
  1084. ;; mime-charset, though it's not valid).
  1085. (if (and cs
  1086. (not (string-match "^[Xx]-" (symbol-name cs)))
  1087. ;; UTF-16 of any variety is invalid for
  1088. ;; text parts and, unfortunately, has
  1089. ;; mime-charset defined both in Mule-UCS
  1090. ;; and versions of Emacs. (The name
  1091. ;; might be `mule-utf-16...' or
  1092. ;; `utf-16...'.)
  1093. (not (string-match "utf-16" (symbol-name cs))))
  1094. (setq systems nil
  1095. charsets (list cs))))))
  1096. charsets))
  1097. ;; If we're XEmacs, and some coding system is appropriate,
  1098. ;; mm-xemacs-find-mime-charset will return an appropriate list.
  1099. ;; Otherwise, we'll get nil, and the next setq will get invoked.
  1100. (setq charsets (mm-xemacs-find-mime-charset b e))
  1101. ;; Fixme: won't work for unibyte Emacs 23:
  1102. ;; We're not multibyte, or a single coding system won't cover it.
  1103. (setq charsets
  1104. (mm-delete-duplicates
  1105. (mapcar 'mm-mime-charset
  1106. (delq 'ascii
  1107. (mm-find-charset-region b e))))))
  1108. (if (and (> (length charsets) 1)
  1109. (memq 'iso-8859-15 charsets)
  1110. (memq 'iso-8859-15 hack-charsets)
  1111. (save-excursion (mm-iso-8859-x-to-15-region b e)))
  1112. (dolist (x mm-iso-8859-15-compatible)
  1113. (setq charsets (delq (car x) charsets))))
  1114. (if (and (memq 'iso-2022-jp-2 charsets)
  1115. (memq 'iso-2022-jp-2 hack-charsets))
  1116. (setq charsets (delq 'iso-2022-jp charsets)))
  1117. ;; Attempt to reduce the number of charsets if utf-8 is available.
  1118. (if (and (featurep 'xemacs)
  1119. (> (length charsets) 1)
  1120. (mm-coding-system-p 'utf-8))
  1121. (let ((mm-coding-system-priorities
  1122. (cons 'utf-8 mm-coding-system-priorities)))
  1123. (setq charsets
  1124. (mm-delete-duplicates
  1125. (mapcar 'mm-mime-charset
  1126. (delq 'ascii
  1127. (mm-find-charset-region b e)))))))
  1128. charsets))
  1129. (defmacro mm-with-unibyte-buffer (&rest forms)
  1130. "Create a temporary buffer, and evaluate FORMS there like `progn'.
  1131. Use unibyte mode for this."
  1132. `(with-temp-buffer
  1133. (mm-disable-multibyte)
  1134. ,@forms))
  1135. (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
  1136. (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
  1137. (defmacro mm-with-multibyte-buffer (&rest forms)
  1138. "Create a temporary buffer, and evaluate FORMS there like `progn'.
  1139. Use multibyte mode for this."
  1140. `(with-temp-buffer
  1141. (mm-enable-multibyte)
  1142. ,@forms))
  1143. (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
  1144. (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
  1145. (defmacro mm-with-unibyte-current-buffer (&rest forms)
  1146. "Evaluate FORMS with current buffer temporarily made unibyte.
  1147. Equivalent to `progn' in XEmacs.
  1148. Note: We recommend not using this macro any more; there should be
  1149. better ways to do a similar thing. The previous version of this macro
  1150. bound the default value of `enable-multibyte-characters' to nil while
  1151. evaluating FORMS but it is no longer done. So, some programs assuming
  1152. it if any may malfunction."
  1153. (if (featurep 'xemacs)
  1154. `(progn ,@forms)
  1155. (let ((multibyte (make-symbol "multibyte")))
  1156. `(let ((,multibyte enable-multibyte-characters))
  1157. (when ,multibyte
  1158. (set-buffer-multibyte nil))
  1159. (prog1
  1160. (progn ,@forms)
  1161. (when ,multibyte
  1162. (set-buffer-multibyte t)))))))
  1163. (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
  1164. (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
  1165. (defun mm-find-charset-region (b e)
  1166. "Return a list of Emacs charsets in the region B to E."
  1167. (cond
  1168. ((and (mm-multibyte-p)
  1169. (fboundp 'find-charset-region))
  1170. ;; Remove composition since the base charsets have been included.
  1171. ;; Remove eight-bit-*, treat them as ascii.
  1172. (let ((css (find-charset-region b e)))
  1173. (dolist (cs
  1174. '(composition eight-bit-control eight-bit-graphic control-1)
  1175. css)
  1176. (setq css (delq cs css)))))
  1177. (t
  1178. ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
  1179. (save-excursion
  1180. (save-restriction
  1181. (narrow-to-region b e)
  1182. (goto-char (point-min))
  1183. (skip-chars-forward "\0-\177")
  1184. (if (eobp)
  1185. '(ascii)
  1186. (let (charset)
  1187. (setq charset
  1188. (and (boundp 'current-language-environment)
  1189. (car (last (assq 'charset
  1190. (assoc current-language-environment
  1191. language-info-alist))))))
  1192. (if (eq charset 'ascii) (setq charset nil))
  1193. (or charset
  1194. (setq charset
  1195. (car (last (assq mail-parse-charset
  1196. mm-mime-mule-charset-alist)))))
  1197. (list 'ascii (or charset 'latin-iso8859-1)))))))))
  1198. (defun mm-auto-mode-alist ()
  1199. "Return an `auto-mode-alist' with only the .gz (etc) thingies."
  1200. (let ((alist auto-mode-alist)
  1201. out)
  1202. (while alist
  1203. (when (listp (cdar alist))
  1204. (push (car alist) out))
  1205. (pop alist))
  1206. (nreverse out)))
  1207. (defvar mm-inhibit-file-name-handlers
  1208. '(jka-compr-handler image-file-handler epa-file-handler)
  1209. "A list of handlers doing (un)compression (etc) thingies.")
  1210. (defun mm-insert-file-contents (filename &optional visit beg end replace
  1211. inhibit)
  1212. "Like `insert-file-contents', but only reads in the file.
  1213. A buffer may be modified in several ways after reading into the buffer due
  1214. to advanced Emacs features, such as file-name-handlers, format decoding,
  1215. `find-file-hooks', etc.
  1216. If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
  1217. This function ensures that none of these modifications will take place."
  1218. (letf* ((format-alist nil)
  1219. (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
  1220. ((default-value 'major-mode) 'fundamental-mode)
  1221. (enable-local-variables nil)
  1222. (after-insert-file-functions nil)
  1223. (enable-local-eval nil)
  1224. (inhibit-file-name-operation (if inhibit
  1225. 'insert-file-contents
  1226. inhibit-file-name-operation))
  1227. (inhibit-file-name-handlers
  1228. (if inhibit
  1229. (append mm-inhibit-file-name-handlers
  1230. inhibit-file-name-handlers)
  1231. inhibit-file-name-handlers))
  1232. (ffh (if (boundp 'find-file-hook)
  1233. 'find-file-hook
  1234. 'find-file-hooks))
  1235. (val (symbol-value ffh)))
  1236. (set ffh nil)
  1237. (unwind-protect
  1238. (insert-file-contents filename visit beg end replace)
  1239. (set ffh val))))
  1240. (defun mm-append-to-file (start end filename &optional codesys inhibit)
  1241. "Append the contents of the region to the end of file FILENAME.
  1242. When called from a function, expects three arguments,
  1243. START, END and FILENAME. START and END are buffer positions
  1244. saying what text to write.
  1245. Optional fourth argument specifies the coding system to use when
  1246. encoding the file.
  1247. If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
  1248. (let ((coding-system-for-write
  1249. (or codesys mm-text-coding-system-for-write
  1250. mm-text-coding-system))
  1251. (inhibit-file-name-operation (if inhibit
  1252. 'append-to-file
  1253. inhibit-file-name-operation))
  1254. (inhibit-file-name-handlers
  1255. (if inhibit
  1256. (append mm-inhibit-file-name-handlers
  1257. inhibit-file-name-handlers)
  1258. inhibit-file-name-handlers)))
  1259. (write-region start end filename t 'no-message)
  1260. (message "Appended to %s" filename)))
  1261. (defun mm-write-region (start end filename &optional append visit lockname
  1262. coding-system inhibit)
  1263. "Like `write-region'.
  1264. If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
  1265. (let ((coding-system-for-write
  1266. (or coding-system mm-text-coding-system-for-write
  1267. mm-text-coding-system))
  1268. (inhibit-file-name-operation (if inhibit
  1269. 'write-region
  1270. inhibit-file-name-operation))
  1271. (inhibit-file-name-handlers
  1272. (if inhibit
  1273. (append mm-inhibit-file-name-handlers
  1274. inhibit-file-name-handlers)
  1275. inhibit-file-name-handlers)))
  1276. (write-region start end filename append visit lockname)))
  1277. (autoload 'gmm-write-region "gmm-utils")
  1278. ;; It is not a MIME function, but some MIME functions use it.
  1279. (if (and (fboundp 'make-temp-file)
  1280. (ignore-errors
  1281. (let ((def (symbol-function 'make-temp-file)))
  1282. (and (byte-code-function-p def)
  1283. (setq def (if (fboundp 'compiled-function-arglist)
  1284. ;; XEmacs
  1285. (eval (list 'compiled-function-arglist def))
  1286. (aref def 0)))
  1287. (>= (length def) 4)
  1288. (eq (nth 3 def) 'suffix)))))
  1289. (defalias 'mm-make-temp-file 'make-temp-file)
  1290. ;; Stolen (and modified for XEmacs) from Emacs 22.
  1291. (defun mm-make-temp-file (prefix &optional dir-flag suffix)
  1292. "Create a temporary file.
  1293. The returned file name (created by appending some random characters at the end
  1294. of PREFIX, and expanding against `temporary-file-directory' if necessary),
  1295. is guaranteed to point to a newly created empty file.
  1296. You can then use `write-region' to write new data into the file.
  1297. If DIR-FLAG is non-nil, create a new empty directory instead of a file.
  1298. If SUFFIX is non-nil, add that at the end of the file name."
  1299. (let ((umask (default-file-modes))
  1300. file)
  1301. (unwind-protect
  1302. (progn
  1303. ;; Create temp files with strict access rights. It's easy to
  1304. ;; loosen them later, whereas it's impossible to close the
  1305. ;; time-window of loose permissions otherwise.
  1306. (set-default-file-modes 448)
  1307. (while (condition-case err
  1308. (progn
  1309. (setq file
  1310. (make-temp-name
  1311. (expand-file-name
  1312. prefix
  1313. (if (fboundp 'temp-directory)
  1314. ;; XEmacs
  1315. (temp-directory)
  1316. temporary-file-directory))))
  1317. (if suffix
  1318. (setq file (concat file suffix)))
  1319. (if dir-flag
  1320. (make-directory file)
  1321. ;; NOTE: This is unsafe if Emacs 20
  1322. ;; users and XEmacs users don't use
  1323. ;; a secure temp directory.
  1324. (gmm-write-region "" nil file nil 'silent
  1325. nil 'excl))
  1326. nil)
  1327. (file-already-exists t)
  1328. ;; The XEmacs version of `make-directory' issues
  1329. ;; `file-error'.
  1330. (file-error (or (and (featurep 'xemacs)
  1331. (file-exists-p file))
  1332. (signal (car err) (cdr err)))))
  1333. ;; the file was somehow created by someone else between
  1334. ;; `make-temp-name' and `write-region', let's try again.
  1335. nil)
  1336. file)
  1337. ;; Reset the umask.
  1338. (set-default-file-modes umask)))))
  1339. (defvar mm-image-load-path-cache nil)
  1340. (defun mm-image-load-path (&optional package)
  1341. (if (and mm-image-load-path-cache
  1342. (equal load-path (car mm-image-load-path-cache)))
  1343. (cdr mm-image-load-path-cache)
  1344. (let (dir result)
  1345. (dolist (path load-path)
  1346. (when (and path
  1347. (file-directory-p
  1348. (setq dir (concat (file-name-directory
  1349. (directory-file-name path))
  1350. "etc/images/" (or package "gnus/")))))
  1351. (push dir result)))
  1352. (setq result (nreverse result)
  1353. mm-image-load-path-cache (cons load-path result))
  1354. result)))
  1355. ;; Fixme: This doesn't look useful where it's used.
  1356. (if (fboundp 'detect-coding-region)
  1357. (defun mm-detect-coding-region (start end)
  1358. "Like `detect-coding-region' except returning the best one."
  1359. (let ((coding-systems
  1360. (detect-coding-region start end)))
  1361. (or (car-safe coding-systems)
  1362. coding-systems)))
  1363. (defun mm-detect-coding-region (start end)
  1364. (let ((point (point)))
  1365. (goto-char start)
  1366. (skip-chars-forward "\0-\177" end)
  1367. (prog1
  1368. (if (eq (point) end) 'ascii (mm-guess-charset))
  1369. (goto-char point)))))
  1370. (declare-function mm-detect-coding-region "mm-util" (start end))
  1371. (if (fboundp 'coding-system-get)
  1372. (defun mm-detect-mime-charset-region (start end)
  1373. "Detect MIME charset of the text in the region between START and END."
  1374. (let ((cs (mm-detect-coding-region start end)))
  1375. (or (coding-system-get cs :mime-charset)
  1376. (coding-system-get cs 'mime-charset))))
  1377. (defun mm-detect-mime-charset-region (start end)
  1378. "Detect MIME charset of the text in the region between START and END."
  1379. (let ((cs (mm-detect-coding-region start end)))
  1380. cs)))
  1381. (eval-when-compile
  1382. (unless (fboundp 'coding-system-to-mime-charset)
  1383. (defalias 'coding-system-to-mime-charset 'ignore)))
  1384. (defun mm-coding-system-to-mime-charset (coding-system)
  1385. "Return the MIME charset corresponding to CODING-SYSTEM.
  1386. To make this function work with XEmacs, the APEL package is required."
  1387. (when coding-system
  1388. (or (and (fboundp 'coding-system-get)
  1389. (or (coding-system-get coding-system :mime-charset)
  1390. (coding-system-get coding-system 'mime-charset)))
  1391. (and (featurep 'xemacs)
  1392. (or (and (fboundp 'coding-system-to-mime-charset)
  1393. (not (eq (symbol-function 'coding-system-to-mime-charset)
  1394. 'ignore)))
  1395. (and (condition-case nil
  1396. (require 'mcharset)
  1397. (error nil))
  1398. (fboundp 'coding-system-to-mime-charset)))
  1399. (coding-system-to-mime-charset coding-system)))))
  1400. (eval-when-compile
  1401. (require 'jka-compr))
  1402. (defun mm-decompress-buffer (filename &optional inplace force)
  1403. "Decompress buffer's contents, depending on jka-compr.
  1404. Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
  1405. agrees with `jka-compr-compression-info-list', decompression is done.
  1406. Signal an error if FORCE is neither nil nor t and compressed data are
  1407. not decompressed because `auto-compression-mode' is disabled.
  1408. If INPLACE is nil, return decompressed data or nil without modifying
  1409. the buffer. Otherwise, replace the buffer's contents with the
  1410. decompressed data. The buffer's multibyteness must be turned off."
  1411. (when (and filename
  1412. (if force
  1413. (prog1 t (require 'jka-compr))
  1414. (and (fboundp 'jka-compr-installed-p)
  1415. (jka-compr-installed-p))))
  1416. (let ((info (jka-compr-get-compression-info filename)))
  1417. (when info
  1418. (unless (or (memq force (list nil t))
  1419. (jka-compr-installed-p))
  1420. (error ""))
  1421. (let ((prog (jka-compr-info-uncompress-program info))
  1422. (args (jka-compr-info-uncompress-args info))
  1423. (msg (format "%s %s..."
  1424. (jka-compr-info-uncompress-message info)
  1425. filename))
  1426. (err-file (jka-compr-make-temp-name))
  1427. (cur (current-buffer))
  1428. (coding-system-for-read mm-binary-coding-system)
  1429. (coding-system-for-write mm-binary-coding-system)
  1430. retval err-msg)
  1431. (message "%s" msg)
  1432. (mm-with-unibyte-buffer
  1433. (insert-buffer-substring cur)
  1434. (condition-case err
  1435. (progn
  1436. (unless (memq (apply 'call-process-region
  1437. (point-min) (point-max)
  1438. prog t (list t err-file) nil args)
  1439. jka-compr-acceptable-retval-list)
  1440. (erase-buffer)
  1441. (insert (mapconcat 'identity
  1442. (split-string
  1443. (prog2
  1444. (insert-file-contents err-file)
  1445. (buffer-string)
  1446. (erase-buffer)) t)
  1447. " ")
  1448. "\n")
  1449. (setq err-msg
  1450. (format "Error while executing \"%s %s < %s\""
  1451. prog (mapconcat 'identity args " ")
  1452. filename)))
  1453. (setq retval (buffer-string)))
  1454. (error
  1455. (setq err-msg (error-message-string err)))))
  1456. (when (file-exists-p err-file)
  1457. (ignore-errors (delete-file err-file)))
  1458. (when inplace
  1459. (unless err-msg
  1460. (delete-region (point-min) (point-max))
  1461. (insert retval))
  1462. (setq retval nil))
  1463. (message "%s" (or err-msg (concat msg "done")))
  1464. retval)))))
  1465. (eval-when-compile
  1466. (unless (fboundp 'coding-system-name)
  1467. (defalias 'coding-system-name 'ignore))
  1468. (unless (fboundp 'find-file-coding-system-for-read-from-filename)
  1469. (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
  1470. (unless (fboundp 'find-operation-coding-system)
  1471. (defalias 'find-operation-coding-system 'ignore)))
  1472. (defun mm-find-buffer-file-coding-system (&optional filename)
  1473. "Find coding system used to decode the contents of the current buffer.
  1474. This function looks for the coding system magic cookie or examines the
  1475. coding system specified by `file-coding-system-alist' being associated
  1476. with FILENAME which defaults to `buffer-file-name'. Data compressed by
  1477. gzip, bzip2, etc. are allowed."
  1478. (unless filename
  1479. (setq filename buffer-file-name))
  1480. (save-excursion
  1481. (let ((decomp (unless ;; No worth to examine charset of tar files.
  1482. (and filename
  1483. (string-match
  1484. "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
  1485. filename))
  1486. (mm-decompress-buffer filename nil t))))
  1487. (when decomp
  1488. (set-buffer (generate-new-buffer " *temp*"))
  1489. (mm-disable-multibyte)
  1490. (insert decomp)
  1491. (setq filename (file-name-sans-extension filename)))
  1492. (goto-char (point-min))
  1493. (unwind-protect
  1494. (cond
  1495. ((boundp 'set-auto-coding-function) ;; Emacs
  1496. (if filename
  1497. (or (funcall (symbol-value 'set-auto-coding-function)
  1498. filename (- (point-max) (point-min)))
  1499. (car (find-operation-coding-system 'insert-file-contents
  1500. filename)))
  1501. (let (auto-coding-alist)
  1502. (condition-case nil
  1503. (funcall (symbol-value 'set-auto-coding-function)
  1504. nil (- (point-max) (point-min)))
  1505. (error nil)))))
  1506. ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
  1507. (let ((case-fold-search t)
  1508. (end (point-at-eol))
  1509. codesys start)
  1510. (or
  1511. (and (re-search-forward "-\\*-+[\t ]*" end t)
  1512. (progn
  1513. (setq start (match-end 0))
  1514. (re-search-forward "[\t ]*-+\\*-" end t))
  1515. (progn
  1516. (setq end (match-beginning 0))
  1517. (goto-char start)
  1518. (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
  1519. (re-search-forward
  1520. "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
  1521. end t)))
  1522. (find-coding-system (setq codesys
  1523. (intern (match-string 1))))
  1524. codesys)
  1525. (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
  1526. nil t)
  1527. (progn
  1528. (setq start (match-end 0))
  1529. (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
  1530. (progn
  1531. (setq end (match-beginning 0))
  1532. (goto-char start)
  1533. (re-search-forward
  1534. "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
  1535. end t))
  1536. (find-coding-system (setq codesys
  1537. (intern (match-string 1))))
  1538. codesys)
  1539. (and (progn
  1540. (goto-char (point-min))
  1541. (setq case-fold-search nil)
  1542. (re-search-forward "^;;;coding system: "
  1543. ;;(+ (point-min) 3000) t))
  1544. nil t))
  1545. (looking-at "[^\t\n\r ]+")
  1546. (find-coding-system
  1547. (setq codesys (intern (match-string 0))))
  1548. codesys)
  1549. (and filename
  1550. (setq codesys
  1551. (find-file-coding-system-for-read-from-filename
  1552. filename))
  1553. (coding-system-name (coding-system-base codesys)))))))
  1554. (when decomp
  1555. (kill-buffer (current-buffer)))))))
  1556. (provide 'mm-util)
  1557. ;;; mm-util.el ends here