From 6175e334a5785f739ff0378d3251f83d97fd7992 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Tue, 17 Dec 2024 19:51:11 +0100 Subject: [PATCH 1/2] prevent accidental amalgamation of graphic token chars in output This addresses #2713. Many thanks to @notoria for reporting this excellent case! Example: ?- portray_clause(A = @). A= @ . true. At other positions the now inserted space is unnecessary, as in: ?- portray_clause((head:- @,b)). head :- @ , b. true. The toplevel has a similar issue: ?- C = # ; false. C = # |<-- cursor is here; redundant space after # There may be a way to solve this issue for all cases like this. --- src/lib/format.pl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/format.pl b/src/lib/format.pl index d1fe1f61d..5389c90dd 100644 --- a/src/lib/format.pl +++ b/src/lib/format.pl @@ -595,6 +595,14 @@ { write_term_to_chars(Lit, [quoted(true),variable_names(VNs),double_quotes(true)], Ls) }, seq(Ls). +literal_(Lit, VNs) --> + { phrase(literal(Lit, VNs), Ls) }, + seq(Ls), + ( { phrase((...,[Last]), Ls), char_type(Last, graphic_token) } -> + " " + ; "" + ). + portray_(Var, VNs) --> { var(Var) }, !, literal(Var, VNs). portray_((Head :- Body), VNs) --> !, literal(Head, VNs), " :-\n", @@ -602,7 +610,7 @@ portray_((Head --> Body), VNs) --> !, literal(Head, VNs), " -->\n", body_(Body, 0, 3, VNs). -portray_(Any, VNs) --> literal(Any, VNs). +portray_(Any, VNs) --> literal_(Any, VNs). body_(Var, C, I, VNs) --> { var(Var) }, !, @@ -627,7 +635,7 @@ body_(A, C1, C1, VNs), "\n", else_branch(B, I, VNs). body_(Goal, C, I, VNs) --> - indent_to(C, I), literal(Goal, VNs). + indent_to(C, I), literal_(Goal, VNs). % True iff Body has the shape ( If -> Then ; Else ). From 8dc80b815fa72da5dad86777447c374ef1350c20 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Tue, 17 Dec 2024 22:51:47 +0100 Subject: [PATCH 2/2] use round brackets for goals involving operators Examples: ?- portray_clause(((+) :- a=b,(-))). (+) :- a=b, (-). true. ?- portray_clause((a :- (b :- c))). a :- (b:-c). true. This addresses #2715. Currently, more round brackets than needed are emitted. Ideally, a better approach to solve the issue here and also in toplevel.pl is found in the future. --- src/lib/format.pl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/format.pl b/src/lib/format.pl index 5389c90dd..2ac4744b8 100644 --- a/src/lib/format.pl +++ b/src/lib/format.pl @@ -593,7 +593,15 @@ literal(Lit, VNs) --> { write_term_to_chars(Lit, [quoted(true),variable_names(VNs),double_quotes(true)], Ls) }, - seq(Ls). + ( { nonvar(Lit), + functor(Lit, F, A), + current_op(Pri, _, F), + ( A =:= 0 + ; Pri >= 1000 + ) } -> + "(", seq(Ls), ")" + ; seq(Ls) + ). literal_(Lit, VNs) --> { phrase(literal(Lit, VNs), Ls) },