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
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)
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). */
29 #include "info-utils.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. */
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
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
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
;
107 window_new_screen_size (int width
, int height
)
109 register WINDOW
*win
;
110 int delta_height
, delta_each
, delta_leftover
;
113 /* If no change, do nothing. */
114 if (width
== the_screen
->width
&& height
== the_screen
->height
)
117 /* If the new window height is too small, make it be zero. */
118 if (height
< (WINDOW_MIN_SIZE
+ the_echo_area
->height
))
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
132 while ((height
- echo_area_required
) / numwins
<= WINDOW_MIN_SIZE
)
134 /* If only one window, make the size of it be zero, and return
139 maybe_free (windows
->line_starts
);
140 windows
->line_starts
= NULL
;
141 windows
->line_count
= 0;
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
)
150 /* Otherwise, delete the first window, and try again. */
154 if (window_deletion_notifier
)
155 (*window_deletion_notifier
) (win
);
157 window_delete_window (win
);
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)))
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
182 for (win
= windows
; win
; win
= win
->next
)
184 if ((win
->width
!= width
) && ((win
->flags
& W_InhibitMode
) == 0))
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
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. */
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). */
209 win
->height
+= delta_leftover
;
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
223 if ((delta_each
< 0) && ((windows
->height
!= 0) && windows
->next
))
227 avail
= the_screen
->height
- (numwins
+ the_echo_area
->height
);
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
)
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
;
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.*/
265 window_make_window (NODE
*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
)
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
;
287 window
->first_row
= active_window
->first_row
+
288 (active_window
->height
- window
->height
);
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);
302 active_window
->flags
|= W_UpdateWindow
;
304 /* Readjust the new and old windows so that their modelines and contents
305 will be displayed correctly. */
307 /* We don't have to do this for WINDOW since window_set_node_of_window ()
309 window_adjust_pagetop (window
);
310 window_make_modeline (window
);
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
;
326 window
->prev
->next
= window
;
330 /* This window is just after the active one. Which window is active is
332 window
->prev
= active_window
;
333 window
->next
= active_window
->next
;
334 active_window
->next
= window
;
336 window
->next
->prev
= window
;
337 #endif /* !SPLIT_BEFORE_ACTIVE */
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) \
345 me->height += diff; \
346 next->height -= diff; \
347 next->first_row += diff; \
348 window_adjust_pagetop (next); \
351 #define grow_me_shrinking_prev(me, prev, diff) \
353 me->height += diff; \
354 prev->height -= diff; \
355 me->first_row -=diff; \
356 window_adjust_pagetop (prev); \
359 #define shrink_me_growing_next(me, next, diff) \
361 me->height -= diff; \
362 next->height += diff; \
363 next->first_row -= diff; \
364 window_adjust_pagetop (next); \
367 #define shrink_me_growing_prev(me, prev, diff) \
369 me->height -= diff; \
370 prev->height += diff; \
371 me->first_row += diff; \
372 window_adjust_pagetop (prev); \
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. */
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)
388 /* Find this window in our chain. */
389 for (win
= windows
; win
; win
= win
->next
)
393 /* If the window is isolated (i.e., doesn't appear in our window list,
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. */
405 /* WINDOW decreasing in size? */
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
)
414 /* If we have two neighboring windows, choose the smaller one to get
418 if (prev
->height
< next
->height
)
419 shrink_me_growing_prev (window
, prev
, abs_amount
);
421 shrink_me_growing_next (window
, next
, abs_amount
);
424 shrink_me_growing_next (window
, next
, abs_amount
);
426 shrink_me_growing_prev (window
, prev
, abs_amount
);
429 /* WINDOW increasing in size? */
432 int total_avail
, next_avail
= 0, prev_avail
= 0;
435 next_avail
= next
->height
- WINDOW_MIN_SIZE
;
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
)
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
);
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
459 if (prev_avail
> next_avail
)
461 change
= prev_avail
- next_avail
;
462 grow_me_shrinking_prev (window
, prev
, change
);
467 change
= next_avail
- prev_avail
;
468 grow_me_shrinking_next (window
, next
, change
);
472 /* Both neighbors are the same size. Split the difference in
473 AMOUNT between them. */
479 /* Odd numbers grow next, even grow prev. */
491 window_adjust_pagetop (prev
);
492 window_adjust_pagetop (next
);
496 prev
->flags
|= W_UpdateWindow
;
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
510 window_tile_windows (int style
)
512 WINDOW
*win
, *last_adjusted
;
513 int numwins
, avail
, per_win_height
, leftover
;
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
;
527 if (numwins
<= 1 || !the_screen
->height
)
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)
542 win
->height
= per_win_height
;
547 last_adjusted
->height
+= leftover
;
549 /* Readjust the first_row of every window in the chain. */
550 for (win
= windows
; win
; win
= win
->next
)
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
563 window_toggle_wrap (WINDOW
*window
)
565 if (window
->flags
& W_NoWrap
)
566 window
->flags
&= ~W_NoWrap
;
568 window
->flags
|= W_NoWrap
;
570 if (window
!= the_echo_area
)
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. */
597 window_set_node_of_window (WINDOW
*window
, NODE
*node
)
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. */
615 window_delete_window (WINDOW
*window
)
617 WINDOW
*next
, *prev
, *window_to_fix
;
622 /* You cannot delete the only window or a permanent window. */
623 if ((!next
&& !prev
) || (window
->flags
& W_WindowIsPerm
))
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. */
646 active_window
= next
;
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
;
656 window_to_fix
= next
;
658 window_to_fix
= prev
;
660 window_to_fix
= windows
;
662 if (window_to_fix
->first_row
> window
->first_row
)
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
;
684 /* For every window in CHAIN, set the flags member to have FLAG set. */
686 window_mark_chain (WINDOW
*chain
, int flag
)
688 register WINDOW
*win
;
690 for (win
= chain
; win
; win
= win
->next
)
694 /* For every window in CHAIN, clear the flags member of FLAG. */
696 window_unmark_chain (WINDOW
*chain
, int flag
)
698 register WINDOW
*win
;
700 for (win
= chain
; win
; win
= win
->next
)
704 /* Return the number of characters it takes to display CHARACTER on the
707 character_width (int character
, int hpos
)
709 int printable_limit
= 127;
713 printable_limit
= 255;
715 if (character
> printable_limit
)
717 else if (iscntrl (character
))
723 width
= the_screen
->width
- hpos
;
726 width
= ((hpos
+ 8) & 0xf8) - hpos
;
732 else if (character
== DEL
)
738 /* Return the number of characters it takes to display STRING on the screen
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. */
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')
760 this_char_width
= character_width (string
[i
], hpos
);
761 width
+= this_char_width
;
762 hpos
+= this_char_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
;
778 contents
= node
->contents
;
779 for (i
= 0, lines
= 1; i
< node
->nodelen
; i
++)
780 if (contents
[i
] == '\n')
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. */
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;
797 window
->line_starts
= NULL
;
798 window
->line_count
= 0;
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
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 *);
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. */
834 && node
->contents
[i
+1] == '['
835 && isdigit (node
->contents
[i
+2]))
837 if (node
->contents
[i
+3] == 'm')
842 else if (isdigit (node
->contents
[i
+3])
843 && node
->contents
[i
+4] == 'm')
849 cwidth
= character_width (c
, hpos
);
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
)
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')
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
)
882 while (i
< node
->nodelen
&& node
->contents
[i
] != '\n')
885 if (node
->contents
[i
] == '\n')
890 hpos
= the_screen
->width
- hpos
;
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. */
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. */
918 window_adjust_pagetop (WINDOW
*window
)
920 register int line
= 0;
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
++)
933 line_start
= window
->line_starts
[line
];
935 if ((line_start
- contents
) > window
->point
)
939 /* The line index preceding the line start which is past point is the
940 one containing point. */
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
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
)
960 window
->pagetop
+= window_scroll_step
;
962 window
->pagetop
= line
- ((window
->height
- 1) / 2);
965 window
->pagetop
= line
- ((window
->height
- 1) / 2);
967 if (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
)
984 start
= window
->pagetop
;
986 for (i
= start
; i
< window
->line_count
; i
++)
988 if ((window
->line_starts
[i
] - window
->node
->contents
) > window
->point
)
995 /* Get and return the goal column for this window. */
997 window_get_goal_column (WINDOW
*window
)
1002 if (window
->goal_column
!= -1)
1003 return (window
->goal_column
);
1005 /* Okay, do the work. Find the printed offset of the cursor
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
)
1017 i
= window_line_of_point (window
);
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. */
1029 && line
[i
] == '\033'
1031 && isdigit (line
[i
+2]))
1033 if (line
[i
+3] == 'm')
1035 else if (isdigit (line
[i
+3]) && line
[i
+4] == 'm')
1038 hpos
+= character_width (line
[i
], hpos
);
1041 hpos
+= character_width (line
[i
], hpos
);
1047 /* Count the number of characters in LINE that precede the printed column
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. */
1058 && line
[i
] == '\033'
1060 && isdigit (line
[i
+2])
1061 && (line
[i
+3] == 'm'
1062 || (isdigit (line
[i
+3]) && line
[i
+4] == 'm')))
1063 while (line
[i
] != 'm')
1066 check
= hpos
+ character_width (line
[i
], hpos
);
1076 /* Create a modeline for WINDOW, and store it in window->modeline. */
1078 window_make_modeline (WINDOW
*window
)
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
)
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");
1097 strcpy (location_indicator
, "Top");
1101 if (lines_remaining
<= window
->height
)
1102 strcpy (location_indicator
, "Bot");
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
;
1128 nodename
= node
->nodename
;
1132 parent
= filename_non_directory (node
->parent
);
1133 modeline_len
+= strlen ("Subfile: ") + strlen (node
->filename
);
1137 filename
= filename_non_directory (node
->filename
);
1139 if (node
->flags
& N_UpdateTags
)
1140 update_message
= _("--*** Tags out of Date ***");
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
);
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
);
1169 sprintf (modeline
+ strlen (modeline
), _(" Subfile: %s"), filename
);
1172 sprintf (modeline
+ strlen (modeline
), "%s", update_message
);
1174 i
= strlen (modeline
);
1176 if (i
>= window
->width
)
1177 modeline
[window
->width
] = '\0';
1180 while (i
< window
->width
)
1181 modeline
[i
++] = '-';
1185 strcpy (window
->modeline
, modeline
);
1190 /* Make WINDOW start displaying at PERCENT percentage of its node. */
1192 window_goto_percentage (WINDOW
*window
, int percent
)
1200 (int) ((float)window
->line_count
* ((float)percent
/ 100.0));
1202 window
->pagetop
= desired_line
;
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. */
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. */
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. */
1236 free_echo_area (void)
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. */
1251 window_clear_echo_area (void)
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. */
1262 window_message_in_echo_area (char *format
, void *arg1
, void *arg2
)
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;
1279 message_in_echo_area (char *format
, void *arg1
, void *arg2
)
1283 add_pointer_to_array (echo_area_node
, old_echo_area_nodes_index
,
1284 old_echo_area_nodes
, old_echo_area_nodes_slots
,
1287 echo_area_node
= NULL
;
1288 window_message_in_echo_area (format
, arg1
, arg2
);
1292 unmessage_in_echo_area (void)
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
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
1329 build_message_buffer (char *format
, void *arg1
, void *arg2
, void *arg3
)
1331 register int i
, len
;
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
];
1353 char *fmt_start
= format
+ i
;
1355 int fmt_len
, formatted_len
;
1360 while (format
[i
] && strchr ("-. +0123456789", format
[i
]))
1368 /* position parameter parameter */
1369 /* better to use bprintf from bfox's metahtml? */
1370 arg_index
= atoi(fmt_start
+ 1) - 1;
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';
1385 /* removed positioned parameter */
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. */
1398 for (j
= fmt_len
- 2; j
>= 0; j
--)
1399 if (isdigit (fmt
[j
]) || fmt
[j
] == '$')
1402 formatted_len
= atoi (fmt
+ j
);
1405 formatted_len
= c
== 's' ? 0 : 1; /* %s can produce empty string */
1409 case '%': /* Insert a percent sign. */
1410 message_buffer_resize (len
+ formatted_len
);
1412 (message_buffer
+ message_buffer_index
, fmt
, "%");
1413 message_buffer_index
+= formatted_len
;
1416 case 's': /* Insert the current arg as a string. */
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
);
1428 (message_buffer
+ message_buffer_index
, fmt
, string
);
1429 message_buffer_index
+= string_len
;
1433 case 'd': /* Insert the current arg as an 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);
1444 (message_buffer
+ message_buffer_index
, fmt
, integer
);
1445 message_buffer_index
= strlen (message_buffer
);
1449 case 'c': /* Insert the current arg as a character. */
1454 long_val
= (long)args
[arg_index
++];
1455 character
= (int)long_val
;
1457 message_buffer_resize (len
+ formatted_len
);
1459 (message_buffer
+ message_buffer_index
, fmt
, character
);
1460 message_buffer_index
+= formatted_len
;
1470 message_buffer
[message_buffer_index
] = '\0';
1473 /* Build a new node which has FORMAT printed with ARG1 and ARG2 as the
1476 build_message_node (char *format
, void *arg1
, void *arg2
)
1480 message_buffer_index
= 0;
1481 build_message_buffer (format
, arg1
, arg2
, 0);
1483 node
= message_buffer_to_node ();
1487 /* Convert the contents of the message buffer to a node. */
1489 message_buffer_to_node (void)
1493 node
= xmalloc (sizeof (NODE
));
1494 node
->filename
= NULL
;
1495 node
->parent
= NULL
;
1496 node
->nodename
= NULL
;
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';
1509 /* Useful functions can be called from outside of window.c. */
1511 initialize_message_buffer (void)
1513 message_buffer_index
= 0;
1516 /* Print FORMAT with ARG1,2 to the end of the current message buffer. */
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)
1530 if (!message_buffer_index
)
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
)
1544 i
= strlen (string
);