Improve format-time-string doc
[emacs.git] / lisp / mh-e / mh-compat.el
blob3dc7a62f3c97543b8973971f0a8ef42300395e8a
1 ;;; mh-compat.el --- make MH-E compatible with various versions of Emacs
3 ;; Copyright (C) 2006-2017 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 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 <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Change Log:
29 ;;; Code:
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
38 ;; versions of Gnus.
40 ;; Items are listed alphabetically (except for mh-require which is
41 ;; needed sooner it would normally appear).
43 (eval-when-compile (require 'mh-acros))
45 (mh-do-in-gnu-emacs
46 (defalias 'mh-require 'require))
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.
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)))))
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)))
72 ;; For XEmacs.
73 (defalias 'mh-cancel-timer
74 (if (fboundp 'cancel-timer)
75 'cancel-timer
76 'delete-itimer))
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))))
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))
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)))
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))))
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
163 `load-path'.
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
171 `image-load-path':
173 ;; Shush compiler.
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)
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))
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))
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."
249 nil)
251 ;; For XEmacs.
252 (defalias 'mh-line-beginning-position
253 (if (fboundp 'line-beginning-position)
254 'line-beginning-position
255 'point-at-bol))
257 ;; For XEmacs.
258 (defalias 'mh-line-end-position
259 (if (fboundp 'line-end-position)
260 'line-end-position
261 'point-at-eol))
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)
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 and by Emacs versions that lack ACCESS-TYPE,
287 introduced in Emacs 24."
288 (if (featurep 'xemacs)
289 `(make-obsolete-variable ,obsolete-name ,current-name)
290 (if (< emacs-major-version 24)
291 `(make-obsolete-variable ,obsolete-name ,current-name ,when)
292 `(make-obsolete-variable ,obsolete-name ,current-name ,when ,access-type))))
294 (defun-mh mh-match-string-no-properties
295 match-string-no-properties (num &optional string)
296 "Return string of text matched by last search, without text properties.
297 This function is used by XEmacs that lacks `match-string-no-properties'.
298 The function `buffer-substring-no-properties' is used instead.
299 The argument STRING is ignored."
300 (buffer-substring-no-properties
301 (match-beginning num) (match-end num)))
303 (defun-mh mh-replace-regexp-in-string replace-regexp-in-string
304 (regexp rep string &optional fixedcase literal subexp start)
305 "Replace REGEXP with REP everywhere in STRING and return result.
306 This function is used by XEmacs that lacks `replace-regexp-in-string'.
307 The function `replace-in-string' is used instead.
308 The arguments FIXEDCASE, SUBEXP, and START, used by
309 `replace-in-string' are ignored."
310 (replace-in-string string regexp rep literal))
312 (defun-mh mh-test-completion
313 test-completion (string collection &optional predicate)
314 "Return non-nil if STRING is a valid completion.
315 XEmacs does not have `test-completion'. This function returns nil
316 on that system." nil)
318 ;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
319 (defconst mh-url-unreserved-chars
321 ?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
322 ?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
323 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
324 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
325 "A list of characters that are _NOT_ reserved in the URL spec.
326 This is taken from RFC 2396.")
328 (defun-mh mh-url-hexify-string url-hexify-string (str)
329 "Escape characters in a string.
330 This is a copy of `url-hexify-string' from url-util.el in Emacs
331 22; needed by Emacs 21."
332 (mapconcat
333 (lambda (char)
334 ;; Fixme: use a char table instead.
335 (if (not (memq char mh-url-unreserved-chars))
336 (if (> char 255)
337 (error "Hexifying multibyte character %s" str)
338 (format "%%%02X" char))
339 (char-to-string char)))
340 str ""))
342 (defun-mh mh-view-mode-enter
343 view-mode-enter (&optional return-to exit-action)
344 "Enter View mode.
345 This function is used by XEmacs that lacks `view-mode-enter'.
346 The function `view-mode' is used instead.
347 The arguments RETURN-TO and EXIT-ACTION are ignored."
348 ;; Shush compiler.
349 (if return-to nil)
350 (if exit-action nil)
351 (view-mode 1))
353 (defun-mh mh-window-full-height-p
354 window-full-height-p (&optional WINDOW)
355 "Return non-nil if WINDOW is not the result of a vertical split.
356 This function is defined in XEmacs as it lacks
357 `window-full-height-p'. The values of the functions
358 `window-height' and `frame-height' are compared instead. The
359 argument WINDOW is ignored."
360 (= (1+ (window-height))
361 (frame-height)))
363 (defmacro mh-write-file-functions ()
364 "Return `write-file-functions' if it exists.
365 Otherwise return `local-write-file-hooks'.
366 This macro exists purely for compatibility. The former symbol is used
367 in Emacs 22 onward while the latter is used in previous versions and
368 XEmacs."
369 (if (boundp 'write-file-functions)
370 ''write-file-functions ;Emacs 22 on
371 ''local-write-file-hooks)) ;XEmacs
373 (provide 'mh-compat)
375 ;; Local Variables:
376 ;; indent-tabs-mode: nil
377 ;; sentence-end-double-space: nil
378 ;; End:
380 ;;; mh-compat.el ends here