aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2021-08-28 22:07:19 +0200
committerTeddy Wing2021-08-28 22:07:19 +0200
commitc8fa19d038ed79c640c5164735179611e95520d6 (patch)
treead987741fd3fdd66342992cf6140f699a7a7b17d
parent729cdbe8c41598aaa70c7c71d708953dd72a6866 (diff)
downloadreadline-incdec-c8fa19d038ed79c640c5164735179611e95520d6.tar.bz2
incdec.bash: Set up point moving after start position
This doesn't work yet as it turns out it requires a change to the `incdec()` Perl subroutine. We want to move point when switching between positive and negative numbers (adding or removing a hypen negative sign), but only if point is on or after the start of the number being incremented. If point is before that, it looks like it's moving. Read the start index from the `incdec()` subroutine and prefix it to the output line in the format: "${start_position}#${output_line}". We can then use the "#" to split the two values and extract them in the Bash functions. Needed to move setting `$READLINE_LINE` to the increment and decrement Bash functions because I now run `__readline_incdec` in a subshell, meaning the current line can't be manipulated with that variable in that function. In the increment and decrement Bash functions, we now check if the start position of the incremented number is less than or equal to `$READLINE_POINT` before trying to move point.
-rw-r--r--incdec.bash37
-rw-r--r--incdec.m4.bash34
-rw-r--r--incdec.m4.pl2
3 files changed, 57 insertions, 16 deletions
diff --git a/incdec.bash b/incdec.bash
index 029d4ea..7144282 100644
--- a/incdec.bash
+++ b/incdec.bash
@@ -23,7 +23,7 @@ function __readline_incdec {
local increment_by="$1"
local backward="$2"
- line=$(perl -s -e '
+ result="$(perl -s -e '
sub incdec {
my ($line, $increment_by, $point_position, $is_backward) = @_;
@@ -76,28 +76,38 @@ my ($output, $start_position) = incdec(
$point_position,
$backward
);
-print $output;
+print "$start_position\#$output";
' \
-- \
-line="$READLINE_LINE" \
-increment_by="$increment_by" \
-point_position="$READLINE_POINT" \
-backward="$backward"
- )
+ )"
- READLINE_LINE="$line"
+ echo "$result"
}
# Increment the nearest number to the left of point by 1.
function __readline_incdec_increment {
local old_line_length="${#READLINE_LINE}"
- __readline_incdec 1 1
+ result="$(__readline_incdec 1 1)"
+
+ line="${result#*#}"
+ READLINE_LINE="$line"
local new_line_length="${#READLINE_LINE}"
+ local start_position="${result%#*}"
+ echo "$start_position .. $READLINE_POINT"
+
# If a negative sign was removed, keep point where it was.
- if [ "$old_line_length" -gt "$new_line_length" ]; then
+ if [ \
+ "$old_line_length" -gt "$new_line_length" \
+ -a \
+ "$start_position" -le "$READLINE_POINT" \
+ ]; then
READLINE_POINT="$(($READLINE_POINT - 1))"
fi
}
@@ -106,12 +116,23 @@ function __readline_incdec_increment {
function __readline_incdec_decrement {
local old_line_length="${#READLINE_LINE}"
- __readline_incdec -1 1
+ result="$(__readline_incdec -1 1)"
+
+ line="${result#*#}"
+ READLINE_LINE="$line"
local new_line_length="${#READLINE_LINE}"
+ local start_position="${result%#*}"
+ echo "$start_position .. $READLINE_POINT"
+
+ # TODO: Point should only move when it's over the number. No, actually when it's >= the start of the number.
# If a negative sign was added, keep point where it was.
- if [ "$old_line_length" -lt "$new_line_length" ]; then
+ if [ \
+ "$old_line_length" -lt "$new_line_length" \
+ -a \
+ "$start_position" -le "$READLINE_POINT" \
+ ]; then
READLINE_POINT="$(($READLINE_POINT + 1))"
fi
}
diff --git a/incdec.m4.bash b/incdec.m4.bash
index ff6bb57..61e7f47 100644
--- a/incdec.m4.bash
+++ b/incdec.m4.bash
@@ -23,7 +23,7 @@ function __readline_incdec {
local increment_by="$1"
local backward="$2"
- line=$(perl -s -e '
+ result="$(perl -s -e '
INCLUDE_INCDEC_PL
' \
-- \
@@ -31,21 +31,31 @@ INCLUDE_INCDEC_PL
-increment_by="$increment_by" \
-point_position="$READLINE_POINT" \
-backward="$backward"
- )
+ )"
- READLINE_LINE="$line"
+ echo "$result"
}
# Increment the nearest number to the left of point by 1.
function __readline_incdec_increment {
local old_line_length="${#READLINE_LINE}"
- __readline_incdec 1 1
+ result="$(__readline_incdec 1 1)"
+
+ line="${result#*#}"
+ READLINE_LINE="$line"
local new_line_length="${#READLINE_LINE}"
+ local start_position="${result%#*}"
+ echo "$start_position .. $READLINE_POINT"
+
# If a negative sign was removed, keep point where it was.
- if [ "$old_line_length" -gt "$new_line_length" ]; then
+ if [ \
+ "$old_line_length" -gt "$new_line_length" \
+ -a \
+ "$start_position" -le "$READLINE_POINT" \
+ ]; then
READLINE_POINT="$(($READLINE_POINT - 1))"
fi
}
@@ -54,13 +64,23 @@ function __readline_incdec_increment {
function __readline_incdec_decrement {
local old_line_length="${#READLINE_LINE}"
- __readline_incdec -1 1
+ result="$(__readline_incdec -1 1)"
+
+ line="${result#*#}"
+ READLINE_LINE="$line"
local new_line_length="${#READLINE_LINE}"
+ local start_position="${result%#*}"
+ echo "$start_position .. $READLINE_POINT"
+
# TODO: Point should only move when it's over the number. No, actually when it's >= the start of the number.
# If a negative sign was added, keep point where it was.
- if [ "$old_line_length" -lt "$new_line_length" ]; then
+ if [ \
+ "$old_line_length" -lt "$new_line_length" \
+ -a \
+ "$start_position" -le "$READLINE_POINT" \
+ ]; then
READLINE_POINT="$(($READLINE_POINT + 1))"
fi
}
diff --git a/incdec.m4.pl b/incdec.m4.pl
index b5d3c72..cd31dc5 100644
--- a/incdec.m4.pl
+++ b/incdec.m4.pl
@@ -6,4 +6,4 @@ my ($output, $start_position) = incdec(
$point_position,
$backward
);
-print $output;
+print "$start_position\#$output";