comparison src/minibuf.c @ 444:576fb035e263 r21-2-37

Import from CVS: tag r21-2-37
author cvs
date Mon, 13 Aug 2007 11:36:19 +0200
parents abe6d1db359e
children 3078fd1074e8
comparison
equal deleted inserted replaced
443:a8296e22da4e 444:576fb035e263
287 } 287 }
288 return 0; 288 return 0;
289 } 289 }
290 290
291 291
292 /* #### Maybe we should allow ALIST to be a hash table. It is wrong 292 /* #### Maybe we should allow COLLECTION to be a hash table.
293 for the use of obarrays to be better-rewarded than the use of 293 It is wrong for the use of obarrays to be better-rewarded than the
294 hash tables. By better-rewarded I mean that you can pass an obarray 294 use of hash tables. By better-rewarded I mean that you can pass an
295 to all of the completion functions, whereas you can't do anything 295 obarray to all of the completion functions, whereas you can't do
296 like that with a hash table. 296 anything like that with a hash table.
297 297
298 To do so, there should probably be a 298 To do so, there should probably be a
299 map_obarray_or_alist_or_hash_table function which would be used by 299 map_obarray_or_alist_or_hash_table function which would be used by
300 both Ftry_completion and Fall_completions. But would the 300 both Ftry_completion and Fall_completions. But would the
301 additional funcalls slow things down? */ 301 additional funcalls slow things down? */
302 302
303 DEFUN ("try-completion", Ftry_completion, 2, 3, 0, /* 303 DEFUN ("try-completion", Ftry_completion, 2, 3, 0, /*
304 Return common substring of all completions of STRING in ALIST. 304 Return common substring of all completions of STRING in COLLECTION.
305 Each car of each element of ALIST is tested to see if it begins with STRING. 305 COLLECTION must be an alist, an obarray, or a function.
306 Each string in COLLECTION is tested to see if it begins with STRING.
306 All that match are compared together; the longest initial sequence 307 All that match are compared together; the longest initial sequence
307 common to all matches is returned as a string. 308 common to all matches is returned as a string. If there is no match
308 If there is no match at all, nil is returned. 309 at all, nil is returned. For an exact match, t is returned.
309 For an exact match, t is returned. 310
310 311 If COLLECTION is an alist, the cars of the elements of the alist
311 ALIST can be an obarray instead of an alist. 312 \(which must be strings) form the set of possible completions.
312 Then the print names of all symbols in the obarray are the possible matches. 313
313 314 If COLLECTION is an obarray, the names of all symbols in the obarray
314 ALIST can also be a function to do the completion itself. 315 are the possible completions.
315 It receives three arguments: the values STRING, PREDICATE and nil. 316
316 Whatever it returns becomes the value of `try-completion'. 317 If COLLECTION is a function, it is called with three arguments: the
317 318 values STRING, PREDICATE and nil. Whatever it returns becomes the
318 If optional third argument PREDICATE is non-nil, 319 value of `try-completion'.
319 it is used to test each possible match. 320
320 The match is a candidate only if PREDICATE returns non-nil. 321 If optional third argument PREDICATE is non-nil, it is used to test
321 The argument given to PREDICATE is the alist element or the symbol from the obarray. 322 each possible match. The match is a candidate only if PREDICATE
323 returns non-nil. The argument given to PREDICATE is the alist element
324 or the symbol from the obarray.
322 */ 325 */
323 (string, alist, pred)) 326 (string, collection, predicate))
324 { 327 {
325 /* This function can GC */ 328 /* This function can GC */
326 Lisp_Object bestmatch, tail; 329 Lisp_Object bestmatch, tail;
327 Charcount bestmatchsize = 0; 330 Charcount bestmatchsize = 0;
328 int list; 331 int list;
332 Lisp_Object bucket; 335 Lisp_Object bucket;
333 Charcount slength, blength; 336 Charcount slength, blength;
334 337
335 CHECK_STRING (string); 338 CHECK_STRING (string);
336 339
337 if (CONSP (alist)) 340 if (CONSP (collection))
338 { 341 {
339 Lisp_Object tem = XCAR (alist); 342 Lisp_Object tem = XCAR (collection);
340 if (SYMBOLP (tem)) /* lambda, autoload, etc. Emacs-lisp sucks */ 343 if (SYMBOLP (tem)) /* lambda, autoload, etc. Emacs-lisp sucks */
341 return call3 (alist, string, pred, Qnil); 344 return call3 (collection, string, predicate, Qnil);
342 else 345 else
343 list = 1; 346 list = 1;
344 } 347 }
345 else if (VECTORP (alist)) 348 else if (VECTORP (collection))
346 list = 0; 349 list = 0;
347 else if (NILP (alist)) 350 else if (NILP (collection))
348 list = 1; 351 list = 1;
349 else 352 else
350 return call3 (alist, string, pred, Qnil); 353 return call3 (collection, string, predicate, Qnil);
351 354
352 bestmatch = Qnil; 355 bestmatch = Qnil;
353 blength = 0; 356 blength = 0;
354 slength = XSTRING_CHAR_LENGTH (string); 357 slength = XSTRING_CHAR_LENGTH (string);
355 358
356 /* If ALIST is not a list, set TAIL just for gc pro. */ 359 /* If COLLECTION is not a list, set TAIL just for gc pro. */
357 tail = alist; 360 tail = collection;
358 if (!list) 361 if (!list)
359 { 362 {
360 obsize = XVECTOR_LENGTH (alist); 363 obsize = XVECTOR_LENGTH (collection);
361 bucket = XVECTOR_DATA (alist)[indice]; 364 bucket = XVECTOR_DATA (collection)[indice];
362 } 365 }
363 else /* warning suppression */ 366 else /* warning suppression */
364 { 367 {
365 obsize = 0; 368 obsize = 0;
366 bucket = Qnil; 369 bucket = Qnil;
403 } 406 }
404 else if (++indice >= obsize) 407 else if (++indice >= obsize)
405 break; 408 break;
406 else 409 else
407 { 410 {
408 bucket = XVECTOR_DATA (alist)[indice]; 411 bucket = XVECTOR_DATA (collection)[indice];
409 continue; 412 continue;
410 } 413 }
411 } 414 }
412 415
413 /* Is this element a possible completion? */ 416 /* Is this element a possible completion? */
422 { 425 {
423 { 426 {
424 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; 427 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
425 int loser; 428 int loser;
426 GCPRO4 (tail, string, eltstring, bestmatch); 429 GCPRO4 (tail, string, eltstring, bestmatch);
427 loser = ignore_completion_p (eltstring, pred, elt); 430 loser = ignore_completion_p (eltstring, predicate, elt);
428 UNGCPRO; 431 UNGCPRO;
429 if (loser) /* reject this one */ 432 if (loser) /* reject this one */
430 continue; 433 continue;
431 } 434 }
432 435
507 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize)); 510 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize));
508 } 511 }
509 512
510 513
511 DEFUN ("all-completions", Fall_completions, 2, 3, 0, /* 514 DEFUN ("all-completions", Fall_completions, 2, 3, 0, /*
512 Search for partial matches to STRING in ALIST. 515 Search for partial matches to STRING in COLLECTION.
513 Each car of each element of ALIST is tested to see if it begins with STRING. 516 COLLECTION must be an alist, an obarray, or a function.
514 The value is a list of all the strings from ALIST that match. 517 Each string in COLLECTION is tested to see if it begins with STRING.
515 ALIST can be an obarray instead of an alist. 518 The value is a list of all the strings from COLLECTION that match.
516 Then the print names of all symbols in the obarray are the possible matches. 519
517 520 If COLLECTION is an alist, the cars of the elements of the alist
518 ALIST can also be a function to do the completion itself. 521 \(which must be strings) form the set of possible completions.
519 It receives three arguments: the values STRING, PREDICATE and t. 522
520 Whatever it returns becomes the value of `all-completions'. 523 If COLLECTION is an obarray, the names of all symbols in the obarray
521 524 are the possible completions.
522 If optional third argument PREDICATE is non-nil, 525
523 it is used to test each possible match. 526 If COLLECTION is a function, it is called with three arguments: the
524 The match is a candidate only if PREDICATE returns non-nil. 527 values STRING, PREDICATE and t. Whatever it returns becomes the
525 The argument given to PREDICATE is the alist element or 528 value of `all-completions'.
526 the symbol from the obarray. 529
530 If optional third argument PREDICATE is non-nil, it is used to test
531 each possible match. The match is a candidate only if PREDICATE
532 returns non-nil. The argument given to PREDICATE is the alist element
533 or the symbol from the obarray.
527 */ 534 */
528 (string, alist, pred)) 535 (string, collection, predicate))
529 { 536 {
530 /* This function can GC */ 537 /* This function can GC */
531 Lisp_Object tail; 538 Lisp_Object tail;
532 Lisp_Object allmatches; 539 Lisp_Object allmatches;
533 int list; 540 int list;
536 Lisp_Object bucket; 543 Lisp_Object bucket;
537 Charcount slength; 544 Charcount slength;
538 545
539 CHECK_STRING (string); 546 CHECK_STRING (string);
540 547
541 if (CONSP (alist)) 548 if (CONSP (collection))
542 { 549 {
543 Lisp_Object tem = XCAR (alist); 550 Lisp_Object tem = XCAR (collection);
544 if (SYMBOLP (tem)) /* lambda, autoload, etc. Emacs-lisp sucks */ 551 if (SYMBOLP (tem)) /* lambda, autoload, etc. Emacs-lisp sucks */
545 return call3 (alist, string, pred, Qt); 552 return call3 (collection, string, predicate, Qt);
546 else 553 else
547 list = 1; 554 list = 1;
548 } 555 }
549 else if (VECTORP (alist)) 556 else if (VECTORP (collection))
550 list = 0; 557 list = 0;
551 else if (NILP (alist)) 558 else if (NILP (collection))
552 list = 1; 559 list = 1;
553 else 560 else
554 return call3 (alist, string, pred, Qt); 561 return call3 (collection, string, predicate, Qt);
555 562
556 allmatches = Qnil; 563 allmatches = Qnil;
557 slength = XSTRING_CHAR_LENGTH (string); 564 slength = XSTRING_CHAR_LENGTH (string);
558 565
559 /* If ALIST is not a list, set TAIL just for gc pro. */ 566 /* If COLLECTION is not a list, set TAIL just for gc pro. */
560 tail = alist; 567 tail = collection;
561 if (!list) 568 if (!list)
562 { 569 {
563 obsize = XVECTOR_LENGTH (alist); 570 obsize = XVECTOR_LENGTH (collection);
564 bucket = XVECTOR_DATA (alist)[indice]; 571 bucket = XVECTOR_DATA (collection)[indice];
565 } 572 }
566 else /* warning suppression */ 573 else /* warning suppression */
567 { 574 {
568 obsize = 0; 575 obsize = 0;
569 bucket = Qnil; 576 bucket = Qnil;
600 } 607 }
601 else if (++indice >= obsize) 608 else if (++indice >= obsize)
602 break; 609 break;
603 else 610 else
604 { 611 {
605 bucket = XVECTOR_DATA (alist)[indice]; 612 bucket = XVECTOR_DATA (collection)[indice];
606 continue; 613 continue;
607 } 614 }
608 } 615 }
609 616
610 /* Is this element a possible completion? */ 617 /* Is this element a possible completion? */
611 618
612 if (STRINGP (eltstring) 619 if (STRINGP (eltstring)
613 && (slength <= XSTRING_CHAR_LENGTH (eltstring)) 620 && (slength <= XSTRING_CHAR_LENGTH (eltstring))
614 /* Reject alternatives that start with space
615 unless the input starts with space. */
616 && ((XSTRING_CHAR_LENGTH (string) > 0 &&
617 string_char (XSTRING (string), 0) == ' ')
618 || string_char (XSTRING (eltstring), 0) != ' ')
619 && (0 > scmp (XSTRING_DATA (eltstring), 621 && (0 > scmp (XSTRING_DATA (eltstring),
620 XSTRING_DATA (string), 622 XSTRING_DATA (string),
621 slength))) 623 slength)))
622 { 624 {
623 /* Yes. Now check whether predicate likes it. */ 625 /* Yes. Now check whether predicate likes it. */
624 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; 626 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
625 int loser; 627 int loser;
626 GCPRO4 (tail, eltstring, allmatches, string); 628 GCPRO4 (tail, eltstring, allmatches, string);
627 loser = ignore_completion_p (eltstring, pred, elt); 629 loser = ignore_completion_p (eltstring, predicate, elt);
628 UNGCPRO; 630 UNGCPRO;
629 if (!loser) 631 if (!loser)
630 /* Ok => put it on the list. */ 632 /* Ok => put it on the list. */
631 allmatches = Fcons (eltstring, allmatches); 633 allmatches = Fcons (eltstring, allmatches);
632 } 634 }