Permalink
Browse files

Fix & improve the line editing

  • Loading branch information...
1 parent ac1a527 commit 86c567491505dde0405350657b9e9200dc085e94 Sound and Fury committed Sep 16, 2010
Showing with 103 additions and 59 deletions.
  1. +1 −0 bits.c
  2. +5 −5 buffer.c
  3. +16 −4 config.c
  4. +68 −46 input.c
  5. +3 −2 input.h
  6. +8 −1 quirc.c
  7. +2 −1 ttyraw.c
View
@@ -148,6 +148,7 @@ void append_char(char **buf, int *l, int *i, char c)
else
{
init_char(buf, l, i);
+ append_char(buf, l, i, c);
}
char *nbuf=*buf;
if((*i)>=(*l))
View
@@ -328,10 +328,10 @@ void in_update(iline inp)
}
else if(inp.right.i<width*0.75)
{
- char ll[width-inp.right.i-3-max(3, (width-inp.right.i)/4)];
- snprintf(ll, width-inp.right.i-3-max(3, (width-inp.right.i)/4), "%s", inp.left.data);
+ char ll[max(3, (width-inp.right.i)/4)];
+ snprintf(ll, max(3, (width-inp.right.i)/4), "%s", inp.left.data);
char *llh=highlight(ll);
- char *lrh=highlight(inp.left.data+inp.left.i-max(3, (width-inp.right.i)/4));
+ char *lrh=highlight(inp.left.data+inp.left.i-width+3+inp.right.i+max(3, (width-inp.right.i)/4));
char *rh=highlight(inp.right.data?inp.right.data:"");
printf("%s...%s" SAVEPOS "%s" CLR RESTPOS, llh, lrh, rh);
free(llh);
@@ -347,8 +347,8 @@ void in_update(iline inp)
snprintf(ll, max(3, c/4)+1, "%s", inp.left.data);
char *llh=highlight(ll);
char *lrh=highlight(inp.left.data+torem+max(3, c/4));
- char rl[width-c-3-max(3, (width-c)/4)];
- snprintf(rl, width-c-3-max(3, (width-c)/4), "%s", inp.right.data);
+ char rl[width-c-2-max(3, (width-c)/4)];
+ snprintf(rl, width-c-2-max(3, (width-c)/4), "%s", inp.right.data);
char *rlh=highlight(rl);
char *rrh=highlight(inp.right.data+inp.right.i-max(3, (width-c)/4));
printf("%s...%s" SAVEPOS "%s...%s" CLR RESTPOS, llh, lrh, rlh, rrh);
View
@@ -35,8 +35,8 @@ int def_config(void)
maxnlen=16;
servs=NULL;
igns=NULL;
- portno="6667";
- username="quirc";
+ portno=strdup("6667");
+ username=strdup("quirc");
fname=(char *)malloc(64+strlen(VERSION_TXT));
nick=strdup("ac");
sprintf(fname, "quIRC %hhu.%hhu.%hhu%s%s : http://github.com/ec429/quIRC", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT[0]?"-":"", VERSION_TXT);
@@ -403,13 +403,25 @@ signed int pargs(int argc, char *argv[])
servs->igns=NULL;
}
else if(strncmp(argv[arg], "--port=", 7)==0)
- portno=argv[arg]+7;
+ {
+ free(portno);
+ portno=strdup(argv[arg]+7);
+ }
else if(strncmp(argv[arg], "--uname=", 8)==0)
- username=argv[arg]+8;
+ {
+ free(username);
+ username=strdup(argv[arg]+8);
+ }
else if(strncmp(argv[arg], "--fname=", 8)==0)
+ {
+ free(fname);
fname=argv[arg]+8;
+ }
else if(strncmp(argv[arg], "--nick=", 7)==0)
+ {
+ free(nick);
nick=strdup(argv[arg]+7);
+ }
else if(servs && (strncmp(argv[arg], "--chan=", 7)==0))
{
chanlist * new=(chanlist *)malloc(sizeof(chanlist));
View
@@ -12,9 +12,7 @@ int inputchar(iline *inp, int *state)
{
printf("\010\010\010" CLA);
unsigned char c=getchar();
- if(!inp->left.i)
- init_ichar(&inp->left);
- append_ichar(&inp->left, c);
+ append_char(&inp->left.data, &inp->left.l, &inp->left.i, c);
if(c!='\t')
ttab=false;
if(strchr("\010\177", c)) // various backspace-type characters
@@ -25,18 +23,36 @@ int inputchar(iline *inp, int *state)
else if((c<32) || (c==0xc2)) // this also stomps on the newline
{
back_ichar(&inp->left);
- if(c==1)
+ if(c==1) // C-a ~= home
+ {
+ i_home(inp);
+ }
+ if(c==5) // C-e ~= end
+ {
+ i_end(inp);
+ }
+ if(c==3) // C-c ~= clear
+ {
+ ifree(inp);
+ }
+ if(c==24) // C-x ~= clear to left
{
- char *nr=(char *)malloc(inp->left.i+inp->right.i+1);
- sprintf(nr, "%s%s", inp->left.data?inp->left.data:"", inp->right.data?inp->right.data:"");
free(inp->left.data);
inp->left.data=NULL;
- free(inp->right.data);
- inp->right.data=nr;
- inp->right.l=inp->left.i+inp->right.i+1;
- inp->right.i=inp->right.l-1;
inp->left.i=inp->left.l=0;
}
+ if(c==11) // C-k ~= clear to right
+ {
+ free(inp->right.data);
+ inp->right.data=NULL;
+ inp->right.i=inp->right.l=0;
+ }
+ if(c==23) // C-w ~= backspace word
+ {
+ while(back_ichar(&inp->left)==' ');
+ while(!strchr(" ", back_ichar(&inp->left)));
+ append_char(&inp->left.data, &inp->left.l, &inp->left.i, ' ');
+ }
if(c=='\t') // tab completion of nicks
{
int sp=max(inp->left.i-1, 0);
@@ -162,8 +178,10 @@ int inputchar(iline *inp, int *state)
case 'C': // ^[[C // Right
if(inp->right.data && *inp->right.data)
{
- append_ichar(&inp->left, inp->right.data[0]);
- inp->right.data=strdup(inp->right.data+1);
+ append_char(&inp->left.data, &inp->left.l, &inp->left.i, inp->right.data[0]);
+ char *nr=strdup(inp->right.data+1);
+ free(inp->right.data);
+ inp->right.data=nr;
inp->right.i--;
inp->right.l=0;
}
@@ -192,28 +210,10 @@ int inputchar(iline *inp, int *state)
}
break;
case 'H': // ^[[H // Home
- if(inp->left.i)
- {
- size_t b=inp->left.i+inp->right.i;
- char *nr=(char *)malloc(b+1);
- sprintf(nr, "%s%s", inp->left.data?inp->left.data:"", inp->right.data?inp->right.data:"");
- ifree(inp);
- inp->right.data=nr;
- inp->right.i=b;
- inp->right.l=b+1;
- }
+ i_home(inp);
break;
case 'F': // ^[[F // End
- if(inp->right.i)
- {
- size_t b=inp->left.i+inp->right.i;
- char *nl=(char *)malloc(b+1);
- sprintf(nl, "%s%s", inp->left.data?inp->left.data:"", inp->right.data?inp->right.data:"");
- ifree(inp);
- inp->left.data=nl;
- inp->left.i=b;
- inp->left.l=b+1;
- }
+ i_end(inp);
break;
case '3': // take another
if(getchar()=='~') // delete
@@ -1398,24 +1398,18 @@ void addtoibuf(ibuffer *i, char *data)
void freeibuf(ibuffer *i)
{
- int l;
- for(l=0;l<(i->filled?i->nlines:i->ptr);l++)
+ if(i->line)
{
- if(i->line[l])
- free(i->line[l]);
+ int l;
+ for(l=0;l<(i->filled?i->nlines:i->ptr);l++)
+ {
+ if(i->line[l])
+ free(i->line[l]);
+ }
+ free(i->line);
}
}
-void init_ichar(ichar *buf)
-{
- init_char(&buf->data, &buf->l, &buf->i);
-}
-
-void append_ichar(ichar *buf, char c)
-{
- append_char(&buf->data, &buf->l, &buf->i, c);
-}
-
char back_ichar(ichar *buf)
{
char c=0;
@@ -1436,3 +1430,31 @@ void ifree(iline *buf)
buf->left.i=buf->left.l=0;
buf->right.i=buf->right.l=0;
}
+
+void i_home(iline *inp)
+{
+ if(inp->left.i)
+ {
+ size_t b=inp->left.i+inp->right.i;
+ char *nr=(char *)malloc(b+1);
+ sprintf(nr, "%s%s", inp->left.data?inp->left.data:"", inp->right.data?inp->right.data:"");
+ ifree(inp);
+ inp->right.data=nr;
+ inp->right.i=b;
+ inp->right.l=b+1;
+ }
+}
+
+void i_end(iline *inp)
+{
+ if(inp->right.i)
+ {
+ size_t b=inp->left.i+inp->right.i;
+ char *nl=(char *)malloc(b+1);
+ sprintf(nl, "%s%s", inp->left.data?inp->left.data:"", inp->right.data?inp->right.data:"");
+ ifree(inp);
+ inp->left.data=nl;
+ inp->left.i=b;
+ inp->left.l=b+1;
+ }
+}
View
@@ -53,7 +53,8 @@ int cmd_handle(char *inp, char **qmsg, fd_set *master, int *fdmax);
void initibuf(ibuffer *i);
void addtoibuf(ibuffer *i, char *data);
void freeibuf(ibuffer *i);
-void init_ichar(ichar *buf); // wrapper around init_char
-void append_ichar(ichar *buf, char c); // wrapper around append_char
char back_ichar(ichar *buf); // returns the deleted char
void ifree(iline *buf);
+
+void i_home(iline *inp);
+void i_end(iline *inp);
View
@@ -321,7 +321,7 @@ int main(int argc, char *argv[])
if(state>0)
printf("quirc exiting\n");
int b;
- for(b=1;b<nbufs;b++)
+ for(b=0;b<nbufs;b++)
{
if((bufs[b].live) && (bufs[b].type==SERVER) && (bufs[b].handle!=0))
{
@@ -334,6 +334,13 @@ int main(int argc, char *argv[])
free_buffer(b);
b--;
}
+ if(bufs) free(bufs);
+ if(username) free(username);
+ if(fname) free(fname);
+ if(nick) free(nick);
+ if(portno) free(portno);
+ freeservlist(servs);
+ n_free(igns);
ttyreset(STDOUT_FILENO);
#ifdef USE_MTRACE
muntrace();
View
@@ -9,6 +9,7 @@ int ttyraw(int fd)
{
/* Set terminal mode as follows:
Noncanonical mode - turn off ICANON.
+ Disable signal generation - turn off ISIG
Turn ECHO mode off.
Disable input parity detection (INPCK).
Disable stripping of eighth bit on input (ISTRIP).
@@ -22,7 +23,7 @@ int ttyraw(int fd)
return(-1);
newtermios = oldtermios;
- newtermios.c_lflag &= ~(ICANON | ECHO);
+ newtermios.c_lflag &= ~(ICANON | ISIG | ECHO);
newtermios.c_iflag &= ~(INPCK | ISTRIP | IXON);
newtermios.c_cflag &= ~(CSIZE | PARENB);
newtermios.c_cflag |= CS8;

0 comments on commit 86c5674

Please sign in to comment.