@@ -278,25 +278,40 @@ do { \
278
278
matches = rl_completion_matches(text, complete_from_query); \
279
279
} while (0)
280
280
281
+ /*
282
+ * libedit will typically include the literal's leading single quote in
283
+ * "text", while readline will not. Adapt our offered strings to fit.
284
+ * But include a quote if there's not one just before "text", to get the
285
+ * user off to the right start.
286
+ */
281
287
#define COMPLETE_WITH_ENUM_VALUE (type ) \
282
288
do { \
283
289
char *_completion_schema; \
284
290
char *_completion_type; \
291
+ bool use_quotes; \
285
292
\
286
293
_completion_schema = strtokx(type, " \t\n\r", ".", "\"", 0, \
287
294
false, false, pset.encoding); \
288
295
(void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \
289
296
false, false, pset.encoding); \
290
297
_completion_type = strtokx(NULL, " \t\n\r", ".", "\"", 0, \
291
- false, false, pset.encoding); \
292
- if (_completion_type == NULL)\
298
+ false, false, pset.encoding); \
299
+ use_quotes = (text[0] == '\'' || \
300
+ start == 0 || rl_line_buffer[start - 1] != '\''); \
301
+ if (_completion_type == NULL) \
293
302
{ \
294
- completion_charp = Query_for_list_of_enum_values; \
303
+ if (use_quotes) \
304
+ completion_charp = Query_for_list_of_enum_values_quoted; \
305
+ else \
306
+ completion_charp = Query_for_list_of_enum_values_unquoted; \
295
307
completion_info_charp = type; \
296
308
} \
297
309
else \
298
310
{ \
299
- completion_charp = Query_for_list_of_enum_values_with_schema; \
311
+ if (use_quotes) \
312
+ completion_charp = Query_for_list_of_enum_values_with_schema_quoted; \
313
+ else \
314
+ completion_charp = Query_for_list_of_enum_values_with_schema_unquoted; \
300
315
completion_info_charp = _completion_type; \
301
316
completion_info_charp2 = _completion_schema; \
302
317
} \
@@ -678,7 +693,7 @@ static const SchemaQuery Query_for_list_of_collations = {
678
693
" AND (pg_catalog.quote_ident(nspname)='%s' "\
679
694
" OR '\"' || nspname || '\"' ='%s') "
680
695
681
- #define Query_for_list_of_enum_values \
696
+ #define Query_for_list_of_enum_values_quoted \
682
697
"SELECT pg_catalog.quote_literal(enumlabel) "\
683
698
" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\
684
699
" WHERE t.oid = e.enumtypid "\
@@ -687,7 +702,16 @@ static const SchemaQuery Query_for_list_of_collations = {
687
702
" OR '\"' || typname || '\"'='%s') "\
688
703
" AND pg_catalog.pg_type_is_visible(t.oid)"
689
704
690
- #define Query_for_list_of_enum_values_with_schema \
705
+ #define Query_for_list_of_enum_values_unquoted \
706
+ "SELECT enumlabel "\
707
+ " FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\
708
+ " WHERE t.oid = e.enumtypid "\
709
+ " AND substring(enumlabel,1,%d)='%s' "\
710
+ " AND (pg_catalog.quote_ident(typname)='%s' "\
711
+ " OR '\"' || typname || '\"'='%s') "\
712
+ " AND pg_catalog.pg_type_is_visible(t.oid)"
713
+
714
+ #define Query_for_list_of_enum_values_with_schema_quoted \
691
715
"SELECT pg_catalog.quote_literal(enumlabel) "\
692
716
" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\
693
717
" WHERE t.oid = e.enumtypid "\
@@ -698,6 +722,17 @@ static const SchemaQuery Query_for_list_of_collations = {
698
722
" AND (pg_catalog.quote_ident(nspname)='%s' "\
699
723
" OR '\"' || nspname || '\"' ='%s') "
700
724
725
+ #define Query_for_list_of_enum_values_with_schema_unquoted \
726
+ "SELECT enumlabel "\
727
+ " FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\
728
+ " WHERE t.oid = e.enumtypid "\
729
+ " AND n.oid = t.typnamespace "\
730
+ " AND substring(enumlabel,1,%d)='%s' "\
731
+ " AND (pg_catalog.quote_ident(typname)='%s' "\
732
+ " OR '\"' || typname || '\"'='%s') "\
733
+ " AND (pg_catalog.quote_ident(nspname)='%s' "\
734
+ " OR '\"' || nspname || '\"' ='%s') "
735
+
701
736
#define Query_for_list_of_template_databases \
702
737
"SELECT pg_catalog.quote_ident(d.datname) "\
703
738
" FROM pg_catalog.pg_database d "\
@@ -4417,8 +4452,12 @@ psql_completion(const char *text, int start, int end)
4417
4452
if (matches == NULL )
4418
4453
{
4419
4454
COMPLETE_WITH_CONST (true, "" );
4455
+ /* Also, prevent Readline from appending stuff to the non-match */
4420
4456
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
4421
4457
rl_completion_append_character = '\0' ;
4458
+ #endif
4459
+ #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
4460
+ rl_completion_suppress_quote = 1 ;
4422
4461
#endif
4423
4462
}
4424
4463
0 commit comments