all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: Returning variable "references" under lexical binding
Date: Wed, 22 May 2013 21:01:59 -0400	[thread overview]
Message-ID: <jwvehcyjzpm.fsf-monnier+gmane.emacs.help@gnu.org> (raw)
In-Reply-To: bp8k3msuf71.fsf@usca1uw-JZWWPM1.sanmateo.corp.akamai.com

> (defun run-periodically-when-idle (seconds func)
>   (let (timer count)
>     (labels ((on-first-idle ()
>                (when timer
>                  (cancel-timer timer))
>                (setq count 1)
>                (run-and-schedule-next))
>              (run-and-schedule-next ()
>                (setq timer
>                  (and (funcall func count)
>                       (run-with-idle-timer
>                        (* seconds (incf count))
>                        nil
>                        #'run-and-schedule-next)))))
>       (list (run-with-idle-timer seconds t #'on-first-idle)
>             (lambda () timer)))))

BTW, for the curious, the byte-compiler will turn the above code into
something similar to:

    (defun run-periodically-when-idle (seconds func)
      (let ((timer (list nil))
            (count (list nil)))
        (letrec ((on-first-idle
                  `(lambda ()
                     (when (car ',timer)
                       (cancel-timer (car ',timer)))
                     (setcar ',count 1)
                     (funcall ',run-and-schedule-next)))
                 (run-and-schedule-next
                  `(lambda ()
                     (setcar ',timer
                             (and (funcall ',func (car ',count))
                                  (run-with-idle-timer
                                   (* ',seconds (incf (car ',count)))
                                   nil
                                   ',run-and-schedule-next))))))
          (list (run-with-idle-timer seconds t on-first-idle)
                `(lambda () (car ',timer))))))
   
So if I were you I'd be tempted to use something like

   (defun run-periodically-when-idle (seconds func)
     (let ((timers (list nil))
           count)
       (labels ((on-first-idle ()
                  (when (cdr timers)
                    (cancel-timer (cdr timers)))
                  (setq count 1)
                  (run-and-schedule-next))
                (run-and-schedule-next ()
                  (setcdr timers
                    (and (funcall func count)
                         (run-with-idle-timer
                          (* seconds (incf count))
                          nil
                          #'run-and-schedule-next)))))
         (setcar timers (run-with-idle-timer seconds t #'on-first-idle))
         timers)))
   
   (defun cancel-periodic-idle-timer (timers)
     (cancel-timer (car timers))
     (aif (cdr timers)
       (cancel-timer it)))

BTW, as you have discovered there is often the need to do what your code
does, and in the packages that need it, they often use
a run-with-timer (i.e. a non-idle timer) for the repetition (which they
usually cancel in something like a pre-command-hook).

In jit-lock, the repetition is done with an idle timer (like here), but
instead of creating a new timer each time, the old timer is re-armed for
a different idle time.

I'm not very satisfied with either of the solutions.  It would be good
to add the functionality directly in timer.el (even if using one of the
existing techniques: at least clients could be later upgraded to a better
technique by improving timer.el).  IOW, I'd welcome a patch to timer.el
providing this feature.


        Stefan




  reply	other threads:[~2013-05-23  1:01 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-20 20:35 Returning variable "references" under lexical binding Sean McAfee
2013-05-21  1:18 ` Barry Margolin
2013-05-21  3:18 ` Stefan Monnier
     [not found] ` <mailman.112.1369106352.22516.help-gnu-emacs@gnu.org>
2013-05-21  5:39   ` Sean McAfee
2013-05-21 12:54     ` Stefan Monnier
2013-05-21 14:23     ` Barry Margolin
2013-05-21 16:38       ` Sean McAfee
2013-05-21 18:06         ` Barry Margolin
2013-05-21 22:43           ` Sean McAfee
2013-05-23  1:01             ` Stefan Monnier [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-05-21 14:41 Barry OReilly

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvehcyjzpm.fsf-monnier+gmane.emacs.help@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.