Skip to content

Commit db84ba6

Browse files
committed
psql: Add variable to control keyword case in tab completion
This adds the variable COMP_KEYWORD_CASE, which controls in what case keywords are completed. This is partially to let users configure the change from commit 69f4f1c, but it also offers more behaviors than were available before.
1 parent cf09230 commit db84ba6

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,22 @@ bar
26562656
</listitem>
26572657
</varlistentry>
26582658

2659+
<varlistentry>
2660+
<term><varname>COMP_KEYWORD_CASE</varname></term>
2661+
<listitem>
2662+
<para>
2663+
Determines which letter case to use when completing an SQL key word.
2664+
If set to <literal>lower</literal> or <literal>upper</literal>, the
2665+
completed word will be in lower or upper case, respectively. If set
2666+
to <literal>preserve-lower</literal>
2667+
or <literal>preserve-upper</literal> (the default), the completed word
2668+
will be in the case of the word already entered, but words being
2669+
completed without anything entered will be in lower or upper case,
2670+
respectively.
2671+
</para>
2672+
</listitem>
2673+
</varlistentry>
2674+
26592675
<varlistentry>
26602676
<term><varname>DBNAME</varname></term>
26612677
<listitem>

src/bin/psql/tab-complete.c

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ static char **complete_from_variables(char *text,
697697
const char *prefix, const char *suffix);
698698
static char *complete_from_files(const char *text, int state);
699699

700-
static char *pg_strdup_same_case(const char *s, const char *ref);
700+
static char *pg_strdup_keyword_case(const char *s, const char *ref);
701701
static PGresult *exec_query(const char *query);
702702

703703
static void get_previous_words(int point, char **previous_words, int nwords);
@@ -3125,7 +3125,7 @@ create_or_drop_command_generator(const char *text, int state, bits32 excluded)
31253125
{
31263126
if ((pg_strncasecmp(name, text, string_length) == 0) &&
31273127
!(words_after_create[list_index - 1].flags & excluded))
3128-
return pg_strdup_same_case(name, text);
3128+
return pg_strdup_keyword_case(name, text);
31293129
}
31303130
/* if nothing matches, return NULL */
31313131
return NULL;
@@ -3412,9 +3412,9 @@ complete_from_list(const char *text, int state)
34123412
if (completion_case_sensitive)
34133413
return pg_strdup(item);
34143414
else
3415-
/* If case insensitive matching was requested initially, return
3416-
* it in the case of what was already entered. */
3417-
return pg_strdup_same_case(item, text);
3415+
/* If case insensitive matching was requested initially, adjust
3416+
* the case according to setting. */
3417+
return pg_strdup_keyword_case(item, text);
34183418
}
34193419
}
34203420

@@ -3451,9 +3451,9 @@ complete_from_const(const char *text, int state)
34513451
if (completion_case_sensitive)
34523452
return pg_strdup(completion_charp);
34533453
else
3454-
/* If case insensitive matching was requested initially, return it
3455-
* in the case of what was already entered. */
3456-
return pg_strdup_same_case(completion_charp, text);
3454+
/* If case insensitive matching was requested initially, adjust the
3455+
* case according to setting. */
3456+
return pg_strdup_keyword_case(completion_charp, text);
34573457
}
34583458
else
34593459
return NULL;
@@ -3561,27 +3561,48 @@ complete_from_files(const char *text, int state)
35613561

35623562

35633563
/*
3564-
* Make a pg_strdup copy of s and convert it to the same case as ref.
3564+
* Make a pg_strdup copy of s and convert the case according to
3565+
* COMP_KEYWORD_CASE variable, using ref as the text that was already entered.
35653566
*/
35663567
static char *
3567-
pg_strdup_same_case(const char *s, const char *ref)
3568+
pg_strdup_keyword_case(const char *s, const char *ref)
35683569
{
35693570
char *ret, *p;
35703571
unsigned char first = ref[0];
3572+
int tocase;
3573+
const char *varval;
3574+
3575+
varval = GetVariable(pset.vars, "COMP_KEYWORD_CASE");
3576+
if (!varval)
3577+
tocase = 0;
3578+
else if (strcmp(varval, "lower") == 0)
3579+
tocase = -2;
3580+
else if (strcmp(varval, "preserve-lower") == 0)
3581+
tocase = -1;
3582+
else if (strcmp(varval, "preserve-upper") == 0)
3583+
tocase = +1;
3584+
else if (strcmp(varval, "upper") == 0)
3585+
tocase = +2;
3586+
else
3587+
tocase = 0;
35713588

3572-
if (isalpha(first))
3573-
{
3574-
ret = pg_strdup(s);
3575-
if (islower(first))
3576-
for (p = ret; *p; p++)
3577-
*p = pg_tolower((unsigned char) *p);
3578-
else
3579-
for (p = ret; *p; p++)
3580-
*p = pg_toupper((unsigned char) *p);
3581-
return ret;
3582-
}
3589+
/* default */
3590+
if (tocase == 0)
3591+
tocase = +1;
3592+
3593+
ret = pg_strdup(s);
3594+
3595+
if (tocase == -2
3596+
|| ((tocase == -1 || tocase == +1) && islower(first))
3597+
|| (tocase == -1 && !isalpha(first))
3598+
)
3599+
for (p = ret; *p; p++)
3600+
*p = pg_tolower((unsigned char) *p);
35833601
else
3584-
return pg_strdup(s);
3602+
for (p = ret; *p; p++)
3603+
*p = pg_toupper((unsigned char) *p);
3604+
3605+
return ret;
35853606
}
35863607

35873608

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy