File: complt.c

package info (click to toggle)
ng 1.5~beta1-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,140 kB
  • sloc: ansic: 43,437; asm: 3,150; sh: 2,539; cpp: 1,234; makefile: 630
file content (657 lines) | stat: -rw-r--r-- 13,068 bytes parent folder | download | duplicates (5)
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
/* $Id: complt.c,v 1.9 2001/07/23 17:09:02 amura Exp $ */
/*
 *	Complete completion functions.
 */

/*
 * $Log: complt.c,v $
 * Revision 1.9  2001/07/23 17:09:02  amura
 * fix raise segmentation fault when completion after shell-command
 *
 * Revision 1.8  2001/05/25 15:36:52  amura
 * now buffers have only one mark (before windows have one mark)
 *
 * Revision 1.7  2001/02/18 17:07:24  amura
 * append AUTOSAVE feature (but NOW not work)
 *
 * Revision 1.6  2001/01/05 14:07:00  amura
 * first implementation of Hojo Kanji support
 *
 * Revision 1.5  2000/11/04 13:43:30  amura
 * estrlen definition is changed to K&R style
 *
 * Revision 1.4  2000/09/05 01:04:54  amura
 * support HANKANA file and buffer
 *
 * Revision 1.3  2000/06/27 01:49:42  amura
 * import to CVS
 *
 * Revision 1.2  2000/06/01  05:24:14  amura
 * More robust
 *
 * Revision 1.1  1999/05/21  02:05:34  amura
 * Initial revision
 *
 */
/* 90.12.10  Created by Sawayanagi Yosirou */

#include    "config.h"
#ifdef	NEW_COMPLETE
#include    "def.h"
#include    "kbd.h"
#include    "complt.h"
#define    LIST_COL    35

static BUFFER    *bp = NULL;
static BUFFER    *prev_bp = NULL;
static WINDOW    *prev_wp = NULL;
static WINDOW    prev_window;

static int complete_funcname pro((char *));
static int complete_buffername pro((char *));
static int complete_filename pro((char *));
static int complete_list_funcnames pro((char *, BUFFER *));
static int complete_list_buffernames pro((char *, BUFFER *));
static int complete_list_filenames pro((char *, BUFFER *));
#ifdef	SS_SUPPORT
static int estrlen pro((char *));
#endif

/*
 * do some completion.
 */
int
complete (buf, flags)
    char    *buf;
    int    flags;
{
    int    res;

    switch (flags & EFAUTO)
      {
      case EFFUNC:
	res = complete_funcname (buf);
	break;
      case EFBUF:
        res = complete_buffername (buf);
	break;
      case EFFILE:
	res = complete_filename (buf);
	break;
      default:
	res = 0; /* dummy to prevent compile time warning */
        panic ("broken complete call: flags");
      }

    return (res);
}

char *
complete_message (matchnum)
    int    matchnum;
{
    char    *msg;

    switch (matchnum)
      {
      case COMPLT_NOT_UNIQUE:
	msg = " [Complete, but not unique]";
	break;
      case COMPLT_AMBIGUOUS:
	msg = " [Ambiguous]";
	break;
      case COMPLT_SOLE:
/*	msg = "";*/
	msg = " [Sole completion]";
	break;
      case COMPLT_NO_MATCH:
	msg = " [No match]";
	break;
      default:
	msg = " [Internal error]";
      }

    return (msg);
}

/* complete function name */
static int
complete_funcname (name)
    char    *name;
{
    int    fnlen;
    int    minlen = 0;
    int    matchnum;
    int    res;
    int    i, j;
    char    *cand;

    fnlen = strlen (name);

    /* compare names and make the common string of them */
    matchnum = 0;
    for (i = name_fent(name, TRUE); i < nfunct; i++)
      {
        cand = functnames[i].n_name;
	j = strncmp (cand, name, fnlen);
        if (j < 0)
	  continue;
        else if (j > 0)
	  break;	/* because functnames[] are in dictionary order */

	if (matchnum == 0)
	  {
	    for (j = fnlen; cand[j] != '\0'; j++)
	      name[j] = cand[j];
	    name[j] = '\0';
	    minlen = j;
	  }
	else
	  {
	    for (j = fnlen; name[j] != '\0'; j++)
              {
		if (cand[j] != name[j])
		    break;
              }
	    name[j] = '\0';
	    if (cand[j] == '\0')
	        minlen = j;
	  }
        matchnum++;
      }

    if (matchnum > 1)
      res = (minlen == (int) strlen (name)) ?
	     COMPLT_NOT_UNIQUE : COMPLT_AMBIGUOUS;
    else if (matchnum == 1)
      res = COMPLT_SOLE;
    else if (matchnum == 0)
      res = COMPLT_NO_MATCH;
    else
      res = -1;

    return (res);
}

static int
complete_buffername (name)
    char    *name;
{
    int    fnlen;
    int    minlen = 0;
    int    matchnum;
    int    res;
    int    j;
    char    *cand;
    LIST    *lh;

    fnlen = strlen (name);

    /* compare names and make the common string of them */
    matchnum = 0;
    for (lh = &(bheadp->b_list); lh != NULL; lh = lh->l_next)
      {
        cand = lh->l_name;
        if (strncmp (cand, name, fnlen) != 0)
	  continue;
	if (matchnum == 0)
	  {
	    for (j = fnlen; cand[j] != '\0'; j++)
	      name[j] = cand[j];
	    name[j] = '\0';
	    minlen = j;
	  }
	else
	  {
            for (j = fnlen; name[j] != '\0'; j++)
              {
                if (cand[j] != name[j])
	          break;
              }
	    name[j] = '\0';
	    if (cand[j] == '\0')
	        minlen = j;
	  }
        matchnum++;
      }

    if (matchnum > 1)
      res = (minlen == (int) strlen (name)) ?
	    COMPLT_NOT_UNIQUE : COMPLT_AMBIGUOUS;
    else if (matchnum == 1)
      res = COMPLT_SOLE;
    else if (matchnum == 0)
      res = COMPLT_NO_MATCH;
    else
      res = -1;

    return (res);
}

#ifndef NO_FILECOMP
static int
complete_filename (name)
    char    *name;
{
    int    fnlen;
    int    minlen = 0;
    int    matchnum;
    int    res;
    int    i, j;
    int    fnnum;
    char    *cand;
    char    *filenames;
    int    fffiles ();

    fnlen = strlen (name);

    if ((fnnum = fffiles (name, &filenames)) == -1)
      return (-1);    /* error */

    /* compare names and make a common string of them */
    matchnum = 0;
    cand = filenames;
    for (i = 0; i < fnnum; i++)
      {
	if (matchnum == 0)
	  {
	    for (j = fnlen; cand[j] != '\0'; j++)
	      name[j] = cand[j];
	    name[j] = '\0';
	    minlen = j;
	  }
	else
	  {
	    for (j = fnlen; name[j] != '\0'; j++)
              {
		if (cand[j] != name[j])
		  break;
	      }
	    name[j] = '\0';
	    if (cand[j] == '\0')
	        minlen = j;
	  }
	matchnum++;
	cand += (strlen (cand) + 1);
      }
    free (filenames);

    if (matchnum > 1)
      res = (minlen == strlen (name)) ? COMPLT_NOT_UNIQUE : COMPLT_AMBIGUOUS;
    else if (matchnum == 1)
      res = COMPLT_SOLE;
    else if (matchnum == 0)
      res = COMPLT_NO_MATCH;
    else
      res = -1;

    return (res);
}
#endif	/* NO_FILECOMP */

int
complete_list_names (buf, flags)
    char    *buf;
    int    flags;
{
    int    res;
    int    cur_row;
    int    cur_col;
    WINDOW    *wp;

    if ((bp = bfind ("*Completions*", TRUE)) == NULL)
      return (FALSE);
#ifdef	AUTOSAVE	/* 96.12.24 by M.Suzuki	*/
    bp->b_flag &= ~(BFCHG|BFACHG);    /* avoid recursive veread */
#else
    bp->b_flag &= ~BFCHG;    /* avoid recursive veread */
#endif	/* AUTOSAVE	*/
    if (bclear (bp) != TRUE)
      return (FALSE);

    if (addline(bp, "Possible completions are:") == FALSE)
      return (FALSE);

    switch (flags & EFAUTO)
      {
      case EFFUNC:
	res = complete_list_funcnames (buf, bp);
	break;
      case EFBUF:
        res = complete_list_buffernames (buf, bp);
	break;
      case EFFILE:
	res = complete_list_filenames (buf, bp);
	break;
      default:
        panic ("broken complete_list_names call: flags");
      }

    cur_row = ttrow;
    cur_col = ttcol;

    bp->b_dotp = lforw (bp->b_linep);	/* put dot at beginning of buffer */
    bp->b_doto = 0;
    /* setup window */
    if (curwp->w_bufp != bp)
      {
	if (wheadp->w_wndp == NULL)
	  {
	    if (splitwind (FFRAND, 0) == FALSE)
	      return (FALSE);
	    prev_wp = wheadp;
	    curwp = wheadp->w_wndp;
	    prev_bp = NULL;
	  }
	else
	  {
            for (wp = wheadp; wp->w_wndp != NULL; wp = wp->w_wndp)
              ;
            prev_wp = curwp;
	    curwp = wp;
	    prev_bp = curwp->w_bufp;
	    prev_window.w_dotp = curwp->w_dotp;
	    prev_window.w_doto = curwp->w_doto;
	    if (prev_window.w_bufp) {
	      prev_window.w_bufp->b_markp = prev_bp->b_markp;
	      prev_window.w_bufp->b_marko = prev_bp->b_marko;
	    }
	  }
      }
    for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
      {
        if (wp->w_bufp == bp)
	  {
	    wp->w_flag |= WFMODE | WFFORCE | WFHARD;
	    wp->w_dotp = bp->b_dotp;
	    wp->w_doto = bp->b_doto;
	  }
      }
    curbp = bp;
    if (showbuffer (bp, curwp, WFFORCE | WFHARD) == FALSE)
      return (FALSE);

    update ();
    ttmove (cur_row, cur_col);

    return (TRUE);
}

static int
complete_list_funcnames (name, bp)
    char    *name;
    BUFFER    *bp;
{
    int    fnlen;
    int    i, j;
    char    *cand;
    char    line[NFILEN];

    fnlen = strlen (name);

    line[0] = '\0';
    for (i = name_fent(name, TRUE); i < nfunct; i++)
      {
        cand = functnames[i].n_name;
	j = strncmp (cand, name, fnlen);
        if (j < 0)
	  continue;
        else if (j > 0)
	  break;	/* because functnames[] are in dictionary order */

	if (line[0] == '\0')
	  {
	    if (strlen (cand) < LIST_COL)
	      strcpy (line, cand);
	    else
	      addline (bp, cand);
	  }
	else
	  {
	    if (strlen (cand) < LIST_COL)
	      {
		for (j = strlen (line); j < LIST_COL; j++)
		  line[j] = ' ';
		line[j] = '\0';
		strcat (line, cand);
		addline (bp, line);
	      }
	    else
	      {
		addline (bp, line);
		addline (bp, cand);
	      }
	    line[0] = '\0';
	  }
      }
    if (line[0] != '\0')
      addline (bp, line);
    return (TRUE);
}

static int
complete_list_buffernames (name, bp)
    char    *name;
    BUFFER    *bp;
{
    int    fnlen;
    int    j;
    char    *cand;
    char    line[NFILEN];
    LIST    *lh;

    fnlen = strlen (name);

    line[0] = '\0';
    for (lh = &(bheadp->b_list); lh != NULL; lh = lh->l_next)
      {
        cand = lh->l_name;
        if (strncmp (cand, name, fnlen) != 0)
	  continue;

	if (line[0] == '\0')
	  {
#ifdef	SS_SUPPORT
	    if (estrlen (cand) < LIST_COL)
#else
	    if (strlen (cand) < LIST_COL)
#endif
	      strcpy (line, cand);
	    else
	      addline (bp, cand);
	  }
	else
	  {
#ifdef	SS_SUPPORT
	    if (estrlen (cand) < LIST_COL)
	      {
		int k = estrlen(line);
		for (j = strlen (line); k < LIST_COL; j++, k++)
#else
	    if (strlen (cand) < LIST_COL)
	      {
		for (j = strlen (line); j < LIST_COL; j++)
#endif
		  line[j] = ' ';
		line[j] = '\0';
		strcat (line, cand);
		addline (bp, line);
	      }
	    else
	      {
		addline (bp, line);
		addline (bp, cand);
	      }
	    line[0] = '\0';
	  }
      }
    if (line[0] != '\0')
      addline (bp, line);
    return (TRUE);
}

static int
complete_list_filenames (name, bp)
    char    *name;
    BUFFER    *bp;
{
    int    dnlen;
    int    i, j;
    int    fnnum;
    char    *cand;
    char    line[NFILEN];
    char    *filenames;
    int    fffiles ();
    char    *file_name_part ();

    dnlen = file_name_part (name) - name;

    if ((fnnum = fffiles (name, &filenames)) == -1)
      return (FALSE);    /* error */

    line[0] = '\0';
    cand = filenames;
    for (i = 0; i < fnnum; i++)
      {
	cand += dnlen;
	if (line[0] == '\0')
	  {
#ifdef	SS_SUPPORT
	    if (estrlen (cand) < LIST_COL)
#else
	    if (strlen (cand) < LIST_COL)
#endif
	      strcpy (line, cand);
	    else
	      addline (bp, cand);
	  }
	else
	  {
#ifdef	SS_SUPPORT
	    if (estrlen (cand) < LIST_COL)
	      {
		int k = estrlen(line);
		for (j = strlen (line); k < LIST_COL; j++, k++)
#else
	    if (strlen (cand) < LIST_COL)
	      {
		for (j = strlen (line); j < LIST_COL; j++)
#endif
		  line[j] = ' ';
		line[j] = '\0';
		strcat (line, cand);
		addline (bp, line);
	      }
	    else
	      {
		addline (bp, line);
		addline (bp, cand);
	      }
	    line[0] = '\0';
	  }
	cand += (strlen (cand) + 1);
      }
    if (line[0] != '\0')
      addline (bp, line);
    free (filenames);
    return (TRUE);
}

int
complete_del_list ()
{
    int    cur_row;
    int    cur_col;

    if (bp == NULL)
      return (FALSE);
    cur_row = ttrow;
    cur_col = ttcol;
    if (prev_bp == NULL)
      {
        if (wheadp->w_wndp != NULL)
	  delwind (FFRAND, 0);
      }
    else
      {
	curbp = prev_bp;
	showbuffer (prev_bp, curwp, WFFORCE | WFHARD);
	curwp->w_dotp = prev_window.w_dotp;
	curwp->w_doto = prev_window.w_doto;
	curwp->w_flag |= WFMOVE;
	curwp = prev_wp;
	curbp = curwp->w_bufp;
	if (prev_window.w_bufp) {
	  curbp->b_markp = prev_window.w_bufp->b_markp;
	  curbp->b_marko = prev_window.w_bufp->b_marko;
	}
      }      
    bp = NULL;
    prev_wp = NULL;
    prev_bp = NULL;
    update ();
    ttmove (cur_row, cur_col);
    /* 91.01.17  Add to delete *Completions* buffer. by S.Yoshida */
    eargset("*Completions*");
    killbuffer(0, 1);

    return (TRUE);
}

int
complete_scroll_up ()
{
    int    cur_row;
    int    cur_col;

    if (bp == NULL)
      return (FALSE);
    cur_row = ttrow;
    cur_col = ttcol;
    backpage (FFRAND, 0);
    update ();
    ttmove (cur_row, cur_col);
    return (TRUE);
}

int
complete_scroll_down ()
{
    int    cur_row;
    int    cur_col;

    if (bp == NULL)
      return (FALSE);
    cur_row = ttrow;
    cur_col = ttcol;
    forwpage (FFRAND, 0);
    update ();
    ttmove (cur_row, cur_col);
    return (TRUE);
}

#ifdef	SS_SUPPORT
static int
estrlen(str)
char *str;
{
    int i = 0;
    while (*str)
    {
#ifdef	HANKANA
	if (ISHANKANA(*str))
	    ;
	else
#endif
#ifdef	HOJO_KANJI
	if (ISHOJO(*str))
	    ;
	else
#endif
	    i++;
	str++;
    }
    return i;
}
#endif	/* SS_SUPPORT */
#endif	/* NEW_COMPLETE */