1 /* echo-area.c -- how to read a line in the echo area.
2 $Id: echo-area.c,v 1.15 2001/12/12 16:19:39 karl Exp $
4 Copyright (C) 1993, 97, 98, 99, 2001 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Written by Brian Fox (bfox@ai.mit.edu). */
26 # define fd_set_cast(x) (int *)(x)
28 # define fd_set_cast(x) (fd_set *)(x)
32 /* Non-zero means that C-g was used to quit reading input. */
33 int info_aborted_echo_area = 0;
35 /* Non-zero means that the echo area is being used to read input. */
36 int echo_area_is_active = 0;
38 /* The address of the last command executed in the echo area. */
39 VFunction *ea_last_executed_command = (VFunction *)NULL;
41 /* Non-zero means that the last command executed while reading input
43 int echo_area_last_command_was_kill = 0;
45 /* Variables which hold on to the current state of the input line. */
46 static char input_line[1 + EA_MAX_INPUT];
47 static char *input_line_prompt;
48 static int input_line_point;
49 static int input_line_beg;
50 static int input_line_end;
51 static NODE input_line_node = {
52 (char *)NULL, (char *)NULL, (char *)NULL, input_line, EA_MAX_INPUT, 0
55 static void echo_area_initialize_node ();
56 static void push_echo_area (), pop_echo_area ();
57 static int echo_area_stack_contains_completions_p ();
59 static void ea_kill_text ();
61 /* Non-zero means we force the user to complete. */
62 static int echo_area_must_complete_p = 0;
63 static int completions_window_p ();
65 /* If non-null, this is a window which was specifically created to display
66 possible completions output. We remember it so we can delete it when
68 static WINDOW *echo_area_completions_window = (WINDOW *)NULL;
70 /* Variables which keep track of the window which was active prior to
71 entering the echo area. */
72 static WINDOW *calling_window = (WINDOW *)NULL;
73 static NODE *calling_window_node = (NODE *)NULL;
74 static long calling_window_point = 0;
75 static long calling_window_pagetop = 0;
77 /* Remember the node and pertinent variables of the calling window. */
79 remember_calling_window (window)
82 /* Only do this if the calling window is not the completions window, or,
83 if it is the completions window and there is no other window. */
84 if (!completions_window_p (window) ||
85 ((window == windows) && !(window->next)))
87 calling_window = window;
88 calling_window_node = window->node;
89 calling_window_point = window->point;
90 calling_window_pagetop = window->pagetop;
94 /* Restore the caller's window so that it shows the node that it was showing
95 on entry to info_read_xxx_echo_area (). */
97 restore_calling_window ()
99 register WINDOW *win, *compwin = (WINDOW *)NULL;
101 /* If the calling window is still visible, and it is the window that
102 we used for completions output, then restore the calling window. */
103 for (win = windows; win; win = win->next)
105 if (completions_window_p (win))
108 if (win == calling_window && win == compwin)
110 window_set_node_of_window (calling_window, calling_window_node);
111 calling_window->point = calling_window_point;
112 calling_window->pagetop = calling_window_pagetop;
113 compwin = (WINDOW *)NULL;
118 /* Delete the completions window if it is still present, it isn't the
119 last window on the screen, and there aren't any prior echo area reads
120 pending which created a completions window. */
123 if ((compwin != windows || windows->next) &&
124 !echo_area_stack_contains_completions_p ())
127 int pagetop, start, end, amount;
129 next = compwin->next;
132 start = next->first_row;
133 end = start + next->height;
134 amount = - (compwin->height + 1);
135 pagetop = next->pagetop;
138 info_delete_window_internal (compwin);
140 /* This is not necessary because info_delete_window_internal ()
141 calls echo_area_inform_of_deleted_window (), which does the
143 #if defined (UNNECESSARY)
144 echo_area_completions_window = (WINDOW *)NULL;
145 #endif /* UNNECESSARY */
149 display_scroll_display (start, end, amount);
150 next->pagetop = pagetop;
151 display_update_display (windows);
157 /* Set up a new input line with PROMPT. */
159 initialize_input_line (prompt)
162 input_line_prompt = prompt;
164 strcpy (input_line, prompt);
166 input_line[0] = '\0';
168 input_line_beg = input_line_end = input_line_point = strlen (prompt);
172 echo_area_after_read ()
176 if (info_aborted_echo_area)
178 info_aborted_echo_area = 0;
179 return_value = (char *)NULL;
183 if (input_line_beg == input_line_end)
184 return_value = xstrdup ("");
187 int line_len = input_line_end - input_line_beg;
188 return_value = (char *) xmalloc (1 + line_len);
189 strncpy (return_value, &input_line[input_line_beg], line_len);
190 return_value[line_len] = '\0';
193 return (return_value);
196 /* Read a line of text in the echo area. Return a malloc ()'ed string,
197 or NULL if the user aborted out of this read. WINDOW is the currently
198 active window, so that we can restore it when we need to. PROMPT, if
199 non-null, is a prompt to print before reading the line. */
201 info_read_in_echo_area (window, prompt)
207 /* If the echo area is already active, remember the current state. */
208 if (echo_area_is_active)
211 /* Initialize our local variables. */
212 initialize_input_line (prompt);
214 /* Initialize the echo area for the first (but maybe not the last) time. */
215 echo_area_initialize_node ();
217 /* Save away the original node of this window, and the window itself,
218 so echo area commands can temporarily use this window. */
219 remember_calling_window (window);
221 /* Let the rest of Info know that the echo area is active. */
222 echo_area_is_active++;
223 active_window = the_echo_area;
225 /* Read characters in the echo area. */
226 info_read_and_dispatch ();
228 echo_area_is_active--;
230 /* Restore the original active window and show point in it. */
231 active_window = calling_window;
232 restore_calling_window ();
233 display_cursor_at_point (active_window);
236 /* Get the value of the line. */
237 line = echo_area_after_read ();
239 /* If there is a previous loop waiting for us, restore it now. */
240 if (echo_area_is_active)
243 /* Return the results to the caller. */
247 /* (re) Initialize the echo area node. */
249 echo_area_initialize_node ()
253 for (i = input_line_end; i < sizeof (input_line); i++)
256 input_line[i - 1] = '\n';
257 window_set_node_of_window (the_echo_area, &input_line_node);
258 input_line[input_line_end] = '\n';
261 /* Prepare to read characters in the echo area. This can initialize the
262 echo area node, but its primary purpose is to side effect the input
263 line buffer contents. */
265 echo_area_prep_read ()
267 if (the_echo_area->node != &input_line_node)
268 echo_area_initialize_node ();
270 the_echo_area->point = input_line_point;
271 input_line[input_line_end] = '\n';
272 display_update_one_window (the_echo_area);
273 display_cursor_at_point (active_window);
277 /* **************************************************************** */
279 /* Echo Area Movement Commands */
281 /* **************************************************************** */
283 DECLARE_INFO_COMMAND (ea_forward, _("Move forward a character"))
286 ea_backward (window, -count, key);
289 input_line_point += count;
290 if (input_line_point > input_line_end)
291 input_line_point = input_line_end;
295 DECLARE_INFO_COMMAND (ea_backward, _("Move backward a character"))
298 ea_forward (window, -count, key);
301 input_line_point -= count;
302 if (input_line_point < input_line_beg)
303 input_line_point = input_line_beg;
307 DECLARE_INFO_COMMAND (ea_beg_of_line, _("Move to the start of this line"))
309 input_line_point = input_line_beg;
312 DECLARE_INFO_COMMAND (ea_end_of_line, _("Move to the end of this line"))
314 input_line_point = input_line_end;
317 #define alphabetic(c) (islower (c) || isupper (c) || isdigit (c))
319 /* Move forward a word in the input line. */
320 DECLARE_INFO_COMMAND (ea_forward_word, _("Move forward a word"))
325 ea_backward_word (window, -count, key);
330 if (input_line_point == input_line_end)
333 /* If we are not in a word, move forward until we are in one.
334 Then, move forward until we hit a non-alphabetic character. */
335 c = input_line[input_line_point];
339 while (++input_line_point < input_line_end)
341 c = input_line[input_line_point];
347 if (input_line_point == input_line_end)
350 while (++input_line_point < input_line_end)
352 c = input_line[input_line_point];
360 DECLARE_INFO_COMMAND (ea_backward_word, _("Move backward a word"))
365 ea_forward_word (window, -count, key);
370 if (input_line_point == input_line_beg)
373 /* Like ea_forward_word (), except that we look at the
374 characters just before point. */
376 c = input_line[input_line_point - 1];
380 while ((--input_line_point) != input_line_beg)
382 c = input_line[input_line_point - 1];
388 while (input_line_point != input_line_beg)
390 c = input_line[input_line_point - 1];
400 DECLARE_INFO_COMMAND (ea_delete, _("Delete the character under the cursor"))
405 ea_rubout (window, -count, key);
408 if (input_line_point == input_line_end)
411 if (info_explicit_arg || count > 1)
415 orig_point = input_line_point;
416 ea_forward (window, count, key);
417 ea_kill_text (orig_point, input_line_point);
418 input_line_point = orig_point;
422 for (i = input_line_point; i < input_line_end; i++)
423 input_line[i] = input_line[i + 1];
430 DECLARE_INFO_COMMAND (ea_rubout, _("Delete the character behind the cursor"))
433 ea_delete (window, -count, key);
438 if (input_line_point == input_line_beg)
441 start = input_line_point;
442 ea_backward (window, count, key);
444 if (info_explicit_arg || count > 1)
445 ea_kill_text (start, input_line_point);
447 ea_delete (window, count, key);
451 DECLARE_INFO_COMMAND (ea_abort, _("Cancel or quit operation"))
453 /* If any text, just discard it, and restore the calling window's node.
455 if (input_line_end != input_line_beg)
457 terminal_ring_bell ();
458 input_line_end = input_line_point = input_line_beg;
459 if (calling_window->node != calling_window_node)
460 restore_calling_window ();
463 info_aborted_echo_area = 1;
466 DECLARE_INFO_COMMAND (ea_newline, _("Accept (or force completion of) this line"))
468 /* Stub does nothing. Simply here to see if it has been executed. */
471 DECLARE_INFO_COMMAND (ea_quoted_insert, _("Insert next character verbatim"))
473 unsigned char character;
475 character = info_get_another_input_char ();
476 ea_insert (window, count, character);
479 DECLARE_INFO_COMMAND (ea_insert, _("Insert this character"))
483 if ((input_line_end + 1) == EA_MAX_INPUT)
485 terminal_ring_bell ();
489 for (i = input_line_end + 1; i != input_line_point; i--)
490 input_line[i] = input_line[i - 1];
492 input_line[input_line_point] = key;
497 DECLARE_INFO_COMMAND (ea_tab_insert, _("Insert a TAB character"))
499 ea_insert (window, count, '\t');
502 /* Transpose the characters at point. If point is at the end of the line,
503 then transpose the characters before point. */
504 DECLARE_INFO_COMMAND (ea_transpose_chars, _("Transpose characters at point"))
506 /* Handle conditions that would make it impossible to transpose
508 if (!count || !input_line_point || (input_line_end - input_line_beg) < 2)
514 if (input_line_point == input_line_end)
516 t = input_line[input_line_point - 1];
518 input_line[input_line_point - 1] = input_line[input_line_point - 2];
519 input_line[input_line_point - 2] = t;
523 t = input_line[input_line_point];
525 input_line[input_line_point] = input_line[input_line_point - 1];
526 input_line[input_line_point - 1] = t;
528 if (count < 0 && input_line_point != input_line_beg)
541 /* **************************************************************** */
543 /* Echo Area Killing and Yanking */
545 /* **************************************************************** */
547 static char **kill_ring = (char **)NULL;
548 static int kill_ring_index = 0; /* Number of kills appearing in KILL_RING. */
549 static int kill_ring_slots = 0; /* Number of slots allocated to KILL_RING. */
550 static int kill_ring_loc = 0; /* Location of current yank pointer. */
552 /* The largest number of kills that we remember at one time. */
553 static int max_retained_kills = 15;
555 DECLARE_INFO_COMMAND (ea_yank, _("Yank back the contents of the last kill"))
560 if (!kill_ring_index)
562 inform_in_echo_area (_("Kill ring is empty"));
566 text = kill_ring[kill_ring_loc];
568 for (i = 0; text[i]; i++)
569 ea_insert (window, 1, text[i]);
572 /* If the last command was yank, or yank_pop, and the text just before
573 point is identical to the current kill item, then delete that text
574 from the line, rotate the index down, and yank back some other text. */
575 DECLARE_INFO_COMMAND (ea_yank_pop, _("Yank back a previous kill"))
579 if (((ea_last_executed_command != ea_yank) &&
580 (ea_last_executed_command != ea_yank_pop)) ||
581 (kill_ring_index == 0))
584 len = strlen (kill_ring[kill_ring_loc]);
586 /* Delete the last yanked item from the line. */
588 register int i, counter;
590 counter = input_line_end - input_line_point;
592 for (i = input_line_point - len; counter; i++, counter--)
593 input_line[i] = input_line[i + len];
595 input_line_end -= len;
596 input_line_point -= len;
599 /* Get a previous kill, and yank that. */
601 if (kill_ring_loc < 0)
602 kill_ring_loc = kill_ring_index - 1;
604 ea_yank (window, count, key);
607 /* Delete the text from point to end of line. */
608 DECLARE_INFO_COMMAND (ea_kill_line, _("Kill to the end of the line"))
612 ea_kill_text (input_line_point, input_line_beg);
613 input_line_point = input_line_beg;
616 ea_kill_text (input_line_point, input_line_end);
619 /* Delete the text from point to beg of line. */
620 DECLARE_INFO_COMMAND (ea_backward_kill_line,
621 _("Kill to the beginning of the line"))
624 ea_kill_text (input_line_point, input_line_end);
627 ea_kill_text (input_line_point, input_line_beg);
628 input_line_point = input_line_beg;
632 /* Delete from point to the end of the current word. */
633 DECLARE_INFO_COMMAND (ea_kill_word, _("Kill the word following the cursor"))
635 int orig_point = input_line_point;
638 ea_backward_kill_word (window, -count, key);
641 ea_forward_word (window, count, key);
643 if (input_line_point != orig_point)
644 ea_kill_text (orig_point, input_line_point);
646 input_line_point = orig_point;
650 /* Delete from point to the start of the current word. */
651 DECLARE_INFO_COMMAND (ea_backward_kill_word,
652 _("Kill the word preceding the cursor"))
654 int orig_point = input_line_point;
657 ea_kill_word (window, -count, key);
660 ea_backward_word (window, count, key);
662 if (input_line_point != orig_point)
663 ea_kill_text (orig_point, input_line_point);
667 /* The way to kill something. This appends or prepends to the last
668 kill, if the last command was a kill command. If FROM is less
669 than TO, then the killed text is appended to the most recent kill,
670 otherwise it is prepended. If the last command was not a kill command,
671 then a new slot is made for this kill. */
673 ea_kill_text (from, to)
676 register int i, counter, distance;
677 int killing_backwards, slot;
680 killing_backwards = (from > to);
682 /* If killing backwards, reverse the values of FROM and TO. */
683 if (killing_backwards)
690 /* Remember the text that we are about to delete. */
691 distance = to - from;
692 killed_text = (char *)xmalloc (1 + distance);
693 strncpy (killed_text, &input_line[from], distance);
694 killed_text[distance] = '\0';
696 /* Actually delete the text from the line. */
697 counter = input_line_end - to;
699 for (i = from; counter; i++, counter--)
700 input_line[i] = input_line[i + distance];
702 input_line_end -= distance;
704 /* If the last command was a kill, append or prepend the killed text to
705 the last command's killed text. */
706 if (echo_area_last_command_was_kill)
710 slot = kill_ring_loc;
711 old = kill_ring[slot];
712 new = (char *)xmalloc (1 + strlen (old) + strlen (killed_text));
714 if (killing_backwards)
716 /* Prepend TEXT to current kill. */
717 strcpy (new, killed_text);
722 /* Append TEXT to current kill. */
724 strcat (new, killed_text);
729 kill_ring[slot] = new;
733 /* Try to store the kill in a new slot, unless that would cause there
734 to be too many remembered kills. */
735 slot = kill_ring_index;
737 if (slot == max_retained_kills)
740 if (slot + 1 > kill_ring_slots)
741 kill_ring = (char **) xrealloc
743 (kill_ring_slots += max_retained_kills) * sizeof (char *));
745 if (slot != kill_ring_index)
746 free (kill_ring[slot]);
750 kill_ring[slot] = killed_text;
752 kill_ring_loc = slot;
755 /* Notice that the last command was a kill. */
756 echo_area_last_command_was_kill++;
759 /* **************************************************************** */
761 /* Echo Area Completion */
763 /* **************************************************************** */
765 /* Pointer to an array of REFERENCE to complete over. */
766 static REFERENCE **echo_area_completion_items = (REFERENCE **)NULL;
768 /* Sorted array of REFERENCE * which is the possible completions found in
769 the variable echo_area_completion_items. If there is only one element,
770 it is the only possible completion. */
771 static REFERENCE **completions_found = (REFERENCE **)NULL;
772 static int completions_found_index = 0;
773 static int completions_found_slots = 0;
775 /* The lowest common denominator found while completing. */
776 static REFERENCE *LCD_completion;
778 /* Internal functions used by the user calls. */
779 static void build_completions (), completions_must_be_rebuilt ();
781 /* Variable which holds the output of completions. */
782 static NODE *possible_completions_output_node = (NODE *)NULL;
784 static char *compwin_name = "*Completions*";
786 /* Return non-zero if WINDOW is a window used for completions output. */
788 completions_window_p (window)
793 if (internal_info_node_p (window->node) &&
794 (strcmp (window->node->nodename, compwin_name) == 0))
800 /* Workhorse for completion readers. If FORCE is non-zero, the user cannot
801 exit unless the line read completes, or is empty. */
803 info_read_completing_internal (window, prompt, completions, force)
806 REFERENCE **completions;
811 /* If the echo area is already active, remember the current state. */
812 if (echo_area_is_active)
815 echo_area_must_complete_p = force;
817 /* Initialize our local variables. */
818 initialize_input_line (prompt);
820 /* Initialize the echo area for the first (but maybe not the last) time. */
821 echo_area_initialize_node ();
823 /* Save away the original node of this window, and the window itself,
824 so echo area commands can temporarily use this window. */
825 remember_calling_window (window);
827 /* Save away the list of items to complete over. */
828 echo_area_completion_items = completions;
829 completions_must_be_rebuilt ();
831 active_window = the_echo_area;
832 echo_area_is_active++;
834 /* Read characters in the echo area. */
837 info_read_and_dispatch ();
839 line = echo_area_after_read ();
841 /* Force the completion to take place if the user hasn't accepted
842 a default or aborted, and if FORCE is active. */
843 if (force && line && *line && completions)
847 build_completions ();
849 /* If there is only one completion, then make the line be that
851 if (completions_found_index == 1)
854 line = xstrdup (completions_found[0]->label);
858 /* If one of the completions matches exactly, then that is okay, so
859 return the current line. */
860 for (i = 0; i < completions_found_index; i++)
861 if (strcasecmp (completions_found[i]->label, line) == 0)
864 line = xstrdup (completions_found[i]->label);
868 /* If no match, go back and try again. */
869 if (i == completions_found_index)
871 if (!completions_found_index)
872 inform_in_echo_area (_("No completions"));
874 inform_in_echo_area (_("Not complete"));
880 echo_area_is_active--;
882 /* Restore the original active window and show point in it. */
883 active_window = calling_window;
884 restore_calling_window ();
885 display_cursor_at_point (active_window);
888 echo_area_completion_items = (REFERENCE **)NULL;
889 completions_must_be_rebuilt ();
891 /* If there is a previous loop waiting for us, restore it now. */
892 if (echo_area_is_active)
898 /* Read a line in the echo area with completion over COMPLETIONS. */
900 info_read_completing_in_echo_area (window, prompt, completions)
903 REFERENCE **completions;
905 return (info_read_completing_internal (window, prompt, completions, 1));
908 /* Read a line in the echo area allowing completion over COMPLETIONS, but
911 info_read_maybe_completing (window, prompt, completions)
914 REFERENCE **completions;
916 return (info_read_completing_internal (window, prompt, completions, 0));
919 DECLARE_INFO_COMMAND (ea_possible_completions, _("List possible completions"))
921 if (!echo_area_completion_items)
923 ea_insert (window, count, key);
927 build_completions ();
929 if (!completions_found_index)
931 terminal_ring_bell ();
932 inform_in_echo_area (_("No completions"));
934 else if ((completions_found_index == 1) && (key != '?'))
936 inform_in_echo_area (_("Sole completion"));
941 int limit, count, max_label = 0;
943 initialize_message_buffer ();
944 printf_to_message_buffer (completions_found_index == 1
945 ? _("One completion:\n")
946 : _("%d completions:\n"),
947 completions_found_index);
949 /* Find the maximum length of a label. */
950 for (i = 0; i < completions_found_index; i++)
952 int len = strlen (completions_found[i]->label);
959 /* Find out how many columns we should print in. */
960 limit = calling_window->width / max_label;
961 if (limit != 1 && (limit * max_label == calling_window->width))
964 /* Avoid a possible floating exception. If max_label > width then
965 the limit will be 0 and a divide-by-zero fault will result. */
969 /* How many iterations of the printing loop? */
970 count = (completions_found_index + (limit - 1)) / limit;
972 /* Watch out for special case. If the number of completions is less
973 than LIMIT, then just do the inner printing loop. */
974 if (completions_found_index < limit)
977 /* Print the sorted items, up-and-down alphabetically. */
978 for (i = 0; i < count; i++)
982 for (j = 0, l = i; j < limit; j++)
984 if (l >= completions_found_index)
989 int printed_length, k;
991 label = completions_found[l]->label;
992 printed_length = strlen (label);
993 printf_to_message_buffer ("%s", label);
997 for (k = 0; k < max_label - printed_length; k++)
998 printf_to_message_buffer (" ");
1003 printf_to_message_buffer ("\n");
1006 /* Make a new node to hold onto possible completions. Don't destroy
1007 dangling pointers. */
1011 temp = message_buffer_to_node ();
1012 add_gcable_pointer (temp->contents);
1013 name_internal_node (temp, compwin_name);
1014 possible_completions_output_node = temp;
1017 /* Find a suitable window for displaying the completions output.
1018 First choice is an existing window showing completions output.
1019 If there is only one window, and it is large, make another
1020 (smaller) window, and use that one. Otherwise, use the caller's
1025 compwin = get_internal_info_window (compwin_name);
1029 /* If we can split the window to display most of the completion
1030 items, then do so. */
1031 if (calling_window->height > (count * 2)
1032 && calling_window->height / 2 >= WINDOW_MIN_SIZE)
1035 #ifdef SPLIT_BEFORE_ACTIVE
1039 active_window = calling_window;
1041 /* Perhaps we can scroll this window on redisplay. */
1042 start = calling_window->first_row;
1043 pagetop = calling_window->pagetop;
1046 window_make_window (possible_completions_output_node);
1047 active_window = the_echo_area;
1048 window_change_window_height
1049 (compwin, -(compwin->height - (count + 2)));
1051 window_adjust_pagetop (calling_window);
1052 remember_calling_window (calling_window);
1054 #if defined (SPLIT_BEFORE_ACTIVE)
1055 /* If the pagetop hasn't changed, scrolling the calling
1056 window is a reasonable thing to do. */
1057 if (pagetop == calling_window->pagetop)
1059 end = start + calling_window->height;
1060 display_scroll_display
1061 (start, end, calling_window->prev->height + 1);
1063 #else /* !SPLIT_BEFORE_ACTIVE */
1064 /* If the pagetop has changed, set the new pagetop here. */
1065 if (pagetop != calling_window->pagetop)
1067 int newtop = calling_window->pagetop;
1068 calling_window->pagetop = pagetop;
1069 set_window_pagetop (calling_window, newtop);
1071 #endif /* !SPLIT_BEFORE_ACTIVE */
1073 echo_area_completions_window = compwin;
1074 remember_window_and_node (compwin, compwin->node);
1077 compwin = calling_window;
1080 if (compwin->node != possible_completions_output_node)
1082 window_set_node_of_window
1083 (compwin, possible_completions_output_node);
1084 remember_window_and_node (compwin, compwin->node);
1087 display_update_display (windows);
1092 DECLARE_INFO_COMMAND (ea_complete, _("Insert completion"))
1094 if (!echo_area_completion_items)
1096 ea_insert (window, count, key);
1100 /* If KEY is SPC, and we are not forcing completion to take place, simply
1102 if (!echo_area_must_complete_p && key == SPC)
1104 ea_insert (window, count, key);
1108 if (ea_last_executed_command == ea_complete)
1110 /* If the keypress is a SPC character, and we have already tried
1111 completing once, and there are several completions, then check
1112 the batch of completions to see if any continue with a space.
1113 If there are some, insert the space character and continue. */
1114 if (key == SPC && completions_found_index > 1)
1116 register int i, offset;
1118 offset = input_line_end - input_line_beg;
1120 for (i = 0; i < completions_found_index; i++)
1121 if (completions_found[i]->label[offset] == ' ')
1124 if (completions_found[i])
1125 ea_insert (window, 1, ' ');
1128 ea_possible_completions (window, count, key);
1134 ea_possible_completions (window, count, key);
1139 input_line_point = input_line_end;
1140 build_completions ();
1142 if (!completions_found_index)
1143 terminal_ring_bell ();
1144 else if (LCD_completion->label[0] == '\0')
1145 ea_possible_completions (window, count, key);
1149 input_line_point = input_line_end = input_line_beg;
1150 for (i = 0; LCD_completion->label[i]; i++)
1151 ea_insert (window, 1, LCD_completion->label[i]);
1155 /* Utility REFERENCE used to store possible LCD. */
1156 static REFERENCE LCD_reference = { (char *)NULL, (char *)NULL, (char *)NULL };
1158 static void remove_completion_duplicates ();
1160 /* Variables which remember the state of the most recent call
1161 to build_completions (). */
1162 static char *last_completion_request = (char *)NULL;
1163 static REFERENCE **last_completion_items = (REFERENCE **)NULL;
1165 /* How to tell the completion builder to reset internal state. */
1167 completions_must_be_rebuilt ()
1169 maybe_free (last_completion_request);
1170 last_completion_request = (char *)NULL;
1171 last_completion_items = (REFERENCE **)NULL;
1174 /* Build a list of possible completions from echo_area_completion_items,
1175 and the contents of input_line. */
1177 build_completions ()
1179 register int i, len;
1180 register REFERENCE *entry;
1182 int informed_of_lengthy_job = 0;
1184 /* If there are no items to complete over, exit immediately. */
1185 if (!echo_area_completion_items)
1187 completions_found_index = 0;
1188 LCD_completion = (REFERENCE *)NULL;
1192 /* Check to see if this call to build completions is the same as the last
1193 call to build completions. */
1194 len = input_line_end - input_line_beg;
1195 request = (char *)xmalloc (1 + len);
1196 strncpy (request, &input_line[input_line_beg], len);
1197 request[len] = '\0';
1199 if (last_completion_request && last_completion_items &&
1200 last_completion_items == echo_area_completion_items &&
1201 (strcmp (last_completion_request, request) == 0))
1207 maybe_free (last_completion_request);
1208 last_completion_request = request;
1209 last_completion_items = echo_area_completion_items;
1211 /* Always start at the beginning of the list. */
1212 completions_found_index = 0;
1213 LCD_completion = (REFERENCE *)NULL;
1215 for (i = 0; (entry = echo_area_completion_items[i]); i++)
1217 if (strncasecmp (request, entry->label, len) == 0)
1218 add_pointer_to_array (entry, completions_found_index,
1219 completions_found, completions_found_slots,
1222 if (!informed_of_lengthy_job && completions_found_index > 100)
1224 informed_of_lengthy_job = 1;
1225 window_message_in_echo_area (_("Building completions..."));
1229 if (!completions_found_index)
1232 /* Sort and prune duplicate entries from the completions array. */
1233 remove_completion_duplicates ();
1235 /* If there is only one completion, just return that. */
1236 if (completions_found_index == 1)
1238 LCD_completion = completions_found[0];
1242 /* Find the least common denominator. */
1244 long shortest = 100000;
1246 for (i = 1; i < completions_found_index; i++)
1252 (c1 = info_tolower (completions_found[i - 1]->label[j])) &&
1253 (c2 = info_tolower (completions_found[i]->label[j]));
1262 maybe_free (LCD_reference.label);
1263 LCD_reference.label = (char *)xmalloc (1 + shortest);
1264 /* Since both the sorting done inside remove_completion_duplicates
1265 and all the comparisons above are case-insensitive, it's
1266 possible that the completion we are going to return is
1267 identical to what the user typed but for the letter-case. This
1268 is confusing, since the user could type FOOBAR<TAB> and get her
1269 string change letter-case for no good reason. So try to find a
1270 possible completion whose letter-case is identical, and if so,
1272 if (completions_found_index > 1)
1274 int req_len = strlen (request);
1276 for (i = 0; i < completions_found_index; i++)
1277 if (strncmp (request, completions_found[i]->label, req_len) == 0)
1279 /* If none of the candidates match exactly, use the first one. */
1280 if (i >= completions_found_index)
1283 strncpy (LCD_reference.label, completions_found[i]->label, shortest);
1284 LCD_reference.label[shortest] = '\0';
1285 LCD_completion = &LCD_reference;
1288 if (informed_of_lengthy_job)
1289 echo_area_initialize_node ();
1292 /* Function called by qsort. */
1294 compare_references (entry1, entry2)
1295 REFERENCE **entry1, **entry2;
1297 return (strcasecmp ((*entry1)->label, (*entry2)->label));
1300 /* Prune duplicate entries from COMPLETIONS_FOUND. */
1302 remove_completion_duplicates ()
1308 if (!completions_found_index)
1311 /* Sort the items. */
1312 qsort (completions_found, completions_found_index, sizeof (REFERENCE *),
1313 compare_references);
1315 for (i = 0, newlen = 1; i < completions_found_index - 1; i++)
1317 if (strcmp (completions_found[i]->label,
1318 completions_found[i + 1]->label) == 0)
1319 completions_found[i] = (REFERENCE *)NULL;
1324 /* We have marked all the dead slots. It is faster to copy the live slots
1325 twice than to prune the dead slots one by one. */
1326 temp = (REFERENCE **)xmalloc ((1 + newlen) * sizeof (REFERENCE *));
1327 for (i = 0, j = 0; i < completions_found_index; i++)
1328 if (completions_found[i])
1329 temp[j++] = completions_found[i];
1331 for (i = 0; i < newlen; i++)
1332 completions_found[i] = temp[i];
1334 completions_found[i] = (REFERENCE *)NULL;
1335 completions_found_index = newlen;
1339 /* Scroll the "other" window. If there is a window showing completions, scroll
1340 that one, otherwise scroll the window which was active on entering the read
1342 DECLARE_INFO_COMMAND (ea_scroll_completions_window, _("Scroll the completions window"))
1347 compwin = get_internal_info_window (compwin_name);
1350 compwin = calling_window;
1352 old_pagetop = compwin->pagetop;
1354 /* Let info_scroll_forward () do the work, and print any messages that
1355 need to be displayed. */
1356 info_scroll_forward (compwin, count, key);
1359 /* Function which gets called when an Info window is deleted while the
1360 echo area is active. WINDOW is the window which has just been deleted. */
1362 echo_area_inform_of_deleted_window (window)
1365 /* If this is the calling_window, forget what we remembered about it. */
1366 if (window == calling_window)
1368 if (active_window != the_echo_area)
1369 remember_calling_window (active_window);
1371 remember_calling_window (windows);
1374 /* If this window was the echo_area_completions_window, then notice that
1375 the window has been deleted. */
1376 if (window == echo_area_completions_window)
1377 echo_area_completions_window = (WINDOW *)NULL;
1380 /* **************************************************************** */
1382 /* Pushing and Popping the Echo Area */
1384 /* **************************************************************** */
1386 /* Push and Pop the echo area. */
1390 REFERENCE **comp_items;
1391 int point, beg, end;
1397 static PUSHED_EA **pushed_echo_areas = (PUSHED_EA **)NULL;
1398 static int pushed_echo_areas_index = 0;
1399 static int pushed_echo_areas_slots = 0;
1401 /* Pushing the echo_area has a side effect of zeroing the completion_items. */
1407 pushed = (PUSHED_EA *)xmalloc (sizeof (PUSHED_EA));
1408 pushed->line = xstrdup (input_line);
1409 pushed->prompt = input_line_prompt;
1410 pushed->point = input_line_point;
1411 pushed->beg = input_line_beg;
1412 pushed->end = input_line_end;
1413 pushed->node = input_line_node;
1414 pushed->comp_items = echo_area_completion_items;
1415 pushed->must_complete = echo_area_must_complete_p;
1416 pushed->compwin = echo_area_completions_window;
1418 add_pointer_to_array (pushed, pushed_echo_areas_index, pushed_echo_areas,
1419 pushed_echo_areas_slots, 4, PUSHED_EA *);
1421 echo_area_completion_items = (REFERENCE **)NULL;
1429 popped = pushed_echo_areas[--pushed_echo_areas_index];
1431 strcpy (input_line, popped->line);
1432 free (popped->line);
1433 input_line_prompt = popped->prompt;
1434 input_line_point = popped->point;
1435 input_line_beg = popped->beg;
1436 input_line_end = popped->end;
1437 input_line_node = popped->node;
1438 echo_area_completion_items = popped->comp_items;
1439 echo_area_must_complete_p = popped->must_complete;
1440 echo_area_completions_window = popped->compwin;
1441 completions_must_be_rebuilt ();
1443 /* If the completion window no longer exists, forget about it. */
1444 if (echo_area_completions_window)
1446 register WINDOW *win;
1448 for (win = windows; win; win = win->next)
1449 if (echo_area_completions_window == win)
1452 /* If the window wasn't found, then it has already been deleted. */
1454 echo_area_completions_window = (WINDOW *)NULL;
1460 /* Returns non-zero if any of the prior stacked calls to read in the echo
1461 area produced a completions window. */
1463 echo_area_stack_contains_completions_p ()
1467 for (i = 0; i < pushed_echo_areas_index; i++)
1468 if (pushed_echo_areas[i]->compwin)
1474 /* **************************************************************** */
1476 /* Error Messages While Reading in Echo Area */
1478 /* **************************************************************** */
1480 #if defined (HAVE_SYS_TIME_H)
1481 # include <sys/time.h>
1482 # define HAVE_STRUCT_TIMEVAL
1483 #endif /* HAVE_SYS_TIME_H */
1489 struct timeval timer;
1494 FD_SET (fileno (stdin), &readfds);
1497 ready = select (fileno (stdin) + 1, &readfds, (fd_set *) NULL,
1498 (fd_set *) NULL, &timer);
1502 /* Print MESSAGE right after the end of the current line, and wait
1503 for input or a couple of seconds, whichever comes first. Then flush the
1504 informational message that was printed. */
1506 inform_in_echo_area (message)
1512 text = xstrdup (message);
1513 for (i = 0; text[i] && text[i] != '\n'; i++)
1517 echo_area_initialize_node ();
1518 sprintf (&input_line[input_line_end], "%s[%s]\n",
1519 echo_area_is_active ? " ": "", text);
1521 the_echo_area->point = input_line_point;
1522 display_update_one_window (the_echo_area);
1523 display_cursor_at_point (active_window);
1526 echo_area_initialize_node ();