bim: update to latest master

This commit is contained in:
K. Lange 2020-12-11 13:49:18 +09:00
parent f2eb103fe8
commit ffcbc6df25
1 changed files with 186 additions and 35 deletions

View File

@ -1,9 +1,9 @@
/**
* This is a baked, single-file version of bim.
* It was built Fri Nov 20 14:06:22 2020
* It is based on git commit 496849bab42290f42e9928f510a1d91534fe2a51
* It was built Fri Dec 11 13:47:59 2020
* It is based on git commit 8dd9969f9b534bd2453146a678c8fcdf8f6f3b06
*/
#define GIT_TAG "496849b-baked"
#define GIT_TAG "8dd9969-baked"
/* Bim - A Text Editor
*
* Copyright (C) 2012-2020 K. Lange
@ -63,6 +63,7 @@
#define LINE_FEED '\n'
#define BACKSPACE_KEY 0x08
#define DELETE_KEY 0x7F
#define DEFAULT_KEY_WAIT -1
enum Key {
KEY_TIMEOUT = -1,
@ -867,7 +868,7 @@ int bim_getkey(int read_timeout) {
uint32_t c;
uint32_t istate = 0;
while ((cin = bim_getch_timeout(read_timeout))) {
while ((cin = bim_getch_timeout((timeout == 1) ? 50 : read_timeout))) {
if (cin == -1) {
if (timeout && this_buf[timeout-1] == '\033')
return KEY_ESCAPE;
@ -900,10 +901,10 @@ int bim_getkey(int read_timeout) {
}
if (timeout >= 2 && this_buf[0] == '\033' && this_buf[1] == 'O') {
switch (c) {
case 'P': timeout = 0; return KEY_F1;
case 'Q': timeout = 0; return KEY_F2;
case 'R': timeout = 0; return KEY_F3;
case 'S': timeout = 0; return KEY_F4;
case 'P': return KEY_F1;
case 'Q': return KEY_F2;
case 'R': return KEY_F3;
case 'S': return KEY_F4;
}
timeout = 0;
continue;
@ -927,25 +928,25 @@ int bim_getkey(int read_timeout) {
}
if (timeout >= 2 && this_buf[0] == '\033' && this_buf[1] == '[') {
switch (c) {
case 'M': timeout = 0; return KEY_MOUSE;
case '<': timeout = 0; return KEY_MOUSE_SGR;
case 'M': return KEY_MOUSE;
case '<': return KEY_MOUSE_SGR;
case 'A': return shift_key(KEY_UP);
case 'B': return shift_key(KEY_DOWN);
case 'C': return shift_key(KEY_RIGHT);
case 'D': return shift_key(KEY_LEFT);
case 'H': timeout = 0; return KEY_HOME;
case 'F': timeout = 0; return KEY_END;
case 'I': timeout = 0; return KEY_PAGE_UP;
case 'G': timeout = 0; return KEY_PAGE_DOWN;
case 'Z': timeout = 0; return KEY_SHIFT_TAB;
case 'H': return KEY_HOME;
case 'F': return KEY_END;
case 'I': return KEY_PAGE_UP;
case 'G': return KEY_PAGE_DOWN;
case 'Z': return KEY_SHIFT_TAB;
case '~':
if (timeout == 3) {
switch (this_buf[2]) {
case '1': timeout = 0; return KEY_HOME;
case '3': timeout = 0; return KEY_DELETE;
case '4': timeout = 0; return KEY_END;
case '5': timeout = 0; return KEY_PAGE_UP;
case '6': timeout = 0; return KEY_PAGE_DOWN;
case '1': return KEY_HOME;
case '3': return KEY_DELETE;
case '4': return KEY_END;
case '5': return KEY_PAGE_UP;
case '6': return KEY_PAGE_DOWN;
}
} else if (timeout == 5) {
if (this_buf[2] == '2' && this_buf[3] == '0' && this_buf[4] == '0') {
@ -6031,7 +6032,7 @@ BIM_COMMAND(buffers,"buffers","Show the open buffers") {
BIM_COMMAND(keyname,"keyname","Press and key and get its name.") {
int c;
render_commandline_message("(press a key)");
while ((c = bim_getkey(200)) == KEY_TIMEOUT);
while ((c = bim_getkey(DEFAULT_KEY_WAIT)) == KEY_TIMEOUT);
render_commandline_message("%d = %s", c, name_from_key(c));
return 0;
}
@ -8948,7 +8949,7 @@ int read_one_character(char * message) {
place_cursor_actual();
}
int c;
while ((c = bim_getkey(200))) {
while ((c = bim_getkey(DEFAULT_KEY_WAIT))) {
if (c == KEY_TIMEOUT) continue;
if (c == KEY_CTRL_V) {
if (!global_config.overlay_mode) {
@ -9742,7 +9743,7 @@ void normal_mode(void) {
render_command_input_buffer();
refresh = 0;
}
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (key != KEY_TIMEOUT) {
refresh = 1;
if (!handle_action(COMMAND_MAP, key))
@ -9755,7 +9756,7 @@ void normal_mode(void) {
render_command_input_buffer();
refresh = 0;
}
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (key != KEY_TIMEOUT) {
refresh = 1;
if (!handle_action(SEARCH_MAP, key)) {
@ -9812,7 +9813,7 @@ void normal_mode(void) {
if (env->mode == MODE_NORMAL) {
place_cursor_actual();
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (handle_nav_buffer(key)) {
if (!handle_action(NORMAL_MAP, key))
if (!handle_action(NAVIGATION_MAP, key))
@ -9820,9 +9821,10 @@ void normal_mode(void) {
}
reset_nav_buffer(key);
} else if (env->mode == MODE_INSERT) {
place_cursor_actual();
int key = bim_getkey(refresh ? 10 : 200);
if (!refresh) place_cursor_actual();
int key = bim_getkey(refresh ? 10 : DEFAULT_KEY_WAIT);
if (key == KEY_TIMEOUT) {
place_cursor_actual();
if (refresh > 1) {
redraw_text();
} else if (refresh) {
@ -9843,7 +9845,7 @@ void normal_mode(void) {
}
} else if (env->mode == MODE_REPLACE) {
place_cursor_actual();
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (key != KEY_TIMEOUT) {
if (handle_action(REPLACE_MAP, key)) {
redraw_text();
@ -9863,7 +9865,7 @@ void normal_mode(void) {
}
} else if (env->mode == MODE_LINE_SELECTION) {
place_cursor_actual();
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (key == KEY_TIMEOUT) continue;
if (handle_nav_buffer(key)) {
@ -9895,7 +9897,7 @@ void normal_mode(void) {
}
} else if (env->mode == MODE_CHAR_SELECTION) {
place_cursor_actual();
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (key == KEY_TIMEOUT) continue;
if (handle_nav_buffer(key)) {
@ -9925,7 +9927,7 @@ void normal_mode(void) {
}
} else if (env->mode == MODE_COL_SELECTION) {
place_cursor_actual();
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (key == KEY_TIMEOUT) continue;
if (handle_nav_buffer(key)) {
@ -9954,7 +9956,7 @@ void normal_mode(void) {
redraw_commandline();
}
} else if (env->mode == MODE_COL_INSERT) {
int key = bim_getkey(refresh ? 10 : 200);
int key = bim_getkey(refresh ? 10 : DEFAULT_KEY_WAIT);
if (key == KEY_TIMEOUT) {
if (refresh) {
redraw_commandline();
@ -9969,7 +9971,7 @@ void normal_mode(void) {
}
} if (env->mode == MODE_DIRECTORY_BROWSE) {
place_cursor_actual();
int key = bim_getkey(200);
int key = bim_getkey(DEFAULT_KEY_WAIT);
if (handle_nav_buffer(key)) {
if (!handle_action(DIRECTORY_BROWSE_MAP, key))
if (!handle_action(NAVIGATION_MAP, key))
@ -10059,6 +10061,15 @@ int has_function(char * name) {
return 0;
}
int find_function(char * name) {
for (int i = 0; i < flex_user_functions_count; ++i) {
if (user_functions[i] && !strcmp(user_functions[i]->command, name)) {
return i;
}
}
return -1;
}
BIM_COMMAND(call,"call","Call a function") {
if (argc < 2) {
render_error("Expected function name");
@ -10132,7 +10143,7 @@ BIM_COMMAND(show_function,"showfunction","Show the commands in a function") {
i++;
if (this && i == global_config.term_height - 3) {
printf("(function continues)");
while (bim_getkey(200) == KEY_TIMEOUT);
while (bim_getkey(DEFAULT_KEY_WAIT) == KEY_TIMEOUT);
}
}
@ -10507,7 +10518,7 @@ BIM_COMMAND(whatis,"whatis","Describe actions bound to a key in different modes.
if (argc < 2) {
render_commandline_message("(press a key)");
while ((key = bim_getkey(200)) == KEY_TIMEOUT);
while ((key = bim_getkey(DEFAULT_KEY_WAIT)) == KEY_TIMEOUT);
} else if (strlen(argv[1]) > 1 && argv[1][0] == '^') {
/* See if it's a valid ctrl key */
if (argv[1][2] != '\0') {
@ -10731,6 +10742,10 @@ void dump_map_commands(const char * name, struct action_map * map) {
}
}
void call_user(int c) {
run_function(user_functions[c]->command);
}
BIM_COMMAND(mapkey,"mapkey","Map a key to an action.") {
if (argc < 2) goto _argument_error;
@ -10791,6 +10806,19 @@ BIM_COMMAND(mapkey,"mapkey","Map a key to an action.") {
}
}
/* See if it's a user function */
char _tmp[30] = {0};
if (has_function(action) && (!action_def || action_def->action == call_user)) {
/* Map a new action for this user function */
if (!action_def) {
add_action((struct action_def){action, call_user, ARG_IS_CUSTOM, "(script-defined function)"});
action_def = &mappable_actions[flex_mappable_actions_count-1];
}
options = "a";
sprintf(_tmp, "%d", find_function(action));
arg = _tmp;
}
if (!action_def) {
render_error("invalid action: %s", action);
return 1;
@ -12363,6 +12391,129 @@ BIM_SYNTAX_COMPLETER(java) {
}
BIM_SYNTAX_EXT(java, 1, java_brace_keyword_qualifier)
static char * syn_js_keywords[] = {
"abstract","arguments","from",
"await","break","case","catch","class","const",
"continue","debugger","default","delete","do","else","enum","eval",
"export","extends","final","finally","for","function","goto",
"if","implements","import","in","instanceof","interface","let","long",
"native","new","package","private","protected","public","return",
"static","super","switch","synchronized","this","throw","throws",
"transienttrue","try","typeof","volatile","while","with","yield",
NULL
};
static char * syn_js_types[] = {
"int","float","double","short","var","void","byte","char","boolean",
NULL
};
static char * syn_js_special[] = {
"true","false","null",
NULL
};
void paint_js_format_string(struct syntax_state * state) {
paint(1, FLAG_STRING);
while (charat() != -1) {
if (charat() == '\\' && nextchar() == '`') {
paint(2, FLAG_ESCAPE);
} else if (charat() == '`') {
paint(1, FLAG_STRING);
return;
} else if (charat() == '\\') {
paint(2, FLAG_ESCAPE);
} else if (charat() == '$' && nextchar() == '{') {
paint(2, FLAG_NUMERAL);
while (charat() != -1 && charat() != '}') {
paint(1, FLAG_NUMERAL);
}
paint(1, FLAG_NUMERAL);
} else {
paint(1, FLAG_STRING);
}
}
}
int syn_js_calculate(struct syntax_state * state) {
switch (state->state) {
case -1:
case 0:
if (!c_keyword_qualifier(lastchar()) && isdigit(charat())) {
paint_c_numeral(state);
return 0;
} else if (!c_keyword_qualifier(lastchar()) && (charat() >= 'A' && charat() <= 'Z')) {
while (charat() != -1 && c_keyword_qualifier(charat())) paint(1, FLAG_TYPE);
return 0;
} else if (charat() == '/' && nextchar() == '/') {
/* C++-style comments */
paint_comment(state);
} else if (charat() == '/' && nextchar() == '*') {
if (paint_c_comment(state) == 1) return 1;
} else if (find_keywords(state, syn_js_keywords, FLAG_KEYWORD, c_keyword_qualifier)) {
return 0;
} else if (find_keywords(state, syn_js_types, FLAG_TYPE, c_keyword_qualifier)) {
return 0;
} else if (find_keywords(state, syn_js_special, FLAG_NUMERAL, c_keyword_qualifier)) {
return 0;
} else if (charat() == '=' && nextchar() == '>') {
paint(2, FLAG_PRAGMA);
return 0;
} else if (charat() == ':' && c_keyword_qualifier(lastchar())) {
/* Assume things before parens are important? */
int original_i = state->i;
state->i--;
while (charat() != -1 && c_keyword_qualifier(charat())) {
paint(1, FLAG_TYPE);
state->i -= 2;
}
state->i = original_i;
paint(1, FLAG_PRAGMA);
return 0;
} else if (charat() == '<') {
paint(1, FLAG_TYPE);
while (charat() != -1 && (charat() == '/' || c_keyword_qualifier(charat()))) {
paint(1, FLAG_TYPE);
}
return 0;
} else if (charat() == '>') {
paint(1, FLAG_TYPE);
return 0;
} else if (charat() == '\"') {
paint_simple_string(state);
return 0;
} else if (charat() == '\'') {
paint_single_string(state);
return 0;
} else if (charat() == '`') {
paint_js_format_string(state);
return 0;
} else if (charat() != -1) {
skip();
return 0;
}
break;
case 1:
if (paint_c_comment(state) == 1) return 1;
return 0;
}
return -1;
}
char * syn_js_ext[] = {".js",".jsx",".ts",".tsx",NULL};
BIM_SYNTAX_COMPLETER(js) {
for (char ** keyword = syn_js_keywords; *keyword; ++keyword) {
add_if_match((*keyword),"(javascript keyword)");
}
for (char ** keyword = syn_js_types; *keyword; ++keyword) {
add_if_match((*keyword),"(javascript type)");
}
return 0;
}
BIM_SYNTAX_EXT(js, 1, c_keyword_qualifier)
char * syn_json_keywords[] = {
"true","false","null",
NULL