summaryrefslogtreecommitdiff
path: root/Build/source/utils/texinfo/info/echo-area.c
blob: d6fa8757d9afa40f439cbb12df80651d69fd4e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
/* echo-area.c -- how to read a line in the echo area.
   $Id: echo-area.c,v 1.12 2008/02/26 16:51:05 karl Exp $

   Copyright (C) 1993, 1997, 1998, 1999, 2001, 2004, 2007
   Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Written by Brian Fox (bfox@ai.mit.edu). */

#include "info.h"

#if defined (FD_SET)
#  if defined (hpux)
#    define fd_set_cast(x) (int *)(x)
#  else
#    define fd_set_cast(x) (fd_set *)(x)
#  endif /* !hpux */
#endif /* FD_SET */

/* Non-zero means that C-g was used to quit reading input. */
int info_aborted_echo_area = 0;

/* Non-zero means that the echo area is being used to read input. */
int echo_area_is_active = 0;

/* The address of the last command executed in the echo area. */
VFunction *ea_last_executed_command = (VFunction *)NULL;

/* Non-zero means that the last command executed while reading input
   killed some text. */
int echo_area_last_command_was_kill = 0;

/* Variables which hold on to the current state of the input line. */
static char input_line[1 + EA_MAX_INPUT];
static const char *input_line_prompt;
static int input_line_point;
static int input_line_beg;
static int input_line_end;
static NODE input_line_node = {
  (char *)NULL, (char *)NULL, (char *)NULL, input_line,
  EA_MAX_INPUT, 0, N_IsInternal
};

static void echo_area_initialize_node (void);
static void push_echo_area (void), pop_echo_area (void);
static int echo_area_stack_contains_completions_p (void);

static void ea_kill_text (int from, int to);

/* Non-zero means we force the user to complete. */
static int echo_area_must_complete_p = 0;
static int completions_window_p (WINDOW *window);

/* If non-null, this is a window which was specifically created to display
   possible completions output.  We remember it so we can delete it when
   appropriate. */
static WINDOW *echo_area_completions_window = (WINDOW *)NULL;

/* Variables which keep track of the window which was active prior to
   entering the echo area. */
static WINDOW *calling_window = (WINDOW *)NULL;
static NODE *calling_window_node = (NODE *)NULL;
static long calling_window_point = 0;
static long calling_window_pagetop = 0;

/* Remember the node and pertinent variables of the calling window. */
static void
remember_calling_window (WINDOW *window)
{
  /* Only do this if the calling window is not the completions window, or,
     if it is the completions window and there is no other window. */
  if (!completions_window_p (window) ||
      ((window == windows) && !(window->next)))
    {
      calling_window = window;
      calling_window_node = window->node;
      calling_window_point = window->point;
      calling_window_pagetop = window->pagetop;
    }
}

/* Restore the caller's window so that it shows the node that it was showing
   on entry to info_read_xxx_echo_area (). */
static void
restore_calling_window (void)
{
  register WINDOW *win, *compwin = (WINDOW *)NULL;

  /* If the calling window is still visible, and it is the window that
     we used for completions output, then restore the calling window. */
  for (win = windows; win; win = win->next)
    {
      if (completions_window_p (win))
        compwin = win;

      if (win == calling_window && win == compwin)
        {
          window_set_node_of_window (calling_window, calling_window_node);
          calling_window->point = calling_window_point;
          calling_window->pagetop = calling_window_pagetop;
          compwin = (WINDOW *)NULL;
          break;
        }
    }

  /* Delete the completions window if it is still present, it isn't the
     last window on the screen, and there aren't any prior echo area reads
     pending which created a completions window. */
  if (compwin)
    {
      if ((compwin != windows || windows->next) &&
          !echo_area_stack_contains_completions_p ())
        {
          WINDOW *next;
          int pagetop = 0;
          int start = 0;
          int end = 0;
          int amount = 0;

          next = compwin->next;
          if (next)
            {
              start = next->first_row;
              end = start + next->height;
              amount = - (compwin->height + 1);
              pagetop = next->pagetop;
            }

          info_delete_window_internal (compwin);

          /* This is not necessary because info_delete_window_internal ()
             calls echo_area_inform_of_deleted_window (), which does the
             right thing. */
#if defined (UNNECESSARY)
          echo_area_completions_window = (WINDOW *)NULL;
#endif /* UNNECESSARY */

          if (next)
            {
              display_scroll_display (start, end, amount);
              next->pagetop = pagetop;
              display_update_display (windows);
            }
        }
    }
}

/* Set up a new input line with PROMPT. */
static void
initialize_input_line (const char *prompt)
{
  input_line_prompt = prompt;
  if (prompt)
    strcpy (input_line, prompt);
  else
    input_line[0] = '\0';

  input_line_beg = input_line_end = input_line_point = strlen (prompt);
}

static char *
echo_area_after_read (void)
{
  char *return_value;

  if (info_aborted_echo_area)
    {
      info_aborted_echo_area = 0;
      return_value = (char *)NULL;
    }
  else
    {
      if (input_line_beg == input_line_end)
        return_value = xstrdup ("");
      else
        {
          int line_len = input_line_end - input_line_beg;
          return_value = (char *) xmalloc (1 + line_len);
          strncpy (return_value, &input_line[input_line_beg], line_len);
          return_value[line_len] = '\0';
        }
    }
  return (return_value);
}

/* Read a line of text in the echo area.  Return a malloc ()'ed string,
   or NULL if the user aborted out of this read.  WINDOW is the currently
   active window, so that we can restore it when we need to.  PROMPT, if
   non-null, is a prompt to print before reading the line. */
char *
info_read_in_echo_area (WINDOW *window, const char *prompt)
{
  char *line;

  /* If the echo area is already active, remember the current state. */
  if (echo_area_is_active)
    push_echo_area ();

  /* Initialize our local variables. */
  initialize_input_line (prompt);

  /* Initialize the echo area for the first (but maybe not the last) time. */
  echo_area_initialize_node ();

  /* Save away the original node of this window, and the window itself,
     so echo area commands can temporarily use this window. */
  remember_calling_window (window);

  /* Let the rest of Info know that the echo area is active. */
  echo_area_is_active++;
  active_window = the_echo_area;

  /* Read characters in the echo area. */
  info_read_and_dispatch ();

  echo_area_is_active--;

  /* Restore the original active window and show point in it. */
  active_window = calling_window;
  restore_calling_window ();
  display_cursor_at_point (active_window);
  fflush (stdout);

  /* Get the value of the line. */
  line = echo_area_after_read ();

  /* If there is a previous loop waiting for us, restore it now. */
  if (echo_area_is_active)
    pop_echo_area ();

  /* Return the results to the caller. */
  return (line);
}

/* (re) Initialize the echo area node. */
static void
echo_area_initialize_node (void)
{
  register int i;

  for (i = input_line_end; (unsigned int) i < sizeof (input_line); i++)
    input_line[i] = ' ';

  input_line[i - 1] = '\n';
  window_set_node_of_window (the_echo_area, &input_line_node);
  input_line[input_line_end] = '\n';
}

/* Prepare to read characters in the echo area.  This can initialize the
   echo area node, but its primary purpose is to side effect the input
   line buffer contents. */
void
echo_area_prep_read (void)
{
  if (the_echo_area->node != &input_line_node)
    echo_area_initialize_node ();

  the_echo_area->point = input_line_point;
  input_line[input_line_end] = '\n';
  display_update_one_window (the_echo_area);
  display_cursor_at_point (active_window);
}


/* **************************************************************** */
/*                                                                  */
/*                   Echo Area Movement Commands                    */
/*                                                                  */
/* **************************************************************** */

DECLARE_INFO_COMMAND (ea_forward, _("Move forward a character"))
{
  if (count < 0)
    ea_backward (window, -count, key);
  else
    {
      input_line_point += count;
      if (input_line_point > input_line_end)
        input_line_point = input_line_end;
    }
}

DECLARE_INFO_COMMAND (ea_backward, _("Move backward a character"))
{
  if (count < 0)
    ea_forward (window, -count, key);
  else
    {
      input_line_point -= count;
      if (input_line_point < input_line_beg)
        input_line_point = input_line_beg;
    }
}

DECLARE_INFO_COMMAND (ea_beg_of_line, _("Move to the start of this line"))
{
  input_line_point = input_line_beg;
}

DECLARE_INFO_COMMAND (ea_end_of_line, _("Move to the end of this line"))
{
  input_line_point = input_line_end;
}

#define alphabetic(c) (islower (c) || isupper (c) || isdigit (c))

/* Move forward a word in the input line. */
DECLARE_INFO_COMMAND (ea_forward_word, _("Move forward a word"))
{
  int c;

  if (count < 0)
    ea_backward_word (window, -count, key);
  else
    {
      while (count--)
        {
          if (input_line_point == input_line_end)
            return;

          /* If we are not in a word, move forward until we are in one.
             Then, move forward until we hit a non-alphabetic character. */
          c = input_line[input_line_point];

          if (!alphabetic (c))
            {
              while (++input_line_point < input_line_end)
                {
                  c = input_line[input_line_point];
                  if (alphabetic (c))
                    break;
                }
            }

          if (input_line_point == input_line_end)
            return;

          while (++input_line_point < input_line_end)
            {
              c = input_line[input_line_point];
              if (!alphabetic (c))
                break;
            }
        }
    }
}

DECLARE_INFO_COMMAND (ea_backward_word, _("Move backward a word"))
{
  int c;

  if (count < 0)
    ea_forward_word (window, -count, key);
  else
    {
      while (count--)
        {
          if (input_line_point == input_line_beg)
            return;

          /* Like ea_forward_word (), except that we look at the
             characters just before point. */

          c = input_line[input_line_point - 1];

          if (!alphabetic (c))
            {
              while ((--input_line_point) != input_line_beg)
                {
                  c = input_line[input_line_point - 1];
                  if (alphabetic (c))
                    break;
                }
            }

          while (input_line_point != input_line_beg)
            {
              c = input_line[input_line_point - 1];
              if (!alphabetic (c))
                break;
              else
                --input_line_point;
            }
        }
    }
}

DECLARE_INFO_COMMAND (ea_delete, _("Delete the character under the cursor"))
{
  register int i;

  if (count < 0)
    ea_rubout (window, -count, key);
  else
    {
      if (input_line_point == input_line_end)
        return;

      if (info_explicit_arg || count > 1)
        {
          int orig_point;

          orig_point = input_line_point;
          ea_forward (window, count, key);
          ea_kill_text (orig_point, input_line_point);
          input_line_point = orig_point;
        }
      else
        {
          for (i = input_line_point; i < input_line_end; i++)
            input_line[i] = input_line[i + 1];

          input_line_end--;
        }
    }
}

DECLARE_INFO_COMMAND (ea_rubout, _("Delete the character behind the cursor"))
{
  if (count < 0)
    ea_delete (window, -count, key);
  else
    {
      int start;

      if (input_line_point == input_line_beg)
        return;

      start = input_line_point;
      ea_backward (window, count, key);

      if (info_explicit_arg || count > 1)
        ea_kill_text (start, input_line_point);
      else
        ea_delete (window, count, key);
    }
}

DECLARE_INFO_COMMAND (ea_abort, _("Cancel or quit operation"))
{
  /* If any text, just discard it, and restore the calling window's node.
     If no text, quit. */
  if (input_line_end != input_line_beg)
    {
      terminal_ring_bell ();
      input_line_end = input_line_point = input_line_beg;
      if (calling_window->node != calling_window_node)
        restore_calling_window ();
    }
  else
    info_aborted_echo_area = 1;
}

DECLARE_INFO_COMMAND (ea_newline, _("Accept (or force completion of) this line"))
{
  /* Stub does nothing.  Simply here to see if it has been executed. */
}

DECLARE_INFO_COMMAND (ea_quoted_insert, _("Insert next character verbatim"))
{
  unsigned char character;

  character = info_get_another_input_char ();
  ea_insert (window, count, character);
}

DECLARE_INFO_COMMAND (ea_insert, _("Insert this character"))
{
  register int i;

  if ((input_line_end + 1) == EA_MAX_INPUT)
    {
      terminal_ring_bell ();
      return;
    }

  for (i = input_line_end + 1; i != input_line_point; i--)
    input_line[i] = input_line[i - 1];

  input_line[input_line_point] = key;
  input_line_point++;
  input_line_end++;
}

DECLARE_INFO_COMMAND (ea_tab_insert, _("Insert a TAB character"))
{
  ea_insert (window, count, '\t');
}

/* Transpose the characters at point.  If point is at the end of the line,
   then transpose the characters before point. */
DECLARE_INFO_COMMAND (ea_transpose_chars, _("Transpose characters at point"))
{
  /* Handle conditions that would make it impossible to transpose
     characters. */
  if (!count || !input_line_point || (input_line_end - input_line_beg) < 2)
    return;

  while (count)
    {
      int t;
      if (input_line_point == input_line_end)
        {
          t = input_line[input_line_point - 1];

          input_line[input_line_point - 1] = input_line[input_line_point - 2];
          input_line[input_line_point - 2] = t;
        }
      else
        {
          t = input_line[input_line_point];

          input_line[input_line_point] = input_line[input_line_point - 1];
          input_line[input_line_point - 1] = t;

          if (count < 0 && input_line_point != input_line_beg)
            input_line_point--;
          else
            input_line_point++;
        }

      if (count < 0)
        count++;
      else
        count--;
    }
}

/* **************************************************************** */
/*                                                                  */
/*                   Echo Area Killing and Yanking                  */
/*                                                                  */
/* **************************************************************** */

static char **kill_ring = (char **)NULL;
static int kill_ring_index = 0; /* Number of kills appearing in KILL_RING. */
static int kill_ring_slots = 0; /* Number of slots allocated to KILL_RING. */
static int kill_ring_loc = 0;   /* Location of current yank pointer. */

/* The largest number of kills that we remember at one time. */
static int max_retained_kills = 15;

DECLARE_INFO_COMMAND (ea_yank, _("Yank back the contents of the last kill"))
{
  register int i;
  register char *text;

  if (!kill_ring_index)
    {
      inform_in_echo_area (_("Kill ring is empty"));
      return;
    }

  text = kill_ring[kill_ring_loc];

  for (i = 0; text[i]; i++)
    ea_insert (window, 1, text[i]);
}

/* If the last command was yank, or yank_pop, and the text just before
   point is identical to the current kill item, then delete that text
   from the line, rotate the index down, and yank back some other text. */
DECLARE_INFO_COMMAND (ea_yank_pop, _("Yank back a previous kill"))
{
  register int len;

  if (((ea_last_executed_command != (VFunction *) ea_yank) &&
       (ea_last_executed_command != (VFunction *) ea_yank_pop)) ||
      (kill_ring_index == 0))
    return;

  len = strlen (kill_ring[kill_ring_loc]);

  /* Delete the last yanked item from the line. */
  {
    register int i, counter;

    counter = input_line_end - input_line_point;
    
    for (i = input_line_point - len; counter; i++, counter--)
      input_line[i] = input_line[i + len];

    input_line_end -= len;
    input_line_point -= len;
  }

  /* Get a previous kill, and yank that. */
  kill_ring_loc--;
  if (kill_ring_loc < 0)
    kill_ring_loc = kill_ring_index - 1;

  ea_yank (window, count, key);
}

/* Delete the text from point to end of line. */
DECLARE_INFO_COMMAND (ea_kill_line, _("Kill to the end of the line"))
{
  if (count < 0)
    {
      ea_kill_text (input_line_point, input_line_beg);
      input_line_point = input_line_beg;
    }
  else
    ea_kill_text (input_line_point, input_line_end);
}

/* Delete the text from point to beg of line. */
DECLARE_INFO_COMMAND (ea_backward_kill_line,
                      _("Kill to the beginning of the line"))
{
  if (count < 0)
    ea_kill_text (input_line_point, input_line_end);
  else
    {
      ea_kill_text (input_line_point, input_line_beg);
      input_line_point = input_line_beg;
    }
}

/* Delete from point to the end of the current word. */
DECLARE_INFO_COMMAND (ea_kill_word, _("Kill the word following the cursor"))
{
  int orig_point = input_line_point;

  if (count < 0)
    ea_backward_kill_word (window, -count, key);
  else
    {
      ea_forward_word (window, count, key);

      if (input_line_point != orig_point)
        ea_kill_text (orig_point, input_line_point);

      input_line_point = orig_point;
    }
}

/* Delete from point to the start of the current word. */
DECLARE_INFO_COMMAND (ea_backward_kill_word,
                      _("Kill the word preceding the cursor"))
{
  int orig_point = input_line_point;

  if (count < 0)
    ea_kill_word (window, -count, key);
  else
    {
      ea_backward_word (window, count, key);

      if (input_line_point != orig_point)
        ea_kill_text (orig_point, input_line_point);
    }
}

/* The way to kill something.  This appends or prepends to the last
   kill, if the last command was a kill command.  If FROM is less
   than TO, then the killed text is appended to the most recent kill,
   otherwise it is prepended.  If the last command was not a kill command,
   then a new slot is made for this kill. */
static void
ea_kill_text (int from, int to)
{
  register int i, counter, distance;
  int killing_backwards, slot;
  char *killed_text;

  killing_backwards = (from > to);

  /* If killing backwards, reverse the values of FROM and TO. */
  if (killing_backwards)
    {
      int temp = from;
      from = to;
      to = temp;
    }

  /* Remember the text that we are about to delete. */
  distance = to - from;
  killed_text = (char *)xmalloc (1 + distance);
  strncpy (killed_text, &input_line[from], distance);
  killed_text[distance] = '\0';

  /* Actually delete the text from the line. */
  counter = input_line_end - to;

  for (i = from; counter; i++, counter--)
    input_line[i] = input_line[i + distance];

  input_line_end -= distance;

  /* If the last command was a kill, append or prepend the killed text to
     the last command's killed text. */
  if (echo_area_last_command_was_kill)
    {
      char *old, *new;

      slot = kill_ring_loc;
      old = kill_ring[slot];
      new = (char *)xmalloc (1 + strlen (old) + strlen (killed_text));

      if (killing_backwards)
        {
          /* Prepend TEXT to current kill. */
          strcpy (new, killed_text);
          strcat (new, old);
        }
      else
        {
          /* Append TEXT to current kill. */
          strcpy (new, old);
          strcat (new, killed_text);
        }

      free (old);
      free (killed_text);
      kill_ring[slot] = new;
    }
  else
    {
      /* Try to store the kill in a new slot, unless that would cause there
         to be too many remembered kills. */
      slot = kill_ring_index;

      if (slot == max_retained_kills)
        slot = 0;

      if (slot + 1 > kill_ring_slots)
        kill_ring = (char **) xrealloc
          (kill_ring,
           (kill_ring_slots += max_retained_kills) * sizeof (char *));

      if (slot != kill_ring_index)
        free (kill_ring[slot]);
      else
        kill_ring_index++;

      kill_ring[slot] = killed_text;

      kill_ring_loc = slot;
    }

  /* Notice that the last command was a kill. */
  echo_area_last_command_was_kill++;
}

/* **************************************************************** */
/*                                                                  */
/*                      Echo Area Completion                        */
/*                                                                  */
/* **************************************************************** */

/* Pointer to an array of REFERENCE to complete over. */
static REFERENCE **echo_area_completion_items = (REFERENCE **)NULL;

/* Sorted array of REFERENCE * which is the possible completions found in
   the variable echo_area_completion_items.  If there is only one element,
   it is the only possible completion. */
static REFERENCE **completions_found = (REFERENCE **)NULL;
static int completions_found_index = 0;
static int completions_found_slots = 0;

/* The lowest common denominator found while completing. */
static REFERENCE *LCD_completion;

/* Internal functions used by the user calls. */
static void build_completions (void), completions_must_be_rebuilt (void);

/* Variable which holds the output of completions. */
static NODE *possible_completions_output_node = (NODE *)NULL;

static char *compwin_name = "*Completions*";

/* Return non-zero if WINDOW is a window used for completions output. */
static int
completions_window_p (WINDOW *window)
{
  int result = 0;

  if (internal_info_node_p (window->node) &&
      (strcmp (window->node->nodename, compwin_name) == 0))
    result = 1;

  return (result);
}

/* Workhorse for completion readers.  If FORCE is non-zero, the user cannot
   exit unless the line read completes, or is empty. */
char *
info_read_completing_internal (WINDOW *window, const char *prompt,
    REFERENCE **completions, int force)
{
  char *line;

  /* If the echo area is already active, remember the current state. */
  if (echo_area_is_active)
    push_echo_area ();

  echo_area_must_complete_p = force;

  /* Initialize our local variables. */
  initialize_input_line (prompt);

  /* Initialize the echo area for the first (but maybe not the last) time. */
  echo_area_initialize_node ();

  /* Save away the original node of this window, and the window itself,
     so echo area commands can temporarily use this window. */
  remember_calling_window (window);

  /* Save away the list of items to complete over. */
  echo_area_completion_items = completions;
  completions_must_be_rebuilt ();

  active_window = the_echo_area;
  echo_area_is_active++;

  /* Read characters in the echo area. */
  while (1)
    {
      info_read_and_dispatch ();

      line = echo_area_after_read ();

      /* Force the completion to take place if the user hasn't accepted
         a default or aborted, and if FORCE is active. */
      if (force && line && *line && completions)
        {
          register int i;

          build_completions ();

          /* If there is only one completion, then make the line be that
             completion. */
          if (completions_found_index == 1)
            {
              free (line);
              line = xstrdup (completions_found[0]->label);
              break;
            }

          /* If one of the completions matches exactly, then that is okay, so
             return the current line. */
          for (i = 0; i < completions_found_index; i++)
            if (mbscasecmp (completions_found[i]->label, line) == 0)
              {
                free (line);
                line = xstrdup (completions_found[i]->label);
                break;
              }

          /* If no match, go back and try again. */
          if (i == completions_found_index)
            {
              if (!completions_found_index)
                inform_in_echo_area (_("No completions"));
              else
                inform_in_echo_area (_("Not complete"));
              continue;
            }
        }
      break;
    }
  echo_area_is_active--;

  /* Restore the original active window and show point in it. */
  active_window = calling_window;
  restore_calling_window ();
  display_cursor_at_point (active_window);
  fflush (stdout);

  echo_area_completion_items = (REFERENCE **)NULL;
  completions_must_be_rebuilt ();

  /* If there is a previous loop waiting for us, restore it now. */
  if (echo_area_is_active)
    pop_echo_area ();

  return (line);
}
  
/* Read a line in the echo area with completion over COMPLETIONS. */
char *
info_read_completing_in_echo_area (WINDOW *window,
    const char *prompt, REFERENCE **completions)
{
  return (info_read_completing_internal (window, prompt, completions, 1));
}

/* Read a line in the echo area allowing completion over COMPLETIONS, but
   not requiring it. */
char *
info_read_maybe_completing (WINDOW *window,
    const char *prompt, REFERENCE **completions)
{
  return (info_read_completing_internal (window, prompt, completions, 0));
}

DECLARE_INFO_COMMAND (ea_possible_completions, _("List possible completions"))
{
  if (!echo_area_completion_items)
    {
      ea_insert (window, count, key);
      return;
    }

  build_completions ();

  if (!completions_found_index)
    {
      terminal_ring_bell ();
      inform_in_echo_area (_("No completions"));
    }
  else if ((completions_found_index == 1) && (key != '?'))
    {
      inform_in_echo_area (_("Sole completion"));
    }
  else
    {
      register int i, l;
      int limit, iterations, max_label = 0;

      initialize_message_buffer ();
      printf_to_message_buffer (completions_found_index == 1
                                ? _("One completion:\n")
                                : _("%d completions:\n"),
				(void *) (long) completions_found_index,
				NULL, NULL);

      /* Find the maximum length of a label. */
      for (i = 0; i < completions_found_index; i++)
        {
          int len = strlen (completions_found[i]->label);
          if (len > max_label)
            max_label = len;
        }

      max_label += 4;

      /* Find out how many columns we should print in. */
      limit = calling_window->width / max_label;
      if (limit != 1 && (limit * max_label == calling_window->width))
        limit--;

      /* Avoid a possible floating exception.  If max_label > width then
         the limit will be 0 and a divide-by-zero fault will result. */
      if (limit == 0)
        limit = 1;

      /* How many iterations of the printing loop? */
      iterations = (completions_found_index + (limit - 1)) / limit;

      /* Watch out for special case.  If the number of completions is less
         than LIMIT, then just do the inner printing loop. */
      if (completions_found_index < limit)
        iterations = 1;

      /* Print the sorted items, up-and-down alphabetically. */
      for (i = 0; i < iterations; i++)
        {
          register int j;

          for (j = 0, l = i; j < limit; j++)
            {
              if (l >= completions_found_index)
                break;
              else
                {
                  char *label;
                  int printed_length, k;

                  label = completions_found[l]->label;
                  printed_length = strlen (label);
                  printf_to_message_buffer ("%s", label, NULL, NULL);

                  if (j + 1 < limit)
                    {
                      for (k = 0; k < max_label - printed_length; k++)
                        printf_to_message_buffer (" ", NULL, NULL, NULL);
                    }
                }
              l += iterations;
            }
          printf_to_message_buffer ("\n", NULL, NULL, NULL);
        }

      /* Make a new node to hold onto possible completions.  Don't destroy
         dangling pointers. */
      {
        NODE *temp;

        temp = message_buffer_to_node ();
        add_gcable_pointer (temp->contents);
        name_internal_node (temp, compwin_name);
        possible_completions_output_node = temp;
      }

      /* Find a suitable window for displaying the completions output.
         First choice is an existing window showing completions output.
         If there is only one window, and it is large, make another
         (smaller) window, and use that one.  Otherwise, use the caller's
         window. */
      {
        WINDOW *compwin;

        compwin = get_internal_info_window (compwin_name);

        if (!compwin)
          {
            /* If we can split the window to display most of the completion
               items, then do so. */
            if (calling_window->height > (iterations * 2)
		&& calling_window->height / 2 >= WINDOW_MIN_SIZE)
              {
                int start, pagetop;
#ifdef SPLIT_BEFORE_ACTIVE
                int end;
#endif

                active_window = calling_window;

                /* Perhaps we can scroll this window on redisplay. */
                start = calling_window->first_row;
                pagetop = calling_window->pagetop;

                compwin =
                  window_make_window (possible_completions_output_node);
                active_window = the_echo_area;
                window_change_window_height
                  (compwin, -(compwin->height - (iterations + 2)));

                window_adjust_pagetop (calling_window);
                remember_calling_window (calling_window);

#if defined (SPLIT_BEFORE_ACTIVE)
                /* If the pagetop hasn't changed, scrolling the calling
                   window is a reasonable thing to do. */
                if (pagetop == calling_window->pagetop)
                  {
                    end = start + calling_window->height;
                    display_scroll_display
                      (start, end, calling_window->prev->height + 1);
                  }
#else /* !SPLIT_BEFORE_ACTIVE */
                /* If the pagetop has changed, set the new pagetop here. */
                if (pagetop != calling_window->pagetop)
                  {
                    int newtop = calling_window->pagetop;
                    calling_window->pagetop = pagetop;
                    set_window_pagetop (calling_window, newtop);
                  }
#endif /* !SPLIT_BEFORE_ACTIVE */

                echo_area_completions_window = compwin;
                remember_window_and_node (compwin, compwin->node);
              }
            else
              compwin = calling_window;
          }

        if (compwin->node != possible_completions_output_node)
          {
            window_set_node_of_window
              (compwin, possible_completions_output_node);
            remember_window_and_node (compwin, compwin->node);
          }

        display_update_display (windows);
      }
    }
}

DECLARE_INFO_COMMAND (ea_complete, _("Insert completion"))
{
  if (!echo_area_completion_items)
    {
      ea_insert (window, count, key);
      return;
    }

  /* If KEY is SPC, and we are not forcing completion to take place, simply
     insert the key. */
  if (!echo_area_must_complete_p && key == SPC)
    {
      ea_insert (window, count, key);
      return;
    }

  if (ea_last_executed_command == (VFunction *) ea_complete)
    {
      /* If the keypress is a SPC character, and we have already tried
         completing once, and there are several completions, then check
         the batch of completions to see if any continue with a space.
         If there are some, insert the space character and continue. */
      if (key == SPC && completions_found_index > 1)
        {
          register int i, offset;

          offset = input_line_end - input_line_beg;

          for (i = 0; i < completions_found_index; i++)
            if (completions_found[i]->label[offset] == ' ')
              break;

          if (completions_found[i])
            ea_insert (window, 1, ' ');
          else
            {
              ea_possible_completions (window, count, key);
              return;
            }
        }
      else
        {
          ea_possible_completions (window, count, key);
          return;
        }
    }

  input_line_point = input_line_end;
  build_completions ();

  if (!completions_found_index)
    terminal_ring_bell ();
  else if (LCD_completion->label[0] == '\0')
    ea_possible_completions (window, count, key);
  else
    {
      register int i;
      input_line_point = input_line_end = input_line_beg;
      for (i = 0; LCD_completion->label[i]; i++)
        ea_insert (window, 1, LCD_completion->label[i]);
    }
}

/* Utility REFERENCE used to store possible LCD. */
static REFERENCE LCD_reference = {
    (char *)NULL, (char *)NULL, (char *)NULL, 0, 0, 0
};

static void remove_completion_duplicates (void);

/* Variables which remember the state of the most recent call
   to build_completions (). */
static char *last_completion_request = (char *)NULL;
static REFERENCE **last_completion_items = (REFERENCE **)NULL;

/* How to tell the completion builder to reset internal state. */
static void
completions_must_be_rebuilt (void)
{
  maybe_free (last_completion_request);
  last_completion_request = (char *)NULL;
  last_completion_items = (REFERENCE **)NULL;
}

/* Build a list of possible completions from echo_area_completion_items,
   and the contents of input_line. */
static void
build_completions (void)
{
  register int i, len;
  register REFERENCE *entry;
  char *request;
  int informed_of_lengthy_job = 0;

  /* If there are no items to complete over, exit immediately. */
  if (!echo_area_completion_items)
    {
      completions_found_index = 0;
      LCD_completion = (REFERENCE *)NULL;
      return;
    }

  /* Check to see if this call to build completions is the same as the last
     call to build completions. */
  len = input_line_end - input_line_beg;
  request = (char *)xmalloc (1 + len);
  strncpy (request, &input_line[input_line_beg], len);
  request[len] = '\0';

  if (last_completion_request && last_completion_items &&
      last_completion_items == echo_area_completion_items &&
      (strcmp (last_completion_request, request) == 0))
    {
      free (request);
      return;
    }

  maybe_free (last_completion_request);
  last_completion_request = request;
  last_completion_items = echo_area_completion_items;

  /* Always start at the beginning of the list. */
  completions_found_index = 0;
  LCD_completion = (REFERENCE *)NULL;

  for (i = 0; (entry = echo_area_completion_items[i]); i++)
    {
      if (mbsncasecmp (request, entry->label, len) == 0)
        add_pointer_to_array (entry, completions_found_index,
                              completions_found, completions_found_slots,
                              20, REFERENCE *);

      if (!informed_of_lengthy_job && completions_found_index > 100)
        {
          informed_of_lengthy_job = 1;
          window_message_in_echo_area (_("Building completions..."),
              NULL, NULL);
        }
    }

  if (!completions_found_index)
    return;

  /* Sort and prune duplicate entries from the completions array. */
  remove_completion_duplicates ();

  /* If there is only one completion, just return that. */
  if (completions_found_index == 1)
    {
      LCD_completion = completions_found[0];
      return;
    }

  /* Find the least common denominator. */
  {
    long shortest = 100000;

    for (i = 1; i < completions_found_index; i++)
      {
        register int j;
        int c1, c2;

        for (j = 0;
             (c1 = info_tolower (completions_found[i - 1]->label[j])) &&
             (c2 = info_tolower (completions_found[i]->label[j]));
             j++)
          if (c1 != c2)
            break;

        if (shortest > j)
          shortest = j;
      }

    maybe_free (LCD_reference.label);
    LCD_reference.label = (char *)xmalloc (1 + shortest);
    /* Since both the sorting done inside remove_completion_duplicates
       and all the comparisons above are case-insensitive, it's
       possible that the completion we are going to return is
       identical to what the user typed but for the letter-case.  This
       is confusing, since the user could type FOOBAR<TAB> and get her
       string change letter-case for no good reason.  So try to find a
       possible completion whose letter-case is identical, and if so,
       use that.  */
    if (completions_found_index > 1)
      {
	int req_len = strlen (request);

        for (i = 0; i < completions_found_index; i++)
          if (strncmp (request, completions_found[i]->label, req_len) == 0)
            break;
        /* If none of the candidates match exactly, use the first one.  */
        if (i >= completions_found_index)
          i = 0;
      }
    strncpy (LCD_reference.label, completions_found[i]->label, shortest);
    LCD_reference.label[shortest] = '\0';
    LCD_completion = &LCD_reference;
  }

  if (informed_of_lengthy_job)
    echo_area_initialize_node ();
}

/* Function called by qsort. */
static int
compare_references (const void *entry1, const void *entry2)
{
  REFERENCE **e1 = (REFERENCE **) entry1;
  REFERENCE **e2 = (REFERENCE **) entry2;

  return (mbscasecmp ((*e1)->label, (*e2)->label));
}

/* Prune duplicate entries from COMPLETIONS_FOUND. */
static void
remove_completion_duplicates (void)
{
  register int i, j;
  REFERENCE **temp;
  int newlen;

  if (!completions_found_index)
    return;

  /* Sort the items. */
  qsort (completions_found, completions_found_index, sizeof (REFERENCE *),
         compare_references);

  for (i = 0, newlen = 1; i < completions_found_index - 1; i++)
    {
      if (strcmp (completions_found[i]->label,
                  completions_found[i + 1]->label) == 0)
        completions_found[i] = (REFERENCE *)NULL;
      else
        newlen++;
    }

  /* We have marked all the dead slots.  It is faster to copy the live slots
     twice than to prune the dead slots one by one. */
  temp = (REFERENCE **)xmalloc ((1 + newlen) * sizeof (REFERENCE *));
  for (i = 0, j = 0; i < completions_found_index; i++)
    if (completions_found[i])
      temp[j++] = completions_found[i];

  for (i = 0; i < newlen; i++)
    completions_found[i] = temp[i];

  completions_found[i] = (REFERENCE *)NULL;
  completions_found_index = newlen;
  free (temp);
}

/* Scroll the "other" window.  If there is a window showing completions, scroll
   that one, otherwise scroll the window which was active on entering the read
   function. */
DECLARE_INFO_COMMAND (ea_scroll_completions_window, _("Scroll the completions window"))
{
  WINDOW *compwin;
  int old_pagetop;

  compwin = get_internal_info_window (compwin_name);

  if (!compwin)
    compwin = calling_window;

  old_pagetop = compwin->pagetop;

  /* Let info_scroll_forward () do the work, and print any messages that
     need to be displayed. */
  info_scroll_forward (compwin, count, key);
}

/* Function which gets called when an Info window is deleted while the
   echo area is active.  WINDOW is the window which has just been deleted. */
void
echo_area_inform_of_deleted_window (WINDOW *window)
{
  /* If this is the calling_window, forget what we remembered about it. */
  if (window == calling_window)
    {
      if (active_window != the_echo_area)
        remember_calling_window (active_window);
      else
        remember_calling_window (windows);
    }

  /* If this window was the echo_area_completions_window, then notice that
     the window has been deleted. */
  if (window == echo_area_completions_window)
    echo_area_completions_window = (WINDOW *)NULL;
}

/* **************************************************************** */
/*                                                                  */
/*                 Pushing and Popping the Echo Area                */
/*                                                                  */
/* **************************************************************** */

/* Push and Pop the echo area. */
typedef struct {
  char *line;
  const char *prompt;
  REFERENCE **comp_items;
  int point, beg, end;
  int must_complete;
  NODE node;
  WINDOW *compwin;
} PUSHED_EA;

static PUSHED_EA **pushed_echo_areas = (PUSHED_EA **)NULL;
static int pushed_echo_areas_index = 0;
static int pushed_echo_areas_slots = 0;

/* Pushing the echo_area has a side effect of zeroing the completion_items. */
static void
push_echo_area (void)
{
  PUSHED_EA *pushed;

  pushed = (PUSHED_EA *)xmalloc (sizeof (PUSHED_EA));
  pushed->line = xstrdup (input_line);
  pushed->prompt = input_line_prompt;
  pushed->point = input_line_point;
  pushed->beg = input_line_beg;
  pushed->end = input_line_end;
  pushed->node = input_line_node;
  pushed->comp_items = echo_area_completion_items;
  pushed->must_complete = echo_area_must_complete_p;
  pushed->compwin = echo_area_completions_window;

  add_pointer_to_array (pushed, pushed_echo_areas_index, pushed_echo_areas,
                        pushed_echo_areas_slots, 4, PUSHED_EA *);

  echo_area_completion_items = (REFERENCE **)NULL;
}

static void
pop_echo_area (void)
{
  PUSHED_EA *popped;

  popped = pushed_echo_areas[--pushed_echo_areas_index];

  strcpy (input_line, popped->line);
  free (popped->line);
  input_line_prompt = popped->prompt;
  input_line_point = popped->point;
  input_line_beg = popped->beg;
  input_line_end = popped->end;
  input_line_node = popped->node;
  echo_area_completion_items = popped->comp_items;
  echo_area_must_complete_p = popped->must_complete;
  echo_area_completions_window = popped->compwin;
  completions_must_be_rebuilt ();

  /* If the completion window no longer exists, forget about it. */
  if (echo_area_completions_window)
    {
      register WINDOW *win;

      for (win = windows; win; win = win->next)
        if (echo_area_completions_window == win)
          break;

      /* If the window wasn't found, then it has already been deleted. */
      if (!win)
        echo_area_completions_window = (WINDOW *)NULL;
    }

  free (popped);
}

/* Returns non-zero if any of the prior stacked calls to read in the echo
   area produced a completions window. */
static int
echo_area_stack_contains_completions_p (void)
{
  register int i;

  for (i = 0; i < pushed_echo_areas_index; i++)
    if (pushed_echo_areas[i]->compwin)
      return (1);

  return (0);
}

/* **************************************************************** */
/*                                                                  */
/*             Error Messages While Reading in Echo Area            */
/*                                                                  */
/* **************************************************************** */

#if defined (HAVE_SYS_TIME_H)
#  include <sys/time.h>
#  define HAVE_STRUCT_TIMEVAL
#endif /* HAVE_SYS_TIME_H */

static void
pause_or_input (void)
{
#ifdef FD_SET
  struct timeval timer;
  fd_set readfds;
  int ready;

  FD_ZERO (&readfds);
  FD_SET (fileno (stdin), &readfds);
  timer.tv_sec = 2;
  timer.tv_usec = 0;
  ready = select (fileno (stdin) + 1, &readfds, (fd_set *) NULL,
                  (fd_set *) NULL, &timer);
#endif /* FD_SET */
}

/* Print MESSAGE right after the end of the current line, and wait
   for input or a couple of seconds, whichever comes first.  Then flush the
   informational message that was printed. */
void
inform_in_echo_area (const char *message)
{
  int i;
  char *text;
  int avail = EA_MAX_INPUT + 1 - input_line_end;

  text = xstrdup (message);
  for (i = 0; text[i] && text[i] != '\n' && i < avail; i++)
    ;
  text[i] = 0;

  echo_area_initialize_node ();
  sprintf (&input_line[input_line_end], "%s[%s]\n",
           echo_area_is_active ? " ": "", text);
  free (text);
  the_echo_area->point = input_line_point;
  display_update_one_window (the_echo_area);
  display_cursor_at_point (active_window);
  fflush (stdout);
  pause_or_input ();
  echo_area_initialize_node ();
}