File: prolog_history.pl

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (172 lines) | stat: -rw-r--r-- 5,069 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@cs.vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2011, VU University Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(prolog_history,
	  [ prolog_history/1
	  ]).
:- use_module(library(base32)).

/** <module> 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.
*/

:- 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.

history_directory(Dir) :-
	absolute_file_name(app_preferences('.swipl-dir-history'),
			   Dir,
			   [ access(write),
			     file_type(directory),
			     file_errors(fail)
			   ]), !.
history_directory(Dir) :-
	absolute_file_name(app_preferences('.'),
			   Home,
			   [ access(write),
			     file_type(directory),
			     file_errors(fail)
			   ]),
	atom_concat(Home, '/.swipl-dir-history', Dir),
	(   exists_directory(Dir)
	->  fail
	;   make_directory(Dir)
	).

%%	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.

dir_history_file(Dir, File) :-
	nonvar(Dir), !,
	history_directory(Base),
	absolute_file_name(Dir, Path),
	base32(Path, Encoded),
	atomic_list_concat([Base, Encoded], /, File).
dir_history_file(Dir, File) :-
	history_directory(HDir),
	directory_files(HDir, Files),
	'$member'(Base32, Files),
	base32(Dir, Base32),
	atomic_list_concat([Dir, Base32], /, File).

% Realise write/read of history for the swipl-win.exe console.

:- if((\+current_predicate(rl_read_history/1),
       current_predicate('$rl_history'/1))).
:- use_module(library(readutil)).

system:rl_read_history(File) :-
	access_file(File, read), !,
	setup_call_cleanup(
	    open(File, read, In, [encoding(utf8)]),
	    read_history(In),
	    close(In)).
system:rl_read_history(_).

read_history(In) :-
	repeat,
	read_line_to_codes(In, Codes),
	(   Codes == end_of_file
	->  !
	;   atom_codes(Line, Codes),
	    rl_add_history(Line),
	    fail
	).

system:rl_write_history(File) :-
	'$rl_history'(Lines),
	(   Lines \== []
	->  setup_call_cleanup(
		open(File, write, Out, [encoding(utf8)]),
		forall(member(Line, Lines),
		       format(Out, '~w~n', [Line])),
		close(Out))
	;   true
	).

:- endif.

:- if(current_predicate(rl_write_history/1)).
write_history(File) :-
	current_prolog_flag(save_history, true), !,
	catch(rl_write_history(File), _, true).
:- endif.
write_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.

:- if(current_predicate(rl_read_history/1)).
:- dynamic
	history_loaded/1.

load_dir_history(File) :-
	(   exists_file(File)
	->  rl_read_history(File),
	    assertz(history_loaded(File))
	;   true
	).

prolog_history(enable) :-
	history_loaded(_), !.
prolog_history(enable) :-
	catch(dir_history_file('.', File), E,
	      (print_message(warning, E),fail)), !,
	catch(load_dir_history(File), E,
	      print_message(warning, E)),
	at_halt(write_history(File)),
	set_prolog_flag(save_history, true).
:- endif.
prolog_history(_) :-
	set_prolog_flag(save_history, false).