diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2021-12-01 12:18:25 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2021-12-01 12:18:25 -0500 |
commit | c2f654930e9f8119b9ed12caab6192d0aafe5ebd (patch) | |
tree | 70b0fd2859b4fff2a88b0ec68215f9aa513e5fd5 | |
parent | 83884682f4df96184549b91869a1cf79dafb4f94 (diff) | |
download | postgresql-c2f654930e9f8119b9ed12caab6192d0aafe5ebd.tar.gz postgresql-c2f654930e9f8119b9ed12caab6192d0aafe5ebd.zip |
psql: treat "--" comments between queries as separate history entries.
If we've not yet collected any non-whitespace, non-comment token for a
new query, flush the current input line to history before reading
another line. This aligns psql's history behavior with the observation
that lines containing only comments are generally not thought of as
being part of the next query. psql's prompting behavior is consistent
with that view, too, since it won't change the prompt until you
enter something that's neither whitespace nor a "--" comment.
Greg Nancarrow, simplified a bit by me
Discussion: https://postgr.es/m/CAJcOf-cAdMVr7azeYR7nWKsNp7qhORzc84rV6d7m7knG5Hrtsw@mail.gmail.com
-rw-r--r-- | src/bin/psql/mainloop.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c index e49ed022938..3b7b4ad4a2a 100644 --- a/src/bin/psql/mainloop.c +++ b/src/bin/psql/mainloop.c @@ -564,9 +564,20 @@ MainLoop(FILE *source) break; } - /* Add line to pending history if we didn't execute anything yet */ - if (pset.cur_cmd_interactive && !line_saved_in_history) - pg_append_history(line, history_buf); + /* + * Add line to pending history if we didn't do so already. Then, if + * the query buffer is still empty, flush out any unsent history + * entry. This means that empty lines (containing only whitespace and + * perhaps a dash-dash comment) that precede a query will be recorded + * as separate history entries, not as part of that query. + */ + if (pset.cur_cmd_interactive) + { + if (!line_saved_in_history) + pg_append_history(line, history_buf); + if (query_buf->len == 0) + pg_send_history(history_buf); + } psql_scan_finish(scan_state); free(line); |