void TextSearch::SetLastResult(TextSelection *sel)
{
    CopySelection(sel);

    ScopedMem<WCHAR> selection(ExtractText(L" "));
    str::NormalizeWS(selection);
    SetText(selection);

    findPage = min(startPage, endPage);
    findIndex = (findPage == startPage ? startGlyph : endGlyph) + (int)str::Len(findText);
    pageText = textCache->GetData(findPage);
    forward = true;
}
CString CTemplateFormater::Format()
{
	ASSERT(!m_strSource.IsEmpty());
	CString szTmpl = CString(m_strSource);
	int iIndex, iTableSize;
	CString szKey, szText;
	int iStartPos, iNextPos;
	
	// Translate the keyword table...
	iTableSize = ARRAY_SIZE(s_KeywordTable);
	iIndex     = 0;
	// Iterate through the table
	while (iIndex < iTableSize)
	{
		// What is the keyword?
		szKey = KEYWORD_BEGIN;
		szKey += s_KeywordTable[iIndex].szKeyword;
		szKey += KEYWORD_END;
		
		// Substitute the correct value for the keyword
		szTmpl.Replace(szKey, s_KeywordTable[iIndex].Proc());
		
		// Next
		iIndex++;
	};
	
	// Translate replaceble parameters
	CStringArray ParamTable;
	
	GetReplacebleParams(ParamTable, szTmpl);
	
	iTableSize = ParamTable.GetSize();
	iIndex = 0;
	
	while (iIndex < iTableSize)
	{
		// What is the keyword?
		szKey  = PARAM_BEGIN;
		szKey += ParamTable[iIndex];
		szKey += PARAM_END;
		
		// Substitute the correct value for the keyword
		szTmpl.Replace(szKey, GetParam(ParamTable[iIndex]));
		
		// Next
		iIndex++;
	}
	
	// Translate the format specifiers
	iTableSize = ARRAY_SIZE(s_FormatSpecTable);
	iIndex     = 0;
	// Iterate through the table
	while (iIndex < iTableSize)
	{
		// What is the specifier?
		szKey = FORMAT_BEGIN;
		szKey += s_FormatSpecTable[iIndex].szSpecifier;
		iStartPos = 0;
		
		// Replace each occurrence of the specifier with
		// the new value
		while (GetNextInstance(szTmpl, szKey, iStartPos))
		{
			// Extract the enclosed text
			szText = ExtractText(szTmpl, szKey, iStartPos, iNextPos);
			
			// Replace the specifier and enclosed text with the
			// updated version
			szTmpl = ReplaceSpecifier(szTmpl, iStartPos, iNextPos,
				s_FormatSpecTable[iIndex].Proc(szText));
			
			iStartPos = iNextPos;
		};
		
		// Next
		iIndex++;
	};
	
	return (szTmpl);
}