1;;; later-do.el --- execute lisp code ... later 2 3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 4;; 2009, 2018 Free Software Foundation, Inc. 5 6;; Author: Jorgen Schaefer <forcer@forcix.cx> 7 8;;; This program is free software; you can redistribute it and/or 9;;; modify it under the terms of the GNU General Public License 10;;; as published by the Free Software Foundation; either version 3 11;;; of the License, or (at your option) any later version. 12 13;;; This program is distributed in the hope that it will be useful, 14;;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16;;; GNU General Public License for more details. 17 18;;; You should have received a copy of the GNU General Public License 19;;; along with this program; if not, write to the Free Software 20;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 21;;; 02110-1301 USA 22 23;;; Commentary 24 25;; This file will execute lisp code ``later on''. This way it is 26;; possible to work while elisp does some longer calculations, if you 27;; can convert those calculations into a sequence of function calls. 28 29;;; Code: 30 31(defvar later-do-version "0.2emms4 (2018-04-07)" 32 "Version string of later-do.") 33 34(defgroup later-do nil 35 "*Running functions ... later!" 36 :prefix "later-do-" 37 :group 'development) 38 39(defcustom later-do-interval 0.5 40 "How many seconds to wait between running events." 41 :group 'later-do 42 :type 'number) 43 44(defcustom later-do-batch 20 45 "How many functions to process before waiting `later-do-interval'. 46The functions are processed from `later-do-list'. Must be 1 or 47greater. Too high a value might make Emacs slower while the 48list is being processed." 49 :group 'later-do 50 :type 'number) 51 52(defvar later-do-list nil 53 "A list of functions to be called later on.") 54 55(defvar later-do-timer nil 56 "The timer that later-do uses.") 57 58(defun later-do (function &rest args) 59 "Apply FUNCTION to ARGS later on. This is an unspecified 60amount of time after this call, and definitely not while lisp is 61still executing. Code added using `later-do' is guaranteed to be 62executed in the sequence it was added." 63 (setq later-do-list (nconc later-do-list 64 (list (cons function args)))) 65 (unless later-do-timer 66 (setq later-do-timer 67 (run-with-timer later-do-interval nil 'later-do-timer)))) 68 69(defun later-do-timer () 70 "Run the next element in `later-do-list', or do nothing if it's 71empty." 72 (if (null later-do-list) 73 (setq later-do-timer nil) 74 (let (res) 75 (unwind-protect 76 (dotimes (b (min later-do-batch (length later-do-list)) res) 77 (let ((fun (caar later-do-list)) 78 (args (cdar later-do-list))) 79 (setq later-do-list (cdr later-do-list)) 80 (setq res (apply fun args))))) 81 (setq later-do-timer (run-with-timer later-do-interval 82 nil 83 'later-do-timer))))) 84 85(provide 'later-do) 86;;; later-do.el ends here 87