--- menu_trim.module.06-form_alter	2008-03-31 22:16:59.000000000 -0400
+++ menu_trim.module.07-block	2008-03-31 22:17:09.000000000 -0400
@@ -184,47 +184,76 @@ function menu_trim_admin_settings_submit
 function menu_trim_block($op = 'list', $delta = 0, $edit = array()) {
   switch ($op) {
     case 'list':
-      $blocks = array();
-      $menus = menu_get_root_menus();
-      foreach ($menus as $mid => $title) {
-        if (variable_get('menu_trim_menu_'. $mid, MENU_TRIM_DISABLED) != MENU_TRIM_DISABLED) {
-          $blocks[$mid]['info'] = check_plain($title) .' ('. t('Menu trim') .')';
+      $menu = _menu_trim_settings_get();
+      if (isset($menu)) {
+        $blocks = array();
+        $menus = menu_get_root_menus();
+        foreach ($menus as $mid => $title) {
+          if ($menu[$mid] && _menu_trim_settings_get_method($menu, $mid) != MENU_TRIM_DISABLED) {
+            $blocks[$mid]['info'] = check_plain($title) .' ('. t('Menu Trim') .')';
+          }
         }
       }
       return $blocks;
       
-    case 'view':
-      if (($mode = variable_get('menu_trim_menu_'. $delta, MENU_TRIM_DISABLED)) != MENU_TRIM_DISABLED) {
-        // Get the trail of the active item in the $delta menu. If the active
-        // item is not in that menu, the menu won't be shown at all.
-        $trail = _menu_get_active_trail_in_submenu($delta);
-        if (is_array($trail)) {
-          // The active item is child of this menu, check for trimming
-          while ($mid = array_pop($trail)) {
-            if (variable_get('menu_trim_item_'. $mid, FALSE)) {
-              // Show the menu using the item as root
-              $item = menu_get_item($mid);
-              $data['subject'] = check_plain($item['title']);
-              $data['content'] = theme('menu_tree', $mid);
-              break;
-            }
-          }
-          if (!$data) {
-            // No trim flag found in the trail, show the full menu
-            $item = menu_get_item($delta);
-            $data['subject'] = check_plain($item['title']);
-            $data['content'] = theme('menu_tree', $delta);
-          }
-        }
-        elseif ($mode == MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS) {
-          // The active item is not child of this menu, show the plain menu
+    case 'view':
+      $menu = _menu_trim_settings_get();
+      $mode = _menu_trim_settings_get_method($menu, $delta);
+      if ($mode != MENU_TRIM_DISABLED) {
+        $trail = _menu_get_active_trail_in_submenu($delta);
+        // Active inclusion verification
+        if (is_array($trail)) {
+          $level = _menu_trim_settings_get_level($menu, $delta);
+          // Level validation
+          if ($level) {
+            $limit = _menu_trim_settings_get_limit($menu, $delta);
+            // Limit verification
+            if (!$limit || (count($trail) > $level)) { // Will only evaluate second part if it's a limit
+              // Trail verification : looking for an item marked trimmable or the level limit
+              while ($mid = array_pop($trail)) {
+                // Level verification                if (count($trail) == $level) {
+                  if ($limit) {
+                    // Show the menu using the item as base                    $item = menu_get_item($mid);
+                    $data['subject'] = check_plain(_menu_trim_settings_filter_title($menu, $delta, $item['title']));
+                    $data['content'] = theme('menu_tree', $mid);
+                    break;                  }
+                  else {
+                    $limit_mid = $mid;
+                    $limit_item = menu_get_item($mid);
+                  }
+                }
+                elseif (_menu_trim_menu_get($mid)) { // Level verification
+                  // Show the menu using the item as base                  $item = menu_get_item($mid);
+                  $data['subject'] = check_plain(_menu_trim_settings_filter_title($menu, $delta, $item['title']));
+                  $data['content'] = theme('menu_tree', $mid);
+                  break;                } // Level verification
+              } // Trail verification
+              // Apply minimum level if it wasn't a limit ; verify that no trailing items was marked and a level has been reached
+              if (!$data && isset($limit_mid, $limit_item)) { // Should have data if there was a limit so skip !$limit
+                // Show the menu using minimum level item as base
+                $data['subject'] = check_plain(_menu_trim_settings_filter_title($menu, $limit_mid, $limit_item['title']));
+                $data['content'] = theme('menu_tree', $limit_mid);
+              }
+            } // Limit verification
+          }
+          else { // Level validation
+            // Trail verification : looking for an item marked trimmable or the level limit            while ($mid = array_pop($trail)) {
+              if (_menu_trim_menu_get($mid)) {
+                // Show the menu using the item as base                $item = menu_get_item($mid);
+                $data['subject'] = check_plain(_menu_trim_settings_filter_title($menu, $delta, $item['title']));
+                $data['content'] = theme('menu_tree', $mid);
+                break;
+              }
+            }
+          } // Level validation
+        } // Active inclusion verification
+        // Verify if is supposed to always show a menu and none has been set
+        if ($mode == MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS && !$data) { 
+          // Show the menu using root item as base
           $item = menu_get_item($delta);
-          $data['subject'] = check_plain($item['title']);
+          $data['subject'] = check_plain(_menu_trim_settings_filter_title($menu, $delta, $item['title']));
           $data['content'] = theme('menu_tree', $delta);
         }
-        // Otherwise, $mode == MENU_TRIM_ENABLED_MENU_SHOW_ACTIVE, do not show
-        // the menu since it has to be shown only when it contains an active
-        // item
       }
       return $data;
   }
