Man Pages Help Menu

I’ve put together an overly complex means of generating a help menu item that displays man pages based on the manpath variable.

It works vaguely similarly to fvwm-menu-directory. I call my script help_builder from a PipeRead to generate a menu of the directories that are in the man path. Selecting one of those directories calls the script again, passing it a specific directory. The script creates a menu with the same name as the directory. For any man pages it encounters, it puts the name of the man page as the items name, and for any directory it makes an entry that will call the script again. So selecting a man page will pop up an xterm with that man page. Selecting a directory makes it open up that directory and parse the content.

There is one small problem with my man page menus… Sometimes if I select an item, the menu doesn’t go away until I click off the menu. Then after I click off the menu, it runs the xterm command like it’s supposed to and pops up a man page. Can anyone tell me why or how to fix it?

I call it from start like this:

AddToFunc StartFunction
+ I PipeRead help_builder

I call the menu like so

AddToMenu HelpMenu
+ "Man Pages" Popup HelpMan

And here is the bash script itself:

#!/bin/bash

#MODIFY THIS TO CHANGE MENU BEHAVIOR
#MENU_COMMAND=Menu|Popup
MENU_COMMAND=Popup

# This script generates fvwm menus for man pages from the manpath.
# It is added as a PipeRead command in the start function - with no arguments
# The Menu HelpMan needs to be bound to a key or something to make it accessible
# Then when items are selected from the HelpMan menu, it calls this script and generates more menus

MAN_DIRS=`echo $MANPATH | sed s/:/\ /g`

function top_level(){
   display_function
   echo DestroyMenu HelpMan
   echo AddToMenu HelpMan MissingSubMenuFunction DoMenu
   echo AddToMenu HelpMan \"Man Pages\" Title
   for DIR in $MAN_DIRS; do
      if [ -d $DIR ]; then
         COUNT=`ls $DIR | wc -l`
         if [ $COUNT -eq 1 ]; then
            FILE=`ls $DIR`;
            if [ $FILE == "whatis" ] ; then
               continue
            fi
         fi
         echo AddToMenu HelpMan $DIR $MENU_COMMAND $DIR
      fi
   done
}

function second_level(){
   l_DIR=$@
   echo DestroyMenu $1
   echo AddToMenu $1 MissingSubMenuFunction DoMenu
   for DIR in $l_DIR/*; do
      if [ -d $DIR ]; then
            #if the directory has something it it, add it, otherwise we don't care
            COUNT=`ls $DIR | wc -l`
            if [ $COUNT -eq 1 ]; then
            #make sure it's not just the damn index, ie whatis
               FILE=`ls $DIR`;
               if [ $FILE == "whatis" ] ; then
                  continue
               fi
            elif [ $COUNT -gt 0 ]; then
               echo AddToMenu $1 $DIR $MENU_COMMAND $DIR
            fi
      elif [ -f $DIR ]; then
            if [ "`echo $DIR | rev | cut -f1 -d'/'| rev`" != "whatis" ] ;  then
               TITLE=`echo $DIR | rev | cut -f1 -d/|rev`
               SECTION=`echo $TITLE | cut -f2 -d'.'`
               TITLE=`echo $TITLE | cut -f1 -d'.'`
               COMMAND="xterm -e man -s $SECTION $TITLE"
               echo AddToMenu $1 \"$TITLE \($SECTION\)\" Exec exec $COMMAND
            fi
      fi
   done
}

function display_function(){
   echo DestroyFunc HelpManFunction
   echo AddToFunc HelpManFunction
   echo + I Exec exec echo \$0
   echo DestroyFunc DoMenu
   echo AddToFunc DoMenu
   echo + I DestroyMenu \$0
   echo + I PipeRead \'help_builder \$0\'
   echo + I Menu \$0
}

if [ $# -eq 0 ]; then
   top_level
elif [ $# -eq 1 ]; then
   second_level $1
fi