; $Id: pan_do_the_macro.pro,v 1.5 2007/04/06 21:45:08 kneller Exp $
;###############################################################################
;
; NAME:
;  PAN_DO_THE_MACRO
;
; PURPOSE:
;  Executes a scripted IDL sequence of commands for use in fitting
;  data with a user-defined function.
;
; CATEGORY:
;  DAVE, Data Analysis, PAN
;
; AUTHOR:
;   Robert M. Dimeo, Ph.D.
;   NIST Center for Neutron Research
;   100 Bureau Drive
;   Gaithersburg, MD 20899
;   Phone: (301) 975-8135
;   E-mail: robert.dimeo@nist.gov
;   http://www.ncnr.nist.gov/staff/dimeo
;
; LICENSE:
;  The software in this file is written by an employee of
;  National Institute of Standards and Technology
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################
function pan_do_the_macro,macro_lines,$
                            qgroup=qgroup   ;040507 ADDING THIS HERE RATHER THAN PUSHING TO/FROM MAIN LEVEL.
                                            ;PUSHING/PULLING FROM MAIN LEVEL IS UNRELIABLE WITHOUT MORE UPDATES.
                                            ;

;help,/traceback
;print,'pan_do_the_macro qgroup = ',qgroup



n_lines = n_elements(macro_lines)
if macro_lines[0] eq '' then return,0
nlines = n_elements(macro_lines)
;release = 6.1
;if (!version).release lt release then  $

   ;GET ALL THE VARIABLE NAMES FOR THIS LEVEL
   this_level = routine_names(/level) ;else $

;   this_level = scope_level()

main_level = 1
; What are the variable names in the current level?
;if (!version).release lt release then begin
   old_current = routine_names(variables=this_level)

   ; What are the variable names in the main level?
   old_main = routine_names(variables=main_level)
;endif else begin
;  old_current = scope_varname(level = this_level)
;  old_main = scope_varname(level = main_level)
;endelse
; Get out all of the old $MAIN$ variables and
; put them in the current level....
n_old_main = n_elements(old_main)

if old_main[0] eq "" then begin
	n_old_main = 0
endif else begin
	; put the main level variables in the current level
	for _i = 0,n_old_main-1 do begin
;	   if (!version).release lt release then begin
		   if n_elements(routine_names(old_main[_i],fetch = main_level)) gt 0 then begin
			   value  = routine_names(old_main[_i], fetch = main_level)
			   dummy  = routine_names(old_main[_i], value, store = this_level)
		   endif
;		endif else begin
;		   if n_elements(scope_varfetch(old_main[_i],/enter,level = main_level)) ne 0 then $
;		   (scope_varfetch(old_main[_i],/enter)) = scope_varfetch(old_main[_i],level = main_level)
;		endelse
	endfor;_i
endelse


;before_execute = routine_names(variables = this_level)
for _i = 0,nlines-1 do begin
;   if (!version).release lt release then begin
	   before_execute = routine_names(variables = this_level)
	   r = execute(macro_lines[_i],1)
	   after_execute = routine_names(variables = this_level)
;   endif else begin
;	   before_execute = scope_varname(level = this_level);
;	   r = execute(macro_lines[_i],1)
;	   after_execute = scope_varname(level = this_level)
;   endelse
	if r ne 1 then begin
		void = dialog_message(!error_state.msg,/error)
		return,0
	endif
; are there any new variables introduced due to execute at this level?
	n_before = n_elements(before_execute)
	n_after = n_elements(after_execute)
	counter = 0L
	if n_before ne n_after then begin
		for _ii = 0,n_after-1 do begin
			whereok = where(before_execute eq after_execute[_ii],count)
			if count eq 0 then begin
				if counter eq 0L then new_var = after_execute[_ii] else $
					new_var = [new_var,after_execute[_ii]]
					counter = counter+1L
			endif
		endfor;_ii
; Promote new variables from this level to the $MAIN$ level
		for _ii = 0,counter-1 do begin
;         if (!version).release lt release then begin
			   if n_elements(routine_names(new_var[_ii],fetch = this_level)) gt 0 then begin
				   value  = routine_names(new_var[_ii], fetch = this_level)
				   dummy  = routine_names(new_var[_ii], value, store = main_level)
			   endif
;			endif else begin
;			   if n_elements(scope_varfetch(new_var[_ii],/enter,level = this_level)) ne 0 then $
;               (scope_varfetch(new_var[_ii], /enter,level = main_level)) = $
;                  scope_varfetch(new_var[_ii])
;			endelse
		endfor;_ii
	endif

; are there any of the same variables in the $MAIN$ level and in the
; current level?  If so then replace $MAIN$ level variables with
; those from the current level.
;   if (!version).release lt release then begin
	   new_main = routine_names(variables = main_level)
	   new_current = routine_names(variables = this_level)
;   endif else begin
;	   new_main = scope_varname(level = main_level)
;	   new_current = scope_varname(level = this_level)
;   endelse
	counter = 0L
	for _ii = 0,n_elements(new_main)-1 do begin
		whereok = where(new_current eq new_main[_ii],count)
		if count ne 0 then begin
			if counter eq 0L then new_var = new_current[whereok[0]] else $
				new_var = [new_var,new_current[whereok[0]]]
			counter = counter+1L
		endif
	endfor;_ii
	for _ii = 0,counter-1 do begin
; get the value from this level
;      if (!version).release lt release then begin
		   if n_elements(routine_names(new_var[_ii],fetch = this_level)) gt 0 then begin
			   value  = routine_names(new_var[_ii], fetch=this_level)
			   ; put the value and variable name into the $MAIN$ level
			   dummy  = routine_names(new_var[_ii], value, store = main_level)
		   endif
;      endif else begin
;         if n_elements(scope_varfetch(new_var[_ii],level=this_level)) ne 0 then $
;			   (scope_varfetch(new_var[_ii], /enter,level = main_level)) = $
;			      scope_varfetch(new_var[_ii],level=this_level)
;      endelse
	endfor;_ii
endfor;_i


return,1
end;pan_do_the_macro
