Index: commands.c
===================================================================
--- commands.c	(revision 6700c605216db6a3c3e92a99664b78cd5bd89dec)
+++ commands.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -2665,5 +2665,5 @@
   owl_editwin_new_style(e, OWL_EDITWIN_STYLE_ONELINE, NULL);
 
-  owl_function_activate_keymap("recv");
+  owl_global_pop_context(&g);
 }
 
@@ -2723,4 +2723,5 @@
   owl_history_reset(hist);
   owl_global_set_typwin_inactive(&g);
+  owl_global_pop_context(&g);
   cmd = owl_strdup(owl_editwin_get_text(e));
   owl_editwin_fullclear(e);
@@ -2743,4 +2744,5 @@
 
   owl_global_set_typwin_inactive(&g);
+  owl_global_pop_context(&g);
   owl_editwin_fullclear(e);
   wnoutrefresh(owl_editwin_get_curswin(e));
@@ -2760,4 +2762,5 @@
   owl_editwin_fullclear(e);
   owl_global_set_typwin_inactive(&g);
+  owl_global_pop_context(&g);
   owl_global_set_needrefresh(&g);
   wnoutrefresh(owl_editwin_get_curswin(e));
@@ -2781,4 +2784,5 @@
 {
   owl_popwin_close(owl_global_get_popwin(&g));
+  owl_global_pop_context(&g);
   owl_viewwin_free(vw);
   owl_global_set_needrefresh(&g);
Index: context.c
===================================================================
--- context.c	(revision 3f8514b91ca8967e8c5d8c5a7be6d441b254bdce)
+++ context.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -52,47 +52,2 @@
   return(ctx->mode & OWL_CTX_INTERACTIVE)?1:0;
 }
-
-void owl_context_set_startup(owl_context *ctx)
-{
-  SET_MODE(ctx, OWL_CTX_STARTUP);
-}
-
-void owl_context_set_readconfig(owl_context *ctx)
-{
-  SET_MODE(ctx, OWL_CTX_READCONFIG);
-}
-
-void owl_context_set_interactive(owl_context *ctx)
-{
-  SET_MODE(ctx, OWL_CTX_INTERACTIVE);
-}
-
-void owl_context_set_popless(owl_context *ctx, owl_viewwin *vw)
-{
-  ctx->data = vw;
-  SET_ACTIVE(ctx, OWL_CTX_POPLESS);
-}
-
-void owl_context_set_recv(owl_context *ctx)
-{
-  SET_ACTIVE(ctx, OWL_CTX_RECV);
-}
-
-void owl_context_set_editmulti(owl_context *ctx, owl_editwin *ew)
-{
-  ctx->data = ew;
-  SET_ACTIVE(ctx, OWL_CTX_EDITMULTI);
-}
-
-void owl_context_set_editline(owl_context *ctx, owl_editwin *ew)
-{
-  ctx->data = ew;
-  SET_ACTIVE(ctx, OWL_CTX_EDITLINE);
-}
-
-void owl_context_set_editresponse(owl_context *ctx, owl_editwin *ew)
-{
-  ctx->data = ew;
-  SET_ACTIVE(ctx, OWL_CTX_EDITRESPONSE);
-}
-
Index: functions.c
===================================================================
--- functions.c	(revision 8a2815bfaabe3c7c85d8ddfc8c321473722540fb)
+++ functions.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -301,4 +301,5 @@
   owl_editwin_set_cbdata(owl_global_get_typwin(&g), data);
   owl_global_set_buffercallback(&g, callback);
+  owl_global_push_context(&g, OWL_CTX_EDITMULTI, e, "editmulti");
 }
 
@@ -1301,4 +1302,5 @@
 
   owl_popwin_up(pw);
+  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless");
   owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
 			owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
@@ -1318,4 +1320,5 @@
 
   owl_popwin_up(pw);
+  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless");
   owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
 		   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
@@ -1940,6 +1943,5 @@
   owl_editwin_redisplay(tw, 0);
 
-  owl_context_set_editline(owl_global_get_context(&g), tw);
-  owl_function_activate_keymap("editline");
+  owl_global_push_context(&g, OWL_CTX_EDITLINE, tw, "editline");
 }
 
@@ -1957,6 +1959,5 @@
   owl_editwin_redisplay(tw, 0);
 
-  owl_context_set_editresponse(owl_global_get_context(&g), tw);
-  owl_function_activate_keymap("editresponse");
+  owl_global_push_context(&g, OWL_CTX_EDITRESPONSE, tw, "editline");
 }
 
@@ -1975,6 +1976,5 @@
   owl_editwin_redisplay(tw, 0);
 
-  owl_context_set_editresponse(owl_global_get_context(&g), tw);
-  owl_function_activate_keymap("editresponse");
+  owl_global_push_context(&g, OWL_CTX_EDITRESPONSE, tw, "editresponse");
 }
 
@@ -2830,11 +2830,4 @@
   if (direction==0) {
     owl_list_append_element(fl, f);
-  }
-}
-
-void owl_function_activate_keymap(const char *keymap)
-{
-  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
-    owl_function_error("Unable to activate keymap '%s'", keymap);
   }
 }
Index: keys.c
===================================================================
--- keys.c	(revision 6aa46119049988c5112434b46eebf54c742f5ddd)
+++ keys.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -312,10 +312,4 @@
   BIND_CMD("C-c",  "",                "no effect in this mode");
   BIND_CMD("C-g",  "",                "no effect in this mode");
-
-
-  /**********************/
-
-  owl_function_activate_keymap("recv");
-
 }
 
Index: owl.c
===================================================================
--- owl.c	(revision b752f1ea37f46cccfd4bf59229e70105e02e6525)
+++ owl.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -538,5 +538,8 @@
   /* Initialize perl */
   owl_function_debugmsg("startup: processing config file");
-  owl_context_set_readconfig(owl_global_get_context(&g));
+
+  owl_global_pop_context(&g);
+  owl_global_push_context(&g, OWL_CTX_READCONFIG, NULL, NULL);
+
   perlerr=owl_perlconfig_initperl(opts.configfile, &argc, &argv, &env);
   if (perlerr) {
@@ -612,5 +615,7 @@
 
   owl_function_debugmsg("startup: setting context interactive");
-  owl_context_set_interactive(owl_global_get_context(&g));
+
+  owl_global_pop_context(&g);
+  owl_global_push_context(&g, OWL_CTX_READCONFIG|OWL_CTX_RECV, NULL, "recv");
 
   owl_select_add_timer(180, 180, owl_zephyr_buddycheck_timer, NULL, NULL);
Index: popexec.c
===================================================================
--- popexec.c	(revision 18fdd5f9be6de827a19aceb95d4769058b145869)
+++ popexec.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -27,4 +27,5 @@
 
   owl_popwin_up(pw);
+  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless");
   owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
 			owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
Index: select.c
===================================================================
--- select.c	(revision b752f1ea37f46cccfd4bf59229e70105e02e6525)
+++ select.c	(revision 2a17b635c6895121650446d2529f46d7e291a486)
@@ -261,35 +261,6 @@
 {
   int ret;
-  owl_popwin *pw;
-  owl_editwin *tw;
 
   owl_global_set_lastinputtime(&g, time(NULL));
-  pw=owl_global_get_popwin(&g);
-  tw=owl_global_get_typwin(&g);
-
-  owl_global_set_lastinputtime(&g, time(NULL));
-  /* find and activate the current keymap.
-   * TODO: this should really get fixed by activating
-   * keymaps as we switch between windows... 
-   */
-  if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
-    owl_context_set_popless(owl_global_get_context(&g), 
-                            owl_global_get_viewwin(&g));
-    owl_function_activate_keymap("popless");
-  } else if (owl_global_is_typwin_active(&g) 
-             && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
-    /*
-      owl_context_set_editline(owl_global_get_context(&g), tw);
-      owl_function_activate_keymap("editline");
-    */
-  } else if (owl_global_is_typwin_active(&g) 
-             && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
-    owl_context_set_editmulti(owl_global_get_context(&g), tw);
-    owl_function_activate_keymap("editmulti");
-  } else {
-    owl_context_set_recv(owl_global_get_context(&g));
-    owl_function_activate_keymap("recv");
-  }
-  /* now actually handle the keypress */
   ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
   if (ret!=0 && ret!=1) {
