… | |
… | |
701 | |
701 | |
702 | sub new { |
702 | sub new { |
703 | my $class = shift; |
703 | my $class = shift; |
704 | |
704 | |
705 | my $self = $class->SUPER::new ( |
705 | my $self = $class->SUPER::new ( |
|
|
706 | min_w => $::WIDTH * 0.25, # workaround for layout problems #d# |
706 | bg => [0, 0, 0, 0.8], |
707 | bg => [0, 0, 0, 0.8], |
707 | @_, |
708 | @_, |
708 | ); |
709 | ); |
709 | |
710 | |
710 | $self->add ($self->{vbox} = new DC::UI::VBox); |
711 | $self->add ($self->{vbox} = new DC::UI::VBox); |
… | |
… | |
801 | |
802 | |
802 | sub reset { |
803 | sub reset { |
803 | my ($self) = @_; |
804 | my ($self) = @_; |
804 | |
805 | |
805 | $self->hide; |
806 | $self->hide; |
|
|
807 | delete $self->{command_lists}; |
806 | delete $self->{command_list}; |
808 | delete $self->{command_list}; |
807 | } |
809 | } |
808 | |
810 | |
809 | sub set_prefix { |
811 | sub set_prefix { |
810 | my ($self, $prefix) = @_; |
812 | my ($self, $prefix) = @_; |
… | |
… | |
844 | $self->{entry}->emit (key_down => $ev); |
846 | $self->{entry}->emit (key_down => $ev); |
845 | } |
847 | } |
846 | |
848 | |
847 | sub update_labels { |
849 | sub update_labels { |
848 | my ($self) = @_; |
850 | my ($self) = @_; |
|
|
851 | |
|
|
852 | use sort qw(stable); |
849 | |
853 | |
850 | my $text = $self->{entry}->get_text; |
854 | my $text = $self->{entry}->get_text; |
851 | |
855 | |
852 | length $text |
856 | length $text |
853 | or return $self->hide; |
857 | or return $self->hide; |
… | |
… | |
863 | if ($text ne $self->{last_search}) { |
867 | if ($text ne $self->{last_search}) { |
864 | my @match; |
868 | my @match; |
865 | |
869 | |
866 | if ($text =~ /^(.*?)\s+$/) { |
870 | if ($text =~ /^(.*?)\s+$/) { |
867 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
871 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
868 | @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); |
872 | @match = ([[$cmd,' (appended whitespace suppresses completion)'], $text]); |
869 | } else { |
873 | } else { |
870 | # @match is [command, penalty, command with arguments] until sort |
874 | # @match is [command, penalty, command with arguments] until sort |
871 | |
875 | |
872 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
876 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
873 | |
877 | |
… | |
… | |
888 | my $regexp_partial = do { |
892 | my $regexp_partial = do { |
889 | my $regexp = "^\Q$text\E(.*)"; |
893 | my $regexp = "^\Q$text\E(.*)"; |
890 | qr<$regexp> |
894 | qr<$regexp> |
891 | }; |
895 | }; |
892 | |
896 | |
893 | for my $list (values %{ $self->{command_list} }) { |
897 | for my $list (@{ $self->{command_lists} }, "spells") { |
894 | for (@$list) { |
898 | for (@{ $self->{command_list}{$list} }) { |
895 | # we only match and score if the first character matches, |
899 | # we only match and score if the first character matches, |
896 | # so quickly rule out all others first. |
900 | # so quickly rule out all others first. |
897 | next unless $first_char = substr $_, 0, 1; |
901 | next unless $first_char = substr $_, 0, 1; |
898 | |
902 | |
899 | my @scores; |
903 | my @scores; |
… | |
… | |
926 | if (my @penalty = $_ =~ $regexp_abbrev) { |
930 | if (my @penalty = $_ =~ $regexp_abbrev) { |
927 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
931 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
928 | } |
932 | } |
929 | |
933 | |
930 | # Pick the best option for this command |
934 | # Pick the best option for this command |
931 | push @match, (sort { |
935 | push @match, (sort { $b->[1] <=> $a->[1] } @scores)[0]; |
932 | $b->[1] <=> $a->[1] |
|
|
933 | } @scores)[0]; |
|
|
934 | } |
936 | } |
935 | } |
937 | } |
936 | |
938 | |
937 | # @match is now [command object, command with arguments] |
939 | # @match is now [command object, command with arguments] |
938 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
940 | @match = map [$_->[0], $_->[2]], |
939 | sort { |
|
|
940 | $b->[1] <=> $a->[1] |
941 | sort { $b->[1] <=> $a->[1] } |
941 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
|
|
942 | or (length $b->[0]) <=> (length $a->[0]) |
|
|
943 | } @match; |
942 | @match; |
944 | } |
943 | } |
945 | |
944 | |
946 | $self->{last_search} = $text; |
945 | $self->{last_search} = $text; |
947 | $self->{last_match} = \@match; |
946 | $self->{last_match} = \@match; |
948 | |
947 | |
… | |
… | |
977 | for my $match (@matches) { |
976 | for my $match (@matches) { |
978 | my $label = shift @labels; |
977 | my $label = shift @labels; |
979 | |
978 | |
980 | if (@labels) { |
979 | if (@labels) { |
981 | $label->set_text ("$match->[1]"); |
980 | $label->set_text ("$match->[1]"); |
982 | $label->set_tooltip ("$match->[0][1]"); |
981 | $label->set_tooltip ("#(command/$match->[1])"); |
983 | } else { |
982 | } else { |
984 | $label->set_text ("..."); |
983 | $label->set_text ("..."); |
985 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
984 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
986 | last; |
985 | last; |
987 | } |
986 | } |