diff -uprN markup_snippets_orig/markup_snippets.js markup_snippets/markup_snippets.js
--- markup_snippets_orig/markup_snippets.js	1970-01-01 01:00:00.000000000 +0100
+++ markup_snippets/markup_snippets.js	2010-01-10 17:10:44.531590979 +0100
@@ -0,0 +1,43 @@
+// $Id$
+
+/*
+ * Attach AJAX/AHAH event handlers for Wysiwyg integration.
+ *
+ * This code takes care of Wysiwyg integration in D6.
+ * Wysiwyg module needs to be able to detach any attached editor before the
+ * AJAX request is made or it cannot be re-attached afterwards.
+ * Drupal 6 does not have detach behaviors so we need to manually call
+ * Drupal.wysiwygDetach(). Wysiwyg's own behavior won't be able to
+ * re-attach the editor either because the context doesn't include the
+ * format selector so we call Drupal.wysiwygAttach() manually too.
+ *
+ * @param context
+ *   A DOM element, supplied by Drupal.attachBehaviors().
+ *
+ * This code is based on Drupal.behaviors.attachWysiwyg from Wysiwyg module. 
+ * @see Drupal.behaviors.attachWysiwyg in Wysiwyg module.
+ */
+Drupal.behaviors.markup_snippets = function (context) {
+  var params;
+  // Add a global AJAX listener for attaching/detaching Wysiwyg module.
+  $('#edit-body-wrapper:not(.markup-snippets-processed)', context).each(function () {
+    $(this).addClass('markup-snippets-processed').bind('ajaxSend', function (event, XMLHttpRequest, ajaxOptions) {
+      // Detach the Wysiwyg module if this is our request.
+      params = $.extend({}, Drupal.wysiwyg.instances['edit-body']);
+      if (params.editor == 'none') {
+        // No editor instance, get parameters from the selected input format.
+        params = Drupal.wysiwyg.getParams($(':input.wysiwyg-field-edit-body:checked, div.wysiwyg-field-edit-body', context).get(0));
+        // Override status to keep the editor disabled when re-attaching.
+        params.status = false;
+      }
+      if (ajaxOptions.url == '/markup_snippets/js') {
+        Drupal.wysiwygDetach(context, params);
+      }
+    }).bind('ajaxComplete', function (event, XMLHttpRequest, ajaxOptions) {
+      // Re-attach the Wysiwyg module if this is our response.
+      if (ajaxOptions.url == '/markup_snippets/js') {
+        Drupal.wysiwygAttach(context, params);
+      }
+    });
+  });
+}
diff -uprN markup_snippets_orig/markup_snippets.js~ markup_snippets/markup_snippets.js~
--- markup_snippets_orig/markup_snippets.js~	1970-01-01 01:00:00.000000000 +0100
+++ markup_snippets/markup_snippets.js~	2010-01-10 17:08:25.191588125 +0100
@@ -0,0 +1,43 @@
+// $Id$
+
+/*
+ * Attach AJAX/AHAH event handlers for Wysiwyg integration.
+ *
+ * This code takes care of Wysiwyg integration in D6.
+ * Wysiwyg module needs to be able to detach any attached editor before the
+ * AJAX request is made or it cannot be re-attached afterwards.
+ * Drupal 6 does not have detach behaviors so we need to manually call
+ * Drupal.wysiwygDetach(). Wysiwyg's own behavior won't be able to
+ * re-attach the editor either because the context doesn't include the
+ * format selector so we call Drupal.wysiwygAttach() manually too.
+ *
+ * @param context
+ *   A DOM element, supplied by Drupal.attachBehaviors().
+ *
+ * This code is based on Drupal.behaviors.attachWysiwyg from Wysiwyg module. 
+ * @see Drupal.behaviors.attachWysiwyg in Wysiwyg module.
+ */
+Drupal.behaviors.markup_snippets = function (context) {
+  var params;
+  // Add a global AJAX listener for attaching/detaching Wysiwyg module.
+  $('#edit-body-wrapper:not(.markup-snippets-processed)', context).each(function () {
+    $(this).addClass('markup-snippets-processed').bind('ajaxSend', function (event, XMLHttpRequest, ajaxOptions) {
+      // Detach the Wysiwyg module if this is our request.
+      params = $.extend({}, Drupal.wysiwyg.instances['edit-body']);
+      if (params.editor == 'none') {
+        // No editor instance, get parameters from the selected input format.
+        params = Drupal.wysiwyg.getParams($(':input.wysiwyg-field-edit-body:checked, div.wysiwyg-field-edit-body', context).get(0));
+        // Override status to keep the editor disabled when re-attaching.
+        params.status = false;
+      }
+      if (ajaxOptions.url == '/markup_snippets/js') {
+        Drupal.wysiwygDetach(context, params);
+      }
+    }).bind('ajaxComplete', function (event, XMLHttpRequest, ajaxOptions) {
+      // Re-attach the Wysiwyg module if this is our response.
+      if (ajaxOptions.url == '/markup_snippets/js') {
+        Drupal.wysiwygAttach(context, params);
+      }
+    });
+  });
+}
diff -uprN markup_snippets_orig/markup_snippets.module markup_snippets/markup_snippets.module
--- markup_snippets_orig/markup_snippets.module	2010-01-09 01:59:38.451586675 +0100
+++ markup_snippets/markup_snippets.module	2010-01-10 17:06:18.491587023 +0100
@@ -11,30 +11,35 @@
  * Implementation of hook_form_alter().
  */
 function markup_snippets_form_alter(&$form, $form_state, $form_id) {
-  
+
   // make sure we're on a node form and user has permission to use snippets
   if ($form['#id'] == 'node-form' && user_access('use snippets')) {
+    // Include the Wysiwyg module integration.
+    if (module_exists('wysiwyg')) {
+      drupal_add_js(drupal_get_path('module', 'markup_snippets') . '/markup_snippets.js');
+    }
+
     // enforce caching for ahah
     $form['#cache'] = TRUE;
-    
+
     // load the content type settings
     $type = node_get_types('type', $form['#node']);
-    
+
     // set up a new fieldset above the node body field
-    $form['body_field']['markup_snippets'] = array(
+    $form['markup_snippets'] = array(
       '#type' => 'fieldset',
       '#title' => t('Mark-up Snippets'),
       '#description' => t('Load saved snippets or save the contents of the current !body_label field.', array('!body_label' => $type->body_label)),
-      '#weight' => -100,
+      '#weight' => -1,
       '#collapsible' => TRUE,
       '#collapsed' => FALSE,
     );
-    
+
     // set variables for the select list
     $disabled = FALSE;
     $default_label = t('Select a snippet >>');
     $options = array($default_label);
-    
+
     // try to load snippets
     if (markup_snippets_get_snippets()) {
       $options = markup_snippets_get_snippets($options);
@@ -45,9 +50,9 @@ function markup_snippets_form_alter(&$fo
       $disabled = TRUE;
       $description = t('There are no snippets saved in the database yet.');
     }
-    
+
     // render the actual form field
-    $form['body_field']['markup_snippets']['markup_snippets_select'] = array(
+    $form['markup_snippets']['markup_snippets_select'] = array(
       '#type' => 'select',
       '#title' => t('Select a snippet to use'),
       '#description' => $description,
@@ -61,15 +66,31 @@ function markup_snippets_form_alter(&$fo
         'event' => 'change',
       ),
     );
-    
+
+    // During AHAH requests, markuo_snippets_select will have a value > 0.
+    if ($form_state['values']['markup_snippets_select'] > 0) {
+      // Change body value here to keep cache synced with the rendered form.
+      $mid = $form_state['values']['markup_snippets_select'];
+      $snippets = markup_snippets_get_snippets(array(), TRUE, $mid);
+      $form['body_field']['body']['#default_value'] = $snippets[$mid]['snippet'];
+    }
+
     // we need a field for naming our snippet
-    $form['body_field']['markup_snippets']['markup_snippets_name'] = array(
+    $form['markup_snippets']['markup_snippets_name'] = array(
       '#type' => 'textfield',
       '#title' => t('Enter a name to save the current snippet'),
       '#size' => 30,
       '#description' => t('If you enter a name in this box then the contents of the !body_label field will be saved as a snippet when you click Save.', array('!body_label' => $type->body_label))
     );
-    
+
+    // We need a custom submit handler for AHAH, must use a button for that,
+    // but it can have #access => FALSE so the user won't see it.
+    $form['markup_snippets']['markup_snippets_submit'] = array(
+      '#type' => 'submit',
+      '#access' => FALSE,
+      '#value' => 'submit_snippet',
+      '#submit' => array('markup_snippets_get_snippets_submit'),
+    );
   }
 }
 
@@ -86,7 +107,7 @@ function markup_snippets_nodeapi(&$node,
         // note, names do not technically need to be unique, but avoids UI confusion
         $query = db_query("SELECT mid FROM {markup_snippets} WHERE name = '%s'", $node->markup_snippets_name);
         $result = db_result($query);
-        
+
         // if the name is in use, warn and do nothing
         if ($result) {
           drupal_set_message(t('Snippet was not saved because a snippet named %snippet already exists.', array('%snippet' => $node->markup_snippets_name)), 'error');
@@ -116,14 +137,14 @@ function markup_snippets_perm() {
  */
 function markup_snippets_menu() {
   $items = array();
-  
+
   $items['admin/content/markup_snippets'] = array(
     'title' => t('Mark-up Snippets'),
     'description' => t('Manage the mark-up snippets saved in the database.'),
     'page callback' => 'markup_snippets_admin',
     'access arguments' => array('administer snippets'),
   );
-  
+
   $items['admin/content/markup_snippets/delete'] = array(
     'title' => t('Delete Mark-up Snippet'),
     'description' => t('Delete a mark-up snippet from the database.'),
@@ -131,7 +152,7 @@ function markup_snippets_menu() {
     'page arguments' => array('markup_snippets_admin_delete'),
     'access arguments' => array('administer snippets'),
   );
-  
+
   $items['admin/content/markup_snippets/edit'] = array(
     'title' => t('Edit Mark-up Snippet'),
     'description' => t('Edit a mark-up snippet in the database.'),
@@ -139,15 +160,14 @@ function markup_snippets_menu() {
     'page arguments' => array('markup_snippets_admin_edit'),
     'access arguments' => array('administer snippets'),
   );
-  
+
   $items['markup_snippets/js'] = array(
     'title' => 'Javascript Node Body Loader',
     'page callback' => 'markup_snippets_js',
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );
-  
-  
+
   return $items;
 }
 
@@ -155,11 +175,11 @@ function markup_snippets_menu() {
  * Delete form for snippets.
  */
 function markup_snippets_admin_delete() {
-  
+
   // set up a default form if we get bad data
   $bad_request_form = array();
   $bad_request_form['#prefix'] = l(t('Go to snippet list'), 'admin/content/markup_snippets');
-  
+
   // check we have a snippet ID
   if (is_numeric(arg(4))) {
     $mid = arg(4);
@@ -169,34 +189,34 @@ function markup_snippets_admin_delete() 
     drupal_set_message(t('You must specify a valid snippet to delete.'), 'error');
     return $bad_request_form;
   }
-  
+
   // attempt to load the snippet
   $snippet = markup_snippets_get_snippets(array(), FALSE, $mid);
-  
+
   // if there's a snippet, load the delete form
   if ($snippet) {
     $form = array();
-  
+
     $form['#prefix'] = t('Are you sure you want to delete the snippet %snippet?', array('%snippet' => $snippet[$mid]));
-    
+
     $form['markup_snippets_mid'] = array(
       '#type' => 'value',
       '#value' => $mid,
     );
-  
+
     $form['markup_snippets_delete'] = array(
       '#type' => 'submit',
       '#default_value' => t('Delete'),
     );
-  
+
     $form['markup_snippets_delete_cancel'] = array(
       '#type' => 'markup',
       '#value' => l(t('Cancel'), 'admin/content/markup_snippets'),
     );
-    
+
     // go back to the snippets list on submit
     $form['#redirect'] = 'admin/content/markup_snippets';
-  
+
     return $form;
   }
   // otherwise send our bad request notice
@@ -218,11 +238,11 @@ function markup_snippets_admin_delete_su
  * Edit form for snippets.
  */
 function markup_snippets_admin_edit() {
-  
+
   // set up a default form if we get bad data
   $bad_request_form = array();
   $bad_request_form['#prefix'] = l(t('Go to snippet list'), 'admin/content/markup_snippets');
-  
+
   // check we have a snippet ID
   if (is_numeric(arg(4))) {
     $mid = arg(4);
@@ -232,46 +252,46 @@ function markup_snippets_admin_edit() {
     drupal_set_message(t('You must specify a valid snippet to edit.'), 'error');
     return $bad_request_form;
   }
-  
+
   // attempt to load the snippet
   $snippet = markup_snippets_get_snippets(array(), TRUE, $mid);
-  
+
   // if there's a snippet, load the edit form
   if ($snippet) {
     $form = array();
-    
+
     $form['markup_snippets_mid'] = array(
       '#type' => 'value',
       '#value' => $mid,
     );
-    
+
     $form['markup_snippets_edit_name'] = array(
       '#type' => 'textfield',
       '#description' => t('The name of your snippet.'),
       '#title' => t('Name'),
       '#default_value' => $snippet[$mid]['name'],
     );
-    
+
     $form['markup_snippets_edit_snippet'] = array(
       '#type' => 'textarea',
       '#description' => t('The actual snippet code.'),
       '#title' => t('Snippet'),
       '#default_value' => $snippet[$mid]['snippet'],
     );
-  
+
     $form['markup_snippets_edit'] = array(
       '#type' => 'submit',
       '#default_value' => t('Save'),
     );
-  
+
     $form['markup_snippets_edit_cancel'] = array(
       '#type' => 'markup',
       '#value' => l(t('Cancel'), 'admin/content/markup_snippets'),
     );
-    
+
     // go back to the snippets list on submit
     $form['#redirect'] = 'admin/content/markup_snippets';
-  
+
     return $form;
   }
   // otherwise send our bad request notice
@@ -289,7 +309,7 @@ function markup_snippets_admin_edit_vali
   if (trim($form_state['values']['markup_snippets_edit_name']) == '') {
     form_set_error('markup_snippets_edit_name', t('Name cannot be empty.'));
   }
-  
+
   if (trim($form_state['values']['markup_snippets_edit_snippet']) == '') {
     form_set_error('markup_snippets_edit_snippet', t('Snippet cannot be empty.'));
   }
@@ -313,13 +333,13 @@ function markup_snippets_admin_edit_subm
 function markup_snippets_admin() {
   // load all snippets
   $snippets = markup_snippets_get_snippets();
-  
+
   // if there are results
   if ($snippets) {
     // build our basic table data
     $header = array(t('Name'), '', '');
     $rows = array();
-    
+
     // for each result build a table row
     foreach ($snippets as $mid => $snippet) {
       $rows[] = array(
@@ -328,7 +348,7 @@ function markup_snippets_admin() {
         l(t('Delete'), 'admin/content/markup_snippets/delete/'.$mid),
       );
     }
-    
+
     // send back a themed HTML table
     return theme('table', $header, $rows);
   }
@@ -337,7 +357,7 @@ function markup_snippets_admin() {
     $output = t('There are no snippets in the database.');
     return '<div>'.$output.'</div>';
   }
-  
+
 }
 
 /**
@@ -367,12 +387,12 @@ function markup_snippets_get_snippets($s
   else {
     $query = db_query("SELECT * FROM {markup_snippets}");
   }
-  
+
   // if there are no results send back a FALSE
   if (!$query) {
     return FALSE;
   }
-  
+
   // otherwise loop through results
   while ($result = db_fetch_object($query)) {
     // check it we want the actual snippet mark-up
@@ -387,32 +407,40 @@ function markup_snippets_get_snippets($s
       $snippets[$result->mid] = t(check_plain($result->name));
     }
   }
-  
+
   return $snippets;
 }
 
+/*
+ * Custom submit handler for AHAH requests.
+ */
+function markup_snippets_get_snippets_submit($form, &$form_state) {
+  $node = $form_state['values'];
+  unset($form_state['submit_handlers']);
+  form_execute_handlers('submit', $form, $form_state);
+  $form_state['node'] = $node;
+  $form_state['rebuild'] = TRUE;
+}
+
 /**
  * Implementation of ahah callback for node form.
  */
 function markup_snippets_js() {
   // THIS CODE IS TAKEN FROM THIS HOWTO: http://drupal.org/node/331941
   // In Drupal 7 this code (up to our custom stuff) will be a core function
-  
+
   // The form is generated in an include file which we need to include manually.
   // Changed from Poll module example:
   include_once drupal_get_path('module', 'node') . '/node.pages.inc';
-  
+
   // AHAH has submitted our form, so we need to rebuild it
   // Note, this is changed from the example above - the example
   // causes an error because the node gets locked.
-  
+
   // Preparing to retrieve form from cache
-  // Old line:
-  // $form_state = array('storage' => NULL, 'submitted' => FALSE);
-  // Our replacement:
-  $form_state = array('storage' => NULL, 'rebuild' => TRUE);
+  $form_state = array('storage' => NULL, 'submitted' => FALSE);
   $form_build_id = $_POST['form_build_id'];
-  
+
   // Retrieve the form from the cache
   $form = form_get_cache($form_build_id, $form_state);
 
@@ -426,35 +454,25 @@ function markup_snippets_js() {
   drupal_process_form($form_id, $form, $form_state);
   // Rebuild the form
   $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
-  
+
   // CUSTOM CODE STARTS HERE
-  
+
   // Now we have the latest $form, with the changes made by our
-  // AHAH enabled field - we can now build our mark-up to return
-  
-  // get the node type info we need for our body field
-  $node = $form['#node'];
-  $type = node_get_types('type', $node);
-  
-  // load the snippet from the posted data
-  $mid = $form_state['post']['markup_snippets_select'];
-  $snippet = markup_snippets_get_snippets(array(), TRUE, $mid);
-  
-  // build the returned mark-up
-  // we are mimicking the output of the node_get_body() function
-  $output = '';
-  $output .= '<label for="edit-body">' . check_plain($type->body_label) . ': </label>';
-  $output .= '<div class="resizeable-textarea"><span>';
-  $output .= '<textarea class="form-textarea resizable textarea-processed" id="edit-body" name="body" rows="20" cols="60">';
-  // switch the body for our snippet
-  $output .= $snippet[$mid]['snippet'];
-  $output .= '</textarea>';
-  $output .= '</span></div>';
-  
+  // AHAH enabled field - we can now render our mark-up to return.
+
+  $changed_field = $form['body_field']['body'];
+  $output = drupal_render($changed_field);
+  // Strip the wrapper as only its contents will be replaced.
+  $output = preg_replace('/^<div.*?class="form-item".*?id="[^"]*?-wrapper".*?>/', '', $output);
+  $output = preg_replace('/<\/div>\s*$/', '', $output);
+
+  // Flush out any errors from form validation.
+  drupal_get_messages();
+
   // ALL DONE
 
   // Final rendering callback - send our mark-up back to the div we
   // specified in our AHAH form information
   drupal_json(array('status' => TRUE, 'data' => $output));
-  
+
 }
