%:- use_module(library(prolog_autoload)).
%:- use_module(library(prolog_pack)).
%:- user:[library('theme/auto.pl')].
%:- multifile(user:file_search_path/2).
%:-   dynamic(user:file_search_path/2).
%user:file_search_path(library,'/opt/logicmoo_workspace/packs_sys/logicmoo_utils/prolog/legacy/').
%user:file_search_path(library,'/opt/logicmoo_workspace/packs_sys/logicmoo_utils/prolog/logicmoo').
%user:file_search_path(library,'/opt/logicmoo_workspace/packs_sys/logicmoo_utils/prolog/debuggery').
/*
:- if( \+ exists_source(library(sldnfdraw))).
%:- attach_packs('/opt/logicmoo_workspace/packs_lib').
:- endif.
:- if( \+ exists_source(library(pfc))).
%:- attach_packs('/opt/logicmoo_workspace/packs_sys').
:- endif.
:- if( \+ exists_source(library(lps_syntax))).
%:- attach_packs('/opt/logicmoo_workspace/packs_web').
:- endif.
*/



/*
:- if(current_prolog_flag(debug,false)).
:- set_prolog_flag(verbose_autoload,false).
:- set_prolog_flag(verbose,silent).
:- set_prolog_flag(verbose_load,silent).
:- endif.
*/

:- '$hide'('$toplevel':restore_debug/0).
:- '$hide'('$toplevel':save_debug/0).
:- '$hide'('$toplevel':residue_vars/2).
:- '$hide'('system':deterministic/1).
:- '$hide'(toplevel_call/2).
:- '$hide'('$toplevel':'$query_loop'/0).
  %[5] [$toplevel] '$execute_goal2'(user:trace, [])
  %[3] [$toplevel] '$query_loop'
  %[2] [$toplevel] '$runtoplevel'

:- use_module(library(prolog_pack)).
%:- if( \+ prolog_pack:current_pack(logicmoo_base)).
:- multifile(user:file_search_path/2).
:-   dynamic(user:file_search_path/2).

dir_from_rc(Rel,Y):-
    ((getenv('LOGICMOO_WS',Dir);
     prolog_load_context(directory,Dir);
     '~/logicmoo_workspace'=Dir;
     '/home/dmiles/logicmoo_workspace'=Dir)),
    absolute_file_name(Rel,Y,[relative_to(Dir),file_type(directory),file_errors(fail)]),
    exists_directory(Y),!.

add_pack_path_rc(Rel):- 
   dir_from_rc(Rel,Y),
   (( \+ user:file_search_path(pack,Y)) ->asserta(user:file_search_path(pack,Y));true).

:- add_pack_path_rc(packs_sys).
:- add_pack_path_rc(packs_usr).
:- add_pack_path_rc(packs_web).
:- add_pack_path_rc(packs_xtra).

:- initialization(attach_packs,now).

% :- break.
/*
:- if(current_prolog_flag(debug,false)).
:- set_prolog_flag(verbose_autoload,false).
:- set_prolog_flag(verbose,silent).
:- set_prolog_flag(verbose_load,silent).
:- endif.
*/

:- if(current_prolog_flag(os_argv,[_])).
% :- pack_list_installed.
:- endif.

:- if(current_prolog_flag(windows, false)).
:- if(exists_source(library(readline))).
 :- use_module(library(readline)).
:- else.
 :- if(exists_source(library(editline))).
  :- use_module(library(editline)).
 :- endif.
:- endif.
setup_hist0:-  '$toplevel':setup_history.
:- setup_hist0.
:- endif.



:- set_prolog_flag(access_level,system).
:- load_files(library(prolog_stack)).
prolog_stack:stack_guard(none).

/*
:- use_module(library(base32)).
:- set_prolog_flag(report_error,true).
:- set_prolog_flag(fileerrors,false).
:- set_prolog_flag(debug_on_error,true).
:- set_prolog_flag(debug,true).
:- set_prolog_flag(optimise,false).
:- set_prolog_flag(last_call_optimisation,false).
:- debug.
:- nodebug(_).
:- Six = 6, set_prolog_stack(global, limit(Six*10**9)),set_prolog_stack(local, limit(Six*10**9)),set_prolog_stack(trail, limit(Six*10**9)).
:- set_prolog_flag(gc,true).
:- set_prolog_flag(gc,false).

% end_of_file.

:- use_module(library(prolog_history)).
:- '$toplevel':'$clean_history'.
:- '$toplevel':setup_history.

% :- prolog_ide(debug_monitor).


:- use_module(library(check)).
*/

/*  -*- Prolog -*-

    SWI-Prolog personalization file
*/

% :- multifile prolog_xref:xref_source_file/3.

:- set_prolog_flag(access_level,user).

% :- set_prolog_flag(scce,pure).
% :- set_prolog_flag(scce,scce_orig).
:- set_prolog_flag(scce,setup_call_cleanup).



% :- set_prolog_flag(access_level,system).
% :- set_prolog_flag(gc,false).
nopt
 :- (initialization((set_prolog_flag(optimise,false), 
         set_prolog_flag(last_call_optimisation,false))),
  use_module(library(pce)),
  use_module(library(prolog_xref)),
  use_module(library(pldoc/doc_access)),
  assert((doc_access:can_edit(X):-nonvar(X))),
  assert((doc_access:allow_from(X):-nonvar(X)))).

ds:- doc_server(3022,[allow(ip(_,_,_,_)),can_edit(true),workers(4)]).


%:- user:use_module(library(lists)).

sys :- set_prolog_flag(access_level,system).
usys :- set_prolog_flag(access_level,user).

mw:- use_module(library(prolog_modewalk)).

mp:-sys, meta_predicate(system: write(7)), meta_predicate(system: writeq(7)), meta_predicate(system:is(7,7)),meta_predicate(system: =:=(7,7)),usys.
lm :- use_module(library(logicmoo_utils)).
lmb :- use_module(library(logicmoo_base)).
lmu :- use_module(library(logicmoo_user)).


sf:- forall(source_file(SF), call(call,load_all(SF))).
dl:-  use_module(library(pldoc/doc_library)),call(call,doc_load_library).



%:-  use_module(library(make)).
%:-  use_module(library(check)).



:- set_prolog_flag(access_level,user).

:- dynamic(user:type_modes/3).

% :- check.

% :- include(yyy).

sm:- 
   current_module(M),
   catch(M:'$mode'(Head, Det),_,fail),
   \+ user:type_modes(M,Head,Det),
   asserta(user:type_modes(M,Head,Det)),fail.

sm:- 
   current_module(M),catch(M:'$pldoc'(F/A, _,_,Sum),_,fail), functor(Head,F,A),
   \+ user:type_modes(M,Head,_Det),\+ user:type_modes(M,_:Head,_Det2),
   format('~q.~n',[parse_modes(M:Head, Sum)]),fail.


sm:- 
   current_module(M),catch(M:'$pldoc'(F//Am2, _,_,Sum),_,fail), A is Am2+2, functor(Head,F,A),
   \+ user:type_modes(M,Head,_Det),\+ user:type_modes(M,_:Head,_Det2),
   format('~q.~n',[parse_modes(M:Head, Sum)]),fail.

:- multifile(check:predicate_name/2).
check:predicate_name(A, D):- prolog_clause:predicate_name(A, D).


:- attach_packs.

:- use_module(library(logicmoo_common)).
%:- attach_packs.

%end_of_file.

gm:- current_module(M), gm(M),fail.
gm.

gm(M):- 
   catch(M:'$pldoc'(F/A, Loc,Text,_),_,fail), 
   format('~q.~n',[M:'$pldoc'(F/A, Loc,Text)]),
   functor(Head,F,A), 
   catch(M:'$mode'(Head,Det),_,fail),
   format('~q.~n',[M:'$mode'(Head, Det)]),
   fail.
   
gm(M):- catch(M:'$mode'(Head,Det),_,fail),
   functor(Head,F,A),
  \+ catch(M:'$pldoc'(F/A, _Loc, _Text,_),_,fail), 
   format('~q.~n',[M:'$mode'(Head, Det)]),
   fail.

gm(_).

%:- gxref.
% current_module(M),catch(M:'$pldoc'(F//A, Det,O,I),_,fail),format('~q.~n',[M:'$pldoc'(F//A, Det,I)]) 
% ,fail.
% current_module(M),catch(M:'$mode'(Head, Det),_,fail),format('~q.~n',[M:'$mode'(Head, Det)]),fail.


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is a sample user-initialisation file for SWI-Prolog. If you wish to
customise prolog, make  a  copy  of  this   file  and  edit  it  to your
preferences.

Installation:

	Unix/MacOS:	~/.swiplrc
	Windows:	<appdata>/swipl.ini (see win_folder(appdata, AppData))

More hints on useful things you  can  put   into  this  file  are in the
SWI-Prolog reference manual. Notably look   at debugger settings, editor
hooks, file_search_path/2, set_prolog_flag/2 and portray/1.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

		 /*******************************
		 *	       IDE		*
		 *******************************/

%	By default, xpce (the  graphics  library)   runs  in  a seperate
%	thread. This allows editing and inspecting your program while it
%	is running. All components of the Prolog development environment
%	are aware of this. If you use   your own xpce based applications
%	and if you develop using xpce, it  might be wise to disable this
%	feature and keep xpce in the thread `main`.

% :- set_prolog_flag(xpce_threaded, false).


		 /*******************************
		 *	      EDITOR		*
		 *******************************/

%	Define the editor to use.  Note that more advanced manipulation
%	of this is defined in the SWI-Prolog reference manual, section
%	"Listing and Editor Interface"
%
%	The value pce_emacs (or built_in) causes the system to use the
%	built-in editor PceEmacs if the environment provides for a GUI.
%	pce_emacs is the default if XPCE is available.
%
%	The second entry defines an arbitrary editor and how to tell
%	SWI-Prolog to open a file with it on a specified line-number.

% :- set_prolog_flag(editor, pce_emacs).
% :- set_prolog_flag(editor, pico).

%:- multifile
%	prolog_edit:edit_command/2.
%
%prolog_edit:edit_command(pico, '%e +%d "%f"').
%prolog_edit:edit_command(pico, '%e "%f"').


		 /*******************************
		 *	      DEBUGGING		*
		 *******************************/

%	If you prefer graphical tracing, add the line below.

% :- (current_prolog_flag(gui, true) -> guitracer ; true).

%	Determine how terms are printed by the debugger and toplevel.  The
%	values here are defaults. max_depth(10) replaces all subterms at
%	a greater depth with elipses (...). See write_term/3 for further
%	explanation and more options.

% :- set_prolog_flag(answer_write_options,
%		     [quoted(true), portray(true), max_depth(10)]).
% :- set_prolog_flag(debugger_write_options,
%		     [quoted(true), portray(true), max_depth(10)]).

%	If you want to suppress printing toplevel query variables
%	starting with an `_'

% :- set_prolog_flag(toplevel_print_anon, false).
:- set_prolog_flag(toplevel_print_anon, true).

%	If you do not want the tracer to stop at at the exit port.

% :- leash(-exit).

%	Uncomment the two lines below  to   include  a  backtrace of the
%	stack on an uncaught exception.  This   makes  the system rather
%	noisy on exceptions, but often allows   you to discover why your
%	program misbehaves without using the debugger.

:- load_files(library(prolog_stack)).
prolog_stack:stack_guard(none).

		 /*******************************
		 *	 CONSOLE FEEDBACK	*
		 *******************************/

%	Use the flag below to  disable   coloured  output  in all cases.
%	Normally,  coloured  output  is  enabled  if  the  output  is  a
%	terminal.

% :- set_prolog_flag(color_term, false).

%	Specify colors for the above,  based   on  the  message kind See
%	ansi_format/3 for specifying visual  effects.   The  table below
%	duplicates the default behavior.  Notably   on  terminals with a
%	dark background, yellow might be a   better  choice for warnings
%	and errors.

%:- multifile user:message_property/2.
%
%user:message_property(informational, color(fg(green))).
%user:message_property(information,   color(fg(green))).
user:message_property(debug,         color(fg(blue),bold)).
%user:message_property(warning,       color(fg(red))).
%user:message_property(error,         color([fg(red),bold])).

%	Specify feedback for loading files.  Values are `full` (feedback
%	at start and end of each  file),   `normal`  (feedback at end of
%	each file), `brief` (feedback  at  end   of  toplevel  file) and
%	`silent` (no feedback).

% :- set_prolog_flag(verbose_load, silent).
% :- set_prolog_flag(verbose_autoload, true).




doc_load_library :-
	set_prolog_flag(verbose_load, false),
	absolute_file_name(swi(library), Dir, [file_type(directory)]),
	load_all(Dir).


load_all([]) :- !.
load_all([H|T]) :-
	load_all(H), !,
	load_all(T).
load_all(Dir0) :-
	atom(Dir0),
	expand_file_name(Dir0, [Dir1]),
	downcase_atom(Dir1, Dir),	% Deal with Windows
	\+ ( blocked(Blocked),
	     sub_atom(Dir, _, _, 0, Blocked)
	   ),
	exists_directory(Dir), !,
	atom_concat(Dir, '/*', Pattern),
	expand_file_name(Pattern, Contents),
	load_all(Contents).
load_all(File) :-
	atom(File),
	file_name_extension(_, pl, File),
	downcase_atom(File, LwrCase),
	\+ ( blocked(Blocked),
	     sub_atom(LwrCase, _, _, 0, Blocked)
	   ), !,
	use_module(File, []).
load_all(Spec) :-
	compound(Spec), !,
	forall(absolute_file_name(Spec, Path,
				  [ access(read),
				    file_errors(fail)
				  ]),
	       load_all(Path)).
load_all(_).

%%	blocked(+Path) is semidet.
%
%	True if file or directory should not   be loaded. Note that file
%	from the directory chr are  already   loaded  by chr.pl. Similar
%	arguments apply for a few others.
%
%	@bug	We force lowercase to make it also work on Windows

blocked('/chr').
blocked('/clpq').
blocked('/clpr').
blocked('/pldoc').
blocked('/ciao').
blocked('/checkselect.pl').
blocked('/checklast.pl').
blocked('/clp/clp_distinct.pl').	% deprecated file
%blocked('/jpl.pl').			% should be added
blocked('/pldoc.pl').
blocked('/index.pl').

blocked('/ciao.pl').			% is an include-file.  We must
					% find a more general solution here
blocked('/commons.pl').
blocked('/swipl-lfr.pl').
blocked('/dcg_basics.pl').		% deprecated file


blocked('/mkindex.pl').
blocked('/metaterm.pl').
blocked('/atts.pl').




:- prolog_debug('MSG_CALL').
:- prolog_debug('MSG_TRACE').
:- prolog_debug('MSG_VMI').

% sudo apt-get install libmysqlclient-dev unixodbc-dev