| 1 | package Psh::Builtins::Bind; | ||
| 2 | |||
| 3 | require Psh::Util; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =item * C<bind [-m keymap] [-lvd] [-q name]> | ||
| 8 | |||
| 9 | =item * C<bind [-m keymap] -f filename> | ||
| 10 | |||
| 11 | =item * C<bind [-m keymap] keyseq:function-name> | ||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | Display current readline key and function bindings, | ||
| 16 | or bind a key sequence to a readline function or | ||
| 17 | macro. The binding syntax accepted is identical to | ||
| 18 | that of .inputrc, but each binding must be passed | ||
| 19 | as a separate argument; e.g., '"\C-x\C-r": | ||
| 20 | re-read-init-file'. Options, if supplied, have the | ||
| 21 | following meanings: | ||
| 22 | |||
| 23 | =over 8 | ||
| 24 | |||
| 25 | =item -m keymap | ||
| 26 | |||
| 27 | Use keymap as the keymap to be affected by | ||
| 28 | the subsequent bindings. Acceptable keymap | ||
| 29 | names are emacs, emacs-standard, emacs-meta, | ||
| 30 | emacs-ctlx, vi, vi-move, vi-command, and vi- | ||
| 31 | insert. vi is equivalent to vi-command; | ||
| 32 | emacs is equivalent to emacs-standard. | ||
| 33 | |||
| 34 | =item -l | ||
| 35 | |||
| 36 | List the names of all readline functions | ||
| 37 | |||
| 38 | =item -v | ||
| 39 | |||
| 40 | List current function names and bindings | ||
| 41 | |||
| 42 | =item -d | ||
| 43 | |||
| 44 | Dump function names and bindings in such a | ||
| 45 | way that they can be re-read | ||
| 46 | |||
| 47 | =item -f filename | ||
| 48 | |||
| 49 | Read key bindings from filename | ||
| 50 | |||
| 51 | =item -q function | ||
| 52 | |||
| 53 | Query about which keys invoke the named | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | # | ||
| 60 | # > TODO: How can we print out the current bindings in an | ||
| 61 | # > ReadLine-implementation-independent way? We should allow rebinding | ||
| 62 | # > of keys if Readline interface allows it, etc. | ||
| 63 | # | ||
| 64 | # This implementation supports GNU fully. It also kind of supports Perl. | ||
| 65 | # All others it will refuse to work with. | ||
| 66 | # | ||
| 67 | # > Info: | ||
| 68 | # > Bind a key: GNU: bind_key Perl: bind | ||
| 69 | # | ||
| 70 | # I implemented GNU using parse_and_bind as it gives a result more | ||
| 71 | # consistant with what bash does. | ||
| 72 | # | ||
| 73 | # > Other interesting stuff | ||
| 74 | # > Perl: set(EditingMode) - vi or emacs keybindings | ||
| 75 | # | ||
| 76 | # I impelemented this as a "keymap". (I notice that internally this | ||
| 77 | # is what the EditingMode's are refered to as anyway.) | ||
| 78 | # | ||
| 79 | # > Perl: set(TcshCompleteMode) - tcsh menu completion mode | ||
| 80 | # > GNU: lots and lots... | ||
| 81 | # | ||
| 82 | |||
| 83 | sub bi_bind { | ||
| 84 | my $status = 0; | ||
| 85 | if (defined($Psh::term) and $Psh::term->can('add_defun')) { | ||
| 86 | my $args = pop; | ||
| 87 | while (my $command = shift(@$args)) { | ||
| 88 | if ($command eq '-l') { | ||
| 89 | for my $function (sort($Psh::term->get_all_function_names)) { | ||
| 90 | Psh::Util::print_out("$function\n"); | ||
| 91 | } | ||
| 92 | } elsif ($command eq '-m') { # Set keymap | ||
| 93 | my $keymap_name = shift(@$args); | ||
| 94 | my $map = $Psh::term->get_keymap_by_name($keymap_name); | ||
| 95 | if (defined($map)) { | ||
| 96 | $Psh::term->set_keymap($map); | ||
| 97 | } else { | ||
| 98 | Psh::Util::print_out(qq{bind: `$keymap_name': illegal keymap name\n}); | ||
| 99 | $status = 1; | ||
| 100 | } | ||
| 101 | } elsif ($command eq '-v') { # Show values | ||
| 102 | for my $function (sort($Psh::term->get_all_function_names)) { | ||
| 103 | Psh::Util::print_out("$function "); | ||
| 104 | my(@keys) = $Psh::term->invoking_keyseqs($function); | ||
| 105 | if (@keys) { | ||
| 106 | Psh::Util::print_out(qq{can be found on "},join('", "',@keys),qq{".\n}); | ||
| 107 | } else { | ||
| 108 | Psh::Util::print_out("is not bound to any keys\n"); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } elsif ($command eq '-d') { # Dump values | ||
| 112 | for my $function (sort($Psh::term->get_all_function_names)) { | ||
| 113 | my(@keys) = $Psh::term->invoking_keyseqs($function); | ||
| 114 | if (@keys) { | ||
| 115 | foreach (@keys) { | ||
| 116 | Psh::Util::print_out(qq{"$_": $function\n}); | ||
| 117 | } | ||
| 118 | } else { | ||
| 119 | Psh::Util::print_out("# $function (not bound)\n"); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } elsif ($command eq '-f') { # Read file | ||
| 123 | my $file = shift(@$args); | ||
| 124 | $Psh::term->read_init_file($file); | ||
| 125 | if ($!) { | ||
| 126 | Psh::Util::print_out("bind: cannot read $file: $!\n"); | ||
| 127 | $status = 1; | ||
| 128 | } | ||
| 129 | } elsif ($command eq '-q') { # Query a single function | ||
| 130 | my $function = shift(@$args); | ||
| 131 | Psh::Util::print_out("$function "); | ||
| 132 | my(@keys) = $Psh::term->invoking_keyseqs($function); | ||
| 133 | if (@keys) { | ||
| 134 | Psh::Util::print_out(qq{can be found on "},join('", "',@keys),qq{".\n}); | ||
| 135 | } else { | ||
| 136 | Psh::Util::print_out("is not bound to any keys\n"); | ||
| 137 | } | ||
| 138 | } elsif ($command =~ /:/) { | ||
| 139 | unless ($command =~ /:./) { | ||
| 140 | # This let's the user do things like this: | ||
| 141 | # bind "\en": history-search-forward "\ep": history-search backward | ||
| 142 | $command .= shift(@$args); | ||
| 143 | } | ||
| 144 | ### This is to make my bash-happy bind statements work. | ||
| 145 | ### This is undoubtably dumb. If someone could tell me | ||
| 146 | ### how to make readline work with M- as meta, I'd be | ||
| 147 | ### enternally grateful. | ||
| 148 | $command =~ s/^M-(.)/"\\e$1"/g; | ||
| 149 | ### | ||
| 150 | $Psh::term->parse_and_bind($command); | ||
| 151 | } else { | ||
| 152 | # print help (unknown option) | ||
| 153 | Psh::Util::print_out("bind: illegal option: $command\n"); | ||
| 154 | Psh::Util::print_out("usage: bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline_func]\n"); | ||
| 155 | $status = 1; | ||
| 156 | last; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } elsif (defined($Psh::term) and $Psh::term->can('bind')) { | ||
| 160 | # Term::ReadLine::Perl | ||
| 161 | my $args = pop; | ||
| 162 | while (my $command = shift(@$args)) { | ||
| 163 | if ($command eq '-l') { | ||
| 164 | Psh::Util::print_out("bind: -l option currently not supported by ".ref($Psh::term)."\n"); | ||
| 165 | $status = 1; | ||
| 166 | last; | ||
| 167 | } elsif ($command eq '-m') { # Set keymap | ||
| 168 | my $keymap_name = shift(@$args); | ||
| 169 | my $map = ($keymap_name =~ /^(emacs|vi)$/i)?$1:undef; | ||
| 170 | if (defined($map)) { | ||
| 171 | $Psh::term->set('EditingMode',$map); | ||
| 172 | } else { | ||
| 173 | Psh::Util::print_out(qq{bind: `$keymap_name': illegal keymap name\n}); | ||
| 174 | $status = 1; | ||
| 175 | } | ||
| 176 | } elsif ($command eq '-v') { # Show values | ||
| 177 | Psh::Util::print_out("bind: -v option currently not supported by ".ref($Psh::term)."\n"); | ||
| 178 | $status = 1; | ||
| 179 | last; | ||
| 180 | } elsif ($command eq '-d') { # Dump values | ||
| 181 | Psh::Util::print_out("bind: -d option currently not supported by ".ref($Psh::term)."\n"); | ||
| 182 | $status = 1; | ||
| 183 | last; | ||
| 184 | } elsif ($command eq '-f') { # Read file | ||
| 185 | Psh::Util::print_out("bind: -f option currently not supported by ".ref($Psh::term)."\n"); | ||
| 186 | $status = 1; | ||
| 187 | last; | ||
| 188 | } elsif ($command eq '-q') { # Query a single function | ||
| 189 | Psh::Util::print_out("bind: -q option currently not supported by ".ref($Psh::term)."\n"); | ||
| 190 | $status = 1; | ||
| 191 | last; | ||
| 192 | } elsif ($command =~ /:/) { | ||
| 193 | unless ($command =~ /:./) { | ||
| 194 | # This let's the user do things like this: | ||
| 195 | # bind "\en": history-search-forward "\ep": history-search backward | ||
| 196 | $command .= shift(@$args); | ||
| 197 | } | ||
| 198 | my($keys,$function) = split(/\s*:\s*/,$command,2); | ||
| 199 | $keys =~ s/^"(.*)"$/$1/ or | ||
| 200 | $keys =~ s/^'(.*)'$/$1/; | ||
| 201 | $Psh::term->bind($keys,$function); | ||
| 202 | } else { | ||
| 203 | # print help (unknown option) | ||
| 204 | Psh::Util::print_out("bind: illegal option: $command\n"); | ||
| 205 | Psh::Util::print_out("usage: bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline_func]\n"); | ||
| 206 | $status = 1; | ||
| 207 | last; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } elsif (defined($Psh::term) and ref($Psh::term)) { | ||
| 211 | Psh::Util::print_out("bind requires a more capable readline than ".ref($Psh::term)."\n"); | ||
| 212 | $status = 1; | ||
| 213 | } else { | ||
| 214 | Psh::Util::print_out("bind: No effect in non-interactive terminal\n"); | ||
| 215 | $status = 1; | ||
| 216 | } | ||
| 217 | return ($status,undef); | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; |