Skip to content

Commit 6b029e8

Browse files
krobelusfaho
authored andcommitted
Provide old implementation of cancel-commandline as fallback
__fish_cancel_commandline was unused (even before) and has some issues on multiline commandlines. Make it use the previously active logic. Closes fish-shell#10935
1 parent 36dcf27 commit 6b029e8

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Notable improvements and fixes
9494

9595
This build system is experimental; the main build system, using ``cmake``, remains the recommended approach for packaging and installation to a prefix.
9696
- A new function ``fish_should_add_to_history`` can be overridden to decide whether a command should be added to the history (:issue:`10302`).
97-
- :kbd:`ctrl-c` during command input no longer prints ``^C`` and a new prompt, but merely clears the command line. This restores the behavior from version 2.2. To revert to the old behavior, use ``bind ctrl-c __fish_cancel_commandline`` (:issue:`10213`).
97+
- :kbd:`ctrl-c` during command input no longer prints ``^C`` and a new prompt, but merely clears the command line. This restores the behavior from version 2.2. To revert to the old behavior, use ``for mode in (bind --list-modes); bind -M $mode ctrl-c cancel-commandline-traditional; end`` (:issue:`10213`).
9898
- Bindings can now mix special input functions and shell commands, so ``bind ctrl-g expand-abbr "commandline -i \n"`` works as expected (:issue:`8186`).
9999
- Special input functions run from bindings via ``commandline -f`` are now applied immediately, instead of after the currently executing binding (:issue:`3031`).
100100
For example, ``commandline -i foo; commandline | grep foo`` succeeds now.
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
1-
function __fish_setup_cancel_text -v fish_color_cancel -v fish_color_normal
2-
set -g __fish_cancel_text "^C"
3-
if set -q fish_color_cancel
4-
set __fish_cancel_text (echo -sn (set_color $fish_color_cancel) $__fish_cancel_text (set_color normal))
5-
end
6-
if command -sq tput
7-
# Clear to EOL (to erase any autosuggestions)
8-
set __fish_cancel_text (echo -sn $__fish_cancel_text (tput el; or tput ce))
9-
end
10-
end
11-
__fish_setup_cancel_text
12-
13-
# This is meant to be bound to something like \cC.
1+
# This is meant to be bound to something like ctrl-c
142
function __fish_cancel_commandline
15-
set -l cmd (commandline)
16-
if test -n "$cmd"
17-
echo -sn $__fish_cancel_text
18-
# `commandline -L` prints the line the cursor is on (starting from the prompt), so move the cursor
19-
# "to the end" then call `commandline -L` to get the total number of lines typed in at the prompt.
20-
commandline -C 10000000
21-
printf (string repeat -n (commandline -L) "\n")
22-
commandline ""
23-
emit fish_cancel
24-
end
25-
26-
# cancel: Close the pager if it's open (#4298)
27-
# repaint: Repaint even if we haven't cancelled anything so the prompt refreshes
28-
# and the terminal scrolls to it.
29-
commandline -f cancel -f repaint
3+
commandline -f cancel-commandline-traditional
304
end

src/input.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const INPUT_FUNCTION_METADATA: &[InputFunctionMetadata] = &[
146146
make_md(L!("beginning-of-line"), ReadlineCmd::BeginningOfLine),
147147
make_md(L!("cancel"), ReadlineCmd::Cancel),
148148
make_md(L!("cancel-commandline"), ReadlineCmd::CancelCommandline),
149+
make_md(L!("cancel-commandline-traditional"), ReadlineCmd::CancelCommandlineTraditional),
149150
make_md(L!("capitalize-word"), ReadlineCmd::CapitalizeWord),
150151
make_md(L!("clear-screen"), ReadlineCmd::ClearScreenAndRepaint),
151152
make_md(L!("complete"), ReadlineCmd::Complete),

src/input_common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub enum ReadlineCmd {
123123
DeleteOrExit,
124124
Exit,
125125
CancelCommandline,
126+
CancelCommandlineTraditional,
126127
Cancel,
127128
Undo,
128129
Redo,

src/reader.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ use crate::kill::{kill_add, kill_replace, kill_yank, kill_yank_rotate};
8787
use crate::libc::MB_CUR_MAX;
8888
use crate::nix::isatty;
8989
use crate::operation_context::{get_bg_context, OperationContext};
90+
use crate::output::parse_color;
9091
use crate::output::Outputter;
9192
use crate::pager::{PageRendering, Pager, SelectionMotion};
9293
use crate::panic::AT_EXIT;
@@ -2305,7 +2306,7 @@ impl<'a> Reader<'a> {
23052306
self.data
23062307
.update_buff_pos(EditableLineTag::Commandline, Some(self.command_line_len()));
23072308
}
2308-
rl::CancelCommandline => {
2309+
rl::CancelCommandline | rl::CancelCommandlineTraditional => {
23092310
if self.conf.exit_on_interrupt {
23102311
self.parser
23112312
.set_last_statuses(Statuses::just(STATUS_CMD_ERROR.unwrap()));
@@ -2315,10 +2316,37 @@ impl<'a> Reader<'a> {
23152316
if self.command_line.is_empty() {
23162317
return;
23172318
}
2319+
if c == rl::CancelCommandlineTraditional {
2320+
// Move cursor to the end of the line.
2321+
let end = self.command_line.len();
2322+
self.update_buff_pos(EditableLineTag::Commandline, Some(end));
2323+
self.autosuggestion.clear();
2324+
// Repaint also changes the actual cursor position
2325+
if self.is_repaint_needed(None) {
2326+
self.layout_and_repaint(L!("cancel"));
2327+
}
2328+
2329+
let mut outp = Outputter::stdoutput().borrow_mut();
2330+
if let Some(fish_color_cancel) = self.vars().get(L!("fish_color_cancel")) {
2331+
outp.set_color(
2332+
parse_color(&fish_color_cancel, false),
2333+
parse_color(&fish_color_cancel, true),
2334+
);
2335+
}
2336+
outp.write_wstr(L!("^C"));
2337+
outp.set_color(RgbColor::RESET, RgbColor::RESET);
2338+
2339+
// We print a newline last so the prompt_sp hack doesn't get us.
2340+
outp.push(b'\n');
2341+
}
23182342
self.push_edit(
23192343
EditableLineTag::Commandline,
23202344
Edit::new(0..self.command_line_len(), L!("").to_owned()),
23212345
);
2346+
if c == rl::CancelCommandlineTraditional {
2347+
self.screen
2348+
.reset_abandoning_line(usize::try_from(termsize_last().width).unwrap());
2349+
}
23222350

23232351
// Post fish_cancel.
23242352
event::fire_generic(self.parser, L!("fish_cancel").to_owned(), vec![]);
@@ -5002,6 +5030,7 @@ fn command_ends_paging(c: ReadlineCmd, focused_on_search_field: bool) -> bool {
50025030
| rl::AcceptAutosuggestion
50035031
| rl::DeleteOrExit
50045032
| rl::CancelCommandline
5033+
| rl::CancelCommandlineTraditional
50055034
| rl::Cancel =>
50065035
// These commands always end paging.
50075036
{

0 commit comments

Comments
 (0)