Replace builtin 'bind_mode' with variable $fish_bind_mode

This commit is contained in:
Julian Aron Prenner 2014-01-15 12:04:52 +01:00
parent 3a9173858b
commit fc21bb6eda
3 changed files with 22 additions and 53 deletions

View file

@ -588,8 +588,8 @@ static int builtin_bind(parser_t &parser, wchar_t **argv)
const wchar_t *bind_mode = DEFAULT_BIND_MODE; const wchar_t *bind_mode = DEFAULT_BIND_MODE;
bool bind_mode_given = false; bool bind_mode_given = false;
const wchar_t *new_bind_mode = DEFAULT_BIND_MODE; const wchar_t *sets_bind_mode = DEFAULT_BIND_MODE;
bool new_bind_mode_given = false; bool sets_bind_mode_given = false;
int use_terminfo = 0; int use_terminfo = 0;
@ -691,8 +691,8 @@ static int builtin_bind(parser_t &parser, wchar_t **argv)
break; break;
case 'm': case 'm':
new_bind_mode = woptarg; sets_bind_mode = woptarg;
new_bind_mode_given = true; sets_bind_mode_given = true;
break; break;
case '?': case '?':
@ -706,9 +706,9 @@ static int builtin_bind(parser_t &parser, wchar_t **argv)
/* /*
* if mode is given, but not new mode, default to new mode to mode * if mode is given, but not new mode, default to new mode to mode
*/ */
if(bind_mode_given && !new_bind_mode_given) if(bind_mode_given && !sets_bind_mode_given)
{ {
new_bind_mode = bind_mode; sets_bind_mode = bind_mode;
} }
switch (mode) switch (mode)
@ -739,7 +739,7 @@ static int builtin_bind(parser_t &parser, wchar_t **argv)
default: default:
{ {
builtin_bind_add(argv[woptind], (const wchar_t **)argv + (woptind + 1), argc - (woptind + 1), bind_mode, new_bind_mode, use_terminfo); builtin_bind_add(argv[woptind], (const wchar_t **)argv + (woptind + 1), argc - (woptind + 1), bind_mode, sets_bind_mode, use_terminfo);
break; break;
} }
@ -773,33 +773,6 @@ static int builtin_bind(parser_t &parser, wchar_t **argv)
} }
/**
The bind mode builtin
*/
static int builtin_bind_mode(parser_t &parser, wchar_t **argv)
{
int res = STATUS_BUILTIN_OK;
int argc = builtin_count_args(argv);
switch (argc)
{
case 1:
{
append_format(stdout_buffer, L"%ls\n", input_get_bind_mode());
break;
}
default:
{
res = STATUS_BUILTIN_ERROR;
append_format(stderr_buffer, _(L"%ls: Expected no parameters, got %d"), argv[0], argc);
break;
}
}
return res;
}
/** /**
The block builtin, used for temporarily blocking events The block builtin, used for temporarily blocking events
*/ */
@ -4058,7 +4031,6 @@ static const builtin_data_t builtin_datas[]=
{ L"begin", &builtin_begin, N_(L"Create a block of code") }, { L"begin", &builtin_begin, N_(L"Create a block of code") },
{ L"bg", &builtin_bg, N_(L"Send job to background") }, { L"bg", &builtin_bg, N_(L"Send job to background") },
{ L"bind", &builtin_bind, N_(L"Handle fish key bindings") }, { L"bind", &builtin_bind, N_(L"Handle fish key bindings") },
{ L"bind_mode", &builtin_bind_mode, N_(L"Set or get the current bind mode") },
{ L"block", &builtin_block, N_(L"Temporarily block delivery of events") }, { L"block", &builtin_block, N_(L"Temporarily block delivery of events") },
{ L"break", &builtin_break_continue, N_(L"Stop the innermost loop") }, { L"break", &builtin_break_continue, N_(L"Stop the innermost loop") },
{ L"breakpoint", &builtin_breakpoint, N_(L"Temporarily halt execution of a script and launch an interactive debug prompt") }, { L"breakpoint", &builtin_breakpoint, N_(L"Temporarily halt execution of a script and launch an interactive debug prompt") },

View file

@ -61,6 +61,9 @@
#include <vector> #include <vector>
#define DEFAULT_TERM L"ansi" #define DEFAULT_TERM L"ansi"
#define FISH_BIND_MODE_VAR L"fish_bind_mode"
/** /**
Struct representing a keybinding. Returned by input_get_mappings. Struct representing a keybinding. Returned by input_get_mappings.
*/ */
@ -231,9 +234,6 @@ static const wchar_t code_arr[] =
/** Mappings for the current input mode */ /** Mappings for the current input mode */
static std::vector<input_mapping_t> mapping_list; static std::vector<input_mapping_t> mapping_list;
#define MAX_BIND_MODE_NAME_SIZE 512
static wchar_t bind_mode[MAX_BIND_MODE_NAME_SIZE] = DEFAULT_BIND_MODE;
/* Terminfo map list */ /* Terminfo map list */
static std::vector<terminfo_mapping_t> terminfo_mappings; static std::vector<terminfo_mapping_t> terminfo_mappings;
@ -261,6 +261,12 @@ static void input_terminfo_init();
*/ */
const wchar_t *input_get_bind_mode() const wchar_t *input_get_bind_mode()
{ {
const wchar_t *bind_mode = DEFAULT_BIND_MODE;
const env_var_t bind_mode_var = env_get_string(FISH_BIND_MODE_VAR);
if(!bind_mode_var.missing())
{
bind_mode = bind_mode_var.c_str();
}
return bind_mode; return bind_mode;
} }
@ -269,16 +275,7 @@ const wchar_t *input_get_bind_mode()
*/ */
bool input_set_bind_mode(const wchar_t *bm) bool input_set_bind_mode(const wchar_t *bm)
{ {
int len = wcslen(bm) * sizeof(wchar_t); env_set(FISH_BIND_MODE_VAR, bm, ENV_GLOBAL);
if(len >= MAX_BIND_MODE_NAME_SIZE)
{
debug(0, L"Error: name for bind mode exceeds maximum size\n");
return false;
}
memset(bind_mode, 0, MAX_BIND_MODE_NAME_SIZE);
memcpy(bind_mode, bm, len);
//debug(0, L"Set bind mode to `%ls'", bind_mode);
return true; return true;
} }
@ -541,6 +538,8 @@ static void input_mapping_execute_matching_or_generic()
{ {
const input_mapping_t *generic = NULL; const input_mapping_t *generic = NULL;
const wchar_t *bind_mode = input_get_bind_mode();
for (int i = 0; i < mapping_list.size(); i++) for (int i = 0; i < mapping_list.size(); i++)
{ {
const input_mapping_t &m = mapping_list.at(i); const input_mapping_t &m = mapping_list.at(i);
@ -548,7 +547,7 @@ static void input_mapping_execute_matching_or_generic()
//debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), 1), //debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), 1),
// m.mode.c_str(), m.sets_mode.c_str()); // m.mode.c_str(), m.sets_mode.c_str());
if(wcscmp(m.mode.c_str(), input_get_bind_mode())) if(wcscmp(m.mode.c_str(), bind_mode))
{ {
//debug(0, L"skipping mapping because mode %ls != %ls\n", m.mode.c_str(), input_get_bind_mode()); //debug(0, L"skipping mapping because mode %ls != %ls\n", m.mode.c_str(), input_get_bind_mode());
continue; continue;
@ -581,8 +580,6 @@ static void input_mapping_execute_matching_or_generic()
wint_t input_readch() wint_t input_readch()
{ {
size_t i;
CHECK_BLOCK(R_NULL); CHECK_BLOCK(R_NULL);
/* /*

View file

@ -1,9 +1,9 @@
function fish_vi_prompt_cm --description "Displays the current mode" function fish_vi_prompt_cm --description "Displays the current mode"
echo -n " " echo -n " "
switch (bind_mode) switch $fish_bind_mode
case default case default
set_color --bold --background green white set_color --bold --background green white
echo "[C]" echo "[N]"
case insert case insert
set_color --bold --background red white set_color --bold --background red white
echo "[I]" echo "[I]"