This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap

92 lines
2.2 KiB
Plaintext
Raw Normal View History

2017-08-21 12:36:48 +01:00
:- use_module(library(yapi)).
2017-08-27 22:27:51 +01:00
:- use_module(library(lists)).
:- use_module(library(maplist)).
2017-08-21 12:36:48 +01:00
:- use_module(library(python)).
:- python_import(sys).
jupyter_query(Self, Cell) :-
setup_call_cleanup(
enter_cell(Self),
python_query(Self, Cell),
exit_cell(Self)
).
enter_cell(_Self) :-
open('//python/sys.stdout', append, _Output, [alias(jupo)]),
open('//python/sys.stdout', append, _, [alias(jupe)]),
set_prolog_flag(user_output, jupo),
set_prolog_flag(user_error, jupe).
exit_cell(_Self) :-
close( jupo),
close( jupe).
completions(S, Self) :-
2017-08-27 22:27:51 +01:00
open_mem_read_stream(S, St),
2017-08-21 12:36:48 +01:00
scan_to_list(St, Tokens),
2017-08-27 22:27:51 +01:00
close(St),
2017-08-21 12:36:48 +01:00
reverse(Tokens, RTokens),
2017-08-27 22:27:51 +01:00
strip_final_tokens(RTokens, MyTokens),
setof( Completion, complete(MyTokens, Completion), Cs),
2017-08-21 12:36:48 +01:00
Self.completions := Cs.
2017-08-27 22:27:51 +01:00
strip_final_tokens(['EOT'|Ts], Ts) :- !.
strip_final_tokens( Ts, Ts ).
2017-08-21 12:36:48 +01:00
complete( [atom(F)|LibRest], C) :-
LibRest = [l, atom(Where)|Consult],
isconsult(Consult, Rest),
\+ arg( Rest ),
check_library( F, Where, C).
complete( [atom(F)|Consult], C) :-
isconsult(Consult, Rest),
\+ arg( Rest ),
check_file( F, C).
complete( [atom(F)|Rest], C) :-
\+ arg( Rest ),
predicate( F, Pred, Arity ),
2017-08-27 22:27:51 +01:00
cont( Arity, F, Pred, C).
2017-08-21 12:36:48 +01:00
2017-08-27 22:27:51 +01:00
isconsult( [l, use_module| Rest], Rest).
isconsult( [l, ensure_loaded| Rest], Rest).
isconsult( [l, compile| Rest], Rest).
isconsult( [l, consult| Rest], Rest).
isconsult( [l, reconsult| Rest], Rest).
isconsult( [l, load_files| Rest], Rest).
isconsult( ['-', ']'| Rest], Rest).
isconsult( [']'| Rest], Rest ).
2017-08-21 12:36:48 +01:00
2017-08-27 22:27:51 +01:00
arg([']'|_]).
arg([l|_]).
2017-08-21 12:36:48 +01:00
check_file(F,C) :-
atom_concat( F, '*' , Pat),
absolute_file_name( Pat, C0, [glob(true)] ),
atom_concat(['\'',C0,'\''], C).
check_library( Lib, F, C) :-
atom_concat( F, '*' , Pat),
LibF =.. [Lib(Pat)],
absolute_file_name( LibF, Lib, [glob(true)] ),
file_directory_name( Lib, Name),
( atom_concat(C, '.yap', Name) -> true ;
atom_concat(C, '.ypp', Name) -> true ;
atom_concat(C, '.prolog', Name) -> true
).
predicate(N,P,A) :-
system_predicate(P0/A),
atom_concat(N,P,P0).
predicate(N,P,A) :-
current_predicate(P0/A),
atom_concat(N,P,P0).
2017-08-27 22:27:51 +01:00
cont(0, F, P, P0)- :-
atom_concat( F, P, PB ).
cont( _, F, P, PB ):-
atom_concat( [F, P, '('], PB ).