6

I'm writing a lispy documentation engine with elisp and to make things easier, I'm trying to modify the following behavior

(prin1-to-string '(cons 'a 'b))
;; => "(cons (quote a) (quote b))"

to be

(prin1-to-string '(cons 'a 'b))
;; => "(cons 'a 'b)"

Is it possible to instruct prin1-to-string to reinstate reader macros like this? Reading through the C sources, I don't think it's possible since by the time the object has been read, we've already used the reader-macro to turn 'a into (quote a) and that ' is lost forever. Is my only hope regex-replace?

Drew
  • 75,699
  • 9
  • 109
  • 225
Sean Allred
  • 6,861
  • 16
  • 85

1 Answers1

6

Just bind print-quoted to a non-nil value around the call to prin1(-to-string).

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • 1
    Wow, thanks! Looks like this behavior is defined in `print.c:print_object`'s `Lisp_Cons` case. That function is getting too big :) – Sean Allred Mar 06 '17 at 03:34