(emacs-major-version, emacs-minor-version): Define at compile-time
[elisp/apel.git] / poe.el
1 ;;; poe.el --- Portable Outfit for Emacsen
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;;      Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;; Keywords: emulation, compatibility, Nemacs, MULE, Emacs/mule, XEmacs
8
9 ;; This file is part of APEL (A Portable Emacs Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (provide 'poe)                          ; beware of circular dependency.
31                                         ; localhook.el depends on poe.el.
32 (require 'pym)
33
34
35 ;;; @ Version information.
36 ;;;
37
38 ;; v18 does not have many features we expect,
39 ;; notably `eval-when-compile' and `eval-and-compile'.
40 (static-when (string= (substring emacs-version 0 2) "18")
41   (require 'poe-18))
42
43 ;; Now we can use them!
44 (eval-and-compile
45   ;; We must define these two constants at compile-time as well as
46   ;; load-time since they are used for compile-time version checking.
47   (defconst-maybe emacs-major-version
48     (progn (string-match "^[0-9]+" emacs-version)
49            (string-to-int (substring emacs-version
50                                      (match-beginning 0)(match-end 0))))
51     "Major version number of this version of Emacs.")
52   (defconst-maybe emacs-minor-version
53     (progn (string-match "^[0-9]+\\.\\([0-9]+\\)" emacs-version)
54            (string-to-int (substring emacs-version
55                                      (match-beginning 1)(match-end 1))))
56     "Minor version number of this version of Emacs."))
57
58 ;; Some ancient version of XEmacs did not provide 'xemacs.
59 (static-when (string-match "XEmacs" emacs-version)
60   (provide 'xemacs))
61
62 ;; `file-coding' was appeared in the spring of 1998, just before XEmacs
63 ;; 21.0. Therefore it is not provided in XEmacs with MULE versions 20.4
64 ;; or earlier.
65 (static-when (featurep 'xemacs)
66   ;; must be load-time check to share .elc between w/ MULE and w/o MULE.
67   (when (featurep 'mule)
68     (provide 'file-coding)))
69
70 (static-when (featurep 'xemacs)
71   (require 'poe-xemacs))
72
73 ;; must be load-time check to share .elc between different systems.
74 (or (fboundp 'open-network-stream)
75     (require 'tcp))
76 \f
77
78 ;;; @ C primitives emulation.
79 ;;;
80
81 ;; (require FEATURE &optional FILENAME NOERROR)
82 ;; Emacs 20.4 and later takes optional 3rd arg NOERROR.
83 (static-condition-case nil
84     ;; compile-time check.
85     (progn
86       (require 'nofeature "nofile" 'noerror)
87       (if (get 'require 'defun-maybe)
88           (error "")))                  ; already redefined.
89   (error
90    ;; load-time check.
91    (or (fboundp 'si:require)
92        (progn
93          (fset 'si:require (symbol-function 'require))
94          (put 'require 'defun-maybe t)
95          (defun require (feature &optional filename noerror)
96            "\
97 If feature FEATURE is not loaded, load it from FILENAME.
98 If FEATURE is not a member of the list `features', then the feature
99 is not loaded; so load the file FILENAME.
100 If FILENAME is omitted, the printname of FEATURE is used as the file name,
101 but in this case `load' insists on adding the suffix `.el' or `.elc'.
102 If the optional third argument NOERROR is non-nil,
103 then return nil if the file is not found.
104 Normally the return value is FEATURE."
105            (if noerror
106                (condition-case nil
107                    (si:require feature filename)
108                  (error))
109              (si:require feature filename)))))))
110
111 ;; Emacs 19.29 and later: (plist-get PLIST PROP)
112 ;; (defun-maybe plist-get (plist prop)
113 ;;   (while (and plist
114 ;;               (not (eq (car plist) prop)))
115 ;;     (setq plist (cdr (cdr plist))))
116 ;;   (car (cdr plist)))
117 (static-unless (and (fboundp 'plist-get)
118                     (not (get 'plist-get 'defun-maybe)))
119   (or (fboundp 'plist-get)
120       (progn
121         (defvar plist-get-internal-symbol)
122         (defun plist-get (plist prop)
123           "\
124 Extract a value from a property list.
125 PLIST is a property list, which is a list of the form
126 \(PROP1 VALUE1 PROP2 VALUE2...\).  This function returns the value
127 corresponding to the given PROP, or nil if PROP is not
128 one of the properties on the list."
129           (setplist 'plist-get-internal-symbol plist)
130           (get 'plist-get-internal-symbol prop))
131         (setq current-load-list (cons 'plist-get current-load-list))
132         (put 'plist-get 'defun-maybe t))))
133
134 ;; Emacs 19.29 and later: (plist-put PLIST PROP VAL)
135 ;; (defun-maybe plist-put (plist prop val)
136 ;;   (catch 'found
137 ;;     (let ((tail plist)
138 ;;           (prev nil))
139 ;;       (while (and tail (cdr tail))
140 ;;         (if (eq (car tail) prop)
141 ;;             (progn
142 ;;               (setcar (cdr tail) val)
143 ;;               (throw 'found plist))
144 ;;           (setq prev tail
145 ;;                 tail (cdr (cdr tail)))))
146 ;;       (if prev
147 ;;           (progn
148 ;;             (setcdr (cdr prev) (list prop val))
149 ;;             plist)
150 ;;         (list prop val)))))
151 (static-unless (and (fboundp 'plist-put)
152                     (not (get 'plist-put 'defun-maybe)))
153   (or (fboundp 'plist-put)
154       (progn
155         (defvar plist-put-internal-symbol)
156         (defun plist-put (plist prop val)
157           "\
158 Change value in PLIST of PROP to VAL.
159 PLIST is a property list, which is a list of the form
160 \(PROP1 VALUE1 PROP2 VALUE2 ...\).  PROP is a symbol and VAL is any object.
161 If PROP is already a property on the list, its value is set to VAL,
162 otherwise the new PROP VAL pair is added.  The new plist is returned;
163 use `\(setq x \(plist-put x prop val\)\)' to be sure to use the new value.
164 The PLIST is modified by side effects."
165           (setplist 'plist-put-internal-symbol plist)
166           (put 'plist-put-internal-symbol prop val)
167           (symbol-plist 'plist-put-internal-symbol))
168         (setq current-load-list (cons 'plist-put current-load-list))
169         (put 'plist-put 'defun-maybe t))))
170
171 ;; Emacs 19.23 and later: (minibuffer-prompt-width)
172 (defun-maybe minibuffer-prompt-width ()
173   "Return the display width of the minibuffer prompt."
174   (save-excursion
175     (set-buffer (window-buffer (minibuffer-window)))
176     (current-column)))
177
178 ;; (read-string PROMPT &optional INITIAL-INPUT HISTORY)
179 ;; Emacs 19.29/XEmacs 19.14(?) and later takes optional 3rd arg HISTORY.
180 (static-unless (or (featurep 'xemacs)
181                    (>= emacs-major-version 20)
182                    (and (= emacs-major-version 19)
183                         (>= emacs-minor-version 29)))
184   (or (fboundp 'si:read-string)
185       (progn
186         (fset 'si:read-string (symbol-function 'read-string))
187         (defun read-string (prompt &optional initial-input history)
188           "\
189 Read a string from the minibuffer, prompting with string PROMPT.
190 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
191 The third arg HISTORY, is dummy for compatibility.
192 See `read-from-minibuffer' for details of HISTORY argument."
193           (si:read-string prompt initial-input)))))
194
195 ;; v18: (string-to-int STRING)
196 ;; v19: (string-to-number STRING)
197 ;; v20: (string-to-number STRING &optional BASE)
198 ;;
199 ;; XXX: `string-to-number' of Emacs 20.3 and earlier is broken.
200 ;;      (string-to-number "1e1" 16) => 10.0, should be 481.
201 (static-condition-case nil
202     ;; compile-time check.
203     (if (= (string-to-number "1e1" 16) 481)
204         (if (get 'string-to-number 'defun-maybe)
205             (error ""))                 ; already redefined.
206       (error ""))                       ; Emacs 20.3 and ealier.
207   (error
208    ;; load-time check.
209    (or (fboundp 'si:string-to-number)
210        (progn
211          (if (fboundp 'string-to-number)
212              (fset 'si:string-to-number (symbol-function 'string-to-number))
213            (fset 'si:string-to-number (symbol-function 'string-to-int))
214            (defalias 'string-to-int 'string-to-number))
215          (put 'string-to-number 'defun-maybe t)
216          (defun string-to-number (string &optional base)
217            "\
218 Convert STRING to a number by parsing it as a decimal number.
219 This parses both integers and floating point numbers.
220 It ignores leading spaces and tabs.
221
222 If BASE, interpret STRING as a number in that base.  If BASE isn't
223 present, base 10 is used.  BASE must be between 2 and 16 (inclusive).
224 If the base used is not 10, floating point is not recognized."
225            (if (or (null base) (= base 10))
226                (si:string-to-number string)
227              (if (or (< base 2)(> base 16))
228                  (signal 'args-out-of-range (cons base nil)))
229              (let ((len (length string))
230                    (pos 0))
231                ;; skip leading whitespace.
232                (while (and (< pos len)
233                            (memq (aref string pos) '(?\  ?\t)))
234                  (setq pos (1+ pos)))
235                (if (= pos len)
236                    0
237                  (let ((number 0)(negative 1)
238                        chr num)
239                    (if (eq (aref string pos) ?-)
240                        (setq negative -1
241                              pos (1+ pos))
242                      (if (eq (aref string pos) ?+)
243                          (setq pos (1+ pos))))
244                    (while (and (< pos len)
245                                (setq chr (aref string pos)
246                                      num (cond
247                                           ((and (<= ?0 chr)(<= chr ?9))
248                                            (- chr ?0))
249                                           ((and (<= ?A chr)(<= chr ?F))
250                                            (+ (- chr ?A) 10))
251                                           ((and (<= ?a chr)(<= chr ?f))
252                                            (+ (- chr ?a) 10))
253                                           (t nil)))
254                                (< num base))
255                      (setq number (+ (* number base) num)
256                            pos (1+ pos)))
257                    (* negative number))))))))))
258
259 ;; Emacs 20.1 and 20.2: (concat-chars &rest CHARS)
260 ;; Emacs 20.3/XEmacs 21.0 and later: (string &rest CHARS)
261 (static-cond
262  ((and (fboundp 'string)
263        (subrp (symbol-function 'string)))
264   ;; Emacs 20.3/XEmacs 21.0 and later.
265   )
266  ((and (fboundp 'concat-chars)
267        (subrp (symbol-function 'concat-chars)))
268   ;; Emacs 20.1 and 20.2.
269   (defalias 'string 'concat-chars))
270  (t
271   ;; Use `defun-maybe' to update `load-history'.
272   (defun-maybe string (&rest chars)
273     "Concatenate all the argument characters and make the result a string."
274     ;; We cannot use (apply 'concat chars) here because `concat' does not
275     ;; work with multibyte chars on Mule 1.* and 2.*.
276     (mapconcat (function char-to-string) chars ""))))
277
278 ;; Mule: (char-before POS)
279 ;; v20: (char-before &optional POS)
280 (static-condition-case nil
281     ;; compile-time check.
282     (progn
283       (char-before)
284       (if (get 'char-before 'defun-maybe)
285           (error "")))                  ; already defined.
286   (wrong-number-of-arguments            ; Mule.
287    ;; load-time check.
288    (or (fboundp 'si:char-before)
289        (progn
290          (fset 'si:char-before (symbol-function 'char-before))
291          (put 'char-before 'defun-maybe t)
292          ;; takes IGNORED for backward compatibility.
293          (defun char-before (&optional pos ignored)
294            "\
295 Return character in current buffer preceding position POS.
296 POS is an integer or a buffer pointer.
297 If POS is out of range, the value is nil."
298            (si:char-before (or pos (point)))))))
299   (void-function                        ; non-Mule.
300    ;; load-time check.
301    (defun-maybe char-before (&optional pos)
302      "\
303 Return character in current buffer preceding position POS.
304 POS is an integer or a buffer pointer.
305 If POS is out of range, the value is nil."
306      (if pos
307          (save-excursion
308            (and (= (goto-char pos) (point))
309                 (not (bobp))
310                 (preceding-char)))
311        (and (not (bobp))
312             (preceding-char)))))
313   (error                                ; found our definition at compile-time.
314    ;; load-time check.
315    (condition-case nil
316        (char-before)
317      (wrong-number-of-arguments         ; Mule.
318       (or (fboundp 'si:char-before)
319           (progn
320             (fset 'si:char-before (symbol-function 'char-before))
321             (put 'char-before 'defun-maybe t)
322             ;; takes IGNORED for backward compatibility.
323             (defun char-before (&optional pos ignored)
324               "\
325 Return character in current buffer preceding position POS.
326 POS is an integer or a buffer pointer.
327 If POS is out of range, the value is nil."
328               (si:char-before (or pos (point)))))))
329      (void-function                     ; non-Mule.
330       (defun-maybe char-before (&optional pos)
331         "\
332 Return character in current buffer preceding position POS.
333 POS is an integer or a buffer pointer.
334 If POS is out of range, the value is nil."
335         (if pos
336             (save-excursion
337               (and (= (goto-char pos) (point))
338                    (not (bobp))
339                    (preceding-char)))
340           (and (not (bobp))
341                (preceding-char))))))))
342
343 ;; v18, v19: (char-after POS)
344 ;; v20: (char-after &optional POS)
345 (static-condition-case nil
346     ;; compile-time check.
347     (progn
348       (char-after)
349       (if (get 'char-after 'defun-maybe)
350           (error "")))                  ; already defined.
351   (wrong-number-of-arguments            ; v18, v19
352    ;; load-time check.
353    (or (fboundp 'si:char-after)
354        (progn
355          (fset 'si:char-after (symbol-function 'char-after))
356          (put 'char-after 'defun-maybe t)
357          (defun char-after (&optional pos)
358            "\
359 Return character in current buffer at position POS.
360 POS is an integer or a buffer pointer.
361 If POS is out of range, the value is nil."
362            (si:char-after (or pos (point)))))))
363   (void-function                        ; NEVER happen?
364    ;; load-time check.
365    (defun-maybe char-after (&optional pos)
366      "\
367 Return character in current buffer at position POS.
368 POS is an integer or a buffer pointer.
369 If POS is out of range, the value is nil."
370      (if pos
371          (save-excursion
372            (and (= (goto-char pos) (point))
373                 (not (eobp))
374                 (following-char)))
375        (and (not (eobp))
376             (following-char)))))
377   (error                                ; found our definition at compile-time.
378    ;; load-time check.
379    (condition-case nil
380        (char-after)
381      (wrong-number-of-arguments         ; v18, v19
382       (or (fboundp 'si:char-after)
383           (progn
384             (fset 'si:char-after (symbol-function 'char-after))
385             (put 'char-after 'defun-maybe t)
386             (defun char-after (&optional pos)
387               "\
388 Return character in current buffer at position POS.
389 POS is an integer or a buffer pointer.
390 If POS is out of range, the value is nil."
391               (si:char-after (or pos (point)))))))
392      (void-function                     ; NEVER happen?
393       (defun-maybe char-after (&optional pos)
394         "\
395 Return character in current buffer at position POS.
396 POS is an integer or a buffer pointer.
397 If POS is out of range, the value is nil."
398         (if pos
399             (save-excursion
400               (and (= (goto-char pos) (point))
401                    (not (eobp))
402                    (following-char)))
403           (and (not (eobp))
404                (following-char))))))))
405
406 ;; Emacs 19.29 and later: (buffer-substring-no-properties START END)
407 (defun-maybe buffer-substring-no-properties (start end)
408   "Return the characters of part of the buffer, without the text properties.
409 The two arguments START and END are character positions;
410 they can be in either order."
411   (let ((string (buffer-substring start end)))
412     (set-text-properties 0 (length string) nil string)
413     string))
414
415 ;; Emacs 19.31 and later: (buffer-live-p OBJECT)
416 (defun-maybe buffer-live-p (object)
417   "Return non-nil if OBJECT is a buffer which has not been killed.
418 Value is nil if OBJECT is not a buffer or if it has been killed."
419   (and object
420        (get-buffer object)
421        (buffer-name (get-buffer object))
422        t))
423
424 ;; Emacs 20: (line-beginning-position &optional N)
425 (defun-maybe line-beginning-position (&optional n)
426   "Return the character position of the first character on the current line.
427 With argument N not nil or 1, move forward N - 1 lines first.
428 If scan reaches end of buffer, return that position.
429 This function does not move point."
430   (save-excursion
431     (forward-line (1- (or n 1)))
432     (point)))
433
434 ;; Emacs 20: (line-end-position &optional N)
435 (defun-maybe line-end-position (&optional n)
436   "Return the character position of the last character on the current line.
437 With argument N not nil or 1, move forward N - 1 lines first.
438 If scan reaches end of buffer, return that position.
439 This function does not move point."
440   (save-excursion
441     (end-of-line (or n 1))
442     (point)))
443 \f
444
445 ;;; @ Basic lisp subroutines emulation. (lisp/subr.el)
446 ;;;
447
448 ;;; @@ Lisp language features.
449
450 (defmacro-maybe push (newelt listname)
451   "Add NEWELT to the list stored in the symbol LISTNAME.
452 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
453 LISTNAME must be a symbol."
454   (list 'setq listname
455         (list 'cons newelt listname)))
456
457 (defmacro-maybe pop (listname)
458   "Return the first element of LISTNAME's value, and remove it from the list.
459 LISTNAME must be a symbol whose value is a list.
460 If the value is nil, `pop' returns nil but does not actually
461 change the list."
462   (list 'prog1 (list 'car listname)
463         (list 'setq listname (list 'cdr listname))))
464
465 (defmacro-maybe when (cond &rest body)
466   "If COND yields non-nil, do BODY, else return nil."
467   (list 'if cond (cons 'progn body)))
468 ;; (def-edebug-spec when (&rest form))
469
470 (defmacro-maybe unless (cond &rest body)
471   "If COND yields nil, do BODY, else return nil."
472   (cons 'if (cons cond (cons nil body))))
473 ;; (def-edebug-spec unless (&rest form))
474
475 (defsubst-maybe caar (x)
476   "Return the car of the car of X."
477   (car (car x)))
478
479 (defsubst-maybe cadr (x)
480   "Return the car of the cdr of X."
481   (car (cdr x)))
482
483 (defsubst-maybe cdar (x)
484   "Return the cdr of the car of X."
485   (cdr (car x)))
486
487 (defsubst-maybe cddr (x)
488   "Return the cdr of the cdr of X."
489   (cdr (cdr x)))
490
491 (defun-maybe last (x &optional n)
492   "Return the last link of the list X.  Its car is the last element.
493 If X is nil, return nil.
494 If N is non-nil, return the Nth-to-last link of X.
495 If N is bigger than the length of X, return X."
496   (if n
497       (let ((m 0) (p x))
498         (while (consp p)
499           (setq m (1+ m) p (cdr p)))
500         (if (<= n 0) p
501           (if (< n m) (nthcdr (- m n) x) x)))
502     (while (cdr x)
503       (setq x (cdr x)))
504     x))
505
506 ;; Actually, `butlast' and `nbutlast' are defined in lisp/cl.el.
507 (defun-maybe butlast (x &optional n)
508   "Returns a copy of LIST with the last N elements removed."
509   (if (and n (<= n 0)) x
510     (nbutlast (copy-sequence x) n)))
511
512 (defun-maybe nbutlast (x &optional n)
513   "Modifies LIST to remove the last N elements."
514   (let ((m (length x)))
515     (or n (setq n 1))
516     (and (< n m)
517          (progn
518            (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
519            x))))
520
521 ;; Emacs 20.3 and later: (assoc-default KEY ALIST &optional TEST DEFAULT)
522 (defun-maybe assoc-default (key alist &optional test default)
523   "Find object KEY in a pseudo-alist ALIST.
524 ALIST is a list of conses or objects.  Each element (or the element's car,
525 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
526 If that is non-nil, the element matches;
527 then `assoc-default' returns the element's cdr, if it is a cons,
528 or DEFAULT if the element is not a cons.
529
530 If no element matches, the value is nil.
531 If TEST is omitted or nil, `equal' is used."
532   (let (found (tail alist) value)
533     (while (and tail (not found))
534       (let ((elt (car tail)))
535         (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
536           (setq found t value (if (consp elt) (cdr elt) default))))
537       (setq tail (cdr tail)))
538     value))
539
540 ;; The following two function use `compare-strings', which we don't
541 ;; support yet.
542 ;; (defun assoc-ignore-case (key alist))
543 ;; (defun assoc-ignore-representation (key alist))
544
545 ;; Emacs 19.29/XEmacs 19.14(?) and later: (rassoc KEY LIST)
546 ;; Actually, `rassoc' is defined in src/fns.c.
547 (defun-maybe rassoc (key list)
548   "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
549 The value is actually the element of LIST whose cdr equals KEY.
550 Elements of LIST that are not conses are ignored."
551   (catch 'found
552     (while list
553       (cond ((not (consp (car list))))
554             ((equal (cdr (car list)) key)
555              (throw 'found (car list))))
556       (setq list (cdr list)))))
557
558 ;;; @@ Hook manipulation functions.
559
560 ;; "localhook" package is written for Emacs 19.28 and earlier.
561 ;; `run-hooks' was a lisp function in Emacs 19.29 and earlier.
562 ;; So, in Emacs 19.29, `run-hooks' and others will be overrided.
563 ;; But, who cares it?
564 (static-unless (subrp (symbol-function 'run-hooks))
565   (require 'localhook))
566
567 ;; Emacs 19.29/XEmacs 19.14(?) and later: (add-to-list LIST-VAR ELEMENT)
568 (defun-maybe add-to-list (list-var element)
569   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
570 The test for presence of ELEMENT is done with `equal'.
571 If you want to use `add-to-list' on a variable that is not defined
572 until a certain package is loaded, you should put the call to `add-to-list'
573 into a hook function that will be run only after loading the package.
574 `eval-after-load' provides one way to do this.  In some cases
575 other hooks, such as major mode hooks, can do the job."
576   (or (member element (symbol-value list-var))
577       (set list-var (cons element (symbol-value list-var)))))
578
579 ;; (eval-after-load FILE FORM)
580 ;; Emacs 19.28 and earlier do not evaluate FORM if FILE is already loaded.
581 ;; XEmacs 20.2 and earlier have `after-load-alist', but refuse to support
582 ;; `eval-after-load'. (see comments in XEmacs/lisp/subr.el.)
583 (static-cond
584  ((featurep 'xemacs)
585   ;; for XEmacs 20.2 and earlier.
586   (defun-maybe eval-after-load (file form)
587     "Arrange that, if FILE is ever loaded, FORM will be run at that time.
588 This makes or adds to an entry on `after-load-alist'.
589 If FILE is already loaded, evaluate FORM right now.
590 It does nothing if FORM is already on the list for FILE.
591 FILE should be the name of a library, with no directory name."
592     ;; Make sure there is an element for FILE.
593     (or (assoc file after-load-alist)
594         (setq after-load-alist (cons (list file) after-load-alist)))
595     ;; Add FORM to the element if it isn't there.
596     (let ((elt (assoc file after-load-alist)))
597       (or (member form (cdr elt))
598           (progn
599             (nconc elt (list form))
600             ;; If the file has been loaded already, run FORM right away.
601             (and (assoc file load-history)
602                  (eval form)))))
603     form))
604  ((>= emacs-major-version 20))
605  ((and (= emacs-major-version 19)
606        (< emacs-minor-version 29))
607   ;; for Emacs 19.28 and earlier.
608   (defun eval-after-load (file form)
609     "Arrange that, if FILE is ever loaded, FORM will be run at that time.
610 This makes or adds to an entry on `after-load-alist'.
611 If FILE is already loaded, evaluate FORM right now.
612 It does nothing if FORM is already on the list for FILE.
613 FILE should be the name of a library, with no directory name."
614     ;; Make sure there is an element for FILE.
615     (or (assoc file after-load-alist)
616         (setq after-load-alist (cons (list file) after-load-alist)))
617     ;; Add FORM to the element if it isn't there.
618     (let ((elt (assoc file after-load-alist)))
619       (or (member form (cdr elt))
620           (progn
621             (nconc elt (list form))
622             ;; If the file has been loaded already, run FORM right away.
623             (and (assoc file load-history)
624                  (eval form)))))
625     form))
626  (t
627   ;; should emulate for v18?
628   ))
629
630 (defun-maybe eval-next-after-load (file)
631   "Read the following input sexp, and run it whenever FILE is loaded.
632 This makes or adds to an entry on `after-load-alist'.
633 FILE should be the name of a library, with no directory name."
634   (eval-after-load file (read)))
635
636 ;;; @@ Input and display facilities.
637
638 ;; XXX: (defun read-passwd (prompt &optional confirm default))
639
640 ;;; @@ Miscellanea.
641
642 ;; Avoid compiler warnings about this variable,
643 ;; which has a special meaning on certain system types.
644 (defvar-maybe buffer-file-type nil
645   "Non-nil if the visited file is a binary file.
646 This variable is meaningful on MS-DOG and Windows NT.
647 On those systems, it is automatically local in every buffer.
648 On other systems, this variable is normally always nil.")
649
650 ;; Emacs 20.1/XEmacs 20.3(?) and later: (save-current-buffer &rest BODY)
651 ;; v20 defines `save-current-buffer' as a C primitive (in src/editfns.c)
652 ;; and introduces a new bytecode Bsave_current_buffer(_1), replacing an
653 ;; obsolete bytecode Bread_char.  To make things worse, Emacs 20.1 and
654 ;; 20.2 have a bug that it will restore the current buffer without
655 ;; confirming that it is alive.
656 ;; This is a source of incompatibility of .elc between v18/v19 and v20.
657 ;; (XEmacs compiler takes care of it if compatibility mode is enabled.)
658 (defmacro-maybe save-current-buffer (&rest body)
659   "Save the current buffer; execute BODY; restore the current buffer.
660 Executes BODY just like `progn'."
661   (` (let ((orig-buffer (current-buffer)))
662        (unwind-protect
663            (progn (,@ body))
664          (if (buffer-live-p orig-buffer)
665              (set-buffer orig-buffer))))))
666
667 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-current-buffer BUFFER &rest BODY)
668 (defmacro-maybe with-current-buffer (buffer &rest body)
669   "Execute the forms in BODY with BUFFER as the current buffer.
670 The value returned is the value of the last form in BODY.
671 See also `with-temp-buffer'."
672   (` (save-current-buffer
673        (set-buffer (, buffer))
674        (,@ body))))
675
676 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-temp-file FILE &rest FORMS)
677 (defmacro-maybe with-temp-file (file &rest forms)
678   "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
679 The value of the last form in FORMS is returned, like `progn'.
680 See also `with-temp-buffer'."
681   (let ((temp-file (make-symbol "temp-file"))
682         (temp-buffer (make-symbol "temp-buffer")))
683     (` (let (((, temp-file) (, file))
684              ((, temp-buffer)
685               (get-buffer-create (generate-new-buffer-name " *temp file*"))))
686          (unwind-protect
687              (prog1
688                  (with-current-buffer (, temp-buffer)
689                    (,@ forms))
690                (with-current-buffer (, temp-buffer)
691                  (widen)
692                  (write-region (point-min) (point-max) (, temp-file) nil 0)))
693            (and (buffer-name (, temp-buffer))
694                 (kill-buffer (, temp-buffer))))))))
695
696 ;; Emacs 20.4 and later: (with-temp-message MESSAGE &rest BODY)
697 ;; This macro uses `current-message', which appears in v20.
698 (static-when (and (fboundp 'current-message)
699                   (subrp (symbol-function 'current-message)))
700   (defmacro-maybe with-temp-message (message &rest body)
701     "\
702 Display MESSAGE temporarily if non-nil while BODY is evaluated.
703 The original message is restored to the echo area after BODY has finished.
704 The value returned is the value of the last form in BODY.
705 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
706 If MESSAGE is nil, the echo area and message log buffer are unchanged.
707 Use a MESSAGE of \"\" to temporarily clear the echo area."
708     (let ((current-message (make-symbol "current-message"))
709           (temp-message (make-symbol "with-temp-message")))
710       (` (let (((, temp-message) (, message))
711                ((, current-message)))
712            (unwind-protect
713                (progn
714                  (when (, temp-message)
715                    (setq (, current-message) (current-message))
716                    (message "%s" (, temp-message))
717                    (,@ body))
718                  (and (, temp-message) (, current-message)
719                       (message "%s" (, current-message))))))))))
720
721 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-temp-buffer &rest FORMS)
722 (defmacro-maybe with-temp-buffer (&rest forms)
723   "Create a temporary buffer, and evaluate FORMS there like `progn'.
724 See also `with-temp-file' and `with-output-to-string'."
725   (let ((temp-buffer (make-symbol "temp-buffer")))
726     (` (let (((, temp-buffer)
727               (get-buffer-create (generate-new-buffer-name " *temp*"))))
728          (unwind-protect
729              (with-current-buffer (, temp-buffer)
730                (,@ forms))
731            (and (buffer-name (, temp-buffer))
732                 (kill-buffer (, temp-buffer))))))))
733
734 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-output-to-string &rest BODY)
735 (defmacro-maybe with-output-to-string (&rest body)
736   "Execute BODY, return the text it sent to `standard-output', as a string."
737   (` (let ((standard-output
738             (get-buffer-create (generate-new-buffer-name " *string-output*"))))
739        (let ((standard-output standard-output))
740          (,@ body))
741        (with-current-buffer standard-output
742          (prog1
743              (buffer-string)
744            (kill-buffer nil))))))
745
746 ;; Emacs 20.1 and later: (combine-after-change-calls &rest BODY)
747 (defmacro-maybe combine-after-change-calls (&rest body)
748   "Execute BODY, but don't call the after-change functions till the end.
749 If BODY makes changes in the buffer, they are recorded
750 and the functions on `after-change-functions' are called several times
751 when BODY is finished.
752 The return value is the value of the last form in BODY.
753
754 If `before-change-functions' is non-nil, then calls to the after-change
755 functions can't be deferred, so in that case this macro has no effect.
756
757 Do not alter `after-change-functions' or `before-change-functions'
758 in BODY.
759
760 This emulating macro does not support after-change functions at all,
761 just execute BODY."
762   (cons 'progn body))
763
764 ;; Emacs 19.29/XEmacs 19.14(?) and later: (match-string NUM &optional STRING)
765 (defun-maybe match-string (num &optional string)
766   "Return string of text matched by last search.
767 NUM specifies which parenthesized expression in the last regexp.
768  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
769 Zero means the entire text matched by the whole regexp or whole string.
770 STRING should be given if the last search was by `string-match' on STRING."
771   (if (match-beginning num)
772       (if string
773           (substring string (match-beginning num) (match-end num))
774         (buffer-substring (match-beginning num) (match-end num)))))
775
776 ;; Emacs 20.3 and later: (match-string-no-properties NUM &optional STRING)
777 (defun-maybe match-string-no-properties (num &optional string)
778   "Return string of text matched by last search, without text properties.
779 NUM specifies which parenthesized expression in the last regexp.
780  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
781 Zero means the entire text matched by the whole regexp or whole string.
782 STRING should be given if the last search was by `string-match' on STRING."
783   (if (match-beginning num)
784       (if string
785           (let ((result
786                  (substring string (match-beginning num) (match-end num))))
787             (set-text-properties 0 (length result) nil result)
788             result)
789         (buffer-substring-no-properties (match-beginning num)
790                                         (match-end num)))))
791
792 ;; Emacs 20.1/XEmacs 20.3(?) and later: (split-string STRING &optional PATTERN)
793 ;; Here is a XEmacs version.
794 (defun-maybe split-string (string &optional pattern)
795   "Return a list of substrings of STRING which are separated by PATTERN.
796 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
797   (or pattern
798       (setq pattern "[ \f\t\n\r\v]+"))
799   ;; The FSF version of this function takes care not to cons in case
800   ;; of infloop.  Maybe we should synch?
801   (let (parts (start 0))
802     (while (string-match pattern string start)
803       (setq parts (cons (substring string start (match-beginning 0)) parts)
804             start (match-end 0)))
805     (nreverse (cons (substring string start) parts))))
806
807 ;; Emacs 20.1/XEmacs 20.3 (but first appeared in Epoch?): (functionp OBJECT)
808 (defun-maybe functionp (object)
809   "Non-nil if OBJECT is a type of object that can be called as a function."
810   (or (subrp object) (byte-code-function-p object)
811       (eq (car-safe object) 'lambda)
812       (and (symbolp object) (fboundp object))))
813 \f
814
815 ;;; @ Window commands emulation. (lisp/window.el)
816 ;;;
817
818 (defmacro-maybe save-selected-window (&rest body)
819   "Execute BODY, then select the window that was selected before BODY."
820   (list 'let
821         '((save-selected-window-window (selected-window)))
822         (list 'unwind-protect
823               (cons 'progn body)
824               (list 'select-window 'save-selected-window-window))))
825 \f
826
827 ;;; @ Basic editing commands emulation. (lisp/simple.el)
828 ;;;
829 \f
830
831 ;;; @ File input and output commands emulation. (lisp/files.el)
832 ;;;
833
834 (defvar-maybe temporary-file-directory
835   (file-name-as-directory
836    (cond ((memq system-type '(ms-dos windows-nt))
837           (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
838          ((memq system-type '(vax-vms axp-vms))
839           (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
840          (t
841           (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
842   "The directory for writing temporary files.")
843
844 ;; Actually, `path-separator' is defined in src/emacs.c and overrided
845 ;; in dos-w32.el.
846 (defvar-maybe path-separator ":"
847   "The directory separator in search paths, as a string.")
848
849 ;; `convert-standard-filename' is defined in lisp/files.el and overrided
850 ;; in lisp/dos-fns.el and lisp/w32-fns.el for each environment.
851 (cond
852  ;; must be load-time check to share .elc between different systems.
853  ((fboundp 'convert-standard-filename))
854  ((memq system-type '(windows-nt ms-dos))
855   ;; should we do (require 'filename) at load-time ?
856   ;; (require 'filename)
857   ;; filename.el requires many modules, so we do not want to load at
858   ;; compile-time. instead, suppress warnings by this.
859   (eval-when-compile
860     (autoload 'filename-maybe-truncate-by-size "filename")
861     (autoload 'filename-special-filter "filename"))
862   (defun convert-standard-filename (filename)
863     "Convert a standard file's name to something suitable for the current OS.
864 This function's standard definition is trivial; it just returns the argument.
865 However, on some systems, the function is redefined
866 with a definition that really does change some file names.
867 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
868 `filename-limit-length' for the basic filename and each parent directory name."
869     (require 'filename)
870     (let* ((names (split-string filename "/"))
871            (drive-name (car names))
872            (filter (function
873                     (lambda (string)
874                       (filename-maybe-truncate-by-size
875                        (filename-special-filter string))))))
876       (cond
877        ((eq 1 (length names))
878         (funcall filter drive-name))
879        ((string-match "^[^/]:$" drive-name)
880         (concat drive-name "/" (mapconcat filter (cdr names) "/")))
881        (t
882         (mapconcat filter names "/"))))))
883  (t
884   (defun convert-standard-filename (filename)
885     "Convert a standard file's name to something suitable for the current OS.
886 This function's standard definition is trivial; it just returns the argument.
887 However, on some systems, the function is redefined
888 with a definition that really does change some file names.
889 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
890 `filename-limit-length' for the basic filename and each parent directory name."
891     filename)))
892
893 (static-cond
894  ((fboundp 'insert-file-contents-literally))
895  ((boundp 'file-name-handler-alist)
896   ;; Use `defun-maybe' to update `load-history'.
897   (defun-maybe insert-file-contents-literally (filename &optional visit
898                                                         beg end replace)
899     "Like `insert-file-contents', q.v., but only reads in the file.
900 A buffer may be modified in several ways after reading into the buffer due
901 to advanced Emacs features, such as file-name-handlers, format decoding,
902 find-file-hooks, etc.
903   This function ensures that none of these modifications will take place."
904     (let (file-name-handler-alist)
905       (insert-file-contents filename visit beg end replace))))
906  (t
907   (defalias 'insert-file-contents-literally 'insert-file-contents)))
908
909 (defun-maybe file-name-sans-extension (filename)
910   "Return FILENAME sans final \"extension\".
911 The extension, in a file name, is the part that follows the last `.'."
912   (save-match-data
913     (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
914           directory)
915       (if (string-match "\\.[^.]*\\'" file)
916           (if (setq directory (file-name-directory filename))
917               (expand-file-name (substring file 0 (match-beginning 0))
918                                 directory)
919             (substring file 0 (match-beginning 0)))
920         filename))))
921 \f
922
923 ;;; @ XEmacs emulation.
924 ;;;
925
926 (defun-maybe find-face (face-or-name)
927   "Retrieve the face of the given name.
928 If FACE-OR-NAME is a face object, it is simply returned.
929 Otherwise, FACE-OR-NAME should be a symbol.  If there is no such face,
930 nil is returned.  Otherwise the associated face object is returned."
931   (car (memq face-or-name (face-list))))
932
933 ;; Emacs 20.5 defines this as an alias for `line-beginning-position'.
934 ;; Therefore, optional 2nd arg BUFFER is not portable.
935 (defun-maybe point-at-bol (&optional n buffer)
936   "Return the character position of the first character on the current line.
937 With argument N not nil or 1, move forward N - 1 lines first.
938 If scan reaches end of buffer, return that position.
939 This function does not move point."
940   (save-excursion
941     (if buffer (set-buffer buffer))
942     (forward-line (1- (or n 1)))
943     (point)))
944
945 ;; Emacs 20.5 defines this as an alias for `line-end-position'.
946 ;; Therefore, optional 2nd arg BUFFER is not portable.
947 (defun-maybe point-at-eol (&optional n buffer)
948   "Return the character position of the last character on the current line.
949 With argument N not nil or 1, move forward N - 1 lines first.
950 If scan reaches end of buffer, return that position.
951 This function does not move point."
952   (save-excursion
953     (if buffer (set-buffer buffer))
954     (end-of-line (or n 1))
955     (point)))
956
957 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
958   "Define OLDFUN as an obsolete alias for function NEWFUN.
959 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
960 as obsolete."
961   (defalias oldfun newfun)
962   (make-obsolete oldfun newfun))
963
964 ;; XEmacs 21: (character-to-event CH &optional EVENT DEVICE)
965 (defun-maybe character-to-event (ch)
966   "Convert keystroke CH into an event structure, replete with bucky bits.
967 Note that CH (the keystroke specifier) can be an integer, a character
968 or a symbol such as 'clear."
969   ch)
970
971 ;; XEmacs 21: (event-to-character EVENT
972 ;;             &optional ALLOW-EXTRA-MODIFIERS ALLOW-META ALLOW-NON-ASCII)
973 (defun-maybe-cond event-to-character (event)
974   "Return the character approximation to the given event object.
975 If the event isn't a keypress, this returns nil."
976   ((and (fboundp 'read-event)
977         (subrp (symbol-function 'read-event)))
978    ;; Emacs 19 and later.
979    (cond
980     ((symbolp event)
981      ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
982      (let ((mask (get event 'event-symbol-element-mask)))
983        (if mask
984            (let ((base (get (car mask) 'ascii-character)))
985              (if base
986                  (logior base (car (cdr mask))))))))
987     ((integerp event) event)))
988   (t
989    ;; v18. Is this correct?
990    event))
991
992 ;; v18: no event; (read-char)
993 ;; Emacs 19, 20.1 and 20.2: (read-event)
994 ;; Emacs 20.3: (read-event &optional PROMPT SUPPRESS-INPUT-METHOD)
995 ;; Emacs 20.4: (read-event &optional PROMPT INHERIT-INPUT-METHOD)
996 ;; XEmacs: (next-event &optional EVENT PROMPT),
997 ;;         (next-command-event &optional EVENT PROMPT)
998 (defun-maybe-cond next-command-event (&optional event prompt)
999   "Read an event object from the input stream.
1000 If EVENT is non-nil, it should be an event object and will be filled
1001 in and returned; otherwise a new event object will be created and
1002 returned.
1003 If PROMPT is non-nil, it should be a string and will be displayed in
1004 the echo area while this function is waiting for an event."
1005   ((and (>= emacs-major-version 20)
1006         (>= emacs-minor-version 4))
1007    ;; Emacs 20.4 and later.
1008    (read-event prompt))                 ; should specify 2nd arg?
1009   ((and (= emacs-major-version 20)
1010         (= emacs-minor-version 3))
1011    ;; Emacs 20.3.
1012    (read-event prompt))                 ; should specify 2nd arg?
1013   ((and (fboundp 'read-event)
1014         (subrp (symbol-function 'read-event)))
1015    ;; Emacs 19, 20.1 and 20.2.
1016    (if prompt (message prompt))
1017    (read-event))
1018   (t
1019    (if prompt (message prompt))
1020    (read-char)))
1021 \f
1022
1023 ;;; @ MULE 2 emulation.
1024 ;;;
1025
1026 (defun-maybe-cond cancel-undo-boundary ()
1027   "Cancel undo boundary."
1028   ((boundp 'buffer-undo-list)
1029    ;; for Emacs 19 and later.
1030    (if (and (consp buffer-undo-list)
1031             (null (car buffer-undo-list)))
1032        (setq buffer-undo-list (cdr buffer-undo-list)))))
1033 \f
1034
1035 ;;; @ End.
1036 ;;;
1037
1038 ;;; poe.el ends here