summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-01-22 18:12:48 +0100
committerJohn Ankarström <john@ankarstrom.se>2022-01-22 18:12:48 +0100
commit594c22ef699fa44ca65bcb6c1b9a0c9c848114f0 (patch)
tree8f8c5aaa4958a7fe916cecf2dec55a6fb6135ccb
downloadsave-594c22ef699fa44ca65bcb6c1b9a0c9c848114f0.tar.gz
A mk.bat
A pce_file_search_path.pl A save.c A save.exe A save.pl A save.pl~
-rw-r--r--mk.bat1
-rw-r--r--pce_file_search_path.pl2
-rw-r--r--save.c32
-rw-r--r--save.exebin0 -> 273511 bytes
-rw-r--r--save.pl85
-rw-r--r--save.pl~82
6 files changed, 202 insertions, 0 deletions
diff --git a/mk.bat b/mk.bat
new file mode 100644
index 0000000..b67a0ef
--- /dev/null
+++ b/mk.bat
@@ -0,0 +1 @@
+swipl-ld -v -ld ld -ld-options,--subsystem,windows -goal true -o save save.c save.pl \ No newline at end of file
diff --git a/pce_file_search_path.pl b/pce_file_search_path.pl
new file mode 100644
index 0000000..dc42ca1
--- /dev/null
+++ b/pce_file_search_path.pl
@@ -0,0 +1,2 @@
+file_search_path(library, pce('prolog/lib')).
+file_search_path(pce, 'C:/program files/swipl/xpce'). \ No newline at end of file
diff --git a/save.c b/save.c
new file mode 100644
index 0000000..faead35
--- /dev/null
+++ b/save.c
@@ -0,0 +1,32 @@
+#include <windows.h>
+#include <SWI-Prolog.h>
+
+INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
+ PSTR lpCmdLine, INT nCmdShow)
+{
+ char *plav[2];
+
+ /* make the argument vector for Prolog */
+
+ plav[0] = "save";
+ plav[1] = NULL;
+
+ /* initialise Prolog */
+
+ if (!PL_initialise(1, plav))
+ PL_halt(1);
+
+ /* Lookup calc/1 and make the arguments and call */
+
+ {
+ predicate_t pred = PL_predicate("main", 0, "user");
+ term_t h0 = PL_new_term_refs(0);
+ int rval;
+
+ rval = PL_call_predicate(NULL, PL_Q_NORMAL, pred, h0);
+
+ PL_halt(rval ? 0 : 1);
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/save.exe b/save.exe
new file mode 100644
index 0000000..40fe22f
--- /dev/null
+++ b/save.exe
Binary files differ
diff --git a/save.pl b/save.pl
new file mode 100644
index 0000000..b771bb4
--- /dev/null
+++ b/save.pl
@@ -0,0 +1,85 @@
+:- consult(pce_file_search_path).
+:- consult(library(pce)).
+:- consult(library(process)).
+
+main :-
+ pce_main_loop(main).
+
+main(_Argv) :-
+ init.
+
+init :-
+ shell("git add .", Status),
+ added(Status).
+
+added(1) :-
+ send(@display, report, error,
+ 'Files could not be added to index.
+Ensure Git is in PATH.').
+
+added(0) :-
+ %send(@pce, load_defaults, 'Defaults'),
+ new(D, dialog('Save')),
+
+ % File browser.
+ new(C, chain),
+ status(C),
+ send(new(B, browser), right, D),
+ send(B, members(C)),
+
+ % Input fields.
+ send(D, append, text('Commit message (optional):',
+ left,
+ bold)),
+ send(D, append, new(E, editor)),
+ send(D, append, button(save,
+ and(message(@prolog, save, E, C),
+ message(D, destroy)))),
+ send(D, append, button(cancel,
+ and(message(@prolog, reset),
+ message(D, destroy)))),
+ send(D, open).
+
+reset :-
+ shell("git reset").
+
+status(C) :-
+ setup_call_cleanup(
+ process_create(path(git), ['status', '--porcelain'],
+ [stdout(pipe(Out))]),
+ read_to_chain(Out, C),
+ close(Out)).
+
+read_to_chain(S, C) :-
+ read_line_to_codes(S, Cs),
+ ( Cs == end_of_file
+ -> true
+ ; atom_codes(A, Cs),
+ send(C, append, A),
+ read_to_chain(S, C)
+ ).
+
+save(Editor, Chain) :-
+ get(Editor?text_buffer, contents, S),
+ send(S, strip),
+ get(S, value, Msg),
+ ( Msg == ''
+ -> chain_atom(Chain, A),
+ commit(A)
+ ; commit(Msg)
+ ).
+
+chain_atom(C, A) :-
+ chain_atom_x(C, '', A).
+
+chain_atom_x(C, A0, A) :-
+ ( get(C, delete_head, A1),
+ atom_concat(A0, A1, A2),
+ atom_concat(A2, '
+', A3),
+ chain_atom_x(C, A3, A)
+ ; A = A0
+ ).
+
+commit(Msg) :-
+ process_create(path(git), ['commit', '-m', Msg], []).
diff --git a/save.pl~ b/save.pl~
new file mode 100644
index 0000000..1a720c4
--- /dev/null
+++ b/save.pl~
@@ -0,0 +1,82 @@
+:- consult(pce_file_search_path).
+:- consult(library(pce)).
+:- consult(library(process)).
+
+main :-
+ pce_main_loop(main).
+
+main(_Argv) :-
+ init.
+
+init :-
+ shell("git add .", Status),
+ added(Status).
+
+added(1) :-
+ send(@display, report, error,
+ 'Files could not be added to index.
+Ensure Git is in PATH.').
+
+added(0) :-
+ %send(@pce, load_defaults, 'Defaults'),
+ new(D, dialog('Save')),
+
+ % File browser.
+ new(C, chain),
+ status(C),
+ send(new(B, browser), right, D),
+ send(B, members(C)),
+
+ % Input fields.
+ send(D, append, text('Commit message (optional):',
+ left,
+ bold)),
+ send(D, append, new(E, editor)),
+ send(D, append, button(save,
+ and(message(@prolog, save, E, C),
+ message(D, destroy)))),
+ send(D, append, button(cancel,
+ and(message(@prolog, reset),
+ message(D, destroy)))),
+ send(D, open).
+
+reset :-
+ shell("git reset").
+
+status(C) :-
+ setup_call_cleanup(
+ process_create(path(git), ['status', '--porcelain'],
+ [stdout(pipe(Out))]),
+ read_to_chain(Out, C),
+ close(Out)).
+
+read_to_chain(S, C) :-
+ read_line_to_codes(S, Cs),
+ ( Cs == end_of_file
+ -> true
+ ; atom_codes(A, Cs),
+ send(C, append, A),
+ read_to_chain(S, C)
+ ).
+
+save(Editor, Chain) :-
+ get(Editor?text_buffer, contents, S),
+ send(S, strip),
+ get(S, value, Msg),
+ ( Msg == ''
+ -> chain_atom(Chain, A),
+ commit(A)
+ ; commit(Msg)
+ ).
+
+chain_atom(C, A) :-
+ chain_atom_x(C, '', A).
+
+chain_atom_x(C, A0, A) :-
+ ( get(C, delete_head, A1),
+ atom_concat(A0, A1, A2),
+ atom_concat(A2, '
+', A3),
+ chain_atom_x(C, A3, A)
+ ; A = A0
+ ).