From 54b198af77449135980ce36fcfb42a5eea18c5c4 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 9 Feb 2016 13:36:15 +1100 Subject: [PATCH] Add a mode to list and cancel timers * doc/lispref/os.texi (Timers): Menton `timer-list'. * lisp/emacs-lisp/timer-list.el: New file. --- doc/lispref/os.texi | 6 ++ etc/NEWS | 4 ++ lisp/emacs-lisp/timer-list.el | 108 ++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 lisp/emacs-lisp/timer-list.el diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi index c5e3672a35a..d57cbcfeb94 100644 --- a/doc/lispref/os.texi +++ b/doc/lispref/os.texi @@ -1849,6 +1849,12 @@ one of these functions; the arrival of the specified time will not cause anything special to happen. @end defun +@findex timer-list +The @code{timer-list} command lists all the currently active timers. +There's only one command available in the buffer displayed: @kbd{c} +(@code{timer-list-cancel}) that will cancel the timer on the line +under point. + @node Idle Timers @section Idle Timers @cindex idle timers diff --git a/etc/NEWS b/etc/NEWS index fd56f0c317a..bcb70d4aeed 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -269,6 +269,10 @@ puny.el library, so that one can visit web sites like ** The new M-s M-w key binding uses eww to search the web for the text in the region. ++++ +** The new `timer-list' command lists all active timers in a buffer +where you can cancel them with the `c' command. + ** M-x suggests shorthands and ignores obsolete commands for completion. ** x-select-enable-clipboard is renamed select-enable-clipboard. x-select-enable-primary and renamed select-enable-primary. diff --git a/lisp/emacs-lisp/timer-list.el b/lisp/emacs-lisp/timer-list.el new file mode 100644 index 00000000000..b54902a45f8 --- /dev/null +++ b/lisp/emacs-lisp/timer-list.el @@ -0,0 +1,108 @@ +;;; timer-list.el --- list active timers in a buffer + +;; Copyright (C) 2016 Free Software Foundation, Inc. + +;; Maintainer: emacs-devel@gnu.org +;; Package: emacs + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;;; Code: + +;;;###autoload +(defun timer-list () + "List all timers in a buffer." + (interactive) + (pop-to-buffer-same-window (get-buffer-create "*timer-list*")) + (let ((inhibit-read-only t)) + (erase-buffer) + (timer-list-mode) + (dolist (timer (append timer-list timer-idle-list)) + (insert (format "%4s %10s %8s %s" + ;; Idle. + (if (aref timer 7) + "*" + " ") + ;; Next time. + (let ((time (float-time (list (aref timer 1) + (aref timer 2) + (aref timer 3))))) + (format "%.2f" + (if (aref timer 7) + time + (- (float-time (list (aref timer 1) + (aref timer 2) + (aref timer 3))) + (float-time))))) + ;; Repeat. + (let ((repeat (aref timer 4))) + (cond + ((numberp repeat) + (format "%.2f" (/ repeat 60))) + ((null repeat) + "-") + (t + (format "%s" repeat)))) + ;; Function. + (let ((function (aref timer 5))) + (replace-regexp-in-string + "\n" " " + (cond + ((byte-code-function-p function) + (replace-regexp-in-string + "[^-A-Za-z0-9 ]" "" + (format "%s" function))) + (t + (format "%s" function))))))) + (put-text-property (line-beginning-position) + (1+ (line-beginning-position)) + 'timer timer) + (insert "\n"))) + (goto-char (point-min))) + +(defvar timer-list-mode-map + (let ((map (make-sparse-keymap))) + (define-key map "c" 'timer-list-cancel) + (easy-menu-define nil map "" + '("Timers" + ["Cancel" timer-list-cancel t])) + map)) + +(define-derived-mode timer-list-mode special-mode "timer-list" + "Mode for listing and controlling timers." + (setq truncate-lines t) + (buffer-disable-undo) + (setq buffer-read-only t) + (setq header-line-format + (format "%4s %10s %8s %s" + "Idle" "Next" "Repeat" "Function"))) + +(defun timer-list-cancel () + "Cancel the timer on the line under point." + (interactive) + (let ((timer (get-text-property (line-beginning-position) 'timer)) + (inhibit-read-only t)) + (unless timer + (error "No timer on the current line")) + (cancel-timer timer) + (delete-region (line-beginning-position) + (line-beginning-position 2)))) + +(provide 'timer-list) + +;;; timer-list.el ends here -- 2.39.2