(use-calist-package): Add missing arg to `format'.
[elisp/apel.git] / poe-xemacs.el
1 ;;; poe-xemacs.el --- poe submodule for XEmacs
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
5
6 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Keywords: emulation, compatibility, 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 XEmacs; see the file COPYING.  If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'pym)
29
30
31 ;;; @ color
32 ;;;
33
34 (defun-maybe set-cursor-color (color-name)
35   "Set the text cursor color of the selected frame to COLOR.
36 When called interactively, prompt for the name of the color to use."
37   (interactive "sColor: ")
38   (set-frame-property (selected-frame) 'cursor-color
39                       (if (color-instance-p color-name)
40                           color-name
41                         (make-color-instance color-name))))
42
43
44 ;;; @ face
45 ;;;
46
47 (defalias-maybe 'face-list 'list-faces)
48
49 (or (memq 'underline (face-list))
50     (and (fboundp 'make-face)
51          (make-face 'underline)))
52
53 (or (face-differs-from-default-p 'underline)
54     (set-face-underline-p 'underline t))
55
56
57 ;;; @ overlay
58 ;;;
59
60 (condition-case nil
61     (require 'overlay)
62   (error
63    (defalias 'make-overlay 'make-extent)
64    (defalias 'overlayp 'extentp)
65    (defalias 'overlay-put 'set-extent-property)
66    (defalias 'overlay-buffer 'extent-buffer)
67    (defun move-overlay (extent start end &optional buffer)
68      (set-extent-endpoints extent start end))
69    (defalias 'delete-overlay 'detach-extent)))
70
71
72 ;;; @ dired
73 ;;;
74
75 (defun-maybe dired-other-frame (dirname &optional switches)
76   "\"Edit\" directory DIRNAME.  Like `dired' but makes a new frame."
77   (interactive (dired-read-dir-and-switches "in other frame "))
78   (switch-to-buffer-other-frame (dired-noselect dirname switches)))
79
80
81 ;;; @ timer
82 ;;;
83
84 (condition-case nil
85     (require 'timer)
86   (error
87    (require 'itimer)
88    (if (and (= emacs-major-version 19) (<= emacs-minor-version 14))
89        (defun-maybe run-at-time (time repeat function &rest args)
90          (start-itimer (make-temp-name "rat")
91                        `(lambda ()
92                           (,function ,@args))
93                        time repeat))
94      (defun-maybe run-at-time (time repeat function &rest args)
95        "Function emulating the function of the same name of Emacs.
96 TIME should be nil meaning now, or a number of seconds from now.
97 Return an itimer object which can be used in either `delete-itimer'
98 or `cancel-timer'."
99        (apply #'start-itimer "run-at-time"
100               function (if time (max time 1e-9) 1e-9)
101               repeat nil t args)))
102    (defalias 'cancel-timer 'delete-itimer)
103    (defun with-timeout-handler (tag)
104      (throw tag 'timeout))
105    (defmacro-maybe with-timeout (list &rest body)
106      (let ((seconds (car list))
107            (timeout-forms (cdr list)))
108        `(let ((with-timeout-tag (cons nil nil))
109               with-timeout-value with-timeout-timer)
110           (if (catch with-timeout-tag
111                 (progn
112                   (setq with-timeout-timer
113                         (run-at-time ,seconds nil
114                                      'with-timeout-handler
115                                      with-timeout-tag))
116                   (setq with-timeout-value (progn . ,body))
117                   nil))
118               (progn . ,timeout-forms)
119             (cancel-timer with-timeout-timer)
120             with-timeout-value))))))
121
122 (require 'broken)
123
124 (broken-facility run-at-time-tick-tock
125   "`run-at-time' is not punctual."
126   ;; Note that it doesn't support XEmacsen prior to the version 19.15
127   ;; since `start-itimer' doesn't pass arguments to a timer function.
128   (or (and (= emacs-major-version 19) (<= emacs-minor-version 14))
129       (condition-case nil
130           (progn
131             (unless (or itimer-process itimer-timer)
132               (itimer-driver-start))
133             ;; Check whether there is a bug to which the difference of
134             ;; the present time and the time when the itimer driver was
135             ;; woken up is subtracted from the initial itimer value.
136             (let* ((inhibit-quit t)
137                    (ctime (current-time))
138                    (itimer-timer-last-wakeup
139                     (prog1
140                         ctime
141                       (setcar ctime (1- (car ctime)))))
142                    (itimer-list nil)
143                    (itimer (start-itimer "run-at-time" 'ignore 5)))
144               (sleep-for 0.1) ;; Accept the timeout interrupt.
145               (prog1
146                   (> (itimer-value itimer) 0)
147                 (delete-itimer itimer))))
148         (error nil))))
149
150 (when-broken run-at-time-tick-tock
151   (defalias 'run-at-time
152     (lambda (time repeat function &rest args)
153       "Function emulating the function of the same name of Emacs.
154 It works correctly for TIME even if there is a bug in the XEmacs core.
155 TIME should be nil meaning now, or a number of seconds from now.
156 Return an itimer object which can be used in either `delete-itimer'
157 or `cancel-timer'."
158       (let ((itimers (list nil)))
159         (setcar
160          itimers
161          (apply #'start-itimer "fixed-run-at-time"
162                 (lambda (itimers repeat function &rest args)
163                   (let ((itimer (car itimers)))
164                     (if repeat
165                         (progn
166                           (set-itimer-function
167                            itimer
168                            (lambda (itimer repeat function &rest args)
169                              (set-itimer-restart itimer repeat)
170                              (set-itimer-function itimer function)
171                              (set-itimer-function-arguments itimer args)
172                              (apply function args)))
173                           (set-itimer-function-arguments
174                            itimer
175                            (append (list itimer repeat function) args)))
176                       (set-itimer-function
177                        itimer
178                        (lambda (itimer function &rest args)
179                          (delete-itimer itimer)
180                          (apply function args)))
181                       (set-itimer-function-arguments
182                        itimer
183                        (append (list itimer function) args)))))
184                 1e-9 (if time (max time 1e-9) 1e-9)
185                 nil t itimers repeat function args))))))
186
187
188 ;;; @ to avoid bug of XEmacs 19.14
189 ;;;
190
191 (or (string-match "^../"
192                   (file-relative-name "/usr/local/share" "/usr/local/lib"))
193     ;; This function was imported from Emacs 19.33.
194     (defun file-relative-name (filename &optional directory)
195       "Convert FILENAME to be relative to DIRECTORY
196 (default: default-directory)."
197       (setq filename (expand-file-name filename)
198             directory (file-name-as-directory
199                        (expand-file-name
200                         (or directory default-directory))))
201       (let ((ancestor ""))
202         (while (not (string-match (concat "^" (regexp-quote directory))
203                                   filename))
204           (setq directory (file-name-directory (substring directory 0 -1))
205                 ancestor (concat "../" ancestor)))
206         (concat ancestor (substring filename (match-end 0))))))
207
208
209 ;;; @ Emacs 20.3 emulation
210 ;;;
211
212 (defalias-maybe 'line-beginning-position 'point-at-bol)
213 (defalias-maybe 'line-end-position 'point-at-eol)
214
215 ;;; @ XEmacs 21 emulation
216 ;;;
217
218 ;; XEmacs 20.5 and later: (set-extent-properties EXTENT PLIST)
219 (defun-maybe set-extent-properties (extent plist)
220   "Change some properties of EXTENT.
221 PLIST is a property list.
222 For a list of built-in properties, see `set-extent-property'."
223   (while plist
224     (set-extent-property extent (car plist) (cadr plist))
225     (setq plist (cddr plist))))  
226
227 ;;; @ end
228 ;;;
229
230 (require 'product)
231 (product-provide (provide 'poe-xemacs) (require 'apel-ver))
232
233 ;;; poe-xemacs.el ends here