fdl.texi is needed.
[dragonfly.git] / contrib / texinfo-4 / info / display.c
1 /* display.c -- How to display Info windows.
2    $Id: display.c,v 1.7 2004/04/11 17:56:45 karl Exp $
3
4    Copyright (C) 1993, 1997, 2003, 2004 Free Software Foundation, Inc.
5
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.
10
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.
15
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.
19
20    Originally written by Brian Fox (bfox@ai.mit.edu). */
21
22 #include "info.h"
23 #include "display.h"
24
25 extern int info_any_buffered_input_p (void); /* Found in session.c. */
26
27 static void free_display (DISPLAY_LINE **display);
28 static DISPLAY_LINE **make_display (int width, int height);
29
30 void handle_tag (char *tag);
31 void handle_tag_start (char *tag);
32 void handle_tag_end (char *tag);
33
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;
37
38 /* Non-zero means do no output. */
39 int display_inhibited = 0;
40
41 /* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */
42 void
43 display_initialize_display (int width, int height)
44 {
45   free_display (the_display);
46   the_display = make_display (width, height);
47   display_clear_display (the_display);
48 }
49
50 /* Clear all of the lines in DISPLAY making the screen blank. */
51 void
52 display_clear_display (DISPLAY_LINE **display)
53 {
54   register int i;
55
56   for (i = 0; display[i]; i++)
57     {
58       display[i]->text[0] = '\0';
59       display[i]->textlen = 0;
60       display[i]->inverse = 0;
61     }
62 }
63
64 /* Non-zero if we didn't completely redisplay a window. */
65 int display_was_interrupted_p = 0;
66
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)
71 {
72   register WINDOW *win;
73
74   display_was_interrupted_p = 0;
75
76   /* For every window in the list, check contents against the display. */
77   for (win = window; win; win = win->next)
78     {
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;
84
85       display_update_one_window (win);
86       if (display_was_interrupted_p)
87         break;
88     }
89
90   /* Always update the echo area. */
91   display_update_one_window (the_echo_area);
92 }
93
94 void
95 handle_tag_start (char *tag)
96 {
97   /* TODO really handle this tag.  */
98   return;
99 }
100
101 void
102 handle_tag_end (char *tag)
103 {
104   /* TODO really handle this tag.  */
105   return;
106 }
107
108 void
109 handle_tag (char *tag)
110 {
111     if (tag[0] == '/')
112       {
113         tag++;
114         handle_tag_end (tag);
115       }
116     else
117       handle_tag_start (tag);
118 }
119
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)
124 {
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;
134
135   /* If display is inhibited, that counts as an interrupted display. */
136   if (display_inhibited)
137     display_was_interrupted_p = 1;
138
139   /* If the window has no height, or display is inhibited, quit now. */
140   if (!win->height || display_inhibited)
141     return;
142
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;
149
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);
155
156   if (!win->node || !win->line_starts)
157     goto done_with_node_display;
158
159   nodetext = win->line_starts[win->pagetop];
160   last_node_char = win->node->contents + win->node->nodelen;
161
162   for (; nodetext < last_node_char; nodetext++)
163     {
164       char *rep = NULL, *rep_carried_over, rep_temp[2];
165       int replen;
166
167       if (isprint (*nodetext))
168         {
169           rep_temp[0] = *nodetext;
170           replen = 1;
171           rep_temp[1] = '\0';
172           rep = rep_temp;
173         }
174       else
175         {
176           if (*nodetext == '\r' || *nodetext == '\n')
177             {
178               replen = win->width - pl_index + pl_ignore;
179             }
180           else if (*nodetext == '\0'
181                    && (nodetext + 2) < last_node_char
182                    && *(nodetext + 1) == '\b'
183                    && *(nodetext + 2) == '[')
184             {
185               /* Found new style tag/cookie \0\b[
186                  Read until the closing tag \0\b] */
187               int element_len = 0;
188               char *element;
189
190               /* Skip the escapes.  */
191               nodetext += 3;
192
193               while (!(*nodetext == '\0'
194                     && *(nodetext + 1) == '\b'
195                     && *(nodetext + 2) == ']'))
196                 {
197                   nodetext++;
198                   element_len++;
199                 }
200
201               element = (char *) malloc (element_len + 1);
202               strncpy (element, nodetext - element_len, element_len);
203
204               /* Skip the escapes.  */
205               nodetext += 2;
206               pl_ignore += element_len + 5;
207               /* Append string terminator.  */
208               element[element_len] = '\0';
209
210               handle_tag (element);
211
212               /* Over and out */
213               free (element);
214
215               continue;
216             }
217           else
218             {
219               rep = printed_representation (*nodetext, pl_index);
220               replen = strlen (rep);
221             }
222         }
223
224       /* Support ANSI escape sequences under -R.  */
225       if (raw_escapes_p
226           && *nodetext == '\033'
227           && nodetext[1] == '['
228           && isdigit (nodetext[2]))
229         {
230           if (nodetext[3] == 'm')
231             pl_ignore += 4;
232           else if (isdigit (nodetext[3]) && nodetext[4] == 'm')
233             pl_ignore += 5;
234         }
235       while (pl_index + 2 >= allocated_win_width - 1)
236         {
237           allocated_win_width *= 2;
238           printed_line = (char *)xrealloc (printed_line, allocated_win_width);
239         }
240
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)
244         {
245           /* Optimize if possible. */
246           if (replen == 1)
247             {
248               printed_line[pl_index++] = *rep;
249             }
250           else
251             {
252               for (i = 0; i < replen; i++)
253                 printed_line[pl_index++] = rep[i];
254             }
255         }
256       else
257         {
258           DISPLAY_LINE *entry;
259
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')
264             {
265               printed_line[pl_index] = '\0';
266               rep_carried_over = (char *)NULL;
267             }
268           else
269             {
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++];
276
277               rep_carried_over = rep + i;
278
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)
283                 {
284                   if (win->flags & W_NoWrap)
285                     printed_line[pl_index++] = '$';
286                   else
287                     printed_line[pl_index++] = '\\';
288                 }
289               printed_line[pl_index] = '\0';
290             }
291
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];
296
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))
304             {
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;
310             }
311
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;
316
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))
320             {
321               /* Move to the proper point on the terminal. */
322               terminal_goto_xy (i, line_index + win->first_row);
323
324               /* If there is any text to print, print it. */
325               if (i != pl_index)
326                 terminal_put_text (printed_line + i);
327
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 ();
335
336               fflush (stdout);
337
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;
348
349               /* Lines showing node text are not in inverse.  Only modelines
350                  have that distinction. */
351               entry->inverse = 0;
352             }
353
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;
358
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 ())
363             {
364               free (printed_line);
365               display_was_interrupted_p = 1;
366               return;
367             }
368
369           /* Reset PL_INDEX to the start of the line. */
370           pl_index = 0;
371           pl_ignore = 0;        /* this is computed per line */
372
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];
378
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))
382             {
383               char *begin;
384
385               pl_index = 0;
386               printed_line[0] = '\0';
387
388               begin = nodetext;
389
390               while ((nodetext < last_node_char) && (*nodetext != '\n'))
391                 nodetext++;
392             }
393         }
394     }
395
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++)
401     {
402       DISPLAY_LINE *entry = display[line_index + win->first_row];
403
404       /* If this line has text on it then make it go away. */
405       if (entry && entry->textlen)
406         {
407           entry->textlen = 0;
408           entry->text[0] = '\0';
409
410           terminal_goto_xy (0, line_index + win->first_row);
411           terminal_clear_to_eol ();
412         }
413     }
414
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)
419     {
420       window_make_modeline (win);
421       line_index = win->first_row + win->height;
422
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))
427         {
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);
436         }
437     }
438
439   /* Okay, this window doesn't need updating anymore. */
440   win->flags &= ~W_UpdateWindow;
441   free (printed_line);
442   fflush (stdout);
443 }
444
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)
452 {
453   register int i, last;
454   DISPLAY_LINE *temp;
455
456   /* If this terminal cannot do scrolling, give up now. */
457   if (!terminal_can_scroll)
458     return;
459
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;
464
465   /* If there is typeahead pending, then don't actually do any scrolling. */
466   if (info_any_buffered_input_p ())
467     return;
468
469   /* Do it on the screen. */
470   terminal_scroll_terminal (start, end, amount);
471
472   /* Now do it in the display buffer so our contents match the screen. */
473   if (amount > 0)
474     {
475       last = end + amount;
476
477       /* Shift the lines to scroll right into place. */
478       for (i = 0; i < (end - start); i++)
479         {
480           temp = the_display[last - i];
481           the_display[last - i] = the_display[end - i];
482           the_display[end - i] = temp;
483         }
484
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++)
488         {
489           the_display[i]->text[0] = '\0';
490           the_display[i]->textlen = 0;
491           the_display[i]->inverse = 0;
492         }
493     }
494
495   if (amount < 0)
496     {
497       last = start + amount;
498       for (i = 0; i < (end - start); i++)
499         {
500           temp = the_display[last + i];
501           the_display[last + i] = the_display[start + i];
502           the_display[start + i] = temp;
503         }
504
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++)
508         {
509           the_display[i]->text[0] = '\0';
510           the_display[i]->textlen = 0;
511           the_display[i]->inverse = 0;
512         }
513     }
514 }
515
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)
523 {
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;
529
530   /* Locate the first line which was displayed on the old window. */
531   old_first = old_pagetop;
532   new_first = window->pagetop;
533
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;
538
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;
543
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++;
551
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++)
554     {
555       for (new = new_first; new < last_new; new++)
556         if (old_starts[old] == window->line_starts[new])
557           {
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;
562
563             /* Scroll these lines if there are enough of them. */
564             {
565               int start, end, amount;
566
567               start = (window->first_row
568                        + ((old + already_scrolled) - old_pagetop));
569               amount = new - (old + already_scrolled);
570               end = window->first_row + window->height;
571
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;
577
578               if ((end - start) > 0)
579                 {
580                   display_scroll_display (start, end, amount);
581
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;
586                 }
587             }
588           }
589     }
590 }
591
592 /* Move the screen cursor to directly over the current character in WINDOW. */
593 void
594 display_cursor_at_point (WINDOW *window)
595 {
596   int vpos, hpos;
597
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);
602 }
603 \f
604 /* **************************************************************** */
605 /*                                                                  */
606 /*                   Functions Static to this File                  */
607 /*                                                                  */
608 /* **************************************************************** */
609
610 /* Make a DISPLAY_LINE ** with width and height. */
611 static DISPLAY_LINE **
612 make_display (int width, int height)
613 {
614   register int i;
615   DISPLAY_LINE **display;
616
617   display = (DISPLAY_LINE **)xmalloc ((1 + height) * sizeof (DISPLAY_LINE *));
618
619   for (i = 0; i < height; i++)
620     {
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;
625     }
626   display[i] = (DISPLAY_LINE *)NULL;
627   return (display);
628 }
629
630 /* Free the storage allocated to DISPLAY. */
631 static void
632 free_display (DISPLAY_LINE **display)
633 {
634   register int i;
635   register DISPLAY_LINE *display_line;
636
637   if (!display)
638     return;
639
640   for (i = 0; (display_line = display[i]); i++)
641     {
642       free (display_line->text);
643       free (display_line);
644     }
645   free (display);
646 }