- /* We will delete too many columns. Let's fill columns
- by spaces so that the remaining text won't move. */
- EMACS_INT actual = PT_BYTE;
- DEC_POS (actual);
- if (FETCH_CHAR (actual) == '\t')
- /* Rather than add spaces, let's just keep the tab. */
- chars_to_delete--;
- else
- spaces_to_insert = actual_clm - target_clm;
+ /* Column the cursor should be placed at after this insertion.
+ The value should be calculated only when necessary. */
+ ptrdiff_t target_clm = curcol + n * cwidth;
+
+ /* The actual cursor position after the trial of moving
+ to column TARGET_CLM. It is greater than TARGET_CLM
+ if the TARGET_CLM is middle of multi-column
+ character. In that case, the new point is set after
+ that character. */
+ ptrdiff_t actual_clm
+ = XFASTINT (Fmove_to_column (make_number (target_clm), Qnil));
+
+ chars_to_delete = PT - pos;
+
+ if (actual_clm > target_clm)
+ {
+ /* We will delete too many columns. Let's fill columns
+ by spaces so that the remaining text won't move. */
+ ptrdiff_t actual = PT_BYTE;
+ DEC_POS (actual);
+ if (FETCH_CHAR (actual) == '\t')
+ /* Rather than add spaces, let's just keep the tab. */
+ chars_to_delete--;
+ else
+ spaces_to_insert = actual_clm - target_clm;
+ }
+
+ SET_PT_BOTH (pos, pos_byte);