Re: [PATCH 12/40] "complete" command and completion word break characters

Pedro Alves
2017-07-17T14:36:22Z

On 07/14/2017 06:49 PM, Keith Seitz wrote:
> On 06/02/2017 05:22 AM, Pedro Alves wrote:
>> First, the complete command has a too-simple approximation of what
>> readline's TAB-completion code does to find the completion word point.
>> Unfortunately, readline doesn't expose the functionality it uses
>> internally, so to fix this this patch copies over the relevant code,
>> and adjusts it a bit to better fit the use cases we need it for.
>> (Specifically, our version avoids relying on the
>> rl_word_break_characters, etc. globals, and instead takes those as
>> arguments.)
>
> re: "copies over the relevant code"
> Is it possible to mention this in/around the copied code? That might make it easier to track differences in the future, e.g., if someone found a problem with the copied code, he could look upstream for a fix (or report a bug).

Indeed. See delta diff below.

>
>>
>> diff --git a/gdb/completer.h b/gdb/completer.h
>> index e554bff..207781d 100644
>> --- a/gdb/completer.h
>> +++ b/gdb/completer.h
>> @@ -203,6 +203,10 @@ extern void complete_line (completion_tracker &tracker,
>> const char *line_buffer,
>> int point);
>>
>> +extern const char *completion_find_completion_word (completion_tracker &tracker,
>> + const char *text,
>> + int *quote_char);
>> +
>
> Missing comment?

Good catch. I've pushed in the patch with the below squashed in.
There was another comment that was not relevant for gdb's copy.

diff --git i/gdb/completer.c w/gdb/completer.c
index 3f25261..196610d 100644
--- i/gdb/completer.c
+++ w/gdb/completer.c
@@ -220,20 +220,19 @@ filename_completer_handle_brkchars (struct cmd_list_element *ignore,
#define RL_QF_BACKSLASH 0x04
#define RL_QF_OTHER_QUOTE 0x08

-/* Find the bounds of the current word for completion purposes, and leave
- rl_point set to the end of the word. This function skips quoted
- substrings (characters between matched pairs of characters in
- rl_completer_quote_characters). First we try to find an unclosed
- quoted substring on which to do matching. If one is not found, we use
- the word break characters to find the boundaries of the current word.
- We call an application-specific function to decide whether or not a
- particular word break character is quoted; if that function returns a
- non-zero result, the character does not break a word. This function
- returns the opening quote character if we found an unclosed quoted
- substring, '\0' otherwise. FP, if non-null, is set to a value saying
- which (shell-like) quote characters we found (single quote, double
- quote, or backslash) anywhere in the string. DP, if non-null, is set to
- the value of the delimiter character that caused a word break. */
+/* Find the bounds of the current word for completion purposes, and
+ return a pointer to the end of the word. This mimics (and is a
+ modified version of) readline's _rl_find_completion_word internal
+ function.
+
+ This function skips quoted substrings (characters between matched
+ pairs of characters in rl_completer_quote_characters). We try to
+ find an unclosed quoted substring on which to do matching. If one
+ is not found, we use the word break characters to find the
+ boundaries of the current word. QC, if non-null, is set to the
+ opening quote character if we found an unclosed quoted substring,
+ '\0' otherwise. DP, if non-null, is set to the value of the
+ delimiter character that caused a word break. */

struct gdb_rl_completion_word_info
{
@@ -342,11 +341,6 @@ gdb_rl_find_completion_word (struct gdb_rl_completion_word_info *info,
/* If we are at an unquoted word break, then advance past it. */
scan = line_buffer[point];

- /* If there is an application-specific function to say whether or
- not a character is quoted and we found a quote character, let
- that function decide whether or not a character is a word break,
- even if it is found in rl_completer_word_break_characters. Don't
- bother if we're at the end of the line, though. */
if (scan)
{
isbrk = strchr (brkchars, scan) != 0;
@@ -1464,8 +1458,7 @@ gdb_completion_word_break_characters ()
return NULL;
}

-/* Get the list of chars that are considered as word breaks
- for the current command. */
+/* See completer.h. */

const char *
completion_find_completion_word (completion_tracker &tracker, const char *text,
diff --git i/gdb/completer.h w/gdb/completer.h
index 1f375f8..cf93cf0 100644
--- i/gdb/completer.h
+++ w/gdb/completer.h
@@ -203,6 +203,12 @@ extern void complete_line (completion_tracker &tracker,
const char *line_buffer,
int point);

+/* Find the bounds of the word in TEXT for completion purposes, and
+ return a pointer to the end of the word. Calls the completion
+ machinery for a handle_brkchars phase (using TRACKER) to figure out
+ the right work break characters for the command in TEXT.
+ QUOTE_CHAR, if non-null, is set to the opening quote character if
+ we found an unclosed quoted substring, '\0' otherwise. */
extern const char *completion_find_completion_word (completion_tracker &tracker,
const char *text,
int *quote_char);