No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / texinfo / info / window.c
blob9d95d7178de02fd871595bdb8910835283c79437
1 /* $NetBSD$ */
3 /* window.c -- windows in Info.
4 Id: window.c,v 1.4 2004/04/11 17:56:46 karl Exp
6 Copyright (C) 1993, 1997, 1998, 2001, 2002, 2003, 2004 Free Software
7 Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Written by Brian Fox (bfox@ai.mit.edu). */
25 #include "info.h"
26 #include "nodes.h"
27 #include "window.h"
28 #include "display.h"
29 #include "info-utils.h"
30 #include "infomap.h"
32 /* The window which describes the screen. */
33 WINDOW *the_screen = NULL;
35 /* The window which describes the echo area. */
36 WINDOW *the_echo_area = NULL;
38 /* The list of windows in Info. */
39 WINDOW *windows = NULL;
41 /* Pointer to the active window in WINDOW_LIST. */
42 WINDOW *active_window = NULL;
44 /* The size of the echo area in Info. It never changes, irregardless of the
45 size of the screen. */
46 #define ECHO_AREA_HEIGHT 1
48 /* Macro returns the amount of space that the echo area truly requires relative
49 to the entire screen. */
50 #define echo_area_required (1 + the_echo_area->height)
52 /* Initalize the window system by creating THE_SCREEN and THE_ECHO_AREA.
53 Create the first window ever.
54 You pass the dimensions of the total screen size. */
55 void
56 window_initialize_windows (int width, int height)
58 the_screen = xmalloc (sizeof (WINDOW));
59 the_echo_area = xmalloc (sizeof (WINDOW));
60 windows = xmalloc (sizeof (WINDOW));
61 active_window = windows;
63 zero_mem (the_screen, sizeof (WINDOW));
64 zero_mem (the_echo_area, sizeof (WINDOW));
65 zero_mem (active_window, sizeof (WINDOW));
67 /* None of these windows has a goal column yet. */
68 the_echo_area->goal_column = -1;
69 active_window->goal_column = -1;
70 the_screen->goal_column = -1;
72 /* The active and echo_area windows are visible.
73 The echo_area is permanent.
74 The screen is permanent. */
75 active_window->flags = W_WindowVisible;
76 the_echo_area->flags = W_WindowIsPerm | W_InhibitMode | W_WindowVisible;
77 the_screen->flags = W_WindowIsPerm;
79 /* The height of the echo area never changes. It is statically set right
80 here, and it must be at least 1 line for display. The size of the
81 initial window cannot be the same size as the screen, since the screen
82 includes the echo area. So, we make the height of the initial window
83 equal to the screen's displayable region minus the height of the echo
84 area. */
85 the_echo_area->height = ECHO_AREA_HEIGHT;
86 active_window->height = the_screen->height - 1 - the_echo_area->height;
87 window_new_screen_size (width, height);
89 /* The echo area uses a different keymap than normal info windows. */
90 the_echo_area->keymap = echo_area_keymap;
91 active_window->keymap = info_keymap;
94 /* Given that the size of the screen has changed to WIDTH and HEIGHT
95 from whatever it was before (found in the_screen->height, ->width),
96 change the size (and possibly location) of each window in the screen.
97 If a window would become too small, call the function DELETER on it,
98 after deleting the window from our chain of windows. If DELETER is NULL,
99 nothing extra is done. The last window can never be deleted, but it can
100 become invisible. */
102 /* If non-null, a function to call with WINDOW as argument when the function
103 window_new_screen_size () has deleted WINDOW. */
104 VFunction *window_deletion_notifier = NULL;
106 void
107 window_new_screen_size (int width, int height)
109 register WINDOW *win;
110 int delta_height, delta_each, delta_leftover;
111 int numwins;
113 /* If no change, do nothing. */
114 if (width == the_screen->width && height == the_screen->height)
115 return;
117 /* If the new window height is too small, make it be zero. */
118 if (height < (WINDOW_MIN_SIZE + the_echo_area->height))
119 height = 0;
120 if (width < 0)
121 width = 0;
123 /* Find out how many windows will change. */
124 for (numwins = 0, win = windows; win; win = win->next, numwins++);
126 /* See if some windows will need to be deleted. This is the case if
127 the screen is getting smaller, and the available space divided by
128 the number of windows is less than WINDOW_MIN_SIZE. In that case,
129 delete some windows and try again until there is either enough
130 space to divy up among the windows, or until there is only one
131 window left. */
132 while ((height - echo_area_required) / numwins <= WINDOW_MIN_SIZE)
134 /* If only one window, make the size of it be zero, and return
135 immediately. */
136 if (!windows->next)
138 windows->height = 0;
139 maybe_free (windows->line_starts);
140 windows->line_starts = NULL;
141 windows->line_count = 0;
142 break;
145 /* If we have some temporary windows, delete one of them. */
146 for (win = windows; win; win = win->next)
147 if (win->flags & W_TempWindow)
148 break;
150 /* Otherwise, delete the first window, and try again. */
151 if (!win)
152 win = windows;
154 if (window_deletion_notifier)
155 (*window_deletion_notifier) (win);
157 window_delete_window (win);
158 numwins--;
161 /* The screen has changed height and width. */
162 delta_height = height - the_screen->height; /* This is how much. */
163 the_screen->height = height; /* This is the new height. */
164 the_screen->width = width; /* This is the new width. */
166 /* Set the start of the echo area. */
167 the_echo_area->first_row = height - the_echo_area->height;
168 the_echo_area->width = width;
170 /* Check to see if the screen can really be changed this way. */
171 if ((!windows->next) && ((windows->height == 0) && (delta_height < 0)))
172 return;
174 /* Divide the change in height among the available windows. */
175 delta_each = delta_height / numwins;
176 delta_leftover = delta_height - (delta_each * numwins);
178 /* Change the height of each window in the chain by delta_each. Change
179 the height of the last window in the chain by delta_each and by the
180 leftover amount of change. Change the width of each window to be
181 WIDTH. */
182 for (win = windows; win; win = win->next)
184 if ((win->width != width) && ((win->flags & W_InhibitMode) == 0))
186 win->width = width;
187 maybe_free (win->modeline);
188 win->modeline = xmalloc (1 + width);
191 win->height += delta_each;
193 /* If the previous height of this window was zero, it was the only
194 window, and it was not visible. Thus we need to compensate for
195 the echo_area. */
196 if (win->height == delta_each)
197 win->height -= (1 + the_echo_area->height);
199 /* If this is not the first window in the chain, then change the
200 first row of it. We cannot just add delta_each to the first row,
201 since this window's first row is the sum of the collective increases
202 that have gone before it. So we just add one to the location of the
203 previous window's modeline. */
204 if (win->prev)
205 win->first_row = (win->prev->first_row + win->prev->height) + 1;
207 /* The last window in the chain gets the extra space (or shrinkage). */
208 if (!win->next)
209 win->height += delta_leftover;
211 if (win->node)
212 recalculate_line_starts (win);
214 win->flags |= W_UpdateWindow;
217 /* If the screen got smaller, check over the windows just shrunk to
218 keep them within bounds. Some of the windows may have gotten smaller
219 than WINDOW_MIN_HEIGHT in which case some of the other windows are
220 larger than the available display space in the screen. Because of our
221 intial test above, we know that there is enough space for all of the
222 windows. */
223 if ((delta_each < 0) && ((windows->height != 0) && windows->next))
225 int avail;
227 avail = the_screen->height - (numwins + the_echo_area->height);
228 win = windows;
230 while (win)
232 if ((win->height < WINDOW_MIN_HEIGHT) ||
233 (win->height > avail))
235 WINDOW *lastwin = NULL;
237 /* Split the space among the available windows. */
238 delta_each = avail / numwins;
239 delta_leftover = avail - (delta_each * numwins);
241 for (win = windows; win; win = win->next)
243 lastwin = win;
244 if (win->prev)
245 win->first_row =
246 (win->prev->first_row + win->prev->height) + 1;
247 win->height = delta_each;
250 /* Give the leftover space (if any) to the last window. */
251 lastwin->height += delta_leftover;
252 break;
254 else
255 win= win->next;
260 /* Make a new window showing NODE, and return that window structure.
261 If NODE is passed as NULL, then show the node showing in the active
262 window. If the window could not be made return a NULL pointer. The
263 active window is not changed.*/
264 WINDOW *
265 window_make_window (NODE *node)
267 WINDOW *window;
269 if (!node)
270 node = active_window->node;
272 /* If there isn't enough room to make another window, return now. */
273 if ((active_window->height / 2) < WINDOW_MIN_SIZE)
274 return (NULL);
276 /* Make and initialize the new window.
277 The fudging about with -1 and +1 is because the following window in the
278 chain cannot start at window->height, since that is where the modeline
279 for the previous window is displayed. The inverse adjustment is made
280 in window_delete_window (). */
281 window = xmalloc (sizeof (WINDOW));
282 window->width = the_screen->width;
283 window->height = (active_window->height / 2) - 1;
284 #if defined (SPLIT_BEFORE_ACTIVE)
285 window->first_row = active_window->first_row;
286 #else
287 window->first_row = active_window->first_row +
288 (active_window->height - window->height);
289 #endif
290 window->keymap = info_keymap;
291 window->goal_column = -1;
292 window->modeline = xmalloc (1 + window->width);
293 window->line_starts = NULL;
294 window->flags = W_UpdateWindow | W_WindowVisible;
295 window_set_node_of_window (window, node);
297 /* Adjust the height of the old active window. */
298 active_window->height -= (window->height + 1);
299 #if defined (SPLIT_BEFORE_ACTIVE)
300 active_window->first_row += (window->height + 1);
301 #endif
302 active_window->flags |= W_UpdateWindow;
304 /* Readjust the new and old windows so that their modelines and contents
305 will be displayed correctly. */
306 #if defined (NOTDEF)
307 /* We don't have to do this for WINDOW since window_set_node_of_window ()
308 already did. */
309 window_adjust_pagetop (window);
310 window_make_modeline (window);
311 #endif /* NOTDEF */
313 /* We do have to readjust the existing active window. */
314 window_adjust_pagetop (active_window);
315 window_make_modeline (active_window);
317 #if defined (SPLIT_BEFORE_ACTIVE)
318 /* This window is just before the active one. The active window gets
319 bumped down one. The active window is not changed. */
320 window->next = active_window;
322 window->prev = active_window->prev;
323 active_window->prev = window;
325 if (window->prev)
326 window->prev->next = window;
327 else
328 windows = window;
329 #else
330 /* This window is just after the active one. Which window is active is
331 not changed. */
332 window->prev = active_window;
333 window->next = active_window->next;
334 active_window->next = window;
335 if (window->next)
336 window->next->prev = window;
337 #endif /* !SPLIT_BEFORE_ACTIVE */
338 return (window);
341 /* These useful macros make it possible to read the code in
342 window_change_window_height (). */
343 #define grow_me_shrinking_next(me, next, diff) \
344 do { \
345 me->height += diff; \
346 next->height -= diff; \
347 next->first_row += diff; \
348 window_adjust_pagetop (next); \
349 } while (0)
351 #define grow_me_shrinking_prev(me, prev, diff) \
352 do { \
353 me->height += diff; \
354 prev->height -= diff; \
355 me->first_row -=diff; \
356 window_adjust_pagetop (prev); \
357 } while (0)
359 #define shrink_me_growing_next(me, next, diff) \
360 do { \
361 me->height -= diff; \
362 next->height += diff; \
363 next->first_row -= diff; \
364 window_adjust_pagetop (next); \
365 } while (0)
367 #define shrink_me_growing_prev(me, prev, diff) \
368 do { \
369 me->height -= diff; \
370 prev->height += diff; \
371 me->first_row += diff; \
372 window_adjust_pagetop (prev); \
373 } while (0)
375 /* Change the height of WINDOW by AMOUNT. This also automagically adjusts
376 the previous and next windows in the chain. If there is only one user
377 window, then no change takes place. */
378 void
379 window_change_window_height (WINDOW *window, int amount)
381 register WINDOW *win, *prev, *next;
383 /* If there is only one window, or if the amount of change is zero,
384 return immediately. */
385 if (!windows->next || amount == 0)
386 return;
388 /* Find this window in our chain. */
389 for (win = windows; win; win = win->next)
390 if (win == window)
391 break;
393 /* If the window is isolated (i.e., doesn't appear in our window list,
394 then quit now. */
395 if (!win)
396 return;
398 /* Change the height of this window by AMOUNT, if that is possible.
399 It can be impossible if there isn't enough available room on the
400 screen, or if the resultant window would be too small. */
402 prev = window->prev;
403 next = window->next;
405 /* WINDOW decreasing in size? */
406 if (amount < 0)
408 int abs_amount = -amount; /* It is easier to deal with this way. */
410 /* If the resultant window would be too small, stop here. */
411 if ((window->height - abs_amount) < WINDOW_MIN_HEIGHT)
412 return;
414 /* If we have two neighboring windows, choose the smaller one to get
415 larger. */
416 if (next && prev)
418 if (prev->height < next->height)
419 shrink_me_growing_prev (window, prev, abs_amount);
420 else
421 shrink_me_growing_next (window, next, abs_amount);
423 else if (next)
424 shrink_me_growing_next (window, next, abs_amount);
425 else
426 shrink_me_growing_prev (window, prev, abs_amount);
429 /* WINDOW increasing in size? */
430 if (amount > 0)
432 int total_avail, next_avail = 0, prev_avail = 0;
434 if (next)
435 next_avail = next->height - WINDOW_MIN_SIZE;
437 if (prev)
438 prev_avail = prev->height - WINDOW_MIN_SIZE;
440 total_avail = next_avail + prev_avail;
442 /* If there isn't enough space available to grow this window, give up. */
443 if (amount > total_avail)
444 return;
446 /* If there aren't two neighboring windows, or if one of the neighbors
447 is larger than the other one by at least AMOUNT, grow that one. */
448 if ((next && !prev) || ((next_avail - amount) >= prev_avail))
449 grow_me_shrinking_next (window, next, amount);
450 else if ((prev && !next) || ((prev_avail - amount) >= next_avail))
451 grow_me_shrinking_prev (window, prev, amount);
452 else
454 int change;
456 /* This window has two neighbors. They both must be shrunk in to
457 make enough space for WINDOW to grow. Make them both the same
458 size. */
459 if (prev_avail > next_avail)
461 change = prev_avail - next_avail;
462 grow_me_shrinking_prev (window, prev, change);
463 amount -= change;
465 else
467 change = next_avail - prev_avail;
468 grow_me_shrinking_next (window, next, change);
469 amount -= change;
472 /* Both neighbors are the same size. Split the difference in
473 AMOUNT between them. */
474 while (amount)
476 window->height++;
477 amount--;
479 /* Odd numbers grow next, even grow prev. */
480 if (amount & 1)
482 prev->height--;
483 window->first_row--;
485 else
487 next->height--;
488 next->first_row++;
491 window_adjust_pagetop (prev);
492 window_adjust_pagetop (next);
495 if (prev)
496 prev->flags |= W_UpdateWindow;
498 if (next)
499 next->flags |= W_UpdateWindow;
501 window->flags |= W_UpdateWindow;
502 window_adjust_pagetop (window);
505 /* Tile all of the windows currently displayed in the global variable
506 WINDOWS. If argument STYLE is TILE_INTERNALS, tile windows displaying
507 internal nodes as well, otherwise do not change the height of such
508 windows. */
509 void
510 window_tile_windows (int style)
512 WINDOW *win, *last_adjusted;
513 int numwins, avail, per_win_height, leftover;
514 int do_internals;
516 numwins = avail = 0;
517 do_internals = (style == TILE_INTERNALS);
519 for (win = windows; win; win = win->next)
520 if (do_internals || !win->node ||
521 (win->node->flags & N_IsInternal) == 0)
523 avail += win->height;
524 numwins++;
527 if (numwins <= 1 || !the_screen->height)
528 return;
530 /* Find the size for each window. Divide the size of the usable portion
531 of the screen by the number of windows. */
532 per_win_height = avail / numwins;
533 leftover = avail - (per_win_height * numwins);
535 last_adjusted = NULL;
536 for (win = windows; win; win = win->next)
538 if (do_internals || !win->node ||
539 (win->node->flags & N_IsInternal) == 0)
541 last_adjusted = win;
542 win->height = per_win_height;
546 if (last_adjusted)
547 last_adjusted->height += leftover;
549 /* Readjust the first_row of every window in the chain. */
550 for (win = windows; win; win = win->next)
552 if (win->prev)
553 win->first_row = win->prev->first_row + win->prev->height + 1;
555 window_adjust_pagetop (win);
556 win->flags |= W_UpdateWindow;
560 /* Toggle the state of line wrapping in WINDOW. This can do a bit of fancy
561 redisplay. */
562 void
563 window_toggle_wrap (WINDOW *window)
565 if (window->flags & W_NoWrap)
566 window->flags &= ~W_NoWrap;
567 else
568 window->flags |= W_NoWrap;
570 if (window != the_echo_area)
572 char **old_starts;
573 int old_lines, old_pagetop;
575 old_starts = window->line_starts;
576 old_lines = window->line_count;
577 old_pagetop = window->pagetop;
579 calculate_line_starts (window);
581 /* Make sure that point appears within this window. */
582 window_adjust_pagetop (window);
584 /* If the pagetop hasn't changed maybe we can do some scrolling now
585 to speed up the display. Many of the line starts will be the same,
586 so scrolling here is a very good optimization.*/
587 if (old_pagetop == window->pagetop)
588 display_scroll_line_starts
589 (window, old_pagetop, old_starts, old_lines);
590 maybe_free (old_starts);
592 window->flags |= W_UpdateWindow;
595 /* Set WINDOW to display NODE. */
596 void
597 window_set_node_of_window (WINDOW *window, NODE *node)
599 window->node = node;
600 window->pagetop = 0;
601 window->point = 0;
602 recalculate_line_starts (window);
603 window->flags |= W_UpdateWindow;
604 /* The display_pos member is nonzero if we're displaying an anchor. */
605 window->point = node ? node->display_pos : 0;
606 window_adjust_pagetop (window);
607 window_make_modeline (window);
610 /* Delete WINDOW from the list of known windows. If this window was the
611 active window, make the next window in the chain be the active window.
612 If the active window is the next or previous window, choose that window
613 as the recipient of the extra space. Otherwise, prefer the next window. */
614 void
615 window_delete_window (WINDOW *window)
617 WINDOW *next, *prev, *window_to_fix;
619 next = window->next;
620 prev = window->prev;
622 /* You cannot delete the only window or a permanent window. */
623 if ((!next && !prev) || (window->flags & W_WindowIsPerm))
624 return;
626 if (next)
627 next->prev = prev;
629 if (!prev)
630 windows = next;
631 else
632 prev->next = next;
634 if (window->line_starts)
635 free (window->line_starts);
637 if (window->modeline)
638 free (window->modeline);
640 if (window == active_window)
642 /* If there isn't a next window, then there must be a previous one,
643 since we cannot delete the last window. If there is a next window,
644 prefer to use that as the active window. */
645 if (next)
646 active_window = next;
647 else
648 active_window = prev;
651 if (next && active_window == next)
652 window_to_fix = next;
653 else if (prev && active_window == prev)
654 window_to_fix = prev;
655 else if (next)
656 window_to_fix = next;
657 else if (prev)
658 window_to_fix = prev;
659 else
660 window_to_fix = windows;
662 if (window_to_fix->first_row > window->first_row)
664 int diff;
666 /* Try to adjust the visible part of the node so that as little
667 text as possible has to move. */
668 diff = window_to_fix->first_row - window->first_row;
669 window_to_fix->first_row = window->first_row;
671 window_to_fix->pagetop -= diff;
672 if (window_to_fix->pagetop < 0)
673 window_to_fix->pagetop = 0;
676 /* The `+ 1' is to offset the difference between the first_row locations.
677 See the code in window_make_window (). */
678 window_to_fix->height += window->height + 1;
679 window_to_fix->flags |= W_UpdateWindow;
681 free (window);
684 /* For every window in CHAIN, set the flags member to have FLAG set. */
685 void
686 window_mark_chain (WINDOW *chain, int flag)
688 register WINDOW *win;
690 for (win = chain; win; win = win->next)
691 win->flags |= flag;
694 /* For every window in CHAIN, clear the flags member of FLAG. */
695 void
696 window_unmark_chain (WINDOW *chain, int flag)
698 register WINDOW *win;
700 for (win = chain; win; win = win->next)
701 win->flags &= ~flag;
704 /* Return the number of characters it takes to display CHARACTER on the
705 screen at HPOS. */
707 character_width (int character, int hpos)
709 int printable_limit = 127;
710 int width = 1;
712 if (ISO_Latin_p)
713 printable_limit = 255;
715 if (character > printable_limit)
716 width = 3;
717 else if (iscntrl (character))
719 switch (character)
721 case '\r':
722 case '\n':
723 width = the_screen->width - hpos;
724 break;
725 case '\t':
726 width = ((hpos + 8) & 0xf8) - hpos;
727 break;
728 default:
729 width = 2;
732 else if (character == DEL)
733 width = 2;
735 return (width);
738 /* Return the number of characters it takes to display STRING on the screen
739 at HPOS. */
741 string_width (char *string, int hpos)
743 register int i, width, this_char_width;
745 for (width = 0, i = 0; string[i]; i++)
747 /* Support ANSI escape sequences for -R. */
748 if (raw_escapes_p
749 && string[i] == '\033'
750 && string[i+1] == '['
751 && isdigit (string[i+2])
752 && (string[i+3] == 'm'
753 || (isdigit (string[i+3]) && string[i+4] == 'm')))
755 while (string[i] != 'm')
756 i++;
757 this_char_width = 0;
759 else
760 this_char_width = character_width (string[i], hpos);
761 width += this_char_width;
762 hpos += this_char_width;
764 return (width);
767 /* Quickly guess the approximate number of lines that NODE would
768 take to display. This really only counts carriage returns. */
770 window_physical_lines (NODE *node)
772 register int i, lines;
773 char *contents;
775 if (!node)
776 return (0);
778 contents = node->contents;
779 for (i = 0, lines = 1; i < node->nodelen; i++)
780 if (contents[i] == '\n')
781 lines++;
783 return (lines);
786 /* Calculate a list of line starts for the node belonging to WINDOW. The line
787 starts are pointers to the actual text within WINDOW->NODE. */
788 void
789 calculate_line_starts (WINDOW *window)
791 register int i, hpos;
792 char **line_starts = NULL;
793 int line_starts_index = 0, line_starts_slots = 0;
794 int bump_index;
795 NODE *node;
797 window->line_starts = NULL;
798 window->line_count = 0;
799 node = window->node;
801 if (!node)
802 return;
804 /* Grovel the node starting at the top, and for each line calculate the
805 width of the characters appearing in that line. Add each line start
806 to our array. */
807 i = 0;
808 hpos = 0;
809 bump_index = 0;
811 while (i < node->nodelen)
813 char *line = node->contents + i;
814 unsigned int cwidth, c;
816 add_pointer_to_array (line, line_starts_index, line_starts,
817 line_starts_slots, 100, char *);
818 if (bump_index)
820 i++;
821 bump_index = 0;
824 while (1)
826 /* The cast to unsigned char is for 8-bit characters, which
827 could be passed as negative integers to character_width
828 and wreak havoc on some naive implementations of iscntrl. */
829 c = (unsigned char) node->contents[i];
831 /* Support ANSI escape sequences for -R. */
832 if (raw_escapes_p
833 && c == '\033'
834 && node->contents[i+1] == '['
835 && isdigit (node->contents[i+2]))
837 if (node->contents[i+3] == 'm')
839 i += 3;
840 cwidth = 0;
842 else if (isdigit (node->contents[i+3])
843 && node->contents[i+4] == 'm')
845 i += 4;
846 cwidth = 0;
848 else
849 cwidth = character_width (c, hpos);
851 else
852 cwidth = character_width (c, hpos);
854 /* If this character fits within this line, just do the next one. */
855 if ((hpos + cwidth) < (unsigned int) window->width)
857 i++;
858 hpos += cwidth;
859 continue;
861 else
863 /* If this character would position the cursor at the start of
864 the next printed screen line, then do the next line. */
865 if (c == '\n' || c == '\r' || c == '\t')
867 i++;
868 hpos = 0;
869 break;
871 else
873 /* This character passes the window width border. Postion
874 the cursor after the printed character, but remember this
875 line start as where this character is. A bit tricky. */
877 /* If this window doesn't wrap lines, proceed to the next
878 physical line here. */
879 if (window->flags & W_NoWrap)
881 hpos = 0;
882 while (i < node->nodelen && node->contents[i] != '\n')
883 i++;
885 if (node->contents[i] == '\n')
886 i++;
888 else
890 hpos = the_screen->width - hpos;
891 bump_index++;
893 break;
898 window->line_starts = line_starts;
899 window->line_count = line_starts_index;
902 /* Given WINDOW, recalculate the line starts for the node it displays. */
903 void
904 recalculate_line_starts (WINDOW *window)
906 maybe_free (window->line_starts);
907 calculate_line_starts (window);
910 /* Global variable control redisplay of scrolled windows. If non-zero, it
911 is the desired number of lines to scroll the window in order to make
912 point visible. A user might set this to 1 for smooth scrolling. If
913 set to zero, the line containing point is centered within the window. */
914 int window_scroll_step = 0;
916 /* Adjust the pagetop of WINDOW such that the cursor point will be visible. */
917 void
918 window_adjust_pagetop (WINDOW *window)
920 register int line = 0;
921 char *contents;
923 if (!window->node)
924 return;
926 contents = window->node->contents;
928 /* Find the first printed line start which is after WINDOW->point. */
929 for (line = 0; line < window->line_count; line++)
931 char *line_start;
933 line_start = window->line_starts[line];
935 if ((line_start - contents) > window->point)
936 break;
939 /* The line index preceding the line start which is past point is the
940 one containing point. */
941 line--;
943 /* If this line appears in the current displayable page, do nothing.
944 Otherwise, adjust the top of the page to make this line visible. */
945 if ((line < window->pagetop) ||
946 (line - window->pagetop > (window->height - 1)))
948 /* The user-settable variable "scroll-step" is used to attempt
949 to make point visible, iff it is non-zero. If that variable
950 is zero, then the line containing point is centered within
951 the window. */
952 if (window_scroll_step < window->height)
954 if ((line < window->pagetop) &&
955 ((window->pagetop - window_scroll_step) <= line))
956 window->pagetop -= window_scroll_step;
957 else if ((line - window->pagetop > (window->height - 1)) &&
958 ((line - (window->pagetop + window_scroll_step)
959 < window->height)))
960 window->pagetop += window_scroll_step;
961 else
962 window->pagetop = line - ((window->height - 1) / 2);
964 else
965 window->pagetop = line - ((window->height - 1) / 2);
967 if (window->pagetop < 0)
968 window->pagetop = 0;
969 window->flags |= W_UpdateWindow;
973 /* Return the index of the line containing point. */
975 window_line_of_point (WINDOW *window)
977 register int i, start = 0;
979 /* Try to optimize. Check to see if point is past the pagetop for
980 this window, and if so, start searching forward from there. */
981 if ((window->pagetop > -1 && window->pagetop < window->line_count) &&
982 (window->line_starts[window->pagetop] - window->node->contents)
983 <= window->point)
984 start = window->pagetop;
986 for (i = start; i < window->line_count; i++)
988 if ((window->line_starts[i] - window->node->contents) > window->point)
989 break;
992 return (i - 1);
995 /* Get and return the goal column for this window. */
997 window_get_goal_column (WINDOW *window)
999 if (!window->node)
1000 return (-1);
1002 if (window->goal_column != -1)
1003 return (window->goal_column);
1005 /* Okay, do the work. Find the printed offset of the cursor
1006 in this window. */
1007 return (window_get_cursor_column (window));
1010 /* Get and return the printed column offset of the cursor in this window. */
1012 window_get_cursor_column (WINDOW *window)
1014 int i, hpos, end;
1015 char *line;
1017 i = window_line_of_point (window);
1019 if (i < 0)
1020 return (-1);
1022 line = window->line_starts[i];
1023 end = window->point - (line - window->node->contents);
1025 for (hpos = 0, i = 0; i < end; i++)
1027 /* Support ANSI escape sequences for -R. */
1028 if (raw_escapes_p
1029 && line[i] == '\033'
1030 && line[i+1] == '['
1031 && isdigit (line[i+2]))
1033 if (line[i+3] == 'm')
1034 i += 3;
1035 else if (isdigit (line[i+3]) && line[i+4] == 'm')
1036 i += 4;
1037 else
1038 hpos += character_width (line[i], hpos);
1040 else
1041 hpos += character_width (line[i], hpos);
1044 return (hpos);
1047 /* Count the number of characters in LINE that precede the printed column
1048 offset of GOAL. */
1050 window_chars_to_goal (char *line, int goal)
1052 register int i, check = 0, hpos;
1054 for (hpos = 0, i = 0; line[i] != '\n'; i++)
1056 /* Support ANSI escape sequences for -R. */
1057 if (raw_escapes_p
1058 && line[i] == '\033'
1059 && line[i+1] == '['
1060 && isdigit (line[i+2])
1061 && (line[i+3] == 'm'
1062 || (isdigit (line[i+3]) && line[i+4] == 'm')))
1063 while (line[i] != 'm')
1064 i++;
1065 else
1066 check = hpos + character_width (line[i], hpos);
1068 if (check > goal)
1069 break;
1071 hpos = check;
1073 return (i);
1076 /* Create a modeline for WINDOW, and store it in window->modeline. */
1077 void
1078 window_make_modeline (WINDOW *window)
1080 register int i;
1081 char *modeline;
1082 char location_indicator[4];
1083 int lines_remaining;
1085 /* Only make modelines for those windows which have one. */
1086 if (window->flags & W_InhibitMode)
1087 return;
1089 /* Find the number of lines actually displayed in this window. */
1090 lines_remaining = window->line_count - window->pagetop;
1092 if (window->pagetop == 0)
1094 if (lines_remaining <= window->height)
1095 strcpy (location_indicator, "All");
1096 else
1097 strcpy (location_indicator, "Top");
1099 else
1101 if (lines_remaining <= window->height)
1102 strcpy (location_indicator, "Bot");
1103 else
1105 float pt, lc;
1106 int percentage;
1108 pt = (float)window->pagetop;
1109 lc = (float)window->line_count;
1111 percentage = 100 * (pt / lc);
1113 sprintf (location_indicator, "%2d%%", percentage);
1117 /* Calculate the maximum size of the information to stick in MODELINE. */
1119 int modeline_len = 0;
1120 char *parent = NULL, *filename = "*no file*";
1121 char *nodename = "*no node*";
1122 const char *update_message = NULL;
1123 NODE *node = window->node;
1125 if (node)
1127 if (node->nodename)
1128 nodename = node->nodename;
1130 if (node->parent)
1132 parent = filename_non_directory (node->parent);
1133 modeline_len += strlen ("Subfile: ") + strlen (node->filename);
1136 if (node->filename)
1137 filename = filename_non_directory (node->filename);
1139 if (node->flags & N_UpdateTags)
1140 update_message = _("--*** Tags out of Date ***");
1143 if (update_message)
1144 modeline_len += strlen (update_message);
1145 modeline_len += strlen (filename);
1146 modeline_len += strlen (nodename);
1147 modeline_len += 4; /* strlen (location_indicator). */
1149 /* 10 for the decimal representation of the number of lines in this
1150 node, and the remainder of the text that can appear in the line. */
1151 modeline_len += 10 + strlen (_("-----Info: (), lines ----, "));
1152 modeline_len += window->width;
1154 modeline = xmalloc (1 + modeline_len);
1156 /* Special internal windows have no filename. */
1157 if (!parent && !*filename)
1158 sprintf (modeline, _("-%s---Info: %s, %d lines --%s--"),
1159 (window->flags & W_NoWrap) ? "$" : "-",
1160 nodename, window->line_count, location_indicator);
1161 else
1162 sprintf (modeline, _("-%s%s-Info: (%s)%s, %d lines --%s--"),
1163 (window->flags & W_NoWrap) ? "$" : "-",
1164 (node && (node->flags & N_IsCompressed)) ? "zz" : "--",
1165 parent ? parent : filename,
1166 nodename, window->line_count, location_indicator);
1168 if (parent)
1169 sprintf (modeline + strlen (modeline), _(" Subfile: %s"), filename);
1171 if (update_message)
1172 sprintf (modeline + strlen (modeline), "%s", update_message);
1174 i = strlen (modeline);
1176 if (i >= window->width)
1177 modeline[window->width] = '\0';
1178 else
1180 while (i < window->width)
1181 modeline[i++] = '-';
1182 modeline[i] = '\0';
1185 strcpy (window->modeline, modeline);
1186 free (modeline);
1190 /* Make WINDOW start displaying at PERCENT percentage of its node. */
1191 void
1192 window_goto_percentage (WINDOW *window, int percent)
1194 int desired_line;
1196 if (!percent)
1197 desired_line = 0;
1198 else
1199 desired_line =
1200 (int) ((float)window->line_count * ((float)percent / 100.0));
1202 window->pagetop = desired_line;
1203 window->point =
1204 window->line_starts[window->pagetop] - window->node->contents;
1205 window->flags |= W_UpdateWindow;
1206 window_make_modeline (window);
1209 /* Get the state of WINDOW, and save it in STATE. */
1210 void
1211 window_get_state (WINDOW *window, SEARCH_STATE *state)
1213 state->node = window->node;
1214 state->pagetop = window->pagetop;
1215 state->point = window->point;
1218 /* Set the node, pagetop, and point of WINDOW. */
1219 void
1220 window_set_state (WINDOW *window, SEARCH_STATE *state)
1222 if (window->node != state->node)
1223 window_set_node_of_window (window, state->node);
1224 window->pagetop = state->pagetop;
1225 window->point = state->point;
1229 /* Manipulating home-made nodes. */
1231 /* A place to buffer echo area messages. */
1232 static NODE *echo_area_node = NULL;
1234 /* Make the node of the_echo_area be an empty one. */
1235 static void
1236 free_echo_area (void)
1238 if (echo_area_node)
1240 maybe_free (echo_area_node->contents);
1241 free (echo_area_node);
1244 echo_area_node = NULL;
1245 window_set_node_of_window (the_echo_area, echo_area_node);
1248 /* Clear the echo area, removing any message that is already present.
1249 The echo area is cleared immediately. */
1250 void
1251 window_clear_echo_area (void)
1253 free_echo_area ();
1254 display_update_one_window (the_echo_area);
1257 /* Make a message appear in the echo area, built from FORMAT, ARG1 and ARG2.
1258 The arguments are treated similar to printf () arguments, but not all of
1259 printf () hair is present. The message appears immediately. If there was
1260 already a message appearing in the echo area, it is removed. */
1261 void
1262 window_message_in_echo_area (char *format, void *arg1, void *arg2)
1264 free_echo_area ();
1265 echo_area_node = build_message_node (format, arg1, arg2);
1266 window_set_node_of_window (the_echo_area, echo_area_node);
1267 display_update_one_window (the_echo_area);
1270 /* Place a temporary message in the echo area built from FORMAT, ARG1
1271 and ARG2. The message appears immediately, but does not destroy
1272 any existing message. A future call to unmessage_in_echo_area ()
1273 restores the old contents. */
1274 static NODE **old_echo_area_nodes = NULL;
1275 static int old_echo_area_nodes_index = 0;
1276 static int old_echo_area_nodes_slots = 0;
1278 void
1279 message_in_echo_area (char *format, void *arg1, void *arg2)
1281 if (echo_area_node)
1283 add_pointer_to_array (echo_area_node, old_echo_area_nodes_index,
1284 old_echo_area_nodes, old_echo_area_nodes_slots,
1285 4, NODE *);
1287 echo_area_node = NULL;
1288 window_message_in_echo_area (format, arg1, arg2);
1291 void
1292 unmessage_in_echo_area (void)
1294 free_echo_area ();
1296 if (old_echo_area_nodes_index)
1297 echo_area_node = old_echo_area_nodes[--old_echo_area_nodes_index];
1299 window_set_node_of_window (the_echo_area, echo_area_node);
1300 display_update_one_window (the_echo_area);
1303 /* A place to build a message. */
1304 static char *message_buffer = NULL;
1305 static int message_buffer_index = 0;
1306 static int message_buffer_size = 0;
1308 /* Ensure that there is enough space to stuff LENGTH characters into
1309 MESSAGE_BUFFER. */
1310 static void
1311 message_buffer_resize (int length)
1313 if (!message_buffer)
1315 message_buffer_size = length + 1;
1316 message_buffer = xmalloc (message_buffer_size);
1317 message_buffer_index = 0;
1320 while (message_buffer_size <= message_buffer_index + length)
1321 message_buffer = (char *)
1322 xrealloc (message_buffer,
1323 message_buffer_size += 100 + (2 * length));
1326 /* Format MESSAGE_BUFFER with the results of printing FORMAT with ARG1 and
1327 ARG2. */
1328 static void
1329 build_message_buffer (char *format, void *arg1, void *arg2, void *arg3)
1331 register int i, len;
1332 void *args[3];
1333 int arg_index = 0;
1335 args[0] = arg1;
1336 args[1] = arg2;
1337 args[2] = arg3;
1339 len = strlen (format);
1341 message_buffer_resize (len);
1343 for (i = 0; format[i]; i++)
1345 if (format[i] != '%')
1347 message_buffer[message_buffer_index++] = format[i];
1348 len--;
1350 else
1352 char c;
1353 char *fmt_start = format + i;
1354 char *fmt;
1355 int fmt_len, formatted_len;
1356 int paramed = 0;
1358 format_again:
1359 i++;
1360 while (format[i] && strchr ("-. +0123456789", format[i]))
1361 i++;
1362 c = format[i];
1364 if (c == '\0')
1365 abort ();
1367 if (c == '$') {
1368 /* position parameter parameter */
1369 /* better to use bprintf from bfox's metahtml? */
1370 arg_index = atoi(fmt_start + 1) - 1;
1371 if (arg_index < 0)
1372 arg_index = 0;
1373 if (arg_index >= 2)
1374 arg_index = 1;
1375 paramed = 1;
1376 goto format_again;
1379 fmt_len = format + i - fmt_start + 1;
1380 fmt = (char *) xmalloc (fmt_len + 1);
1381 strncpy (fmt, fmt_start, fmt_len);
1382 fmt[fmt_len] = '\0';
1384 if (paramed) {
1385 /* removed positioned parameter */
1386 char *p;
1387 for (p = fmt + 1; *p && *p != '$'; p++) {
1390 strcpy(fmt + 1, p + 1);
1393 /* If we have "%-98s", maybe 98 calls for a longer string. */
1394 if (fmt_len > 2)
1396 int j;
1398 for (j = fmt_len - 2; j >= 0; j--)
1399 if (isdigit (fmt[j]) || fmt[j] == '$')
1400 break;
1402 formatted_len = atoi (fmt + j);
1404 else
1405 formatted_len = c == 's' ? 0 : 1; /* %s can produce empty string */
1407 switch (c)
1409 case '%': /* Insert a percent sign. */
1410 message_buffer_resize (len + formatted_len);
1411 sprintf
1412 (message_buffer + message_buffer_index, fmt, "%");
1413 message_buffer_index += formatted_len;
1414 break;
1416 case 's': /* Insert the current arg as a string. */
1418 char *string;
1419 int string_len;
1421 string = (char *)args[arg_index++];
1422 string_len = strlen (string);
1424 if (formatted_len > string_len)
1425 string_len = formatted_len;
1426 message_buffer_resize (len + string_len);
1427 sprintf
1428 (message_buffer + message_buffer_index, fmt, string);
1429 message_buffer_index += string_len;
1431 break;
1433 case 'd': /* Insert the current arg as an integer. */
1435 long long_val;
1436 int integer;
1438 long_val = (long)args[arg_index++];
1439 integer = (int)long_val;
1441 message_buffer_resize (len + formatted_len > 32
1442 ? formatted_len : 32);
1443 sprintf
1444 (message_buffer + message_buffer_index, fmt, integer);
1445 message_buffer_index = strlen (message_buffer);
1447 break;
1449 case 'c': /* Insert the current arg as a character. */
1451 long long_val;
1452 int character;
1454 long_val = (long)args[arg_index++];
1455 character = (int)long_val;
1457 message_buffer_resize (len + formatted_len);
1458 sprintf
1459 (message_buffer + message_buffer_index, fmt, character);
1460 message_buffer_index += formatted_len;
1462 break;
1464 default:
1465 abort ();
1467 free (fmt);
1470 message_buffer[message_buffer_index] = '\0';
1473 /* Build a new node which has FORMAT printed with ARG1 and ARG2 as the
1474 contents. */
1475 NODE *
1476 build_message_node (char *format, void *arg1, void *arg2)
1478 NODE *node;
1480 message_buffer_index = 0;
1481 build_message_buffer (format, arg1, arg2, 0);
1483 node = message_buffer_to_node ();
1484 return (node);
1487 /* Convert the contents of the message buffer to a node. */
1488 NODE *
1489 message_buffer_to_node (void)
1491 NODE *node;
1493 node = xmalloc (sizeof (NODE));
1494 node->filename = NULL;
1495 node->parent = NULL;
1496 node->nodename = NULL;
1497 node->flags = 0;
1498 node->display_pos =0;
1500 /* Make sure that this buffer ends with a newline. */
1501 node->nodelen = 1 + strlen (message_buffer);
1502 node->contents = xmalloc (1 + node->nodelen);
1503 strcpy (node->contents, message_buffer);
1504 node->contents[node->nodelen - 1] = '\n';
1505 node->contents[node->nodelen] = '\0';
1506 return (node);
1509 /* Useful functions can be called from outside of window.c. */
1510 void
1511 initialize_message_buffer (void)
1513 message_buffer_index = 0;
1516 /* Print FORMAT with ARG1,2 to the end of the current message buffer. */
1517 void
1518 printf_to_message_buffer (char *format, void *arg1, void *arg2, void *arg3)
1520 build_message_buffer (format, arg1, arg2, arg3);
1523 /* Return the current horizontal position of the "cursor" on the most
1524 recently output message buffer line. */
1526 message_buffer_length_this_line (void)
1528 register int i;
1530 if (!message_buffer_index)
1531 return (0);
1533 for (i = message_buffer_index; i && message_buffer[i - 1] != '\n'; i--);
1535 return (string_width (message_buffer + i, 0));
1538 /* Pad STRING to COUNT characters by inserting blanks. */
1540 pad_to (int count, char *string)
1542 register int i;
1544 i = strlen (string);
1546 if (i >= count)
1547 string[i++] = ' ';
1548 else
1550 while (i < count)
1551 string[i++] = ' ';
1553 string[i] = '\0';
1555 return (i);