From c2e2469a72cbbd078a2fa3f300716f6e4b2ebc81 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Tue, 29 Mar 2016 19:01:13 +0900
Subject: [PATCH] Sample implement of case-sensitive additional list.

---
 src/bin/psql/tab-complete.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 73c5601..5d102d7 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -312,6 +312,10 @@ do { \
 	COMPLETE_WITH_LIST_CS(list); \
 } while (0)
 
+#define CHOOSE_CASE(b, e, u, l) (l && islower(text[0]) ? b l e : b u e)
+#define ADDLIST(q, u, l) \
+	CHOOSE_CASE(q "UNION SELECT * FROM (VALUES ", ") as x", u, l)
+
 /*
  * Assembly instructions for schema queries
  */
@@ -1437,9 +1441,12 @@ psql_completion(const char *text, int start, int end)
 
 	/* ALTER TABLE */
 	else if (Matches2("ALTER", "TABLE"))
-		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
-								   "UNION SELECT 'IF EXISTS'"
-								   "UNION SELECT 'ALL IN TABLESPACE'");
+		COMPLETE_WITH_SCHEMA_QUERY(
+			Query_for_list_of_tables,
+			ADDLIST("",
+					"('IF EXISTS'), ('ALL IN TABLESPACE')",
+					"('if exists'), ('all in tablespace')"));
+
 	/* Try ALTER TABLE after removing optional words IF EXISTS*/
 	else if (HeadMatches2("ALTER", "TABLE") &&
 			 MidMatchAndRemove2(2, "IF", "EXISTS") &&
@@ -1726,8 +1733,9 @@ psql_completion(const char *text, int start, int end)
 
 	/* ALTER POLICY */
 	else if (Matches2("ALTER", "POLICY"))
-		COMPLETE_WITH_QUERY(Query_for_list_of_policies
-							"UNION SELECT 'IF EXISTS'");
+		COMPLETE_WITH_QUERY(
+			ADDLIST(Query_for_list_of_policies,
+					"('IF EXISTS')", "('if exists')"));
 	/* Try ALTER POLICY after removing optional words IF EXISTS */
 	else if (HeadMatches2("ALTER", "POLICY") &&
 			 MidMatchAndRemove2(2, "IF", "EXISTS") &&
-- 
1.8.3.1

