forked from seletskiy/urxvt-vim-insert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvim-insert
122 lines (96 loc) · 2.34 KB
/
vim-insert
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
#!perl -w
use strict;
use File::Temp qw/tempfile/;
sub on_init {
my($self) = @_;
$self->{phase} = 'off';
$self->{paste_data} = '';
}
sub on_tt_paste {
my($self, $octets) = @_;
if ($self->{phase} eq 'off') {
if ($self->is_in_vim()) {
$self->tt_paste('');
$self->{paste_data} = $octets;
$self->{phase} = 'ctrl-o';
return 1;
} else {
return $self->safe_paste($octets);
}
}
()
}
sub safe_paste {
my($self, $octets) = @_;
my ($clipboard_handle, $clipboard_file_name) = tempfile(
SUFFIX=>'.safe-paste',
UNLINK=>0,
);
$octets =~ s/^\n+//g;
if ($octets =~ /\n/m) {
my $command = $self->x_resource('safe-paste.command');
print $clipboard_handle $octets;
close($clipboard_handle);
my $urxvt_pid = $$;
my $pid = fork;
if ($pid) {
waitpid($pid, 0);
return $!;
} else {
exec(($command, $urxvt_pid, $clipboard_file_name));
}
return 1
}
()
}
sub on_refresh_end {
my($self) = @_;
if ($self->{phase} eq 'ctrl-o') {
# vim :h i_CTRL-R_CTRL-O
$self->tt_paste(':if@*==""|set paste|else|call feedkeys("\<C-R>\<C-O>*","n")|endif');
$self->{phase} = 'paste-mode';
return;
}
if ($self->{phase} eq 'paste-mode') {
if ($self->is_on_paste()) {
$self->tt_paste($self->{paste_data});
$self->{phase} = 'paste-done';
} else {
$self->{paste_data} = '';
$self->{phase} = 'off';
}
return;
}
if ($self->{phase} eq 'paste-done') {
$self->tt_paste('');
$self->{phase} = 'paste-disable';
return;
}
if ($self->{phase} eq 'paste-disable') {
$self->tt_paste(':set nopaste');
$self->{phase} = 'off';
return;
}
}
sub is_in_vim {
my($self) = @_;
my $term = $self->{term};
my $last_line = $term->ROW_t($term->nrow-1);
if ($last_line =~ "^-- INSERT --") {
return 1;
} else {
return 0;
}
}
sub is_on_paste {
my($self) = @_;
my $term = $self->{term};
my $last_line = $term->ROW_t($term->nrow-1);
if ($last_line =~ '^-- INSERT \(paste\) --') {
return 1;
} else {
return 0;
}
}