171
|
1 /* display.c -- How to display Info windows. */ |
|
2 |
|
3 /* This file is part of GNU Info, a program for reading online documentation |
|
4 stored in Info format. |
|
5 |
|
6 Copyright (C) 1993 Free Software Foundation, Inc. |
|
7 |
|
8 This program is free software; you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation; either version 2, or (at your option) |
|
11 any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program; if not, write to the Free Software |
1315
|
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
171
|
21 |
|
22 Written by Brian Fox (bfox@ai.mit.edu). */ |
|
23 |
1242
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
171
|
28 #include <stdio.h> |
|
29 #include <ctype.h> |
|
30 #include <sys/types.h> |
|
31 #include <sys/stat.h> |
|
32 #include "display.h" |
|
33 |
|
34 extern int info_any_buffered_input_p (); /* Found in session.c. */ |
|
35 |
|
36 static void free_display (); |
|
37 static DISPLAY_LINE **make_display (); |
|
38 |
|
39 /* An array of display lines which tell us what is currently visible on |
|
40 the display. */ |
|
41 DISPLAY_LINE **the_display = (DISPLAY_LINE **)NULL; |
|
42 |
|
43 /* Non-zero means do no output. */ |
|
44 int display_inhibited = 0; |
|
45 |
|
46 /* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */ |
|
47 void |
|
48 display_initialize_display (width, height) |
|
49 int width, height; |
|
50 { |
|
51 free_display (the_display); |
|
52 the_display = make_display (width, height); |
|
53 display_clear_display (the_display); |
|
54 } |
|
55 |
|
56 /* Clear all of the lines in DISPLAY making the screen blank. */ |
|
57 void |
|
58 display_clear_display (display) |
|
59 DISPLAY_LINE **display; |
|
60 { |
|
61 register int i; |
|
62 register DISPLAY_LINE *display_line; |
|
63 |
|
64 for (i = 0; display_line = display[i]; i++) |
|
65 { |
|
66 display[i]->text[0] = '\0'; |
|
67 display[i]->textlen = 0; |
|
68 display[i]->inverse = 0; |
|
69 } |
|
70 } |
|
71 |
|
72 /* Non-zero if we didn't completely redisplay a window. */ |
|
73 int display_was_interrupted_p = 0; |
|
74 |
|
75 /* Update the windows pointed to by WINDOW in the_display. This actually |
|
76 writes the text on the screen. */ |
|
77 void |
|
78 display_update_display (window) |
|
79 WINDOW *window; |
|
80 { |
|
81 register WINDOW *win; |
|
82 |
|
83 display_was_interrupted_p = 0; |
|
84 |
|
85 /* For every window in the list, check contents against the display. */ |
|
86 for (win = window; win; win = win->next) |
|
87 { |
|
88 /* Only re-display visible windows which need updating. */ |
|
89 if (((win->flags & W_WindowVisible) == 0) || |
|
90 ((win->flags & W_UpdateWindow) == 0) || |
|
91 (win->height == 0)) |
|
92 continue; |
|
93 |
|
94 display_update_one_window (win); |
|
95 if (display_was_interrupted_p) |
|
96 break; |
|
97 } |
|
98 |
|
99 /* Always update the echo area. */ |
|
100 display_update_one_window (the_echo_area); |
|
101 } |
|
102 |
|
103 /* Display WIN on the_display. Unlike display_update_display (), this |
|
104 function only does one window. */ |
|
105 void |
|
106 display_update_one_window (win) |
|
107 WINDOW *win; |
|
108 { |
|
109 register char *nodetext; /* Current character to display. */ |
|
110 register char *last_node_char; /* Position of the last character in node. */ |
|
111 register int i; /* General use index. */ |
|
112 char *printed_line; /* Buffer for a printed line. */ |
|
113 int pl_index = 0; /* Index into PRINTED_LINE. */ |
|
114 int line_index = 0; /* Number of lines done so far. */ |
|
115 DISPLAY_LINE **display = the_display; |
|
116 |
|
117 /* If display is inhibited, that counts as an interrupted display. */ |
|
118 if (display_inhibited) |
|
119 display_was_interrupted_p = 1; |
|
120 |
|
121 /* If the window has no height, or display is inhibited, quit now. */ |
|
122 if (!win->height || display_inhibited) |
|
123 return; |
|
124 |
|
125 /* If the window's first row doesn't appear in the_screen, then it |
|
126 cannot be displayed. This can happen when the_echo_area is the |
|
127 window to be displayed, and the screen has shrunk to less than one |
|
128 line. */ |
|
129 if ((win->first_row < 0) || (win->first_row > the_screen->height)) |
|
130 return; |
|
131 |
|
132 /* Print each line in the window into our local buffer, and then |
|
133 check the contents of that buffer against the display. If they |
|
134 differ, update the display. */ |
|
135 printed_line = (char *)xmalloc (1 + win->width); |
|
136 |
|
137 if (!win->node || !win->line_starts) |
|
138 goto done_with_node_display; |
|
139 |
|
140 nodetext = win->line_starts[win->pagetop]; |
|
141 last_node_char = win->node->contents + win->node->nodelen; |
|
142 |
|
143 for (; nodetext < last_node_char; nodetext++) |
|
144 { |
|
145 char *rep, *rep_carried_over, rep_temp[2]; |
|
146 int replen; |
|
147 |
|
148 if (isprint (*nodetext)) |
|
149 { |
|
150 rep_temp[0] = *nodetext; |
|
151 replen = 1; |
|
152 rep_temp[1] = '\0'; |
|
153 rep = rep_temp; |
|
154 } |
|
155 else |
|
156 { |
|
157 if (*nodetext == '\r' || *nodetext == '\n') |
|
158 { |
|
159 replen = win->width - pl_index; |
|
160 } |
|
161 else |
|
162 { |
|
163 rep = printed_representation (*nodetext, pl_index); |
|
164 replen = strlen (rep); |
|
165 } |
|
166 } |
|
167 |
|
168 /* If this character can be printed without passing the width of |
|
169 the line, then stuff it into the line. */ |
|
170 if (replen + pl_index < win->width) |
|
171 { |
|
172 /* Optimize if possible. */ |
|
173 if (replen == 1) |
|
174 { |
|
175 printed_line[pl_index++] = *rep; |
|
176 } |
|
177 else |
|
178 { |
|
179 for (i = 0; i < replen; i++) |
|
180 printed_line[pl_index++] = rep[i]; |
|
181 } |
|
182 } |
|
183 else |
|
184 { |
|
185 DISPLAY_LINE *entry; |
|
186 |
|
187 /* If this character cannot be printed in this line, we have |
|
188 found the end of this line as it would appear on the screen. |
|
189 Carefully print the end of the line, and then compare. */ |
|
190 if (*nodetext == '\n' || *nodetext == '\r' || *nodetext == '\t') |
|
191 { |
|
192 printed_line[pl_index] = '\0'; |
|
193 rep_carried_over = (char *)NULL; |
|
194 } |
|
195 else |
|
196 { |
|
197 /* The printed representation of this character extends into |
|
198 the next line. Remember the offset of the last character |
|
199 printed out of REP so that we can carry the character over |
|
200 to the next line. */ |
|
201 for (i = 0; pl_index < (win->width - 1);) |
|
202 printed_line[pl_index++] = rep[i++]; |
|
203 |
|
204 rep_carried_over = rep + i; |
|
205 |
|
206 /* If printing the last character in this window couldn't |
|
207 possibly cause the screen to scroll, place a backslash |
|
208 in the rightmost column. */ |
|
209 if (1 + line_index + win->first_row < the_screen->height) |
|
210 { |
|
211 if (win->flags & W_NoWrap) |
|
212 printed_line[pl_index++] = '$'; |
|
213 else |
|
214 printed_line[pl_index++] = '\\'; |
|
215 } |
|
216 printed_line[pl_index] = '\0'; |
|
217 } |
|
218 |
|
219 /* We have the exact line as it should appear on the screen. |
|
220 Check to see if this line matches the one already appearing |
|
221 on the screen. */ |
|
222 entry = display[line_index + win->first_row]; |
|
223 |
|
224 /* If the screen line is inversed, then we have to clear |
|
225 the line from the screen first. Why, I don't know. */ |
|
226 if (entry->inverse) |
|
227 { |
|
228 terminal_goto_xy (0, line_index + win->first_row); |
|
229 terminal_clear_to_eol (); |
|
230 entry->inverse = 0; |
|
231 entry->text[0] = '\0'; |
|
232 entry->textlen = 0; |
|
233 } |
|
234 |
|
235 /* Find the offset where these lines differ. */ |
|
236 for (i = 0; i < pl_index; i++) |
|
237 if (printed_line[i] != entry->text[i]) |
|
238 break; |
|
239 |
|
240 /* If the lines are not the same length, or if they differed |
|
241 at all, we must do some redrawing. */ |
|
242 if ((i != pl_index) || (pl_index != entry->textlen)) |
|
243 { |
|
244 /* Move to the proper point on the terminal. */ |
|
245 terminal_goto_xy (i, line_index + win->first_row); |
|
246 |
|
247 /* If there is any text to print, print it. */ |
|
248 if (i != pl_index) |
|
249 terminal_put_text (printed_line + i); |
|
250 |
|
251 /* If the printed text didn't extend all the way to the edge |
|
252 of the window, and text was appearing between here and the |
|
253 edge of the window, clear from here to the end of the line. */ |
|
254 if ((pl_index < win->width && pl_index < entry->textlen) || |
|
255 (entry->inverse)) |
|
256 terminal_clear_to_eol (); |
|
257 |
|
258 fflush (stdout); |
|
259 |
|
260 /* Update the display text buffer. */ |
|
261 strcpy (entry->text + i, printed_line + i); |
|
262 entry->textlen = pl_index; |
|
263 |
|
264 /* Lines showing node text are not in inverse. Only modelines |
|
265 have that distinction. */ |
|
266 entry->inverse = 0; |
|
267 } |
|
268 |
|
269 /* We have done at least one line. Increment our screen line |
|
270 index, and check against the bottom of the window. */ |
|
271 if (++line_index == win->height) |
|
272 break; |
|
273 |
|
274 /* A line has been displayed, and the screen reflects that state. |
|
275 If there is typeahead pending, then let that typeahead be read |
|
276 now, instead of continuing with the display. */ |
|
277 if (info_any_buffered_input_p ()) |
|
278 { |
|
279 free (printed_line); |
|
280 display_was_interrupted_p = 1; |
|
281 return; |
|
282 } |
|
283 |
|
284 /* Reset PL_INDEX to the start of the line. */ |
|
285 pl_index = 0; |
|
286 |
|
287 /* If there are characters from REP left to print, stuff them |
|
288 into the buffer now. */ |
|
289 if (rep_carried_over) |
|
290 for (; rep[pl_index]; pl_index++) |
|
291 printed_line[pl_index] = rep[pl_index]; |
|
292 |
|
293 /* If this window has chosen not to wrap lines, skip to the end |
|
294 of the physical line in the buffer, and start a new line here. */ |
|
295 if (pl_index && (win->flags & W_NoWrap)) |
|
296 { |
|
297 char *begin; |
|
298 |
|
299 pl_index = 0; |
|
300 printed_line[0] = '\0'; |
|
301 |
|
302 begin = nodetext; |
|
303 |
|
304 while ((nodetext < last_node_char) && (*nodetext != '\n')) |
|
305 nodetext++; |
|
306 } |
|
307 } |
|
308 } |
|
309 |
|
310 done_with_node_display: |
|
311 /* We have reached the end of the node or the end of the window. If it |
|
312 is the end of the node, then clear the lines of the window from here |
|
313 to the end of the window. */ |
|
314 for (; line_index < win->height; line_index++) |
|
315 { |
|
316 DISPLAY_LINE *entry = display[line_index + win->first_row]; |
|
317 |
|
318 /* If this line has text on it then make it go away. */ |
|
319 if (entry && entry->textlen) |
|
320 { |
|
321 entry->textlen = 0; |
|
322 entry->text[0] = '\0'; |
|
323 |
|
324 terminal_goto_xy (0, line_index + win->first_row); |
|
325 terminal_clear_to_eol (); |
|
326 } |
|
327 } |
|
328 |
|
329 /* Finally, if this window has a modeline it might need to be redisplayed. |
|
330 Check the window's modeline against the one in the display, and update |
|
331 if necessary. */ |
|
332 if ((win->flags & W_InhibitMode) == 0) |
|
333 { |
|
334 window_make_modeline (win); |
|
335 line_index = win->first_row + win->height; |
|
336 |
|
337 /* This display line must both be in inverse, and have the same |
|
338 contents. */ |
|
339 if ((!display[line_index]->inverse) || |
|
340 (strcmp (display[line_index]->text, win->modeline) != 0)) |
|
341 { |
|
342 terminal_goto_xy (0, line_index); |
|
343 terminal_begin_inverse (); |
|
344 terminal_put_text (win->modeline); |
|
345 terminal_end_inverse (); |
|
346 strcpy (display[line_index]->text, win->modeline); |
|
347 display[line_index]->inverse = 1; |
|
348 display[line_index]->textlen = strlen (win->modeline); |
|
349 fflush (stdout); |
|
350 } |
|
351 } |
|
352 |
|
353 /* Okay, this window doesn't need updating anymore. */ |
|
354 win->flags &= ~W_UpdateWindow; |
|
355 free (printed_line); |
|
356 fflush (stdout); |
|
357 } |
|
358 |
|
359 /* Scroll the region of the_display starting at START, ending at END, and |
|
360 moving the lines AMOUNT lines. If AMOUNT is less than zero, the lines |
|
361 are moved up in the screen, otherwise down. Actually, it is possible |
|
362 for no scrolling to take place in the case that the terminal doesn't |
|
363 support it. This doesn't matter to us. */ |
|
364 void |
|
365 display_scroll_display (start, end, amount) |
|
366 int start, end, amount; |
|
367 { |
|
368 register int i, last; |
|
369 DISPLAY_LINE *temp; |
|
370 |
|
371 /* If this terminal cannot do scrolling, give up now. */ |
|
372 if (!terminal_can_scroll) |
|
373 return; |
|
374 |
|
375 /* If there isn't anything displayed on the screen because it is too |
|
376 small, quit now. */ |
|
377 if (!the_display[0]) |
|
378 return; |
|
379 |
|
380 /* If there is typeahead pending, then don't actually do any scrolling. */ |
|
381 if (info_any_buffered_input_p ()) |
|
382 return; |
|
383 |
|
384 /* Do it on the screen. */ |
|
385 terminal_scroll_terminal (start, end, amount); |
|
386 |
|
387 /* Now do it in the display buffer so our contents match the screen. */ |
|
388 if (amount > 0) |
|
389 { |
|
390 last = end + amount; |
|
391 |
|
392 /* Shift the lines to scroll right into place. */ |
|
393 for (i = 0; i < (end - start); i++) |
|
394 { |
|
395 temp = the_display[last - i]; |
|
396 the_display[last - i] = the_display[end - i]; |
|
397 the_display[end - i] = temp; |
|
398 } |
|
399 |
|
400 /* The lines have been shifted down in the buffer. Clear all of the |
|
401 lines that were vacated. */ |
|
402 for (i = start; i != (start + amount); i++) |
|
403 { |
|
404 the_display[i]->text[0] = '\0'; |
|
405 the_display[i]->textlen = 0; |
|
406 the_display[i]->inverse = 0; |
|
407 } |
|
408 } |
|
409 |
|
410 if (amount < 0) |
|
411 { |
|
412 last = start + amount; |
|
413 for (i = 0; i < (end - start); i++) |
|
414 { |
|
415 temp = the_display[last + i]; |
|
416 the_display[last + i] = the_display[start + i]; |
|
417 the_display[start + i] = temp; |
|
418 } |
|
419 |
|
420 /* The lines have been shifted up in the buffer. Clear all of the |
|
421 lines that are left over. */ |
|
422 for (i = end + amount; i != end; i++) |
|
423 { |
|
424 the_display[i]->text[0] = '\0'; |
|
425 the_display[i]->textlen = 0; |
|
426 the_display[i]->inverse = 0; |
|
427 } |
|
428 } |
|
429 } |
|
430 |
|
431 /* Try to scroll lines in WINDOW. OLD_PAGETOP is the pagetop of WINDOW before |
|
432 having had its line starts recalculated. OLD_STARTS is the list of line |
|
433 starts that used to appear in this window. OLD_COUNT is the number of lines |
|
434 that appear in the OLD_STARTS array. */ |
|
435 void |
|
436 display_scroll_line_starts (window, old_pagetop, old_starts, old_count) |
|
437 WINDOW *window; |
|
438 int old_pagetop, old_count; |
|
439 char **old_starts; |
|
440 { |
|
441 register int i, old, new; /* Indices into the line starts arrays. */ |
|
442 int last_new, last_old; /* Index of the last visible line. */ |
|
443 int old_first, new_first; /* Index of the first changed line. */ |
|
444 int unchanged_at_top = 0; |
|
445 int already_scrolled = 0; |
|
446 |
|
447 /* Locate the first line which was displayed on the old window. */ |
|
448 old_first = old_pagetop; |
|
449 new_first = window->pagetop; |
|
450 |
|
451 /* Find the last line currently visible in this window. */ |
|
452 last_new = window->pagetop + (window->height - 1); |
|
453 if (last_new > window->line_count) |
|
454 last_new = window->line_count - 1; |
|
455 |
|
456 /* Find the last line which used to be currently visible in this window. */ |
|
457 last_old = old_pagetop + (window->height - 1); |
|
458 if (last_old > old_count) |
|
459 last_old = old_count - 1; |
|
460 |
|
461 for (old = old_first, new = new_first; |
|
462 old < last_old && new < last_new; |
|
463 old++, new++) |
|
464 if (old_starts[old] != window->line_starts[new]) |
|
465 break; |
|
466 else |
|
467 unchanged_at_top++; |
|
468 |
|
469 /* Loop through the old lines looking for a match in the new lines. */ |
|
470 for (old = old_first + unchanged_at_top; old < last_old; old++) |
|
471 { |
|
472 for (new = new_first; new < last_new; new++) |
|
473 if (old_starts[old] == window->line_starts[new]) |
|
474 { |
|
475 /* Find the extent of the matching lines. */ |
|
476 for (i = 0; (old + i) < last_old; i++) |
|
477 if (old_starts[old + i] != window->line_starts[new + i]) |
|
478 break; |
|
479 |
|
480 /* Scroll these lines if there are enough of them. */ |
|
481 { |
|
482 int start, end, amount; |
|
483 |
|
484 start = (window->first_row |
|
485 + ((old + already_scrolled) - old_pagetop)); |
|
486 amount = new - (old + already_scrolled); |
|
487 end = window->first_row + window->height; |
|
488 |
|
489 /* If we are shifting the block of lines down, then the last |
|
490 AMOUNT lines will become invisible. Thus, don't bother |
|
491 scrolling them. */ |
|
492 if (amount > 0) |
|
493 end -= amount; |
|
494 |
|
495 if ((end - start) > 0) |
|
496 { |
|
497 display_scroll_display (start, end, amount); |
|
498 |
|
499 /* Some lines have been scrolled. Simulate the scrolling |
|
500 by offsetting the value of the old index. */ |
|
501 old += i; |
|
502 already_scrolled += amount; |
|
503 } |
|
504 } |
|
505 } |
|
506 } |
|
507 } |
|
508 |
|
509 /* Move the screen cursor to directly over the current character in WINDOW. */ |
|
510 void |
|
511 display_cursor_at_point (window) |
|
512 WINDOW *window; |
|
513 { |
|
514 int vpos, hpos; |
|
515 |
|
516 vpos = window_line_of_point (window) - window->pagetop + window->first_row; |
|
517 hpos = window_get_cursor_column (window); |
|
518 terminal_goto_xy (hpos, vpos); |
|
519 } |
|
520 |
|
521 /* **************************************************************** */ |
|
522 /* */ |
|
523 /* Functions Static to this File */ |
|
524 /* */ |
|
525 /* **************************************************************** */ |
|
526 |
|
527 /* Make a DISPLAY_LINE ** with width and height. */ |
|
528 static DISPLAY_LINE ** |
|
529 make_display (width, height) |
|
530 int width, height; |
|
531 { |
|
532 register int i; |
|
533 DISPLAY_LINE **display; |
|
534 |
|
535 display = (DISPLAY_LINE **)xmalloc ((1 + height) * sizeof (DISPLAY_LINE *)); |
|
536 |
|
537 for (i = 0; i < height; i++) |
|
538 { |
|
539 display[i] = (DISPLAY_LINE *)xmalloc (sizeof (DISPLAY_LINE)); |
|
540 display[i]->text = (char *)xmalloc (1 + width); |
|
541 display[i]->textlen = 0; |
|
542 display[i]->inverse = 0; |
|
543 } |
|
544 display[i] = (DISPLAY_LINE *)NULL; |
|
545 return (display); |
|
546 } |
|
547 |
|
548 /* Free the storage allocated to DISPLAY. */ |
|
549 static void |
|
550 free_display (display) |
|
551 DISPLAY_LINE **display; |
|
552 { |
|
553 register int i; |
|
554 register DISPLAY_LINE *display_line; |
|
555 |
|
556 if (!display) |
|
557 return; |
|
558 |
|
559 for (i = 0; display_line = display[i]; i++) |
|
560 { |
|
561 free (display_line->text); |
|
562 free (display_line); |
|
563 } |
|
564 free (display); |
|
565 } |