X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=pd_readline.c;h=b4b886efa8ef540bb510ffbed48c4a55ebf1141c;hb=1ad7deb43b6a1157e9d9afac48d7e66e41fa7356;hp=5bf04dc7d8715e2388ebb8fb247653535c522a95;hpb=4bb27266f935c9aafad6870ffc8847fc65c8120f;p=pd_readline diff --git a/pd_readline.c b/pd_readline.c index 5bf04dc..b4b886e 100644 --- a/pd_readline.c +++ b/pd_readline.c @@ -7,8 +7,21 @@ /* up-arrow key. */ /* This code is released to the public domain. */ /* "Share and enjoy...." ;) */ +/* See the UNLICENSE file for details. */ + +/* TO DO - */ +/* a) Add support for Home and End (of line) keys. */ +/* b) Add support for function keys. */ +/* c) Add support for Insert key so that text can be */ +/* inserted. */ +/* d) Put much of the code into a header file. */ +/* e) Change so that pressing Enter adds the current */ +/* line of commands to the command-history. */ +/* ( May look at using Ctrl-D to exit, as Python */ +/* does with its command-line. ) */ +/* f) Add support for copying and pasting text via */ +/* Ctrl-C and Ctrl-V. */ - #include #include @@ -85,6 +98,7 @@ char *chop(char *s) char hist[20][80]; + /* Read the file into the array of strings. */ void readfile(char *fname) { @@ -141,11 +155,6 @@ int main(void) /* Our "command-history" file. */ readfile("test.txt"); -/* Main buffer for the command-line. */ -char buffer[80]; - -/* Main buffer "pointer" */ -int bufpnt = 0; /* "Pointer" for the history file. */ int histpnt = 20; @@ -154,16 +163,13 @@ int histpnt = 20; while(1) { - int key = getch(); - buffer[bufpnt] = key; - bufpnt += 1; + int key = getch(); /* Printable chars. */ if ( (key >= 32) && (key <= 126) ) { /* We have a printable key so print it. */ - putchar(key); - bufpnt += 1; + putchar(key); } /* Up arrow is 27, 91, 65. ( ESC [ A ) */ @@ -178,13 +184,10 @@ int histpnt = 20; /* Backspace */ else if(key == 127) { - /* Move left 1 char and delete that char */ - bufpnt -= 1; + /* Move left 1 char and delete that char */ printf("\033[1D"); printf("\040"); - printf("\033[1D"); - /* Move 1 char to left again */ - bufpnt -= 1; + printf("\033[1D"); } /* We have an escape key-sequence */ @@ -200,11 +203,7 @@ int histpnt = 20; histpnt -= 1; /* Clear to end of line. */ printf("\033[80D"); - printf("\033[K"); - /* Move buffer pointer to start of line */ - bufpnt = 0; - /* Clear the array. */ - memset(buffer, 0, sizeof(char)*80); + printf("\033[K"); /* Print the pointed-at command-sequence. */ putline(hist[histpnt]); } @@ -215,24 +214,20 @@ int histpnt = 20; histpnt += 1; /* Clear to end of line. */ printf("\033[80D"); - printf("\033[K"); - /* Move buffer pointer to start of line */ - bufpnt = 0; - /* Clear the array. */ - memset(buffer, 0, sizeof(char)*80); + printf("\033[K"); /* Print the pointed-at command-sequence. */ putline(hist[histpnt]); } if(key == 67) /* Left arrow */ { - /* Move one character to the left. */ + /* Move one character to the left. */ printf("\033[1C"); } if(key == 68) /* Right arrow */ { - /* Move one character to the right. */ + /* Move one character to the right. */ printf("\033[1D"); }