fi-basic-lep.el 25 KB
Newer Older
Dave Cooper's avatar
Dave Cooper committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
;; Copyright (c) 1987-2002 Franz Inc, Berkeley, Ca.
;;
;; Permission is granted to any individual or institution to use, copy,
;; modify, and distribute this software, and to distribute modified
;; versions, provided that this complete copyright and permission notice is
;; maintained, intact, in all copies and supporting documentation.
;;
;; Franz Incorporated provides this software "as is" without
;; express or implied warranty.

;; The basic lep code that implements connections and sessions

(defvar fi::trace-lep-filter nil)	; for debugging

(defvar session) ;; yuck

(defun fi::show-error-text (format-string &rest args)
  (if (cdr fi:pop-up-temp-window-behavior)
      (apply 'fi:show-some-text nil format-string args)
    (let ((fi:pop-up-temp-window-behavior
	   (cons (car fi:pop-up-temp-window-behavior) 't)))
      (apply 'fi:show-some-text nil format-string args))))

(defun fi:show-some-text (xpackage text &rest args)
  (when args (setq text (apply (function format) text args)))
  (let ((n (string-match "[\n]+\\'" text)))
    (when n (setq text (substring text 0 n))))
  (if (and (not (eq 'minibuffer (car fi:pop-up-temp-window-behavior)))
	   (null (cdr fi:pop-up-temp-window-behavior)))
      (fi::show-some-text-1 text (or xpackage (fi::package)))
    (let* ((window (minibuffer-window))
	   (width (window-width window))
	   (lines/len (fi::frob-string text)))
      (if (or (and (eq 'minibuffer (car fi:pop-up-temp-window-behavior))
		   (fi::will-fit-in-minibuffer-p text))
	      (and (< (car lines/len) 2)
		   (<= (second lines/len) width)))
	  (progn
	    (message "%s" text)
	    (fi::note-background-reply))
	(fi::show-some-text-1 text (or xpackage (fi::package)))))))

;; Why are these necessary????
(defvar resize-mini-windows)
(defvar max-mini-window-height)

;; would have been nice to use display-message-or-buffer...
(defun fi::will-fit-in-minibuffer-p (string)
  (let ((lines (fi::count-lines-in-string string)))
    (if (or (<= lines 1)
	    (<= lines
		(if (and (boundp 'resize-mini-windows)
			 resize-mini-windows)
		    (cond ((floatp max-mini-window-height)
			   (* (frame-height) max-mini-window-height))
			  ((integerp max-mini-window-height)
			   max-mini-window-height)
			  (t 1))
		  1)))
	t
      nil)))

(defun fi::count-lines-in-string (string)
  (with-temp-buffer
      (progn
	(insert string)
	(count-lines (point-min) (point-max)))))

(defun fi::frob-string (text)
  (let ((start 0)
	(lines 0)
	(length (length text))
	(max-length 0)
	last
	m)
    (while (and (setq m (string-match "$" text start))
		(< m length))
      (setq last m)
      (let ((len (- m start)))
	(if (> len max-length) (setq max-length len)))
      (setq lines (+ lines 1)
	    start (1+ m)))
    (if (not (eq m last)) (setq lines (1+ lines)))
    (let ((len (- length start)))
      (if (> len max-length) (setq max-length len)))
    (list lines max-length)))

(defvar fi::show-some-text-1-first-time t)

(defvar fi::*show-some-text-buffer-name* "*CL-temp*")

(defun fi::show-some-text-1 (text apackage &optional hook &rest args)
  ;;A note from cer:
  ;;  I think there is some buffer local variable called package
  ;;  Outside of the save-excursion package has the correct value.
  ;;  Inside it has the value of NIL.
  ;;  Consequently fi:package ends up as NIL in *CL-temp* buffers.
  ;;  I renamed package to apackage and that seemed to fix it.
  (let ((buffer (get-buffer-create fi::*show-some-text-buffer-name*)))
    ;; fill the buffer
    (save-excursion
      (set-buffer buffer)
      (erase-buffer)
      (fi:common-lisp-mode)
      (setq fi:package apackage)
      (insert text)
      (goto-char (point-min)))
    (fi::display-pop-up-window buffer hook args))
  (fi::note-background-reply)
  (when fi::show-some-text-1-first-time
    (message
     "%s"
     (substitute-command-keys
      (format
       "Type \\[fi:lisp-delete-pop-up-window] to remove %s window."
       fi::*show-some-text-buffer-name*)))
    (setq fi::show-some-text-1-first-time nil)))

;;;;;;;;;;;;;;;;;;;;;;TODO
;;; The filter should not abort because of errors. Some how the errors
;;; should be printed.

(defun fi::make-connection (buffer host process)
  (list ':connection
	process
	nil				; sessions
	-1				; session id counter
	host
	buffer))

(defun fi::connection-process (c) (second c))

(defun fi::connection-sessions (c) (third c))
(defun fi::set-connection-sessions (c nv) (setf (third c) nv))

(defun fi::connection-session-id (c) (fourth c))
(defun fi::set-connection-session-id (c nv) (setf (fourth c) nv))

(defun fi::connection-host (c) (fifth c)) ; not used, apparently

(defun fi::connection-buffer (c) (sixth c))

(defvar fi::*connection* nil)

(defun fi::lep-open-connection-p ()
  (and fi::*connection*
       (fi:process-running-p (fi::connection-process fi::*connection*))
       fi::*connection*))

(defun fi::ensure-lep-connection ()
  (or (fi::lep-open-connection-p)
      (fi::try-and-start-lep-connection)
      (error "no connection")))

(defun fi:reset-lep-connection ()
  "Reset the Lisp-editor protocol connection."
  (interactive)
  (when (fboundp 'set-menubar-dirty-flag)
    (set-menubar-dirty-flag))
  (setq fi::*connection* nil))

;;; Start up a connection as soon as we know where to connect to.


(if (boundp 'fi:start-lisp-interface-hook)
    (push 'fi::auto-ensure-lep-connection fi:start-lisp-interface-hook))

(defun fi::auto-ensure-lep-connection ()
  (message "Trying to start connection...")
  (fi::ensure-lep-connection)
  (message "Trying to start connection...done."))

(defun fi::try-and-start-lep-connection ()
  (let ((buffer (process-buffer (progn
				  (fi::sublisp-select)
				  (get-process fi::process-name)))))
    (if buffer
	(save-excursion
	  (set-buffer buffer)
	  (fi::start-connection))
      (fi:error "
An internal error has occurred.  There is no Lisp process and the
emacs-lisp interface cannot be started.
"))))

(defun fi::find-connection-from-process (process)
  fi::*connection*)

(defun fi::find-session (connection id)
  "Search a CONNECTION looking for a session with the ID"
  (let ((sessions (fi::connection-sessions connection)))
    (while (and sessions (not (eq (fi::session-id (car sessions)) id)))
      (setq sessions (cdr sessions)))
    (and sessions (car sessions))))

(defun fi::start-connection ()
  (fi::make-connection-to-lisp fi::lisp-host
			       fi::lisp-port
			       fi::lisp-password))

;; Removed defunct fi::old-emacs-mule-p.  cac 7jun05

(defvar fi:use-emacs-mule-lisp-listeners t
  "*Flag which determines whether to set a buffer's Lisp listener's
*terminal-io* stream-external-format to :emacs-mule.  This flag should
normally be set to true.  Setting this flag to nil restores the
buffer-coding-systems setting behavior to be as it was in the Allegro CL 6.2
release.")

;; Removed defunct fi::emacs-mule-p.  cac 7jun05

(defun fi::coding-system-name (cs)
  (when (listp cs)
    (setq cs (car cs)))
  (when (fboundp 'coding-system-name)	; xemacs only
    (setq cs (coding-system-name cs)))
  cs)

;; spr30075
;; Notes: eli on xemacs requires the emacs-base package.  The mule-sumo should
;; also be installed for xemacs with mule to provide mule-ucs.
;;
;; Load mule-ucs if available.  This defines a good utf-8.
;; 
(condition-case nil (require 'un-define) (error nil))

(defun fi::lisp-connection-coding-system ()
  (if (and (string-match "xemacs" emacs-version)
	   (string< "21.5" emacs-version))
      ;; xemacs 21.5 has a good utf-8
      'utf-8
    ;; else check if we can load mule-ucs utf-8
    (if (member 'un-define features)
	;; then we have mule-ucs utf-8
	'utf-8
      ;; else return nil if no emacs-mule
      (when (fboundp 'coding-system-list)
	(car (member 'emacs-mule (coding-system-list)))))))

(defun fi::make-connection-to-lisp (host port passwd)
  (let* ((proc-name (format " *LEP %s %d %d*" host port passwd))
	 ;; buffer-name used to be non-nil only when fi::lep-debug
	 ;; was non-nil, but changes to lep::make-session-for-lisp
	 ;; depend on there being a buffer associated with the
	 ;; process.  So, we now do this in all cases.
	 (buffer-name proc-name)
	 (buffer (when buffer-name
		   (get-buffer-create buffer-name)))
	 (process (fi::open-network-stream proc-name nil host port))
	 (coding-system (fi::lisp-connection-coding-system)))
    (when buffer
      (bury-buffer buffer)
      (save-excursion (set-buffer buffer) (erase-buffer))
      (set-process-buffer process buffer))
    (when coding-system 
      ;; bug12456 -mikel
      (fi::set-process-coding process coding-system))
    (set-process-filter process 'fi::lep-connection-filter)
    ;; new stuff to indicate that we want the lisp editor protocol
    (process-send-string process ":lep\n")
    (process-send-string process (format "\"%s\"\n" proc-name))
    (process-send-string process (format "%d \n" passwd))
    ;; Send the class of the editor to the lisp.
    ;; This might affect something!
    ;; For example, gnu 19 has some good features.
;;;; The following works in xemacs 20.x when this file is compiled
;;;; with emacs 19.x, but we don't want to install this hack since
;;;; there are hundreds of other places a similar hack would have to
;;;; be installed.
    (let ((version-string (remove ?\" (emacs-version))))
      (process-send-string
       process
       (if coding-system
	   (format "\"%s :external-format %s\"\n" version-string coding-system)
	 (format "\"%s\"\n" version-string))))
    (prog1
	(setq fi::*connection*
	  (fi::make-connection (current-buffer) host process))
      (when (fboundp 'set-menubar-dirty-flag)
	(set-menubar-dirty-flag)))))

(defvar fi::debug-subprocess-filter nil)
(defvar fi::debug-subprocess-filter-output nil)

(defun fi::lep-connection-filter (process string)
  ;; When a complete sexpression comes back from the lisp, read it and then
  ;; handle it
  (when fi::debug-subprocess-filter
    (push string fi::debug-subprocess-filter-output))
  (let ((inhibit-quit t)
	(buffer (or (process-buffer process)
		    (get-buffer-create " LEP temp "))))
    (when fi::trace-lep-filter
      (fi::trace-debug (format "lep-connection-filter: %s" string)))
    (save-excursion
      (set-buffer buffer)
      (goto-char (point-max))
      (insert string))
    (let (form error)
      (while
	  (condition-case nil
	      (save-excursion
		(set-buffer buffer)
		(and
		 (null error)
		 (not (eq (point-max) (point-min)))
		 (progn
		   (goto-char (point-min))
		   (forward-sexp)
		   (let ((p (point)))
		     (goto-char (point-min))
		     (condition-case nil
			 (progn (setq form (read (current-buffer))) t)
		       (error (setq error t)))
		     (delete-region (point-min) p))
		   t)))
	    (error nil))
	(if error
	    (error "error reading return value: %s" string)
	  (fi::handle-lep-input process form))))))

(defun fi::trace-debug (string)
  (save-excursion
    (set-buffer (get-buffer-create "*LEP-DEBUG*"))
    (goto-char (point-max))
    (insert "\n")
    (insert string)
    (insert "\n")
    ;;(insert (with-output-to-string (backtrace))) (insert "\n")
    ))

(defun fi::handle-lep-input (process form)
  "A reply is (session-id . rest) or (nil . rest)"
  (when fi::trace-lep-filter
    (fi::trace-debug 
     (format "handle-lep-input:\n  process: %s\n  form: %s"
		   process form)))
  (let* ((id (car form))
	 (connection (fi::find-connection-from-process process))
	 (session (fi::find-session connection id)))
    (cond (session
	   (fi::handle-session-reply session (cdr form)))
	  ((car form)
	   (error "something for nonexistent session: %s" form))
	  (t (fi::handle-sessionless-reply (cdr form))))))

(defun fi::handle-session-reply (session form)
  "A session reply is (:error Message) or (nil . results)"
  (when (fi::session-oncep session)
    (fi::delete-session session))
  (if (eq (car form) ':error)
      (if (fi::session-error-function session)
	  (apply (fi::session-error-function session)
		 (second form)
		 (fi::session-error-arguments session))
	(error (second form)))
    (apply (fi::session-function session)
	   (if (fi::session-arguments session)
	     (append (cdr form) (fi::session-arguments session))
	     (cdr form)))))

(defun fi::delete-session (session)
  (let ((connection (fi::session-connection session)))
    (fi::set-connection-sessions
     connection
     (delq session (fi::connection-sessions connection)))))

(defun fi::handle-sessionless-reply (form)
  ;; A session-less reply is either (:error message) or (:request fn . args)
  (cond ((eq (car form) ':error)
	 (error (second form)))
	((eq (car form) ':request)
	 (condition-case error
	     (apply (fi::intern-it (second form)) (cddr form))
	   (error
	    (fi::show-error-text "Request error: %s"
				 (fi::prin1-to-string error)))))
	(t (error "Funny request received: %s" form))))


(defun fi::make-session (id oncep &optional fn args error-fn error-args)
  (list 'session id fn args error-fn error-args oncep))

(defun fi::session-id (s) (second s))

(defun fi::session-function (s) (third s))
(defun fi::set-session-function (s nv) (setf (third s) nv))


(defun fi::session-arguments (s) (fourth s))
(defun fi::set-session-arguments (s nv) (setf (fourth s) nv))

(defun fi::session-error-function (s) (fifth s))
(defun fi::set-session-error-function (s nv) (setf (fifth s) nv))

(defun fi::session-error-arguments (s) (sixth s))
(defun fi::set-session-error-arguments (s nv) (setf (sixth s) nv))

(defun fi::session-oncep (s) (seventh s))

(defun fi::session-connection (s) fi::*connection*)

(defun fi::modify-session-continuation (session continuation-and-arguments
					error-continuation)
  (fi::set-session-function session (car continuation-and-arguments))
  (fi::set-session-arguments session (cdr continuation-and-arguments))
  (fi::set-session-error-function session (car error-continuation))
  (fi::set-session-error-arguments session (cdr error-continuation)))

(defun fi::make-new-session (connection oncep continuation-and-arguments
			     &optional error-continuation)
  (let* ((id (fi::connection-session-id connection))
	 (session (fi::make-session id
				oncep
				(car continuation-and-arguments)
				(cdr continuation-and-arguments)
				(car error-continuation)
				(cdr error-continuation))))
    (fi::set-connection-session-id connection (1- id))
    (fi::add-session connection session)
    session))

(defun fi::remove-session (connection session)
  (fi::set-connection-sessions
   connection
   (delq session (fi::connection-sessions connection))))

(defun fi::add-session (connection session)
  (fi::set-connection-sessions
   connection
   (cons session (fi::connection-sessions connection))))



;;(defun lep::handle-query (session function arguments)
;;  (lep::send-back-reply session (apply function arguments)))


(defun lep::send-request-in-new-session (session-class oncep session-arguments
					 continuation-and-arguments
					 &optional error-continuation-and-arguments
						   ignore-package)
  (unless (fi::lep-open-connection-p)
    (error "There is no connection to Lisp.  See fi:common-lisp documentation."))

  (let* ((connection (fi::ensure-lep-connection))
	 (session (fi::make-new-session connection oncep
					continuation-and-arguments
					error-continuation-and-arguments))
	 (process (fi::connection-process connection)))
    (process-send-string process (fi::prin1-to-string
			  (list* nil
				 'lep::make-session session-class
				 ':session-id (fi::session-id session)
				 ':buffer-readtable (fi::string-to-keyword
						     fi:readtable)
				 (if (or ignore-package
					 (fi::member-plist ':buffer-package
							   session-arguments))
				     session-arguments
				   (list* ':buffer-package
					  (fi::string-to-keyword
					   (fi::package))
					  session-arguments)))))
    (process-send-string process "\n")
    session))

(defmacro fi::make-request (type-and-options continuation
			    &optional error-continuation ignore-package)
  (list 'lep::send-request-in-new-session
	(list 'quote (car type-and-options))
	t
	(cons 'list (fi::quote-every-other-one (cdr type-and-options)))
	(list* 'list
	       (list 'function
		     (list* 'lambda (append (fi::listify (second continuation))
					    (fi::listify (first continuation)))
			    (cddr continuation)))
	       (first continuation))
	(list* 'list (list 'function
			   (list* 'lambda (append (fi::listify (second error-continuation))
						  (fi::listify (first error-continuation)))
				  (cddr error-continuation)))
	       (first error-continuation))
	ignore-package))


(defmacro fi::make-complex-request (type-and-options continuation
				    &optional error-continuation)
  (list 'lep::send-request-in-new-session
	(list 'quote (car type-and-options))
	nil
	(cons 'list (fi::quote-every-other-one (cdr type-and-options)))
	(list* 'list (list 'function
			   (list* 'lambda
				  (append (fi::listify (second continuation))
					  (fi::listify (first continuation)))
				  (cddr continuation)))
	       (first continuation))
	(list* 'list (list 'function
			   (list* 'lambda
				  (append (fi::listify (second error-continuation))
					  (fi::listify (first error-continuation)))
				  (cddr error-continuation)))
	       (first error-continuation))))

(defun lep::send-request-in-existing-session (session session-class oncep
					      session-arguments
					      continuation-and-arguments
					      &optional error-continuation-and-arguments)
  (let* ((connection (fi::session-connection session))
	 (process (fi::connection-process connection)))
    (process-send-string process
		 (fi::prin1-to-string
		  (list* (fi::session-id session) session-class session-arguments)))
    (process-send-string process "\n")))

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

(defun lep::send-request-in-session (session session-class session-arguments
				     continuation-and-arguments
				     &optional error-continuation-and-arguments)
  (let* ((connection (fi::ensure-lep-connection))
	 (process (fi::connection-process connection)))
    (fi::modify-session-continuation
     session continuation-and-arguments error-continuation-and-arguments)
    (process-send-string process
		 (fi::prin1-to-string (list* (fi::session-id session)
					     ':request
					     session-class
					     session-arguments)))
    (process-send-string process "\n")))

(defmacro fi::make-request-in-existing-session (session type-and-options
						continuation
						&optional error-continuation)
  (list 'lep::send-request-in-session
	session
	(list 'quote (car type-and-options))
	(cons 'list (fi::quote-every-other-one (cdr type-and-options)))
	(list* 'list (list 'function
			   (list* 'lambda
				  (append (fi::listify (second continuation))
					  (fi::listify (first continuation)))
				  (cddr continuation)))
	       (first continuation))
	(list* 'list
	       (list 'function
		     (list* 'lambda (append (fi::listify (second error-continuation))
					    (fi::listify (first error-continuation)))
			    (cddr error-continuation)))
	       (first error-continuation))))

(defun fi::intern-it (s)
  (if (stringp s) (intern s) s))

(defvar connection) ;; ugly, but necessary
(defvar process) ;; this, too...  crikey, this is grossssss!!!!!

(defun lep::make-session-for-lisp (session-id replyp oncep function &rest args)
  (let ((session (fi::make-session session-id nil))
	(done nil)
	(dead (or (eq 'closed (process-status process))
		  ;; The following test relies on
		  ;; fi::make-connection-to-lisp associating the process
		  ;; with a buffer IN ALL CASES, not just for debugging.
		  (null (process-buffer process)))))
    (fi::add-session connection session)
    (unwind-protect
	(when (not dead)
	  (condition-case error
	      (let* ((result (apply (fi::intern-it function) args)))
		(when replyp
		  (process-send-string process
				       (fi::prin1-to-string
					(list* (fi::session-id session)
					       ':reply
					       result)))
		  (process-send-string process "\n")))
	    (error
	     (if replyp
		 (progn
		   (process-send-string process
					(fi::prin1-to-string
					 (list (fi::session-id session)
					       ':error
					       (fi::prin1-to-string error))))

		   (process-send-string process "\n"))
	       (fi::show-error-text
		"Error %s in %s\nwith args: %s\nstack dump:\n%s"
		(fi::prin1-to-string
		 (if (and (consp error) (cdr error))
		     (cdr error)
		   error))
		function
		args
		(when (fboundp 'backtrace)
		  (with-output-to-string (backtrace)))))))
	  (setq done t))
      (when (and (not dead) (not done))
	(process-send-string process
		     (fi::prin1-to-string (list (fi::session-id session)
						':error
						':aborted))))
      (when (or dead oncep)
	(lep::kill-session session)))))

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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun fi::eval-in-lisp-wait-for-connection ()
  (if (not (fi::lep-open-connection-p))
      (let ((i 0) (max 20))
	(while (and (< i max)
		    fi::common-lisp-backdoor-main-process-name
		    (get-process fi::common-lisp-backdoor-main-process-name)
		    (not (fi::lep-open-connection-p))
		    (progn (sleep-for 3) t))
	  (setq i (+ i 1))))))

(defun fi:eval-in-lisp-asynchronous (string &rest args)
  "Apply (Emacs Lisp) format to STRING and ARGS and asychronously evaluate
the result in the Common Lisp to which we are connected."
  (fi::eval-in-lisp-wait-for-connection)
  (let ((string (if args (apply 'format string args) string)))
    (fi::make-request
	;;fi::frob-case-to-lisp removed - 18jan94 smh
	(lep::eval-from-emacs-session :string string)
      ;; Normal continuation
      (() (value)
       ;; ignore the value...
       nil)
      ((string) (error)
       (fi::show-error-text "error evaluating %s: %s" string error)))))

(defvar fi:lisp-evalserver-number-reads 200
  "*The number of times the Lisp eval server tries to read from the
lisp-evalserver process before giving up.  Without this feature Emacs would
hang if Lisp got into an infinite loop while printing.  If the size of the
values returned to Emacs is large, then the value of this variable should
be increased.")

(defun fi:eval-in-lisp (string &rest args)
  "Apply (Emacs Lisp) format to STRING and ARGS and sychronously evaluate
the result in the Common Lisp to which we are connected."
  (fi::eval-in-lisp-wait-for-connection)
  (let ((string (fi::defontify-string
		    (if args (apply 'format string args) string))))
    ;;fi::frob-case-to-lisp removed - 18jan94 smh
    (car (lep::eval-session-in-lisp 'lep::eval-from-emacs-session
				    ':string string))))

(defun lep::eval-session-in-lisp (function &rest arguments)
  (let* ((result-cons (list nil nil nil))
	 (session (lep::send-request-in-new-session
		   function
		   t
		   arguments
		   (list (function fi::immediate-reply-continuation)
			 result-cons)
		   (list (function fi::immediate-reply-error-continuation)
			 result-cons))))
    (fi::wait-for-reply-to-come-back result-cons)))

(defun fi::immediate-reply-continuation (&rest results)
  (let ((r (butlast results))
	(c (car (last results))))
    (fi::stash-in-cons c r nil)))

(defun fi::immediate-reply-error-continuation (error result-cons)
  (fi::stash-in-cons result-cons error t))

(defun fi::stash-in-cons (c r p)
  (setf (third c) p)
  (setf (second c) r)
  (setf (first c) t))

;;; This is complicated because we have to wait for output multiple times.

(defun fi::wait-for-reply-to-come-back (result-cons)
  (when (not (car result-cons))
    (let ((count fi:lisp-evalserver-number-reads))
      (while (and (> (setq count (1- count)) 0)
		  (null (car result-cons)))
	(accept-process-output
	 (fi::connection-process (fi::ensure-lep-connection)))))
    (when (not (car result-cons)) (error "Eval in lisp timed out"))
    (if (third result-cons)
	(error (second result-cons))
      (second result-cons))))

(defun lep::make-request-in-session-and-wait (session function &rest arguments)
  "Send a request to SESSION consisting of FUNCTION and ARGUMENTS and wait
for the reply to come back. It returns a list of values."
  (let* ((result-cons (list nil nil nil))
	 (session (lep::send-request-in-session
		   session
		   function
		   arguments
		   (list (function fi::immediate-reply-continuation)
			 result-cons)
		   (list (function fi::immediate-reply-error-continuation)
			 result-cons))))
    (fi::wait-for-reply-to-come-back result-cons)))