Find that lost screen session, episode 2.
Posted Thu, 05 Jun 2008
xterm sets an environment variable in child processes: WINDOWID. This is the X window id of the xterm window. Using this, we can extend upon my last post and come up with a much neater solution. Knowing what screen session you want to bring forward (assuming it's running in an xterm), we can run a command inside that session that grabs the $WINDOWID variable in the shell and uses xdotool to activate the window.
session=$(sh screen-find.sh "#freebsdhelp") STY=$session screen -X screen sh -c 'xdotool windowactivate $WINDOWID'Running this causes my IRC window to be activated by xdotool, which means it is now active, focused, and on top.
This isn't entirely optimal, because it assumes the xterm attached to that screen session is the xterm that launched it. If you run 'xterm -e screen -RR' and close the xterm (don't log out of the shell), then rerun 'xterm -e screen -RR' it will attach to that previous screen session, but the WINDOWID will understandably not be correct any longer.
So what do we do? Using the screen session given, we create a new window in that session and set the title of that window to a generated string. We then use xdotool to search for that string and activate the window. Once we get there, we can kill that new screen window we created and we are left with the terminal holding our screen session sitting in front of us.
I wrote a script to do just that tonight: screen-activate.sh. Example usage: screen-activate.sh 24072.pts-25.snack
This has a great benefit of supporting every terminal program that understands how to set the terminal window title when screen changes it's title. I have tested my .screenrc in Eterm, Konsole, gnome-terminal, and xterm - all know when screen changes it's title if you put this in your .screenrc:
hardstatus string "[%n] %h - %t" termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen (not title yet)\007' # Might need this: termcapinfo * '' 'hs:ts=\E_:fs=\E\\:ds=\E_\E\\'
What I do to make this work for screen sessions that are running over ssh from elsewhere is:
defhstatus ">^E0` ^Ew"
backtick 0 60 60 screentitle
(screentitle is a script which figures out what to "name" the current session... hostname, or hostname+pid)
Then there's a long hairy Ratpoison ruby script that searches for ">hostname" in the title, parses the "w" hardstatus output and ends with:
if window and window.screen_session
screen = window.screen_session.detect { |x| regex.match x.title }
exit 1 if window == current_window and screen.number == window.screen_session.number
Ratpoison::ScreenSession.select screen.number if screen
end
This lets me have a "type to select window" that can cycle through xterms AND individual screen sub-windows, and also quick selects for things like "irssi on host X", "vim on host Y", etc...