Put the word jumping code in functions

This commit is contained in:
James R 2020-01-08 12:58:19 -08:00
parent 0d3c03ccd6
commit 702b23ec3f
4 changed files with 52 additions and 44 deletions

View File

@ -32,6 +32,7 @@
#include "d_main.h" #include "d_main.h"
#include "m_menu.h" #include "m_menu.h"
#include "filesrch.h" #include "filesrch.h"
#include "m_misc.h"
#ifdef _WINDOWS #ifdef _WINDOWS
#include "win32/win_main.h" #include "win32/win_main.h"
@ -41,8 +42,6 @@
#include "hardware/hw_main.h" #include "hardware/hw_main.h"
#endif #endif
#define PUNCTUATION "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
#define MAXHUDLINES 20 #define MAXHUDLINES 20
static boolean con_started = false; // console has been initialised static boolean con_started = false; // console has been initialised
@ -822,61 +821,23 @@ boolean CON_Responder(event_t *ev)
if (input_cur != 0) if (input_cur != 0)
{ {
if (ctrldown) if (ctrldown)
{ input_cur = M_JumpWordReverse(inputlines[inputline], input_cur);
int (*is)(int);
char *line;
int c;
line = inputlines[inputline];
c = line[--input_cur];
if (isspace(c))
is = isspace;
else if (ispunct(c))
is = ispunct;
else
is = isalnum;
c = (*is)(line[input_cur]);
while (input_cur > 0 &&
(*is)(line[input_cur - 1]) == c)
input_cur--;
}
else else
{
--input_cur; --input_cur;
}
} }
if (!shiftdown) if (!shiftdown)
input_sel = input_cur; input_sel = input_cur;
return true; return true;
} }
else if (key == KEY_RIGHTARROW) else if (key == KEY_RIGHTARROW)
{ {
if (ctrldown) if (input_cur < input_len)
{ {
char *line; if (ctrldown)
int c; input_cur += M_JumpWord(&inputlines[inputline][input_cur]);
line = &inputlines[inputline][input_cur];
c = line[0];
if (isspace(c))
input_cur += strspn(line, " ");
else if (ispunct(c))
input_cur += strspn(line, PUNCTUATION);
else else
{
if (isspace(line[1]))
input_cur += 1 + strspn(&line[1], " ");
else
input_cur += strcspn(line, " "PUNCTUATION);
}
}
else
{
if (input_cur < input_len)
++input_cur; ++input_cur;
} }
if (!shiftdown) if (!shiftdown)
input_sel = input_cur; input_sel = input_cur;
return true; return true;

View File

@ -541,6 +541,8 @@ INT32 I_GetKey(void);
#define PATHSEP "/" #define PATHSEP "/"
#endif #endif
#define PUNCTUATION "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
// Compile date and time and revision. // Compile date and time and revision.
extern const char *compdate, *comptime, *comprevision, *compbranch; extern const char *compdate, *comptime, *comprevision, *compbranch;

View File

@ -2540,3 +2540,40 @@ void M_MkdirEach(const char *path, int start, int mode)
{ {
M_MkdirEachUntil(path, start, -1, mode); M_MkdirEachUntil(path, start, -1, mode);
} }
int M_JumpWord(const char *line)
{
int c;
c = line[0];
if (isspace(c))
return strspn(line, " ");
else if (ispunct(c))
return strspn(line, PUNCTUATION);
else
{
if (isspace(line[1]))
return 1 + strspn(&line[1], " ");
else
return strcspn(line, " "PUNCTUATION);
}
}
int M_JumpWordReverse(const char *line, int offset)
{
int (*is)(int);
int c;
c = line[--offset];
if (isspace(c))
is = isspace;
else if (ispunct(c))
is = ispunct;
else
is = isalnum;
c = (*is)(line[offset]);
while (offset > 0 &&
(*is)(line[offset - 1]) == c)
offset--;
return offset;
}

View File

@ -101,6 +101,14 @@ boolean M_IsPathAbsolute (const char *path);
void M_MkdirEach (const char *path, int start, int mode); void M_MkdirEach (const char *path, int start, int mode);
void M_MkdirEachUntil (const char *path, int start, int end, int mode); void M_MkdirEachUntil (const char *path, int start, int end, int mode);
/* Return offset to the first word in a string. */
/* E.g. cursor += M_JumpWord(line + cursor); */
int M_JumpWord (const char *s);
/* Return index of the last word behind offset bytes in a string. */
/* E.g. cursor = M_JumpWordReverse(line, cursor); */
int M_JumpWordReverse (const char *line, int offset);
// counting bits, for weapon ammo code, usually // counting bits, for weapon ammo code, usually
FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size);