 
 ;;; @@ 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)

	    (goto-char end)
	    (while (looking-at "^ *$") (forward-line -1))
	    (end-of-line)
	    (setq end (point))))
	)
      )
    (if assembled-file
	; Loading the assembled file will give the result of True;
	;(yacas-string (concat "Load\(\"" assembled-file "\"\);"))
	(let ((assembled-file-buffer) (assembled-file-buffer-contents))
	  (save-excursion
	    (set-buffer (find-file-noselect assembled-file))
	    (setq assembled-file-buffer-contents 
		  (buffer-substring-no-properties (point-min) (point-max))))
	  (setq assembled-file-buffer-contents 
		(yacas-replace-chars-in-string (string-to-char "\n")
				      (string-to-char " ") 
				      assembled-file-buffer-contents))
	  (yacas-string (concat "[" assembled-file-buffer-contents "];")))
      (yacas-region start end)
      )))


(defun ynb-copy-last-tex-output ()
  "Copy the last output from Yacas to the kill-ring,
removing the beginning \"$ and ending $\";"
  (interactive)
  (let ((out-start)
	(out-end)
	(old-buffer (current-buffer))
        (yacas-shell (if  (get-buffer-process (current-buffer))
                         (current-buffer)
                         (get-buffer "*Yacas*"))))
    (yacas-wait)
    (if (null yacas-shell)
        (message "No Yacas output buffer")
      (pop-to-buffer yacas-shell)
      (save-excursion
	(end-of-buffer)
	(re-search-backward yacas-input-prompt-string)
	(re-search-backward "\$\"")
	(setq out-end (point))
	(re-search-backward yacas-output-prompt-string)
	(re-search-forward "\"")
	(forward-char 1)
	(setq out-start (point))
	(copy-region-as-kill out-start out-end)))
    (pop-to-buffer old-buffer)))


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

  (setq texmode-map text-mode-map)
  (defun texmode () (text-mode))))

(defvar yacas-notebook-mode-map nil
  "Keymap used in yacas-notebook-mode.")

(if yacas-notebook-mode-map ()
  (let ((map (copy-keymap texmode-map)))
    (define-key map "\C-c+" 'ynb-forward-cell)
    (define-key map "\C-c-" 'ynb-backward-cell)
    (define-key map "\C-c\C-va" 'ynb-update-all)
    (define-key map "\C-c\C-vA" 'ynb-tex-update-all)
    (define-key map "\C-c\C-vq" 'ynb-eval-init)
    (define-key map "\C-c\C-vi" 'ynb-update-init)
    (define-key map "\C-c\C-vI" 'ynb-tex-update-init)
    (define-key map "\C-c?" 'ynb-info-help)
    (define-key map "\C-c\C-u" nil)
    (define-key map "\C-c\C-o" 'ynb-create-cell)
    (define-key map "\C-c\C-ul" 'ynb-replace-line)
    (define-key map "\C-c\C-uL" 'ynb-replace-line-with-tex)
    (define-key map "\C-c\C-s" 'ynb-send-cell)
    (define-key map "\C-c\C-uc" 'ynb-update-cell)
    (define-key map "\C-c\C-uC" 'ynb-tex-update-cell)
    (define-key map "\C-c\C-d" 'ynb-delete-output)
    (define-key map "\C-c\C-q" 'ynb-toggle-init)
    (define-key map "\C-c\C-x" 'ynb-package-part)
    (define-key map "\C-c@" 'ynb-assemble)
    (define-key map [(control c) (control tab)] 
      'ynb-insert-complete-name)
    (setq yacas-notebook-mode-map map)))

(define-derived-mode yacas-notebook-mode texmode "Yacas-Notebook"
  "This is a mode intended to allow the user to write documents that
