Index: /branches/kapa-mods-2007-05/Ohana/src/gastro/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/gastro/src/plotstuff.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/gastro/src/plotstuff.c	(revision 13402)
@@ -13,100 +13,31 @@
 int open_graph (int N) {
 
-# ifdef ANSI
-#   define F_SETFL      4   
-#   define O_NONBLOCK 0200000  
-#   define       AF_UNIX         1          
-#   define       SOCK_STREAM     1          
-#define ENOENT          2       /* No such file or directory    */
-# endif /* ANSI */
-
-  int InitSocket, status;
-  socklen_t addreslen;
-  struct sockaddr_un Address;
-  char temp[100];
-  char socket_name[100];
+  char name[100];
   
   active = N;
-  sprintf (socket_name, "/tmp/kapa%d", N);
-  sprintf (temp, "rm -f %s", socket_name);
-  system (temp);
-    
-  strcpy (Address.sun_path, socket_name); 
-  Address.sun_family = AF_UNIX; 
-  InitSocket = socket (AF_UNIX, SOCK_STREAM, 0); 
-  status = bind (InitSocket, (struct sockaddr *) &Address, sizeof (Address));
-  status = listen (InitSocket, 1);
-  
-  sprintf (temp, "kapa %s &", socket_name);
-# ifndef DEBUG
-  system (temp);
-# else  
-  fprintf (stderr, "start kapa, press return: %s\n", temp);
-  fscanf (stdin, "%d", &i);
-# endif
-  
-  addreslen =  sizeof (Address);
-  Xgraph[N] = accept (InitSocket, (struct sockaddr *) &Address, &addreslen);
+
+  sprintf (name, "gastro [%d]", N);
+  Xgraph[N] = KapaOpen ("kapa2", name);
+
   if (Xgraph[N] < 0) {
     fprintf (stderr, "error starting kapa\n");
     return (FALSE);
   }
-  else {
-    return (TRUE);
-  }
-  
+  return (TRUE);
 }
 
 void DonePlotting (Graphdata *graphmode, int N) {
-  char buffer[65], buffer2[65];
 
-  write (Xgraph[N], "DBOX", 4);
-  sprintf (buffer, "%f %f %f %f ", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
-  sprintf (buffer2, "NBYTES: %6d ", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-
-  sprintf (buffer, "%s %s %s", "2222", "2222", "2222");
-  sprintf (buffer2, "NBYTES: %6d ", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
+  if (Xgraph[N] == 0) return;
+  KapaBox (Xgraph[N], graphmode);
+  return;
 }
 
 void PrepPlotting (int Npts, Graphdata *graphmode, int N) {
 
-  int i, status;
-  char buffer[128], buffer2[128];
-
   active = N;
   if (Npts < 1) return;
 
-  /* test Xgraph[N], flush junk from pipe */
-  signal (SIGPIPE, XDead);
-  fcntl (Xgraph[N], F_SETFL,  O_NONBLOCK); 
-  for (i = 0; (read (Xgraph[N], buffer, 64) > 0) && (i < 20); i++);
-  fcntl (Xgraph[N], F_SETFL, !O_NONBLOCK); 
-  
-  if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  
-  /* tell kapa to look for the incoming image */
-  status = write (Xgraph[N], "PLOT", 4); 
-
-  /* send Xgraph[N] the plot details */
-  sprintf (buffer, "%8d %8d %d %d %d %d %f %f ", 
-	   Npts, graphmode[0].style, 
-	   graphmode[0].ptype, graphmode[0].ltype, 
-	   graphmode[0].etype, graphmode[0].color, 
-	   graphmode[0].lweight, graphmode[0].size);
-  sprintf (buffer2, "NBYTES: %6d ", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-  
-  sprintf (buffer, "%f %f %f %f ", 
-	   graphmode[0].xmin, graphmode[0].xmax, 
-	   graphmode[0].ymin, graphmode[0].ymax);
-  sprintf (buffer2, "NBYTES: %6d ", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-
+  KapaPrepPlot (Xgraph[N], Npts, graphmode);
 }
 
@@ -116,9 +47,6 @@
 
   if (Npts < 1) return;
-
   active = N;
-  Nbytes = Npts * sizeof (float);
-  write (Xgraph[N], vect, Nbytes);
-
+  KapaPlotVector (Xgraph[N], Npts, vect);
 }
 
@@ -135,8 +63,7 @@
   
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  
-  write (Xgraph[N], "ERAS", 4);
+  KapaClear (Xgraph[N], TRUE);
+}
 
-}
 /* include these lines to plot a pair of vectors: 
 
Index: /branches/kapa-mods-2007-05/Ohana/src/gastro2/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/gastro2/src/plotstuff.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/gastro2/src/plotstuff.c	(revision 13402)
@@ -13,47 +13,16 @@
 int open_graph (int N) {
 
-# ifdef ANSI
-#   define F_SETFL      4   
-#   define O_NONBLOCK 0200000  
-#   define       AF_UNIX         1          
-#   define       SOCK_STREAM     1          
-#define ENOENT          2       /* No such file or directory    */
-# endif /* ANSI */
-
-  int InitSocket, status;
-  socklen_t addreslen;
-  struct sockaddr_un Address;
-  char temp[100];
-  char socket_name[100];
+  char name[100];
   
   active = N;
-  sprintf (socket_name, "/tmp/kapa%d", N);
-  sprintf (temp, "rm -f %s", socket_name);
-  system (temp);
-    
-  strcpy (Address.sun_path, socket_name); 
-  Address.sun_family = AF_UNIX; 
-  InitSocket = socket (AF_UNIX, SOCK_STREAM, 0); 
-  status = bind (InitSocket, (struct sockaddr *) &Address, sizeof (Address));
-  status = listen (InitSocket, 1);
-  
-  sprintf (temp, "kapa %s -name %d &", socket_name, N);
-# ifndef DEBUG
-  system (temp);
-# else  
-  fprintf (stderr, "start kapa, press return: %s\n", temp);
-  fscanf (stdin, "%d", &i);
-# endif
-  
-  addreslen =  sizeof (Address);
-  Xgraph[N] = accept (InitSocket, (struct sockaddr *) &Address, &addreslen);
+
+  sprintf (name, "gastro [%d]", N);
+  Xgraph[N] = KapaOpen ("kapa2", name);
+
   if (Xgraph[N] < 0) {
     fprintf (stderr, "error starting kapa\n");
     return (FALSE);
   }
-  else {
-    return (TRUE);
-  }
-  
+  return (TRUE);
 }
 
@@ -61,14 +30,5 @@
   char buffer[65], buffer2[65];
 
-  write (Xgraph[N], "DBOX", 4);
-  sprintf (buffer, "%f %f %f %f", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
-  sprintf (buffer2, "NBYTES: %6d", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-
-  sprintf (buffer, "%s %s %s", "2222", "2222", "2222");
-  sprintf (buffer2, "NBYTES: %6d", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
+  KapaBox (Xgraph[N], graphmode);
 }
 
@@ -81,32 +41,5 @@
   if (Npts < 1) return;
 
-  /* test Xgraph[N], flush junk from pipe */
-  signal (SIGPIPE, XDead);
-  fcntl (Xgraph[N], F_SETFL,  O_NONBLOCK); 
-  for (i = 0; (read (Xgraph[N], buffer, 64) > 0) && (i < 20); i++);
-  fcntl (Xgraph[N], F_SETFL, !O_NONBLOCK); 
-  
-  if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  
-  /* tell kapa to look for the incoming image */
-  status = write (Xgraph[N], "PLOT", 4); 
-
-  /* send Xgraph[N] the plot details */
-  sprintf (buffer, "%8d %8d %d %d %d %d %d %f %f", 
-	   Npts, graphmode[0].style, 
-	   graphmode[0].ptype, graphmode[0].ltype, 
-	   graphmode[0].etype, graphmode[0].ebar, graphmode[0].color, 
-	   graphmode[0].lweight, graphmode[0].size);
-  sprintf (buffer2, "NBYTES: %6d", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-  
-  sprintf (buffer, "%f %f %f %f", 
-	   graphmode[0].xmin, graphmode[0].xmax, 
-	   graphmode[0].ymin, graphmode[0].ymax);
-  sprintf (buffer2, "NBYTES: %6d", (unsigned int) strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-
+  KapaPrepPlot (Xgraph[N], Npts, graphmode);
 }
 
@@ -116,9 +49,6 @@
 
   if (Npts < 1) return;
-
   active = N;
-  Nbytes = Npts * sizeof (float);
-  write (Xgraph[N], vect, Nbytes);
-
+  KapaPlotVector (Xgraph[N], Npts, vect);
 }
 
@@ -135,8 +65,7 @@
   
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  
-  write (Xgraph[N], "ERAS", 4);
+  KapaClear (Xgraph[N], TRUE);
+}
 
-}
 /* include these lines to plot a pair of vectors: 
 
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h	(revision 13402)
@@ -61,4 +61,5 @@
 int           ErasePlots          PROTO((void));
 int           EraseSections       PROTO((void));
+int           EraseImage          PROTO((void));
 
 int           LoadVectorData      PROTO((int sock, KapaGraphWidget *graph, int N, char *type));
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c	(revision 13402)
@@ -15,5 +15,5 @@
 
   int status;
-  char buffer[32];
+  char word[5];
 
   // check if we have a valid connection. if not, see if we can get one
@@ -26,6 +26,6 @@
 
   /***** read (4 byte) message word from socket ****/
-  status = read (sock, buffer, 4);
-  buffer[4] = 0;
+  status = read (sock, word, 4);
+  word[4] = 0;
   switch (status) {
   case -1:                          /* no input from pipe: continue */
@@ -49,6 +49,6 @@
   /***** handle different messages ****/
   if (ACTIVE_CURSOR) {
-    if (strcmp (buffer, "NCUR")) {
-      fprintf (stderr, "wrong end message %s\n", buffer);
+    if (strcmp (word, "NCUR")) {
+      fprintf (stderr, "wrong end message %s\n", word);
       return (TRUE);
     }
@@ -57,151 +57,183 @@
   } 
 
-  if (!strcmp (buffer, "QUIT")) return (FALSE);
-  
-  if (!strcmp (buffer, "CURS")) {
+  if (!strcmp (word, "QUIT")) return (FALSE);
+  
+  if (!strcmp (word, "CURS")) {
     ACTIVE_CURSOR = TRUE;
     return (TRUE);
   }
   
-  if (!strcmp (buffer, "PSIT")) {
+  if (!strcmp (word, "PSIT")) {
     status = PScommand (sock);
-    write (sock, "DONE", 4);
-    return (status);
-  }
-  
-  if (!strcmp (buffer, "PNGF")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  if (!strcmp (word, "PNGF")) {
     status = PNGit (sock);
-    write (sock, "DONE", 4);
-    return (status);
-  }
-  
-  if (!strcmp (buffer, "PPMF")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  if (!strcmp (word, "PPMF")) {
     status = PPMit (sock);
-    write (sock, "DONE", 4);
-    return (status);
-  }
-  
-  if (!strcmp (buffer, "DBOX")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  if (!strcmp (word, "DBOX")) {
     status = LoadFrame (sock);
-    return (status);
-  }
-  
-  if (!strcmp (buffer, "PLOT")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  if (!strcmp (word, "PLOT")) {
     status = LoadObject (sock);
-    return (status);
-  }
-  
-  if (!strcmp (buffer, "LABL")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  if (!strcmp (word, "LABL")) {
     status = LoadLabels (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "PTXT")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "PTXT")) {
     status = LoadTextlines (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "RSIZ")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "RSIZ")) {
     status = Resize (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "LIMS")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "LIMS")) {
     GetLimits (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "SLIM")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "SLIM")) {
     status = SetLimits (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "SSEC")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "SSEC")) {
     status = SetSection (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "LSEC")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "LSEC")) {
     status = ListSection (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "DSEC")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "DSEC")) {
     status = DefineSection (sock);
-    return (TRUE);
-  }
-  
-  if (!strcmp (buffer, "FONT")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
+  if (!strcmp (word, "FONT")) {
     status = SetFont (sock);
+    KiiSendCommand (sock, 4, "DONE");
     return (TRUE);
   }
   
   /* Erase Section */
-  if (!strcmp (buffer, "ERSC")) {
+  if (!strcmp (word, "ERSC")) {
     status = EraseCurrentPlot ();
-    return (status);
-  }
-  
-  /* Erase Section */
-  if (!strcmp (buffer, "ERAS")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  /* Erase Plots */
+  if (!strcmp (word, "ERSP")) {
     status = ErasePlots ();
-    return (status);
-  }
-  
-  /* Don't Erase Section */
-  if (!strcmp (buffer, "ERSS")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  
+  /* Erase Sections */
+  if (!strcmp (word, "ERSS")) {
     status = EraseSections ();
-    return (status);
-  }
-  
-
-  if (!strcmp (buffer, "READ")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  /* Erase Image */
+  if (!strcmp (word, "ERSI")) {
+    status = EraseImage ();
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  /* Erase Overlay for this section */
+  if (!strcmp (word, "ERSO")) {
+    status = EraseOverlay (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "READ")) {
     status = LoadPicture (sock);
-    return (status);
-  }
-
-  // XXX duplicate Command word
-  if (!strcmp (buffer, "ERAS")) {
-    status = EraseOverlay (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "LOAD")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "LOAD")) {
     status = LoadOverlay (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "TICK")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  /* this is not currently used?
+     tickmarks were equivalent to the overlays, but fixed wrt the window.
+     this functionality is probably supplanted by the graph 
+  if (!strcmp (word, "TICK")) {
     status = LoadTickmarks (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "SAVE")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+  */
+
+  if (!strcmp (word, "SAVE")) {
     status = SaveOverlay (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "CSVE")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "CSVE")) {
     status = CSaveOverlay (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "JPEG")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "JPEG")) {
     status = JPEGit24 (sock);
-    write (sock, "DONE", 4);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "CENT")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "CENT")) {
     status = Center (sock);
-    return (status);
-  }
-
-  if (!strcmp (buffer, "NPIX")) {
+    KiiSendCommand (sock, 4, "DONE");
+    return (status);
+  }
+
+  if (!strcmp (word, "NPIX")) {
     GetPixelCount (sock);
-    return (TRUE);
-  }
-
-  fprintf (stderr, "unknown signal %s\n", buffer);
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+
+  fprintf (stderr, "unknown signal %s\n", word);
 
   return (TRUE);
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseImage.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseImage.c	(revision 13402)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseImage.c	(revision 13402)
@@ -0,0 +1,21 @@
+# include "Ximage.h"
+
+int EraseImage () {
+
+  char buffer[256];
+  int i, status, N;
+  Graphic *graphic;
+  Section *section;
+  KapaImageWidget *image;
+
+  graphic = GetGraphic();
+  section = GetActiveSection();
+  image = section->image;
+  if (image == NULL) return (TRUE);
+
+  FreeImage (image);
+  section->image = NULL;
+
+  if (USE_XWINDOW) Refresh ();
+  return (TRUE);
+}
Index: /branches/kapa-mods-2007-05/Ohana/src/kii/event/CheckPipe.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kii/event/CheckPipe.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/kii/event/CheckPipe.c	(revision 13402)
@@ -123,4 +123,5 @@
   }
 
+  /* is this no longer used ?
   if (!strcmp (buffer, "NPIX")) {
     sprintf (buffer, "NPIX:   %8d", layout[0].Npixels);
@@ -128,4 +129,5 @@
     return (TRUE);
   }
+  */
 
   fprintf (stderr, "unknown signal %s\n", buffer);
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/doc/api.txt
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/doc/api.txt	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/doc/api.txt	(revision 13402)
@@ -1,32 +1,35 @@
 
-COMM | Kapa Function  | libkapa API 
------------------------------------
-NCUR | !ACTIVE_CURSOR | KiiCursorOff
-QUIT | Quit           |  
-CURS
-PSIT
-PNGF
-PPMF
-DBOX
-PLOT
-LABL
-PTXT
-RSIZ
-LIMS
-SLIM
-SSEC
-LSEC
-DSEC
-FONT
-ERSC
-ERAS
-ERSS
-READ
-ERAS
-LOAD
-TICK
-SAVE
-CSVE
-JPEG
-CENT
-NPIX
+COMM | Kapa Function  	| libkapa file  | libkapa API 
+-----------------------------------------------------
+DBOX | LoadFrame      	| KapaWindow.c  | KapaBox
+ERSP | ErasePlots       | KapaWindow.c  | KapaClearPlots
+ERSS | EraseSections    | KapaWindow.c  | KapaClearSections
+ERSI | EraseImage       | KapaWindow.c  | KapaClearImage
+ERSC | EraseCurrentPlot | KapaWindow.c  | KapaClearCurrentPlot
+LSEC | ListSection    	| KapaWindow.c  | KapaGetSection
+PNGF | PNGit          	| KiiConvert.c  | KapaPNG
+PPMF | PPMit          	| KiiConvert.c  | KapaPPM
+PLOT | LoadObject     	| KapaWindow.c  | KapaPrepPlot
+SSEC | SetSection     	| KapaWindow.c  | KapaSelectSection
+LABL | LoadLabels     	| KapaWindow.c  | KapaSendLabel
+PTXT | LoadTextlines  	| KapaWindow.c  | KapaSendTextline
+FONT | SetFont        	| KapaWindow.c  | KapaSetFont
+SLIM | SetLimits      	| KapaWindow.c  | KapaSetLimits
+DSEC | DefineSection  	| KapaWindow.c  | KapaSetSection
+CENT | Center           | KapaWindow.c  | KiiCenter
+QUIT | Quit           	| KiiOpen.c     | KiiClose
+NCUR | !ACTIVE_CURSOR 	| KiiCursor.c   | KiiCursorOff
+CURS | +ACTIVE_CURSOR 	| KiiCursor.c   | KiiCursorOn
+ERSO | EraseOverlay     | KiiOverlay.c  | KiiEraseOverlay
+JPEG | JPEGit24         | KiiConvert.c  | KiiJPEG
+LOAD | LoadOverlay      | KiiOverlay.c  | KiiLoadOverlay
+READ | LoadPicture      | KiiPicture.c  | KiiNewPicture1D
+READ | LoadPicture      | KiiPicture.c  | KiiNewPicture2D
+PSIT | PScommand      	| KiiConvert.c  | KiiPS
+RSIZ | Resize         	| KapaWindow.c  | KiiResize
+SAVE | SaveOverlay      | KiiOverlay.c  | KiiSaveOverlay
+CSVE | CSaveOverlay     | KiiOverlay.c  | KiiSaveOverlay
+LIMS | GetLimits      	| KapaWindow.c  | KapaGetLimits
+
+TICK | LoadTickmarks    | XXX not used
+NPIX | GetPixelCount    | XXX not used?
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h	(revision 13402)
@@ -140,5 +140,8 @@
 int KiiCenter (int fd, double x, double y, int zoom);
 int KapaBox (int fd, Graphdata *graphdata);
-int KapaClear (int fd, int ClearSection);
+int KapaClearCurrentPlot (int fd);
+int KapaClearPlots (int fd);
+int KapaClearSections (int fd);
+int KapaClearImage (int fd);
 int KapaInitGraph (Graphdata *graphdata);
 int KapaPrepPlot (int fd, int Npts, Graphdata *graphmode);
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/IOfuncs.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/IOfuncs.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/IOfuncs.c	(revision 13402)
@@ -116,16 +116,21 @@
 
   ALLOCATE (message, char, length + 1);
+  fcntl (device, F_SETFL, !O_NONBLOCK);  // block until we get the response??
 
   /* read Nbytes from the device */
   status = read (device, message, length);
-  if (status != length) fprintf (stderr, "Kii/Kapa comm error\n");
-  message[status] = 0;
-  /* make the string easy to parse */
+  fcntl (device, F_SETFL, O_NONBLOCK);  // unblock
+
+  if (status != length) {
+      fprintf (stderr, "Kii/Kapa comm error\n");
+      return (0);
+  }
+  message[status] = 0; // make the string easy to parse
 
   /* scan the incoming message */
   va_start (argp, format);
-  status = vsscanf (message, format, argp);
+  vsscanf (message, format, argp);
   va_end (argp);
 
-  return (status);
+  return (1);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KapaWindow.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KapaWindow.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KapaWindow.c	(revision 13402)
@@ -1,3 +1,16 @@
 # include <kapa_internal.h>
+
+int KiiCenter (int fd, double x, double y, int zoom) {
+
+  KiiSendCommand (fd, 4, "CENT");
+  KiiSendMessage (fd, "%8.3f %8.3f %8d ", x, y, zoom);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
+  return (TRUE);
+}
 
 int KiiResize (int fd, int Nx, int Ny) {
@@ -5,11 +18,10 @@
   KiiSendCommand (fd, 4, "RSIZ");
   KiiSendMessage (fd, "%d %d", Nx, Ny); 
-  return (TRUE);
-}
-
-int KiiCenter (int fd, double x, double y, int zoom) {
-
-  KiiSendCommand (fd, 4, "CENT");
-  KiiSendMessage (fd, "%8.3f %8.3f %8d ", x, y, zoom);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -23,13 +35,53 @@
 
   KiiSendMessage (fd, "%s %s %s", graphdata[0].axis, graphdata[0].labels, graphdata[0].ticks);
-  return (TRUE);
-}
-
-int KapaClear (int fd, int ClearSection) {
-
-  if (ClearSection) {
-    KiiSendCommand (fd, 4, "ERAS");
-  } else {
-    KiiSendCommand (fd, 4, "ERSS");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
+  return (TRUE);
+}
+
+int KapaClearCurrentPlot (int fd) {
+  
+  KiiSendCommand (fd, 4, "ERSC");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  return (TRUE);
+}
+
+int KapaClearPlots (int fd) {
+  
+  KiiSendCommand (fd, 4, "ERSP");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  return (TRUE);
+}
+
+int KapaClearSections (int fd) {
+  
+  KiiSendCommand (fd, 4, "ERSS");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  return (TRUE);
+}
+
+int KapaClearImage (int fd) {
+  
+  KiiSendCommand (fd, 4, "ERSI");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
   }
   return (TRUE);
@@ -76,4 +128,11 @@
 		  graphmode[0].xmin, graphmode[0].xmax, 
 		  graphmode[0].ymin, graphmode[0].ymax);
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -93,4 +152,11 @@
   KiiSendCommand (fd, 16, "%s", name);
   KiiSendCommand (fd, 16, "%d", size);
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -101,4 +167,11 @@
   KiiSendMessage (fd, "%6d", mode);
   KiiSendData (fd, string, strlen(string));
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -109,4 +182,11 @@
   KiiSendMessage (fd, "%f %f %f", x, y, angle);
   KiiSendData (fd, string, strlen(string));
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -116,4 +196,26 @@
   KiiSendCommand (fd, 4, "SLIM");
   KiiSendMessage (fd, "%g %g %g %g ", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
+  return (TRUE);
+}
+
+// for now, this just gets the dimensions
+int KapaGetLimits (int fd, float *dx, float *dy) {
+
+  KiiSendCommand (kapa, 4, "LIMS"); 
+  KiiScanMessage (kapa, "%lf %lf", dx, dy); 
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -128,4 +230,11 @@
 		  section[0].dx,
 		  section[0].dy);
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -135,4 +244,11 @@
   KiiSendCommand (fd, 4, "SSEC");
   KiiSendMessage (fd, "%s", name);
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
   return (TRUE);
 }
@@ -142,4 +258,11 @@
   KiiSendCommand (fd, 4, "LSEC");
   KiiSendMessage (fd, "%s", name);
-  return (TRUE);
-}
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
+  return (TRUE);
+}
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiConvert.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiConvert.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiConvert.c	(revision 13402)
@@ -9,5 +9,9 @@
 
   /* block for the response, which says the operation completed */
-  read (fd, buffer, 4);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
@@ -21,5 +25,9 @@
 
   /* block for the response, which says the operation completed */
-  read (fd, buffer, 4);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
@@ -33,5 +41,9 @@
 
   /* block for the response, which says the operation completed */
-  read (fd, buffer, 4);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
@@ -47,5 +59,9 @@
 
   /* block for the response, which says the operation completed */
-  read (fd, buffer, 4);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiCursor.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiCursor.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiCursor.c	(revision 13402)
@@ -2,10 +2,20 @@
 
 int KiiCursorOn (int fd) {
-  write (fd, "CURS", 4); /* ask Source to look for the keystrokes */
+  KiiSendCommand (fd, 4, "CURS");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
 
 int KiiCursorOff (int fd) {
-  write (fd, "NCUR", 4); /* ask Source to look for the keystrokes */
+  KiiSendCommand (fd, 4, "NCUR");
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiOverlay.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiOverlay.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiOverlay.c	(revision 13402)
@@ -88,4 +88,9 @@
      conversions back and forth
   */
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
@@ -97,7 +102,12 @@
   KiiSelectOverlay (overname, &n);
     
-  KiiSendCommand (fd, 4, "ERAS");
+  KiiSendCommand (fd, 4, "ERSO");
   KiiSendCommand (fd, 16, "OVERLAY %7d ", n);
 
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
@@ -116,4 +126,10 @@
 
   KiiSendMessage (fd, "FILE: %d %s", n, file);
+
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiPicture.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiPicture.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KiiPicture.c	(revision 13402)
@@ -58,4 +58,9 @@
   free (outbuffer);
 
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
@@ -125,4 +130,9 @@
   free (outbuffer);
 
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
   return (TRUE);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.astro/region.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.astro/region.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.astro/region.c	(revision 13402)
@@ -5,5 +5,5 @@
   char string[256];
   double Ra, Dec, Radius;
-  double dx, dy;
+  float dx, dy;
   int N, kapa;
   char *name;
@@ -67,6 +67,5 @@
   
   /* ask kapa for coordinate limits, so get the right aspect ratio */
-  KiiSendCommand (kapa, 4, "LIMS"); 
-  KiiScanMessage (kapa, "%lf %lf", &dx, &dy); 
+  KapaGetLimits (kapa, &dx, &dy);
   dx = fabs (dx);
   dy = fabs (dy); 
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/PlotVectors.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/PlotVectors.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/PlotVectors.c	(revision 13402)
@@ -6,17 +6,5 @@
 
   if (!GetGraph (NULL, &kapa, NULL)) return (FALSE);
-  
-  /* tell kapa to look for the incoming image */
-  KiiSendCommand (kapa, 4, "PLOT"); 
-  
-  /* send kapa the plot details */
-  KiiSendMessage (kapa, "%8d %8d %d %d %d %d %d %f %f", 
-		  Npts, graphmode[0].style, 
-		  graphmode[0].ptype, graphmode[0].ltype, 
-		  graphmode[0].etype, graphmode[0].ebar, graphmode[0].color, 
-		  graphmode[0].lweight, graphmode[0].size);
-  KiiSendMessage (kapa, "%g %g %g %g", 
-		  graphmode[0].xmin, graphmode[0].xmax, 
-		  graphmode[0].ymin, graphmode[0].ymax);
+  KapaPrepPlot (kapa, Npts, graphmode);
   return (TRUE);
 }
@@ -24,8 +12,5 @@
 int PlotVector (int Npts, float *values) {
 
-  int Nbytes;
-
-  Nbytes = Npts * sizeof (float);
-  write (kapa, values, Nbytes);
+  KapaPlotVector (kapa, Npts, values);
   return (TRUE);
 }
@@ -33,20 +18,22 @@
 int PlotVectorPair (int N, float *xValues, float *yValues, Graphdata *graphmode) {
 
-    PrepPlotting (N, graphmode);
-    PlotVector (N, xValues);
-    PlotVector (N, yValues);
+  PrepPlotting (N, graphmode);
+  PlotVector (N, xValues);
+  PlotVector (N, yValues);
 
-    return (TRUE);
+  return (TRUE);
 }
 
 int PlotVectorTriplet (int N, float *xValues, float *yValues, float *zValues, Graphdata *graphmode) {
 
-    PrepPlotting (N, graphmode);
-    PlotVector (N, xValues);
-    PlotVector (N, yValues);
-    PlotVector (N, zValues);
+  PrepPlotting (N, graphmode);
+  PlotVector (N, xValues);
+  PlotVector (N, yValues);
+  PlotVector (N, zValues);
 
-    return (TRUE);
+  return (TRUE);
 }
 
 /* we don't use GetGraph in PlotVector because it flushes the connection */
+/* PlotVector currently operates on the static identified socket 
+   rather than the currently active socket -- must be preceeded by PrepPlotting */
Index: /branches/kapa-mods-2007-05/Ohana/src/relastro/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/relastro/src/plotstuff.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/relastro/src/plotstuff.c	(revision 13402)
@@ -21,5 +21,9 @@
 int open_graph (int N) {
 
-  Xgraph[N] = KiiOpen ("kapa", NULL);
+  char name[100];
+
+  sprintf (name, "gastro [%d]", N);
+  Xgraph[N] = KapaOpen ("kapa2", name);
+
   if (Xgraph[N] < 0) {
     fprintf (stderr, "error starting kapa\n");
@@ -61,16 +65,5 @@
   KapaClear (Xgraph[N], TRUE);
 
-  KiiSendCommand (Xgraph[N], 4, "PLOT");
-  
-  /* send Xgraph the plot details */
-  KiiSendMessage (Xgraph[N], "%8d %8d %d %d %d %d %d %f %f", 
-		  Npts, graphmode[0].style, 
-		  graphmode[0].ptype, graphmode[0].ltype, 
-		  graphmode[0].etype, graphmode[0].ebar, graphmode[0].color, 
-		  graphmode[0].lweight, graphmode[0].size);
-  KiiSendMessage (Xgraph[N], "%g %g %g %g", 
-		  graphmode[0].xmin, graphmode[0].xmax, 
-		  graphmode[0].ymin, graphmode[0].ymax);
-
+  KapaPrepPlot (Xgraph[N], Npts, graphmode);
   return;
 }
@@ -95,6 +88,5 @@
   }
 
-  Nbytes = Npts * sizeof (float);
-  write (Xgraph[N], values, Nbytes);
+  KapaPlotVector (Xgraph[N], Npts, values);
   free (values);
   return;
Index: /branches/kapa-mods-2007-05/Ohana/src/relphot/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/relphot/src/plotstuff.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/relphot/src/plotstuff.c	(revision 13402)
@@ -21,5 +21,9 @@
 int open_graph (int N) {
 
-  Xgraph[N] = KiiOpen ("kapa", NULL);
+  char name[100];
+
+  sprintf (name, "gastro [%d]", N);
+  Xgraph[N] = KapaOpen ("kapa2", name);
+
   if (Xgraph[N] < 0) {
     fprintf (stderr, "error starting kapa\n");
@@ -61,16 +65,5 @@
   KapaClear (Xgraph[N], TRUE);
 
-  KiiSendCommand (Xgraph[N], 4, "PLOT");
-  
-  /* send Xgraph the plot details */
-  KiiSendMessage (Xgraph[N], "%8d %8d %d %d %d %d %d %f %f", 
-		  Npts, graphmode[0].style, 
-		  graphmode[0].ptype, graphmode[0].ltype, 
-		  graphmode[0].etype, graphmode[0].ebar, graphmode[0].color, 
-		  graphmode[0].lweight, graphmode[0].size);
-  KiiSendMessage (Xgraph[N], "%g %g %g %g", 
-		  graphmode[0].xmin, graphmode[0].xmax, 
-		  graphmode[0].ymin, graphmode[0].ymax);
-
+  KapaPrepPlot (Xgraph[N], Npts, graphmode);
   return;
 }
@@ -95,6 +88,5 @@
   }
 
-  Nbytes = Npts * sizeof (float);
-  write (Xgraph[N], values, Nbytes);
+  KapaPlotVector (Xgraph[N], Npts, values);
   free (values);
   return;
Index: /branches/kapa-mods-2007-05/Ohana/src/uniphot/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/uniphot/src/plotstuff.c	(revision 13401)
+++ /branches/kapa-mods-2007-05/Ohana/src/uniphot/src/plotstuff.c	(revision 13402)
@@ -13,120 +13,40 @@
 int open_graph (int N) {
 
-  int i, InitSocket, status, addreslen;
-  struct sockaddr_un Address;
-  char temp[100], *display_name;
-  char socket_name[100];
-  FILE *f;
+  char name[100];
   
   active = N;
-  sprintf (socket_name, "/tmp/kapa%d", N);
-  sprintf (temp, "rm -f %s", socket_name);
-  system (temp);
-    
-  strcpy (Address.sun_path, socket_name); 
-  Address.sun_family = AF_UNIX; 
-  InitSocket = socket (AF_UNIX, SOCK_STREAM, 0); 
-  status = bind (InitSocket, &Address, sizeof (Address));
-  status = listen (InitSocket, 1);
-  
-  sprintf (temp, "kapa %s &", socket_name);
-# ifndef DEBUG
-  system (temp);
-# else  
-  fprintf (stderr, "start kapa, press return: %s\n", temp);
-  fscanf (stdin, "%d", &i);
-# endif
-  
-  addreslen =  sizeof (Address);
-  Xgraph[N] = accept (InitSocket, &Address, &addreslen);
+
+  sprintf (name, "gastro [%d]", N);
+  Xgraph[N] = KapaOpen ("kapa2", name);
+
   if (Xgraph[N] < 0) {
     fprintf (stderr, "error starting kapa\n");
     return (FALSE);
   }
-  else {
-    return (TRUE);
-  }
-  
+  return (TRUE);
 }
 
 void DonePlotting (Graphdata *graphmode, int N) {
-  char buffer[65], buffer2[65];
 
   if (Xgraph[N] == 0) return;
-
-  write (Xgraph[N], "DBOX", 4);
-  sprintf (buffer, "%f %f %f %f", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
-  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-
-  sprintf (buffer, "%s %s %s", "2222", "2222", "2222");
-  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-}
-
-void JpegPlot (Graphdata *graphmode, int N, char *filename) {
-  char buffer[65], buffer2[65];
-
-  if (Xgraph[N] == 0) return;
-
-  write (Xgraph[N], "PNGF", 4);
-  sprintf (buffer, "LEN: %11d", strlen (filename));
-  write (Xgraph[N], buffer, 16);
-  write (Xgraph[N], filename, strlen (filename));
-  read (Xgraph[N], buffer, 4);
-}
-
-void PSPlot (Graphdata *graphmode, int N, char *filename) {
-  char buffer[65], buffer2[65];
-
-  if (Xgraph[N] == 0) return;
-
-  write (Xgraph[N], "PSIT", 4);
-  sprintf (buffer, "LEN: %11d", strlen (filename));
-  write (Xgraph[N], buffer, 16);
-  write (Xgraph[N], filename, strlen (filename));
-  read (Xgraph[N], buffer, 4);
+  KapaBox (Xgraph[N], graphmode);
+  return;
 }
 
 void PrepPlotting (int Npts, Graphdata *graphmode, int N) {
 
-  int i, status;
-  char buffer[128], buffer2[128];
-
   active = N;
   if (Npts < 1) return;
 
-  if (Xgraph[N] < 1) if (!open_graph(N)) return;
+  KapaPrepPlot (Xgraph[N], Npts, graphmode);
+}
 
-  /* test Xgraph[N], flush junk from pipe */
-  signal (SIGPIPE, XDead);
-  fcntl (Xgraph[N], F_SETFL,  O_NONBLOCK); 
-  for (i = 0; (read (Xgraph[N], buffer, 64) > 0) && (i < 20); i++);
-  fcntl (Xgraph[N], F_SETFL, !O_NONBLOCK); 
-  
-  write (Xgraph[N], "ERAS", 4);
-  
-  /* tell kapa to look for the incoming image */
-  status = write (Xgraph[N], "PLOT", 4); 
+void PlotVector (int Npts, float *vect, int mode, int N) {
 
-  /* send Xgraph[N] the plot details */
-  sprintf (buffer, "%8d %8d %d %d %d %d %f %f", 
-	   Npts, graphmode[0].style, 
-	   graphmode[0].ptype, graphmode[0].ltype, 
-	   graphmode[0].etype, graphmode[0].color, 
-	   graphmode[0].lweight, graphmode[0].size);
-  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
-  
-  sprintf (buffer, "%f %f %f %f", 
-	   graphmode[0].xmin, graphmode[0].xmax, 
-	   graphmode[0].ymin, graphmode[0].ymax);
-  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
-  write (Xgraph[N], buffer2, 16);
-  write (Xgraph[N], buffer, strlen (buffer));
+  int Nbytes;
 
+  if (Npts < 1) return;
+  active = N;
+  KapaPlotVector (Xgraph[N], Npts, vect);
 }
 
@@ -137,27 +57,36 @@
   if (Xgraph[N] == 0) return;
 
-  write (Xgraph[N], "LABL", 4);
-  sprintf (buffer, " %6d %6d", strlen (string), 2);
-  write (Xgraph[N], buffer, 16);
-  write (Xgraph[N], string, strlen (string));
+  KapaSendLabel (Xgraph[N[, string, 4);
 }
 
-void PlotVector (int Npts, double *vect, int mode, int N) {
+void PlotReset (int N) {
 
-  float *values;
-  int i, Nbytes;
+  char buffer[128];
+  int i;
 
-  active = N;
-  if (Npts < 1) return;
+  /* test Xgraph[N], flush junk from pipe */
+  signal (SIGPIPE, XDead);
+  fcntl (Xgraph[N], F_SETFL,  O_NONBLOCK); 
+  for (i = 0; (read (Xgraph[N], buffer, 64) > 0) && (i < 20); i++);
+  fcntl (Xgraph[N], F_SETFL, !O_NONBLOCK); 
+  
+  if (Xgraph[N] < 1) if (!open_graph(N)) return;
+  KapaClear (Xgraph[N], TRUE);
+}
 
-  ALLOCATE (values, float, Npts);
-  for (i = 0; i < Npts; i++) {
-    values[i] = vect[i];
-  }
+void JpegPlot (Graphdata *graphmode, int N, char *filename) {
 
-  Nbytes = Npts * sizeof (float);
-  write (Xgraph[N], values, Nbytes);
-  free (values);
+  if (Xgraph[N] == 0) return;
 
+  KapaPNG (Xgraph[N], filename);
+  return;
+}
+
+void PSPlot (Graphdata *graphmode, int N, char *filename) {
+
+  if (Xgraph[N] == 0) return;
+
+  KiiPS (Xgraph[N], filename, TRUE, KAPA_PS_NEWPLOT, "default");
+  return;
 }
 
