summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-04-05 06:53:24 -0700
committerKaz Kylheku <kaz@kylheku.com>2018-04-05 06:53:24 -0700
commite69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0 (patch)
tree8dceea3e5455d6c8e711cbdfc78525bc87257fc6
parent34efdb8b2e45eb6543a8fbaadf8867ee58870677 (diff)
downloadtxr-e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0.tar.gz
txr-e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0.tar.bz2
txr-e69a99cc4c183a98dd380fdaf47a5a1dcb5d68a0.zip
printer: improve object formatting.
There is an issue with the printer in that it produces output whereby objects continue on the same line after a multi-line object, e.g: (foo (foobly bar xyzzy quux) (oops same line)) rather than: (foo (foobly bar xyzzy quux) (oops same line)) There is a simple fix for this: set a flag to force a line break on the next width-check operation whenever an object has been broken into multiple lines. width-check can return a Boolean indication whether it generated a line break, and so aggregate object printing routines can tell whether their object has been broken into lines, and set the flag. * stream.h (struct strm_base): New member, force_break. (force_break): Declared. * stream.c (strm_base_init): Extent initializer to cover force_break flag. (put_string, put_char): Clear the force_break flag whenever we hit column zero. (width_check): If indent mode is on, and force_break is true, generate a break. Clear force_break. (force_break): New function. (stream_init): Register force-break intrinsic. * buf.c (buf_print): Set the force break flag if the buffer was broken into multiple lines. * hash.c (hash_print_op): Set the force break flag if the hash was broken into multiple lines. * lib.c (obj_print_impl): Same logic for lists. * struct.c (struct_inst_print): Same logic for structs. * tests/009/json.expected, tests/011/macros-2.expected, tests/012/struct.tl, tests/017/glob-zarray.expected: Update expected textual output to reflect new formatting.
-rw-r--r--buf.c7
-rw-r--r--hash.c8
-rw-r--r--lib.c14
-rw-r--r--stream.c20
-rw-r--r--stream.h2
-rw-r--r--struct.c6
-rw-r--r--tests/009/json.expected8
-rw-r--r--tests/011/macros-2.expected5
-rw-r--r--tests/012/struct.tl6
-rw-r--r--tests/017/glob-zarray.expected3
10 files changed, 62 insertions, 17 deletions
diff --git a/buf.c b/buf.c
index 3e1ed76a..efd2e4a6 100644
--- a/buf.c
+++ b/buf.c
@@ -622,6 +622,7 @@ val buf_print(val buf, val stream_in)
val save_mode = test_set_indent_mode(stream, num_fast(indent_off),
num_fast(indent_data));
val save_indent;
+ int fb = 0;
put_string(lit("#b'"), stream);
@@ -630,12 +631,16 @@ val buf_print(val buf, val stream_in)
while (len-- > 0) {
format(stream, lit("~,02x"), num_fast(*data++), nao);
if ((++count & 7) == 0 && len)
- width_check(stream, chr(' '));
+ if (width_check(stream, chr(' ')))
+ fb = 1;
}
set_indent(stream, save_indent);
set_indent_mode(stream, save_mode);
+ if (fb)
+ force_break(stream);
+
return put_char(chr('\''), stream);
}
diff --git a/hash.c b/hash.c
index cc4c36ca..cc1b161f 100644
--- a/hash.c
+++ b/hash.c
@@ -408,7 +408,7 @@ static cnum hash_hash_op(val obj, int *count)
static void hash_print_op(val hash, val out, val pretty, struct strm_ctx *ctx)
{
struct hash *h = coerce(struct hash *, hash->co.handle);
- int need_space = 0;
+ int need_space = 0, force_br = 0;
val save_mode = test_set_indent_mode(out, num_fast(indent_off),
num_fast(indent_data));
val save_indent;
@@ -461,7 +461,8 @@ static void hash_print_op(val hash, val out, val pretty, struct strm_ctx *ctx)
while ((cell = hash_next(iter))) {
val key = car(cell);
val value = cdr(cell);
- width_check(out, chr(' '));
+ if (width_check(out, chr(' ')))
+ force_br = 1;
put_string(lit("("), out);
obj_print_impl(key, out, pretty, ctx);
@@ -476,6 +477,9 @@ static void hash_print_op(val hash, val out, val pretty, struct strm_ctx *ctx)
}
put_string(lit(")"), out);
+ if (force_br)
+ force_break(out);
+
set_indent_mode(out, save_mode);
set_indent(out, save_indent);
}
diff --git a/lib.c b/lib.c
index cf800c52..f2a62d11 100644
--- a/lib.c
+++ b/lib.c
@@ -11109,6 +11109,7 @@ val obj_print_impl(val obj, val out, val pretty, struct strm_ctx *ctx)
val iter;
val closepar = chr(')');
val indent = zero;
+ int force_br = 0;
if (sym == dwim_s && consp(cdr(obj))) {
put_char(chr('['), out);
@@ -11181,7 +11182,8 @@ finish:
iter = nil;
goto dot;
} else if (consp(d)) {
- width_check(out, chr(' '));
+ if (width_check(out, chr(' ')))
+ force_br = 1;
} else {
dot:
put_string(lit(" . "), out);
@@ -11189,6 +11191,9 @@ dot:
put_char(closepar, out);
}
}
+
+ if (force_br)
+ force_break(out);
}
if (save_indent)
@@ -11299,6 +11304,7 @@ dot:
val save_mode = test_set_indent_mode(out, num_fast(indent_off),
num_fast(indent_data));
val save_indent;
+ int force_br = 0;
put_string(lit("#("), out);
@@ -11308,11 +11314,15 @@ dot:
val elem = obj->v.vec[i];
obj_print_impl(elem, out, pretty, ctx);
if (i < length - 1)
- width_check(out, chr(' '));
+ if (width_check(out, chr(' ')))
+ force_br = 1;
}
put_char(chr(')'), out);
+ if (force_br)
+ force_break(out);
+
set_indent(out, save_indent);
set_indent_mode(out, save_mode);
}
diff --git a/stream.c b/stream.c
index 56563c0c..3887e77f 100644
--- a/stream.c
+++ b/stream.c
@@ -103,7 +103,7 @@ val shell, shell_arg;
void strm_base_init(struct strm_base *s)
{
- static struct strm_base init = { indent_off, 60, 10, 0, 0, 0 };
+ static struct strm_base init = { indent_off, 60, 10, 0, 0, 0, 0 };
*s = init;
}
@@ -3512,6 +3512,7 @@ val put_string(val string, val stream_in)
switch (*p) {
case '\n':
col = 0;
+ s->force_break = 0;
break;
case '\t':
col = (col + 1) | 7;
@@ -3540,6 +3541,7 @@ val put_char(val ch, val stream_in)
case L'\n':
ops->put_char(stream, ch);
s->column = 0;
+ s->force_break = 0;
break;
case L'\t':
if (s->column == 0 && s->indent_mode != indent_off) {
@@ -3697,14 +3699,25 @@ val width_check(val stream, val alt)
if ((s->indent_mode == indent_code &&
s->column >= s->indent_chars + s->code_width) ||
(s->indent_mode == indent_data &&
- s->column >= s->indent_chars + s->data_width))
+ s->column >= s->indent_chars + s->data_width) ||
+ (s->indent_mode != indent_off && s->force_break))
{
put_char(chr('\n'), stream);
+ s->force_break = 0;
+ return t;
} else if (alt) {
put_char(alt, stream);
}
- return t;
+ return nil;
+}
+
+val force_break(val stream)
+{
+ struct strm_base *s = coerce(struct strm_base *,
+ cobj_handle(stream, stream_s));
+ s->force_break = 1;
+ return stream;
}
struct strm_ctx *get_set_ctx(val stream, struct strm_ctx *ctx)
@@ -4596,6 +4609,7 @@ void stream_init(void)
reg_fun(intern(lit("set-indent"), user_package), func_n2(set_indent));
reg_fun(intern(lit("inc-indent"), user_package), func_n2(inc_indent));
reg_fun(intern(lit("width-check"), user_package), func_n2(width_check));
+ reg_fun(intern(lit("force-break"), user_package), func_n1(force_break));
reg_varl(intern(lit("indent-off"), user_package), num_fast(indent_off));
reg_varl(intern(lit("indent-data"), user_package), num_fast(indent_data));
reg_varl(intern(lit("indent-code"), user_package), num_fast(indent_code));
diff --git a/stream.h b/stream.h
index bb581371..12750639 100644
--- a/stream.h
+++ b/stream.h
@@ -49,6 +49,7 @@ struct strm_base {
cnum code_width;
cnum indent_chars;
cnum column;
+ unsigned force_break;
struct strm_ctx *ctx;
};
@@ -209,6 +210,7 @@ val get_indent(val stream);
val set_indent(val stream, val indent);
val inc_indent(val stream, val delta);
val width_check(val stream, val alt);
+val force_break(val stream);
struct strm_ctx *get_set_ctx(val stream, struct strm_ctx *);
struct strm_ctx *get_ctx(val stream);
val get_string(val stream, val nchars, val close_after_p);
diff --git a/struct.c b/struct.c
index 5f614af2..943cc393 100644
--- a/struct.c
+++ b/struct.c
@@ -1410,6 +1410,7 @@ static void struct_inst_print(val obj, val out, val pretty,
val save_mode = test_set_indent_mode(out, num_fast(indent_off),
num_fast(indent_data));
val save_indent, iter, once;
+ int force_br = 0;
int compat = opt_compat && opt_compat <= 154;
if (!compat || pretty) {
@@ -1433,7 +1434,8 @@ static void struct_inst_print(val obj, val out, val pretty,
put_char(chr(' '), out);
once = nil;
} else {
- width_check(out, chr(' '));
+ if (width_check(out, chr(' ')))
+ force_br = 1;
}
obj_print_impl(sym, out, pretty, ctx);
put_char(chr(' '), out);
@@ -1441,6 +1443,8 @@ static void struct_inst_print(val obj, val out, val pretty,
}
}
put_char(chr(')'), out);
+ if (force_br)
+ force_break(out);
set_indent_mode(out, save_mode);
set_indent(out, save_indent);
}
diff --git a/tests/009/json.expected b/tests/009/json.expected
index 8bc4ab9a..8c3fc4c6 100644
--- a/tests/009/json.expected
+++ b/tests/009/json.expected
@@ -19,8 +19,9 @@ AST: #H(() ("web-app" #H(() ("servlet-mapping" #H(() ("cofaxTools" "/tools/*") (
("dataStoreUrl" "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon")
("useDataStore" :true) ("cachePackageTagsTrack" 200.0) ("searchEngineFileTemplate" "forSearchEngines.htm")
("cachePackageTagsStore" 200.0) ("cachePagesRefresh" 10.0)))
- ("servlet-name" "cofaxCDS")) #H(() ("servlet-class" "org.cofax.cds.EmailServlet") ("init-param" #H(() ("mailHost" "mail1") ("mailHostOverride" "mail2")))
- ("servlet-name" "cofaxEmail"))
+ ("servlet-name" "cofaxCDS"))
+ #H(() ("servlet-class" "org.cofax.cds.EmailServlet") ("init-param" #H(() ("mailHost" "mail1") ("mailHostOverride" "mail2")))
+ ("servlet-name" "cofaxEmail"))
#H(() ("servlet-class" "org.cofax.cds.AdminServlet") ("servlet-name" "cofaxAdmin"))
#H(() ("servlet-class" "org.cofax.cds.FileServlet") ("servlet-name" "fileServlet"))
#H(() ("servlet-class" "org.cofax.cms.CofaxToolsServlet") ("init-param" #H(() ("lookInContext" 1.0) ("removePageCache" "/content/admin/remove?cache=pages&id=")
@@ -29,7 +30,8 @@ AST: #H(() ("web-app" #H(() ("servlet-mapping" #H(() ("cofaxTools" "/tools/*") (
("log" 1.0) ("adminGroupID" 4.0) ("templatePath" "toolstemplates/")
("betaServer" :true) ("dataLogLocation" "/usr/local/tomcat/logs/dataLog.log")
("fileTransferFolder" "/usr/local/tomcat/webapps/content/fileTransferFolder")))
- ("servlet-name" "cofaxTools")))) ("taglib" #H(() ("taglib-uri" "cofax.tld") ("taglib-location" "/WEB-INF/tlds/cofax.tld"))))))
+ ("servlet-name" "cofaxTools"))))
+ ("taglib" #H(() ("taglib-uri" "cofax.tld") ("taglib-location" "/WEB-INF/tlds/cofax.tld"))))))
Unmatched junk: ""
diff --git a/tests/011/macros-2.expected b/tests/011/macros-2.expected
index 7dad1247..0e4cb622 100644
--- a/tests/011/macros-2.expected
+++ b/tests/011/macros-2.expected
@@ -14,8 +14,9 @@
((< i 100) ())
() (block #:cnt-blk-0001
(if (< (sys:setq i (succ i))
- 20) (return-from
- #:cnt-blk-0001))
+ 20)
+ (return-from
+ #:cnt-blk-0001))
(if (> i 30)
(return-from
#:brk-blk-0002))
diff --git a/tests/012/struct.tl b/tests/012/struct.tl
index 7b0b7fd8..99b2a3eb 100644
--- a/tests/012/struct.tl
+++ b/tests/012/struct.tl
@@ -50,11 +50,13 @@
(set *gensym-counter* 0)
(stest (ignwarn (sys:expand 's.(a).d))
"(slot (call (slot s 'a)\n \
- \ s) 'd)")
+ \ s)\n \
+ \ 'd)")
(set *gensym-counter* 0)
(stest (ignwarn (sys:expand 's.(a b c).d))
"(slot (call (slot s 'a)\n \
- \ s b c)\n 'd)")
+ \ s b c)\n \
+ \ 'd)")
(test s.a 100)
diff --git a/tests/017/glob-zarray.expected b/tests/017/glob-zarray.expected
index 4bfc0810..cd54b421 100644
--- a/tests/017/glob-zarray.expected
+++ b/tests/017/glob-zarray.expected
@@ -1,3 +1,4 @@
0
#S(glob-t pathc 4 pathv #("tests/001/query-1.txr" "tests/001/query-2.txr" "tests/001/query-3.txr"
- "tests/001/query-4.txr") reserve 0)
+ "tests/001/query-4.txr")
+ reserve 0)