34
35:- module(prolog_history,
36 [ prolog_history/1
37 ]). 38:- use_module(library(base32)). 39
40:- multifile
41 prolog:history/2.
57:- create_prolog_flag(save_history, true, [type(boolean)]).
64history_directory(Dir) :-
65 absolute_file_name(app_preferences('.swipl-dir-history'),
66 Dir,
67 [ access(write),
68 file_type(directory),
69 file_errors(fail)
70 ]),
71 !.
72history_directory(Dir) :-
73 absolute_file_name(app_preferences('.'),
74 Home,
75 [ access(write),
76 file_type(directory),
77 file_errors(fail)
78 ]),
79 atom_concat(Home, '/.swipl-dir-history', Dir),
80 ( exists_directory(Dir)
81 -> fail
82 ; make_directory(Dir)
83 ).
90dir_history_file(Dir, File) :-
91 nonvar(Dir),
92 !,
93 history_directory(Base),
94 absolute_file_name(Dir, Path),
95 base32(Path, Encoded),
96 atomic_list_concat([Base, Encoded], /, File).
97dir_history_file(Dir, File) :-
98 history_directory(HDir),
99 directory_files(HDir, Files),
100 '$member'(Base32, Files),
101 base32(Dir, Base32),
102 !,
103 atomic_list_concat([Dir, Base32], /, File).
104
105write_history(File) :-
106 current_prolog_flag(save_history, true),
107 catch(prolog:history(user_input, save(File)), _, true), !.
108write_history(_).
122:- dynamic
123 history_loaded/1. 124
125load_dir_history(File) :-
126 ( exists_file(File),
127 prolog:history(user_input, load(File))
128 -> assertz(history_loaded(File))
129 ; true
130 ).
131
132prolog_history(enable) :-
133 history_loaded(_),
134 !.
135prolog_history(enable) :-
136 catch(dir_history_file('.', File), E,
137 (print_message(warning, E),fail)),
138 catch(load_dir_history(File), E,
139 print_message(warning, E)),
140 !,
141 at_halt(write_history(File)),
142 set_prolog_flag(save_history, true).
143prolog_history(_) :-
144 set_prolog_flag(save_history, false).
145
146 149
150:- if(current_predicate('$rl_history'/1)). 151:- use_module(library(readutil)). 152
153prolog:history(_, load(File)) :-
154 access_file(File, read),
155 !,
156 setup_call_cleanup(
157 open(File, read, In, [encoding(utf8)]),
158 read_history(In),
159 close(In)).
160prolog:history(_, load(_)).
161
162read_history(In) :-
163 repeat,
164 read_line_to_codes(In, Codes),
165 ( Codes == end_of_file
166 -> !
167 ; atom_codes(Line, Codes),
168 rl_add_history(Line),
169 fail
170 ).
171
172prolog:history(_, save(File)) :-
173 '$rl_history'(Lines),
174 ( Lines \== []
175 -> setup_call_cleanup(
176 open(File, write, Out, [encoding(utf8)]),
177 forall(member(Line, Lines),
178 format(Out, '~w~n', [Line])),
179 close(Out))
180 ; true
181 ).
182
183:- endif.
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
~/.swipl-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. */