Index: command.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/command.c,v
retrieving revision 1.163
diff -c -r1.163 command.c
*** command.c	4 Mar 2006 04:30:40 -0000	1.163
--- command.c	5 Mar 2006 03:42:13 -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: input.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.c,v
retrieving revision 1.50
diff -c -r1.50 input.c
*** input.c	13 Feb 2006 17:09:25 -0000	1.50
--- input.c	5 Mar 2006 03:42:13 -0000
***************
*** 148,153 ****
--- 148,158 ----
  		enum histcontrol HC;
  		
  		s = history_buf->data;
+ 		
+ 		/* Flushing of empty buffer should do nothing */
+ 		if  (*s == 0)
+ 			return;
+ 		
  		prev_hist = NULL;
  			
  		HC = GetHistControlConfig();
***************
*** 297,309 ****
  }
  
  
  bool
! saveHistory(char *fname)
  {
  #ifdef USE_READLINE
  	if (useHistory && fname)
  	{
! 		encode_history();		
  		if (write_history(fname) == 0)
  			return true;
  
--- 302,321 ----
  }
  
  
+ /* 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;
  
***************
*** 333,339 ****
  		if (hist_size >= 0)
  			stifle_history(hist_size);
  
! 		saveHistory(psql_history);
  		free(psql_history);
  		psql_history = NULL;
  	}
--- 345,351 ----
  		if (hist_size >= 0)
  			stifle_history(hist_size);
  
! 		saveHistory(psql_history, true);
  		free(psql_history);
  		psql_history = NULL;
  	}
Index: input.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.h,v
retrieving revision 1.24
diff -c -r1.24 input.h
*** input.h	11 Feb 2006 21:55:35 -0000	1.24
--- input.h	5 Mar 2006 03:42:13 -0000
***************
*** 37,43 ****
  char	   *gets_fromFile(FILE *source);
  
  void		initializeInput(int flags);
! bool		saveHistory(char *fname);
  
  void pgadd_history(char *s, PQExpBuffer history_buf);
  void pgclear_history(PQExpBuffer history_buf);
--- 37,43 ----
  char	   *gets_fromFile(FILE *source);
  
  void		initializeInput(int flags);
! bool		saveHistory(char *fname, bool encodeFlag);
  
  void pgadd_history(char *s, PQExpBuffer history_buf);
  void pgclear_history(PQExpBuffer history_buf);
Index: mainloop.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/mainloop.c,v
retrieving revision 1.70
diff -c -r1.70 mainloop.c
*** mainloop.c	11 Feb 2006 21:55:35 -0000	1.70
--- mainloop.c	5 Mar 2006 03:42:14 -0000
***************
*** 110,116 ****
  			slashCmdStatus = PSQL_CMD_UNKNOWN;
  			prompt_status = PROMPT_READY;
  			if (pset.cur_cmd_interactive)
! 				pgclear_history(history_buf);			
  
  			if (pset.cur_cmd_interactive)
  				putc('\n', stdout);
--- 110,116 ----
  			slashCmdStatus = PSQL_CMD_UNKNOWN;
  			prompt_status = PROMPT_READY;
  			if (pset.cur_cmd_interactive)
! 				pgflush_history(history_buf);			
  
  			if (pset.cur_cmd_interactive)
  				putc('\n', stdout);
***************
*** 301,307 ****
  				break;
  		}
  		
! 		if (pset.cur_cmd_interactive && prompt_status != PROMPT_CONTINUE)
  			/*
  			 *	Pass all the contents of history_buf to readline
  			 *	and free the history buffer.
--- 301,308 ----
  				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
  			 *	and free the history buffer.
