;;; File: emacs-side-interaction.el
;;; Code for enabling interaction between KPML and the emacs for
;;; editing resource definitions.
;;; Taken from ACL FAQ: 4.2: LEP Protocol
;;;
;;; ========================================
;;; N.B.
;;;
;;; Don't forget to make sure that the Allegro fi-site-init.el (usually
;;; in the eli subdirectory of the ACL release) gets loaded so that
;;; you can properly start KPML within a running Emacs process before
;;; working with the KPML-Emacs combination!
;;; ========================================

   ;;;; ED-WAIT

   (defun ed-quit ()
     (interactive)
     (fi::pop-metadot-session))

   ;;;; ED-SEXP

   (defun lep::edit-form (form)
     (switch-to-buffer (get-buffer-create "*ed*"))
     (erase-buffer)
     (insert (prin1-to-string form))
     (goto-char (point-min))
     (setq buffer-file-name "/tmp/foo.cl")
     (fi:common-lisp-mode))

;;;; ED-SEXP-WAIT
;;;; with extra exit command for "do nothing" c-c c-z

   (defvar lep::ed-sexp-wait-session nil)

   (defun lep::edit-string-wait (string)
     (setq lep::ed-sexp-wait-session session)
     (switch-to-buffer (get-buffer-create "*kpml-resource-editor-buffer*"))
     (erase-buffer)
     (insert string)
     (goto-char (point-min))
     (setq buffer-file-name "/tmp/foo.cl")
     (fi:common-lisp-mode)
     (define-key (current-local-map) "\C-c\C-c" 'ed-sexp-wait-done)
     (define-key (current-local-map) "\C-c\C-z" 'ed-sexp-wait-done-noop))

   (defun ed-sexp-wait-done ()
     (interactive)
     (lep-send-reply lep::ed-sexp-wait-session (buffer-string))
     (setq lep::ed-sexp-wait-session nil)
     (bury-buffer))


;;; The following function should be defined in the emacs-lisp interface,
;;; but it didn't make it into the ACL 4.1 release.

   (defun lep-send-reply (session string)
     (let* ((connection (fi::session-connection session))
            (process (fi::connection-process connection)))
       (send-string process
                    (prin1-to-string
                     (list (fi::session-id session) ':reply string)))
       (send-string process "\n")))


;;; KPML additions...

   (defun ed-sexp-wait-done-noop ()
     (interactive)
     (lep-send-reply lep::ed-sexp-wait-session "NOOP")
     (setq lep::ed-sexp-wait-session nil)
     (bury-buffer))

   (defvar lep::gen-string-no 1)

   (defun lep::show-string-nowait (string)
     (setq lep::ed-sexp-wait-session session)
     (switch-to-buffer (get-buffer-create 
			(concat "*generated-string*<" lep::gen-string-no ">")))
     (erase-buffer)
     (insert string)
     (goto-char (point-min))
     (setq buffer-file-name 
       (concat "/tmp/kpml-generated-string-" lep::gen-string-no ".text"))
     (setq lep::gen-string-no (+ lep::gen-string-no 1))
     (fi:common-lisp-mode)
     (setq lep::ed-sexp-wait-session nil)
     )

  