Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ctrl-Left and Ctrl-Right #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ void linenoiseEditBackspace(struct linenoiseState *l) {
}
}

/* Delete the previosu word, maintaining the cursor at the start of the
/* Delete the previous word, maintaining the cursor at the start of the
* current word. */
void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
size_t old_pos = l->pos;
Expand All @@ -783,6 +783,41 @@ void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
refreshLine(l);
}

/* Jump to the begining of the previous word */
void linenoiseEditPrevWord(struct linenoiseState *l) {
while (l->pos > 0 && l->buf[l->pos-1] == ' ')
l->pos--;
while (l->pos > 0 && l->buf[l->pos-1] != ' ')
l->pos--;
refreshLine(l);
}

/* Jump to the space after the current word */
void linenoiseEditNextWord(struct linenoiseState *l) {

while (l->pos < l->len)
{
if (l->buf[l->pos] != ' ')
break;
if (l->buf[l->pos] == '\0')
break;

l->pos++;
}

while (l->pos < l->len)
{
if (l->buf[l->pos] == ' ')
break;
if (l->buf[l->pos] == '\0')
break;

l->pos++;
}

refreshLine(l);
}

/* This function is the core of the line editing capability of linenoise.
* It expects 'fd' to be already in "raw mode" so that every key pressed
* will be returned ASAP to read().
Expand Down Expand Up @@ -821,7 +856,7 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
while(1) {
char c;
int nread;
char seq[3];
char seq[5];

nread = read(l.ifd,&c,1);
if (nread <= 0) return l.len;
Expand Down Expand Up @@ -901,12 +936,32 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
if (seq[1] >= '0' && seq[1] <= '9') {
/* Extended escape, read additional byte. */
if (read(l.ifd,seq+2,1) == -1) break;
if (seq[2] == '~') {
switch (seq[2]) {
case '~':
switch(seq[1]) {
case '3': /* Delete key. */
linenoiseEditDelete(&l);
break;
}
break;

case ';': ;
/* Even more extended escape, read additional 2 bytes */
if (read(l.ifd,seq+3,1) == -1) break;
if (read(l.ifd,seq+4,1) == -1) break;
if (seq[3] == '5')
{
switch(seq[4])
{
case 'D': // Ctrl Left
linenoiseEditPrevWord(&l);
break;
case 'C': // Ctrl Right
linenoiseEditNextWord(&l);
break;
}
}
break;
}
} else {
switch(seq[1]) {
Expand Down