uAutoCompleter Class Reference

#include <uMenu.h>

Inheritance diagram for uAutoCompleter:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 uAutoCompleter (std::deque< tString > &words)
 Constructor.
virtual int Complete (tString &string, unsigned pos)
 Attempts the completion.
virtual ~uAutoCompleter ()
void SetIgnorecase (bool ignorecase)
 Enable or disable case ignoring?

Protected Member Functions

virtual int FindLengthOfLastWord (tString &string, unsigned pos)
 Finds the space to the last delimiter.
virtual void FindPossibleWords (tString word, std::deque< tString > &results)
 Finds the possible completions for a word.
virtual tString FindClosestMatch (tString &word, std::deque< tString > &results)
 Attempts to complete as much of the word as possible.
virtual void ShowPossibilities (std::deque< tString > &results, tString &word)
 Prints the possible completions to the console.
virtual int DoCompletion (tString &string, int pos, int len, tString &match)
 Replaces the word the cursor is on by the closest match.
virtual int DoFullCompletion (tString &string, int pos, int len, tString &match)
 Replaces the word the cursor is on by the given match and a space.
virtual int TryCompletion (tString &string, unsigned pos, unsigned len)
 Attempt completion with a certain word length.
virtual tString Simplify (tString const &str)
 Simplifies a string, by default converts it to lowercase.

Protected Attributes

std::deque< tString > & m_PossibleWords
 The words that can be used for completion.
int m_LastCompletion
bool m_ignorecase


Detailed Description

A class that can provide auto- completion and supports overwriting of parts for special cases.

The default implementation implements the typical bash- like auto- completion, if you need more than that derive your own class.

Definition at line 412 of file uMenu.h.


Constructor & Destructor Documentation

uAutoCompleter::uAutoCompleter ( std::deque< tString > &  words  ) 

Constructor.

Parameters:
words a deque containing the possible words that can be used for completion

Definition at line 868 of file uMenu.cpp.

00870                                                        :
00871         m_PossibleWords(words),
00872         m_LastCompletion(-1),
        m_ignorecase(true)

virtual uAutoCompleter::~uAutoCompleter (  )  [inline, virtual]

Definition at line 431 of file uMenu.h.

00431 {}


Member Function Documentation

int uAutoCompleter::FindLengthOfLastWord ( tString string,
unsigned  pos 
) [protected, virtual]

Finds the space to the last delimiter.

Parameters:
string the string that should be tested
pos the cursor position within the string
Returns:
-1 if the cursor isn't at the end of a word, the number of characters to the previous space if it is

Definition at line 877 of file uMenu.cpp.

00879                                                                       {
00880     int charright = ' ', charleft  = ' ';
00881     if(string.size() >= 1) {
00882         if (pos == string.size()) {
00883             charleft=string.at(pos-1);
00884         }
00885         else if (pos < string.size()) {
00886             charright=string.at(pos);
00887             if(pos>1) {
00888                 charleft=string.at(pos-1);
00889             }
00890         }
00891     }
00892     if(charright != ' ' && charleft != ' ')
00893         return -1; //no completion possible
00894     if(charleft == ' ')
00895         return 0;
00896     else

void uAutoCompleter::FindPossibleWords ( tString  word,
std::deque< tString > &  results 
) [protected, virtual]

Finds the possible completions for a word.

Parameters:
word the part of the word that is searched for
results the list where the possible completions will be saved

Definition at line 900 of file uMenu.cpp.

00902                                                                                {
00903     if(m_ignorecase)
00904         word = Simplify(word);
00905     for(std::deque<tString>::iterator i=m_PossibleWords.begin(); i!=m_PossibleWords.end(); ++i) {
00906         size_t pos;
00907         if((pos = (m_ignorecase ? Simplify(*i) : *i).find(word)) != tString::npos) {
00908             if(isalpha((*i)[pos]) && pos != 0 && isalpha((*i)[pos-1]))
00909                 continue; //both the char we're at and the one before is alphanumeric; we're in the middle of a word
00910             results.push_back(*i);
00911         }

tString uAutoCompleter::FindClosestMatch ( tString word,
std::deque< tString > &  results 
) [protected, virtual]

Attempts to complete as much of the word as possible.

Parameters:
word the part of the word that is searched for
results the list of possible completions
Returns:
the match, if any

Definition at line 916 of file uMenu.cpp.

00918                                                                                   {
00919     tString ret(m_ignorecase?Simplify(word):word);
00920     unsigned int len = ret.size();
00921     while(true) { // complete right side
00922         std::deque<tString>::iterator i;
00923         i = results.begin();
00924         bool found=true;
00925         tString::size_type pos=(m_ignorecase?Simplify(*i):*i).find(ret);
00926         if(pos!=tString::npos && pos+len < i->size()) {
00927             found=false;
00928             ret+=(m_ignorecase ? tolower(i->at(pos+len)) : i->at(pos+len));
00929             ++i;
00930             for(; i!=results.end(); ++i) {
00931                 if((m_ignorecase?Simplify(*i):*i).find(ret) == tString::npos) {
00932                     found=true;
00933                     ret.erase(ret.length()-1);
00934                     break;
00935                 }
00936             }
00937         }
00938         if (found) //we found a mismatch
00939             break;
00940         else
00941             len++;
00942     }
00943     while(true) { // something similar, but now for the left side of the word
00944         std::deque<tString>::iterator i;
00945         i = results.begin();
00946         bool found=true;
00947         tString::size_type pos=(m_ignorecase?Simplify(*i):*i).find(ret);
00948         if(pos!=tString::npos && pos > 0) {
00949             found=false;
00950             ret.insert(ret.begin(),(m_ignorecase ? tolower(i->at(pos-1)) : i->at(pos-1)));
00951             ++i;
00952             for(; i!=results.end(); ++i) {
00953                 if((m_ignorecase?Simplify(*i):*i).find(ret) == tString::npos) {
00954                     found=true;
00955                     ret.erase(0,1);
00956                     break;
00957                 }
00958             }
00959         }
00960         if (found)  //we found a mismatch
00961             break;
00962         else
00963             len++;
00964     }

void uAutoCompleter::ShowPossibilities ( std::deque< tString > &  results,
tString word 
) [protected, virtual]

Prints the possible completions to the console.

Parameters:
results the list of possible completions
word the word that was already typed, it will be highlighted

Definition at line 968 of file uMenu.cpp.

00970                                                                                 {
00971     if(results.size() > 10) {
00972         con << tOutput("$tab_completion_toomanyresults");
00973     }
00974     else {
00975         con << tOutput("$tab_completion_results");
00976         tString::size_type len=word.length();
00977         for(std::deque<tString>::iterator i=results.begin(); i!=results.end(); ++i) {
00978             tString::size_type pos=(m_ignorecase?Simplify(*i):*i).find(word);
00979             con << i->SubStr(0,pos)
00980             << "0xff8888"
00981             << i->SubStr(pos, len)
00982             << "0xffffff"
00983             << i->SubStr(pos+len)
00984             << "\n";
00985         }

int uAutoCompleter::DoCompletion ( tString string,
int  pos,
int  len,
tString match 
) [protected, virtual]

Replaces the word the cursor is on by the closest match.

Parameters:
string the string in which the completion should take place
pos the position in strind where the replaced word ends
len the length of the word that is already there
match the string that will be inserted into the other one
Returns:
the new cursor position

Definition at line 992 of file uMenu.cpp.

00994                                                                                   {
00995     string.erase(pos-len, len);
00996     string.insert(pos-len, match);

int uAutoCompleter::DoFullCompletion ( tString string,
int  pos,
int  len,
tString match 
) [protected, virtual]

Replaces the word the cursor is on by the given match and a space.

Parameters:
string the string in which the completion should take place
pos the position in string where the replaced word ends
len the length of the word that is already there
match the string that will be inserted into the other one
Returns:
the new cursor position

Reimplemented in eAutoCompleterChat.

Definition at line 1003 of file uMenu.cpp.

01005                                                                                       {
01006     tString actualString = match + " ";

int uAutoCompleter::TryCompletion ( tString string,
unsigned  pos,
unsigned  len 
) [protected, virtual]

Attempt completion with a certain word length.

Parameters:
string the string in which the completion should take place
pos the cursor position
len the length of the word
Returns:
the new position on success (something was found), -1 on failure

Definition at line 1012 of file uMenu.cpp.

01014                                                                              {
01015     tString word(string.SubStr(pos-len, len));
01016     std::deque<tString> results;
01017     FindPossibleWords(word, results);
01018     if(results.size() > 1){
01019         tString match(FindClosestMatch(word, results));
01020         if(match == word) { //no completion took place
01021             ShowPossibilities(results, word);
01022             return pos;
01023         }
01024         else { //do the completion
01025             return DoCompletion(string, pos, len, match);
01026         }
01027     }
01028     else if(!results.empty()){
01029         return DoFullCompletion(string, pos, len, results.front());
01030     }

tString uAutoCompleter::Simplify ( tString const &  str  )  [protected, virtual]

Simplifies a string, by default converts it to lowercase.

Parameters:
str the string to simplify
Returns:
the simplified string(in this case everything converted to lowercase)

Reimplemented in eAutoCompleterChat.

Definition at line 1057 of file uMenu.cpp.

01059                                                    {

int uAutoCompleter::Complete ( tString string,
unsigned  pos 
) [virtual]

Attempts the completion.

Parameters:
string the string in which the completion should take place
pos the cursor position
Returns:
the new cursor position

Reimplemented in gAutoCompleterConsole.

Definition at line 1035 of file uMenu.cpp.

Referenced by gAutoCompleterConsole::Complete().

01037                                                           {
01038     if(m_LastCompletion != -1 && (unsigned)m_LastCompletion < pos) {
01039         int res = TryCompletion(string, pos, pos - m_LastCompletion);
01040         if(res != -1) return res;
01041     }
01042     int len = FindLengthOfLastWord(string, pos);
01043     if(len == -1) return pos; //in the middle of a word...
01044     int res = TryCompletion(string, pos, len);
01045     if(res != -1) {
01046         m_LastCompletion = pos - len;
01047         return res;
01048     }

Here is the caller graph for this function:

void uAutoCompleter::SetIgnorecase ( bool  ignorecase  ) 

Enable or disable case ignoring?

Parameters:
ignorecase ignore upper and lower case?

Definition at line 1051 of file uMenu.cpp.

References m_ignorecase.

01053                                                   {


Member Data Documentation

std::deque<tString>& uAutoCompleter::m_PossibleWords [protected]

The words that can be used for completion.

Definition at line 414 of file uMenu.h.

int uAutoCompleter::m_LastCompletion [protected]

Definition at line 415 of file uMenu.h.

bool uAutoCompleter::m_ignorecase [protected]

Definition at line 416 of file uMenu.h.

Referenced by SetIgnorecase().


The documentation for this class was generated from the following files:
Generated on Sat Mar 15 23:59:53 2008 for Armagetron Advanced by  doxygen 1.5.4