comparison src/minibuf.c @ 53251:9bb418538a0f

(read_minibuf): Allow INITIAL to be a cons of a string and an integer. Adapt the docstring accordingly. (Fread_from_minibuffer): Delete code moved into read_minibuf. Doc fix. (Fread_minibuffer, Fread_no_blanks_input): Adapt to changes in read_minibuf. (Fcompleting_read): Delete code moved into read_minibuf. (Ftest_completion): Make it handle obarrays and hash tables correctly.
author Luc Teirlinck <teirllm@auburn.edu>
date Tue, 23 Dec 2003 22:20:04 +0000
parents 695cf19ef79e
children 93fc4ea3546c
comparison
equal deleted inserted replaced
53250:d0090d33e7d8 53251:9bb418538a0f
402 if (PT < prompt_end) 402 if (PT < prompt_end)
403 error ("Cannot do completion in the prompt"); 403 error ("Cannot do completion in the prompt");
404 return make_buffer_string (prompt_end, PT, 1); 404 return make_buffer_string (prompt_end, PT, 1);
405 } 405 }
406 406
407 /* Read from the minibuffer using keymap MAP, initial contents INITIAL 407 /* Read from the minibuffer using keymap MAP and initial contents INITIAL,
408 (a string), putting point minus BACKUP_N bytes from the end of INITIAL, 408 putting point minus BACKUP_N bytes from the end of INITIAL,
409 prompting with PROMPT (a string), using history list HISTVAR 409 prompting with PROMPT (a string), using history list HISTVAR
410 with initial position HISTPOS. (BACKUP_N should be <= 0.) 410 with initial position HISTPOS. INITIAL should be a string or a
411 cons of a string and an integer. BACKUP_N should be <= 0, or
412 Qnil, which is equivalent to 0. If INITIAL is a cons, BACKUP_N is
413 ignored and replaced with an integer that puts point N characters
414 from the beginning of INITIAL, where N is the CDR of INITIAL, or at
415 the beginning of INITIAL if N <= 0.
411 416
412 Normally return the result as a string (the text that was read), 417 Normally return the result as a string (the text that was read),
413 but if EXPFLAG is nonzero, read it and return the object read. 418 but if EXPFLAG is nonzero, read it and return the object read.
414 If HISTVAR is given, save the value read on that history only if it doesn't 419 If HISTVAR is given, save the value read on that history only if it doesn't
415 match the front of that history list exactly. The value is pushed onto 420 match the front of that history list exactly. The value is pushed onto
417 422
418 DEFALT specifies the default value for the sake of history commands. 423 DEFALT specifies the default value for the sake of history commands.
419 424
420 If ALLOW_PROPS is nonzero, we do not throw away text properties. 425 If ALLOW_PROPS is nonzero, we do not throw away text properties.
421 426
422 if INHERIT_INPUT_METHOD is nonzeor, the minibuffer inherit the 427 if INHERIT_INPUT_METHOD is nonzero, the minibuffer inherits the
423 current input method. */ 428 current input method. */
424 429
425 static Lisp_Object 430 static Lisp_Object
426 read_minibuf (map, initial, prompt, backup_n, expflag, 431 read_minibuf (map, initial, prompt, backup_n, expflag,
427 histvar, histpos, defalt, allow_props, inherit_input_method) 432 histvar, histpos, defalt, allow_props, inherit_input_method)
439 Lisp_Object val; 444 Lisp_Object val;
440 int count = SPECPDL_INDEX (); 445 int count = SPECPDL_INDEX ();
441 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method; 446 Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
442 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; 447 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
443 Lisp_Object enable_multibyte; 448 Lisp_Object enable_multibyte;
449 int pos = INTEGERP (backup_n) ? XINT (backup_n) : 0;
444 450
445 /* String to add to the history. */ 451 /* String to add to the history. */
446 Lisp_Object histstring; 452 Lisp_Object histstring;
447 453
448 extern Lisp_Object Qfront_sticky; 454 extern Lisp_Object Qfront_sticky;
454 #ifdef HAVE_X_WINDOWS 460 #ifdef HAVE_X_WINDOWS
455 if (display_hourglass_p) 461 if (display_hourglass_p)
456 cancel_hourglass (); 462 cancel_hourglass ();
457 #endif 463 #endif
458 464
465 if (!NILP (initial))
466 {
467 if (CONSP (initial))
468 {
469 backup_n = Fcdr (initial);
470 initial = Fcar (initial);
471 CHECK_STRING (initial);
472 if (!NILP (backup_n))
473 {
474 CHECK_NUMBER (backup_n);
475 /* Convert to distance from end of input. */
476 if (XINT (backup_n) < 1)
477 /* A number too small means the beginning of the string. */
478 pos = - SCHARS (initial);
479 else
480 pos = XINT (backup_n) - 1 - SCHARS (initial);
481 }
482 }
483 else
484 CHECK_STRING (initial);
485 }
459 val = Qnil; 486 val = Qnil;
460 ambient_dir = current_buffer->directory; 487 ambient_dir = current_buffer->directory;
461 input_method = Qnil; 488 input_method = Qnil;
462 enable_multibyte = Qnil; 489 enable_multibyte = Qnil;
463 490
480 build_string ("Command attempted to use minibuffer while in minibuffer")); 507 build_string ("Command attempted to use minibuffer while in minibuffer"));
481 } 508 }
482 509
483 if (noninteractive) 510 if (noninteractive)
484 { 511 {
485 val = read_minibuf_noninteractive (map, initial, prompt, backup_n, 512 val = read_minibuf_noninteractive (map, initial, prompt,
513 make_number (pos),
486 expflag, histvar, histpos, defalt, 514 expflag, histvar, histpos, defalt,
487 allow_props, inherit_input_method); 515 allow_props, inherit_input_method);
488 UNGCPRO; 516 UNGCPRO;
489 return unbind_to (count, val); 517 return unbind_to (count, val);
490 } 518 }
631 659
632 /* Put in the initial input. */ 660 /* Put in the initial input. */
633 if (!NILP (initial)) 661 if (!NILP (initial))
634 { 662 {
635 Finsert (1, &initial); 663 Finsert (1, &initial);
636 if (INTEGERP (backup_n)) 664 Fforward_char (make_number (pos));
637 Fforward_char (backup_n);
638 } 665 }
639 666
640 clear_message (1, 1); 667 clear_message (1, 1);
641 current_buffer->keymap = map; 668 current_buffer->keymap = map;
642 669
882 In that case, HISTVAR is the history list variable to use, 909 In that case, HISTVAR is the history list variable to use,
883 and HISTPOS is the initial position (the position in the list 910 and HISTPOS is the initial position (the position in the list
884 which INITIAL-CONTENTS corresponds to). 911 which INITIAL-CONTENTS corresponds to).
885 Positions are counted starting from 1 at the beginning of the list. 912 Positions are counted starting from 1 at the beginning of the list.
886 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available 913 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
887 for history commands; but `read-from-minibuffer' does NOT return DEFAULT-VALUE 914 for history commands; but, unless READ is non-nil, `read-from-minibuffer'
888 if the user enters empty input! It returns the empty string. 915 does NOT return DEFAULT-VALUE if the user enters empty input! It returns
916 the empty string.
889 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits 917 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
890 the current input method and the setting of `enable-multibyte-characters'. 918 the current input method and the setting of `enable-multibyte-characters'.
891 If the variable `minibuffer-allow-text-properties' is non-nil, 919 If the variable `minibuffer-allow-text-properties' is non-nil,
892 then the string which is returned includes whatever text properties 920 then the string which is returned includes whatever text properties
893 were present in the minibuffer. Otherwise the value has no text properties. */) 921 were present in the minibuffer. Otherwise the value has no text properties. */)
894 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method) 922 (prompt, initial_contents, keymap, read, hist, default_value, inherit_input_method)
895 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value; 923 Lisp_Object prompt, initial_contents, keymap, read, hist, default_value;
896 Lisp_Object inherit_input_method; 924 Lisp_Object inherit_input_method;
897 { 925 {
898 int pos = 0; 926 Lisp_Object histvar, histpos, val;
899 Lisp_Object histvar, histpos, position, val;
900 struct gcpro gcpro1; 927 struct gcpro gcpro1;
901 928
902 position = Qnil;
903
904 CHECK_STRING (prompt); 929 CHECK_STRING (prompt);
905 if (!NILP (initial_contents))
906 {
907 if (CONSP (initial_contents))
908 {
909 position = Fcdr (initial_contents);
910 initial_contents = Fcar (initial_contents);
911 }
912 CHECK_STRING (initial_contents);
913 if (!NILP (position))
914 {
915 CHECK_NUMBER (position);
916 /* Convert to distance from end of input. */
917 if (XINT (position) < 1)
918 /* A number too small means the beginning of the string. */
919 pos = - SCHARS (initial_contents);
920 else
921 pos = XINT (position) - 1 - SCHARS (initial_contents);
922 }
923 }
924
925 if (NILP (keymap)) 930 if (NILP (keymap))
926 keymap = Vminibuffer_local_map; 931 keymap = Vminibuffer_local_map;
927 else 932 else
928 keymap = get_keymap (keymap, 1, 0); 933 keymap = get_keymap (keymap, 1, 0);
929 934
942 if (NILP (histpos)) 947 if (NILP (histpos))
943 XSETFASTINT (histpos, 0); 948 XSETFASTINT (histpos, 0);
944 949
945 GCPRO1 (default_value); 950 GCPRO1 (default_value);
946 val = read_minibuf (keymap, initial_contents, prompt, 951 val = read_minibuf (keymap, initial_contents, prompt,
947 make_number (pos), !NILP (read), 952 Qnil, !NILP (read),
948 histvar, histpos, default_value, 953 histvar, histpos, default_value,
949 minibuffer_allow_text_properties, 954 minibuffer_allow_text_properties,
950 !NILP (inherit_input_method)); 955 !NILP (inherit_input_method));
951 UNGCPRO; 956 UNGCPRO;
952 return val; 957 return val;
958 is a string to insert in the minibuffer before reading. */) 963 is a string to insert in the minibuffer before reading. */)
959 (prompt, initial_contents) 964 (prompt, initial_contents)
960 Lisp_Object prompt, initial_contents; 965 Lisp_Object prompt, initial_contents;
961 { 966 {
962 CHECK_STRING (prompt); 967 CHECK_STRING (prompt);
963 if (!NILP (initial_contents))
964 CHECK_STRING (initial_contents);
965 return read_minibuf (Vminibuffer_local_map, initial_contents, 968 return read_minibuf (Vminibuffer_local_map, initial_contents,
966 prompt, Qnil, 1, Qminibuffer_history, 969 prompt, Qnil, 1, Qminibuffer_history,
967 make_number (0), Qnil, 0, 0); 970 make_number (0), Qnil, 0, 0);
968 } 971 }
969 972
1010 the current input method and the setting of `enable-multibyte-characters'. */) 1013 the current input method and the setting of `enable-multibyte-characters'. */)
1011 (prompt, initial, inherit_input_method) 1014 (prompt, initial, inherit_input_method)
1012 Lisp_Object prompt, initial, inherit_input_method; 1015 Lisp_Object prompt, initial, inherit_input_method;
1013 { 1016 {
1014 CHECK_STRING (prompt); 1017 CHECK_STRING (prompt);
1015 if (! NILP (initial))
1016 CHECK_STRING (initial);
1017
1018 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil, 1018 return read_minibuf (Vminibuffer_local_ns_map, initial, prompt, Qnil,
1019 0, Qminibuffer_history, make_number (0), Qnil, 0, 1019 0, Qminibuffer_history, make_number (0), Qnil, 0,
1020 !NILP (inherit_input_method)); 1020 !NILP (inherit_input_method));
1021 } 1021 }
1022 1022
1576 `completion-ignore-case' is non-nil. */) 1576 `completion-ignore-case' is non-nil. */)
1577 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method) 1577 (prompt, table, predicate, require_match, initial_input, hist, def, inherit_input_method)
1578 Lisp_Object prompt, table, predicate, require_match, initial_input; 1578 Lisp_Object prompt, table, predicate, require_match, initial_input;
1579 Lisp_Object hist, def, inherit_input_method; 1579 Lisp_Object hist, def, inherit_input_method;
1580 { 1580 {
1581 Lisp_Object val, histvar, histpos, position; 1581 Lisp_Object val, histvar, histpos;
1582 Lisp_Object init;
1583 int pos = 0;
1584 int count = SPECPDL_INDEX (); 1582 int count = SPECPDL_INDEX ();
1585 struct gcpro gcpro1; 1583 struct gcpro gcpro1;
1586 1584
1587 init = initial_input;
1588 GCPRO1 (def); 1585 GCPRO1 (def);
1589 1586
1590 specbind (Qminibuffer_completion_table, table); 1587 specbind (Qminibuffer_completion_table, table);
1591 specbind (Qminibuffer_completion_predicate, predicate); 1588 specbind (Qminibuffer_completion_predicate, predicate);
1592 specbind (Qminibuffer_completion_confirm, 1589 specbind (Qminibuffer_completion_confirm,
1593 EQ (require_match, Qt) ? Qnil : require_match); 1590 EQ (require_match, Qt) ? Qnil : require_match);
1594 last_exact_completion = Qnil; 1591 last_exact_completion = Qnil;
1595 1592
1596 position = Qnil;
1597 if (!NILP (init))
1598 {
1599 if (CONSP (init))
1600 {
1601 position = Fcdr (init);
1602 init = Fcar (init);
1603 }
1604 CHECK_STRING (init);
1605 if (!NILP (position))
1606 {
1607 CHECK_NUMBER (position);
1608 /* Convert to distance from end of input. */
1609 pos = XINT (position) - SCHARS (init);
1610 }
1611 }
1612
1613 if (SYMBOLP (hist)) 1593 if (SYMBOLP (hist))
1614 { 1594 {
1615 histvar = hist; 1595 histvar = hist;
1616 histpos = Qnil; 1596 histpos = Qnil;
1617 } 1597 }
1626 XSETFASTINT (histpos, 0); 1606 XSETFASTINT (histpos, 0);
1627 1607
1628 val = read_minibuf (NILP (require_match) 1608 val = read_minibuf (NILP (require_match)
1629 ? Vminibuffer_local_completion_map 1609 ? Vminibuffer_local_completion_map
1630 : Vminibuffer_local_must_match_map, 1610 : Vminibuffer_local_must_match_map,
1631 init, prompt, make_number (pos), 0, 1611 initial_input, prompt, Qnil, 0,
1632 histvar, histpos, def, 0, 1612 histvar, histpos, def, 0,
1633 !NILP (inherit_input_method)); 1613 !NILP (inherit_input_method));
1634 1614
1635 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def)) 1615 if (STRINGP (val) && SCHARS (val) == 0 && ! NILP (def))
1636 val = def; 1616 val = def;
1648 If ALIST is a function, it is called with three arguments: 1628 If ALIST is a function, it is called with three arguments:
1649 the values STRING, PREDICATE and `lambda'. */) 1629 the values STRING, PREDICATE and `lambda'. */)
1650 (string, alist, predicate) 1630 (string, alist, predicate)
1651 Lisp_Object string, alist, predicate; 1631 Lisp_Object string, alist, predicate;
1652 { 1632 {
1653 Lisp_Object regexps, tem = Qnil; 1633 Lisp_Object regexps, tail, tem = Qnil;
1654 int i = 0; 1634 int i = 0;
1655 1635
1656 CHECK_STRING (string); 1636 CHECK_STRING (string);
1657 1637
1658 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist)))) 1638 if ((CONSP (alist) && (!SYMBOLP (XCAR (alist)) || NILP (XCAR (alist))))
1674 if (STRING_MULTIBYTE (string)) 1654 if (STRING_MULTIBYTE (string))
1675 string = Fstring_make_unibyte (string); 1655 string = Fstring_make_unibyte (string);
1676 else 1656 else
1677 string = Fstring_make_multibyte (string); 1657 string = Fstring_make_multibyte (string);
1678 1658
1679 tem = oblookup (Vminibuffer_completion_table, 1659 tem = oblookup (alist,
1680 SDATA (string), 1660 SDATA (string),
1681 SCHARS (string), 1661 SCHARS (string),
1682 SBYTES (string)); 1662 SBYTES (string));
1683 if (!SYMBOLP (tem))
1684 return Qnil;
1685 } 1663 }
1664
1665 if (completion_ignore_case && !SYMBOLP (tem))
1666 {
1667 for (i = XVECTOR (alist)->size - 1; i >= 0; i--)
1668 {
1669 tail = XVECTOR (alist)->contents[i];
1670 if (SYMBOLP (tail))
1671 while (1)
1672 {
1673 if (EQ((Fcompare_strings (string, make_number (0), Qnil,
1674 Fsymbol_name (tail),
1675 make_number (0) , Qnil, Qt)),
1676 Qt))
1677 {
1678 tem = tail;
1679 break;
1680 }
1681 if (XSYMBOL (tail)->next == 0)
1682 break;
1683 XSETSYMBOL (tail, XSYMBOL (tail)->next);
1684 }
1685 }
1686 }
1687
1688 if (!SYMBOLP (tem))
1689 return Qnil;
1686 } 1690 }
1687 else if (HASH_TABLE_P (alist)) 1691 else if (HASH_TABLE_P (alist))
1688 { 1692 {
1689 i = hash_lookup (XHASH_TABLE (alist), string, NULL); 1693 struct Lisp_Hash_Table *h = XHASH_TABLE (alist);
1694 i = hash_lookup (h, string, NULL);
1690 if (i >= 0) 1695 if (i >= 0)
1691 tem = HASH_KEY (XHASH_TABLE (alist), i); 1696 tem = HASH_KEY (h, i);
1692 else 1697 else
1698 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
1699 if (!NILP (HASH_HASH (h, i)) &&
1700 EQ (Fcompare_strings (string, make_number (0), Qnil,
1701 HASH_KEY (h, i), make_number (0) , Qnil,
1702 completion_ignore_case ? Qt : Qnil),
1703 Qt))
1704 {
1705 tem = HASH_KEY (h, i);
1706 break;
1707 }
1708 if (!STRINGP (tem))
1693 return Qnil; 1709 return Qnil;
1694 } 1710 }
1695 else 1711 else
1696 return call3 (alist, string, predicate, Qlambda); 1712 return call3 (alist, string, predicate, Qlambda);
1697 1713