; $Id: filemenu.pro,v 1.1 1992/03/13 23:15:15 jeffry Exp $
;+
; NAME:		FILEMENU
; PURPOSE:
;	Creates a menu of files matching the type file
; 	you ask for ie: *.pro.  Returns the name of the 
;	file chosen by the user.
; CALLING SEQUENCE:
;	selected_file = filemenu('*.pro',xpos=300,ypos=300)
;	print,selected_file
; INPUTS:
;	match-catagory of files to display
; KEYWORD PARAMETERS:
;	xpos-x position of the menu
;	ypos-y position of the menu
; OUTPUTS:
;	Returns the name of the file chosen
; RESTRICTIONS:
;	Must be running on a windowing system
; TESTED ON THESE PLATFORMS:
;	SUN 4 - DEC Station - DEC Alpha
; WARNING: THIS IS FOR IDL
; MODIFICATION HISTORY:
;	SAL 2/92 , Precision Visuals, Inc.
;	SME 2/92- Changed tvmenu to x_tvmenu , Precision Visuals, Inc.
;	A.Csillaghy, Dec. 92, ETHZ
;		added option to exit the menu without read
;		added general_menu option for vt100 use
;	1/95 Option to read also compressed ('.gz') file versions
;	4/96 IDL version + ".z" compressed file versions read too
;-

function filemenu, match, xpos=xpos, ypos=ypos, $
	EXIT = exit, COMPRESSED = compressed

if n_elements(match) eq 0 then match = '*.*'
if n_elements(xpos) eq 0 then xpos = 300
if n_elements(ypos) eq 0 then ypos = 300

spawn, 'pwd', cur_dir
cur_dir = cur_dir(0)
dir = '  '+cur_dir+'   '
done = 0

while done eq 0 do begin
  list = '..'
  spawn,'ls -l', all
  for i=0,n_elements(all)-1 do begin
    if strmid(all(i),0,1) eq 'd' then $
      IF StrPos(!version.(1),'OSF') EQ 0 THEN $
        list = [list, './'+StrTrim( strmid(all(i),54,30), 2 )] $
      ELSE $
        list = [list, './'+StrTrim( strmid(all(i),45,30), 2)]
  endfor
  match_list = findfile(match)
  IF Keyword_Set(COMPRESSED) THEN BEGIN
    matchList2 = FindFile( match + '.gz' )
    matchList3 = FindFile( match + '.z' )
    matchList4 = FindFile( match + '.Z' )
  ENDIF ELSE matchList2 = ''

  if match_list(0) ne '' then $
    list = [list, match_list]

  IF Keyword_Set(COMPRESSED) THEN BEGIN
  
    sortop=0
    IF matchList2(0) NE '' THEN BEGIN
      list = [ list, matchList2 ]
      sortop = 1
    ENDIF
    IF matchList3(0) NE '' THEN BEGIN
      list = [ list, matchList3 ]
      sortop = 1
    ENDIF
    IF matchList4(0) NE '' THEN BEGIN
      list = [ list, matchList4 ]
      sortop = 1
    ENDIF
    IF sortop THEN list = list( Sort( list ) )

  ENDIF

  IF Keyword_Set( EXIT ) THEN list = [list, 'Exit this Menu']

  indx = general_menu( StrTrim( list, 2), $
                       'Choose file, current directory = '+ dir)-1

  IF Keyword_Set( EXIT ) AND indx EQ N_Elements( list )-1 THEN $
	RETURN, ''

  file = list(indx)
  if strmid(file,0,1) eq '.' then begin
    cd,file
    spawn,'pwd',dir
    dir = '  '+dir(0)+'   '
  endif else done = 1
endwhile

; cd, cur_dir
return, file
end
