/* -*-ObjC-*-
*******************************************************************************
*
* File:         TocMatrix.m
* RCS:          /usr/local/sources/CVS/EnhanceMail/TocMatrix.m,v 1.6 1998/07/02 22:00:17 tom Exp
* Description:  
* Author:       Carl Edman
* Created:      Fri Oct 20 14:47:00 1995
* Modified:     Fri Apr 19 13:32:20 1996 (Carl Edman) cedman@capitalist.princeton.edu
* Language:     C
* Package:      N/A
* Status:       Experimental (Do Not Distribute)
*
* (C) Copyright 1995, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import "EnhanceMail.h"
#import <regex.h>
#import "TocMatrix.h"
#import "MailBox.h"
#import "SimpleString.h"
#import "Preferences.h"

static id cellar=nil;
static BOOL searchFlagged = NO;

@implementation EnhanceTocMatrix

+ finishLoading:(struct mach_header *)header
{
   [self poseAs:[self superclass]];
   return self;
}

- findForward:(BOOL)forward all:(BOOL)all
{
   id panel=[FindPanel new];
   id box=[[window delegate] mailbox];
   id ret;
   const char *findString = [panel findField];

   if ([panel searchMessage] && ![panel regExpr] && findString &&
       (box!=nil) && [box hasStore])
   {
      id list=nil,tmp=nil;
      int i;
      
      if (cellar==nil) cellar=[[HashTable alloc] init];

      tmp=[[SimpleString alloc] init];
      [tmp appendString:"whole(Content parse("];
      [tmp appendString:findString];
      [tmp appendString:"))"];
      list=[box query:[tmp string]];
      tmp=[tmp free];
      
      if (list!=nil)
      {
         for(i=[list count]-1;i>=0;i--)
	 {
            tmp=[self cellForMsgId:[[list objectAt:i] msgId]];
            if (tmp) [cellar insertKey:tmp value:tmp];
	 }
         list=[[list freeObjects] free];
      }
   }
   else
   {
      if (cellar!=nil) cellar=[cellar free];
   }

   // match flagged messages when searching for EnhanceFlagSearchString
   // -- analogous to unread messages when searching for "·" (Alt-7)
   searchFlagged = (EnhanceFlagSearchString && *EnhanceFlagSearchString &&
		    strcmp(EnhanceFlagSearchString, findString) == 0 &&
		    ![panel searchMessage]);

   ret=[super findForward:forward all:all];
   if (cellar!=nil) cellar=[cellar free];
   searchFlagged = NO;
   return ret;
}


#define ENHANCE_MATCH_CELL(mailCell, inMes) { \
	    if (searchFlagged && !inMes && [mailCell isFlagged]) return YES; \
	    if ((cellar!=nil) && ![cellar isKey:mailCell]) return NO; \
	}

// This is invoked by [super findForward:all:]
- (BOOL)cell:(MailCell *)mailCell containsRegExpr:(struct regex *)regex searchMessage:(BOOL)inMes
{
   ENHANCE_MATCH_CELL(mailCell, inMes);
   return [super cell:mailCell containsRegExpr:regex searchMessage:inMes];
}

// This is used in Japanese Mail.app instead.
- (BOOL)cell:(MailCell *)mailCell contains16bitRegExpr:(struct regexJ *)regex searchMessage:(BOOL)inMes
{
   ENHANCE_MATCH_CELL(mailCell, inMes);
   return [super cell:mailCell contains16bitRegExpr:regex searchMessage:inMes];
}

@end // EnhanceTocMatrix
