+#if ENABLE_FEATURE_LESS_REWRAP
+static void re_wrap(void)
+{
+ int w = width;
+ int rem;
+ int src_idx;
+ int dst_idx;
+ int new_cur_fline = 0;
+ uint32_t lineno;
+ char linebuf[w + 1];
+ const char **old_flines = flines;
+ const char *s;
+ char **new_flines = NULL;
+ char *d;
+
+ if (option_mask32 & FLAG_N)
+ w -= 8;
+
+ src_idx = 0;
+ dst_idx = 0;
+ s = old_flines[0];
+ lineno = LINENO(s);
+ d = linebuf;
+ rem = w;
+ while (1) {
+ *d = *s;
+ if (*d != '\0') {
+ s++;
+ d++;
+ rem--;
+ if (rem == 0) {
+ int sz;
+ /* new line is full, create next one */
+ *d = '\0';
+ next_new:
+ sz = (d - linebuf) + 1; /* + 1: NUL */
+ d = ((char*)xmalloc(sz + 4)) + 4;
+ LINENO(d) = lineno;
+ memcpy(d, linebuf, sz);
+ new_flines = xrealloc_vector(new_flines, 8, dst_idx);
+ new_flines[dst_idx] = d;
+ dst_idx++;
+ if (rem) {
+ /* did we come here thru "goto next_new"? */
+ if (src_idx > max_fline)
+ break;
+ lineno = LINENO(s);
+ }
+ d = linebuf;
+ rem = w;
+ }
+ continue;
+ }
+ /* *d == NUL: old line ended, go to next old one */
+ free(MEMPTR(old_flines[src_idx]));
+ /* btw, convert cur_fline... */
+ if (cur_fline == src_idx) {
+ new_cur_fline = dst_idx;
+ }
+ src_idx++;
+ /* no more lines? finish last new line (and exit the loop) */
+ if (src_idx > max_fline) {
+ goto next_new;
+ }
+ s = old_flines[src_idx];
+ if (lineno != LINENO(s)) {
+ /* this is not a continuation line!
+ * create next _new_ line too */
+ goto next_new;
+ }
+ }
+
+ free(old_flines);
+ flines = (const char **)new_flines;
+
+ max_fline = dst_idx - 1;
+ linepos = 0; // XXX
+ cur_fline = new_cur_fline;
+ /* max_lineno is screen-size independent */
+}
+#endif
+