diff options
| author | Teddy Wing | 2021-08-21 16:41:51 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-08-21 18:03:04 +0200 |
| commit | 6676e8367f81393930cd6ab900d1d0cea35e0173 (patch) | |
| tree | f89695981b04171e843c7d065fb9c63e1c188120 /incdec | |
| parent | efdc8d92d0a4bd3c6acd82b6736de4e73e9af6f0 (diff) | |
| download | readline-incdec-6676e8367f81393930cd6ab900d1d0cea35e0173.tar.bz2 | |
incdec: Get increment working in a Readline binding
Copy-pasted the `incdec` subroutine from `incdec.pm` into the Bash
function. Eventually I'll have a Make rule for this.
Add some new Perl code to the `perl -e` call that calls the `incdec()`
subroutine with arguments from the shell environment. Set the current
Readline line to the result of `incdec()` to increment a number.
I was getting an error "bash_execute_unix_command: cannot find keymap
for command" when I tried to assign the binding to "\C-xa". Not sure
what's going on there but assigned it to a different one in the
meantime.
Diffstat (limited to 'incdec')
| -rw-r--r-- | incdec | 70 |
1 files changed, 69 insertions, 1 deletions
@@ -34,7 +34,7 @@ function __readline_incdec_incdec2 { # perl # } -bind -x '"\C-xa+":incdec' +# bind -x '"\C-xa+":incdec' # bind '"\C-xa+": "\C-e$(incdec)\e\C-e"' # bind '"\C-xa+": "\C-e`incdec`"' # bind '"\C-xaa":\C-xa+' @@ -49,6 +49,74 @@ bind -x '"\C-xasrp": __readline_incdec_save_readline_point' # bind '"\C-xaa": \C-xasrp' # bind '"\C-xaa": __readline_incdec_incdec2' +function __readline_incdec { + local increment_by="$1" + local backward="$2" + + # local incdec = + # print "$line, ${"increment-by"}, ${"point-position"}, $backward"; + + line=$(perl -s -e ' +sub incdec { + my ($line, $increment_by, $point_position, $is_backward) = @_; + + $point_position ||= 0; + $is_backward ||= 0; + + my $start_position = 0; + my $previous_match_start = 0; + while ($line =~ /(\d+)/g) { + if ($is_backward) { + if ($point_position < $-[0]) { + $start_position = $previous_match_start; + + last; + } + + $previous_match_start = $-[0]; + + } + else { + if ($point_position < $+[0]) { + $start_position = $-[0]; + + last; + } + } + } + + if ($is_backward && $point_position == length $line) { + $start_position = $previous_match_start; + } + + pos($line) = $start_position; + $line =~ s/\G([^\d]*)(\d+)/$1 . ($2 + $increment_by)/e; + + return $line; +} + + # print "$line, ${\"increment-by\"}, ${\"point-position\"}, $backward"; + my $output = incdec($line, ${"increment-by"}, ${"point-position"}, $backward); + print $output; +' \ + -- \ + -line="$READLINE_LINE" \ + -increment-by="$increment_by" \ + -point-position="$READLINE_POINT" \ + -backward="$backward" + ) + + # echo "$line" + + READLINE_LINE="$line" +} + +function __readline_incdec_increment { + __readline_incdec 1 1 +} + +bind -x '"\C-xa+": __readline_incdec_increment' + # 2021.01.15: Idea: Maybe try using $EDITOR |
