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