Re: [HACKERS] psql case preserving completion

2012-02-07 Thread Bruce Momjian
On Wed, Feb 01, 2012 at 08:19:24PM +0200, Peter Eisentraut wrote:
 On tis, 2012-01-17 at 16:46 +0900, Fujii Masao wrote:
  When I tested the patch, create ta was converted unexpectedly to
  create TABLE
  though alter ta was successfully converted to alter table. As far
  as I read the patch,
  you seems to have forgotten to change create_or_drop_command_generator() or
  something.
 
 Thanks, fixed and committed.

I have to admit I like the capitalized keywords, but don't normally type
them, but it must be just me because no one else said anything.

-- 
  Bruce Momjian  br...@momjian.ushttp://momjian.us
  EnterpriseDB http://enterprisedb.com

  + It's impossible for everything to be true. +

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] psql case preserving completion

2012-02-07 Thread Robert Haas
On Tue, Feb 7, 2012 at 8:02 PM, Bruce Momjian br...@momjian.us wrote:
 On Wed, Feb 01, 2012 at 08:19:24PM +0200, Peter Eisentraut wrote:
 On tis, 2012-01-17 at 16:46 +0900, Fujii Masao wrote:
  When I tested the patch, create ta was converted unexpectedly to
  create TABLE
  though alter ta was successfully converted to alter table. As far
  as I read the patch,
  you seems to have forgotten to change create_or_drop_command_generator() or
  something.

 Thanks, fixed and committed.

 I have to admit I like the capitalized keywords, but don't normally type
 them, but it must be just me because no one else said anything.

Yeah, I liked the old behavior, too.  But I figured it was a sign that
I'm an old fuddy-duddy.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] psql case preserving completion

2012-02-01 Thread Peter Eisentraut
On tis, 2012-01-17 at 16:46 +0900, Fujii Masao wrote:
 When I tested the patch, create ta was converted unexpectedly to
 create TABLE
 though alter ta was successfully converted to alter table. As far
 as I read the patch,
 you seems to have forgotten to change create_or_drop_command_generator() or
 something.

Thanks, fixed and committed.



-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] psql case preserving completion

2012-01-16 Thread Fujii Masao
On Thu, Jan 12, 2012 at 5:29 AM, Peter Eisentraut pete...@gmx.net wrote:
 In psql, the tab completion always converts key words to upper case.  In
 practice, I and I think most users type in lower case.  So then you end
 up with commands looking like this:

 = alter TABLE foo add CONSTRAINT bar check (a  0);

 To address this, I have implemented a slightly different completion mode
 that looks at the word being completed and converts the completed word
 to the case of the original word.  (Well, it looks at the first letter.)

 In fact, since almost all completions in psql are of this nature, I made
 this the default mode for COMPLETE_WITH_CONST and COMPLETE_WITH_LIST and
 added a new macro COMPLETE_WITH_LIST_CS that uses the old case-sensitive
 behavior. The latter is used mainly for completing backslash commands.

 After playing with this a little, I find the behavior more pleasing.
 Less yelling. ;-)

 Patch attached.

When I tested the patch, create ta was converted unexpectedly to
create TABLE
though alter ta was successfully converted to alter table. As far
as I read the patch,
you seems to have forgotten to change create_or_drop_command_generator() or
something.

Regards,

-- 
Fujii Masao
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


[HACKERS] psql case preserving completion

2012-01-11 Thread Peter Eisentraut
In psql, the tab completion always converts key words to upper case.  In
practice, I and I think most users type in lower case.  So then you end
up with commands looking like this:

= alter TABLE foo add CONSTRAINT bar check (a  0);

To address this, I have implemented a slightly different completion mode
that looks at the word being completed and converts the completed word
to the case of the original word.  (Well, it looks at the first letter.)

In fact, since almost all completions in psql are of this nature, I made
this the default mode for COMPLETE_WITH_CONST and COMPLETE_WITH_LIST and
added a new macro COMPLETE_WITH_LIST_CS that uses the old case-sensitive
behavior. The latter is used mainly for completing backslash commands.

After playing with this a little, I find the behavior more pleasing.
Less yelling. ;-)

Patch attached.
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index a27ef69..5d042f0 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -132,6 +132,7 @@ static const char *const * completion_charpp;	/* to pass a list of strings */
 static const char *completion_info_charp;		/* to pass a second string */
 static const char *completion_info_charp2;		/* to pass a third string */
 static const SchemaQuery *completion_squery;	/* to pass a SchemaQuery */
+static bool completion_case_sensitive;			/* completion is case sensitive */
 
 /*
  * A few macros to ease typing. You can use these to complete the given
@@ -155,15 +156,24 @@ do { \
 	matches = completion_matches(text, complete_from_schema_query); \
 } while (0)
 
+#define COMPLETE_WITH_LIST_CS(list) \
+do { \
+	completion_charpp = list; \
+	completion_case_sensitive = true; \
+	matches = completion_matches(text, complete_from_list); \
+} while (0)
+
 #define COMPLETE_WITH_LIST(list) \
 do { \
 	completion_charpp = list; \
+	completion_case_sensitive = false; \
 	matches = completion_matches(text, complete_from_list); \
 } while (0)
 
 #define COMPLETE_WITH_CONST(string) \
 do { \
 	completion_charp = string; \
+	completion_case_sensitive = false; \
 	matches = completion_matches(text, complete_from_const); \
 } while (0)
 
@@ -771,7 +781,7 @@ psql_completion(char *text, int start, int end)
 
 	/* If a backslash command was started, continue */
 	if (text[0] == '\\')
-		COMPLETE_WITH_LIST(backslash_commands);
+		COMPLETE_WITH_LIST_CS(backslash_commands);
 
 	/* Variable interpolation */
 	else if (text[0] == ':'  text[1] != ':')
@@ -2864,7 +2874,7 @@ psql_completion(char *text, int start, int end)
 			null, fieldsep, tuples_only, title, tableattr,
 		linestyle, pager, recordsep, NULL};
 
-		COMPLETE_WITH_LIST(my_list);
+		COMPLETE_WITH_LIST_CS(my_list);
 	}
 	else if (strcmp(prev2_wd, \\pset) == 0)
 	{
@@ -2874,14 +2884,14 @@ psql_completion(char *text, int start, int end)
 			{unaligned, aligned, wrapped, html, latex,
 			troff-ms, NULL};
 
-			COMPLETE_WITH_LIST(my_list);
+			COMPLETE_WITH_LIST_CS(my_list);
 		}
 		else if (strcmp(prev_wd, linestyle) == 0)
 		{
 			static const char *const my_list[] =
 			{ascii, old-ascii, unicode, NULL};
 
-			COMPLETE_WITH_LIST(my_list);
+			COMPLETE_WITH_LIST_CS(my_list);
 		}
 	}
 	else if (strcmp(prev_wd, \\set) == 0)
@@ -3234,6 +3244,31 @@ _complete_from_query(int is_schema_query, const char *text, int state)
 
 
 /*
+ * Make a pg_strdup copy of s and convert it to the same case as ref.
+ */
+static char *
+pg_strdup_same_case(const char *s, const char *ref)
+{
+	char *ret, *p;
+	unsigned char first = ref[0];
+
+	if (isalpha(first))
+	{
+		ret = pg_strdup(s);
+		if (islower(first))
+			for (p = ret; *p; p++)
+*p = pg_tolower((unsigned char) *p);
+		else
+			for (p = ret; *p; p++)
+*p = pg_toupper((unsigned char) *p);
+		return ret;
+	}
+	else
+		return pg_strdup(s);
+}
+
+
+/*
  * This function returns in order one of a fixed, NULL pointer terminated list
  * of strings (if matching). This can be used if there are only a fixed number
  * SQL words that can appear at certain spot.
@@ -3255,7 +3290,7 @@ complete_from_list(const char *text, int state)
 	{
 		list_index = 0;
 		string_length = strlen(text);
-		casesensitive = true;
+		casesensitive = completion_case_sensitive;
 		matches = 0;
 	}
 
@@ -3270,7 +3305,14 @@ complete_from_list(const char *text, int state)
 
 		/* Second pass is case insensitive, don't bother counting matches */
 		if (!casesensitive  pg_strncasecmp(text, item, string_length) == 0)
-			return pg_strdup(item);
+		{
+			if (completion_case_sensitive)
+return pg_strdup(item);
+			else
+/* If case insensitive matching was requested initially, return
+ * it in the case of what was already entered. */
+return pg_strdup_same_case(item, text);
+		}
 	}
 
 	/*
@@ -3300,12 +3342,16 @@ complete_from_list(const char *text, int state)
 static char *
 complete_from_const(const char *text, int state)
 {
-	(void) text;/* We don't care about what was entered
- * already. */
-
 	

Re: [HACKERS] psql case preserving completion

2012-01-11 Thread Pavel Stehule
2012/1/11 Peter Eisentraut pete...@gmx.net:
 In psql, the tab completion always converts key words to upper case.  In
 practice, I and I think most users type in lower case.  So then you end
 up with commands looking like this:

 = alter TABLE foo add CONSTRAINT bar check (a  0);

 To address this, I have implemented a slightly different completion mode
 that looks at the word being completed and converts the completed word
 to the case of the original word.  (Well, it looks at the first letter.)

 In fact, since almost all completions in psql are of this nature, I made
 this the default mode for COMPLETE_WITH_CONST and COMPLETE_WITH_LIST and
 added a new macro COMPLETE_WITH_LIST_CS that uses the old case-sensitive
 behavior. The latter is used mainly for completing backslash commands.

 After playing with this a little, I find the behavior more pleasing.
 Less yelling. ;-)

 Patch attached.

+1

Pavel



 --
 Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
 To make changes to your subscription:
 http://www.postgresql.org/mailpref/pgsql-hackers


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers