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