comparison lisp/gnus/mm-util.el @ 100993:a16e9f7c2536

Merge from gnus--devo--0 Revision: emacs@sv.gnu.org/emacs--devo--0--patch-1513
author Miles Bader <miles@gnu.org>
date Fri, 09 Jan 2009 03:01:50 +0000
parents a9dc0e7c3f2b
children a22ed49b5bef
comparison
equal deleted inserted replaced
100992:5cb6d276b93a 100993:a16e9f7c2536
38 (require 'timer)) 38 (require 'timer))
39 (require 'timer))) 39 (require 'timer)))
40 40
41 (defvar mm-mime-mule-charset-alist ) 41 (defvar mm-mime-mule-charset-alist )
42 42
43 ;; Emulate functions that are not available in every (X)Emacs version.
44 ;; The name of a function is prefixed with mm-, like `mm-char-int' for
45 ;; `char-int' that is a native XEmacs function, not available in Emacs.
46 ;; Gnus programs all should use mm- functions, not the original ones.
43 (eval-and-compile 47 (eval-and-compile
44 (mapc 48 (mapc
45 (lambda (elem) 49 (lambda (elem)
46 (let ((nfunc (intern (format "mm-%s" (car elem))))) 50 (let ((nfunc (intern (format "mm-%s" (car elem)))))
47 (if (fboundp (car elem)) 51 (if (fboundp (car elem))
48 (defalias nfunc (car elem)) 52 (defalias nfunc (car elem))
49 (defalias nfunc (cdr elem))))) 53 (defalias nfunc (cdr elem)))))
50 `((coding-system-list . ignore) 54 `(;; `coding-system-list' is not available in XEmacs 21.4 built
55 ;; without the `file-coding' feature.
56 (coding-system-list . ignore)
57 ;; `char-int' is an XEmacs function, not available in Emacs.
51 (char-int . identity) 58 (char-int . identity)
59 ;; `coding-system-equal' is an Emacs function, not available in XEmacs.
52 (coding-system-equal . equal) 60 (coding-system-equal . equal)
61 ;; `annotationp' is an XEmacs function, not available in Emacs.
53 (annotationp . ignore) 62 (annotationp . ignore)
63 ;; `set-buffer-file-coding-system' is not available in XEmacs 21.4
64 ;; built without the `file-coding' feature.
54 (set-buffer-file-coding-system . ignore) 65 (set-buffer-file-coding-system . ignore)
66 ;; `read-charset' is an Emacs function, not available in XEmacs.
55 (read-charset 67 (read-charset
56 . ,(lambda (prompt) 68 . ,(lambda (prompt)
57 "Return a charset." 69 "Return a charset."
58 (intern 70 (intern
59 (completing-read 71 (completing-read
60 prompt 72 prompt
61 (mapcar (lambda (e) (list (symbol-name (car e)))) 73 (mapcar (lambda (e) (list (symbol-name (car e))))
62 mm-mime-mule-charset-alist) 74 mm-mime-mule-charset-alist)
63 nil t)))) 75 nil t))))
76 ;; `subst-char-in-string' is not available in XEmacs 21.4.
64 (subst-char-in-string 77 (subst-char-in-string
65 . ,(lambda (from to string &optional inplace) 78 . ,(lambda (from to string &optional inplace)
66 ;; stolen (and renamed) from nnheader.el 79 ;; stolen (and renamed) from nnheader.el
67 "Replace characters in STRING from FROM to TO. 80 "Replace characters in STRING from FROM to TO.
68 Unless optional argument INPLACE is non-nil, return a new string." 81 Unless optional argument INPLACE is non-nil, return a new string."
73 (while (< idx len) 86 (while (< idx len)
74 (when (= (aref string idx) from) 87 (when (= (aref string idx) from)
75 (aset string idx to)) 88 (aset string idx to))
76 (setq idx (1+ idx))) 89 (setq idx (1+ idx)))
77 string))) 90 string)))
91 ;; `replace-in-string' is an XEmacs function, not available in Emacs.
78 (replace-in-string 92 (replace-in-string
79 . ,(lambda (string regexp rep &optional literal) 93 . ,(lambda (string regexp rep &optional literal)
80 "See `replace-regexp-in-string', only the order of args differs." 94 "See `replace-regexp-in-string', only the order of args differs."
81 (replace-regexp-in-string regexp rep string nil literal))) 95 (replace-regexp-in-string regexp rep string nil literal)))
96 ;; `string-as-unibyte' is an Emacs function, not available in XEmacs.
82 (string-as-unibyte . identity) 97 (string-as-unibyte . identity)
98 ;; `string-make-unibyte' is an Emacs function, not available in XEmacs.
83 (string-make-unibyte . identity) 99 (string-make-unibyte . identity)
84 ;; string-as-multibyte often doesn't really do what you think it does. 100 ;; string-as-multibyte often doesn't really do what you think it does.
85 ;; Example: 101 ;; Example:
86 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201) 102 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
87 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300) 103 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
97 ;; generally a problem in itself. 113 ;; generally a problem in itself.
98 ;; Here is an approximate equivalence table to help think about it: 114 ;; Here is an approximate equivalence table to help think about it:
99 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule) 115 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
100 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary) 116 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
101 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system) 117 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
118 ;; `string-as-multibyte' is an Emacs function, not available in XEmacs.
102 (string-as-multibyte . identity) 119 (string-as-multibyte . identity)
120 ;; `multibyte-string-p' is an Emacs function, not available in XEmacs.
103 (multibyte-string-p . ignore) 121 (multibyte-string-p . ignore)
122 ;; `insert-byte' is available only in Emacs 23.1 or greater.
104 (insert-byte . insert-char) 123 (insert-byte . insert-char)
124 ;; `multibyte-char-to-unibyte' is an Emacs function, not available
125 ;; in XEmacs.
105 (multibyte-char-to-unibyte . identity) 126 (multibyte-char-to-unibyte . identity)
127 ;; `set-buffer-multibyte' is an Emacs function, not available in XEmacs.
106 (set-buffer-multibyte . ignore) 128 (set-buffer-multibyte . ignore)
129 ;; `special-display-p' is an Emacs function, not available in XEmacs.
107 (special-display-p 130 (special-display-p
108 . ,(lambda (buffer-name) 131 . ,(lambda (buffer-name)
109 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame." 132 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
110 (and special-display-function 133 (and special-display-function
111 (or (and (member buffer-name special-display-buffer-names) t) 134 (or (and (member buffer-name special-display-buffer-names) t)
117 (throw 'return t)) 140 (throw 'return t))
118 (and (consp elem) 141 (and (consp elem)
119 (stringp (car elem)) 142 (stringp (car elem))
120 (string-match (car elem) buffer-name) 143 (string-match (car elem) buffer-name)
121 (throw 'return (cdr elem))))))))) 144 (throw 'return (cdr elem)))))))))
145 ;; `substring-no-properties' is available only in Emacs 22.1 or greater.
122 (substring-no-properties 146 (substring-no-properties
123 . ,(lambda (string &optional from to) 147 . ,(lambda (string &optional from to)
124 "Return a substring of STRING, without text properties. 148 "Return a substring of STRING, without text properties.
125 It starts at index FROM and ending before TO. 149 It starts at index FROM and ending before TO.
126 TO may be nil or omitted; then the substring runs to the end of STRING. 150 TO may be nil or omitted; then the substring runs to the end of STRING.
128 If FROM or TO is negative, it counts from the end. 152 If FROM or TO is negative, it counts from the end.
129 153
130 With one argument, just copy STRING without its properties." 154 With one argument, just copy STRING without its properties."
131 (setq string (substring string (or from 0) to)) 155 (setq string (substring string (or from 0) to))
132 (set-text-properties 0 (length string) nil string) 156 (set-text-properties 0 (length string) nil string)
133 string))))) 157 string))
134 158 ;; `line-number-at-pos' is available only in Emacs 22.1 or greater
159 ;; and XEmacs 21.5.
160 (line-number-at-pos
161 . ,(lambda (&optional pos)
162 "Return (narrowed) buffer line number at position POS.
163 If POS is nil, use current buffer location.
164 Counting starts at (point-min), so the value refers
165 to the contents of the accessible portion of the buffer."
166 (let ((opoint (or pos (point))) start)
167 (save-excursion
168 (goto-char (point-min))
169 (setq start (point))
170 (goto-char opoint)
171 (forward-line 0)
172 (1+ (count-lines start (point))))))))))
173
174 ;; `decode-coding-string', `encode-coding-string', `decode-coding-region'
175 ;; and `encode-coding-region' are available in Emacs and XEmacs built with
176 ;; the `file-coding' feature, but the XEmacs versions treat nil, that is
177 ;; given as the `coding-system' argument, as the `binary' coding system.
135 (eval-and-compile 178 (eval-and-compile
136 (if (featurep 'xemacs) 179 (if (featurep 'xemacs)
137 (if (featurep 'file-coding) 180 (if (featurep 'file-coding)
138 ;; Don't modify string if CODING-SYSTEM is nil.
139 (progn 181 (progn
140 (defun mm-decode-coding-string (str coding-system) 182 (defun mm-decode-coding-string (str coding-system)
141 (if coding-system 183 (if coding-system
142 (decode-coding-string str coding-system) 184 (decode-coding-string str coding-system)
143 str)) 185 str))
158 (defalias 'mm-decode-coding-string 'decode-coding-string) 200 (defalias 'mm-decode-coding-string 'decode-coding-string)
159 (defalias 'mm-encode-coding-string 'encode-coding-string) 201 (defalias 'mm-encode-coding-string 'encode-coding-string)
160 (defalias 'mm-decode-coding-region 'decode-coding-region) 202 (defalias 'mm-decode-coding-region 'decode-coding-region)
161 (defalias 'mm-encode-coding-region 'encode-coding-region))) 203 (defalias 'mm-encode-coding-region 'encode-coding-region)))
162 204
205 ;; `string-to-multibyte' is available only in Emacs 22.1 or greater.
163 (defalias 'mm-string-to-multibyte 206 (defalias 'mm-string-to-multibyte
164 (cond 207 (cond
165 ((featurep 'xemacs) 208 ((featurep 'xemacs)
166 'identity) 209 'identity)
167 ((fboundp 'string-to-multibyte) 210 ((fboundp 'string-to-multibyte)
171 "Return a multibyte string with the same individual chars as STRING." 214 "Return a multibyte string with the same individual chars as STRING."
172 (mapconcat 215 (mapconcat
173 (lambda (ch) (mm-string-as-multibyte (char-to-string ch))) 216 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
174 string ""))))) 217 string "")))))
175 218
219 ;; `char-or-char-int-p' is an XEmacs function, not available in Emacs.
176 (eval-and-compile 220 (eval-and-compile
177 (defalias 'mm-char-or-char-int-p 221 (defalias 'mm-char-or-char-int-p
178 (cond 222 (cond
179 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p) 223 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
180 ((fboundp 'char-valid-p) 'char-valid-p) 224 ((fboundp 'char-valid-p) 'char-valid-p)
181 (t 'identity)))) 225 (t 'identity))))
226
227 ;; `ucs-to-char' is a function that Mule-UCS provides.
228 (if (featurep 'xemacs)
229 (cond ((and (fboundp 'unicode-to-char) ;; XEmacs 21.5.
230 (subrp (symbol-function 'unicode-to-char)))
231 (if (featurep 'mule)
232 (defalias 'mm-ucs-to-char 'unicode-to-char)
233 (defun mm-ucs-to-char (codepoint)
234 "Convert Unicode codepoint to character."
235 (or (unicode-to-char codepoint) ?#))))
236 ((featurep 'mule)
237 (defun mm-ucs-to-char (codepoint)
238 "Convert Unicode codepoint to character."
239 (if (fboundp 'ucs-to-char) ;; Mule-UCS is loaded.
240 (progn
241 (defalias 'mm-ucs-to-char
242 (lambda (codepoint)
243 "Convert Unicode codepoint to character."
244 (condition-case nil
245 (or (ucs-to-char codepoint) ?#)
246 (error ?#))))
247 (mm-ucs-to-char codepoint))
248 (condition-case nil
249 (or (int-to-char codepoint) ?#)
250 (error ?#)))))
251 (t
252 (defun mm-ucs-to-char (codepoint)
253 "Convert Unicode codepoint to character."
254 (condition-case nil
255 (or (int-to-char codepoint) ?#)
256 (error ?#)))))
257 (if (let ((char (make-char 'japanese-jisx0208 36 34)))
258 (eq char (decode-char 'ucs char)))
259 ;; Emacs 23.
260 (defalias 'mm-ucs-to-char 'identity)
261 (defun mm-ucs-to-char (codepoint)
262 "Convert Unicode codepoint to character."
263 (or (decode-char 'ucs codepoint) ?#))))
182 264
183 ;; Fixme: This seems always to be used to read a MIME charset, so it 265 ;; Fixme: This seems always to be used to read a MIME charset, so it
184 ;; should be re-named and fixed (in Emacs) to offer completion only on 266 ;; should be re-named and fixed (in Emacs) to offer completion only on
185 ;; proper charset names (base coding systems which have a 267 ;; proper charset names (base coding systems which have a
186 ;; mime-charset defined). XEmacs doesn't believe in mime-charset; 268 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;