Below is one working solution that determines whether your cursor is in the “whole” or “fractional” part of a Euro price string (matching the pattern %d+%.%d%d€) and adjusts the number accordingly. In this example, if the cursor is within the whole‑number part it increments/decrements the whole euros (and resets cents to 0), while if the cursor is in the fractional part it only affects the cents. It even recalculates the new cursor position relative to the part that was edited.
Note: In dial.nvim the text passed to your add function is just the matched string (for example, "4.99€"), and the cursor is a number in the range 1 .. #text (so the first digit is at position 1). This example uses that fact.
local euro_price_augend = require("dial.augend").user.new({
-- A custom finder: search for a price matching (<digits>).(<2 digits>)€
find = function(text, pos)
local pattern = "(%d+)%.(%d%d)€"
local s, e = text:find(pattern, pos)
if s then
-- Return start, end, and text of the match.
return { s = s, e = e, text = text:sub(s, e) }
else
return nil
end
end,
add = function(text, addend, cursor)
-- Extract the euro and cents parts from the matched text.
local whole_str, frac_str = text:match("^(%d+)%.(%d%d)€$")
if not whole_str or not frac_str then
return nil
end
local whole = tonumber(whole_str)
local frac = tonumber(frac_str)
local orig_whole_length = #whole_str
local new_cursor = cursor -- default
-- Decide what to change based on the cursor position.
-- (We assume the dot is at position (orig_whole_length + 1) in "4.99€")
if cursor <= orig_whole_length then
-- Cursor is in the whole (euros) part.
whole = whole + addend
if whole < 0 then
whole = 0
end
-- Reset cents
frac = 0
-- Now determine the new cursor position:
-- If the number of digits in the updated whole value changes, update the cursor accordingly.
local new_whole_str = tostring(whole)
new_cursor = math.min(cursor, #new_whole_str)
else
-- Cursor is in the fractional part.
-- Here we decide that any position after the whole part (including the dot) is for the cents.
frac = frac + addend
if frac >= 100 then
frac = frac % 100
elseif frac < 0 then
frac = (frac + 100) % 100
end
-- Determine offset inside the cents relative to the current pattern.
-- The dot is at position (orig_whole_length + 1) so let’s subtract that out.
local offset_in_frac = cursor - (orig_whole_length + 1)
-- For a fixed cents value, we expect two digits.
offset_in_frac = math.min(offset_in_frac, 1) -- clamp to 0 or 1
-- In the new text the whole part may have a different width:
local new_whole_str = tostring(whole)
new_cursor = (#new_whole_str) + 1 + offset_in_frac
end
local new_text = string.format("%d.%02d€", whole, frac)
return { text = new_text, cursor = new_cursor }
end,
})
-- Now register the new augend in your augends group. For example:
require("dial.config").augends:register_group {
default = {
euro_price_augend,
-- other augends if any...
},
}