1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
use strict;
use File::Temp qw(tmpnam tempfile tempdir);
use IO::Socket::UNIX;
use POSIX qw(mkfifo);
use Irssi;
our $VERSION = '1.00';
our %IRSSI = {
authors => 'Teddy Wing',
contact => 'irssi@teddywing.com',
name => 'Vimput',
description => '',
license => 'GPL',
};
use constant VIMPUT_IPC_COMMAND_PREFIX => '%%%___VIMPUT___%%%: ';
use constant CTRL_X => 24;
# The location of the temporary file where prompt contents are written.
sub vimput_file {
Irssi::get_irssi_dir() . '/VIMPUT_MSG';
}
# Write the given string to our vimput_file.
sub write_input {
my ($message) = @_;
open my $handle, '>', vimput_file or die $!;
print $handle $message;
close $handle;
}
# Open a Tmux split containing a Vim instance editing the vimput_file.
sub open_tmux_split {
my ($fifo) = @_;
if (!$ENV{TMUX}) {
print 'no tmux'; # TODO: Replace with Irssi print
# MSGLEVEL_CLIENTERROR
return;
}
my $random_unused_filename = tmpnam();
# my $command = "vim ${\vimput_file}";
my $command = "vim -c 'set buftype=acwrite' -c 'read ${\vimput_file}' -c '1 delete _' -c 'autocmd BufWriteCmd <buffer> :write $fifo | set nomodified' $random_unused_filename";
system('tmux', 'split-window', $command);
}
sub update_input_line_when_finished {
# my ($handle, $filename) = tempfile();
# print $filename;
# my $tempdir = tempdir('vimput-XXXXXXXXXX');
# my $fifo_path = "$tempdir/fifo";
# my $fifo;
# $fifo->autoflush(1);
# my $pid = fork();
# die $! if not defined $pid;
# if ($pid == 0) { # child
# my $fifo_path = tmpnam();
# print 'F: ' . $fifo_path;
#
# mkfifo($fifo_path, 0600) or die $!;
# my $tag;
# my @args = ($fifo, \$tag);
# $tag = Irssi::input_add(
# fileno($fifo),
# Irssi::INPUT_READ,
# \&adljkhadhadfhjkl,
# \@args
# );
# open $fifo, '<', $fifo_path or die $!;
# open_tmux_split($fifo_path);
#
# $fifo->autoflush(1);
# while (<$fifo>) {
# # if ($_) {
# # print 'hello';
# print $_;
# # }
# }
# close $fifo;
# exit;
# open $fifo, '<', $fifo_path or die $!;
# my $x = 0;
# while (<$fifo>) {
# last if $x > 5;
# print $_;
# if ($_) {
# print $_;
# Irssi::gui_input_set($_);
#
# last;
# }
# sleep 2;
# $x++;
# }
# close $fifo;
# }
# else {
# Irssi::pidwait_add($pid);
# }
# open my $handle, "cat ${\vimput_file} |" or die $!;
# while (<$handle>) {
# print $_;
# }
# close $handle;
# sub update_line {
# open my $handle, '<', vimput_file or die $!;
# while (<$handle>) {
# Irssi::gui_input_set($_);
# }
# close $handle;
# }
#
# my $tag = Irssi::timeout_add(1000, \&update_line);
# my $fuckyoumotherfucker = '/tmp/fucking-fifo';
# unlink $fuckyoumotherfucker;
# open_tmux_split($fuckyoumotherfucker);
#
# mkfifo($fuckyoumotherfucker, 0600) or die $!;
# open my $fifo, '<', $fuckyoumotherfucker, or die $!;
# while (<$fifo>) {
# print $_;
# }
# close $fifo;
# unlink $fuckyoumotherfucker;
my ($read_handle, $write_handle, $command_handle, $fuckface);
pipe($read_handle, $write_handle);
pipe($fuckface, $command_handle);
# pipe($read_handle, $command_handle);
# $write_handle->autoflush(1);
# $write_handle->blocking(0);
my $pid = fork();
if (!defined $pid) {
print "Failed to fork: $!"; # TODO: Irssi print
close $read_handle;
close $write_handle;
close $command_handle;
close $fuckface;
return;
}
if ($pid == 0) {
# my $fuckyoumotherfucker = '/tmp/fucking-fifo';
# unlink $fuckyoumotherfucker;
#
# # TODO: This needs to be done in the parent
# open_tmux_split('/tmp/fucking-other-file', $fuckyoumotherfucker);
#
# mkfifo($fuckyoumotherfucker, 0600) or die $!;
# open my $fifo, '<', $fuckyoumotherfucker, or die $!;
# while (<$fifo>) {
# chomp $_;
# # Irssi::gui_input_set($_);
# print $write_handle $_;
# }
# close $fifo;
# unlink $fuckyoumotherfucker;
my $fifo_path = tmpnam();
# my $tempdir = tempdir('vimput.XXXXXXXXXX', TMPDIR => 1, CLEANUP => 1);
# my $fifo_path = "$tempdir/fifo";
# print $write_handle VIMPUT_IPC_COMMAND_PREFIX . $fifo_path;
# print $command_handle $fifo_path;
close $command_handle;
open_tmux_split($fifo_path);
mkfifo($fifo_path, 0600) or die $!;
open my $fifo, '<', $fifo_path or die $!;
$fifo->autoflush(1);
while (<$fifo>) {
print $write_handle $_;
}
close $fifo;
# my $socket_path = $fifo_path;
#
# my $socket = IO::Socket::UNIX->new(
# Local => $socket_path,
# Type => SOCK_STREAM,
# Listen => 1,
# ) or die "Failed to create socket: $!";
#
# # $socket->blocking(0);
#
# my $connection = $socket->accept();
# $connection->autoflush(1);
#
# while (my $line = <$connection>) {
# print $write_handle $line;
# }
#
# close $socket;
close $write_handle;
POSIX::_exit(0);
}
else {
close $write_handle;
close $command_handle;
Irssi::pidwait_add($pid);
my $pipe_tag;
my @args = ($read_handle, \$pipe_tag);
$pipe_tag = Irssi::input_add(
fileno $read_handle,
Irssi::INPUT_READ,
\&pipe_input,
\@args,
);
my $p2;
my @ar2 = ($fuckface, \$p2);
$p2 = Irssi::input_add(
fileno $fuckface,
Irssi::INPUT_READ,
\&pipe_open_tmux_split,
\@ar2,
);
}
}
sub pipe_input {
my ($args) = @_;
my ($read_handle, $pipe_tag) = @$args;
my $input = <$read_handle>;
print 'I: ' . $input;
# TODO: Add $forked to not spawn more than one children unnecessarily
close $read_handle;
Irssi::input_remove($$pipe_tag);
}
sub pipe_open_tmux_split {
my ($args) = @_;
my ($read_handle, $pipe_tag) = @$args;
my $fifo_path = <$read_handle>;
# open_tmux_split('rando', $fifo_path);
# TODO: Add $forked to not spawn more than one children unnecessarily
close $read_handle;
Irssi::input_remove($$pipe_tag);
}
# TODO: Find out if it's possible to do this is a command
Irssi::signal_add_last 'gui key pressed' => sub {
my ($key) = @_;
if ($key eq CTRL_X) {
write_input(Irssi::parse_special('$L', undef, 0));
# open_tmux_split();
update_input_line_when_finished();
}
};
|