From 887f1fbd61a6628d81c12deeb5aaf02355d7f145 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Thu, 14 Sep 2023 18:28:16 +0200 Subject: [PATCH] Allow communicating with top-level via tty instead of TCP --- sweep.pl | 13 ++++++++++++- sweeprolog.el | 31 ++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/sweep.pl b/sweep.pl index d9d10e9..efffccb 100644 --- a/sweep.pl +++ b/sweep.pl @@ -95,7 +95,8 @@ sweep_functors_collection/2, sweep_compound_functors_collection/2, sweep_term_variable_names/2, - sweep_goal_may_cut/2 + sweep_goal_may_cut/2, + sweep_top_level_start_pty/2 ]). :- use_module(library(pldoc)). @@ -772,6 +773,16 @@ write_sweep_module_location :- format('M ~w~n', Path). :- endif. +sweep_top_level_start_pty([Name|Buffer], _) :- + thread_create(sweep_top_level_pty_client(Name), T, [detached(true)]), + thread_property(T, id(Id)), + asserta(sweep_top_level_thread_buffer(Id, Buffer)). + +sweep_top_level_pty_client(Name) :- + open(Name, read, InStream, [eof_action(reset)]), + open(Name, write, OutStream), + sweep_top_level_client(InStream, OutStream, ip(127,0,0,1)). + sweep_top_level_server(_, Port) :- tcp_socket(ServerSocket), tcp_setopt(ServerSocket, reuseaddr), diff --git a/sweeprolog.el b/sweeprolog.el index 3941d81..1c6d172 100644 --- a/sweeprolog.el +++ b/sweeprolog.el @@ -3131,6 +3131,12 @@ function with PROC and MSG." #'sweeprolog-top-level-sentinel) (add-hook 'kill-buffer-hook #'comint-write-input-ring nil t))) +(defcustom sweeprolog-top-level-use-pty + (not (memq system-type '(ms-dos windows-nt))) + "Whether to communicate with top-levels using pseudo-terminal (\"pty\"). + +By default, this is t on systems where Emacs can use a pty.") + (defun sweeprolog-top-level-buffer (&optional name) "Return a Prolog top-level buffer named NAME. @@ -3138,20 +3144,27 @@ If NAME is nil, use the default name \"*sweeprolog-top-level*\". If the buffer already exists, ensure it is associated with a live top-level." - (unless sweeprolog-prolog-server-port - (sweeprolog-start-prolog-server)) (let ((buf (get-buffer-create (or name "*sweeprolog-top-level*")))) (unless (process-live-p (get-buffer-process buf)) (with-current-buffer buf (unless (derived-mode-p 'sweeprolog-top-level-mode) (sweeprolog-top-level-mode))) - (unless (sweeprolog--query-once "sweep" "sweep_accept_top_level_client" - (buffer-name buf)) - (error "Failed to create new top-level!")) - (make-comint-in-buffer "sweeprolog-top-level" - buf - (cons "localhost" - sweeprolog-prolog-server-port)) + (if sweeprolog-top-level-use-pty + (progn + (make-comint-in-buffer "sweeprolog-top-level" buf nil) + (process-send-eof (get-buffer-process buf)) + (sweeprolog--query-once "sweep" "sweep_top_level_start_pty" + (cons (process-tty-name + (get-buffer-process buf)) + (buffer-name buf)))) + (unless sweeprolog-prolog-server-port + (sweeprolog-start-prolog-server)) + (sweeprolog--query-once "sweep" "sweep_accept_top_level_client" + (buffer-name buf)) + (make-comint-in-buffer "sweeprolog-top-level" + buf + (cons "localhost" + sweeprolog-prolog-server-port))) (unless comint-last-prompt (accept-process-output (get-buffer-process buf) 1)) (sweeprolog-top-level-setup-history buf) -- 2.39.2