unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Making sure I'm not checking email (or doing other things) too often
@ 2016-02-14 21:05 Marcin Borkowski
  2016-02-15  1:29 ` Emanuel Berg
  2016-02-15 14:40 ` Michael Heerdegen
  0 siblings, 2 replies; 22+ messages in thread
From: Marcin Borkowski @ 2016-02-14 21:05 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

thanks to Michael Heerdegen, my code is now working, and even though it
is not finished, I'm quite happy/proud of it.  I will finish it within
a week or two and blog about it, but please let me show the current
state here, especially that I suspect a few people could find it handy.

The problem: I hit U (mu4e-update-mail-and-index) way too often.  I want
Emacs to help me fight this unproductive habit.

Also, I'm a mathematician, and I want to have a general solution to the
problem of launching certain commands too often.

So, here is my current solution.  It is not perfect, I will tinker with
it a bit more later.

--8<---------------cut here---------------start------------->8---
(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)))
--8<---------------cut here---------------end--------------->8---

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.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2016-03-06 14:22 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-14 21:05 Making sure I'm not checking email (or doing other things) too often Marcin Borkowski
2016-02-15  1:29 ` Emanuel Berg
2016-02-15 15:21   ` Marcin Borkowski
2016-02-15 16:32     ` tomas
2016-02-16  0:48       ` Emanuel Berg
2016-02-16  7:38         ` tomas
2016-02-17  2:38           ` Emanuel Berg
2016-02-16  0:37     ` Emanuel Berg
     [not found]   ` <mailman.4745.1455549737.843.help-gnu-emacs@gnu.org>
2016-02-15 21:10     ` Joost Kremers
2016-02-16  0:56       ` Emanuel Berg
2016-02-17 13:42       ` Rusi
2016-02-18  0:24         ` Emanuel Berg
2016-02-15 14:40 ` Michael Heerdegen
2016-02-15 14:58   ` Marcin Borkowski
2016-02-15 15:08     ` Michael Heerdegen
2016-02-15 15:18       ` Marcin Borkowski
2016-03-06 13:26   ` Marcin Borkowski
2016-03-06 13:36     ` Marcin Borkowski
2016-03-06 13:59       ` Michael Heerdegen
2016-03-06 13:39     ` Michael Heerdegen
2016-03-06 14:09       ` Marcin Borkowski
2016-03-06 14:22         ` Michael Heerdegen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).