anchor.diff:      ?wrangler_io("\nCMD: ~p:rename_mod(~p, ~p,~p, ~p).\n", [?MODULE, FileName, NewName, SearchPaths, TabWidth]),
       case refac_util:is_fun_name(NewName) of   %% module name and function name follow the same rules.
         true ->
  -          {ok, {AnnAST, Info}}= refac_util:parse_annotate_file(FileName,true, SearchPaths, TabWidth),
  -	    case get_module_name(Info) of 
  -		{ok, OldModName} ->
  +	    {ok, {AnnAST, Info}}= refac_util:parse_annotate_file(FileName,true, SearchPaths, TabWidth),
  +	    case lists:keysearch(module, 1, Info) of 
  +		{value, {module, OldModName}} ->
   		    NewModName = list_to_atom(NewName),
  -		    case (NewModName =/= OldModName) of
  -			true -> 
  -			    NewFileName = filename:dirname(FileName)++"/"++NewName++".erl",
  -			    case filename_exists(NewFileName, SearchPaths) of 
  -				false -> 
  -				    ?wrangler_io("The current file under refactoring is:\n~p\n",[FileName]), 
  -				    AnnAST1 = rename_mod_1(AnnAST, OldModName, NewModName),
  -				    check_atoms(AnnAST1, OldModName),
  -				    ?wrangler_io("\nChecking client modules in the following search paths: \n~p\n",[SearchPaths]),
  -				    ClientFiles = refac_util:get_client_files(FileName, SearchPaths),
  -				    Results = rename_mod_in_client_modules(ClientFiles, 
  -									   OldModName, NewModName,SearchPaths, TabWidth),
  -				    case Editor of 
  -					emacs ->
  -					    refac_util:write_refactored_files_for_preview([{{FileName, NewFileName}, AnnAST1}|Results]),
  -					    ChangedClientFiles = lists:map(fun({{F, _F}, _AST}) -> F end, Results),
  -					    ChangedFiles = [FileName | ChangedClientFiles],
  -					    ?wrangler_io
  -					       ("The following files are to be changed by this refactoring:\n~p\n",
  -						[ChangedFiles]),
  -					    {ok, ChangedFiles};
  -					eclipse ->
  -					    Results1 =[{{FileName, NewFileName}, AnnAST1}|Results],
  -					    Res = lists:map(fun({{FName, NewFName}, AST}) -> {FName, NewFName, 
  -											      refac_prettypr:print_ast(refac_util:file_format(FName),AST)} end, Results1),
  -					    {ok, Res}
  -				    end;
  -				true -> {error, "The new module/file name has been used!"}
  -			    end;
  -			_ ->
  -			    {error, "New module name is the same as the old name."}
  -		    end;
  -		{error, Reason} -> {error, Reason}
  +		    TestFrameWorkUsed = refac_util:test_framework_used(FileName),
  +		    case pre_cond_check(FileName, OldModName, NewModName, TestFrameWorkUsed) of 
  +			ok -> do_rename_mod(FileName, [{OldModName, NewModName}], AnnAST, SearchPaths, Editor, TabWidth);
  +			Other -> Other
  +		    end;
  +		false -> {error, "Wrangler could not infer the current module name."}
   	    end;
   	false -> {error, "Invalid new module name!"}
       end.
   
  -filename_exists(NewFileName, SearchPaths) ->
  -    Files = refac_util:expand_files(SearchPaths, ".erl"),
  -    lists:member(NewFileName, Files).
  -    
  -get_module_name(ModInfo) ->				      
  -    case lists:keysearch(module, 1, ModInfo) of
  -	{value, {module, ModName}} -> {ok, ModName};
  +
  +pre_cond_check(FileName, OldModName, NewModName, TestFrameWorkUsed) ->
  +    case NewModName =/= OldModName of 
  +	true ->
  +	    NewFileName = filename:dirname(FileName)++"/"++atom_to_list(NewModName)++".erl",
  +	    %% Here we assume the application file and test file are in the same dir; maybe to restrictive?
  +	    EunitTestFileName= filename:dirname(FileName)++"/"++atom_to_list(OldModName)++"_tests.erl",  
  +	    case filelib:is_file(NewFileName) of
  +		false -> 
  +		    TestFrameWorkUsed = refac_util:test_framework_used(FileName),
  +		    case TestFrameWorkUsed of 
  +			[] -> ok;
  +			_ -> case (catch test_framework_aware_name_checking(TestFrameWorkUsed, OldModName, NewModName)) of 
  +				 ok ->
  +				     case filelib:is_file(EunitTestFileName) of 
  +					 true -> case lists:member(eunit,refac_util:test_framework_used(EunitTestFileName)) of 
  +						     true ->
  +							 NewTestFileName = filename:dirname(FileName)++"/"++atom_to_list(NewModName)++"_tests.erl",
  +							 case filelib:is_file(NewTestFileName) of 
  +							     false ->
  +								{question, "Also rename the test module: "++ atom_to_list(OldModName)++"_tests.erl" ++ "?"};
  +							     true -> {warning, "This module has a test module, but Wrangler won't to rename the test module"
  +								      " because the new test module name is already in used, still continue?"}
  +							 end;
  +						     false -> ok
  +						 end;
  +					 _->  ok
  +				     end;
  +				 Others -> Others
  +			     end		    
  +		    end;
  +		true ->{error, "The new module/file name has been used!"}
  +	    end;
   	false ->
  -	    {error, "Can not get the current module name."}
  -    end.
  -	    
  -
  -%% =====================================================================
  -%% rename_fun(Tree:syntaxTree(), oldName::{atom(),atom(), integer()}, 
  -%%            {DefinePos,NewName}::{{integer(),integer{},atom()
  -%%   
  -rename_mod_1(Tree, OldModName, NewModName) ->
  -    {Tree1, _} =refac_util:stop_tdTP(fun do_rename_mod/2, Tree, {OldModName, NewModName}),
  -    Tree1.
  -
  -do_rename_mod(Tree, {OldModName, NewModName}) ->
  +	    {error, "New module name is the same as the old name."}
  +    end.
  +
  +
  +do_rename_mod(FileName, OldNewModPairs, AnnAST, SearchPaths,Editor, TabWidth) ->
  +    {OldModName, NewModName} = hd(OldNewModPairs),
  +    NewFileName = filename:dirname(FileName)++"/"++ atom_to_list(NewModName)++".erl",
  +    TestFileName = filename:dirname(FileName)++"/"++ atom_to_list(OldModName)++"_tests.erl",
  +    NewTestFileName = filename:dirname(FileName)++"/"++atom_to_list(NewModName)++"_tests.erl",
  +    Pid = start_atom_process(),
  +    ?wrangler_io("The current file under refactoring is:\n~p\n",[FileName]), 
  +    {AnnAST1, _C1}=do_rename_mod_1(AnnAST, {FileName,OldNewModPairs,Pid}),
  +    OldModNames = element(1, lists:unzip(OldNewModPairs)),
  +    check_atoms(FileName, AnnAST1, OldModNames, Pid),
  +    TestModRes = case length(OldNewModPairs) of 
  +	      2 ->  ?wrangler_io("The current file under refactoring is:\n~p\n",[TestFileName]), 
  +		    {ok, {TestAnnAST, _Info}}= refac_util:parse_annotate_file(TestFileName,true, SearchPaths, TabWidth),
  +		    {TestAnnAST1, _C2} =do_rename_mod_1(TestAnnAST, {FileName,OldNewModPairs, Pid}), 
  +		    check_atoms(FileName,TestAnnAST1, OldModNames, Pid),
  +		    [{{TestFileName, NewTestFileName}, TestAnnAST1}];
  +	      _ -> []
  +	  end,
  +
  +    ?wrangler_io("\nChecking client modules in the following search paths: \n~p\n",[SearchPaths]),
  +    ClientFiles1 = refac_util:get_client_files(FileName, SearchPaths),
  +    ClientFiles2 = case length(OldNewModPairs)==2 of 
  +		      true ->  refac_util:get_client_files(TestFileName, SearchPaths);
  +		      _ -> []
  +		  end,
  +    ClientFiles = ClientFiles1++ClientFiles2 -- [FileName, TestFileName],
  +    Results = rename_mod_in_client_modules(ClientFiles, OldNewModPairs,SearchPaths, TabWidth, Pid),
  +    case Editor of 
  +	emacs ->
  +	    output_atom_warning_msg(Pid, OldModNames),
  +	    stop_atom_process(Pid),
  +	    refac_util:write_refactored_files_for_preview([{{FileName, NewFileName}, AnnAST1}|(TestModRes++Results)]),
  +	    ChangedClientFiles = lists:map(fun({{F, _F}, _AST}) -> F end, Results),
  +	    ChangedFiles = case length(OldNewModPairs) of 
  +			       2 ->[FileName, TestFileName| ChangedClientFiles];
  +			       1 ->[FileName|ChangedClientFiles]
  +			   end,
  +	    ?wrangler_io("The following files are to be changed by this refactoring:\n~p\n",[ChangedFiles]),
  +	    {ok, ChangedFiles};
  +	eclipse ->
  +	    Results1 =[{{FileName, NewFileName}, AnnAST1}|(TestModRes++Results)],
  +	    Res = lists:map(fun({{FName, NewFName}, AST}) -> {FName, NewFName, 
  +			      refac_prettypr:print_ast(refac_util:file_format(FName),AST)} end, Results1),
  +	    {ok, Res}
  +    end.
  +  
  + 
  +-spec(rename_mod_1/5::(filename(), string(), [dir()], integer(),boolean()) ->{ok, [filename()]}).
  +rename_mod_1(FileName, NewName, SearchPaths, TabWidth, RenameTestMod) ->     %% currently only for Emacs; need to extend to Eclipse.
  +    {ok, {AnnAST, Info}}= refac_util:parse_annotate_file(FileName,true, SearchPaths),
  +    {value, {module, OldModName}} = lists:keysearch(module, 1, Info), 
  +    NewModName = list_to_atom(NewName),
  +    TestFrameWorkUsed = refac_util:test_framework_used(FileName), 
  +    case lists:member(eunit, TestFrameWorkUsed) andalso RenameTestMod of 
  +	true ->
  +	    TestModName = list_to_atom(atom_to_list(OldModName) ++ "_tests"),
  +	    NewTestModName = list_to_atom(NewName++"_tests"),
  +	    do_rename_mod(FileName, [{OldModName, NewModName}, {TestModName, NewTestModName}], 
  +			  AnnAST, SearchPaths, emacs, TabWidth);
  +	false ->
  +	    do_rename_mod(FileName, [{OldModName, NewModName}], AnnAST, SearchPaths, emacs, TabWidth)
  +    end.
  +    
  +do_rename_mod_1(Tree, {FileName, OldNewModPairs,Pid}) ->
  +    {AnnAST1, C1}=refac_util:full_tdTP(fun do_rename_mod_2/2, Tree, {FileName,OldNewModPairs, Pid}),
  +    {AnnAST2, C2} = case lists:member(eunit, refac_util:test_framework_used(FileName)) of 
  +			true ->do_rename_mod_in_eunit_funs(FileName, AnnAST1, OldNewModPairs,Pid);
  +			_ -> {AnnAST1, C1}
  +		    end,
  +    {AnnAST2, C1 or C2}.
  +
  +do_rename_mod_2(Tree, {FileName,OldNewModPairs, Pid}) ->
       case refac_syntax:type(Tree) of
          attribute -> 
   	    AttrName = refac_syntax:attribute_name(Tree),
   	    case refac_syntax:atom_value(AttrName) of 
   	       module -> Args = refac_syntax:attribute_arguments(Tree),
  -			 F = fun (Arg) -> case refac_syntax:atom_value(Arg) of 
  -					      OldModName -> refac_syntax:copy_attrs(Arg, refac_syntax:atom(NewModName));
  +			 F = fun (Arg) -> case lists:keysearch(refac_syntax:atom_value(Arg),1, OldNewModPairs) of 
  +					      {value, {_OldModName, NewModName}} -> 
  +						  copy_pos_attrs(Arg, refac_syntax:atom(NewModName));
   					      _ -> Arg
   					  end
   			     end,
   			 Args1 = lists:map(F, Args),
  -			 Tree1 = refac_syntax:copy_attrs(Tree, refac_syntax:attribute(AttrName, Args1)),
  -			 {Tree1, true};
  +			 Tree1 = copy_pos_attrs(Tree, refac_syntax:attribute(AttrName, Args1)),
  +			 {Tree1, Tree=/=Tree1};
   		import -> Args = refac_syntax:attribute_arguments(Tree),
   			  case Args of 
  -			      [H|T] -> case refac_syntax:atom_value(H) of 
  -					   OldModName -> 
  -					       H1 = refac_syntax:copy_attrs(H, refac_syntax:atom(NewModName)),
  -					       Tree1 = refac_syntax:copy_attrs(Tree, 
  -									       refac_syntax:attribute(AttrName, [H1|T])),
  +			      [H|T] -> case lists:keysearch(refac_syntax:atom_value(H),1, OldNewModPairs) of 
  +					   {value, {_OldModName, NewModName}} ->
  +					       H1 = copy_pos_attrs(H, refac_syntax:atom(NewModName)),
  +					       Tree1 = copy_pos_attrs(Tree,refac_syntax:attribute(AttrName, [H1|T])),
   					       {Tree1, true};
   					   _ -> {Tree, false}
   				       end;
  
positive.code: 		_ -> {Tree, false}
  	    end;
         application ->
  	    case application_info(Tree) of 
  		{{_, apply},3} -> transform_apply_call(Tree, OldModName, NewModName);
  		{{_, spawn}, 3} ->transform_spawn_call(Tree, OldModName, NewModName);
  		{{_, spawn}, 4} -> transform_spawn_call(Tree, OldModName, NewModName); 
  		{{_, spawn_link}, 3} ->transform_spawn_call(Tree, OldModName, NewModName);
  		{{_, spawn_link}, 4} ->transform_spawn_call(Tree, OldModName, NewModName);
  		_ -> {Tree, false}
  	    end;
  	module_qualifier ->
  	    Mod = refac_syntax:module_qualifier_argument(Tree),
  	    Fun = refac_syntax:module_qualifier_body(Tree),
  	    case refac_syntax:type(Mod) of 
  		atom ->  case refac_syntax:atom_value(Mod) of 
  			     OldModName->
  				 Mod1 = refac_syntax:copy_attrs(Mod, refac_syntax:atom(NewModName)),
  				 Tree1= refac_syntax:copy_attrs(Tree, refac_syntax:module_qualifier(Mod1, Fun)),
  				 {Tree1, true};
  			     _ -> {Tree, false}
  			 end;
  		_ -> {Tree, false}
  	    end;
  	_ -> {Tree, false}
      end.
  rename_mod_in_client_modules(Files, OldModName, NewModName, SearchPaths, TabWidth) ->
      case Files 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),
  		   {AnnAST1, Changed} = rename_mod_in_client_module_1(AnnAST, OldModName, NewModName),
  		   check_atoms(AnnAST1, OldModName),
  		   if Changed ->
  			   [{{F,F}, AnnAST1}|rename_mod_in_client_modules(Fs, OldModName, NewModName, SearchPaths, TabWidth)];
  		      true -> rename_mod_in_client_modules(Fs, OldModName, NewModName, SearchPaths, TabWidth)
  		   end
  	end.    
  
  rename_mod_in_client_module_1(Tree, OldModName, NewModName) ->
      refac_util:stop_tdTP(fun do_rename_mod/2, Tree, {OldModName, NewModName}).
  
  
  transform_apply_call(Node,OldModName, NewModName) ->
      Operator = refac_syntax:application_operator(Node),
      Arguments = refac_syntax:application_arguments(Node),
      case Arguments of 
  	[Mod, Fun, Args] ->
  	    Mod1 = refac_util:try_evaluation([refac_syntax:revert(Mod)]),
  	    case Mod1 of 
  		{value,OldModName} ->
  		    Mod2 = refac_syntax:atom(NewModName),
  		    {refac_syntax:copy_pos(Node,(refac_syntax:copy_attrs(Node, 
  						refac_syntax:application(Operator, [Mod2, Fun, Args])))), false};
  		_ -> {Node, false}
  	    end;
  	_ -> {Node, false}
      end.
   	     
  transform_spawn_call(Node,OldModName, NewModName) ->
      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)]),
      case Mod1 of 
  	{value,OldModName} ->
  	    Mod2 = refac_syntax:atom(NewModName),
  	    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))), false};
  	_ -> {Node, false}
      end.
  
  
  %% =====================================================================
  %% @spec application_info(Tree::syntaxTree())->term()
  %% ====================================================================       
  application_info(Node) ->
      case refac_syntax:type(Node) of 
  	application ->
  	    Operator = refac_syntax:application_operator(Node),
  	    Arguments = refac_syntax:application_arguments(Node),
  	    Arity = length(Arguments),
  	    case refac_syntax:type(Operator) of 
  		atom -> Op = refac_syntax:atom_value(Operator),
  			{{none,Op}, Arity}; 
  		module_qualifier ->
  		        Mod = refac_syntax:module_qualifier_argument(Operator),
  		        Fun = refac_syntax:module_qualifier_body(Operator),
  		        T1 = refac_syntax:type(Mod), 
  		        T2 = refac_syntax:type(Fun),
  		        case T1 of 
  		  	    atom -> 
  				Mod1 = refac_syntax:atom_value(Mod),
  				case T2 of 
  					atom -> Fun1 = refac_syntax:atom_value(Fun),
  						{{Mod1, Fun1}, Arity};
  				        _ ->{{Mod1, expressionfunname}, Arity}
  					end;
  			    _ -> case T2 of 
  				     atom -> Fun1 = refac_syntax:atom_value(Fun),
  					     {{expressionmodname, Fun1}, Arity};
  				     _ -> {{expressionmodname,expressionfunname}, Arity}
  				 end
  			    end;
  		_  -> {{none,expressionoperator}, Arity}
  	    end;
  	_ -> erlang:error(bagarg)
      end.
  
  
  
  	
  
negative.code: 
  -module(refac_rename_mod).
  
  -export([rename_mod/4, rename_mod_1/5, rename_mod_eclipse/4]).
  
  -import(refac_rename_fun, [check_atoms/4, start_atom_process/0, 
  			   output_atom_warning_msg/2, stop_atom_process/1]).
  
  -include("../include/wrangler.hrl").
  %% =====================================================================
  