]> git.eshelyaron.com Git - dict.git/commitdiff
ADDED: sweep-load-buffer
authorEshel Yaron <me@eshelyaron.com>
Sun, 11 Sep 2022 15:43:24 +0000 (18:43 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sun, 11 Sep 2022 15:43:24 +0000 (18:43 +0300)
sweep.el
sweep.pl

index 9dcc1166db4bd74ae57129d4536137946351d35d..ac15758d8f5a216c4a1901cc57809d8c5f4d1c96 100644 (file)
--- a/sweep.el
+++ b/sweep.el
@@ -867,6 +867,20 @@ module name, F is a functor name and N is its arity."
           (sweep-close-query)
           sol)))))
 
+(defun sweep-load-buffer (&optional buffer)
+  (interactive)
+  (with-current-buffer (or buffer (current-buffer))
+    (let* ((beg (point-min))
+           (end (point-max))
+           (contents (buffer-substring-no-properties beg end)))
+      (sweep-open-query "user"
+                        "sweep"
+                        "sweep_load_buffer"
+                        (cons contents (buffer-file-name)))
+      (let ((sol (sweep-next-solution)))
+        (sweep-close-query)
+        sol))))
+
 ;;;###autoload
 (defun sweep-top-level (&optional buffer)
   "Run a Prolog top-level in BUFFER.
index 3313d8cc13e0fe7e4c25bacc989daba4a97b3a52..0f8f06d0fdad7e67294cbefc123cb21160aa1d8f 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -40,6 +40,7 @@
             sweep_identifier_at_point/2,
             sweep_expand_file_name/2,
             sweep_path_module/2,
+            sweep_load_buffer/2,
             sweep_colourise_query/2,
             sweep_predicate_references/2,
             sweep_predicate_location/2,
@@ -66,6 +67,8 @@
 :- use_module(library(help)).
 :- use_module(library(prolog_server)).
 
+:- meta_predicate with_buffer_stream(-, +, 0).
+
 :- dynamic sweep_current_color/3,
            sweep_open/2,
            sweep_source_time/2,
@@ -633,3 +636,24 @@ should_handle_message_kind(error, "error").
 should_handle_message_kind(warning, "warning").
 should_handle_message_kind(informational, "informational").
 should_handle_message_kind(debug(Topic0), ["debug"|Topic]) :- atom_string(Topic0, Topic).
+
+sweep_load_buffer([String|Path0], Result) :-
+    atom_string(Path, Path0),
+    with_buffer_stream(Stream,
+                       String,
+                       sweep_load_buffer_(Stream, Path, Result)).
+
+sweep_load_buffer_(Stream, Path, []) :-
+    set_stream(Stream, file_name(Path)),
+    load_files(Path, [stream(Stream)]).
+
+with_buffer_stream(Stream, String, Goal) :-
+    setup_call_cleanup(( new_memory_file(H),
+                         insert_memory_file(H, 0, String),
+                         open_memory_file(H, read, Stream, [encoding(utf8)]),
+                         set_stream(Stream, encoding(utf8))
+                       ),
+                       Goal,
+                       ( close(Stream),
+                         free_memory_file(H)
+                       )).