* nnheaderxm.el (nnheader-xmas-run-at-time): Use a simple function
[gnus] / lisp / nnheaderxm.el
1 ;;; nnheaderxm.el --- making Gnus backends work under XEmacs
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU 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 (if (condition-case nil
31         (progn
32           (unless (or itimer-process itimer-timer)
33             (itimer-driver-start))
34           ;; Check whether there is a bug to which the difference of
35           ;; the present time and the time when the itimer driver was
36           ;; woken up is subtracted from the initial itimer value.
37           (let* ((inhibit-quit t)
38                  (ctime (current-time))
39                  (itimer-timer-last-wakeup
40                   (prog1
41                       ctime
42                     (setcar ctime (1- (car ctime)))))
43                  (itimer-list nil)
44                  (itimer (start-itimer "nnheader-run-at-time" 'ignore 5)))
45             (sleep-for 0.1) ;; Accept the timeout interrupt.
46             (prog1
47                 (> (itimer-value itimer) 0)
48               (delete-itimer itimer))))
49       (error nil))
50     (defun nnheader-xmas-run-at-time (time repeat function &rest args)
51       "Emulating function run as `run-at-time'.
52 TIME should be nil meaning now, or a number of seconds from now.
53 Return an itimer object which can be used in either `delete-itimer'
54 or `cancel-timer'."
55       (apply #'start-itimer "nnheader-run-at-time"
56              function (if time (max time 1e-9) 1e-9)
57              repeat nil t args))
58   (defun nnheader-xmas-run-at-time (time repeat function &rest args)
59     "Emulating function run as `run-at-time' in the right way.
60 TIME should be nil meaning now, or a number of seconds from now.
61 Return an itimer object which can be used in either `delete-itimer'
62 or `cancel-timer'."
63     (let ((itimers (list nil)))
64       (setcar
65        itimers
66        (apply #'start-itimer "nnheader-run-at-time"
67               (lambda (itimers repeat function &rest args)
68                 (let ((itimer (car itimers)))
69                   (if repeat
70                       (progn
71                         (set-itimer-function
72                          itimer
73                          (lambda (itimer repeat function &rest args)
74                            (set-itimer-restart itimer repeat)
75                            (set-itimer-function itimer function)
76                            (set-itimer-function-arguments itimer args)
77                            (apply function args)))
78                         (set-itimer-function-arguments
79                          itimer
80                          (append (list itimer repeat function) args)))
81                     (set-itimer-function
82                      itimer
83                      (lambda (itimer function &rest args)
84                        (delete-itimer itimer)
85                        (apply function args)))
86                     (set-itimer-function-arguments
87                      itimer
88                      (append (list itimer function) args)))))
89               1e-9 (if time (max time 1e-9) 1e-9)
90               nil t itimers repeat function args)))))
91
92 (defalias 'nnheader-run-at-time 'nnheader-xmas-run-at-time)
93 (defalias 'nnheader-cancel-timer 'delete-itimer)
94 (defalias 'nnheader-cancel-function-timers 'ignore)
95 (defalias 'nnheader-string-as-multibyte 'identity)
96
97 (provide 'nnheaderxm)
98
99 ;;; nnheaderxm.el ends here