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
    6    Copyright (c)  1985-2025, 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('$history',
   39          [ read_term_with_history/2           % -Term, +Line
   40          ]).   41
   42:- multifile
   43    prolog:history/2.
 read_term_with_history(-Term, +Options)
Read a term guide by Options and maintain a history similar to most Unix shells.
   50read_term_with_history(Term, Options) :-
   51    '$option'(prompt(Prompt), Options, '~! ?-'),
   52    '$option'(input(Input), Options, user_input),
   53    repeat,
   54        prompt_history(Prompt),
   55        '$toplevel':read_query_line(Input, Raw),
   56        read_history_(Raw, Term, Options),
   57    !.
   58
   59read_history_(Raw, _Term, Options) :-
   60    '$option'(show(Raw), Options, history),
   61    list_history,
   62    !,
   63    fail.
   64read_history_(Raw, _Term, Options) :-
   65    '$option'(help(Raw), Options, '!help'),
   66    '$option'(show(Show), Options, '!history'),
   67    print_message(help, history(help(Show, Raw))),
   68    !,
   69    fail.
   70read_history_(Raw, Term, Options) :-
   71    expand_history(Raw, Expanded, Changed),
   72    add_to_history(Expanded, Options),
   73    '$option'(module(Module), Options, Var),
   74    (   Module == Var
   75    ->  '$current_typein_module'(Module)
   76    ;   true
   77    ),
   78    '$option'(variable_names(Bindings), Options, Bindings0),
   79    history_blob_mode(Changed, BlobMode),
   80    catch(read_term_from_atom(Expanded, Term0,
   81                              [ module(Module),
   82                                variable_names(Bindings0),
   83                                blob(BlobMode)
   84                              ]),
   85          E,
   86          (   print_message(error, E),
   87              fail
   88          )),
   89    (   var(Term0)
   90    ->  Term = Term0,
   91        Bindings = Bindings0
   92    ;   (   Changed == true
   93        ->  print_message(query, history(expanded(Expanded)))
   94        ;   true
   95        ),
   96        Term = Term0,
   97        Bindings = Bindings0
   98    ).
 history_blob_mode(+Changed, -Mode) is det
Decide how read_term/3 should deal with a blob written as <Type>(...). Text the user just typed may hold a blob of this process, so we resolve it. Text substituted from the history may come from a previous process, where an address that happens to match a live blob would resolve to an unrelated object.
  108history_blob_mode(true, dead) :- !.             % expanded from history
  109history_blob_mode(_, resolve).
 list_history
Write recorded history events using print_message/2.
  115list_history :-
  116    prolog:history(current_input, events(Events0)),
  117    !,
  118    '$reverse'(Events0, Events),
  119    print_message(query, history(history(Events))).
  120list_history :-
  121    print_message(query, history(no_history)).
 prompt_history(+Prompt)
Set the prompt using prompt1/1, substituting '~!' by the event number.
  128prompt_history('') :-
  129    !,
  130    ttyflush.
  131prompt_history(Prompt) :-
  132    (   prolog:history(current_input, curr(Curr, _))
  133    ->  This is Curr + 1
  134    ;   This = 1
  135    ),
  136    atom_codes(Prompt, SP),
  137    atom_codes(This, ST),
  138    (   atom_codes('~!', Repl),
  139        substitute(Repl, ST, SP, String)
  140    ->  prompt1(String)
  141    ;   prompt1(Prompt)
  142    ),
  143    ttyflush.
 substitute(+Old, +New, +String, -Substituted) is semidet
substitute first occurence of Old in String by New
  149substitute(Old, New, String, Substituted) :-
  150    '$append'(Head, OldAndTail, String),
  151    '$append'(Old, Tail, OldAndTail),
  152    !,
  153    '$append'(Head, New, HeadAndNew),
  154    '$append'(HeadAndNew, Tail, Substituted).
 add_to_history(+Line:atom, +Options) is det
Add Line to the command line editing history. Line contains the query as an atom without the Prolog full stop.
  161add_to_history(end_of_file, _) :- !.
  162add_to_history(Line, Options) :-
  163    '$option'(no_save(NoSave), Options),
  164    catch(term_string(Query, Line), error(_,_), fail),
  165    nonvar(Query),
  166    memberchk(Query, NoSave),
  167    !.
  168add_to_history(Line, _Options) :-
  169    format(string(CompleteLine), '~W~W',
  170           [ Line, [partial(true)],
  171             '.',  [partial(true)]
  172           ]),
  173    catch(prolog:history(user_input, add(CompleteLine)), _, fail),
  174    !.
  175add_to_history(_, _).
 expand_history(+Raw, -Expanded)
Expand Raw using the available history list. Expandations performed are:

!match % Last event starting <match> !n % Event nr. <n> !! % last event

Note: the first character after a '!' should be a letter or number to avoid problems with the cut.

  188expand_history(Raw, Expanded, Changed) :-
  189    atom_chars(Raw, RawString),
  190    expand_history2(RawString, ExpandedString, Changed),
  191    atom_chars(Expanded, ExpandedString),
  192    !.
  193
  194expand_history2([!], [!], false) :- !.
  195expand_history2([!, C|Rest], [!|Expanded], Changed) :-
  196    not_event_char(C),
  197    !,
  198    expand_history2([C|Rest], Expanded, Changed).
  199expand_history2([!|Rest], Expanded, true) :-
  200    !,
  201    match_event(Rest, Event, NewRest),
  202    '$append'(Event, RestExpanded, Expanded),
  203    !,
  204    expand_history2(NewRest, RestExpanded, _).
  205expand_history2(['\''|In], ['\''|Out], Changed) :-
  206    !,
  207    skip_quoted(In, '\'', Out, Tin, Tout),
  208    expand_history2(Tin, Tout, Changed).
  209expand_history2(['"'|In], ['"'|Out], Changed) :-
  210    !,
  211    skip_quoted(In, '"', Out, Tin, Tout),
  212    expand_history2(Tin, Tout, Changed).
  213expand_history2([H|T], [H|R], Changed) :-
  214    !,
  215    expand_history2(T, R, Changed).
  216expand_history2([], [], false).
  217
  218skip_quoted([Q|T],Q,[Q|R], T, R) :- !.
  219skip_quoted([\,Q|T0],Q,[\,Q|T], In, Out) :-
  220    !,
  221    skip_quoted(T0, Q, T, In, Out).
  222skip_quoted([Q,Q|T0],Q,[Q,Q|T], In, Out) :-
  223    !,
  224    skip_quoted(T0, Q, T, In, Out).
  225skip_quoted([C|T0],Q,[C|T], In, Out) :-
  226    !,
  227    skip_quoted(T0, Q, T, In, Out).
  228skip_quoted([], _, [], [], []).
 get_last_event(-Chars) is semidet
return last event typed as a list of characters without the Prolog full stop.
  235get_last_event(Event) :-
  236    prolog:history(current_input, first(_Num, String)),
  237    string_chars(String, Event0),
  238    remove_full_stop(Event0, Event),
  239    !.
  240get_last_event(_) :-
  241    print_message(query, history(no_event)),
  242    fail.
  243
  244remove_full_stop(In, Out) :-
  245    phrase(remove_full_stop(Out), In).
  246
  247remove_full_stop([]) -->
  248    spaces, ['.'], spaces, eos,
  249    !.
  250remove_full_stop([H|T]) -->
  251    [H], !,
  252    remove_full_stop(T).
  253remove_full_stop([]) -->
  254    [].
  255
  256spaces --> space, !, spaces.
  257spaces --> [].
  258
  259space -->
  260    [C],
  261    { char_type(C, space) }.
  262
  263eos([], []).
  264
  265%   match_event(+Spec, -Event, -Rest)
  266%   Use Spec as a specification of and event and return the event as Event
  267%   and what is left of Spec as Rest.
  268
  269match_event(Spec, Event, Rest) :-
  270    find_event(Spec, Event, Rest),
  271    !.
  272match_event(_, _, _) :-
  273    print_message(query, history(no_event)),
  274    fail.
  275
  276not_event_char(C) :- code_type(C, csym), !, fail.
  277not_event_char(!) :- !, fail.
  278not_event_char(_).
  279
  280find_event([!|Left], Event, Left) :-
  281    !,
  282    get_last_event(Event).
  283find_event([N|Rest], Event, Left) :-
  284    code_type(N, digit),
  285    !,
  286    take_number([N|Rest], NumCodes, Left),
  287    number_codes(Number, NumCodes),
  288    prolog:history(current_input, event(Number, String)),
  289    string_chars(String, Event0),
  290    remove_full_stop(Event0, Event).
  291find_event(Spec, Event, Left) :-
  292    take_string(Spec, String, Left),
  293    matching_event(String, Event).
  294
  295take_string([C|Rest], [C|String], Left) :-
  296    code_type(C, csym),
  297    !,
  298    take_string(Rest, String, Left).
  299take_string([C|Rest], [], [C|Rest]) :- !.
  300take_string([], [], []).
  301
  302take_number([C|Rest], [C|String], Left) :-
  303    code_type(C, digit),
  304    !,
  305    take_string(Rest, String, Left).
  306take_number([C|Rest], [], [C|Rest]) :- !.
  307take_number([], [], []).
 matching_event(+String, -Chars) is semidet
Return first event with prefix String as a list of Prolog chars without trailing full stop.
  314matching_event(String, Chars) :-
  315    prolog:history(current_input, prev_str(String, _Num, String)),
  316    string_chars(String, Chars0),
  317    remove_full_stop(Chars0, Chars)