mh-compat.el (mh-image-search-load-path): Compatibility code. Emacs 21 and
[bpt/emacs.git] / lisp / mh-e / mh-compat.el
CommitLineData
dda00b2c
BW
1;;; mh-compat.el --- make MH-E compatibile with various versions of Emacs
2
3;; Copyright (C) 2006 Free Software Foundation, Inc.
4
5;; Author: Bill Wohler <wohler@newt.com>
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
26
27;;; Commentary:
28
29;;; Change Log:
30
31;;; Code:
32
33;; This is a good place to gather code that is used for compatibility
34;; between different versions of Emacs. Please document which versions
35;; of Emacs that the defsubst, defalias, or defmacro applies. That
36;; way, it's easy to occasionally go through this file and see which
37;; macros we can retire.
38
06e7028b 39;; Please use mh-gnus.el when providing compatibility with different
d5dc8c56 40;; versions of Gnus.
dda00b2c 41
d5dc8c56
BW
42;; Items are listed alphabetically (except for mh-require which is
43;; needed by a lesser character).
44
45(require 'mh-acros)
46
47(mh-do-in-gnu-emacs
48 (defalias 'mh-require 'require))
49
50(mh-do-in-xemacs
51 (defun mh-require (feature &optional filename noerror)
52 "If feature FEATURE is not loaded, load it from FILENAME.
53If FEATURE is not a member of the list `features', then the feature
54is not loaded; so load the file FILENAME.
55If FILENAME is omitted, the printname of FEATURE is used as the file name.
56If the optional third argument NOERROR is non-nil,
57then return nil if the file is not found instead of signaling an error.
58
59Simulate NOERROR argument in XEmacs which lacks it."
60 (if (not (featurep feature))
61 (if filename
62 (load filename noerror t)
63 (load (format "%s" feature) noerror t)))))
dda00b2c 64
06e7028b
BW
65(mh-defun-compat mh-assoc-string assoc-string (key list case-fold)
66 "Like `assoc' but specifically for strings.
dda00b2c 67Case is ignored if CASE-FOLD is non-nil.
d5dc8c56
BW
68This function is used by Emacs versions that lack `assoc-string',
69introduced in Emacs 22."
06e7028b
BW
70 (if case-fold
71 (assoc-ignore-case key list)
72 (assoc key list)))
73
d5dc8c56
BW
74;; For XEmacs.
75(defalias 'mh-cancel-timer
76 (if (fboundp 'cancel-timer)
77 'cancel-timer
78 'delete-itimer))
79
80(mh-defun-compat mh-display-color-cells display-color-cells (&optional display)
81 "Return the number of color cells supported by DISPLAY.
82This function is used by XEmacs to always return 0 when compiling
83to avoid compiling errors. Otherwise uses `device-color-cells'."
84 (if mh-compiling-flag
85 0
86 (device-color-cells display)))
dda00b2c
BW
87
88(defmacro mh-display-completion-list (completions &optional common-substring)
89 "Display the list of COMPLETIONS.
06e7028b
BW
90See documentation for `display-completion-list' for a description of the
91arguments COMPLETIONS and perhaps COMMON-SUBSTRING.
d5dc8c56
BW
92This macro is used by Emacs versions that lack a COMMON-SUBSTRING
93argument, introduced in Emacs 22."
dda00b2c
BW
94 (if (< emacs-major-version 22)
95 `(display-completion-list ,completions)
96 `(display-completion-list ,completions ,common-substring)))
97
06e7028b
BW
98(defmacro mh-face-foreground (face &optional frame inherit)
99 "Return the foreground color name of FACE, or nil if unspecified.
100See documentation for `face-foreground' for a description of the
101arguments FACE, FRAME, and perhaps INHERIT.
d5dc8c56
BW
102This macro is used by Emacs versions that lack an INHERIT argument,
103introduced in Emacs 22."
06e7028b
BW
104 (if (< emacs-major-version 22)
105 `(face-foreground ,face ,frame)
106 `(face-foreground ,face ,frame ,inherit)))
107
108(defmacro mh-face-background (face &optional frame inherit)
109 "Return the background color name of face, or nil if unspecified.
110See documentation for `back-foreground' for a description of the
111arguments FACE, FRAME, and INHERIT.
d5dc8c56
BW
112This macro is used by Emacs versions that lack an INHERIT argument,
113introduced in Emacs 22."
06e7028b
BW
114 (if (< emacs-major-version 22)
115 `(face-background ,face ,frame)
116 `(face-background ,face ,frame ,inherit)))
117
fc0f755f
PG
118(mh-defun-compat mh-image-search-load-path
119 image-search-load-path (file &optional path)
120 "Emacs 21 and XEmacs don't have `image-search-load-path'.
121This function returns nil on those systems."
122 nil)
123
d5dc8c56
BW
124;; For XEmacs.
125(defalias 'mh-line-beginning-position
126 (if (fboundp 'line-beginning-position)
127 'line-beginning-position
128 'point-at-bol))
129
130;; For XEmacs.
131(defalias 'mh-line-end-position
132 (if (fboundp 'line-end-position)
133 'line-end-position
134 'point-at-eol))
135
136(mh-require 'mailabbrev nil t)
137(mh-defun-compat mh-mail-abbrev-make-syntax-table
138 mail-abbrev-make-syntax-table ()
139 "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
fc0f755f 140This function returns nil on those systems."
d5dc8c56
BW
141 nil)
142
143(mh-defun-compat mh-match-string-no-properties
144 match-string-no-properties (num &optional string)
145 "Return string of text matched by last search, without text properties.
146This function is used by XEmacs that lacks `match-string-no-properties'.
147The function `buffer-substring-no-properties' is used instead.
148The argument STRING is ignored."
149 (buffer-substring-no-properties
150 (match-beginning num) (match-end num)))
151
152(mh-defun-compat mh-replace-regexp-in-string replace-regexp-in-string
613cd8c5 153 (regexp rep string &optional fixedcase literal subexp start)
d5dc8c56
BW
154 "Replace REGEXP with REP everywhere in STRING and return result.
155This function is used by XEmacs that lacks `replace-regexp-in-string'.
156The function `replace-in-string' is used instead.
157The arguments FIXEDCASE, LITERAL, SUBEXP, and START are ignored."
158 (replace-in-string string regexp rep))
159
06e7028b
BW
160;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
161(if (not (boundp 'url-unreserved-chars))
e6e91eda 162 (defconst mh-url-unreserved-chars
06e7028b
BW
163 '(
164 ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
165 ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z
166 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
167 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
168 "A list of characters that are _NOT_ reserved in the URL spec.
169This is taken from RFC 2396."))
170
171(mh-defun-compat mh-url-hexify-string url-hexify-string (str)
172 "Escape characters in a string.
173This is a copy of `url-hexify-string' from url-util.el in Emacs
17422; needed by Emacs 21."
175 (mapconcat
176 (lambda (char)
177 ;; Fixme: use a char table instead.
178 (if (not (memq char mh-url-unreserved-chars))
179 (if (> char 255)
180 (error "Hexifying multibyte character %s" str)
181 (format "%%%02X" char))
182 (char-to-string char)))
183 str ""))
184
d5dc8c56
BW
185(mh-defun-compat mh-view-mode-enter
186 view-mode-enter (&optional return-to exit-action)
187 "Enter View mode.
188This function is used by XEmacs that lacks `view-mode-enter'.
189The function `view-mode' is used instead.
190The arguments RETURN-TO and EXIT-ACTION are ignored."
191 ;; Shush compiler.
192 (if return-to nil)
193 (if exit-action nil)
194 (view-mode 1))
195
06e7028b
BW
196(defmacro mh-write-file-functions ()
197 "Return `write-file-functions' if it exists.
198Otherwise return `local-write-file-hooks'.
199This macro exists purely for compatibility. The former symbol is used
200in Emacs 22 onward while the latter is used in previous versions and
201XEmacs."
202 (if (boundp 'write-file-functions)
203 ''write-file-functions ;Emacs 22 on
204 ''local-write-file-hooks)) ;XEmacs
205
dda00b2c
BW
206(provide 'mh-compat)
207
208;; Local Variables:
209;; no-byte-compile: t
210;; indent-tabs-mode: nil
211;; sentence-end-double-space: nil
212;; End:
213
a1ab640d 214;; arch-tag: 577b0eab-a5cd-45e1-8d9f-c1a426f4d73c
dda00b2c 215;;; mh-compat.el ends here