[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
ediprolog.el 0.9w -- Interaction with SWI-Prolog
From: |
Markus Triska |
Subject: |
ediprolog.el 0.9w -- Interaction with SWI-Prolog |
Date: |
Tue, 20 May 2008 16:51:57 +0200 |
New:
*) handle toplevel changes of newer SWI-Prolog versions
*) preserve process output more faithfully
*) improve C-c handling
Project page:
http://www.logic.at/prolog/ediprolog/ediprolog.html
Code:
;;; ediprolog.el --- Emacs does Interactive Prolog
;; Copyright (C) 2006, 2007, 2008 Markus Triska
;; Author: Markus Triska <address@hidden>
;; Keywords: languages, processes
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; These definitions let you transparently interact with SWI-Prolog in
;; all buffers. You can load and syntax-check Prolog programs and
;; evaluate queries with minimal exposure to the toplevel. Queries
;; start with `?-' or `:-', possibly preceded by `%' and whitespace.
;; Copy ediprolog.el to your load-path and add to your .emacs:
;; (require 'ediprolog)
;; (global-set-key [f10] 'ediprolog-dwim)
;; Restart Emacs and customize ediprolog with
;;
;; M-x customize-group RET ediprolog RET
;;
;; Press F10 in a Prolog program to load it (point is moved to the
;; first error, if any). Press F10 on a query in the program to
;; evaluate it. Query results are inserted into the buffer, and you
;; interact with SWI-Prolog as on a terminal. Use C-g to unblock Emacs
;; in the case of long-running queries. To resume interaction with the
;; toplevel when the query is finished, use M-x ediprolog-toplevel.
;; Use M-x ediprolog-localize before loading a program to make the
;; Prolog process buffer-local. This way, you can run distinct
;; processes simultaneously. Revert with M-x ediprolog-unlocalize.
;; Tested with SWI-Prolog 5.6.55 + Emacs 22.2 and 23.0.60.
;;; Code:
(defconst ediprolog-version "0.9w")
(defgroup ediprolog nil
"Transparent interaction with SWI-Prolog."
:group 'languages
:group 'processes)
;;;###autoload
(defcustom ediprolog-program
(or (executable-find "swipl") (executable-find "pl") "swipl")
"Program name of the Prolog executable."
:group 'ediprolog
:type 'string)
;;;###autoload
(defcustom ediprolog-program-switches nil
"List of switches passed to the Prolog process. Example:
'(\"-G128M\" \"-O\")"
:group 'ediprolog
:type '(repeat string))
;;;###autoload
(defcustom ediprolog-prefix "%@ "
"String to prepend when inserting output from the Prolog
process into the buffer."
:group 'ediprolog
:type 'string)
(defvar ediprolog-more-solutions nil
"Whether more solutions could be pending.")
(defvar ediprolog-process nil "A Prolog process.")
(defvar ediprolog-first-error-line nil
"Line of the first compilation error, if any.")
(defvar ediprolog-consult-output nil
"Whether consulting yielded any messages.")
(defvar ediprolog-seen-prompt nil
"Whether a prompt was (recently) emitted by the Prolog process.")
(defvar ediprolog-read-term nil
"Whether the Prolog process waits for the user to enter a term.")
(defvar ediprolog-indent-prefix ""
"Any whitespace occurring before the most recently executed query.")
(defvar ediprolog-temp-file nil
"File name of a temporary file used for consulting the buffer.")
(defmacro ediprolog-wait-for-prompt-after (form)
"Evaluate FORM and wait for prompt."
`(progn
(setq ediprolog-seen-prompt nil)
,form
(set-process-filter ediprolog-process 'ediprolog-wait-for-prompt-filter)
(while (and (ediprolog-running) (not ediprolog-seen-prompt))
(if (ediprolog-emacs-geq-22)
(accept-process-output ediprolog-process 0.1)
(accept-process-output ediprolog-process)))))
(defun ediprolog-emacs-geq-22 ()
"Whether the major version of Emacs is greater or equal 22."
(>= emacs-major-version 22))
(defun ediprolog-run-prolog ()
"Start a Prolog process."
;; If the Emacs version supports it (>= 22), the invocation buffer
;; is stored as a process property, and the process's buffer is nil
;; to avoid spurious messages like "Process killed." in the buffer.
(ediprolog-wait-for-prompt-after
(setq ediprolog-process
(apply #'start-process "ediprolog"
(if (ediprolog-emacs-geq-22)
nil
(current-buffer))
ediprolog-program "-q" "-g" "'$set_prompt'('?- ')"
ediprolog-program-switches)))
(unless (ediprolog-running)
(error "Couldn't start Prolog process."))
(let ((lang (getenv "LANG")))
(when (or (null lang) (string= lang ""))
(set-process-coding-system
ediprolog-process buffer-file-coding-system buffer-file-coding-system)))
(ediprolog-update-process-buffer))
(defun ediprolog-kill-prolog ()
"Kill the Prolog process."
(when (ediprolog-running)
(unless (ediprolog-emacs-geq-22)
(set-process-buffer ediprolog-process nil))
(kill-process ediprolog-process)
(setq ediprolog-process nil)))
(defun ediprolog-update-process-buffer ()
"Associate the current buffer with the running Prolog process."
(if (ediprolog-emacs-geq-22)
(process-put ediprolog-process :invocation-buffer (current-buffer))
(set-process-buffer ediprolog-process (current-buffer))))
(defun ediprolog-wait-for-prompt-filter (proc string)
"Filter used to set `ediprolog-seen-prompt' when prompt appears."
;; This filter knows a bit about the syntax of ERROR messages
;; and stores the line of the first error.
(let ((lines (split-string string "\n")))
(dolist (el '("Yes" "" "true."))
(setq lines (delete el lines)))
(dolist (str lines)
(if (string= str "?- ")
(setq ediprolog-seen-prompt t)
(princ (format "%s\n" str))
(setq ediprolog-consult-output t)
(when (and (not ediprolog-first-error-line)
(string-match (concat "^ERROR: (?"
(regexp-quote ediprolog-temp-file)
":\\([0-9]+\\)") str))
(setq ediprolog-first-error-line
(string-to-number (match-string 1 str))))))))
;;;###autoload
(defun ediprolog-dwim (&optional arg)
"Load current buffer into Prolog or post query (Do What I Mean).
If invoked on a line starting with `:-' or `?-', possibly
preceded by `%' and whitespace, call `ediprolog-interact' with
the query as argument. Otherwise, call `ediprolog-consult'.
With prefix argument 0, kill the Prolog process. With prefix 1,
equivalent to `ediprolog-consult'. With prefix 2, equivalent to
`ediprolog-consult' with argument t. With just C-u, first call
`ediprolog-consult' and then, if point is on a query, call
`ediprolog-interact' with it as argument. Analogously, C-u C-u
for `ediprolog-consult' with argument t. With other prefix
arguments, equivalent to `ediprolog-remove-interactions'."
(interactive "P")
(when (eq arg 0)
(if (ediprolog-running)
(progn
(ediprolog-kill-prolog)
(message "Prolog process killed."))
(message "No Prolog process running.")))
(unless (or (null arg) (equal arg '(4)) (equal arg '(16))
(eq arg 0) (eq arg 1) (eq arg 2))
(ediprolog-remove-interactions)
(message "Interactions removed."))
(when (or (equal arg '(4)) (equal arg '(16)) (eq arg 1) (eq arg 2))
(ediprolog-consult (or (eq arg 2) (equal arg '(16)))))
(when (or (null arg) (equal arg '(4)) (equal arg '(16)))
(if (and (not (and transient-mark-mode mark-active))
(save-excursion
(beginning-of-line)
(looking-at "\\([\t ]*\\)%*[\t ]*[:?]-")))
(progn
;; whitespace preceding the query is the indentation level
(setq ediprolog-indent-prefix (match-string 1))
(let* ((from (goto-char (match-end 0)))
(to (if (re-search-forward "\\.[\t ]*\\(?:%.*\\)?$" nil t)
;; omit trailing whitespace
(+ (point) (skip-chars-backward "\t "))
(error "Missing `.' at the end of this query")))
(query (buffer-substring-no-properties from to)))
(end-of-line)
(insert "\n" ediprolog-indent-prefix ediprolog-prefix)
(ediprolog-interact
(format "%s\n" (mapconcat #'identity
;; `%' can precede each query line
(split-string query "\n[ \t%]*") " ")))))
(unless arg
(ediprolog-consult)))))
;;;###autoload
(defun ediprolog-interact (query)
"Send QUERY to Prolog process and interact as on a terminal.
You can use \\[keyboard-quit] to unblock Emacs in the case of
longer-running queries. When the query completes and the toplevel
asks for input, use \\[ediprolog-toplevel] to resume interaction
with the Prolog process."
(unless (ediprolog-running)
(ediprolog-run-prolog))
(set-marker (process-mark ediprolog-process) (point))
(ediprolog-update-process-buffer)
;; set more verbose mode for time/1 and other messages
(ediprolog-wait-for-prompt-after
(process-send-string ediprolog-process
"set_prolog_flag(verbose,normal).\n"))
(set-process-filter ediprolog-process 'ediprolog-interact-filter)
(setq ediprolog-more-solutions t
ediprolog-read-term nil)
(process-send-string ediprolog-process query)
(ediprolog-toplevel))
(defun ediprolog-toplevel ()
"Start or resume Prolog toplevel interaction in the buffer.
You can use this function if you have previously quit (with
\\[keyboard-quit]) waiting for a longer-running query and now
want to resume interaction with the toplevel."
(interactive)
(let ((buffer (if (ediprolog-emacs-geq-22)
(process-get ediprolog-process :invocation-buffer)
(process-buffer ediprolog-process))))
(select-window (display-buffer buffer)))
(while (ediprolog-more-solutions)
(let (string
char)
;; poll for user input; meanwhile, process output can arrive
(while (and (ediprolog-more-solutions) (null string))
(goto-char (process-mark ediprolog-process))
(if ediprolog-read-term
(progn
(setq string (concat (read-string "Input: ") "\n"))
(ediprolog-insert-at-marker
string ediprolog-indent-prefix ediprolog-prefix)
(setq ediprolog-read-term nil))
(setq char (if (ediprolog-emacs-geq-22)
(read-event nil nil 0.1)
(read-event nil nil)))
;; TODO: use `characterp' with recent CVS Emacs
(cond ((char-valid-p char) (setq string (char-to-string char)))
(char (message "Press h for help."))))) ; no time-out
(when (ediprolog-more-solutions)
(if (eq char ?\C-c) ; char can be nil too
;; sending C-c directly yields strange SWI buffering
(interrupt-process ediprolog-process)
(process-send-string ediprolog-process string)))))
(when (ediprolog-running)
(ediprolog-wait-for-prompt-after
(process-send-string ediprolog-process
"set_prolog_flag(verbose,silent).\n"))))
;;;###autoload
(defun ediprolog-remove-interactions ()
"Remove all lines starting with `ediprolog-prefix' from buffer.
In transient mark mode, the function operates on the region if it
is active."
(interactive)
(save-excursion
(save-restriction
(when (and transient-mark-mode mark-active)
(narrow-to-region (region-beginning) (region-end)))
(goto-char (point-min))
(flush-lines (concat "^[\t ]*" (regexp-quote ediprolog-prefix))))))
;;;###autoload
(defun ediprolog-consult (&optional new-process)
"Buffer is loaded into a Prolog process. If NEW-PROCESS is
non-nil, start a new process. Otherwise use the existing process,
if any. Process output not equal `Yes', `true' or `?- ' is
displayed. In case of errors, point is moved to the position of
the first error, and the mark is left at the previous position.
In transient mark mode, the function operates on the region if it
is active."
(interactive)
(when new-process
(ediprolog-kill-prolog))
(unless (ediprolog-running)
(ediprolog-run-prolog))
(ediprolog-update-process-buffer)
(if ediprolog-temp-file
(write-region "" nil ediprolog-temp-file nil 'silent)
(setq ediprolog-temp-file (make-temp-file "ediprolog")))
(let ((start (if (and transient-mark-mode mark-active)
(region-beginning) (point-min)))
(end (if (and transient-mark-mode mark-active)
(region-end) (point-max))))
(write-region start end ediprolog-temp-file nil 'silent)
(setq ediprolog-consult-output nil
ediprolog-first-error-line nil)
(ediprolog-wait-for-prompt-after
(process-send-string ediprolog-process
(format "['%s'].\n" ediprolog-temp-file)))
(if ediprolog-first-error-line
(unless (and transient-mark-mode mark-active)
(let ((msg (current-message)))
(goto-line ediprolog-first-error-line) ; shows "Mark set"
(princ msg)))
(unless ediprolog-consult-output
(message "%s consulted." (if (and transient-mark-mode mark-active)
"Region" "Buffer"))))))
(defun ediprolog-running ()
"True iff `ediprolog-process' is a running process."
(and (processp ediprolog-process)
(eq (process-status ediprolog-process) 'run)))
(defun ediprolog-more-solutions ()
"True iff there could be more solutions from the process."
(and ediprolog-more-solutions (ediprolog-running)))
(defun ediprolog-interact-filter (proc string)
"Insert output from the process and update the state."
(let ((buffer (if (ediprolog-emacs-geq-22)
(process-get proc :invocation-buffer)
(process-buffer proc))))
(when (string= string "\n")
(setq string ""))
(with-temp-buffer
(insert string)
;; read a term from the user?
(goto-char (point-min))
(setq ediprolog-read-term (re-search-forward "^|: " nil t))
;; check for prompt
(goto-char (point-min))
(when (re-search-forward "^\\?- " nil t)
(setq ediprolog-more-solutions nil)
(replace-match ""))
;; remove newlines after the final answer substitution
(goto-char (point-max))
(skip-chars-backward "\n")
(when (eq (char-before) ?\.)
(delete-region (point) (point-max)))
;; precede each line with ediprolog prefices
(goto-char (point-min))
(while (search-forward "\n" nil t)
(replace-match
(format "\n%s%s" ediprolog-indent-prefix ediprolog-prefix)))
(setq string (buffer-string)))
(when (and (ediprolog-running) (buffer-live-p buffer))
(with-current-buffer buffer
(let ((near (<= (abs (- (point) (process-mark ediprolog-process))) 1)))
(ediprolog-insert-at-marker string)
(when near
;; catch up with output if point was reasonably close
(goto-char (process-mark ediprolog-process))))))))
(defun ediprolog-insert-at-marker (&rest args)
"Insert strings ARGS at marker and update the marker."
(save-excursion
(goto-char (process-mark ediprolog-process))
(end-of-line)
(apply #'insert args)
(set-marker (process-mark ediprolog-process) (point))))
(defun ediprolog-map-variables (func)
"Call FUNC with all ediprolog variables that can become buffer-local."
(mapc func '(ediprolog-more-solutions
ediprolog-process
ediprolog-first-error-line
ediprolog-consult-output
ediprolog-seen-prompt
ediprolog-read-term
ediprolog-indent-prefix
ediprolog-temp-file)))
;;;###autoload
(defun ediprolog-localize ()
"After `ediprolog-localize', any Prolog process started from
this buffer becomes buffer-local."
(interactive)
(unless (local-variable-p 'ediprolog-process)
(ediprolog-map-variables #'make-local-variable)
(setq ediprolog-temp-file nil
ediprolog-process nil)))
(defun ediprolog-unlocalize ()
"Revert the effect of `ediprolog-localize'."
(interactive)
(when (local-variable-p 'ediprolog-process)
(ediprolog-kill-prolog)
(ediprolog-map-variables #'kill-local-variable)))
(provide 'ediprolog)
;;; ediprolog.el ends here
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- ediprolog.el 0.9w -- Interaction with SWI-Prolog,
Markus Triska <=