Skip to content

Commit

Permalink
SERVER-4312 add meta-C, meta-L and meta-U to mongo shell
Browse files Browse the repository at this point in the history
Added meta-C (capitalize word), meta-L (lowercase word) and
meta-U (uppercase word) readline features to the shell.  Changed
the InputBuffer struct to a class, most members are now private.
Merged some common code for ctrl-R and ctrl-S keystrokes when
already in search mode.
  • Loading branch information
Tad Marshall committed Dec 22, 2011
1 parent 4691085 commit 6dbda20
Showing 1 changed file with 63 additions and 19 deletions.
82 changes: 63 additions & 19 deletions third_party/linenoise/linenoise.cpp
Expand Up @@ -341,21 +341,22 @@ class KillRing {

};

struct InputBuffer {
class InputBuffer {
char* buf;
int buflen;
int len;
int pos;

void clearScreen( PromptInfo& pi );
int incrementalHistorySearch( PromptInfo& pi, int startChar );
int completeLine( PromptInfo& pi );
void refreshLine( PromptBase& pi );

public:
InputBuffer( char* buffer, int bufferLen ) : buf( buffer ), buflen( bufferLen - 1 ), len( 0 ), pos( 0 ) {
buf[0] = 0;
}

int getInputLine( PromptInfo& pi );
void refreshLine( PromptBase& pi );
int completeLine( PromptInfo& pi );
void clearScreen( PromptInfo& pi );
int incrementalHistorySearch( PromptInfo& pi, int startChar );

};

Expand Down Expand Up @@ -1359,25 +1360,17 @@ int InputBuffer::incrementalHistorySearch( PromptInfo& pi, int startChar ) {

// these characters stay in search mode and update the display
case ctrlChar( 'S' ):
if ( dp.direction == -1 ) {
dp.direction = 1;
dp.updateSearchPrompt();
}
else {
searchAgain = true;
}
break;

case ctrlChar( 'R' ):
if ( dp.searchTextLen == 0 ) { // if no current search text, recall previous text
dp.updateSearchText( previousSearchText.c_str() );
}
if ( dp.direction == 1 ) {
dp.direction = -1;
dp.updateSearchPrompt();
if ( ( dp.direction == 1 && c == ctrlChar( 'R' ) ) ||
( dp.direction == -1 && c == ctrlChar( 'S' ) ) ) {
dp.direction = 0 - dp.direction; // reverse direction
dp.updateSearchPrompt(); // change the prompt
}
else {
searchAgain = true;
searchAgain = true; // same direction, search again
}
break;

Expand Down Expand Up @@ -1587,6 +1580,23 @@ int InputBuffer::getInputLine( PromptInfo& pi ) {
if ( write( 1, "^C", 2 ) == -1 ) return -1; // Display the ^C we got
return -1;

case META + 'c': // meta-C, give word initial Cap
case META + 'C':
killRing.lastAction = KillRing::actionOther;
if ( pos < len ) {
while ( pos < len && !isalnum( buf[pos] ) ) {
++pos;
}
if ( pos < len && buf[pos] >= 'a' && buf[pos] <= 'z' ) {
buf[pos] += 'A' - 'a';
}
while ( pos < len && isalnum( buf[pos] ) ) {
++pos;
}
refreshLine( pi );
}
break;

// ctrl-D, delete the character under the cursor
// on an empty line, exit the shell
case ctrlChar( 'D' ):
Expand Down Expand Up @@ -1704,6 +1714,23 @@ int InputBuffer::getInputLine( PromptInfo& pi ) {
clearScreen( pi );
break;

case META + 'l': // meta-L, lowercase word
case META + 'L':
killRing.lastAction = KillRing::actionOther;
if ( pos < len ) {
while ( pos < len && !isalnum( buf[pos] ) ) {
++pos;
}
while ( pos < len && isalnum( buf[pos] ) ) {
if ( buf[pos] >= 'A' && buf[pos] <= 'Z' ) {
buf[pos] += 'a' - 'A';
}
++pos;
}
refreshLine( pi );
}
break;

case ctrlChar( 'N' ): // ctrl-N, recall next line in history
case ctrlChar( 'P' ): // ctrl-P, recall previous line in history
case DOWN_ARROW_KEY:
Expand Down Expand Up @@ -1763,6 +1790,23 @@ int InputBuffer::getInputLine( PromptInfo& pi ) {
killRing.lastAction = KillRing::actionKill;
break;

case META + 'u': // meta-U, uppercase word
case META + 'U':
killRing.lastAction = KillRing::actionOther;
if ( pos < len ) {
while ( pos < len && !isalnum( buf[pos] ) ) {
++pos;
}
while ( pos < len && isalnum( buf[pos] ) ) {
if ( buf[pos] >= 'a' && buf[pos] <= 'z' ) {
buf[pos] += 'A' - 'a';
}
++pos;
}
refreshLine( pi );
}
break;

// ctrl-W, kill to whitespace (not word) to left of cursor
case ctrlChar( 'W' ):
if ( pos > 0 ) {
Expand Down

0 comments on commit 6dbda20

Please sign in to comment.