1 ;;; mh-compat.el --- make MH-E compatible with various versions of Emacs
3 ;; Copyright (C) 2006-2016 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 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
31 ;; This is a good place to gather code that is used for compatibility
32 ;; between different versions of Emacs. Please document which versions
33 ;; of Emacs that the defsubst, defalias, or defmacro applies. That
34 ;; way, it's easy to occasionally go through this file and see which
35 ;; macros we can retire.
37 ;; Please use mh-gnus.el when providing compatibility with different
40 ;; Items are listed alphabetically (except for mh-require which is
41 ;; needed sooner it would normally appear).
46 (defalias 'mh-require
'require
))
49 (defun mh-require (feature &optional filename noerror
)
50 "If feature FEATURE is not loaded, load it from FILENAME.
51 If FEATURE is not a member of the list `features', then the feature
52 is not loaded; so load the file FILENAME.
53 If FILENAME is omitted, the printname of FEATURE is used as the file name.
54 If the optional third argument NOERROR is non-nil,
55 then return nil if the file is not found instead of signaling an error.
57 Simulate NOERROR argument in XEmacs which lacks it."
58 (if (not (featurep feature
))
60 (load filename noerror t
)
61 (load (format "%s" feature
) noerror t
)))))
63 (defun-mh mh-assoc-string assoc-string
(key list case-fold
)
64 "Like `assoc' but specifically for strings.
65 Case is ignored if CASE-FOLD is non-nil.
66 This function is used by Emacs versions that lack `assoc-string',
67 introduced in Emacs 22."
69 (assoc-ignore-case key list
)
73 (defalias 'mh-cancel-timer
74 (if (fboundp 'cancel-timer
)
78 ;; Emacs 24 renamed flet to cl-flet.
80 (if (fboundp 'cl-flet
)
84 (defun mh-display-color-cells (&optional display
)
85 "Return the number of color cells supported by DISPLAY.
86 This function is used by XEmacs to return 2 when `device-color-cells'
87 or `display-color-cells' returns nil. This happens when compiling or
88 running on a tty and causes errors since `display-color-cells' is
89 expected to return an integer."
90 (cond ((fboundp 'display-color-cells
) ; GNU Emacs, XEmacs 21.5b28
91 (or (display-color-cells display
) 2))
92 ((fboundp 'device-color-cells
) ; XEmacs 21.4
93 (or (device-color-cells display
) 2))
96 (defmacro mh-display-completion-list
(completions &optional common-substring
)
97 "Display the list of COMPLETIONS.
98 See documentation for `display-completion-list' for a description of the
99 arguments COMPLETIONS.
100 The optional argument COMMON-SUBSTRING, if non-nil, should be a string
101 specifying a common substring for adding the faces
102 `completions-first-difference' and `completions-common-part' to
104 (cond ((< emacs-major-version
22) `(display-completion-list ,completions
))
105 ((fboundp 'completion-hilit-commonality
) ; Emacs 23.1 and later
106 `(display-completion-list
107 (completion-hilit-commonality ,completions
108 ,(length common-substring
) nil
)))
110 `(display-completion-list ,completions
,common-substring
))))
112 (defmacro mh-face-foreground
(face &optional frame inherit
)
113 "Return the foreground color name of FACE, or nil if unspecified.
114 See documentation for `face-foreground' for a description of the
115 arguments FACE, FRAME, and perhaps 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-foreground ,face
,frame
)
120 `(face-foreground ,face
,frame
,inherit
)))
122 (defmacro mh-face-background
(face &optional frame inherit
)
123 "Return the background color name of face, or nil if unspecified.
124 See documentation for `back-foreground' for a description of the
125 arguments FACE, FRAME, and INHERIT.
126 This macro is used by Emacs versions that lack an INHERIT argument,
127 introduced in Emacs 22."
128 (if (< emacs-major-version
22)
129 `(face-background ,face
,frame
)
130 `(face-background ,face
,frame
,inherit
)))
132 (defun-mh mh-font-lock-add-keywords font-lock-add-keywords
133 (mode keywords
&optional how
)
134 "XEmacs does not have `font-lock-add-keywords'.
135 This function returns nil on that system.")
137 (defun-mh mh-image-load-path-for-library
138 image-load-path-for-library
(library image
&optional path no-error
)
139 "Return a suitable search path for images used by LIBRARY.
141 It searches for IMAGE in `image-load-path' (excluding
142 \"`data-directory'/images\") and `load-path', followed by a path
143 suitable for LIBRARY, which includes \"../../etc/images\" and
144 \"../etc/images\" relative to the library file itself, and then
145 in \"`data-directory'/images\".
147 Then this function returns a list of directories which contains
148 first the directory in which IMAGE was found, followed by the
149 value of `load-path'. If PATH is given, it is used instead of
152 If NO-ERROR is non-nil and a suitable path can't be found, don't
153 signal an error. Instead, return a list of directories as before,
154 except that nil appears in place of the image directory.
156 Here is an example that uses a common idiom to provide
157 compatibility with versions of Emacs that lack the variable
161 (defvar image-load-path)
163 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
164 (image-load-path (cons (car load-path)
165 (when (boundp \\='image-load-path)
167 (mh-tool-bar-folder-buttons-init))"
168 (unless library
(error "No library specified"))
169 (unless image
(error "No image specified"))
170 (let (image-directory image-directory-load-path
)
171 ;; Check for images in image-load-path or load-path.
174 ;; Images in image-load-path.
175 (mh-image-search-load-path image
)
176 ;; Images in load-path.
177 (locate-library image
)))
179 ;; Since the image might be in a nested directory (for
180 ;; example, mail/attach.pbm), adjust `image-directory'
183 (setq dir
(file-name-directory dir
))
184 (while (setq parent
(file-name-directory img
))
185 (setq img
(directory-file-name parent
)
186 dir
(expand-file-name "../" dir
))))
187 (setq image-directory-load-path dir
))
189 ;; If `image-directory-load-path' isn't Emacs's image directory,
190 ;; it's probably a user preference, so use it. Then use a
191 ;; relative setting if possible; otherwise, use
192 ;; `image-directory-load-path'.
194 ;; User-modified image-load-path?
195 ((and image-directory-load-path
196 (not (equal image-directory-load-path
197 (file-name-as-directory
198 (expand-file-name "images" data-directory
)))))
199 (setq image-directory image-directory-load-path
))
200 ;; Try relative setting.
201 ((let (library-name d1ei d2ei
)
202 ;; First, find library in the load-path.
203 (setq library-name
(locate-library library
))
204 (if (not library-name
)
205 (error "Cannot find library %s in load-path" library
))
206 ;; And then set image-directory relative to that.
209 d2ei
(file-name-as-directory
211 (concat (file-name-directory library-name
) "../../etc/images")))
213 d1ei
(file-name-as-directory
215 (concat (file-name-directory library-name
) "../etc/images"))))
216 (setq image-directory
217 ;; Set it to nil if image is not found.
218 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
219 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
220 ;; Use Emacs's image directory.
221 (image-directory-load-path
222 (setq image-directory image-directory-load-path
))
224 (message "Could not find image %s for library %s" image library
))
226 (error "Could not find image %s for library %s" image library
)))
228 ;; Return an augmented `path' or `load-path'.
229 (nconc (list image-directory
)
230 (delete image-directory
(copy-sequence (or path load-path
))))))
232 (defun-mh mh-image-search-load-path
233 image-search-load-path
(file &optional path
)
234 "Emacs 21 and XEmacs don't have `image-search-load-path'.
235 This function returns nil on those systems."
239 (defalias 'mh-line-beginning-position
240 (if (fboundp 'line-beginning-position
)
241 'line-beginning-position
245 (defalias 'mh-line-end-position
246 (if (fboundp 'line-end-position
)
250 (mh-require 'mailabbrev nil t
)
251 (defun-mh mh-mail-abbrev-make-syntax-table
252 mail-abbrev-make-syntax-table
()
253 "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
254 This function returns nil on those systems."
257 (defmacro mh-define-obsolete-variable-alias
258 (obsolete-name current-name
&optional when docstring
)
259 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
260 See documentation for `define-obsolete-variable-alias' for a description
261 of the arguments OBSOLETE-NAME, CURRENT-NAME, and perhaps WHEN
262 and DOCSTRING. This macro is used by XEmacs that lacks WHEN and
263 DOCSTRING arguments."
264 (if (featurep 'xemacs
)
265 `(define-obsolete-variable-alias ,obsolete-name
,current-name
)
266 `(define-obsolete-variable-alias ,obsolete-name
,current-name
,when
,docstring
)))
268 (defmacro mh-make-obsolete-variable
(obsolete-name current-name
&optional when access-type
)
269 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
270 See documentation for `make-obsolete-variable' for a description
271 of the arguments OBSOLETE-NAME, CURRENT-NAME, and perhaps WHEN
272 and ACCESS-TYPE. This macro is used by XEmacs that lacks WHEN and
273 ACCESS-TYPE arguments."
274 (if (featurep 'xemacs
)
275 `(make-obsolete-variable ,obsolete-name
,current-name
)
276 `(make-obsolete-variable ,obsolete-name
,current-name
,when
,access-type
)))
278 (defmacro mh-make-obsolete-variable
(obsolete-name current-name
&optional when access-type
)
279 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
280 See documentation for `make-obsolete-variable' for a description
281 of the arguments OBSOLETE-NAME, CURRENT-NAME, and perhaps WHEN
282 and ACCESS-TYPE. This macro is used by XEmacs that lacks WHEN and
283 ACCESS-TYPE arguments and by Emacs versions that lack ACCESS-TYPE,
284 introduced in Emacs 24."
285 (if (featurep 'xemacs
)
286 `(make-obsolete-variable ,obsolete-name
,current-name
)
287 (if (< emacs-major-version
24)
288 `(make-obsolete-variable ,obsolete-name
,current-name
,when
)
289 `(make-obsolete-variable ,obsolete-name
,current-name
,when
,access-type
))))
291 (defun-mh mh-match-string-no-properties
292 match-string-no-properties
(num &optional string
)
293 "Return string of text matched by last search, without text properties.
294 This function is used by XEmacs that lacks `match-string-no-properties'.
295 The function `buffer-substring-no-properties' is used instead.
296 The argument STRING is ignored."
297 (buffer-substring-no-properties
298 (match-beginning num
) (match-end num
)))
300 (defun-mh mh-replace-regexp-in-string replace-regexp-in-string
301 (regexp rep string
&optional fixedcase literal subexp start
)
302 "Replace REGEXP with REP everywhere in STRING and return result.
303 This function is used by XEmacs that lacks `replace-regexp-in-string'.
304 The function `replace-in-string' is used instead.
305 The arguments FIXEDCASE, SUBEXP, and START, used by
306 `replace-in-string' are ignored."
307 (replace-in-string string regexp rep literal
))
309 (defun-mh mh-test-completion
310 test-completion
(string collection
&optional predicate
)
311 "Return non-nil if STRING is a valid completion.
312 XEmacs does not have `test-completion'. This function returns nil
313 on that system." nil
)
315 ;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
316 (if (not (boundp 'url-unreserved-chars
))
317 (defconst mh-url-unreserved-chars
319 ?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
320 ?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
321 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
322 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
323 "A list of characters that are _NOT_ reserved in the URL spec.
324 This is taken from RFC 2396."))
326 (defun-mh mh-url-hexify-string url-hexify-string
(str)
327 "Escape characters in a string.
328 This is a copy of `url-hexify-string' from url-util.el in Emacs
329 22; needed by Emacs 21."
332 ;; Fixme: use a char table instead.
333 (if (not (memq char mh-url-unreserved-chars
))
335 (error "Hexifying multibyte character %s" str
)
336 (format "%%%02X" char
))
337 (char-to-string char
)))
340 (defun-mh mh-view-mode-enter
341 view-mode-enter
(&optional return-to exit-action
)
343 This function is used by XEmacs that lacks `view-mode-enter'.
344 The function `view-mode' is used instead.
345 The arguments RETURN-TO and EXIT-ACTION are ignored."
351 (defun-mh mh-window-full-height-p
352 window-full-height-p
(&optional WINDOW
)
353 "Return non-nil if WINDOW is not the result of a vertical split.
354 This function is defined in XEmacs as it lacks
355 `window-full-height-p'. The values of the functions
356 `window-height' and `frame-height' are compared instead. The
357 argument WINDOW is ignored."
358 (= (1+ (window-height))
361 (defmacro mh-write-file-functions
()
362 "Return `write-file-functions' if it exists.
363 Otherwise return `local-write-file-hooks'.
364 This macro exists purely for compatibility. The former symbol is used
365 in Emacs 22 onward while the latter is used in previous versions and
367 (if (boundp 'write-file-functions
)
368 ''write-file-functions
;Emacs 22 on
369 ''local-write-file-hooks
)) ;XEmacs
374 ;; no-byte-compile: t
375 ;; indent-tabs-mode: nil
376 ;; sentence-end-double-space: nil
379 ;;; mh-compat.el ends here