(picture-mode-exit): Run picture-mode-exit-hook.
[emacs.git] / lisp / mh-e / mh-compat.el
blob256a8cfe8313aec9a6e046e501d43a13d3a6ee1b
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>
7 ;; Keywords: mail
8 ;; See: mh-e.el
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;;; Change Log:
31 ;;; Code:
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
40 ;; versions of Gnus.
42 ;; Items are listed alphabetically (except for mh-require which is
43 ;; needed by a lesser character).
45 (require 'mh-acros)
47 (mh-do-in-gnu-emacs
48 (defalias 'mh-require 'require))
50 (mh-do-in-xemacs
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))
61 (if filename
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."
70 (if case-fold
71 (assoc-ignore-case key list)
72 (assoc key list)))
74 ;; For XEmacs.
75 (defalias 'mh-cancel-timer
76 (if (fboundp 'cancel-timer)
77 'cancel-timer
78 'delete-itimer))
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
131 `load-path'.
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
139 `image-load-path':
141 ;; Shush compiler.
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)
147 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.
153 (let ((img image)
154 (dir (or
155 ;; Images in image-load-path.
156 (mh-image-search-load-path image)
157 ;; Images in load-path.
158 (locate-library image)))
159 parent)
160 ;; Since the image might be in a nested directory (for
161 ;; example, mail/attach.pbm), adjust `image-directory'
162 ;; accordingly.
163 (when dir
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'.
174 (cond
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.
188 (setq
189 ;; Go down 2 levels.
190 d2ei (file-name-as-directory
191 (expand-file-name
192 (concat (file-name-directory library-name) "../../etc/images")))
193 ;; Go down 1 level.
194 d1ei (file-name-as-directory
195 (expand-file-name
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))
204 (no-error
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."
217 nil)
219 ;; For XEmacs.
220 (defalias 'mh-line-beginning-position
221 (if (fboundp 'line-beginning-position)
222 'line-beginning-position
223 'point-at-bol))
225 ;; For XEmacs.
226 (defalias 'mh-line-end-position
227 (if (fboundp 'line-end-position)
228 'line-end-position
229 'point-at-eol))
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."
236 nil)
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."
271 (mapconcat
272 (lambda (char)
273 ;; Fixme: use a char table instead.
274 (if (not (memq char mh-url-unreserved-chars))
275 (if (> char 255)
276 (error "Hexifying multibyte character %s" str)
277 (format "%%%02X" char))
278 (char-to-string char)))
279 str ""))
281 (mh-defun-compat mh-view-mode-enter
282 view-mode-enter (&optional return-to exit-action)
283 "Enter View mode.
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."
287 ;; Shush compiler.
288 (if return-to nil)
289 (if exit-action nil)
290 (view-mode 1))
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
297 XEmacs."
298 (if (boundp 'write-file-functions)
299 ''write-file-functions ;Emacs 22 on
300 ''local-write-file-hooks)) ;XEmacs
302 (provide 'mh-compat)
304 ;; Local Variables:
305 ;; no-byte-compile: t
306 ;; indent-tabs-mode: nil
307 ;; sentence-end-double-space: nil
308 ;; End:
310 ;; arch-tag: 577b0eab-a5cd-45e1-8d9f-c1a426f4d73c
311 ;;; mh-compat.el ends here