Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_defun() can define 13 functions max. #23

Closed
bolangi opened this issue Oct 20, 2023 · 12 comments
Closed

add_defun() can define 13 functions max. #23

bolangi opened this issue Oct 20, 2023 · 12 comments

Comments

@bolangi
Copy link

bolangi commented Oct 20, 2023

The BUGS section of the manual says up to 16 functions may be defined using add_defun(). This test code fails after 13 functions are defined.

use v5.30;
use Term::ReadLine;
my $term = Term::ReadLine->new('custom function test');
my $coderef = sub { say "hello" };
my %escape_code;
read_esc_codes();
my $i = 0;
while ( my ($func_name,$seq) = each %escape_code) {
	$term->add_defun($func_name, $coderef);
	$term->bind_keyseq($seq, $func_name);
	say ++$i;
}


sub read_esc_codes {
%escape_code = qw(

  Escape  	\\e

  Insert  	\\e[2~
  Home  	\\e[1~
  PageUp  	\\e[5~

  Delete  	\\e[3~
  End  		\\e[4~
  PageDown  \\e[6~

  Up  		\\e[A
  Left  	\\e[D
  Down  	\\e[B
  Right  	\\e[C

  F1		\\eOP
  F2		\\eOQ
  F3		\\eOR
  F4		\\eOS
  F5		\\e[15~ 
  F6		\\e[17~ 
  F7		\\e[18~ 
  F8		\\e[19~ 
  F9		\\e[20~ 
  F10		\\e[21~ 
  F11		\\e[23~ 
  F12		\\e[24~ 

  Keypad/	\\eOo
  Keypad*	\\eOj
  Keypad-   \\eOm
  Keypad+   \\eOk
  Keypad7   \\eOw
  Keypad8   \\eOx
  Keypad9   \\eOy
  Keypad4   \\eOt
  Keypad5   \\eOu
  Keypad6   \\eOv
  Keypad1   \\eOq
  Keypad2   \\eOr
  Keypad3   \\eOs
  Keypad0   \\eOp
  Keypad.   \\eOn
  KeypadEnter   \\eOM
  
);
}
@hirooih
Copy link
Owner

hirooih commented Oct 21, 2023

Thank you for your report.

3 functions are already defined by this module.

--- a/Gnu.pm
+++ b/Gnu.pm
@@ -2291,6 +2291,7 @@ GTK+ support in addition to Tk.
 L<the bug tracker on GitHub|https://github.com/hirooih/perl-trg/issues>.
 
 =item C<add_defun()> can define up to 16 functions.
+And some of them are used already.
 
 =item Some functions and variables do not have test code yet.  Your
 contribution is welcome.  See F<t/readline.t> for details.

I have not found a way to eliminate this limitation. It requires to define a function dynamically.

See Line 1638-1681 of Gnu.xs for the current implementation.
If you need to define more functions, increase the size of fntbl[].

@bolangi
Copy link
Author

bolangi commented Oct 21, 2023 via email

@hirooih
Copy link
Owner

hirooih commented Oct 22, 2023

I think the memory costs are acceptable in these years.

The issue is how many functions are enough. This is the first request since 1996 when I released the first version. It seems that it is the time to double the size of the fntbl[].

By the way, do you really need so many add_defun() calls? Your example above needs only one add_defun() call.
As described here a custom function has two arguments. You can use the second argument, $key, to distinguish which key sequence is used. Does this solve your issue?

@bolangi
Copy link
Author

bolangi commented Oct 22, 2023 via email

@hirooih
Copy link
Owner

hirooih commented Oct 22, 2023

Does this solve your issue?

Yes, I could certainly dispatch on the key sequence.

Good!

After some thought, I decide not to change the size of the table this time.

I will add a link to this issue ticket in the bug section of the documentation.
I will change it when I really have a good reason to do it.

Again, thank you for your report.

@bolangi
Copy link
Author

bolangi commented Oct 22, 2023 via email

@bolangi
Copy link
Author

bolangi commented Oct 23, 2023 via email

@hirooih
Copy link
Owner

hirooih commented Oct 23, 2023

Sorry I was wrong.
The second argument $key is an int value. It does not work for a key sequence.
We have to use rl_executing_keyseq variable.

sub hotkey_dispatch {
    say string_to_hex_dump($term->Attribs->{executing_keyseq});
}
sub string_to_hex_dump {
    my ($string) = @_;
    my $hex_dump = '';
    for my $char (split //, $string) {
        $hex_dump .= sprintf("%02X ", ord($char));
    }
    return $hex_dump;
}

@bolangi
Copy link
Author

bolangi commented Oct 24, 2023 via email

@hirooih
Copy link
Owner

hirooih commented Oct 25, 2023

I'm curious that this script doesn't correctly capture many of the key combinations (such as Ctrl-Insert, Shift-F3) that showkey -a displays.

I have never heard of the command showkey before.

On my terminal Ctrl-Insert displays nothing, Shift-F3 displays \e[1;2R (6 characters). There is no such long sequence in your example.

Does this ( rl_set_timeout ) help you?

@bolangi
Copy link
Author

bolangi commented Oct 27, 2023 via email

@hirooih
Copy link
Owner

hirooih commented Oct 28, 2023

Thank you for your assistance.

You are welcome.

I've created PR #24. It will be included in the next release.

Thanks!.

@hirooih hirooih closed this as completed Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants