anchor.diff:      state_to_record(FileName, SearchPaths, TabWidth, eqc_fsm).
   
   eqc_fsm_to_record_1(FileName, RecordName, RecordFields, StateFuns, IsTuple, SearchPaths, TabWidth) ->
  -    ?wrangler_io("\nCMD: ~p:eqc_fsm_to_record_1(~p,~p,~p,",
  -		 [?MODULE, FileName, RecordName, RecordFields]),
  -    ?wrangler_io(format_state_funs(StateFuns),[]),
  -    ?wrangler_io(",~p,~p, ~p).\n", [IsTuple, SearchPaths, TabWidth]),
  -    eqc_fsm_to_record(FileName, RecordName, RecordFields, StateFuns, 
  +    format_args(FileName, RecordName, RecordFields, StateFuns, IsTuple, SearchPaths, TabWidth,	"eqc_fsm_to_record_1"),
  +    eqc_fsm_to_record(FileName, RecordName, RecordFields, StateFuns,
   		      IsTuple, SearchPaths, TabWidth, emacs, "").
   
   eqc_fsm_to_record_1_eclipse(FileName, RecordName, RecordFields, StateFuns, IsTuple, SearchPaths, TabWidth) ->
  
positive.code:     state_to_record_1(FileName, RecordName, RecordFields, StateFuns, IsTuple, 
  		      gen_fsm, SearchPaths, TabWidth, Editor, Cmd).
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  state_to_record(FileName, SearchPaths, TabWidth, SM) ->
      {ok, {_, Info}} = refac_util:parse_annotate_file(FileName, true, SearchPaths, TabWidth),
      {value, {module, ModName}} = lists:keysearch(module, 1, Info),
      case is_statemachine_used(Info, SM) of
  	true ->
  	    check_current_state_type(FileName, ModName, SM);
  	false -> throw({error, atom_to_list(SM)++ " is not used by this module."})
      end.
  
  
  state_to_record_1(FileName, RecordName, RecordFields, StateFuns, IsTuple, SM, SearchPaths, TabWidth, Editor, Cmd) ->
      {ok, {AnnAST, Info}} = refac_util:parse_annotate_file(FileName, true, SearchPaths, TabWidth),
      {value, {module, ModName}} = lists:keysearch(module, 1, Info),
      RecordExists = pre_cond_check(RecordName, RecordFields, Info),
      RecordFields1 = [list_to_atom(F) || F <- RecordFields],
      RecordName1 = list_to_atom(RecordName),
      AnnAST1 = do_state_to_record(ModName, AnnAST, RecordName1, RecordFields1, 
  				 StateFuns,RecordExists, IsTuple, SM),
      case Editor of
  	emacs ->
  	    refac_util:write_refactored_files_for_preview([{{FileName, FileName}, AnnAST1}], Cmd),
  	    {ok, [FileName]};
  	eclipse ->
  	    Content = refac_prettypr:print_ast(refac_util:file_format(FileName), AnnAST1),
  	    {ok, [{FileName, FileName, Content}]}
      end.
  
  
  do_state_to_record(ModName, AnnAST, RecordName, RecordFields, StateFuns, RecordExists, IsTuple, SM) ->
      Forms = refac_syntax:form_list_elements(AnnAST),
      Fun = fun(F) ->
  		  case refac_syntax:type(F) of
  		      function ->
  			  do_state_to_record_1(ModName, F, RecordName, RecordFields,
  					       StateFuns, IsTuple, SM);
  		      _ -> F
  		  end
  	  end,
      Forms1 = [Fun(F)|| F <- Forms],
      Forms2 = add_util_funs(Forms1, RecordName, RecordFields),
      case RecordExists of
  	true ->
  	    refac_syntax:form_list(Forms2);
  	false ->
  	    RecordName1 = refac_syntax:atom(RecordName),
  	    RecordFields1 = [refac_syntax:record_field(refac_syntax:atom(FieldName))
  			     || FieldName <- RecordFields],
  	    RecordDef = refac_syntax:attribute(
  			  refac_syntax:atom(record),
  			  [RecordName1, refac_syntax:tuple(RecordFields1)]),
  	    {Fs1, Fs2} = lists:splitwith(
  			   fun (F) -> refac_syntax:type(F) == comment orelse is_not_type_attrubute(F)
  			   end, Forms2),
  	    {Fs11, Fs12} = lists:splitwith(
  			     fun (F) -> refac_syntax:type(F) == comment end, lists:reverse(Fs1)),
  	    refac_syntax:form_list(lists:reverse(Fs12) ++ [RecordDef] ++ lists:reverse(Fs11) ++ Fs2)
      end.
  
  do_state_to_record_1(ModName, Fun, RecordName, RecordFields, StateFuns, IsTuple, SM) ->
      As = refac_syntax:get_ann(Fun),
      {value, {fun_def, {M, F, A, _, _}}} = lists:keysearch(fun_def, 1, As),
      CallBacks =  callbacks(ModName, StateFuns, SM),
      NewFun = case lists:keysearch({M, F, A}, 1, CallBacks) of
  		 {value, {{M, F, A}, {PatIndex, ReturnState}}} ->
  		     do_state_to_record_in_callback_fun(
  		       PatIndex, Fun, RecordName, RecordFields, IsTuple, ReturnState, SM);
  		 false -> Fun
  	     end,
      wrap_fun_interface(NewFun, ModName, RecordName, RecordFields, IsTuple, StateFuns, SM).
  	  
  		 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
  
  do_state_to_record_in_callback_fun(0, Fun, RecordName, RecordFields, IsTuple, ReturnState, SM)->
      case ReturnState of 
  	true ->
  	    do_state_to_record_in_init_fun(Fun, RecordName, RecordFields, IsTuple, SM);
  	false ->
  	    Fun
      end;
  do_state_to_record_in_callback_fun(PatIndex, Fun, RecordName, RecordFields, IsTuple, ReturnState, SM) ->
      FunName = refac_syntax:function_name(Fun),
      Cs = refac_syntax:function_clauses(Fun),
      Cs1 = [do_state_to_record_in_callback_clause(PatIndex, C, RecordName, RecordFields, 
  						 IsTuple, ReturnState, SM) 
  	   || C <- Cs],
      refac_util:rewrite(Fun, refac_syntax:function(FunName, Cs1)).
  
  
  do_state_to_record_in_callback_clause(PatIndex, C, RecordName, RecordFields, IsTuple, ReturnState, SM) ->
      P = list_to_tuple(refac_syntax:clause_patterns(C)),
      G = refac_syntax:clause_guard(C),
      B = refac_syntax:clause_body(C),
      State = element(PatIndex, P),
      case refac_syntax:type(State) of
        match_expr ->
  	  {State1, DefPs} = tuple_to_record_in_match_expr_pattern(
  			      State, RecordName, RecordFields, IsTuple),
  	  P1 = tuple_to_list(setelement(PatIndex, P, State1)),
  	  B1 = do_state_to_record_in_callback_fun_clause_body(
  		 PatIndex, B, RecordName, RecordFields, DefPs, IsTuple, ReturnState, SM),
  	  refac_util:rewrite(C, refac_syntax:clause(P1, G, B1));
        tuple when IsTuple ->
  	  RecordExpr = tuple_to_record_expr(State, RecordName, RecordFields),
  	  State1 = refac_util:rewrite(State, RecordExpr),
  	  P1 = tuple_to_list(setelement(PatIndex, P, State1)),
  	  B1 = do_state_to_record_in_callback_fun_clause_body(
  		 PatIndex, B, RecordName, RecordFields, [], IsTuple, ReturnState, SM),
  	  refac_util:rewrite(C, refac_syntax:clause(P1, G, B1));
        variable ->
  	  Pos = refac_syntax:get_pos(State),
  	  B1 = do_state_to_record_in_callback_fun_clause_body(
  		 PatIndex, B, RecordName, RecordFields, [Pos], IsTuple, ReturnState, SM),
  	  refac_util:rewrite(C, refac_syntax:clause(tuple_to_list(P), G, B1));
        underscore ->
  	  C;
        _ when IsTuple ->
  	  Pos = refac_syntax:get_pos(State),
  	  throw({error, "Wrangler did not know how to transform the pattern at location: " 
  		 ++ io_lib:format("~p", [Pos])});
        _ when length(RecordFields) == 1 ->
  	  Fields = [mk_record_field(hd(RecordFields), State)],
  	  State1 = refac_util:rewrite(State, mk_record_expr(RecordName, Fields)),
  	  P1 = tuple_to_list(setelement(PatIndex, P, State1)),
  	  B1 = do_state_to_record_in_callback_fun_clause_body(
  		 PatIndex, B, RecordName, RecordFields, [], IsTuple, ReturnState, SM),
  	  refac_util:rewrite(C, refac_syntax:clause(P1, G, B1));
        _ ->
  	  Pos = refac_syntax:get_pos(State),
  	  throw({error, "Wrangler did not know how to transform the pattern at location: " 
  		 ++ io_lib:format("~p", [Pos])})
      end.
  
  tuple_to_record_in_match_expr_pattern(State, RecordName, RecordFields, IsTuple)->	   
      P=refac_syntax:match_expr_pattern(State),
      B = refac_syntax:match_expr_body(State),
      {P1, Pos1} =
  	tuple_to_record_in_match_expr_pattern_1(P, RecordName, RecordFields, IsTuple),
      {B1, Pos2} = 
  	tuple_to_record_in_match_expr_pattern_1(B, RecordName, RecordFields, IsTuple),
      {refac_util:rewrite(State, refac_syntax:match_expr(P1,B1)), Pos1++Pos2}.
  
  tuple_to_record_in_match_expr_pattern_1(P, RecordName, RecordFields, IsTuple) ->
      case refac_syntax:type(P) of
        variable ->
  	  {P, [refac_syntax:get_pos(P)]};
        tuple when IsTuple ->
  	  RecordExpr = tuple_to_record_expr(P, RecordName, RecordFields),
  	  RecP = refac_util:rewrite(P, RecordExpr),
  	  {RecP, []};
        match_expr ->
  	  tuple_to_record_in_match_expr_pattern(P, RecordName, RecordFields, IsTuple);
        _ ->
  	  case IsTuple of
  	    true ->
  		Pos = refac_syntax:get_pos(P),
  		throw({error, "Wrangler did not know how to transform the pattern at location: " ++
  				io_lib:format("~p", [Pos])});
  	    false ->
  		Fields = [mk_record_field(hd(RecordFields), P)],
  		RecP = refac_util:rewrite(P, mk_record_expr(RecordName, Fields)),
  		{RecP, []}
  	  end
      end.
  do_state_to_record_in_callback_fun_clause_body(_PatIndex, Body, RecordName, RecordFields, 
  					       DefPs, IsTuple, ReturnState, SM) ->
      refac_io:format("RecordFields:\n~p\n", [RecordFields]),
      Fun = fun (Node, _Others) ->
  		  case refac_syntax:type(Node) of
  		      application when IsTuple ->
  			  As = refac_syntax:get_ann(refac_syntax:application_operator(Node)),
  			  case lists:keysearch(fun_def, 1, As) of
  			      {value, {fun_def, {erlang, element, 2, _, _}}} ->
  				  element_to_record_access(RecordName, RecordFields, DefPs, Node);
  			      {value, {fun_def, {erlang, setelement, 3, _, _}}} ->
  				  setelement_to_record_expr(Node, RecordName, RecordFields, DefPs, IsTuple);			  
  			      _ -> Node
  			  end;
  		      variable ->
  			  As = refac_syntax:get_ann(Node),
  			  case lists:keysearch(def, 1, As) of
  			      {value, {def, DefinePos}} ->
  				  case DefinePos -- DefPs =/= DefinePos of
  				      true ->
  					  make_record_to_tuple_app(Node, RecordName, RecordFields, IsTuple);
  				      false ->
  					  Node
  				  end;
  			      false -> Node
  			  end;
  		      _ -> Node
  		  end
  	  end,
      Body1 = refac_util:full_buTP(Fun, refac_syntax:block_expr(Body), {}),
      Body2 = is_tuple_to_is_record(Body1, RecordName),
      Body3 = refac_syntax:block_expr_body(Body2),
      case ReturnState of 
  	true ->
   	    do_state_to_record_in_init_fun_clause_body(
  	      Body3, RecordName, RecordFields, DefPs, IsTuple, SM);
  	false ->
  	    Body3
      end.
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
  do_state_to_record_in_init_fun(Fun, RecordName, RecordFields, IsTuple, SM) ->
      FunName = refac_syntax:function_name(Fun),
      [C] = refac_syntax:function_clauses(Fun),
      C1=do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM),
      refac_util:rewrite(Fun, refac_syntax:function(FunName, [C1])).
  
  do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) ->
      P = refac_syntax:clause_patterns(C),
      G = refac_syntax:clause_guard(C),
      B = refac_syntax:clause_body(C),
      B1=do_state_to_record_in_init_fun_clause_body(B, RecordName, RecordFields,[], IsTuple, SM),
      refac_util:rewrite(C, refac_syntax:clause(P, G, B1)).
  
  do_state_to_record_in_init_fun_clause_body(Body, RecordName, RecordFields, DefPs, IsTuple, SM)
      when SM == gen_fsm ->
      Msg = "Wrangler did not know how to transform the expression at location: ",
      [LastExpr| Exprs] = lists:reverse(Body),
      Pos = refac_syntax:get_pos(LastExpr),
      case refac_syntax:type(LastExpr) of
        atom -> Body;
        tuple ->
  	    Es = refac_syntax:tuple_elements(LastExpr), 
  	    Size = length(Es),
  	    case Size =< 2  of
  		true ->
  		    Body;
  		false ->
  		    Tag = hd(refac_syntax:tuple_elements(LastExpr)),
  		    case refac_syntax:type(Tag) of
  			atom ->
  			    case refac_syntax:atom_value(Tag) of
  				ok -> 
  				    gen_fsm_state_to_record(RecordName, RecordFields, LastExpr, 3, IsTuple);
  				next_state ->
  				    gen_fsm_state_to_record(RecordName, RecordFields, LastExpr, 3, IsTuple);
  				reply -> 
  					gen_fsm_state_to_record(RecordName, RecordFields, LastExpr, 4, IsTuple);
  				stop ->
  				    gen_fsm_state_to_record(RecordName, RecordFields, LastExpr, Size, IsTuple);
  				_ ->
  				    throw({error, Msg ++ io_lib:format("~p", [Pos])})
  			    end;
  			_ ->
  			    throw({error, Msg ++ io_lib:format("~p", [Pos])})
  		    end
  	    end;
        variable ->
  	  As = refac_syntax:get_ann(LastExpr),
  	  case lists:keysearch(def, 1, As) of
  	    {value, {def, DefinePos}} ->
  		case is_used_only_once(refac_syntax:block_expr(Body), DefinePos) of
  		  true ->
  		      {Body1, Modified} =
  			  do_state_to_record_in_match_expr(
  			    refac_syntax:block_expr(Body), LastExpr, DefinePos, RecordName, RecordFields, IsTuple, SM),
  		      case Modified of
  			true ->
  			    refac_syntax:block_expr_body(Body1);
  			false ->
  			    throw({error, Msg ++ io_lib:format("~p", [Pos])})
  		      end;
  		  false ->
  		      throw({error, Msg ++ io_lib:format("~p", [Pos])})
  		end;
  	    _ ->
  		throw({error, Msg ++ io_lib:format("~p", [Pos])})
  	  end;
        case_expr ->
  	  Args = refac_syntax:case_expr_argument(LastExpr),
  	  Cs = refac_syntax:case_expr_clauses(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:case_expr(Args, Cs1)),
  	  lists:reverse([LastExpr1| Exprs]);
        if_expr ->
  	  Cs = refac_syntax:if_expr_clauses(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:if_expr(Cs1)),
  	  lists:reverse([LastExpr1| Exprs]);
        cond_expr ->
  	  Cs = refac_syntax:cond_expr_clauses(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:cond_expr(Cs1)),
  	  lists:reverse([LastExpr1| Exprs]);
        block_expr ->
  	  B = refac_syntax:block_expr_body(LastExpr),
  	  B1 = do_state_to_record_in_init_fun_clause_body(B, RecordName, RecordFields, DefPs, IsTuple, SM),
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:block_expr(B1)),
  	  lists:reverse([LastExpr1| Exprs]);
        receive_expr ->
  	  Cs = refac_syntax:receive_expr_clauses(LastExpr),
  	  T = refac_syntax:receive_expr_timeout(LastExpr),
  	  A = refac_syntax:receive_expr_action(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:receive_expr(Cs1, T, A)),
  	  lists:reverse([LastExpr1| Exprs]);
        _ ->
  	  throw({error, Msg ++ io_lib:format("~p", [Pos])})
      end;
  
  do_state_to_record_in_init_fun_clause_body(Body, RecordName, RecordFields, DefPs, IsTuple, SM)
      when SM == eqc_statem orelse SM == eqc_fsm ->
      [LastExpr| Exprs] = lists:reverse(Body),
      case refac_syntax:type(LastExpr) of
        tuple when IsTuple ->
  	  RecordExpr = tuple_to_record_expr(LastExpr, RecordName, RecordFields),
  	  LastExpr1 = refac_util:rewrite(LastExpr, RecordExpr),
  	  lists:reverse([LastExpr1| Exprs]);
        variable when IsTuple ->
  	  As = refac_syntax:get_ann(LastExpr),
  	  case lists:keysearch(def, 1, As) of
  	    {value, {def, DefinePos}} ->
  		case is_used_only_once(refac_syntax:block_expr(Body), DefinePos) of
  		  true ->
  		      {Body1, Modified} =
  			  do_state_to_record_in_match_expr(
  			    refac_syntax:block_expr(Body), LastExpr, DefinePos, RecordName, RecordFields, IsTuple, SM),
  		      case Modified of
  			true ->
  			    refac_syntax:block_expr_body(Body1);
  			false ->
  			    LastExpr1 = make_tuple_to_record_app(LastExpr, RecordName, RecordFields, IsTuple),
  			    lists:reverse([LastExpr1| Exprs])
  		      end;
  		  false ->
  		      LastExpr1 = make_tuple_to_record_app(LastExpr, RecordName, RecordFields, IsTuple),
  		      lists:reverse([LastExpr1| Exprs])
  		end;
  	    _ ->
  		LastExpr1 = make_tuple_to_record_app(LastExpr, RecordName, RecordFields, IsTuple),
  		lists:reverse([LastExpr1| Exprs])
  	  end;
        case_expr ->
  	  Args = refac_syntax:case_expr_argument(LastExpr),
  	  Cs = refac_syntax:case_expr_clauses(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:case_expr(Args, Cs1)),
  	  lists:reverse([LastExpr1| Exprs]);
        if_expr ->
  	  Cs = refac_syntax:if_expr_clauses(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:if_expr(Cs1)),
  	  lists:reverse([LastExpr1| Exprs]);
        cond_expr ->
  	  Cs = refac_syntax:cond_expr_clauses(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:cond_expr(Cs1)),
  	  lists:reverse([LastExpr1| Exprs]);
        block_expr ->
  	  B = refac_syntax:block_expr_body(LastExpr),
  	  B1 = do_state_to_record_in_init_fun_clause_body(B, RecordName, RecordFields, DefPs, IsTuple, SM),
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:block_expr(B1)),
  	  lists:reverse([LastExpr1| Exprs]);
        receive_expr ->
  	  Cs = refac_syntax:receive_expr_clauses(LastExpr),
  	  T = refac_syntax:receive_expr_timeout(LastExpr),
  	  A = refac_syntax:receive_expr_action(LastExpr),
  	  Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM) || C <- Cs],
  	  LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:receive_expr(Cs1, T, A)),
  	  lists:reverse([LastExpr1| Exprs]);
        _ when IsTuple ->
  	  LastExpr1 = make_tuple_to_record_app(LastExpr, RecordName, RecordFields, IsTuple),
  	  lists:reverse([LastExpr1| Exprs]);
        _ ->
  	  Fields = [mk_record_field(hd(RecordFields), refac_util:reset_attrs(LastExpr))],
  	  LastExpr1 = refac_util:rewrite(LastExpr, mk_record_expr(RecordName, Fields)),
  	  lists:reverse([LastExpr1| Exprs])
      end.
  
  do_state_to_record_in_match_expr(Body, _LastExpr, DefinePos, RecordName, RecordFields, _IsTuple, SM)
      when SM == eqc_statem orelse SM == eqc_fsm ->
      Fun = fun (Node, _Others) ->
  		  case refac_syntax:type(Node) of
  		    match_expr ->
  			P = refac_syntax:match_expr_pattern(Node),
  			B = refac_syntax:match_expr_body(Node),
  			case refac_syntax:type(P) of
  			  variable ->
  			      Pos = refac_syntax:get_pos(P),
  			      case lists:member(Pos, DefinePos) of
  				true ->
  				    case refac_syntax:type(B) of
  				      tuple ->
  					  B1 = tuple_to_record_expr(B, RecordName, RecordFields),
  					  Node1 = refac_syntax:match_expr(P, refac_util:rewrite(B, B1)),
  					  {refac_util:rewrite(Node, Node1), true};
  				      _ ->
  					  {Node, false}
  				    end;
  				false ->
  				    {Node, false}
  			      end;
  			  _ -> {Node, false}
  			end;
  		    _ -> {Node, false}
  		  end
  	  end,
      refac_util:stop_tdTP(Fun, Body, {});
  do_state_to_record_in_match_expr(Body, LastExpr, DefinePos, RecordName, RecordFields, IsTuple,SM)
      when SM == gen_fsm ->
      Msg = "Wrangler did not know how to transform the expression at location: ",
      Pos = refac_syntax:get_pos(LastExpr),
      Fun1 = fun (B) ->
  		   case refac_syntax:type(B) of
  		       tuple ->
  			   Es = refac_syntax:tuple_elements(B),
  			   Size = length(Es),
  			   case Size =< 2  of 
  			       true ->{B, true};
  			       false ->
  				   Tag = hd(Es),
  				   case refac_syntax:type(Tag) of
  				       atom ->
  					   case refac_syntax:atom_value(Tag) of
  					       ok -> 
  						   gen_fsm_state_to_record(RecordName, RecordFields, B, 3, IsTuple);
  					       next_state -> 
  						   gen_fsm_state_to_record(RecordName, RecordFields, B, 3, IsTuple);
  					       reply ->
  						   gen_fsm_state_to_record(RecordName, RecordFields, B, 4, IsTuple);
  					       stop ->
  						   gen_fsm_state_to_record(RecordName, RecordFields, B, Size, IsTuple);
  					       _ ->
  						   throw({error, Msg ++ io_lib:format("~p", [Pos])})
  					   end;
  				       _ -> throw({error, Msg ++ io_lib:format("~p", [Pos])})
  				   end
  			   end;
  		     atom ->
  			 {B, true};
  		     _ ->
  			 throw({error, Msg ++ io_lib:format("~p", [Pos])})
  		   end
  	   end,
      Fun = fun (Node, _Others) ->
  		  case refac_syntax:type(Node) of
  		    match_expr ->
  			P = refac_syntax:match_expr_pattern(Node),
  			B = refac_syntax:match_expr_body(Node),
  			case refac_syntax:type(P) of
  			  variable ->
  			      Pos = refac_syntax:get_pos(P),
  			      case lists:member(Pos, DefinePos) of
  				true ->
  				      {B1, Modified} = Fun1(B),
  				      case Modified of
  					true ->
  					    Node1 = refac_syntax:match_expr(P, B1),
  					    {refac_util:rewrite(Node, Node1), true};
  					false ->
  					  {Node, false}
  				    end;
  				false ->
  				    {Node, false}
  			      end;
  			  _ -> {Node, false}
  			end;
  		    _ -> {Node, false}
  		  end
  	  end,
      refac_util:stop_tdTP(Fun, Body, {}).
  
  gen_fsm_state_to_record(RecordName, RecordFields, B, Nth, IsTuple) ->
      State = element(Nth, B),
      case refac_syntax:type(State) of
  	tuple when IsTuple ->
  	    State1 =tuple_to_record_expr(State, RecordName, RecordFields),
  	    setelement(Nth, B, State1);
  	_ -> 
  	    State1 = make_tuple_to_record_app(State, RecordName, RecordFields, IsTuple),
  	    setelement(Nth, B, State1)
      end.
  
  
  pre_cond_check(RecordName, FieldNames, ModInfo) ->
      check_record_and_field_names(RecordName, FieldNames),
  
negative.code: 		{value, {RecordName, Fields}} ->
  		    case length(Fields) =/= length(FieldNames) of
  			true ->
  			    throw({error, "Record with the same name, "
  				   "but different number of fields, "
  				   "is already in scope."});
  			false ->
  			    ok
  		    end,
  		    FieldNames1 = [F || {F, _V} <- Fields],
  		    case lists:usort(FieldNames)== lists:usort(FieldNames1) of 
  			true ->
  			    true;
  			false ->
  			    throw({error, "Record with the same name, "
  				   "but different field names, "
  				   "is already in scope."})
  		    end
  	    end;
  	false ->
  	    false
      end.
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  %%                  Transformation                                             %%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  state_to_record(FileName, SearchPaths, TabWidth, SM) ->
      {ok, {_,  ModInfo}} = refac_util:parse_annotate_file(FileName, true, SearchPaths, TabWidth),
      {value, {module, ModName}} = lists:keysearch(module, 1, ModInfo),
      case is_statemachine_used(ModInfo, SM) of
  	true ->
  	    check_current_state_type(FileName, ModName, ModInfo, SM);
  	{error, Msg}-> throw({error, Msg})
      end.
  
  
  state_to_record_1(FileName, RecordName, RecordFields, StateFuns, IsTuple, SM, SearchPaths, TabWidth, Editor, Cmd) ->
      {ok, {AnnAST, Info}} = refac_util:parse_annotate_file(FileName, true, SearchPaths, TabWidth),
      {value, {module, ModName}} = lists:keysearch(module, 1, Info),
      RecordExists = pre_cond_check(RecordName, RecordFields, Info),
      RecordFields1 = [list_to_atom(F) || F <- RecordFields],
      AnnAST1 = do_state_to_record(ModName, Info, AnnAST, list_to_atom(RecordName), RecordFields1, 
  				 StateFuns,RecordExists, IsTuple, SM),
      case Editor of
  	emacs ->
  	    refac_util:write_refactored_files_for_preview([{{FileName, FileName}, AnnAST1}], Cmd),
  	    {ok, [FileName]};
  	eclipse ->
  	    Content = refac_prettypr:print_ast(refac_util:file_format(FileName), AnnAST1),
  	    {ok, [{FileName, FileName, Content}]}
      end.
  
  
  
  do_state_to_record(ModName, ModInfo,  AnnAST, RecordName, RecordFields, StateFuns, RecordExists, IsTuple, SM) ->
      Forms = refac_syntax:form_list_elements(AnnAST),
      TupleToRecordFunName = tuple_to_record_fun_name(ModInfo, RecordName),
      RecordToTupleFunName = record_to_tuple_fun_name(ModInfo, RecordName),
      Fun = fun (F) ->
  		  case refac_syntax:type(F) of
  		    function ->
  			do_state_to_record_1(ModName, F, RecordName, RecordFields, StateFuns, IsTuple, SM, 
  					     TupleToRecordFunName, RecordToTupleFunName);
  		    _ -> F
  		  end
  	  end,
      Forms1 = [Fun(F) || F <- Forms],
      Forms2 = add_util_funs(Forms1, RecordName, RecordFields,TupleToRecordFunName, RecordToTupleFunName),
      case RecordExists of
        true ->
  	  refac_syntax:form_list(Forms2);
        false ->
  	  RecordDef = mk_record_attribute(RecordName, RecordFields),
  	  NewForms =  insert_record_attribute(Forms2, RecordDef),
  	  refac_syntax:form_list(NewForms)
      end.
  
  do_state_to_record_1(ModName, Fun, RecordName, RecordFields, StateFuns, IsTuple, 
  		     SM, TupleToRecordFunName, RecordToTupleFunName) ->
      As = refac_syntax:get_ann(Fun),
      {value, {fun_def, {M, F, A, _, _}}} = lists:keysearch(fun_def, 1, As),
      CallBacks =  callbacks(ModName, StateFuns, SM),
      NewFun = case lists:keysearch({M, F, A}, 1, CallBacks) of
  		 {value, {{M, F, A}, {PatIndex, ReturnState}}} ->
  		     ?debug("MFA1:\n~p\n", [{M,F,A}]),
  		     do_state_to_record_in_callback_fun(
  		       PatIndex, Fun, RecordName, RecordFields, IsTuple, ReturnState, SM, 
  		       TupleToRecordFunName, RecordToTupleFunName);
  		 false -> Fun
  	     end,
      wrap_fun_interface(NewFun, ModName, RecordName, RecordFields, IsTuple, StateFuns, SM,
  		      TupleToRecordFunName, RecordToTupleFunName).
  	  
  		 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
  do_state_to_record_in_callback_fun(PatIndex, Fun, RecordName, RecordFields, IsTuple, ReturnState, SM,
  				  TupleToRecordFunName, RecordToTupleFunName) ->
      FunName = refac_syntax:function_name(Fun),
      Cs = refac_syntax:function_clauses(Fun),
      Cs1 = [do_state_to_record_in_callback_clause(PatIndex, C, RecordName, RecordFields, 
  						 IsTuple, ReturnState, SM, TupleToRecordFunName, 
  						 RecordToTupleFunName) 
  	   || C <- Cs],
      refac_util:rewrite(Fun, refac_syntax:function(FunName, Cs1)).
  
  
  do_state_to_record_in_callback_clause(PatIndex, C, RecordName, RecordFields, IsTuple, ReturnState, SM, 
  				     TupleToRecordFunName, RecordToTupleFunName) ->
      Ps = refac_syntax:clause_patterns(C),
      G = refac_syntax:clause_guard(C),
      B = refac_syntax:clause_body(C),
      {Ps1, DefPs}  = do_state_to_record_in_pats(Ps, PatIndex, RecordName, RecordFields, IsTuple), 
      B1 = do_state_to_record_in_callback_fun_clause_body(
  	   B, RecordName, RecordFields, DefPs, IsTuple, ReturnState, SM,TupleToRecordFunName, RecordToTupleFunName),
      B2= remove_record_tuple_conversions(refac_syntax:block_expr(B1), TupleToRecordFunName, RecordToTupleFunName),
      B3= refac_syntax:block_expr_body(B2),
      refac_util:rewrite(C, refac_syntax:clause(Ps1, G, B3)).
  
  
  do_state_to_record_in_pats(Ps, PatIndex, RecordName, RecordFields, IsTuple) ->
      Res =[do_state_to_record_in_pats_1({P, Index}, PatIndex, RecordName, RecordFields, IsTuple) || 
  	     {P, Index}<- lists:zip(Ps, lists:seq(1, length(Ps)))],
      {NewPs, DefPs} =lists:unzip(Res),
      {NewPs, lists:append(DefPs)}.
  
  do_state_to_record_in_pats_1({P, Index}, PatIndex, RecordName, RecordFields, IsTuple) ->
      case lists:member(Index, PatIndex) of 
  	true ->
  	    do_state_to_record_in_pats_2(P, RecordName, RecordFields, IsTuple);
  	false ->
  	    {P,[]}
      end.
  
  do_state_to_record_in_pats_2(P, RecordName, RecordFields, IsTuple) ->
      case refac_syntax:type(P) of
  	variable ->
  	    {P, [refac_syntax:get_pos(P)]};
  	tuple when IsTuple ->
  	    RecordExpr = tuple_to_record_expr(P, RecordName, RecordFields),
  	    {refac_util:rewrite(P, RecordExpr),[]};
  	match_expr ->
  	    tuple_to_record_in_match_expr_pattern(
  	      P, RecordName, RecordFields, IsTuple);
  	underscore ->
  	    {P, []};
  	_ when IsTuple ->
  	    Pos = refac_syntax:get_pos(P),
  	    throw({error, "Wrangler did not know how to transform the pattern at location: " 
  		   ++ io_lib:format("~p", [Pos])});
  	_ when length(RecordFields) == 1 ->
  	    Fields = [mk_record_field(hd(RecordFields), P)],
  	    P1 = refac_util:rewrite(P, mk_record_expr(RecordName, Fields)),
  	    {P1, []};
        _ ->
  	  Pos = refac_syntax:get_pos(P),
  	  throw({error, "Wrangler did not know how to transform the pattern at location: " 
  		 ++ io_lib:format("~p", [Pos])})
      end.
  
  tuple_to_record_in_match_expr_pattern(State, RecordName, RecordFields, IsTuple)->	   
      P=refac_syntax:match_expr_pattern(State),
      B = refac_syntax:match_expr_body(State),
      {P1, Pos1} =
  	do_state_to_record_in_pats_2(P, RecordName, RecordFields, IsTuple),
      {B1, Pos2} = 
  	do_state_to_record_in_pats_2(B, RecordName, RecordFields, IsTuple),
      {refac_util:rewrite(State, refac_syntax:match_expr(P1,B1)), Pos1++Pos2}.
  
  
  do_state_to_record_in_callback_fun_clause_body(Body, RecordName, RecordFields, 
  					       DefPs, IsTuple, ReturnState, SM,
  					      TupleToRecordFunName, RecordToTupleFunName) ->
      ?debug("RecordFields:\n~p\n", [RecordFields]),
      Fun = fun (Node, _Others) ->
  		  case refac_syntax:type(Node) of
  		      application when IsTuple ->
  			  As = refac_syntax:get_ann(refac_syntax:application_operator(Node)),
  			  case lists:keysearch(fun_def, 1, As) of
  			      {value, {fun_def, {erlang, element, 2, _, _}}} ->
  				  element_to_record_access(RecordName, RecordFields, DefPs, Node,RecordToTupleFunName);
  			      {value, {fun_def, {erlang, setelement, 3, _, _}}} ->
  				  setelement_to_record_expr(Node, RecordName, RecordFields, DefPs, IsTuple,
  							  TupleToRecordFunName, RecordToTupleFunName);			  
  			      _ -> Node
  			  end;
  		      variable ->
  			  As = refac_syntax:get_ann(Node),
  			  case lists:keysearch(def, 1, As) of
  			      {value, {def, DefinePos}} ->
  				  case DefinePos -- DefPs =/= DefinePos of
  				      true ->
  					  make_record_to_tuple_app(Node, RecordName, RecordFields, IsTuple, 
  								  TupleToRecordFunName, RecordToTupleFunName);
  				      false ->
  					  Node
  				  end;
  			      false -> Node
  			  end;
  		      _ -> Node
  		  end
  	  end,
      Body1 = refac_util:full_buTP(Fun, refac_syntax:block_expr(Body), {}),
      Body2 = is_tuple_to_is_record(Body1, RecordName,RecordToTupleFunName),
      Body3 = refac_syntax:block_expr_body(Body2),
      case ReturnState of 
  	true ->
   	    do_state_to_record_in_init_fun_clause_body(
  	      Body3, RecordName, RecordFields, DefPs, IsTuple, SM,
  	      TupleToRecordFunName, RecordToTupleFunName);
  	false ->
  	    Body3
      end.
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM,
  				     TupleToRecordFunName, RecordToTupleFunName) ->
      P = refac_syntax:clause_patterns(C),
      G = refac_syntax:clause_guard(C),
      B = refac_syntax:clause_body(C),
      B1=do_state_to_record_in_init_fun_clause_body(B, RecordName, RecordFields,[], IsTuple, SM,
  						 TupleToRecordFunName, RecordToTupleFunName),
      refac_util:rewrite(C, refac_syntax:clause(P, G, B1)).
  
   
  		      
  do_state_to_record_in_init_fun_clause_body(Body, RecordName, RecordFields, DefPs, IsTuple, SM, 
  					 TupleToRecordFunName, RecordToTupleFunName) ->
      Msg = "Wrangler did not know how to transform the expression at location: ",
      [LastExpr| Exprs] = lists:reverse(Body),
      Pos = refac_syntax:get_pos(LastExpr),
      case refac_syntax:type(LastExpr) of
  	tuple when IsTuple ->
  	    case SM of
  		gen_fsm ->
  		    LastExpr1= tuple_to_record_in_gen_fsm(
  			       LastExpr, RecordName, RecordFields, IsTuple, Msg, Pos,
  				TupleToRecordFunName, RecordToTupleFunName),
  		    lists:reverse([LastExpr1| Exprs]);		
  		_ -> 
  		    LastExpr1= tuple_to_record_expr(LastExpr, RecordName, RecordFields),
  		    lists:reverse([LastExpr1| Exprs])
  	    end;
  	variable  ->
  	    As = refac_syntax:get_ann(LastExpr),
  	    case lists:keysearch(def, 1, As) of
  		{value, {def, DefinePos}} ->
  		    case is_used_only_once(refac_syntax:block_expr(Body), DefinePos) of
  			true ->
  			    {Body1, Modified} =
  				do_state_to_record_in_match_expr(
  				  refac_syntax:block_expr(Body), LastExpr, DefinePos, RecordName, RecordFields, IsTuple, 
  				  SM, TupleToRecordFunName, RecordToTupleFunName),
  			    case Modified of
  				true ->
  				    refac_syntax:block_expr_body(Body1);
  				false ->
  				    fail_or_tuple_to_record_app(Body, RecordName, RecordFields, IsTuple, SM,
  							       TupleToRecordFunName, RecordToTupleFunName)
  			    end;
  			false ->
  			    fail_or_tuple_to_record_app(Body, RecordName, RecordFields, IsTuple, SM,
  						       TupleToRecordFunName, RecordToTupleFunName)
  		    end;
  		_ ->
  		    fail_or_tuple_to_record_app(Body, RecordName, RecordFields, IsTuple, SM,
  					       TupleToRecordFunName, RecordToTupleFunName) 
  	    end;
  	case_expr ->
  	    Args = refac_syntax:case_expr_argument(LastExpr),
  	    Cs = refac_syntax:case_expr_clauses(LastExpr),
  	    Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM,
  							TupleToRecordFunName, RecordToTupleFunName) || C <- Cs],
  	    LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:case_expr(Args, Cs1)),
  	    lists:reverse([LastExpr1| Exprs]);
  	if_expr ->
  	    Cs = refac_syntax:if_expr_clauses(LastExpr),
  	    Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM,
  							TupleToRecordFunName, RecordToTupleFunName) || C <- Cs],
  	    LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:if_expr(Cs1)),
  	    lists:reverse([LastExpr1| Exprs]);
  	cond_expr ->
  	    Cs = refac_syntax:cond_expr_clauses(LastExpr),
  	    Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM,
  							 TupleToRecordFunName, RecordToTupleFunName) || C <- Cs],
  	    LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:cond_expr(Cs1)),
  	    lists:reverse([LastExpr1| Exprs]);
  	block_expr ->
  	    B = refac_syntax:block_expr_body(LastExpr),
  	    B1 = do_state_to_record_in_init_fun_clause_body(B, RecordName, RecordFields, DefPs, IsTuple, SM,
  							   TupleToRecordFunName, RecordToTupleFunName),
  	    LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:block_expr(B1)),
  	    lists:reverse([LastExpr1| Exprs]);
  	receive_expr ->
  	    Cs = refac_syntax:receive_expr_clauses(LastExpr),
  	    T = refac_syntax:receive_expr_timeout(LastExpr),
  	    A = refac_syntax:receive_expr_action(LastExpr),
  	    Cs1 = [do_state_to_record_in_init_fun_clause(C, RecordName, RecordFields, IsTuple, SM,
  							TupleToRecordFunName, RecordToTupleFunName) || C <- Cs],
  	    LastExpr1 = refac_util:rewrite(LastExpr, refac_syntax:receive_expr(Cs1, T, A)),
  	    lists:reverse([LastExpr1| Exprs]);
  	atom when SM ==gen_fsm ->
  	    Body;
  	_ when IsTuple ->
  	    fail_or_tuple_to_record_app(Body, RecordName, RecordFields, IsTuple, SM, 
  					TupleToRecordFunName, RecordToTupleFunName);
  	_ when SM==gen_fsm ->
  	     throw({error, Msg ++ io_lib:format("~p", [Pos])});
  	_ ->
  	    Fields = [mk_record_field(hd(RecordFields), LastExpr)],
  	    LastExpr1 = refac_util:rewrite(LastExpr, mk_record_expr(RecordName, Fields)),
  	    lists:reverse([LastExpr1| Exprs])
      end.
  
  fail_or_tuple_to_record_app(Body, RecordName, RecordFields, IsTuple, SM, 
  			    TupleToRecordFunName, RecordToTupleFunName) ->
      Msg = "Wrangler did not know how to transform the expression at location: ",
      [LastExpr| Exprs] = lists:reverse(Body),
      Pos = refac_syntax:get_pos(LastExpr),
      case SM of 
  	gen_fsm ->
  	    throw({error, Msg ++ io_lib:format("~p", [Pos])});
  	_ ->
  	    LastExpr1 = make_tuple_to_record_app(LastExpr, RecordName, RecordFields, IsTuple,
  						TupleToRecordFunName, RecordToTupleFunName),
  	    lists:reverse([LastExpr1| Exprs])
      end.
  	    
  do_state_to_record_in_match_expr(Body, _LastExpr, DefinePos, RecordName, RecordFields, _IsTuple, SM,
  				_TupleToRecordFunName, RecordToTupleFunName)
      when SM == eqc_statem orelse SM == eqc_fsm ->
      Fun = fun (Node, _Others) ->
  		  case refac_syntax:type(Node) of
  		    match_expr ->
  			P = refac_syntax:match_expr_pattern(Node),
  			B = refac_syntax:match_expr_body(Node),
  			case refac_syntax:type(P) of
  			  variable ->
  			      Pos = refac_syntax:get_pos(P),
  			      case lists:member(Pos, DefinePos) of
  				true ->
  				    case refac_syntax:type(B) of
  					tuple ->
  					    B1 = tuple_to_record_expr(B, RecordName, RecordFields),
  					    Node1 = refac_syntax:match_expr(P, refac_util:rewrite(B, B1)),
  					    {refac_util:rewrite(Node, Node1), true};
  					application ->
  					    case is_app(B, {RecordToTupleFunName, 1}) of
  						true ->
  						    [T] = refac_syntax:application_arguments(B),
  						    {T, true};
  						false ->
  						    {Node, false}
  					    end;
  				      _ ->
  					  {Node, false}
  				    end;
  				false ->
  				    {Node, false}
  			      end;
  			  _ -> {Node, false}
  			end;
  		    _ -> {Node, false}
  		  end
  	  end,
      refac_util:stop_tdTP(Fun, Body, {});
  
  do_state_to_record_in_match_expr(Body, LastExpr, DefinePos, RecordName, RecordFields, IsTuple, SM,
  				TupleToRecordFunName, RecordToTupleFunName)
      when SM == gen_fsm ->
      Msg = "Wrangler did not know how to transform the expression at location: ",
      Pos = refac_syntax:get_pos(LastExpr),
      Fun1 = fun (B) ->
  		   case refac_syntax:type(B) of
  		       tuple -> {tuple_to_record_in_gen_fsm(B,RecordName, RecordFields, 
  							    IsTuple, Msg, Pos, TupleToRecordFunName, 
  							    RecordToTupleFunName), true};
  		       variable ->
  			   {B, false};
  		       _ ->
  			   throw({error, Msg ++ io_lib:format("~p", [Pos])})
  		   end
  	   end,
      Fun = fun (Node, _Others) ->
  		  case refac_syntax:type(Node) of
  		    match_expr ->
  			P = refac_syntax:match_expr_pattern(Node),
  			B = refac_syntax:match_expr_body(Node),
  			case refac_syntax:type(P) of
  			  variable ->
  			      Pos1 = refac_syntax:get_pos(P),
  			      case lists:member(Pos1, DefinePos) of
  				  true ->
  				      {B1, Modified} = Fun1(B),
  				      case Modified of
  					  true ->
  					      Node1 = refac_syntax:match_expr(P, B1),
  					      {refac_util:rewrite(Node, Node1), true};
  					  false ->
  					      {Node, false}
  				      end;
  				  false ->
  				    {Node, false}
  			      end;
  			  _ -> {Node, false}
  			end;
  		    _ -> {Node, false}
  		  end
  	  end,
      refac_util:stop_tdTP(Fun, Body, {}).
  
  tuple_to_record_in_gen_fsm(Tuple, RecordName, RecordFields, IsTuple, Msg, Pos,
  			   TupleToRecordFunName, RecordToTupleFunName) ->
      Es = refac_syntax:tuple_elements(Tuple),
      Size = length(Es),
      case Size =< 2 of
  	true -> Tuple;
  	false ->
  	    Tag = hd(Es),
  	    case refac_syntax:type(Tag) of
  		atom ->
  		    case refac_syntax:atom_value(Tag) of
  			ok ->
  			    gen_fsm_state_to_record(RecordName, RecordFields, Tuple, 3, IsTuple, 
  						   TupleToRecordFunName, RecordToTupleFunName);
  			next_state ->
  			    gen_fsm_state_to_record(RecordName, RecordFields, Tuple, 3, IsTuple,
  						   TupleToRecordFunName, RecordToTupleFunName);
  			reply ->
  			    gen_fsm_state_to_record(RecordName, RecordFields, Tuple, 4, IsTuple,
  						   TupleToRecordFunName, RecordToTupleFunName);
  			stop ->
  			    gen_fsm_state_to_record(RecordName, RecordFields, Tuple, Size, IsTuple,
  						   TupleToRecordFunName, RecordToTupleFunName);
  			_ ->
  			    throw({error, Msg ++ io_lib:format("~p", [Pos])})
  		    end;
  		_ -> throw({error, Msg ++ io_lib:format("~p", [Pos])})
  	    end
      end.
  
  gen_fsm_state_to_record(RecordName, RecordFields, B, Nth, IsTuple, 
  			TupleToRecordFunName, RecordToTupleFunName) ->
      Es = list_to_tuple(refac_syntax:tuple_elements(B)),
      State = element(Nth, Es),
      case refac_syntax:type(State) of
  	tuple when IsTuple ->
  	    State1 =tuple_to_record_expr(State, RecordName, RecordFields),
  	    Es1=tuple_to_list(setelement(Nth, Es, State1)),
  	    refac_util:rewrite(B, refac_syntax:tuple(Es1));	    
  	_ -> 
  	    State1 = make_tuple_to_record_app(State, RecordName, RecordFields, IsTuple, 
  					     TupleToRecordFunName, RecordToTupleFunName),
  	    Es1= tuple_to_list(setelement(Nth, Es, State1)),
  	    refac_util:rewrite(B, refac_syntax:tuple(Es1))
      end.
  
  
  
  wrap_fun_interface(Form, ModName, RecordName, RecordFields, IsTuple, StateFuns,SM,
  		  TupleToRecordFunName, RecordToTupleFunName) ->
      Form1 = wrap_fun_interface_in_arg(Form, ModName, RecordName, RecordFields, IsTuple, StateFuns,SM,
  				     TupleToRecordFunName, RecordToTupleFunName),
      wrap_fun_interface_in_return(Form1, ModName, RecordName, RecordFields, IsTuple, StateFuns, SM,
  				TupleToRecordFunName, RecordToTupleFunName).
  
  wrap_fun_interface_in_return(Form, ModName, RecordName, RecordFields,IsTuple, StateFuns, SM,
  			    TupleToRecordFunName, RecordToTupleFunName) ->
      Fun= fun(Node, _Others) ->
  		 case is_callback_fun_app(Node, ModName, StateFuns, SM) of
  		     {true, _PatIndex, true} ->
  