1 ;;; mh-compat.el --- make MH-E compatibile with various versions of Emacs
3 ;; Copyright (C) 2006 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
39 ;; Please use mh-gnus.el when providing compatibility with different
42 ;; Items are listed alphabetically (except for mh-require which is
43 ;; needed by a lesser character).
48 (defalias 'mh-require
'require
))
51 (defun mh-require (feature &optional filename noerror
)
52 "If feature FEATURE is not loaded, load it from FILENAME.
53 If FEATURE is not a member of the list `features', then the feature
54 is not loaded; so load the file FILENAME.
55 If FILENAME is omitted, the printname of FEATURE is used as the file name.
56 If the optional third argument NOERROR is non-nil,
57 then return nil if the file is not found instead of signaling an error.
59 Simulate NOERROR argument in XEmacs which lacks it."
60 (if (not (featurep feature
))
62 (load filename noerror t
)
63 (load (format "%s" feature
) noerror t
)))))
65 (mh-defun-compat mh-assoc-string assoc-string
(key list case-fold
)
66 "Like `assoc' but specifically for strings.
67 Case is ignored if CASE-FOLD is non-nil.
68 This function is used by Emacs versions that lack `assoc-string',
69 introduced in Emacs 22."
71 (assoc-ignore-case key list
)
75 (defalias 'mh-cancel-timer
76 (if (fboundp 'cancel-timer
)
80 (mh-defun-compat mh-display-color-cells display-color-cells
(&optional display
)
81 "Return the number of color cells supported by DISPLAY.
82 This function is used by XEmacs to return 2 when
83 `device-color-cells' returns nil. This happens when compiling or
84 running on a tty and causes errors since `display-color-cells' is
85 expected to return an integer."
86 (or (device-color-cells display
) 2))
88 (defmacro mh-display-completion-list
(completions &optional common-substring
)
89 "Display the list of COMPLETIONS.
90 See documentation for `display-completion-list' for a description of the
91 arguments COMPLETIONS and perhaps COMMON-SUBSTRING.
92 This macro is used by Emacs versions that lack a COMMON-SUBSTRING
93 argument, introduced in Emacs 22."
94 (if (< emacs-major-version
22)
95 `(display-completion-list ,completions
)
96 `(display-completion-list ,completions
,common-substring
)))
98 (defmacro mh-face-foreground
(face &optional frame inherit
)
99 "Return the foreground color name of FACE, or nil if unspecified.
100 See documentation for `face-foreground' for a description of the
101 arguments FACE, FRAME, and perhaps INHERIT.
102 This macro is used by Emacs versions that lack an INHERIT argument,
103 introduced in Emacs 22."
104 (if (< emacs-major-version
22)
105 `(face-foreground ,face
,frame
)
106 `(face-foreground ,face
,frame
,inherit
)))
108 (defmacro mh-face-background
(face &optional frame inherit
)
109 "Return the background color name of face, or nil if unspecified.
110 See documentation for `back-foreground' for a description of the
111 arguments FACE, FRAME, and INHERIT.
112 This macro is used by Emacs versions that lack an INHERIT argument,
113 introduced in Emacs 22."
114 (if (< emacs-major-version
22)
115 `(face-background ,face
,frame
)
116 `(face-background ,face
,frame
,inherit
)))
118 (mh-defun-compat mh-image-load-path-for-library
119 image-load-path-for-library
(library image
&optional path no-error
)
120 "Return a suitable search path for images used by LIBRARY.
122 It searches for IMAGE in `image-load-path' (excluding
123 \"`data-directory'/images\") and `load-path', followed by a path
124 suitable for LIBRARY, which includes \"../../etc/images\" and
125 \"../etc/images\" relative to the library file itself, and then
126 in \"`data-directory'/images\".
128 Then this function returns a list of directories which contains
129 first the directory in which IMAGE was found, followed by the
130 value of `load-path'. If PATH is given, it is used instead of
133 If NO-ERROR is non-nil and a suitable path can't be found, don't
134 signal an error. Instead, return a list of directories as before,
135 except that nil appears in place of the image directory.
137 Here is an example that uses a common idiom to provide
138 compatibility with versions of Emacs that lack the variable
142 (defvar image-load-path)
144 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
145 (image-load-path (cons (car load-path)
146 (when (boundp 'image-load-path)
148 (mh-tool-bar-folder-buttons-init))"
149 (unless library
(error "No library specified"))
150 (unless image
(error "No image specified"))
151 (let (image-directory image-directory-load-path
)
152 ;; Check for images in image-load-path or load-path.
155 ;; Images in image-load-path.
156 (mh-image-search-load-path image
)
157 ;; Images in load-path.
158 (locate-library image
)))
160 ;; Since the image might be in a nested directory (for
161 ;; example, mail/attach.pbm), adjust `image-directory'
164 (setq dir
(file-name-directory dir
))
165 (while (setq parent
(file-name-directory img
))
166 (setq img
(directory-file-name parent
)
167 dir
(expand-file-name "../" dir
))))
168 (setq image-directory-load-path dir
))
170 ;; If `image-directory-load-path' isn't Emacs' image directory,
171 ;; it's probably a user preference, so use it. Then use a
172 ;; relative setting if possible; otherwise, use
173 ;; `image-directory-load-path'.
175 ;; User-modified image-load-path?
176 ((and image-directory-load-path
177 (not (equal image-directory-load-path
178 (file-name-as-directory
179 (expand-file-name "images" data-directory
)))))
180 (setq image-directory image-directory-load-path
))
181 ;; Try relative setting.
182 ((let (library-name d1ei d2ei
)
183 ;; First, find library in the load-path.
184 (setq library-name
(locate-library library
))
185 (if (not library-name
)
186 (error "Cannot find library %s in load-path" library
))
187 ;; And then set image-directory relative to that.
190 d2ei
(file-name-as-directory
192 (concat (file-name-directory library-name
) "../../etc/images")))
194 d1ei
(file-name-as-directory
196 (concat (file-name-directory library-name
) "../etc/images"))))
197 (setq image-directory
198 ;; Set it to nil if image is not found.
199 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
200 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
201 ;; Use Emacs' image directory.
202 (image-directory-load-path
203 (setq image-directory image-directory-load-path
))
205 (message "Could not find image %s for library %s" image library
))
207 (error "Could not find image %s for library %s" image library
)))
209 ;; Return an augmented `path' or `load-path'.
210 (nconc (list image-directory
)
211 (delete image-directory
(copy-sequence (or path load-path
))))))
213 (mh-defun-compat mh-image-search-load-path
214 image-search-load-path
(file &optional path
)
215 "Emacs 21 and XEmacs don't have `image-search-load-path'.
216 This function returns nil on those systems."
220 (defalias 'mh-line-beginning-position
221 (if (fboundp 'line-beginning-position
)
222 'line-beginning-position
226 (defalias 'mh-line-end-position
227 (if (fboundp 'line-end-position
)
231 (mh-require 'mailabbrev nil t
)
232 (mh-defun-compat mh-mail-abbrev-make-syntax-table
233 mail-abbrev-make-syntax-table
()
234 "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
235 This function returns nil on those systems."
238 (mh-defun-compat mh-match-string-no-properties
239 match-string-no-properties
(num &optional string
)
240 "Return string of text matched by last search, without text properties.
241 This function is used by XEmacs that lacks `match-string-no-properties'.
242 The function `buffer-substring-no-properties' is used instead.
243 The argument STRING is ignored."
244 (buffer-substring-no-properties
245 (match-beginning num
) (match-end num
)))
247 (mh-defun-compat mh-replace-regexp-in-string replace-regexp-in-string
248 (regexp rep string
&optional fixedcase literal subexp start
)
249 "Replace REGEXP with REP everywhere in STRING and return result.
250 This function is used by XEmacs that lacks `replace-regexp-in-string'.
251 The function `replace-in-string' is used instead.
252 The arguments FIXEDCASE, SUBEXP, and START, used by
253 `replace-in-string' are ignored."
254 (replace-in-string string regexp rep literal
))
256 ;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
257 (if (not (boundp 'url-unreserved-chars
))
258 (defconst mh-url-unreserved-chars
260 ?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
261 ?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
262 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
263 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
264 "A list of characters that are _NOT_ reserved in the URL spec.
265 This is taken from RFC 2396."))
267 (mh-defun-compat mh-url-hexify-string url-hexify-string
(str)
268 "Escape characters in a string.
269 This is a copy of `url-hexify-string' from url-util.el in Emacs
270 22; needed by Emacs 21."
273 ;; Fixme: use a char table instead.
274 (if (not (memq char mh-url-unreserved-chars
))
276 (error "Hexifying multibyte character %s" str
)
277 (format "%%%02X" char
))
278 (char-to-string char
)))
281 (mh-defun-compat mh-view-mode-enter
282 view-mode-enter
(&optional return-to exit-action
)
284 This function is used by XEmacs that lacks `view-mode-enter'.
285 The function `view-mode' is used instead.
286 The arguments RETURN-TO and EXIT-ACTION are ignored."
292 (defmacro mh-write-file-functions
()
293 "Return `write-file-functions' if it exists.
294 Otherwise return `local-write-file-hooks'.
295 This macro exists purely for compatibility. The former symbol is used
296 in Emacs 22 onward while the latter is used in previous versions and
298 (if (boundp 'write-file-functions
)
299 ''write-file-functions
;Emacs 22 on
300 ''local-write-file-hooks
)) ;XEmacs
305 ;; no-byte-compile: t
306 ;; indent-tabs-mode: nil
307 ;; sentence-end-double-space: nil
310 ;; arch-tag: 577b0eab-a5cd-45e1-8d9f-c1a426f4d73c
311 ;;; mh-compat.el ends here