Skip to content

Commit

Permalink
Move some support functions out of z.c
Browse files Browse the repository at this point in the history
  • Loading branch information
smaclennan committed Dec 29, 2016
1 parent 498b1d2 commit ac4c76e
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 210 deletions.
44 changes: 44 additions & 0 deletions bcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,43 @@ static struct zbuff *Bufflist_tail;
struct zbuff *Curbuff; /* the current buffer */
struct buff *Bbuff; /* the current low-level buffer */

/* Get the word at the current buffer point and store in 'word'.
* Get at the most 'max' characters.
* Leaves the point alone.
*/
int getbword(char word[], int max, int (*valid)(int))
{
int i;
struct mark tmark;

bmrktopnt(Bbuff, &tmark);
if (!bistoken(Buff()))
bmoveto(Bbuff, bistoken, BACKWARD);
bmovepast(Bbuff, bistoken, BACKWARD);
for (i = 0; !bisend(Bbuff) && valid(Buff()) && i < max; ++i, bmove1(Bbuff))
word[i] = Buff();
word[i] = '\0';
bpnttomrk(Bbuff, &tmark);
return i;
}

/* Get the current buffer text and store in 'txt'.
* Get at the most 'max' characters.
* Leaves the point alone.
*/
char *getbtxt(char txt[], int max)
{
int i;
struct mark tmark;

bmrktopnt(Bbuff, &tmark);
for (btostart(Bbuff), i = 0; !bisend(Bbuff) && i < max; bmove1(Bbuff), ++i)
txt[i] = Buff();
txt[i] = '\0';
bpnttomrk(Bbuff, &tmark);
return txt;
}

static void switchto_part(void)
{
char word[STRMAX + 1];
Expand Down Expand Up @@ -128,6 +165,13 @@ void Zdelete_buffer(void)

#define WASTED (BUFNAMMAX + 14)

/* Limit a filename to at most Colmax - 'num' cols */
char *limit(char *fname, int num)
{
int off = strlen(fname) - (Colmax - num);
return off > 0 ? fname + off : fname;
}

static void lstbuff(struct zbuff *tbuff)
{
binstr(Bbuff, "%-16s %s%s %8u %s\n", tbuff->bname,
Expand Down
79 changes: 79 additions & 0 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
int Arg;
bool Argp;

int bisword(int c)
{
return isalnum(c) || c == '_' || c == '.' || c == '-';
}

int bistoken(int c)
{
return bisword(c) || c == '/';
}

int biswhite(int c)
{
return c == ' ' || c == '\t';
}

/* Word COMMANDS */

/* Find the start of a word. */
Expand Down Expand Up @@ -532,6 +547,48 @@ void Zsave_and_exit(void)
Zexit();
}

/* ask Yes/No question.
* Returns YES, NO, BANG, or ABORT
*/
static int ask2(const char *msg, bool allow_bang)
{
int rc = BADCHAR;
unsigned cmd;

putpaw("%s", msg);
while (rc == BADCHAR)
switch (cmd = tgetkb()) {
case 'y':
case 'Y':
rc = YES;
break;
case 'N':
case 'n':
rc = NO;
break;
case '!':
if (allow_bang)
rc = BANG;
else
tbell();
break;
default:
tbell();
if (Keys[cmd] == ZABORT)
rc = ABORT;
}
clrpaw();
return rc;
}

/* ask Yes/No question.
* Returns YES, NO, or ABORT
*/
int ask(const char *msg)
{
return ask2(msg, false);
}

/* Prompt to save buffer if the buffer has been modified.
* Always saves if 'must' is true or saveOnExit is set.
* Returns false if the user ABORTS the prompt.
Expand Down Expand Up @@ -687,6 +744,18 @@ void Zarg(void)
CMD(Keys[Cmd]);
}

/* Delay before displaying a prompt and wait for a cmd */
int delayprompt(const char *msg)
{
int cmd, rc = tdelay(600);
if (rc)
putpaw(msg);
cmd = tgetkb();
if (rc)
clrpaw();
return cmd;
}

static void do_prefix_cmd(const char *prompt, int mask)
{
Cmd = delayprompt(prompt);
Expand Down Expand Up @@ -1092,3 +1161,13 @@ void Zundo(void)
#else
void Zundo(void) { tbell(); }
#endif

/* Put in the right number of tabs and spaces */
void tindent(int arg)
{
if (VAR(VSPACETAB) == 0)
for (; arg >= Tabsize; arg -= Tabsize)
binsert(Bbuff, '\t');
while (arg-- > 0)
binsert(Bbuff, ' ');
}
18 changes: 18 additions & 0 deletions delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ void delinit(void)
Killbuff = bcreate();
}

/* Was the last command a delete to kill buffer command? */
bool delcmd(void)
{
switch (Lfunc) {
case ZDELETE_TO_EOL:
case ZDELETE_LINE:
case ZDELETE_REGION:
case ZDELETE_WORD:
case ZDELETE_PREVIOUS_WORD:
case ZCOPY_REGION:
case ZCOPY_WORD:
case ZAPPEND_KILL:
return true;
default:
return false;
}
}

static void copytomrk(struct mark *tmark)
{
if (delcmd())
Expand Down
10 changes: 10 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
static char Fname[PATHMAX + 1];
int raw_mode;

/* Return a pointer to the start of the last part of fname */
char *lastpart(char *fname)
{
char *p = strrchr(fname, '/');
if (p)
return p + 1;
else
return fname;
}

static bool cp(char *from, char *to)
{
FILE *in, *out;
Expand Down
3 changes: 0 additions & 3 deletions proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ void os_init(void);
/* General routines */

int ask(const char *);
int ask2(const char *, bool);
int bgetcol(bool, int);
int bmakecol(int);
void zswitchto(struct zbuff *);
Expand Down Expand Up @@ -238,8 +237,6 @@ void checkpipes(int type);
void siginit(void);
bool unvoke(struct zbuff *);

const char *func2name(Byte func);

/* for getfname */
int getfname(const char *, char *);
int nmatch(char *, char *);
Expand Down
Loading

0 comments on commit ac4c76e

Please sign in to comment.