1 /* display.c -- How to display Info windows.
2 $Id: display.c,v 1.6 1997/07/24 21:13:27 karl Exp $
4 Copyright (C) 1993, 97 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Written by Brian Fox (bfox@ai.mit.edu). */
25 extern int info_any_buffered_input_p (); /* Found in session.c. */
27 static void free_display ();
28 static DISPLAY_LINE **make_display ();
30 /* An array of display lines which tell us what is currently visible on
32 DISPLAY_LINE **the_display = (DISPLAY_LINE **)NULL;
34 /* Non-zero means do no output. */
35 int display_inhibited = 0;
37 /* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */
39 display_initialize_display (width, height)
42 free_display (the_display);
43 the_display = make_display (width, height);
44 display_clear_display (the_display);
47 /* Clear all of the lines in DISPLAY making the screen blank. */
49 display_clear_display (display)
50 DISPLAY_LINE **display;
53 register DISPLAY_LINE *display_line;
55 for (i = 0; (display_line = display[i]); i++)
57 display[i]->text[0] = '\0';
58 display[i]->textlen = 0;
59 display[i]->inverse = 0;
63 /* Non-zero if we didn't completely redisplay a window. */
64 int display_was_interrupted_p = 0;
66 /* Update the windows pointed to by WINDOW in the_display. This actually
67 writes the text on the screen. */
69 display_update_display (window)
74 display_was_interrupted_p = 0;
76 /* For every window in the list, check contents against the display. */
77 for (win = window; win; win = win->next)
79 /* Only re-display visible windows which need updating. */
80 if (((win->flags & W_WindowVisible) == 0) ||
81 ((win->flags & W_UpdateWindow) == 0) ||
85 display_update_one_window (win);
86 if (display_was_interrupted_p)
90 /* Always update the echo area. */
91 display_update_one_window (the_echo_area);
94 /* Display WIN on the_display. Unlike display_update_display (), this
95 function only does one window. */
97 display_update_one_window (win)
100 register char *nodetext; /* Current character to display. */
101 register char *last_node_char; /* Position of the last character in node. */
102 register int i; /* General use index. */
103 char *printed_line; /* Buffer for a printed line. */
104 int pl_index = 0; /* Index into PRINTED_LINE. */
105 int line_index = 0; /* Number of lines done so far. */
106 DISPLAY_LINE **display = the_display;
108 /* If display is inhibited, that counts as an interrupted display. */
109 if (display_inhibited)
110 display_was_interrupted_p = 1;
112 /* If the window has no height, or display is inhibited, quit now. */
113 if (!win->height || display_inhibited)
116 /* If the window's first row doesn't appear in the_screen, then it
117 cannot be displayed. This can happen when the_echo_area is the
118 window to be displayed, and the screen has shrunk to less than one
120 if ((win->first_row < 0) || (win->first_row > the_screen->height))
123 /* Print each line in the window into our local buffer, and then
124 check the contents of that buffer against the display. If they
125 differ, update the display. */
126 printed_line = (char *)xmalloc (1 + win->width);
128 if (!win->node || !win->line_starts)
129 goto done_with_node_display;
131 nodetext = win->line_starts[win->pagetop];
132 last_node_char = win->node->contents + win->node->nodelen;
134 for (; nodetext < last_node_char; nodetext++)
136 char *rep, *rep_carried_over, rep_temp[2];
139 if (isprint (*nodetext))
141 rep_temp[0] = *nodetext;
148 if (*nodetext == '\r' || *nodetext == '\n')
150 replen = win->width - pl_index;
154 rep = printed_representation (*nodetext, pl_index);
155 replen = strlen (rep);
159 /* If this character can be printed without passing the width of
160 the line, then stuff it into the line. */
161 if (replen + pl_index < win->width)
163 /* Optimize if possible. */
166 printed_line[pl_index++] = *rep;
170 for (i = 0; i < replen; i++)
171 printed_line[pl_index++] = rep[i];
178 /* If this character cannot be printed in this line, we have
179 found the end of this line as it would appear on the screen.
180 Carefully print the end of the line, and then compare. */
181 if (*nodetext == '\n' || *nodetext == '\r' || *nodetext == '\t')
183 printed_line[pl_index] = '\0';
184 rep_carried_over = (char *)NULL;
188 /* The printed representation of this character extends into
189 the next line. Remember the offset of the last character
190 printed out of REP so that we can carry the character over
192 for (i = 0; pl_index < (win->width - 1);)
193 printed_line[pl_index++] = rep[i++];
195 rep_carried_over = rep + i;
197 /* If printing the last character in this window couldn't
198 possibly cause the screen to scroll, place a backslash
199 in the rightmost column. */
200 if (1 + line_index + win->first_row < the_screen->height)
202 if (win->flags & W_NoWrap)
203 printed_line[pl_index++] = '$';
205 printed_line[pl_index++] = '\\';
207 printed_line[pl_index] = '\0';
210 /* We have the exact line as it should appear on the screen.
211 Check to see if this line matches the one already appearing
213 entry = display[line_index + win->first_row];
215 /* If the screen line is inversed, then we have to clear
216 the line from the screen first. Why, I don't know. */
219 terminal_goto_xy (0, line_index + win->first_row);
220 terminal_clear_to_eol ();
222 entry->text[0] = '\0';
226 /* Find the offset where these lines differ. */
227 for (i = 0; i < pl_index; i++)
228 if (printed_line[i] != entry->text[i])
231 /* If the lines are not the same length, or if they differed
232 at all, we must do some redrawing. */
233 if ((i != pl_index) || (pl_index != entry->textlen))
235 /* Move to the proper point on the terminal. */
236 terminal_goto_xy (i, line_index + win->first_row);
238 /* If there is any text to print, print it. */
240 terminal_put_text (printed_line + i);
242 /* If the printed text didn't extend all the way to the edge
243 of the window, and text was appearing between here and the
244 edge of the window, clear from here to the end of the line. */
245 if ((pl_index < win->width && pl_index < entry->textlen) ||
247 terminal_clear_to_eol ();
251 /* Update the display text buffer. */
252 strcpy (entry->text + i, printed_line + i);
253 entry->textlen = pl_index;
255 /* Lines showing node text are not in inverse. Only modelines
256 have that distinction. */
260 /* We have done at least one line. Increment our screen line
261 index, and check against the bottom of the window. */
262 if (++line_index == win->height)
265 /* A line has been displayed, and the screen reflects that state.
266 If there is typeahead pending, then let that typeahead be read
267 now, instead of continuing with the display. */
268 if (info_any_buffered_input_p ())
271 display_was_interrupted_p = 1;
275 /* Reset PL_INDEX to the start of the line. */
278 /* If there are characters from REP left to print, stuff them
279 into the buffer now. */
280 if (rep_carried_over)
281 for (; rep[pl_index]; pl_index++)
282 printed_line[pl_index] = rep[pl_index];
284 /* If this window has chosen not to wrap lines, skip to the end
285 of the physical line in the buffer, and start a new line here. */
286 if (pl_index && (win->flags & W_NoWrap))
291 printed_line[0] = '\0';
295 while ((nodetext < last_node_char) && (*nodetext != '\n'))
301 done_with_node_display:
302 /* We have reached the end of the node or the end of the window. If it
303 is the end of the node, then clear the lines of the window from here
304 to the end of the window. */
305 for (; line_index < win->height; line_index++)
307 DISPLAY_LINE *entry = display[line_index + win->first_row];
309 /* If this line has text on it then make it go away. */
310 if (entry && entry->textlen)
313 entry->text[0] = '\0';
315 terminal_goto_xy (0, line_index + win->first_row);
316 terminal_clear_to_eol ();
320 /* Finally, if this window has a modeline it might need to be redisplayed.
321 Check the window's modeline against the one in the display, and update
323 if ((win->flags & W_InhibitMode) == 0)
325 window_make_modeline (win);
326 line_index = win->first_row + win->height;
328 /* This display line must both be in inverse, and have the same
330 if ((!display[line_index]->inverse) ||
331 (strcmp (display[line_index]->text, win->modeline) != 0))
333 terminal_goto_xy (0, line_index);
334 terminal_begin_inverse ();
335 terminal_put_text (win->modeline);
336 terminal_end_inverse ();
337 strcpy (display[line_index]->text, win->modeline);
338 display[line_index]->inverse = 1;
339 display[line_index]->textlen = strlen (win->modeline);
344 /* Okay, this window doesn't need updating anymore. */
345 win->flags &= ~W_UpdateWindow;
350 /* Scroll the region of the_display starting at START, ending at END, and
351 moving the lines AMOUNT lines. If AMOUNT is less than zero, the lines
352 are moved up in the screen, otherwise down. Actually, it is possible
353 for no scrolling to take place in the case that the terminal doesn't
354 support it. This doesn't matter to us. */
356 display_scroll_display (start, end, amount)
357 int start, end, amount;
359 register int i, last;
362 /* If this terminal cannot do scrolling, give up now. */
363 if (!terminal_can_scroll)
366 /* If there isn't anything displayed on the screen because it is too
371 /* If there is typeahead pending, then don't actually do any scrolling. */
372 if (info_any_buffered_input_p ())
375 /* Do it on the screen. */
376 terminal_scroll_terminal (start, end, amount);
378 /* Now do it in the display buffer so our contents match the screen. */
383 /* Shift the lines to scroll right into place. */
384 for (i = 0; i < (end - start); i++)
386 temp = the_display[last - i];
387 the_display[last - i] = the_display[end - i];
388 the_display[end - i] = temp;
391 /* The lines have been shifted down in the buffer. Clear all of the
392 lines that were vacated. */
393 for (i = start; i != (start + amount); i++)
395 the_display[i]->text[0] = '\0';
396 the_display[i]->textlen = 0;
397 the_display[i]->inverse = 0;
403 last = start + amount;
404 for (i = 0; i < (end - start); i++)
406 temp = the_display[last + i];
407 the_display[last + i] = the_display[start + i];
408 the_display[start + i] = temp;
411 /* The lines have been shifted up in the buffer. Clear all of the
412 lines that are left over. */
413 for (i = end + amount; i != end; i++)
415 the_display[i]->text[0] = '\0';
416 the_display[i]->textlen = 0;
417 the_display[i]->inverse = 0;
422 /* Try to scroll lines in WINDOW. OLD_PAGETOP is the pagetop of WINDOW before
423 having had its line starts recalculated. OLD_STARTS is the list of line
424 starts that used to appear in this window. OLD_COUNT is the number of lines
425 that appear in the OLD_STARTS array. */
427 display_scroll_line_starts (window, old_pagetop, old_starts, old_count)
429 int old_pagetop, old_count;
432 register int i, old, new; /* Indices into the line starts arrays. */
433 int last_new, last_old; /* Index of the last visible line. */
434 int old_first, new_first; /* Index of the first changed line. */
435 int unchanged_at_top = 0;
436 int already_scrolled = 0;
438 /* Locate the first line which was displayed on the old window. */
439 old_first = old_pagetop;
440 new_first = window->pagetop;
442 /* Find the last line currently visible in this window. */
443 last_new = window->pagetop + (window->height - 1);
444 if (last_new > window->line_count)
445 last_new = window->line_count - 1;
447 /* Find the last line which used to be currently visible in this window. */
448 last_old = old_pagetop + (window->height - 1);
449 if (last_old > old_count)
450 last_old = old_count - 1;
452 for (old = old_first, new = new_first;
453 old < last_old && new < last_new;
455 if (old_starts[old] != window->line_starts[new])
460 /* Loop through the old lines looking for a match in the new lines. */
461 for (old = old_first + unchanged_at_top; old < last_old; old++)
463 for (new = new_first; new < last_new; new++)
464 if (old_starts[old] == window->line_starts[new])
466 /* Find the extent of the matching lines. */
467 for (i = 0; (old + i) < last_old; i++)
468 if (old_starts[old + i] != window->line_starts[new + i])
471 /* Scroll these lines if there are enough of them. */
473 int start, end, amount;
475 start = (window->first_row
476 + ((old + already_scrolled) - old_pagetop));
477 amount = new - (old + already_scrolled);
478 end = window->first_row + window->height;
480 /* If we are shifting the block of lines down, then the last
481 AMOUNT lines will become invisible. Thus, don't bother
486 if ((end - start) > 0)
488 display_scroll_display (start, end, amount);
490 /* Some lines have been scrolled. Simulate the scrolling
491 by offsetting the value of the old index. */
493 already_scrolled += amount;
500 /* Move the screen cursor to directly over the current character in WINDOW. */
502 display_cursor_at_point (window)
507 vpos = window_line_of_point (window) - window->pagetop + window->first_row;
508 hpos = window_get_cursor_column (window);
509 terminal_goto_xy (hpos, vpos);
513 /* **************************************************************** */
515 /* Functions Static to this File */
517 /* **************************************************************** */
519 /* Make a DISPLAY_LINE ** with width and height. */
520 static DISPLAY_LINE **
521 make_display (width, height)
525 DISPLAY_LINE **display;
527 display = (DISPLAY_LINE **)xmalloc ((1 + height) * sizeof (DISPLAY_LINE *));
529 for (i = 0; i < height; i++)
531 display[i] = (DISPLAY_LINE *)xmalloc (sizeof (DISPLAY_LINE));
532 display[i]->text = (char *)xmalloc (1 + width);
533 display[i]->textlen = 0;
534 display[i]->inverse = 0;
536 display[i] = (DISPLAY_LINE *)NULL;
540 /* Free the storage allocated to DISPLAY. */
542 free_display (display)
543 DISPLAY_LINE **display;
546 register DISPLAY_LINE *display_line;
551 for (i = 0; (display_line = display[i]); i++)
553 free (display_line->text);