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 made flet obsolete and suggested either cl-flet or
79 ;; cl-letf. This macro is based upon gmm-flet from Gnus.
80 (defmacro mh-flet
(bindings &rest body
)
81 "Make temporary overriding function definitions.
82 This is an analogue of a dynamically scoped `let' that operates on
83 the function cell of FUNCs rather than their value cell.
85 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
86 (if (fboundp 'cl-letf
)
87 `(cl-letf ,(mapcar (lambda (binding)
88 `((symbol-function ',(car binding
))
89 (lambda ,@(cdr binding
))))
92 `(flet ,bindings
,@body
)))
93 (put 'mh-flet
'lisp-indent-function
1)
94 (put 'mh-flet
'edebug-form-spec
95 '((&rest
(sexp sexp
&rest form
)) &rest form
))
97 (defun mh-display-color-cells (&optional display
)
98 "Return the number of color cells supported by DISPLAY.
99 This function is used by XEmacs to return 2 when `device-color-cells'
100 or `display-color-cells' returns nil. This happens when compiling or
101 running on a tty and causes errors since `display-color-cells' is
102 expected to return an integer."
103 (cond ((fboundp 'display-color-cells
) ; GNU Emacs, XEmacs 21.5b28
104 (or (display-color-cells display
) 2))
105 ((fboundp 'device-color-cells
) ; XEmacs 21.4
106 (or (device-color-cells display
) 2))
109 (defmacro mh-display-completion-list
(completions &optional common-substring
)
110 "Display the list of COMPLETIONS.
111 See documentation for `display-completion-list' for a description of the
112 arguments COMPLETIONS.
113 The optional argument COMMON-SUBSTRING, if non-nil, should be a string
114 specifying a common substring for adding the faces
115 `completions-first-difference' and `completions-common-part' to
117 (cond ((< emacs-major-version
22) `(display-completion-list ,completions
))
118 ((fboundp 'completion-hilit-commonality
) ; Emacs 23.1 and later
119 `(display-completion-list
120 (completion-hilit-commonality ,completions
121 ,(length common-substring
) nil
)))
123 `(display-completion-list ,completions
,common-substring
))))
125 (defmacro mh-face-foreground
(face &optional frame inherit
)
126 "Return the foreground color name of FACE, or nil if unspecified.
127 See documentation for `face-foreground' for a description of the
128 arguments FACE, FRAME, and perhaps INHERIT.
129 This macro is used by Emacs versions that lack an INHERIT argument,
130 introduced in Emacs 22."
131 (if (< emacs-major-version
22)
132 `(face-foreground ,face
,frame
)
133 `(face-foreground ,face
,frame
,inherit
)))
135 (defmacro mh-face-background
(face &optional frame inherit
)
136 "Return the background color name of face, or nil if unspecified.
137 See documentation for `back-foreground' for a description of the
138 arguments FACE, FRAME, and INHERIT.
139 This macro is used by Emacs versions that lack an INHERIT argument,
140 introduced in Emacs 22."
141 (if (< emacs-major-version
22)
142 `(face-background ,face
,frame
)
143 `(face-background ,face
,frame
,inherit
)))
145 (defun-mh mh-font-lock-add-keywords font-lock-add-keywords
146 (mode keywords
&optional how
)
147 "XEmacs does not have `font-lock-add-keywords'.
148 This function returns nil on that system.")
150 (defun-mh mh-image-load-path-for-library
151 image-load-path-for-library
(library image
&optional path no-error
)
152 "Return a suitable search path for images used by LIBRARY.
154 It searches for IMAGE in `image-load-path' (excluding
155 \"`data-directory'/images\") and `load-path', followed by a path
156 suitable for LIBRARY, which includes \"../../etc/images\" and
157 \"../etc/images\" relative to the library file itself, and then
158 in \"`data-directory'/images\".
160 Then this function returns a list of directories which contains
161 first the directory in which IMAGE was found, followed by the
162 value of `load-path'. If PATH is given, it is used instead of
165 If NO-ERROR is non-nil and a suitable path can't be found, don't
166 signal an error. Instead, return a list of directories as before,
167 except that nil appears in place of the image directory.
169 Here is an example that uses a common idiom to provide
170 compatibility with versions of Emacs that lack the variable
174 (defvar image-load-path)
176 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
177 (image-load-path (cons (car load-path)
178 (when (boundp \\='image-load-path)
180 (mh-tool-bar-folder-buttons-init))"
181 (unless library
(error "No library specified"))
182 (unless image
(error "No image specified"))
183 (let (image-directory image-directory-load-path
)
184 ;; Check for images in image-load-path or load-path.
187 ;; Images in image-load-path.
188 (mh-image-search-load-path image
)
189 ;; Images in load-path.
190 (locate-library image
)))
192 ;; Since the image might be in a nested directory (for
193 ;; example, mail/attach.pbm), adjust `image-directory'
196 (setq dir
(file-name-directory dir
))
197 (while (setq parent
(file-name-directory img
))
198 (setq img
(directory-file-name parent
)
199 dir
(expand-file-name "../" dir
))))
200 (setq image-directory-load-path dir
))
202 ;; If `image-directory-load-path' isn't Emacs's image directory,
203 ;; it's probably a user preference, so use it. Then use a
204 ;; relative setting if possible; otherwise, use
205 ;; `image-directory-load-path'.
207 ;; User-modified image-load-path?
208 ((and image-directory-load-path
209 (not (equal image-directory-load-path
210 (file-name-as-directory
211 (expand-file-name "images" data-directory
)))))
212 (setq image-directory image-directory-load-path
))
213 ;; Try relative setting.
214 ((let (library-name d1ei d2ei
)
215 ;; First, find library in the load-path.
216 (setq library-name
(locate-library library
))
217 (if (not library-name
)
218 (error "Cannot find library %s in load-path" library
))
219 ;; And then set image-directory relative to that.
222 d2ei
(file-name-as-directory
224 (concat (file-name-directory library-name
) "../../etc/images")))
226 d1ei
(file-name-as-directory
228 (concat (file-name-directory library-name
) "../etc/images"))))
229 (setq image-directory
230 ;; Set it to nil if image is not found.
231 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
232 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
233 ;; Use Emacs's image directory.
234 (image-directory-load-path
235 (setq image-directory image-directory-load-path
))
237 (message "Could not find image %s for library %s" image library
))
239 (error "Could not find image %s for library %s" image library
)))
241 ;; Return an augmented `path' or `load-path'.
242 (nconc (list image-directory
)
243 (delete image-directory
(copy-sequence (or path load-path
))))))
245 (defun-mh mh-image-search-load-path
246 image-search-load-path
(file &optional path
)
247 "Emacs 21 and XEmacs don't have `image-search-load-path'.
248 This function returns nil on those systems."
252 (defalias 'mh-line-beginning-position
253 (if (fboundp 'line-beginning-position
)
254 'line-beginning-position
258 (defalias 'mh-line-end-position
259 (if (fboundp 'line-end-position
)
263 (mh-require 'mailabbrev nil t
)
264 (defun-mh mh-mail-abbrev-make-syntax-table
265 mail-abbrev-make-syntax-table
()
266 "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
267 This function returns nil on those systems."
270 (defmacro mh-define-obsolete-variable-alias
271 (obsolete-name current-name
&optional when docstring
)
272 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
273 See documentation for `define-obsolete-variable-alias' for a description
274 of the arguments OBSOLETE-NAME, CURRENT-NAME, and perhaps WHEN
275 and DOCSTRING. This macro is used by XEmacs that lacks WHEN and
276 DOCSTRING arguments."
277 (if (featurep 'xemacs
)
278 `(define-obsolete-variable-alias ,obsolete-name
,current-name
)
279 `(define-obsolete-variable-alias ,obsolete-name
,current-name
,when
,docstring
)))
281 (defmacro mh-make-obsolete-variable
(obsolete-name current-name
&optional when access-type
)
282 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
283 See documentation for `make-obsolete-variable' for a description
284 of the arguments OBSOLETE-NAME, CURRENT-NAME, and perhaps WHEN
285 and ACCESS-TYPE. This macro is used by XEmacs that lacks WHEN and
286 ACCESS-TYPE arguments."
287 (if (featurep 'xemacs
)
288 `(make-obsolete-variable ,obsolete-name
,current-name
)
289 `(make-obsolete-variable ,obsolete-name
,current-name
,when
,access-type
)))
291 (defmacro mh-make-obsolete-variable
(obsolete-name current-name
&optional when access-type
)
292 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
293 See documentation for `make-obsolete-variable' for a description
294 of the arguments OBSOLETE-NAME, CURRENT-NAME, and perhaps WHEN
295 and ACCESS-TYPE. This macro is used by XEmacs that lacks WHEN and
296 ACCESS-TYPE arguments and by Emacs versions that lack ACCESS-TYPE,
297 introduced in Emacs 24."
298 (if (featurep 'xemacs
)
299 `(make-obsolete-variable ,obsolete-name
,current-name
)
300 (if (< emacs-major-version
24)
301 `(make-obsolete-variable ,obsolete-name
,current-name
,when
)
302 `(make-obsolete-variable ,obsolete-name
,current-name
,when
,access-type
))))
304 (defun-mh mh-match-string-no-properties
305 match-string-no-properties
(num &optional string
)
306 "Return string of text matched by last search, without text properties.
307 This function is used by XEmacs that lacks `match-string-no-properties'.
308 The function `buffer-substring-no-properties' is used instead.
309 The argument STRING is ignored."
310 (buffer-substring-no-properties
311 (match-beginning num
) (match-end num
)))
313 (defun-mh mh-replace-regexp-in-string replace-regexp-in-string
314 (regexp rep string
&optional fixedcase literal subexp start
)
315 "Replace REGEXP with REP everywhere in STRING and return result.
316 This function is used by XEmacs that lacks `replace-regexp-in-string'.
317 The function `replace-in-string' is used instead.
318 The arguments FIXEDCASE, SUBEXP, and START, used by
319 `replace-in-string' are ignored."
320 (replace-in-string string regexp rep literal
))
322 (defun-mh mh-test-completion
323 test-completion
(string collection
&optional predicate
)
324 "Return non-nil if STRING is a valid completion.
325 XEmacs does not have `test-completion'. This function returns nil
326 on that system." nil
)
328 ;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
329 (if (not (boundp 'url-unreserved-chars
))
330 (defconst mh-url-unreserved-chars
332 ?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
333 ?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
334 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
335 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
336 "A list of characters that are _NOT_ reserved in the URL spec.
337 This is taken from RFC 2396."))
339 (defun-mh mh-url-hexify-string url-hexify-string
(str)
340 "Escape characters in a string.
341 This is a copy of `url-hexify-string' from url-util.el in Emacs
342 22; needed by Emacs 21."
345 ;; Fixme: use a char table instead.
346 (if (not (memq char mh-url-unreserved-chars
))
348 (error "Hexifying multibyte character %s" str
)
349 (format "%%%02X" char
))
350 (char-to-string char
)))
353 (defun-mh mh-view-mode-enter
354 view-mode-enter
(&optional return-to exit-action
)
356 This function is used by XEmacs that lacks `view-mode-enter'.
357 The function `view-mode' is used instead.
358 The arguments RETURN-TO and EXIT-ACTION are ignored."
364 (defun-mh mh-window-full-height-p
365 window-full-height-p
(&optional WINDOW
)
366 "Return non-nil if WINDOW is not the result of a vertical split.
367 This function is defined in XEmacs as it lacks
368 `window-full-height-p'. The values of the functions
369 `window-height' and `frame-height' are compared instead. The
370 argument WINDOW is ignored."
371 (= (1+ (window-height))
374 (defmacro mh-write-file-functions
()
375 "Return `write-file-functions' if it exists.
376 Otherwise return `local-write-file-hooks'.
377 This macro exists purely for compatibility. The former symbol is used
378 in Emacs 22 onward while the latter is used in previous versions and
380 (if (boundp 'write-file-functions
)
381 ''write-file-functions
;Emacs 22 on
382 ''local-write-file-hooks
)) ;XEmacs
387 ;; no-byte-compile: t
388 ;; indent-tabs-mode: nil
389 ;; sentence-end-double-space: nil
392 ;;; mh-compat.el ends here