View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org/projects/xpce/
    6    Copyright (c)  2011-2023, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9                              SWI-Prolog Solutions b.v.
   10    All rights reserved.
   11
   12    Redistribution and use in source and binary forms, with or without
   13    modification, are permitted provided that the following conditions
   14    are met:
   15
   16    1. Redistributions of source code must retain the above copyright
   17       notice, this list of conditions and the following disclaimer.
   18
   19    2. Redistributions in binary form must reproduce the above copyright
   20       notice, this list of conditions and the following disclaimer in
   21       the documentation and/or other materials provided with the
   22       distribution.
   23
   24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   25    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   27    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   28    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   29    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   30    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   31    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   32    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   34    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   35    POSSIBILITY OF SUCH DAMAGE.
   36*/
   37
   38:- module(prolog_colour,
   39          [ prolog_colourise_stream/3,  % +Stream, +SourceID, :ColourItem
   40            prolog_colourise_stream/4,  % +Stream, +SourceID, :ColourItem, +Opts
   41            prolog_colourise_term/4,    % +Stream, +SourceID, :ColourItem, +Opts
   42            prolog_colourise_query/3,   % +String, +SourceID, :ColourItem
   43            syntax_colour/2,            % +Class, -Attributes
   44            syntax_message//1           % +Class
   45          ]).   46:- use_module(library(record),[(record)/1, op(_,_,record)]).   47:- use_module(library(debug),[debug/3]).   48:- autoload(library(apply),[maplist/3]).   49:- autoload(library(error),[is_of_type/2]).   50:- autoload(library(lists),[member/2,append/3]).   51:- autoload(library(operators),
   52	    [push_operators/1,pop_operators/0,push_op/3]).   53:- autoload(library(option),[option/3]).   54:- autoload(library(predicate_options),
   55	    [current_option_arg/2,current_predicate_options/3]).   56:- autoload(library(prolog_clause),[predicate_name/2]).   57:- autoload(library(prolog_source),
   58	    [ load_quasi_quotation_syntax/2,
   59	      read_source_term_at_location/3,
   60	      prolog_canonical_source/2
   61	    ]).   62:- autoload(library(prolog_xref),
   63	    [ xref_option/2,
   64	      xref_public_list/3,
   65	      xref_op/2,
   66	      xref_prolog_flag/4,
   67	      xref_module/2,
   68	      xref_meta/3,
   69	      xref_source_file/4,
   70	      xref_defined/3,
   71	      xref_called/3,
   72	      xref_defined_class/3,
   73	      xref_exported/2,
   74	      xref_hook/1
   75	    ]).   76
   77:- meta_predicate
   78    prolog_colourise_stream(+, +, 3),
   79    prolog_colourise_stream(+, +, 3, +),
   80    prolog_colourise_query(+, +, 3),
   81    prolog_colourise_term(+, +, 3, +).   82
   83:- predicate_options(prolog_colourise_term/4, 4,
   84                     [ subterm_positions(-any)
   85                     ]).   86:- predicate_options(prolog_colourise_stream/4, 4,
   87                     [ operators(list(any))
   88                     ]).

Prolog syntax colouring support.

This module defines reusable code to colourise Prolog source.

To be done
- : The one-term version */
   98:- multifile
   99    style/2,                        % +ColourClass, -Attributes
  100    message//1,                     % +ColourClass
  101    term_colours/2,                 % +SourceTerm, -ColourSpec
  102    goal_colours/2,                 % +Goal, -ColourSpec
  103    goal_colours/3,                 % +Goal, +Class, -ColourSpec
  104    directive_colours/2,            % +Goal, -ColourSpec
  105    goal_classification/2,          % +Goal, -Class
  106    vararg_goal_classification/3.   % +Name, +Arity, -Class
  107
  108
  109:- record
  110    colour_state(source_id_list,
  111                 module,
  112                 stream,
  113                 closure,
  114                 singletons).  115
  116colour_state_source_id(State, SourceID) :-
  117    colour_state_source_id_list(State, SourceIDList),
  118    member(SourceID, SourceIDList).
 prolog_colourise_stream(+Stream, +SourceID, :ColourItem) is det
 prolog_colourise_stream(+Stream, +SourceID, :ColourItem, +Opts) is det
Determine colour fragments for the data on Stream. SourceID is the canonical identifier of the input as known to the cross-referencer, i.e., as created using xref_source(SourceID).

ColourItem is a closure that is called for each identified fragment with three additional arguments:

Options

operators(+Ops)
Provide an initial list of additional operators.
  139prolog_colourise_stream(Fd, SourceId, ColourItem) :-
  140    prolog_colourise_stream(Fd, SourceId, ColourItem, []).
  141prolog_colourise_stream(Fd, SourceId, ColourItem, Options) :-
  142    to_list(SourceId, SourceIdList),
  143    make_colour_state([ source_id_list(SourceIdList),
  144                        stream(Fd),
  145                        closure(ColourItem)
  146                      ],
  147                      TB),
  148    option(operators(Ops), Options, []),
  149    setup_call_cleanup(
  150        save_settings(TB, Ops, State),
  151        colourise_stream(Fd, TB),
  152        restore_settings(State)).
  153
  154to_list(List, List) :-
  155    is_list(List),
  156    !.
  157to_list(One, [One]).
  158
  159
  160colourise_stream(Fd, TB) :-
  161    (   peek_char(Fd, #)            % skip #! script line
  162    ->  skip(Fd, 10)
  163    ;   true
  164    ),
  165    repeat,
  166        colour_state_module(TB, SM),
  167        character_count(Fd, Start),
  168        catch(read_term(Fd, Term,
  169                        [ subterm_positions(TermPos),
  170                          singletons(Singletons0),
  171                          module(SM),
  172                          comments(Comments)
  173                        ]),
  174              E,
  175              read_error(E, TB, Start, Fd)),
  176        fix_operators(Term, SM, TB),
  177        warnable_singletons(Singletons0, Singletons),
  178        colour_state_singletons(TB, Singletons),
  179        (   colourise_term(Term, TB, TermPos, Comments)
  180        ->  true
  181        ;   arg(1, TermPos, From),
  182            print_message(warning,
  183                          format('Failed to colourise ~p at index ~d~n',
  184                                 [Term, From]))
  185        ),
  186        Term == end_of_file,
  187    !.
  188
  189save_settings(TB, Ops, state(Style, Flags, OSM, Xref)) :-
  190    (   source_module(TB, SM)
  191    ->  true
  192    ;   SM = prolog_colour_ops
  193    ),
  194    set_xref(Xref, true),
  195    '$set_source_module'(OSM, SM),
  196    colour_state_module(TB, SM),
  197    maplist(qualify_op(SM), Ops, QOps),
  198    push_operators(QOps),
  199    syntax_flags(Flags),
  200    '$style_check'(Style, Style).
  201
  202qualify_op(M, op(P,T,[]), Q)            => Q = op(P,T,M:[]).
  203qualify_op(M, op(P,T,N), Q), atom(N)    => Q = op(P,T,M:N).
  204qualify_op(M, op(P,T,L), Q), is_list(Q) =>
  205    Q = op(P, T, QL),
  206    maplist(qualify_op_name(M), L, QL).
  207qualify_op(_, Op, Q)			=> Q = Op.
  208
  209qualify_op_name(M, N,  Q), atom(N) => Q = M:N.
  210qualify_op_name(M, [], Q)          => Q = M:[].
  211qualify_op_name(_, V,  Q)          => Q = V.
  212
  213restore_settings(state(Style, Flags, OSM, Xref)) :-
  214    restore_syntax_flags(Flags),
  215    '$style_check'(_, Style),
  216    pop_operators,
  217    '$set_source_module'(OSM),
  218    set_xref(_, Xref).
  219
  220set_xref(Old, New) :-
  221    current_prolog_flag(xref, Old),
  222    !,
  223    set_prolog_flag(xref, New).
  224set_xref(false, New) :-
  225    set_prolog_flag(xref, New).
  226
  227
  228syntax_flags(Pairs) :-
  229    findall(set_prolog_flag(Flag, Value),
  230            syntax_flag(Flag, Value),
  231            Pairs).
  232
  233syntax_flag(Flag, Value) :-
  234    syntax_flag(Flag),
  235    current_prolog_flag(Flag, Value).
  236
  237restore_syntax_flags([]).
  238restore_syntax_flags([set_prolog_flag(Flag, Value)|T]) :-
  239    set_prolog_flag(Flag, Value),
  240    restore_syntax_flags(T).
 source_module(+State, -Module) is semidet
True when Module is the module context into which the file is loaded. This is the module of the file if File is a module file, or the load context of File if File is not included or the module context of the file into which the file was included.
  249source_module(TB, Module) :-
  250    colour_state_source_id_list(TB, []),
  251    !,
  252    colour_state_module(TB, Module).
  253source_module(TB, Module) :-
  254    colour_state_source_id(TB, SourceId),
  255    xref_option(SourceId, module(Module)),
  256    !.
  257source_module(TB, Module) :-
  258    (   colour_state_source_id(TB, File),
  259        atom(File)
  260    ;   colour_state_stream(TB, Fd),
  261        is_stream(Fd),
  262        stream_property(Fd, file_name(File))
  263    ),
  264    module_context(File, [], Module).
  265
  266module_context(File, _, Module) :-
  267    source_file_property(File, module(Module)),
  268    !.
  269module_context(File, Seen, Module) :-
  270    source_file_property(File, included_in(File2, _Line)),
  271    \+ memberchk(File, Seen),
  272    !,
  273    module_context(File2, [File|Seen], Module).
  274module_context(File, _, Module) :-
  275    source_file_property(File, load_context(Module, _, _)).
 read_error(+Error, +TB, +Start, +Stream) is failure
If this is a syntax error, create a syntax-error fragment.
  282read_error(Error, TB, Start, EndSpec) :-
  283    (   syntax_error(Error, Id, CharNo)
  284    ->  message_to_string(error(syntax_error(Id), _), Msg),
  285        (   integer(EndSpec)
  286        ->  End = EndSpec
  287        ;   character_count(EndSpec, End)
  288        ),
  289        show_syntax_error(TB, CharNo:Msg, Start-End),
  290        fail
  291    ;   throw(Error)
  292    ).
  293
  294syntax_error(error(syntax_error(Id), stream(_S, _Line, _LinePos, CharNo)),
  295             Id, CharNo).
  296syntax_error(error(syntax_error(Id), file(_S, _Line, _LinePos, CharNo)),
  297             Id, CharNo).
  298syntax_error(error(syntax_error(Id), string(_Text, CharNo)),
  299             Id, CharNo).
 warnable_singletons(+Singletons, -Warn) is det
Warn is the subset of the singletons that we warn about.
  305warnable_singletons([], []).
  306warnable_singletons([H|T0], List) :-
  307    H = (Name=_Var),
  308    (   '$is_named_var'(Name)
  309    ->  List = [H|T]
  310    ;   List = T
  311    ),
  312    warnable_singletons(T0, T).
 colour_item(+Class, +TB, +Pos) is det
  316colour_item(Class, TB, Pos) :-
  317    arg(1, Pos, Start),
  318    arg(2, Pos, End),
  319    Len is End - Start,
  320    colour_state_closure(TB, Closure),
  321    call(Closure, Class, Start, Len).
 safe_push_op(+Prec, +Type, :Name, +State)
Define operators into the default source module and register them to be undone by pop_operators/0.
  329safe_push_op(P, T, N0, State) :-
  330    colour_state_module(State, CM),
  331    strip_module(CM:N0, M, N),
  332    (   is_list(N),
  333        N \== []                                % define list as operator
  334    ->  acyclic_term(N),
  335        forall(member(Name, N),
  336               safe_push_op(P, T, M:Name, State))
  337    ;   push_op(P, T, M:N)
  338    ),
  339    debug(colour, ':- ~w.', [op(P,T,M:N)]).
 fix_operators(+Term, +Module, +State) is det
Fix flags that affect the syntax, such as operators and some style checking options. Src is the canonical source as required by the cross-referencer.
  347fix_operators((:- Directive), M, Src) :-
  348    ground(Directive),
  349    catch(process_directive(Directive, M, Src), _, true),
  350    !.
  351fix_operators(_, _, _).
  352
  353:- multifile
  354    prolog:xref_update_syntax/2.  355
  356process_directive(Directive, M, _Src) :-
  357    prolog:xref_update_syntax(Directive, M),
  358    !.
  359process_directive(style_check(X), _, _) :-
  360    !,
  361    style_check(X).
  362process_directive(set_prolog_flag(Flag, Value), M, _) :-
  363    syntax_flag(Flag),
  364    !,
  365    set_prolog_flag(M:Flag, Value).
  366process_directive(M:op(P,T,N), _, Src) :-
  367    !,
  368    process_directive(op(P,T,N), M, Src).
  369process_directive(op(P,T,N), M, Src) :-
  370    !,
  371    safe_push_op(P, T, M:N, Src).
  372process_directive(module(_Name, Export), M, Src) :-
  373    !,
  374    forall(member(op(P,A,N), Export),
  375           safe_push_op(P,A,M:N, Src)).
  376process_directive(use_module(Spec), _, Src) :-
  377    !,
  378    catch(process_use_module1(Spec, Src), _, true).
  379process_directive(use_module(Spec, Imports), _, Src) :-
  380    !,
  381    catch(process_use_module2(Spec, Imports, Src), _, true).
  382process_directive(Directive, _, Src) :-
  383    prolog_source:expand((:-Directive), Src, _).
  384
  385syntax_flag(character_escapes).
  386syntax_flag(var_prefix).
  387syntax_flag(allow_variable_name_as_functor).
  388syntax_flag(allow_dot_in_atom).
 process_use_module1(+Imports, +Src)
Get the exported operators from the referenced files.
  394process_use_module1([], _) :- !.
  395process_use_module1([H|T], Src) :-
  396    !,
  397    process_use_module1(H, Src),
  398    process_use_module1(T, Src).
  399process_use_module1(File, Src) :-
  400    (   xref_public_list(File, Src,
  401                         [ exports(Exports),
  402                           silent(true),
  403                           path(Path)
  404                         ])
  405    ->  forall(member(op(P,T,N), Exports),
  406               safe_push_op(P,T,N,Src)),
  407        colour_state_module(Src, SM),
  408        (   member(Syntax/4, Exports),
  409            load_quasi_quotation_syntax(SM:Path, Syntax),
  410            fail
  411        ;   true
  412        )
  413    ;   true
  414    ).
  415
  416process_use_module2(File, Imports, Src) :-
  417    (   xref_public_list(File, Src,
  418                         [ exports(Exports),
  419                           silent(true),
  420                           path(Path)
  421                         ])
  422    ->  forall(( member(op(P,T,N), Exports),
  423                 member(op(P,T,N), Imports)),
  424               safe_push_op(P,T,N,Src)),
  425        colour_state_module(Src, SM),
  426        (   member(Syntax/4, Exports),
  427            member(Syntax/4, Imports),
  428            load_quasi_quotation_syntax(SM:Path, Syntax),
  429            fail
  430        ;   true
  431        )
  432    ;   true
  433    ).
 prolog_colourise_query(+Query:string, +SourceId, :ColourItem)
Colourise a query, to be executed in the context of SourceId.
Arguments:
SourceId- Execute Query in the context of the cross-referenced environment SourceID.
  442prolog_colourise_query(QueryString, SourceID, ColourItem) :-
  443    query_colour_state(SourceID, ColourItem, TB),
  444    setup_call_cleanup(
  445        save_settings(TB, [], State),
  446        colourise_query(QueryString, TB),
  447        restore_settings(State)).
  448
  449query_colour_state(module(Module), ColourItem, TB) :-
  450    !,
  451    make_colour_state([ source_id_list([]),
  452                        module(Module),
  453                        closure(ColourItem)
  454                      ],
  455                      TB).
  456query_colour_state(SourceID, ColourItem, TB) :-
  457    to_list(SourceID, SourceIDList),
  458    make_colour_state([ source_id_list(SourceIDList),
  459                        closure(ColourItem)
  460                      ],
  461                      TB).
  462
  463
  464colourise_query(QueryString, TB) :-
  465    colour_state_module(TB, SM),
  466    string_length(QueryString, End),
  467    (   catch(term_string(Query, QueryString,
  468                          [ subterm_positions(TermPos),
  469                            singletons(Singletons0),
  470                            module(SM),
  471                            comments(Comments)
  472                          ]),
  473              E,
  474              read_error(E, TB, 0, End))
  475    ->  warnable_singletons(Singletons0, Singletons),
  476        colour_state_singletons(TB, Singletons),
  477        colourise_comments(Comments, TB),
  478        (   Query == end_of_file
  479        ->  true
  480        ;   colourise_body(Query, TB, TermPos)
  481        )
  482    ;   true                        % only a syntax error
  483    ).
 prolog_colourise_term(+Stream, +SourceID, :ColourItem, +Options)
Colourise the next term on Stream. Unlike prolog_colourise_stream/3, this predicate assumes it is reading a single term rather than the entire stream. This implies that it cannot adjust syntax according to directives that precede it.

Options:

subterm_positions(-TermPos)
Return complete term-layout. If an error is read, this is a term error_position(StartClause, EndClause, ErrorPos)
  498prolog_colourise_term(Stream, SourceId, ColourItem, Options) :-
  499    to_list(SourceId, SourceIdList),
  500    make_colour_state([ source_id_list(SourceIdList),
  501                        stream(Stream),
  502                        closure(ColourItem)
  503                      ],
  504                      TB),
  505    option(subterm_positions(TermPos), Options, _),
  506    findall(Op, xref_op(SourceId, Op), Ops),
  507    debug(colour, 'Ops from ~p: ~p', [SourceId, Ops]),
  508    findall(Opt, xref_flag_option(SourceId, Opt), Opts),
  509    character_count(Stream, Start),
  510    (   source_module(TB, Module)
  511    ->  true
  512    ;   Module = prolog_colour_ops
  513    ),
  514    read_source_term_at_location(
  515        Stream, Term,
  516        [ module(Module),
  517          operators(Ops),
  518          error(Error),
  519          subterm_positions(TermPos),
  520          singletons(Singletons0),
  521          comments(Comments)
  522        | Opts
  523        ]),
  524    (   var(Error)
  525    ->  warnable_singletons(Singletons0, Singletons),
  526        colour_state_singletons(TB, Singletons),
  527        colour_item(range, TB, TermPos),            % Call to allow clearing
  528        colourise_term(Term, TB, TermPos, Comments)
  529    ;   character_count(Stream, End),
  530        TermPos = error_position(Start, End, Pos),
  531        colour_item(range, TB, TermPos),
  532        show_syntax_error(TB, Error, Start-End),
  533        Error = Pos:_Message
  534    ).
  535
  536xref_flag_option(TB, var_prefix(Bool)) :-
  537    xref_prolog_flag(TB, var_prefix, Bool, _Line).
  538
  539show_syntax_error(TB, Pos:Message, Range) :-
  540    integer(Pos),
  541    !,
  542    End is Pos + 1,
  543    colour_item(syntax_error(Message, Range), TB, Pos-End).
  544show_syntax_error(TB, _:Message, Range) :-
  545    colour_item(syntax_error(Message, Range), TB, Range).
  546
  547
  548singleton(Var, TB) :-
  549    colour_state_singletons(TB, Singletons),
  550    member_var(Var, Singletons).
  551
  552member_var(V, [_=V2|_]) :-
  553    V == V2,
  554    !.
  555member_var(V, [_|T]) :-
  556    member_var(V, T).
 colourise_term(+Term, +TB, +Termpos, +Comments)
Colourise the next Term.
bug
- The colour spec is closed with fullstop, but the position information does not include the full stop location, so all we can do is assume it is behind the term.
  567colourise_term(Term, TB, TermPos, Comments) :-
  568    colourise_comments(Comments, TB),
  569    (   Term == end_of_file
  570    ->  true
  571    ;   colourise_term(Term, TB, TermPos),
  572        colourise_fullstop(TB, TermPos)
  573    ).
  574
  575colourise_fullstop(TB, TermPos) :-
  576    arg(2, TermPos, EndTerm),
  577    Start is EndTerm,
  578    End is Start+1,
  579    colour_item(fullstop, TB, Start-End).
  580
  581colourise_comments(-, _).
  582colourise_comments([], _).
  583colourise_comments([H|T], TB) :-
  584    colourise_comment(H, TB),
  585    colourise_comments(T, TB).
  586
  587colourise_comment((-)-_, _) :- !.
  588colourise_comment(Pos-Comment, TB) :-
  589    comment_style(Comment, Style),
  590    stream_position_data(char_count, Pos, Start),
  591    string_length(Comment, Len),
  592    End is Start + Len + 1,
  593    colour_item(comment(Style), TB, Start-End).
  594
  595comment_style(Comment, structured) :-           % Starts %%, %! or /**
  596    structured_comment_start(Start),
  597    sub_string(Comment, 0, Len, _, Start),
  598    Next is Len+1,
  599    string_code(Next, Comment, NextCode),
  600    code_type(NextCode, space),
  601    !.
  602comment_style(Comment, line) :-                 % Starts %
  603    sub_string(Comment, 0, _, _, '%'),
  604    !.
  605comment_style(_, block).                        % Starts /*
 structured_comment_start(-Start)
Copied from library(pldoc/doc_process). Unfortunate, but we do not want to force loading pldoc.
  612structured_comment_start('%%').
  613structured_comment_start('%!').
  614structured_comment_start('/**').
 colourise_term(+Term, +TB, +Pos)
Colorise a file toplevel term.
  620colourise_term(Var, TB, Start-End) :-
  621    var(Var),
  622    !,
  623    colour_item(instantiation_error, TB, Start-End).
  624colourise_term(_, _, Pos) :-
  625    var(Pos),
  626    !.
  627colourise_term(Term, TB, parentheses_term_position(PO,PC,Pos)) :-
  628    !,
  629    colour_item(parentheses, TB, PO-PC),
  630    colourise_term(Term, TB, Pos).
  631colourise_term(Term, TB, Pos) :-
  632    term_colours(Term, FuncSpec-ArgSpecs),
  633    !,
  634    Pos = term_position(F,T,FF,FT,ArgPos),
  635    colour_item(term, TB, F-T),     % TBD: Allow specifying by term_colours/2?
  636    specified_item(FuncSpec, Term, TB, FF-FT),
  637    specified_items(ArgSpecs, Term, TB, ArgPos).
  638colourise_term((Pre=>Body), TB,
  639               term_position(F,T,FF,FT,[PP,BP])) :-
  640    nonvar(Pre),
  641    Pre = (Head,Cond),
  642    PP = term_position(_HF,_HT,_HFF,_HFT,[HP,CP]),
  643    !,
  644    colour_item(clause,         TB, F-T),
  645    colour_item(neck(=>),       TB, FF-FT),
  646    colourise_clause_head(Head, TB, HP),
  647    colour_item(rule_condition, TB, CP),
  648    colourise_body(Cond, Head,  TB, CP),
  649    colourise_body(Body, Head,  TB, BP).
  650colourise_term(Term, TB,
  651               term_position(F,T,FF,FT,[HP,BP])) :-
  652    neck(Term, Head, Body, Neck),
  653    !,
  654    colour_item(clause,         TB, F-T),
  655    colour_item(neck(Neck),     TB, FF-FT),
  656    colourise_clause_head(Head, TB, HP),
  657    colourise_body(Body, Head,  TB, BP).
  658colourise_term(((Head,RHC) --> Body), TB,
  659               term_position(F,T,FF,FT,
  660                             [ term_position(_,_,_,_,[HP,RHCP]),
  661                               BP
  662                             ])) :-
  663    !,
  664    colour_item(grammar_rule,       TB, F-T),
  665    colour_item(dcg_right_hand_ctx, TB, RHCP),
  666    colourise_term_arg(RHC, TB, RHCP),
  667    colour_item(neck(-->),          TB, FF-FT),
  668    colourise_extended_head(Head, 2, TB, HP),
  669    colourise_dcg(Body, Head,       TB, BP).
  670colourise_term((Head --> Body), TB,                     % TBD: expansion!
  671               term_position(F,T,FF,FT,[HP,BP])) :-
  672    !,
  673    colour_item(grammar_rule,       TB, F-T),
  674    colour_item(neck(-->),          TB, FF-FT),
  675    colourise_extended_head(Head, 2, TB, HP),
  676    colourise_dcg(Body, Head,       TB, BP).
  677colourise_term(:->(Head, Body), TB,
  678               term_position(F,T,FF,FT,[HP,BP])) :-
  679    !,
  680    colour_item(method,             TB, F-T),
  681    colour_item(neck(:->), TB, FF-FT),
  682    colour_method_head(send(Head),  TB, HP),
  683    colourise_method_body(Body,     TB, BP).
  684colourise_term(:<-(Head, Body), TB,
  685               term_position(F,T,FF,FT,[HP,BP])) :-
  686    !,
  687    colour_item(method,            TB, F-T),
  688    colour_item(neck(:<-), TB, FF-FT),
  689    colour_method_head(get(Head),  TB, HP),
  690    colourise_method_body(Body,    TB, BP).
  691colourise_term((:- Directive), TB, Pos) :-
  692    !,
  693    colour_item(directive, TB, Pos),
  694    Pos = term_position(_F,_T,FF,FT,[ArgPos]),
  695    colour_item(neck(directive), TB, FF-FT),
  696    colourise_directive(Directive, TB, ArgPos).
  697colourise_term((?- Directive), TB, Pos) :-
  698    !,
  699    colourise_term((:- Directive), TB, Pos).
  700colourise_term(end_of_file, _, _) :- !.
  701colourise_term(Fact, TB, Pos) :-
  702    !,
  703    colour_item(clause, TB, Pos),
  704    colourise_clause_head(Fact, TB, Pos).
  705
  706neck((Head  :- Body), Head, Body, :-).
  707neck((Head  => Body), Head, Body, =>).
  708neck(?=>(Head, Body), Head, Body, ?=>).
 colourise_extended_head(+Head, +ExtraArgs, +TB, +Pos) is det
Colourise a clause-head that is extended by term_expansion, getting ExtraArgs more arguments (e.g., DCGs add two more arguments.
  716colourise_extended_head(Head, N, TB, Pos) :-
  717    extend(Head, N, TheHead),
  718    colourise_clause_head(TheHead, TB, Pos).
  719
  720extend(M:Head, N, M:ExtHead) :-
  721    nonvar(Head),
  722    !,
  723    extend(Head, N, ExtHead).
  724extend(Head, N, ExtHead) :-
  725    compound(Head),
  726    !,
  727    compound_name_arguments(Head, Name, Args),
  728    length(Extra, N),
  729    append(Args, Extra, NArgs),
  730    compound_name_arguments(ExtHead, Name, NArgs).
  731extend(Head, N, ExtHead) :-
  732    atom(Head),
  733    !,
  734    length(Extra, N),
  735    compound_name_arguments(ExtHead, Head, Extra).
  736extend(Head, _, Head).
  737
  738
  739colourise_clause_head(_, _, Pos) :-
  740    var(Pos),
  741    !.
  742colourise_clause_head(Head, TB, parentheses_term_position(PO,PC,Pos)) :-
  743    colour_item(parentheses, TB, PO-PC),
  744    colourise_clause_head(Head, TB, Pos).
  745colourise_clause_head(M:Head, TB, QHeadPos) :-
  746    QHeadPos = term_position(_,_,QF,QT,[MPos,HeadPos]),
  747    head_colours(M:Head, meta-[_, ClassSpec-ArgSpecs]),
  748    !,
  749    colourise_module(M, TB, MPos),
  750    colour_item(functor, TB, QF-QT),
  751    functor_position(HeadPos, FPos, ArgPos),
  752    (   ClassSpec == classify
  753    ->  classify_head(TB, Head, Class)
  754    ;   Class = ClassSpec
  755    ),
  756    colour_item(head_term(Class, Head), TB, QHeadPos),
  757    colour_item(head(Class, Head), TB, FPos),
  758    specified_items(ArgSpecs, Head, TB, ArgPos).
  759colourise_clause_head(#(Macro), TB, term_position(_,_,HF,HT,[MPos])) :-
  760    expand_macro(TB, Macro, Head),
  761    !,
  762    macro_term_string(Head, String),
  763    functor_position(MPos, FPos, _),
  764    classify_head(TB, Head, Class),
  765    colour_item(macro(String), TB, HF-HT),
  766    colour_item(head_term(Class, Head), TB, MPos),
  767    colour_item(head(Class, Head), TB, FPos),
  768    colourise_term_args(Macro, TB, MPos).
  769colourise_clause_head(Head, TB, Pos) :-
  770    head_colours(Head, ClassSpec-ArgSpecs),
  771    !,
  772    functor_position(Pos, FPos, ArgPos),
  773    (   ClassSpec == classify
  774    ->  classify_head(TB, Head, Class)
  775    ;   Class = ClassSpec
  776    ),
  777    colour_item(head_term(Class, Head), TB, Pos),
  778    colour_item(head(Class, Head), TB, FPos),
  779    specified_items(ArgSpecs, Head, TB, ArgPos).
  780colourise_clause_head(:=(Eval, Ret), TB,
  781                      term_position(_,_,AF,AT,
  782                                    [ term_position(_,_,SF,ST,
  783                                                    [ SelfPos,
  784                                                      FuncPos
  785                                                    ]),
  786                                      RetPos
  787                                    ])) :-
  788    Eval =.. [.,M,Func],
  789    FuncPos = term_position(_,_,FF,FT,_),
  790    !,
  791    colourise_term_arg(M, TB, SelfPos),
  792    colour_item(func_dot, TB, SF-ST),               % .
  793    colour_item(dict_function(Func), TB, FF-FT),
  794    colourise_term_args(Func, TB, FuncPos),
  795    colour_item(dict_return_op, TB, AF-AT),         % :=
  796    colourise_term_arg(Ret, TB, RetPos).
  797colourise_clause_head(Head, TB, Pos) :-
  798    functor_position(Pos, FPos, _),
  799    classify_head(TB, Head, Class),
  800    colour_item(head_term(Class, Head), TB, Pos),
  801    colour_item(head(Class, Head), TB, FPos),
  802    colourise_term_args(Head, TB, Pos).
 colourise_extern_head(+Head, +Module, +TB, +Pos)
Colourise the head specified as Module:Head. Normally used for adding clauses to multifile predicates in other modules.
  809colourise_extern_head(Head, M, TB, Pos) :-
  810    functor_position(Pos, FPos, _),
  811    colour_item(head(extern(M), Head), TB, FPos),
  812    colourise_term_args(Head, TB, Pos).
  813
  814colour_method_head(SGHead, TB, Pos) :-
  815    arg(1, SGHead, Head),
  816    functor_name(SGHead, SG),
  817    functor_position(Pos, FPos, _),
  818    colour_item(method(SG), TB, FPos),
  819    colourise_term_args(Head, TB, Pos).
 functor_position(+Term, -FunctorPos, -ArgPosList)
Get the position of a functor and its argument. Unfortunately this goes wrong for lists, who have two `functor-positions'.
  826functor_position(term_position(_,_,FF,FT,ArgPos), FF-FT, ArgPos) :- !.
  827functor_position(list_position(F,_T,Elms,none), F-FT, Elms) :-
  828    !,
  829    FT is F + 1.
  830functor_position(dict_position(_,_,FF,FT,KVPos), FF-FT, KVPos) :- !.
  831functor_position(brace_term_position(F,T,Arg), F-T, [Arg]) :- !.
  832functor_position(Pos, Pos, []).
  833
  834colourise_module(Term, TB, Pos) :-
  835    (   var(Term)
  836    ;   atom(Term)
  837    ),
  838    !,
  839    colour_item(module(Term), TB, Pos).
  840colourise_module(_, TB, Pos) :-
  841    colour_item(type_error(module), TB, Pos).
 colourise_directive(+Body, +TB, +Pos)
Colourise the body of a directive.
  847colourise_directive(_,_,Pos) :-
  848    var(Pos),
  849    !.
  850colourise_directive(Dir, TB, parentheses_term_position(PO,PC,Pos)) :-
  851    !,
  852    colour_item(parentheses, TB, PO-PC),
  853    colourise_directive(Dir, TB, Pos).
  854colourise_directive((A,B), TB, term_position(_,_,_,_,[PA,PB])) :-
  855    !,
  856    colourise_directive(A, TB, PA),
  857    colourise_directive(B, TB, PB).
  858colourise_directive(Body, TB, Pos) :-
  859    nonvar(Body),
  860    directive_colours(Body, ClassSpec-ArgSpecs),   % specified
  861    !,
  862    functor_position(Pos, FPos, ArgPos),
  863    (   ClassSpec == classify
  864    ->  goal_classification(TB, Body, [], Class)
  865    ;   Class = ClassSpec
  866    ),
  867    colour_item(goal(Class, Body), TB, FPos),
  868    specified_items(ArgSpecs, Body, TB, ArgPos).
  869colourise_directive(Body, TB, Pos) :-
  870    colourise_body(Body, TB, Pos).
  871
  872
  873%       colourise_body(+Body, +TB, +Pos)
  874%
  875%       Breaks down to colourise_goal/3.
  876
  877colourise_body(Body, TB, Pos) :-
  878    colourise_body(Body, [], TB, Pos).
  879
  880colourise_body(Body, Origin, TB, Pos) :-
  881    colour_item(body, TB, Pos),
  882    colourise_goals(Body, Origin, TB, Pos).
 colourise_method_body(+MethodBody, +TB, +Pos)
Colourise the optional "comment":: as pce(comment) and proceed with the body.
To be done
- Get this handled by a hook.
  891colourise_method_body(_, _, Pos) :-
  892    var(Pos),
  893    !.
  894colourise_method_body(Body, TB, parentheses_term_position(PO,PC,Pos)) :-
  895    !,
  896    colour_item(parentheses, TB, PO-PC),
  897    colourise_method_body(Body, TB, Pos).
  898colourise_method_body(::(_Comment,Body), TB,
  899                      term_position(_F,_T,_FF,_FT,[CP,BP])) :-
  900    !,
  901    colour_item(comment(string), TB, CP),
  902    colourise_body(Body, TB, BP).
  903colourise_method_body(Body, TB, Pos) :-         % deal with pri(::) < 1000
  904    Body =.. [F,A,B],
  905    control_op(F),
  906    !,
  907    Pos = term_position(_F,_T,FF,FT,
  908                        [ AP,
  909                          BP
  910                        ]),
  911    colour_item(control, TB, FF-FT),
  912    colourise_method_body(A, TB, AP),
  913    colourise_body(B, TB, BP).
  914colourise_method_body(Body, TB, Pos) :-
  915    colourise_body(Body, TB, Pos).
  916
  917control_op(',').
  918control_op((;)).
  919control_op((->)).
  920control_op((*->)).
 colourise_goals(+Body, +Origin, +TB, +Pos)
Colourise the goals in a body.
  926colourise_goals(_, _, _, Pos) :-
  927    var(Pos),
  928    !.
  929colourise_goals(Body, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
  930    !,
  931    colour_item(parentheses, TB, PO-PC),
  932    colourise_goals(Body, Origin, TB, Pos).
  933colourise_goals(Body, Origin, TB, term_position(_,_,FF,FT,ArgPos)) :-
  934    body_compiled(Body),
  935    !,
  936    colour_item(control, TB, FF-FT),
  937    colourise_subgoals(ArgPos, 1, Body, Origin, TB).
  938colourise_goals(Goal, Origin, TB, Pos) :-
  939    colourise_goal(Goal, Origin, TB, Pos).
  940
  941colourise_subgoals([], _, _, _, _).
  942colourise_subgoals([Pos|T], N, Body, Origin, TB) :-
  943    arg(N, Body, Arg),
  944    colourise_goals(Arg, Origin, TB, Pos),
  945    NN is N + 1,
  946    colourise_subgoals(T, NN, Body, Origin, TB).
 colourise_dcg(+Body, +Head, +TB, +Pos)
Breaks down to colourise_dcg_goal/3.
  952colourise_dcg(Body, Head, TB, Pos) :-
  953    colour_item(dcg, TB, Pos),
  954    (   dcg_extend(Head, Origin)
  955    ->  true
  956    ;   Origin = Head
  957    ),
  958    colourise_dcg_goals(Body, Origin, TB, Pos).
  959
  960colourise_dcg_goals(Var, _, TB, Pos) :-
  961    var(Var),
  962    !,
  963    colour_item(goal(meta,Var), TB, Pos).
  964colourise_dcg_goals(_, _, _, Pos) :-
  965    var(Pos),
  966    !.
  967colourise_dcg_goals(Body, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
  968    !,
  969    colour_item(parentheses, TB, PO-PC),
  970    colourise_dcg_goals(Body, Origin, TB, Pos).
  971colourise_dcg_goals({Body}, Origin, TB, brace_term_position(F,T,Arg)) :-
  972    !,
  973    colour_item(dcg(plain), TB, F-T),
  974    colourise_goals(Body, Origin, TB, Arg).
  975colourise_dcg_goals([], _, TB, Pos) :-
  976    !,
  977    colour_item(dcg(terminal), TB, Pos).
  978colourise_dcg_goals(List, _, TB, list_position(F,T,Elms,Tail)) :-
  979    List = [_|_],
  980    !,
  981    colour_item(dcg(terminal), TB, F-T),
  982    colourise_list_args(Elms, Tail, List, TB, classify).
  983colourise_dcg_goals(_, _, TB, string_position(F,T)) :-
  984    integer(F),
  985    !,
  986    colour_item(dcg(string), TB, F-T).
  987colourise_dcg_goals(Body, Origin, TB, term_position(_,_,FF,FT,ArgPos)) :-
  988    dcg_body_compiled(Body),       % control structures
  989    !,
  990    colour_item(control, TB, FF-FT),
  991    colourise_dcg_subgoals(ArgPos, 1, Body, Origin, TB).
  992colourise_dcg_goals(Goal, Origin, TB, Pos) :-
  993    colourise_dcg_goal(Goal, Origin, TB, Pos).
  994
  995colourise_dcg_subgoals([], _, _, _, _).
  996colourise_dcg_subgoals([Pos|T], N, Body, Origin, TB) :-
  997    arg(N, Body, Arg),
  998    colourise_dcg_goals(Arg, Origin, TB, Pos),
  999    NN is N + 1,
 1000    colourise_dcg_subgoals(T, NN, Body, Origin, TB).
 1001
 1002dcg_extend(Term, _) :-
 1003    var(Term), !, fail.
 1004dcg_extend(M:Term, M:Goal) :-
 1005    dcg_extend(Term, Goal).
 1006dcg_extend(Term, Goal) :-
 1007    compound(Term),
 1008    !,
 1009    compound_name_arguments(Term, Name, Args),
 1010    append(Args, [_,_], NArgs),
 1011    compound_name_arguments(Goal, Name, NArgs).
 1012dcg_extend(Term, Goal) :-
 1013    atom(Term),
 1014    !,
 1015    compound_name_arguments(Goal, Term, [_,_]).
 1016
 1017dcg_body_compiled(G) :-
 1018    body_compiled(G),
 1019    !.
 1020dcg_body_compiled((_|_)).
 1021
 1022%       colourise_dcg_goal(+Goal, +Origin, +TB, +Pos).
 1023
 1024colourise_dcg_goal(!, Origin, TB, TermPos) :-
 1025    !,
 1026    colourise_goal(!, Origin, TB, TermPos).
 1027colourise_dcg_goal(Goal, Origin, TB, TermPos) :-
 1028    dcg_extend(Goal, TheGoal),
 1029    !,
 1030    colourise_goal(TheGoal, Origin, TB, TermPos).
 1031colourise_dcg_goal(Goal, _, TB, Pos) :-
 1032    colourise_term_args(Goal, TB, Pos).
 colourise_goal(+Goal, +Origin, +TB, +Pos)
Colourise access to a single goal.
To be done
- Quasi Quotations are coloured as a general term argument. Possibly we should do something with the goal information it refers to, in particular if this goal is not defined.
 1043                                        % Deal with list as goal (consult)
 1044colourise_goal(_,_,_,Pos) :-
 1045    var(Pos),
 1046    !.
 1047colourise_goal(Goal, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
 1048    !,
 1049    colour_item(parentheses, TB, PO-PC),
 1050    colourise_goal(Goal, Origin, TB, Pos).
 1051colourise_goal(Goal, _, TB, Pos) :-
 1052    Pos = list_position(F,T,Elms,TailPos),
 1053    Goal = [_|_],
 1054    !,
 1055    FT is F + 1,
 1056    AT is T - 1,
 1057    colour_item(goal_term(built_in, Goal), TB, Pos),
 1058    colour_item(goal(built_in, Goal), TB, F-FT),
 1059    colour_item(goal(built_in, Goal), TB, AT-T),
 1060    colourise_file_list(Goal, TB, Elms, TailPos, any).
 1061colourise_goal(Goal, Origin, TB, Pos) :-
 1062    Pos = list_position(F,T,Elms,Tail),
 1063    callable(Goal),
 1064    Goal =.. [_,GH,GT|_],
 1065    !,
 1066    goal_classification(TB, Goal, Origin, Class),
 1067    FT is F + 1,
 1068    AT is T - 1,
 1069    colour_item(goal_term(Class, Goal), TB, Pos),
 1070    colour_item(goal(Class, Goal), TB, F-FT),
 1071    colour_item(goal(Class, Goal), TB, AT-T),
 1072    colourise_list_args(Elms, Tail, [GH|GT], TB, classify).
 1073colourise_goal(Goal, _Origin, TB, Pos) :-
 1074    Pos = quasi_quotation_position(_F,_T,_QQType,_QQTypePos,_CPos),
 1075    !,
 1076    colourise_term_arg(Goal, TB, Pos).
 1077colourise_goal(#(Macro), Origin, TB, term_position(_,_,HF,HT,[MPos])) :-
 1078    expand_macro(TB, Macro, Goal),
 1079    !,
 1080    macro_term_string(Goal, String),
 1081    goal_classification(TB, Goal, Origin, Class),
 1082    (   MPos = term_position(_,_,FF,FT,_ArgPos)
 1083    ->  FPos = FF-FT
 1084    ;   FPos = MPos
 1085    ),
 1086    colour_item(macro(String), TB, HF-HT),
 1087    colour_item(goal_term(Class, Goal), TB, MPos),
 1088    colour_item(goal(Class, Goal), TB, FPos),
 1089    colourise_goal_args(Goal, TB, MPos).
 1090colourise_goal(Goal, Origin, TB, Pos) :-
 1091    strip_module(Goal, _, PGoal),
 1092    nonvar(PGoal),
 1093    (   goal_classification(TB, Goal, Origin, ClassInferred),
 1094        call_goal_colours(Goal, ClassInferred, ClassSpec-ArgSpecs)
 1095    ->  true
 1096    ;   call_goal_colours(Goal, ClassSpec-ArgSpecs)
 1097    ),
 1098    !,                                          % specified
 1099    functor_position(Pos, FPos, ArgPos),
 1100    (   ClassSpec == classify
 1101    ->  goal_classification(TB, Goal, Origin, Class)
 1102    ;   Class = ClassSpec
 1103    ),
 1104    colour_item(goal_term(Class, Goal), TB, Pos),
 1105    colour_item(goal(Class, Goal), TB, FPos),
 1106    colour_dict_braces(TB, Pos),
 1107    specified_items(ArgSpecs, Goal, TB, ArgPos).
 1108colourise_goal(Module:Goal, _Origin, TB, QGoalPos) :-
 1109    QGoalPos = term_position(_,_,QF,QT,[PM,PG]),
 1110    !,
 1111    colourise_module(Module, TB, PM),
 1112    colour_item(functor, TB, QF-QT),
 1113    (   PG = term_position(_,_,FF,FT,_)
 1114    ->  FP = FF-FT
 1115    ;   FP = PG
 1116    ),
 1117    (   callable(Goal)
 1118    ->  qualified_goal_classification(Module:Goal, TB, Class),
 1119        colour_item(goal_term(Class, Goal), TB, QGoalPos),
 1120        colour_item(goal(Class, Goal), TB, FP),
 1121        colourise_goal_args(Goal, Module, TB, PG)
 1122    ;   var(Goal)
 1123    ->  colourise_term_arg(Goal, TB, PG)
 1124    ;   colour_item(type_error(callable), TB, PG)
 1125    ).
 1126colourise_goal(Op, _Origin, TB, Pos) :-
 1127    nonvar(Op),
 1128    Op = op(_,_,_),
 1129    !,
 1130    colourise_op_declaration(Op, TB, Pos).
 1131colourise_goal(Goal, Origin, TB, Pos) :-
 1132    goal_classification(TB, Goal, Origin, Class),
 1133    (   Pos = term_position(_,_,FF,FT,_ArgPos)
 1134    ->  FPos = FF-FT
 1135    ;   FPos = Pos
 1136    ),
 1137    colour_item(goal_term(Class, Goal), TB, Pos),
 1138    colour_item(goal(Class, Goal), TB, FPos),
 1139    colourise_goal_args(Goal, TB, Pos).
 1140
 1141% make sure to emit a fragment for the braces of tag{k:v, ...} or
 1142% {...} that is mapped to something else.
 1143
 1144colour_dict_braces(TB, dict_position(_F,T,_TF,TT,_KVPos)) :-
 1145    !,
 1146    BStart is TT+1,
 1147    colour_item(dict_content, TB, BStart-T).
 1148colour_dict_braces(TB, brace_term_position(F,T,_Arg)) :-
 1149    !,
 1150    colour_item(brace_term, TB, F-T).
 1151colour_dict_braces(_, _).
 colourise_goal_args(+Goal, +TB, +Pos)
Colourise the arguments to a goal. This predicate deals with meta- and database-access predicates.
 1158colourise_goal_args(Goal, TB, Pos) :-
 1159    colourization_module(TB, Module),
 1160    colourise_goal_args(Goal, Module, TB, Pos).
 1161
 1162colourization_module(TB, Module) :-
 1163    (   colour_state_source_id(TB, SourceId),
 1164        xref_module(SourceId, Module)
 1165    ->  true
 1166    ;   Module = user
 1167    ).
 1168
 1169colourise_goal_args(Goal, M, TB, term_position(_,_,_,_,ArgPos)) :-
 1170    !,
 1171    (   meta_args(Goal, TB, MetaArgs)
 1172    ->  colourise_meta_args(1, Goal, M, MetaArgs, TB, ArgPos)
 1173    ;   colourise_goal_args(1, Goal, M, TB, ArgPos)
 1174    ).
 1175colourise_goal_args(Goal, M, TB, brace_term_position(_,_,ArgPos)) :-
 1176    !,
 1177    (   meta_args(Goal, TB, MetaArgs)
 1178    ->  colourise_meta_args(1, Goal, M, MetaArgs, TB, [ArgPos])
 1179    ;   colourise_goal_args(1, Goal, M, TB, [ArgPos])
 1180    ).
 1181colourise_goal_args(_, _, _, _).                % no arguments
 1182
 1183colourise_goal_args(_, _, _, _, []) :- !.
 1184colourise_goal_args(N, Goal, Module, TB, [P0|PT]) :-
 1185    colourise_option_arg(Goal, Module, N, TB, P0),
 1186    !,
 1187    NN is N + 1,
 1188    colourise_goal_args(NN, Goal, Module, TB, PT).
 1189colourise_goal_args(N, Goal, Module, TB, [P0|PT]) :-
 1190    arg(N, Goal, Arg),
 1191    colourise_term_arg(Arg, TB, P0),
 1192    NN is N + 1,
 1193    colourise_goal_args(NN, Goal, Module, TB, PT).
 1194
 1195
 1196colourise_meta_args(_, _, _, _, _, []) :- !.
 1197colourise_meta_args(N, Goal, Module, MetaArgs, TB, [P0|PT]) :-
 1198    colourise_option_arg(Goal, Module, N, TB, P0),
 1199    !,
 1200    NN is N + 1,
 1201    colourise_meta_args(NN, Goal, Module, MetaArgs, TB, PT).
 1202colourise_meta_args(N, Goal, Module, MetaArgs, TB, [P0|PT]) :-
 1203    arg(N, Goal, Arg),
 1204    arg(N, MetaArgs, MetaSpec),
 1205    colourise_meta_arg(MetaSpec, Arg, TB, P0),
 1206    NN is N + 1,
 1207    colourise_meta_args(NN, Goal, Module, MetaArgs, TB, PT).
 1208
 1209colourise_meta_arg(MetaSpec, Arg, TB, Pos) :-
 1210    nonvar(Arg),
 1211    expand_meta(MetaSpec, Arg, Expanded),
 1212    !,
 1213    colourise_goal(Expanded, [], TB, Pos). % TBD: recursion
 1214colourise_meta_arg(MetaSpec, Arg, TB, Pos) :-
 1215    nonvar(Arg),
 1216    MetaSpec == //,
 1217    !,
 1218    colourise_dcg_goals(Arg, //, TB, Pos).
 1219colourise_meta_arg(_, Arg, TB, Pos) :-
 1220    colourise_term_arg(Arg, TB, Pos).
 meta_args(+Goal, +TB, -ArgSpec) is semidet
Return a copy of Goal, where each meta-argument is an integer representing the number of extra arguments or the atom // for indicating a DCG body. The non-meta arguments are unbound variables.

E.g. meta_args(maplist(foo,x,y), X) --> X = maplist(2,_,_)

NOTE: this could be cached if performance becomes an issue.

 1233meta_args(Goal, TB, VarGoal) :-
 1234    colour_state_source_id(TB, SourceId),
 1235    xref_meta(SourceId, Goal, _),
 1236    !,
 1237    compound_name_arity(Goal, Name, Arity),
 1238    compound_name_arity(VarGoal, Name, Arity),
 1239    xref_meta(SourceId, VarGoal, MetaArgs),
 1240    instantiate_meta(MetaArgs).
 1241
 1242instantiate_meta([]).
 1243instantiate_meta([H|T]) :-
 1244    (   var(H)
 1245    ->  H = 0
 1246    ;   H = V+N
 1247    ->  V = N
 1248    ;   H = //(V)
 1249    ->  V = (//)
 1250    ),
 1251    instantiate_meta(T).
 expand_meta(+MetaSpec, +Goal, -Expanded) is semidet
Add extra arguments to the goal if the meta-specifier is an integer (see above).
 1258expand_meta(MetaSpec, Goal, Goal) :-
 1259    MetaSpec == 0.
 1260expand_meta(MetaSpec, M:Goal, M:Expanded) :-
 1261    atom(M),
 1262    !,
 1263    expand_meta(MetaSpec, Goal, Expanded).
 1264expand_meta(MetaSpec, Goal, Expanded) :-
 1265    integer(MetaSpec),
 1266    MetaSpec > 0,
 1267    (   atom(Goal)
 1268    ->  functor(Expanded, Goal, MetaSpec)
 1269    ;   compound(Goal)
 1270    ->  compound_name_arguments(Goal, Name, Args0),
 1271        length(Extra, MetaSpec),
 1272        append(Args0, Extra, Args),
 1273        compound_name_arguments(Expanded, Name, Args)
 1274    ).
 colourise_setof(+Term, +TB, +Pos)
Colourise the 2nd argument of setof/bagof
 1280colourise_setof(Var^G, TB, term_position(_,_,FF,FT,[VP,GP])) :-
 1281    !,
 1282    colourise_term_arg(Var, TB, VP),
 1283    colour_item(ext_quant, TB, FF-FT),
 1284    colourise_setof(G, TB, GP).
 1285colourise_setof(Term, TB, Pos) :-
 1286    colourise_goal(Term, [], TB, Pos).
 1287
 1288%       colourise_db(+Arg, +TB, +Pos)
 1289%
 1290%       Colourise database modification calls (assert/1, retract/1 and
 1291%       friends.
 1292
 1293colourise_db((Head:-Body), TB, term_position(_,_,_,_,[HP,BP])) :-
 1294    !,
 1295    colourise_db(Head, TB, HP),
 1296    colourise_body(Body, Head, TB, BP).
 1297colourise_db(Module:Head, TB, term_position(_,_,QF,QT,[MP,HP])) :-
 1298    !,
 1299    colourise_module(Module, TB, MP),
 1300    colour_item(functor, TB, QF-QT),
 1301    (   atom(Module),
 1302        colour_state_source_id(TB, SourceId),
 1303        xref_module(SourceId, Module)
 1304    ->  colourise_db(Head, TB, HP)
 1305    ;   colourise_db(Head, TB, HP)
 1306    ).
 1307colourise_db(Head, TB, Pos) :-
 1308    colourise_goal(Head, '<db-change>', TB, Pos).
 colourise_option_args(+Goal, +Module, +Arg:integer, +TB, +ArgPos) is semidet
Colourise predicate options for the Arg-th argument of Module:Goal
 1317colourise_option_arg(Goal, Module, Arg, TB, ArgPos) :-
 1318    goal_name_arity(Goal, Name, Arity),
 1319    current_option_arg(Module:Name/Arity, Arg),
 1320    current_predicate_options(Module:Name/Arity, Arg, OptionDecl),
 1321    debug(emacs, 'Colouring option-arg ~w of ~p',
 1322          [Arg, Module:Name/Arity]),
 1323    arg(Arg, Goal, Options),
 1324    colourise_option(Options, Module, Goal, Arg, OptionDecl, TB, ArgPos).
 1325
 1326colourise_option(Options0, Module, Goal, Arg, OptionDecl, TB, Pos0) :-
 1327    strip_option_module_qualifier(Goal, Module, Arg, TB,
 1328                                  Options0, Pos0, Options, Pos),
 1329    (   Pos = list_position(F, T, ElmPos, TailPos)
 1330    ->  colour_item(list, TB, F-T),
 1331        colourise_option_list(Options, OptionDecl, TB, ElmPos, TailPos)
 1332    ;   (   var(Options)
 1333        ;   Options == []
 1334        )
 1335    ->  colourise_term_arg(Options, TB, Pos)
 1336    ;   colour_item(type_error(list), TB, Pos)
 1337    ).
 1338
 1339strip_option_module_qualifier(Goal, Module, Arg, TB,
 1340                              M:Options, term_position(_,_,_,_,[MP,Pos]),
 1341                              Options, Pos) :-
 1342    predicate_property(Module:Goal, meta_predicate(Head)),
 1343    arg(Arg, Head, :),
 1344    !,
 1345    colourise_module(M, TB, MP).
 1346strip_option_module_qualifier(_, _, _, _,
 1347                              Options, Pos, Options, Pos).
 1348
 1349
 1350colourise_option_list(_, _, _, [], none) :- !.
 1351colourise_option_list(Tail, _, TB, [], TailPos) :-
 1352    !,
 1353    colourise_term_arg(Tail, TB, TailPos).
 1354colourise_option_list([H|T], OptionDecl, TB, [HPos|TPos], TailPos) :-
 1355    colourise_option(H, OptionDecl, TB, HPos),
 1356    colourise_option_list(T, OptionDecl, TB, TPos, TailPos).
 1357
 1358colourise_option(Opt, _, TB, Pos) :-
 1359    var(Opt),
 1360    !,
 1361    colourise_term_arg(Opt, TB, Pos).
 1362colourise_option(Opt, OptionDecl, TB, term_position(_,_,FF,FT,ValPosList)) :-
 1363    !,
 1364    generalise_term(Opt, GenOpt),
 1365    (   memberchk(GenOpt, OptionDecl)
 1366    ->  colour_item(option_name, TB, FF-FT),
 1367        Opt =.. [Name|Values],
 1368        GenOpt =.. [Name|Types],
 1369        colour_option_values(Values, Types, TB, ValPosList)
 1370    ;   colour_item(no_option_name, TB, FF-FT),
 1371        colourise_term_args(ValPosList, 1, Opt, TB)
 1372    ).
 1373colourise_option(_, _, TB, Pos) :-
 1374    colour_item(type_error(option), TB, Pos).
 1375
 1376colour_option_values([], [], _, _).
 1377colour_option_values([V0|TV], [T0|TT], TB, [P0|TP]) :-
 1378    (   (   var(V0)
 1379        ;   is_of_type(T0, V0)
 1380        ;   T0 = list(_),
 1381            member(E, V0),
 1382            var(E)
 1383        ;   dict_field_extraction(V0)
 1384        )
 1385    ->  colourise_term_arg(V0, TB, P0)
 1386    ;   callable(V0),
 1387        (   T0 = callable
 1388        ->  N = 0
 1389        ;   T0 = (callable+N)
 1390        )
 1391    ->  colourise_meta_arg(N, V0, TB, P0)
 1392    ;   colour_item(type_error(T0), TB, P0)
 1393    ),
 1394    colour_option_values(TV, TT, TB, TP).
 colourise_files(+Arg, +TB, +Pos, +Why)
Colourise the argument list of one of the file-loading predicates.
Arguments:
Why- is one of any or imported
 1403colourise_files(List, TB, list_position(F,T,Elms,TailPos), Why) :-
 1404    !,
 1405    colour_item(list, TB, F-T),
 1406    colourise_file_list(List, TB, Elms, TailPos, Why).
 1407colourise_files(M:Spec, TB, term_position(_,_,_,_,[MP,SP]), Why) :-
 1408    !,
 1409    colourise_module(M, TB, MP),
 1410    colourise_files(Spec, TB, SP, Why).
 1411colourise_files(Var, TB, P, _) :-
 1412    var(Var),
 1413    !,
 1414    colour_item(var, TB, P).
 1415colourise_files(Spec0, TB, Pos, Why) :-
 1416    strip_module(Spec0, _, Spec),
 1417    (   colour_state_source_id(TB, Source),
 1418        prolog_canonical_source(Source, SourceId),
 1419        catch(xref_source_file(Spec, Path, SourceId, [silent(true)]),
 1420              _, fail)
 1421    ->  (   Why = imported,
 1422            \+ resolves_anything(TB, Path),
 1423            exports_something(TB, Path)
 1424        ->  colour_item(file_no_depend(Path), TB, Pos)
 1425        ;   colour_item(file(Path), TB, Pos)
 1426        )
 1427    ;   colour_item(nofile, TB, Pos)
 1428    ).
 colourise_file_list(+Files, +TB, +ElmPos, +TailPos, +Why)
 1432colourise_file_list([], _, [], none, _).
 1433colourise_file_list(Last, TB, [], TailPos, _Why) :-
 1434    (   var(Last)
 1435    ->  colourise_term(Last, TB, TailPos)
 1436    ;   colour_item(type_error(list), TB, TailPos)
 1437    ).
 1438colourise_file_list([H|T], TB, [PH|PT], TailPos, Why) :-
 1439    colourise_files(H, TB, PH, Why),
 1440    colourise_file_list(T, TB, PT, TailPos, Why).
 1441
 1442resolves_anything(TB, Path) :-
 1443    colour_state_source_id(TB, SourceId),
 1444    xref_defined(SourceId, Head, imported(Path)),
 1445    xref_called(SourceId, Head, _),
 1446    !.
 1447
 1448exports_something(TB, Path) :-
 1449    colour_state_source_id(TB, SourceId),
 1450    xref_defined(SourceId, _, imported(Path)),
 1451    !.
 colourise_directory(+Arg, +TB, +Pos)
Colourise argument that should be an existing directory.
 1457colourise_directory(Spec, TB, Pos) :-
 1458    (   colour_state_source_id(TB, SourceId),
 1459        catch(xref_source_file(Spec, Path, SourceId,
 1460                               [ file_type(directory),
 1461                                 silent(true)
 1462                               ]),
 1463              _, fail)
 1464    ->  colour_item(directory(Path), TB, Pos)
 1465    ;   colour_item(nofile, TB, Pos)
 1466    ).
 colourise_langoptions(+Term, +TB, +Pos) is det
Colourise the 3th argument of module/3
 1472colourise_langoptions([], _, _) :- !.
 1473colourise_langoptions([H|T], TB, list_position(PF,PT,[HP|TP],_)) :-
 1474    !,
 1475    colour_item(list, TB, PF-PT),
 1476    colourise_langoptions(H, TB, HP),
 1477    colourise_langoptions(T, TB, TP).
 1478colourise_langoptions(Spec, TB, Pos) :-
 1479    colourise_files(library(dialect/Spec), TB, Pos, imported).
 colourise_class(ClassName, TB, Pos)
Colourise an XPCE class.
 1485colourise_class(ClassName, TB, Pos) :-
 1486    colour_state_source_id(TB, SourceId),
 1487    classify_class(SourceId, ClassName, Classification),
 1488    colour_item(class(Classification, ClassName), TB, Pos).
 classify_class(+SourceId, +ClassName, -Classification)
Classify an XPCE class. As long as this code is in this module rather than using hooks, we do not want to load xpce unless it is already loaded.
 1496classify_class(SourceId, Name, Class) :-
 1497    xref_defined_class(SourceId, Name, Class),
 1498    !.
 1499classify_class(_SourceId, Name, Class) :-
 1500    current_predicate(pce:send_class/3),
 1501    (   current_predicate(classify_class/2)
 1502    ->  true
 1503    ;   use_module(library(pce_meta), [classify_class/2])
 1504    ),
 1505    member(G, [classify_class(Name, Class)]),
 1506    call(G).
 colourise_term_args(+Term, +TB, +Pos)
colourise head/body principal terms.
 1512colourise_term_args(Term, TB,
 1513                    term_position(_,_,_,_,ArgPos)) :-
 1514    !,
 1515    colourise_term_args(ArgPos, 1, Term, TB).
 1516colourise_term_args(_, _, _).
 1517
 1518colourise_term_args([], _, _, _).
 1519colourise_term_args([Pos|T], N, Term, TB) :-
 1520    arg(N, Term, Arg),
 1521    colourise_term_arg(Arg, TB, Pos),
 1522    NN is N + 1,
 1523    colourise_term_args(T, NN, Term, TB).
 colourise_term_arg(+Term, +TB, +Pos)
Colourise an arbitrary Prolog term without context of its semantical role.
 1530colourise_term_arg(_, _, Pos) :-
 1531    var(Pos),
 1532    !.
 1533colourise_term_arg(Arg, TB, parentheses_term_position(PO,PC,Pos)) :-
 1534    !,
 1535    colour_item(parentheses, TB, PO-PC),
 1536    colourise_term_arg(Arg, TB, Pos).
 1537colourise_term_arg(Var, TB, Pos) :-                     % variable
 1538    var(Var), Pos = _-_,
 1539    !,
 1540    (   singleton(Var, TB)
 1541    ->  colour_item(singleton, TB, Pos)
 1542    ;   colour_item(var, TB, Pos)
 1543    ).
 1544colourise_term_arg(List, TB, list_position(F, T, Elms, Tail)) :-
 1545    !,
 1546    colour_item(list, TB, F-T),
 1547    colourise_list_args(Elms, Tail, List, TB, classify).    % list
 1548colourise_term_arg(String, TB, string_position(F, T)) :-    % string
 1549    !,
 1550    (   string(String)
 1551    ->  colour_item(string, TB, F-T)
 1552    ;   String = [H|_]
 1553    ->  (   integer(H)
 1554        ->  colour_item(codes, TB, F-T)
 1555        ;   colour_item(chars, TB, F-T)
 1556        )
 1557    ;   String == []
 1558    ->  colour_item(codes, TB, F-T)
 1559    ).
 1560colourise_term_arg(_, TB,
 1561                   quasi_quotation_position(F,T,QQType,QQTypePos,CPos)) :-
 1562    !,
 1563    colourise_qq_type(QQType, TB, QQTypePos),
 1564    functor_name(QQType, Type),
 1565    colour_item(qq_content(Type), TB, CPos),
 1566    arg(1, CPos, SE),
 1567    SS is SE-2,
 1568    FE is F+2,
 1569    TS is T-2,
 1570    colour_item(qq(open),  TB, F-FE),
 1571    colour_item(qq(sep),   TB, SS-SE),
 1572    colour_item(qq(close), TB, TS-T).
 1573colourise_term_arg({Term}, TB, brace_term_position(F,T,Arg)) :-
 1574    !,
 1575    colour_item(brace_term, TB, F-T),
 1576    colourise_term_arg(Term, TB, Arg).
 1577colourise_term_arg(Map, TB, dict_position(F,T,TF,TT,KVPos)) :-
 1578    !,
 1579    is_dict(Map, Tag),
 1580    colour_item(dict, TB, F-T),
 1581    TagPos = TF-TT,
 1582    (   var(Tag)
 1583    ->  (   singleton(Tag, TB)
 1584        ->  colour_item(singleton, TB, TagPos)
 1585        ;   colour_item(var, TB, TagPos)
 1586        )
 1587    ;   colour_item(dict_tag, TB, TagPos)
 1588    ),
 1589    BStart is TT+1,
 1590    colour_item(dict_content, TB, BStart-T),
 1591    colourise_dict_kv(Map, TB, KVPos).
 1592colourise_term_arg([](List,Term), TB,                   % [] as operator
 1593                   term_position(_,_,0,0,[ListPos,ArgPos])) :-
 1594    !,
 1595    colourise_term_arg(List, TB, ListPos),
 1596    colourise_term_arg(Term, TB, ArgPos).
 1597colourise_term_arg(#(Macro), TB, term_position(_,_,HF,HT,[MPos])) :-
 1598    expand_macro(TB, Macro, Term),
 1599    !,
 1600    macro_term_string(Term, String),
 1601    colour_item(macro(String), TB, HF-HT),
 1602    colourise_term_arg(Macro, TB, MPos).
 1603colourise_term_arg(Compound, TB, Pos) :-                % compound
 1604    compound(Compound),
 1605    !,
 1606    (   Pos = term_position(_F,_T,FF,FT,_ArgPos)
 1607    ->  colour_item(functor, TB, FF-FT)             % TBD: Infix/Postfix?
 1608    ;   true                                        % TBD: When is this
 1609    ),
 1610    colourise_term_args(Compound, TB, Pos).
 1611colourise_term_arg(EmptyList, TB, Pos) :-
 1612    EmptyList == [],
 1613    !,
 1614    colour_item(empty_list, TB, Pos).
 1615colourise_term_arg(Atom, TB, Pos) :-
 1616    atom(Atom),
 1617    !,
 1618    colour_item(atom, TB, Pos).
 1619colourise_term_arg(Integer, TB, Pos) :-
 1620    integer(Integer),
 1621    !,
 1622    colour_item(int, TB, Pos).
 1623colourise_term_arg(Rational, TB, Pos) :-
 1624    rational(Rational),
 1625    !,
 1626    colour_item(rational(Rational), TB, Pos).
 1627colourise_term_arg(Float, TB, Pos) :-
 1628    float(Float),
 1629    !,
 1630    colour_item(float, TB, Pos).
 1631colourise_term_arg(_Arg, _TB, _Pos) :-
 1632    true.
 1633
 1634colourise_list_args([HP|TP], Tail, [H|T], TB, How) :-
 1635    specified_item(How, H, TB, HP),
 1636    colourise_list_args(TP, Tail, T, TB, How).
 1637colourise_list_args([], none, _, _, _) :- !.
 1638colourise_list_args([], TP, T, TB, How) :-
 1639    specified_item(How, T, TB, TP).
 colourise_expression(+Term, +TB, +Pos)
colourise arithmetic expressions.
 1646colourise_expression(_, _, Pos) :-
 1647    var(Pos),
 1648    !.
 1649colourise_expression(Arg, TB, parentheses_term_position(PO,PC,Pos)) :-
 1650    !,
 1651    colour_item(parentheses, TB, PO-PC),
 1652    colourise_expression(Arg, TB, Pos).
 1653colourise_expression(Compound, TB, Pos) :-
 1654    compound(Compound), Pos = term_position(_F,_T,FF,FT,_ArgPos),
 1655    !,
 1656    (   dict_field_extraction(Compound)
 1657    ->  colourise_term_arg(Compound, TB, Pos)
 1658    ;   current_arithmetic_function(Compound)
 1659    ->  colour_item(function, TB, FF-FT)
 1660    ;   colour_item(no_function, TB, FF-FT)
 1661    ),
 1662    colourise_expression_args(Compound, TB, Pos).
 1663colourise_expression(Atom, TB, Pos) :-
 1664    atom(Atom),
 1665    !,
 1666    (   current_arithmetic_function(Atom)
 1667    ->  colour_item(function, TB, Pos)
 1668    ;   colour_item(no_function, TB, Pos)
 1669    ).
 1670colourise_expression(NumOrVar, TB, Pos) :-
 1671    Pos = _-_,
 1672    !,
 1673    colourise_term_arg(NumOrVar, TB, Pos).
 1674colourise_expression(_Arg, TB, Pos) :-
 1675    colour_item(type_error(evaluable), TB, Pos).
 1676
 1677dict_field_extraction(Term) :-
 1678    compound(Term),
 1679    compound_name_arity(Term, '.', 2),
 1680    Term \= [_|_].                        % traditional mode
 1681
 1682
 1683colourise_expression_args(roundtoward(Expr, Mode), TB,
 1684                          term_position(_,_,_,_,[ExprPos, ModePos])) :-
 1685    !,
 1686    colourise_expression(Expr, TB, ExprPos),
 1687    colourise_round_mode(Mode, TB, ModePos).
 1688colourise_expression_args(Term, TB,
 1689                          term_position(_,_,_,_,ArgPos)) :-
 1690    !,
 1691    colourise_expression_args(ArgPos, 1, Term, TB).
 1692colourise_expression_args(_, _, _).
 1693
 1694colourise_expression_args([], _, _, _).
 1695colourise_expression_args([Pos|T], N, Term, TB) :-
 1696    arg(N, Term, Arg),
 1697    colourise_expression(Arg, TB, Pos),
 1698    NN is N + 1,
 1699    colourise_expression_args(T, NN, Term, TB).
 1700
 1701colourise_round_mode(Mode, TB, Pos) :-
 1702    var(Mode),
 1703    !,
 1704    colourise_term_arg(Mode, TB, Pos).
 1705colourise_round_mode(Mode, TB, Pos) :-
 1706    round_mode(Mode),
 1707    !,
 1708    colour_item(identifier, TB, Pos).
 1709colourise_round_mode(_Mode, TB, Pos) :-
 1710    colour_item(domain_error(rounding_mode), TB, Pos).
 1711
 1712round_mode(to_nearest).
 1713round_mode(to_positive).
 1714round_mode(to_negative).
 1715round_mode(to_zero).
 colourise_qq_type(+QQType, +TB, +QQTypePos)
Colouring the type part of a quasi quoted term
 1721colourise_qq_type(QQType, TB, QQTypePos) :-
 1722    functor_position(QQTypePos, FPos, _),
 1723    colour_item(qq_type, TB, FPos),
 1724    colourise_term_args(QQType, TB, QQTypePos).
 1725
 1726qq_position(quasi_quotation_position(_,_,_,_,_)).
 colourise_dict_kv(+Dict, +TB, +KVPosList)
Colourise the name-value pairs in the dict
 1732colourise_dict_kv(_, _, []) :- !.
 1733colourise_dict_kv(Dict, TB, [key_value_position(_F,_T,SF,ST,K,KP,VP)|KV]) :-
 1734    colour_item(dict_key, TB, KP),
 1735    colour_item(dict_sep, TB, SF-ST),
 1736    get_dict(K, Dict, V),
 1737    colourise_term_arg(V, TB, VP),
 1738    colourise_dict_kv(Dict, TB, KV).
 colourise_exports(+List, +TB, +Pos)
Colourise the module export-list (or any other list holding terms of the form Name/Arity referring to predicates).
 1746colourise_exports([], TB, Pos) :- !,
 1747    colourise_term_arg([], TB, Pos).
 1748colourise_exports(List, TB, list_position(F,T,ElmPos,Tail)) :-
 1749    !,
 1750    colour_item(list, TB, F-T),
 1751    (   Tail == none
 1752    ->  true
 1753    ;   colour_item(type_error(list), TB, Tail)
 1754    ),
 1755    colourise_exports2(List, TB, ElmPos).
 1756colourise_exports(_, TB, Pos) :-
 1757    colour_item(type_error(list), TB, Pos).
 1758
 1759colourise_exports2([G0|GT], TB, [P0|PT]) :-
 1760    !,
 1761    colourise_declaration(G0, export, TB, P0),
 1762    colourise_exports2(GT, TB, PT).
 1763colourise_exports2(_, _, _).
 colourise_imports(+List, +File, +TB, +Pos)
Colourise import list from use_module/2, importing from File.
 1770colourise_imports(List, File, TB, Pos) :-
 1771    (   colour_state_source_id(TB, SourceId),
 1772        ground(File),
 1773        catch(xref_public_list(File, SourceId,
 1774                               [ path(Path),
 1775                                 public(Public),
 1776                                 silent(true)
 1777                               ] ), _, fail)
 1778    ->  true
 1779    ;   Public = [],
 1780        Path = (-)
 1781    ),
 1782    colourise_imports(List, Path, Public, TB, Pos).
 1783
 1784colourise_imports([], _, _, TB, Pos) :-
 1785    !,
 1786    colour_item(empty_list, TB, Pos).
 1787colourise_imports(List, File, Public, TB, list_position(F,T,ElmPos,Tail)) :-
 1788    !,
 1789    colour_item(list, TB, F-T),
 1790    (   Tail == none
 1791    ->  true
 1792    ;   colour_item(type_error(list), TB, Tail)
 1793    ),
 1794    colourise_imports2(List, File, Public, TB, ElmPos).
 1795colourise_imports(except(Except), File, Public, TB,
 1796                  term_position(_,_,FF,FT,[LP])) :-
 1797    !,
 1798    colour_item(keyword(except), TB, FF-FT),
 1799    colourise_imports(Except, File, Public, TB, LP).
 1800colourise_imports(_, _, _, TB, Pos) :-
 1801    colour_item(type_error(list), TB, Pos).
 1802
 1803colourise_imports2([G0|GT], File, Public, TB, [P0|PT]) :-
 1804    !,
 1805    colourise_import(G0, File, TB, P0),
 1806    colourise_imports2(GT, File, Public, TB, PT).
 1807colourise_imports2(_, _, _, _, _).
 1808
 1809
 1810colourise_import(PI as Name, File, TB, term_position(_,_,FF,FT,[PP,NP])) :-
 1811    pi_to_term(PI, Goal),
 1812    !,
 1813    colour_item(goal(imported(File), Goal), TB, PP),
 1814    rename_goal(Goal, Name, NewGoal),
 1815    goal_classification(TB, NewGoal, [], Class),
 1816    colour_item(goal(Class, NewGoal), TB, NP),
 1817    colour_item(keyword(as), TB, FF-FT).
 1818colourise_import(PI, File, TB, Pos) :-
 1819    pi_to_term(PI, Goal),
 1820    colour_state_source_id(TB, SourceID),
 1821    (   \+ xref_defined(SourceID, Goal, imported(File))
 1822    ->  colour_item(undefined_import, TB, Pos)
 1823    ;   \+ xref_called(SourceID, Goal, _)
 1824    ->  colour_item(unused_import, TB, Pos)
 1825    ),
 1826    !.
 1827colourise_import(PI, _, TB, Pos) :-
 1828    colourise_declaration(PI, import, TB, Pos).
 colourise_declaration(+Decl, ?Which, +TB, +Pos) is det
Colourise declaration sequences as used by module/2, dynamic/1, etc.
 1835colourise_declaration(PI, _, TB, term_position(F,T,FF,FT,[NamePos,ArityPos])) :-
 1836    pi_to_term(PI, Goal),
 1837    !,
 1838    goal_classification(TB, Goal, [], Class),
 1839    colour_item(predicate_indicator(Class, Goal), TB, F-T),
 1840    colour_item(goal(Class, Goal), TB, NamePos),
 1841    colour_item(predicate_indicator, TB, FF-FT),
 1842    colour_item(arity, TB, ArityPos).
 1843colourise_declaration(Module:PI, _, TB,
 1844                      term_position(_,_,QF,QT,[PM,PG])) :-
 1845    atom(Module), pi_to_term(PI, Goal),
 1846    !,
 1847    colourise_module(M, TB, PM),
 1848    colour_item(functor, TB, QF-QT),
 1849    colour_item(predicate_indicator(extern(M), Goal), TB, PG),
 1850    PG = term_position(_,_,FF,FT,[NamePos,ArityPos]),
 1851    colour_item(goal(extern(M), Goal), TB, NamePos),
 1852    colour_item(predicate_indicator, TB, FF-FT),
 1853    colour_item(arity, TB, ArityPos).
 1854colourise_declaration(Module:PI, _, TB,
 1855                      term_position(_,_,QF,QT,[PM,PG])) :-
 1856    atom(Module), nonvar(PI), PI = Name/Arity,
 1857    !,                                  % partial predicate indicators
 1858    colourise_module(Module, TB, PM),
 1859    colour_item(functor, TB, QF-QT),
 1860    (   (var(Name) ; atom(Name)),
 1861        (var(Arity) ; integer(Arity), Arity >= 0)
 1862    ->  colourise_term_arg(PI, TB, PG)
 1863    ;   colour_item(type_error(predicate_indicator), TB, PG)
 1864    ).
 1865colourise_declaration(op(N,T,P), Which, TB, Pos) :-
 1866    (   Which == export
 1867    ;   Which == import
 1868    ),
 1869    !,
 1870    colour_item(exported_operator, TB, Pos),
 1871    colourise_op_declaration(op(N,T,P), TB, Pos).
 1872colourise_declaration(Module:Goal, table, TB,
 1873                      term_position(_,_,QF,QT,
 1874                                    [PM,term_position(_F,_T,FF,FT,ArgPos)])) :-
 1875    atom(Module), callable(Goal),
 1876    !,
 1877    colourise_module(Module, TB, PM),
 1878    colour_item(functor, TB, QF-QT),
 1879    goal_classification(TB, Module:Goal, [], Class),
 1880    compound_name_arguments(Goal, _, Args),
 1881    colour_item(goal(Class, Goal), TB, FF-FT),
 1882    colourise_table_modes(Args, TB, ArgPos).
 1883colourise_declaration(Goal, table, TB, term_position(_F,_T,FF,FT,ArgPos)) :-
 1884    callable(Goal),
 1885    !,
 1886    compound_name_arguments(Goal, _, Args),
 1887    goal_classification(TB, Goal, [], Class),
 1888    colour_item(goal(Class, Goal), TB, FF-FT),
 1889    colourise_table_modes(Args, TB, ArgPos).
 1890colourise_declaration(Goal, table, TB, Pos) :-
 1891    atom(Goal),
 1892    !,
 1893    goal_classification(TB, Goal, [], Class),
 1894    colour_item(goal(Class, Goal), TB, Pos).
 1895colourise_declaration(Partial, _Which, TB, Pos) :-
 1896    compatible_with_pi(Partial),
 1897    !,
 1898    colourise_term_arg(Partial, TB, Pos).
 1899colourise_declaration(_, Which, TB, Pos) :-
 1900    colour_item(type_error(declaration(Which)), TB, Pos).
 1901
 1902compatible_with_pi(Term) :-
 1903    var(Term),
 1904    !.
 1905compatible_with_pi(Name/Arity) :-
 1906    !,
 1907    var_or_atom(Name),
 1908    var_or_nonneg(Arity).
 1909compatible_with_pi(Name//Arity) :-
 1910    !,
 1911    var_or_atom(Name),
 1912    var_or_nonneg(Arity).
 1913compatible_with_pi(M:T) :-
 1914    var_or_atom(M),
 1915    compatible_with_pi(T).
 1916
 1917var_or_atom(X) :- var(X), !.
 1918var_or_atom(X) :- atom(X).
 1919var_or_nonneg(X) :- var(X), !.
 1920var_or_nonneg(X) :- integer(X), X >= 0, !.
 1921
 1922pi_to_term(Name/Arity, Term) :-
 1923    (atom(Name)->true;Name==[]), integer(Arity), Arity >= 0,
 1924    !,
 1925    functor(Term, Name, Arity).
 1926pi_to_term(Name//Arity0, Term) :-
 1927    atom(Name), integer(Arity0), Arity0 >= 0,
 1928    !,
 1929    Arity is Arity0 + 2,
 1930    functor(Term, Name, Arity).
 1931
 1932colourise_meta_declarations((Head,Tail), Extra, TB,
 1933                            term_position(_,_,_,_,[PH,PT])) :-
 1934    !,
 1935    colourise_meta_declaration(Head, Extra, TB, PH),
 1936    colourise_meta_declarations(Tail, Extra, TB, PT).
 1937colourise_meta_declarations(Last, Extra, TB, Pos) :-
 1938    colourise_meta_declaration(Last, Extra, TB, Pos).
 1939
 1940colourise_meta_declaration(M:Head, Extra, TB,
 1941                           term_position(_,_,QF,QT,
 1942                                         [ MP,
 1943                                           term_position(_,_,FF,FT,ArgPos)
 1944                                         ])) :-
 1945    compound(Head),
 1946    !,
 1947    colourise_module(M, TB, MP),
 1948    colour_item(functor, TB, QF-QT),
 1949    colour_item(goal(extern(M),Head), TB, FF-FT),
 1950    compound_name_arguments(Head, _, Args),
 1951    colourise_meta_decls(Args, Extra, TB, ArgPos).
 1952colourise_meta_declaration(Head, Extra, TB, term_position(_,_,FF,FT,ArgPos)) :-
 1953    compound(Head),
 1954    !,
 1955    goal_classification(TB, Head, [], Class),
 1956    colour_item(goal(Class, Head), TB, FF-FT),
 1957    compound_name_arguments(Head, _, Args),
 1958    colourise_meta_decls(Args, Extra, TB, ArgPos).
 1959colourise_meta_declaration([H|T], Extra, TB, list_position(LF,LT,[HP],TP)) :-
 1960    !,
 1961    colour_item(list, TB, LF-LT),
 1962    colourise_meta_decls([H,T], Extra, TB, [HP,TP]).
 1963colourise_meta_declaration(_, _, TB, Pos) :-
 1964    !,
 1965    colour_item(type_error(compound), TB, Pos).
 1966
 1967colourise_meta_decls([], _, _, []).
 1968colourise_meta_decls([Arg|ArgT], Extra, TB, [PosH|PosT]) :-
 1969    colourise_meta_decl(Arg, Extra, TB, PosH),
 1970    colourise_meta_decls(ArgT, Extra, TB, PosT).
 1971
 1972colourise_meta_decl(Arg, Extra, TB, Pos) :-
 1973    nonvar(Arg),
 1974    (   valid_meta_decl(Arg)
 1975    ->  true
 1976    ;   memberchk(Arg, Extra)
 1977    ),
 1978    colour_item(meta(Arg), TB, Pos).
 1979colourise_meta_decl(_, _, TB, Pos) :-
 1980    colour_item(error, TB, Pos).
 1981
 1982valid_meta_decl(:).
 1983valid_meta_decl(*).
 1984valid_meta_decl(//).
 1985valid_meta_decl(^).
 1986valid_meta_decl(?).
 1987valid_meta_decl(+).
 1988valid_meta_decl(-).
 1989valid_meta_decl(I) :- integer(I), between(0,9,I).
 colourise_declarations(+Term, +Which, +TB, +Pos)
Colourise specification for dynamic/1, table/1, etc. Includes processing options such as :- dynamic p/1 as incremental..
 1996colourise_declarations(List, Which, TB, list_position(F,T,Elms,none)) :-
 1997    !,
 1998    colour_item(list, TB, F-T),
 1999    colourise_list_declarations(List, Which, TB, Elms).
 2000colourise_declarations(Term, Which, TB, parentheses_term_position(PO,PC,Pos)) :-
 2001    !,
 2002    colour_item(parentheses, TB, PO-PC),
 2003    colourise_declarations(Term, Which, TB, Pos).
 2004colourise_declarations((Head,Tail), Which, TB,
 2005                             term_position(_,_,_,_,[PH,PT])) :-
 2006    !,
 2007    colourise_declarations(Head, Which, TB, PH),
 2008    colourise_declarations(Tail, Which, TB, PT).
 2009colourise_declarations(as(Spec, Options), Which, TB,
 2010                             term_position(_,_,FF,FT,[PH,PT])) :-
 2011    !,
 2012    colour_item(keyword(as), TB, FF-FT),
 2013    colourise_declarations(Spec, Which, TB, PH),
 2014    colourise_decl_options(Options, Which, TB, PT).
 2015colourise_declarations(PI, Which, TB, Pos) :-
 2016    colourise_declaration(PI, Which, TB, Pos).
 2017
 2018colourise_list_declarations([], _, _, []).
 2019colourise_list_declarations([H|T], Which, TB, [HP|TP]) :-
 2020    colourise_declaration(H, Which, TB, HP),
 2021    colourise_list_declarations(T, Which, TB, TP).
 2022
 2023
 2024colourise_table_modes([], _, _).
 2025colourise_table_modes([H|T], TB, [PH|PT]) :-
 2026    colourise_table_mode(H, TB, PH),
 2027    colourise_table_modes(T, TB, PT).
 2028
 2029colourise_table_mode(H, TB, Pos) :-
 2030    table_mode(H, Mode),
 2031    !,
 2032    colour_item(table_mode(Mode), TB, Pos).
 2033colourise_table_mode(lattice(Spec), TB, term_position(_F,_T,FF,FT,[ArgPos])) :-
 2034    !,
 2035    colour_item(table_mode(lattice), TB, FF-FT),
 2036    table_moded_call(Spec, 3, TB, ArgPos).
 2037colourise_table_mode(po(Spec), TB, term_position(_F,_T,FF,FT,[ArgPos])) :-
 2038    !,
 2039    colour_item(table_mode(po), TB, FF-FT),
 2040    table_moded_call(Spec, 2, TB, ArgPos).
 2041colourise_table_mode(_, TB, Pos) :-
 2042    colour_item(type_error(table_mode), TB, Pos).
 2043
 2044table_mode(Var, index) :-
 2045    var(Var),
 2046    !.
 2047table_mode(+, index).
 2048table_mode(index, index).
 2049table_mode(-, first).
 2050table_mode(first, first).
 2051table_mode(last, last).
 2052table_mode(min, min).
 2053table_mode(max, max).
 2054table_mode(sum, sum).
 2055
 2056table_moded_call(Atom, Arity, TB, Pos) :-
 2057    atom(Atom),
 2058    functor(Head, Atom, Arity),
 2059    goal_classification(TB, Head, [], Class),
 2060    colour_item(goal(Class, Head), TB, Pos).
 2061table_moded_call(Atom/Arity, Arity, TB,
 2062                 term_position(_,_,FF,FT,[NP,AP])) :-
 2063    atom(Atom),
 2064    !,
 2065    functor(Head, Atom, Arity),
 2066    goal_classification(TB, Head, [], Class),
 2067    colour_item(goal(Class, Head), TB, NP),
 2068    colour_item(predicate_indicator, TB, FF-FT),
 2069    colour_item(arity, TB, AP).
 2070table_moded_call(Head, Arity, TB, Pos) :-
 2071    Pos = term_position(_,_,FF,FT,_),
 2072    compound(Head),
 2073    !,
 2074    compound_name_arity(Head, _Name, Arity),
 2075    goal_classification(TB, Head, [], Class),
 2076    colour_item(goal(Class, Head), TB, FF-FT),
 2077    colourise_term_args(Head, TB, Pos).
 2078table_moded_call(_, _, TB, Pos) :-
 2079    colour_item(type_error(predicate_name_or_indicator), TB, Pos).
 2080
 2081colourise_decl_options(Options, Which, TB,
 2082                       parentheses_term_position(_,_,Pos)) :-
 2083    !,
 2084    colourise_decl_options(Options, Which, TB, Pos).
 2085colourise_decl_options((Head,Tail), Which, TB,
 2086                        term_position(_,_,_,_,[PH,PT])) :-
 2087    !,
 2088    colourise_decl_options(Head, Which, TB, PH),
 2089    colourise_decl_options(Tail, Which, TB, PT).
 2090colourise_decl_options(Option, Which, TB, Pos) :-
 2091    ground(Option),
 2092    valid_decl_option(Option, Which),
 2093    !,
 2094    functor(Option, Name, _),
 2095    (   Pos = term_position(_,_,FF,FT,[ArgPos])
 2096    ->  colour_item(decl_option(Name), TB, FF-FT),
 2097        (   arg(1, Option, Value),
 2098            nonneg_or_false(Value)
 2099        ->  colourise_term_arg(Value, TB, ArgPos)
 2100        ;   colour_item(type_error(decl_option_value(Which)), TB, ArgPos)
 2101        )
 2102    ;   colour_item(decl_option(Name), TB, Pos)
 2103    ).
 2104colourise_decl_options(_, Which, TB, Pos) :-
 2105    colour_item(type_error(decl_option(Which)), TB, Pos).
 2106
 2107valid_decl_option(subsumptive,         table).
 2108valid_decl_option(variant,             table).
 2109valid_decl_option(incremental,         table).
 2110valid_decl_option(monotonic,           table).
 2111valid_decl_option(opaque,              table).
 2112valid_decl_option(lazy,                table).
 2113valid_decl_option(monotonic,           dynamic).
 2114valid_decl_option(incremental,         dynamic).
 2115valid_decl_option(abstract(_),         dynamic).
 2116valid_decl_option(opaque,              dynamic).
 2117valid_decl_option(shared,              table).
 2118valid_decl_option(private,             table).
 2119valid_decl_option(subgoal_abstract(_), table).
 2120valid_decl_option(answer_abstract(_),  table).
 2121valid_decl_option(max_answers(_),      table).
 2122valid_decl_option(shared,              dynamic).
 2123valid_decl_option(private,             dynamic).
 2124valid_decl_option(local,               dynamic).
 2125valid_decl_option(multifile,           _).
 2126valid_decl_option(discontiguous,       _).
 2127valid_decl_option(volatile,            _).
 2128
 2129nonneg_or_false(Value) :-
 2130    var(Value),
 2131    !.
 2132nonneg_or_false(Value) :-
 2133    integer(Value), Value >= 0,
 2134    !.
 2135nonneg_or_false(off).
 2136nonneg_or_false(false).
 colourise_op_declaration(Op, TB, Pos) is det
 2140colourise_op_declaration(op(P,T,N), TB, term_position(_,_,FF,FT,[PP,TP,NP])) :-
 2141    colour_item(goal(built_in, op(N,T,P)), TB, FF-FT),
 2142    colour_op_priority(P, TB, PP),
 2143    colour_op_type(T, TB, TP),
 2144    colour_op_name(N, TB, NP).
 2145
 2146colour_op_name(_, _, Pos) :-
 2147    var(Pos),
 2148    !.
 2149colour_op_name(Name, TB, parentheses_term_position(PO,PC,Pos)) :-
 2150    !,
 2151    colour_item(parentheses, TB, PO-PC),
 2152    colour_op_name(Name, TB, Pos).
 2153colour_op_name(Name, TB, Pos) :-
 2154    var(Name),
 2155    !,
 2156    colour_item(var, TB, Pos).
 2157colour_op_name(Name, TB, Pos) :-
 2158    (atom(Name) ; Name == []),
 2159    !,
 2160    colour_item(identifier, TB, Pos).
 2161colour_op_name(Module:Name, TB, term_position(_F,_T,QF,QT,[MP,NP])) :-
 2162    !,
 2163    colourise_module(Module, TB, MP),
 2164    colour_item(functor, TB, QF-QT),
 2165    colour_op_name(Name, TB, NP).
 2166colour_op_name(List, TB, list_position(F,T,Elems,none)) :-
 2167    !,
 2168    colour_item(list, TB, F-T),
 2169    colour_op_names(List, TB, Elems).
 2170colour_op_name(_, TB, Pos) :-
 2171    colour_item(error, TB, Pos).
 2172
 2173colour_op_names([], _, []).
 2174colour_op_names([H|T], TB, [HP|TP]) :-
 2175    colour_op_name(H, TB, HP),
 2176    colour_op_names(T, TB, TP).
 2177
 2178colour_op_type(Type, TB, Pos) :-
 2179    var(Type),
 2180    !,
 2181    colour_item(var, TB, Pos).
 2182colour_op_type(Type, TB, Pos) :-
 2183    op_type(Type),
 2184    !,
 2185    colour_item(op_type(Type), TB, Pos).
 2186colour_op_type(_, TB, Pos) :-
 2187    colour_item(error, TB, Pos).
 2188
 2189colour_op_priority(Priority, TB, Pos) :-
 2190    var(Priority), colour_item(var, TB, Pos).
 2191colour_op_priority(Priority, TB, Pos) :-
 2192    integer(Priority),
 2193    between(0, 1200, Priority),
 2194    !,
 2195    colour_item(int, TB, Pos).
 2196colour_op_priority(_, TB, Pos) :-
 2197    colour_item(error, TB, Pos).
 2198
 2199op_type(fx).
 2200op_type(fy).
 2201op_type(xf).
 2202op_type(yf).
 2203op_type(xfy).
 2204op_type(xfx).
 2205op_type(yfx).
 colourise_prolog_flag_name(+Name, +TB, +Pos)
Colourise the name of a Prolog flag
 2212colourise_prolog_flag_name(_, _, Pos) :-
 2213    var(Pos),
 2214    !.
 2215colourise_prolog_flag_name(Name, TB, parentheses_term_position(PO,PC,Pos)) :-
 2216    !,
 2217    colour_item(parentheses, TB, PO-PC),
 2218    colourise_prolog_flag_name(Name, TB, Pos).
 2219colourise_prolog_flag_name(Name, TB, Pos) :-
 2220    atom(Name),
 2221    !,
 2222    (   current_prolog_flag(Name, _)
 2223    ->  colour_item(flag_name(Name), TB, Pos)
 2224    ;   known_flag(Name)
 2225    ->  colour_item(known_flag_name(Name), TB, Pos)
 2226    ;   colour_item(no_flag_name(Name), TB, Pos)
 2227    ).
 2228colourise_prolog_flag_name(Name, TB, Pos) :-
 2229    colourise_term(Name, TB, Pos).
 2230
 2231% Some flags are know, but can be unset.
 2232known_flag(android).
 2233known_flag(android_api).
 2234known_flag(apple).
 2235known_flag(asan).
 2236known_flag(conda).
 2237known_flag(dde).
 2238known_flag(emscripten).
 2239known_flag(executable_format).
 2240known_flag(gc_thread).
 2241known_flag(gmp_version).
 2242known_flag(gui).
 2243known_flag(max_rational_size).
 2244known_flag(mitigate_spectre).
 2245known_flag(pid).
 2246known_flag(pipe).
 2247known_flag(posix_shell).
 2248known_flag(shared_home).
 2249known_flag(shared_table_space).
 2250known_flag(system_thread_id).
 2251known_flag(threads).
 2252known_flag(unix).
 2253known_flag(windows).
 2254known_flag(wine_version).
 2255known_flag(xpce).
 2256
 2257		 /*******************************
 2258		 *             MACROS		*
 2259		 *******************************/
 expand_macro(+TB, +Macro, -Expanded) is semidet
To be done
- This only works if the code is compiled. Ideally we'd also make this work for not compiled code.
 2266expand_macro(TB, Macro, Expanded) :-
 2267    colour_state_source_id(TB, SourceId),
 2268    (   xref_module(SourceId, M)
 2269    ->  true
 2270    ;   M = user
 2271    ),
 2272    current_predicate(M:'$macro'/2),
 2273    catch(M:'$macro'(Macro, Expanded),
 2274          error(_, _),
 2275          fail),
 2276    !.
 2277
 2278macro_term_string(Term, String) :-
 2279    copy_term_nat(Term, Copy),
 2280    numbervars(Copy, 0, _, [singletons(true)]),
 2281    term_string(Copy, String,
 2282                [ portray(true),
 2283                  max_depth(2),
 2284                  numbervars(true)
 2285                ]).
 2286
 2287
 2288                 /*******************************
 2289                 *        CONFIGURATION         *
 2290                 *******************************/
 2291
 2292%       body_compiled(+Term)
 2293%
 2294%       Succeeds if term is a construct handled by the compiler.
 2295
 2296body_compiled((_,_)).
 2297body_compiled((_->_)).
 2298body_compiled((_*->_)).
 2299body_compiled((_;_)).
 2300body_compiled(\+_).
 goal_classification(+TB, +Goal, +Origin, -Class)
Classify Goal appearing in TB and called from a clause with head Origin. For directives, Origin is [].
 2307goal_classification(_, QGoal, _, Class) :-
 2308    strip_module(QGoal, _, Goal),
 2309    (   var(Goal)
 2310    ->  !, Class = meta
 2311    ;   \+ callable(Goal)
 2312    ->  !, Class = not_callable
 2313    ).
 2314goal_classification(_, Goal, Origin, recursion) :-
 2315    callable(Origin),
 2316    generalise_term(Goal, Origin),
 2317    !.
 2318goal_classification(TB, Goal, _, How) :-
 2319    colour_state_source_id(TB, SourceId),
 2320    xref_defined(SourceId, Goal, How),
 2321    How \= public(_),
 2322    !.
 2323goal_classification(TB, Goal, _, Class) :-
 2324    (   colour_state_source_id(TB, SourceId),
 2325        xref_module(SourceId, Module)
 2326    ->  true
 2327    ;   Module = user
 2328    ),
 2329    call_goal_classification(Goal, Module, Class),
 2330    !.
 2331goal_classification(TB, Goal, _, How) :-
 2332    colour_state_module(TB, Module),
 2333    atom(Module),
 2334    Module \== prolog_colour_ops,
 2335    predicate_property(Module:Goal, imported_from(From)),
 2336    !,
 2337    How = imported(From).
 2338goal_classification(_TB, _Goal, _, undefined).
 goal_classification(+Goal, +Module, -Class)
Multifile hookable classification for non-local goals.
 2344call_goal_classification(Goal, Module, Class) :-
 2345    catch(global_goal_classification(Goal, Module, Class), _,
 2346          Class = type_error(callable)).
 2347
 2348global_goal_classification(Goal, _, built_in) :-
 2349    built_in_predicate(Goal),
 2350    !.
 2351global_goal_classification(Goal, _, autoload(From)) :-  % SWI-Prolog
 2352    predicate_property(Goal, autoload(From)).
 2353global_goal_classification(Goal, Module, Class) :-      % SWI-Prolog
 2354    strip_module(Goal, _, PGoal),
 2355    current_predicate(_, user:PGoal),
 2356    !,
 2357    (   Module == user
 2358    ->  Class = global(GClass, Location),
 2359        global_location(user:Goal, Location),
 2360        global_class(user:Goal, GClass)
 2361    ;   Class = global
 2362    ).
 2363global_goal_classification(Goal, _, Class) :-
 2364    compound(Goal),
 2365    compound_name_arity(Goal, Name, Arity),
 2366    vararg_goal_classification(Name, Arity, Class).
 2367
 2368global_location(Goal, File:Line) :-
 2369    predicate_property(Goal, file(File)),
 2370    predicate_property(Goal, line_count(Line)),
 2371    !.
 2372global_location(_, -).
 2373
 2374global_class(Goal, dynamic)   :- predicate_property(Goal, dynamic), !.
 2375global_class(Goal, multifile) :- predicate_property(Goal, multifile), !.
 2376global_class(Goal, tabled)    :- predicate_property(Goal, tabled), !.
 2377global_class(_,    static).
 vararg_goal_classification(+Name, +Arity, -Class) is semidet
Multifile hookable classification for vararg predicates.
 2384vararg_goal_classification(call, Arity, built_in) :-
 2385    Arity >= 1.
 2386vararg_goal_classification(send_super, Arity, expanded) :- % XPCE (TBD)
 2387    Arity >= 2.
 2388vararg_goal_classification(get_super, Arity, expanded) :-  % XPCE (TBD)
 2389    Arity >= 3.
 qualified_goal_classification(:Goal, +TB, -Class)
Classify an explicitly qualified goal.
 2395qualified_goal_classification(Goal, TB, Class) :-
 2396    goal_classification(TB, Goal, [], Class),
 2397    Class \== undefined,
 2398    !.
 2399qualified_goal_classification(Module:Goal, _, extern(Module, How)) :-
 2400    predicate_property(Module:Goal, visible),
 2401    !,
 2402    (   (   predicate_property(Module:Goal, public)
 2403        ;   predicate_property(Module:Goal, exported)
 2404        )
 2405    ->  How = (public)
 2406    ;   How = (private)
 2407    ).
 2408qualified_goal_classification(Module:_, _, extern(Module, unknown)).
 classify_head(+TB, +Head, -Class)
Classify a clause head
 2414classify_head(TB, Goal, exported) :-
 2415    colour_state_source_id(TB, SourceId),
 2416    xref_exported(SourceId, Goal),
 2417    !.
 2418classify_head(_TB, Goal, hook) :-
 2419    xref_hook(Goal),
 2420    !.
 2421classify_head(TB, Goal, hook) :-
 2422    colour_state_source_id(TB, SourceId),
 2423    xref_module(SourceId, M),
 2424    xref_hook(M:Goal),
 2425    !.
 2426classify_head(TB, Goal, Class) :-
 2427    built_in_predicate(Goal),
 2428    (   system_module(TB)
 2429    ->  (   predicate_property(system:Goal, iso)
 2430        ->  Class = def_iso
 2431        ;   goal_name(Goal, Name),
 2432            \+ sub_atom(Name, 0, _, _, $)
 2433        ->  Class = def_swi
 2434        )
 2435    ;   (   predicate_property(system:Goal, iso)
 2436        ->  Class = iso
 2437        ;   Class = built_in
 2438        )
 2439    ).
 2440classify_head(TB, Goal, unreferenced) :-
 2441    colour_state_source_id(TB, SourceId),
 2442    \+ (xref_called(SourceId, Goal, By), By \= Goal),
 2443    !.
 2444classify_head(TB, Goal, test) :-
 2445    Goal = test(_),
 2446    colour_state_source_id(TB, SourceId),
 2447    xref_called(SourceId, Goal, '<test_unit>'(_Unit)),
 2448    !.
 2449classify_head(TB, Goal, test) :-
 2450    Goal = test(_, _),
 2451    colour_state_source_id(TB, SourceId),
 2452    xref_called(SourceId, Goal, '<test_unit>'(_Unit)),
 2453    !.
 2454classify_head(TB, Goal, How) :-
 2455    colour_state_source_id(TB, SourceId),
 2456    (   xref_defined(SourceId, Goal, imported(From))
 2457    ->  How = imported(From)
 2458    ;   xref_defined(SourceId, Goal, How)
 2459    ),
 2460    !.
 2461classify_head(_TB, _Goal, undefined).
 2462
 2463built_in_predicate(Goal) :-
 2464    predicate_property(system:Goal, built_in),
 2465    !.
 2466built_in_predicate(module(_, _)).       % reserved expanded constructs
 2467built_in_predicate(module(_, _, _)).
 2468built_in_predicate(if(_)).
 2469built_in_predicate(elif(_)).
 2470built_in_predicate(else).
 2471built_in_predicate(endif).
 2472
 2473goal_name(_:G, Name) :- nonvar(G), !, goal_name(G, Name).
 2474goal_name(G, Name) :- callable(G), functor_name(G, Name).
 2475
 2476system_module(TB) :-
 2477    colour_state_source_id(TB, SourceId),
 2478    xref_module(SourceId, M),
 2479    module_property(M, class(system)).
 2480
 2481generalise_term(Specific, General) :-
 2482    (   compound(Specific)
 2483    ->  compound_name_arity(Specific, Name, Arity),
 2484        compound_name_arity(General0, Name, Arity),
 2485        General = General0
 2486    ;   General = Specific
 2487    ).
 2488
 2489rename_goal(Goal0, Name, Goal) :-
 2490    (   compound(Goal0)
 2491    ->  compound_name_arity(Goal0, _, Arity),
 2492        compound_name_arity(Goal, Name, Arity)
 2493    ;   Goal = Name
 2494    ).
 2495
 2496functor_name(Term, Name) :-
 2497    (   compound(Term)
 2498    ->  compound_name_arity(Term, Name, _)
 2499    ;   atom(Term)
 2500    ->  Name = Term
 2501    ).
 2502
 2503goal_name_arity(Goal, Name, Arity) :-
 2504    (   compound(Goal)
 2505    ->  compound_name_arity(Goal, Name, Arity)
 2506    ;   atom(Goal)
 2507    ->  Name = Goal, Arity = 0
 2508    ).
 2509
 2510
 2511call_goal_colours(Term, Colours) :-
 2512    goal_colours(Term, Colours),
 2513    !.
 2514call_goal_colours(Term, Colours) :-
 2515    def_goal_colours(Term, Colours).
 2516
 2517call_goal_colours(Term, Class, Colours) :-
 2518    goal_colours(Term, Class, Colours),
 2519    !.
 2520%call_goal_colours(Term, Class, Colours) :-
 2521%    def_goal_colours(Term, Class, Colours).
 2522
 2523
 2524%       Specify colours for individual goals.
 2525
 2526def_goal_colours(_ is _,                 built_in-[classify,expression]).
 2527def_goal_colours(_ < _,                  built_in-[expression,expression]).
 2528def_goal_colours(_ > _,                  built_in-[expression,expression]).
 2529def_goal_colours(_ =< _,                 built_in-[expression,expression]).
 2530def_goal_colours(_ >= _,                 built_in-[expression,expression]).
 2531def_goal_colours(_ =\= _,                built_in-[expression,expression]).
 2532def_goal_colours(_ =:= _,                built_in-[expression,expression]).
 2533def_goal_colours(module(_,_),            built_in-[identifier,exports]).
 2534def_goal_colours(module(_,_,_),          built_in-[identifier,exports,langoptions]).
 2535def_goal_colours(use_module(_),          built_in-[imported_file]).
 2536def_goal_colours(use_module(File,_),     built_in-[file,imports(File)]).
 2537def_goal_colours(autoload(_),            built_in-[imported_file]).
 2538def_goal_colours(autoload(File,_),       built_in-[file,imports(File)]).
 2539def_goal_colours(reexport(_),            built_in-[file]).
 2540def_goal_colours(reexport(File,_),       built_in-[file,imports(File)]).
 2541def_goal_colours(dynamic(_),             built_in-[declarations(dynamic)]).
 2542def_goal_colours(thread_local(_),        built_in-[declarations(thread_local)]).
 2543def_goal_colours(module_transparent(_),  built_in-[declarations(module_transparent)]).
 2544def_goal_colours(discontiguous(_),       built_in-[declarations(discontiguous)]).
 2545def_goal_colours(multifile(_),           built_in-[declarations(multifile)]).
 2546def_goal_colours(volatile(_),            built_in-[declarations(volatile)]).
 2547def_goal_colours(public(_),              built_in-[declarations(public)]).
 2548def_goal_colours(det(_),                 built_in-[declarations(det)]).
 2549def_goal_colours(table(_),               built_in-[declarations(table)]).
 2550def_goal_colours(meta_predicate(_),      built_in-[meta_declarations]).
 2551def_goal_colours(consult(_),             built_in-[file]).
 2552def_goal_colours(include(_),             built_in-[file]).
 2553def_goal_colours(ensure_loaded(_),       built_in-[file]).
 2554def_goal_colours(load_files(_),          built_in-[file]).
 2555def_goal_colours(load_files(_,_),        built_in-[file,options]).
 2556def_goal_colours(setof(_,_,_),           built_in-[classify,setof,classify]).
 2557def_goal_colours(bagof(_,_,_),           built_in-[classify,setof,classify]).
 2558def_goal_colours(predicate_options(_,_,_), built_in-[predicate,classify,classify]).
 2559% Database access
 2560def_goal_colours(assert(_),              built_in-[db]).
 2561def_goal_colours(asserta(_),             built_in-[db]).
 2562def_goal_colours(assertz(_),             built_in-[db]).
 2563def_goal_colours(assert(_,_),            built_in-[db,classify]).
 2564def_goal_colours(asserta(_,_),           built_in-[db,classify]).
 2565def_goal_colours(assertz(_,_),           built_in-[db,classify]).
 2566def_goal_colours(retract(_),             built_in-[db]).
 2567def_goal_colours(retractall(_),          built_in-[db]).
 2568def_goal_colours(clause(_,_),            built_in-[db,classify]).
 2569def_goal_colours(clause(_,_,_),          built_in-[db,classify,classify]).
 2570% misc
 2571def_goal_colours(set_prolog_flag(_,_),   built_in-[prolog_flag_name,classify]).
 2572def_goal_colours(current_prolog_flag(_,_), built_in-[prolog_flag_name,classify]).
 2573% XPCE stuff
 2574def_goal_colours(pce_autoload(_,_),      classify-[classify,file]).
 2575def_goal_colours(pce_image_directory(_), classify-[directory]).
 2576def_goal_colours(new(_, _),              built_in-[classify,pce_new]).
 2577def_goal_colours(send_list(_,_,_),       built_in-pce_arg_list).
 2578def_goal_colours(send(_,_),              built_in-[pce_arg,pce_selector]).
 2579def_goal_colours(get(_,_,_),             built_in-[pce_arg,pce_selector,pce_arg]).
 2580def_goal_colours(send_super(_,_),        built_in-[pce_arg,pce_selector]).
 2581def_goal_colours(get_super(_,_),         built_in-[pce_arg,pce_selector,pce_arg]).
 2582def_goal_colours(get_chain(_,_,_),       built_in-[pce_arg,pce_selector,pce_arg]).
 2583def_goal_colours(Pce,                    built_in-pce_arg) :-
 2584    compound(Pce),
 2585    functor_name(Pce, Functor),
 2586    pce_functor(Functor).
 2587
 2588pce_functor(send).
 2589pce_functor(get).
 2590pce_functor(send_super).
 2591pce_functor(get_super).
 2592
 2593
 2594                 /*******************************
 2595                 *        SPECIFIC HEADS        *
 2596                 *******************************/
 2597
 2598head_colours(file_search_path(_,_), hook-[identifier,classify]).
 2599head_colours(library_directory(_),  hook-[file]).
 2600head_colours(resource(_,_),         hook-[identifier,file]).
 2601head_colours(resource(_,_,_),       hook-[identifier,file,classify]).
 2602
 2603head_colours(Var, _) :-
 2604    var(Var),
 2605    !,
 2606    fail.
 2607head_colours(M:H, Colours) :-
 2608    M == user,
 2609    head_colours(H, HC),
 2610    HC = hook - _,
 2611    !,
 2612    Colours = meta-[module(user), HC ].
 2613head_colours(M:H, Colours) :-
 2614    atom(M), callable(H),
 2615    xref_hook(M:H),
 2616    !,
 2617    Colours = meta-[module(M), hook-classify ].
 2618head_colours(M:_, meta-[module(M),extern(M)]).
 2619
 2620
 2621                 /*******************************
 2622                 *             STYLES           *
 2623                 *******************************/
 def_style(+Pattern, -Style)
Define the style used for the given pattern. Definitions here can be overruled by defining rules for style/2
 2631def_style(goal(built_in,_),        [colour(blue)]).
 2632def_style(goal(imported(_),_),     [colour(blue)]).
 2633def_style(goal(autoload(_),_),     [colour(navy_blue)]).
 2634def_style(goal(global,_),          [colour(navy_blue)]).
 2635def_style(goal(global(dynamic,_),_), [colour(magenta)]).
 2636def_style(goal(global(_,_),_),     [colour(navy_blue)]).
 2637def_style(goal(undefined,_),       [colour(red)]).
 2638def_style(goal(thread_local(_),_), [colour(magenta), underline(true)]).
 2639def_style(goal(dynamic(_),_),      [colour(magenta)]).
 2640def_style(goal(multifile(_),_),    [colour(navy_blue)]).
 2641def_style(goal(expanded,_),        [colour(blue), underline(true)]).
 2642def_style(goal(extern(_),_),       [colour(blue), underline(true)]).
 2643def_style(goal(extern(_,private),_), [colour(red)]).
 2644def_style(goal(extern(_,public),_), [colour(blue)]).
 2645def_style(goal(recursion,_),       [underline(true)]).
 2646def_style(goal(meta,_),            [colour(red4)]).
 2647def_style(goal(foreign(_),_),      [colour(darkturquoise)]).
 2648def_style(goal(local(_),_),        []).
 2649def_style(goal(constraint(_),_),   [colour(darkcyan)]).
 2650def_style(goal(not_callable,_),    [background(orange)]).
 2651
 2652def_style(function,                [colour(blue)]).
 2653def_style(no_function,             [colour(red)]).
 2654
 2655def_style(option_name,             [colour('#3434ba')]).
 2656def_style(no_option_name,          [colour(red)]).
 2657
 2658def_style(neck(_),		   [bold(true)]).
 2659
 2660def_style(head(exported,_),        [colour(blue), bold(true)]).
 2661def_style(head(public(_),_),       [colour('#016300'), bold(true)]).
 2662def_style(head(extern(_),_),       [colour(blue), bold(true)]).
 2663def_style(head(dynamic,_),         [colour(magenta), bold(true)]).
 2664def_style(head(multifile,_),       [colour(navy_blue), bold(true)]).
 2665def_style(head(unreferenced,_),    [colour(red), bold(true)]).
 2666def_style(head(hook,_),            [colour(blue), underline(true)]).
 2667def_style(head(meta,_),            []).
 2668def_style(head(constraint(_),_),   [colour(darkcyan), bold(true)]).
 2669def_style(head(imported(_),_),     [colour(darkgoldenrod4), bold(true)]).
 2670def_style(head(built_in,_),        [background(orange), bold(true)]).
 2671def_style(head(iso,_),             [background(orange), bold(true)]).
 2672def_style(head(def_iso,_),         [colour(blue), bold(true)]).
 2673def_style(head(def_swi,_),         [colour(blue), bold(true)]).
 2674def_style(head(test,_),            [colour('#01bdbd'), bold(true)]).
 2675def_style(head(_,_),               [bold(true)]).
 2676def_style(rule_condition,	   [background('#d4ffe3')]).
 2677
 2678def_style(module(_),               [colour(dark_slate_blue)]).
 2679def_style(comment(_),              [colour(dark_green)]).
 2680
 2681def_style(directive,               [background(grey90)]).
 2682def_style(method(_),               [bold(true)]).
 2683
 2684def_style(var,                     [colour(red4)]).
 2685def_style(singleton,               [bold(true), colour(red4)]).
 2686def_style(unbound,                 [colour(red), bold(true)]).
 2687def_style(quoted_atom,             [colour(navy_blue)]).
 2688def_style(string,                  [colour(navy_blue)]).
 2689def_style(rational(_),		   [colour(steel_blue)]).
 2690def_style(codes,                   [colour(navy_blue)]).
 2691def_style(chars,                   [colour(navy_blue)]).
 2692def_style(nofile,                  [colour(red)]).
 2693def_style(file(_),                 [colour(blue), underline(true)]).
 2694def_style(file_no_depend(_),       [colour(blue), underline(true), background(pink)]).
 2695def_style(directory(_),            [colour(blue)]).
 2696def_style(class(built_in,_),       [colour(blue), underline(true)]).
 2697def_style(class(library(_),_),     [colour(navy_blue), underline(true)]).
 2698def_style(class(local(_,_,_),_),   [underline(true)]).
 2699def_style(class(user(_),_),        [underline(true)]).
 2700def_style(class(user,_),           [underline(true)]).
 2701def_style(class(undefined,_),      [colour(red), underline(true)]).
 2702def_style(prolog_data,             [colour(blue), underline(true)]).
 2703def_style(flag_name(_),            [colour(blue)]).
 2704def_style(known_flag_name(_),      [colour(blue), background(pink)]).
 2705def_style(no_flag_name(_),         [colour(red)]).
 2706def_style(unused_import,           [colour(blue), background(pink)]).
 2707def_style(undefined_import,        [colour(red)]).
 2708
 2709def_style(constraint(_),           [colour(darkcyan)]).
 2710
 2711def_style(keyword(_),              [colour(blue)]).
 2712def_style(identifier,              [bold(true)]).
 2713def_style(delimiter,               [bold(true)]).
 2714def_style(expanded,                [colour(blue), underline(true)]).
 2715def_style(hook(_),                 [colour(blue), underline(true)]).
 2716def_style(op_type(_),              [colour(blue)]).
 2717
 2718def_style(qq_type,                 [bold(true)]).
 2719def_style(qq(_),                   [colour(blue), bold(true)]).
 2720def_style(qq_content(_),           [colour(red4)]).
 2721
 2722def_style(dict_tag,                [bold(true)]).
 2723def_style(dict_key,                [bold(true)]).
 2724def_style(dict_function(_),        [colour(navy_blue)]).
 2725def_style(dict_return_op,          [colour(blue)]).
 2726
 2727def_style(hook,                    [colour(blue), underline(true)]).
 2728def_style(dcg_right_hand_ctx,      [background('#d4ffe3')]).
 2729
 2730def_style(error,                   [background(orange)]).
 2731def_style(type_error(_),           [background(orange)]).
 2732def_style(domain_error(_),         [background(orange)]).
 2733def_style(syntax_error(_,_),       [background(orange)]).
 2734def_style(instantiation_error,     [background(orange)]).
 2735
 2736def_style(decl_option(_),	   [bold(true)]).
 2737def_style(table_mode(_),	   [bold(true)]).
 2738
 2739def_style(macro(_),                [colour(blue), underline(true)]).
 syntax_colour(?Class, ?Attributes) is nondet
True when a range classified Class must be coloured using Attributes. Attributes is a list of:

Attributes may be the empty list. This is used for cases where -for example- a menu is associated with the fragment. If syntax_colour/2 fails, no fragment is created for the region.

 2755syntax_colour(Class, Attributes) :-
 2756    (   style(Class, Attributes)            % user hook
 2757    ;   def_style(Class, Attributes)        % system default
 2758    ).
 term_colours(+Term, -FunctorColour, -ArgColours)
Define colourisation for specific terms.
 2765term_colours((?- Directive), Colours) :-
 2766    term_colours((:- Directive), Colours).
 2767term_colours((prolog:Head --> _),
 2768             neck(-->) - [ expanded - [ module(prolog),
 2769                                        hook(message) - [ identifier
 2770                                                        ]
 2771                                      ],
 2772                           dcg_body(prolog:Head)
 2773                         ]) :-
 2774    prolog_message_hook(Head).
 2775
 2776prolog_message_hook(message(_)).
 2777prolog_message_hook(deprecated(_)).
 2778prolog_message_hook(error_message(_)).
 2779prolog_message_hook(message_context(_)).
 2780prolog_message_hook(message_location(_)).
 2781
 2782%       XPCE rules
 2783
 2784term_colours(variable(_, _, _, _),
 2785             expanded - [ identifier,
 2786                          classify,
 2787                          classify,
 2788                          comment(string)
 2789                        ]).
 2790term_colours(variable(_, _, _),
 2791             expanded - [ identifier,
 2792                          classify,
 2793                          atom
 2794                        ]).
 2795term_colours(handle(_, _, _),
 2796             expanded - [ classify,
 2797                          classify,
 2798                          classify
 2799                        ]).
 2800term_colours(handle(_, _, _, _),
 2801             expanded - [ classify,
 2802                          classify,
 2803                          classify,
 2804                          classify
 2805                        ]).
 2806term_colours(class_variable(_,_,_,_),
 2807             expanded - [ identifier,
 2808                          pce(type),
 2809                          pce(default),
 2810                          comment(string)
 2811                        ]).
 2812term_colours(class_variable(_,_,_),
 2813             expanded - [ identifier,
 2814                          pce(type),
 2815                          pce(default)
 2816                        ]).
 2817term_colours(delegate_to(_),
 2818             expanded - [ classify
 2819                        ]).
 2820term_colours((:- encoding(_)),
 2821             expanded - [ expanded - [ classify
 2822                                     ]
 2823                        ]).
 2824term_colours((:- pce_begin_class(_, _, _)),
 2825             expanded - [ expanded - [ identifier,
 2826                                       pce_new,
 2827                                       comment(string)
 2828                                     ]
 2829                        ]).
 2830term_colours((:- pce_begin_class(_, _)),
 2831             expanded - [ expanded - [ identifier,
 2832                                       pce_new
 2833                                     ]
 2834                        ]).
 2835term_colours((:- pce_extend_class(_)),
 2836             expanded - [ expanded - [ identifier
 2837                                     ]
 2838                        ]).
 2839term_colours((:- pce_end_class),
 2840             expanded - [ expanded
 2841                        ]).
 2842term_colours((:- pce_end_class(_)),
 2843             expanded - [ expanded - [ identifier
 2844                                     ]
 2845                        ]).
 2846term_colours((:- use_class_template(_)),
 2847             expanded - [ expanded - [ pce_new
 2848                                     ]
 2849                        ]).
 2850term_colours((:- emacs_begin_mode(_,_,_,_,_)),
 2851             expanded - [ expanded - [ identifier,
 2852                                       classify,
 2853                                       classify,
 2854                                       classify,
 2855                                       classify
 2856                                     ]
 2857                        ]).
 2858term_colours((:- emacs_extend_mode(_,_)),
 2859             expanded - [ expanded - [ identifier,
 2860                                       classify
 2861                                     ]
 2862                        ]).
 2863term_colours((:- pce_group(_)),
 2864             expanded - [ expanded - [ identifier
 2865                                     ]
 2866                        ]).
 2867term_colours((:- pce_global(_, new(_))),
 2868             expanded - [ expanded - [ identifier,
 2869                                       pce_arg
 2870                                     ]
 2871                        ]).
 2872term_colours((:- emacs_end_mode),
 2873             expanded - [ expanded
 2874                        ]).
 2875term_colours(pce_ifhostproperty(_,_),
 2876             expanded - [ classify,
 2877                          classify
 2878                        ]).
 2879term_colours((_,_),
 2880             error - [ classify,
 2881                       classify
 2882                     ]).
 specified_item(+Specified, +Term, +TB, +TermPosition) is det
Colourise an item that is explicitly classified by the user using term_colours/2 or goal_colours/2.
 2889specified_item(_Class, _Term, _TB, Pos) :-
 2890    var(Pos),
 2891    !.
 2892specified_item(Class, Term, TB, parentheses_term_position(PO,PC,Pos)) :-
 2893    !,
 2894    colour_item(parentheses, TB, PO-PC),
 2895    specified_item(Class, Term, TB, Pos).
 2896specified_item(_, Var, TB, Pos) :-
 2897    (   var(Var)
 2898    ;   qq_position(Pos)
 2899    ),
 2900    !,
 2901    colourise_term_arg(Var, TB, Pos).
 2902                                        % generic classification
 2903specified_item(classify, Term, TB, Pos) :-
 2904    !,
 2905    colourise_term_arg(Term, TB, Pos).
 2906                                        % classify as head
 2907specified_item(head, Term, TB, Pos) :-
 2908    !,
 2909    colourise_clause_head(Term, TB, Pos).
 2910                                        % expanded head (DCG=2, ...)
 2911specified_item(head(+N), Term, TB, Pos) :-
 2912    !,
 2913    colourise_extended_head(Term, N, TB, Pos).
 2914                                        % M:Head
 2915specified_item(extern(M), Term, TB, Pos) :-
 2916    !,
 2917    colourise_extern_head(Term, M, TB, Pos).
 2918                                        % classify as body
 2919specified_item(body, Term, TB, Pos) :-
 2920    !,
 2921    colourise_body(Term, TB, Pos).
 2922specified_item(body(Goal), _Term0, TB, Pos) :-
 2923    !,
 2924    colourise_body(Goal, TB, Pos).
 2925specified_item(dcg_body(Head), Term, TB, Pos) :-
 2926    !,
 2927    colourise_dcg(Term, Head, TB, Pos).
 2928specified_item(setof, Term, TB, Pos) :-
 2929    !,
 2930    colourise_setof(Term, TB, Pos).
 2931specified_item(meta(MetaSpec), Term, TB, Pos) :-
 2932    !,
 2933    colourise_meta_arg(MetaSpec, Term, TB, Pos).
 2934                                        % DCG goal in body
 2935specified_item(dcg, Term, TB, Pos) :-
 2936    !,
 2937    colourise_dcg(Term, [], TB, Pos).
 2938                                        % assert/retract arguments
 2939specified_item(db, Term, TB, Pos) :-
 2940    !,
 2941    colourise_db(Term, TB, Pos).
 2942                                        % error(Error)
 2943specified_item(error(Error), _Term, TB, Pos) :-
 2944    colour_item(Error, TB, Pos).
 2945                                        % files
 2946specified_item(file(Path), _Term, TB, Pos) :-
 2947    !,
 2948    colour_item(file(Path), TB, Pos).
 2949specified_item(file, Term, TB, Pos) :-
 2950    !,
 2951    colourise_files(Term, TB, Pos, any).
 2952specified_item(imported_file, Term, TB, Pos) :-
 2953    !,
 2954    colourise_files(Term, TB, Pos, imported).
 2955specified_item(langoptions, Term, TB, Pos) :-
 2956    !,
 2957    colourise_langoptions(Term, TB, Pos).
 2958specified_item(expression, Term, TB, Pos) :-
 2959    !,
 2960    colourise_expression(Term, TB, Pos).
 2961                                        % directory
 2962specified_item(directory, Term, TB, Pos) :-
 2963    !,
 2964    colourise_directory(Term, TB, Pos).
 2965                                        % [Name/Arity, ...]
 2966specified_item(exports, Term, TB, Pos) :-
 2967    !,
 2968    colourise_exports(Term, TB, Pos).
 2969                                        % [Name/Arity, ...]
 2970specified_item(imports(File), Term, TB, Pos) :-
 2971    !,
 2972    colourise_imports(Term, File, TB, Pos).
 2973                                        % Name/Arity
 2974specified_item(import(File), Term, TB, Pos) :-
 2975    !,
 2976    colourise_import(Term, File, TB, Pos).
 2977                                        % Name/Arity, ...
 2978specified_item(predicates, Term, TB, Pos) :-
 2979    !,
 2980    colourise_declarations(Term, predicate_indicator, TB, Pos).
 2981                                        % Name/Arity
 2982specified_item(predicate, Term, TB, Pos) :-
 2983    !,
 2984    colourise_declaration(Term, predicate_indicator, TB, Pos).
 2985                                        % head(Arg, ...)
 2986specified_item(meta_declarations, Term, TB, Pos) :-
 2987    !,
 2988    colourise_meta_declarations(Term, [], TB, Pos).
 2989specified_item(meta_declarations(Extra), Term, TB, Pos) :-
 2990    !,
 2991    colourise_meta_declarations(Term, Extra, TB, Pos).
 2992specified_item(declarations(Which), Term, TB, Pos) :-
 2993    !,
 2994    colourise_declarations(Term, Which, TB, Pos).
 2995                                        % set_prolog_flag(Name, _)
 2996specified_item(prolog_flag_name, Term, TB, Pos) :-
 2997    !,
 2998    colourise_prolog_flag_name(Term, TB, Pos).
 2999                                        % XPCE new argument
 3000specified_item(pce_new, Term, TB, Pos) :-
 3001    !,
 3002    (   atom(Term)
 3003    ->  colourise_class(Term, TB, Pos)
 3004    ;   compound(Term)
 3005    ->  functor_name(Term, Class),
 3006        Pos = term_position(_,_,FF, FT, ArgPos),
 3007        colourise_class(Class, TB, FF-FT),
 3008        specified_items(pce_arg, Term, TB, ArgPos)
 3009    ;   colourise_term_arg(Term, TB, Pos)
 3010    ).
 3011                                        % Generic XPCE arguments
 3012specified_item(pce_arg, new(X), TB,
 3013               term_position(_,_,_,_,[ArgPos])) :-
 3014    !,
 3015    specified_item(pce_new, X, TB, ArgPos).
 3016specified_item(pce_arg, new(X, T), TB,
 3017               term_position(_,_,_,_,[P1, P2])) :-
 3018    !,
 3019    colourise_term_arg(X, TB, P1),
 3020    specified_item(pce_new, T, TB, P2).
 3021specified_item(pce_arg, @(Ref), TB, Pos) :-
 3022    !,
 3023    colourise_term_arg(@(Ref), TB, Pos).
 3024specified_item(pce_arg, prolog(Term), TB,
 3025               term_position(_,_,FF,FT,[ArgPos])) :-
 3026    !,
 3027    colour_item(prolog_data, TB, FF-FT),
 3028    colourise_term_arg(Term, TB, ArgPos).
 3029specified_item(pce_arg, Term, TB, Pos) :-
 3030    compound(Term),
 3031    Term \= [_|_],
 3032    \+ is_dict(Term),
 3033    !,
 3034    specified_item(pce_new, Term, TB, Pos).
 3035specified_item(pce_arg, Term, TB, Pos) :-
 3036    !,
 3037    colourise_term_arg(Term, TB, Pos).
 3038                                        % List of XPCE arguments
 3039specified_item(pce_arg_list, List, TB, list_position(F,T,Elms,Tail)) :-
 3040    !,
 3041    colour_item(list, TB, F-T),
 3042    colourise_list_args(Elms, Tail, List, TB, pce_arg).
 3043specified_item(pce_arg_list, Term, TB, Pos) :-
 3044    !,
 3045    specified_item(pce_arg, Term, TB, Pos).
 3046                                        % XPCE selector
 3047specified_item(pce_selector, Term, TB,
 3048               term_position(_,_,_,_,ArgPos)) :-
 3049    !,
 3050    specified_items(pce_arg, Term, TB, ArgPos).
 3051specified_item(pce_selector, Term, TB, Pos) :-
 3052    colourise_term_arg(Term, TB, Pos).
 3053                                        % Nested specification
 3054specified_item(FuncSpec-ArgSpecs, Term, TB,
 3055               term_position(_,_,FF,FT,ArgPos)) :-
 3056    !,
 3057    specified_item(FuncSpec, Term, TB, FF-FT),
 3058    specified_items(ArgSpecs, Term, TB, ArgPos).
 3059                                        % Nested for {...}
 3060specified_item(FuncSpec-[ArgSpec], {Term}, TB,
 3061               brace_term_position(F,T,ArgPos)) :-
 3062    !,
 3063    specified_item(FuncSpec, {Term}, TB, F-T),
 3064    specified_item(ArgSpec, Term, TB, ArgPos).
 3065                                        % Specified
 3066specified_item(FuncSpec-ElmSpec, List, TB,
 3067               list_position(F,T,ElmPos,TailPos)) :-
 3068    !,
 3069    colour_item(FuncSpec, TB, F-T),
 3070    specified_list(ElmSpec, List, TB, ElmPos, TailPos).
 3071specified_item(Class, _, TB, Pos) :-
 3072    colour_item(Class, TB, Pos).
 specified_items(+Spec, +Term, +TB, +PosList)
 3076specified_items(Specs, Term, TB, PosList) :-
 3077    is_dict(Term),
 3078    !,
 3079    specified_dict_kv(PosList, Term, TB, Specs).
 3080specified_items(Specs, Term, TB, PosList) :-
 3081    is_list(Specs),
 3082    !,
 3083    specified_arglist(Specs, 1, Term, TB, PosList).
 3084specified_items(Spec, Term, TB, PosList) :-
 3085    specified_argspec(PosList, Spec, 1, Term, TB).
 3086
 3087
 3088specified_arglist([], _, _, _, _).
 3089specified_arglist(_, _, _, _, []) :- !.         % Excess specification args
 3090specified_arglist([S0|ST], N, T, TB, [P0|PT]) :-
 3091    (   S0 == options,
 3092        colourization_module(TB, Module),
 3093        colourise_option_arg(T, Module, N, TB, P0)
 3094    ->  true
 3095    ;   arg(N, T, Term),
 3096        specified_item(S0, Term, TB, P0)
 3097    ),
 3098    NN is N + 1,
 3099    specified_arglist(ST, NN, T, TB, PT).
 3100
 3101specified_argspec([], _, _, _, _).
 3102specified_argspec([P0|PT], Spec, N, T, TB) :-
 3103    arg(N, T, Term),
 3104    specified_item(Spec, Term, TB, P0),
 3105    NN is N + 1,
 3106    specified_argspec(PT, Spec, NN, T, TB).
 3107
 3108
 3109%       specified_list(+Spec, +List, +TB, +PosList, TailPos)
 3110
 3111specified_list([], [], _, [], _).
 3112specified_list([HS|TS], [H|T], TB, [HP|TP], TailPos) :-
 3113    !,
 3114    specified_item(HS, H, TB, HP),
 3115    specified_list(TS, T, TB, TP, TailPos).
 3116specified_list(Spec, [H|T], TB, [HP|TP], TailPos) :-
 3117    specified_item(Spec, H, TB, HP),
 3118    specified_list(Spec, T, TB, TP, TailPos).
 3119specified_list(_, _, _, [], none) :- !.
 3120specified_list(Spec, Tail, TB, [], TailPos) :-
 3121    specified_item(Spec, Tail, TB, TailPos).
 specified_dict_kv(+PosList, +Term, +TB, +Specs)
Arguments:
Specs- is a list of dict_kv(+Key, +KeySpec, +ArgSpec)
 3127specified_dict_kv([], _, _, _).
 3128specified_dict_kv([key_value_position(_F,_T,SF,ST,K,KP,VP)|Pos],
 3129                  Dict, TB, Specs) :-
 3130    specified_dict_kv1(K, Specs, KeySpec, ValueSpec),
 3131    colour_item(KeySpec, TB, KP),
 3132    colour_item(dict_sep, TB, SF-ST),
 3133    get_dict(K, Dict, V),
 3134    specified_item(ValueSpec, V, TB, VP),
 3135    specified_dict_kv(Pos, Dict, TB, Specs).
 3136
 3137specified_dict_kv1(Key, Specs, KeySpec, ValueSpec) :-
 3138    Specs = [_|_],
 3139    memberchk(dict_kv(Key, KeySpec, ValueSpec), Specs),
 3140    !.
 3141specified_dict_kv1(Key, dict_kv(Key2, KeySpec, ValueSpec), KeySpec, ValueSpec) :-
 3142    \+ Key \= Key2,
 3143    !.              % do not bind Key2
 3144specified_dict_kv1(_, _, dict_key, classify).
 3145
 3146
 3147                 /*******************************
 3148                 *         DESCRIPTIONS         *
 3149                 *******************************/
 3150
 3151syntax_message(Class) -->
 3152    message(Class),
 3153    !.
 3154syntax_message(qq(_)) -->
 3155    [ 'Quasi quote delimiter' ].
 3156syntax_message(qq_type) -->
 3157    [ 'Quasi quote type term' ].
 3158syntax_message(qq_content(Type)) -->
 3159    [ 'Quasi quote content (~w syntax)'-[Type] ].
 3160syntax_message(goal(Class, Goal)) -->
 3161    !,
 3162    goal_message(Class, Goal).
 3163syntax_message(class(Type, Class)) -->
 3164    !,
 3165    xpce_class_message(Type, Class).
 3166syntax_message(dict_return_op) -->
 3167    !,
 3168    [ ':= separates function from return value' ].
 3169syntax_message(dict_function) -->
 3170    !,
 3171    [ 'Function on a dict' ].
 3172syntax_message(ext_quant) -->
 3173    !,
 3174    [ 'Existential quantification operator' ].
 3175syntax_message(hook(message)) -->
 3176    [ 'Rule for print_message/2' ].
 3177syntax_message(module(Module)) -->
 3178    (   { current_module(Module) }
 3179    ->  (   { module_property(Module, file(File)) }
 3180        ->  [ 'Module ~w defined in ~w'-[Module,File] ]
 3181        ;   [ 'Module ~w'-[Module] ]
 3182        )
 3183    ;   [ 'Module ~w (not loaded)'-[Module] ]
 3184    ).
 3185syntax_message(decl_option(incremental)) -->
 3186    [ 'Keep affected tables consistent' ].
 3187syntax_message(decl_option(abstract)) -->
 3188    [ 'Add abstracted goal to table dependency graph' ].
 3189syntax_message(decl_option(volatile)) -->
 3190    [ 'Do not include predicate in a saved program' ].
 3191syntax_message(decl_option(multifile)) -->
 3192    [ 'Clauses are spread over multiple files' ].
 3193syntax_message(decl_option(discontiguous)) -->
 3194    [ 'Clauses are not contiguous' ].
 3195syntax_message(decl_option(private)) -->
 3196    [ 'Tables or clauses are private to a thread' ].
 3197syntax_message(decl_option(local)) -->
 3198    [ 'Tables or clauses are private to a thread' ].
 3199syntax_message(decl_option(shared)) -->
 3200    [ 'Tables or clauses are shared between threads' ].
 3201syntax_message(decl_option(_Opt)) -->
 3202    [ 'Predicate property' ].
 3203syntax_message(rational(Value)) -->
 3204    [ 'Rational number ~w'-[Value] ].
 3205syntax_message(rule_condition) -->
 3206    [ 'Guard' ].
 3207syntax_message(neck(=>)) -->
 3208    [ 'Rule' ].
 3209syntax_message(neck(-->)) -->
 3210    [ 'Grammar rule' ].
 3211syntax_message(macro(String)) -->
 3212    [ 'Macro indicator (expands to ~s)'-[String] ].
 3213syntax_message(flag_name(Name)) -->
 3214    [ 'Prolog flag ~w'-[Name] ].
 3215syntax_message(known_flag_name(Name)) -->
 3216    [ 'Prolog flag ~w (not set; known)'-[Name] ].
 3217syntax_message(no_flag_name(Name)) -->
 3218    [ 'Prolog flag ~w (not set)'-[Name] ].
 3219
 3220goal_message(meta, _) -->
 3221    [ 'Meta call' ].
 3222goal_message(not_callable, _) -->
 3223    [ 'Goal is not callable (type error)' ].
 3224goal_message(expanded, _) -->
 3225    [ 'Expanded goal' ].
 3226goal_message(Class, Goal) -->
 3227    { predicate_name(Goal, PI) },
 3228    [ 'Call to ~q'-PI ],
 3229    goal_class(Class).
 3230
 3231goal_class(recursion) -->
 3232    [ ' (recursive call)' ].
 3233goal_class(undefined) -->
 3234    [ ' (undefined)' ].
 3235goal_class(global) -->
 3236    [ ' (Auto-imported from module user)' ].
 3237goal_class(global(Class, File:Line)) -->
 3238    [ ' (~w in user module from '-[Class], url(File:Line), ')' ].
 3239goal_class(global(Class, source_location(File,Line))) -->
 3240    [ ' (~w in user module from '-[Class], url(File:Line), ')' ].
 3241goal_class(global(Class, -)) -->
 3242    [ ' (~w in user module)'-[Class] ].
 3243goal_class(imported(From)) -->
 3244    [ ' (imported from ~q)'-[From] ].
 3245goal_class(extern(_, private)) -->
 3246    [ ' (WARNING: private predicate)' ].
 3247goal_class(extern(_, public)) -->
 3248    [ ' (public predicate)' ].
 3249goal_class(extern(_)) -->
 3250    [ ' (cross-module call)' ].
 3251goal_class(Class) -->
 3252    [ ' (~p)'-[Class] ].
 3253
 3254xpce_class_message(Type, Class) -->
 3255    [ 'XPCE ~w class ~q'-[Type, Class] ]