echo-area.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /* echo-area.c -- how to read a line in the echo area.
  2. $Id$
  3. Copyright 1993, 1997, 1998, 1999, 2001, 2004, 2007, 2008, 2011, 2013,
  4. 2014, 2015 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Brian Fox. */
  16. #include "info.h"
  17. #include "session.h"
  18. #include "display.h"
  19. #include "info-utils.h"
  20. #include "echo-area.h"
  21. /* Non-zero means that C-g was used to quit reading input. */
  22. int info_aborted_echo_area = 0;
  23. /* Non-zero means that the echo area is being used to read input. */
  24. int echo_area_is_active = 0;
  25. /* The address of the last command executed in the echo area. */
  26. static VFunction *ea_last_executed_command = NULL;
  27. /* Non-zero means that the last command executed while reading input
  28. killed some text. */
  29. int echo_area_last_command_was_kill = 0;
  30. /* Variables which hold on to the current state of the input line. */
  31. static char input_line[1 + EA_MAX_INPUT]; /* Contents of echo area, including
  32. any prompt. */
  33. static int input_line_point; /* Offset into input_line of point */
  34. static int input_line_beg; /* End of prompt, and start of user input. */
  35. static int input_line_end; /* End of user input. */
  36. static NODE input_line_node = {
  37. NULL, NULL, NULL, input_line,
  38. EA_MAX_INPUT, 0, N_IsInternal
  39. };
  40. static void echo_area_initialize_node (void);
  41. static void push_echo_area (void), pop_echo_area (void);
  42. static int echo_area_stack_contains_completions_p (void);
  43. static void ea_kill_text (int from, int to);
  44. static void ea_remove_text (int from, int to);
  45. /* Non-zero means we force the user to complete. */
  46. static int echo_area_must_complete_p = 0;
  47. static int completions_window_p (WINDOW *window);
  48. /* If non-null, this is a window which was specifically created to display
  49. possible completions output. We remember it so we can delete it when
  50. appropriate. */
  51. static WINDOW *echo_area_completions_window = NULL;
  52. /* Variables which keep track of the window which was active prior to
  53. entering the echo area. */
  54. static WINDOW *calling_window = NULL;
  55. static NODE *calling_window_node = NULL;
  56. /* Remember the node and pertinent variables of the calling window. */
  57. static void
  58. remember_calling_window (WINDOW *window)
  59. {
  60. /* Only do this if the calling window is not the completions window, or,
  61. if it is the completions window and there is no other window. */
  62. if (!completions_window_p (window) ||
  63. ((window == windows) && !(window->next)))
  64. {
  65. calling_window = window;
  66. calling_window_node = window->node;
  67. }
  68. }
  69. /* Restore the caller's window so that it shows the node that it was showing
  70. on entry to info_read_xxx_echo_area (). */
  71. static void
  72. restore_calling_window (void)
  73. {
  74. register WINDOW *win, *compwin = NULL;
  75. /* If the calling window is still visible, and it is the window that
  76. we used for completions output, then restore the calling window. */
  77. for (win = windows; win; win = win->next)
  78. {
  79. if (completions_window_p (win))
  80. compwin = win;
  81. if (win == calling_window && win == compwin)
  82. {
  83. forget_node (win);
  84. compwin = NULL;
  85. break;
  86. }
  87. }
  88. /* Delete the completions window if it is still present, it isn't the
  89. last window on the screen, and there aren't any prior echo area reads
  90. pending which created a completions window. */
  91. if (compwin)
  92. {
  93. if ((compwin != windows || windows->next)
  94. && !echo_area_stack_contains_completions_p ())
  95. {
  96. info_delete_window_internal (compwin);
  97. }
  98. }
  99. }
  100. /* Set up a new input line with PROMPT. */
  101. static void
  102. initialize_input_line (const char *prompt)
  103. {
  104. strcpy (input_line, prompt);
  105. input_line_beg = input_line_end = input_line_point = strlen (prompt);
  106. }
  107. static char *
  108. echo_area_after_read (void)
  109. {
  110. char *return_value;
  111. if (info_aborted_echo_area)
  112. {
  113. info_aborted_echo_area = 0;
  114. return_value = NULL;
  115. }
  116. else
  117. {
  118. if (input_line_beg == input_line_end)
  119. return_value = xstrdup ("");
  120. else
  121. {
  122. int line_len = input_line_end - input_line_beg;
  123. return_value = xmalloc (1 + line_len);
  124. strncpy (return_value, &input_line[input_line_beg], line_len);
  125. return_value[line_len] = '\0';
  126. }
  127. }
  128. return return_value;
  129. }
  130. static void
  131. read_and_dispatch_in_echo_area (void)
  132. {
  133. while (1)
  134. {
  135. int count;
  136. VFunction *cmd;
  137. int lk = 0;
  138. lk = echo_area_last_command_was_kill;
  139. echo_area_prep_read ();
  140. if (!info_any_buffered_input_p ())
  141. display_update_display ();
  142. display_cursor_at_point (active_window);
  143. /* Do the selected command. */
  144. cmd = read_key_sequence (echo_area_keymap, 0, 0, 1, &count);
  145. if (cmd)
  146. {
  147. (*cmd) (the_echo_area, count);
  148. ea_last_executed_command = cmd;
  149. }
  150. else
  151. ea_last_executed_command = 0;
  152. /* Echo area commands that do killing increment the value of
  153. ECHO_AREA_LAST_COMMAND_WAS_KILL. Thus, if there is no
  154. change in the value of this variable, the last command
  155. executed was not a kill command. */
  156. if (lk == echo_area_last_command_was_kill)
  157. echo_area_last_command_was_kill = 0;
  158. if (cmd == ea_newline || info_aborted_echo_area)
  159. {
  160. ea_last_executed_command = NULL;
  161. break;
  162. }
  163. }
  164. }
  165. /* Read a line of text in the echo area. Return a malloc ()'ed string,
  166. or NULL if the user aborted out of this read. PROMPT is the prompt
  167. to print before reading the line. */
  168. char *
  169. info_read_in_echo_area (const char *prompt)
  170. {
  171. char *line;
  172. /* If the echo area is already active, remember the current state. */
  173. if (echo_area_is_active)
  174. push_echo_area ();
  175. /* Initialize our local variables. */
  176. initialize_input_line (prompt);
  177. /* Initialize the echo area for the first (but maybe not the last) time. */
  178. echo_area_initialize_node ();
  179. /* Save away the original node of this window, and the window itself,
  180. so echo area commands can temporarily use this window. */
  181. remember_calling_window (active_window);
  182. /* Let the rest of Info know that the echo area is active. */
  183. echo_area_is_active++;
  184. active_window = the_echo_area;
  185. /* Read characters in the echo area. */
  186. read_and_dispatch_in_echo_area ();
  187. window_clear_echo_area ();
  188. echo_area_is_active--;
  189. /* Restore the original active window and show point in it. */
  190. active_window = calling_window;
  191. restore_calling_window ();
  192. display_cursor_at_point (active_window);
  193. fflush (stdout);
  194. /* Get the value of the line. */
  195. line = echo_area_after_read ();
  196. /* If there is a previous loop waiting for us, restore it now. */
  197. if (echo_area_is_active)
  198. pop_echo_area ();
  199. /* Return the results to the caller. */
  200. return line;
  201. }
  202. /* (re) Initialize the echo area node. */
  203. static void
  204. echo_area_initialize_node (void)
  205. {
  206. register int i;
  207. for (i = input_line_end; (unsigned int) i < sizeof (input_line); i++)
  208. input_line[i] = ' ';
  209. input_line[i - 1] = '\n';
  210. window_set_node_of_window (the_echo_area, &input_line_node);
  211. input_line[input_line_end] = '\n';
  212. }
  213. /* Prepare to read characters in the echo area. This can initialize the
  214. echo area node, but its primary purpose is to side effect the input
  215. line buffer contents. */
  216. void
  217. echo_area_prep_read (void)
  218. {
  219. if (the_echo_area->node != &input_line_node)
  220. echo_area_initialize_node ();
  221. the_echo_area->point = input_line_point;
  222. input_line[input_line_end] = '\n';
  223. /* Mark the line map as invalid. This causes window_compute_line_map to
  224. recalculate it when it is called via display_cursor_at_point below.
  225. Otherwise adding or removing multi-column characters (like tabs) lead
  226. to incorrect cursor positioning. */
  227. the_echo_area->line_map.used = 0;
  228. display_update_one_window (the_echo_area);
  229. display_cursor_at_point (active_window);
  230. }
  231. /* **************************************************************** */
  232. /* */
  233. /* Echo Area Movement Commands */
  234. /* */
  235. /* **************************************************************** */
  236. DECLARE_INFO_COMMAND (ea_forward, _("Move forward a character"))
  237. {
  238. if (count < 0)
  239. ea_backward (window, -count);
  240. else
  241. {
  242. mbi_iterator_t iter;
  243. mbi_init (iter, input_line + input_line_point,
  244. input_line_end - input_line_point);
  245. while (mbi_avail (iter) && count--)
  246. {
  247. mbi_advance (iter);
  248. input_line_point = mbi_cur_ptr (iter) - input_line;
  249. if (input_line_point > input_line_end)
  250. input_line_point = input_line_end;
  251. }
  252. }
  253. }
  254. DECLARE_INFO_COMMAND (ea_backward, _("Move backward a character"))
  255. {
  256. if (count < 0)
  257. ea_forward (window, -count);
  258. else
  259. {
  260. char *ptr = input_line + input_line_point;
  261. while (count--)
  262. {
  263. /* Go back one character. Go back by bytes until we look at a valid
  264. multi-byte sequence. */
  265. ptr = input_line + input_line_point;
  266. while (ptr > input_line)
  267. {
  268. ptr--;
  269. if ((long) mbrlen (ptr,
  270. input_line + input_line_point - ptr, 0) > 0)
  271. break;
  272. }
  273. input_line_point = ptr - input_line;
  274. if (input_line_point < input_line_beg)
  275. input_line_point = input_line_beg;
  276. }
  277. }
  278. }
  279. DECLARE_INFO_COMMAND (ea_beg_of_line, _("Move to the start of this line"))
  280. {
  281. input_line_point = input_line_beg;
  282. }
  283. DECLARE_INFO_COMMAND (ea_end_of_line, _("Move to the end of this line"))
  284. {
  285. input_line_point = input_line_end;
  286. }
  287. #define alphabetic(c) (islower (c) || isupper (c) || isdigit (c))
  288. /* Move forward a word in the input line. */
  289. DECLARE_INFO_COMMAND (ea_forward_word, _("Move forward a word"))
  290. {
  291. int c;
  292. if (count < 0)
  293. ea_backward_word (window, -count);
  294. else
  295. {
  296. while (count--)
  297. {
  298. if (input_line_point == input_line_end)
  299. return;
  300. /* If we are not in a word, move forward until we are in one.
  301. Then, move forward until we hit a non-alphabetic character. */
  302. c = input_line[input_line_point];
  303. if (!alphabetic (c))
  304. {
  305. while (++input_line_point < input_line_end)
  306. {
  307. c = input_line[input_line_point];
  308. if (alphabetic (c))
  309. break;
  310. }
  311. }
  312. if (input_line_point == input_line_end)
  313. return;
  314. while (++input_line_point < input_line_end)
  315. {
  316. c = input_line[input_line_point];
  317. if (!alphabetic (c))
  318. break;
  319. }
  320. }
  321. }
  322. }
  323. DECLARE_INFO_COMMAND (ea_backward_word, _("Move backward a word"))
  324. {
  325. int c;
  326. if (count < 0)
  327. ea_forward_word (window, -count);
  328. else
  329. {
  330. while (count--)
  331. {
  332. if (input_line_point == input_line_beg)
  333. return;
  334. /* Like ea_forward_word (), except that we look at the
  335. characters just before point. */
  336. c = input_line[input_line_point - 1];
  337. if (!alphabetic (c))
  338. {
  339. while ((--input_line_point) != input_line_beg)
  340. {
  341. c = input_line[input_line_point - 1];
  342. if (alphabetic (c))
  343. break;
  344. }
  345. }
  346. while (input_line_point != input_line_beg)
  347. {
  348. c = input_line[input_line_point - 1];
  349. if (!alphabetic (c))
  350. break;
  351. else
  352. --input_line_point;
  353. }
  354. }
  355. }
  356. }
  357. DECLARE_INFO_COMMAND (ea_delete, _("Delete the character under the cursor"))
  358. {
  359. if (count < 0)
  360. ea_rubout (window, -count);
  361. else
  362. {
  363. int orig_point;
  364. if (input_line_point == input_line_end)
  365. return;
  366. orig_point = input_line_point;
  367. ea_forward (window, count);
  368. if (ea_explicit_arg || count > 1)
  369. ea_kill_text (orig_point, input_line_point);
  370. else
  371. ea_remove_text (orig_point, input_line_point);
  372. input_line_point = orig_point;
  373. }
  374. }
  375. DECLARE_INFO_COMMAND (ea_rubout, _("Delete the character behind the cursor"))
  376. {
  377. if (count < 0)
  378. ea_delete (window, -count);
  379. else
  380. {
  381. int start;
  382. if (input_line_point == input_line_beg)
  383. {
  384. /* Abort echo area read if backspace at start of area and user input
  385. is empty. */
  386. if (input_line_beg == input_line_end)
  387. info_aborted_echo_area = 1;
  388. return;
  389. }
  390. start = input_line_point;
  391. ea_backward (window, count);
  392. if (ea_explicit_arg || count > 1)
  393. ea_kill_text (start, input_line_point);
  394. else
  395. ea_delete (window, count);
  396. }
  397. }
  398. DECLARE_INFO_COMMAND (ea_abort, _("Cancel or quit operation"))
  399. {
  400. /* If any text, just discard it, and restore the calling window's node.
  401. If no text, quit. */
  402. if (input_line_end != input_line_beg)
  403. {
  404. terminal_ring_bell ();
  405. input_line_end = input_line_point = input_line_beg;
  406. if (calling_window->node != calling_window_node)
  407. restore_calling_window ();
  408. }
  409. else
  410. info_aborted_echo_area = 1;
  411. }
  412. DECLARE_INFO_COMMAND (ea_newline, _("Accept (or force completion of) this line"))
  413. {
  414. /* Stub does nothing. Simply here to see if it has been executed. */
  415. }
  416. DECLARE_INFO_COMMAND (ea_quoted_insert, _("Insert next character verbatim"))
  417. {
  418. int character;
  419. character = get_another_input_key ();
  420. if (character >= 256 || character < 0)
  421. return;
  422. ea_insert (window, count, character);
  423. }
  424. void
  425. ea_insert (WINDOW *window, int count, int key)
  426. {
  427. register int i;
  428. if ((input_line_end + 1) == EA_MAX_INPUT)
  429. {
  430. terminal_ring_bell ();
  431. return;
  432. }
  433. for (i = input_line_end + 1; i != input_line_point; i--)
  434. input_line[i] = input_line[i - 1];
  435. input_line[input_line_point] = key;
  436. input_line_point++;
  437. input_line_end++;
  438. window_line_map_init (window);
  439. }
  440. DECLARE_INFO_COMMAND (ea_tab_insert, _("Insert a TAB character"))
  441. {
  442. ea_insert (window, count, '\t');
  443. }
  444. /* Swap characters in INPUT_LINE. The first starts at C1 and ends at C1E, the
  445. second starts at C2 and ends at C2E, with C1 < C1E <= C2 < C2E. */
  446. static void
  447. ea_swap_chars (int c1, int c1e, int c2, int c2e)
  448. {
  449. int len1, len2;
  450. char *tmp;
  451. len1 = c1e - c1;
  452. len2 = c2e - c2;
  453. if (len1 >= len2)
  454. {
  455. /* Save first character. */
  456. tmp = xmalloc (len1);
  457. memcpy (tmp, input_line + c1, len1);
  458. /* Move the second character to where the first was. */
  459. memcpy (input_line + c1, input_line + c2, len2);
  460. /* Shift the part in between the characters backwards. */
  461. memmove (input_line + c1 + len2, input_line + c1e, c2 - c1e);
  462. /* Restore the first character at the end. */
  463. memcpy (input_line + c2 - (len1 - len2), tmp, len1);
  464. free (tmp);
  465. }
  466. else /* len2 > len1 */
  467. {
  468. /* Save second character. */
  469. tmp = xmalloc (len2);
  470. memcpy (tmp, input_line + c2, len2);
  471. /* Move first character to end of second character. */
  472. memcpy (input_line + c2e - len1, input_line + c1, len1);
  473. /* Shift the part in between the characters forwards. */
  474. memmove (input_line + c1e + (len2 - len1), input_line + c1e, c2 - c1e);
  475. /* Place the second character at the beginning. */
  476. memcpy (input_line + c1, tmp, len2);
  477. free (tmp);
  478. }
  479. }
  480. /* Transpose the characters at point. If point is at the end of the line,
  481. then transpose the characters before point. */
  482. DECLARE_INFO_COMMAND (ea_transpose_chars, _("Transpose characters at point"))
  483. {
  484. while (count)
  485. {
  486. if (input_line_point == input_line_end || count < 0)
  487. {
  488. /* Swap two characters before point. */
  489. int c1, c2, c2e;
  490. c2e = input_line_point;
  491. ea_backward (window, 1);
  492. c2 = input_line_point;
  493. ea_backward (window, 1);
  494. c1 = input_line_point;
  495. if (c1 != c2) /* There are two characters in this line. */
  496. ea_swap_chars (c1, c2, c2, c2e);
  497. if (count > 0)
  498. /* Restore point. */
  499. input_line_point = c2e;
  500. else
  501. input_line_point = c1 + c2e - c2;
  502. }
  503. else
  504. {
  505. int c1, c2, c2e;
  506. c2 = input_line_point;
  507. ea_forward (window, 1);
  508. c2e = input_line_point;
  509. if (c2e == c2)
  510. return; /* Shouldn't happen. */
  511. input_line_point = c2;
  512. ea_backward (window, 1);
  513. c1 = input_line_point;
  514. if (c1 == c2e)
  515. return; /* Can't go earlier in line. */
  516. ea_swap_chars (c1, c2, c2, c2e);
  517. /* Set point is after swapped pair. */
  518. input_line_point = c2e;
  519. }
  520. if (count < 0)
  521. count++;
  522. else
  523. count--;
  524. }
  525. }
  526. /* **************************************************************** */
  527. /* */
  528. /* Echo Area Killing and Yanking */
  529. /* */
  530. /* **************************************************************** */
  531. static char **kill_ring = NULL;
  532. static int kill_ring_index = 0; /* Number of kills appearing in KILL_RING. */
  533. static int kill_ring_slots = 0; /* Number of slots allocated to KILL_RING. */
  534. static int kill_ring_loc = 0; /* Location of current yank pointer. */
  535. /* The largest number of kills that we remember at one time. */
  536. static int max_retained_kills = 15;
  537. DECLARE_INFO_COMMAND (ea_yank, _("Yank back the contents of the last kill"))
  538. {
  539. register int i;
  540. register char *text;
  541. if (!kill_ring_index)
  542. {
  543. inform_in_echo_area (_("Kill ring is empty"));
  544. return;
  545. }
  546. text = kill_ring[kill_ring_loc];
  547. for (i = 0; text[i]; i++)
  548. ea_insert (window, 1, text[i]);
  549. }
  550. /* If the last command was yank, or yank_pop, and the text just before
  551. point is identical to the current kill item, then delete that text
  552. from the line, rotate the index down, and yank back some other text. */
  553. DECLARE_INFO_COMMAND (ea_yank_pop, _("Yank back a previous kill"))
  554. {
  555. register int len;
  556. if (((ea_last_executed_command != (VFunction *) ea_yank) &&
  557. (ea_last_executed_command != (VFunction *) ea_yank_pop)) ||
  558. (kill_ring_index == 0))
  559. return;
  560. len = strlen (kill_ring[kill_ring_loc]);
  561. /* Delete the last yanked item from the line. */
  562. {
  563. register int i, counter;
  564. counter = input_line_end - input_line_point;
  565. for (i = input_line_point - len; counter; i++, counter--)
  566. input_line[i] = input_line[i + len];
  567. input_line_end -= len;
  568. input_line_point -= len;
  569. }
  570. /* Get a previous kill, and yank that. */
  571. kill_ring_loc--;
  572. if (kill_ring_loc < 0)
  573. kill_ring_loc = kill_ring_index - 1;
  574. ea_yank (window, count);
  575. }
  576. /* Delete the text from point to end of line. */
  577. DECLARE_INFO_COMMAND (ea_kill_line, _("Kill to the end of the line"))
  578. {
  579. if (count < 0)
  580. {
  581. ea_kill_text (input_line_point, input_line_beg);
  582. input_line_point = input_line_beg;
  583. }
  584. else
  585. ea_kill_text (input_line_point, input_line_end);
  586. }
  587. /* Delete the text from point to beg of line. */
  588. DECLARE_INFO_COMMAND (ea_backward_kill_line,
  589. _("Kill to the beginning of the line"))
  590. {
  591. if (count < 0)
  592. ea_kill_text (input_line_point, input_line_end);
  593. else
  594. {
  595. ea_kill_text (input_line_point, input_line_beg);
  596. input_line_point = input_line_beg;
  597. }
  598. }
  599. /* Delete from point to the end of the current word. */
  600. DECLARE_INFO_COMMAND (ea_kill_word, _("Kill the word following the cursor"))
  601. {
  602. int orig_point = input_line_point;
  603. if (count < 0)
  604. ea_backward_kill_word (window, -count);
  605. else
  606. {
  607. ea_forward_word (window, count);
  608. if (input_line_point != orig_point)
  609. ea_kill_text (orig_point, input_line_point);
  610. input_line_point = orig_point;
  611. }
  612. window_line_map_init (window);
  613. }
  614. /* Delete from point to the start of the current word. */
  615. DECLARE_INFO_COMMAND (ea_backward_kill_word,
  616. _("Kill the word preceding the cursor"))
  617. {
  618. int orig_point = input_line_point;
  619. if (count < 0)
  620. ea_kill_word (window, -count);
  621. else
  622. {
  623. ea_backward_word (window, count);
  624. if (input_line_point != orig_point)
  625. ea_kill_text (orig_point, input_line_point);
  626. }
  627. window_line_map_init (window);
  628. }
  629. /* Remove text from offsets FROM to TO. Unlike 'ea_kill_text' nothing is
  630. saved in the kill ring. */
  631. static void
  632. ea_remove_text (int from, int to)
  633. {
  634. int distance, i, counter;
  635. counter = input_line_end - to;
  636. distance = to - from;
  637. for (i = from; counter; i++, counter--)
  638. input_line[i] = input_line[i + distance];
  639. input_line_end -= distance;
  640. }
  641. /* The way to kill something. This appends or prepends to the last
  642. kill, if the last command was a kill command. If FROM is less
  643. than TO, then the killed text is appended to the most recent kill,
  644. otherwise it is prepended. If the last command was not a kill command,
  645. then a new slot is made for this kill. */
  646. static void
  647. ea_kill_text (int from, int to)
  648. {
  649. register int distance;
  650. int killing_backwards, slot;
  651. char *killed_text;
  652. killing_backwards = (from > to);
  653. /* If killing backwards, reverse the values of FROM and TO. */
  654. if (killing_backwards)
  655. {
  656. int temp = from;
  657. from = to;
  658. to = temp;
  659. }
  660. /* Remember the text that we are about to delete. */
  661. distance = to - from;
  662. killed_text = xmalloc (1 + distance);
  663. strncpy (killed_text, &input_line[from], distance);
  664. killed_text[distance] = '\0';
  665. /* Actually delete the text from the line. */
  666. ea_remove_text (from, to);
  667. /* If the last command was a kill, append or prepend the killed text to
  668. the last command's killed text. */
  669. if (echo_area_last_command_was_kill)
  670. {
  671. char *old, *new;
  672. slot = kill_ring_loc;
  673. old = kill_ring[slot];
  674. new = xmalloc (1 + strlen (old) + strlen (killed_text));
  675. if (killing_backwards)
  676. {
  677. /* Prepend TEXT to current kill. */
  678. strcpy (new, killed_text);
  679. strcat (new, old);
  680. }
  681. else
  682. {
  683. /* Append TEXT to current kill. */
  684. strcpy (new, old);
  685. strcat (new, killed_text);
  686. }
  687. free (old);
  688. free (killed_text);
  689. kill_ring[slot] = new;
  690. }
  691. else
  692. {
  693. /* Try to store the kill in a new slot, unless that would cause there
  694. to be too many remembered kills. */
  695. slot = kill_ring_index;
  696. if (slot == max_retained_kills)
  697. slot = 0;
  698. if (slot + 1 > kill_ring_slots)
  699. kill_ring = xrealloc (kill_ring,
  700. (kill_ring_slots += max_retained_kills)
  701. * sizeof (char *));
  702. if (slot != kill_ring_index)
  703. free (kill_ring[slot]);
  704. else
  705. kill_ring_index++;
  706. kill_ring[slot] = killed_text;
  707. kill_ring_loc = slot;
  708. }
  709. /* Notice that the last command was a kill. */
  710. echo_area_last_command_was_kill++;
  711. }
  712. /* **************************************************************** */
  713. /* */
  714. /* Echo Area Completion */
  715. /* */
  716. /* **************************************************************** */
  717. /* Pointer to an array of REFERENCE to complete over. */
  718. REFERENCE **echo_area_completion_items = NULL;
  719. /* Sorted array of REFERENCE * which is the possible completions found in
  720. the variable echo_area_completion_items. If there is only one element,
  721. it is the only possible completion. */
  722. static REFERENCE **completions_found = NULL;
  723. static size_t completions_found_index = 0;
  724. static size_t completions_found_slots = 0;
  725. /* The lowest common denominator found while completing. */
  726. static REFERENCE *LCD_completion;
  727. /* Function to choose which references to offer as completion options. */
  728. static reference_bool_fn completion_exclude_func = 0;
  729. /* Internal functions used by the user calls. */
  730. static void build_completions (void);
  731. static void completions_must_be_rebuilt (void);
  732. /* Variable which holds the output of completions. */
  733. static NODE *possible_completions_output_node = NULL;
  734. static char *compwin_name = "*Completions*";
  735. /* Return non-zero if WINDOW is a window used for completions output. */
  736. static int
  737. completions_window_p (WINDOW *window)
  738. {
  739. int result = 0;
  740. if (internal_info_node_p (window->node) &&
  741. (strcmp (window->node->nodename, compwin_name) == 0))
  742. result = 1;
  743. return result;
  744. }
  745. /* Workhorse for completion readers. If FORCE is non-zero, the user cannot
  746. exit unless the line read completes, or is empty. Use EXCLUDE_FUNC to
  747. exclude items in COMPLETIONS. */
  748. char *
  749. info_read_completing_internal (const char *prompt, REFERENCE **completions,
  750. int force, reference_bool_fn exclude_func)
  751. {
  752. char *line;
  753. /* If the echo area is already active, remember the current state. */
  754. if (echo_area_is_active)
  755. push_echo_area ();
  756. echo_area_must_complete_p = force;
  757. /* Initialize our local variables. */
  758. initialize_input_line (prompt);
  759. /* Initialize the echo area for the first (but maybe not the last) time. */
  760. echo_area_initialize_node ();
  761. /* Save away the original node of this window, and the window itself,
  762. so echo area commands can temporarily use this window. */
  763. remember_calling_window (active_window);
  764. /* Save away the list of items to complete over. */
  765. echo_area_completion_items = completions;
  766. completions_must_be_rebuilt ();
  767. completion_exclude_func = exclude_func;
  768. active_window = the_echo_area;
  769. echo_area_is_active++;
  770. window_line_map_init (active_window);
  771. /* Read characters in the echo area. */
  772. while (1)
  773. {
  774. read_and_dispatch_in_echo_area ();
  775. line = echo_area_after_read ();
  776. /* Force the completion to take place if the user hasn't accepted
  777. a default or aborted, and if FORCE is active. */
  778. if (force && line && *line && completions)
  779. {
  780. size_t i;
  781. build_completions ();
  782. /* If there is only one completion, then make the line be that
  783. completion. */
  784. if (completions_found_index == 1)
  785. {
  786. free (line);
  787. line = xstrdup (completions_found[0]->label);
  788. break;
  789. }
  790. /* If one of the completions matches exactly, then that is okay, so
  791. return the current line. */
  792. for (i = 0; i < completions_found_index; i++)
  793. if (mbscasecmp (completions_found[i]->label, line) == 0)
  794. {
  795. free (line);
  796. line = xstrdup (completions_found[i]->label);
  797. break;
  798. }
  799. /* If no match, go back and try again. */
  800. if (i == completions_found_index)
  801. {
  802. if (!completions_found_index)
  803. inform_in_echo_area (_("No completions"));
  804. else
  805. inform_in_echo_area (_("Not complete"));
  806. continue;
  807. }
  808. }
  809. break;
  810. }
  811. echo_area_is_active--;
  812. window_clear_echo_area ();
  813. /* Restore the original active window and show point in it. */
  814. active_window = calling_window;
  815. restore_calling_window ();
  816. display_cursor_at_point (active_window);
  817. fflush (stdout);
  818. echo_area_completion_items = NULL;
  819. completions_must_be_rebuilt ();
  820. /* If there is a previous loop waiting for us, restore it now. */
  821. if (echo_area_is_active)
  822. pop_echo_area ();
  823. return line;
  824. }
  825. /* Read a line in the echo area with completion over COMPLETIONS. */
  826. char *
  827. info_read_completing_in_echo_area (const char *prompt, REFERENCE **completions)
  828. {
  829. return info_read_completing_internal (prompt, completions, 1, 0);
  830. }
  831. /* Read a line in the echo area allowing completion over COMPLETIONS, but
  832. not requiring it. */
  833. char *
  834. info_read_maybe_completing (const char *prompt, REFERENCE **completions)
  835. {
  836. return info_read_completing_internal (prompt, completions, 0, 0);
  837. }
  838. /* Read a line in the echo area with completion over COMPLETIONS, using
  839. EXCLUDE to exclude items from the completion list. */
  840. char *
  841. info_read_completing_in_echo_area_with_exclusions (const char *prompt,
  842. REFERENCE **completions, reference_bool_fn exclude)
  843. {
  844. return info_read_completing_internal (prompt, completions, 1, exclude);
  845. }
  846. DECLARE_INFO_COMMAND (ea_possible_completions, _("List possible completions"))
  847. {
  848. build_completions ();
  849. if (!completions_found_index)
  850. {
  851. terminal_ring_bell ();
  852. inform_in_echo_area (_("No completions"));
  853. }
  854. else if (completions_found_index == 1)
  855. {
  856. inform_in_echo_area (_("Sole completion"));
  857. }
  858. else
  859. {
  860. size_t i, l;
  861. size_t limit, iterations, max_label = 0;
  862. struct text_buffer message;
  863. text_buffer_init (&message);
  864. text_buffer_printf (&message, ngettext ("%d completion:\n",
  865. "%d completions:\n",
  866. completions_found_index),
  867. completions_found_index);
  868. /* Find the maximum length of a label. */
  869. for (i = 0; i < completions_found_index; i++)
  870. {
  871. int len = strlen (completions_found[i]->label);
  872. if (len > max_label)
  873. max_label = len;
  874. }
  875. max_label += 4;
  876. /* Find out how many columns we should print in. */
  877. limit = calling_window->width / max_label;
  878. if (limit != 1 && (limit * max_label == calling_window->width))
  879. limit--;
  880. /* Avoid a possible floating exception. If max_label > width then
  881. the limit will be 0 and a divide-by-zero fault will result. */
  882. if (limit == 0)
  883. limit = 1;
  884. /* How many iterations of the printing loop? */
  885. iterations = (completions_found_index + (limit - 1)) / limit;
  886. /* Watch out for special case. If the number of completions is less
  887. than LIMIT, then just do the inner printing loop. */
  888. if (completions_found_index < limit)
  889. iterations = 1;
  890. /* Print the sorted items, up-and-down alphabetically. */
  891. for (i = 0; i < iterations; i++)
  892. {
  893. register int j;
  894. for (j = 0, l = i; j < limit; j++)
  895. {
  896. if (l >= completions_found_index)
  897. break;
  898. else
  899. {
  900. char *label;
  901. int printed_length, k;
  902. label = completions_found[l]->label;
  903. printed_length = strlen (label);
  904. text_buffer_printf (&message, "%s", label);
  905. if (j + 1 < limit)
  906. {
  907. for (k = 0; k < max_label - printed_length; k++)
  908. text_buffer_printf (&message, " ");
  909. }
  910. }
  911. l += iterations;
  912. }
  913. text_buffer_printf (&message, "\n");
  914. }
  915. /* Make a new node to hold onto possible completions. Don't destroy
  916. dangling pointers. */
  917. {
  918. NODE *temp;
  919. temp = text_buffer_to_node (&message);
  920. name_internal_node (temp, xstrdup (compwin_name));
  921. possible_completions_output_node = temp;
  922. possible_completions_output_node->flags |= N_WasRewritten;
  923. }
  924. /* Find a suitable window for displaying the completions output.
  925. First choice is an existing window showing completions output.
  926. If there is only one window, and it is large, make another
  927. (smaller) window, and use that one. Otherwise, use the caller's
  928. window. */
  929. {
  930. WINDOW *compwin;
  931. compwin = get_internal_info_window (compwin_name);
  932. if (!compwin)
  933. {
  934. /* If we can split the window to display most of the completion
  935. items, then do so. */
  936. if (calling_window->height > (iterations * 2)
  937. && calling_window->height / 2 >= WINDOW_MIN_SIZE)
  938. {
  939. remember_calling_window (calling_window);
  940. active_window = calling_window;
  941. compwin = window_make_window ();
  942. window_change_window_height
  943. (compwin, -(compwin->height - (iterations + 2)));
  944. echo_area_completions_window = compwin;
  945. active_window = the_echo_area;
  946. }
  947. else
  948. compwin = calling_window;
  949. }
  950. /* Clear any completion nodes already showing from the window history.
  951. This could happen if the user presses TAB more than once. */
  952. while (compwin->node && (compwin->node->flags & N_IsInternal)
  953. && !strcmp (compwin->node->nodename, compwin_name))
  954. forget_node (compwin);
  955. info_set_node_of_window (compwin, possible_completions_output_node);
  956. display_update_display ();
  957. }
  958. }
  959. }
  960. DECLARE_INFO_COMMAND (ea_complete, _("Insert completion"))
  961. {
  962. if (ea_last_executed_command == (VFunction *) ea_complete)
  963. {
  964. ea_possible_completions (window, count);
  965. return;
  966. }
  967. input_line_point = input_line_end;
  968. build_completions ();
  969. if (!completions_found_index)
  970. terminal_ring_bell ();
  971. else if (LCD_completion->label[0] == '\0')
  972. ea_possible_completions (window, count);
  973. else
  974. {
  975. register int i;
  976. input_line_point = input_line_end = input_line_beg;
  977. for (i = 0; LCD_completion->label[i]; i++)
  978. ea_insert (window, 1, LCD_completion->label[i]);
  979. }
  980. }
  981. /* Utility REFERENCE used to store possible LCD. */
  982. static REFERENCE LCD_reference = {
  983. NULL, NULL, NULL, 0, 0, 0
  984. };
  985. static void remove_completion_duplicates (void);
  986. /* Variables which remember the state of the most recent call
  987. to build_completions (). */
  988. static char *last_completion_request = NULL;
  989. static REFERENCE **last_completion_items = NULL;
  990. /* How to tell the completion builder to reset internal state. */
  991. static void
  992. completions_must_be_rebuilt (void)
  993. {
  994. free (last_completion_request);
  995. last_completion_request = NULL;
  996. last_completion_items = NULL;
  997. }
  998. /* Build a list of possible completions from echo_area_completion_items,
  999. and the contents of input_line. */
  1000. static void
  1001. build_completions (void)
  1002. {
  1003. size_t i;
  1004. int len;
  1005. register REFERENCE *entry;
  1006. char *request;
  1007. int informed_of_lengthy_job = 0;
  1008. /* If there are no items to complete over, exit immediately. */
  1009. if (!echo_area_completion_items)
  1010. {
  1011. completions_found_index = 0;
  1012. LCD_completion = NULL;
  1013. return;
  1014. }
  1015. /* Check to see if this call to build completions is the same as the last
  1016. call to build completions. */
  1017. len = input_line_end - input_line_beg;
  1018. request = xmalloc (1 + len);
  1019. strncpy (request, &input_line[input_line_beg], len);
  1020. request[len] = '\0';
  1021. if (last_completion_request && last_completion_items &&
  1022. last_completion_items == echo_area_completion_items &&
  1023. (strcmp (last_completion_request, request) == 0))
  1024. {
  1025. free (request);
  1026. return;
  1027. }
  1028. free (last_completion_request);
  1029. last_completion_request = request;
  1030. last_completion_items = echo_area_completion_items;
  1031. /* Always start at the beginning of the list. */
  1032. completions_found_index = 0;
  1033. LCD_completion = NULL;
  1034. for (i = 0; (entry = echo_area_completion_items[i]); i++)
  1035. {
  1036. /* Skip certain items (for example, we might only want
  1037. a list of menu items). */
  1038. if (completion_exclude_func && completion_exclude_func (entry))
  1039. continue;
  1040. if (mbsncasecmp (request, entry->label, len) == 0)
  1041. add_pointer_to_array (entry, completions_found_index,
  1042. completions_found, completions_found_slots,
  1043. 20);
  1044. if (!informed_of_lengthy_job && completions_found_index > 100)
  1045. {
  1046. informed_of_lengthy_job = 1;
  1047. window_message_in_echo_area (_("Building completions..."));
  1048. }
  1049. }
  1050. if (!completions_found_index)
  1051. return;
  1052. /* Sort and prune duplicate entries from the completions array. */
  1053. remove_completion_duplicates ();
  1054. /* If there is only one completion, just return that. */
  1055. if (completions_found_index == 1)
  1056. {
  1057. LCD_completion = completions_found[0];
  1058. return;
  1059. }
  1060. /* Find the least common denominator. */
  1061. {
  1062. long shortest = 100000;
  1063. for (i = 1; i < completions_found_index; i++)
  1064. {
  1065. register int j;
  1066. int c1, c2;
  1067. for (j = 0;
  1068. (c1 = tolower (completions_found[i - 1]->label[j]))
  1069. && (c2 = tolower (completions_found[i]->label[j]));
  1070. j++)
  1071. if (c1 != c2)
  1072. break;
  1073. if (shortest > j)
  1074. shortest = j;
  1075. }
  1076. free (LCD_reference.label);
  1077. LCD_reference.label = xmalloc (1 + shortest);
  1078. /* Since both the sorting done inside remove_completion_duplicates
  1079. and all the comparisons above are case-insensitive, it's
  1080. possible that the completion we are going to return is
  1081. identical to what the user typed but for the letter-case. This
  1082. is confusing, since the user could type FOOBAR<TAB> and get her
  1083. string change letter-case for no good reason. So try to find a
  1084. possible completion whose letter-case is identical, and if so,
  1085. use that. */
  1086. if (completions_found_index > 1)
  1087. {
  1088. int req_len = strlen (request);
  1089. for (i = 0; i < completions_found_index; i++)
  1090. if (strncmp (request, completions_found[i]->label, req_len) == 0)
  1091. break;
  1092. /* If none of the candidates match exactly, use the first one. */
  1093. if (i >= completions_found_index)
  1094. i = 0;
  1095. }
  1096. strncpy (LCD_reference.label, completions_found[i]->label, shortest);
  1097. LCD_reference.label[shortest] = '\0';
  1098. LCD_completion = &LCD_reference;
  1099. }
  1100. if (informed_of_lengthy_job)
  1101. echo_area_initialize_node ();
  1102. }
  1103. /* Function called by qsort. */
  1104. static int
  1105. compare_references (const void *entry1, const void *entry2)
  1106. {
  1107. REFERENCE **e1 = (REFERENCE **) entry1;
  1108. REFERENCE **e2 = (REFERENCE **) entry2;
  1109. return mbscasecmp ((*e1)->label, (*e2)->label);
  1110. }
  1111. /* Prune duplicate entries from COMPLETIONS_FOUND. */
  1112. static void
  1113. remove_completion_duplicates (void)
  1114. {
  1115. size_t i, j;
  1116. REFERENCE **temp;
  1117. int newlen;
  1118. if (!completions_found_index)
  1119. return;
  1120. /* Sort the items. */
  1121. qsort (completions_found, completions_found_index, sizeof (REFERENCE *),
  1122. compare_references);
  1123. for (i = 0, newlen = 1; i < completions_found_index - 1; i++)
  1124. {
  1125. if (strcmp (completions_found[i]->label,
  1126. completions_found[i + 1]->label) == 0)
  1127. completions_found[i] = NULL;
  1128. else
  1129. newlen++;
  1130. }
  1131. /* We have marked all the dead slots. It is faster to copy the live slots
  1132. twice than to prune the dead slots one by one. */
  1133. temp = xmalloc ((1 + newlen) * sizeof (REFERENCE *));
  1134. for (i = 0, j = 0; i < completions_found_index; i++)
  1135. if (completions_found[i])
  1136. temp[j++] = completions_found[i];
  1137. for (i = 0; i < newlen; i++)
  1138. completions_found[i] = temp[i];
  1139. completions_found[i] = NULL;
  1140. completions_found_index = newlen;
  1141. free (temp);
  1142. }
  1143. /* Scroll the "other" window. If there is a window showing completions, scroll
  1144. that one, otherwise scroll the window which was active on entering the read
  1145. function. */
  1146. DECLARE_INFO_COMMAND (ea_scroll_completions_window, _("Scroll the completions window"))
  1147. {
  1148. WINDOW *compwin;
  1149. compwin = get_internal_info_window (compwin_name);
  1150. if (!compwin)
  1151. compwin = calling_window;
  1152. /* Let info_scroll_forward () do the work, and print any messages that
  1153. need to be displayed. */
  1154. info_scroll_forward (compwin, count);
  1155. }
  1156. /* Function which gets called when an Info window is deleted while the
  1157. echo area is active. WINDOW is the window which has just been deleted. */
  1158. void
  1159. echo_area_inform_of_deleted_window (WINDOW *window)
  1160. {
  1161. /* If this is the calling_window, forget what we remembered about it. */
  1162. if (window == calling_window)
  1163. {
  1164. if (active_window != the_echo_area)
  1165. remember_calling_window (active_window);
  1166. else
  1167. remember_calling_window (windows);
  1168. }
  1169. /* If this window was the echo_area_completions_window, then notice that
  1170. the window has been deleted. */
  1171. if (window == echo_area_completions_window)
  1172. echo_area_completions_window = NULL;
  1173. }
  1174. /* **************************************************************** */
  1175. /* */
  1176. /* Pushing and Popping the Echo Area */
  1177. /* */
  1178. /* **************************************************************** */
  1179. /* Push and Pop the echo area. */
  1180. typedef struct {
  1181. char *line;
  1182. REFERENCE **comp_items;
  1183. int point, beg, end;
  1184. int must_complete;
  1185. NODE node;
  1186. WINDOW *compwin;
  1187. } PUSHED_EA;
  1188. static PUSHED_EA **pushed_echo_areas = NULL;
  1189. static size_t pushed_echo_areas_index = 0;
  1190. static size_t pushed_echo_areas_slots = 0;
  1191. /* Pushing the echo_area has a side effect of zeroing the completion_items. */
  1192. static void
  1193. push_echo_area (void)
  1194. {
  1195. PUSHED_EA *pushed;
  1196. pushed = xmalloc (sizeof (PUSHED_EA));
  1197. pushed->line = xstrdup (input_line);
  1198. pushed->point = input_line_point;
  1199. pushed->beg = input_line_beg;
  1200. pushed->end = input_line_end;
  1201. pushed->node = input_line_node;
  1202. pushed->comp_items = echo_area_completion_items;
  1203. pushed->must_complete = echo_area_must_complete_p;
  1204. pushed->compwin = echo_area_completions_window;
  1205. add_pointer_to_array (pushed, pushed_echo_areas_index, pushed_echo_areas,
  1206. pushed_echo_areas_slots, 4);
  1207. echo_area_completion_items = NULL;
  1208. }
  1209. static void
  1210. pop_echo_area (void)
  1211. {
  1212. PUSHED_EA *popped;
  1213. popped = pushed_echo_areas[--pushed_echo_areas_index];
  1214. strcpy (input_line, popped->line);
  1215. free (popped->line);
  1216. input_line_point = popped->point;
  1217. input_line_beg = popped->beg;
  1218. input_line_end = popped->end;
  1219. input_line_node = popped->node;
  1220. echo_area_completion_items = popped->comp_items;
  1221. echo_area_must_complete_p = popped->must_complete;
  1222. echo_area_completions_window = popped->compwin;
  1223. completions_must_be_rebuilt ();
  1224. /* If the completion window no longer exists, forget about it. */
  1225. if (echo_area_completions_window)
  1226. {
  1227. register WINDOW *win;
  1228. for (win = windows; win; win = win->next)
  1229. if (echo_area_completions_window == win)
  1230. break;
  1231. /* If the window wasn't found, then it has already been deleted. */
  1232. if (!win)
  1233. echo_area_completions_window = NULL;
  1234. }
  1235. free (popped);
  1236. }
  1237. /* Returns non-zero if any of the prior stacked calls to read in the echo
  1238. area produced a completions window. */
  1239. static int
  1240. echo_area_stack_contains_completions_p (void)
  1241. {
  1242. size_t i;
  1243. for (i = 0; i < pushed_echo_areas_index; i++)
  1244. if (pushed_echo_areas[i]->compwin)
  1245. return 1;
  1246. return 0;
  1247. }
  1248. /* **************************************************************** */
  1249. /* */
  1250. /* Error Messages While Reading in Echo Area */
  1251. /* */
  1252. /* **************************************************************** */
  1253. #if defined (HAVE_SYS_TIME_H)
  1254. # include <sys/time.h>
  1255. # define HAVE_STRUCT_TIMEVAL
  1256. #endif /* HAVE_SYS_TIME_H */
  1257. #if !defined (FD_SET) && defined (__MINGW32__)
  1258. # define WIN32_LEAN_AND_MEAN
  1259. # include <windows.h>
  1260. #endif
  1261. static void
  1262. pause_or_input (void)
  1263. {
  1264. #ifdef FD_SET
  1265. struct timeval timer;
  1266. fd_set readfds;
  1267. FD_ZERO (&readfds);
  1268. FD_SET (fileno (stdin), &readfds);
  1269. timer.tv_sec = 2;
  1270. timer.tv_usec = 0;
  1271. select (fileno (stdin) + 1, &readfds, NULL, NULL, &timer);
  1272. #elif defined (__MINGW32__)
  1273. /* This is signalled on key release, so flush it and wait again. */
  1274. WaitForSingleObject (GetStdHandle (STD_INPUT_HANDLE), 2000);
  1275. FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
  1276. WaitForSingleObject (GetStdHandle (STD_INPUT_HANDLE), 2000);
  1277. #endif /* FD_SET */
  1278. }
  1279. /* Print MESSAGE right after the end of the current line, and wait
  1280. for input or a couple of seconds, whichever comes first. Then flush the
  1281. informational message that was printed. */
  1282. void
  1283. inform_in_echo_area (const char *message)
  1284. {
  1285. int i;
  1286. char *text;
  1287. int avail = EA_MAX_INPUT + 1 - input_line_end;
  1288. text = xstrdup (message);
  1289. for (i = 0; text[i] && text[i] != '\n' && i < avail; i++)
  1290. ;
  1291. text[i] = 0;
  1292. echo_area_initialize_node ();
  1293. sprintf (&input_line[input_line_end], "%s[%s]\n",
  1294. echo_area_is_active ? " ": "", text);
  1295. free (text);
  1296. the_echo_area->point = input_line_point;
  1297. display_update_one_window (the_echo_area);
  1298. display_cursor_at_point (active_window);
  1299. fflush (stdout);
  1300. pause_or_input ();
  1301. echo_area_initialize_node ();
  1302. }