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