5824235423ac698d647b6419b8a767be155f7ade
[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 (or
85  (condition-case nil
86      (require 'timer-funcs)
87    (error nil))
88  (condition-case nil
89      (require 'timer)
90    (error nil))
91  (progn
92    (require 'itimer)
93    (if (and (= emacs-major-version 19) (<= emacs-minor-version 14))
94        (defun-maybe run-at-time (time repeat function &rest args)
95          (start-itimer (make-temp-name "rat")
96                        `(lambda ()
97                           (,function ,@args))
98                        time repeat))
99      (defun-maybe run-at-time (time repeat function &rest args)
100        "Function emulating the function of the same name of Emacs.
101 TIME should be nil meaning now, or a number of seconds from now.
102 Return an itimer object which can be used in either `delete-itimer'
103 or `cancel-timer'."
104        (apply #'start-itimer "run-at-time"
105               function (if time (max time 1e-9) 1e-9)
106               repeat nil t args)))
107    (defalias 'cancel-timer 'delete-itimer)
108    (defun with-timeout-handler (tag)
109      (throw tag 'timeout))
110    (defmacro-maybe with-timeout (list &rest body)
111      (let ((seconds (car list))
112            (timeout-forms (cdr list)))
113        `(let ((with-timeout-tag (cons nil nil))
114               with-timeout-value with-timeout-timer)
115           (if (catch with-timeout-tag
116                 (progn
117                   (setq with-timeout-timer
118                         (run-at-time ,seconds nil
119                                      'with-timeout-handler
120                                      with-timeout-tag))
121                   (setq with-timeout-value (progn . ,body))
122                   nil))
123               (progn . ,timeout-forms)
124             (cancel-timer with-timeout-timer)
125             with-timeout-value))))))
126
127 (require 'broken)
128
129 (broken-facility run-at-time-tick-tock
130   "`run-at-time' is not punctual."
131   ;; Note that it doesn't support XEmacsen prior to the version 19.15
132   ;; since `start-itimer' doesn't pass arguments to a timer function.
133   (or (and (= emacs-major-version 19) (<= emacs-minor-version 14))
134       (condition-case nil
135           (progn
136             (unless (or itimer-process itimer-timer)
137               (itimer-driver-start))
138             ;; Check whether there is a bug to which the difference of
139             ;; the present time and the time when the itimer driver was
140             ;; woken up is subtracted from the initial itimer value.
141             (let* ((inhibit-quit t)
142                    (ctime (current-time))
143                    (itimer-timer-last-wakeup
144                     (prog1
145                         ctime
146                       (setcar ctime (1- (car ctime)))))
147                    (itimer-list nil)
148                    (itimer (start-itimer "run-at-time" 'ignore 5)))
149               (sleep-for 0.1) ;; Accept the timeout interrupt.
150               (prog1
151                   (> (itimer-value itimer) 0)
152                 (delete-itimer itimer))))
153         (error nil))))
154
155 (when-broken run-at-time-tick-tock
156   (defalias 'run-at-time
157     (lambda (time repeat function &rest args)
158       "Function emulating the function of the same name of Emacs.
159 It works correctly for TIME even if there is a bug in the XEmacs core.
160 TIME should be nil meaning now, or a number of seconds from now.
161 Return an itimer object which can be used in either `delete-itimer'
162 or `cancel-timer'."
163       (let ((itimers (list nil)))
164         (setcar
165          itimers
166          (apply #'start-itimer "fixed-run-at-time"
167                 (lambda (itimers repeat function &rest args)
168                   (let ((itimer (car itimers)))
169                     (if repeat
170                         (progn
171                           (set-itimer-function
172                            itimer
173                            (lambda (itimer repeat function &rest args)
174                              (set-itimer-restart itimer repeat)
175                              (set-itimer-function itimer function)
176                              (set-itimer-function-arguments itimer args)
177                              (apply function args)))
178                           (set-itimer-function-arguments
179                            itimer
180                            (append (list itimer repeat function) args)))
181                       (set-itimer-function
182                        itimer
183                        (lambda (itimer function &rest args)
184                          (delete-itimer itimer)
185                          (apply function args)))
186                       (set-itimer-function-arguments
187                        itimer
188                        (append (list itimer function) args)))))
189                 1e-9 (if time (max time 1e-9) 1e-9)
190                 nil t itimers repeat function args))))))
191
192
193 ;;; @ to avoid bug of XEmacs 19.14
194 ;;;
195
196 (or (string-match "^../"
197                   (file-relative-name "/usr/local/share" "/usr/local/lib"))
198     ;; This function was imported from Emacs 19.33.
199     (defun file-relative-name (filename &optional directory)
200       "Convert FILENAME to be relative to DIRECTORY
201 (default: default-directory)."
202       (setq filename (expand-file-name filename)
203             directory (file-name-as-directory
204                        (expand-file-name
205                         (or directory default-directory))))
206       (let ((ancestor ""))
207         (while (not (string-match (concat "^" (regexp-quote directory))
208                                   filename))
209           (setq directory (file-name-directory (substring directory 0 -1))
210                 ancestor (concat "../" ancestor)))
211         (concat ancestor (substring filename (match-end 0))))))
212
213
214 ;;; @ Emacs 20.3 emulation
215 ;;;
216
217 (defalias-maybe 'line-beginning-position 'point-at-bol)
218 (defalias-maybe 'line-end-position 'point-at-eol)
219
220 ;;; @ XEmacs 21 emulation
221 ;;;
222
223 ;; XEmacs 20.5 and later: (set-extent-properties EXTENT PLIST)
224 (defun-maybe set-extent-properties (extent plist)
225   "Change some properties of EXTENT.
226 PLIST is a property list.
227 For a list of built-in properties, see `set-extent-property'."
228   (while plist
229     (set-extent-property extent (car plist) (cadr plist))
230     (setq plist (cddr plist))))  
231
232 ;;; @ end
233 ;;;
234
235 (require 'product)
236 (product-provide (provide 'poe-xemacs) (require 'apel-ver))
237
238 ;;; poe-xemacs.el ends here