 
 ;;; @@ Yacas-Notebook functions for "yacas" cells
 
-(defun ynb-send-line ()
-  "Send the current line to the yacas process."
-  (interactive)
-  (save-excursion
-    (beginning-of-line)
-    (setq bg (point))
-    (end-of-line)
-    (setq ed (point))
-    (yacas-region bg ed)
-    (if ynb-texform
-	(yacas-string ynb-tex-string))))
-
-
-(defun ynb-send-cell ()
+(defun ynb-send-cell (&optional tex)
   "Send input to process. Point must be in a cell."
   (interactive)
   (if (not (ynb-cell-p))
       (error "Not in Yacas cell"))
   (let ((home-buffer (current-buffer))
-        assembled-file start end)
+        assembled-file 
+        start 
+        end
+        cell)
     (if (ynb-reference-p)
         (progn
           (widen) ; So cell references will be found
           (setq assembled-file (ynb-assemble-cell t)))
       (save-excursion
-        (goto-char (ynb-cell-start))
-        (setq start (point))
-        (if (not (setq end (ynb-output-p)))
-            (setq end (ynb-cell-end))))
-      (progn
 	(goto-char (ynb-cell-start))
         ;; Now I want to skip over any blank lines at the beginning of the cell
 	(beginning-of-line)


(defvar ynb-source-buffer nil
  "Buffer from which ynb-collate-cells works.")

(defconst ynb-temp-suffix 0
  "Temporary filename suffix.  Incremented by 1 for each filename.")

	    (goto-char end)
	    (while (looking-at "^ *$") (forward-line -1))
	    (end-of-line)
	    (setq end (point))))))
    (if assembled-file
        (progn
          (set-buffer (find-file-noselect assembled-file))
          (setq cell (buffer-substring-no-properties (point-min) (point-max))))
      (setq cell (buffer-substring-no-properties start end)))
    (ynb-send-block cell tex)))

(defun ynb-single-command-get-output (string tex)
  "Send a command to the Yacas process."
  (let ((output)
        (end))
    (setq string (yacas-strip-string string))
    (yacas-start)
    (save-excursion
      (set-buffer (process-buffer inferior-yacas-process))
      (goto-char (point-max))
      (insert string)
      (setq yacas-input-end (point))
      (comint-send-input))
    (while (not (yacas-new-prompt-p))
      (sit-for 0.100))
    (if tex
        (yacas-single-command ynb-tex-string))
    (save-excursion
      (set-buffer (process-buffer inferior-yacas-process))
      (goto-char (point-max))
      (if tex
          (progn
            (re-search-backward yacas-input-prompt-string)
            (re-search-backward "\$\"")
            (setq end (point))
            (goto-char (1+ yacas-input-end))
;            (re-search-backward yacas-output-prompt-string)
            (re-search-forward "\"")
            (forward-char 1)
            (setq output 
                  (concat "$$"
                          (buffer-substring-no-properties (point) end)
                          "$$")))
        (forward-line -1)
        (end-of-line)
        (forward-char 1)
        (setq end (point))
;        (re-search-backward (concat "^" yacas-output-prompt-string))
;        (goto-char (match-end 0))
        (goto-char (1+ yacas-input-end))
        (setq output 
              (concat 
               (make-string (length yacas-output-prompt-string) ?\ )
               (buffer-substring-no-properties (point) end)))
        (setq output (replace-regexp-in-string "Out>" "    " output))))
    (if (and tex (not (string= ynb-cell-output "")))
        (setq ynb-cell-output (concat ynb-cell-output "\n" output))
      (setq ynb-cell-output (concat ynb-cell-output output)))))

(defun ynb-send-block (stuff &optional tex)
  "Send a block of code to Yacas."
  (yacas-start)
  (setq ynb-cell-output "")
  (while (string-match ";" stuff)
    (setq end (1+ (string-match ";" stuff)))
    (ynb-single-command-get-output (substring stuff 0 end) tex)
    (setq stuff (substring stuff end))))

(defun ynb-get-output (tex)
  "Insert last output from Yacas.
Assumes point in cell.  Output inserted at end of cell."
  (goto-char (ynb-cell-end))
  (forward-line 1)		 ; Insert marker before output
  (open-line 1)
