Advertisement
Guest User

active window man page

a guest
Feb 15th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.32 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # gets name of active window
  4. # opens appropriate manpage term
  5.  
  6. # should handle gui windows, programs running in terms, and even programs running in screen
  7.  
  8. function getWinID() {
  9.    xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | grep -v "CUT_BUFFER0" | cut -d' ' -f5
  10. }
  11. function getWinTitle() {
  12.     xprop -id $(getWinID) | grep "WM_CLASS(STRING)" | sed -e 's/.*= "\([^"]*\)".*/\1/'
  13. }
  14.  
  15. function getWinPID() {
  16.     xprop -id $(getWinID) | grep "NET_WM_PID(CARDINAL)" | cut -d' ' -f 3
  17. }
  18.  
  19. # if the title is a shell, term, or screen this digs out the children
  20. function exhume() {
  21.     _TITLE=$TITLE
  22.     _PID=$PID
  23.  
  24.     TITLE=$1
  25.     PID=$2
  26.     COUNT=$3
  27.  
  28.     #echo -e "exhuming:\t $1 $2"
  29.     if [[ $_PID -eq $PID && $COUNT -gt 5 ]] ; then
  30.         #echo "pid repeat or count over 5"
  31.         echo ""
  32.     else
  33.         case $TITLE in
  34.             xterm*|rxvt*|x-terminal-emulator)
  35.             TITLE=$(ps --ppid $PID -o comm= | tail -n 1)
  36.             PID=$(ps --ppid $PID -o pid= | tail -n 1)
  37.             exhume $TITLE $PID $(( $COUNT + 1 ))
  38.             ;;
  39.  
  40.             bash)
  41.             PROCS=$(ps --ppid $PID -o pid= | wc -w)
  42.             #echo "Does bash have children?  ... $PROCS"
  43.             if [[ $PROCS -gt 0 ]] ; then
  44.                 TITLE=$(ps --ppid $PID -o comm= | tail -n 1)
  45.                 PID=$(ps --ppid $PID -o pid= | tail -n 1)
  46.                 exhume $TITLE $PID $(( $COUNT + 1 ))
  47.             fi
  48.             ;;
  49.  
  50.             screen)
  51.             TITLE=$(xprop -id $(getWinID) | grep "WM_NAME(STRING)" | sed -e 's/.*<\(.*\)>.*/\1/')
  52.             # assumes hard status includes name of program in angle braces
  53.             # we can also add the number to hardstatus and then grab the nth pid.  assumes no screens were destoryed and recreated becasue their children would have a higher pid.
  54.  
  55.             # screen is esp tricky because the screen pid you get may not be the original session.  a screen that attaches elsewhere has no hcildren.
  56.             TITLE='screen'
  57.             ;;
  58.  
  59.             Navigator)
  60.             TITLE="firefox"
  61.             ;;
  62. # ssh breaks this as well. involves connecting to another system, so probably not worth scripting around.
  63.         esac
  64.     fi
  65. }
  66.  
  67. TITLE=$(getWinTitle)
  68. PID=$(getWinPID)
  69.  
  70. exhume $TITLE $PID 0
  71. xterm -bg "#4a525a" -fg white -e "man $TITLE || sleep 2 " &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement