(file-name-sans-extension): New function <copied from Emacs 20.3.5>.
[elisp/apel.git] / poe.el
1 ;;; poe.el --- Portable Outfit for Emacsen; -*-byte-compile-dynamic: t;-*-
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, NEmacs, MULE, Emacs/mule, XEmacs
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This modules does not includes MULE related features.  MULE related
28 ;; features are supported by `poem'.
29
30 ;;; Code:
31
32 (defmacro defun-maybe (name &rest everything-else)
33   (or (and (fboundp name)
34            (not (get name 'defun-maybe)))
35       (` (or (fboundp (quote (, name)))
36              (progn
37                (defun (, name) (,@ everything-else))
38                (put (quote (, name)) 'defun-maybe t)
39                ))
40          )))
41
42 (defmacro defmacro-maybe (name &rest everything-else)
43   (or (and (fboundp name)
44            (not (get name 'defmacro-maybe)))
45       (` (or (fboundp (quote (, name)))
46              (progn
47                (defmacro (, name) (,@ everything-else))
48                (put (quote (, name)) 'defmacro-maybe t)
49                ))
50          )))
51
52 (defmacro-maybe defsubst (name arglist &rest body)
53   "Define an inline function.  The syntax is just like that of `defun'."
54   (cons 'defun (cons name (cons arglist body)))
55   )
56
57 (defmacro defsubst-maybe (name &rest everything-else)
58   (or (and (fboundp name)
59            (not (get name 'defsubst-maybe)))
60       (` (or (fboundp (quote (, name)))
61              (progn
62                (defsubst (, name) (,@ everything-else))
63                (put (quote (, name)) 'defsubst-maybe t)
64                ))
65          )))
66
67 (defmacro defalias-maybe (symbol definition)
68   (setq symbol (eval symbol))
69   (or (and (fboundp symbol)
70            (not (get symbol 'defalias-maybe)))
71       (` (or (fboundp (quote (, symbol)))
72              (progn
73                (defalias (quote (, symbol)) (, definition))
74                (put (quote (, symbol)) 'defalias-maybe t)
75                ))
76          )))
77
78 (put 'defun-maybe 'lisp-indent-function 'defun)
79 (put 'defsubst-maybe 'lisp-indent-function 'defun)
80 (put 'defmacro-maybe 'lisp-indent-function 'defun)
81
82 (defmacro defvar-maybe (name &rest everything-else)
83   (or (and (boundp name)
84            (not (get name 'defvar-maybe)))
85       (` (or (boundp (quote (, name)))
86              (progn
87                (defvar (, name) (,@ everything-else))
88                (put (quote (, name)) 'defvar-maybe t)
89                ))
90          )))
91
92 (defmacro defconst-maybe (name &rest everything-else)
93   (or (and (boundp name)
94            (not (get name 'defconst-maybe))
95            )
96       (` (or (boundp (quote (, name)))
97              (progn
98                (defconst (, name) (,@ everything-else))
99                (put (quote (, name)) 'defconst-maybe t)
100                ))
101          )))
102
103 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
104   (unless (stringp doc)
105     (setq everything-else (cons doc everything-else)
106           doc nil)
107     )
108   (or (and (fboundp name)
109            (not (get name 'defun-maybe)))
110       (` (unless (fboundp (quote (, name)))
111            (cond (,@ (mapcar (function
112                               (lambda (case)
113                                 (list (car case)
114                                       (if doc
115                                           (` (defun (, name) (, args)
116                                                (, doc)
117                                                (,@ (cdr case))))
118                                         (` (defun (, name) (, args)
119                                              (,@ (cdr case))))
120                                         ))))
121                              everything-else)))
122            (put (quote (, name)) 'defun-maybe t)
123            ))))
124
125 (defsubst subr-fboundp (symbol)
126   "Return t if SYMBOL's function definition is a built-in function."
127   (and (fboundp symbol)
128        (subrp (symbol-function symbol))))
129
130 (defconst-maybe emacs-major-version (string-to-int emacs-version))
131 (defconst-maybe emacs-minor-version
132   (string-to-int
133    (substring emacs-version
134               (string-match (format "%d\\." emacs-major-version)
135                             emacs-version))))
136
137 (cond ((featurep 'xemacs)
138        (require 'poe-xemacs)
139        )
140       ((string-match "XEmacs" emacs-version)
141        (provide 'xemacs)
142        (require 'poe-xemacs)
143        )
144       ((> emacs-major-version 20))
145       ((= emacs-major-version 20)
146        (cond ((subr-fboundp 'string)
147               ;; Emacs 20.3 or later
148               )
149              ((subr-fboundp 'concat-chars)
150               ;; Emacs 20.1 or later
151               (defalias 'string 'concat-chars)
152               ))
153        )
154       ((= emacs-major-version 19))
155       (t
156        (require 'poe-18)
157        ))
158
159
160 ;;; @ Emacs 19.23 emulation
161 ;;;
162
163 (defun-maybe minibuffer-prompt-width ()
164   "Return the display width of the minibuffer prompt."
165   (save-excursion
166     (set-buffer (window-buffer (minibuffer-window)))
167     (current-column)))
168
169
170 ;;; @ Emacs 19.29 emulation
171 ;;;
172
173 (defvar-maybe path-separator ":"
174   "Character used to separate concatenated paths.")
175
176 (defun-maybe buffer-substring-no-properties (start end)
177   "Return the characters of part of the buffer, without the text properties.
178 The two arguments START and END are character positions;
179 they can be in either order. [Emacs 19.29 emulating function]"
180   (let ((string (buffer-substring start end)))
181     (set-text-properties 0 (length string) nil string)
182     string))
183
184 (defun-maybe match-string (num &optional string)
185   "Return string of text matched by last search.
186 NUM specifies which parenthesized expression in the last regexp.
187  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
188 Zero means the entire text matched by the whole regexp or whole string.
189 STRING should be given if the last search was by `string-match' on STRING.
190 \[Emacs 19.29 emulating function]"
191   (if (match-beginning num)
192       (if string
193           (substring string (match-beginning num) (match-end num))
194         (buffer-substring (match-beginning num) (match-end num)))))
195
196 (or (featurep 'xemacs)
197     (>= emacs-major-version 20)
198     (and (= emacs-major-version 19)
199          (>= emacs-minor-version 29))
200     ;; for Emacs 19.28 or earlier
201     (fboundp 'si:read-string)
202     (eval-and-compile
203       (fset 'si:read-string (symbol-function 'read-string))
204       (defun read-string (prompt &optional initial-input history)
205         "Read a string from the minibuffer, prompting with string PROMPT.
206 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
207 The third arg HISTORY, is dummy for compatibility.
208 See `read-from-minibuffer' for details of HISTORY argument."
209         (si:read-string prompt initial-input))
210       ))
211
212 (defun-maybe rassoc (key list)
213   "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
214 The value is actually the element of LIST whose cdr equals KEY."
215   (catch 'found
216     (while list
217       (if (equal (cdr (car list)) key)
218           (throw 'found (car list))
219         )
220       (setq list (cdr list)))
221     ))
222
223 (defun-maybe file-name-sans-extension (filename)
224   "Return FILENAME sans final \"extension\".
225 The extension, in a file name, is the part that follows the last `.'."
226   (save-match-data
227     (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
228           directory)
229       (if (string-match "\\.[^.]*\\'" file)
230           (if (setq directory (file-name-directory filename))
231               (expand-file-name (substring file 0 (match-beginning 0))
232                                 directory)
233             (substring file 0 (match-beginning 0)))
234         filename))))
235
236 (defmacro-maybe make-local-hook (hook))
237
238 ;; They are not Emacs features
239
240 (defmacro-maybe add-local-hook (hook function &optional append)
241   (if (fboundp 'make-local-hook)
242       (list 'add-hook hook function append t)
243     (list 'add-hook hook function append)
244     ))
245
246 (defmacro-maybe remove-local-hook (hook function)
247   (if (fboundp 'make-local-hook)
248       (list 'remove-hook hook function t)
249     (list 'remove-hook hook function)
250     ))
251
252
253 ;;; @ Emacs 19.30 emulation
254 ;;;
255
256 ;; imported from Emacs 19.30.
257 (defun-maybe add-to-list (list-var element)
258   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
259 If you want to use `add-to-list' on a variable that is not defined
260 until a certain package is loaded, you should put the call to `add-to-list'
261 into a hook function that will be run only after loading the package.
262 \[Emacs 19.30 emulating function]"
263   (or (member element (symbol-value list-var))
264       (set list-var (cons element (symbol-value list-var)))))
265
266 (cond ((fboundp 'insert-file-contents-literally))
267       ((boundp 'file-name-handler-alist)
268        (defun insert-file-contents-literally
269          (filename &optional visit beg end replace)
270          "Like `insert-file-contents', q.v., but only reads in the file.
271 A buffer may be modified in several ways after reading into the buffer due
272 to advanced Emacs features, such as file-name-handlers, format decoding,
273 find-file-hooks, etc.
274   This function ensures that none of these modifications will take place.
275 \[Emacs 19.30 emulating function]"
276          (let (file-name-handler-alist)
277            (insert-file-contents filename visit beg end replace)))
278        )
279       (t
280        (defalias 'insert-file-contents-literally 'insert-file-contents)
281        ))
282
283
284 ;;; @ Emacs 19.31 emulation
285 ;;;
286
287 (defun-maybe buffer-live-p (object)
288   "Return non-nil if OBJECT is a buffer which has not been killed.
289 Value is nil if OBJECT is not a buffer or if it has been killed.
290 \[Emacs 19.31 emulating function]"
291   (and object
292        (get-buffer object)
293        (buffer-name (get-buffer object))))
294
295 ;; imported from Emacs 19.33.
296 (defmacro-maybe save-selected-window (&rest body)
297   "Execute BODY, then select the window that was selected before BODY.
298 \[Emacs 19.31 emulating function]"
299   (list 'let
300         '((save-selected-window-window (selected-window)))
301         (list 'unwind-protect
302               (cons 'progn body)
303               (list 'select-window 'save-selected-window-window))))
304
305
306 ;;; @ Emacs 20.1 emulation
307 ;;;
308
309 ;; imported from Emacs 20.2.
310 (defmacro-maybe when (cond &rest body)
311   "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
312   (list 'if cond (cons 'progn body)))
313
314 ;; imported from Emacs 20.3.
315 (defmacro-maybe unless (cond &rest body)
316   "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
317   (cons 'if (cons cond (cons nil body))))
318
319 ;; imported from Emacs 20.3.
320 (defun-maybe last (x &optional n)
321   "Return the last link of the list X.  Its car is the last element.
322 If X is nil, return nil.
323 If N is non-nil, return the Nth-to-last link of X.
324 If N is bigger than the length of X, return X."
325   (if n
326       (let ((m 0) (p x))
327         (while (consp p)
328           (setq m (1+ m) p (cdr p)))
329         (if (<= n 0) p
330           (if (< n m) (nthcdr (- m n) x) x)))
331     (while (cdr x)
332       (setq x (cdr x)))
333     x))
334
335 (defmacro-maybe save-current-buffer (&rest body)
336   "Save the current buffer; execute BODY; restore the current buffer.
337 Executes BODY just like `progn'."
338   (` (let ((orig-buffer (current-buffer)))
339        (unwind-protect
340            (progn (,@ body))
341          (set-buffer orig-buffer)))))
342
343 ;; imported from Emacs 20.2.
344 (defmacro-maybe with-current-buffer (buffer &rest body)
345   "Execute the forms in BODY with BUFFER as the current buffer.
346 The value returned is the value of the last form in BODY.
347 See also `with-temp-buffer'."
348   (` (save-current-buffer
349        (set-buffer (, buffer))
350        (,@ body))))
351
352 ;; imported from Emacs 20.2.
353 (defmacro-maybe with-temp-file (file &rest forms)
354   "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
355 The value of the last form in FORMS is returned, like `progn'.
356 See also `with-temp-buffer'."
357   (let ((temp-file (make-symbol "temp-file"))
358         (temp-buffer (make-symbol "temp-buffer")))
359     (` (let (((, temp-file) (, file))
360              ((, temp-buffer)
361               (get-buffer-create (generate-new-buffer-name " *temp file*"))))
362          (unwind-protect
363              (prog1
364                  (with-current-buffer (, temp-buffer)
365                    (,@ forms))
366                (with-current-buffer (, temp-buffer)
367                  (widen)
368                  (write-region (point-min) (point-max) (, temp-file) nil 0)))
369            (and (buffer-name (, temp-buffer))
370                 (kill-buffer (, temp-buffer))))))))
371
372 ;; imported from Emacs 20.2.
373 (defmacro-maybe with-temp-buffer (&rest forms)
374   "Create a temporary buffer, and evaluate FORMS there like `progn'.
375 See also `with-temp-file' and `with-output-to-string'."
376   (let ((temp-buffer (make-symbol "temp-buffer")))
377     (` (let (((, temp-buffer)
378               (get-buffer-create (generate-new-buffer-name " *temp*"))))
379          (unwind-protect
380              (with-current-buffer (, temp-buffer)
381                (,@ forms))
382            (and (buffer-name (, temp-buffer))
383                 (kill-buffer (, temp-buffer))))))))
384
385 (defmacro-maybe combine-after-change-calls (&rest body)
386   "Execute BODY."
387   (cons 'progn body))
388
389 ;; imported from Emacs 20.3. (cl function)
390 (defun-maybe butlast (x &optional n)
391   "Returns a copy of LIST with the last N elements removed."
392   (if (and n (<= n 0)) x
393     (nbutlast (copy-sequence x) n)))
394
395 ;; imported from Emacs 20.3. (cl function)
396 (defun-maybe nbutlast (x &optional n)
397   "Modifies LIST to remove the last N elements."
398   (let ((m (length x)))
399     (or n (setq n 1))
400     (and (< n m)
401          (progn
402            (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
403            x))))
404
405 ;; imported from XEmacs 21.
406 (defun-maybe split-string (string &optional pattern)
407   "Return a list of substrings of STRING which are separated by PATTERN.
408 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
409   (or pattern
410       (setq pattern "[ \f\t\n\r\v]+"))
411   ;; The FSF version of this function takes care not to cons in case
412   ;; of infloop.  Maybe we should synch?
413   (let (parts (start 0))
414     (while (string-match pattern string start)
415       (setq parts (cons (substring string start (match-beginning 0)) parts)
416             start (match-end 0)))
417     (nreverse (cons (substring string start) parts))))
418
419
420 ;;; @ Emacs 20.3 emulation
421 ;;;
422
423 ;; imported from Emacs 20.3.91.
424 (defvar-maybe temporary-file-directory
425   (file-name-as-directory
426    (cond ((memq system-type '(ms-dos windows-nt))
427           (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
428          ((memq system-type '(vax-vms axp-vms))
429           (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
430          (t
431           (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
432   "The directory for writing temporary files.")
433
434 (defun-maybe line-beginning-position (&optional n)
435   "Return the character position of the first character on the current line.
436 With argument N not nil or 1, move forward N - 1 lines first.
437 If scan reaches end of buffer, return that position.
438 This function does not move point."
439   (save-excursion
440     (if n
441         (forward-line (1- n))
442       )
443     (beginning-of-line)
444     (point)))
445
446 (defun-maybe line-end-position (&optional n)
447   "Return the character position of the last character on the current line.
448 With argument N not nil or 1, move forward N - 1 lines first.
449 If scan reaches end of buffer, return that position.
450 This function does not move point."
451   (save-excursion
452     (if n
453         (forward-line (1- n))
454       )
455     (end-of-line)
456     (point)))
457
458 (defun-maybe string (&rest chars)
459   "Concatenate all the argument characters and make the result a string."
460   (mapconcat (function char-to-string) chars "")
461   )
462
463     
464 ;;; @ XEmacs emulation
465 ;;;
466
467 (defun-maybe find-face (face-or-name)
468   "Retrieve the face of the given name.
469 If FACE-OR-NAME is a face object, it is simply returned.
470 Otherwise, FACE-OR-NAME should be a symbol.  If there is no such face,
471 nil is returned.  Otherwise the associated face object is returned.
472 \[XEmacs emulating function]"
473   (car (memq face-or-name (face-list)))
474   )
475
476 (defun-maybe point-at-bol (&optional n buffer)
477   "Return the character position of the first character on the current line.
478 With argument N not nil or 1, move forward N - 1 lines first.
479 If scan reaches end of buffer, return that position.
480 This function does not move point. [XEmacs emulating function]"
481   (save-excursion
482     (if buffer
483         (set-buffer buffer)
484       )
485     (line-beginning-position n)
486     ))
487
488 (defun-maybe point-at-eol (&optional n buffer)
489   "Return the character position of the last character on the current line.
490 With argument N not nil or 1, move forward N - 1 lines first.
491 If scan reaches end of buffer, return that position.
492 This function does not move point. [XEmacs emulating function]"
493   (save-excursion
494     (if buffer
495         (set-buffer buffer)
496       )
497     (line-end-position n)
498     ))
499
500 (defun-maybe functionp (obj)
501   "Returns t if OBJ is a function, nil otherwise.
502 \[XEmacs emulating function]"
503   (or (subrp obj)
504       (byte-code-function-p obj)
505       (and (symbolp obj)(fboundp obj))
506       (and (consp obj)(eq (car obj) 'lambda))
507       ))
508
509 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
510   "Define OLDFUN as an obsolete alias for function NEWFUN.
511 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
512 as obsolete. [XEmacs emulating function]"
513   (defalias oldfun newfun)
514   (make-obsolete oldfun newfun)
515   )
516
517 (when (subr-fboundp 'read-event)
518   ;; for Emacs 19 or later
519
520   (defun-maybe-cond next-command-event (&optional event prompt)
521     "Read an event object from the input stream.
522 If EVENT is non-nil, it should be an event object and will be filled
523 in and returned; otherwise a new event object will be created and
524 returned.
525 If PROMPT is non-nil, it should be a string and will be displayed in
526 the echo area while this function is waiting for an event.
527 \[XEmacs emulating function]"
528     ((subr-fboundp 'string)
529      ;; for Emacs 20.3 or later
530      (read-event prompt t)
531      )
532     (t
533      (if prompt (message prompt))
534      (read-event)
535      ))
536
537   (defsubst-maybe character-to-event (ch)
538     "Convert keystroke CH into an event structure, replete with bucky bits.
539 Note that CH (the keystroke specifier) can be an integer, a character
540 or a symbol such as 'clear. [XEmacs emulating function]"
541     ch)
542
543   (defun-maybe event-to-character (event)
544     "Return the character approximation to the given event object.
545 If the event isn't a keypress, this returns nil.
546 \[XEmacs emulating function]"
547     (cond ((symbolp event)
548            ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
549            (let ((mask (get event 'event-symbol-element-mask)))
550              (if mask
551                  (let ((base (get (car mask) 'ascii-character)))
552                    (if base
553                        (logior base (car (cdr mask)))
554                      )))))
555           ((integerp event) event)
556           ))
557   )
558
559
560 ;;; @ MULE 2 emulation
561 ;;;
562
563 (defun-maybe-cond cancel-undo-boundary ()
564   "Cancel undo boundary. [MULE 2.3 emulating function]"
565   ((boundp 'buffer-undo-list)
566    ;; for Emacs 19.7 or later
567    (if (and (consp buffer-undo-list)
568             ;; if car is nil.
569             (null (car buffer-undo-list)))
570        (setq buffer-undo-list (cdr buffer-undo-list))
571      ))
572   (t
573    ;; for anything older than Emacs 19.7.    
574    ))
575
576
577 ;;; @ end
578 ;;;
579
580 (provide 'poe)
581
582 ;;; poe.el ends here