Repository navigation
Detect the PTY foreground process without shell integration - #723
Merged
Merged
Conversation
Ghostel could only tell that "a command is running" through OSC 133 shell-integration markers, and never which program. Query the PTY's foreground process group instead — the same signal terminal emulators use for close confirmation: - PosixPtyProcess gains a TIOCGPGRP query on the PTY primary, exposed through the NativeProcess facade (under the backend-handoff mutex) and registered as ghostel--pty-foreground-pgid. The ioctl must run on the primary: a re-opened replica fd is not the caller's controlling terminal, so the kernel rejects it with ENOTTY. ConPTY has no foreground process-group concept and returns nil. - ghostel-foreground-pid dispatches per PTY path: the native module owns the primary fd on the native path; Emacs owns it on the Emacs-PTY path, where process-running-child-p answers the same question (nil = shell at prompt, integer = child pgid, t = unknown). - ghostel-command-running-p combines the probe with the OSC 133 C→D state; the OSC arm still covers remote (TRAMP) sessions, where the probe only sees the local client. - ghostel-query-before-killing's auto setting now defers to ghostel-command-running-p, so it works without shell integration and also asks while an ssh client or nested shell holds the foreground — killing the buffer would kill that session. - ghostel--shell-pgid supplies the baseline: ghostel-macos-login-shell runs the shell under login(1), which forks, so ghostel--pid is login and the idle shell never matches it. Only with such a wrapper (ghostel--shell-forked) is a direct child of ghostel--pid adopted as the shell and cached; without one, a command the shell starts is a direct child too and would be adopted in its place. - New abnormal hook ghostel-foreground-change-functions (buffer, pgid, comm), sampled after each redraw while non-nil and run outside the render inhibits. The foreground identity is the (pgid . comm) pair: an early sample can catch a forked command before exec, still under the shell's name, and the pair comparison re-fires once the real name shows up with the program's next output. process-attributes is slow, so comm is re-read only when the pgid changed or while a command still carries the shell's comm (remembered from samples where the shell holds the foreground); an idle prompt or an exec'ed command costs one ioctl per redraw. - ghostel-bookmark--shell-idle-p and consult-ghostel--history-insert move off the raw OSC 133 flag onto ghostel-command-running-p, so they also refuse while an ssh client or nested shell holds the terminal, with no shell integration required. The predicate needs a shell to compare the foreground against, so it never fires in a buffer that runs a program directly (ghostel-exec, ghostel-compile); the defcustom, CHANGELOG and README say so. Tests cover both PTY backends live (probe tracking, hook firing through the redraw path, interrupt/reclaim, and a forked shell reading as idle) plus the predicate, child-state mapping, shell-pgid adoption, and the comm re-read gating with mocked probes.
`ghostel-line-mode-send' runs `ghostel-line-mode-send-functions' with the input text while it is still in the buffer. The first function returning non-nil takes over and the input is neither deleted nor sent. The actual send lives in the new `ghostel-line-mode-send-input', which such a function calls when it decides to send after all.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Ghostel could only tell that "a command is running" through OSC 133 shell-integration markers, and never which program. Without shell integration,
ghostel-query-before-killing'sautosetting never asked, and even with it, an ssh client or nested shell sitting at its own prompt looked idle, so killing the buffer silently killed that session.Approach
Query the PTY's foreground process group instead, the same signal terminal emulators use for close confirmation.
PosixPtyProcessgains aTIOCGPGRPquery on the PTY primary, exposed through theNativeProcessfacade under the backend-handoff mutex and registered asghostel--pty-foreground-pgid. The ioctl must run on the primary: a re-opened replica fd is not the caller's controlling terminal and the kernel rejects it withENOTTY. ConPTY has no foreground process-group concept and returns nil.process-running-child-panswers the same question (nil = shell at prompt, integer = child pgid, t = unknown).ghostel-foreground-piddispatches between the two.ghostel-command-running-pcombines the probe with the OSC 133 C→D state. The OSC arm still covers remote (TRAMP) sessions, where the probe only sees the local client.ghostel-macos-login-shellruns the shell underlogin(1), which forks, soghostel--pidisloginand the idle shell never matches it. Only with such a wrapper is a direct child ofghostel--pidadopted as the shell and cached; without one, a command the shell starts is a direct child too and would be adopted in its place.ghostel-foreground-change-functions(BUFFER PGID COMM), sampled after each redraw while non-nil and run outside the render inhibits. The identity is the(pgid . comm)pair: an early sample can catch a forked command beforeexec, still under the shell's name, and the pair comparison re-fires once the real name shows up.process-attributesis slow, socommis re-read only when the pgid changed or while a command still carries the shell's comm; an idle prompt or an exec'ed command costs one ioctl per redraw.User-visible changes
ghostel-query-before-killingautoworks without shell integration and also asks while an ssh client or nested shell holds the foreground. This also gates thesave-buffers-kill-emacsconfirmation.ghostel-bookmarkandconsult-ghostel's history insert move off the raw OSC 133 flag ontoghostel-command-running-p, so they also refuse while a nested session holds the terminal.ghostel-exec,ghostel-compile). The defcustom, CHANGELOG and README say so.Tests
test/ghostel-foreground-test.elcovers both PTY backends live (probe tracking, hook firing through the redraw path, interrupt/reclaim, and a forked shell reading as idle) plus the predicate, child-state mapping, shell-pgid adoption, and the comm re-read gating with mocked probes.