Skip to content

Commit 501a936

Browse files
committed
Merge branch 'suspend-with-shell-for-jj-fzf'
* Branch commit log: README.md: describe contirb/suspend-with-shell.el contrib/suspend-with-shell.el: use (suspend-emacs) to run a custom command This works without use of `ioctl(TIOCSTI)` that (suspend-emacs) relies on Signed-off-by: Tim Janik <timj@gnu.org>
2 parents 57cf5eb + 3c67587 commit 501a936

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ These scripts are aimed at developers and provide useful utilities for working w
8888

8989
* **jj-undirty.el:** A simple Emacs lisp script that automatically runs `jj status` every time a buffer is saved to snapshot file modifications.
9090
`Usage: (load (expand-file-name "~/jj-fzf/contrib/jj-undirty.el"))`
91+
This will install an after-save-hook that calls `jj-undirty` to snapshot the changes in a saved buffer in a jj repository.
92+
93+
* **suspend-with-shell.el:** A simple Emacs lisp script that allows to suspend Emacs with a custom command.
94+
```
95+
Usage:
96+
;; Suspend emacs with a custom command, without using `ioctl(TIOCSTI)`
97+
(load (expand-file-name "~/jj-fzf/contrib/suspend-with-shell.el"))
98+
;; Suspend emacs and start jj-fzf on Ctrl+T
99+
(global-set-key (kbd "C-t") (lambda () (interactive) (suspend-with-shell "jj-fzf")))
100+
```
91101

92102
<!-- LICENSE -->
93103
## License

contrib/suspend-with-shell.el

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
;; This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2+
3+
;; == suspend-with-shell ==
4+
;; Wrap `(suspend-emacs)` so that a subshell is executed with $SHELL pointing
5+
;; to a script that will run `COMMAND`. This way, emacs can be suspended
6+
;; to run another terminal process on the same tty, without using
7+
;; `ioctl(TIOCSTI)` - which `(suspend-emacs)` relies on but is not available
8+
;; in recent kernel versions.
9+
(defun suspend-with-shell (COMMAND)
10+
"Call (suspend-emacs) with $SHELL assigned to a script that will run COMMAND"
11+
(interactive)
12+
(let ((oldshell (getenv "SHELL"))
13+
(tfile (make-temp-file "emacssubshell"))
14+
(script (concat "#!/usr/bin/env bash\nset -Eeu #-x\n" COMMAND "\n"))
15+
(cannot-suspend 't)) ; force suspend-emacs to use sys_subshell
16+
(with-temp-file tfile
17+
(insert script))
18+
(set-file-modes tfile #o700 'nofollow)
19+
;; see sys_subshell() in https://github.com/emacs-mirror/emacs/blob/master/src/keyboard.c
20+
(setenv "SHELL" tfile)
21+
(suspend-emacs) ; this calls system($SHELL)
22+
(setenv "SHELL" oldshell)
23+
(delete-file tfile)
24+
)
25+
)

0 commit comments

Comments
 (0)