cider

Jim Newton 2025-11-01T18:03:52.374109Z

I’m printing some text to stderr during a macro expansion, and I expected it might show up at the cursor when I evaluate code in cider which causes the macro to expand. But instead it is printed in the repl in RED. Is there a way to get it to show up somehow as text annotation at the site where the macro expansion occurs? Here is the code which prints the diagnostic message

(defn print-unreachable-warning [code-exprs]
  (binding [*out* *err*]
    (printf "Unreachable code: ")
    (if (sequential? code-exprs)
      (doseq [word (interpose " " (map str code-exprs))]
        (printf "%s" word))
      (printf "%s" code-exprs))
    (printf "\n")))

Jim Newton 2025-11-01T18:05:08.791669Z

it would be great for the message to print at the cursor or in the emacs mini-buffer when I press C-M-x

Jim Newton 2025-11-01T19:54:09.508809Z

anyway, shouldn’t C-M-x capture the stderr of the evaluation and display it in the minibuffer?

2025-11-18T12:41:06.310559Z

You may want to look into writing a clj-kondo hook for your macro. That way you'll get the red squigglies for common clojure dev setups.

Jim Newton 2025-11-06T12:32:31.823169Z

I agree that what is printed to stderr is not necessarily an error. I cannot throw an exception, because it is not an error, as you alluded to.

Jim Newton 2025-11-06T12:34:29.030379Z

I didn’t understand your suggestion. What do I need to do to get the squiggly message? Are you saying that I just need to print a line somewhere in my output, in my 100s of lines of output, that looks like something warning, /path/to/file:line:column — message I don’t know how to compute the line and column number. How can I compute the line and column of the macro being evaluated?

Jim Newton 2025-11-06T12:40:18.505979Z

Here’s what I tried.

(defmacro xyzzy [x]
  (print "something warning, /i/dont/know/the/path/to/file:0:0 -- message\n")
  `(list ~x))
         

(defn abc []
  (xyzzy 100))
when I evaluate the defun abc… It prints the message into the repl, but doesn’t display any squiggles

Jim Newton 2025-11-06T12:45:01.499959Z

also tried the following

(defmacro xyzzy [x]
  (binding [*out* *err*]
    (print "something warning, /i/dont/know/the/path/to/file:0:0 -- message\n"))
  `(list ~x))
         

(defn abc []
  (xyzzy 100))
it prints to stderr in red in the repl, but no squiggles in the editor buffer

oyakushev 2025-11-06T14:24:02.637589Z

At macroexpansion time you can use *file* and take meta from the the form you are macroexpanding, there will be line and column. Try an LLM, it should help you with that.

Jim Newton 2025-11-06T14:40:42.468479Z

ok, I can do that. but if I know the file name, do I simply need to print a string of that format?

oyakushev 2025-11-06T14:44:10.597409Z

Yep

Jim Newton 2025-11-06T14:46:24.350859Z

OK, I think I’m doing what you suggested. It doesn’t seem to work. The message is still being printed into the REPL, and not displayed at the evaluation point, and no squiggles. Any suggestions?

(defmacro xyzzy [x]
  ;; Desktop/algebra-1-grader
  (let [m (meta &form)]
    (printf "something warning, %s:%d:%d - message\n"
            *file* (:line m) (:column m) m))
  `(list ~x))
         

(defn abc []
  (xyzzy 100))

Jim Newton 2025-11-06T14:46:49.642139Z

when I evaluate the abc definition, the message is printed in the REPL, not in squiggles

oyakushev 2025-11-05T09:02:58.043149Z

Why would it display in the minibuffer or inline? We don't do that for any output. You are confusing this with the error message displayed in the minibuffer when exception is thrown.

Jim Newton 2025-11-05T15:59:16.152999Z

thanks for your comment. But what is the logic of this? If I evaluate an expression in an editor buffer which prints messages to stderr. The message is printed in the repl window, and nothing notifies me that an error message has been printed.

Jim Newton 2025-11-05T15:59:39.764129Z

It seems to me that error messages should be displayed at the place where I pressed C-M-x. right?

Jim Newton 2025-11-05T16:01:04.594819Z

The value returned is displayed in the editor buffer, right? why not also the error message?

Jim Newton 2025-11-05T16:04:13.389149Z

is there a way I could write my own cider evaluate function which wraps cider-evaluate-defun-at-point and not only displays the return value, but also displays the error message, or better yet, decorates the expression having been evaluated with squiggly underline whose hover text is the error message?

oyakushev 2025-11-05T17:33:43.755659Z

Writing to stderr does not strictly mean there was an error. For example, reflection warnings print to stderr but they don't prevent code from working. Stderr is just an output stream. If you want to convey that something erroneous happened, it is better to throw the actual exception.

oyakushev 2025-11-05T17:37:27.629639Z

I understand your issue that you want the stderr messages for your particular case to be more visible. After all, we do the same with aforementioned reflection warnings – we parse the output and present it with squigly lines.

oyakushev 2025-11-05T17:38:24.878709Z

Actually, you can use that.

oyakushev 2025-11-05T17:40:06.623389Z

oyakushev 2025-11-05T17:40:58.953369Z

If you format your warning in this way: anyword warning, /path/to/file:line:column - arbitrary text and keep it on the same line, the form will be squigly-highlighted with an on-hover warning