--- modules/filter/filter.module.orig	Thu Mar  8 17:01:08 2007
+++ modules/filter/filter.module	Thu Mar  8 16:42:42 2007
@@ -30,7 +30,8 @@
 <p><em>Input formats</em> define a way of processing user-supplied text in Drupal. Every input format has its own settings of which <em>filters</em> to apply. Possible filters include stripping out malicious HTML and making URLs clickable.</p>
 <p>Users can choose between the available input formats when submitting content.</p>
 <p>Below you can configure which input formats are available to which roles, as well as choose a default input format (used for imported content, for example).</p>
-<p>Note that (1) the default format is always available to all roles, and (2) all filter formats can always be used by roles with the "administer filters" permission even if they are not explicitly listed in the Roles column of this table.</p>');
+<p>Note that (1) the default format is always available to all roles, and (2) all filter formats can always be used by roles with the "administer filters" permission even if they are not explicitly listed in the Roles column of this table.</p>
+<p>Weights dictate the selection of a default filter format. The heaviest available to a user (based on her roles) will be chosen.</p>');
 
     case 'admin/settings/filters/'. arg(3):
       return t('
@@ -316,6 +317,7 @@
     $options[$id] = '';
     $form[$format->name]['id'] = array('#value' => $id);
     $form[$format->name]['roles'] = array('#value' => $default ? t('All roles may use default format') : ($roles ? implode(', ',$roles) : t('No roles may use this format')));
+    $form[$format->name]['weight'] = array('#value' => $format->weight);
     $form[$format->name]['configure'] = array('#value' => l(t('configure'), 'admin/settings/filters/'. $id));
     $form[$format->name]['delete'] = array('#value' => $default ? '' : l(t('delete'), 'admin/settings/filters/delete/'. $id));
   }
@@ -340,13 +342,14 @@
         drupal_render($form['default'][$element['id']['#value']]),
         check_plain($name),
         drupal_render($element['roles']),
+        drupal_render($element['weight']),
         drupal_render($element['configure']),
         drupal_render($element['delete'])
       );
       unset($form[$name]);
     }
   }
-  $header = array(t('Default'), t('Name'), t('Roles'), array('data' => t('Operations'), 'colspan' => 2));
+  $header = array(t('Default'), t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
   $output = theme('table', $header, $rows);
   $output .= drupal_render($form);
 
@@ -430,6 +433,14 @@
       $form['roles'][$rid]['#disabled'] = TRUE;
     }
   }
+
+  // Add the default selection weight
+  $form['weight'] = array('#type' => 'weight',
+    '#title' => t('Weight'),
+    '#default_value' => $format->weight,
+    '#description' => t('The heaviest weight filter format available to the user will be chosen as the default for text entry fields.'),
+  );
+
   // Table with filters
   $all = filter_list_all();
   $enabled = filter_list_format($format->format);
@@ -524,7 +535,8 @@
   }
   $roles = ','. implode(',', ($form_values['default_format'] ? array_keys(user_roles()) : $roles)) .',';
 
-  db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
+  // XXX
+  db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s', weight=%d WHERE format = %d", $cache, $name, $roles, $form_values['weight'], $format);
 
   cache_clear_all($format .':', 'cache_filter', TRUE);
 
@@ -678,8 +690,27 @@
  * Resolve a format id, including the default format.
  */
 function filter_resolve_format($format) {
-  return $format == FILTER_FORMAT_DEFAULT ? variable_get('filter_default_format', 1) : $format;
+  if ($format == FILTER_FORMAT_DEFAULT) {
+    $allf = filter_formats();
+
+    $format = variable_get('filter_default_format', 1);
+    if (isset($allf[$format])) {
+      $max = $allf[$format]->weight;
+    } else {
+      $max = -11;
+    }
+
+    // Find the heaviest format available to this user
+    foreach ($allf as $id => $f) {
+      if ($max < $f->weight) {
+        $max = $f->weight;
+        $format = $id;
+      }
+    }
+  }
+  return $format;
 }
+
 /**
  * Check if text in a certain input format is allowed to be cached.
  */
--- modules/system/system.install.orig	Thu Mar  8 17:03:29 2007
+++ modules/system/system.install	Thu Mar  8 16:49:27 2007
@@ -310,6 +310,7 @@
         name varchar(255) NOT NULL default '',
         roles varchar(255) NOT NULL default '',
         cache tinyint NOT NULL default '0',
+        weight tinyint NOT NULL default '0',
         PRIMARY KEY (format),
         UNIQUE KEY (name)
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
@@ -775,6 +776,7 @@
         name varchar(255) NOT NULL default '',
         roles varchar(255) NOT NULL default '',
         cache smallint NOT NULL default '0',
+        weight smallint NOT NULL default '0',
         PRIMARY KEY (format),
         UNIQUE (name)
       )");
@@ -1088,9 +1090,9 @@
 
   db_query("INSERT INTO {node_access} VALUES (0, 0, 'all', 1, 0, 0)");
 
-  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('Filtered HTML',',1,2,',1)");
-  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('PHP code','',0)");
-  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('Full HTML','',1)");
+  db_query("INSERT INTO {filter_formats} (name, roles, cache, weight) VALUES ('Filtered HTML',',1,2,',1,0)");
+  db_query("INSERT INTO {filter_formats} (name, roles, cache, weight) VALUES ('PHP code','',0,0)");
+  db_query("INSERT INTO {filter_formats} (name, roles, cache, weight) VALUES ('Full HTML','',1,2)");
 
   db_query("INSERT INTO {filters} VALUES (1,'filter',3,0)");
   db_query("INSERT INTO {filters} VALUES (1,'filter',0,1)");
