help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Making sure I'm not checking email (or doing other things) too often


From: Marcin Borkowski
Subject: Making sure I'm not checking email (or doing other things) too often
Date: Sun, 14 Feb 2016 22:05:02 +0100
User-agent: mu4e 0.9.13; emacs 25.1.50.2

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]