1;;; mh-compat.el --- make MH-E compatibile with various versions of Emacs
2
3;; Copyright (C) 2006, 2007 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
39;; Please use mh-gnus.el when providing compatibility with different
40;; versions of Gnus.
41
42;; Items are listed alphabetically (except for mh-require which is
43;; needed sooner it would normally appear).
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)))))
64
65(defun-mh mh-assoc-string assoc-string (key list case-fold)
66  "Like `assoc' but specifically for strings.
67Case is ignored if CASE-FOLD is non-nil.
68This function is used by Emacs versions that lack `assoc-string',
69introduced in Emacs 22."
70  (if case-fold
71      (assoc-ignore-case key list)
72    (assoc key list)))
73
74;; For XEmacs.
75(defalias 'mh-cancel-timer
76  (if (fboundp 'cancel-timer)
77      'cancel-timer
78    'delete-itimer))
79
80(defun-mh 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 return 2 when
83`device-color-cells' returns nil. This happens when compiling or
84running on a tty and causes errors since `display-color-cells' is
85expected to return an integer."
86  (or (device-color-cells display) 2))
87
88(defmacro mh-display-completion-list (completions &optional common-substring)
89  "Display the list of COMPLETIONS.
90See documentation for `display-completion-list' for a description of the
91arguments COMPLETIONS and perhaps COMMON-SUBSTRING.
92This macro is used by Emacs versions that lack a COMMON-SUBSTRING
93argument, introduced in Emacs 22."
94  (if (< emacs-major-version 22)
95      `(display-completion-list ,completions)
96    `(display-completion-list ,completions ,common-substring)))
97
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.
102This macro is used by Emacs versions that lack an INHERIT argument,
103introduced in Emacs 22."
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.
112This macro is used by Emacs versions that lack an INHERIT argument,
113introduced in Emacs 22."
114  (if (< emacs-major-version 22)
115      `(face-background ,face ,frame)
116    `(face-background ,face ,frame ,inherit)))
117
118(defun-mh mh-font-lock-add-keywords font-lock-add-keywords
119  (mode keywords &optional how)
120  "XEmacs does not have `font-lock-add-keywords'.
121This function returns nil on that system.")
122
123(defun-mh mh-image-load-path-for-library
124  image-load-path-for-library (library image &optional path no-error)
125  "Return a suitable search path for images used by LIBRARY.
126
127It searches for IMAGE in `image-load-path' (excluding
128\"`data-directory'/images\") and `load-path', followed by a path
129suitable for LIBRARY, which includes \"../../etc/images\" and
130\"../etc/images\" relative to the library file itself, and then
131in \"`data-directory'/images\".
132
133Then this function returns a list of directories which contains
134first the directory in which IMAGE was found, followed by the
135value of `load-path'. If PATH is given, it is used instead of
136`load-path'.
137
138If NO-ERROR is non-nil and a suitable path can't be found, don't
139signal an error. Instead, return a list of directories as before,
140except that nil appears in place of the image directory.
141
142Here is an example that uses a common idiom to provide
143compatibility with versions of Emacs that lack the variable
144`image-load-path':
145
146    ;; Shush compiler.
147    (defvar image-load-path)
148
149    (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
150           (image-load-path (cons (car load-path)
151                                  (when (boundp 'image-load-path)
152                                    image-load-path))))
153      (mh-tool-bar-folder-buttons-init))"
154  (unless library (error "No library specified"))
155  (unless image   (error "No image specified"))
156  (let (image-directory image-directory-load-path)
157    ;; Check for images in image-load-path or load-path.
158    (let ((img image)
159          (dir (or
160                ;; Images in image-load-path.
161                (mh-image-search-load-path image)
162                ;; Images in load-path.
163                (locate-library image)))
164          parent)
165      ;; Since the image might be in a nested directory (for
166      ;; example, mail/attach.pbm), adjust `image-directory'
167      ;; accordingly.
168      (when dir
169        (setq dir (file-name-directory dir))
170        (while (setq parent (file-name-directory img))
171          (setq img (directory-file-name parent)
172                dir (expand-file-name "../" dir))))
173      (setq image-directory-load-path dir))
174
175    ;; If `image-directory-load-path' isn't Emacs' image directory,
176    ;; it's probably a user preference, so use it. Then use a
177    ;; relative setting if possible; otherwise, use
178    ;; `image-directory-load-path'.
179    (cond
180     ;; User-modified image-load-path?
181     ((and image-directory-load-path
182           (not (equal image-directory-load-path
183                       (file-name-as-directory
184                        (expand-file-name "images" data-directory)))))
185      (setq image-directory image-directory-load-path))
186     ;; Try relative setting.
187     ((let (library-name d1ei d2ei)
188        ;; First, find library in the load-path.
189        (setq library-name (locate-library library))
190        (if (not library-name)
191            (error "Cannot find library %s in load-path" library))
192        ;; And then set image-directory relative to that.
193        (setq
194         ;; Go down 2 levels.
195         d2ei (file-name-as-directory
196               (expand-file-name
197                (concat (file-name-directory library-name) "../../etc/images")))
198         ;; Go down 1 level.
199         d1ei (file-name-as-directory
200               (expand-file-name
201                (concat (file-name-directory library-name) "../etc/images"))))
202        (setq image-directory
203              ;; Set it to nil if image is not found.
204              (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
205                    ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
206     ;; Use Emacs' image directory.
207     (image-directory-load-path
208      (setq image-directory image-directory-load-path))
209     (no-error
210      (message "Could not find image %s for library %s" image library))
211     (t
212      (error "Could not find image %s for library %s" image library)))
213
214    ;; Return an augmented `path' or `load-path'.
215    (nconc (list image-directory)
216           (delete image-directory (copy-sequence (or path load-path))))))
217
218(defun-mh mh-image-search-load-path
219  image-search-load-path (file &optional path)
220  "Emacs 21 and XEmacs don't have `image-search-load-path'.
221This function returns nil on those systems."
222  nil)
223
224;; For XEmacs.
225(defalias 'mh-line-beginning-position
226  (if (fboundp 'line-beginning-position)
227      'line-beginning-position
228    'point-at-bol))
229
230;; For XEmacs.
231(defalias 'mh-line-end-position
232  (if (fboundp 'line-end-position)
233      'line-end-position
234    'point-at-eol))
235
236(mh-require 'mailabbrev nil t)
237(defun-mh mh-mail-abbrev-make-syntax-table
238  mail-abbrev-make-syntax-table ()
239  "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
240This function returns nil on those systems."
241  nil)
242
243(defun-mh mh-match-string-no-properties
244  match-string-no-properties (num &optional string)
245  "Return string of text matched by last search, without text properties.
246This function is used by XEmacs that lacks `match-string-no-properties'.
247The function `buffer-substring-no-properties' is used instead.
248The argument STRING is ignored."
249  (buffer-substring-no-properties
250   (match-beginning num) (match-end num)))
251
252(defun-mh mh-replace-regexp-in-string replace-regexp-in-string
253  (regexp rep string &optional fixedcase literal subexp start)
254  "Replace REGEXP with REP everywhere in STRING and return result.
255This function is used by XEmacs that lacks `replace-regexp-in-string'.
256The function `replace-in-string' is used instead.
257The arguments FIXEDCASE, SUBEXP, and START, used by
258`replace-in-string' are ignored."
259  (replace-in-string string regexp rep literal))
260
261;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
262(if (not (boundp 'url-unreserved-chars))
263    (defconst mh-url-unreserved-chars
264      '(
265        ?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
266        ?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
267        ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
268        ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
269      "A list of characters that are _NOT_ reserved in the URL spec.
270This is taken from RFC 2396."))
271
272(defun-mh mh-url-hexify-string url-hexify-string (str)
273  "Escape characters in a string.
274This is a copy of `url-hexify-string' from url-util.el in Emacs
27522; needed by Emacs 21."
276  (mapconcat
277   (lambda (char)
278     ;; Fixme: use a char table instead.
279     (if (not (memq char mh-url-unreserved-chars))
280         (if (> char 255)
281               (error "Hexifying multibyte character %s" str)
282           (format "%%%02X" char))
283       (char-to-string char)))
284   str ""))
285
286(defun-mh mh-view-mode-enter
287  view-mode-enter (&optional return-to exit-action)
288  "Enter View mode.
289This function is used by XEmacs that lacks `view-mode-enter'.
290The function `view-mode' is used instead.
291The arguments RETURN-TO and EXIT-ACTION are ignored."
292  ;; Shush compiler.
293  (if return-to nil)
294  (if exit-action nil)
295  (view-mode 1))
296
297(defmacro mh-write-file-functions ()
298  "Return `write-file-functions' if it exists.
299Otherwise return `local-write-file-hooks'.
300This macro exists purely for compatibility. The former symbol is used
301in Emacs 22 onward while the latter is used in previous versions and
302XEmacs."
303  (if (boundp 'write-file-functions)
304      ''write-file-functions            ;Emacs 22 on
305    ''local-write-file-hooks))          ;XEmacs
306
307(provide 'mh-compat)
308
309;; Local Variables:
310;; no-byte-compile: t
311;; indent-tabs-mode: nil
312;; sentence-end-double-space: nil
313;; End:
314
315;; arch-tag: 577b0eab-a5cd-45e1-8d9f-c1a426f4d73c
316;;; mh-compat.el ends here
317