How can I tell my bash prompt to indicate whether there's a backgrounded emacsclient session exists

68 Views Asked by At

I'm using, OS X, and mainly terminal and emacsclient.

When I do shell stuff, I background my emacsclient with Control-Z

Someties I forget whether i've done that, and end up spawning additional emacsclient sessions, which I don;t want to do.

It would be cool if the bash prompt can tell me whether emacsclient jobs up in the jobs output

2

There are 2 best solutions below

0
On

Minimal example for bash, using sleep instead of emacsclient.

PS1="\`if jobs | grep -q sleep; then echo 'sleep jobs' ; else echo 'no sleep jobs' ; fi\`\\\$ "

You might want to filter on stopped jobs (jobs -s).

You can get fancier by echoing escape sequences instead of just strings to colorize it.

0
On

While I think @jpkota provides a workable answer, I wonder if maybe your worrying too much. Provided emacsclient is working OK, there is no problem with having multiple emacsclient sessions running at once - in fact, it is sort of designed to do that. The emacsclient connections are light-weight and if there is a chance you might need to use the same file/buffer again, you may as well keep them around and just open new ones when needed and get rid of the ones you don't think you will need. The whole benefit of emacscleint is that opening new windows/buffers is really fast and if you use the GUI verison, they just pop up in their own window.

There is also a package in elpa which may be useful called osx-pseudo-daemon, which addresses a problem that can occur if you close all emacsclient windows which prevents the main emacs from responding (this is when yu run emacs from launchctl.

What I tend to do is run emacsclient in GUI mode rather than inside a terminal. When I run emacsclient I put it in the background so that it doesn't block my terminal and use the -c flag.(I actually have a shell script which makes this easy - see http://emacsformacosx.com/tips for some ideas. I leave the emacsclient window open and just switch to it if I need to do some emacs editing etc.