Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/command.c,v
retrieving revision 1.164
diff -c -c -r1.164 command.c
*** src/bin/psql/command.c	5 Mar 2006 15:58:51 -0000	1.164
--- src/bin/psql/command.c	21 Mar 2006 12:50:34 -0000
***************
*** 753,759 ****
  
  		expand_tilde(&fname);
  		/* This scrolls off the screen when using /dev/tty */
! 		success = saveHistory(fname ? fname : DEVTTY);
  		if (success && !quiet && fname)
  			printf(gettext("Wrote history to file \"%s/%s\".\n"),
  				   pset.dirname ? pset.dirname : ".", fname);
--- 753,759 ----
  
  		expand_tilde(&fname);
  		/* This scrolls off the screen when using /dev/tty */
! 		success = saveHistory(fname ? fname : DEVTTY, false);
  		if (success && !quiet && fname)
  			printf(gettext("Wrote history to file \"%s/%s\".\n"),
  				   pset.dirname ? pset.dirname : ".", fname);
Index: src/bin/psql/input.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/input.c,v
retrieving revision 1.52
diff -c -c -r1.52 input.c
*** src/bin/psql/input.c	6 Mar 2006 04:45:21 -0000	1.52
--- src/bin/psql/input.c	21 Mar 2006 12:50:34 -0000
***************
*** 148,153 ****
--- 148,157 ----
  	{
  		enum histcontrol HC;
  		
+ 		/* Flushing of empty buffer should do nothing */
+ 		if  (*s == 0)
+ 			return;
+ 		
  		prev_hist = NULL;
  			
  		HC = GetHistControlConfig();
***************
*** 295,307 ****
  }
  
  
  bool
! saveHistory(char *fname)
  {
  #ifdef USE_READLINE
  	if (useHistory && fname)
  	{
! 		encode_history();		
  		if (write_history(fname) == 0)
  			return true;
  
--- 299,318 ----
  }
  
  
+ /* This function is designed for saving the readline history when user 
+  * run \s command or when psql finishes. 
+  * We have an argument named encodeFlag to handle those cases differently
+  * In that case of call via \s we don't really need to encode \n as \x01,
+  * but when we save history for Readline we must do that conversion
+  */
  bool
! saveHistory(char *fname, bool encodeFlag)
  {
  #ifdef USE_READLINE
  	if (useHistory && fname)
  	{
! 		if (encodeFlag)
! 			encode_history();
  		if (write_history(fname) == 0)
  			return true;
  
***************
*** 331,337 ****
  		if (hist_size >= 0)
  			stifle_history(hist_size);
  
! 		saveHistory(psql_history);
  		free(psql_history);
  		psql_history = NULL;
  	}
--- 342,348 ----
  		if (hist_size >= 0)
  			stifle_history(hist_size);
  
! 		saveHistory(psql_history, true);
  		free(psql_history);
  		psql_history = NULL;
  	}
Index: src/bin/psql/input.h
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/input.h,v
retrieving revision 1.26
diff -c -c -r1.26 input.h
*** src/bin/psql/input.h	6 Mar 2006 04:45:21 -0000	1.26
--- src/bin/psql/input.h	21 Mar 2006 12:50:34 -0000
***************
*** 37,43 ****
  char	   *gets_fromFile(FILE *source);
  
  void		initializeInput(int flags);
! bool		saveHistory(char *fname);
  
  void pg_append_history(char *s, PQExpBuffer history_buf);
  void pg_clear_history(PQExpBuffer history_buf);
--- 37,43 ----
  char	   *gets_fromFile(FILE *source);
  
  void		initializeInput(int flags);
! bool		saveHistory(char *fname, bool encodeFlag);
  
  void pg_append_history(char *s, PQExpBuffer history_buf);
  void pg_clear_history(PQExpBuffer history_buf);
Index: src/bin/psql/mainloop.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/mainloop.c,v
retrieving revision 1.73
diff -c -c -r1.73 mainloop.c
*** src/bin/psql/mainloop.c	6 Mar 2006 15:09:04 -0000	1.73
--- src/bin/psql/mainloop.c	21 Mar 2006 12:50:34 -0000
***************
*** 112,118 ****
  			slashCmdStatus = PSQL_CMD_UNKNOWN;
  			prompt_status = PROMPT_READY;
  			if (pset.cur_cmd_interactive)
! 				pg_clear_history(history_buf);			
  
  			if (pset.cur_cmd_interactive)
  				putc('\n', stdout);
--- 112,118 ----
  			slashCmdStatus = PSQL_CMD_UNKNOWN;
  			prompt_status = PROMPT_READY;
  			if (pset.cur_cmd_interactive)
! 				pg_write_history(history_buf->data);
  
  			if (pset.cur_cmd_interactive)
  				putc('\n', stdout);
***************
*** 321,327 ****
  				break;
  		}
  
! 		if (pset.cur_cmd_interactive && prompt_status != PROMPT_CONTINUE)
  		{
  			/*
  			 *	Pass all the contents of history_buf to readline
--- 321,328 ----
  				break;
  		}
  
! 		if ((pset.cur_cmd_interactive && prompt_status == PROMPT_READY) ||
! 			(GetVariableBool(pset.vars, "SINGLELINE") && prompt_status == PROMPT_CONTINUE))
  		{
  			/*
  			 *	Pass all the contents of history_buf to readline
