In wakatime-mode, there are a bunch of process calls that is done by the wakatime-call function. It calls a predefined shell command, so no real magic inside.

Now I’d like to make it a bit more error prone by adding a retry count. However, as this is done by a process sentinel, I have to create a lambda for this. A process sentinel takes two parameters, process and signal.

Question is, is it possible to somehow pass the value of a local (ie. function parameter) variable to a lambda for use, thus converting the lambda into a clojure? I’d prefer to go without moving to lexical binding.

Backquote is not an option (or is it?), as the variable I want to reference is not in the function’s argument list.

Here is the function:

(defun wakatime-call (savep &optional retrying)
  "Call WakaTime command."
    ((command (wakatime-client-command savep t))
     (process-environment (if wakatime-python-path
                              (cons (format "PYTHONPATH=%s"
     (process (start-process
               (generate-new-buffer " *WakaTime messages*")

    (set-process-sentinel process
                          `(lambda (process signal)
                             (when (memq (process-status process) '(exit signal))
                               (kill-buffer (process-buffer process))
                               (let ((exit-status (process-exit-status process)))
                                 (when (and (not (= 0 exit-status)) (not (= 102 exit-status)))
                                   (error "WakaTime Error (%s)" exit-status))
                                 (when (= 102 exit-status)
                                        ; If we are retrying already, error out
                                   (if retrying
                                       (error "WakaTime Error (%s)" exit-status)
                                        ; otherwise, ask for an API key and call ourselves
                                        ; recursively
                                     (wakatime-call savep t)))))))

    (set-process-query-on-exit-flag process nil)))

The goal is to access savep and retrying in the process sentinel’s lambda/closure.

  • There are several duplicate or near-duplicate questions. See 1 and 2, for instance. You need to either (a) use lexical binding or (b) insert the value of the argument, not just its name (symbol), into the lambda form.
    – Drew
    Commented Sep 30, 2016 at 14:29
  • 1 seems not to be an option for me (or I just don’t understand this whole lambda/clojure thing correctly). I’m still looking into option 2. Commented Oct 3, 2016 at 10:33
  • n.b. "Clojure" is a programming language. You are referring to a "closure". Not the same thing (despite being pronounced the same way :)
    – phils
    Commented Oct 3, 2016 at 11:19
  • Please show the code (ideally a minimal example), rather than describing it.
    – phils
    Commented Oct 3, 2016 at 11:25
  • @phils: that’s a typo of mine, as I was just talking about Clojure with a friend minutes before. Commented Oct 3, 2016 at 11:30


Browse other questions tagged or ask your own question.