Index: /branches/kapa-mods-2007-05/Ohana/src/addstar/src/SEDops.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/addstar/src/SEDops.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/addstar/src/SEDops.c	(revision 13404)
@@ -111,5 +111,5 @@
   graphdata.style = 2;
   graphdata.ptype = 2;
-  KapaClear (Xgraph, TRUE);
+  KapaClearSections (Xgraph);
   magSection.name = strcreate ("mag");
   magSection.x  = 0;
@@ -156,5 +156,5 @@
   SWAP (graphdata.ymin, graphdata.ymax);
 
-  KapaClear (Xgraph, TRUE);
+  KapaClearSections (Xgraph);
   KapaSetSection (Xgraph, &magSection);
   KapaSetLimits (Xgraph, &graphdata);
Index: /branches/kapa-mods-2007-05/Ohana/src/gastro/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/gastro/src/plotstuff.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/gastro/src/plotstuff.c	(revision 13404)
@@ -36,4 +36,6 @@
 void PrepPlotting (int Npts, Graphdata *graphmode, int N) {
 
+  if (Xgraph[N] == 0) return;
+
   active = N;
   if (Npts < 1) return;
@@ -63,5 +65,5 @@
   
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  KapaClear (Xgraph[N], TRUE);
+  KapaClearSections (Xgraph[N]);
 }
 
Index: /branches/kapa-mods-2007-05/Ohana/src/gastro2/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/gastro2/src/plotstuff.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/gastro2/src/plotstuff.c	(revision 13404)
@@ -28,13 +28,13 @@
 
 void DonePlotting (Graphdata *graphmode, int N) {
-  char buffer[65], buffer2[65];
 
+  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];
+  if (Xgraph[N] == 0) return;
 
   active = N;
@@ -65,5 +65,5 @@
   
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  KapaClear (Xgraph[N], TRUE);
+  KapaClearSections (Xgraph[N]);
 }
 
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h	(revision 13404)
@@ -56,4 +56,5 @@
 int           SetSection          PROTO((int sock));
 int           ListSection         PROTO((int sock));
+int           MoveSection         PROTO((int sock));
 int           DefineSection       PROTO((int sock));
 int           SetFont             PROTO((int sock));
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c	(revision 13404)
@@ -142,4 +142,10 @@
   }
   
+  if (!strcmp (word, "MSEC")) {
+    status = MoveSection (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    return (TRUE);
+  }
+  
   if (!strcmp (word, "FONT")) {
     status = SetFont (sock);
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Sections.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Sections.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Sections.c	(revision 13404)
@@ -154,2 +154,64 @@
     return;
 }
+
+// return TRUE even for nonsense cases to avoid quitting kapa
+int MoveSection (int sock) {
+
+  char name[128];
+  char direction[16];
+  Section *tmpSection = NULL;
+
+  KiiScanMessage (sock, "%s %s", name, direction);
+  
+  N = GetSectionByName (name);
+  if (N < 0) {
+    fprintf (stderr, "section %s not found\n", name);
+    return (TRUE);
+  }
+
+  if (!strcasecmp (direction, "up")) {
+      if (N < 0) return (TRUE);
+      if (N > Nsections - 2) return (TRUE);
+      tmpSection = sections[N];
+      sections[N] = sections[N+1];
+      sections[N] = tmpSection;
+      Refresh (1);
+      return (TRUE);
+  }
+
+  if (!strcasecmp (direction, "down")) {
+      if (N < 1) return (TRUE);
+      if (N > Nsections - 1) return (TRUE);
+      tmpSection = sections[N];
+      sections[N] = sections[N-1];
+      sections[N] = tmpSection;
+      Refresh (1);
+      return (TRUE);
+  }
+
+  if (!strcasecmp (direction, "top")) {
+      if (N < 0) return (TRUE);
+      if (N > Nsections - 2) return (TRUE);
+      tmpSection = sections[N];
+      for (i = N; i < Nsections - 1; i++) {
+	sections[i] = sections[i+1];
+      }
+      sections[i] = tmpSection;
+      Refresh (1);
+      return (TRUE);
+  }
+
+  if (!strcasecmp (direction, "bottom")) {
+      if (N < 1) return (TRUE);
+      if (N > Nsections - 1) return (TRUE);
+      tmpSection = sections[N];
+      for (i = N; i >= 1; i--) {
+	sections[i] = sections[i-1];
+      }
+      sections[i] = tmpSection;
+      Refresh (1);
+      return (TRUE);
+  }
+  fprintf (stderr, "unknown direction %s for MoveSection\n", direction);
+  return (TRUE);
+}
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/doc/api.txt
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/doc/api.txt	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/doc/api.txt	(revision 13404)
@@ -17,4 +17,5 @@
 SLIM | SetLimits      	| KapaWindow.c  | KapaSetLimits
 DSEC | DefineSection  	| KapaWindow.c  | KapaSetSection
+MSEC | MoveSection  	| KapaWindow.c  | KapaMoveSection
 CENT | Center           | KapaWindow.c  | KiiCenter
 QUIT | Quit           	| KiiOpen.c     | KiiClose
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h	(revision 13404)
@@ -154,4 +154,5 @@
 int KapaSelectSection (int fd, char *name);
 int KapaGetSection (int fd, char *name);
+int KapaMoveSection (int fd, KapaSection *section, char *direction);
 
 /* KapaColors */
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KapaWindow.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KapaWindow.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/KapaWindow.c	(revision 13404)
@@ -240,4 +240,26 @@
 }
 
+int KapaMoveSection (int fd, char *name, char *direction) {
+
+  if (!strcasecmp(direction, "up")) goto valid;
+  if (!strcasecmp(direction, "down")) goto valid;
+  if (!strcasecmp(direction, "top")) goto valid;
+  if (!strcasecmp(direction, "bottom")) goto valid;
+  
+  fprintf (stderr, "unexpected direction %s\n", direction); 
+  return (FALSE);
+
+valid:
+  KiiSendCommand (fd, 4, "MSEC");
+  KiiSendMessage (fd, "%s %s", name, direction);
+  KiiScanCommand (fd, 4, "%s", word);
+  if (strcmp (word, "DONE")) {
+      fprintf (stderr, "unexpected response %s\n", word); 
+      return (FALSE);
+  }
+  
+  return (TRUE);
+}
+
 int KapaSelectSection (int fd, char *name) {
 
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 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.astro/region.c	(revision 13404)
@@ -107,5 +107,5 @@
   graphmode.coords.cdelt1 = graphmode.coords.cdelt2 = 1.0;
 
-  KapaClear (kapa, TRUE);
+  KapaClearSections (kapa);
   KapaSetLimits (kapa, &graphmode);
 
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/clear.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/clear.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/clear.c	(revision 13404)
@@ -1,7 +1,8 @@
 # include "data.h"
 
+// default is to clear all plots, but not the sections or the images
 int clear (int argc, char **argv) {
   
-  int N, ClearSection;
+  int N;
   int kapa;
   char *name;
@@ -16,17 +17,32 @@
   FREE (name);
 
-  /* XXX need to distinguish between Plot, AllPlots, Image, AllSections */
-  ClearSection = FALSE;
-  if ((N = get_argument (argc, argv, "-s"))) {
-    remove_argument (N, &argc, argv);
-    ClearSection = TRUE;
+  // clear all sections
+  if ((N = get_argument (argc, argv, "-s")) || 
+      (N = get_argument (argc, argv, "-section"))) {
+      KapaClearSections (kapa);
+      return (TRUE);
+  }
+
+  // clear all sections
+  if ((N = get_argument (argc, argv, "-graph"))) {
+      KapaClearCurrentPlot (kapa);
+      return (TRUE);
+  }
+
+  // clear image
+  if ((N = get_argument (argc, argv, "-image"))) {
+      KapaClearImage (kapa);
+      return (TRUE);
   }
 
   if (argc != 1) {
-    gprint (GP_ERR, "USAGE: clear [-n Xgraph]\n");
+    gprint (GP_ERR, "USAGE: clear [-n Xgraph] [-s|-section] [-image] [-graph]\n");
+    gprint (GP_ERR, "       [-s|-section] : clear all sections\n");
+    gprint (GP_ERR, "       [-graph]      : clear current graph\n");
+    gprint (GP_ERR, "       [-image]      : clear current image\n");
     return (FALSE);
   }
 
-  KapaClear (kapa, ClearSection);
+  KapaClearPlots (kapa);
   return (TRUE);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/section.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/section.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/section.c	(revision 13404)
@@ -1,7 +1,9 @@
 # include "data.h"
+
+enum {NONE, LIST, UP, DOWN, TOP, BOTTOM};
 
 int section (int argc, char **argv) {
   
-  int N, List;
+  int N, action;
   int kapa;
   char *name;
@@ -9,8 +11,24 @@
   KapaSection section;
 
-  List = FALSE;
+  action = NONE;
   if ((N = get_argument (argc, argv, "-list"))) {
     remove_argument (N, &argc, argv);
-    List = TRUE;
+    action = LIST;
+  }
+  if ((N = get_argument (argc, argv, "-up"))) {
+    remove_argument (N, &argc, argv);
+    action = UP;
+  }
+  if ((N = get_argument (argc, argv, "-down"))) {
+    remove_argument (N, &argc, argv);
+    action = DOWN;
+  }
+  if ((N = get_argument (argc, argv, "-top"))) {
+    remove_argument (N, &argc, argv);
+    action = TOP;
+  }
+  if ((N = get_argument (argc, argv, "-bottom"))) {
+    remove_argument (N, &argc, argv);
+    action = BOTTOM;
   }
 
@@ -28,4 +46,5 @@
     KapaGetSection (kapa, "*");
     gprint (GP_ERR, "USAGE: section name [x y dx dy]\n");
+    gprint (GP_ERR, "USAGE: section name [-list] [-up] [-down] [-top] [-bottom]\n");
     return (TRUE);
   } 
@@ -33,8 +52,25 @@
   if (argc == 2) {
     /* select / show section */
-    if (List) {
-      KapaGetSection (kapa, argv[1]);
-    } else {
-      KapaSelectSection (kapa, argv[1]);
+    switch (action) {
+      case NONE:
+	KapaSelectSection (kapa, argv[1]);
+	break;
+
+      case UP:
+	KapaMoveSection (kapa, argv[1], "up");
+	break;
+      case DOWN:
+	KapaMoveSection (kapa, argv[1], "down");
+	break;
+      case TOP:
+	KapaMoveSection (kapa, argv[1], "top");
+	break;
+      case BOTTOM:
+	KapaMoveSection (kapa, argv[1], "bottom");
+	break;
+
+      case LIST:
+	KapaGetSection (kapa, argv[1]);
+	break;
     }
     return (TRUE);
@@ -52,4 +88,5 @@
   }
   gprint (GP_ERR, "USAGE: section name [x y dx dy]\n");
+  gprint (GP_ERR, "USAGE: section name [-list] [-up] [-down] [-top] [-bottom]\n");
   return (FALSE);
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/dvo/fitcolors.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/dvo/fitcolors.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/dvo/fitcolors.c	(revision 13404)
@@ -95,5 +95,5 @@
     graphdata.style = 2;
     graphdata.ptype = 2;
-    KapaClear (kapa, TRUE);
+    KapaClearSections (kapa);
     KapaSetFont (kapa, "helvetica", 14);
 
@@ -337,5 +337,5 @@
 	    sprintf (filename, "%s.%02d.png", plotname, Nplot);
 	    KapaPNG (kapa, filename);
-	    KapaClear (kapa, TRUE);
+	    KapaClearSections (kapa);
 	    Nplot++;
 	  }
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/dvo/fitsed.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/dvo/fitsed.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/dvo/fitsed.c	(revision 13404)
@@ -161,5 +161,5 @@
     graphdata.style = 2;
     graphdata.ptype = 2;
-    KapaClear (kapa, TRUE);
+    KapaClearSections (kapa);
     magSection.name = strcreate ("mag");
     magSection.x  = 0;
@@ -261,5 +261,5 @@
 	SWAP (graphdata.ymin, graphdata.ymax);
 
-	KapaClear (kapa, TRUE);
+	KapaClearSections (kapa);
 	KapaSetSection (kapa, &magSection);
     	KapaSetLimits (kapa, &graphdata);
Index: /branches/kapa-mods-2007-05/Ohana/src/relastro/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/relastro/src/plotstuff.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/relastro/src/plotstuff.c	(revision 13404)
@@ -63,5 +63,5 @@
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
 
-  KapaClear (Xgraph[N], TRUE);
+  KapaClearSections (Xgraph[N]);
 
   KapaPrepPlot (Xgraph[N], Npts, graphmode);
Index: /branches/kapa-mods-2007-05/Ohana/src/relphot/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/relphot/src/plotstuff.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/relphot/src/plotstuff.c	(revision 13404)
@@ -63,5 +63,5 @@
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
 
-  KapaClear (Xgraph[N], TRUE);
+  KapaClearSections (Xgraph[N]);
 
   KapaPrepPlot (Xgraph[N], Npts, graphmode);
Index: /branches/kapa-mods-2007-05/Ohana/src/uniphot/src/plotstuff.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/uniphot/src/plotstuff.c	(revision 13403)
+++ /branches/kapa-mods-2007-05/Ohana/src/uniphot/src/plotstuff.c	(revision 13404)
@@ -72,5 +72,5 @@
   
   if (Xgraph[N] < 1) if (!open_graph(N)) return;
-  KapaClear (Xgraph[N], TRUE);
+  KapaClearSections (Xgraph[N]);
 }
 
