… | |
… | |
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+$/) { |
… | |
… | |
875 | } else { |
881 | } else { |
876 | # @match is [command, penalty, command with arguments] until sort |
882 | # @match is [command, penalty, command with arguments] until sort |
877 | |
883 | |
878 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
884 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
879 | |
885 | |
|
|
886 | my $first_char = substr $cmd, 0, 1; |
|
|
887 | |
880 | my $regexp_abbrev = do { |
888 | my $regexp_abbrev = do { |
881 | my ($beg, @chr) = split //, lc $cmd; |
889 | my ($beg, @chr) = split //, lc $cmd; |
882 | |
890 | |
883 | # the following regex is used to match our "completion entry" |
891 | # the following regex is used to match our "completion entry" |
884 | # to an actual command - the parentheses match kind of "overhead" |
892 | # to an actual command - the parentheses match kind of "overhead" |
885 | # - the more characters the parentheses match, the less attractive |
893 | # - the more characters the parentheses match, the less attractive |
886 | # is the match. |
894 | # is the match. |
887 | my $regexp = "^\Q$beg\E" |
895 | my $regexp = "^\Q$beg\E" |
888 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
896 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
889 | qr<$regexp> |
897 | qr<$regexp> |
890 | }; |
898 | }; |
891 | |
899 | |
892 | my $regexp_partial = do { |
900 | my $regexp_partial = do { |
893 | my $regexp = "^\Q$text\E(.*)"; |
901 | my $regexp = "^\Q$text\E(.*)"; |
894 | qr<$regexp> |
902 | qr<$regexp> |
895 | }; |
903 | }; |
896 | |
904 | |
897 | for (keys %{$self->{command}}) { |
905 | for my $list (values %{ $self->{command_list} }) { |
|
|
906 | for (@$list) { |
|
|
907 | # we only match and score if the first character matches, |
|
|
908 | # so quickly rule out all others first. |
|
|
909 | next unless $first_char = substr $_, 0, 1; |
|
|
910 | |
898 | my @scores; |
911 | my @scores; |
899 | |
912 | |
900 | # 1. Complete command [with args] |
913 | # 1. Complete command [with args] |
901 | # command is a prefix of the text |
914 | # command is a prefix of the text |
902 | # score is length of complete command matched |
915 | # score is length of complete command matched |
903 | # e.g. "invoke summon pet monster bat" |
916 | # e.g. "invoke summon pet monster bat" |
904 | # "invoke" "summon pet monster bat" = 6 |
917 | # "invoke" "summon pet monster bat" = 6 |
905 | # "invoke summon pet monster" "bat" = 25 |
918 | # "invoke summon pet monster" "bat" = 25 |
906 | if ($text =~ /^\Q$_\E(.*)/) { |
919 | if ((substr $text, 0, length $_) eq $_) { |
907 | 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]; |
908 | } |
946 | } |
909 | |
|
|
910 | # 2. Partial command |
|
|
911 | # text is a prefix of the full command |
|
|
912 | # score is the length of the input text |
|
|
913 | # e.g. "invoke s" |
|
|
914 | # "invoke small fireball" = 8 |
|
|
915 | # "invoke summon pet monster" = 8 |
|
|
916 | |
|
|
917 | if ($_ =~ $regexp_partial) { |
|
|
918 | push @scores, [$_, length $text, $_]; |
|
|
919 | } |
|
|
920 | |
|
|
921 | # 3. Abbreviation match |
|
|
922 | # attempts to use first word of text as an abbreviated command |
|
|
923 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
924 | |
|
|
925 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
926 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
927 | } |
|
|
928 | |
|
|
929 | # Pick the best option for this command |
|
|
930 | push @match, (sort { |
|
|
931 | $b->[1] <=> $a->[1] |
|
|
932 | } @scores)[0]; |
|
|
933 | } |
947 | } |
934 | |
948 | |
935 | # @match is now [command object, command with arguments] |
949 | # @match is now [command object, command with arguments] |
936 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
950 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
937 | sort { |
951 | sort { |