(defvar Cat-have-code-file t)
(defvar Cat-use-buffer-p nil)

;;; generic function to create xterm X windows.

(defun xterm-process (command-line &optional xterm-args destruct-delay
				   shell-args alternate-shell)
  "Executes COMMAND-LINE in a shell in an independent xterm.
The shell is specified by emacs's  shell-file-name .
The xterm is started with an optional argument string XTERM-ARGS.
After the command exits, xterm waits DESTRUCT-DELAY seconds before exiting.
The shell can be given optional SHELL-ARGS,
and can be specified by ALTERNATE-SHELL rather than by  shell-file-name .
See  xterm , which starts an xterm, but doesnt run a command."
  (interactive "sWhat command should be run in xterm?: 
sAny xterm arguments? [defualt=none]: 
sAfter finishing command, keep xterm up for how many seconds?[default=3]: ")
  (call-process "/bin/sh" nil 0 nil
		"-c"
		(concat "xterm "
			(if (or (not xterm-args) ; non-interactive not given
				(equal xterm-args "")) ; interactive not given
			    ""
			  xterm-args)
			" -e "
			;; Shell
			(if (or (not alternate-shell)
					; "" check not realy needed
					; till alt-shell made interactive.
				(equal alternate-shell ""))
			    shell-file-name
			  alternate-shell)
			" "
			;; Shell args
			(if (or (not shell-args)
					; "" check not yet needed.
				(equal shell-args "")) 
			    ""
			  shell-args)
			"-c \""
			command-line
			";sleep "
			(if (or (not destruct-delay)
				(equal destruct-delay ""))
			    "3"
			  destruct-delay)
			"\"")))

;;; functions to get around Info's  "last node" capability.

(defun Cat-Info-erase-last ()
  (setq Info-history (cdr Info-history)))

(defun Cat-Info-lastx2 ()
  (Cat-Info-erase-last)
  (Info-last))


;;; functions to unpack connection information from library entries.

(defun Cat-terminal-type (type)
  (cond 
   ((not type) 'vt100)
   ((eq type 'tn3270) type)
   ((eq type 't-opt)
    (if (y-or-n-p
	 "Can use both vt100(y:default) or tn3270(n) interface. Enter ")
	'vt100
      'tn3270))))

(defun Cat-invoke-terminal (simple-type)
  (cond ((eq simple-type 'vt100) "telnet")
	((eq simple-type 'tn3270) "tn3270")))

(defun Cat-telnet-address (simple-type addresses)
  (cond ((stringp addresses) addresses)
	((not(listp addresses)) (error "adrs n lst"))
	((stringp (car addresses)) (car addresses))
	(t (car(cdr (assq simple-type addresses))))))

;;; use buffer or window

(defun Cat-buf/term? ()
  (Info-goto-node "use buffer?")
  (if (y-or-n-p "Use buffer rather than window?")
      (setq Cat-use-buffer-p t)
    (setq Cat-use-buffer-p nil))
  (Cat-Info-erase-last)
  (Info-last))

;;; Telnet

(defun Cat-connect-hesitantly (return-node
			     term-title
			     addresses
			     &optional type)
  (Info-goto-node "make connection?")
  (Cat-Info-erase-last)
  (if (yes-or-no-p (concat "Initiate new telnet contact with library? "))
      (if Cat-use-buffer-p
	  (message "NOT IMPLEMENTED")
	(Cat-xterm return-node term-title addresses type))
    (message "No new terminal started.")
    (Info-last)))


;;; Xterm stuff

(defun Cat-xterm-args (term-name)
  (cond ((stringp term-name) (concat "-T \"" term-name "\" "))
	((listp term-name)    (concat "-T \"" (car term-name) "\" "
				      "-n \"" (car(cdr term-name)) "\" "))))




(defun Cat-xterm (return-node term-title addresses &optional type)
  "Creates an xterm named TERM-LABLE and starts a telnet to the first
address in ADDRESSES.  Any other addresses are not used.
If TYPE is nil, uses vanilla  telnet .
If TYPE is TN3270, uses  tn3270 .
If TYPE is T-OPT, asks what to use.
If RETURN-NODE is non-nil, then Info-goto's there."
  (let* ((simple-type (Cat-terminal-type type))
	 (term (Cat-invoke-terminal simple-type))
	 (telnet-address (Cat-telnet-address simple-type addresses)))
    (message "Xterm starting ...")
    (xterm-process 
     (concat
      term " "
      telnet-address
      ";echo telnet done.  This window will self destruct in 10 seconds... ")
     (Cat-xterm-args term-title)
     10
     )
    (message "Xterm starting ... Started.")
    (if return-node (progn (Info-goto-node return-node)
			   (Cat-Info-erase-last)
			   (Cat-Info-erase-last))
      (Info-last))))

;;; Buffer stuff

