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