Skip to content

Commit

Permalink
fix #102 multi-line command line editing
Browse files Browse the repository at this point in the history
  • Loading branch information
boeckmann authored and PerditionC committed Jun 2, 2024
1 parent 5a55f8a commit 9ec4779
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef enum {
unsigned char far *maxy = MK_FP(0x40, 0x84);
*/
#define MAX_X (*(unsigned int far*)MK_FP(0x40, 0x4a))
#define MAX_Y (*(unsigned char far*)MK_FP(0x40, 0x84) == 0 ? 25 : *(unsigned char far*)MK_FP(0x40, 0x84)) /* when 0040:0084 contains 0, assume 25 rows (CGA...) */
#define MAX_Y (*(unsigned char far*)MK_FP(0x40, 0x84) == 0 ? 24 : *(unsigned char far*)MK_FP(0x40, 0x84)) /* when 0040:0084 contains 0, assume 25 rows (CGA...) */
#define SCREEN_COLS MAX_X
#define SCREEN_ROWS (MAX_Y + 1)

Expand Down
40 changes: 30 additions & 10 deletions lib/cmdinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "../include/keys.h"
#include "../include/misc.h"

static unsigned orgx, orgy; /* start of current line */
static unsigned orgx, orgy; /* start of current command input */

#define MK_PTR(type,seg,ofs) ((type FAR*) MK_FP (seg, ofs))
/* safer edition of MK_FP (Arkady) */
Expand Down Expand Up @@ -137,6 +137,14 @@ static void clrcmdline(char * const str, const unsigned pos, const int maxlen)
}
}

/* calculates the number of lines to scroll if cursor is initially at position
curx,cury and cursor is advanced numchars characters */
static unsigned numLinesToScroll(const unsigned curx, unsigned cury, const unsigned numchars)
{
cury += (curx + numchars - 1) / SCREEN_COLS;
return (cury <= SCREEN_ROWS) ? 0 : cury - SCREEN_ROWS;
}

/* read in a command line */
void readcommandEnhanced(char * const str, const int maxlen)
{
Expand All @@ -147,10 +155,11 @@ void readcommandEnhanced(char * const str, const int maxlen)
#endif
#ifdef FEATURE_HISTORY
int histLevel = 0;
char prvLine[MAX_INTERNAL_COMMAND_SIZE];
char prvLine[MAX_INTERNAL_COMMAND_SIZE] = { '\0' };
#endif
unsigned curx;
unsigned cury;
unsigned lines;
int count;
unsigned current = 0;
unsigned charcount = 0;
Expand All @@ -164,6 +173,7 @@ void readcommandEnhanced(char * const str, const int maxlen)

orgx = wherex();
orgy = wherey();

memset(str, 0, maxlen);

_setcursortype(_NORMALCURSOR);
Expand Down Expand Up @@ -259,18 +269,17 @@ void readcommandEnhanced(char * const str, const int maxlen)
complete_filename(str, charcount);
charcount = strlen(str);
current = charcount;

goxy(orgx, orgy);
outs(str);
if ((strlen(str) > (MAX_X - orgx)) && (orgy == MAX_Y + 1))
orgy--;
orgy -= numLinesToScroll(orgx, orgy, charcount);
} else { /* if second TAB, list matches */
if (show_completion_matches(str, charcount))
{
printprompt();
orgx = wherex();
orgy = wherey();
outs(str);
orgy -= numLinesToScroll(orgx, orgy, charcount);
}
}
}
Expand All @@ -280,6 +289,12 @@ void readcommandEnhanced(char * const str, const int maxlen)
#endif

case KEY_ENTER: /* end input, return to main */
if (current != charcount) {
/* make sure we locate cursor to end of command before
spawning process */
goxy(orgx, orgy);
outs(str);
}

#ifdef FEATURE_HISTORY
if(str[0])
Expand Down Expand Up @@ -332,6 +347,7 @@ void readcommandEnhanced(char * const str, const int maxlen)
if(charcount < strlen(prvLine)) {
outs(strcpy(&str[charcount], &prvLine[charcount]));
current = charcount = strlen(str);
orgy -= numLinesToScroll(orgx, orgy, charcount);
}
break;

Expand All @@ -342,6 +358,7 @@ void readcommandEnhanced(char * const str, const int maxlen)
clrcmdline(str, current, maxlen);
strcpy(str, prvLine);
current = charcount = strlen(str);
orgy -= numLinesToScroll(orgx, orgy, charcount);
outs(str);
histGet(histLevel - 1, prvLine, sizeof(prvLine));
}
Expand All @@ -353,6 +370,7 @@ void readcommandEnhanced(char * const str, const int maxlen)
strcpy(prvLine, str);
histGet(++histLevel, str, maxlen);
current = charcount = strlen(str);
orgy -= numLinesToScroll(orgx, orgy, charcount);
outs(str);
}
break;
Expand Down Expand Up @@ -422,23 +440,25 @@ void readcommandEnhanced(char * const str, const int maxlen)
for (count = charcount; count > current; count--)
str[count] = str[count - 1];
str[current++] = ch;
charcount++;
curx = wherex() + 1;
cury = wherey();
if (curx > MAX_X) { curx = 1; cury++; };
outs(&str[current - 1]);
if ((strlen(str) > (MAX_X - orgx)) && (orgy == MAX_Y + 1))
cury--;
lines = numLinesToScroll(orgx, orgy, charcount);
cury -= lines;
orgy -= lines;
goxy(curx, cury);
charcount++;
}
else
{
if (current == charcount)
charcount++;
str[current++] = ch;
outc(ch);
lines = numLinesToScroll(orgx, orgy, charcount);
orgy -= lines;
}
if ((strlen(str) > (MAX_X - orgx)) && (orgy == MAX_Y + 1))
orgy--;
}
else
beep();
Expand Down

0 comments on commit 9ec4779

Please sign in to comment.