-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdinToEditor
executable file
·175 lines (131 loc) · 4.22 KB
/
stdinToEditor
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
#!/usr/bin/env perl -w
#
# stdinToEditor: Pipe stdin straight to an editor.
# 2007-12-17: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
our %metadata = (
"title" => "stdinToEditor",
"description" => "Pipe stdin straight to an editor.",
"rightsHolder" => "Steven J. DeRose",
"creator" => "http://viaf.org/viaf/50334488",
"type" => "http://purl.org/dc/dcmitype/Software",
"language" => "Perl 5.18",
"created" => "2007-12-17",
"modified" => "2022-12-08",
"publisher" => "http://github.com/sderose",
"license" => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{"modified"};
=pod
=head1 Usage
stdinToEditor [options] [file]
Copies STDIN to a temp file, and launches your $EDITOR
on it. In other words, lets you pipe straight into your editor.
B<Note>: With a few editors (such as C<bbedit>), you can just pipe into them.
This script is mainly useful with other editors.
=head1 Options
=over
=item * B<--keep>
Don't delete the temp file afterwards.
=item * B<--line> I<n>
After opening, move to line number I<n>.
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
=item * B<--outfile file>
Use this file instead of a temp file.
=item * B<--target> I<regex>
After opening, move to the first line that matches I<regex> (unfinished).
Only works for C<emacs> at the moment.
=item * B<--title> I<text>
Add a I<-t> I<text> option to the command line invoking the editor.
=item * B<--verbose> OR B<-v>.
Add more messages.
=item * B<--version>
Display version info and exit.
=back
=head1 Known bugs and limitations
I<--target> only works for C<emacs> at the moment. Information on how to
accomplish this with other editors would be welcome.
=head1 History
2007-12-17: Written by Steven J. DeRose.
2008-01-16 sjd: Clean up temp file.
2008-09-23 sjd: Option to choose/keep temp file.
2010-08-10 sjd: perldoc, -k, close ofh. Background the editor.
2010-09-12 sjd: Cleanup.
2012-05-14 sjd: Cleanup.
2015-08-25 sjd: Add --title.
2016-02-24: Add --line and --target.
2022-12-08: New layout. Non-decimal args.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut
###############################################################################
#
my $keep = 0;
my $line = 0;
my $outfile = "";
my $quiet = 0;
my $target = "";
my $title = "";
my $verbose = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"h|help|?" => sub { system "perldoc $0"; exit; },
"keep!" => \$keep,
"line|goto-line=o" => \$line,
"outfile=s" => \$outfile,
"q|quiet!" => \$quiet,
"target=s" => \$target,
"t|title=s" => \$title,
"v|verbose+" => \$verbose,
"version" => sub {
print "Version of $VERSION_DATE by Steven J. DeRose\n";
exit;
}
);
($result) || die "Bad options.\n";
###############################################################################
#
my $tfile = $outfile;
if ($tfile eq "") {
$tfile = "/tmp/stdinToEditor_" . int(rand(100000));
($verbose) && warn "Temp file is at '$tfile'.\n";
}
open my $ofh, ">$tfile" || die "Unable to open temp file at '$tfile'.\n";
my $recnum = 0;
while (<>) {
print $ofh $_;
$recnum++;
}
close $ofh;
($verbose) && warn "$recnum records written to temp file '$tfile'.\n";
my $cmd = $ENV{EDITOR};
if ($title) {
$cmd .= " 0-t '$title'";
}
if ($line) {
$cmd .= " +$line";
}
if ($target) {
if ($ENV{EDITOR} eq "emacs") {
$cmd .= " --eval \"(isearch-forward-regexp '$target')\"";
}
else {
warn "Don't know how to do --target for editor '$ENV{EDITOR}'.\n";
}
}
$cmd .= " " . $tfile . " &";
($verbose) && warn "Running: " . $cmd . "\n";
system $cmd;
if (!$keep) {
system "sleep 3";
($verbose) && warn "Deleting temp file '$tfile' (--keep to keep it).\n";
system "rm $tfile";
}
else {
print "stdinToEditor saved input to '$tfile'.\n";
}