Function: prin1-to-string

prin1-to-string is a function defined in print.c.

Signature

(prin1-to-string OBJECT &optional NOESCAPE OVERRIDES)

Documentation

Return a string containing the printed representation of OBJECT.

OBJECT can be any Lisp object. This function outputs quoting characters when necessary to make output that read can handle, whenever possible, unless the optional second argument NOESCAPE is non-nil. For complex objects, the behavior is controlled by print-level and print-length, which see.

OBJECT is any of the Lisp data types: a number, a string, a symbol, a list, a buffer, a window, a frame, etc.

See prin1 for the meaning of OVERRIDES.

A printed representation of an object is text which describes that object.

View in manual

Probably introduced at or before Emacs version 16.

Source Code

// Defined in /usr/src/emacs/src/print.c
{
  specpdl_ref count = SPECPDL_INDEX ();

  specbind (Qinhibit_modification_hooks, Qt);

  if (!NILP (overrides))
    print_bind_overrides (overrides);

  /* Save and restore this: we are altering a buffer
     but we don't want to deactivate the mark just for that.
     No need for specbind, since errors deactivate the mark.  */
  Lisp_Object save_deactivate_mark = Vdeactivate_mark;

  struct print_context pc = print_prepare (Vprin1_to_string_buffer);
  print (object, pc.printcharfun, NILP (noescape));
  /* Make Vprin1_to_string_buffer be the default buffer after print_finish */
  print_finish (&pc);

  struct buffer *previous = current_buffer;
  set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  object = Fbuffer_string ();
  if (SBYTES (object) == SCHARS (object))
    STRING_SET_UNIBYTE (object);

  /* Note that this won't make prepare_to_modify_buffer call
     ask-user-about-supersession-threat because this buffer
     does not visit a file.  */
  Ferase_buffer ();
  set_buffer_internal (previous);

  Vdeactivate_mark = save_deactivate_mark;

  return unbind_to (count, object);
}