<commit_before>/*  $Id$

    Designed and implemented <NAME>
    E-mail: <EMAIL>

    Copyright (C) 1997 University of Amsterdam. All rights reserved.
*/

:- module(prolog_navigator,
	  [ 
	  ]).
:- use_module(library(pce)).
:- use_module(library(toc_filesystem)).
:- use_module(library(pce_report)).
:- use_module(library(toolbar)).
:- use_module(library(helpidx)).
:- use_module(library('trace/util')).
:- use_module(browse_xref).
:- require([ start_emacs/0
	   , '$qlf_info'/4
	   , append/3
	   , concat/3
	   , concat_atom/2
	   , concat_atom/3
	   , file_name_extension/3
	   , flatten/2
	   , gensym/2
	   , member/2
	   , once/1
	   , pce_image_directory/1
	   ]).

:- pce_autoload(prolog_debug_status, library('trace/status')).
:- pce_image_directory(library('trace/icons')).

:- dynamic
	prolog_overview_window/1.

resource(edit,	      image, image('16x16/edit.xpm')).
resource(up,	      image, image('16x16/up.xpm')).
resource(refresh,     image, image('16x16/refresh.xpm')).
resource(butterfly,   image, image('butterfly.xpm')).
resource(dbgsettings, image, image('16x16/dbgsettings.xpm')).

:- pce_begin_class(prolog_navigator, frame,
		   "Prolog source navigator").

initialise(SB, Root:directory) :->
	send_super(SB, initialise, 'Prolog Navigator'),
	send(SB, icon, resource(butterfly)),
	send(SB, append, new(D, dialog)),
	send(new(W, prolog_source_structure(Root)), below, D),
	send(D, append, new(tool_bar(W))),
	send(D, gap, size(0, 2)),
	send(D, pen, 0),
	send(SB, fill_tool_bar),
	send(new(report_dialog), below, W).

tool_bar(SB, TB:tool_bar) :<-
	"Get the toolbar"::
	get(SB, member, dialog, D),
	get(D, member, tool_bar, TB).

fill_tool_bar(SB) :->
	"Fill the toolbar"::
	get(SB, tool_bar, TB),
	send_list(TB, append,
		  [ tool_button(up,
				resource(up),
				'Up one level'),
		    tool_button(refresh,
				resource(refresh),
				'Update view'),
		    gap,
		    tool_button(debug_settings,
				resource(dbgsettings),
				'Edit breakpoints'),
		    tool_button(edit,
				resource(edit),
				'Open file in editor')
		  ]).

goto(SB, File:file, Line:int) :->
	"Expand and highlight tree for given location"::
	get(SB, member, prolog_source_structure, FB),
	send(FB, goto, File, Line).

:- pce_end_class(prolog_navigator).


:- pce_begin_class(prolog_source_structure, toc_filesystem,
		   "Browser for (prolog) source-files").

class_variable(auto_refresh, int*, @nil).
class_variable(size,	size,	size(200, 500),
	       "Intial window size").

variable(file_pattern,	regex := '.*\.pl$', get, "Pattern of showed files").

initialise(FB, Root:directory) :->
	send_super(FB, initialise, Root),
	send(FB?frame, label, 'SWI-Prolog Navigator'),
	asserta(prolog_overview_window(FB)).

unlink(FB) :->
	retractall(prolog_overview_window(FB)),
	send_super(FB, unlink).

make_file_node(_FB, File:file, Node:sb_prolog_file) :<-
	"Return a Prolog source-file node"::
	new(Node, sb_prolog_file(File)).

:- pce_group(navigate).

file_node(FB, File:name, Create:[bool], Node:toc_node) :<-
	"Get node for file, possibly add it to the tree"::
	canonical_source_file(File, Path),
	(   get(FB, node, Path, Node)
	->  true
	;   Create == @on
	->  file_directory_name(Path, Dir),
	    get(FB, dir_node, Dir, @on, DirNode),
	    send(DirNode, collapsed, @off),
	    get(FB, node, Path, Node)
	).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
->goto: File, Line opens  the  tree   such  that  the indicated position
becomes visible and selects the entity   holding the specified location.
First it looks for the file, then it  assumes the sons are in file-order
and looks for the first son after the requested line. After finding this
it iterates on this.

This method is intended to synchronise with an editor.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

goto(FB, File:'file|node', Line:int) :->
	"Show indicated position"::
	(   send(File, instance_of, file)
	->  get(FB, file_node, File?name, @on, Node)
	;   Node = File
	),
	(   (   get(Node, collapsed, @nil)
	    ;	send(Node, instance_of, sb_predicate)
	    )
	->  send(FB?tree, selection, Node),
	    send(FB, normalise, Node)
	;   send(Node, collapsed, @off),
	    new(N, var),
	    (	get(Node?sons, find,
		    and(message(@arg1, has_get_method, line),
			if(and(@arg1?line \== @nil,
			       @arg1?line > Line),
			   new(and),
			   and(assign(N, @arg1, global),
			       new(or)))),
		    _)
	    ->	(   get(N, '_value', @nil)
		->  send(FB?tree, selection, Node),
		    send(FB, normalise, Node)
		;   send(FB, goto, N, Line)
		)
	    ;	get(Node?sons, tail, Last),
		send(FB, goto, Last, Line)
	    )
	).

:- pce_group(popup).

popup(FB, Id:any, Popup:popup) :<-
	"Return popup from current node"::
	get(FB, node, Id, Node),
	send(Node, has_get_method, popup),
	get(Node, popup, Popup).

:- pce_group(edit).

edit(FB) :->
	"Edit selected file"::
	(   get(FB, selection, Sel),
	    get(Sel, head, FileNode),
	    send(FileNode, instance_of, sb_prolog_file),
	    get(FileNode, identifier, File),
	    start_emacs,
	    send(@emacs, open_file, File)
	;   send(FB, report, warning, 'No selected file'),
	    fail
	).

debug_settings(FB) :-><commit_msg>* Fixed type-error<commit_after>/*  $Id$

    Designed and implemented <NAME>
    E-mail: <EMAIL>

    Copyright (C) 1997 University of Amsterdam. All rights reserved.
*/

:- module(prolog_navigator,
	  [ 
	  ]).
:- use_module(library(pce)).
:- use_module(library(toc_filesystem)).
:- use_module(library(pce_report)).
:- use_module(library(toolbar)).
:- use_module(library(helpidx)).
:- use_module(library('trace/util')).
:- use_module(browse_xref).
:- require([ start_emacs/0
	   , '$qlf_info'/4
	   , append/3
	   , concat/3
	   , concat_atom/2
	   , concat_atom/3
	   , file_name_extension/3
	   , flatten/2
	   , gensym/2
	   , member/2
	   , once/1
	   , pce_image_directory/1
	   ]).

:- pce_autoload(prolog_debug_status, library('trace/status')).
:- pce_image_directory(library('trace/icons')).

:- dynamic
	prolog_overview_window/1.

resource(edit,	      image, image('16x16/edit.xpm')).
resource(up,	      image, image('16x16/up.xpm')).
resource(refresh,     image, image('16x16/refresh.xpm')).
resource(butterfly,   image, image('butterfly.xpm')).
resource(dbgsettings, image, image('16x16/dbgsettings.xpm')).

:- pce_begin_class(prolog_navigator, frame,
		   "Prolog source navigator").

initialise(SB, Root:directory) :->
	send_super(SB, initialise, 'Prolog Navigator'),
	send(SB, icon, resource(butterfly)),
	send(SB, append, new(D, dialog)),
	send(new(W, prolog_source_structure(Root)), below, D),
	send(D, append, new(tool_bar(W))),
	send(D, gap, size(0, 2)),
	send(D, pen, 0),
	send(SB, fill_tool_bar),
	send(new(report_dialog), below, W).

tool_bar(SB, TB:tool_bar) :<-
	"Get the toolbar"::
	get(SB, member, dialog, D),
	get(D, member, tool_bar, TB).

fill_tool_bar(SB) :->
	"Fill the toolbar"::
	get(SB, tool_bar, TB),
	send_list(TB, append,
		  [ tool_button(up,
				resource(up),
				'Up one level'),
		    tool_button(refresh,
				resource(refresh),
				'Update view'),
		    gap,
		    tool_button(debug_settings,
				resource(dbgsettings),
				'Edit breakpoints'),
		    tool_button(edit,
				resource(edit),
				'Open file in editor')
		  ]).

goto(SB, File:file, Line:int) :->
	"Expand and highlight tree for given location"::
	get(SB, member, prolog_source_structure, FB),
	send(FB, goto, File, Line).

:- pce_end_class(prolog_navigator).


:- pce_begin_class(prolog_source_structure, toc_filesystem,
		   "Browser for (prolog) source-files").

class_variable(auto_refresh, int*, @nil).
class_variable(size,	size,	size(200, 500),
	       "Intial window size").

variable(file_pattern,	regex := '.*\.pl$', get, "Pattern of showed files").

initialise(FB, Root:directory) :->
	send_super(FB, initialise, Root),
	send(FB?frame, label, 'SWI-Prolog Navigator'),
	asserta(prolog_overview_window(FB)).

unlink(FB) :->
	retractall(prolog_overview_window(FB)),
	send_super(FB, unlink).

make_file_node(_FB, File:file, Node:sb_prolog_file) :<-
	"Return a Prolog source-file node"::
	new(Node, sb_prolog_file(File)).

:- pce_group(navigate).

file_node(FB, File:name, Create:[bool], Node:toc_node) :<-
	"Get node for file, possibly add it to the tree"::
	canonical_source_file(File, Path),
	(   get(FB, node, Path, Node)
	->  true
	;   Create == @on
	->  file_directory_name(Path, Dir),
	    get(FB, dir_node, Dir, @on, DirNode),
	    send(DirNode, collapsed, @off),
	    get(FB, node, Path, Node)
	).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
->goto: File, Line opens  the  tree   such  that  the indicated position
becomes visible and selects the entity   holding the specified location.
First it looks for the file, then it  assumes the sons are in file-order
and looks for the first son after the requested line. After finding this
it iterates on this.

This method is intended to synchronise with an editor.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

goto(FB, File:'file|node', Line:int) :->
	"Show indicated position"::
	(   send(File, instance_of, file)
	->  get(FB, file_node, File?name, @on, Node)
	;   Node = File
	),
	(   (   get(Node, collapsed, @nil)
	    ;	send(Node, instance_of, sb_predicate)
	    )
	->  send(FB?tree, selection, Node),
	    send(FB, normalise, Node)
	;   send(Node, collapsed, @off),
	    new(N, var(value := @nil)),
	    (	get(Node?sons, find,
		    and(message(@arg1, has_get_method, line),
			if(and(@arg1?line \== @nil,
			       @arg1?line > Line),
			   new(and),
			   and(assign(N, @arg1, global),
			       new(or)))),
		    _)
	    ->	get(N, '_value', N2),
	        (   N2 == @nil
		->  send(FB?tree, selection, Node),
		    send(FB, normalise, Node)
		;   send(FB, goto, N2, Line)
		)
	    ;	get(Node?sons, tail, Last),
		send(FB, goto, Last, Line)
	    )
	).

:- pce_group(popup).

popup(FB, Id:any, Popup:popup) :<-
	"Return popup from current node"::
	get(FB, node, Id, Node),
	send(Node, has_get_method, popup),
	get(Node, popup, Popup).

:- pce_group(edit).

edit(FB) :->
	"Edit selected file"::
	(   get(FB, selection, Sel),
	    get(Sel, head, FileNode),
	    send(FileNode, instance_of, sb_prolog_file),
	    get(FileNode, identifier, File),
	    start_emacs,
	    send(@emacs, open_file, File)
	;   send(FB, report, warning, 'No selected file'),
	    fail
	).

debug_settings(FB) :->
{'fraction_non_alphanumeric': 0.17201614453200076, 'fraction_numerical': 0.009129348452815683, 'mean_word_length': 3.325436408977556, 'pattern_counts': {'":': 16, '<': 15, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3211825', 'n_tokens_mistral': 4186, 'n_tokens_neox': 3901, 'n_words': 841}