Skip to content

Commit

Permalink
prevent accidental amalgamation of graphic token chars in output
Browse files Browse the repository at this point in the history
This addresses mthom#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.
  • Loading branch information
triska committed Dec 17, 2024
1 parent 7e22c12 commit 6175e33
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib/format.pl
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,22 @@
{ 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",
body_(Body, 0, 3, VNs).
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) }, !,
Expand All @@ -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 ).
Expand Down

0 comments on commit 6175e33

Please sign in to comment.