0

I'd like to pretty-print an Elisp form with some arbitrary prefix string inserted at the beginning of each output line.

For example, given the following variable:

(setq var '((abc . 11)
            (def . 22)
            (ghi . 33)))

I'd like something like (pp-with-prefix var " ---> ") (or something like it) to be able to add the " ---> " prefix string (or anything) so I could get something like the following printed:

 ---> ((abc . 11)
 --->  (def . 22)
 --->  (ghi . 33))

instead of what (pp var) would normally print:

((abc . 11)
 (def . 22)
 (ghi . 33))
PRouleau
  • 744
  • 3
  • 10
  • 1
    Use `pp-to-string` to produce the string and then do string manipulation to do anything you want. The `s` string library will probably be useful. See https://github.com/magnar4s/s.el – NickD Sep 24 '21 at 19:38
  • @NickD That's exactly what I was looking for! Thanks. Please add this as the answer and I'll accept it as the answer. – PRouleau Sep 24 '21 at 19:41

1 Answers1

1

All that pp does is call pp-to-string on the object and then it calls princ to print the string. So you can use pp-to-string in your own function and then manipulate the resulting string any way you want, before printing it out.

The s string library might be useful for such manipulation. See https://github.com/magnars/s.el

NickD
  • 27,023
  • 3
  • 23
  • 42