- struct ascii_line *line = &a->lines[y];
-
- if (x0 >= line->width)
- {
- /* The common case: adding new characters at the end of a line. */
- ds_put_byte_multiple (&line->s, ' ', x0 - line->width);
- line->width = x1;
- return ds_put_uninit (&line->s, n);
- }
- else if (x0 == x1)
- return NULL;
- else
- {
- /* An unusual case: overwriting characters in the middle of a line. We
- don't keep any kind of mapping from bytes to display positions, so we
- have to iterate over the whole line starting from the beginning. */
- struct ascii_pos p0, p1;
- char *s;
-
- /* Find the positions of the first and last character. We must find the
- both characters' positions before changing the line, because that
- would prevent finding the other character's position. */
- find_ascii_pos (line, x0, &p0);
- if (x1 < line->width)
- find_ascii_pos (line, x1, &p1);
-
- /* If a double-width character occupies both x0 - 1 and x0, then replace
- its first character width by '?'. */
- s = ds_data (&line->s);
- while (p0.x0 < x0)
- {
- s[p0.ofs0++] = '?';
- p0.x0++;
- }
-
- if (x1 >= line->width)
- {
- ds_truncate (&line->s, p0.ofs0);
- line->width = x1;
- return ds_put_uninit (&line->s, n);
- }
-
- /* If a double-width character occupies both x1 - 1 and x1, then we need
- to replace its second character width by '?'. */
- if (p1.x0 < x1)
- {
- do
- {
- s[--p1.ofs1] = '?';
- p1.x0++;
- }
- while (p1.x0 < x1);
- return ds_splice_uninit (&line->s, p0.ofs0, p1.ofs1 - p0.ofs0, n);
- }
-
- return ds_splice_uninit (&line->s, p0.ofs0, p1.ofs0 - p0.ofs0, n);
- }