summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-20 21:36:33 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-20 21:36:33 -0700
commit6d8df2a98bae323078951e4d4268a30b4743e31e (patch)
treefeae27bbcc443512d1ade58098c71bb79baa7727 /lib.c
parenteb6b20a502f7c20b6a5cfc9155dcbf2cb06c895e (diff)
downloadtxr-6d8df2a98bae323078951e4d4268a30b4743e31e.tar.gz
txr-6d8df2a98bae323078951e4d4268a30b4743e31e.tar.bz2
txr-6d8df2a98bae323078951e4d4268a30b4743e31e.zip
Changes to the printing framework.
The print function now takes an optional boolean for pretty printing. The print method is also called with a third argument; hence structures can customize both standard printing and pretty printing. * lib.c (obj_print): Take pretty argument, and pass it down to obj_print_impl. This makes obj_pprint redundant. (obj_pprint): Function removed: it was identical to obj_print except for passing t down to obj_print_impl for the pretty argument. These two wrappers had started small and got bigger with identical changes done in parallel. (pprint): New function. (tostring, dump): Pass nil for pretty argument of obj_print. (tostringp): Use pprint instead of obj_pprint. * lib.h (obj_print): Declaration updated. (obj_pprint): Declaration removed. (print, pprint): Declared. * eval.c (prinl): Pass nil for pretty_p argument of obj_print. Do the stream defaulting here; obj_print doesn't do it. (pprinl): Pass t for pretty_p argument of obj_print, and do stream argument defaulting. (eval_init): Register print to new print function rather than directly to obj_print. Register pprint to new pprint function rather than obj_pprint. * hash.c (hash_print_op): Call obj_print_impl to print the :equal-based keyword, rather than obj_print. Pass down the pretty flag. All the other keywords are treated this way; this fixes an inconsistency. * match.c (dump_var): Call pprint instead of obj_pprint. * stream.c (formatv): Call obj_print, with a calculated pretty argument instead of switching between obj_pprint and obj_print. * struct.c (struct_inst_print): Except when in backward compatibility mode, call the object's print method in both pretty and regular printing mode, passing the mode as a third argument. * tests/012/oop.tl (defstruct animal): Support third argument in print method. Make it optional because there are some explicit calls which don't pass the argument. * txr.1: Documentation updated for print method and the print function. Revised text for some of the related functions. Added compat notes.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c51
1 files changed, 12 insertions, 39 deletions
diff --git a/lib.c b/lib.c
index 834685b4..f81b791a 100644
--- a/lib.c
+++ b/lib.c
@@ -9425,10 +9425,9 @@ tail:
}
}
-val obj_print(val obj, val stream)
+val obj_print(val obj, val out, val pretty)
{
val ret = nil;
- val out = default_arg(stream, std_output);
val save_mode = get_indent_mode(out);
val save_indent = get_indent(out);
struct strm_ctx *ctx_orig = get_ctx(out);
@@ -9448,7 +9447,7 @@ val obj_print(val obj, val stream)
}
}
- ret = obj_print_impl(obj, out, nil, ctx);
+ ret = obj_print_impl(obj, out, pretty, ctx);
uw_unwind {
set_indent_mode(out, save_mode);
@@ -9462,54 +9461,28 @@ val obj_print(val obj, val stream)
return ret;
}
-val obj_pprint(val obj, val stream)
+val print(val obj, val stream, val pretty)
{
- volatile val ret = nil;
- val out = default_arg(stream, std_output);
- val save_mode = get_indent_mode(out);
- val save_indent = get_indent(out);
- struct strm_ctx *ctx_orig = get_ctx(out);
- struct strm_ctx *volatile ctx = ctx_orig, ctx_struct;
-
- uw_simple_catch_begin;
-
- if (ctx) {
- populate_obj_hash(obj, ctx);
- } else {
- if (cdr(lookup_var(nil, print_circle_s))) {
- ctx = &ctx_struct;
- ctx->obj_hash = make_hash(nil, nil, nil);
- ctx->counter = zero;
- get_set_ctx(out, ctx);
- populate_obj_hash(obj, ctx);
- }
- }
-
- ret = obj_print_impl(obj, out, t, ctx);
-
- uw_unwind {
- set_indent_mode(out, save_mode);
- set_indent(out, save_indent);
- if (ctx != ctx_orig)
- get_set_ctx(out, ctx_orig);
- }
-
- uw_catch_end;
+ return obj_print(obj, default_arg(stream, std_output),
+ default_bool_arg(pretty));
+}
- return ret;
+val pprint(val obj, val stream)
+{
+ return obj_print(obj, default_arg(stream, std_output), t);
}
val tostring(val obj)
{
val ss = make_string_output_stream();
- obj_print(obj, ss);
+ obj_print(obj, ss, nil);
return get_string_from_stream(ss);
}
val tostringp(val obj)
{
val ss = make_string_output_stream();
- obj_pprint(obj, ss);
+ pprint(obj, ss);
return get_string_from_stream(ss);
}
@@ -10001,7 +9974,7 @@ int compat_fixup(int compat_ver)
void dump(val obj, val out)
{
- obj_print(obj, out);
+ obj_print(obj, out, nil);
put_char(chr('\n'), out);
}