/* str_grow guarantees that the Str buf has n free bytes * returns a pointer to the new Str */ Str * str_grow(Str *s, const int n) { Str *new_s; if (str_avail(s) >= n) return s; new_s = lrealloc(s, offsetof(Str, data) + s->cap + n + 1); new_s->cap =+ n; new_s->data[new_s->cap] = '\0'; return new_s; }
/* * Insert a character. While defining a macro, create a "LINE" containing * all inserted characters. */ int selfinsert(int f, int n) { struct line *lp; int c; int count; if (n < 0) return (FALSE); if (n == 0) return (TRUE); c = key.k_chars[key.k_count - 1]; if (macrodef && macrocount < MAXMACRO) { if (f & FFARG) macrocount -= 2; /* last command was insert -- tack on the end */ if (lastflag & CFINS) { macrocount--; /* Ensure the line can handle the new characters */ if (maclcur->l_size < maclcur->l_used + n) { if (lrealloc(maclcur, maclcur->l_used + n) == FALSE) return (FALSE); } maclcur->l_used += n; /* Copy in the new data */ for (count = maclcur->l_used - n; count < maclcur->l_used; count++) maclcur->l_text[count] = c; } else { macro[macrocount - 1].m_funct = insert; if ((lp = lalloc(n)) == NULL) return (FALSE); lp->l_bp = maclcur; lp->l_fp = maclcur->l_fp; maclcur->l_fp = lp; maclcur = lp; for (count = 0; count < n; count++) lp->l_text[count] = c; } thisflag |= CFINS; } if (c == '\n') { do { count = lnewline(); } while (--n && count == TRUE); return (count); } /* overwrite mode */ if (curbp->b_flag & BFOVERWRITE) { lchange(WFEDIT); while (curwp->w_doto < llength(curwp->w_dotp) && n--) lputc(curwp->w_dotp, curwp->w_doto++, c); if (n <= 0) return (TRUE); } return (linsert(n, c)); }