1 ;;; mh-compat.el --- make MH-E compatibile with various versions of Emacs
3 ;; Copyright (C) 2006, 2007 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 3, 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 sooner it would normally appear).
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 (defun-mh 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 (defun mh-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 `device-color-cells'
83 or `display-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 (cond ((fboundp 'display-color-cells
) ; GNU Emacs, XEmacs 21.5b28
87 (or (display-color-cells display
) 2))
88 ((fboundp 'device-color-cells
) ; XEmacs 21.4
89 (or (device-color-cells display
) 2))
92 (defmacro mh-display-completion-list
(completions &optional common-substring
)
93 "Display the list of COMPLETIONS.
94 See documentation for `display-completion-list' for a description of the
95 arguments COMPLETIONS and perhaps COMMON-SUBSTRING.
96 This macro is used by Emacs versions that lack a COMMON-SUBSTRING
97 argument, introduced in Emacs 22."
98 (if (< emacs-major-version
22)
99 `(display-completion-list ,completions
)
100 `(display-completion-list ,completions
,common-substring
)))
102 (defmacro mh-face-foreground
(face &optional frame inherit
)
103 "Return the foreground color name of FACE, or nil if unspecified.
104 See documentation for `face-foreground' for a description of the
105 arguments FACE, FRAME, and perhaps INHERIT.
106 This macro is used by Emacs versions that lack an INHERIT argument,
107 introduced in Emacs 22."
108 (if (< emacs-major-version
22)
109 `(face-foreground ,face
,frame
)
110 `(face-foreground ,face
,frame
,inherit
)))
112 (defmacro mh-face-background
(face &optional frame inherit
)
113 "Return the background color name of face, or nil if unspecified.
114 See documentation for `back-foreground' for a description of the
115 arguments FACE, FRAME, and INHERIT.
116 This macro is used by Emacs versions that lack an INHERIT argument,
117 introduced in Emacs 22."
118 (if (< emacs-major-version
22)
119 `(face-background ,face
,frame
)
120 `(face-background ,face
,frame
,inherit
)))
122 (defun-mh mh-font-lock-add-keywords font-lock-add-keywords
123 (mode keywords
&optional how
)
124 "XEmacs does not have `font-lock-add-keywords'.
125 This function returns nil on that system.")
127 (defun-mh mh-image-load-path-for-library
128 image-load-path-for-library
(library image
&optional path no-error
)
129 "Return a suitable search path for images used by LIBRARY.
131 It searches for IMAGE in `image-load-path' (excluding
132 \"`data-directory'/images\") and `load-path', followed by a path
133 suitable for LIBRARY, which includes \"../../etc/images\" and
134 \"../etc/images\" relative to the library file itself, and then
135 in \"`data-directory'/images\".
137 Then this function returns a list of directories which contains
138 first the directory in which IMAGE was found, followed by the
139 value of `load-path'. If PATH is given, it is used instead of
142 If NO-ERROR is non-nil and a suitable path can't be found, don't
143 signal an error. Instead, return a list of directories as before,
144 except that nil appears in place of the image directory.
146 Here is an example that uses a common idiom to provide
147 compatibility with versions of Emacs that lack the variable
151 (defvar image-load-path)
153 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
154 (image-load-path (cons (car load-path)
155 (when (boundp 'image-load-path)
157 (mh-tool-bar-folder-buttons-init))"
158 (unless library
(error "No library specified"))
159 (unless image
(error "No image specified"))
160 (let (image-directory image-directory-load-path
)
161 ;; Check for images in image-load-path or load-path.
164 ;; Images in image-load-path.
165 (mh-image-search-load-path image
)
166 ;; Images in load-path.
167 (locate-library image
)))
169 ;; Since the image might be in a nested directory (for
170 ;; example, mail/attach.pbm), adjust `image-directory'
173 (setq dir
(file-name-directory dir
))
174 (while (setq parent
(file-name-directory img
))
175 (setq img
(directory-file-name parent
)
176 dir
(expand-file-name "../" dir
))))
177 (setq image-directory-load-path dir
))
179 ;; If `image-directory-load-path' isn't Emacs' image directory,
180 ;; it's probably a user preference, so use it. Then use a
181 ;; relative setting if possible; otherwise, use
182 ;; `image-directory-load-path'.
184 ;; User-modified image-load-path?
185 ((and image-directory-load-path
186 (not (equal image-directory-load-path
187 (file-name-as-directory
188 (expand-file-name "images" data-directory
)))))
189 (setq image-directory image-directory-load-path
))
190 ;; Try relative setting.
191 ((let (library-name d1ei d2ei
)
192 ;; First, find library in the load-path.
193 (setq library-name
(locate-library library
))
194 (if (not library-name
)
195 (error "Cannot find library %s in load-path" library
))
196 ;; And then set image-directory relative to that.
199 d2ei
(file-name-as-directory
201 (concat (file-name-directory library-name
) "../../etc/images")))
203 d1ei
(file-name-as-directory
205 (concat (file-name-directory library-name
) "../etc/images"))))
206 (setq image-directory
207 ;; Set it to nil if image is not found.
208 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
209 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
210 ;; Use Emacs' image directory.
211 (image-directory-load-path
212 (setq image-directory image-directory-load-path
))
214 (message "Could not find image %s for library %s" image library
))
216 (error "Could not find image %s for library %s" image library
)))
218 ;; Return an augmented `path' or `load-path'.
219 (nconc (list image-directory
)
220 (delete image-directory
(copy-sequence (or path load-path
))))))
222 (defun-mh mh-image-search-load-path
223 image-search-load-path
(file &optional path
)
224 "Emacs 21 and XEmacs don't have `image-search-load-path'.
225 This function returns nil on those systems."
229 (defalias 'mh-line-beginning-position
230 (if (fboundp 'line-beginning-position
)
231 'line-beginning-position
235 (defalias 'mh-line-end-position
236 (if (fboundp 'line-end-position
)
240 (mh-require 'mailabbrev nil t
)
241 (defun-mh mh-mail-abbrev-make-syntax-table
242 mail-abbrev-make-syntax-table
()
243 "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
244 This function returns nil on those systems."
247 (defun-mh mh-match-string-no-properties
248 match-string-no-properties
(num &optional string
)
249 "Return string of text matched by last search, without text properties.
250 This function is used by XEmacs that lacks `match-string-no-properties'.
251 The function `buffer-substring-no-properties' is used instead.
252 The argument STRING is ignored."
253 (buffer-substring-no-properties
254 (match-beginning num
) (match-end num
)))
256 (defun-mh mh-replace-regexp-in-string replace-regexp-in-string
257 (regexp rep string
&optional fixedcase literal subexp start
)
258 "Replace REGEXP with REP everywhere in STRING and return result.
259 This function is used by XEmacs that lacks `replace-regexp-in-string'.
260 The function `replace-in-string' is used instead.
261 The arguments FIXEDCASE, SUBEXP, and START, used by
262 `replace-in-string' are ignored."
263 (replace-in-string string regexp rep literal
))
265 ;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
266 (if (not (boundp 'url-unreserved-chars
))
267 (defconst mh-url-unreserved-chars
269 ?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
270 ?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
271 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
272 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
273 "A list of characters that are _NOT_ reserved in the URL spec.
274 This is taken from RFC 2396."))
276 (defun-mh mh-url-hexify-string url-hexify-string
(str)
277 "Escape characters in a string.
278 This is a copy of `url-hexify-string' from url-util.el in Emacs
279 22; needed by Emacs 21."
282 ;; Fixme: use a char table instead.
283 (if (not (memq char mh-url-unreserved-chars
))
285 (error "Hexifying multibyte character %s" str
)
286 (format "%%%02X" char
))
287 (char-to-string char
)))
290 (defun-mh mh-view-mode-enter
291 view-mode-enter
(&optional return-to exit-action
)
293 This function is used by XEmacs that lacks `view-mode-enter'.
294 The function `view-mode' is used instead.
295 The arguments RETURN-TO and EXIT-ACTION are ignored."
301 (defmacro mh-write-file-functions
()
302 "Return `write-file-functions' if it exists.
303 Otherwise return `local-write-file-hooks'.
304 This macro exists purely for compatibility. The former symbol is used
305 in Emacs 22 onward while the latter is used in previous versions and
307 (if (boundp 'write-file-functions
)
308 ''write-file-functions
;Emacs 22 on
309 ''local-write-file-hooks
)) ;XEmacs
314 ;; no-byte-compile: t
315 ;; indent-tabs-mode: nil
316 ;; sentence-end-double-space: nil
319 ;; arch-tag: 577b0eab-a5cd-45e1-8d9f-c1a426f4d73c
320 ;;; mh-compat.el ends here