Term-ReadLine-Zoid-0.07/0000755000000000000000000000000010150362403014663 5ustar rootroot00000000000000Term-ReadLine-Zoid-0.07/lib/0000755000000000000000000000000010150362403015431 5ustar rootroot00000000000000Term-ReadLine-Zoid-0.07/lib/Term/0000755000000000000000000000000010150362403016340 5ustar rootroot00000000000000Term-ReadLine-Zoid-0.07/lib/Term/ReadLine/0000755000000000000000000000000010150362403020023 5ustar rootroot00000000000000Term-ReadLine-Zoid-0.07/lib/Term/ReadLine/Zoid/0000755000000000000000000000000010150362403020730 5ustar rootroot00000000000000Term-ReadLine-Zoid-0.07/lib/Term/ReadLine/Zoid/ISearch.pm0000644000000000000000000000531110150362403022604 0ustar rootroot00000000000000package Term::ReadLine::Zoid::ISearch; use strict; use base 'Term::ReadLine::Zoid'; no warnings; # undef == '' down here our $VERSION = 0.06; our %_keymap = ( backspace => 'backward_delete_char', ctrl_R => 'isearch_again', _on_switch => 'is_switch', _default => 'self_insert' ); sub keymap { return \%_keymap } sub is_switch { my $self = shift; $$self{is_lock} = undef; $$self{is_hist_p} = -1; $$self{is_save} = [[''], [0,0], undef]; } sub is_switch_back { my ($self, $key) = @_; $$self{_hist_save} = $self->save(); @$self{qw/lines pos hist_p/} = @{$$self{is_save}}; $self->switch_mode(); $self->do_key($key); } sub draw { # rendering this inc mode is kinda consuming my ($self, @args) = @_; my $save = $self->save(); my $string = join "\n", @{$$self{lines}}; $$self{prompt} = "i-search qr($string): "; goto DRAW unless length $string; my ($result, $match, $hist_p) = (undef, '', $$self{is_hist_p}); $$self{last_search} = ['b', $string]; my $reg = eval { qr/^(.*?$string)/ }; goto DRAW if $@; while ($hist_p < $#{$$self{history}}) { $hist_p++; next unless $$self{history}[$hist_p] =~ $reg; ($result, $match) = ($$self{history}[$hist_p], $1); last; } if (defined $result) { push @{$$self{last_search}}, $hist_p; $$self{is_lock} = undef; $$self{lines} = [ split /\n/, $result ]; my @match = split /\n/, $match; $$self{pos} = [length($match[-1]), $#match]; } else { $$self{is_lock} = 1 } DRAW: Term::ReadLine::Zoid::draw($self, @args); $$self{is_save} = [ $$self{lines}, $$self{pos}, $hist_p]; $self->restore($save); } sub self_insert { if ($_[0]{is_lock}) { $_[0]->bell } elsif ($_[0]->key_binding($_[1], $_[0]{config}{default_mode})) { goto \&is_switch_back; } else { goto \&Term::ReadLine::Zoid::self_insert } } sub isearch_again { $_[0]{is_hist_p} = $_[0]{last_search}[-1] if $_[0]{last_search} } 1; __END__ =head1 NAME Term::ReadLine::Zoid::ISearch - a readline incremental search mode =head1 SYNOPSIS This class is used as a mode under L, see there for usage details. =head1 DESCRIPTION This mode is intended as a work alike for the incremental search found in the gnu readline library. In this mode the string you enter is regarded as a B which is used to do an incremental histroy search. Pressing '^R' repeatingly will give alternative results. Special keys like movements or the C drop you out of this mode and set the edit line to the last search result. =head1 AUTHOR Jaap Karssenberg || Pardus [Larus] Epardus@cpan.orgE Copyright (c) 2004 Jaap G Karssenberg. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO =cut Term-ReadLine-Zoid-0.07/lib/Term/ReadLine/Zoid/ViCommand.pm0000644000000000000000000006451510150362403023156 0ustar rootroot00000000000000package Term::ReadLine::Zoid::ViCommand; use strict; use vars '$AUTOLOAD'; no strict 'refs'; use AutoLoader; use base 'Term::ReadLine::Zoid'; no warnings; # undef == '' down here our $VERSION = 0.05; sub AUTOLOAD { # more intelligent inheritance my $sub = $AUTOLOAD; $sub =~ s/.*:://; my $m = $_[0]->can($sub) ? 'AutoLoader' : 'Term::ReadLine::Zoid'; ${$m.'::AUTOLOAD'} = $AUTOLOAD; goto &{$m.'::AUTOLOAD'}; } =head1 NAME Term::ReadLine::Zoid::ViCommand - a readline command mode =head1 SYNOPSIS This class is used as a mode under L, see there for usage details. =head1 DESCRIPTION This mode provides a "vi command mode" as specified by the posix spec for the sh(1) utility. It intends to include at least all key-bindings mentioned by the posix spec for the vi mode in sh(1). It also contains some extensions borrowed from vim(1) and some private extensions. This mode has a "kill buffer" that stores the last killed text so it can be yanked again. This buffer has only one value, it isn't a "kill ring". =head1 KEY MAPPING Since ViCommand inherits from MultiLine, which in turn inherits from Term::ReadLine::Zoid, key bindings are also inherited unless explicitly overloaded. Control-d is ignored in this mode. =over 4 =cut our %_keymap = ( return => 'accept_line', ctrl_D => 'bell', ctrl_Z => 'sigtstp', backspace => 'backward_char', escape => 'vi_reset', ctrl_A => 'vi_increment', ctrl_X => 'vi_increment', _on_switch => 'vi_switch', _isa => 'multiline', # but wait .. self_insert is overloaded ); sub keymap { return \%_keymap } sub vi_switch { my $self = shift; return $$self{_loop} = undef if $$self{_vi_mini_b}; $$self{vi_command} = ''; $$self{vi_history} ||= []; $self->backward_char unless $_[1] or $$self{pos}[0] == 0; } our @vi_motions = (' ', ',', qw/0 b F l W ^ $ ; E f T w | B e h t/); our %vi_subs = ( '#' => 'vi_comment', '=' => 'vi_complete', '\\' => 'vi_complete', '*' => 'vi_complete', '@' => 'vi_macro', '~' => 'vi_case', '.' => 'vi_repeat', ' ' => 'forward_char', '^' => 'vi_home', '$' => 'end_of_line', '0' => 'beginning_of_line', '|' => 'vi_cursor', ';' => 'vi_c_repeat', ',' => 'vi_c_repeat', '_' => 'vi_topic', '-' => 'vi_K', '+' => 'vi_J', 'l' => 'forward_char', 'h' => 'backward_char', 't' => 'vi_F', 'T' => 'vi_F', ); our %vi_commands = ( '/' => 'bsearch', '?' => 'fsearch', '!' => 'shell', 'q' => 'quit', ); sub self_insert { my ($self, $key) = @_; if (length($key) > 1) { # no vague chars $self->bell; $$self{vi_command} = ''; } else { $$self{vi_command} .= $key } if ($$self{vi_command} =~ /^[\/\?\:]/) { $self->vi_mini_buffer($key) } elsif ($$self{vi_command} =~ /^0|^(\d*)(\D)/) { # this is where a command gets executed my ($cnt, $cmd) = ($1||1, $2||'0'); my $sub = $vi_subs{$cmd} || 'vi_'.uc($cmd); #print STDERR "string: $$self{vi_command} cnt: $cnt sub: $sub\n"; my $r; if ($self->can($sub)) { my $s = $self->save(); $r = $self->$sub($cmd, $cnt); # key, count push @{$$self{undostack}}, $s unless lc($cmd) eq 'u' or join("\n", @{$$s{lines}}) eq join("\n", @{$$self{lines}}); } else { $self->bell() } $$self{vi_last_cmd} = $$self{vi_command} if $$self{vi_command} && ! grep( {$_ eq $cmd} @vi_motions, '.'); # for repeat ('.') $$self{vi_command} = ''; #print STDERR "return: $r vi_last_cmd: $$self{vi_last_cmd}\n"; return $r; } else { return if $$self{vi_command} =~ /^\d+$/; #print STDERR "string: $$self{vi_command} rejected\n"; $self->bell; $$self{vi_command} = ''; } return 0; } # ############ # # Key bindings # # ############ # # Subs get args ($self, $key, $count) sub vi_reset { $_[0]{vi_command} = ''; return 0 } sub sigtstp { kill 20, $$ } # SIGTSTP =item escape Reset the command mode. =item return =item ^J Return the current edit line to the application for execution. =item ^Z Send a SIGSTOP to the process of the application. Might not work when the application ignores those, which is something shells tend to do. =item i Switch back to insert mode. =item I Switch back to insert mode at the begin of the edit line. =item a Enter insert mode after the current cursor position. =item A Enter insert mode at the end of the edit line. =cut sub vi_I { $_[0]{pos}[0] = 0 if $_[1] eq 'I'; $_[0]->switch_mode(); } sub vi_A { ($_[1] eq 'A') ? $_[0]->end_of_line : $_[0]->forward_char ; $_[0]->switch_mode(); } =item m Switch to multiline insert mode, see L. (private extension) =item M Switch to multiline insert mode at the end of the edit buffer. (private extension) =cut sub vi_M { if ($_[1] eq 'M') { $_[0]{pos}[1] = $#{$_[0]{lines}}; $_[0]->end_of_line; } else { $_[0]->forward_char } $_[0]->switch_mode('multiline') } =item R Enter insert mode with replace toggled on. (vim extension) =cut sub vi_R { my $self = shift; return $self->vi_r(@_) if $_[0] eq 'r'; $self->switch_mode(); $$self{replace} = 1; } ## more bindings are defined in __END__ section for autosplit ## __END__ ## Two helper subs ## sub _get_chr { # get extra argument my $self = shift; my $chr = $self->key_name( $self->read_key ); return $self->vi_reset if $chr eq 'escape'; return undef if length $chr > 1; #print STDERR "got argument chr: $chr\n"; $$self{vi_command} .= $chr; return $chr; } sub _do_motion { # get and do a motion my ($self, $ignore, $cnt) = @_; my $key = $self->key_name( $self->read_key ); return $self->vi_reset if $key eq 'escape'; return $self->bell unless grep {$_ eq $key} @vi_motions, $ignore, qw/left right up down home end/; my $vi_cmd = $$self{vi_command}; #print STDERR "got argument motion: $key\n"; my $re = 1; unless ($key eq $ignore) { my $pos = [@{$$self{pos}}]; # force copy $$self{vi_command} = (grep {$_ eq $key} qw/0 ^ $/) ? '' : $cnt ; $re = $self->do_key($key, $cnt); $$self{pos} = $pos unless $re; # reset pos if unsuccessfull $$self{pos}[0]++ if lc($key) eq 'e' and $$self{pos}[0] < length $$self{lines}[ $$self{pos}[1] ]; # always one exception :S } $$self{vi_command} = $vi_cmd . $key; return $re; } =item # Makes current edit line a comment that will be listed in the history, but won't be executed. Only works if the 'comment_begin' option is set. =cut sub vi_comment { $_[0]{lines}[ $_[0]{pos}[1] ] = $_[0]{config}{comment_begin} . ' ' . $_[0]{lines}[ $_[0]{pos}[1] ]; $_[0]{poss}[0] += 2 unless $_[0]{poss}[1]; } =item = Display possible shell word completions, does not modify the edit line. =item \ Do pathname completion (using File::Glob) and insert the largest matching part in the edit line. =item * Do pathname completion but inserts B matches. =cut sub vi_complete { my ($self, $key) = @_; return $self->possible_completions() if $key eq '='; my $buffer = join "\n", @{$$self{lines}}; my $begin = substr $buffer, 0, $self->pos2off($$self{pos}), ''; $begin =~ s/(\S*)$//; my $glob = $1; $$self{pos}[0] -= length $1; use File::Glob ':glob'; $glob .= '*' unless $glob =~ /[\*\?\[]/; my @list = bsd_glob($glob, GLOB_TILDE | GLOB_BRACE); my $string; if ($key eq '\\') { @list = $self->longest_match(@list); $string = shift(@list); $self->output(@list); } elsif ($key eq '*') { $string = join ' ', @list } $$self{pos}[0] += length $string; @{$$self{lines}} = split /\n/, $begin . $string . $buffer; $self->switch_mode() if $key eq '*'; } =item [I] @ I Regard the contents of the alias _char as a macro with editing commands. This seems a rather obfuscated feature of the posix spec to me. See also below for the L command. Note that the I argument is not posix compliant, but it seems silly not to use it. =cut sub vi_macro { my ($self, undef, $cnt) = @_; my $n = $self->_get_chr; #print STDERR "macro arg was: $n\n"; return $self->bell unless $n =~ /^\w$/; return unless exists $$self{config}{aliases}{'_'.$n}; my $macro = $$self{config}{aliases}{"_$n"}; for (1..$cnt) { $self->switch_mode(); $self->press($macro); } } =item [I] ~ Reverse case for I characters. =cut sub vi_case { # reverse case my ($self, undef, $cnt) = @_; my $str = substr $$self{lines}[ $$self{pos}[1] ], $$self{pos}[0], $cnt, ''; $str =~ s/(([[:lower:]]+)|[[:upper:]]+)/$2 ? uc($1) : lc($1)/eg; substr $$self{lines}[ $$self{pos}[1] ], $$self{pos}[0], 0, $str; $$self{pos}[0] += length $str; } =item [I] . Repeat the last non-motion command. If no count is specified the original count of the command is used. =cut sub vi_repeat { my ($self, undef, $cnt) = @_; undef $cnt if $$self{vi_command} !~ /^$cnt/; return $self->bell if ! length $$self{vi_last_cmd}; #print STDERR "repeat last command: $$self{vi_last_cmd}\n"; $$self{vi_last_cmd} =~ /^(\d*)(.)(.*)/; die "BUG: we ain't gonna loop all day !" if $2 eq '.'; $$self{vi_command} = defined $cnt ? $cnt : $1 || ''; $self->unread_key($3); $self->self_insert($2); } =item v Edit the buffer with the editor specified by the C environment variable or the L option, defaults to 'vi'. This function requires the L module from cpan, which in turn needs File::Spec and other packages. If these are not available this functions is disabled. =cut sub vi_V { my $self = shift; my $string = $$self{config}{editor} || $ENV{EDITOR} || 'vi %'; $string .= ' %' unless $string =~ /\%/; $self->shell($string); } =item [I] l =item [I] I Move the cursor to the right. =item [I] h Move the cursor to the left. =cut ## vi_L and vi_H are implemented by parent left n right =item [I] w =item [I] W Move the cursor to the begin of the next word or bigword. (A bigword exists of non-whitespace chars, while a word exists of alphanumeric chars only.) =cut sub vi_W { # no error, just end of line my ($self, $key, $cnt) = @_; my $w = ($key eq 'W') ? '\\S' : '\\w'; my $l = $$self{lines}[ $$self{pos}[1] ]; for (1..$cnt) { if ($l =~ /^.{$$self{pos}[0]}(.+?)(?end_of_line; last; } } return 1; } =item [I] e =item [I] E Move the cursor to the end of the current word or bigword. =cut sub vi_E { # no error, just end of line my ($self, $key, $cnt) = @_; my $w = ($key eq 'E') ? '\\S' : '\\w'; my $l = $$self{lines}[ $$self{pos}[1] ]; for (1..$cnt) { if ($l =~ /^.{$$self{pos}[0]}($w?.*?$w+)/) { $$self{pos}[0] += length($1) - 1 } else { $self->end_of_line; last; } } return 1; } =item [I] b =item [I] B Move the cursor to the begin of the current word or bigword. =cut sub vi_B { # no error, just begin of line my ($self, $key, $cnt) = @_; my $w = ($key eq 'B') ? '\\S' : '\\w'; my $l = $$self{lines}[ $$self{pos}[1] ]; for (1..$cnt) { $l = substr($l, 0, $$self{pos}[0]); if ($l =~ /($w+[^$w]*)$/) { $$self{pos}[0] -= length $1 } else { $self->beginning_of_line; last; } } return 1; } =item ^ Move the cursor to the first non-whitespace on the edit line. =item $ Move the cursor to the end of the edit line. =item 0 Move the cursor to the begin of the edit line. =cut sub vi_home { my $self = shift; $$self{lines}[ $$self{pos}[1] ] =~ /^(\s*)/; $$self{pos}[0] = length $1; return 1; } =item [I] | Set the cursor to position I (1-based). =cut sub vi_cursor { $_[0]{pos}[0] = $_[2] - 1; 1; } =item [I] f I Set cursor to I'th occurrence of I to the right. The cursor is placed on I. =item [I] F I Set cursor to I'th occurrence of I to the left. The cursor is placed on I. =item [I] t I Set cursor to I'th occurrence of I to the right. The cursor is placed before I. =item [I] T I Set cursor to I'th occurrence of I to the left. The cursor is placed after I. =cut sub vi_F { my ($self, $key, $cnt, $chr) = @_; unless ($chr) { $chr = $self->_get_chr(); return $self->bell if length $chr > 1; $$self{vi_last_c_move} = [$key, $chr]; } my ($l, $x) = ( $$self{lines}[ $$self{pos}[1] ], $$self{pos}[0] ); if ($key eq 'T' or $key eq 'F') { $l = substr($l, 0, $x); return $self->bell unless $l =~ /.*((?:$chr.*){$cnt})$/; $$self{pos}[0] -= length($1) - (($key eq 'T') ? 1 : 0); return length($1); } else { # ($key eq 't' || $key eq 'f') return $self->bell unless $l =~ /^..{$x}((?:.*?$chr){$cnt})/; $$self{pos}[0] += length($1) - (($key eq 't') ? 1 : 0); return length($1); } } ## vi_T is aliased to vi_F in %vi_subs =item [I] ; Repeat the last 'f', 'F', 't', or 'T' command. Count of last command is ignored. =item [I] , Like ';' but with direction reversed. =cut sub vi_c_repeat { my ($self, $key, $cnt) = @_; return $self->bell unless $$self{vi_last_c_move}; my ($ckey, $chr) = @{ $$self{vi_last_c_move} }; $ckey = ($ckey eq 't' or $ckey eq 'f') ? uc($ckey) : lc($ckey) if $key eq ','; $self->vi_F($ckey, $cnt, $chr); } =item [I] c I Delete characters between the current position and the position after the I, I applies to I. After the deletion enter insert mode. The "motion" 'c' deletes the current edit line. =item C Delete from cursor to end of line and enter insert mode. =cut sub vi_C { # like vi_D but without killbuf and with insert mode my ($self, $key, $cnt) = @_; my $pos = [ @{$$self{pos}} ]; # force copy if ($key eq 'C') { $self->end_of_line } else { return unless $self->_do_motion('c', $cnt) } if ($$self{vi_command} =~ /cc$/) { splice(@{$$self{lines}}, $$self{pos}[1], 1) } else { $self->substring('', $pos, $$self{pos}) } $self->switch_mode(); } =item S Delete current line and enter insert mode. =cut sub vi_S { my $self = shift; $$self{lines}[ $$self{pos}[1] ] = ''; $self->{pos}[0] = 0; $self->switch_mode(); } =item [I] r I Replace the character under the cursor (and the I characters next to it) with I. =cut sub vi_r { # this sub is an exception in the naming scheme my ($self, undef, $cnt) = @_; my $chr = $self->_get_chr(); substr $$self{lines}[ $$self{pos}[1] ], $$self{pos}[0], $cnt, $chr x $cnt; $$self{pos}[0] += $cnt - 1; } =item [I] _ Insert a white space followed by the last (or I'th) bigword from the previous history entry ans enter insert mode. Quotes are not respected by this function. =cut sub vi_topic { my ($self, undef, $cnt) = @_; $cnt = ($cnt == 1 and $$self{vi_command} !~ /^1/) ? -1 : $cnt-1; return $self->bell unless @{$$self{history}}; my $buffer = join "\n", $$self{history}[0]; $buffer =~ s/^\s+|\s+$//g; my @words = split /\s+/, $buffer; my $string = " $words[$cnt]"; $self->substring($string); $$self{pos}[0] .= length $string; $self->switch_mode(); } =item [I] x Delete I characters and place them in the save buffer. =item [I] X Delete I characters before the cursor position and place them in the save buffer. ('x' is like 'delete', 'X' like backspace) =cut sub vi_X { my ($self, $key, $cnt) = @_; if ($key eq 'X') { return $self->bell if $$self{pos}[0] < $cnt; $$self{pos}[0] -= $cnt; } $$self{killbuf} = substr $$self{lines}[ $$self{pos}[1] ], $$self{pos}[0], $cnt, ''; } =item [I] d I Delete from the current cursor position to the position resulting from I times I. The deleted part will be placed in the save buffer. The "motion" 'd' deletes the current line. =item D Delete from the cursor position until the end of the line and put the deleted part in the save buffer. =cut sub vi_D { my ($self, $key, $cnt) = @_; my $pos = [ @{$$self{pos}} ]; # force copy if ($key eq 'D') { $self->end_of_line } else { return unless $self->_do_motion('d', $cnt) } if ($$self{vi_command} =~ /dd$/) { $$self{killbuf} = splice(@{$$self{lines}}, $$self{pos}[1], 1)."\n"; } else { $$self{killbuf} = $self->substring('', $pos, $$self{pos}) } } =item [I] y I Yank (copy) characters from the current cursor position to the position resulting from I times I to the save buffer. the "motion" 'y' yanks the current line. =item Y Like y but from cursor till end of line. =cut sub vi_Y { # like vi_D but only copies, doesn't delete my ($self, $key, $cnt) = @_; my $pos = [ @{$$self{pos}} ]; # force copy if ($key eq 'Y') { $self->end_of_line } else { return unless $self->_do_motion('y', $cnt) } if ($$self{vi_command} =~ /yy$/) { $$self{killbuf} = $$self{lines}[ $$self{pos}[1] ]."\n"; } else { $$self{killbuf} = $self->substring(undef, $pos, $$self{pos}) } $$self{pos} = $pos; # reset pos } =item [I] p Insert I copies of the the save buffer after the cursor. =item [I] P Insert I copies of the the save buffer before the cursor. =cut sub vi_P { my ($self, $key, $cnt) = @_; return unless length $$self{killbuf}; $self->forward_char if $key eq 'p'; $self->substring($$self{killbuf} x $cnt); } =item u Undo the last command that changed the edit line. =item U Undo all changes. TODO all changes since when ? since entering the command mode ? =cut sub vi_U { my ($self, $key, $cnt) = @_; return $self->bell() unless @{$$self{undostack}}; $self->restore(pop @{$$self{undostack}}); } =item [I] k =item [I] - Go I lines backward in history. =cut sub vi_K { $_[0]->previous_history || last for 1 .. $_[2]; $_[0]->beginning_of_line; } =item [I] j =item [I] + Go I lines forward in history. =cut sub vi_J { $_[0]->next_history || last for 1 .. $_[2]; $_[0]->beginning_of_line; } =item [I] G Go to history entry number I, or to the first history entry. =cut sub vi_G { return $_[0]->bell if $_[2] > @{$_[0]{history}}; $_[0]->set_history( @{$_[0]{history}} - $_[2] ); # we keep the history in the reversed direction } =item n Repeat the last history search by either the '/' or '?' minibuffers or the incremental search mode. =item N Repeat the last history search in the oposite direction. =cut sub vi_N { # last_search = [ dir, string, hist_p ] my ($self, $key, undef, $dir) = @_; # dir == direction return $self->bell unless $$self{last_search}; $dir ||= $$self{last_search}[0]; $dir =~ tr/bf/fb/ if $key eq 'N'; # reverse dir my $reg = eval { qr/$$self{last_search}[1]/ }; return $self->bell if $@; my ($succes, $hist_p) = (0, $$self{last_search}[2]); #print STDERR "lookign from $hist_p for: $reg\n"; if ($dir eq 'b') { while ($hist_p < $#{$$self{history}}) { $hist_p++; next unless $$self{history}[$hist_p] =~ $reg; $succes++; last; } } else { # $dir eq 'f' $hist_p = scalar @{$$self{history}} if $hist_p < 0; while ($hist_p > 0) { $hist_p--; next unless $$self{history}[$hist_p] =~ $reg; $succes++; last; } } #print STDERR "succes: $succes at: $hist_p\n"; if ($succes) { $self->set_history($hist_p); $$self{last_search}[2] = $hist_p; return 1; } else { return $self->bell } } =item : Opens a command mini buffer. This is a very minimalistic execution environment that can for instance be used to modify options if the application doesn't provide a method to do so. Also it is used for quick hacks ;) The execution of this buffer happens entirely without returning to the application. (This is a vim extension) =cut sub vi_mini_buffer { my ($self, $key) = @_; $self->switch_mode('insert'); my $save = $self->save(); @$self{qw/_vi_mini_b prompt lines pos/} = (1, $key, [''], [0,0]); $self->loop(); my $str = join "\n", @{$$self{lines}}; @$self{qw/_vi_mini_b _loop/} = (undef, 1); $self->restore($save); $self->switch_mode('command', 'no_left'); my $cmd = $key; if ($key eq ':') { $str =~ s/^([!\/?])|^\s*(\S+)(\s+|$)// or return $self->bell; $cmd = $1 || $2; } $cmd = exists($vi_commands{$cmd}) ? $vi_commands{$cmd} : $cmd; #print STDERR "mini buffer got cmd, string: $cmd, $str\n"; return $self->bell unless $self->can($cmd); return $self->$cmd($str); } =item / Opens a mini buffer where you can type a pattern to search backward through the history. The search patterns are not globs (as posix would have them), but are evaluated as perl regexes. An empty pattern repeats the previous search. =item ? Like '/' but searches in the forward direction. =cut sub bsearch { my ($self, $string) = @_; if (length $string) { $$self{last_search} = ['b', $string, -1]; eval { qr/$string/ }; if ($@) { $self->output($@); return $self->bell; } } return $self->vi_N('n', undef, 'b'); } sub fsearch { my ($self, $string) = @_; if (length $string) { $$self{last_search} = ['f', $string, -1]; eval { qr/$string/ }; if ($@) { $self->output($@); return $self->bell; } } return $self->vi_N('n', undef, 'f'); } =item ^A If cursor is on a number, increment it. (This is a vim extension) FIXME bit buggy =item ^X If cursor is on a number, decrement it. (This is a vim extension) FIXME bit buggy =cut sub vi_increment { my ($self, $key) = @_; my ($l, $x) = ( $$self{lines}[ $$self{pos}[1] ], $$self{pos}[0] ); my $add = ($key eq 'ctrl_A') ? 1 : -1; return $self->bell unless $l =~ /^(.{0,$x}?)(0x(?i:[a-f\d])+|\d+)(.*?)$/; # FIXME triple check this regexp my ($pre, $int, $post) = ($1, $2, $3); $int = ($int =~ /^0x/) ? sprintf("0x%x", hex($int) + $add) : ($int + $add) ; $$self{lines}[ $$self{pos}[1] ] = $pre . $int . $post; } # ######## # # Commands # # ######## # =back =head1 COMMANDS These can be used from the ":" mini buffer. Some commands are borrowed from vim, but no guarantee what so ever. =over 4 =item B Return undef to the application (like '^D' in insert mode). =item B [I<+o>|I<-o>] [I for a description. By default enabled. =item beat This option can contain a CODE reference. It is called on the heartbeat event. =item bell This option can contain a CODE reference. The default is C, which makes the terminal ring a bell. =item comment_begin This option can be set to a string, if the edit line starts with this string the line is regarded to be a comment and is not returned to the application, but it will appear in the history if 'autohistory' is also set. Defaults to "#". When there are multiple lines in the buffer they all need to start with the comment string for the buffer to be regarded as a comment. =item completion_function This option can contain either a code ref or the name of a function to perform completion. For compatibility with Term::ReadLine::Perl the global scalar C<$readline::rl_completion_function> will be checked if this option isn't defined. The function will get the following arguments: C<$word>, C<$buffer>, C<$start>. Where C<$word> is the word before the cursor, while C<$buffer> is the complete text on the command line; C<$start> is the offset of C<$word> in C<$buffer>. The function should return a list of possible completions of C<$word>. The completion list is checked for double entries. There is B default. FIXME tell about the meta fields for advanced completion =item default_mode Specifies the mode the buffer starts in when you do a C, also other modes return to this mode if you exit them. The default is 'insert' which is the single-line insert mode. If you always want to edit in multiline mode set this option to 'multiline'. =item maxcomplete Maximum number of completions to be displayed, when the number of completions is bigger the user is asked before displaying them. If set to zero completions are always displayed. If this option is set to the string 'pager' the user is asked when the number of completions is to big to fit on screen and a pager would be used. =item minline This option controls which lines are included in the history, lines shorter then this number are ignored. When set to "0" all lines are included in the history, when set to C all lines are ignored. Defaults to "0". =item PS2 This option can contain the prompt to be used for extra buffer lines. It defaults to C<< "> " >>. Although the "PS1" prompt (as specified as an argument to the C method) can contain newlines, the PS2 prompt can't. =item RPS1 This option can contain a string that will be shown on the right side of the screen. This is known as the "right prompt" and the idea is stolen from zsh(1). =item title Used to set the terminal title, defaults to the appname. =item low_latency Changes the escape sequences are read from input. If true delays evalution of the escape key till the next char is known. By default disabled. =back =head1 FILES This module reads a rc-file on intialisation, either F<$HOME/.perl_rl_zoid_rc>, F<$HOME/.zoid/perl_rl_zoid_rc> or F. The rc-file is a perl script with access to the Term::ReadLine::Zoid object through the method C. If you want to have different behaviour for different applications, try to check for C<< $rl->{appname} >>. # in for example ~/.perl_rl_zoid_rc my $rl = Term::ReadLine::Zoid->current(); # set low latency $rl->Attribs()->{low_latency} = 1; # alias control-space to escape $rl->bindchr( chr(0), 'escape' ); # create an ad hoc macro $rl->bindkey('^P', sub { $rl->press('mplayer -vo sdl ') } ); =head1 METHODS =head2 ReadLine api Functions specified by the L documentation. =over 4 =item C Simple constructor. Arguments are the application name (used for default prompt and title string) and optional filehandles for input and output. =item C Returns the name of the current ReadLine module actually used. =item C Returns a string entered by the user. The final newline is stripped, though the string might contain newlines elsewhere. The prompt only supports the escape "!" for the history number of the current line, use "!!" for a literal "!". All other escapes you need to parse yourself, before supplying the prompt. The prompt defaults to C<< "$appname !> " >>. If you want to do more with your prompt see L. C<$preput> can be used to set some text on the edit line allready. =item C =item C Add a command to the history (subject to the L option). If L is set this method will be called automaticly by L. =item C Returns the filehandle used for input. =item C Returns the filehandle used for output. =item C Sets L option to C<$value> and returns old value. =item C TODO - what uses does this have ? =item C Returns a reference to the options hash. =item C Returns a reference to a hash with names of implemented features. Be aware that the naming scheme is quite arbitrary, this module uses the same names as Term::ReadLine::Gnu for common features. =back =head2 Extended api =over 4 =item C =item C Simple acces to the history arry, the "set" function supports both a list and a reference, the "get" function uses "wantarray". Not sure which behaviour is compatible with T:RL::Gnu. =item C Returns number of columns and lines on the terminal. =item C This method can be called to continue the previous C call. Can be used to build a custom auto-mulitline feature. =item C Returns the current T:RL::Zoid object, for use in rc files, see L. =item C Bind a CODE reference to a key, the function gets called when the key is typed with the key name as an argument. The C<$map> argument is optional and can be either "default", "command", "isearch" or "multiline". If C<$sub> is not a reference it is considered an alias; these aliases are not recursive. For alphanumeric characters the name is the character itself, special characters have long speaking names and control characters are prefixed with a '^'. Binding combination with the meta- or alt-key is not supported (see L). =back =head2 Private api Methods for use in overload classes. I =over 4 =item C Switch to input mode C<$mode>; changes the key map and reblesses the object if the C<_on_switch> key returns a class name. =item C Reset all temporary attributes. =item C Returns a ref with a copy of some temporary attributes. Can be used to switch between multiple edit lines in combination with L. =item C Restores saved attributes. =item C Sets history entry C<$int> in the buffer. =item C Returns the longest match among the completions followed by the completions itself. Used for completion functions. =back =head1 DEVELOPMENT FIXME minimum subroutines new mode-class FIXME how to set up a keymap FIXME how to add a keymap/mode =head1 NOTES With most modern keymappings the combination of the meta key (alt) with a letter is identical with an escape character followed by that letter. Some functioality may in time be moved to the ::Base package. =head1 TODO UTF8 support, or general charset support, would be nice but at the moment I lack the means to test these things. If anyone has ideas or suggestions about this please contact me. =head1 BUGS Line wrap doesn't always displays the last character on the line right, no functional bug though. If the buffer size exceeds the screen size some bugs appear in the rendering. Please mail the author if you find any other bugs. =head1 AUTHOR Jaap Karssenberg || Pardus [Larus] Epardus@cpan.orgE Copyright (c) 2004 Jaap G Karssenberg. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L, L, L, L, L, L, L, L =cut Term-ReadLine-Zoid-0.07/Changes0000644000000000000000000000554510150362403016167 0ustar rootroot00000000000000Revision history for Term-ReadLine-Zoid - another ReadLine package 0.07 Mon Nov 22 2004 Maintenance release, just one bugfix Fri Oct 22 - Fixed a bug with the completion of quoted strings 0.06 Wed Sep 15 2004 - Fixed a bug in the clear_screen() function - Touched documentation for FileBrowse - Changed return_empty_string logic - Added T:RL:Z::Emacs with 'emacs' and 'emacs_multiline' modes - Added ^O (operate_and_get_next) 0.05 Mon Sep 6 2004 Release to accompany the 0.90 release of Zoidberg to Adam.pm - Made 'beat' also an attribute Sun Sep 5 - Made it possible for "quote" meta field to be a CODE reference so the application can handle escaping in a custom way Mon Aug 30 - Some bugfixes in FileBrowse - Modified behaviour of ^D for multiline buffers - Added basic support for buffers that have more lines then the screen Mon Aug 9 - Reverse direction of arrays in SetHistory to be compatible with T::RL::Gnu's behaviour - Added the FileBrowse (fbrowse) mode Wed Aug 4 - Added support to set attributes through the PERL_RL variable - Added the 'default_mode' attribute - Refactored the way keymaps and modes are handled - Renamed most key routines with functional names - Incorporated T:RL:Z:MultiLine into T:RL:Zoid, not all modii need to have a .pm anymore, there still is a 'multiline' keymap of course Tue Aug 3 - Fixed behaviour of 'e' and 'E' in ViCommand - ISearch does now repeated search at ^R - Changed behaviour on terminal resize a bit Tue Jul 20 - Added automatic logic for the 'maxcomplete' option Mon Jul 12 - Fixed 'quoted' vs 'quote' logic for advanced completion 0.04 Sun Jul 4 2004 - Made cursor position take control chars into account - Reversed the output of GetHistory after testing with cpan(1) Thu Jul 1 - Fixed a bug, escaped returns are now really removed - Fixed a bug, expansion works again in a non-zoid application, like cpan(1) - Added pager support for long lists of completions 0.03 Tue Jun 29 2004 - Fixed a bug in the expansion code regarding escaping - Fixed 'bindchr' and 'bindkey' vi commands - Added code for '=', '*', and '\' keybindings in the vi mode 0.02 Tue May 25 2004 Mon May 24 - Added support for more advanced completion functions Sun May 23 - Added "RPS1" setting after reading something about zsh on /. - Made it possible for the prompts to be a scalar reference Fri May 21 - Fixed an AUTOLOADER inheritance problem leading to bugs in zoid Sun May 16 - Added some simple emacs bindings like ^F, ^B etc. Tue Mar 30 - Fixed typo in the synopsis, patch by Mike Castle 0.01 Sun Mar 28 2004 Initial feature set is complete enough to release Thu Jan 15 2004 Initialised the module Term-ReadLine-Zoid-0.07/t/0000755000000000000000000000000010150362403015126 5ustar rootroot00000000000000Term-ReadLine-Zoid-0.07/t/00_use_ok.t0000644000000000000000000000046710150362403017106 0ustar rootroot00000000000000require Test::More; @modules = qw/ Term::ReadLine::Zoid::Base Term::ReadLine::Zoid Term::ReadLine::Zoid::ISearch Term::ReadLine::Zoid::ViCommand Term::ReadLine::Zoid::FileBrowse /; Test::More->import(tests => scalar @modules); use_ok($_) for @modules; # should we do a syntax check for the .al files ? Term-ReadLine-Zoid-0.07/t/30_vicommand.t0000644000000000000000000000735310150362403017602 0ustar rootroot00000000000000 use strict; BEGIN { require Test::More; use Term::ReadLine::Zoid::ViCommand; Test::More->import( tests => 18 + scalar @Term::ReadLine::Zoid::ViCommand::vi_motions - 6 ); } my $t; $t = Term::ReadLine::Zoid->new('test'); $t->{config}{bell} = sub {}; # Else the "\cG" fucks up test harness # test routines sub test_reset { $_[0]->reset(); $_[0]->switch_mode('command'); $_[0]->{lines} = [ 'duss ja', 'nou ja', 'test 123' ]; # 3 X 7,6,8 $_[0]->{pos} = [5, 1]; } # escape test_reset $t; $t->{vi_command} = '!**^sgf@#$34342dfs#$fsg#$4g^$^)*!fgd'; $t->press("\e"); ok $t->{vi_command} eq '', 'escape reset'; # h, l/space (left, right) test_reset $t; $t->press('4h'); is_deeply $t->{pos}, [1,1], 'h 1'; ok $t->self_insert('h'), 'h 2'; $t->{pos} = [0,0]; ok !$t->self_insert('h'), 'h 3'; $t->press('7 '); is_deeply $t->{pos}, [7,0], 'space'; ok $t->self_insert('l'), 'l 1'; $t->{pos} = [8,2]; ok !$t->self_insert('l'), 'l 2'; # control-z # i # I # a # A # m # M # R # # # = # \ # * # [I] @ I $t->reset; $t->{lines} = [ 'test123 123' ]; $t->{pos} = [1,0]; $t->{config}{aliases}{_A} = "\elldl"; $t->press('@A'); ok $t->{lines}[0] eq 'tet123 123', 'macro 1'; $t->press('3@A'); ok $t->{lines}[0] eq 'tet2 23', 'macro 2'; # [I] ~ $t->reset; $t->{lines} = [ 'nou ja duss jaa' ]; $t->{pos} = [0,0]; $t->press('~3l~2l~4l~'); ok $t->{lines}[0] eq 'Nou Ja Duss Jaa', '~ 1'; $t->press('015~'); ok $t->{lines}[0] eq 'nOU jA dUSS jAA', '~ 2'; # [I] . $t->reset; $t->{lines} = [ 'nou ja duss jaa' ]; $t->{pos} = [11,0]; $t->press('2dh'); ok $t->{lines}[0] eq 'nou ja du jaa', '3dh'; $t->press('..'); ok $t->{lines}[0] eq 'nou j jaa', '..'; $t->press('4.'); ok $t->{lines}[0] eq 'n jaa', '4.'; # v # [I] w # [I] W # [I] e # [I] E # [I] b # [I] B # ^ # $ # 0 # [I] | # [I] f I # [I] F I # [I] t I # [I] T I #TODO multiline the fFtT function #test_reset $t; #$t->press("\e"); #for ( ['fs', []], ['Fs', []], ['ts', []], ['Ts', []] ) { # test_reset $t; # $t->press($$_[0]); # print "# $$_[0]: $t->{pos}[0], $t->{pos}[1]\n"; # is_deeply $t->{pos}, $$_[1], $$_[0]; #} # [I] ; # [I] , # [I] c I # C # S # [I] r I $t->reset; $t->{lines} = ['gdgfgfffghg']; $t->{pos} = [2,0]; $t->press('rall3ra'); ok $t->{lines}[0] eq 'gdafaaafghg', 'r'; # [I] _ $t->reset; $t->{history} = ['word1 word2 word3']; $t->press("_\el2_\el1_"); ok $t->{lines}[0] eq ' word3 word2 word1', '_'; # [I] x # [I] X # [I] d I # D # lines have to change, the exact change isn't tested here, # thats up to the motion test test_reset $t; $t->press("\e"); my $lines = join '', @{$$t{lines}}; #TODO multiline the fFtT function #$t->press('fs'); # for repeat movements for my $k (@Term::ReadLine::Zoid::ViCommand::vi_motions, 'd') { test_reset $t; # print STDERR "# pos: $$t{pos}[0],$$t{pos}[1] lines: ".join( "\n", @{$$t{lines}})."\n"; my $p = 'd'.$k; next if grep {$_ eq $k} qw/f F t T ;/, ','; # $p .= 's' if grep {$_ eq $k} qw/f F t T/; $p = '1'.$p if $k eq '|'; # print STDERR "# pressing $p\n"; $t->press($p); # print STDERR "# pos: $$t{pos}[0],$$t{pos}[1] lines: ".join( "\n", @{$$t{lines}})."\n"; ok length( join( '', @{$$t{lines}}) ) < length $lines, $p; } test_reset $t; $t->press('D'); ok length( join( '', @{$$t{lines}}) ) < length $lines, 'D'; # [I] y I # Y # [I] p # [I] P # u # U # [I] k # [I] - # [I] j # [I] + # [I] G # control-a # control-x # TODO # : # B [I<+o>|I<-o>] [I