]> git.eshelyaron.com Git - dict.git/commitdiff
ADDED: sweep-top-level command and appropriate mode
authorEshel Yaron <me@eshelyaron.com>
Mon, 29 Aug 2022 16:50:55 +0000 (19:50 +0300)
committerEshel Yaron <me@eshelyaron.com>
Mon, 29 Aug 2022 16:50:55 +0000 (19:50 +0300)
README.org
sweep.el
sweep.pl

index 021160bb9e3ec5f6b5c73cef2de1923ac39fe334..e6450c0d8ebdeb1b6bb8df5a56bf76421110fce3 100644 (file)
@@ -194,6 +194,21 @@ permutations of the list =(1 2 3 4 5)=:
     num)
 #+end_src
 
+
+* The Prolog top-level
+
+#+FINDEX: sweep-top-level
+=sweep= provides a classic Prolog top-level interface for interacting
+with the embedded Prolog runtime.  To start the top-level, use =M-x
+sweep-top-level=.  This open a dedicated buffer called
+=*sweep-top-level*= which hosts a live Prolog top-level.  If the
+top-level buffer already exists, =sweep-top-level= will reuse it.  To
+create another one or more top-level buffers, run the command =C-x x u=
+(or =M-x rename-uniquely=) in the buffer called =*sweep-top-level*= and
+then run =M-x sweep-top-level= again.  This will change the name of the
+original top-level buffer to something like =*sweep-top-level*<2>= and
+allow the new top-level to claim the buffer name =*sweep-top-level*=.
+
 * Finding Prolog code
 :PROPERTIES:
 :CUSTOM_ID: finding-prolog-code
index 0ba158916150d88d0ca06a970a8726537ee5ba6f..ba4e34240f1455b290a768845f64cb41d0dd0a80 100644 (file)
--- a/sweep.el
+++ b/sweep.el
@@ -26,6 +26,8 @@
 
 ;;; Code:
 
+(require 'comint)
+
 (defgroup sweep nil
   "SWI-Prolog Embedded in Emacs."
   :group 'prolog)
@@ -65,6 +67,9 @@
   :type '(list string)
   :group 'sweep)
 
+
+(defvar sweep-prolog-server-port 12345)
+
 ;;;###autoload
 (defun sweep-module-compile ()
   "Compile sweep-module."
           (require 'sweep-module))
       (error "Sweep will not work until `sweep-module' is compiled!"))))
 
+(defun sweep-start-prolog-server ()
+  (sweep-open-query "user"
+                    "sweep"
+                    "sweep_start_prolog_server"
+                    sweep-prolog-server-port)
+  (let ((sol (sweep-next-solution)))
+    (sweep-close-query)
+    sol))
+
 (defun sweep-init ()
+  (setq sweep-prolog-server-port (+ (random 10000)
+                                    sweep-prolog-server-port))
   (apply #'sweep-initialize
          (cons (expand-file-name "bin/swipl" (file-name-directory
                                               load-file-name))
-               (cons "-q" sweep-init-args))))
+               (cons "-q" sweep-init-args)))
+  (sweep-start-prolog-server))
 
 (defun sweep-predicates-collection ()
   (sweep-open-query "user" "sweep" "sweep_predicates_collection" nil)
@@ -208,14 +225,58 @@ module name, F is a functor name and N is its arity."
         (message "Package install successful.")
       (user-error "Pacakge installation failed"))))
 
-;;;; Testing:
+;; (defun sweep-file-handler (operation &rest args)
+;;   (cond ((eq operation 'expand-file-name) (apply sweep-expand-file-name args) )
+;;         ;; ((eq operation 'file-name-all-completions))
+;;         ;; ((eq operation 'file-name-completion))
+;;         (t (let ((inhibit-file-name-handlers
+;;                   (cons 'my-file-handler
+;;                         (and (eq inhibit-file-name-operation operation)
+;;                              inhibit-file-name-handlers)))
+;;                  (inhibit-file-name-operation operation))
+;;              (apply operation args)))))
+
+;; (defun sweep-expand-file-name (name &optional dir)
+;;   (sweep-open-query "user" "sweep" "sweep_expand_file_name" (cons name dir))
+;;   (let ((sol (sweep-next-solution)))
+;;     (sweep-close-query)
+;;     (when (sweep-true-p sol)
+;;       (cdr sol))))
+
+;;;###autoload
+(defun sweep-top-level ()
+  "Start an interactive Prolog top-level."
+  (interactive)
+  (let ((buf (get-buffer-create "*sweep-top-level*")))
+    (with-current-buffer buf
+      (unless (eq major-mode 'sweep-top-level-mode)
+       (sweep-top-level-mode)))
+    (make-comint-in-buffer "sweep-top-level"
+                           buf
+                           (cons "localhost"
+                                 sweep-prolog-server-port))
+    (select-window (display-buffer buf))))
+
+
+;;;###autoload
+(define-derived-mode sweep-top-level-mode comint-mode "sweep Top-level"
+  "Major mode for interacting with an inferior Prolog interpreter."
+  :group 'sweep-top-level
+  (setq-local comint-prompt-regexp           (rx (seq line-start "?- "))
+              comint-input-ignoredups        t
+              comint-prompt-read-only        t
+              comint-delimiter-argument-list '(?,)
+              comment-start "%"))
 
-;; (add-to-list 'load-path (file-name-directory (buffer-file-name)))
-;; (require 'sweep)
 
 (sweep--ensure-module)
 (when sweep-init-on-load (sweep-init))
 
+;;;; Testing:
+
+;; (add-to-list 'load-path (file-name-directory (buffer-file-name)))
+;; (require 'sweep)
+
 (provide 'sweep)
 
 ;;; sweep.el ends here
index 6f9ef2a01b6116c4062071fcbea532b7be571861..c423dd5c9dba650e890f51b9fd5e3b158d69af68 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -5,6 +5,7 @@
             sweep_predicates_collection/2,
             sweep_modules_collection/2,
             sweep_packs_collection/2,
+            sweep_start_prolog_server/2,
             sweep_pack_install/2,
             sweep_module_path/2
           ]).
@@ -20,6 +21,7 @@
 :- use_module(library(pldoc/man_index)).
 :- use_module(library(lynx/html_text)).
 :- use_module(library(prolog_pack)).
+:- use_module(library(prolog_server)).
 
 :- dynamic sweep_current_color/3,
            sweep_open/2,
@@ -230,3 +232,11 @@ sweep_pack_info(pack(Name0, _, Desc0, Version0, URLS0), [Name, Desc, Version, UR
 
 sweep_pack_install(PackName, []) :-
     atom_string(Pack, PackName), pack_install(Pack, [silent(true), upgrade(true), interactive(false)]).
+
+
+% sweep_expand_file_name([SpecString|_Dir], Path) :-
+%     term_string(Spec, String),
+%     absolute_file_name(library(lists), Path, [access(exist), extensions(['pl', '']), solutions(all)]).
+
+sweep_start_prolog_server(Port, []) :-
+    prolog_server(Port, []).