File: screen.c

package info (click to toggle)
utalk 1.0.1.beta-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 340 kB
  • ctags: 667
  • sloc: ansic: 5,581; makefile: 68; sh: 1
file content (640 lines) | stat: -rw-r--r-- 19,124 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* utalk, a UDP-based "talk" replacement, using srdp
   by Roger Espel Llima    <roger.espel.llima@pobox.com>

   screen.c

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation. See the file LICENSE for details.
*/

#include <string.h>
#include <stdlib.h>
#include "globals.h"
#include "termcap.h"
#include "util.h"
#include "srdpdata.h"
#include "struct.h"
#include "functions.h"
#include "screen.h"
#include "menu.h"

static char ascii_trans[] = " !cLxY|$'ca<--R_o+23'mP.,1o>123?AAAAAAACEEEEIIIIDNOOOOO*0UUUUYPBaaaaaaaceeeeiiiidnooooo/0uuuuypy";

int active_buffers = 2;

/* all absolute coordinates assume left upper screen = 1,1 */
/* all lines are numbered from 1 */

static char statusbuf[300];

static char *make_left(struct user *u) {
  static char left[12];

  if (u == users[0])
    strcpy(left, "[m]");
  else if ((u->info.flags&SRDP_FL_RECVD) == 0)
    strcpy(left, "[n]");
  else if ((u->info.flags&SRDP_FL_BROKEN) != 0)
    strcpy(left, "[b]");
  else
    strcpy(left, "[c]");
  if (u == users[0] && active_window == -1)
    strcat(left, "[*]");
/*  else if (u == 0 && active_window == 0)
    strcat(left, "[R]"); */
  else if (u == users[active_window])
    strcat(left, "[R]");
  else
    strcat(left, "---");
  return left;
}

void doflags(struct user *u) {
/* recalculates the flags for window u, and redisplays them if they
   have changed */
  char *left;
  int len, x = xcursor, y = ycursor;

  if (active_window == u->last_active &&
      u->info.flags == u->last_flags)
    return;
  u->last_active = active_window;
  u->last_flags = u->info.flags;
  left = make_left(u);
  if (strcmp(u->lefts, left) != 0) {
    strcpy(u->lefts, left);
    len = strlen(left);
    if (len>cols-2) len = cols-2;
    left[len] = '-';
    left[len+1] = 0;
    m_gotoxy(2, u->ystatus);
    m_putscreen(left, len+1, 0);
    if (x >= 0 && y >= 0) m_gotoxy(x, y);
  }
}

void setstatus(struct user *u, char *s) {
/* sets the status line, or just redisplays it if fed a NULL; always
   recalculates the flags on the left */

  int len, llen, x = xcursor, y = ycursor;
  char *left;

  left = make_left(u);
  llen = strlen(left);
  if (llen>cols-2) llen = cols-2;
  strcpy(u->lefts, left);
  if (s != NULL)
    strncpy(u->statuslines, s, 290);
  memset(statusbuf, '-', cols);
  len = strlen(u->statuslines)+2;
  if (len > cols-4-llen) len = cols-4-llen;
  if (((cols-len)>>1) >= llen+3) {
    statusbuf[((cols-len)>>1)] = statusbuf[((cols+len-2)>>1)] = ' ';
    memcpy(statusbuf+((cols+2-len)>>1), u->statuslines, len-2);
  } else {
    statusbuf[llen+3] = statusbuf[llen+len+2] = ' ';
    memcpy(statusbuf+llen+4, u->statuslines, len-2);
  }
  memcpy(statusbuf+1, left, llen);
  statusbuf[llen+1] = '-';
  m_gotoxy(1, u->ystatus);
  m_putscreen(statusbuf, cols, 0);
  if (x >= 0 && y >= 0) m_gotoxy(x, y);
}

void settopic(unsigned char *s) {
  setstatus(users[0], s);
}

static void add_pline(struct user *u, int idx, struct logical_line *lline,
		      int offset, int length) {
/* inserts an empty pline with number idx, pointing at lline, offset, length;
   we assume 1 <= idx <= (nplines+1) */
  int c;

  if (u->nplines >= u->p_arr_length) {
    u->p_arr_length += PLINE_CHUNK;                               
    u->plines = (struct physical_line *)myrealloc(u->plines,
	       (u->p_arr_length+8)*sizeof(struct physical_line));
  }
  for (c = u->nplines; c >= idx; c--)
    u->plines[c+1] = u->plines[c];
  u->nplines++;
  u->plines[idx].line = lline;
  u->plines[idx].offset = offset;
  u->plines[idx].length = length;
}

static struct logical_line *add_empty_lline(struct user *u, srdp_u32 seq) {
/* add a lline at the end of the buffer, and make a pline point to it */

  struct logical_line *l;

  l = (struct logical_line *)mymalloc(sizeof (struct logical_line));
  if (u->last_lline != NULL)
    l->number = 1+u->last_lline->number;
  else
    l->number = 1;
  l->length = 0;
  l->arr_length = LINE_CHUNK;
  l->chars = mymalloc(LINE_CHUNK+8);
  l->seqs = (srdp_u32 *)mymalloc((LINE_CHUNK+8)*sizeof(srdp_u32));
  l->len_seq = seq;
  l->any_deleted = 0;
  l->next = NULL;
  l->prev = u->last_lline;
  if (l->prev != NULL) l->prev->next = l;
  u->last_lline = l;
  if (u->first_lline == NULL) u->first_lline = l;
  add_pline(u, u->nplines+1, l, 1, 0);
  return l;
}

void init_screen(void) {
  int i;
  struct user *u;

  users[0] = mymalloc(sizeof (struct user));
  users[1] = mymalloc(sizeof (struct user));
  users[0]->winsize = (lines-2)>>1;
  users[1]->winsize = (lines-3)>>1;
  users[0]->ystatus = 1;
  users[1]->ystatus = 2+users[0]->winsize;
  users[0]->yfirst = 2;
  users[1]->yfirst = 1+users[1]->ystatus;
  users[0]->last_active = users[1]->last_active = -2;
  setstatus(users[0], "utalk version " UTALK_VERSION);
  setstatus(users[1], "not connected");
  m_gotoxy(1, 2);
  flush_term();

  for (i = 0; i<active_buffers; ++i) {
    u = users[i];
    u->first_lline = u->last_lline = NULL;
    u->p_arr_length = PLINE_CHUNK;
    u->plines = (struct physical_line *)mymalloc((u->p_arr_length+8)*
	    sizeof(struct physical_line));
    u->nplines = 0;
    add_empty_lline(u, 0);
    u->first_visible_pline = 1;
  }
  current_lline = users[0]->first_lline;
}

static void printchar(unsigned char c) {
  if (c == 0) {
  } else if (c == 0xff) {
    c = ' ';
    m_putscreen((char *)&c, 1, 0);
  } else if (c<' ') {
    setinv();
    c += '@';
    m_putscreen((char *)&c, 1, ATTR_INVERSE);
    normal();
  } else if (c >= 0x7f && c<0xa0) {
    c += '@';
    c &= 0x7f;
    setinv();
    m_putscreen((char *)&c, 1, ATTR_INVERSE);
    normal();
  } else if (c >= 0xa0) {
    if (!eight_bit_clean)
      c = ascii_trans[c-0xa0];
    m_putscreen((char *)&c, 1, 0);
  } else m_putscreen((char *)&c, 1, 0);
}

static void draw_line_from_plines(struct user *u, int pl) {
/* pl is a pline number; assumes pl is a visible line, but it can
   be pl>u->nplines */

  struct logical_line *l;
  int idx, max;

  m_gotoxy(1, u->yfirst+pl-u->first_visible_pline);
  if (pl>u->nplines)
    m_cleareol();
  else {
    l = u->plines[pl].line;
    idx = u->plines[pl].offset;
    max = l->length;
    while (idx <= max && xcursor>0 && xcursor <= cols)
      printchar((unsigned char)l->chars[idx++]);
    if (xcursor >= 0) m_cleareol();
  }
}

static void draw_end_of_line_from_plines(struct user *u, int pl, int idx,
					 int x0) {
  struct logical_line *l;
  int max;

  m_gotoxy(x0, u->yfirst+pl-u->first_visible_pline);
  l = u->plines[pl].line;
  max = l->length;
  while (idx <= max && xcursor>0 && xcursor <= cols)
    printchar((unsigned char)l->chars[idx++]);
  if (xcursor >= 0) m_cleareol();
}

static void redraw_from_plines(struct user *u) {
  int pl;

  for (pl = u->first_visible_pline; pl<u->first_visible_pline+u->winsize; ++pl)
    draw_line_from_plines(u, pl);
}

void scroll_to_pline(struct user *u, int pl) {
#ifdef DEBUG
  fprintf(stderr, "scroll to pline %u %u\r\n", u, pl);  /* DEBUG */
#endif /* DEBUG */
  if (pl == u->first_visible_pline) {
    /* nothing! */
  } else if (pl == u->first_visible_pline+1) {
    if (pl <= u->nplines) {
      u->first_visible_pline++;
      m_scrollup(u->yfirst, u->yfirst+u->winsize-1);
      draw_line_from_plines(u, u->first_visible_pline+u->winsize-1);
    }
  } else if (pl == u->first_visible_pline-1) {
    if (pl >= 1) {
      u->first_visible_pline--;
      m_scrolldown(u->yfirst, u->yfirst+u->winsize-1);
      draw_line_from_plines(u, u->first_visible_pline);
    }
  } else if (pl >= 1 && pl <= u->nplines) {
    u->first_visible_pline = pl;
    redraw_from_plines(u);
  }
}

int find_pline(struct user *u, struct logical_line *l) {
/* finds the lline's first pline */
  
  int p1 = 1, p2 = u->nplines, p3;

  while (u->plines[p1].line->number < l->number && 
	u->plines[p2].line->number > l->number) {
    p3 = (p1+p2)>>1;
    if (u->plines[p3].line->number < l->number)
      p1 = p3;
    else
      p2 = p3;
  }
  if (u->plines[p2].line->number == l->number) p1 = p2;
  while (p1>1 && u->plines[p1-1].line->number == l->number) p1--;
  return p1;
}

struct logical_line *find_lline(struct user *u, int n, srdp_u32 seq) {
/* finds the lline, creating it if necessary but not displaying anything */
  
  int p1 = 1, p2 = u->nplines, p3;
  struct logical_line *l;

  if (n <= u->last_lline->number) {
    while (u->plines[p1].line->number < n && u->plines[p2].line->number > n) {
      p3 = (p1+p2)>>1;
      if (u->plines[p3].line->number < n)
	p1 = p3;
      else
	p2 = p3;
    }
    if (u->plines[p2].line->number == n) p1 = p2;
    return u->plines[p1].line;
  } else {
    while (n>u->last_lline->number)
      l = add_empty_lline(u, seq);
    return l;
  }
} 

static void extend_lline(struct user *u, struct logical_line *l, srdp_u32 seq,
			 int len) {
/* extends the lline to length len, if it was shorter, updating the
   plines, and the screen if visible, and updating u->first_visible_pline
   if it changed; ignores it if seq<l->len_seq */

  int pl, toadd, addhere, pointing, c;

  if (seq>l->len_seq)
    l->len_seq = seq;
  else if (seq<l->len_seq)
    return;
  if (l->length >= len) return;
  pointing = 1+l->length;
  toadd = len-l->length;
  if (len>l->arr_length) {
    l->arr_length += LINE_CHUNK;
    l->chars = (unsigned char *)myrealloc(l->chars, l->arr_length+8);
    l->seqs = (srdp_u32 *)myrealloc(l->seqs, (l->arr_length+8)*sizeof(srdp_u32));
  }
  for (c = 0; c<toadd; c++) {
    l->chars[c+1+l->length] = 0xff;
    l->seqs[c+1+l->length] = 0;
  }
  l->length = len;
  l->len_seq = seq;
  pl = find_pline(u, l);
  while (pl<u->nplines && u->plines[pl+1].line->number == l->number) pl++;
  /* pl = last pline for l */

  addhere = cols-u->plines[pl].length;
  if (addhere>toadd) addhere = toadd;
  if (addhere>0) {
    toadd -= addhere;
    u->plines[pl].length += addhere;
    pointing += addhere;
  }

  while (toadd>0) {
    addhere = (toadd <= cols ? toadd : cols);
    add_pline(u, pl+1, l, pointing, addhere);
    if (pl >= u->first_visible_pline && pl<u->first_visible_pline+u->winsize) {
      /* extending a line the last pline of which is visible */
      if (u->nplines == u->first_visible_pline+u->winsize) {
	/* and we're at the end of the buffer (special case) */
	u->first_visible_pline++;
	m_scrollup(u->yfirst, pl+1-u->first_visible_pline+u->yfirst);
      } else if (pl != u->first_visible_pline+u->winsize-1)
      m_scrolldown(u->yfirst+pl+1-u->first_visible_pline,
		 u->yfirst+u->winsize-1);
    } else if (pl<u->first_visible_pline) u->first_visible_pline++;
    pl++;
    pointing += addhere;
    toadd -= addhere;
  }
}

static void remove_pline(struct user *u, int pl) {
/* removes pline number pl */
  int c;

  for (c = pl; c<u->nplines; c++)
    u->plines[c] = u->plines[c+1];
  u->nplines--;
}

int get_plength(struct user *u, int *pl, int pos) {
/* returns the position in the pline pl or one of the following (if they
   refer to the same lline), not counting nulls, at which the character 
   'pos' in the lline is, i.e a cursor position (left = 1); updates
   pl to point to that line; can be called with pos = 0; if called with
   pos>length-of-the-right-lline, returns the position after, which
   can be cols+1 */

  int r = 1, i = 0;

  while (*pl<u->nplines && u->plines[*pl].line == u->plines[(*pl)+1].line &&
	 u->plines[(*pl)+1].offset <= pos)
    (*pl)++;
  if (!u->plines[*pl].line->any_deleted) {
    r = pos - u->plines[*pl].offset + 1;
    if (r > u->plines[*pl].line->length - u->plines[*pl].offset + 2)
      r = u->plines[*pl].line->length - u->plines[*pl].offset + 2;
    if (r < 1)
      r = 1;
  } else {
    while (u->plines[*pl].offset+i < pos &&
	   (u->plines[*pl].offset+i <= u->plines[*pl].line->length)) {
      if (u->plines[*pl].line->chars[u->plines[*pl].offset+i] != 0) r++;
      ++i;
    }
  }
  return r;
}

void shorten_lline(struct user *u, struct logical_line *l, srdp_u32 seq,
		   int len) {
/* shortens the lline to length len, if it was shorter, and lengthens
   if it was longer, updating the plines, and the screen if visible, and 
   updating u->first_visible_pline if it changed ; ignores it if 
   seq<l->len_seq */

  int pl, pos, len0;

#ifdef DEBUG
  fprintf(stderr, "shorten_lline %d %d %d\r\n", u, l->number, len); /* DEBUG */
#endif /* DEBUG */
  
  if (seq>l->len_seq)
    l->len_seq = seq;
  else if (seq<l->len_seq)
    return;
  extend_lline(u, l, seq, len);
  len0 = l->length;
  l->length = len;
  l->len_seq = seq;
  pl = find_pline(u, l);
  while (pl<u->nplines && u->plines[pl+1].line->number == l->number) pl++;
  while (u->plines[pl].offset>len && u->plines[pl].offset>1) {
    remove_pline(u, pl);
    if (pl >= u->first_visible_pline && pl<u->first_visible_pline+u->winsize) {
      m_scrollup(u->yfirst+pl-u->first_visible_pline, u->yfirst+u->winsize-1);
      if (u->first_visible_pline>u->nplines) {
	u->first_visible_pline--;
	draw_line_from_plines(u, u->first_visible_pline);
      }
    } else if (pl<u->first_visible_pline) u->first_visible_pline--;
    pl--;
  }

  if (len>0)
    pos = get_plength(u, &pl, len);
  else
    pos = 0;
  u->plines[pl].length = pos;

  if (pos<cols && pl >= u->first_visible_pline &&
      pl<u->first_visible_pline+u->winsize)
    if (len == len0-1 && pos<cols-1) {
      m_gotoxy(pos+2, u->yfirst+pl-u->first_visible_pline);
      m_backspace();
    } else {
      m_gotoxy(pos+1, u->yfirst+pl-u->first_visible_pline);
      m_cleareol();
    }
}

void set_cursor(struct user *u, struct logical_line *l, srdp_u32 seq,
		int pos) {
/* puts the cursor at the given position, unless the line is shorter
   with a higher seq number; will scroll if necessary */

  int pl;

#ifdef DEBUG
  fprintf(stderr, "set_cursor %d %d %d\r\n", u, l->number, pos);  /* DEBUG */
#endif  /* DEBUG */

  if (seq<l->len_seq && pos<l->length) return;
  extend_lline(u, l, seq, pos-1);
  pl = find_pline(u, l);
  if (pos <= l->length)
    pos = get_plength(u, &pl, pos);
  else if (pos != 1) {
    pos = get_plength(u, &pl, pos-1);
    if (++pos>cols) pos = cols;
  }
  if (pl<u->first_visible_pline)
    scroll_to_pline(u, pl);
  else if (pl >= u->first_visible_pline+u->winsize)
    scroll_to_pline(u, pl-u->winsize+1);
  m_gotoxy(pos, u->yfirst+pl-u->first_visible_pline);
}

void write_char(struct user *u, struct logical_line *l, srdp_u32 seq, int pos, 
		unsigned char c) {
/* puts the character at the given position in the llines, will fix
   the plines if it's a null, and prints it if it's visible */

  int pl, ppos, cpl;
  char oc;

#ifdef DEBUG
  if (c >= ' ') /* DEBUG */
    fprintf(stderr, "write_char %d %d %d '%c'\r\n", u, l->number, pos, c); /* DEBUG */
  else /* DEBUG */
    fprintf(stderr, "write_char %d %d %d %.2x\r\n", u, l->number, pos, c); /* DEBUG */
#endif /* DEBUG */
  if (seq<l->len_seq && pos>l->length) return;
  extend_lline(u, l, seq, pos);
  pl = find_pline(u, l);
  if (l->seqs[pos]>seq) return;
  ppos = get_plength(u, &pl, pos);
  oc = l->chars[pos];
  l->chars[pos] = c;
  l->seqs[pos] = seq;
  if (c == 0) {
    l->any_deleted = 1;
    if (oc == 0)
      return;
    cpl = pl;
    u->plines[cpl].length--;
    if (pl >= u->first_visible_pline && pl<u->first_visible_pline+u->winsize)
      draw_end_of_line_from_plines(u, pl, pos, ppos);
    while (cpl<u->nplines && u->plines[cpl+1].line == l &&
	   u->plines[cpl+1].length>0) {
      u->plines[cpl].length++;
      u->plines[cpl+1].length--;
      while (u->plines[cpl+1].offset <= l->length && 
	     l->chars[u->plines[cpl+1].offset++] == 0);
      cpl++;
      if (cpl >= u->first_visible_pline && cpl<u->first_visible_pline+u->winsize)
	draw_line_from_plines(u, cpl);
    }
    if (cpl <= u->nplines && u->plines[cpl].line == l && 
	u->plines[cpl].length == 0) {
      /* need to destroy cpl */
      remove_pline(u, cpl);
      if (cpl >= u->first_visible_pline && 
	  cpl<u->first_visible_pline+u->winsize) {
	m_scrollup(u->yfirst+cpl-u->first_visible_pline, u->yfirst+u->winsize-1);
	if (u->first_visible_pline>u->nplines) {
	  u->first_visible_pline--;
	  draw_line_from_plines(u, u->first_visible_pline);
	}
      } else if (cpl<u->first_visible_pline) u->first_visible_pline--;
    }
    if (pl >= u->first_visible_pline && pl<u->first_visible_pline+u->winsize)
      m_gotoxy(ppos, u->yfirst+pl-u->first_visible_pline);
  } else if (pl >= u->first_visible_pline && 
	     pl<u->first_visible_pline+u->winsize) {
    m_gotoxy(ppos, u->yfirst+pl-u->first_visible_pline);
    printchar(c);
  } else if (u == users[0] || u != users[active_window == -1 ? 0 : active_window]) {
    cpl = pl+1-u->winsize;
    if (cpl<1) cpl = 1;
    scroll_to_pline(u, cpl);
    m_gotoxy(ppos, u->yfirst+pl-u->first_visible_pline);
    printchar(c);
  }
}

void recalc_all(void) {
  struct logical_line **oldcll, *l;
  struct user *u;
  int *oldpos, lastpl, idx, pl, keeppl = 0, i;

  oldpos = (int *)mymalloc(active_buffers*sizeof(int));
  oldcll = (struct logical_line **)mymalloc(active_buffers*sizeof(l));
  for (i = 0; i<active_buffers; i++) {
    u = users[i];
    if (u->first_visible_pline+u->winsize-1>u->nplines)
      lastpl = u->nplines;
    else
      lastpl = u->first_visible_pline+u->winsize-1;
    oldcll[i] = u->plines[lastpl].line;
    if (lastpl == u->nplines || u->plines[lastpl+1].line != oldcll[i])
      oldpos[i] = oldcll[i]->length;
    else
      oldpos[i] = u->plines[lastpl+1].offset-1;
  }
  users[0]->winsize = (lines-2)>>1;
  users[1]->winsize = (lines-3)>>1;
  users[0]->ystatus = 1;
  users[1]->ystatus = 2+users[0]->winsize;
  users[0]->yfirst = 2;
  users[1]->yfirst = 1+users[1]->ystatus;
  setstatus(users[0], NULL);
  setstatus(users[1], NULL);
  for (i = 0; i<active_buffers; i++) {
  /* try to keep oldcll, oldpos visible at the bottom of the screen */
    u = users[i];
    u->nplines = 0;
    l = u->first_lline;
    while (l != NULL) {
      idx = 1;
      add_pline(u, u->nplines+1, l, 1, 0);
      pl = u->nplines;
      while (idx <= l->length) {
	if (u->plines[pl].length >= cols) {
	  add_pline(u, u->nplines+1, l, idx, 0);
	  pl = u->nplines;
	}
	if (l == oldcll[i] && idx == oldpos[i]) keeppl = pl;
	if (l->chars[idx++] != 0) u->plines[pl].length++;
      }
      l = l->next;
    }
    if (keeppl >= u->winsize)
      u->first_visible_pline = 1+keeppl-u->winsize;
    else
      u->first_visible_pline = 1;
    redraw_from_plines(u);
  }
  free(oldcll);
  free(oldpos);
}

void pl_redraw_screen_zone(int y1, int y2) {
  struct user *u;
  int i, pl;
  
  for (i=0; i<active_buffers; i++) {
    u = users[i];
    if (u->ystatus >= y1 && u->ystatus <= y2)
      setstatus(u, NULL);
    if (u->yfirst <= y2 && u->yfirst + u->winsize - 1 >= y1) {
      pl = u->first_visible_pline;
      if (u->yfirst <= y1)
	pl = u->first_visible_pline + y1 - u->yfirst;
      while (pl < u->first_visible_pline + u->winsize &&
	     pl <= u->first_visible_pline + y2 - u->yfirst)
	draw_line_from_plines(u, pl++);
    }
  }
}

void pl_redraw_all(void) {
  int i;

  for (i=0; i<active_buffers; i++) {
    setstatus(users[i], NULL);
    redraw_from_plines(users[i]);
  }
}