Skip to content

Commit

Permalink
First stab at dropping all support for readlines inputrc files and in…
Browse files Browse the repository at this point in the history
…stead using an internal system for performing keybinding.

darcs-hash:20070925161447-75c98-1feaef88a4b518badb7879f598f06ab650a8f93b.gz
  • Loading branch information
liljencrantz committed Sep 25, 2007
1 parent af9c206 commit cf8e746
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 1,421 deletions.
254 changes: 239 additions & 15 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,28 +402,183 @@ static void builtin_missing_argument( const wchar_t *cmd, const wchar_t *opt )
#include "builtin_ulimit.c"
#include "builtin_jobs.c"

static void builtin_bind_list()
{
array_list_t lst;
int i;


al_init( &lst );
input_mapping_get_names( &lst );

for( i=0; i<al_get_count(&lst); i++ )
{
wchar_t *seq = (wchar_t *)al_get( &lst, i );

const wchar_t *tname = input_terminfo_get_name( seq );
wchar_t *ecmd = escape( input_mapping_get( seq ), 1 );

if( tname )
{
sb_printf( sb_out, L"bind -k %ls %ls\n", tname, ecmd );
}
else
{
wchar_t *eseq = escape( seq, 1 );

sb_printf( sb_out, L"bind %ls %ls\n", eseq, ecmd );
free( eseq );
}

free( ecmd );

}

al_destroy( &lst );
}

static void builtin_bind_key_names( int all )
{
array_list_t lst;
int i;

al_init( &lst );
input_terminfo_get_names( &lst, !all );

for( i=0; i<al_get_count(&lst); i++ )
{
wchar_t *seq = (wchar_t *)al_get( &lst, i );

sb_printf( sb_out, L"%ls\n", seq );
}

al_destroy( &lst );
}

static void builtin_bind_function_names()
{
array_list_t lst;
int i;

al_init( &lst );
input_function_get_names( &lst );

for( i=0; i<al_get_count(&lst); i++ )
{
wchar_t *seq = (wchar_t *)al_get( &lst, i );

sb_printf( sb_out, L"%ls\n", seq );
}

al_destroy( &lst );
}

static int builtin_bind_add( wchar_t *seq, wchar_t *cmd, int terminfo )
{

if( terminfo )
{
const wchar_t *seq2 = input_terminfo_get_sequence( seq );
if( seq2 )
{
input_mapping_add( seq2, cmd );
}
else
{
return 1;
}

}
else
{
input_mapping_add( seq, cmd );
}

return 0;

}

static void builtin_bind_erase( wchar_t **seq, int all )
{
if( all )
{
int i;
array_list_t lst;
al_init( &lst );

input_mapping_get_names( &lst );

for( i=0; i<al_get_count( &lst ); i++ )
{
input_mapping_erase( (wchar_t *)al_get( &lst, i ) );
}

al_destroy( &lst );
}
else
{
while( *seq )
{
input_mapping_erase( *seq++ );
}

}

}


/**
The bind builtin, used for setting character sequences
*/
static int builtin_bind( wchar_t **argv )
{

enum
{
BIND_INSERT,
BIND_ERASE,
BIND_KEY_NAMES,
BIND_FUNCTION_NAMES
}
;

int i;
int argc=builtin_count_args( argv );

int mode = BIND_INSERT;
int res = STATUS_BUILTIN_OK;
int all = 0;

int use_terminfo = 0;

woptind=0;

const static struct woption
long_options[] =
{
{
L"set-mode", required_argument, 0, 'M'
L"all", no_argument, 0, 'a'
}
,
{
L"erase", no_argument, 0, 'e'
}
,
{
L"function-names", no_argument, 0, 'f'
}
,
{
L"help", no_argument, 0, 'h'
}
,
{
L"key", no_argument, 0, 'k'
}
,
{
L"key-names", no_argument, 0, 'K'
}
,
{
0, 0, 0, 0
}
Expand All @@ -433,15 +588,15 @@ static int builtin_bind( wchar_t **argv )
while( 1 )
{
int opt_index = 0;

int opt = wgetopt_long( argc,
argv,
L"M:h",
long_options,
&opt_index );
argv,
L"aehkKf",
long_options,
&opt_index );

if( opt == -1 )
break;

switch( opt )
{
case 0:
Expand All @@ -455,28 +610,97 @@ static int builtin_bind( wchar_t **argv )

return STATUS_BUILTIN_ERROR;

case 'M':
input_set_mode( woptarg );
case 'a':
all = 1;
break;

case 'e':
mode = BIND_ERASE;
break;


case 'h':
builtin_print_help( argv[0], sb_out );
return STATUS_BUILTIN_OK;

case 'k':
use_terminfo = 1;
break;

case 'K':
mode = BIND_KEY_NAMES;
break;

case 'f':
mode = BIND_FUNCTION_NAMES;
break;

case '?':
builtin_unknown_option( argv[0], argv[woptind-1] );
return STATUS_BUILTIN_ERROR;

}

}

for( i=woptind; i<argc; i++ )
switch( mode )
{
input_parse_inputrc_line( argv[i] );
}

case BIND_ERASE:
{
builtin_bind_erase( &argv[woptind], all);
break;
}

case BIND_INSERT:
{
switch( argc-woptind )
{
case 0:
{
builtin_bind_list();
break;
}

return STATUS_BUILTIN_OK;
case 2:
{
builtin_bind_add(argv[woptind], argv[woptind+1], use_terminfo );
break;
}

default:
{
res = STATUS_BUILTIN_ERROR;
sb_printf( sb_err, _(L"%ls: Expected zero or two parameters, got %d"), argv[0], argc-woptind );
break;
}
}
break;
}

case BIND_KEY_NAMES:
{
builtin_bind_key_names( all );
break;
}


case BIND_FUNCTION_NAMES:
{
builtin_bind_function_names();
break;
}


default:
{
res = STATUS_BUILTIN_ERROR;
sb_printf( sb_err, _(L"%ls: Invalid state\n"), argv[0] );
break;
}
}

return res;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion builtin_commandline.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static int builtin_commandline( wchar_t **argv )
}
for( i=woptind; i<argc; i++ )
{
wint_t c = input_get_code( argv[i] );
wint_t c = input_function_get_code( argv[i] );
if( c != -1 )
{
/*
Expand Down
48 changes: 34 additions & 14 deletions doc_src/bind.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
\section bind bind - handle key bindings
\section bind bind - handle fish key bindings

\subsection bind-synopsis Synopsis
<tt>bind [OPTIONS] [BINDINGS...]</tt>
<tt>bind [OPTIONS] SEQUENCE COMMAND</tt>

The <tt>bind</tt> builtin causes fish to add the readline style bindings specified by BINDINGS to the list of key bindings, as if they appeared in your <tt>~/.fish_inputrc</tt> file.
\subsection bind-description Description

For more information on the syntax keyboard bindings, use <tt>man
readline</tt> to access the readline documentation. The available commands
are listed in the <a href="index.html#editor">Command Line Editor</a> section
of the fish manual - but you may also use any fish command! To write such
commands, see the <a href="#commandline">commandline</a> builtin. It's good
practice to put the code into a <tt><a href="#function">function</a> -b</tt>
and bind to the function name.
The <tt>bind</tt> builtin causes fish to add a key binding from the specified sequence.

SEQUENCE is the character sequence to bind to. Usually, one would use
fish escape sequences to express them. For example, Alt-w can be
written as <tt>\\ew</tt>, and Control-x can be written as
<tt>\\cx</tt>.

If the -k switch is used, the name of the key (such as down, up or
backspace) is used instead of a sequence. The names used are the same
as the corresponding curses variables, but without the 'key_'
prefix. (See man 5 terminfo for more information, or use <tt>bind
--names</tt> for a list of all available named keys)

COMMAND can be any fish command, but it can also be one of a set of
special input functions. These include functions for moving the
cursor, operating on the kill-ring, performing tab completion,
etc. Use 'bind -N' for a complete list of these input functions.

When COMMAND is a shellscript command, it is a good practice to put
the actual code into a <a href="#function">function</a> and simply
bind to the function name.

- <tt>-a</tt> or <tt>--all</tt> If --print-key-names is specified, show all key names, not only the ones that actually are defined for the current terminal. If erase mode is specified, this switch will cause all current bindings to be erased.
- <tt>-e</tt> or <tt>--erase</tt> Erase mode. All non-switch arguments are interpreted as character sequences and any commands associated with those sequences are erased.
- <tt>-h</tt> or <tt>--help</tt> Display help and exit
- <tt>-k</tt> or <tt>--key</tt> Specify a key name, such as 'left' or 'backspace' instead of a character sequence
- <tt>-K</tt> or <tt>--key-names</tt> Display a list of available key names
- <tt>-f</tt> or <tt>--function-names</tt> Display a list of available input functions

\subsection bind-description Description
- <tt>-M MODE</tt> or <tt>--set-mode=MODE</tt> sets the current input mode to MODE.

\subsection bind-example Example

<tt>bind -M vi</tt> changes to the vi input mode
<tt>bind \cd 'exit'</tt> causes fish to exit on Control-d

<tt>bind -k ppage history-search-backward</tt> Causes fish to perform a history search when the page up key is pressed

<tt>bind '"\\M-j": jobs'</tt> Binds the jobs command to the Alt-j keyboard shortcut
Loading

0 comments on commit cf8e746

Please sign in to comment.