Function: nrepl-make-eval-handler

nrepl-make-eval-handler is a byte-compiled function defined in nrepl-client.el.

Signature

(nrepl-make-eval-handler &key ON-VALUE ON-STDOUT ON-STDERR ON-DONE ON-NS ON-STATUS ON-EVAL-ERROR ON-CONTENT-TYPE ON-TRUNCATED)

Documentation

Build a callback for an nREPL eval-style response stream.

Returns a function of one argument (the decoded response dict). The handler is a transport-layer building block: it dispatches on the standard eval response slots and invokes whichever of the keyword sub-handlers has been provided. All UI concerns (namespace tracking, default error handling, need-input prompting, status messages) live in higher layers -- see cider-make-eval-handler for the editor wrapper.

Sub-handlers (ON-VALUE, ON-STDOUT, ON-STDERR, ON-DONE, ON-NS, ON-STATUS, ON-EVAL-ERROR, ON-CONTENT-TYPE and ON-TRUNCATED), all optional:

  :on-value called with VALUE when the response carries one.
  :on-stdout called with the OUT string for stdout chunks.
  :on-stderr called with the ERR string for stderr chunks.
  :on-done called with no arguments on the final "done" status.
  :on-ns called with NS whenever the response carries an ns
                    slot, regardless of which other slots are present.
  :on-status called with (STATUS RESPONSE) when the response
                    carries a status slot. STATUS is the list of
                    status flags; RESPONSE is the full response dict
                    so the handler can read sibling slots if needed.
  :on-eval-error called with no arguments on "eval-error" status.
  :on-content-type called with (BODY CONTENT-TYPE) when the response
                    carries a content-type slot. BODY has been
                    base64-decoded when the response indicates so.
  :on-truncated called with no arguments when the response is flagged
                    as truncated by nrepl.middleware.print.

Source Code

;; Defined in ~/.emacs.d/elpa/cider-20260822.1520/nrepl-client.el
(cl-defun nrepl-make-eval-handler (&key on-value on-stdout on-stderr on-done
                                        on-ns on-status
                                        on-eval-error on-content-type on-truncated)
  "Build a callback for an nREPL `eval'-style response stream.

Returns a function of one argument (the decoded response dict).  The
handler is a transport-layer building block: it dispatches on the
standard eval response slots and invokes whichever of the keyword
sub-handlers has been provided.  All UI concerns (namespace tracking,
default error handling, need-input prompting, status messages) live in
higher layers -- see `cider-make-eval-handler' for the editor wrapper.

Sub-handlers (ON-VALUE, ON-STDOUT, ON-STDERR, ON-DONE, ON-NS, ON-STATUS,
ON-EVAL-ERROR, ON-CONTENT-TYPE and ON-TRUNCATED), all optional:

  :on-value         called with VALUE when the response carries one.
  :on-stdout        called with the OUT string for stdout chunks.
  :on-stderr        called with the ERR string for stderr chunks.
  :on-done          called with no arguments on the final \"done\" status.
  :on-ns            called with NS whenever the response carries an `ns'
                    slot, regardless of which other slots are present.
  :on-status        called with (STATUS RESPONSE) when the response
                    carries a `status' slot.  STATUS is the list of
                    status flags; RESPONSE is the full response dict
                    so the handler can read sibling slots if needed.
  :on-eval-error    called with no arguments on \"eval-error\" status.
  :on-content-type  called with (BODY CONTENT-TYPE) when the response
                    carries a `content-type' slot.  BODY has been
                    base64-decoded when the response indicates so.
  :on-truncated     called with no arguments when the response is flagged
                    as truncated by `nrepl.middleware.print'."
  (lambda (response)
    (nrepl-dbind-response response (content-type content-transfer-encoding body
                                                 value ns out err status id)
      (when (and ns on-ns)
        (funcall on-ns ns))
      ;; The value/output slots are mutually exclusive within a single
      ;; message, so they stay in a `cond'.
      (cond ((and content-type on-content-type)
             (funcall on-content-type
                      (if (string= content-transfer-encoding "base64")
                          (base64-decode-string body)
                        body)
                      content-type))
            (value
             (when on-value (funcall on-value value)))
            (out
             (when on-stdout (funcall on-stdout out)))
            (err
             (when on-stderr (funcall on-stderr err))))
      ;; A `status' can accompany any of the slots above in the same
      ;; message -- e.g. jank sends `value' and `("done")' together -- so
      ;; it has to be handled independently rather than as a `cond' branch,
      ;; otherwise the prompt is never refreshed on such responses (#3869).
      (when status
        (when on-status (funcall on-status status response))
        (when (and on-truncated
                   (member "nrepl.middleware.print/truncated" status))
          (funcall on-truncated))
        (when (and on-eval-error (member "eval-error" status))
          (funcall on-eval-error))
        (when (member "done" status)
          (nrepl--mark-id-completed id)
          (when on-done (funcall on-done)))))))