-
Notifications
You must be signed in to change notification settings - Fork 1
/
_e-gnu
executable file
·81 lines (73 loc) · 2.17 KB
/
_e-gnu
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
#!/usr/bin/perl -w
# Wed 23 Aug 12:06:08 BST 2017
(my $email='ch%christianjaeger,ch')=~ tr/%,/@./;
use strict; use warnings FATAL => 'uninitialized';
use Chj::xperlfunc;
use Chj::xpipe;
use Chj::xsysopen;
use POSIX qw(setsid);
# Protect emacsclient from ctl-c (and other signals?), but still wait
# for it to finish, and submit result code and outputs (but stripped
# from stuff we don't want to see)
my $alternate_editor= $ENV{EMACS_ALTERNATE_EDITOR} // "";
my ($sigr, $sigw)= xpipe;
if (my $daemonizerpid= xfork) {
xwaitpid $daemonizerpid;
$sigw->xclose;
# block until buffer is closed, or more precisely, emacsclient is
# finished, and receive its status:
my $status= $sigr->xcontent;
$status=~ /^(\d+)\z/s or die "bug, status='$status'";
my $statuscode= $1+0;
my $exitcode= $statuscode >> 8;
my $signal= $statuscode & 255;
# print "exited with exitcode=$exitcode and signal=$signal\n";
if ($signal) {
kill $signal, $$;
exit 99; # whatever, just in case we're not being terminated
} else {
exit $exitcode;
}
} else {
$sigr->xclose;
setsid; # prevent signals from crossing over (stop ctl-c)
my ($streamr, $streamw)= xpipe;
if (my $pid= xfork) {
$streamw->xclose;
{
my $log= xsysopen("$ENV{HOME}/._e-gnu.log",
O_CREAT|O_WRONLY|O_APPEND,
0600);
# It appears that emacsclient does buffering if it sees
# stdout/err not to be connected to a terminal. And it
# doesn't have an option, so, sigh. Well, makes filtering
# easier.
my $have_written = 0;
while (<$streamr>) {
s/^Waiting for Emacs\.\.\.//;
unless (/^$/) {
$log->syswrite(localtime()." ".$_);
if (not $have_written) {
warn "starting Emacs instance\n";
$have_written = 1;
}
}
}
$log->close;
}
xwaitpid $pid;
$sigw->xprint("$?");
$sigw->xclose;
exit 0;
} else {
$streamr->xclose;
$sigw->xclose;
$streamw->xdup2(1);
$streamw->xdup2(2);
$streamw->xclose;
xexec("emacsclient",
"-c",
"--alternate-editor=$alternate_editor",
@ARGV);
}
}