Skip to content

Commit 11268dd

Browse files
committed
Ticket #4625: Followup cosmetical changes
Prefer ';' over '\n' in the shell initialization string. This reduces the number of times the shell prints the prompt and executes the related prompt functions, whose output we ignore. Also be a little more consistent with whitespaces, and remove the "eval" hack for bash 1.x (#4599). Signed-off-by: Egmont Koblinger <[email protected]>
1 parent 7a6cc8a commit 11268dd

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

src/subshell/common.c

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,34 +1271,35 @@ init_subshell_precmd (void)
12711271
*
12721272
*/
12731273
static const char *precmd_fallback =
1274-
" " // Useful if the shell supports HISTCONTROL=ignorespace like functionality
1275-
"MC_PS1_SAVED=\"$PS1\"; " // Save custom PS1
1276-
"precmd() { "
1277-
" if [ ! \"${PWD##$HOME}\" ]; then "
1278-
" MC_PWD=\"~\"; "
1279-
" else "
1280-
" [ \"${PWD##$HOME/}\" = \"$PWD\" ] && MC_PWD=\"$PWD\" || MC_PWD=\"~/${PWD##$HOME/}\"; "
1281-
" fi; "
1282-
" echo \"${MC_PS1_SAVED:-$USER@$(hostname -s):$MC_PWD\\$ }\"; "
1283-
" pwd>&%d; "
1284-
" kill -STOP $$; "
1285-
"}; "
1286-
"PRECMD=precmd; "
1287-
"PS1='$($PRECMD)'\n";
1274+
// Leading whitespace is useful if the shell supports HISTCONTROL=ignorespace like
1275+
// functionality
1276+
" MC_PS1_SAVED=\"$PS1\";" // Save custom PS1
1277+
" precmd() {"
1278+
" if [ ! \"${PWD##$HOME}\" ]; then"
1279+
" MC_PWD=\"~\";"
1280+
" else"
1281+
" [ \"${PWD##$HOME/}\" = \"$PWD\" ] && MC_PWD=\"$PWD\" || MC_PWD=\"~/${PWD##$HOME/}\";"
1282+
" fi;"
1283+
" echo \"${MC_PS1_SAVED:-$USER@$(hostname -s):$MC_PWD\\$ }\";"
1284+
" pwd>&%d;"
1285+
" kill -STOP $$;"
1286+
" };"
1287+
" PRECMD=precmd;"
1288+
" PS1='$($PRECMD)'\n";
12881289

12891290
switch (mc_global.shell->type)
12901291
{
12911292
case SHELL_BASH:
12921293
return g_strdup_printf (
1293-
" mc_print_command_buffer () { printf \"%%s\\\\n\" \"$READLINE_LINE\" >&%d; }\n"
1294-
" bind -x '\"\\e" SHELL_BUFFER_KEYBINDING "\":\"mc_print_command_buffer\"'\n"
1294+
" mc_print_command_buffer () { printf \"%%s\\\\n\" \"$READLINE_LINE\" >&%d; };"
1295+
" bind -x '\"\\e" SHELL_BUFFER_KEYBINDING "\":\"mc_print_command_buffer\"';"
12951296
" bind -x '\"\\e" SHELL_CURSOR_KEYBINDING
1296-
"\":\"echo $BASH_VERSINFO:$READLINE_POINT >&%d\"'\n"
1297+
"\":\"echo $BASH_VERSINFO:$READLINE_POINT >&%d\"';"
12971298
" if test ${BASH_VERSION%%%%.*} -ge 5 && [[ ${PROMPT_COMMAND@a} == *a* ]] 2> "
1298-
"/dev/null; then\n"
1299-
" eval \"PROMPT_COMMAND+=( 'pwd>&%d;kill -STOP $$' )\"\n"
1300-
" else\n"
1301-
" PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND\n}'pwd>&%d;kill -STOP $$'\n"
1299+
"/dev/null; then"
1300+
" PROMPT_COMMAND+=( 'pwd>&%d; kill -STOP $$' );"
1301+
" else"
1302+
" PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND\n}'pwd>&%d; kill -STOP $$';"
13021303
" fi\n",
13031304
command_buffer_pipe[WRITE], command_buffer_pipe[WRITE], subshell_pipe[WRITE],
13041305
subshell_pipe[WRITE]);
@@ -1319,35 +1320,37 @@ init_subshell_precmd (void)
13191320

13201321
case SHELL_KSH:
13211322
// pdksh based variants support \x placeholders but not any "precmd" functionality
1322-
return g_strdup_printf (" PS1='$(pwd>&%d; kill -STOP $$)'"
1323-
"\"${PS1:-\\u@\\h:\\w\\$ }\"\n",
1323+
return g_strdup_printf (" PS1='$(pwd>&%d; kill -STOP $$)'\"${PS1:-\\u@\\h:\\w\\$ }\"\n",
13241324
subshell_pipe[WRITE]);
13251325

13261326
case SHELL_ZSH:
13271327
return g_strdup_printf (
1328-
" mc_print_command_buffer () { printf \"%%s\\\\n\" \"$BUFFER\" >&%d; }\n"
1329-
" zle -N mc_print_command_buffer\n"
1330-
" bindkey '^[" SHELL_BUFFER_KEYBINDING "' mc_print_command_buffer\n"
1331-
" mc_print_cursor_position () { echo $CURSOR >&%d}\n"
1332-
" zle -N mc_print_cursor_position\n"
1333-
" bindkey '^[" SHELL_CURSOR_KEYBINDING "' mc_print_cursor_position\n"
1334-
" _mc_precmd(){ pwd>&%d;kill -STOP $$ }; precmd_functions+=(_mc_precmd)\n",
1328+
" mc_print_command_buffer () { printf \"%%s\\\\n\" \"$BUFFER\" >&%d; };"
1329+
" zle -N mc_print_command_buffer;"
1330+
" bindkey '^[" SHELL_BUFFER_KEYBINDING "' mc_print_command_buffer;"
1331+
" mc_print_cursor_position () { echo $CURSOR >&%d; };"
1332+
" zle -N mc_print_cursor_position;"
1333+
" bindkey '^[" SHELL_CURSOR_KEYBINDING "' mc_print_cursor_position;"
1334+
" _mc_precmd() { pwd>&%d; kill -STOP $$; };"
1335+
" precmd_functions+=(_mc_precmd)\n",
13351336
command_buffer_pipe[WRITE], command_buffer_pipe[WRITE], subshell_pipe[WRITE]);
13361337

13371338
case SHELL_TCSH:
1338-
return g_strdup_printf ("set echo_style=both; "
1339-
"set prompt='%%n@%%m:%%~%%# '; "
1340-
"alias precmd 'echo -n;echo $cwd:q >>%s; kill -STOP $$'\n",
1339+
// tcsh doesn't support ignorespace, but it doesn't hurt, let's keep our style consistent
1340+
return g_strdup_printf (" set echo_style=both;"
1341+
" set prompt='%%n@%%m:%%~%%# ';"
1342+
" alias precmd 'echo -n; echo $cwd:q >>%s; kill -STOP $$'\n",
13411343
tcsh_fifo);
13421344

13431345
case SHELL_FISH:
13441346
return g_strdup_printf (" bind \\e" SHELL_BUFFER_KEYBINDING " 'commandline >&%d';"
1345-
"bind \\e" SHELL_CURSOR_KEYBINDING " 'commandline -C >&%d';"
1346-
"if not functions -q fish_prompt_mc;"
1347-
"functions -e fish_right_prompt;"
1348-
"functions -c fish_prompt fish_prompt_mc; end;"
1349-
"function fish_prompt;"
1350-
"echo \"$PWD\">&%d; fish_prompt_mc; kill -STOP $fish_pid; end\n",
1347+
" bind \\e" SHELL_CURSOR_KEYBINDING " 'commandline -C >&%d';"
1348+
" if not functions -q fish_prompt_mc;"
1349+
" functions -e fish_right_prompt;"
1350+
" functions -c fish_prompt fish_prompt_mc;"
1351+
" end;"
1352+
" function fish_prompt;"
1353+
" echo \"$PWD\">&%d; fish_prompt_mc; kill -STOP $fish_pid; end\n",
13511354
command_buffer_pipe[WRITE], command_buffer_pipe[WRITE],
13521355
subshell_pipe[WRITE]);
13531356
default:

0 commit comments

Comments
 (0)