gcc41: update README.DELETED
[dragonfly.git] / contrib / texinfo-4 / info / display.c
blob1a9fd0a9ae4afc5e2a35dac2d9cdbaecd6d89c29
1 /* display.c -- How to display Info windows.
2 $Id: display.c,v 1.7 2004/04/11 17:56:45 karl Exp $
4 Copyright (C) 1993, 1997, 2003, 2004 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)
9 any later version.
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 Originally written by Brian Fox (bfox@ai.mit.edu). */
22 #include "info.h"
23 #include "display.h"
25 extern int info_any_buffered_input_p (void); /* Found in session.c. */
27 static void free_display (DISPLAY_LINE **display);
28 static DISPLAY_LINE **make_display (int width, int height);
30 void handle_tag (char *tag);
31 void handle_tag_start (char *tag);
32 void handle_tag_end (char *tag);
34 /* An array of display lines which tell us what is currently visible on
35 the display. */
36 DISPLAY_LINE **the_display = (DISPLAY_LINE **)NULL;
38 /* Non-zero means do no output. */
39 int display_inhibited = 0;
41 /* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */
42 void
43 display_initialize_display (int width, int height)
45 free_display (the_display);
46 the_display = make_display (width, height);
47 display_clear_display (the_display);
50 /* Clear all of the lines in DISPLAY making the screen blank. */
51 void
52 display_clear_display (DISPLAY_LINE **display)
54 register int i;
56 for (i = 0; display[i]; i++)
58 display[i]->text[0] = '\0';
59 display[i]->textlen = 0;
60 display[i]->inverse = 0;
64 /* Non-zero if we didn't completely redisplay a window. */
65 int display_was_interrupted_p = 0;
67 /* Update the windows pointed to by WINDOW in the_display. This actually
68 writes the text on the screen. */
69 void
70 display_update_display (WINDOW *window)
72 register WINDOW *win;
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) ||
82 (win->height == 0))
83 continue;
85 display_update_one_window (win);
86 if (display_was_interrupted_p)
87 break;
90 /* Always update the echo area. */
91 display_update_one_window (the_echo_area);
94 void
95 handle_tag_start (char *tag)
97 /* TODO really handle this tag. */
98 return;
101 void
102 handle_tag_end (char *tag)
104 /* TODO really handle this tag. */
105 return;
108 void
109 handle_tag (char *tag)
111 if (tag[0] == '/')
113 tag++;
114 handle_tag_end (tag);
116 else
117 handle_tag_start (tag);
120 /* Display WIN on the_display. Unlike display_update_display (), this
121 function only does one window. */
122 void
123 display_update_one_window (WINDOW *win)
125 register char *nodetext; /* Current character to display. */
126 register char *last_node_char; /* Position of the last character in node. */
127 register int i; /* General use index. */
128 char *printed_line; /* Buffer for a printed line. */
129 int pl_index = 0; /* Index into PRINTED_LINE. */
130 int line_index = 0; /* Number of lines done so far. */
131 int pl_ignore = 0; /* How many chars use zero width on screen. */
132 int allocated_win_width;
133 DISPLAY_LINE **display = the_display;
135 /* If display is inhibited, that counts as an interrupted display. */
136 if (display_inhibited)
137 display_was_interrupted_p = 1;
139 /* If the window has no height, or display is inhibited, quit now. */
140 if (!win->height || display_inhibited)
141 return;
143 /* If the window's first row doesn't appear in the_screen, then it
144 cannot be displayed. This can happen when the_echo_area is the
145 window to be displayed, and the screen has shrunk to less than one
146 line. */
147 if ((win->first_row < 0) || (win->first_row > the_screen->height))
148 return;
150 /* Print each line in the window into our local buffer, and then
151 check the contents of that buffer against the display. If they
152 differ, update the display. */
153 allocated_win_width = win->width + 1;
154 printed_line = (char *)xmalloc (allocated_win_width);
156 if (!win->node || !win->line_starts)
157 goto done_with_node_display;
159 nodetext = win->line_starts[win->pagetop];
160 last_node_char = win->node->contents + win->node->nodelen;
162 for (; nodetext < last_node_char; nodetext++)
164 char *rep = NULL, *rep_carried_over, rep_temp[2];
165 int replen;
167 if (isprint (*nodetext))
169 rep_temp[0] = *nodetext;
170 replen = 1;
171 rep_temp[1] = '\0';
172 rep = rep_temp;
174 else
176 if (*nodetext == '\r' || *nodetext == '\n')
178 replen = win->width - pl_index + pl_ignore;
180 else if (*nodetext == '\0'
181 && (nodetext + 2) < last_node_char
182 && *(nodetext + 1) == '\b'
183 && *(nodetext + 2) == '[')
185 /* Found new style tag/cookie \0\b[
186 Read until the closing tag \0\b] */
187 int element_len = 0;
188 char *element;
190 /* Skip the escapes. */
191 nodetext += 3;
193 while (!(*nodetext == '\0'
194 && *(nodetext + 1) == '\b'
195 && *(nodetext + 2) == ']'))
197 nodetext++;
198 element_len++;
201 element = (char *) malloc (element_len + 1);
202 strncpy (element, nodetext - element_len, element_len);
204 /* Skip the escapes. */
205 nodetext += 2;
206 pl_ignore += element_len + 5;
207 /* Append string terminator. */
208 element[element_len] = '\0';
210 handle_tag (element);
212 /* Over and out */
213 free (element);
215 continue;
217 else
219 rep = printed_representation (*nodetext, pl_index);
220 replen = strlen (rep);
224 /* Support ANSI escape sequences under -R. */
225 if (raw_escapes_p
226 && *nodetext == '\033'
227 && nodetext[1] == '['
228 && isdigit (nodetext[2]))
230 if (nodetext[3] == 'm')
231 pl_ignore += 4;
232 else if (isdigit (nodetext[3]) && nodetext[4] == 'm')
233 pl_ignore += 5;
235 while (pl_index + 2 >= allocated_win_width - 1)
237 allocated_win_width *= 2;
238 printed_line = (char *)xrealloc (printed_line, allocated_win_width);
241 /* If this character can be printed without passing the width of
242 the line, then stuff it into the line. */
243 if (replen + pl_index < win->width + pl_ignore)
245 /* Optimize if possible. */
246 if (replen == 1)
248 printed_line[pl_index++] = *rep;
250 else
252 for (i = 0; i < replen; i++)
253 printed_line[pl_index++] = rep[i];
256 else
258 DISPLAY_LINE *entry;
260 /* If this character cannot be printed in this line, we have
261 found the end of this line as it would appear on the screen.
262 Carefully print the end of the line, and then compare. */
263 if (*nodetext == '\n' || *nodetext == '\r' || *nodetext == '\t')
265 printed_line[pl_index] = '\0';
266 rep_carried_over = (char *)NULL;
268 else
270 /* The printed representation of this character extends into
271 the next line. Remember the offset of the last character
272 printed out of REP so that we can carry the character over
273 to the next line. */
274 for (i = 0; pl_index < (win->width + pl_ignore - 1);)
275 printed_line[pl_index++] = rep[i++];
277 rep_carried_over = rep + i;
279 /* If printing the last character in this window couldn't
280 possibly cause the screen to scroll, place a backslash
281 in the rightmost column. */
282 if (1 + line_index + win->first_row < the_screen->height)
284 if (win->flags & W_NoWrap)
285 printed_line[pl_index++] = '$';
286 else
287 printed_line[pl_index++] = '\\';
289 printed_line[pl_index] = '\0';
292 /* We have the exact line as it should appear on the screen.
293 Check to see if this line matches the one already appearing
294 on the screen. */
295 entry = display[line_index + win->first_row];
297 /* If the screen line is inversed, then we have to clear
298 the line from the screen first. Why, I don't know.
299 (But don't do this if we have no visible entries, as can
300 happen if the window is shrunk very small.) */
301 if ((entry && entry->inverse)
302 /* Need to erase the line if it has escape sequences. */
303 || (raw_escapes_p && strchr (entry->text, '\033') != 0))
305 terminal_goto_xy (0, line_index + win->first_row);
306 terminal_clear_to_eol ();
307 entry->inverse = 0;
308 entry->text[0] = '\0';
309 entry->textlen = 0;
312 /* Find the offset where these lines differ. */
313 for (i = 0; i < pl_index; i++)
314 if (printed_line[i] != entry->text[i])
315 break;
317 /* If the lines are not the same length, or if they differed
318 at all, we must do some redrawing. */
319 if ((i != pl_index) || (pl_index != entry->textlen))
321 /* Move to the proper point on the terminal. */
322 terminal_goto_xy (i, line_index + win->first_row);
324 /* If there is any text to print, print it. */
325 if (i != pl_index)
326 terminal_put_text (printed_line + i);
328 /* If the printed text didn't extend all the way to the edge
329 of the window, and text was appearing between here and the
330 edge of the window, clear from here to the end of the line. */
331 if ((pl_index < win->width + pl_ignore
332 && pl_index < entry->textlen)
333 || (entry->inverse))
334 terminal_clear_to_eol ();
336 fflush (stdout);
338 /* Update the display text buffer. */
339 if (strlen (printed_line) > (unsigned int) screenwidth)
340 /* printed_line[] can include more than screenwidth
341 characters if we are under -R and there are escape
342 sequences in it. However, entry->text was
343 allocated (in display_initialize_display) for
344 screenwidth characters only. */
345 entry->text = xrealloc (entry->text, strlen (printed_line)+1);
346 strcpy (entry->text + i, printed_line + i);
347 entry->textlen = pl_index;
349 /* Lines showing node text are not in inverse. Only modelines
350 have that distinction. */
351 entry->inverse = 0;
354 /* We have done at least one line. Increment our screen line
355 index, and check against the bottom of the window. */
356 if (++line_index == win->height)
357 break;
359 /* A line has been displayed, and the screen reflects that state.
360 If there is typeahead pending, then let that typeahead be read
361 now, instead of continuing with the display. */
362 if (info_any_buffered_input_p ())
364 free (printed_line);
365 display_was_interrupted_p = 1;
366 return;
369 /* Reset PL_INDEX to the start of the line. */
370 pl_index = 0;
371 pl_ignore = 0; /* this is computed per line */
373 /* If there are characters from REP left to print, stuff them
374 into the buffer now. */
375 if (rep_carried_over)
376 for (; rep[pl_index]; pl_index++)
377 printed_line[pl_index] = rep[pl_index];
379 /* If this window has chosen not to wrap lines, skip to the end
380 of the physical line in the buffer, and start a new line here. */
381 if (pl_index && (win->flags & W_NoWrap))
383 char *begin;
385 pl_index = 0;
386 printed_line[0] = '\0';
388 begin = nodetext;
390 while ((nodetext < last_node_char) && (*nodetext != '\n'))
391 nodetext++;
396 done_with_node_display:
397 /* We have reached the end of the node or the end of the window. If it
398 is the end of the node, then clear the lines of the window from here
399 to the end of the window. */
400 for (; line_index < win->height; line_index++)
402 DISPLAY_LINE *entry = display[line_index + win->first_row];
404 /* If this line has text on it then make it go away. */
405 if (entry && entry->textlen)
407 entry->textlen = 0;
408 entry->text[0] = '\0';
410 terminal_goto_xy (0, line_index + win->first_row);
411 terminal_clear_to_eol ();
415 /* Finally, if this window has a modeline it might need to be redisplayed.
416 Check the window's modeline against the one in the display, and update
417 if necessary. */
418 if ((win->flags & W_InhibitMode) == 0)
420 window_make_modeline (win);
421 line_index = win->first_row + win->height;
423 /* This display line must both be in inverse, and have the same
424 contents. */
425 if ((!display[line_index]->inverse) ||
426 (strcmp (display[line_index]->text, win->modeline) != 0))
428 terminal_goto_xy (0, line_index);
429 terminal_begin_inverse ();
430 terminal_put_text (win->modeline);
431 terminal_end_inverse ();
432 strcpy (display[line_index]->text, win->modeline);
433 display[line_index]->inverse = 1;
434 display[line_index]->textlen = strlen (win->modeline);
435 fflush (stdout);
439 /* Okay, this window doesn't need updating anymore. */
440 win->flags &= ~W_UpdateWindow;
441 free (printed_line);
442 fflush (stdout);
445 /* Scroll the region of the_display starting at START, ending at END, and
446 moving the lines AMOUNT lines. If AMOUNT is less than zero, the lines
447 are moved up in the screen, otherwise down. Actually, it is possible
448 for no scrolling to take place in the case that the terminal doesn't
449 support it. This doesn't matter to us. */
450 void
451 display_scroll_display (int start, int end, int amount)
453 register int i, last;
454 DISPLAY_LINE *temp;
456 /* If this terminal cannot do scrolling, give up now. */
457 if (!terminal_can_scroll)
458 return;
460 /* If there isn't anything displayed on the screen because it is too
461 small, quit now. */
462 if (!the_display[0])
463 return;
465 /* If there is typeahead pending, then don't actually do any scrolling. */
466 if (info_any_buffered_input_p ())
467 return;
469 /* Do it on the screen. */
470 terminal_scroll_terminal (start, end, amount);
472 /* Now do it in the display buffer so our contents match the screen. */
473 if (amount > 0)
475 last = end + amount;
477 /* Shift the lines to scroll right into place. */
478 for (i = 0; i < (end - start); i++)
480 temp = the_display[last - i];
481 the_display[last - i] = the_display[end - i];
482 the_display[end - i] = temp;
485 /* The lines have been shifted down in the buffer. Clear all of the
486 lines that were vacated. */
487 for (i = start; i != (start + amount); i++)
489 the_display[i]->text[0] = '\0';
490 the_display[i]->textlen = 0;
491 the_display[i]->inverse = 0;
495 if (amount < 0)
497 last = start + amount;
498 for (i = 0; i < (end - start); i++)
500 temp = the_display[last + i];
501 the_display[last + i] = the_display[start + i];
502 the_display[start + i] = temp;
505 /* The lines have been shifted up in the buffer. Clear all of the
506 lines that are left over. */
507 for (i = end + amount; i != end; i++)
509 the_display[i]->text[0] = '\0';
510 the_display[i]->textlen = 0;
511 the_display[i]->inverse = 0;
516 /* Try to scroll lines in WINDOW. OLD_PAGETOP is the pagetop of WINDOW before
517 having had its line starts recalculated. OLD_STARTS is the list of line
518 starts that used to appear in this window. OLD_COUNT is the number of lines
519 that appear in the OLD_STARTS array. */
520 void
521 display_scroll_line_starts (WINDOW *window, int old_pagetop,
522 char **old_starts, int old_count)
524 register int i, old, new; /* Indices into the line starts arrays. */
525 int last_new, last_old; /* Index of the last visible line. */
526 int old_first, new_first; /* Index of the first changed line. */
527 int unchanged_at_top = 0;
528 int already_scrolled = 0;
530 /* Locate the first line which was displayed on the old window. */
531 old_first = old_pagetop;
532 new_first = window->pagetop;
534 /* Find the last line currently visible in this window. */
535 last_new = window->pagetop + (window->height - 1);
536 if (last_new > window->line_count)
537 last_new = window->line_count - 1;
539 /* Find the last line which used to be currently visible in this window. */
540 last_old = old_pagetop + (window->height - 1);
541 if (last_old > old_count)
542 last_old = old_count - 1;
544 for (old = old_first, new = new_first;
545 old < last_old && new < last_new;
546 old++, new++)
547 if (old_starts[old] != window->line_starts[new])
548 break;
549 else
550 unchanged_at_top++;
552 /* Loop through the old lines looking for a match in the new lines. */
553 for (old = old_first + unchanged_at_top; old < last_old; old++)
555 for (new = new_first; new < last_new; new++)
556 if (old_starts[old] == window->line_starts[new])
558 /* Find the extent of the matching lines. */
559 for (i = 0; (old + i) < last_old; i++)
560 if (old_starts[old + i] != window->line_starts[new + i])
561 break;
563 /* Scroll these lines if there are enough of them. */
565 int start, end, amount;
567 start = (window->first_row
568 + ((old + already_scrolled) - old_pagetop));
569 amount = new - (old + already_scrolled);
570 end = window->first_row + window->height;
572 /* If we are shifting the block of lines down, then the last
573 AMOUNT lines will become invisible. Thus, don't bother
574 scrolling them. */
575 if (amount > 0)
576 end -= amount;
578 if ((end - start) > 0)
580 display_scroll_display (start, end, amount);
582 /* Some lines have been scrolled. Simulate the scrolling
583 by offsetting the value of the old index. */
584 old += i;
585 already_scrolled += amount;
592 /* Move the screen cursor to directly over the current character in WINDOW. */
593 void
594 display_cursor_at_point (WINDOW *window)
596 int vpos, hpos;
598 vpos = window_line_of_point (window) - window->pagetop + window->first_row;
599 hpos = window_get_cursor_column (window);
600 terminal_goto_xy (hpos, vpos);
601 fflush (stdout);
604 /* **************************************************************** */
605 /* */
606 /* Functions Static to this File */
607 /* */
608 /* **************************************************************** */
610 /* Make a DISPLAY_LINE ** with width and height. */
611 static DISPLAY_LINE **
612 make_display (int width, int height)
614 register int i;
615 DISPLAY_LINE **display;
617 display = (DISPLAY_LINE **)xmalloc ((1 + height) * sizeof (DISPLAY_LINE *));
619 for (i = 0; i < height; i++)
621 display[i] = (DISPLAY_LINE *)xmalloc (sizeof (DISPLAY_LINE));
622 display[i]->text = (char *)xmalloc (1 + width);
623 display[i]->textlen = 0;
624 display[i]->inverse = 0;
626 display[i] = (DISPLAY_LINE *)NULL;
627 return (display);
630 /* Free the storage allocated to DISPLAY. */
631 static void
632 free_display (DISPLAY_LINE **display)
634 register int i;
635 register DISPLAY_LINE *display_line;
637 if (!display)
638 return;
640 for (i = 0; (display_line = display[i]); i++)
642 free (display_line->text);
643 free (display_line);
645 free (display);