zsh-workers
 help / color / mirror / code / Atom feed
From: Joey Pabalinas <joeypabalinas@gmail.com>
To: Vincent Lefevre <vincent@vinc17.net>
Cc: zsh-workers@zsh.org, Joey Pabalinas <joeypabalinas@gmail.com>
Subject: Re: long-standing tty related issue: wrapped Emacs not suspended
Date: Thu, 20 Sep 2018 05:43:42 -1000	[thread overview]
Message-ID: <20180920154342.vzuloutkfk57tpqv@gmail.com> (raw)
In-Reply-To: <20180920123005.GA20647@zira.vinc17.org>

[-- Attachment #1: Type: text/plain, Size: 2494 bytes --]

On Thu, Sep 20, 2018 at 02:30:05PM +0200, Vincent Lefevre wrote:
> A long-standing tty related issue...
> 
> I often run Emacs in background:
> 
>   emacs &
> 
> This makes sense as it has its own X interface. But I sometimes do
> this when I forgot that I was in a SSH session with no X forwarding,
> and this makes the terminal unusable since Emacs is not suspended
> and both zsh and Emacs try to get terminal input.
> 
> Now I've noticed that when I run the Emacs binary directly, Emacs
> is suspended as expected. But when Emacs is wrapped in a function,
> it is not suspended. After "zsh -f":
> 
> zira% e() { emacs -nw "$@"; }
> zira% e &
> 
> I cannot quit Emacs or get the zsh prompt. I need to kill the
> terminal.
> 
> I've tested the same thing with other shells: dash behaves like
> zsh, and bash, ksh93 and mksh immediately terminate.
> 
> The same thing happens when using a sh script instead of a function.
> 
> Is this a bug? In any case, can this be solved?

So there is a bit of subtlety at work here cause your issues.

Note the difference between these two `pstree $$` outputs:

The broken case:
$ f() { emacs -nw "$@"; }; f &
zsh─┬─pstree
    ├─zsh───emacs───{emacs}

A working alternative:
$ f() { emacs -nw "$@" & }; f
zsh─┬─pstree
    ├─emacs───{emacs}

In the broken one, your terminal control commands like fg are being sent
to the parent zsh instance, which resumes the zsh subshell. I don't know
the specifics of how emacs' internals work, but usually editors tend to
suspend themselves when they can no longer read/write to or from the
foreground terminal, and Long story short, emacs is stuck suspended and
nested in a subshell, unable to receive your resume commands as the
subshell is intercepting them.

I don't actually know if you can resume emacs in that state, but anyway the
simpler workaround is to use something like `f() { emacs -nw "$@" & }; f`
where having the & inside the function itself avoids the extra subshell and
gives you expected behavior.

I don't know if I would really call this a bug, as pretty much all
common shells these have this same behavior or some other similar
way of handling like simply exiting when put in this odd situation.

I am unsure if this due to historical cruft with how terminals
are implemented these days, so maybe someone else can shed some light
on the reason this is the case.

-- 
Cheers,
Joey Pabalinas

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2018-09-20 15:44 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-20 12:30 Vincent Lefevre
2018-09-20 15:43 ` Joey Pabalinas [this message]
2018-09-20 23:10   ` Vincent Lefevre
2018-09-21 16:57 ` Peter Stephenson
2018-09-21 23:14   ` Joey Pabalinas
2018-09-22  1:17   ` Bart Schaefer
2018-09-22  5:51     ` Joey Pabalinas
2018-09-22 18:54       ` Vincent Lefevre
2018-09-22 21:27         ` Joey Pabalinas
2018-09-23  7:21           ` Vincent Lefevre
2018-09-24 19:51   ` Peter Stephenson
2018-09-25 10:37     ` Peter Stephenson
2018-09-25 17:04       ` Daniel Shahaf
2018-09-26  5:47       ` Bart Schaefer
2018-09-26 13:41         ` Peter Stephenson
2018-09-26 15:17         ` Peter Stephenson
     [not found]         ` <20180926161708.39be6402@camnpupstephen.cam.scsc.local>
2018-10-02 15:32           ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180920154342.vzuloutkfk57tpqv@gmail.com \
    --to=joeypabalinas@gmail.com \
    --cc=vincent@vinc17.net \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).