1 /* complete.c -- filename completion for readline. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_SYS_FILE_H)
34 #if defined (HAVE_UNISTD_H)
36 #endif /* HAVE_UNISTD_H */
38 #if defined (HAVE_STDLIB_H)
41 # include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
56 #include "posixstat.h"
58 /* System-specific feature definitions and include files. */
62 /* Some standard library routines. */
65 #include "rlprivate.h"
68 typedef int QSFUNC (const void *, const void *);
70 typedef int QSFUNC ();
79 /* Unix version of a hidden file. Could be different on other systems. */
80 #define HIDDEN_FILE(fname) ((fname)[0] == '.')
82 /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
84 #if defined(HAVE_GETPWENT) && \
85 (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE))
86 extern struct passwd *getpwent PARAMS((void));
87 #endif /* defiend (HAVE_GETPWENT) && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */
89 /* If non-zero, then this is the address of a function to call when
90 completing a word would normally display the list of possible matches.
91 This function is called instead of actually doing the display.
92 It takes three arguments: (char **matches, int num_matches, int max_length)
93 where MATCHES is the array of strings that matched, NUM_MATCHES is the
94 number of strings in that array, and MAX_LENGTH is the length of the
95 longest string in that array. */
96 rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL;
98 #if defined (VISIBLE_STATS)
102 static int stat_char PARAMS((char *));
105 static char *rl_quote_filename PARAMS((char *, int, char *));
107 static void set_completion_defaults PARAMS((int));
108 static int get_y_or_n PARAMS((int));
109 static int _rl_internal_pager PARAMS((int));
110 static char *printable_part PARAMS((char *));
111 static int print_filename PARAMS((char *, char *));
113 static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
115 static char **remove_duplicate_matches PARAMS((char **));
116 static void insert_match PARAMS((char *, int, int, char *));
117 static int append_to_match PARAMS((char *, int, int, int));
118 static void insert_all_matches PARAMS((char **, int, char *));
119 static void display_matches PARAMS((char **));
120 static int compute_lcd_of_matches PARAMS((char **, int, const char *));
121 static int postprocess_matches PARAMS((char ***, int));
123 static char *make_quoted_replacement PARAMS((char *, int, char *));
125 /* **************************************************************** */
127 /* Completion matching, from readline's point of view. */
129 /* **************************************************************** */
131 /* Variables known only to the readline library. */
133 /* If non-zero, non-unique completions always show the list of matches. */
134 int _rl_complete_show_all = 0;
136 /* If non-zero, completed directory names have a slash appended. */
137 int _rl_complete_mark_directories = 1;
139 /* If non-zero, the symlinked directory completion behavior introduced in
140 readline-4.2a is disabled, and symlinks that point to directories have
141 a slash appended (subject to the value of _rl_complete_mark_directories).
142 This is user-settable via the mark-symlinked-directories variable. */
143 int _rl_complete_mark_symlink_dirs = 0;
145 /* If non-zero, completions are printed horizontally in alphabetical order,
147 int _rl_print_completions_horizontally;
149 /* Non-zero means that case is not significant in filename completion. */
150 #if defined (__MSDOS__) && !defined (__DJGPP__)
151 int _rl_completion_case_fold = 1;
153 int _rl_completion_case_fold;
156 /* If non-zero, don't match hidden files (filenames beginning with a `.' on
157 Unix) when doing filename completion. */
158 int _rl_match_hidden_files = 1;
160 /* Global variables available to applications using readline. */
162 #if defined (VISIBLE_STATS)
163 /* Non-zero means add an additional character to each filename displayed
164 during listing completion iff rl_filename_completion_desired which helps
165 to indicate the type of file being listed. */
166 int rl_visible_stats = 0;
167 #endif /* VISIBLE_STATS */
169 /* If non-zero, then this is the address of a function to call when
170 completing on a directory name. The function is called with
171 the address of a string (the current directory name) as an arg. */
172 rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
174 rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
176 /* Non-zero means readline completion functions perform tilde expansion. */
177 int rl_complete_with_tilde_expansion = 0;
179 /* Pointer to the generator function for completion_matches ().
180 NULL means to use rl_filename_completion_function (), the default filename
182 rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
184 /* Pointer to alternative function to create matches.
185 Function is called with TEXT, START, and END.
186 START and END are indices in RL_LINE_BUFFER saying what the boundaries
188 If this function exists and returns NULL then call the value of
189 rl_completion_entry_function to try to match, otherwise use the
190 array of strings returned. */
191 rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;
193 /* Non-zero means to suppress normal filename completion after the
194 user-specified completion function has been called. */
195 int rl_attempted_completion_over = 0;
197 /* Set to a character indicating the type of completion being performed
198 by rl_complete_internal, available for use by application completion
200 int rl_completion_type = 0;
202 /* Up to this many items will be displayed in response to a
203 possible-completions call. After that, we ask the user if
204 she is sure she wants to see them all. */
205 int rl_completion_query_items = 100;
207 int _rl_page_completions = 1;
209 /* The basic list of characters that signal a break between words for the
210 completer routine. The contents of this variable is what breaks words
211 in the shell, i.e. " \t\n\"\\'`@$><=" */
212 const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
214 /* List of basic quoting characters. */
215 const char *rl_basic_quote_characters = "\"'";
217 /* The list of characters that signal a break between words for
218 rl_complete_internal. The default list is the contents of
219 rl_basic_word_break_characters. */
220 const char *rl_completer_word_break_characters = (const char *)NULL;
222 /* List of characters which can be used to quote a substring of the line.
223 Completion occurs on the entire substring, and within the substring
224 rl_completer_word_break_characters are treated as any other character,
225 unless they also appear within this list. */
226 const char *rl_completer_quote_characters = (const char *)NULL;
228 /* List of characters that should be quoted in filenames by the completer. */
229 const char *rl_filename_quote_characters = (const char *)NULL;
231 /* List of characters that are word break characters, but should be left
232 in TEXT when it is passed to the completion function. The shell uses
233 this to help determine what kind of completing to do. */
234 const char *rl_special_prefixes = (const char *)NULL;
236 /* If non-zero, then disallow duplicates in the matches. */
237 int rl_ignore_completion_duplicates = 1;
239 /* Non-zero means that the results of the matches are to be treated
240 as filenames. This is ALWAYS zero on entry, and can only be changed
241 within a completion entry finder function. */
242 int rl_filename_completion_desired = 0;
244 /* Non-zero means that the results of the matches are to be quoted using
245 double quotes (or an application-specific quoting mechanism) if the
246 filename contains any characters in rl_filename_quote_chars. This is
247 ALWAYS non-zero on entry, and can only be changed within a completion
248 entry finder function. */
249 int rl_filename_quoting_desired = 1;
251 /* This function, if defined, is called by the completer when real
252 filename completion is done, after all the matching names have been
253 generated. It is passed a (char**) known as matches in the code below.
254 It consists of a NULL-terminated array of pointers to potential
255 matching strings. The 1st element (matches[0]) is the maximal
256 substring that is common to all matches. This function can re-arrange
257 the list of matches as required, but all elements of the array must be
258 free()'d if they are deleted. The main intent of this function is
259 to implement FIGNORE a la SunOS csh. */
260 rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;
262 /* Set to a function to quote a filename in an application-specific fashion.
263 Called with the text to quote, the type of match found (single or multiple)
264 and a pointer to the quoting character to be used, which the function can
266 rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
268 /* Function to call to remove quoting characters from a filename. Called
269 before completion is attempted, so the embedded quotes do not interfere
270 with matching names in the file system. Readline doesn't do anything
271 with this; it's set only by applications. */
272 rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;
274 /* Function to call to decide whether or not a word break character is
275 quoted. If a character is quoted, it does not break words for the
277 rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
279 /* If non-zero, the completion functions don't append anything except a
280 possible closing quote. This is set to 0 by rl_complete_internal and
281 may be changed by an application-specific completion function. */
282 int rl_completion_suppress_append = 0;
284 /* Character appended to completed words when at the end of the line. The
285 default is a space. */
286 int rl_completion_append_character = ' ';
288 /* If non-zero, a slash will be appended to completed filenames that are
289 symbolic links to directory names, subject to the value of the
290 mark-directories variable (which is user-settable). This exists so
291 that application completion functions can override the user's preference
292 (set via the mark-symlinked-directories variable) if appropriate.
293 It's set to the value of _rl_complete_mark_symlink_dirs in
294 rl_complete_internal before any application-specific completion
295 function is called, so without that function doing anything, the user's
296 preferences are honored. */
297 int rl_completion_mark_symlink_dirs;
299 /* If non-zero, inhibit completion (temporarily). */
300 int rl_inhibit_completion;
302 /* Variables local to this file. */
304 /* Local variable states what happened during the last completion attempt. */
305 static int completion_changed_buffer;
307 /*************************************/
309 /* Bindable completion functions */
311 /*************************************/
313 /* Complete the word at or before point. You have supplied the function
314 that does the initial simple matching selection algorithm (see
315 rl_completion_matches ()). The default is to do filename completion. */
317 rl_complete (ignore, invoking_key)
318 int ignore, invoking_key;
320 if (rl_inhibit_completion)
321 return (_rl_insert_char (ignore, invoking_key));
322 else if (rl_last_func == rl_complete && !completion_changed_buffer)
323 return (rl_complete_internal ('?'));
324 else if (_rl_complete_show_all)
325 return (rl_complete_internal ('!'));
327 return (rl_complete_internal (TAB));
330 /* List the possible completions. See description of rl_complete (). */
332 rl_possible_completions (ignore, invoking_key)
333 int ignore, invoking_key;
335 return (rl_complete_internal ('?'));
339 rl_insert_completions (ignore, invoking_key)
340 int ignore, invoking_key;
342 return (rl_complete_internal ('*'));
345 /* Return the correct value to pass to rl_complete_internal performing
346 the same tests as rl_complete. This allows consecutive calls to an
347 application's completion function to list possible completions and for
348 an application-specific completion function to honor the
349 show-all-if-ambiguous readline variable. */
351 rl_completion_mode (cfunc)
352 rl_command_func_t *cfunc;
354 if (rl_last_func == cfunc && !completion_changed_buffer)
356 else if (_rl_complete_show_all)
362 /************************************/
364 /* Completion utility functions */
366 /************************************/
368 /* Set default values for readline word completion. These are the variables
369 that application completion functions can change or inspect. */
371 set_completion_defaults (what_to_do)
374 /* Only the completion entry function can change these. */
375 rl_filename_completion_desired = 0;
376 rl_filename_quoting_desired = 1;
377 rl_completion_type = what_to_do;
378 rl_completion_suppress_append = 0;
380 /* The completion entry function may optionally change this. */
381 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
384 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
386 get_y_or_n (for_pager)
393 RL_SETSTATE(RL_STATE_MOREINPUT);
395 RL_UNSETSTATE(RL_STATE_MOREINPUT);
397 if (c == 'y' || c == 'Y' || c == ' ')
399 if (c == 'n' || c == 'N' || c == RUBOUT)
402 _rl_abort_internal ();
403 if (for_pager && (c == NEWLINE || c == RETURN))
405 if (for_pager && (c == 'q' || c == 'Q'))
412 _rl_internal_pager (lines)
417 fprintf (rl_outstream, "--More--");
418 fflush (rl_outstream);
420 _rl_erase_entire_line ();
429 #if defined (VISIBLE_STATS)
430 /* Return the character which best describes FILENAME.
431 `@' for symbolic links
436 `%' for character special devices
437 `#' for block special devices */
445 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
446 r = lstat (filename, &finfo);
448 r = stat (filename, &finfo);
455 if (S_ISDIR (finfo.st_mode))
457 #if defined (S_ISCHR)
458 else if (S_ISCHR (finfo.st_mode))
461 #if defined (S_ISBLK)
462 else if (S_ISBLK (finfo.st_mode))
465 #if defined (S_ISLNK)
466 else if (S_ISLNK (finfo.st_mode))
469 #if defined (S_ISSOCK)
470 else if (S_ISSOCK (finfo.st_mode))
472 #endif /* S_ISSOCK */
473 #if defined (S_ISFIFO)
474 else if (S_ISFIFO (finfo.st_mode))
477 else if (S_ISREG (finfo.st_mode))
479 if (access (filename, X_OK) == 0)
484 #endif /* VISIBLE_STATS */
486 /* Return the portion of PATHNAME that should be output when listing
487 possible completions. If we are hacking filename completion, we
488 are only interested in the basename, the portion following the
489 final slash. Otherwise, we return what we were passed. Since
490 printing empty strings is not very informative, if we're doing
491 filename completion, and the basename is the empty string, we look
492 for the previous slash and return the portion following that. If
493 there's no previous slash, we just return what we were passed. */
495 printable_part (pathname)
500 if (rl_filename_completion_desired == 0) /* don't need to do anything */
503 temp = strrchr (pathname, '/');
504 #if defined (__MSDOS__)
505 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
509 if (temp == 0 || *temp == '\0')
511 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
512 Look for a previous slash and, if one is found, return the portion
513 following that slash. If there's no previous slash, just return the
514 pathname we were passed. */
515 else if (temp[1] == '\0')
517 for (x = temp - 1; x > pathname; x--)
520 return ((*x == '/') ? x + 1 : pathname);
526 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
527 are using it, check for and output a single character for `special'
528 filenames. Return the number of characters we output. */
534 putc ('^', rl_outstream); \
535 putc (UNCTRL (c), rl_outstream); \
538 else if (c == RUBOUT) \
540 putc ('^', rl_outstream); \
541 putc ('?', rl_outstream); \
546 putc (c, rl_outstream); \
552 print_filename (to_print, full_pathname)
553 char *to_print, *full_pathname;
556 #if !defined (VISIBLE_STATS)
559 for (s = to_print; *s; s++)
564 char *s, c, *new_full_pathname;
565 int extension_char, slen, tlen;
567 for (s = to_print; *s; s++)
572 if (rl_filename_completion_desired && rl_visible_stats)
574 /* If to_print != full_pathname, to_print is the basename of the
575 path passed. In this case, we try to expand the directory
576 name before checking for the stat character. */
577 if (to_print != full_pathname)
579 /* Terminate the directory name. */
583 /* If setting the last slash in full_pathname to a NUL results in
584 full_pathname being the empty string, we are trying to complete
585 files in the root directory. If we pass a null string to the
586 bash directory completion hook, for example, it will expand it
587 to the current directory. We just want the `/'. */
588 s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/");
589 if (rl_directory_completion_hook)
590 (*rl_directory_completion_hook) (&s);
593 tlen = strlen (to_print);
594 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
595 strcpy (new_full_pathname, s);
596 new_full_pathname[slen] = '/';
597 strcpy (new_full_pathname + slen + 1, to_print);
599 extension_char = stat_char (new_full_pathname);
601 free (new_full_pathname);
606 s = tilde_expand (full_pathname);
607 extension_char = stat_char (s);
613 putc (extension_char, rl_outstream);
617 #endif /* VISIBLE_STATS */
622 rl_quote_filename (s, rtype, qcp)
629 r = (char *)xmalloc (strlen (s) + 2);
630 *r = *rl_completer_quote_characters;
633 *qcp = *rl_completer_quote_characters;
637 /* Find the bounds of the current word for completion purposes, and leave
638 rl_point set to the end of the word. This function skips quoted
639 substrings (characters between matched pairs of characters in
640 rl_completer_quote_characters). First we try to find an unclosed
641 quoted substring on which to do matching. If one is not found, we use
642 the word break characters to find the boundaries of the current word.
643 We call an application-specific function to decide whether or not a
644 particular word break character is quoted; if that function returns a
645 non-zero result, the character does not break a word. This function
646 returns the opening quote character if we found an unclosed quoted
647 substring, '\0' otherwise. FP, if non-null, is set to a value saying
648 which (shell-like) quote characters we found (single quote, double
649 quote, or backslash) anywhere in the string. DP, if non-null, is set to
650 the value of the delimiter character that caused a word break. */
653 _rl_find_completion_word (fp, dp)
656 int scan, end, found_quote, delimiter, pass_next, isbrk;
660 found_quote = delimiter = 0;
663 if (rl_completer_quote_characters)
665 /* We have a list of characters which can be used in pairs to
666 quote substrings for the completer. Try to find the start
667 of an unclosed quoted substring. */
668 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
669 for (scan = pass_next = 0; scan < end; scan++)
677 /* Shell-like semantics for single quotes -- don't allow backslash
678 to quote anything in single quotes, especially not the closing
679 quote. If you don't like this, take out the check on the value
681 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
684 found_quote |= RL_QF_BACKSLASH;
688 if (quote_char != '\0')
690 /* Ignore everything until the matching close quote char. */
691 if (rl_line_buffer[scan] == quote_char)
693 /* Found matching close. Abandon this substring. */
698 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
700 /* Found start of a quoted substring. */
701 quote_char = rl_line_buffer[scan];
703 /* Shell-like quoting conventions. */
704 if (quote_char == '\'')
705 found_quote |= RL_QF_SINGLE_QUOTE;
706 else if (quote_char == '"')
707 found_quote |= RL_QF_DOUBLE_QUOTE;
709 found_quote |= RL_QF_OTHER_QUOTE;
714 if (rl_point == end && quote_char == '\0')
716 /* We didn't find an unclosed quoted substring upon which to do
717 completion, so use the word break characters to find the
718 substring on which to complete. */
719 #if defined (HANDLE_MULTIBYTE)
720 while (rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_ANY))
725 scan = rl_line_buffer[rl_point];
727 if (strchr (rl_completer_word_break_characters, scan) == 0)
730 /* Call the application-specific function to tell us whether
731 this word break character is quoted and should be skipped. */
732 if (rl_char_is_quoted_p && found_quote &&
733 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
736 /* Convoluted code, but it avoids an n^2 algorithm with calls
737 to char_is_quoted. */
742 /* If we are at an unquoted word break, then advance past it. */
743 scan = rl_line_buffer[rl_point];
745 /* If there is an application-specific function to say whether or not
746 a character is quoted and we found a quote character, let that
747 function decide whether or not a character is a word break, even
748 if it is found in rl_completer_word_break_characters. Don't bother
749 if we're at the end of the line, though. */
752 if (rl_char_is_quoted_p)
753 isbrk = (found_quote == 0 ||
754 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
755 strchr (rl_completer_word_break_characters, scan) != 0;
757 isbrk = strchr (rl_completer_word_break_characters, scan) != 0;
761 /* If the character that caused the word break was a quoting
762 character, then remember it as the delimiter. */
763 if (rl_basic_quote_characters &&
764 strchr (rl_basic_quote_characters, scan) &&
765 (end - rl_point) > 1)
768 /* If the character isn't needed to determine something special
769 about what kind of completion to perform, then advance past it. */
770 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
784 gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
787 rl_compentry_func_t *our_func;
788 int found_quote, quote_char;
790 char **matches, *temp;
792 /* If the user wants to TRY to complete, but then wants to give
793 up and use the default completion function, they set the
794 variable rl_attempted_completion_function. */
795 if (rl_attempted_completion_function)
797 matches = (*rl_attempted_completion_function) (text, start, end);
799 if (matches || rl_attempted_completion_over)
801 rl_attempted_completion_over = 0;
806 /* Beware -- we're stripping the quotes here. Do this only if we know
807 we are doing filename completion and the application has defined a
808 filename dequoting function. */
811 if (found_quote && our_func == rl_filename_completion_function &&
812 rl_filename_dequoting_function)
814 /* delete single and double quotes */
815 temp = (*rl_filename_dequoting_function) (text, quote_char);
816 text = temp; /* not freeing text is not a memory leak */
819 matches = rl_completion_matches (text, our_func);
824 /* Filter out duplicates in MATCHES. This frees up the strings in
827 remove_duplicate_matches (matches)
835 /* Sort the items. */
836 for (i = 0; matches[i]; i++)
839 /* Sort the array without matches[0], since we need it to
840 stay in place no matter what. */
842 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
844 /* Remember the lowest common denominator for it may be unique. */
845 lowest_common = savestring (matches[0]);
847 for (i = newlen = 0; matches[i + 1]; i++)
849 if (strcmp (matches[i], matches[i + 1]) == 0)
852 matches[i] = (char *)&dead_slot;
858 /* We have marked all the dead slots with (char *)&dead_slot.
859 Copy all the non-dead entries into a new array. */
860 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
861 for (i = j = 1; matches[i]; i++)
863 if (matches[i] != (char *)&dead_slot)
864 temp_array[j++] = matches[i];
866 temp_array[j] = (char *)NULL;
868 if (matches[0] != (char *)&dead_slot)
871 /* Place the lowest common denominator back in [0]. */
872 temp_array[0] = lowest_common;
874 /* If there is one string left, and it is identical to the
875 lowest common denominator, then the LCD is the string to
877 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
879 free (temp_array[1]);
880 temp_array[1] = (char *)NULL;
885 /* Find the common prefix of the list of matches, and put it into
888 compute_lcd_of_matches (match_list, matches, text)
893 register int i, c1, c2, si;
894 int low; /* Count of max-matched characters. */
895 #if defined (HANDLE_MULTIBYTE)
901 /* If only one match, just use that. Otherwise, compare each
902 member of the list with the next, finding out where they
906 match_list[0] = match_list[1];
907 match_list[1] = (char *)NULL;
911 for (i = 1, low = 100000; i < matches; i++)
913 #if defined (HANDLE_MULTIBYTE)
914 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
916 memset (&ps1, 0, sizeof (mbstate_t));
917 memset (&ps2, 0, sizeof (mbstate_t));
920 if (_rl_completion_case_fold)
923 (c1 = _rl_to_lower(match_list[i][si])) &&
924 (c2 = _rl_to_lower(match_list[i + 1][si]));
926 #if defined (HANDLE_MULTIBYTE)
927 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
929 v = mbrtowc (&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
930 mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
931 wc1 = towlower (wc1);
932 wc2 = towlower (wc2);
946 (c1 = match_list[i][si]) &&
947 (c2 = match_list[i + 1][si]);
949 #if defined (HANDLE_MULTIBYTE)
950 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
952 mbstate_t ps_back = ps1;
953 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
955 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
968 /* If there were multiple matches, but none matched up to even the
969 first character, and the user typed something, use that as the
970 value of matches[0]. */
971 if (low == 0 && text && *text)
973 match_list[0] = (char *)xmalloc (strlen (text) + 1);
974 strcpy (match_list[0], text);
978 match_list[0] = (char *)xmalloc (low + 1);
980 /* XXX - this might need changes in the presence of multibyte chars */
982 /* If we are ignoring case, try to preserve the case of the string
983 the user typed in the face of multiple matches differing in case. */
984 if (_rl_completion_case_fold)
986 /* sort the list to get consistent answers. */
987 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
992 for (i = 1; i <= matches; i++)
993 if (strncmp (match_list[i], text, si) == 0)
995 strncpy (match_list[0], match_list[i], low);
998 /* no casematch, use first entry */
1000 strncpy (match_list[0], match_list[1], low);
1003 /* otherwise, just use the text the user typed. */
1004 strncpy (match_list[0], text, low);
1007 strncpy (match_list[0], match_list[1], low);
1009 match_list[0][low] = '\0';
1016 postprocess_matches (matchesp, matching_filenames)
1018 int matching_filenames;
1020 char *t, **matches, **temp_matches;
1023 matches = *matchesp;
1028 /* It seems to me that in all the cases we handle we would like
1029 to ignore duplicate possiblilities. Scan for the text to
1030 insert being identical to the other completions. */
1031 if (rl_ignore_completion_duplicates)
1033 temp_matches = remove_duplicate_matches (matches);
1035 matches = temp_matches;
1038 /* If we are matching filenames, then here is our chance to
1039 do clever processing by re-examining the list. Call the
1040 ignore function with the array as a parameter. It can
1041 munge the array, deleting matches as it desires. */
1042 if (rl_ignore_some_completions_function && matching_filenames)
1044 for (nmatch = 1; matches[nmatch]; nmatch++)
1046 (void)(*rl_ignore_some_completions_function) (matches);
1047 if (matches == 0 || matches[0] == 0)
1050 *matchesp = (char **)0;
1055 /* If we removed some matches, recompute the common prefix. */
1056 for (i = 1; matches[i]; i++)
1058 if (i > 1 && i < nmatch)
1061 compute_lcd_of_matches (matches, i - 1, t);
1067 *matchesp = matches;
1071 /* A convenience function for displaying a list of strings in
1072 columnar format on readline's output stream. MATCHES is the list
1073 of strings, in argv format, LEN is the number of strings in MATCHES,
1074 and MAX is the length of the longest string in MATCHES. */
1076 rl_display_match_list (matches, len, max)
1080 int count, limit, printed_len, lines;
1084 /* How many items of MAX length can we fit in the screen window? */
1086 limit = _rl_screenwidth / max;
1087 if (limit != 1 && (limit * max == _rl_screenwidth))
1090 /* Avoid a possible floating exception. If max > _rl_screenwidth,
1091 limit will be 0 and a divide-by-zero fault will result. */
1095 /* How many iterations of the printing loop? */
1096 count = (len + (limit - 1)) / limit;
1098 /* Watch out for special case. If LEN is less than LIMIT, then
1099 just do the inner printing loop.
1100 0 < len <= limit implies count = 1. */
1102 /* Sort the items if they are not already sorted. */
1103 if (rl_ignore_completion_duplicates == 0)
1104 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1109 if (_rl_print_completions_horizontally == 0)
1111 /* Print the sorted items, up-and-down alphabetically, like ls. */
1112 for (i = 1; i <= count; i++)
1114 for (j = 0, l = i; j < limit; j++)
1116 if (l > len || matches[l] == 0)
1120 temp = printable_part (matches[l]);
1121 printed_len = print_filename (temp, matches[l]);
1124 for (k = 0; k < max - printed_len; k++)
1125 putc (' ', rl_outstream);
1131 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1133 lines = _rl_internal_pager (lines);
1141 /* Print the sorted items, across alphabetically, like ls -x. */
1142 for (i = 1; matches[i]; i++)
1144 temp = printable_part (matches[i]);
1145 printed_len = print_filename (temp, matches[i]);
1146 /* Have we reached the end of this line? */
1149 if (i && (limit > 1) && (i % limit) == 0)
1153 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1155 lines = _rl_internal_pager (lines);
1161 for (k = 0; k < max - printed_len; k++)
1162 putc (' ', rl_outstream);
1169 /* Display MATCHES, a list of matching filenames in argv format. This
1170 handles the simple case -- a single match -- first. If there is more
1171 than one match, we compute the number of strings in the list and the
1172 length of the longest string, which will be needed by the display
1173 function. If the application wants to handle displaying the list of
1174 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1175 address of a function, and we just call it. If we're handling the
1176 display ourselves, we just call rl_display_match_list. We also check
1177 that the list of matches doesn't exceed the user-settable threshold,
1178 and ask the user if he wants to see the list if there are more matches
1179 than RL_COMPLETION_QUERY_ITEMS. */
1181 display_matches (matches)
1187 /* Move to the last visible line of a possibly-multiple-line command. */
1188 _rl_move_vert (_rl_vis_botlin);
1190 /* Handle simple case first. What if there is only one answer? */
1191 if (matches[1] == 0)
1193 temp = printable_part (matches[0]);
1195 print_filename (temp, matches[0]);
1198 rl_forced_update_display ();
1199 rl_display_fixed = 1;
1204 /* There is more than one answer. Find out how many there are,
1205 and find the maximum printed length of a single entry. */
1206 for (max = 0, i = 1; matches[i]; i++)
1208 temp = printable_part (matches[i]);
1209 len = strlen (temp);
1217 /* If the caller has defined a display hook, then call that now. */
1218 if (rl_completion_display_matches_hook)
1220 (*rl_completion_display_matches_hook) (matches, len, max);
1224 /* If there are many items, then ask the user if she really wants to
1226 if (len >= rl_completion_query_items)
1229 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1230 fflush (rl_outstream);
1231 if (get_y_or_n (0) == 0)
1235 rl_forced_update_display ();
1236 rl_display_fixed = 1;
1242 rl_display_match_list (matches, len, max);
1244 rl_forced_update_display ();
1245 rl_display_fixed = 1;
1249 make_quoted_replacement (match, mtype, qc)
1252 char *qc; /* Pointer to quoting character, if any */
1254 int should_quote, do_replace;
1257 /* If we are doing completion on quoted substrings, and any matches
1258 contain any of the completer_word_break_characters, then auto-
1259 matically prepend the substring with a quote character (just pick
1260 the first one from the list of such) if it does not already begin
1261 with a quote string. FIXME: Need to remove any such automatically
1262 inserted quote character when it no longer is necessary, such as
1263 if we change the string we are completing on and the new set of
1264 matches don't require a quoted substring. */
1265 replacement = match;
1267 should_quote = match && rl_completer_quote_characters &&
1268 rl_filename_completion_desired &&
1269 rl_filename_quoting_desired;
1272 should_quote = should_quote && (!qc || !*qc ||
1273 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1277 /* If there is a single match, see if we need to quote it.
1278 This also checks whether the common prefix of several
1279 matches needs to be quoted. */
1280 should_quote = rl_filename_quote_characters
1281 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
1284 do_replace = should_quote ? mtype : NO_MATCH;
1285 /* Quote the replacement, since we found an embedded
1286 word break character in a potential match. */
1287 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1288 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1290 return (replacement);
1294 insert_match (match, start, mtype, qc)
1302 oqc = qc ? *qc : '\0';
1303 replacement = make_quoted_replacement (match, mtype, qc);
1305 /* Now insert the match. */
1308 /* Don't double an opening quote character. */
1309 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1310 replacement[0] == *qc)
1312 /* If make_quoted_replacement changed the quoting character, remove
1313 the opening quote and insert the (fully-quoted) replacement. */
1314 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1315 replacement[0] != oqc)
1317 _rl_replace_text (replacement, start, rl_point - 1);
1318 if (replacement != match)
1323 /* Append any necessary closing quote and a separator character to the
1324 just-inserted match. If the user has specified that directories
1325 should be marked by a trailing `/', append one of those instead. The
1326 default trailing character is a space. Returns the number of characters
1327 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1328 has them) and don't add a suffix for a symlink to a directory. A
1329 nontrivial match is one that actually adds to the word being completed.
1330 The variable rl_completion_mark_symlink_dirs controls this behavior
1331 (it's initially set to the what the user has chosen, indicated by the
1332 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1333 application's completion function). */
1335 append_to_match (text, delimiter, quote_char, nontrivial_match)
1337 int delimiter, quote_char, nontrivial_match;
1339 char temp_string[4], *filename;
1340 int temp_string_index, s;
1343 temp_string_index = 0;
1344 if (quote_char && rl_point && rl_line_buffer[rl_point - 1] != quote_char)
1345 temp_string[temp_string_index++] = quote_char;
1348 temp_string[temp_string_index++] = delimiter;
1349 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
1350 temp_string[temp_string_index++] = rl_completion_append_character;
1352 temp_string[temp_string_index++] = '\0';
1354 if (rl_filename_completion_desired)
1356 filename = tilde_expand (text);
1357 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1358 ? LSTAT (filename, &finfo)
1359 : stat (filename, &finfo);
1360 if (s == 0 && S_ISDIR (finfo.st_mode))
1362 if (_rl_complete_mark_directories)
1364 /* This is clumsy. Avoid putting in a double slash if point
1365 is at the end of the line and the previous character is a
1367 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1369 else if (rl_line_buffer[rl_point] != '/')
1370 rl_insert_text ("/");
1374 /* Don't add anything if the filename is a symlink and resolves to a
1376 else if (s == 0 && S_ISLNK (finfo.st_mode) &&
1377 stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode))
1382 if (rl_point == rl_end && temp_string_index)
1383 rl_insert_text (temp_string);
1389 if (rl_point == rl_end && temp_string_index)
1390 rl_insert_text (temp_string);
1393 return (temp_string_index);
1397 insert_all_matches (matches, point, qc)
1405 rl_begin_undo_group ();
1406 /* remove any opening quote character; make_quoted_replacement will add
1408 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1410 rl_delete_text (point, rl_point);
1415 for (i = 1; matches[i]; i++)
1417 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1418 rl_insert_text (rp);
1419 rl_insert_text (" ");
1420 if (rp != matches[i])
1426 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1427 rl_insert_text (rp);
1428 rl_insert_text (" ");
1429 if (rp != matches[0])
1432 rl_end_undo_group ();
1436 _rl_free_match_list (matches)
1444 for (i = 0; matches[i]; i++)
1449 /* Complete the word at or before point.
1450 WHAT_TO_DO says what to do with the completion.
1451 `?' means list the possible completions.
1452 TAB means do standard completion.
1453 `*' means insert all of the possible completions.
1454 `!' means to do standard completion, and list all possible completions if
1455 there is more than one. */
1457 rl_complete_internal (what_to_do)
1461 rl_compentry_func_t *our_func;
1462 int start, end, delimiter, found_quote, i, nontrivial_lcd;
1463 char *text, *saved_line_buffer;
1466 RL_SETSTATE(RL_STATE_COMPLETING);
1468 set_completion_defaults (what_to_do);
1470 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1471 our_func = rl_completion_entry_function
1472 ? rl_completion_entry_function
1473 : rl_filename_completion_function;
1475 /* We now look backwards for the start of a filename/variable word. */
1477 found_quote = delimiter = 0;
1481 /* This (possibly) changes rl_point. If it returns a non-zero char,
1482 we know we have an open quote. */
1483 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
1488 text = rl_copy_text (start, end);
1489 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
1490 /* nontrivial_lcd is set if the common prefix adds something to the word
1492 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
1498 FREE (saved_line_buffer);
1499 completion_changed_buffer = 0;
1500 RL_UNSETSTATE(RL_STATE_COMPLETING);
1504 /* If we are matching filenames, the attempted completion function will
1505 have set rl_filename_completion_desired to a non-zero value. The basic
1506 rl_filename_completion_function does this. */
1507 i = rl_filename_completion_desired;
1509 if (postprocess_matches (&matches, i) == 0)
1512 FREE (saved_line_buffer);
1513 completion_changed_buffer = 0;
1514 RL_UNSETSTATE(RL_STATE_COMPLETING);
1522 /* Insert the first match with proper quoting. */
1524 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
1526 /* If there are more matches, ring the bell to indicate.
1527 If we are in vi mode, Posix.2 says to not ring the bell.
1528 If the `show-all-if-ambiguous' variable is set, display
1529 all the matches immediately. Otherwise, if this was the
1530 only match, and we are hacking files, check the file to
1531 see if it was a directory. If so, and the `mark-directories'
1532 variable is set, add a '/' to the name. If not, and we
1533 are at the end of the line, then add a space. */
1536 if (what_to_do == '!')
1538 display_matches (matches);
1541 else if (rl_editing_mode != vi_mode)
1542 rl_ding (); /* There are other matches remaining. */
1545 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
1550 insert_all_matches (matches, start, "e_char);
1554 display_matches (matches);
1558 fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do);
1560 FREE (saved_line_buffer);
1561 RL_UNSETSTATE(RL_STATE_COMPLETING);
1565 _rl_free_match_list (matches);
1567 /* Check to see if the line has changed through all of this manipulation. */
1568 if (saved_line_buffer)
1570 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
1571 free (saved_line_buffer);
1574 RL_UNSETSTATE(RL_STATE_COMPLETING);
1578 /***************************************************************/
1580 /* Application-callable completion match generator functions */
1582 /***************************************************************/
1584 /* Return an array of (char *) which is a list of completions for TEXT.
1585 If there are no completions, return a NULL pointer.
1586 The first entry in the returned array is the substitution for TEXT.
1587 The remaining entries are the possible completions.
1588 The array is terminated with a NULL pointer.
1590 ENTRY_FUNCTION is a function of two args, and returns a (char *).
1591 The first argument is TEXT.
1592 The second is a state argument; it should be zero on the first call, and
1593 non-zero on subsequent calls. It returns a NULL pointer to the caller
1594 when there are no more matches.
1597 rl_completion_matches (text, entry_function)
1599 rl_compentry_func_t *entry_function;
1601 /* Number of slots in match_list. */
1602 int match_list_size;
1604 /* The list of matches. */
1607 /* Number of matches actually found. */
1610 /* Temporary string binder. */
1614 match_list_size = 10;
1615 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
1616 match_list[1] = (char *)NULL;
1618 while (string = (*entry_function) (text, matches))
1620 if (matches + 1 == match_list_size)
1621 match_list = (char **)xrealloc
1622 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
1624 match_list[++matches] = string;
1625 match_list[matches + 1] = (char *)NULL;
1628 /* If there were any matches, then look through them finding out the
1629 lowest common denominator. That then becomes match_list[0]. */
1631 compute_lcd_of_matches (match_list, matches, text);
1632 else /* There were no matches. */
1635 match_list = (char **)NULL;
1637 return (match_list);
1640 /* A completion function for usernames.
1641 TEXT contains a partial username preceded by a random
1642 character (usually `~'). */
1644 rl_username_completion_function (text, state)
1648 #if defined (__WIN32__) || defined (__OPENNT)
1649 return (char *)NULL;
1650 #else /* !__WIN32__ && !__OPENNT) */
1651 static char *username = (char *)NULL;
1652 static struct passwd *entry;
1653 static int namelen, first_char, first_char_loc;
1661 first_char_loc = first_char == '~';
1663 username = savestring (&text[first_char_loc]);
1664 namelen = strlen (username);
1668 #ifdef HAVE_GETPWENT
1669 while (entry = getpwent ())
1671 /* Null usernames should result in all users as possible completions. */
1672 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
1679 #ifdef HAVE_GETPWENT
1682 return ((char *)NULL);
1686 value = (char *)xmalloc (2 + strlen (entry->pw_name));
1690 strcpy (value + first_char_loc, entry->pw_name);
1692 if (first_char == '~')
1693 rl_filename_completion_desired = 1;
1697 #endif /* !__WIN32__ && !__OPENNT */
1700 /* Okay, now we write the entry_function for filename completion. In the
1701 general case. Note that completion in the shell is a little different
1702 because of all the pathnames that must be followed when looking up the
1703 completion for a command. */
1705 rl_filename_completion_function (text, state)
1709 static DIR *directory = (DIR *)NULL;
1710 static char *filename = (char *)NULL;
1711 static char *dirname = (char *)NULL;
1712 static char *users_dirname = (char *)NULL;
1713 static int filename_len;
1716 struct dirent *entry;
1718 /* If we don't have any state, then do some initialization. */
1721 /* If we were interrupted before closing the directory or reading
1722 all of its contents, close it. */
1725 closedir (directory);
1726 directory = (DIR *)NULL;
1730 FREE (users_dirname);
1732 filename = savestring (text);
1735 dirname = savestring (text);
1737 temp = strrchr (dirname, '/');
1739 #if defined (__MSDOS__)
1740 /* special hack for //X/... */
1741 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
1742 temp = strrchr (dirname + 3, '/');
1747 strcpy (filename, ++temp);
1750 #if defined (__MSDOS__)
1751 /* searches from current directory on the drive */
1752 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
1754 strcpy (filename, dirname + 2);
1764 /* We aren't done yet. We also support the "~user" syntax. */
1766 /* Save the version of the directory that the user typed. */
1767 users_dirname = savestring (dirname);
1769 if (*dirname == '~')
1771 temp = tilde_expand (dirname);
1776 if (rl_directory_rewrite_hook)
1777 (*rl_directory_rewrite_hook) (&dirname);
1779 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
1781 free (users_dirname);
1782 users_dirname = savestring (dirname);
1785 directory = opendir (dirname);
1786 filename_len = strlen (filename);
1788 rl_filename_completion_desired = 1;
1791 /* At this point we should entertain the possibility of hacking wildcarded
1792 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
1793 contains globbing characters, then build an array of directories, and
1794 then map over that list while completing. */
1795 /* *** UNIMPLEMENTED *** */
1797 /* Now that we have some state, we can read the directory. */
1799 entry = (struct dirent *)NULL;
1800 while (directory && (entry = readdir (directory)))
1802 /* Special case for no filename. If the user has disabled the
1803 `match-hidden-files' variable, skip filenames beginning with `.'.
1804 All other entries except "." and ".." match. */
1805 if (filename_len == 0)
1807 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (entry->d_name))
1810 if (entry->d_name[0] != '.' ||
1811 (entry->d_name[1] &&
1812 (entry->d_name[1] != '.' || entry->d_name[2])))
1817 /* Otherwise, if these match up to the length of filename, then
1819 if (_rl_completion_case_fold)
1821 if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) &&
1822 (((int)D_NAMLEN (entry)) >= filename_len) &&
1823 (_rl_strnicmp (filename, entry->d_name, filename_len) == 0))
1828 if ((entry->d_name[0] == filename[0]) &&
1829 (((int)D_NAMLEN (entry)) >= filename_len) &&
1830 (strncmp (filename, entry->d_name, filename_len) == 0))
1840 closedir (directory);
1841 directory = (DIR *)NULL;
1846 dirname = (char *)NULL;
1851 filename = (char *)NULL;
1855 free (users_dirname);
1856 users_dirname = (char *)NULL;
1859 return (char *)NULL;
1863 /* dirname && (strcmp (dirname, ".") != 0) */
1864 if (dirname && (dirname[0] != '.' || dirname[1]))
1866 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
1868 dirlen = strlen (dirname);
1869 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
1870 strcpy (temp, dirname);
1871 /* Canonicalization cuts off any final slash present. We
1872 may need to add it back. */
1873 if (dirname[dirlen - 1] != '/')
1875 temp[dirlen++] = '/';
1876 temp[dirlen] = '\0';
1881 dirlen = strlen (users_dirname);
1882 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
1883 strcpy (temp, users_dirname);
1884 /* Make sure that temp has a trailing slash here. */
1885 if (users_dirname[dirlen - 1] != '/')
1886 temp[dirlen++] = '/';
1889 strcpy (temp + dirlen, entry->d_name);
1892 temp = savestring (entry->d_name);
1898 /* An initial implementation of a menu completion function a la tcsh. The
1899 first time (if the last readline command was not rl_menu_complete), we
1900 generate the list of matches. This code is very similar to the code in
1901 rl_complete_internal -- there should be a way to combine the two. Then,
1902 for each item in the list of matches, we insert the match in an undoable
1903 fashion, with the appropriate character appended (this happens on the
1904 second and subsequent consecutive calls to rl_menu_complete). When we
1905 hit the end of the match list, we restore the original unmatched text,
1906 ring the bell, and reset the counter to zero. */
1908 rl_menu_complete (count, ignore)
1911 rl_compentry_func_t *our_func;
1912 int matching_filenames, found_quote;
1914 static char *orig_text;
1915 static char **matches = (char **)0;
1916 static int match_list_index = 0;
1917 static int match_list_size = 0;
1918 static int orig_start, orig_end;
1919 static char quote_char;
1920 static int delimiter;
1922 /* The first time through, we generate the list of matches and set things
1923 up to insert them. */
1924 if (rl_last_func != rl_menu_complete)
1926 /* Clean up from previous call, if any. */
1929 _rl_free_match_list (matches);
1931 match_list_index = match_list_size = 0;
1932 matches = (char **)NULL;
1934 /* Only the completion entry function can change these. */
1935 set_completion_defaults ('%');
1937 our_func = rl_completion_entry_function
1938 ? rl_completion_entry_function
1939 : rl_filename_completion_function;
1941 /* We now look backwards for the start of a filename/variable word. */
1942 orig_end = rl_point;
1943 found_quote = delimiter = 0;
1947 /* This (possibly) changes rl_point. If it returns a non-zero char,
1948 we know we have an open quote. */
1949 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
1951 orig_start = rl_point;
1952 rl_point = orig_end;
1954 orig_text = rl_copy_text (orig_start, orig_end);
1955 matches = gen_completion_matches (orig_text, orig_start, orig_end,
1956 our_func, found_quote, quote_char);
1958 /* If we are matching filenames, the attempted completion function will
1959 have set rl_filename_completion_desired to a non-zero value. The basic
1960 rl_filename_completion_function does this. */
1961 matching_filenames = rl_filename_completion_desired;
1963 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
1967 matches = (char **)0;
1969 orig_text = (char *)0;
1970 completion_changed_buffer = 0;
1974 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
1976 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
1977 code below should take care of it. */
1980 /* Now we have the list of matches. Replace the text between
1981 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
1982 matches[match_list_index], and add any necessary closing char. */
1984 if (matches == 0 || match_list_size == 0)
1988 matches = (char **)0;
1989 completion_changed_buffer = 0;
1993 match_list_index = (match_list_index + count) % match_list_size;
1994 if (match_list_index < 0)
1995 match_list_index += match_list_size;
1997 if (match_list_index == 0 && match_list_size > 1)
2000 insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
2004 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2005 append_to_match (matches[match_list_index], delimiter, quote_char,
2006 strcmp (orig_text, matches[match_list_index]));
2009 completion_changed_buffer = 1;