
(in-package "KPML-I")

;;; File: emacs-actions.lisp
;;; Author: John.
;;; 
;;; THE FOLLOWING SHOULD ONLY BE ACTIVATED
;;; WHEN KPML IS RUNNING IN THE CONTEXT OF EMACS AND KPML-I UNDER CLIM
;;; USING ACL4.3. 
;;; **  The alternative for ACL-PC 3.0, CLIM2.1 is kept in:  **
;;; **    acl-clim/winacl-actions.lisp                       **
;;;
;;; Use is made of the ACL lep protocol...
;;;
;;; Provides the function definitions that link the general editing
;;; actions with those using the Lep for linking to Emacs under Unix.
;;;

(defun EDITING-POSSIBLE? () 
  (if (lep:lep-is-running)
      T
    (let ((stream 
	   (clim:get-frame-pane *resource-inspector-frame* 'inspector)))
      (format stream
	      "~%Do not seem to be running within Emacs; cannot edit!
If Emacs is being used, try issuing (kpml-i::editing-on) from a
KPML Lisp listener.")
      nil)))


(defun EDIT-KPML-FORM-AND-WAIT (form)
  (lep::EDIT-KPML-FORM-AND-WAIT form))


;;; ----------------------------------------------------------------------
;;; Inquiry implementations...
;;;
;;; 1.0.14
;;; The following used to use lep:with-output-to-temp-buffer but this appears
;;; to give problems... This is now changed to be more in line with the
;;; other commands for editing linguistic objects. Behaviour only changed
;;; for *new* inquiry implementations however; the rest stays as it
;;; is described in the manual (1.0). An additional emacs-lisp
;;; interface function is defined: this one takes the first form
;;; of the temporary buffer as a package definition (if it is one) which
;;; is used when evaluating the second form.  This is given below but
;;; should be folded into lisp-side-interaction.lisp.
;;;
;;; 1.0.15
;;; ability to edit inquiry implementations defined at top-level added.
;;;
;;; 1.1 
;;; searches back from end of file, not from beginning, in order
;;; to find last definition in file in case of redefinitions.

(defun EDIT-INQUIRY-IMPLEMENTATION (inquiry-implementation)
  (let ((file (excl::source-file inquiry-implementation))
	(s (clim:get-frame-pane *resource-inspector-frame* 'inspector)))
    (cond ((pathnamep file)
	   (format s "~%Changing edit buffer to find file: ~W" file)
	   (lep::eval-in-emacs
	    (format nil "(find-file ~S)" (namestring file)))
	   (lep::eval-in-emacs "(end-of-buffer)")           ;; 1.1
	   (lep::eval-in-emacs 
	    (format nil "(search-backward \"(defun ~A\")"  ;; 1.1
		    (string inquiry-implementation))))
	  ((eq file :top-level)                            ;;1.0.15
	   (let ((def (if (fboundp inquiry-implementation)
			  (function-lambda-expression 
			   (symbol-function inquiry-implementation))
			nil)))
	     (unless def
	       (format s "~%Implementation not available.")
	       (return-from EDIT-INQUIRY-IMPLEMENTATION nil))
	     (let ((result                           ;; 1.0.14
		    (lep::edit-kpml-form-and-wait-2
		     (with-output-to-string (stream)
		       (format stream "(in-package ~S)~3%~
   (defun ~A ~A ~{~S~})~2%"
			       (package-name 
				(symbol-package 
				 inquiry-implementation))
			       inquiry-implementation
			       (second def)
			       (cddr (caddr def)))))))
	       (if result (kpml::kpml-eval result)
		 (format s
			 "~%No inquiry implementation changed.")))))
	  (t  
	   (let ((result                           ;; 1.0.14
		  (lep::edit-kpml-form-and-wait-2
		   (with-output-to-string (stream)
		     (format stream "(in-package ~S)~3%~
                                      (defun ~A ~S
   \"Inquiry implementation function not yet defined.\"
   )~2%"
			     (package-name 
			      (symbol-package inquiry-implementation))
			     inquiry-implementation 
			     (guess-parameters-for 
			      inquiry-implementation))))))
	     (if result (kpml::kpml-eval result)
	       (format s
		       "~%No inquiry implementation changed.")))))))




