Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IsBalanced() optional #28

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bestline.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ static int gotcont;
static int gotwinch;
static signed char rawmode;
static char maskmode;
static char balancemode;
static char ispaused;
static char iscapital;
static unsigned historylen;
Expand Down Expand Up @@ -3129,7 +3130,7 @@ static ssize_t bestlineEdit(int stdin_fd, int stdout_fd, const char *prompt,
}
break;
case '\r':
if (IsBalanced(&l)) {
if (!balancemode || IsBalanced(&l)) {
l.final = 1;
free(history[--historylen]);
history[historylen] = 0;
Expand Down Expand Up @@ -3607,3 +3608,13 @@ void bestlineMaskModeEnable(void) {
void bestlineMaskModeDisable(void) {
maskmode = 0;
}

/**
* Enables or disables "balance mode".
*
* When it is enabled, bestline() will block until parentheses are
* balanced. This is useful for code but not for free text.
*/
void bestlineBalanceMode(char mode) {
balancemode = mode;
}
1 change: 1 addition & 0 deletions bestline.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void bestlineHistoryFree(void);
void bestlineClearScreen(int);
void bestlineMaskModeEnable(void);
void bestlineMaskModeDisable(void);
void bestlineBalanceMode(char);
void bestlineDisableRawMode(void);
void bestlineFree(void *);

Expand Down
4 changes: 4 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ int main(int argc, char **argv) {
bestlineMaskModeEnable();
} else if (!strncmp(line, "/unmask", 7)) {
bestlineMaskModeDisable();
} else if (!strncmp(line, "/balance", 8)) {
bestlineBalanceMode(1);
} else if (!strncmp(line, "/unbalance", 10)) {
bestlineBalanceMode(0);
} else if (line[0] == '/') {
fputs("Unreconized command: ", stdout);
fputs(line, stdout);
Expand Down