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-2021, 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:- autoload(library(apply),[maplist/3]).   48:- autoload(library(debug),[debug/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(Head, TB, Pos) :-
  760    head_colours(Head, ClassSpec-ArgSpecs),
  761    !,
  762    functor_position(Pos, FPos, ArgPos),
  763    (   ClassSpec == classify
  764    ->  classify_head(TB, Head, Class)
  765    ;   Class = ClassSpec
  766    ),
  767    colour_item(head_term(Class, Head), TB, Pos),
  768    colour_item(head(Class, Head), TB, FPos),
  769    specified_items(ArgSpecs, Head, TB, ArgPos).
  770colourise_clause_head(:=(Eval, Ret), TB,
  771                      term_position(_,_,AF,AT,
  772                                    [ term_position(_,_,SF,ST,
  773                                                    [ SelfPos,
  774                                                      FuncPos
  775                                                    ]),
  776                                      RetPos
  777                                    ])) :-
  778    Eval =.. [.,M,Func],
  779    FuncPos = term_position(_,_,FF,FT,_),
  780    !,
  781    colourise_term_arg(M, TB, SelfPos),
  782    colour_item(func_dot, TB, SF-ST),               % .
  783    colour_item(dict_function(Func), TB, FF-FT),
  784    colourise_term_args(Func, TB, FuncPos),
  785    colour_item(dict_return_op, TB, AF-AT),         % :=
  786    colourise_term_arg(Ret, TB, RetPos).
  787colourise_clause_head(Head, TB, Pos) :-
  788    functor_position(Pos, FPos, _),
  789    classify_head(TB, Head, Class),
  790    colour_item(head_term(Class, Head), TB, Pos),
  791    colour_item(head(Class, Head), TB, FPos),
  792    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.
  799colourise_extern_head(Head, M, TB, Pos) :-
  800    functor_position(Pos, FPos, _),
  801    colour_item(head(extern(M), Head), TB, FPos),
  802    colourise_term_args(Head, TB, Pos).
  803
  804colour_method_head(SGHead, TB, Pos) :-
  805    arg(1, SGHead, Head),
  806    functor_name(SGHead, SG),
  807    functor_position(Pos, FPos, _),
  808    colour_item(method(SG), TB, FPos),
  809    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'.
  816functor_position(term_position(_,_,FF,FT,ArgPos), FF-FT, ArgPos) :- !.
  817functor_position(list_position(F,_T,Elms,none), F-FT, Elms) :-
  818    !,
  819    FT is F + 1.
  820functor_position(dict_position(_,_,FF,FT,KVPos), FF-FT, KVPos) :- !.
  821functor_position(brace_term_position(F,T,Arg), F-T, [Arg]) :- !.
  822functor_position(Pos, Pos, []).
  823
  824colourise_module(Term, TB, Pos) :-
  825    (   var(Term)
  826    ;   atom(Term)
  827    ),
  828    !,
  829    colour_item(module(Term), TB, Pos).
  830colourise_module(_, TB, Pos) :-
  831    colour_item(type_error(module), TB, Pos).
 colourise_directive(+Body, +TB, +Pos)
Colourise the body of a directive.
  837colourise_directive(_,_,Pos) :-
  838    var(Pos),
  839    !.
  840colourise_directive(Dir, TB, parentheses_term_position(PO,PC,Pos)) :-
  841    !,
  842    colour_item(parentheses, TB, PO-PC),
  843    colourise_directive(Dir, TB, Pos).
  844colourise_directive((A,B), TB, term_position(_,_,_,_,[PA,PB])) :-
  845    !,
  846    colourise_directive(A, TB, PA),
  847    colourise_directive(B, TB, PB).
  848colourise_directive(Body, TB, Pos) :-
  849    nonvar(Body),
  850    directive_colours(Body, ClassSpec-ArgSpecs),   % specified
  851    !,
  852    functor_position(Pos, FPos, ArgPos),
  853    (   ClassSpec == classify
  854    ->  goal_classification(TB, Body, [], Class)
  855    ;   Class = ClassSpec
  856    ),
  857    colour_item(goal(Class, Body), TB, FPos),
  858    specified_items(ArgSpecs, Body, TB, ArgPos).
  859colourise_directive(Body, TB, Pos) :-
  860    colourise_body(Body, TB, Pos).
  861
  862
  863%       colourise_body(+Body, +TB, +Pos)
  864%
  865%       Breaks down to colourise_goal/3.
  866
  867colourise_body(Body, TB, Pos) :-
  868    colourise_body(Body, [], TB, Pos).
  869
  870colourise_body(Body, Origin, TB, Pos) :-
  871    colour_item(body, TB, Pos),
  872    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.
  881colourise_method_body(_, _, Pos) :-
  882    var(Pos),
  883    !.
  884colourise_method_body(Body, TB, parentheses_term_position(PO,PC,Pos)) :-
  885    !,
  886    colour_item(parentheses, TB, PO-PC),
  887    colourise_method_body(Body, TB, Pos).
  888colourise_method_body(::(_Comment,Body), TB,
  889                      term_position(_F,_T,_FF,_FT,[CP,BP])) :-
  890    !,
  891    colour_item(comment(string), TB, CP),
  892    colourise_body(Body, TB, BP).
  893colourise_method_body(Body, TB, Pos) :-         % deal with pri(::) < 1000
  894    Body =.. [F,A,B],
  895    control_op(F),
  896    !,
  897    Pos = term_position(_F,_T,FF,FT,
  898                        [ AP,
  899                          BP
  900                        ]),
  901    colour_item(control, TB, FF-FT),
  902    colourise_method_body(A, TB, AP),
  903    colourise_body(B, TB, BP).
  904colourise_method_body(Body, TB, Pos) :-
  905    colourise_body(Body, TB, Pos).
  906
  907control_op(',').
  908control_op((;)).
  909control_op((->)).
  910control_op((*->)).
 colourise_goals(+Body, +Origin, +TB, +Pos)
Colourise the goals in a body.
  916colourise_goals(_, _, _, Pos) :-
  917    var(Pos),
  918    !.
  919colourise_goals(Body, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
  920    !,
  921    colour_item(parentheses, TB, PO-PC),
  922    colourise_goals(Body, Origin, TB, Pos).
  923colourise_goals(Body, Origin, TB, term_position(_,_,FF,FT,ArgPos)) :-
  924    body_compiled(Body),
  925    !,
  926    colour_item(control, TB, FF-FT),
  927    colourise_subgoals(ArgPos, 1, Body, Origin, TB).
  928colourise_goals(Goal, Origin, TB, Pos) :-
  929    colourise_goal(Goal, Origin, TB, Pos).
  930
  931colourise_subgoals([], _, _, _, _).
  932colourise_subgoals([Pos|T], N, Body, Origin, TB) :-
  933    arg(N, Body, Arg),
  934    colourise_goals(Arg, Origin, TB, Pos),
  935    NN is N + 1,
  936    colourise_subgoals(T, NN, Body, Origin, TB).
 colourise_dcg(+Body, +Head, +TB, +Pos)
Breaks down to colourise_dcg_goal/3.
  942colourise_dcg(Body, Head, TB, Pos) :-
  943    colour_item(dcg, TB, Pos),
  944    (   dcg_extend(Head, Origin)
  945    ->  true
  946    ;   Origin = Head
  947    ),
  948    colourise_dcg_goals(Body, Origin, TB, Pos).
  949
  950colourise_dcg_goals(Var, _, TB, Pos) :-
  951    var(Var),
  952    !,
  953    colour_item(goal(meta,Var), TB, Pos).
  954colourise_dcg_goals(_, _, _, Pos) :-
  955    var(Pos),
  956    !.
  957colourise_dcg_goals(Body, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
  958    !,
  959    colour_item(parentheses, TB, PO-PC),
  960    colourise_dcg_goals(Body, Origin, TB, Pos).
  961colourise_dcg_goals({Body}, Origin, TB, brace_term_position(F,T,Arg)) :-
  962    !,
  963    colour_item(dcg(plain), TB, F-T),
  964    colourise_goals(Body, Origin, TB, Arg).
  965colourise_dcg_goals([], _, TB, Pos) :-
  966    !,
  967    colour_item(dcg(terminal), TB, Pos).
  968colourise_dcg_goals(List, _, TB, list_position(F,T,Elms,Tail)) :-
  969    List = [_|_],
  970    !,
  971    colour_item(dcg(terminal), TB, F-T),
  972    colourise_list_args(Elms, Tail, List, TB, classify).
  973colourise_dcg_goals(_, _, TB, string_position(F,T)) :-
  974    integer(F),
  975    !,
  976    colour_item(dcg(string), TB, F-T).
  977colourise_dcg_goals(Body, Origin, TB, term_position(_,_,FF,FT,ArgPos)) :-
  978    dcg_body_compiled(Body),       % control structures
  979    !,
  980    colour_item(control, TB, FF-FT),
  981    colourise_dcg_subgoals(ArgPos, 1, Body, Origin, TB).
  982colourise_dcg_goals(Goal, Origin, TB, Pos) :-
  983    colourise_dcg_goal(Goal, Origin, TB, Pos).
  984
  985colourise_dcg_subgoals([], _, _, _, _).
  986colourise_dcg_subgoals([Pos|T], N, Body, Origin, TB) :-
  987    arg(N, Body, Arg),
  988    colourise_dcg_goals(Arg, Origin, TB, Pos),
  989    NN is N + 1,
  990    colourise_dcg_subgoals(T, NN, Body, Origin, TB).
  991
  992dcg_extend(Term, _) :-
  993    var(Term), !, fail.
  994dcg_extend(M:Term, M:Goal) :-
  995    dcg_extend(Term, Goal).
  996dcg_extend(Term, Goal) :-
  997    compound(Term),
  998    !,
  999    compound_name_arguments(Term, Name, Args),
 1000    append(Args, [_,_], NArgs),
 1001    compound_name_arguments(Goal, Name, NArgs).
 1002dcg_extend(Term, Goal) :-
 1003    atom(Term),
 1004    !,
 1005    compound_name_arguments(Goal, Term, [_,_]).
 1006
 1007dcg_body_compiled(G) :-
 1008    body_compiled(G),
 1009    !.
 1010dcg_body_compiled((_|_)).
 1011
 1012%       colourise_dcg_goal(+Goal, +Origin, +TB, +Pos).
 1013
 1014colourise_dcg_goal(!, Origin, TB, TermPos) :-
 1015    !,
 1016    colourise_goal(!, Origin, TB, TermPos).
 1017colourise_dcg_goal(Goal, Origin, TB, TermPos) :-
 1018    dcg_extend(Goal, TheGoal),
 1019    !,
 1020    colourise_goal(TheGoal, Origin, TB, TermPos).
 1021colourise_dcg_goal(Goal, _, TB, Pos) :-
 1022    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.
 1033                                        % Deal with list as goal (consult)
 1034colourise_goal(_,_,_,Pos) :-
 1035    var(Pos),
 1036    !.
 1037colourise_goal(Goal, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
 1038    !,
 1039    colour_item(parentheses, TB, PO-PC),
 1040    colourise_goal(Goal, Origin, TB, Pos).
 1041colourise_goal(Goal, _, TB, Pos) :-
 1042    Pos = list_position(F,T,Elms,TailPos),
 1043    Goal = [_|_],
 1044    !,
 1045    FT is F + 1,
 1046    AT is T - 1,
 1047    colour_item(goal_term(built_in, Goal), TB, Pos),
 1048    colour_item(goal(built_in, Goal), TB, F-FT),
 1049    colour_item(goal(built_in, Goal), TB, AT-T),
 1050    colourise_file_list(Goal, TB, Elms, TailPos, any).
 1051colourise_goal(Goal, Origin, TB, Pos) :-
 1052    Pos = list_position(F,T,Elms,Tail),
 1053    callable(Goal),
 1054    Goal =.. [_,GH,GT|_],
 1055    !,
 1056    goal_classification(TB, Goal, Origin, Class),
 1057    FT is F + 1,
 1058    AT is T - 1,
 1059    colour_item(goal_term(Class, Goal), TB, Pos),
 1060    colour_item(goal(Class, Goal), TB, F-FT),
 1061    colour_item(goal(Class, Goal), TB, AT-T),
 1062    colourise_list_args(Elms, Tail, [GH|GT], TB, classify).
 1063colourise_goal(Goal, _Origin, TB, Pos) :-
 1064    Pos = quasi_quotation_position(_F,_T,_QQType,_QQTypePos,_CPos),
 1065    !,
 1066    colourise_term_arg(Goal, TB, Pos).
 1067colourise_goal(Goal, Origin, TB, Pos) :-
 1068    strip_module(Goal, _, PGoal),
 1069    nonvar(PGoal),
 1070    (   goal_classification(TB, Goal, Origin, ClassInferred),
 1071        call_goal_colours(Goal, ClassInferred, ClassSpec-ArgSpecs)
 1072    ->  true
 1073    ;   call_goal_colours(Goal, ClassSpec-ArgSpecs)
 1074    ),
 1075    !,                                          % specified
 1076    functor_position(Pos, FPos, ArgPos),
 1077    (   ClassSpec == classify
 1078    ->  goal_classification(TB, Goal, Origin, Class)
 1079    ;   Class = ClassSpec
 1080    ),
 1081    colour_item(goal_term(Class, Goal), TB, Pos),
 1082    colour_item(goal(Class, Goal), TB, FPos),
 1083    colour_dict_braces(TB, Pos),
 1084    specified_items(ArgSpecs, Goal, TB, ArgPos).
 1085colourise_goal(Module:Goal, _Origin, TB, QGoalPos) :-
 1086    QGoalPos = term_position(_,_,QF,QT,[PM,PG]),
 1087    !,
 1088    colourise_module(Module, TB, PM),
 1089    colour_item(functor, TB, QF-QT),
 1090    (   PG = term_position(_,_,FF,FT,_)
 1091    ->  FP = FF-FT
 1092    ;   FP = PG
 1093    ),
 1094    (   callable(Goal)
 1095    ->  qualified_goal_classification(Module:Goal, TB, Class),
 1096        colour_item(goal_term(Class, Goal), TB, QGoalPos),
 1097        colour_item(goal(Class, Goal), TB, FP),
 1098        colourise_goal_args(Goal, Module, TB, PG)
 1099    ;   var(Goal)
 1100    ->  colourise_term_arg(Goal, TB, PG)
 1101    ;   colour_item(type_error(callable), TB, PG)
 1102    ).
 1103colourise_goal(Op, _Origin, TB, Pos) :-
 1104    nonvar(Op),
 1105    Op = op(_,_,_),
 1106    !,
 1107    colourise_op_declaration(Op, TB, Pos).
 1108colourise_goal(Goal, Origin, TB, Pos) :-
 1109    goal_classification(TB, Goal, Origin, Class),
 1110    (   Pos = term_position(_,_,FF,FT,_ArgPos)
 1111    ->  FPos = FF-FT
 1112    ;   FPos = Pos
 1113    ),
 1114    colour_item(goal_term(Class, Goal), TB, Pos),
 1115    colour_item(goal(Class, Goal), TB, FPos),
 1116    colourise_goal_args(Goal, TB, Pos).
 1117
 1118% make sure to emit a fragment for the braces of tag{k:v, ...} or
 1119% {...} that is mapped to something else.
 1120
 1121colour_dict_braces(TB, dict_position(_F,T,_TF,TT,_KVPos)) :-
 1122    !,
 1123    BStart is TT+1,
 1124    colour_item(dict_content, TB, BStart-T).
 1125colour_dict_braces(TB, brace_term_position(F,T,_Arg)) :-
 1126    !,
 1127    colour_item(brace_term, TB, F-T).
 1128colour_dict_braces(_, _).
 colourise_goal_args(+Goal, +TB, +Pos)
Colourise the arguments to a goal. This predicate deals with meta- and database-access predicates.
 1135colourise_goal_args(Goal, TB, Pos) :-
 1136    colourization_module(TB, Module),
 1137    colourise_goal_args(Goal, Module, TB, Pos).
 1138
 1139colourization_module(TB, Module) :-
 1140    (   colour_state_source_id(TB, SourceId),
 1141        xref_module(SourceId, Module)
 1142    ->  true
 1143    ;   Module = user
 1144    ).
 1145
 1146colourise_goal_args(Goal, M, TB, term_position(_,_,_,_,ArgPos)) :-
 1147    !,
 1148    (   meta_args(Goal, TB, MetaArgs)
 1149    ->  colourise_meta_args(1, Goal, M, MetaArgs, TB, ArgPos)
 1150    ;   colourise_goal_args(1, Goal, M, TB, ArgPos)
 1151    ).
 1152colourise_goal_args(Goal, M, TB, brace_term_position(_,_,ArgPos)) :-
 1153    !,
 1154    (   meta_args(Goal, TB, MetaArgs)
 1155    ->  colourise_meta_args(1, Goal, M, MetaArgs, TB, [ArgPos])
 1156    ;   colourise_goal_args(1, Goal, M, TB, [ArgPos])
 1157    ).
 1158colourise_goal_args(_, _, _, _).                % no arguments
 1159
 1160colourise_goal_args(_, _, _, _, []) :- !.
 1161colourise_goal_args(N, Goal, Module, TB, [P0|PT]) :-
 1162    colourise_option_arg(Goal, Module, N, TB, P0),
 1163    !,
 1164    NN is N + 1,
 1165    colourise_goal_args(NN, Goal, Module, TB, PT).
 1166colourise_goal_args(N, Goal, Module, TB, [P0|PT]) :-
 1167    arg(N, Goal, Arg),
 1168    colourise_term_arg(Arg, TB, P0),
 1169    NN is N + 1,
 1170    colourise_goal_args(NN, Goal, Module, TB, PT).
 1171
 1172
 1173colourise_meta_args(_, _, _, _, _, []) :- !.
 1174colourise_meta_args(N, Goal, Module, MetaArgs, TB, [P0|PT]) :-
 1175    colourise_option_arg(Goal, Module, N, TB, P0),
 1176    !,
 1177    NN is N + 1,
 1178    colourise_meta_args(NN, Goal, Module, MetaArgs, TB, PT).
 1179colourise_meta_args(N, Goal, Module, MetaArgs, TB, [P0|PT]) :-
 1180    arg(N, Goal, Arg),
 1181    arg(N, MetaArgs, MetaSpec),
 1182    colourise_meta_arg(MetaSpec, Arg, TB, P0),
 1183    NN is N + 1,
 1184    colourise_meta_args(NN, Goal, Module, MetaArgs, TB, PT).
 1185
 1186colourise_meta_arg(MetaSpec, Arg, TB, Pos) :-
 1187    nonvar(Arg),
 1188    expand_meta(MetaSpec, Arg, Expanded),
 1189    !,
 1190    colourise_goal(Expanded, [], TB, Pos). % TBD: recursion
 1191colourise_meta_arg(MetaSpec, Arg, TB, Pos) :-
 1192    nonvar(Arg),
 1193    MetaSpec == //,
 1194    !,
 1195    colourise_dcg_goals(Arg, //, TB, Pos).
 1196colourise_meta_arg(_, Arg, TB, Pos) :-
 1197    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.

 1210meta_args(Goal, TB, VarGoal) :-
 1211    colour_state_source_id(TB, SourceId),
 1212    xref_meta(SourceId, Goal, _),
 1213    !,
 1214    compound_name_arity(Goal, Name, Arity),
 1215    compound_name_arity(VarGoal, Name, Arity),
 1216    xref_meta(SourceId, VarGoal, MetaArgs),
 1217    instantiate_meta(MetaArgs).
 1218
 1219instantiate_meta([]).
 1220instantiate_meta([H|T]) :-
 1221    (   var(H)
 1222    ->  H = 0
 1223    ;   H = V+N
 1224    ->  V = N
 1225    ;   H = //(V)
 1226    ->  V = (//)
 1227    ),
 1228    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).
 1235expand_meta(MetaSpec, Goal, Goal) :-
 1236    MetaSpec == 0.
 1237expand_meta(MetaSpec, M:Goal, M:Expanded) :-
 1238    atom(M),
 1239    !,
 1240    expand_meta(MetaSpec, Goal, Expanded).
 1241expand_meta(MetaSpec, Goal, Expanded) :-
 1242    integer(MetaSpec),
 1243    MetaSpec > 0,
 1244    (   atom(Goal)
 1245    ->  functor(Expanded, Goal, MetaSpec)
 1246    ;   compound(Goal)
 1247    ->  compound_name_arguments(Goal, Name, Args0),
 1248        length(Extra, MetaSpec),
 1249        append(Args0, Extra, Args),
 1250        compound_name_arguments(Expanded, Name, Args)
 1251    ).
 colourise_setof(+Term, +TB, +Pos)
Colourise the 2nd argument of setof/bagof
 1257colourise_setof(Var^G, TB, term_position(_,_,FF,FT,[VP,GP])) :-
 1258    !,
 1259    colourise_term_arg(Var, TB, VP),
 1260    colour_item(ext_quant, TB, FF-FT),
 1261    colourise_setof(G, TB, GP).
 1262colourise_setof(Term, TB, Pos) :-
 1263    colourise_goal(Term, [], TB, Pos).
 1264
 1265%       colourise_db(+Arg, +TB, +Pos)
 1266%
 1267%       Colourise database modification calls (assert/1, retract/1 and
 1268%       friends.
 1269
 1270colourise_db((Head:-Body), TB, term_position(_,_,_,_,[HP,BP])) :-
 1271    !,
 1272    colourise_db(Head, TB, HP),
 1273    colourise_body(Body, Head, TB, BP).
 1274colourise_db(Module:Head, TB, term_position(_,_,QF,QT,[MP,HP])) :-
 1275    !,
 1276    colourise_module(Module, TB, MP),
 1277    colour_item(functor, TB, QF-QT),
 1278    (   atom(Module),
 1279        colour_state_source_id(TB, SourceId),
 1280        xref_module(SourceId, Module)
 1281    ->  colourise_db(Head, TB, HP)
 1282    ;   colourise_db(Head, TB, HP)
 1283    ).
 1284colourise_db(Head, TB, Pos) :-
 1285    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
 1294colourise_option_arg(Goal, Module, Arg, TB, ArgPos) :-
 1295    goal_name_arity(Goal, Name, Arity),
 1296    current_option_arg(Module:Name/Arity, Arg),
 1297    current_predicate_options(Module:Name/Arity, Arg, OptionDecl),
 1298    debug(emacs, 'Colouring option-arg ~w of ~p',
 1299          [Arg, Module:Name/Arity]),
 1300    arg(Arg, Goal, Options),
 1301    colourise_option(Options, Module, Goal, Arg, OptionDecl, TB, ArgPos).
 1302
 1303colourise_option(Options0, Module, Goal, Arg, OptionDecl, TB, Pos0) :-
 1304    strip_option_module_qualifier(Goal, Module, Arg, TB,
 1305                                  Options0, Pos0, Options, Pos),
 1306    (   Pos = list_position(F, T, ElmPos, TailPos)
 1307    ->  colour_item(list, TB, F-T),
 1308        colourise_option_list(Options, OptionDecl, TB, ElmPos, TailPos)
 1309    ;   (   var(Options)
 1310        ;   Options == []
 1311        )
 1312    ->  colourise_term_arg(Options, TB, Pos)
 1313    ;   colour_item(type_error(list), TB, Pos)
 1314    ).
 1315
 1316strip_option_module_qualifier(Goal, Module, Arg, TB,
 1317                              M:Options, term_position(_,_,_,_,[MP,Pos]),
 1318                              Options, Pos) :-
 1319    predicate_property(Module:Goal, meta_predicate(Head)),
 1320    arg(Arg, Head, :),
 1321    !,
 1322    colourise_module(M, TB, MP).
 1323strip_option_module_qualifier(_, _, _, _,
 1324                              Options, Pos, Options, Pos).
 1325
 1326
 1327colourise_option_list(_, _, _, [], none) :- !.
 1328colourise_option_list(Tail, _, TB, [], TailPos) :-
 1329    !,
 1330    colourise_term_arg(Tail, TB, TailPos).
 1331colourise_option_list([H|T], OptionDecl, TB, [HPos|TPos], TailPos) :-
 1332    colourise_option(H, OptionDecl, TB, HPos),
 1333    colourise_option_list(T, OptionDecl, TB, TPos, TailPos).
 1334
 1335colourise_option(Opt, _, TB, Pos) :-
 1336    var(Opt),
 1337    !,
 1338    colourise_term_arg(Opt, TB, Pos).
 1339colourise_option(Opt, OptionDecl, TB, term_position(_,_,FF,FT,ValPosList)) :-
 1340    !,
 1341    generalise_term(Opt, GenOpt),
 1342    (   memberchk(GenOpt, OptionDecl)
 1343    ->  colour_item(option_name, TB, FF-FT),
 1344        Opt =.. [Name|Values],
 1345        GenOpt =.. [Name|Types],
 1346        colour_option_values(Values, Types, TB, ValPosList)
 1347    ;   colour_item(no_option_name, TB, FF-FT),
 1348        colourise_term_args(ValPosList, 1, Opt, TB)
 1349    ).
 1350colourise_option(_, _, TB, Pos) :-
 1351    colour_item(type_error(option), TB, Pos).
 1352
 1353colour_option_values([], [], _, _).
 1354colour_option_values([V0|TV], [T0|TT], TB, [P0|TP]) :-
 1355    (   (   var(V0)
 1356        ;   is_of_type(T0, V0)
 1357        ;   T0 = list(_),
 1358            member(E, V0),
 1359            var(E)
 1360        ;   functor(V0, '.', 2),
 1361            V0 \= [_|_]
 1362        )
 1363    ->  colourise_term_arg(V0, TB, P0)
 1364    ;   callable(V0),
 1365        (   T0 = callable
 1366        ->  N = 0
 1367        ;   T0 = (callable+N)
 1368        )
 1369    ->  colourise_meta_arg(N, V0, TB, P0)
 1370    ;   colour_item(type_error(T0), TB, P0)
 1371    ),
 1372    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
 1381colourise_files(List, TB, list_position(F,T,Elms,TailPos), Why) :-
 1382    !,
 1383    colour_item(list, TB, F-T),
 1384    colourise_file_list(List, TB, Elms, TailPos, Why).
 1385colourise_files(M:Spec, TB, term_position(_,_,_,_,[MP,SP]), Why) :-
 1386    !,
 1387    colourise_module(M, TB, MP),
 1388    colourise_files(Spec, TB, SP, Why).
 1389colourise_files(Var, TB, P, _) :-
 1390    var(Var),
 1391    !,
 1392    colour_item(var, TB, P).
 1393colourise_files(Spec0, TB, Pos, Why) :-
 1394    strip_module(Spec0, _, Spec),
 1395    (   colour_state_source_id(TB, Source),
 1396        prolog_canonical_source(Source, SourceId),
 1397        catch(xref_source_file(Spec, Path, SourceId, [silent(true)]),
 1398              _, fail)
 1399    ->  (   Why = imported,
 1400            \+ resolves_anything(TB, Path),
 1401            exports_something(TB, Path)
 1402        ->  colour_item(file_no_depend(Path), TB, Pos)
 1403        ;   colour_item(file(Path), TB, Pos)
 1404        )
 1405    ;   colour_item(nofile, TB, Pos)
 1406    ).
 colourise_file_list(+Files, +TB, +ElmPos, +TailPos, +Why)
 1410colourise_file_list([], _, [], none, _).
 1411colourise_file_list(Last, TB, [], TailPos, _Why) :-
 1412    (   var(Last)
 1413    ->  colourise_term(Last, TB, TailPos)
 1414    ;   colour_item(type_error(list), TB, TailPos)
 1415    ).
 1416colourise_file_list([H|T], TB, [PH|PT], TailPos, Why) :-
 1417    colourise_files(H, TB, PH, Why),
 1418    colourise_file_list(T, TB, PT, TailPos, Why).
 1419
 1420resolves_anything(TB, Path) :-
 1421    colour_state_source_id(TB, SourceId),
 1422    xref_defined(SourceId, Head, imported(Path)),
 1423    xref_called(SourceId, Head, _),
 1424    !.
 1425
 1426exports_something(TB, Path) :-
 1427    colour_state_source_id(TB, SourceId),
 1428    xref_defined(SourceId, _, imported(Path)),
 1429    !.
 colourise_directory(+Arg, +TB, +Pos)
Colourise argument that should be an existing directory.
 1435colourise_directory(Spec, TB, Pos) :-
 1436    (   colour_state_source_id(TB, SourceId),
 1437        catch(xref_source_file(Spec, Path, SourceId,
 1438                               [ file_type(directory),
 1439                                 silent(true)
 1440                               ]),
 1441              _, fail)
 1442    ->  colour_item(directory(Path), TB, Pos)
 1443    ;   colour_item(nofile, TB, Pos)
 1444    ).
 colourise_langoptions(+Term, +TB, +Pos) is det
Colourise the 3th argument of module/3
 1450colourise_langoptions([], _, _) :- !.
 1451colourise_langoptions([H|T], TB, list_position(PF,PT,[HP|TP],_)) :-
 1452    !,
 1453    colour_item(list, TB, PF-PT),
 1454    colourise_langoptions(H, TB, HP),
 1455    colourise_langoptions(T, TB, TP).
 1456colourise_langoptions(Spec, TB, Pos) :-
 1457    colourise_files(library(dialect/Spec), TB, Pos, imported).
 colourise_class(ClassName, TB, Pos)
Colourise an XPCE class.
 1463colourise_class(ClassName, TB, Pos) :-
 1464    colour_state_source_id(TB, SourceId),
 1465    classify_class(SourceId, ClassName, Classification),
 1466    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.
 1474classify_class(SourceId, Name, Class) :-
 1475    xref_defined_class(SourceId, Name, Class),
 1476    !.
 1477classify_class(_SourceId, Name, Class) :-
 1478    current_predicate(pce:send_class/3),
 1479    (   current_predicate(classify_class/2)
 1480    ->  true
 1481    ;   use_module(library(pce_meta), [classify_class/2])
 1482    ),
 1483    member(G, [classify_class(Name, Class)]),
 1484    call(G).
 colourise_term_args(+Term, +TB, +Pos)
colourise head/body principal terms.
 1490colourise_term_args(Term, TB,
 1491                    term_position(_,_,_,_,ArgPos)) :-
 1492    !,
 1493    colourise_term_args(ArgPos, 1, Term, TB).
 1494colourise_term_args(_, _, _).
 1495
 1496colourise_term_args([], _, _, _).
 1497colourise_term_args([Pos|T], N, Term, TB) :-
 1498    arg(N, Term, Arg),
 1499    colourise_term_arg(Arg, TB, Pos),
 1500    NN is N + 1,
 1501    colourise_term_args(T, NN, Term, TB).
 1502
 1503colourise_term_arg(_, _, Pos) :-
 1504    var(Pos),
 1505    !.
 1506colourise_term_arg(Arg, TB, parentheses_term_position(PO,PC,Pos)) :-
 1507    !,
 1508    colour_item(parentheses, TB, PO-PC),
 1509    colourise_term_arg(Arg, TB, Pos).
 1510colourise_term_arg(Var, TB, Pos) :-                     % variable
 1511    var(Var), Pos = _-_,
 1512    !,
 1513    (   singleton(Var, TB)
 1514    ->  colour_item(singleton, TB, Pos)
 1515    ;   colour_item(var, TB, Pos)
 1516    ).
 1517colourise_term_arg(List, TB, list_position(F, T, Elms, Tail)) :-
 1518    !,
 1519    colour_item(list, TB, F-T),
 1520    colourise_list_args(Elms, Tail, List, TB, classify).    % list
 1521colourise_term_arg(String, TB, string_position(F, T)) :-       % string
 1522    !,
 1523    (   string(String)
 1524    ->  colour_item(string, TB, F-T)
 1525    ;   String = [H|_]
 1526    ->  (   integer(H)
 1527        ->  colour_item(codes, TB, F-T)
 1528        ;   colour_item(chars, TB, F-T)
 1529        )
 1530    ;   String == []
 1531    ->  colour_item(codes, TB, F-T)
 1532    ).
 1533colourise_term_arg(_, TB,
 1534                   quasi_quotation_position(F,T,QQType,QQTypePos,CPos)) :-
 1535    !,
 1536    colourise_qq_type(QQType, TB, QQTypePos),
 1537    functor_name(QQType, Type),
 1538    colour_item(qq_content(Type), TB, CPos),
 1539    arg(1, CPos, SE),
 1540    SS is SE-2,
 1541    FE is F+2,
 1542    TS is T-2,
 1543    colour_item(qq(open),  TB, F-FE),
 1544    colour_item(qq(sep),   TB, SS-SE),
 1545    colour_item(qq(close), TB, TS-T).
 1546colourise_term_arg({Term}, TB, brace_term_position(F,T,Arg)) :-
 1547    !,
 1548    colour_item(brace_term, TB, F-T),
 1549    colourise_term_arg(Term, TB, Arg).
 1550colourise_term_arg(Map, TB, dict_position(F,T,TF,TT,KVPos)) :-
 1551    !,
 1552    is_dict(Map, Tag),
 1553    colour_item(dict, TB, F-T),
 1554    TagPos = TF-TT,
 1555    (   var(Tag)
 1556    ->  (   singleton(Tag, TB)
 1557        ->  colour_item(singleton, TB, TagPos)
 1558        ;   colour_item(var, TB, TagPos)
 1559        )
 1560    ;   colour_item(dict_tag, TB, TagPos)
 1561    ),
 1562    BStart is TT+1,
 1563    colour_item(dict_content, TB, BStart-T),
 1564    colourise_dict_kv(Map, TB, KVPos).
 1565colourise_term_arg([](List,Term), TB,                   % [] as operator
 1566                   term_position(_,_,0,0,[ListPos,ArgPos])) :-
 1567    !,
 1568    colourise_term_arg(List, TB, ListPos),
 1569    colourise_term_arg(Term, TB, ArgPos).
 1570colourise_term_arg(Compound, TB, Pos) :-                % compound
 1571    compound(Compound),
 1572    !,
 1573    (   Pos = term_position(_F,_T,FF,FT,_ArgPos)
 1574    ->  colour_item(functor, TB, FF-FT)             % TBD: Infix/Postfix?
 1575    ;   true                                        % TBD: When is this
 1576    ),
 1577    colourise_term_args(Compound, TB, Pos).
 1578colourise_term_arg(EmptyList, TB, Pos) :-
 1579    EmptyList == [],
 1580    !,
 1581    colour_item(empty_list, TB, Pos).
 1582colourise_term_arg(Atom, TB, Pos) :-
 1583    atom(Atom),
 1584    !,
 1585    colour_item(atom, TB, Pos).
 1586colourise_term_arg(Integer, TB, Pos) :-
 1587    integer(Integer),
 1588    !,
 1589    colour_item(int, TB, Pos).
 1590colourise_term_arg(Rational, TB, Pos) :-
 1591    rational(Rational),
 1592    !,
 1593    colour_item(rational(Rational), TB, Pos).
 1594colourise_term_arg(Float, TB, Pos) :-
 1595    float(Float),
 1596    !,
 1597    colour_item(float, TB, Pos).
 1598colourise_term_arg(_Arg, _TB, _Pos) :-
 1599    true.
 1600
 1601colourise_list_args([HP|TP], Tail, [H|T], TB, How) :-
 1602    specified_item(How, H, TB, HP),
 1603    colourise_list_args(TP, Tail, T, TB, How).
 1604colourise_list_args([], none, _, _, _) :- !.
 1605colourise_list_args([], TP, T, TB, How) :-
 1606    specified_item(How, T, TB, TP).
 colourise_qq_type(+QQType, +TB, +QQTypePos)
Colouring the type part of a quasi quoted term
 1612colourise_qq_type(QQType, TB, QQTypePos) :-
 1613    functor_position(QQTypePos, FPos, _),
 1614    colour_item(qq_type, TB, FPos),
 1615    colourise_term_args(QQType, TB, QQTypePos).
 1616
 1617qq_position(quasi_quotation_position(_,_,_,_,_)).
 colourise_dict_kv(+Dict, +TB, +KVPosList)
Colourise the name-value pairs in the dict
 1623colourise_dict_kv(_, _, []) :- !.
 1624colourise_dict_kv(Dict, TB, [key_value_position(_F,_T,SF,ST,K,KP,VP)|KV]) :-
 1625    colour_item(dict_key, TB, KP),
 1626    colour_item(dict_sep, TB, SF-ST),
 1627    get_dict(K, Dict, V),
 1628    colourise_term_arg(V, TB, VP),
 1629    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).
 1637colourise_exports([], TB, Pos) :- !,
 1638    colourise_term_arg([], TB, Pos).
 1639colourise_exports(List, TB, list_position(F,T,ElmPos,Tail)) :-
 1640    !,
 1641    colour_item(list, TB, F-T),
 1642    (   Tail == none
 1643    ->  true
 1644    ;   colour_item(type_error(list), TB, Tail)
 1645    ),
 1646    colourise_exports2(List, TB, ElmPos).
 1647colourise_exports(_, TB, Pos) :-
 1648    colour_item(type_error(list), TB, Pos).
 1649
 1650colourise_exports2([G0|GT], TB, [P0|PT]) :-
 1651    !,
 1652    colourise_declaration(G0, export, TB, P0),
 1653    colourise_exports2(GT, TB, PT).
 1654colourise_exports2(_, _, _).
 colourise_imports(+List, +File, +TB, +Pos)
Colourise import list from use_module/2, importing from File.
 1661colourise_imports(List, File, TB, Pos) :-
 1662    (   colour_state_source_id(TB, SourceId),
 1663        ground(File),
 1664        catch(xref_public_list(File, SourceId,
 1665                               [ path(Path),
 1666                                 public(Public),
 1667                                 silent(true)
 1668                               ] ), _, fail)
 1669    ->  true
 1670    ;   Public = [],
 1671        Path = (-)
 1672    ),
 1673    colourise_imports(List, Path, Public, TB, Pos).
 1674
 1675colourise_imports([], _, _, TB, Pos) :-
 1676    !,
 1677    colour_item(empty_list, TB, Pos).
 1678colourise_imports(List, File, Public, TB, list_position(F,T,ElmPos,Tail)) :-
 1679    !,
 1680    colour_item(list, TB, F-T),
 1681    (   Tail == none
 1682    ->  true
 1683    ;   colour_item(type_error(list), TB, Tail)
 1684    ),
 1685    colourise_imports2(List, File, Public, TB, ElmPos).
 1686colourise_imports(except(Except), File, Public, TB,
 1687                  term_position(_,_,FF,FT,[LP])) :-
 1688    !,
 1689    colour_item(keyword(except), TB, FF-FT),
 1690    colourise_imports(Except, File, Public, TB, LP).
 1691colourise_imports(_, _, _, TB, Pos) :-
 1692    colour_item(type_error(list), TB, Pos).
 1693
 1694colourise_imports2([G0|GT], File, Public, TB, [P0|PT]) :-
 1695    !,
 1696    colourise_import(G0, File, TB, P0),
 1697    colourise_imports2(GT, File, Public, TB, PT).
 1698colourise_imports2(_, _, _, _, _).
 1699
 1700
 1701colourise_import(PI as Name, File, TB, term_position(_,_,FF,FT,[PP,NP])) :-
 1702    pi_to_term(PI, Goal),
 1703    !,
 1704    colour_item(goal(imported(File), Goal), TB, PP),
 1705    rename_goal(Goal, Name, NewGoal),
 1706    goal_classification(TB, NewGoal, [], Class),
 1707    colour_item(goal(Class, NewGoal), TB, NP),
 1708    colour_item(keyword(as), TB, FF-FT).
 1709colourise_import(PI, File, TB, Pos) :-
 1710    pi_to_term(PI, Goal),
 1711    colour_state_source_id(TB, SourceID),
 1712    (   \+ xref_defined(SourceID, Goal, imported(File))
 1713    ->  colour_item(undefined_import, TB, Pos)
 1714    ;   \+ xref_called(SourceID, Goal, _)
 1715    ->  colour_item(unused_import, TB, Pos)
 1716    ),
 1717    !.
 1718colourise_import(PI, _, TB, Pos) :-
 1719    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.
 1726colourise_declaration(PI, _, TB, term_position(F,T,FF,FT,[NamePos,ArityPos])) :-
 1727    pi_to_term(PI, Goal),
 1728    !,
 1729    goal_classification(TB, Goal, [], Class),
 1730    colour_item(predicate_indicator(Class, Goal), TB, F-T),
 1731    colour_item(goal(Class, Goal), TB, NamePos),
 1732    colour_item(predicate_indicator, TB, FF-FT),
 1733    colour_item(arity, TB, ArityPos).
 1734colourise_declaration(Module:PI, _, TB,
 1735                      term_position(_,_,QF,QT,[PM,PG])) :-
 1736    atom(Module), pi_to_term(PI, Goal),
 1737    !,
 1738    colourise_module(M, TB, PM),
 1739    colour_item(functor, TB, QF-QT),
 1740    colour_item(predicate_indicator(extern(M), Goal), TB, PG),
 1741    PG = term_position(_,_,FF,FT,[NamePos,ArityPos]),
 1742    colour_item(goal(extern(M), Goal), TB, NamePos),
 1743    colour_item(predicate_indicator, TB, FF-FT),
 1744    colour_item(arity, TB, ArityPos).
 1745colourise_declaration(Module:PI, _, TB,
 1746                      term_position(_,_,QF,QT,[PM,PG])) :-
 1747    atom(Module), nonvar(PI), PI = Name/Arity,
 1748    !,                                  % partial predicate indicators
 1749    colourise_module(Module, TB, PM),
 1750    colour_item(functor, TB, QF-QT),
 1751    (   (var(Name) ; atom(Name)),
 1752        (var(Arity) ; integer(Arity), Arity >= 0)
 1753    ->  colourise_term_arg(PI, TB, PG)
 1754    ;   colour_item(type_error(predicate_indicator), TB, PG)
 1755    ).
 1756colourise_declaration(op(N,T,P), Which, TB, Pos) :-
 1757    (   Which == export
 1758    ;   Which == import
 1759    ),
 1760    !,
 1761    colour_item(exported_operator, TB, Pos),
 1762    colourise_op_declaration(op(N,T,P), TB, Pos).
 1763colourise_declaration(Module:Goal, table, TB,
 1764                      term_position(_,_,QF,QT,
 1765                                    [PM,term_position(_F,_T,FF,FT,ArgPos)])) :-
 1766    atom(Module), callable(Goal),
 1767    !,
 1768    colourise_module(Module, TB, PM),
 1769    colour_item(functor, TB, QF-QT),
 1770    goal_classification(TB, Module:Goal, [], Class),
 1771    compound_name_arguments(Goal, _, Args),
 1772    colour_item(goal(Class, Goal), TB, FF-FT),
 1773    colourise_table_modes(Args, TB, ArgPos).
 1774colourise_declaration(Goal, table, TB, term_position(_F,_T,FF,FT,ArgPos)) :-
 1775    callable(Goal),
 1776    !,
 1777    compound_name_arguments(Goal, _, Args),
 1778    goal_classification(TB, Goal, [], Class),
 1779    colour_item(goal(Class, Goal), TB, FF-FT),
 1780    colourise_table_modes(Args, TB, ArgPos).
 1781colourise_declaration(Goal, table, TB, Pos) :-
 1782    atom(Goal),
 1783    !,
 1784    goal_classification(TB, Goal, [], Class),
 1785    colour_item(goal(Class, Goal), TB, Pos).
 1786colourise_declaration(Partial, _Which, TB, Pos) :-
 1787    compatible_with_pi(Partial),
 1788    !,
 1789    colourise_term_arg(Partial, TB, Pos).
 1790colourise_declaration(_, Which, TB, Pos) :-
 1791    colour_item(type_error(declaration(Which)), TB, Pos).
 1792
 1793compatible_with_pi(Term) :-
 1794    var(Term),
 1795    !.
 1796compatible_with_pi(Name/Arity) :-
 1797    !,
 1798    var_or_atom(Name),
 1799    var_or_nonneg(Arity).
 1800compatible_with_pi(Name//Arity) :-
 1801    !,
 1802    var_or_atom(Name),
 1803    var_or_nonneg(Arity).
 1804compatible_with_pi(M:T) :-
 1805    var_or_atom(M),
 1806    compatible_with_pi(T).
 1807
 1808var_or_atom(X) :- var(X), !.
 1809var_or_atom(X) :- atom(X).
 1810var_or_nonneg(X) :- var(X), !.
 1811var_or_nonneg(X) :- integer(X), X >= 0, !.
 1812
 1813pi_to_term(Name/Arity, Term) :-
 1814    (atom(Name)->true;Name==[]), integer(Arity), Arity >= 0,
 1815    !,
 1816    functor(Term, Name, Arity).
 1817pi_to_term(Name//Arity0, Term) :-
 1818    atom(Name), integer(Arity0), Arity0 >= 0,
 1819    !,
 1820    Arity is Arity0 + 2,
 1821    functor(Term, Name, Arity).
 1822
 1823colourise_meta_declarations((Head,Tail), Extra, TB,
 1824                            term_position(_,_,_,_,[PH,PT])) :-
 1825    !,
 1826    colourise_meta_declaration(Head, Extra, TB, PH),
 1827    colourise_meta_declarations(Tail, Extra, TB, PT).
 1828colourise_meta_declarations(Last, Extra, TB, Pos) :-
 1829    colourise_meta_declaration(Last, Extra, TB, Pos).
 1830
 1831colourise_meta_declaration(M:Head, Extra, TB,
 1832                           term_position(_,_,QF,QT,
 1833                                         [ MP,
 1834                                           term_position(_,_,FF,FT,ArgPos)
 1835                                         ])) :-
 1836    compound(Head),
 1837    !,
 1838    colourise_module(M, TB, MP),
 1839    colour_item(functor, TB, QF-QT),
 1840    colour_item(goal(extern(M),Head), TB, FF-FT),
 1841    compound_name_arguments(Head, _, Args),
 1842    colourise_meta_decls(Args, Extra, TB, ArgPos).
 1843colourise_meta_declaration(Head, Extra, TB, term_position(_,_,FF,FT,ArgPos)) :-
 1844    compound(Head),
 1845    !,
 1846    goal_classification(TB, Head, [], Class),
 1847    colour_item(goal(Class, Head), TB, FF-FT),
 1848    compound_name_arguments(Head, _, Args),
 1849    colourise_meta_decls(Args, Extra, TB, ArgPos).
 1850colourise_meta_declaration([H|T], Extra, TB, list_position(LF,LT,[HP],TP)) :-
 1851    !,
 1852    colour_item(list, TB, LF-LT),
 1853    colourise_meta_decls([H,T], Extra, TB, [HP,TP]).
 1854colourise_meta_declaration(_, _, TB, Pos) :-
 1855    !,
 1856    colour_item(type_error(compound), TB, Pos).
 1857
 1858colourise_meta_decls([], _, _, []).
 1859colourise_meta_decls([Arg|ArgT], Extra, TB, [PosH|PosT]) :-
 1860    colourise_meta_decl(Arg, Extra, TB, PosH),
 1861    colourise_meta_decls(ArgT, Extra, TB, PosT).
 1862
 1863colourise_meta_decl(Arg, Extra, TB, Pos) :-
 1864    nonvar(Arg),
 1865    (   valid_meta_decl(Arg)
 1866    ->  true
 1867    ;   memberchk(Arg, Extra)
 1868    ),
 1869    colour_item(meta(Arg), TB, Pos).
 1870colourise_meta_decl(_, _, TB, Pos) :-
 1871    colour_item(error, TB, Pos).
 1872
 1873valid_meta_decl(:).
 1874valid_meta_decl(*).
 1875valid_meta_decl(//).
 1876valid_meta_decl(^).
 1877valid_meta_decl(?).
 1878valid_meta_decl(+).
 1879valid_meta_decl(-).
 1880valid_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..
 1887colourise_declarations(List, Which, TB, list_position(F,T,Elms,none)) :-
 1888    !,
 1889    colour_item(list, TB, F-T),
 1890    colourise_list_declarations(List, Which, TB, Elms).
 1891colourise_declarations(Term, Which, TB, parentheses_term_position(PO,PC,Pos)) :-
 1892    !,
 1893    colour_item(parentheses, TB, PO-PC),
 1894    colourise_declarations(Term, Which, TB, Pos).
 1895colourise_declarations((Head,Tail), Which, TB,
 1896                             term_position(_,_,_,_,[PH,PT])) :-
 1897    !,
 1898    colourise_declarations(Head, Which, TB, PH),
 1899    colourise_declarations(Tail, Which, TB, PT).
 1900colourise_declarations(as(Spec, Options), Which, TB,
 1901                             term_position(_,_,FF,FT,[PH,PT])) :-
 1902    !,
 1903    colour_item(keyword(as), TB, FF-FT),
 1904    colourise_declarations(Spec, Which, TB, PH),
 1905    colourise_decl_options(Options, Which, TB, PT).
 1906colourise_declarations(PI, Which, TB, Pos) :-
 1907    colourise_declaration(PI, Which, TB, Pos).
 1908
 1909colourise_list_declarations([], _, _, []).
 1910colourise_list_declarations([H|T], Which, TB, [HP|TP]) :-
 1911    colourise_declaration(H, Which, TB, HP),
 1912    colourise_list_declarations(T, Which, TB, TP).
 1913
 1914
 1915colourise_table_modes([], _, _).
 1916colourise_table_modes([H|T], TB, [PH|PT]) :-
 1917    colourise_table_mode(H, TB, PH),
 1918    colourise_table_modes(T, TB, PT).
 1919
 1920colourise_table_mode(H, TB, Pos) :-
 1921    table_mode(H, Mode),
 1922    !,
 1923    colour_item(table_mode(Mode), TB, Pos).
 1924colourise_table_mode(lattice(Spec), TB, term_position(_F,_T,FF,FT,[ArgPos])) :-
 1925    !,
 1926    colour_item(table_mode(lattice), TB, FF-FT),
 1927    table_moded_call(Spec, 3, TB, ArgPos).
 1928colourise_table_mode(po(Spec), TB, term_position(_F,_T,FF,FT,[ArgPos])) :-
 1929    !,
 1930    colour_item(table_mode(po), TB, FF-FT),
 1931    table_moded_call(Spec, 2, TB, ArgPos).
 1932colourise_table_mode(_, TB, Pos) :-
 1933    colour_item(type_error(table_mode), TB, Pos).
 1934
 1935table_mode(Var, index) :-
 1936    var(Var),
 1937    !.
 1938table_mode(+, index).
 1939table_mode(index, index).
 1940table_mode(-, first).
 1941table_mode(first, first).
 1942table_mode(last, last).
 1943table_mode(min, min).
 1944table_mode(max, max).
 1945table_mode(sum, sum).
 1946
 1947table_moded_call(Atom, Arity, TB, Pos) :-
 1948    atom(Atom),
 1949    functor(Head, Atom, Arity),
 1950    goal_classification(TB, Head, [], Class),
 1951    colour_item(goal(Class, Head), TB, Pos).
 1952table_moded_call(Atom/Arity, Arity, TB,
 1953                 term_position(_,_,FF,FT,[NP,AP])) :-
 1954    atom(Atom),
 1955    !,
 1956    functor(Head, Atom, Arity),
 1957    goal_classification(TB, Head, [], Class),
 1958    colour_item(goal(Class, Head), TB, NP),
 1959    colour_item(predicate_indicator, TB, FF-FT),
 1960    colour_item(arity, TB, AP).
 1961table_moded_call(Head, Arity, TB, Pos) :-
 1962    Pos = term_position(_,_,FF,FT,_),
 1963    compound(Head),
 1964    !,
 1965    compound_name_arity(Head, _Name, Arity),
 1966    goal_classification(TB, Head, [], Class),
 1967    colour_item(goal(Class, Head), TB, FF-FT),
 1968    colourise_term_args(Head, TB, Pos).
 1969table_moded_call(_, _, TB, Pos) :-
 1970    colour_item(type_error(predicate_name_or_indicator), TB, Pos).
 1971
 1972colourise_decl_options(Options, Which, TB,
 1973                       parentheses_term_position(_,_,Pos)) :-
 1974    !,
 1975    colourise_decl_options(Options, Which, TB, Pos).
 1976colourise_decl_options((Head,Tail), Which, TB,
 1977                        term_position(_,_,_,_,[PH,PT])) :-
 1978    !,
 1979    colourise_decl_options(Head, Which, TB, PH),
 1980    colourise_decl_options(Tail, Which, TB, PT).
 1981colourise_decl_options(Option, Which, TB, Pos) :-
 1982    ground(Option),
 1983    valid_decl_option(Option, Which),
 1984    !,
 1985    functor(Option, Name, _),
 1986    (   Pos = term_position(_,_,FF,FT,[ArgPos])
 1987    ->  colour_item(decl_option(Name), TB, FF-FT),
 1988        (   arg(1, Option, Value),
 1989            nonneg_or_false(Value)
 1990        ->  colourise_term_arg(Value, TB, ArgPos)
 1991        ;   colour_item(type_error(decl_option_value(Which)), TB, ArgPos)
 1992        )
 1993    ;   colour_item(decl_option(Name), TB, Pos)
 1994    ).
 1995colourise_decl_options(_, Which, TB, Pos) :-
 1996    colour_item(type_error(decl_option(Which)), TB, Pos).
 1997
 1998valid_decl_option(subsumptive,         table).
 1999valid_decl_option(variant,             table).
 2000valid_decl_option(incremental,         table).
 2001valid_decl_option(monotonic,           table).
 2002valid_decl_option(opaque,              table).
 2003valid_decl_option(lazy,                table).
 2004valid_decl_option(monotonic,           dynamic).
 2005valid_decl_option(incremental,         dynamic).
 2006valid_decl_option(abstract(_),         dynamic).
 2007valid_decl_option(opaque,              dynamic).
 2008valid_decl_option(shared,              table).
 2009valid_decl_option(private,             table).
 2010valid_decl_option(subgoal_abstract(_), table).
 2011valid_decl_option(answer_abstract(_),  table).
 2012valid_decl_option(max_answers(_),      table).
 2013valid_decl_option(shared,              dynamic).
 2014valid_decl_option(private,             dynamic).
 2015valid_decl_option(local,               dynamic).
 2016valid_decl_option(multifile,           _).
 2017valid_decl_option(discontiguous,       _).
 2018valid_decl_option(volatile,            _).
 2019
 2020nonneg_or_false(Value) :-
 2021    var(Value),
 2022    !.
 2023nonneg_or_false(Value) :-
 2024    integer(Value), Value >= 0,
 2025    !.
 2026nonneg_or_false(off).
 2027nonneg_or_false(false).
 colourise_op_declaration(Op, TB, Pos) is det
 2031colourise_op_declaration(op(P,T,N), TB, term_position(_,_,FF,FT,[PP,TP,NP])) :-
 2032    colour_item(goal(built_in, op(N,T,P)), TB, FF-FT),
 2033    colour_op_priority(P, TB, PP),
 2034    colour_op_type(T, TB, TP),
 2035    colour_op_name(N, TB, NP).
 2036
 2037colour_op_name(_, _, Pos) :-
 2038    var(Pos),
 2039    !.
 2040colour_op_name(Name, TB, parentheses_term_position(PO,PC,Pos)) :-
 2041    !,
 2042    colour_item(parentheses, TB, PO-PC),
 2043    colour_op_name(Name, TB, Pos).
 2044colour_op_name(Name, TB, Pos) :-
 2045    var(Name),
 2046    !,
 2047    colour_item(var, TB, Pos).
 2048colour_op_name(Name, TB, Pos) :-
 2049    (atom(Name) ; Name == []),
 2050    !,
 2051    colour_item(identifier, TB, Pos).
 2052colour_op_name(Module:Name, TB, term_position(_F,_T,QF,QT,[MP,NP])) :-
 2053    !,
 2054    colourise_module(Module, TB, MP),
 2055    colour_item(functor, TB, QF-QT),
 2056    colour_op_name(Name, TB, NP).
 2057colour_op_name(List, TB, list_position(F,T,Elems,none)) :-
 2058    !,
 2059    colour_item(list, TB, F-T),
 2060    colour_op_names(List, TB, Elems).
 2061colour_op_name(_, TB, Pos) :-
 2062    colour_item(error, TB, Pos).
 2063
 2064colour_op_names([], _, []).
 2065colour_op_names([H|T], TB, [HP|TP]) :-
 2066    colour_op_name(H, TB, HP),
 2067    colour_op_names(T, TB, TP).
 2068
 2069colour_op_type(Type, TB, Pos) :-
 2070    var(Type),
 2071    !,
 2072    colour_item(var, TB, Pos).
 2073colour_op_type(Type, TB, Pos) :-
 2074    op_type(Type),
 2075    !,
 2076    colour_item(op_type(Type), TB, Pos).
 2077colour_op_type(_, TB, Pos) :-
 2078    colour_item(error, TB, Pos).
 2079
 2080colour_op_priority(Priority, TB, Pos) :-
 2081    var(Priority), colour_item(var, TB, Pos).
 2082colour_op_priority(Priority, TB, Pos) :-
 2083    integer(Priority),
 2084    between(0, 1200, Priority),
 2085    !,
 2086    colour_item(int, TB, Pos).
 2087colour_op_priority(_, TB, Pos) :-
 2088    colour_item(error, TB, Pos).
 2089
 2090op_type(fx).
 2091op_type(fy).
 2092op_type(xf).
 2093op_type(yf).
 2094op_type(xfy).
 2095op_type(xfx).
 2096op_type(yfx).
 colourise_prolog_flag_name(+Name, +TB, +Pos)
Colourise the name of a Prolog flag
 2103colourise_prolog_flag_name(_, _, Pos) :-
 2104    var(Pos),
 2105    !.
 2106colourise_prolog_flag_name(Name, TB, parentheses_term_position(PO,PC,Pos)) :-
 2107    !,
 2108    colour_item(parentheses, TB, PO-PC),
 2109    colourise_prolog_flag_name(Name, TB, Pos).
 2110colourise_prolog_flag_name(Name, TB, Pos) :-
 2111    atom(Name),
 2112    !,
 2113    (   current_prolog_flag(Name, _)
 2114    ->  colour_item(flag_name(Name), TB, Pos)
 2115    ;   colour_item(no_flag_name(Name), TB, Pos)
 2116    ).
 2117colourise_prolog_flag_name(Name, TB, Pos) :-
 2118    colourise_term(Name, TB, Pos).
 2119
 2120
 2121                 /*******************************
 2122                 *        CONFIGURATION         *
 2123                 *******************************/
 2124
 2125%       body_compiled(+Term)
 2126%
 2127%       Succeeds if term is a construct handled by the compiler.
 2128
 2129body_compiled((_,_)).
 2130body_compiled((_->_)).
 2131body_compiled((_*->_)).
 2132body_compiled((_;_)).
 2133body_compiled(\+_).
 goal_classification(+TB, +Goal, +Origin, -Class)
Classify Goal appearing in TB and called from a clause with head Origin. For directives, Origin is [].
 2140goal_classification(_, QGoal, _, Class) :-
 2141    strip_module(QGoal, _, Goal),
 2142    (   var(Goal)
 2143    ->  !, Class = meta
 2144    ;   \+ callable(Goal)
 2145    ->  !, Class = not_callable
 2146    ).
 2147goal_classification(_, Goal, Origin, recursion) :-
 2148    callable(Origin),
 2149    generalise_term(Goal, Origin),
 2150    !.
 2151goal_classification(TB, Goal, _, How) :-
 2152    colour_state_source_id(TB, SourceId),
 2153    xref_defined(SourceId, Goal, How),
 2154    How \= public(_),
 2155    !.
 2156goal_classification(TB, Goal, _, Class) :-
 2157    (   colour_state_source_id(TB, SourceId),
 2158        xref_module(SourceId, Module)
 2159    ->  true
 2160    ;   Module = user
 2161    ),
 2162    call_goal_classification(Goal, Module, Class),
 2163    !.
 2164goal_classification(TB, Goal, _, How) :-
 2165    colour_state_module(TB, Module),
 2166    atom(Module),
 2167    Module \== prolog_colour_ops,
 2168    predicate_property(Module:Goal, imported_from(From)),
 2169    !,
 2170    How = imported(From).
 2171goal_classification(_TB, _Goal, _, undefined).
 goal_classification(+Goal, +Module, -Class)
Multifile hookable classification for non-local goals.
 2177call_goal_classification(Goal, Module, Class) :-
 2178    catch(global_goal_classification(Goal, Module, Class), _,
 2179          Class = type_error(callable)).
 2180
 2181global_goal_classification(Goal, _, built_in) :-
 2182    built_in_predicate(Goal),
 2183    !.
 2184global_goal_classification(Goal, _, autoload(From)) :-  % SWI-Prolog
 2185    predicate_property(Goal, autoload(From)).
 2186global_goal_classification(Goal, Module, Class) :-      % SWI-Prolog
 2187    strip_module(Goal, _, PGoal),
 2188    current_predicate(_, user:PGoal),
 2189    !,
 2190    (   Module == user
 2191    ->  Class = global(GClass, Location),
 2192        global_location(user:Goal, Location),
 2193        global_class(user:Goal, GClass)
 2194    ;   Class = global
 2195    ).
 2196global_goal_classification(Goal, _, Class) :-
 2197    compound(Goal),
 2198    compound_name_arity(Goal, Name, Arity),
 2199    vararg_goal_classification(Name, Arity, Class).
 2200
 2201global_location(Goal, File:Line) :-
 2202    predicate_property(Goal, file(File)),
 2203    predicate_property(Goal, line_count(Line)),
 2204    !.
 2205global_location(_, -).
 2206
 2207global_class(Goal, dynamic)   :- predicate_property(Goal, dynamic), !.
 2208global_class(Goal, multifile) :- predicate_property(Goal, multifile), !.
 2209global_class(Goal, tabled)    :- predicate_property(Goal, tabled), !.
 2210global_class(_,    static).
 vararg_goal_classification(+Name, +Arity, -Class) is semidet
Multifile hookable classification for vararg predicates.
 2217vararg_goal_classification(call, Arity, built_in) :-
 2218    Arity >= 1.
 2219vararg_goal_classification(send_super, Arity, expanded) :- % XPCE (TBD)
 2220    Arity >= 2.
 2221vararg_goal_classification(get_super, Arity, expanded) :-  % XPCE (TBD)
 2222    Arity >= 3.
 qualified_goal_classification(:Goal, +TB, -Class)
Classify an explicitly qualified goal.
 2228qualified_goal_classification(Goal, TB, Class) :-
 2229    goal_classification(TB, Goal, [], Class),
 2230    Class \== undefined,
 2231    !.
 2232qualified_goal_classification(Module:Goal, _, extern(Module, How)) :-
 2233    predicate_property(Module:Goal, visible),
 2234    !,
 2235    (   (   predicate_property(Module:Goal, public)
 2236        ;   predicate_property(Module:Goal, exported)
 2237        )
 2238    ->  How = (public)
 2239    ;   How = (private)
 2240    ).
 2241qualified_goal_classification(Module:_, _, extern(Module, unknown)).
 classify_head(+TB, +Head, -Class)
Classify a clause head
 2247classify_head(TB, Goal, exported) :-
 2248    colour_state_source_id(TB, SourceId),
 2249    xref_exported(SourceId, Goal),
 2250    !.
 2251classify_head(_TB, Goal, hook) :-
 2252    xref_hook(Goal),
 2253    !.
 2254classify_head(TB, Goal, hook) :-
 2255    colour_state_source_id(TB, SourceId),
 2256    xref_module(SourceId, M),
 2257    xref_hook(M:Goal),
 2258    !.
 2259classify_head(TB, Goal, Class) :-
 2260    built_in_predicate(Goal),
 2261    (   system_module(TB)
 2262    ->  (   predicate_property(system:Goal, iso)
 2263        ->  Class = def_iso
 2264        ;   goal_name(Goal, Name),
 2265            \+ sub_atom(Name, 0, _, _, $)
 2266        ->  Class = def_swi
 2267        )
 2268    ;   (   predicate_property(system:Goal, iso)
 2269        ->  Class = iso
 2270        ;   Class = built_in
 2271        )
 2272    ).
 2273classify_head(TB, Goal, unreferenced) :-
 2274    colour_state_source_id(TB, SourceId),
 2275    \+ (xref_called(SourceId, Goal, By), By \= Goal),
 2276    !.
 2277classify_head(TB, Goal, test) :-
 2278    Goal = test(_),
 2279    colour_state_source_id(TB, SourceId),
 2280    xref_called(SourceId, Goal, '<test_unit>'(_Unit)),
 2281    !.
 2282classify_head(TB, Goal, test) :-
 2283    Goal = test(_, _),
 2284    colour_state_source_id(TB, SourceId),
 2285    xref_called(SourceId, Goal, '<test_unit>'(_Unit)),
 2286    !.
 2287classify_head(TB, Goal, How) :-
 2288    colour_state_source_id(TB, SourceId),
 2289    (   xref_defined(SourceId, Goal, imported(From))
 2290    ->  How = imported(From)
 2291    ;   xref_defined(SourceId, Goal, How)
 2292    ),
 2293    !.
 2294classify_head(_TB, _Goal, undefined).
 2295
 2296built_in_predicate(Goal) :-
 2297    predicate_property(system:Goal, built_in),
 2298    !.
 2299built_in_predicate(module(_, _)).       % reserved expanded constructs
 2300built_in_predicate(module(_, _, _)).
 2301built_in_predicate(if(_)).
 2302built_in_predicate(elif(_)).
 2303built_in_predicate(else).
 2304built_in_predicate(endif).
 2305
 2306goal_name(_:G, Name) :- nonvar(G), !, goal_name(G, Name).
 2307goal_name(G, Name) :- callable(G), functor_name(G, Name).
 2308
 2309system_module(TB) :-
 2310    colour_state_source_id(TB, SourceId),
 2311    xref_module(SourceId, M),
 2312    module_property(M, class(system)).
 2313
 2314generalise_term(Specific, General) :-
 2315    (   compound(Specific)
 2316    ->  compound_name_arity(Specific, Name, Arity),
 2317        compound_name_arity(General0, Name, Arity),
 2318        General = General0
 2319    ;   General = Specific
 2320    ).
 2321
 2322rename_goal(Goal0, Name, Goal) :-
 2323    (   compound(Goal0)
 2324    ->  compound_name_arity(Goal0, _, Arity),
 2325        compound_name_arity(Goal, Name, Arity)
 2326    ;   Goal = Name
 2327    ).
 2328
 2329functor_name(Term, Name) :-
 2330    (   compound(Term)
 2331    ->  compound_name_arity(Term, Name, _)
 2332    ;   atom(Term)
 2333    ->  Name = Term
 2334    ).
 2335
 2336goal_name_arity(Goal, Name, Arity) :-
 2337    (   compound(Goal)
 2338    ->  compound_name_arity(Goal, Name, Arity)
 2339    ;   atom(Goal)
 2340    ->  Name = Goal, Arity = 0
 2341    ).
 2342
 2343
 2344call_goal_colours(Term, Colours) :-
 2345    goal_colours(Term, Colours),
 2346    !.
 2347call_goal_colours(Term, Colours) :-
 2348    def_goal_colours(Term, Colours).
 2349
 2350call_goal_colours(Term, Class, Colours) :-
 2351    goal_colours(Term, Class, Colours),
 2352    !.
 2353%call_goal_colours(Term, Class, Colours) :-
 2354%    def_goal_colours(Term, Class, Colours).
 2355
 2356
 2357%       Specify colours for individual goals.
 2358
 2359def_goal_colours(module(_,_),            built_in-[identifier,exports]).
 2360def_goal_colours(module(_,_,_),          built_in-[identifier,exports,langoptions]).
 2361def_goal_colours(use_module(_),          built_in-[imported_file]).
 2362def_goal_colours(use_module(File,_),     built_in-[file,imports(File)]).
 2363def_goal_colours(autoload(_),            built_in-[imported_file]).
 2364def_goal_colours(autoload(File,_),       built_in-[file,imports(File)]).
 2365def_goal_colours(reexport(_),            built_in-[file]).
 2366def_goal_colours(reexport(File,_),       built_in-[file,imports(File)]).
 2367def_goal_colours(dynamic(_),             built_in-[declarations(dynamic)]).
 2368def_goal_colours(thread_local(_),        built_in-[declarations(thread_local)]).
 2369def_goal_colours(module_transparent(_),  built_in-[declarations(module_transparent)]).
 2370def_goal_colours(discontiguous(_),       built_in-[declarations(discontiguous)]).
 2371def_goal_colours(multifile(_),           built_in-[declarations(multifile)]).
 2372def_goal_colours(volatile(_),            built_in-[declarations(volatile)]).
 2373def_goal_colours(public(_),              built_in-[declarations(public)]).
 2374def_goal_colours(det(_),                 built_in-[declarations(det)]).
 2375def_goal_colours(table(_),               built_in-[declarations(table)]).
 2376def_goal_colours(meta_predicate(_),      built_in-[meta_declarations]).
 2377def_goal_colours(consult(_),             built_in-[file]).
 2378def_goal_colours(include(_),             built_in-[file]).
 2379def_goal_colours(ensure_loaded(_),       built_in-[file]).
 2380def_goal_colours(load_files(_),          built_in-[file]).
 2381def_goal_colours(load_files(_,_),        built_in-[file,options]).
 2382def_goal_colours(setof(_,_,_),           built_in-[classify,setof,classify]).
 2383def_goal_colours(bagof(_,_,_),           built_in-[classify,setof,classify]).
 2384def_goal_colours(predicate_options(_,_,_), built_in-[predicate,classify,classify]).
 2385% Database access
 2386def_goal_colours(assert(_),              built_in-[db]).
 2387def_goal_colours(asserta(_),             built_in-[db]).
 2388def_goal_colours(assertz(_),             built_in-[db]).
 2389def_goal_colours(assert(_,_),            built_in-[db,classify]).
 2390def_goal_colours(asserta(_,_),           built_in-[db,classify]).
 2391def_goal_colours(assertz(_,_),           built_in-[db,classify]).
 2392def_goal_colours(retract(_),             built_in-[db]).
 2393def_goal_colours(retractall(_),          built_in-[db]).
 2394def_goal_colours(clause(_,_),            built_in-[db,classify]).
 2395def_goal_colours(clause(_,_,_),          built_in-[db,classify,classify]).
 2396% misc
 2397def_goal_colours(set_prolog_flag(_,_),   built_in-[prolog_flag_name,classify]).
 2398def_goal_colours(current_prolog_flag(_,_), built_in-[prolog_flag_name,classify]).
 2399% XPCE stuff
 2400def_goal_colours(pce_autoload(_,_),      classify-[classify,file]).
 2401def_goal_colours(pce_image_directory(_), classify-[directory]).
 2402def_goal_colours(new(_, _),              built_in-[classify,pce_new]).
 2403def_goal_colours(send_list(_,_,_),       built_in-pce_arg_list).
 2404def_goal_colours(send(_,_),              built_in-[pce_arg,pce_selector]).
 2405def_goal_colours(get(_,_,_),             built_in-[pce_arg,pce_selector,pce_arg]).
 2406def_goal_colours(send_super(_,_),        built_in-[pce_arg,pce_selector]).
 2407def_goal_colours(get_super(_,_),         built_in-[pce_arg,pce_selector,pce_arg]).
 2408def_goal_colours(get_chain(_,_,_),       built_in-[pce_arg,pce_selector,pce_arg]).
 2409def_goal_colours(Pce,                    built_in-pce_arg) :-
 2410    compound(Pce),
 2411    functor_name(Pce, Functor),
 2412    pce_functor(Functor).
 2413
 2414pce_functor(send).
 2415pce_functor(get).
 2416pce_functor(send_super).
 2417pce_functor(get_super).
 2418
 2419
 2420                 /*******************************
 2421                 *        SPECIFIC HEADS        *
 2422                 *******************************/
 2423
 2424head_colours(file_search_path(_,_), hook-[identifier,classify]).
 2425head_colours(library_directory(_),  hook-[file]).
 2426head_colours(resource(_,_),         hook-[identifier,file]).
 2427head_colours(resource(_,_,_),       hook-[identifier,file,classify]).
 2428
 2429head_colours(Var, _) :-
 2430    var(Var),
 2431    !,
 2432    fail.
 2433head_colours(M:H, Colours) :-
 2434    M == user,
 2435    head_colours(H, HC),
 2436    HC = hook - _,
 2437    !,
 2438    Colours = meta-[module(user), HC ].
 2439head_colours(M:H, Colours) :-
 2440    atom(M), callable(H),
 2441    xref_hook(M:H),
 2442    !,
 2443    Colours = meta-[module(M), hook-classify ].
 2444head_colours(M:_, meta-[module(M),extern(M)]).
 2445
 2446
 2447                 /*******************************
 2448                 *             STYLES           *
 2449                 *******************************/
 def_style(+Pattern, -Style)
Define the style used for the given pattern. Definitions here can be overruled by defining rules for style/2
 2457def_style(goal(built_in,_),        [colour(blue)]).
 2458def_style(goal(imported(_),_),     [colour(blue)]).
 2459def_style(goal(autoload(_),_),     [colour(navy_blue)]).
 2460def_style(goal(global,_),          [colour(navy_blue)]).
 2461def_style(goal(global(dynamic,_),_), [colour(magenta)]).
 2462def_style(goal(global(_,_),_),     [colour(navy_blue)]).
 2463def_style(goal(undefined,_),       [colour(red)]).
 2464def_style(goal(thread_local(_),_), [colour(magenta), underline(true)]).
 2465def_style(goal(dynamic(_),_),      [colour(magenta)]).
 2466def_style(goal(multifile(_),_),    [colour(navy_blue)]).
 2467def_style(goal(expanded,_),        [colour(blue), underline(true)]).
 2468def_style(goal(extern(_),_),       [colour(blue), underline(true)]).
 2469def_style(goal(extern(_,private),_), [colour(red)]).
 2470def_style(goal(extern(_,public),_), [colour(blue)]).
 2471def_style(goal(recursion,_),       [underline(true)]).
 2472def_style(goal(meta,_),            [colour(red4)]).
 2473def_style(goal(foreign(_),_),      [colour(darkturquoise)]).
 2474def_style(goal(local(_),_),        []).
 2475def_style(goal(constraint(_),_),   [colour(darkcyan)]).
 2476def_style(goal(not_callable,_),    [background(orange)]).
 2477
 2478def_style(option_name,             [colour('#3434ba')]).
 2479def_style(no_option_name,          [colour(red)]).
 2480
 2481def_style(neck(_),		   [bold(true)]).
 2482
 2483def_style(head(exported,_),        [colour(blue), bold(true)]).
 2484def_style(head(public(_),_),       [colour('#016300'), bold(true)]).
 2485def_style(head(extern(_),_),       [colour(blue), bold(true)]).
 2486def_style(head(dynamic,_),         [colour(magenta), bold(true)]).
 2487def_style(head(multifile,_),       [colour(navy_blue), bold(true)]).
 2488def_style(head(unreferenced,_),    [colour(red), bold(true)]).
 2489def_style(head(hook,_),            [colour(blue), underline(true)]).
 2490def_style(head(meta,_),            []).
 2491def_style(head(constraint(_),_),   [colour(darkcyan), bold(true)]).
 2492def_style(head(imported(_),_),     [colour(darkgoldenrod4), bold(true)]).
 2493def_style(head(built_in,_),        [background(orange), bold(true)]).
 2494def_style(head(iso,_),             [background(orange), bold(true)]).
 2495def_style(head(def_iso,_),         [colour(blue), bold(true)]).
 2496def_style(head(def_swi,_),         [colour(blue), bold(true)]).
 2497def_style(head(test,_),            [colour('#01bdbd'), bold(true)]).
 2498def_style(head(_,_),               [bold(true)]).
 2499def_style(rule_condition,	   [background('#d4ffe3')]).
 2500
 2501def_style(module(_),               [colour(dark_slate_blue)]).
 2502def_style(comment(_),              [colour(dark_green)]).
 2503
 2504def_style(directive,               [background(grey90)]).
 2505def_style(method(_),               [bold(true)]).
 2506
 2507def_style(var,                     [colour(red4)]).
 2508def_style(singleton,               [bold(true), colour(red4)]).
 2509def_style(unbound,                 [colour(red), bold(true)]).
 2510def_style(quoted_atom,             [colour(navy_blue)]).
 2511def_style(string,                  [colour(navy_blue)]).
 2512def_style(rational(_),		   [colour(steel_blue)]).
 2513def_style(codes,                   [colour(navy_blue)]).
 2514def_style(chars,                   [colour(navy_blue)]).
 2515def_style(nofile,                  [colour(red)]).
 2516def_style(file(_),                 [colour(blue), underline(true)]).
 2517def_style(file_no_depend(_),       [colour(blue), underline(true), background(pink)]).
 2518def_style(directory(_),            [colour(blue)]).
 2519def_style(class(built_in,_),       [colour(blue), underline(true)]).
 2520def_style(class(library(_),_),     [colour(navy_blue), underline(true)]).
 2521def_style(class(local(_,_,_),_),   [underline(true)]).
 2522def_style(class(user(_),_),        [underline(true)]).
 2523def_style(class(user,_),           [underline(true)]).
 2524def_style(class(undefined,_),      [colour(red), underline(true)]).
 2525def_style(prolog_data,             [colour(blue), underline(true)]).
 2526def_style(flag_name(_),            [colour(blue)]).
 2527def_style(no_flag_name(_),         [colour(red)]).
 2528def_style(unused_import,           [colour(blue), background(pink)]).
 2529def_style(undefined_import,        [colour(red)]).
 2530
 2531def_style(constraint(_),           [colour(darkcyan)]).
 2532
 2533def_style(keyword(_),              [colour(blue)]).
 2534def_style(identifier,              [bold(true)]).
 2535def_style(delimiter,               [bold(true)]).
 2536def_style(expanded,                [colour(blue), underline(true)]).
 2537def_style(hook(_),                 [colour(blue), underline(true)]).
 2538def_style(op_type(_),              [colour(blue)]).
 2539
 2540def_style(qq_type,                 [bold(true)]).
 2541def_style(qq(_),                   [colour(blue), bold(true)]).
 2542def_style(qq_content(_),           [colour(red4)]).
 2543
 2544def_style(dict_tag,                [bold(true)]).
 2545def_style(dict_key,                [bold(true)]).
 2546def_style(dict_function(_),        [colour(navy_blue)]).
 2547def_style(dict_return_op,          [colour(blue)]).
 2548
 2549def_style(hook,                    [colour(blue), underline(true)]).
 2550def_style(dcg_right_hand_ctx,      [background('#d4ffe3')]).
 2551
 2552def_style(error,                   [background(orange)]).
 2553def_style(type_error(_),           [background(orange)]).
 2554def_style(syntax_error(_,_),       [background(orange)]).
 2555def_style(instantiation_error,     [background(orange)]).
 2556
 2557def_style(decl_option(_),	   [bold(true)]).
 2558def_style(table_mode(_),	   [bold(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.

 2574syntax_colour(Class, Attributes) :-
 2575    (   style(Class, Attributes)            % user hook
 2576    ;   def_style(Class, Attributes)        % system default
 2577    ).
 term_colours(+Term, -FunctorColour, -ArgColours)
Define colourisation for specific terms.
 2584term_colours((?- Directive), Colours) :-
 2585    term_colours((:- Directive), Colours).
 2586term_colours((prolog:Head --> _),
 2587             neck(-->) - [ expanded - [ module(prolog),
 2588                                        hook(message) - [ identifier
 2589                                                        ]
 2590                                      ],
 2591                           dcg_body(prolog:Head)
 2592                         ]) :-
 2593    prolog_message_hook(Head).
 2594
 2595prolog_message_hook(message(_)).
 2596prolog_message_hook(deprecated(_)).
 2597prolog_message_hook(error_message(_)).
 2598prolog_message_hook(message_context(_)).
 2599prolog_message_hook(message_location(_)).
 2600
 2601%       XPCE rules
 2602
 2603term_colours(variable(_, _, _, _),
 2604             expanded - [ identifier,
 2605                          classify,
 2606                          classify,
 2607                          comment(string)
 2608                        ]).
 2609term_colours(variable(_, _, _),
 2610             expanded - [ identifier,
 2611                          classify,
 2612                          atom
 2613                        ]).
 2614term_colours(handle(_, _, _),
 2615             expanded - [ classify,
 2616                          classify,
 2617                          classify
 2618                        ]).
 2619term_colours(handle(_, _, _, _),
 2620             expanded - [ classify,
 2621                          classify,
 2622                          classify,
 2623                          classify
 2624                        ]).
 2625term_colours(class_variable(_,_,_,_),
 2626             expanded - [ identifier,
 2627                          pce(type),
 2628                          pce(default),
 2629                          comment(string)
 2630                        ]).
 2631term_colours(class_variable(_,_,_),
 2632             expanded - [ identifier,
 2633                          pce(type),
 2634                          pce(default)
 2635                        ]).
 2636term_colours(delegate_to(_),
 2637             expanded - [ classify
 2638                        ]).
 2639term_colours((:- encoding(_)),
 2640             expanded - [ expanded - [ classify
 2641                                     ]
 2642                        ]).
 2643term_colours((:- pce_begin_class(_, _, _)),
 2644             expanded - [ expanded - [ identifier,
 2645                                       pce_new,
 2646                                       comment(string)
 2647                                     ]
 2648                        ]).
 2649term_colours((:- pce_begin_class(_, _)),
 2650             expanded - [ expanded - [ identifier,
 2651                                       pce_new
 2652                                     ]
 2653                        ]).
 2654term_colours((:- pce_extend_class(_)),
 2655             expanded - [ expanded - [ identifier
 2656                                     ]
 2657                        ]).
 2658term_colours((:- pce_end_class),
 2659             expanded - [ expanded
 2660                        ]).
 2661term_colours((:- pce_end_class(_)),
 2662             expanded - [ expanded - [ identifier
 2663                                     ]
 2664                        ]).
 2665term_colours((:- use_class_template(_)),
 2666             expanded - [ expanded - [ pce_new
 2667                                     ]
 2668                        ]).
 2669term_colours((:- emacs_begin_mode(_,_,_,_,_)),
 2670             expanded - [ expanded - [ identifier,
 2671                                       classify,
 2672                                       classify,
 2673                                       classify,
 2674                                       classify
 2675                                     ]
 2676                        ]).
 2677term_colours((:- emacs_extend_mode(_,_)),
 2678             expanded - [ expanded - [ identifier,
 2679                                       classify
 2680                                     ]
 2681                        ]).
 2682term_colours((:- pce_group(_)),
 2683             expanded - [ expanded - [ identifier
 2684                                     ]
 2685                        ]).
 2686term_colours((:- pce_global(_, new(_))),
 2687             expanded - [ expanded - [ identifier,
 2688                                       pce_arg
 2689                                     ]
 2690                        ]).
 2691term_colours((:- emacs_end_mode),
 2692             expanded - [ expanded
 2693                        ]).
 2694term_colours(pce_ifhostproperty(_,_),
 2695             expanded - [ classify,
 2696                          classify
 2697                        ]).
 2698term_colours((_,_),
 2699             error - [ classify,
 2700                       classify
 2701                     ]).
 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.
 2708specified_item(_Class, _Term, _TB, Pos) :-
 2709    var(Pos),
 2710    !.
 2711specified_item(Class, Term, TB, parentheses_term_position(PO,PC,Pos)) :-
 2712    !,
 2713    colour_item(parentheses, TB, PO-PC),
 2714    specified_item(Class, Term, TB, Pos).
 2715specified_item(_, Var, TB, Pos) :-
 2716    (   var(Var)
 2717    ;   qq_position(Pos)
 2718    ),
 2719    !,
 2720    colourise_term_arg(Var, TB, Pos).
 2721                                        % generic classification
 2722specified_item(classify, Term, TB, Pos) :-
 2723    !,
 2724    colourise_term_arg(Term, TB, Pos).
 2725                                        % classify as head
 2726specified_item(head, Term, TB, Pos) :-
 2727    !,
 2728    colourise_clause_head(Term, TB, Pos).
 2729                                        % expanded head (DCG=2, ...)
 2730specified_item(head(+N), Term, TB, Pos) :-
 2731    !,
 2732    colourise_extended_head(Term, N, TB, Pos).
 2733                                        % M:Head
 2734specified_item(extern(M), Term, TB, Pos) :-
 2735    !,
 2736    colourise_extern_head(Term, M, TB, Pos).
 2737                                        % classify as body
 2738specified_item(body, Term, TB, Pos) :-
 2739    !,
 2740    colourise_body(Term, TB, Pos).
 2741specified_item(body(Goal), _Term0, TB, Pos) :-
 2742    !,
 2743    colourise_body(Goal, TB, Pos).
 2744specified_item(dcg_body(Head), Term, TB, Pos) :-
 2745    !,
 2746    colourise_dcg(Term, Head, TB, Pos).
 2747specified_item(setof, Term, TB, Pos) :-
 2748    !,
 2749    colourise_setof(Term, TB, Pos).
 2750specified_item(meta(MetaSpec), Term, TB, Pos) :-
 2751    !,
 2752    colourise_meta_arg(MetaSpec, Term, TB, Pos).
 2753                                        % DCG goal in body
 2754specified_item(dcg, Term, TB, Pos) :-
 2755    !,
 2756    colourise_dcg(Term, [], TB, Pos).
 2757                                        % assert/retract arguments
 2758specified_item(db, Term, TB, Pos) :-
 2759    !,
 2760    colourise_db(Term, TB, Pos).
 2761                                        % error(Error)
 2762specified_item(error(Error), _Term, TB, Pos) :-
 2763    colour_item(Error, TB, Pos).
 2764                                        % files
 2765specified_item(file(Path), _Term, TB, Pos) :-
 2766    !,
 2767    colour_item(file(Path), TB, Pos).
 2768specified_item(file, Term, TB, Pos) :-
 2769    !,
 2770    colourise_files(Term, TB, Pos, any).
 2771specified_item(imported_file, Term, TB, Pos) :-
 2772    !,
 2773    colourise_files(Term, TB, Pos, imported).
 2774specified_item(langoptions, Term, TB, Pos) :-
 2775    !,
 2776    colourise_langoptions(Term, TB, Pos).
 2777
 2778                                        % directory
 2779specified_item(directory, Term, TB, Pos) :-
 2780    !,
 2781    colourise_directory(Term, TB, Pos).
 2782                                        % [Name/Arity, ...]
 2783specified_item(exports, Term, TB, Pos) :-
 2784    !,
 2785    colourise_exports(Term, TB, Pos).
 2786                                        % [Name/Arity, ...]
 2787specified_item(imports(File), Term, TB, Pos) :-
 2788    !,
 2789    colourise_imports(Term, File, TB, Pos).
 2790                                        % Name/Arity
 2791specified_item(import(File), Term, TB, Pos) :-
 2792    !,
 2793    colourise_import(Term, File, TB, Pos).
 2794                                        % Name/Arity, ...
 2795specified_item(predicates, Term, TB, Pos) :-
 2796    !,
 2797    colourise_declarations(Term, predicate_indicator, TB, Pos).
 2798                                        % Name/Arity
 2799specified_item(predicate, Term, TB, Pos) :-
 2800    !,
 2801    colourise_declaration(Term, predicate_indicator, TB, Pos).
 2802                                        % head(Arg, ...)
 2803specified_item(meta_declarations, Term, TB, Pos) :-
 2804    !,
 2805    colourise_meta_declarations(Term, [], TB, Pos).
 2806specified_item(meta_declarations(Extra), Term, TB, Pos) :-
 2807    !,
 2808    colourise_meta_declarations(Term, Extra, TB, Pos).
 2809specified_item(declarations(Which), Term, TB, Pos) :-
 2810    !,
 2811    colourise_declarations(Term, Which, TB, Pos).
 2812                                        % set_prolog_flag(Name, _)
 2813specified_item(prolog_flag_name, Term, TB, Pos) :-
 2814    !,
 2815    colourise_prolog_flag_name(Term, TB, Pos).
 2816                                        % XPCE new argument
 2817specified_item(pce_new, Term, TB, Pos) :-
 2818    !,
 2819    (   atom(Term)
 2820    ->  colourise_class(Term, TB, Pos)
 2821    ;   compound(Term)
 2822    ->  functor_name(Term, Class),
 2823        Pos = term_position(_,_,FF, FT, ArgPos),
 2824        colourise_class(Class, TB, FF-FT),
 2825        specified_items(pce_arg, Term, TB, ArgPos)
 2826    ;   colourise_term_arg(Term, TB, Pos)
 2827    ).
 2828                                        % Generic XPCE arguments
 2829specified_item(pce_arg, new(X), TB,
 2830               term_position(_,_,_,_,[ArgPos])) :-
 2831    !,
 2832    specified_item(pce_new, X, TB, ArgPos).
 2833specified_item(pce_arg, new(X, T), TB,
 2834               term_position(_,_,_,_,[P1, P2])) :-
 2835    !,
 2836    colourise_term_arg(X, TB, P1),
 2837    specified_item(pce_new, T, TB, P2).
 2838specified_item(pce_arg, @(Ref), TB, Pos) :-
 2839    !,
 2840    colourise_term_arg(@(Ref), TB, Pos).
 2841specified_item(pce_arg, prolog(Term), TB,
 2842               term_position(_,_,FF,FT,[ArgPos])) :-
 2843    !,
 2844    colour_item(prolog_data, TB, FF-FT),
 2845    colourise_term_arg(Term, TB, ArgPos).
 2846specified_item(pce_arg, Term, TB, Pos) :-
 2847    compound(Term),
 2848    Term \= [_|_],
 2849    \+ is_dict(Term),
 2850    !,
 2851    specified_item(pce_new, Term, TB, Pos).
 2852specified_item(pce_arg, Term, TB, Pos) :-
 2853    !,
 2854    colourise_term_arg(Term, TB, Pos).
 2855                                        % List of XPCE arguments
 2856specified_item(pce_arg_list, List, TB, list_position(F,T,Elms,Tail)) :-
 2857    !,
 2858    colour_item(list, TB, F-T),
 2859    colourise_list_args(Elms, Tail, List, TB, pce_arg).
 2860specified_item(pce_arg_list, Term, TB, Pos) :-
 2861    !,
 2862    specified_item(pce_arg, Term, TB, Pos).
 2863                                        % XPCE selector
 2864specified_item(pce_selector, Term, TB,
 2865               term_position(_,_,_,_,ArgPos)) :-
 2866    !,
 2867    specified_items(pce_arg, Term, TB, ArgPos).
 2868specified_item(pce_selector, Term, TB, Pos) :-
 2869    colourise_term_arg(Term, TB, Pos).
 2870                                        % Nested specification
 2871specified_item(FuncSpec-ArgSpecs, Term, TB,
 2872               term_position(_,_,FF,FT,ArgPos)) :-
 2873    !,
 2874    specified_item(FuncSpec, Term, TB, FF-FT),
 2875    specified_items(ArgSpecs, Term, TB, ArgPos).
 2876                                        % Nested for {...}
 2877specified_item(FuncSpec-[ArgSpec], {Term}, TB,
 2878               brace_term_position(F,T,ArgPos)) :-
 2879    !,
 2880    specified_item(FuncSpec, {Term}, TB, F-T),
 2881    specified_item(ArgSpec, Term, TB, ArgPos).
 2882                                        % Specified
 2883specified_item(FuncSpec-ElmSpec, List, TB,
 2884               list_position(F,T,ElmPos,TailPos)) :-
 2885    !,
 2886    colour_item(FuncSpec, TB, F-T),
 2887    specified_list(ElmSpec, List, TB, ElmPos, TailPos).
 2888specified_item(Class, _, TB, Pos) :-
 2889    colour_item(Class, TB, Pos).
 specified_items(+Spec, +Term, +TB, +PosList)
 2893specified_items(Specs, Term, TB, PosList) :-
 2894    is_dict(Term),
 2895    !,
 2896    specified_dict_kv(PosList, Term, TB, Specs).
 2897specified_items(Specs, Term, TB, PosList) :-
 2898    is_list(Specs),
 2899    !,
 2900    specified_arglist(Specs, 1, Term, TB, PosList).
 2901specified_items(Spec, Term, TB, PosList) :-
 2902    specified_argspec(PosList, Spec, 1, Term, TB).
 2903
 2904
 2905specified_arglist([], _, _, _, _).
 2906specified_arglist(_, _, _, _, []) :- !.         % Excess specification args
 2907specified_arglist([S0|ST], N, T, TB, [P0|PT]) :-
 2908    (   S0 == options,
 2909        colourization_module(TB, Module),
 2910        colourise_option_arg(T, Module, N, TB, P0)
 2911    ->  true
 2912    ;   arg(N, T, Term),
 2913        specified_item(S0, Term, TB, P0)
 2914    ),
 2915    NN is N + 1,
 2916    specified_arglist(ST, NN, T, TB, PT).
 2917
 2918specified_argspec([], _, _, _, _).
 2919specified_argspec([P0|PT], Spec, N, T, TB) :-
 2920    arg(N, T, Term),
 2921    specified_item(Spec, Term, TB, P0),
 2922    NN is N + 1,
 2923    specified_argspec(PT, Spec, NN, T, TB).
 2924
 2925
 2926%       specified_list(+Spec, +List, +TB, +PosList, TailPos)
 2927
 2928specified_list([], [], _, [], _).
 2929specified_list([HS|TS], [H|T], TB, [HP|TP], TailPos) :-
 2930    !,
 2931    specified_item(HS, H, TB, HP),
 2932    specified_list(TS, T, TB, TP, TailPos).
 2933specified_list(Spec, [H|T], TB, [HP|TP], TailPos) :-
 2934    specified_item(Spec, H, TB, HP),
 2935    specified_list(Spec, T, TB, TP, TailPos).
 2936specified_list(_, _, _, [], none) :- !.
 2937specified_list(Spec, Tail, TB, [], TailPos) :-
 2938    specified_item(Spec, Tail, TB, TailPos).
 specified_dict_kv(+PosList, +Term, +TB, +Specs)
Arguments:
Specs- is a list of dict_kv(+Key, +KeySpec, +ArgSpec)
 2944specified_dict_kv([], _, _, _).
 2945specified_dict_kv([key_value_position(_F,_T,SF,ST,K,KP,VP)|Pos],
 2946                  Dict, TB, Specs) :-
 2947    specified_dict_kv1(K, Specs, KeySpec, ValueSpec),
 2948    colour_item(KeySpec, TB, KP),
 2949    colour_item(dict_sep, TB, SF-ST),
 2950    get_dict(K, Dict, V),
 2951    specified_item(ValueSpec, V, TB, VP),
 2952    specified_dict_kv(Pos, Dict, TB, Specs).
 2953
 2954specified_dict_kv1(Key, Specs, KeySpec, ValueSpec) :-
 2955    Specs = [_|_],
 2956    memberchk(dict_kv(Key, KeySpec, ValueSpec), Specs),
 2957    !.
 2958specified_dict_kv1(Key, dict_kv(Key2, KeySpec, ValueSpec), KeySpec, ValueSpec) :-
 2959    \+ Key \= Key2,
 2960    !.              % do not bind Key2
 2961specified_dict_kv1(_, _, dict_key, classify).
 2962
 2963
 2964                 /*******************************
 2965                 *         DESCRIPTIONS         *
 2966                 *******************************/
 2967
 2968syntax_message(Class) -->
 2969    message(Class),
 2970    !.
 2971syntax_message(qq(_)) -->
 2972    [ 'Quasi quote delimiter' ].
 2973syntax_message(qq_type) -->
 2974    [ 'Quasi quote type term' ].
 2975syntax_message(qq_content(Type)) -->
 2976    [ 'Quasi quote content (~w syntax)'-[Type] ].
 2977syntax_message(goal(Class, Goal)) -->
 2978    !,
 2979    goal_message(Class, Goal).
 2980syntax_message(class(Type, Class)) -->
 2981    !,
 2982    xpce_class_message(Type, Class).
 2983syntax_message(dict_return_op) -->
 2984    !,
 2985    [ ':= separates function from return value' ].
 2986syntax_message(dict_function) -->
 2987    !,
 2988    [ 'Function on a dict' ].
 2989syntax_message(ext_quant) -->
 2990    !,
 2991    [ 'Existential quantification operator' ].
 2992syntax_message(hook(message)) -->
 2993    [ 'Rule for print_message/2' ].
 2994syntax_message(module(Module)) -->
 2995    (   { current_module(Module) }
 2996    ->  (   { module_property(Module, file(File)) }
 2997        ->  [ 'Module ~w defined in ~w'-[Module,File] ]
 2998        ;   [ 'Module ~w'-[Module] ]
 2999        )
 3000    ;   [ 'Module ~w (not loaded)'-[Module] ]
 3001    ).
 3002syntax_message(decl_option(incremental)) -->
 3003    [ 'Keep affected tables consistent' ].
 3004syntax_message(decl_option(abstract)) -->
 3005    [ 'Add abstracted goal to table dependency graph' ].
 3006syntax_message(decl_option(volatile)) -->
 3007    [ 'Do not include predicate in a saved program' ].
 3008syntax_message(decl_option(multifile)) -->
 3009    [ 'Clauses are spread over multiple files' ].
 3010syntax_message(decl_option(discontiguous)) -->
 3011    [ 'Clauses are not contiguous' ].
 3012syntax_message(decl_option(private)) -->
 3013    [ 'Tables or clauses are private to a thread' ].
 3014syntax_message(decl_option(local)) -->
 3015    [ 'Tables or clauses are private to a thread' ].
 3016syntax_message(decl_option(shared)) -->
 3017    [ 'Tables or clauses are shared between threads' ].
 3018syntax_message(decl_option(_Opt)) -->
 3019    [ 'Predicate property' ].
 3020syntax_message(rational(Value)) -->
 3021    [ 'Rational number ~w'-[Value] ].
 3022syntax_message(rule_condition) -->
 3023    [ 'Guard' ].
 3024syntax_message(neck(=>)) -->
 3025    [ 'Rule' ].
 3026syntax_message(neck(-->)) -->
 3027    [ 'Grammar rule' ].
 3028
 3029goal_message(meta, _) -->
 3030    [ 'Meta call' ].
 3031goal_message(not_callable, _) -->
 3032    [ 'Goal is not callable (type error)' ].
 3033goal_message(expanded, _) -->
 3034    [ 'Expanded goal' ].
 3035goal_message(Class, Goal) -->
 3036    { predicate_name(Goal, PI) },
 3037    [ 'Call to ~q'-PI ],
 3038    goal_class(Class).
 3039
 3040goal_class(recursion) -->
 3041    [ ' (recursive call)' ].
 3042goal_class(undefined) -->
 3043    [ ' (undefined)' ].
 3044goal_class(global) -->
 3045    [ ' (Auto-imported from module user)' ].
 3046goal_class(global(Class, File:Line)) -->
 3047    [ ' (~w in user module from '-[Class], url(File:Line), ')' ].
 3048goal_class(global(Class, source_location(File,Line))) -->
 3049    [ ' (~w in user module from '-[Class], url(File:Line), ')' ].
 3050goal_class(global(Class, -)) -->
 3051    [ ' (~w in user module)'-[Class] ].
 3052goal_class(imported(From)) -->
 3053    [ ' (imported from ~q)'-[From] ].
 3054goal_class(extern(_, private)) -->
 3055    [ ' (WARNING: private predicate)' ].
 3056goal_class(extern(_, public)) -->
 3057    [ ' (public predicate)' ].
 3058goal_class(extern(_)) -->
 3059    [ ' (cross-module call)' ].
 3060goal_class(Class) -->
 3061    [ ' (~p)'-[Class] ].
 3062
 3063xpce_class_message(Type, Class) -->
 3064    [ 'XPCE ~w class ~q'-[Type, Class] ]