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
;;; Code:
+(require 'comint)
+
(defgroup sweep nil
"SWI-Prolog Embedded in Emacs."
:group 'prolog)
: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)
(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
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
]).
:- 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,
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, []).