2003-08-20  Yasuhiro Ohara <yasu@sfc.wide.ad.jp>

	* command.c: Fix <cr> display problem for command line
	  description.
diff --git a/lib/command.c b/lib/command.c
index e8c9203..8c60fc4 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -1529,6 +1529,9 @@
   vector matchvec;
   struct cmd_element *cmd_element;
   int index;
+  int ret;
+  enum match_type match;
+  char *command;
   static struct desc desc_cr = { "<cr>", "" };
 
   /* Set index. */
@@ -1541,14 +1544,10 @@
   matchvec = vector_init (INIT_MATCHVEC_SIZE);
 
   /* Filter commands. */
+  /* Only words precedes current word will be checked in this loop. */
   for (i = 0; i < index; i++)
     {
-      enum match_type match;
-      char *command;
-      int ret;
-
       command = vector_slot (vline, i);
-
       match = cmd_filter_by_completion (command, cmd_vector, i);
 
       if (match == vararg_match)
@@ -1570,7 +1569,6 @@
 	      }
 
 	  vector_set (matchvec, &desc_cr);
-
 	  vector_free (cmd_vector);
 
 	  return matchvec;
@@ -1593,6 +1591,11 @@
   /* Prepare match vector */
   /*  matchvec = vector_init (INIT_MATCHVEC_SIZE); */
 
+  /* Make sure that cmd_vector is filtered based on current word */
+  command = vector_slot (vline, index);
+  if (command)
+    match = cmd_filter_by_completion (command, cmd_vector, index);
+
   /* Make description vector. */
   for (i = 0; i < vector_max (cmd_vector); i++)
     if ((cmd_element = vector_slot (cmd_vector, i)) != NULL)
@@ -1600,12 +1603,13 @@
 	char *string = NULL;
 	vector strvec = cmd_element->strvec;
 
-	if (index > vector_max (strvec))
+        /* if command is NULL, index may be equal to vector_max */
+	if (command && index >= vector_max (strvec))
 	  vector_slot (cmd_vector, i) = NULL;
 	else
 	  {
-	    /* Check is command is completed. */
-	    if (index == vector_max (strvec))
+	    /* Check if command is completed. */
+            if (command == NULL && index == vector_max (strvec))
 	      {
 		string = "<cr>";
 		if (! desc_unique_string (matchvec, string))
@@ -1620,7 +1624,7 @@
 		for (j = 0; j < vector_max (descvec); j++)
 		  {
 		    desc = vector_slot (descvec, j);
-		    string = cmd_entry_function_desc (vector_slot (vline, index), desc->cmd);
+		    string = cmd_entry_function_desc (command, desc->cmd);
 		    if (string)
 		      {
 			/* Uniqueness check */