… | |
… | |
33 | ); |
33 | ); |
34 | |
34 | |
35 | $self |
35 | $self |
36 | } |
36 | } |
37 | |
37 | |
38 | sub add_command { |
38 | sub add_command {#d# remove |
39 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
39 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
40 | |
40 | |
41 | (my $data = $command) =~ s/\\//g; |
41 | (my $data = $command) =~ s/\\//g; |
42 | |
42 | |
43 | $tooltip =~ s/^\s+//; |
43 | $tooltip =~ s/^\s+//; |
44 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
44 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
45 | $tooltip =~ s/\s+$//; |
45 | $tooltip =~ s/\s+$//; |
46 | |
46 | |
47 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
47 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
48 | } |
|
|
49 | |
|
|
50 | sub clr_commands { |
|
|
51 | my ($self) = @_; |
|
|
52 | |
|
|
53 | %{$::COMPLETER->{command}} = (); |
|
|
54 | |
|
|
55 | $::COMPLETER->hide |
|
|
56 | if $::COMPLETER; |
|
|
57 | } |
48 | } |
58 | |
49 | |
59 | sub server_login { |
50 | sub server_login { |
60 | my ($server) = @_; |
51 | my ($server) = @_; |
61 | |
52 | |
… | |
… | |
818 | ); |
809 | ); |
819 | |
810 | |
820 | $self |
811 | $self |
821 | } |
812 | } |
822 | |
813 | |
|
|
814 | sub reset { |
|
|
815 | my ($self) = @_; |
|
|
816 | |
|
|
817 | $self->hide; |
|
|
818 | delete $self->{command_list}; |
|
|
819 | } |
|
|
820 | |
823 | sub set_prefix { |
821 | sub set_prefix { |
824 | my ($self, $prefix) = @_; |
822 | my ($self, $prefix) = @_; |
825 | |
823 | |
826 | $self->{entry}->set_text ($prefix); |
824 | $self->{entry}->set_text ($prefix); |
827 | $self->show; |
825 | $self->show; |
… | |
… | |
863 | |
861 | |
864 | my $text = $self->{entry}->get_text; |
862 | my $text = $self->{entry}->get_text; |
865 | |
863 | |
866 | length $text |
864 | length $text |
867 | or return $self->hide; |
865 | or return $self->hide; |
|
|
866 | |
|
|
867 | return unless $::CONN; |
|
|
868 | |
|
|
869 | # regenerate spell list if necessary |
|
|
870 | $self->{command_list}{spells} ||= [ |
|
|
871 | map { ("cast $_->{name}", "invoke $_->{name}") } |
|
|
872 | values %{ $::CONN->{spell} } |
|
|
873 | ]; |
868 | |
874 | |
869 | if ($text ne $self->{last_search}) { |
875 | if ($text ne $self->{last_search}) { |
870 | my @match; |
876 | my @match; |
871 | |
877 | |
872 | if ($text =~ /^(.*?)\s+$/) { |
878 | if ($text =~ /^(.*?)\s+$/) { |
… | |
… | |
894 | my $regexp_partial = do { |
900 | my $regexp_partial = do { |
895 | my $regexp = "^\Q$text\E(.*)"; |
901 | my $regexp = "^\Q$text\E(.*)"; |
896 | qr<$regexp> |
902 | qr<$regexp> |
897 | }; |
903 | }; |
898 | |
904 | |
899 | for (keys %{$self->{command}}) { |
905 | for my $list (values %{ $self->{command_list} }) { |
|
|
906 | for (@$list) { |
900 | # we only match and score if the first character matches, |
907 | # we only match and score if the first character matches, |
901 | # so quickly rule out all others first. |
908 | # so quickly rule out all others first. |
902 | next unless $first_char = substr $_, 0, 1; |
909 | next unless $first_char = substr $_, 0, 1; |
903 | |
910 | |
904 | my @scores; |
911 | my @scores; |
905 | |
912 | |
906 | # 1. Complete command [with args] |
913 | # 1. Complete command [with args] |
907 | # command is a prefix of the text |
914 | # command is a prefix of the text |
908 | # score is length of complete command matched |
915 | # score is length of complete command matched |
909 | # e.g. "invoke summon pet monster bat" |
916 | # e.g. "invoke summon pet monster bat" |
910 | # "invoke" "summon pet monster bat" = 6 |
917 | # "invoke" "summon pet monster bat" = 6 |
911 | # "invoke summon pet monster" "bat" = 25 |
918 | # "invoke summon pet monster" "bat" = 25 |
912 | if ((substr $text, 0, length $_) eq $_) { |
919 | if ((substr $text, 0, length $_) eq $_) { |
913 | push @scores, [$_, length $_, $text]; |
920 | push @scores, [$_, length $_, $text]; |
|
|
921 | } |
|
|
922 | |
|
|
923 | # 2. Partial command |
|
|
924 | # text is a prefix of the full command |
|
|
925 | # score is the length of the input text |
|
|
926 | # e.g. "invoke s" |
|
|
927 | # "invoke small fireball" = 8 |
|
|
928 | # "invoke summon pet monster" = 8 |
|
|
929 | |
|
|
930 | if ($_ =~ $regexp_partial) { |
|
|
931 | push @scores, [$_, length $text, $_]; |
|
|
932 | } |
|
|
933 | |
|
|
934 | # 3. Abbreviation match |
|
|
935 | # attempts to use first word of text as an abbreviated command |
|
|
936 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
937 | |
|
|
938 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
939 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
940 | } |
|
|
941 | |
|
|
942 | # Pick the best option for this command |
|
|
943 | push @match, (sort { |
|
|
944 | $b->[1] <=> $a->[1] |
|
|
945 | } @scores)[0]; |
914 | } |
946 | } |
915 | |
|
|
916 | # 2. Partial command |
|
|
917 | # text is a prefix of the full command |
|
|
918 | # score is the length of the input text |
|
|
919 | # e.g. "invoke s" |
|
|
920 | # "invoke small fireball" = 8 |
|
|
921 | # "invoke summon pet monster" = 8 |
|
|
922 | |
|
|
923 | if ($_ =~ $regexp_partial) { |
|
|
924 | push @scores, [$_, length $text, $_]; |
|
|
925 | } |
|
|
926 | |
|
|
927 | # 3. Abbreviation match |
|
|
928 | # attempts to use first word of text as an abbreviated command |
|
|
929 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
930 | |
|
|
931 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
932 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
933 | } |
|
|
934 | |
|
|
935 | # Pick the best option for this command |
|
|
936 | push @match, (sort { |
|
|
937 | $b->[1] <=> $a->[1] |
|
|
938 | } @scores)[0]; |
|
|
939 | } |
947 | } |
940 | |
948 | |
941 | # @match is now [command object, command with arguments] |
949 | # @match is now [command object, command with arguments] |
942 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
950 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
943 | sort { |
951 | sort { |