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,N), op(P,T,M:N)) :-
  203    atom(N), !.
  204qualify_op(M, op(P,T,L), op(P,T,QL)) :-
  205    is_list(L), !,
  206    maplist(qualify_op_name(M), L, QL).
  207qualify_op(_, Op, Op).
  208
  209qualify_op_name(M, N, M:N) :-
  210    atom(N),
  211    !.
  212qualify_op_name(_, N, N).
  213
  214restore_settings(state(Style, Flags, OSM, Xref)) :-
  215    restore_syntax_flags(Flags),
  216    '$style_check'(_, Style),
  217    pop_operators,
  218    '$set_source_module'(OSM),
  219    set_xref(_, Xref).
  220
  221set_xref(Old, New) :-
  222    current_prolog_flag(xref, Old),
  223    !,
  224    set_prolog_flag(xref, New).
  225set_xref(false, New) :-
  226    set_prolog_flag(xref, New).
  227
  228
  229syntax_flags(Pairs) :-
  230    findall(set_prolog_flag(Flag, Value),
  231            syntax_flag(Flag, Value),
  232            Pairs).
  233
  234syntax_flag(Flag, Value) :-
  235    syntax_flag(Flag),
  236    current_prolog_flag(Flag, Value).
  237
  238restore_syntax_flags([]).
  239restore_syntax_flags([set_prolog_flag(Flag, Value)|T]) :-
  240    set_prolog_flag(Flag, Value),
  241    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.
  250source_module(TB, Module) :-
  251    colour_state_source_id_list(TB, []),
  252    !,
  253    colour_state_module(TB, Module).
  254source_module(TB, Module) :-
  255    colour_state_source_id(TB, SourceId),
  256    xref_option(SourceId, module(Module)),
  257    !.
  258source_module(TB, Module) :-
  259    (   colour_state_source_id(TB, File),
  260        atom(File)
  261    ;   colour_state_stream(TB, Fd),
  262        is_stream(Fd),
  263        stream_property(Fd, file_name(File))
  264    ),
  265    module_context(File, [], Module).
  266
  267module_context(File, _, Module) :-
  268    source_file_property(File, module(Module)),
  269    !.
  270module_context(File, Seen, Module) :-
  271    source_file_property(File, included_in(File2, _Line)),
  272    \+ memberchk(File, Seen),
  273    !,
  274    module_context(File2, [File|Seen], Module).
  275module_context(File, _, Module) :-
  276    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.
  283read_error(Error, TB, Start, EndSpec) :-
  284    (   syntax_error(Error, Id, CharNo)
  285    ->  message_to_string(error(syntax_error(Id), _), Msg),
  286        (   integer(EndSpec)
  287        ->  End = EndSpec
  288        ;   character_count(EndSpec, End)
  289        ),
  290        show_syntax_error(TB, CharNo:Msg, Start-End),
  291        fail
  292    ;   throw(Error)
  293    ).
  294
  295syntax_error(error(syntax_error(Id), stream(_S, _Line, _LinePos, CharNo)),
  296             Id, CharNo).
  297syntax_error(error(syntax_error(Id), file(_S, _Line, _LinePos, CharNo)),
  298             Id, CharNo).
  299syntax_error(error(syntax_error(Id), string(_Text, CharNo)),
  300             Id, CharNo).
 warnable_singletons(+Singletons, -Warn) is det
Warn is the subset of the singletons that we warn about.
  306warnable_singletons([], []).
  307warnable_singletons([H|T0], List) :-
  308    H = (Name=_Var),
  309    (   '$is_named_var'(Name)
  310    ->  List = [H|T]
  311    ;   List = T
  312    ),
  313    warnable_singletons(T0, T).
 colour_item(+Class, +TB, +Pos) is det
  317colour_item(Class, TB, Pos) :-
  318    arg(1, Pos, Start),
  319    arg(2, Pos, End),
  320    Len is End - Start,
  321    colour_state_closure(TB, Closure),
  322    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.
  330safe_push_op(P, T, N0, State) :-
  331    colour_state_module(State, CM),
  332    strip_module(CM:N0, M, N),
  333    (   is_list(N),
  334        N \== []                                % define list as operator
  335    ->  acyclic_term(N),
  336        forall(member(Name, N),
  337               safe_push_op(P, T, M:Name, State))
  338    ;   push_op(P, T, M:N)
  339    ),
  340    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.
  348fix_operators((:- Directive), M, Src) :-
  349    ground(Directive),
  350    catch(process_directive(Directive, M, Src), _, true),
  351    !.
  352fix_operators(_, _, _).
  353
  354process_directive(style_check(X), _, _) :-
  355    !,
  356    style_check(X).
  357process_directive(set_prolog_flag(Flag, Value), M, _) :-
  358    syntax_flag(Flag),
  359    !,
  360    set_prolog_flag(M:Flag, Value).
  361process_directive(M:op(P,T,N), _, Src) :-
  362    !,
  363    process_directive(op(P,T,N), M, Src).
  364process_directive(op(P,T,N), M, Src) :-
  365    !,
  366    safe_push_op(P, T, M:N, Src).
  367process_directive(module(_Name, Export), M, Src) :-
  368    !,
  369    forall(member(op(P,A,N), Export),
  370           safe_push_op(P,A,M:N, Src)).
  371process_directive(use_module(Spec), _, Src) :-
  372    !,
  373    catch(process_use_module1(Spec, Src), _, true).
  374process_directive(use_module(Spec, Imports), _, Src) :-
  375    !,
  376    catch(process_use_module2(Spec, Imports, Src), _, true).
  377process_directive(Directive, _, Src) :-
  378    prolog_source:expand((:-Directive), Src, _).
  379
  380syntax_flag(character_escapes).
  381syntax_flag(var_prefix).
  382syntax_flag(allow_variable_name_as_functor).
  383syntax_flag(allow_dot_in_atom).
 process_use_module1(+Imports, +Src)
Get the exported operators from the referenced files.
  389process_use_module1([], _) :- !.
  390process_use_module1([H|T], Src) :-
  391    !,
  392    process_use_module1(H, Src),
  393    process_use_module1(T, Src).
  394process_use_module1(File, Src) :-
  395    (   xref_public_list(File, Src,
  396                         [ exports(Exports),
  397                           silent(true),
  398                           path(Path)
  399                         ])
  400    ->  forall(member(op(P,T,N), Exports),
  401               safe_push_op(P,T,N,Src)),
  402        colour_state_module(Src, SM),
  403        (   member(Syntax/4, Exports),
  404            load_quasi_quotation_syntax(SM:Path, Syntax),
  405            fail
  406        ;   true
  407        )
  408    ;   true
  409    ).
  410
  411process_use_module2(File, Imports, Src) :-
  412    (   xref_public_list(File, Src,
  413                         [ exports(Exports),
  414                           silent(true),
  415                           path(Path)
  416                         ])
  417    ->  forall(( member(op(P,T,N), Exports),
  418                 member(op(P,T,N), Imports)),
  419               safe_push_op(P,T,N,Src)),
  420        colour_state_module(Src, SM),
  421        (   member(Syntax/4, Exports),
  422            member(Syntax/4, Imports),
  423            load_quasi_quotation_syntax(SM:Path, Syntax),
  424            fail
  425        ;   true
  426        )
  427    ;   true
  428    ).
 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.
  437prolog_colourise_query(QueryString, SourceID, ColourItem) :-
  438    query_colour_state(SourceID, ColourItem, TB),
  439    setup_call_cleanup(
  440        save_settings(TB, [], State),
  441        colourise_query(QueryString, TB),
  442        restore_settings(State)).
  443
  444query_colour_state(module(Module), ColourItem, TB) :-
  445    !,
  446    make_colour_state([ source_id_list([]),
  447                        module(Module),
  448                        closure(ColourItem)
  449                      ],
  450                      TB).
  451query_colour_state(SourceID, ColourItem, TB) :-
  452    to_list(SourceID, SourceIDList),
  453    make_colour_state([ source_id_list(SourceIDList),
  454                        closure(ColourItem)
  455                      ],
  456                      TB).
  457
  458
  459colourise_query(QueryString, TB) :-
  460    colour_state_module(TB, SM),
  461    string_length(QueryString, End),
  462    (   catch(term_string(Query, QueryString,
  463                          [ subterm_positions(TermPos),
  464                            singletons(Singletons0),
  465                            module(SM),
  466                            comments(Comments)
  467                          ]),
  468              E,
  469              read_error(E, TB, 0, End))
  470    ->  warnable_singletons(Singletons0, Singletons),
  471        colour_state_singletons(TB, Singletons),
  472        colourise_comments(Comments, TB),
  473        (   Query == end_of_file
  474        ->  true
  475        ;   colourise_body(Query, TB, TermPos)
  476        )
  477    ;   true                        % only a syntax error
  478    ).
 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)
  493prolog_colourise_term(Stream, SourceId, ColourItem, Options) :-
  494    to_list(SourceId, SourceIdList),
  495    make_colour_state([ source_id_list(SourceIdList),
  496                        stream(Stream),
  497                        closure(ColourItem)
  498                      ],
  499                      TB),
  500    option(subterm_positions(TermPos), Options, _),
  501    findall(Op, xref_op(SourceId, Op), Ops),
  502    debug(colour, 'Ops from ~p: ~p', [SourceId, Ops]),
  503    findall(Opt, xref_flag_option(SourceId, Opt), Opts),
  504    character_count(Stream, Start),
  505    (   source_module(TB, Module)
  506    ->  true
  507    ;   Module = prolog_colour_ops
  508    ),
  509    read_source_term_at_location(
  510        Stream, Term,
  511        [ module(Module),
  512          operators(Ops),
  513          error(Error),
  514          subterm_positions(TermPos),
  515          singletons(Singletons0),
  516          comments(Comments)
  517        | Opts
  518        ]),
  519    (   var(Error)
  520    ->  warnable_singletons(Singletons0, Singletons),
  521        colour_state_singletons(TB, Singletons),
  522        colour_item(range, TB, TermPos),            % Call to allow clearing
  523        colourise_term(Term, TB, TermPos, Comments)
  524    ;   character_count(Stream, End),
  525        TermPos = error_position(Start, End, Pos),
  526        colour_item(range, TB, TermPos),
  527        show_syntax_error(TB, Error, Start-End),
  528        Error = Pos:_Message
  529    ).
  530
  531xref_flag_option(TB, var_prefix(Bool)) :-
  532    xref_prolog_flag(TB, var_prefix, Bool, _Line).
  533
  534show_syntax_error(TB, Pos:Message, Range) :-
  535    integer(Pos),
  536    !,
  537    End is Pos + 1,
  538    colour_item(syntax_error(Message, Range), TB, Pos-End).
  539show_syntax_error(TB, _:Message, Range) :-
  540    colour_item(syntax_error(Message, Range), TB, Range).
  541
  542
  543singleton(Var, TB) :-
  544    colour_state_singletons(TB, Singletons),
  545    member_var(Var, Singletons).
  546
  547member_var(V, [_=V2|_]) :-
  548    V == V2,
  549    !.
  550member_var(V, [_|T]) :-
  551    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.
  562colourise_term(Term, TB, TermPos, Comments) :-
  563    colourise_comments(Comments, TB),
  564    (   Term == end_of_file
  565    ->  true
  566    ;   colourise_term(Term, TB, TermPos),
  567        colourise_fullstop(TB, TermPos)
  568    ).
  569
  570colourise_fullstop(TB, TermPos) :-
  571    arg(2, TermPos, EndTerm),
  572    Start is EndTerm,
  573    End is Start+1,
  574    colour_item(fullstop, TB, Start-End).
  575
  576colourise_comments(-, _).
  577colourise_comments([], _).
  578colourise_comments([H|T], TB) :-
  579    colourise_comment(H, TB),
  580    colourise_comments(T, TB).
  581
  582colourise_comment((-)-_, _) :- !.
  583colourise_comment(Pos-Comment, TB) :-
  584    comment_style(Comment, Style),
  585    stream_position_data(char_count, Pos, Start),
  586    string_length(Comment, Len),
  587    End is Start + Len + 1,
  588    colour_item(comment(Style), TB, Start-End).
  589
  590comment_style(Comment, structured) :-           % Starts %%, %! or /**
  591    structured_comment_start(Start),
  592    sub_string(Comment, 0, Len, _, Start),
  593    Next is Len+1,
  594    string_code(Next, Comment, NextCode),
  595    code_type(NextCode, space),
  596    !.
  597comment_style(Comment, line) :-                 % Starts %
  598    sub_string(Comment, 0, _, _, '%'),
  599    !.
  600comment_style(_, block).                        % Starts /*
 structured_comment_start(-Start)
Copied from library(pldoc/doc_process). Unfortunate, but we do not want to force loading pldoc.
  607structured_comment_start('%%').
  608structured_comment_start('%!').
  609structured_comment_start('/**').
 colourise_term(+Term, +TB, +Pos)
Colorise a file toplevel term.
  615colourise_term(Var, TB, Start-End) :-
  616    var(Var),
  617    !,
  618    colour_item(instantiation_error, TB, Start-End).
  619colourise_term(_, _, Pos) :-
  620    var(Pos),
  621    !.
  622colourise_term(Term, TB, parentheses_term_position(PO,PC,Pos)) :-
  623    !,
  624    colour_item(parentheses, TB, PO-PC),
  625    colourise_term(Term, TB, Pos).
  626colourise_term(Term, TB, Pos) :-
  627    term_colours(Term, FuncSpec-ArgSpecs),
  628    !,
  629    Pos = term_position(F,T,FF,FT,ArgPos),
  630    colour_item(term, TB, F-T),     % TBD: Allow specifying by term_colours/2?
  631    specified_item(FuncSpec, Term, TB, FF-FT),
  632    specified_items(ArgSpecs, Term, TB, ArgPos).
  633colourise_term((Pre=>Body), TB,
  634               term_position(F,T,FF,FT,[PP,BP])) :-
  635    nonvar(Pre),
  636    Pre = (Head,Cond),
  637    PP = term_position(_HF,_HT,_HFF,_HFT,[HP,CP]),
  638    !,
  639    colour_item(clause,         TB, F-T),
  640    colour_item(neck(=>),       TB, FF-FT),
  641    colourise_clause_head(Head, TB, HP),
  642    colour_item(rule_condition, TB, CP),
  643    colourise_body(Cond, Head,  TB, CP),
  644    colourise_body(Body, Head,  TB, BP).
  645colourise_term(Term, TB,
  646               term_position(F,T,FF,FT,[HP,BP])) :-
  647    neck(Term, Head, Body, Neck),
  648    !,
  649    colour_item(clause,         TB, F-T),
  650    colour_item(neck(Neck),     TB, FF-FT),
  651    colourise_clause_head(Head, TB, HP),
  652    colourise_body(Body, Head,  TB, BP).
  653colourise_term(((Head,RHC) --> Body), TB,
  654               term_position(F,T,FF,FT,
  655                             [ term_position(_,_,_,_,[HP,RHCP]),
  656                               BP
  657                             ])) :-
  658    !,
  659    colour_item(grammar_rule,       TB, F-T),
  660    colour_item(dcg_right_hand_ctx, TB, RHCP),
  661    colourise_term_arg(RHC, TB, RHCP),
  662    colour_item(neck(-->),          TB, FF-FT),
  663    colourise_extended_head(Head, 2, TB, HP),
  664    colourise_dcg(Body, Head,       TB, BP).
  665colourise_term((Head --> Body), TB,                     % TBD: expansion!
  666               term_position(F,T,FF,FT,[HP,BP])) :-
  667    !,
  668    colour_item(grammar_rule,       TB, F-T),
  669    colour_item(neck(-->),          TB, FF-FT),
  670    colourise_extended_head(Head, 2, TB, HP),
  671    colourise_dcg(Body, Head,       TB, BP).
  672colourise_term(:->(Head, Body), TB,
  673               term_position(F,T,FF,FT,[HP,BP])) :-
  674    !,
  675    colour_item(method,             TB, F-T),
  676    colour_item(neck(:->), TB, FF-FT),
  677    colour_method_head(send(Head),  TB, HP),
  678    colourise_method_body(Body,     TB, BP).
  679colourise_term(:<-(Head, Body), TB,
  680               term_position(F,T,FF,FT,[HP,BP])) :-
  681    !,
  682    colour_item(method,            TB, F-T),
  683    colour_item(neck(:<-), TB, FF-FT),
  684    colour_method_head(get(Head),  TB, HP),
  685    colourise_method_body(Body,    TB, BP).
  686colourise_term((:- Directive), TB, Pos) :-
  687    !,
  688    colour_item(directive, TB, Pos),
  689    Pos = term_position(_F,_T,FF,FT,[ArgPos]),
  690    colour_item(neck(directive), TB, FF-FT),
  691    colourise_directive(Directive, TB, ArgPos).
  692colourise_term((?- Directive), TB, Pos) :-
  693    !,
  694    colourise_term((:- Directive), TB, Pos).
  695colourise_term(end_of_file, _, _) :- !.
  696colourise_term(Fact, TB, Pos) :-
  697    !,
  698    colour_item(clause, TB, Pos),
  699    colourise_clause_head(Fact, TB, Pos).
  700
  701neck((Head  :- Body), Head, Body, :-).
  702neck((Head  => Body), Head, Body, =>).
  703neck(?=>(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.
  711colourise_extended_head(Head, N, TB, Pos) :-
  712    extend(Head, N, TheHead),
  713    colourise_clause_head(TheHead, TB, Pos).
  714
  715extend(M:Head, N, M:ExtHead) :-
  716    nonvar(Head),
  717    !,
  718    extend(Head, N, ExtHead).
  719extend(Head, N, ExtHead) :-
  720    compound(Head),
  721    !,
  722    compound_name_arguments(Head, Name, Args),
  723    length(Extra, N),
  724    append(Args, Extra, NArgs),
  725    compound_name_arguments(ExtHead, Name, NArgs).
  726extend(Head, N, ExtHead) :-
  727    atom(Head),
  728    !,
  729    length(Extra, N),
  730    compound_name_arguments(ExtHead, Head, Extra).
  731extend(Head, _, Head).
  732
  733
  734colourise_clause_head(_, _, Pos) :-
  735    var(Pos),
  736    !.
  737colourise_clause_head(Head, TB, parentheses_term_position(PO,PC,Pos)) :-
  738    colour_item(parentheses, TB, PO-PC),
  739    colourise_clause_head(Head, TB, Pos).
  740colourise_clause_head(M:Head, TB, QHeadPos) :-
  741    QHeadPos = term_position(_,_,QF,QT,[MPos,HeadPos]),
  742    head_colours(M:Head, meta-[_, ClassSpec-ArgSpecs]),
  743    !,
  744    colourise_module(M, TB, MPos),
  745    colour_item(functor, TB, QF-QT),
  746    functor_position(HeadPos, FPos, ArgPos),
  747    (   ClassSpec == classify
  748    ->  classify_head(TB, Head, Class)
  749    ;   Class = ClassSpec
  750    ),
  751    colour_item(head_term(Class, Head), TB, QHeadPos),
  752    colour_item(head(Class, Head), TB, FPos),
  753    specified_items(ArgSpecs, Head, TB, ArgPos).
  754colourise_clause_head(Head, TB, Pos) :-
  755    head_colours(Head, ClassSpec-ArgSpecs),
  756    !,
  757    functor_position(Pos, FPos, ArgPos),
  758    (   ClassSpec == classify
  759    ->  classify_head(TB, Head, Class)
  760    ;   Class = ClassSpec
  761    ),
  762    colour_item(head_term(Class, Head), TB, Pos),
  763    colour_item(head(Class, Head), TB, FPos),
  764    specified_items(ArgSpecs, Head, TB, ArgPos).
  765colourise_clause_head(:=(Eval, Ret), TB,
  766                      term_position(_,_,AF,AT,
  767                                    [ term_position(_,_,SF,ST,
  768                                                    [ SelfPos,
  769                                                      FuncPos
  770                                                    ]),
  771                                      RetPos
  772                                    ])) :-
  773    Eval =.. [.,M,Func],
  774    FuncPos = term_position(_,_,FF,FT,_),
  775    !,
  776    colourise_term_arg(M, TB, SelfPos),
  777    colour_item(func_dot, TB, SF-ST),               % .
  778    colour_item(dict_function(Func), TB, FF-FT),
  779    colourise_term_args(Func, TB, FuncPos),
  780    colour_item(dict_return_op, TB, AF-AT),         % :=
  781    colourise_term_arg(Ret, TB, RetPos).
  782colourise_clause_head(Head, TB, Pos) :-
  783    functor_position(Pos, FPos, _),
  784    classify_head(TB, Head, Class),
  785    colour_item(head_term(Class, Head), TB, Pos),
  786    colour_item(head(Class, Head), TB, FPos),
  787    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.
  794colourise_extern_head(Head, M, TB, Pos) :-
  795    functor_position(Pos, FPos, _),
  796    colour_item(head(extern(M), Head), TB, FPos),
  797    colourise_term_args(Head, TB, Pos).
  798
  799colour_method_head(SGHead, TB, Pos) :-
  800    arg(1, SGHead, Head),
  801    functor_name(SGHead, SG),
  802    functor_position(Pos, FPos, _),
  803    colour_item(method(SG), TB, FPos),
  804    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'.
  811functor_position(term_position(_,_,FF,FT,ArgPos), FF-FT, ArgPos) :- !.
  812functor_position(list_position(F,_T,Elms,none), F-FT, Elms) :-
  813    !,
  814    FT is F + 1.
  815functor_position(dict_position(_,_,FF,FT,KVPos), FF-FT, KVPos) :- !.
  816functor_position(brace_term_position(F,T,Arg), F-T, [Arg]) :- !.
  817functor_position(Pos, Pos, []).
  818
  819colourise_module(Term, TB, Pos) :-
  820    (   var(Term)
  821    ;   atom(Term)
  822    ),
  823    !,
  824    colour_item(module(Term), TB, Pos).
  825colourise_module(_, TB, Pos) :-
  826    colour_item(type_error(module), TB, Pos).
 colourise_directive(+Body, +TB, +Pos)
Colourise the body of a directive.
  832colourise_directive(_,_,Pos) :-
  833    var(Pos),
  834    !.
  835colourise_directive(Dir, TB, parentheses_term_position(PO,PC,Pos)) :-
  836    !,
  837    colour_item(parentheses, TB, PO-PC),
  838    colourise_directive(Dir, TB, Pos).
  839colourise_directive((A,B), TB, term_position(_,_,_,_,[PA,PB])) :-
  840    !,
  841    colourise_directive(A, TB, PA),
  842    colourise_directive(B, TB, PB).
  843colourise_directive(Body, TB, Pos) :-
  844    nonvar(Body),
  845    directive_colours(Body, ClassSpec-ArgSpecs),   % specified
  846    !,
  847    functor_position(Pos, FPos, ArgPos),
  848    (   ClassSpec == classify
  849    ->  goal_classification(TB, Body, [], Class)
  850    ;   Class = ClassSpec
  851    ),
  852    colour_item(goal(Class, Body), TB, FPos),
  853    specified_items(ArgSpecs, Body, TB, ArgPos).
  854colourise_directive(Body, TB, Pos) :-
  855    colourise_body(Body, TB, Pos).
  856
  857
  858%       colourise_body(+Body, +TB, +Pos)
  859%
  860%       Breaks down to colourise_goal/3.
  861
  862colourise_body(Body, TB, Pos) :-
  863    colourise_body(Body, [], TB, Pos).
  864
  865colourise_body(Body, Origin, TB, Pos) :-
  866    colour_item(body, TB, Pos),
  867    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.
  876colourise_method_body(_, _, Pos) :-
  877    var(Pos),
  878    !.
  879colourise_method_body(Body, TB, parentheses_term_position(PO,PC,Pos)) :-
  880    !,
  881    colour_item(parentheses, TB, PO-PC),
  882    colourise_method_body(Body, TB, Pos).
  883colourise_method_body(::(_Comment,Body), TB,
  884                      term_position(_F,_T,_FF,_FT,[CP,BP])) :-
  885    !,
  886    colour_item(comment(string), TB, CP),
  887    colourise_body(Body, TB, BP).
  888colourise_method_body(Body, TB, Pos) :-         % deal with pri(::) < 1000
  889    Body =.. [F,A,B],
  890    control_op(F),
  891    !,
  892    Pos = term_position(_F,_T,FF,FT,
  893                        [ AP,
  894                          BP
  895                        ]),
  896    colour_item(control, TB, FF-FT),
  897    colourise_method_body(A, TB, AP),
  898    colourise_body(B, TB, BP).
  899colourise_method_body(Body, TB, Pos) :-
  900    colourise_body(Body, TB, Pos).
  901
  902control_op(',').
  903control_op((;)).
  904control_op((->)).
  905control_op((*->)).
 colourise_goals(+Body, +Origin, +TB, +Pos)
Colourise the goals in a body.
  911colourise_goals(_, _, _, Pos) :-
  912    var(Pos),
  913    !.
  914colourise_goals(Body, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
  915    !,
  916    colour_item(parentheses, TB, PO-PC),
  917    colourise_goals(Body, Origin, TB, Pos).
  918colourise_goals(Body, Origin, TB, term_position(_,_,FF,FT,ArgPos)) :-
  919    body_compiled(Body),
  920    !,
  921    colour_item(control, TB, FF-FT),
  922    colourise_subgoals(ArgPos, 1, Body, Origin, TB).
  923colourise_goals(Goal, Origin, TB, Pos) :-
  924    colourise_goal(Goal, Origin, TB, Pos).
  925
  926colourise_subgoals([], _, _, _, _).
  927colourise_subgoals([Pos|T], N, Body, Origin, TB) :-
  928    arg(N, Body, Arg),
  929    colourise_goals(Arg, Origin, TB, Pos),
  930    NN is N + 1,
  931    colourise_subgoals(T, NN, Body, Origin, TB).
 colourise_dcg(+Body, +Head, +TB, +Pos)
Breaks down to colourise_dcg_goal/3.
  937colourise_dcg(Body, Head, TB, Pos) :-
  938    colour_item(dcg, TB, Pos),
  939    (   dcg_extend(Head, Origin)
  940    ->  true
  941    ;   Origin = Head
  942    ),
  943    colourise_dcg_goals(Body, Origin, TB, Pos).
  944
  945colourise_dcg_goals(Var, _, TB, Pos) :-
  946    var(Var),
  947    !,
  948    colour_item(goal(meta,Var), TB, Pos).
  949colourise_dcg_goals(_, _, _, Pos) :-
  950    var(Pos),
  951    !.
  952colourise_dcg_goals(Body, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
  953    !,
  954    colour_item(parentheses, TB, PO-PC),
  955    colourise_dcg_goals(Body, Origin, TB, Pos).
  956colourise_dcg_goals({Body}, Origin, TB, brace_term_position(F,T,Arg)) :-
  957    !,
  958    colour_item(dcg(plain), TB, F-T),
  959    colourise_goals(Body, Origin, TB, Arg).
  960colourise_dcg_goals([], _, TB, Pos) :-
  961    !,
  962    colour_item(dcg(terminal), TB, Pos).
  963colourise_dcg_goals(List, _, TB, list_position(F,T,Elms,Tail)) :-
  964    List = [_|_],
  965    !,
  966    colour_item(dcg(terminal), TB, F-T),
  967    colourise_list_args(Elms, Tail, List, TB, classify).
  968colourise_dcg_goals(_, _, TB, string_position(F,T)) :-
  969    integer(F),
  970    !,
  971    colour_item(dcg(string), TB, F-T).
  972colourise_dcg_goals(Body, Origin, TB, term_position(_,_,FF,FT,ArgPos)) :-
  973    dcg_body_compiled(Body),       % control structures
  974    !,
  975    colour_item(control, TB, FF-FT),
  976    colourise_dcg_subgoals(ArgPos, 1, Body, Origin, TB).
  977colourise_dcg_goals(Goal, Origin, TB, Pos) :-
  978    colourise_dcg_goal(Goal, Origin, TB, Pos).
  979
  980colourise_dcg_subgoals([], _, _, _, _).
  981colourise_dcg_subgoals([Pos|T], N, Body, Origin, TB) :-
  982    arg(N, Body, Arg),
  983    colourise_dcg_goals(Arg, Origin, TB, Pos),
  984    NN is N + 1,
  985    colourise_dcg_subgoals(T, NN, Body, Origin, TB).
  986
  987dcg_extend(Term, _) :-
  988    var(Term), !, fail.
  989dcg_extend(M:Term, M:Goal) :-
  990    dcg_extend(Term, Goal).
  991dcg_extend(Term, Goal) :-
  992    compound(Term),
  993    !,
  994    compound_name_arguments(Term, Name, Args),
  995    append(Args, [_,_], NArgs),
  996    compound_name_arguments(Goal, Name, NArgs).
  997dcg_extend(Term, Goal) :-
  998    atom(Term),
  999    !,
 1000    compound_name_arguments(Goal, Term, [_,_]).
 1001
 1002dcg_body_compiled(G) :-
 1003    body_compiled(G),
 1004    !.
 1005dcg_body_compiled((_|_)).
 1006
 1007%       colourise_dcg_goal(+Goal, +Origin, +TB, +Pos).
 1008
 1009colourise_dcg_goal(!, Origin, TB, TermPos) :-
 1010    !,
 1011    colourise_goal(!, Origin, TB, TermPos).
 1012colourise_dcg_goal(Goal, Origin, TB, TermPos) :-
 1013    dcg_extend(Goal, TheGoal),
 1014    !,
 1015    colourise_goal(TheGoal, Origin, TB, TermPos).
 1016colourise_dcg_goal(Goal, _, TB, Pos) :-
 1017    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.
 1028                                        % Deal with list as goal (consult)
 1029colourise_goal(_,_,_,Pos) :-
 1030    var(Pos),
 1031    !.
 1032colourise_goal(Goal, Origin, TB, parentheses_term_position(PO,PC,Pos)) :-
 1033    !,
 1034    colour_item(parentheses, TB, PO-PC),
 1035    colourise_goal(Goal, Origin, TB, Pos).
 1036colourise_goal(Goal, _, TB, Pos) :-
 1037    Pos = list_position(F,T,Elms,TailPos),
 1038    Goal = [_|_],
 1039    !,
 1040    FT is F + 1,
 1041    AT is T - 1,
 1042    colour_item(goal_term(built_in, Goal), TB, Pos),
 1043    colour_item(goal(built_in, Goal), TB, F-FT),
 1044    colour_item(goal(built_in, Goal), TB, AT-T),
 1045    colourise_file_list(Goal, TB, Elms, TailPos, any).
 1046colourise_goal(Goal, Origin, TB, Pos) :-
 1047    Pos = list_position(F,T,Elms,Tail),
 1048    callable(Goal),
 1049    Goal =.. [_,GH,GT|_],
 1050    !,
 1051    goal_classification(TB, Goal, Origin, Class),
 1052    FT is F + 1,
 1053    AT is T - 1,
 1054    colour_item(goal_term(Class, Goal), TB, Pos),
 1055    colour_item(goal(Class, Goal), TB, F-FT),
 1056    colour_item(goal(Class, Goal), TB, AT-T),
 1057    colourise_list_args(Elms, Tail, [GH|GT], TB, classify).
 1058colourise_goal(Goal, _Origin, TB, Pos) :-
 1059    Pos = quasi_quotation_position(_F,_T,_QQType,_QQTypePos,_CPos),
 1060    !,
 1061    colourise_term_arg(Goal, TB, Pos).
 1062colourise_goal(Goal, Origin, TB, Pos) :-
 1063    strip_module(Goal, _, PGoal),
 1064    nonvar(PGoal),
 1065    (   goal_classification(TB, Goal, Origin, ClassInferred),
 1066        call_goal_colours(Goal, ClassInferred, ClassSpec-ArgSpecs)
 1067    ->  true
 1068    ;   call_goal_colours(Goal, ClassSpec-ArgSpecs)
 1069    ),
 1070    !,                                          % specified
 1071    functor_position(Pos, FPos, ArgPos),
 1072    (   ClassSpec == classify
 1073    ->  goal_classification(TB, Goal, Origin, Class)
 1074    ;   Class = ClassSpec
 1075    ),
 1076    colour_item(goal_term(Class, Goal), TB, Pos),
 1077    colour_item(goal(Class, Goal), TB, FPos),
 1078    colour_dict_braces(TB, Pos),
 1079    specified_items(ArgSpecs, Goal, TB, ArgPos).
 1080colourise_goal(Module:Goal, _Origin, TB, QGoalPos) :-
 1081    QGoalPos = term_position(_,_,QF,QT,[PM,PG]),
 1082    !,
 1083    colourise_module(Module, TB, PM),
 1084    colour_item(functor, TB, QF-QT),
 1085    (   PG = term_position(_,_,FF,FT,_)
 1086    ->  FP = FF-FT
 1087    ;   FP = PG
 1088    ),
 1089    (   callable(Goal)
 1090    ->  qualified_goal_classification(Module:Goal, TB, Class),
 1091        colour_item(goal_term(Class, Goal), TB, QGoalPos),
 1092        colour_item(goal(Class, Goal), TB, FP),
 1093        colourise_goal_args(Goal, Module, TB, PG)
 1094    ;   var(Goal)
 1095    ->  colourise_term_arg(Goal, TB, PG)
 1096    ;   colour_item(type_error(callable), TB, PG)
 1097    ).
 1098colourise_goal(Op, _Origin, TB, Pos) :-
 1099    nonvar(Op),
 1100    Op = op(_,_,_),
 1101    !,
 1102    colourise_op_declaration(Op, TB, Pos).
 1103colourise_goal(Goal, Origin, TB, Pos) :-
 1104    goal_classification(TB, Goal, Origin, Class),
 1105    (   Pos = term_position(_,_,FF,FT,_ArgPos)
 1106    ->  FPos = FF-FT
 1107    ;   FPos = Pos
 1108    ),
 1109    colour_item(goal_term(Class, Goal), TB, Pos),
 1110    colour_item(goal(Class, Goal), TB, FPos),
 1111    colourise_goal_args(Goal, TB, Pos).
 1112
 1113% make sure to emit a fragment for the braces of tag{k:v, ...} or
 1114% {...} that is mapped to something else.
 1115
 1116colour_dict_braces(TB, dict_position(_F,T,_TF,TT,_KVPos)) :-
 1117    !,
 1118    BStart is TT+1,
 1119    colour_item(dict_content, TB, BStart-T).
 1120colour_dict_braces(TB, brace_term_position(F,T,_Arg)) :-
 1121    !,
 1122    colour_item(brace_term, TB, F-T).
 1123colour_dict_braces(_, _).
 colourise_goal_args(+Goal, +TB, +Pos)
Colourise the arguments to a goal. This predicate deals with meta- and database-access predicates.
 1130colourise_goal_args(Goal, TB, Pos) :-
 1131    colourization_module(TB, Module),
 1132    colourise_goal_args(Goal, Module, TB, Pos).
 1133
 1134colourization_module(TB, Module) :-
 1135    (   colour_state_source_id(TB, SourceId),
 1136        xref_module(SourceId, Module)
 1137    ->  true
 1138    ;   Module = user
 1139    ).
 1140
 1141colourise_goal_args(Goal, M, TB, term_position(_,_,_,_,ArgPos)) :-
 1142    !,
 1143    (   meta_args(Goal, TB, MetaArgs)
 1144    ->  colourise_meta_args(1, Goal, M, MetaArgs, TB, ArgPos)
 1145    ;   colourise_goal_args(1, Goal, M, TB, ArgPos)
 1146    ).
 1147colourise_goal_args(Goal, M, TB, brace_term_position(_,_,ArgPos)) :-
 1148    !,
 1149    (   meta_args(Goal, TB, MetaArgs)
 1150    ->  colourise_meta_args(1, Goal, M, MetaArgs, TB, [ArgPos])
 1151    ;   colourise_goal_args(1, Goal, M, TB, [ArgPos])
 1152    ).
 1153colourise_goal_args(_, _, _, _).                % no arguments
 1154
 1155colourise_goal_args(_, _, _, _, []) :- !.
 1156colourise_goal_args(N, Goal, Module, TB, [P0|PT]) :-
 1157    colourise_option_arg(Goal, Module, N, TB, P0),
 1158    !,
 1159    NN is N + 1,
 1160    colourise_goal_args(NN, Goal, Module, TB, PT).
 1161colourise_goal_args(N, Goal, Module, TB, [P0|PT]) :-
 1162    arg(N, Goal, Arg),
 1163    colourise_term_arg(Arg, TB, P0),
 1164    NN is N + 1,
 1165    colourise_goal_args(NN, Goal, Module, TB, PT).
 1166
 1167
 1168colourise_meta_args(_, _, _, _, _, []) :- !.
 1169colourise_meta_args(N, Goal, Module, MetaArgs, TB, [P0|PT]) :-
 1170    colourise_option_arg(Goal, Module, N, TB, P0),
 1171    !,
 1172    NN is N + 1,
 1173    colourise_meta_args(NN, Goal, Module, MetaArgs, TB, PT).
 1174colourise_meta_args(N, Goal, Module, MetaArgs, TB, [P0|PT]) :-
 1175    arg(N, Goal, Arg),
 1176    arg(N, MetaArgs, MetaSpec),
 1177    colourise_meta_arg(MetaSpec, Arg, TB, P0),
 1178    NN is N + 1,
 1179    colourise_meta_args(NN, Goal, Module, MetaArgs, TB, PT).
 1180
 1181colourise_meta_arg(MetaSpec, Arg, TB, Pos) :-
 1182    nonvar(Arg),
 1183    expand_meta(MetaSpec, Arg, Expanded),
 1184    !,
 1185    colourise_goal(Expanded, [], TB, Pos). % TBD: recursion
 1186colourise_meta_arg(MetaSpec, Arg, TB, Pos) :-
 1187    nonvar(Arg),
 1188    MetaSpec == //,
 1189    !,
 1190    colourise_dcg_goals(Arg, //, TB, Pos).
 1191colourise_meta_arg(_, Arg, TB, Pos) :-
 1192    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.

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

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