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)  2011-2019, VU University Amsterdam
    7                              CWI, Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(prolog_history,
   37          [ prolog_history/1
   38          ]).   39:- use_module(library(base32)).   40
   41:- multifile
   42    prolog:history/2.

Per-directory persistent commandline history

This module implements persistency of the commandline history over Prolog sessions on Prolog installations that are based on the GNU readline library (default for the development version on Unix systems).

The history is stored in the directory <config>/dir-history. For each directory for which it keeps the history, there is file whose name is the base32 encoding of the directory path.

This file is normally loaded when Prolog is started if user_input is a terminal and the system supports history. */

   58:- create_prolog_flag(save_history, true, [type(boolean)]).
 history_directory(-Dir) is semidet
Dir is the directory where the per-directory history databases are stored.
   65history_directory(Dir) :-
   66    absolute_file_name(user_app_config('dir-history'),
   67                       Dir,
   68                       [ access(write),
   69                         file_type(directory),
   70                         file_errors(fail)
   71                       ]),
   72    !.
   73history_directory(Dir) :-
   74    absolute_file_name(user_app_config('.'),
   75                       ConfigDir,
   76                       [ access(write),
   77                         file_type(directory),
   78                         file_errors(fail)
   79                       ]),
   80    atom_concat(ConfigDir, '/dir-history', Dir),
   81    (   exists_directory(Dir)
   82    ->  '$my_file'(Dir)
   83    ;   file_directory_name(Dir, Parent),
   84        '$my_file'(Parent),
   85        make_directory(Dir)
   86    ).
 dir_history_file(+Dir, -File) is det
dir_history_file(?Dir, ?File) is nondet
File is the history file for a Prolog session running in Dir.
   93dir_history_file(Dir, File) :-
   94    nonvar(Dir),
   95    !,
   96    history_directory(Base),
   97    absolute_file_name(Dir, Path),
   98    base32(Path, Encoded),
   99    atomic_list_concat([Base, Encoded], /, File).
  100dir_history_file(Dir, File) :-
  101    history_directory(HDir),
  102    directory_files(HDir, Files),
  103    '$member'(Base32, Files),
  104    base32(Dir, Base32),
  105    !,
  106    atomic_list_concat([Dir, Base32], /, File).
  107
  108write_history(File) :-
  109    current_prolog_flag(save_history, true),
  110    catch(prolog:history(user_input, save(File)), _, true), !.
  111write_history(_).
 prolog_history(+Action) is det
Execute Action on the history. Action is one of
enable
Enable history. First loads history for the current directory. Loading the history is done at most once.
disable
Sets the Prolog flag save_history to false, such that the history is not saved on halt.
  125:- dynamic
  126    history_loaded/1.  127
  128load_dir_history(File) :-
  129    (   exists_file(File),
  130        prolog:history(user_input, load(File))
  131    ->  assertz(history_loaded(File))
  132    ;   true
  133    ).
  134
  135prolog_history(enable) :-
  136    history_loaded(_),
  137    !.
  138prolog_history(enable) :-
  139    catch(dir_history_file('.', File), E,
  140          (print_message(warning, E),fail)),
  141    catch(load_dir_history(File), E,
  142          print_message(warning, E)),
  143    !,
  144    at_halt(write_history(File)),
  145    set_prolog_flag(save_history, true).
  146prolog_history(_) :-
  147    set_prolog_flag(save_history, false).
  148
  149		 /*******************************
  150		 *      SWIPL-WIN SUPPORT	*
  151		 *******************************/
  152
  153:- if(current_predicate('$rl_history'/1)).  154:- use_module(library(readutil)).  155
  156prolog:history(_, load(File)) :-
  157    access_file(File, read),
  158    !,
  159    setup_call_cleanup(
  160        open(File, read, In, [encoding(utf8)]),
  161        read_history(In),
  162        close(In)).
  163prolog:history(_, load(_)).
  164
  165read_history(In) :-
  166    repeat,
  167    read_line_to_codes(In, Codes),
  168    (   Codes == end_of_file
  169    ->  !
  170    ;   atom_codes(Line, Codes),
  171        rl_add_history(Line),
  172        fail
  173    ).
  174
  175prolog:history(_, save(File)) :-
  176    '$rl_history'(Lines),
  177    (   Lines \== []
  178    ->  setup_call_cleanup(
  179            open(File, write, Out, [encoding(utf8)]),
  180            forall(member(Line, Lines),
  181                   format(Out, '~w~n', [Line])),
  182            close(Out))
  183    ;   true
  184    ).
  185
  186:- endif.