anchor.diff:      ?wrangler_io("\nCMD: ~p:move_fun(~p, ~p, ~p, ~p, ~p, ~p, ~p).",
   		 [?MODULE, FName, Line, Col, TargetModorFileName, CreateNewFile, SearchPaths, TabWidth]),
       {TargetFName, TargetModName} = get_target_file_mod_name(FName, TargetModorFileName),
  -    case TargetFName of 
  -	FName -> {error, "The target module is the same as the current module."};
  -	_ -> case filelib:is_file(TargetFName) orelse CreateNewFile == t orelse CreateNewFile == true of
  -		 true ->
  -		     {ok, {AnnAST, Info}} = refac_util:parse_annotate_file(FName, true, SearchPaths, TabWidth),
  -		     case refac_util:pos_to_fun_def(AnnAST, {Line, Col}) of
  -			 {ok, Def} ->
  -			     {value, {fun_def, {ModName, FunName, Arity, _Pos1, _Pos2}}} =
  -				 lists:keysearch(fun_def, 1, refac_syntax:get_ann(Def)),
  -			     NewTargetFile = case not filelib:is_file(TargetFName) andalso (CreateNewFile == t orelse CreateNewFile == true) of
  -						 true -> create_new_file(TargetFName, TargetModName),
  -							 true;
  -						 _ -> false
  -					     end,
  -			     R = side_cond_check({FName, ModName, FunName, Arity, Def}, TargetFName, list_to_atom(TargetModName), Def, SearchPaths, TabWidth),
  -			     case R of
  -				 true ->
  -				     {ok, {TargetAnnAST, Info1}} = refac_util:parse_annotate_file(TargetFName, true, SearchPaths, TabWidth),  
  -				     {AnnAST1, TargetAnnAST1} =
  -					 do_transformation({AnnAST, Info}, {TargetAnnAST, Info1},{ModName, FunName, Arity}, TargetModName),
  -				     case refac_util:is_exported({FunName, Arity}, Info) of
  -					 true ->
  -					     ?wrangler_io("\nChecking client modules in the following search paths: \n~p\n", [SearchPaths]),
  -					     ClientFiles = lists:delete(TargetFName,
  -									refac_util:get_client_files(FName, SearchPaths)),
  -					     Results = refactor_in_client_modules(ClientFiles,
  -										  {ModName, FunName, Arity}, TargetModName, SearchPaths, TabWidth),
  -					     case Editor of
  -						 emacs ->
  -						     io:format("Results:\n~p\n", [Results]),
  -						     refac_util:write_refactored_files_for_preview([{{FName, FName}, AnnAST1},
  -												    {{TargetFName, TargetFName, NewTargetFile}, TargetAnnAST1}| Results]),
  -						     ChangedClientFiles =
  -							 lists:map(fun ({{F, _F}, _AST}) -> F end, Results),
  -						     ChangedFiles = [FName, TargetFName| ChangedClientFiles],
  -						     ?wrangler_io("The following files are to be changed by this refactoring:\n~p\n", [ChangedFiles]),
  -						     {ok, ChangedFiles};
  -						 eclipse ->
  -						     Results1 = [{{FName, FName}, AnnAST1},
  -								 {{TargetFName, TargetFName}, TargetAnnAST1}| Results],
  -						     Res = lists:map(fun ({{FName1, NewFName1}, AST}) ->
  -									     {FName1, NewFName1, refac_prettypr:print_ast(refac_util:file_format(FName1),AST)}
  -								     end,
  -								     Results1),
  -						     {ok, Res}
  -					     end;
  -					 false ->
  -					     case Editor of
  -						 emacs ->
  -						     refac_util:write_refactored_files_for_preview([{{FName, FName}, AnnAST1},
  -												    {{TargetFName, TargetFName, NewTargetFile}, TargetAnnAST1}]),
  -						     ChangedFiles = [FName, TargetFName],
  -						     ?wrangler_io("The following files are to be changed by this refactoring:\n~p\n",
  -								  [ChangedFiles]),
  -						     {ok, ChangedFiles};
  -						 eclipse ->
  -						     Results1 = [{{FName, FName}, AnnAST1}, {{TargetFName, TargetFName}, TargetAnnAST1}],
  -						     Res = lists:map(fun ({{FName1, NewFName1}, AST}) ->
  -									     {FName1, NewFName1, refac_prettypr:print_ast(refac_util:file_format(FName1),AST)}
  -								     end,
  -								     Results1),
  -						     {ok, Res}
  -					     end
  -				     end;
  -				 {error, Reason} -> {error, Reason}
  -			     end;
  -			 {error, Reason} -> {error, Reason}
  -		     end;
  -		 false -> {error, " Target module/file does not exist."}
  -	     end
  +    case TargetFName of
  +	FName -> throw({error, "The target module is the same as the current module."});
  +	_ -> ok
  +    end,
  +    case filelib:is_file(TargetFName) orelse CreateNewFile == t orelse CreateNewFile == true of
  +	true -> ok;
  +	false -> throw({error, " Target module/file does not exist."})
  +    end,
  +    {ok, {AnnAST, Info}} = refac_util:parse_annotate_file(FName, true, SearchPaths, TabWidth),
  +    case refac_util:pos_to_fun_def(AnnAST, {Line, Col}) of
  +	{ok, Def} ->
  +	    {value, {fun_def, {ModName, FunName, Arity, _Pos1, _Pos2}}} =
  +		lists:keysearch(fun_def, 1, refac_syntax:get_ann(Def)),
  +	    NewTargetFile = case not filelib:is_file(TargetFName) andalso
  +				(CreateNewFile == t orelse CreateNewFile == true)
  +				of
  +				true -> create_new_file(TargetFName, TargetModName),
  +					true;
  +				_ -> false
  +			    end,
  +	    case side_cond_check({FName, ModName, FunName, Arity}, TargetFName,
  +				 TargetModName, Def, SearchPaths, TabWidth)
  +		of
  +		{error, Reason} ->
  +		    case NewTargetFile of
  +			true -> file:delete(TargetFName);
  +			_ -> ok
  +		    end,
  +		    throw({error, Reason});
  +		true -> ok
  +	    end,
  +	    Pid = start_atom_process(),
  +	    {ok, {TargetAnnAST, TargetInfo}} = refac_util:parse_annotate_file(TargetFName, true, SearchPaths, TabWidth),
  +	    {AnnAST1, TargetAnnAST1} =
  +		do_transformation(FName, {AnnAST, Info}, {TargetAnnAST, TargetInfo}, {ModName, FunName, Arity},
  +				  TargetModName, SearchPaths, TabWidth, Pid),
  +	    check_atoms(FName, AnnAST1, [FunName], Pid),
  +	    check_atoms(TargetFName, TargetAnnAST1, [FunName], Pid),
  +	    Results = case refac_util:is_exported({FunName, Arity}, Info) of
  +			  true ->
  +			      ?wrangler_io("\nChecking client modules in the following search paths: \n~p\n", [SearchPaths]),
  +			      ClientFiles = lists:delete(TargetFName, refac_util:get_client_files(FName, SearchPaths)),
  +			      refactor_in_client_modules(ClientFiles, {ModName, FunName, Arity}, TargetModName, SearchPaths, TabWidth, Pid);
  +			  false ->
  +			      []
  +		      end,
  +	    output_atom_warning_msg(Pid, not_renamed_warn_msg(FunName), renamed_warn_msg(ModName)),
  +	    stop_atom_process(Pid),
  +	    case Editor of
  +		emacs ->
  +		    refac_util:write_refactored_files_for_preview([{{FName, FName}, AnnAST1},
  +								   {{TargetFName, TargetFName, NewTargetFile}, TargetAnnAST1}| Results]),
  +		    ChangedClientFiles = lists:map(fun ({{F, _F}, _AST}) -> F end, Results),
  +		    ChangedFiles = [FName, TargetFName| ChangedClientFiles],
  +		    ?wrangler_io("The following files are to be changed by this refactoring:\n~p\n", [ChangedFiles]),
  +		    {ok, ChangedFiles};
  +		eclipse ->
  +		    Results1 = [{{FName, FName}, AnnAST1}, {{TargetFName, TargetFName}, TargetAnnAST1}| Results],
  +		    Res = lists:map(fun ({{FName1, NewFName1}, AST}) ->
  +					    {FName1, NewFName1, refac_prettypr:print_ast(refac_util:file_format(FName1), AST)}
  +				    end,
  +				    Results1),
  +		    {ok, Res}
  +	    end;
  +	{error, Reason} -> {error, Reason}
       end.
   
   get_target_file_mod_name(CurrentFName, TargetModorFileName) ->
  
positive.code: 		  case lists:keysearch(fun_def,1,refac_syntax:get_ann(T1)) of
  		      {value, {fun_def, {ModName, FunName, Arity, _, _}}} ->
  			  case {ModName, FunName, Arity} of 
  			      {M, F, A} -> set_default_ann(T1);
  			      _ -> refac_syntax:set_pos(
  				     refac_syntax:remove_comments(
  				       refac_syntax:set_ann(T1, 
  							    [{fun_def, {ModName, FunName, Arity, {0,0},{0,0}}}])),{0,0})
  			  end;	      
  		      _ -> set_default_ann(T1)
  		  end
  	  end,
      refac_syntax_lib:map(Fun, Node).
  				       
  set_default_ann(Node) ->
      refac_syntax:set_pos(refac_syntax:remove_comments(refac_syntax:set_ann(Node, [])), {0,0}).
  
  
  has_implicit_fun_call(Tree) ->    
      F=fun(T,S) ->
  	      case refac_syntax:type(T) of 
  		  arity_qualifier-> S++[true];
  		  _  -> S
  	      end
        end,
      R = refac_syntax_lib:fold(F, [], Tree),
      lists:member(true, R).
  
  do_transformation({AnnAST, Info}, {TargetAnnAST, Info1}, {ModName, FunName, Arity}, TargetModName) ->
      Forms = refac_syntax:form_list_elements(AnnAST),
      FunToBeMoved = hd([F || F <- Forms, not is_not_the_fun(F, {ModName, FunName, Arity})]),
      FunsToBeExported = local_funs_to_be_exported(FunToBeMoved, {ModName, FunName, Arity}, Info),
      InScopeFunsInTargetMod = refac_util:inscope_funs(Info1),
      FunToBeMoved1 = transform_fun(FunToBeMoved, {ModName, FunName, Arity}, TargetModName, InScopeFunsInTargetMod),
      {AnnAST1, FunIsUsed} = do_remove_fun(AnnAST, {ModName, FunName, Arity}, FunsToBeExported, TargetModName),
      IsExported = refac_util:is_exported({FunName, Arity}, Info),
      Export = FunIsUsed or IsExported,
      TargetAnnAST1 = do_add_fun({TargetAnnAST, Info1}, FunToBeMoved1, {ModName, FunName, Arity}, TargetModName, Export),
      {AnnAST1, TargetAnnAST1}.
  
  %%==============================================================================
  %% Functions defined in the current module, but used by the function to be moved.
  %%===============================================================================
  local_funs_to_be_exported(Node, {ModName, FunName, Arity}, Info) ->
      Funs = fun (T, S) ->
  		   case refac_syntax:type(T) of
  		     application ->
  			 Operator = refac_syntax:application_operator(T),
  			 case application_info(T) of
  			   {{none, F}, A} ->
  			       case get_fun_def_info(Operator) of
  				 {ModName, F, A, _P} when {F, A} =/= {FunName, Arity} ->
  				     ordsets:add_element({F, A}, S);
  				 _ -> S
  			       end;
  			   _ -> S
  			 end;
  		     _ -> S
  		   end
  	   end,
      Fs = refac_syntax_lib:fold(Funs, ordsets:new(), Node),
      [F || F <- Fs, not refac_util:is_exported(F, Info)].
  
  
  %%======================================================================================
  %% Transform the function to be moved.
  %%======================================================================================
  transform_fun(Form, {ModName, FunName, Arity}, TargetModName, InScopeFunsInTargetMod) -> 
        {Form1, _} =refac_util:stop_tdTP(fun do_transform_fun/2, Form, {{ModName, FunName, Arity},
  							  TargetModName, InScopeFunsInTargetMod}),
        Form1.
  
  do_transform_fun(Node, {{ModName, FunName, Arity}, TargetModName, InScopeFunsInTargetMod}) ->
      MakeApp = fun (Node1, Operator1, Arguments1,ModName1, FunName1) ->
   		     Operator2 =refac_syntax:copy_attrs(Operator1,
   					     refac_syntax:module_qualifier(refac_syntax:atom(ModName1),
   									   refac_syntax:atom(FunName1))),
   		     Node2= refac_syntax:copy_attrs(Node1,
   						     refac_syntax:application(Operator2,Arguments1)),
   		     {Node2, false}
   	     end,
      case refac_syntax:type(Node) of 
   	  application ->
  	    Operator = refac_syntax:application_operator(Node),
  	    Arguments = refac_syntax:application_arguments(Node),
  	    case application_info(Node) of 
  		  {{none, F}, A} ->
    		      case {F, A} == {FunName, Arity} of 
    			  true -> {Node, false};
    			  false ->  case refac_syntax:type(Operator) of 
  				       atom ->{M, F, A, _P} = get_fun_def_info(Operator),
  					       case lists:member({M, F, A}, InScopeFunsInTargetMod) of 
  						  true -> {Node, false};
  						  _  ->MakeApp(Node, Operator, Arguments, M, F)
  					      end;
  				       _ -> {Node, false}
  				   end				   
    		      end;
   	 	  {{ModName, FunName}, Arity} ->MakeApp(Node,Operator, Arguments, TargetModName, FunName);
  		  _ -> {Node, false}
   	      end;
          _  -> {Node, false}
      end.
  
  
  %%=========================================================================
  %% Remove the function from the current module. 
  %%=========================================================================
  do_remove_fun(AnnAST, {ModName, FunName, Arity}, FunsToBeExported, TargetModName) ->
      Forms  = refac_syntax:form_list_elements(AnnAST),
      Res = [process(Form, {ModName, FunName, Arity}, TargetModName)||Form <-Forms, 
  								       is_not_the_fun(Form, {ModName, FunName, Arity})],
      Forms1 = lists:map(fun({F, _M}) -> F end, Res),
      Fun_is_Used = lists:member(true, lists:map(fun({_F, M}) -> M end, Res)),
      NewForms = case FunsToBeExported of 
  		   [] -> Forms1;
  		   [_H|_T] ->  Export = make_export(FunsToBeExported),
  			       {Forms11, Forms12} = lists:splitwith(fun(F) -> refac_syntax:type(F)==attribute 
  								  orelse refac_syntax:type(F) == comment end, Forms1),
  			       Forms11++Export++Forms12
  	       end,
      {refac_syntax:copy_attrs(AnnAST, refac_syntax:form_list(NewForms)), Fun_is_Used}.
  
  %%=======================================================================
  %% Add the function to the target module.
  %%=======================================================================
  do_add_fun({TargetAnnAST, Info}, FunToBeMoved, {ModName, FunName, Arity}, TargetModName, ToBeExported) ->
      Forms = refac_syntax:form_list_elements(TargetAnnAST),
      FunWithSameName = [F || F <- Forms, not is_not_the_fun(F, {list_to_atom(TargetModName), FunName, Arity})],
      NewFun = case FunWithSameName of
  	       [] -> [FunToBeMoved];
  	       _ -> []
  	     end,
      Forms1 = [process_forms_in_target_module(Form, {ModName, FunName, Arity}, TargetModName) || Form <- Forms],
      IsExported = refac_util:is_exported({FunName, Arity}, Info),
      NewForms = case ToBeExported andalso not IsExported of
  		 false -> Forms1 ++ NewFun;
  		 true -> Export = make_export([{FunName, Arity}]),
  			 {Forms11, Forms12} = lists:splitwith(fun (F) -> refac_syntax:type(F) == attribute
  									   orelse refac_syntax:type(F) == comment
  							      end, Forms1),
  			 Forms11 ++ Export ++ Forms12 ++ NewFun
  	       end,
      refac_syntax:copy_attrs(TargetAnnAST, refac_syntax:form_list(NewForms)).
  
  %%=========================================================================
  %% Refacotoring in the current module. 
  %%========================================================================
  process(Form, {ModName, FunName, Arity}, TargetModName) ->
      case refac_syntax:type(Form) of 
  	function -> add_change_module_qualifier(Form, {ModName, FunName, Arity}, TargetModName);
  	attribute -> Name = refac_syntax:attribute_name(Form),
  		     case refac_syntax:type(Name) of 
  			 atom ->case refac_syntax:atom_value(Name) of
  				    export -> [L] = refac_syntax:attribute_arguments(Form),
  					      Es = refac_syntax:list_elements(L),
  					      Es1 =[E  || E<-Es, 
  						  {refac_syntax:atom_value(refac_syntax:arity_qualifier_body(E)),
  						   refac_syntax:integer_value(refac_syntax:arity_qualifier_argument(E))}
  						   =/={FunName, Arity}],
  					      case length(Es1) == 0  of
  						  true -> {comment([""]), false};
  						  _ ->
  						      {refac_syntax:copy_attrs(Form, 
  							  refac_syntax:attribute(Name,[refac_syntax:list(Es1)])), false}
  						  end;
  				    _  -> {Form, false}
  				end;
  			 _  -> {Form,false}
  		     end;
  	_-> {Form,false}
      end.
  
  %%============================================================================
  %% Refactoring in the target module.
  %%============================================================================
  process_forms_in_target_module(Form, {ModName, FunName, Arity}, TargetModName) ->
      case refac_syntax:type(Form) of
        function -> remove_module_qualifier(Form, {ModName, FunName, Arity}, TargetModName);
        attribute -> Name = refac_syntax:attribute_name(Form),
  		   case refac_syntax:type(Name) of
  		     atom -> case refac_syntax:atom_value(Name) of
  			       import ->
  				   [H, L] = refac_syntax:attribute_arguments(Form),
  				   Es = refac_syntax:list_elements(L),
  				   Es1 = [E || E <- Es,
  					       {refac_syntax:atom_value(refac_syntax:arity_qualifier_body(E)),
  						refac_syntax:integer_value(refac_syntax:arity_qualifier_argument(E))}
  						 =/= {FunName, Arity}],
  				   case length(Es1) == 0 of
  				     true -> comment([""]);
  				     _ -> refac_syntax:copy_attrs(Form,
  								  refac_syntax:attribute(Name, [H, refac_syntax:list(Es1)]))
  				   end;
  			       _ -> Form
  			     end;
  		     _ -> Form
  		   end;
        _ -> Form
      end.
  
  %%============================================================================
  %% Add/remove module qualifier.
  %%============================================================================
  remove_module_qualifier(Form, {ModName, FunName, Arity}, TargetModName) ->    
      {Form1, _} = refac_util:stop_tdTP(fun do_remove_module_qualifier/2, Form, {{ModName, FunName, Arity}, TargetModName}),
      Form1.
  
  do_remove_module_qualifier(Node, {{ModName, FunName, Arity}, TargetModName}) ->
      case refac_syntax:type(Node) of 
  	application ->
  	    Op = refac_syntax:application_operator(Node),
  	    Args =refac_syntax:application_arguments(Node),
  	    case application_info(Node) of 
  		{{none, FunName}, Arity} -> {Node, true};
  		{{ModName, FunName}, Arity} -> Op1 = refac_syntax:copy_attrs(Op,refac_syntax:atom(FunName)),
  					       Node1 = refac_syntax:copy_attrs(Node, refac_syntax:application(Op1, Args)),
  					       {Node1, true};
  		{{_, apply},2} -> transform_apply_call(Node,{ModName, FunName, Arity}, TargetModName);
  		{{_, apply},3} -> transform_apply_call(Node, {ModName, FunName, Arity}, TargetModName);
  		{{_, spawn}, 3} ->transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName);
  		{{_, spawn}, 4} -> transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName); 
  		{{_, spawn_link}, 3} ->transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName);
  		{{_, spawn_link}, 4} ->transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName);
  		_ -> {Node, true}
  	    end;		
  	_ ->{Node, false}
      end.
  	    
  add_change_module_qualifier(Form, {ModName, FunName, Arity}, TargetModName) ->
      refac_util:full_tdTP(fun do_add_change_module_qualifier/2,
  			 Form, {{ModName, FunName, Arity}, TargetModName}).
     
  
  do_add_module_qualifier(Node, {{ModName, FunName, Arity}, TargetModName}) ->
     MakeApp = fun (Node1, Operator1, Arguments1,TargetModName1, FunName1) ->
  		     Operator2 =refac_syntax:copy_attrs(Operator1,
  							refac_syntax:module_qualifier(refac_syntax:atom(TargetModName1),
  										      refac_syntax:atom(FunName1))),
  		     Node2= refac_syntax:copy_attrs(Node1, refac_syntax:application(Operator2,Arguments1)),
  		     {Node2, false}
  	     end,
      case refac_syntax:type(Node) of 
  	application ->
  	    Operator = refac_syntax:application_operator(Node),
  	    Arguments = refac_syntax:application_arguments(Node),
  	    As = refac_syntax:get_ann(Op),
  	    case lists:keysearch(fun_def, 1, As)) of 
  	       {value, {fun_def, {Mod1, Fun1, Ari1, _}}} ->
  		 case lists:keysearch({Mod1, Fun1, Ari1}, 1, apply_style_funs()) of 
  
  
  
  		   
  %% 	      case application_info(Node) of 
  %% 		  {{none, FunName}, Arity} -> MakeApp(Node, Operator, Arguments, TargetModName, FunName);	      
  %% 		  {{ModName, FunName}, Arity} ->MakeApp(Node,Operator, Arguments, TargetModName, FunName);
  %% 		  {{_, apply},2} -> transform_apply_call(Node,{ModName, FunName, Arity}, TargetModName);
  %% 		  {{_, apply},3} -> transform_apply_call(Node, {ModName, FunName, Arity}, TargetModName);
  %% 		  {{_, spawn}, 3} ->transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName);
  %% 		  {{_, spawn}, 4} -> transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName); 
  %% 		  {{_, spawn_link}, 3} ->transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName);
  %% 		  {{_, spawn_link}, 4} ->transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName);
  %% 		   _ -> {Node, false}
  %% 	      end;
  %%        implicit_fun ->
  %% 	   Name = refac_syntax:implicit_fun_name(Node),
  %% 	   B = refac_syntax:atom_value(refac_syntax:arity_qualifier_body(Name)),
  %% 	   A = refac_syntax:integer_value(refac_syntax:arity_qualifier_argument(Name)),
  %% 	   case {B, A} of
  %% 	       {FunName, Arity} ->
  %% 		   FunName1 = refac_syntax:module_qualifier(refac_syntax:atom(TargetModName),
  %% 							    refac_syntax:atom(FunName)),
  %% 		   Node1 = refac_syntax:implicit_fun(FunName1, refac_syntax:arity_qualifier_argument(Name)),
  %% 		   {refac_syntax:copy_attrs(Node, Node1), true};
  %% 	       _ -> {Node, false}
  %% 	   end;
  %%        _  -> {Node, false}
  %%    end.
  %% ====================================================================================
  %%  Processing Client modules.
  %%====================================================================================
  
  refactor_in_client_modules(ClientFiles,{ModName, FunName, Arity}, TargetModName, SearchPaths, TabWidth) ->
      case ClientFiles of 
  	[] -> [];
  	[F | Fs] ->
  	    ?wrangler_io("The current file under refactoring is:\n~p\n", [F]),
  	    {ok, {AnnAST, Info}} =refac_util:parse_annotate_file(F,true, SearchPaths, TabWidth),      %% level=1
  	    {AnnAST1, Changed} = 
  				refactor_in_client_module_1({AnnAST, Info}, {ModName, FunName, Arity}, TargetModName),
  		if Changed ->
  		    [{{F,F}, AnnAST1} | refactor_in_client_modules(Fs, {ModName, FunName, Arity}, TargetModName, SearchPaths, TabWidth)];
  	       true -> refactor_in_client_modules(Fs, {ModName, FunName, Arity}, TargetModName, SearchPaths, TabWidth)
  	    end
  	end.
  refactor_in_client_module_1({AnnAST, _Info}, {ModName, FunName, Arity}, TargetModName) ->
      Forms  = refac_syntax:form_list_elements(AnnAST),
      Res = [process_in_client_module(Form, {ModName, FunName, Arity}, TargetModName)||Form <-Forms],
      Forms1 = lists:map(fun({F, _M}) -> F end, Res),
      Changed = lists:member(true, lists:map(fun({_F, M}) -> M end, Res)),
      {refac_syntax:copy_attrs(AnnAST, refac_syntax:form_list(Forms1)), Changed}.
  
  process_in_client_module(Form, {ModName, FunName, Arity}, TargetModName) ->
      case refac_syntax:type(Form) of
        function -> add_module_qualifier(Form, {ModName, FunName, Arity}, TargetModName);
        attribute -> Name = refac_syntax:attribute_name(Form),
  		   case refac_syntax:type(Name) of
  		     atom -> case refac_syntax:atom_value(Name) of
  			       import ->
  				   [H, L] = refac_syntax:attribute_arguments(Form),
  				   Es = refac_syntax:list_elements(L),
  				   Es1 = [E || E <- Es,
  					       {refac_syntax:atom_value(refac_syntax:arity_qualifier_body(E)),
  						refac_syntax:integer_value(refac_syntax:arity_qualifier_argument(E))}
  						 =/= {FunName, Arity}],
  				   {refac_syntax:copy_attrs(Form,
  							    refac_syntax:attribute(Name, [H, refac_syntax:list(Es1)])),
  				    length(Es) =/= length(Es1)};
  			       _ -> {Form, false}
  			     end;
  		     _ -> {Form, false}
  		   end;
        _ -> {Form, false}
      end.
       
  %%================================================================================
  %%              Some Utility Functions 
  %%================================================================================
  
  make_export(Names) ->
      Es = [refac_syntax:arity_qualifier(refac_syntax:atom(F),
  				       refac_syntax:integer(A))
  	  || {F, A} <- Names],
     %% comment(["** The following export is added by Wrangler. **"]),
      [erl_syntax:attribute(erl_syntax:atom('export'), [erl_syntax:list(Es)])].
  
  is_not_the_fun(Form, {ModName, FunName, Arity}) ->
      case refac_syntax:type(Form) of 
  	function -> 
  	    As = refac_syntax:get_ann(Form),
  	    case lists:keysearch(fun_def,1,As) of 
  		{value, {fun_def, {ModName, FunName, Arity, _Pos1, _Pos2}}} ->
  		    false;
  		_ -> true
  	    end;	    
  	_ -> true
      end.
  
  transform_apply_call(Node, {ModName, FunName, Arity}, TargetModName) ->
      Message = fun
  		(Pos) -> ?wrangler_io("WARNING: function ***apply*** is used at location({line, col}):~p, and wrangler "
  				      "could not decide whether this site should be refactored, please check!!!\n",
  				      [Pos])
  	      end,
      Operator = refac_syntax:application_operator(Node),
      Arguments = refac_syntax:application_arguments(Node),
      case Arguments of
        [Fun, Args] ->
  	  case refac_syntax:type(Fun) of
  	    implicit_fun ->
  		Name = refac_syntax:implicit_fun_name(Fun),
  		B = refac_syntax:atom_value(refac_syntax:arity_qualifier_body(Name)),
  		A = refac_syntax:integer_value(refac_syntax:arity_qualifier_argument(Name)),
  		case {B, A} of
  		  {FunName, Arity} ->
  		      FunName1 = refac_syntax:module_qualifier(refac_syntax:atom(TargetModName),
  							       refac_syntax:atom(FunName)),
  		      Fun1 = refac_syntax:implicit_fun(FunName1, refac_syntax:arity_qualifier_argument(Name)),
  		      {refac_syntax:copy_attrs(Node, refac_syntax:application(Operator, [Fun1, Args])), true};
  		  _ -> {Node, false}
  		end;
  	    _ -> {Node, false}
  	  end;
        [Mod, Fun, Args] ->
  	  Mod1 = refac_util:try_evaluation([refac_syntax:revert(Mod)]),
  	  Fun1 = refac_util:try_evaluation([refac_syntax:revert(Fun)]),
  	  Pos = refac_syntax:get_pos(Node),
  	  case Fun1 of
  	    {value, FunName} ->
  		case Mod1 of
  		  {value, ModName} ->
  		      case refac_syntax:type(Args) of
  			list ->
  			    case refac_syntax:list_length(Args) of
  			      Arity ->
  				  Mod2 = refac_syntax:atom(TargetModName),
  				  {refac_syntax:copy_pos(Node, refac_syntax:copy_attrs(Node,
  										       refac_syntax:application(Operator, [Mod2, Fun, Args]))), true};
  			      _ -> {Node, false}
  			    end;
  			nil -> if Arity == 0 ->
  				      Mod2 = refac_syntax:atom(TargetModName),
  				      {refac_syntax:copy_pos(Node, refac_syntax:copy_attrs(Node,
  											   refac_syntax:application(Operator, [Mod2, Fun, Args]))), true};
  				  true -> {Node, false}
  			       end;
  			_ -> Message(Pos),
  			     {Node, false}
  		      end;
  		  {value, _} -> {Node, false};
  		  {error, _Reason} ->
  		      case refac_syntax:type(Args) of
  			list -> case refac_syntax:list_length(Args) of
  				  Arity -> Message(Pos),
  					   {Node, false};
  				  _ -> {Node, false}
  				end;
  			_ -> Message(Pos),
  			     {Node, false}
  		      end
  		end;
  	    {value, _} -> {Node, false};
  	    {error, _Reason} ->
  		case Mod1 of
  		  {value, ModName} ->
  		      case refac_syntax:type(Args) of
  			list -> case refac_syntax:list_length(Args) of
  				  Arity -> Message(Pos),
  					   {Node, false};
  				  _ -> {Node, false}
  				end;
  			_ -> Message(Pos),
  			     {Node, false}
  		      end;
  		  {value, _} -> {Node, false};
  		  {error, _Reason} -> case refac_syntax:type(Args) of
  					list -> case refac_syntax:list_length(Args) of
  						  Arity -> Message(Pos),
  							   {Node, false};
  						  _ -> {Node, false}
  						end;
  					_ -> Message(Pos),
  					     {Node, false}
  				      end
  		end
  	  end;
        _ -> {Node, false}
      end.
  
  transform_spawn_call(Node, {ModName, FunName, Arity}, TargetModName) ->
      Message = fun
  		(Pos) -> ?wrangler_io("WARNING: function ***spawn*** is used at location({line, col}):~p, and wrangler "
  				      "could not decide whether this site should be refactored, please check!!!\n",
  				      [Pos])
  	      end,
      Operator = refac_syntax:application_operator(Node),
      Arguments = refac_syntax:application_arguments(Node),
      [N, Mod, Fun, Args] = if length(Arguments) == 4 -> Arguments;
  			     true -> [none] ++ Arguments
  			  end,
      Mod1 = refac_util:try_evaluation([refac_syntax:revert(Mod)]),
      Fun1 = refac_util:try_evaluation([refac_syntax:revert(Fun)]),
      Pos = refac_syntax:get_pos(Node),
      case Fun1 of
        {value, FunName} ->
  	  case Mod1 of
  	    {value, ModName} ->
  		case refac_syntax:type(Args) of
  		  list ->
  		      case refac_syntax:list_length(Args) of
  			Arity ->
  			    Mod2 = refac_syntax:atom(TargetModName),
  			    App = if length(Arguments) == 4 ->
  					 refac_syntax:application(Operator, [N, Mod2, Fun, Args]);
  				     true -> refac_syntax:application(Operator, [Mod2, Fun, Args])
  				  end,
  			    {refac_syntax:copy_pos(Node, refac_syntax:copy_attrs(Node, App)), true};
  			_ -> {Node, false}
  		      end;
  		  nil -> if Arity == 0 ->
  				Mod2 = refac_syntax:atom(TargetModName),
  				App = if length(Arguments) == 4 ->
  					     refac_syntax:application(Operator, [N, Mod2, Fun, Args]);
  					 true -> refac_syntax:application(Operator, [Mod2, Fun, Args])
  				      end,
  				{refac_syntax:copy_pos(Node, refac_syntax:copy_attrs(Node, App)), true};
  			    true -> {Node, false}
  			 end;
  		  _ -> Message(Pos),
  		       {Node, false}
  		end;
  	    {value, _} -> {Node, false};
  	    {error, _Reason} ->
  		case refac_syntax:type(Args) of
  		  list -> case refac_syntax:list_length(Args) of
  			    Arity -> Message(Pos),
  				     {Node, false};
  			    _ -> {Node, false}
  			  end;
  		  _ -> Message(Pos),
  		       {Node, false}
  		end
  	  end;
        {value, _} -> {Node, false};
        {error, _Reason} ->
  	  case Mod1 of
  	    {value, ModName} ->
  		case refac_syntax:type(Args) of
  		  list -> case refac_syntax:list_length(Args) of
  			    Arity -> Message(Pos),
  				     {Node, false};
  			    _ -> {Node, false}
  			  end;
  		  _ -> Message(Pos),
  		       {Node, false}
  		end;
  	    {value, _} -> {Node, false};
  	    {error, _Reason} -> case refac_syntax:type(Args) of
  				  list -> case refac_syntax:list_length(Args) of
  					    Arity -> Message(Pos),
  						     {Node, false};
  					    _ -> {Node, false}
  					  end;
  				  _ -> Message(Pos),
  				       {Node, false}
  				end
  	  end
      end.
  
  get_fun_def_info(Node) ->
      As = refac_syntax:get_ann(Node),
      case lists:keysearch(fun_def, 1, As) of
        {value,
         {fun_def, {Mod, FunName, Arity, _UsePos, DefinePos}}} ->
  	  {Mod, FunName, Arity, DefinePos};
        _ -> false
      end.
  
  
  is_mod_name(A) ->
      refac_util:is_fun_name(A).
  
  
  comment(Txt) ->
      comment(Txt, ?COMMENT_PREFIX).
  
  comment(Txt, Prefix) ->
      erl_syntax:comment(prefix_lines(split_lines(Txt), Prefix)).
  
  prefix_lines([L | Ls], Prefix) ->
      [Prefix ++ L | prefix_lines(Ls, Prefix)];
  prefix_lines([], _) ->
      [].
  
  split_lines(Ls) ->
      split_lines(Ls, []).
  
  split_lines([L | Ls], Ls1) ->
      split_lines(Ls, split_lines(L, [], Ls1));
  split_lines([], Ls1) ->
      lists:reverse(Ls1).
  
  %% split_lines([$\r, $\n | Cs], Cs1, Ls) ->
  %%     split_lines_1(Cs, Cs1, Ls);
  %% split_lines([$\r | Cs], Cs1, Ls) ->
  %%     split_lines_1(Cs, Cs1, Ls);
  %% split_lines([$\n | Cs], Cs1, Ls) ->
  %%     split_lines_1(Cs, Cs1, Ls);
  %% split_lines([C | Cs], Cs1, Ls) ->
  %%     split_lines(Cs, [C | Cs1], Ls);
  split_lines([], Cs, Ls) ->
      [lists:reverse(Cs) | Ls].
  
  %% split_lines_1(Cs, Cs1, Ls) ->
  %%     split_lines(Cs, [], [lists:reverse(Cs1) | Ls]).
  
negative.code:     FunDef21=reset_attrs(FunDef2, {M2, F2, A2}),
      FunDef11 == FunDef21.
  
  
  check_macros_records(FileName, TargetFileName, FunDef, SearchPaths, TabWidth) ->
      UsedMacros = collect_used_macros(FunDef),
      UsedRecords = collect_used_records(FunDef),
      case (UsedMacros == []) andalso (UsedRecords == []) of
        true -> true;
        _ ->
  	  Dir = filename:dirname(FileName),
  	  DefaultIncl1 = [".", "..", "../hrl", "../incl", "../inc", "../include"],
  	  DefaultIncl2 = [filename:join(Dir, X) || X <- DefaultIncl1],
  	  NewSearchPaths = SearchPaths ++ DefaultIncl2,
  	  case refac_epp:parse_file(FileName, NewSearchPaths, [], TabWidth, refac_util:file_format(FileName)) of
  	    {ok, AST, {MDefs, _MUses}} ->
  		  UsedMacroDefs = [{Name, {Args, refac_util:concat_toks(Toks)}}
  				   || {{_, Name}, {Args, Toks}} <- MDefs,
  				      lists:member(Name, UsedMacros)],
  		  UsedRecordDefs = case UsedRecords of
  				   [] -> [];
  				   _ ->
  				       Info = get_mod_info_from_parse_tree(AST),
  				       case lists:keysearch(records, 1, Info) of
  					 {value, {records, RecordDefs}} ->
  					     [{Name, lists:keysort(1, [{F, format(FDef)} || {F, FDef} <- Fields])}
  					      || {Name, Fields} <- RecordDefs, lists:member(Name, UsedRecords)];
  					 _ -> []
  				       end
  				 end,
  		case length(UsedMacros) > length(UsedMacroDefs) of
  		  true -> UnDefinedUsedMacros = UsedMacros -- [Name || {Name, _Def} <- UsedMacroDefs],
  			  ?wrangler_io("\nThe following macros are used by the function selected, but not defined.\n~p\n", [UnDefinedUsedMacros]),
  			  throw({error, "Some macros used by the function selected are not defined."});
  		   false -> ok
  		end,
  		case length(UsedRecords) > length(UsedRecordDefs) of
  		  true ->
  		      UnDefinedUsedRecords = UsedRecords -- [Name || {Name, _Fields} <- UsedRecordDefs],
  		      ?wrangler_io("\nThe following records are used by the function selected, but not defined.\n~p\n", [UnDefinedUsedRecords]),
  		      throw({error, "Some records used by the function selected are not defined."});
  		  false -> ok
  		end,
  		case refac_epp:parse_file(TargetFileName, NewSearchPaths, [], TabWidth, refac_util:file_format(TargetFileName)) of
  		    {ok, TargetAST, {MDefs1, _MUses1}} ->
  			UsedMacroDefsInTargetFile = [{Name, {Args, refac_util:concat_toks(Toks)}}
  						     || {{_, Name}, {Args, Toks}} <- MDefs1, lists:member(Name, UsedMacros)],
  			case length(UsedMacros) > length(UsedMacroDefsInTargetFile) of
  			    false ->
  				case length(UsedMacros) == length(UsedMacroDefsInTargetFile) of
  				    true ->
  					case lists:keysort(1, UsedMacroDefs) == lists:keysort(1, UsedMacroDefsInTargetFile) of
  					    true -> ok;
  					    _ ->
  						?wrangler_io("\nThe following macros used by the function selected "
  							     "are defined differently in the target module:\n~p\n",
  						[element(1, lists:unzip(lists:keysort(1, UsedMacroDefs) -- lists:keysort(1, UsedMacroDefsInTargetFile)))]),
  						throw({error, "Some records used by the function selected are defined differently in the target module."})
  					end;
  				    _ -> ok
  				end;
  			    true ->
  			    ?wrangler_io("\nThe following macros used by the function selected "
  					 "are not defined in the target module.\n~p\n", [UsedMacros -- UsedMacroDefsInTargetFile]),
  			    throw({error, "Some macros used by the function selected are not defined in the target module."})
  		      end,
  		      TargetModInfo = get_mod_info_from_parse_tree(TargetAST),
  		      UsedRecordDefsInTargetFile = case lists:keysearch(records, 1, TargetModInfo) of
  						     {value, {records, RecordDefsInTargetFile}} ->
  							 [{Name, lists:keysort(1, [{F, format(FDef)} || {F, FDef} <- Fields])}
  							  || {Name, Fields} <- RecordDefsInTargetFile,
  							     lists:member(Name, UsedRecords)];
  						     _ -> []
  						   end,
  		      case length(UsedRecords) > length(UsedRecordDefsInTargetFile) of
  			true ->
  			      ?wrangler_io("\nThe following records used by the function selected "
  					 "are not defined in the target module.\n~p\n", [UsedRecords -- element(1, lists:unzip(UsedRecordDefsInTargetFile))]),
  			      throw({error, "Some records used by the function selected are not defined in the target file."});
  			  _ -> case lists:keysort(1, UsedRecordDefs) == lists:keysort(1, UsedRecordDefsInTargetFile) of
  				   true -> true;
  				   _ ->
  				       ?wrangler_io("\nThe following records used by the function selected "
  						"are defined differently in the target module:\n~p\n",
  						[element(1, lists:unzip(lists:keysort(1, UsedRecordDefs) -- lists:keysort(1, UsedRecordDefsInTargetFile)))]),
  				   throw({error, "Some records used by the function selected are defined differently in the target module."})
  			       end
  		      end;
  		    _ -> throw({error, "Refactoring failed because Wrangler could not parse the target module."})
  		end;
  	      _ -> throw({error, "Refactoring failed because Wrangler could not parse the current module."})
  	  end
      end.
  
  get_mod_info_from_parse_tree(AST) ->
      AST1 = lists:filter(fun (F) ->
  				case F of
  				    {attribute, _, file, _} -> false;
  				    {attribute, _, type, {{record, _}, _, _}} -> false;
  				    _ -> true
  				end
  			end, AST),
      SyntaxTree = refac_recomment:recomment_forms(AST1, []),
      Info=refac_syntax_lib:analyze_forms(SyntaxTree),
      Info.
      
  		    
  collect_used_macros(FunDef) ->
      F = fun(T, S) ->
  		case refac_syntax:type(T) of
  