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