From 601aecbf549fd771541ac0b328d3c5e3a480c452 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 22 Jan 2018 23:10:12 +0100 Subject: open_tmux_split: Mirror cursor position in Vim Get the current cursor position in Irssi and make the Vim cursor position the same. Makes editing more cognitively coherent. This way, if you've already been editing the Irssi line and the cursor is in the middle of the line, you can pick up right where you left off in Vim. --- vimput.pl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'vimput.pl') diff --git a/vimput.pl b/vimput.pl index 2597933..a316528 100644 --- a/vimput.pl +++ b/vimput.pl @@ -66,7 +66,7 @@ sub write_input { # Open a Tmux split containing a Vim instance editing the vimput_file. sub open_tmux_split { - my ($fifo, $error_handle) = @_; + my ($fifo, $error_handle, $cursor_position) = @_; if (!$ENV{TMUX}) { print $error_handle ERROR_PREFIX . 'Not running in tmux.'; @@ -76,7 +76,7 @@ sub open_tmux_split { my $random_unused_filename = tmpnam(); - my $command = "vim -c 'set buftype=acwrite' -c 'read ${\vimput_file}' -c '1 delete _' -c 'autocmd BufWriteCmd :write $fifo | set nomodified' $random_unused_filename"; + my $command = "vim -c 'set buftype=acwrite' -c 'read ${\vimput_file}' -c '1 delete _' -c 'normal! ${cursor_position}l' -c 'autocmd BufWriteCmd :write $fifo | set nomodified' $random_unused_filename"; system('tmux', 'split-window', $command); return 1; @@ -89,6 +89,8 @@ sub open_tmux_split { sub open_tmux_and_update_input_line_when_finished { return if $forked; + my ($cursor_position) = @_; + my ($read_handle, $write_handle); pipe($read_handle, $write_handle); @@ -113,7 +115,7 @@ sub open_tmux_and_update_input_line_when_finished { if (is_child_fork($pid)) { my $fifo_path = tmpnam(); - open_tmux_split($fifo_path, $write_handle) or do { + open_tmux_split($fifo_path, $write_handle, $cursor_position) or do { cleanup(); POSIX::_exit(1); }; @@ -251,6 +253,8 @@ Irssi::command_bind('help', sub { # Main entrypoint. sub vimput { + my $cursor_position = Irssi::gui_input_get_pos(); + write_input(Irssi::parse_special('$L', undef, 0)); - open_tmux_and_update_input_line_when_finished(); + open_tmux_and_update_input_line_when_finished($cursor_position); } -- cgit v1.2.3