From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Making sure I'm not checking email (or doing other things) too often Date: Mon, 15 Feb 2016 15:58:19 +0100 Message-ID: <87si0u10ms.fsf@mbork.pl> References: <87d1rz2ebl.fsf@mbork.pl> <87h9hayr31.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1455548341 326 80.91.229.3 (15 Feb 2016 14:59:01 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 15 Feb 2016 14:59:01 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Feb 15 15:58:53 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aVKc1-0000Y9-C0 for geh-help-gnu-emacs@m.gmane.org; Mon, 15 Feb 2016 15:58:53 +0100 Original-Received: from localhost ([::1]:33014 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVKc0-00053E-JS for geh-help-gnu-emacs@m.gmane.org; Mon, 15 Feb 2016 09:58:52 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43595) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVKbl-00052h-98 for help-gnu-emacs@gnu.org; Mon, 15 Feb 2016 09:58:41 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aVKbg-0005Eu-Ct for help-gnu-emacs@gnu.org; Mon, 15 Feb 2016 09:58:37 -0500 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:36800) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVKbg-0005EP-2e for help-gnu-emacs@gnu.org; Mon, 15 Feb 2016 09:58:32 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 610439D2003; Mon, 15 Feb 2016 15:58:29 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id G3WBhLoxaJhk; Mon, 15 Feb 2016 15:58:23 +0100 (CET) Original-Received: from localhost (unknown [109.232.24.28]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 1E3F69D2001; Mon, 15 Feb 2016 15:58:21 +0100 (CET) User-agent: mu4e 0.9.13; emacs 25.1.50.2 In-reply-to: <87h9hayr31.fsf@web.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2a01:5e00:2:52::8 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:109179 Archived-At: On 2016-02-15, at 15:40, Michael Heerdegen wrote: > Marcin Borkowski writes: > >> (defun not-too-often-add-guard (fun interval) >> "Add a not-too-often guard to FUN with INTERVAL. >> This means that if FUN gets called less than INTERVAL minutes >> after last call, the use is asked whether s?he really wants to >> run the command." >> (let* ((fun-name (symbol-name fun)) >> (nto-int-sym >> (intern (concat "not-too-often-interval-" fun-name))) >> (nto-last-time-sym >> (intern (concat "not-too-often-last-time-" fun-name))) >> (nto-guard-sym >> (intern (concat "not-too-often-guard-" fun-name)))) >> (set nto-int-sym interval) >> (set nto-last-time-sym 0) >> (fset nto-guard-sym (lambda (orig-fun &rest args) >> (let ((elapsed (time-to-seconds >> (time-subtract >> (current-time) >> (symbol-value nto-last-time-sym))))) >> (if (< elapsed >> (* 60 (symbol-value nto-int-sym))) >> (cond ((y-or-n-p >> (format "You called %s %s minutes ago. Are you sure you want to proceed? " >> fun-name (/ elapsed 60.0))) >> (set nto-last-time-sym (current-time)) >> (apply orig-fun args)) >> (t >> (keyboard-quit))) >> (set nto-last-time-sym (current-time)))))) >> (put nto-guard-sym 'function-documentation >> (format >> "Issue a warning if function `%s' is called less than %s minutes from last call." >> fun-name interval)) >> (advice-add fun :around nto-guard-sym))) >> >> Now I can say e.g. >> >> (not-too-often-add-guard 'mu4e-update-mail-and-index 15) >> >> and have Emacs warn me if I want to check for new email sooner than 15 >> minutes after last time. >> >> Opinions welcome. > > That looks very good! Thanks! (Alas, there's a nasty bug. But it'll be polished out.) > [ An alternative approach would be to make `not-too-often-add-guard' a > macro. This would avoid the need of using `symbol-value' which some > find not aesthetic, but that's more a matter of personal taste, and you > surely know that already. ] I did not even consider it, because what I want to do is to feed `not-too-often-add-guard' to mapc. The assumption is that the user will have an alist of command/time interval pairs. > FWIW, the functionality "let a command behave differently depending on > how much time since its last invocation has elapsed" is useful in other > contexts too. E.g. when you want to make a key to behave differently > when you hit it multiple times in fast succession. Nice, I didn't think about it. > Factoring out the greatest common divider would give you something like > this: > > --8<---------------cut here---------------start------------->8--- > -*- lexical-binding: t -*- > > (defun stopwatch () > "Return a fresh stopwatch. > This is a function accepting zero arguments that upon each call > will return the time difference from its last call in seconds. > When called the first time it will return nil." > (let ((last-invocation nil)) > (lambda () > (prog1 (and last-invocation > (time-to-seconds (time-subtract (current-time) last-invocation))) > (setq last-invocation (current-time)))))) > --8<---------------cut-here---------------end--------------->8--- > > With that, one could make your advice functions reference and use such a > stopwatch object (each an individual one, of course). Thanks, I'll probably make use of it, if you have nothing against (I plan to eventually put this on Melpa). > Regards, > > Michael. Best, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University