Remove time-stamp annoyance.
[emacs.git] / lisp / mh-e / mh-compat.el
blob0a0bc41779dae2ca8c4ef9ca336912c647c684c7
1 ;;; mh-compat.el --- make MH-E compatibile with various versions of Emacs
3 ;; Copyright (C) 2006, 2007, 2008 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 3, 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 sooner it would normally appear).
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 (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."
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 (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))
90 (t 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
140 `load-path'.
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
148 `image-load-path':
150 ;; Shush compiler.
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)
156 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.
162 (let ((img image)
163 (dir (or
164 ;; Images in image-load-path.
165 (mh-image-search-load-path image)
166 ;; Images in load-path.
167 (locate-library image)))
168 parent)
169 ;; Since the image might be in a nested directory (for
170 ;; example, mail/attach.pbm), adjust `image-directory'
171 ;; accordingly.
172 (when dir
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'.
183 (cond
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.
197 (setq
198 ;; Go down 2 levels.
199 d2ei (file-name-as-directory
200 (expand-file-name
201 (concat (file-name-directory library-name) "../../etc/images")))
202 ;; Go down 1 level.
203 d1ei (file-name-as-directory
204 (expand-file-name
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))
213 (no-error
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."
226 nil)
228 ;; For XEmacs.
229 (defalias 'mh-line-beginning-position
230 (if (fboundp 'line-beginning-position)
231 'line-beginning-position
232 'point-at-bol))
234 ;; For XEmacs.
235 (defalias 'mh-line-end-position
236 (if (fboundp 'line-end-position)
237 'line-end-position
238 'point-at-eol))
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."
245 nil)
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."
280 (mapconcat
281 (lambda (char)
282 ;; Fixme: use a char table instead.
283 (if (not (memq char mh-url-unreserved-chars))
284 (if (> char 255)
285 (error "Hexifying multibyte character %s" str)
286 (format "%%%02X" char))
287 (char-to-string char)))
288 str ""))
290 (defun-mh mh-view-mode-enter
291 view-mode-enter (&optional return-to exit-action)
292 "Enter View mode.
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."
296 ;; Shush compiler.
297 (if return-to nil)
298 (if exit-action nil)
299 (view-mode 1))
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
306 XEmacs."
307 (if (boundp 'write-file-functions)
308 ''write-file-functions ;Emacs 22 on
309 ''local-write-file-hooks)) ;XEmacs
311 (provide 'mh-compat)
313 ;; Local Variables:
314 ;; no-byte-compile: t
315 ;; indent-tabs-mode: nil
316 ;; sentence-end-double-space: nil
317 ;; End:
319 ;; arch-tag: 577b0eab-a5cd-45e1-8d9f-c1a426f4d73c
320 ;;; mh-compat.el ends here