View source with formatted 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)  2017-2025, VU University Amsterdam
    7                              CWI Amsterdam
    8                              SWI-Prolog Solutions b.v.
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(editline,
   38          [ el_wrap/0,                          % wrap user_input, etc.
   39            el_wrap/1,                          % +Options
   40            el_wrap/4,                          % +Prog, +Input, +Output, +Error
   41            el_wrap/5,                          % +Prog, +Input, +Output, +Error, +Options
   42            el_wrapped/1,                       % +Input
   43            el_unwrap/1,                        % +Input
   44
   45            el_source/2,                        % +Input, +File
   46            el_bind/2,                          % +Input, +Args
   47            el_set/2,                           % +Input, +Action
   48            el_get/2,                           % +Input, ?Property
   49            el_addfn/4,                         % +Input, +Name, +Help, :Goal
   50            el_cursor/2,                        % +Input, +Move
   51            el_line/2,                          % +Input, -Line
   52            el_insertstr/2,                     % +Input, +Text
   53            el_deletestr/2,                     % +Input, +Count
   54
   55            el_history/2,                       % +Input, ?Action
   56            el_history_events/2,                % +Input, -Events
   57            el_add_history/2,                   % +Input, +Line
   58            el_write_history/2,                 % +Input, +FileName
   59            el_read_history/2,                  % +Input, +FileName
   60
   61	    el_version/1			% -Version:integer
   62          ]).   63:- autoload(library(apply),[maplist/2,maplist/3]).   64:- autoload(library(lists),[reverse/2,max_list/2,append/3,member/2]).   65:- autoload(library(solution_sequences),[call_nth/2]).   66:- autoload(library(option), [merge_options/3]).   67
   68:- use_foreign_library(foreign(libedit4pl)).   69
   70:- initialization el_wrap_if_ok.   71
   72:- meta_predicate
   73    el_addfn(+,+,+,3).   74
   75:- multifile
   76    el_setup/1,                         % +Input
   77    prolog:complete_input/4.   78
   79
   80/** <module> BSD libedit based command line editing
   81
   82This library wraps the BSD  libedit   command  line  editor. The binding
   83provides a high level API to enable   command line editing on the Prolog
   84user streams and low level predicates  to   apply  the  library on other
   85streams and program the library.
   86*/
   87
   88el_wrap_if_ok :-
   89    \+ current_prolog_flag(readline, readline),
   90    stream_property(user_input, tty(true)),
   91    !,
   92    el_wrap.
   93el_wrap_if_ok.
   94
   95%!  el_wrap is det.
   96%!  el_wrap(+Options) is det.
   97%
   98%   Enable using editline on the standard   user streams if `user_input`
   99%   is connected to a terminal. This is   the  high level predicate used
  100%   for most purposes. The remainder of the library interface deals with
  101%   low level predicates  that  allows   for  applying  and  programming
  102%   libedit in non-standard situations.
  103%
  104%   The library is registered  with  _ProgName_   set  to  `swipl`  (see
  105%   el_wrap/4).
  106%
  107%   Options processed:
  108%
  109%     - pipes(+Boolean)
  110%       Used by Epilog windows to indicate we are reading from a Windows
  111%       named pipe in _overlapped_ mode.  Ignored on other platforms.
  112%     - history(+Size)
  113%       Size of the history.  Default is defined by the Prolog flag
  114%       `history` or `100` if this flag is not defined.
  115%     - alert_signo(+Integer)
  116%       Signal used for making thread_signal/2 work while the thread
  117%       is in a blocking system call.
  118
  119el_wrap :-
  120    el_wrap([]).
  121
  122el_wrap(_) :-
  123    el_wrapped(user_input),
  124    !.
  125el_wrap(Options) :-
  126    stream_property(user_input, tty(true)), !,
  127    findall(Opt, el_default(Opt), Defaults),
  128    merge_options(Options, Defaults, Options1),
  129    el_wrap(swipl, user_input, user_output, user_error, Options1),
  130    add_prolog_commands(user_input),
  131    ignore(el_set(user_input, wordchars("_"))),
  132    forall(el_setup(user_input), true),
  133    enable_bracketed_paste(user_input),
  134    enable_windows_eof(user_input).
  135el_wrap(_).
  136
  137el_default(history(Size)) :-
  138    current_prolog_flag(history, Value),
  139    (   integer(Value),
  140        Value >= 0
  141    ->  Size = Value
  142    ;   Value == false
  143    ->  Size = 0
  144    ).
  145:- if(current_predicate(prolog_alert_signal/2)).  146el_default(alert_signo(SigNo)) :-
  147    prolog_alert_signal(SigName, SigName),
  148    current_signal(SigName, SigNo, _Handler).
  149:- endif.  150
  151add_prolog_commands(Input) :-
  152    el_addfn(Input, complete, 'Complete atoms and files', complete),
  153    el_addfn(Input, show_completions, 'List completions', show_completions),
  154    el_addfn(Input, electric, 'Indicate matching bracket', electric),
  155    el_addfn(Input, isearch_history, 'Incremental search in history',
  156             isearch_history),
  157    el_addfn(Input, bracketed_paste, 'Handle bracketed paste', bracketed_paste),
  158    el_bind(Input, ["^I",  complete]),
  159    el_bind(Input, ["^[?", show_completions]),
  160    el_bind(Input, ["^R",  isearch_history]),
  161    bind_electric(Input),
  162    add_paste_quoted(Input),
  163    el_source(Input, _).
  164
  165%!  enable_bracketed_paste(+Input) is det.
  166%
  167%   Sync bracketed paste mode with the current editor: bind ESC[200~
  168%   to bracketed_paste/3 and enable the mode in emacs, unbind and
  169%   disable it in vi.  In vi mode ESC leaves insert mode, so the
  170%   ESC[200~ start marker cannot be dispatched as a key binding.
  171%   Called from el_wrap/1 after the el_setup/1 hook, since that hook
  172%   may switch editor with el_bind/2 `-v` or `-e`.
  173
  174enable_bracketed_paste(Input) :-
  175    (   el_get(Input, editor(vi))
  176    ->  el_bind(Input, ['-r', "\e[200~"]),
  177        el_set(Input, bracketed_paste(false))
  178    ;   el_bind(Input, ["\e[200~", bracketed_paste]),
  179        el_set(Input, bracketed_paste(true))
  180    ).
  181
  182%!  enable_windows_eof(+Input) is det.
  183%
  184%   Bind ``^Z`` to send end-of-file on Windows, matching the platform
  185%   convention (compare with ``^D`` on Unix).  Routed through libedit's
  186%   built-in `em-delete-or-list` (emacs) or `vi-list-or-eof` (vi), both
  187%   of which return EOF on an empty line.
  188
  189enable_windows_eof(Input) :-
  190    current_prolog_flag(windows, true),
  191    !,
  192    (   el_get(Input, editor(vi))
  193    ->  el_bind(Input, ["^Z", 'vi-list-or-eof'])
  194    ;   el_bind(Input, ["^Z", 'em-delete-or-list'])
  195    ).
  196enable_windows_eof(_).
  197
  198%!  el_wrap(+ProgName:atom, +In:stream, +Out:stream, +Error:stream) is det.
  199%!  el_wrap(+ProgName:atom, +In:stream, +Out:stream, +Error:stream, +Options) is det.
  200%
  201%   Enable editline on  the  stream-triple   <In,Out,Error>.  From  this
  202%   moment on In is a handle to the command line editor.  Options:
  203%
  204%     - pipes(true)
  205%       Windows only. Assume the I/O is using pipes rather than a
  206%       console.  This is used for the Epilog terminal.
  207%
  208%   @arg ProgName is the name of the invoking program, used when reading
  209%   the editrc(5) file to determine which settings to use.
  210
  211el_wrap(ProgName, In, Out, Error) :-
  212    el_wrap(ProgName, In, Out, Error, []).
  213
  214%!  el_setup(+In:stream) is nondet.
  215%
  216%   This hooks is called as   forall(el_setup(Input),  true) _after_ the
  217%   input stream has been wrapped, the default Prolog commands have been
  218%   added and the  default  user  setup   file  has  been  sourced using
  219%   el_source/2. It can be used to define and bind additional commands.
  220
  221%!  el_wrapped(+In:stream) is semidet.
  222%
  223%   True if In is a stream wrapped by el_wrap/3.
  224
  225%!  el_unwrap(+In:stream) is det.
  226%
  227%   Remove the libedit wrapper for In and   the related output and error
  228%   streams.
  229%
  230%   @bug The wrapper creates =|FILE*|= handles that cannot be closed and
  231%   thus wrapping and unwrapping implies a (modest) memory leak.
  232
  233%!  el_source(+In:stream, +File) is det.
  234%
  235%   Initialise editline by reading the contents of File.  If File is
  236%   unbound try =|$HOME/.editrc|=
  237
  238
  239%!  el_bind(+In:stream, +Args) is det.
  240%
  241%   Invoke the libedit `bind` command  with   the  given  arguments. The
  242%   example below lists the current key bindings.
  243%
  244%   ```
  245%   ?- el_bind(user_input, ['-a']).
  246%   ```
  247%
  248%   The predicate el_bind/2 is typically used   to bind commands defined
  249%   using el_addfn/4. Note that the C proxy   function has only the last
  250%   character of the command as context to find the Prolog binding. This
  251%   implies we cannot both bind  e.g.,  "^[?"   _and_  "?"  to  a Prolog
  252%   function.
  253%
  254%   @see editrc(5) for more information.
  255
  256%!  el_addfn(+Input:stream, +Command, +Help, :Goal) is det.
  257%
  258%   Add a new command to the command  line editor associated with Input.
  259%   Command is the name of the command,  Help is the help string printed
  260%   with e.g. =|bind -a|= (see el_bind/2)  and   Goal  is  called of the
  261%   associated key-binding is activated.  Goal is called as
  262%
  263%       call(:Goal, +Input, +Char, -Continue)
  264%
  265%   where Input is the input stream providing access to the editor, Char
  266%   the activating character and Continue must   be instantated with one
  267%   of the known continuation  codes  as   defined  by  libedit: `norm`,
  268%   `newline`, `eof`, `arghack`, `refresh`,   `refresh_beep`,  `cursor`,
  269%   `redisplay`, `error` or `fatal`. In addition, the following Continue
  270%   code is provided.
  271%
  272%     * electric(Move, TimeOut, Continue)
  273%     Show _electric caret_ at Move positions to the left of the normal
  274%     cursor positions for the given TimeOut.  Continue as defined by
  275%     the Continue value.
  276%
  277%   The registered Goal typically used el_line/2 to fetch the input line
  278%   and el_cursor/2, el_insertstr/2 and/or  el_deletestr/2 to manipulate
  279%   the input line.
  280%
  281%   Normally el_bind/2 is used to associate   the defined command with a
  282%   keyboard sequence.
  283%
  284%   @see el_set(3) =EL_ADDFN= for details.
  285
  286%!  el_set(+Input:stream, +Action) is semidet.
  287%
  288%   Interface to el_set() and el_wset().   Currently provided values for
  289%   Action are:
  290%
  291%     - wordchars(+Text)
  292%       Set the characters considered part of a _word_.  This feature
  293%       depends on el_wsey() ``EL_WORDCHARS``, which is only provided
  294%       in some recent versions of `libedit`.
  295%     - bracketed_paste(+Boolean)
  296%       Enable or disable bracketed paste mode.  When enabled, the
  297%       terminal is asked to bracket pasted text with ``ESC[200~`` /
  298%       ``ESC[201~`` before each prompt, which the default bindings
  299%       route through bracketed_paste/3.  Disabling sends the matching
  300%       ``ESC[?2004l`` sequence immediately.  enable_bracketed_paste/1
  301%       manages this based on the current editor; you normally do not
  302%       need to set it directly.
  303%
  304%   This predicate fails silently of Action  is not implemented. Illegal
  305%   input raises in an exception.
  306
  307%!  el_get(+Input:stream, ?Property) is semidet.
  308%
  309%   Interface to el_get().  Currently supported Property terms:
  310%
  311%     - editor(-Editor)
  312%       Editor is unified with `emacs` or `vi`, reflecting the current
  313%       keymap selected via el_bind/2 with `-e` / `-v`.
  314%     - bracketed_paste(-Boolean)
  315%       Whether bracketed paste mode is currently enabled; see
  316%       el_set/2.
  317%
  318%   Any other Property raises a `domain_error(editline_property, _)`.
  319
  320%!  el_line(+Input:stream, -Line) is det.
  321%
  322%   Fetch the currently buffered input line. Line is a term line(Before,
  323%   After), where `Before` is  a  string   holding  the  text before the
  324%   cursor and `After` is a string holding the text after the cursor.
  325
  326%!  el_cursor(+Input:stream, +Move:integer) is det.
  327%
  328%   Move the cursor Move  character   forwards  (positive)  or backwards
  329%   (negative).
  330
  331%!  el_insertstr(+Input:stream, +Text) is det.
  332%
  333%   Insert Text at the cursor.
  334
  335%!  el_deletestr(+Input:stream, +Count) is det.
  336%
  337%   Delete Count characters before the cursor.
  338
  339%!  el_history(+In:stream, ?Action) is det.
  340%
  341%   Perform a generic action on the history. This provides an incomplete
  342%   interface to history() from libedit.  Supported actions are:
  343%
  344%     - clear
  345%       Clear the history.
  346%     - setsize(+Integer)
  347%       Set size of history to size elements.
  348%     - getsize(-Integer)
  349%       Unify Integer with the maximum size of the history.  Note that
  350%       this is _not_ the same as el_history() using ``H_GETSIZE``,
  351%       which returns the number of currently saved events. The number
  352%       of saved events may be computed from `first` and `last` or
  353%       using el_history_events/2.
  354%     - setunique(+Boolean)
  355%       Set flag that adjacent identical event strings should not be
  356%       entered into the history.
  357%     - first(-Num, -String)
  358%     - last(-Num, -String)
  359%     - curr(-Num, -String)
  360%     - prev(-Num, -String)
  361%     - next(-Num, -String)
  362%       Retrieve an event.  Num is the event number and String is the
  363%       event string.  Note that `first` is the most recent event and
  364%       `last` the oldest.
  365%     - set(Num)
  366%       Set the notion of _current_ to Num.
  367%     - prev_str(+Search, -Num, -String)
  368%     - next_str(+Search, -Num, -String)
  369%       Retrieve the previous or next event whose String starts with
  370%       Search.
  371%     - event(+Num, -String)
  372%       True when String represents event Num.   This is an extension to
  373%       the history() API, retrieving a numbered event without changing
  374%       the current notion.
  375
  376%!  el_history_events(+In:stream, -Events:list(pair)) is det.
  377%
  378%   Unify Events with a list of pairs   of  the form `Num-String`, where
  379%   `Num` is the event number  and   `String`  is  the associated string
  380%   without terminating newline.
  381
  382%!  el_add_history(+In:stream, +Line:text) is det.
  383%
  384%   Add a line to the command line history.
  385
  386%!  el_read_history(+In:stream, +File:file) is det.
  387%
  388%   Read the history saved using el_write_history/2.
  389%
  390%   @arg File is a file specification for absolute_file_name/3.
  391
  392%!  el_write_history(+In:stream, +File:file) is det.
  393%
  394%   Save editline history to File.  The   history  may be reloaded using
  395%   el_read_history/2.
  396%
  397%   @arg File is a file specification for absolute_file_name/3.
  398
  399%!  el_version(-Version)
  400%
  401%   True when Version  is ``LIBEDIT_MAJOR*10000 + LIBEDIT_MINOR*100``.
  402%   The  version is  generated from  the include  file ``histedit.h``,
  403%   which implies that the actual version of the shared library may be
  404%   different.
  405
  406%!  prolog:history(+Input, ?Action) is semidet.
  407%
  408%   Provide  the  plugable  interface  into   the  system  command  line
  409%   management.
  410
  411:- multifile
  412    prolog:history/2.  413
  414prolog:history(Input, enabled) :-
  415    !,
  416    el_wrapped(Input),
  417    el_history(Input, getsize(Size)),
  418    Size > 0.
  419prolog:history(Input, add(Line)) :-
  420    !,
  421    el_add_history(Input, Line).
  422prolog:history(Input, load(File)) :-
  423    !,
  424    compat_read_history(Input, File).
  425prolog:history(Input, save(File)) :-
  426    !,
  427    el_write_history(Input, File).
  428prolog:history(Input, events(Events)) :-
  429    !,
  430    el_history_events(Input, Events).
  431prolog:history(Input, Command) :-
  432    public_command(Command),
  433    !,
  434    el_history(Input, Command).
  435
  436public_command(first(_Num, _String)).
  437public_command(curr(_Num, _String)).
  438public_command(event(_Num, _String)).
  439public_command(prev_str(_Search, _Num, _String)).
  440public_command(clear).
  441
  442%!  compat_read_history(+Input, +File) is det.
  443%
  444%   Read the saved history. This loads both  the LibEdit and old history
  445%   format used by `swipl-win.exe` before migrating to SDL.
  446
  447compat_read_history(Input, File) :-
  448    catch(el_read_history(Input, File), error(editline(_),_), fail),
  449    !.
  450compat_read_history(Input, File) :-
  451    access_file(File, read),
  452    setup_call_cleanup(
  453        open(File, read, In, [encoding(utf8)]),
  454        read_old_history(Input, In),
  455        close(In)),
  456    !.
  457compat_read_history(_, _).
  458
  459read_old_history(Input, From) :-
  460    catch('$raw_read'(From, Line), error(_,_), fail),
  461    (   Line == end_of_file
  462    ->  true
  463    ;   string_concat(Line, '.', Event),
  464        el_add_history(Input, Event),
  465        read_old_history(Input, From)
  466    ).
  467
  468		 /*******************************
  469		 *        ELECTRIC CARET	*
  470		 *******************************/
  471
  472%!  bind_electric(+Input) is det.
  473%
  474%   Bind known close statements for electric input
  475
  476bind_electric(Input) :-
  477    forall(bracket(_Open, Close), bind_code(Input, Close, electric)),
  478    forall(quote(Close), bind_code(Input, Close, electric)).
  479
  480bind_code(Input, Code, Command) :-
  481    string_codes(Key, [Code]),
  482    el_bind(Input, [Key, Command]).
  483
  484
  485%!  electric(+Input, +Char, -Continue) is det.
  486
  487electric(Input, Char, Continue) :-
  488    string_codes(Str, [Char]),
  489    el_insertstr(Input, Str),
  490    el_line(Input, line(Before, _)),
  491    (   string_codes(Before, Codes),
  492        nesting(Codes, 0, Nesting),
  493        reverse(Nesting, [Close|RevNesting])
  494    ->  (   Close = open(_,_)                   % open quote
  495        ->  Continue = refresh
  496        ;   matching_open(RevNesting, Close, _, Index)
  497        ->  string_length(Before, Len),         % Proper match
  498            Move is Index-Len,
  499            Continue = electric(Move, 500, refresh)
  500        ;   Continue = refresh_beep             % Not properly nested
  501        )
  502    ;   Continue = refresh_beep
  503    ).
  504
  505matching_open_index(String, Index) :-
  506    string_codes(String, Codes),
  507    nesting(Codes, 0, Nesting),
  508    reverse(Nesting, [Close|RevNesting]),
  509    matching_open(RevNesting, Close, _, Index).
  510
  511matching_open([Open|Rest], Close, Rest, Index) :-
  512    Open = open(Index,_),
  513    match(Open, Close),
  514    !.
  515matching_open([Close1|Rest1], Close, Rest, Index) :-
  516    Close1 = close(_,_),
  517    matching_open(Rest1, Close1, Rest2, _),
  518    matching_open(Rest2, Close, Rest, Index).
  519
  520match(open(_,Open),close(_,Close)) :-
  521    (   bracket(Open, Close)
  522    ->  true
  523    ;   Open == Close,
  524        quote(Open)
  525    ).
  526
  527bracket(0'(, 0')).
  528bracket(0'[, 0']).
  529bracket(0'{, 0'}).
  530
  531quote(0'\').
  532quote(0'\").
  533quote(0'\`).
  534
  535nesting([], _, []).
  536nesting([H|T], I, Nesting) :-
  537    (   bracket(H, _Close)
  538    ->  Nesting = [open(I,H)|Nest]
  539    ;   bracket(_Open, H)
  540    ->  Nesting = [close(I,H)|Nest]
  541    ),
  542    !,
  543    I2 is I+1,
  544    nesting(T, I2, Nest).
  545nesting([0'0, 0'\'|T], I, Nesting) :-
  546    !,
  547    phrase(skip_code, T, T1),
  548    difflist_length(T, T1, Len),
  549    I2 is I+Len+2,
  550    nesting(T1, I2, Nesting).
  551nesting([H|T], I, Nesting) :-
  552    quote(H),
  553    !,
  554    (   phrase(skip_quoted(H), T, T1)
  555    ->  difflist_length(T, T1, Len),
  556        I2 is I+Len+1,
  557        Nesting = [open(I,H),close(I2,H)|Nest],
  558        nesting(T1, I2, Nest)
  559    ;   Nesting = [open(I,H)]                   % Open quote
  560    ).
  561nesting([_|T], I, Nesting) :-
  562    I2 is I+1,
  563    nesting(T, I2, Nesting).
  564
  565difflist_length(List, Tail, Len) :-
  566    difflist_length(List, Tail, 0, Len).
  567
  568difflist_length(List, Tail, Len0, Len) :-
  569    List == Tail,
  570    !,
  571    Len = Len0.
  572difflist_length([_|List], Tail, Len0, Len) :-
  573    Len1 is Len0+1,
  574    difflist_length(List, Tail, Len1, Len).
  575
  576skip_quoted(H) -->
  577    [H],
  578    !.
  579skip_quoted(H) -->
  580    "\\", [H],
  581    !,
  582    skip_quoted(H).
  583skip_quoted(H) -->
  584    [_],
  585    skip_quoted(H).
  586
  587skip_code -->
  588    "\\", [_],
  589    !.
  590skip_code -->
  591    [_].
  592
  593
  594		 /*******************************
  595		 *           COMPLETION		*
  596		 *******************************/
  597
  598%!  complete(+Input, +Char, -Continue) is det.
  599%
  600%   Implementation of the registered `complete`   editline function. The
  601%   predicate is called with three arguments,  the first being the input
  602%   stream used to access  the  libedit   functions  and  the second the
  603%   activating character. The last argument tells   libedit  what to do.
  604%   Consult el_set(3), =EL_ADDFN= for details.
  605
  606
  607:- dynamic
  608    last_complete/2.  609
  610complete(Input, _Char, Continue) :-
  611    el_line(Input, line(Before, After)),
  612    ensure_input_completion,
  613    prolog:complete_input(Before, After, Delete, Completions),
  614    (   Completions = [One]
  615    ->  string_length(Delete, Len),
  616        el_deletestr(Input, Len),
  617        complete_text(One, Text),
  618        el_insertstr(Input, Text),
  619        Continue = refresh
  620    ;   Completions == []
  621    ->  Continue = refresh_beep
  622    ;   get_time(Now),
  623        retract(last_complete(TLast, Before)),
  624        Now - TLast < 2
  625    ->  nl(user_error),
  626        list_alternatives(Completions),
  627        Continue = redisplay
  628    ;   retractall(last_complete(_,_)),
  629        get_time(Now),
  630        asserta(last_complete(Now, Before)),
  631        common_competion(Completions, Extend),
  632        (   Delete == Extend
  633        ->  Continue = refresh_beep
  634        ;   string_length(Delete, Len),
  635            el_deletestr(Input, Len),
  636            el_insertstr(Input, Extend),
  637            Continue = refresh
  638        )
  639    ).
  640
  641:- dynamic
  642    input_completion_loaded/0.  643
  644ensure_input_completion :-
  645    input_completion_loaded,
  646    !.
  647ensure_input_completion :-
  648    predicate_property(prolog:complete_input(_,_,_,_),
  649                       number_of_clauses(N)),
  650    N > 0,
  651    !.
  652ensure_input_completion :-
  653    exists_source(library(console_input)),
  654    !,
  655    use_module(library(console_input), []),
  656    asserta(input_completion_loaded).
  657ensure_input_completion.
  658
  659
  660%!  show_completions(+Input, +Char, -Continue) is det.
  661%
  662%   Editline command to show possible completions.
  663
  664show_completions(Input, _Char, Continue) :-
  665    el_line(Input, line(Before, After)),
  666    prolog:complete_input(Before, After, _Delete, Completions),
  667    nl(user_error),
  668    list_alternatives(Completions),
  669    Continue = redisplay.
  670
  671complete_text(Text-_Comment, Text) :- !.
  672complete_text(Text, Text).
  673
  674%!  common_competion(+Alternatives, -Common) is det.
  675%
  676%   True when Common is the common prefix of all candidate Alternatives.
  677
  678common_competion(Alternatives, Common) :-
  679    maplist(atomic, Alternatives),
  680    !,
  681    common_prefix(Alternatives, Common).
  682common_competion(Alternatives, Common) :-
  683    maplist(complete_text, Alternatives, AltText),
  684    !,
  685    common_prefix(AltText, Common).
  686
  687%!  common_prefix(+Atoms, -Common) is det.
  688%
  689%   True when Common is the common prefix of all Atoms.
  690
  691common_prefix([A1|T], Common) :-
  692    common_prefix_(T, A1, Common).
  693
  694common_prefix_([], Common, Common).
  695common_prefix_([H|T], Common0, Common) :-
  696    common_prefix(H, Common0, Common1),
  697    common_prefix_(T, Common1, Common).
  698
  699%!  common_prefix(+A1, +A2, -Prefix:string) is det.
  700%
  701%   True when Prefix is the common prefix of the atoms A1 and A2
  702
  703common_prefix(A1, A2, Prefix) :-
  704    sub_atom(A1, 0, _, _, A2),
  705    !,
  706    Prefix = A2.
  707common_prefix(A1, A2, Prefix) :-
  708    sub_atom(A2, 0, _, _, A1),
  709    !,
  710    Prefix = A1.
  711common_prefix(A1, A2, Prefix) :-
  712    atom_codes(A1, C1),
  713    atom_codes(A2, C2),
  714    list_common_prefix(C1, C2, C),
  715    string_codes(Prefix, C).
  716
  717list_common_prefix([H|T0], [H|T1], [H|T]) :-
  718    !,
  719    list_common_prefix(T0, T1, T).
  720list_common_prefix(_, _, []).
  721
  722
  723
  724%!  list_alternatives(+Alternatives)
  725%
  726%   List possible completions at the current point.
  727%
  728%   @tbd currently ignores the Comment in Text-Comment alternatives.
  729
  730list_alternatives(Alternatives) :-
  731    maplist(atomic, Alternatives),
  732    !,
  733    length(Alternatives, Count),
  734    maplist(atom_length, Alternatives, Lengths),
  735    max_list(Lengths, Max),
  736    tty_size(_, Cols),
  737    ColW is Max+2,
  738    Columns is max(1, Cols // ColW),
  739    RowCount is (Count+Columns-1)//Columns,
  740    length(Rows, RowCount),
  741    to_matrix(Alternatives, Rows, Rows),
  742    (   RowCount > 11
  743    ->  length(First, 10),
  744        Skipped is RowCount - 10,
  745        append(First, _, Rows),
  746        maplist(write_row(ColW), First),
  747        format(user_error, '... skipped ~D rows~n', [Skipped])
  748    ;   maplist(write_row(ColW), Rows)
  749    ).
  750list_alternatives(Alternatives) :-
  751    maplist(complete_text, Alternatives, AltText),
  752    list_alternatives(AltText).
  753
  754to_matrix([], _, Rows) :-
  755    !,
  756    maplist(close_list, Rows).
  757to_matrix([H|T], [RH|RT], Rows) :-
  758    !,
  759    add_list(RH, H),
  760    to_matrix(T, RT, Rows).
  761to_matrix(List, [], Rows) :-
  762    to_matrix(List, Rows, Rows).
  763
  764add_list(Var, Elem) :-
  765    var(Var), !,
  766    Var = [Elem|_].
  767add_list([_|T], Elem) :-
  768    add_list(T, Elem).
  769
  770close_list(List) :-
  771    append(List, [], _),
  772    !.
  773
  774write_row(ColW, Row) :-
  775    length(Row, Columns),
  776    make_format(Columns, ColW, Format),
  777    format(user_error, Format, Row).
  778
  779make_format(N, ColW, Format) :-
  780    format(string(PerCol), '~~w~~t~~~d+', [ColW]),
  781    Front is N - 1,
  782    length(LF, Front),
  783    maplist(=(PerCol), LF),
  784    append(LF, ['~w~n'], Parts),
  785    atomics_to_string(Parts, Format).
  786
  787
  788		 /*******************************
  789		 *             SEARCH		*
  790		 *******************************/
  791
  792%!  isearch_history(+Input, +Char, -Continue) is det.
  793%
  794%   Incremental search through the history.  The behavior is based
  795%   on GNU readline.
  796
  797isearch_history(Input, _Char, Continue) :-
  798    el_line(Input, line(Before, After)),
  799    string_concat(Before, After, Current),
  800    string_length(Current, Len),
  801    search_print('', "", Current),
  802    search(Input, "", Current, 1, Line),
  803    el_deletestr(Input, Len),
  804    el_insertstr(Input, Line),
  805    Continue = redisplay.
  806
  807search(Input, For, Current, Nth, Line) :-
  808    el_getc(Input, Next),
  809    Next \== -1,
  810    !,
  811    search(Next, Input, For, Current, Nth, Line).
  812search(_Input, _For, _Current, _Nth, "").
  813
  814search(7, _Input, _, Current, _, Current) :-    % C-g: abort
  815    !,
  816    clear_line.
  817search(18, Input, For, Current, Nth, Line) :-   % C-r: search previous
  818    !,
  819    N2 is Nth+1,
  820    search_(Input, For, Current, N2, Line).
  821search(19, Input, For, Current, Nth, Line) :-   % C-s: search next
  822    !,
  823    N2 is max(1,Nth-1),
  824    search_(Input, For, Current, N2, Line).
  825search(127, Input, For, Current, _Nth, Line) :- % DEL/BS: shorten search
  826    sub_string(For, 0, _, 1, For1),
  827    !,
  828    search_(Input, For1, Current, 1, Line).
  829search(Char, Input, For, Current, Nth, Line) :-
  830    code_type(Char, cntrl),
  831    !,
  832    search_end(Input, For, Current, Nth, Line),
  833    el_push(Input, Char).
  834search(Char, Input, For, Current, _Nth, Line) :-
  835    format(string(For1), '~w~c', [For,Char]),
  836    search_(Input, For1, Current, 1, Line).
  837
  838search_(Input, For1, Current, Nth, Line) :-
  839    (   find_in_history(Input, For1, Current, Nth, Candidate)
  840    ->  search_print('', For1, Candidate)
  841    ;   search_print('failed ', For1, Current)
  842    ),
  843    search(Input, For1, Current, Nth, Line).
  844
  845search_end(Input, For, Current, Nth, Line) :-
  846    (   find_in_history(Input, For, Current, Nth, Line)
  847    ->  true
  848    ;   Line = Current
  849    ),
  850    clear_line.
  851
  852find_in_history(_, "", Current, _, Current) :-
  853    !.
  854find_in_history(Input, For, _, Nth, Line) :-
  855    el_history_events(Input, History),
  856    call_nth(( member(_N-Line, History),
  857               sub_string(Line, _, _, _, For)
  858             ),
  859             Nth),
  860    !.
  861
  862search_print(State, Search, Current) :-
  863    format(user_error, '\r(~wreverse-i-search)`~w\': ~w\e[0K',
  864           [State, Search, Current]).
  865
  866clear_line :-
  867    format(user_error, '\r\e[0K', []).
  868
  869
  870                /*******************************
  871                *         PASTE QUOTED         *
  872                *******************************/
  873
  874:- meta_predicate
  875    with_quote_flags(+,+,0).  876
  877add_paste_quoted(Input) :-
  878    current_prolog_flag(gui, true),
  879    !,
  880    el_addfn(Input, paste_quoted, 'Paste as quoted atom', paste_quoted),
  881    el_bind(Input, ["^Y",  paste_quoted]).
  882add_paste_quoted(_).
  883
  884%!  paste_quoted(+Input, +Char, -Continue) is det.
  885%
  886%   Paste the selection as quoted Prolog value.   The quoting type
  887%   depends on the quote before the caret.  If there is no quote
  888%   before the caret we paste as an atom.
  889
  890paste_quoted(Input, _Char, Continue) :-
  891    clipboard_content(String),
  892    quote_text(Input, String, Quoted),
  893    el_insertstr(Input, Quoted),
  894    Continue = refresh.
  895
  896quote_text(Input, String, Value) :-
  897    el_line(Input, line(Before, _After)),
  898    (   sub_string(Before, _, 1, 0, Quote)
  899    ->  true
  900    ;   Quote = "'"
  901    ),
  902    quote_text(Input, Quote, String, Value).
  903
  904quote_text(Input, "'", Text, Quoted) =>
  905    format(string(Quoted), '~q', [Text]),
  906    el_deletestr(Input, 1).
  907quote_text(Input, "\"", Text, Quoted) =>
  908    atom_string(Text, String),
  909    with_quote_flags(
  910        string, codes,
  911        format(string(Quoted), '~q', [String])),
  912    el_deletestr(Input, 1).
  913quote_text(Input, "`", Text, Quoted) =>
  914    atom_string(Text, String),
  915    with_quote_flags(
  916        codes, string,
  917        format(string(Quoted), '~q', [String])),
  918    el_deletestr(Input, 1).
  919quote_text(_, _, Text, Quoted) =>
  920    format(string(Quoted), '~q', [Text]).
  921
  922with_quote_flags(Double, Back, Goal) :-
  923    current_prolog_flag(double_quotes, ODouble),
  924    current_prolog_flag(back_quotes, OBack),
  925    setup_call_cleanup(
  926        ( set_prolog_flag(double_quotes, Double),
  927          set_prolog_flag(back_quotes, Back) ),
  928        Goal,
  929        ( set_prolog_flag(double_quotes, ODouble),
  930          set_prolog_flag(back_quotes, OBack) )).
  931
  932clipboard_content(Text) :-
  933    current_prolog_flag(gui, true),
  934    !,
  935    autoload_call(in_pce_thread_sync(
  936                      autoload_call(
  937                          get(@(display), paste, primary, string(Text))))).
  938clipboard_content("").
  939
  940
  941                /*******************************
  942                *       BRACKETED PASTE        *
  943                *******************************/
  944
  945%!  bracketed_paste(+Input, +Char, -Continue) is det.
  946%
  947%   Handler for the bracketed paste start sequence ESC[200~.  Reads
  948%   characters until the end sequence ESC[201~ is received and inserts
  949%   the collected text literally, bypassing per-character key dispatch.
  950%
  951%   The terminal is asked to enable bracketed paste mode (ESC[?2004h)
  952%   from the C layer each time a prompt is issued.
  953
  954bracketed_paste(Input, _Char, Continue) :-
  955    collect_paste(Input, [], RevCodes),
  956    reverse(RevCodes, Codes),
  957    string_codes(Text, Codes),
  958    el_insertstr(Input, Text),
  959    Continue = refresh.
  960
  961%!  collect_paste(+Input, +RevAcc, -RevResult) is det.
  962%
  963%   Read characters one at a time, accumulating them in reverse order.
  964%   Stop when the reversed accumulator starts with the end sequence
  965%   ESC[201~ (27,91,50,48,49,126) and return the reversed content that
  966%   precedes it.
  967
  968collect_paste(Input, RevCodes, Result) :-
  969    el_getc(Input, Char),
  970    (   Char == -1                          % EOF / error
  971    ->  Result = RevCodes
  972    ;   paste_char(Char, Char1),
  973        RevCodes1 = [Char1|RevCodes],
  974        (   RevCodes1 = [0'~,0'1,0'0,0'2,0'[,0'\e|Rest]  % ESC[201~ reversed
  975        ->  Result = Rest
  976        ;   collect_paste(Input, RevCodes1, Result)
  977        )
  978    ).
  979
  980%!  paste_char(+Raw, -Char) is det.
  981%
  982%   Translate a raw character code from bracketed paste to the intended
  983%   code.  The tty line discipline in edit mode has INLCR set, which maps
  984%   LF (10) to CR (13) before libedit reads it.  We reverse that here so
  985%   pasted newlines are inserted as actual newlines.
  986
  987paste_char(0'\r, 0'\n) :- !.               % CR → LF (INLCR maps \n to \r in edit mode)
  988paste_char(C,   C).
  989
  990
  991                /*******************************
  992                *           MESSAGE            *
  993                *******************************/
  994
  995:- multifile prolog:error_message//1.  996
  997prolog:error_message(editline(Msg)) -->
  998    [ 'editline: ~s'-[Msg] ]