From: Eshel Yaron Date: Mon, 29 Aug 2022 16:50:55 +0000 (+0300) Subject: ADDED: sweep-top-level command and appropriate mode X-Git-Tag: v0.2.0~70 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c5b1504ca8528743a8528aa2c824d8536f0a7c29;p=dict.git ADDED: sweep-top-level command and appropriate mode --- diff --git a/README.org b/README.org index 021160b..e6450c0 100644 --- a/README.org +++ b/README.org @@ -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 diff --git a/sweep.el b/sweep.el index 0ba1589..ba4e342 100644 --- 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." @@ -90,11 +95,23 @@ (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 diff --git a/sweep.pl b/sweep.pl index 6f9ef2a..c423dd5 100644 --- 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, []).