]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-compat.el
* src/macfont.m (mac_font_shape): Make sure that total_advance is increasing.
[gnu-emacs] / lisp / mh-e / mh-compat.el
1 ;;; mh-compat.el --- make MH-E compatible with various versions of Emacs
2
3 ;; Copyright (C) 2006-2016 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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; Change Log:
28
29 ;;; Code:
30
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.
36
37 ;; Please use mh-gnus.el when providing compatibility with different
38 ;; versions of Gnus.
39
40 ;; Items are listed alphabetically (except for mh-require which is
41 ;; needed sooner it would normally appear).
42
43 (require 'mh-acros)
44
45 (mh-do-in-gnu-emacs
46 (defalias 'mh-require 'require))
47
48 (mh-do-in-xemacs
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.
56
57 Simulate NOERROR argument in XEmacs which lacks it."
58 (if (not (featurep feature))
59 (if filename
60 (load filename noerror t)
61 (load (format "%s" feature) noerror t)))))
62
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."
68 (if case-fold
69 (assoc-ignore-case key list)
70 (assoc key list)))
71
72 ;; For XEmacs.
73 (defalias 'mh-cancel-timer
74 (if (fboundp 'cancel-timer)
75 'cancel-timer
76 'delete-itimer))
77
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.
84
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))))
90 bindings)
91 ,@body)
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))
96
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))
107 (t 2)))
108
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
116 the completions."
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)))
122 (t ; Emacs 22
123 `(display-completion-list ,completions ,common-substring))))
124
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)))
134
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)))
144
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.")
149
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.
153
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\".
159
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
163 `load-path'.
164
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.
168
169 Here is an example that uses a common idiom to provide
170 compatibility with versions of Emacs that lack the variable
171 `image-load-path':
172
173 ;; Shush compiler.
174 (defvar image-load-path)
175
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)
179 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.
185 (let ((img image)
186 (dir (or
187 ;; Images in image-load-path.
188 (mh-image-search-load-path image)
189 ;; Images in load-path.
190 (locate-library image)))
191 parent)
192 ;; Since the image might be in a nested directory (for
193 ;; example, mail/attach.pbm), adjust `image-directory'
194 ;; accordingly.
195 (when dir
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))
201
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'.
206 (cond
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.
220 (setq
221 ;; Go down 2 levels.
222 d2ei (file-name-as-directory
223 (expand-file-name
224 (concat (file-name-directory library-name) "../../etc/images")))
225 ;; Go down 1 level.
226 d1ei (file-name-as-directory
227 (expand-file-name
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))
236 (no-error
237 (message "Could not find image %s for library %s" image library))
238 (t
239 (error "Could not find image %s for library %s" image library)))
240
241 ;; Return an augmented `path' or `load-path'.
242 (nconc (list image-directory)
243 (delete image-directory (copy-sequence (or path load-path))))))
244
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."
249 nil)
250
251 ;; For XEmacs.
252 (defalias 'mh-line-beginning-position
253 (if (fboundp 'line-beginning-position)
254 'line-beginning-position
255 'point-at-bol))
256
257 ;; For XEmacs.
258 (defalias 'mh-line-end-position
259 (if (fboundp 'line-end-position)
260 'line-end-position
261 'point-at-eol))
262
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."
268 nil)
269
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)))
280
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)))
290
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))))
303
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)))
312
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))
321
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)
327
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
331 '(
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."))
338
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."
343 (mapconcat
344 (lambda (char)
345 ;; Fixme: use a char table instead.
346 (if (not (memq char mh-url-unreserved-chars))
347 (if (> char 255)
348 (error "Hexifying multibyte character %s" str)
349 (format "%%%02X" char))
350 (char-to-string char)))
351 str ""))
352
353 (defun-mh mh-view-mode-enter
354 view-mode-enter (&optional return-to exit-action)
355 "Enter View mode.
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."
359 ;; Shush compiler.
360 (if return-to nil)
361 (if exit-action nil)
362 (view-mode 1))
363
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))
372 (frame-height)))
373
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
379 XEmacs."
380 (if (boundp 'write-file-functions)
381 ''write-file-functions ;Emacs 22 on
382 ''local-write-file-hooks)) ;XEmacs
383
384 (provide 'mh-compat)
385
386 ;; Local Variables:
387 ;; no-byte-compile: t
388 ;; indent-tabs-mode: nil
389 ;; sentence-end-double-space: nil
390 ;; End:
391
392 ;;; mh-compat.el ends here