Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d1c866e

Browse files
committed
Make psql redisplay the query buffer after \e.
Up to now, whatever you'd edited was put back into the query buffer but not redisplayed, which is less than user-friendly. But we can improve that just by printing the text along with a prompt, if we enforce that the editing result ends with a newline (which it typically would anyway). You then continue typing more lines if you want, or you can type ";" or do \g or \r or another \e. This is intentionally divorced from readline's processing, for simplicity and so that it works the same with or without readline enabled. We discussed possibly integrating things more closely with readline; but that seems difficult, uncertainly portable across different readline and libedit versions, and of limited real benefit anyway. Let's try the simple way and see if it's good enough. Patch by me, thanks to Fabien Coelho and Laurenz Albe for review Discussion: https://postgr.es/m/13192.1572318028@sss.pgh.pa.us
1 parent 73b06cf commit d1c866e

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

doc/src/sgml/ref/psql-ref.sgml

+11-5
Original file line numberDiff line numberDiff line change
@@ -1831,9 +1831,13 @@ testdb=>
18311831
the normal rules of <application>psql</application>, treating the
18321832
whole buffer as a single line. Any complete queries are immediately
18331833
executed; that is, if the query buffer contains or ends with a
1834-
semicolon, everything up to that point is executed. Whatever remains
1835-
will wait in the query buffer; type semicolon or <literal>\g</literal> to
1836-
send it, or <literal>\r</literal> to cancel it by clearing the query buffer.
1834+
semicolon, everything up to that point is executed and removed from
1835+
the query buffer. Whatever remains in the query buffer is
1836+
redisplayed. Type semicolon or <literal>\g</literal> to send it,
1837+
or <literal>\r</literal> to cancel it by clearing the query buffer.
1838+
</para>
1839+
1840+
<para>
18371841
Treating the buffer as a single line primarily affects meta-commands:
18381842
whatever is in the buffer after a meta-command will be taken as
18391843
argument(s) to the meta-command, even if it spans multiple lines.
@@ -1893,7 +1897,8 @@ Tue Oct 26 21:40:57 CEST 1999
18931897
in the form of a <command>CREATE OR REPLACE FUNCTION</command> or
18941898
<command>CREATE OR REPLACE PROCEDURE</command> command.
18951899
Editing is done in the same way as for <literal>\edit</literal>.
1896-
After the editor exits, the updated command waits in the query buffer;
1900+
After the editor exits, the updated command is executed immediately
1901+
if you added a semicolon to it. Otherwise it is redisplayed;
18971902
type semicolon or <literal>\g</literal> to send it, or <literal>\r</literal>
18981903
to cancel.
18991904
</para>
@@ -1969,7 +1974,8 @@ Tue Oct 26 21:40:57 CEST 1999
19691974
This command fetches and edits the definition of the named view,
19701975
in the form of a <command>CREATE OR REPLACE VIEW</command> command.
19711976
Editing is done in the same way as for <literal>\edit</literal>.
1972-
After the editor exits, the updated command waits in the query buffer;
1977+
After the editor exits, the updated command is executed immediately
1978+
if you added a semicolon to it. Otherwise it is redisplayed;
19731979
type semicolon or <literal>\g</literal> to send it, or <literal>\r</literal>
19741980
to cancel.
19751981
</para>

src/bin/psql/command.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3508,7 +3508,8 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
35083508
{
35093509
unsigned int ql = query_buf->len;
35103510

3511-
if (ql == 0 || query_buf->data[ql - 1] != '\n')
3511+
/* force newline-termination of what we send to editor */
3512+
if (ql > 0 && query_buf->data[ql - 1] != '\n')
35123513
{
35133514
appendPQExpBufferChar(query_buf, '\n');
35143515
ql++;

src/bin/psql/mainloop.c

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ MainLoop(FILE *source)
4747
volatile int successResult = EXIT_SUCCESS;
4848
volatile backslashResult slashCmdStatus = PSQL_CMD_UNKNOWN;
4949
volatile promptStatus_t prompt_status = PROMPT_READY;
50+
volatile bool need_redisplay = false;
5051
volatile int count_eof = 0;
5152
volatile bool die_on_error = false;
5253
FILE *prev_cmd_source;
@@ -118,6 +119,7 @@ MainLoop(FILE *source)
118119
count_eof = 0;
119120
slashCmdStatus = PSQL_CMD_UNKNOWN;
120121
prompt_status = PROMPT_READY;
122+
need_redisplay = false;
121123
pset.stmt_lineno = 1;
122124
cancel_pressed = false;
123125

@@ -152,6 +154,18 @@ MainLoop(FILE *source)
152154
/* May need to reset prompt, eg after \r command */
153155
if (query_buf->len == 0)
154156
prompt_status = PROMPT_READY;
157+
/* If query buffer came from \e, redisplay it with a prompt */
158+
if (need_redisplay)
159+
{
160+
if (query_buf->len > 0)
161+
{
162+
fputs(get_prompt(PROMPT_READY, cond_stack), stdout);
163+
fputs(query_buf->data, stdout);
164+
fflush(stdout);
165+
}
166+
need_redisplay = false;
167+
}
168+
/* Now we can fetch a line */
155169
line = gets_interactive(get_prompt(prompt_status, cond_stack),
156170
query_buf);
157171
}
@@ -518,6 +532,10 @@ MainLoop(FILE *source)
518532
{
519533
/* should not see this in inactive branch */
520534
Assert(conditional_active(cond_stack));
535+
/* ensure what came back from editing ends in a newline */
536+
if (query_buf->len > 0 &&
537+
query_buf->data[query_buf->len - 1] != '\n')
538+
appendPQExpBufferChar(query_buf, '\n');
521539
/* rescan query_buf as new input */
522540
psql_scan_finish(scan_state);
523541
free(line);
@@ -529,6 +547,8 @@ MainLoop(FILE *source)
529547
pset.encoding, standard_strings());
530548
line_saved_in_history = false;
531549
prompt_status = PROMPT_READY;
550+
/* we'll want to redisplay after parsing what we have */
551+
need_redisplay = true;
532552
}
533553
else if (slashCmdStatus == PSQL_CMD_TERMINATE)
534554
break;

0 commit comments

Comments
 (0)