1

Suppose I have a function that is 'noisy' and prints to standard output rudely.

(defun not-my-noisy-function ()
  "A function outside my control"
  (do-something-useful)
  (print "Haha! Butts!"))

I would like to be able to make calls to that function without corresponding messages in stdout. I was hoping that there was an elisp construct which would evaluate the form discarding standard-output from its body. A little bit like ignore-errors, but applicable to an output stream instead of thrown exceptions. I haven't been about to find anything in the vein of "suppress"/"silent"/"discard"/"redirect" for output streams.

(evaluate-silently ;; macro I wish existed
  (not-my-noisy-function))

My first thought of setting standard-output to nil doesn't seem to do the trick.

(let ((standard-output nil))
  (print "Nope; doesn't work.  You can still hear me."))

Interestingly the elisp manual says it must not be nil and doesn't say anything about values other than t, a buffer, or a marker. I can't seem to find documentation about an "real" streams in the elisp manual; only optional parameters to output functions which only seem to duplicate captured standard output rather redirecting it. with-output-to-string and with-output-to-temp-buffer also only seem to duplicate output to a buffer.

As a last recourse I could manually examine problematic functions and use cl-flet (to override print,message, etc), but I would rather not take that approach. Unfortunately this is the only technique that I've identified to accomplish this.

(cl-flet ((print (&rest body) nil))
  (not-my-noisy-function)
  (print "I am being ignored!?!!"))

Is there a better way? I hope I just haven't divined the correct apropos incantation.

Drew
  • 77,472
  • 10
  • 114
  • 243
ebpa
  • 7,449
  • 29
  • 55

1 Answers1

3

You were following a good line of enquiry regarding standard-output, but just didn't manage to find the right information.

The value of standard-output has to be a valid value for the PRINTCHARFUN argument, which C-hf print describes nicely:

Optional argument PRINTCHARFUN is the output stream, which can be one
of these:

   - a buffer, in which case output is inserted into that buffer at point;
   - a marker, in which case output is inserted at marker's position;
   - a function, in which case that function is called once for each
     character of OBJECT's printed representation;
   - a symbol, in which case that symbol's function definition is called; or
   - t, in which case the output is displayed in the echo area.

If PRINTCHARFUN is omitted, the value of `standard-output' (which see)
is used instead.

Hence you can simply bind standard-output to the canonical function for doing nothing:

(let ((standard-output 'ignore))
  (print "hello"))
phils
  • 50,977
  • 3
  • 79
  • 122