anchor.diff:  %% Replace an expression/expression sequence with a function call/match expression whose right-hand side is the function call.
   %% ==========================================================================================================================
    
  -
   do_replace_expr_with_fun_call(Tree, {Expr, {Range, NewExp}})->
       case length(Expr) of 
   	1 -> do_replace_expr_with_fun_call_1(Tree, {Range, NewExp});
   	_  -> do_replace_expr_with_fun_call_2(Tree, {Range, NewExp})
       end.
  -						   
  +
   
   
   do_replace_expr_with_fun_call_1(Tree, {Range, NewExp}) ->
       case refac_util:get_range(Tree) of 
  - 	Range ->
  -	     {NewExp, true};
  + 	Range -> {NewExp, true};
    	_  -> {Tree, false}
  -     end.
  - 	
  +    end.
  +
   do_replace_expr_with_fun_call_2(Tree, {{StartLoc, EndLoc}, NewExp}) ->
  -      case refac_syntax:type(Tree) of
  +    Fun=fun(Exprs) ->
  +		{Exprs1, Exprs2} = lists:splitwith(
  +				     fun(E) -> 
  +					     element(1, refac_util:get_range(E)) =/= StartLoc
  +				     end, Exprs),
  +		case Exprs2 of
  +		    [] -> {Exprs, false};
  +		    _ -> {_Exprs21, Exprs22} = 
  +			     lists:splitwith(
  +			       fun(E) -> 
  +				       element(2, refac_util:get_range(E)) =/= EndLoc
  +			       end, Exprs),
  +			 case Exprs22 of
  +			     [] -> {Exprs, false};  %% THIS SHOULD NOT HAPPEN.
  +			     _ -> {Exprs1 ++ [NewExp| tl(Exprs22)], true}
  +			 end
  +		end
  +	end,
  +    case refac_syntax:type(Tree) of
   	clause ->
   	    Exprs = refac_syntax:clause_body(Tree),
  -	    {Exprs1, Exprs2} = lists:splitwith(fun(E) -> element(1,refac_util:get_range(E)) =/= StartLoc end, Exprs),
  -	    {NewBody, Modified} = case Exprs2 of 
  -				      [] -> {Exprs, false};
  -				      _ -> {_Exprs21, Exprs22} = lists:splitwith(fun(E) -> element(2, refac_util:get_range(E)) =/= EndLoc end, Exprs),
  -					   case Exprs22 of 
  -					       [] -> {Exprs, false};  %% THIS SHOULD NOT HAPPEN.
  -					       _ -> {Exprs1++[NewExp|tl(Exprs22)], true}
  -					   end
  -				  end,
  +	    {NewBody, Modified} = Fun(Exprs),
   	    Pats = refac_syntax:clause_patterns(Tree),
   	    G = refac_syntax:clause_guard(Tree),
  -	    {refac_syntax:copy_pos(Tree, refac_syntax:copy_attrs(Tree, refac_syntax:clause(Pats, G, NewBody))), Modified};
  +	    {refac_util:rewrite(Tree, refac_syntax:clause(Pats, G, NewBody)), Modified};
   	block_expr ->
   	    Exprs = refac_syntax:block_expr_body(Tree),
  -	    {Exprs1, Exprs2} = lists:splitwith(fun(E) -> element(1,refac_util:get_range(E)) =/= StartLoc end, Exprs),
  -	    {NewBody, Modified} = case Exprs2 of 
  -				      [] -> {Exprs, false};
  -				      _ -> {_Exprs21, Exprs22} = lists:splitwith(fun(E) -> element(2, refac_util:get_range(E)) =/= EndLoc end, Exprs),
  -					   case Exprs22 of 
  -					       [] -> {Exprs, false};  %% THIS SHOULD NOT HAPPEN.
  -					       _ -> {Exprs1++[NewExp|tl(Exprs22)], true}
  -					   end
  -				  end,
  -	    {refac_syntax:copy_pos(Tree, refac_syntax:copy_attrs(Tree, refac_syntax:block_expr(NewBody))), Modified};	 
  -	_  -> {Tree, false}
  -    end.
  -    
  +	    {NewBody, Modified} = Fun(Exprs),
  +	    {refac_util:rewrite(Tree,refac_syntax:block_expr(NewBody)), Modified};
  +	try_expr ->
  +	    Exprs = refac_syntax:try_expr_body(Tree),
  +	    {NewBody, Modified} = Fun(Exprs),
  +	    Cs = refac_syntax:try_expr_clauses(Tree),
  +	    Handlers = refac_syntax:try_expr_handlers(Tree),
  +	    After = refac_syntax:try_expr_after(Tree),
  +	    Tree1 = refac_util:rewrite(Tree, refac_syntax:try_expr(NewBody, Cs, Handlers, After)),
  +	    {Tree1, Modified};
  +	_ -> {Tree, false}
  +    end.
   
   %% =============================================================================================
   %% Search expression/expression sequence with are instances of of the selected function clause.
  
positive.code: do_search_candidate_exprs_2(AnnAST, ExpList) ->
      Len = length(ExpList),
      LastExp = lists:last(ExpList),
      HasExportExp = case refac_syntax:type(LastExp) of 
  		      variable -> true;
  		       tuple -> lists:all(fun(E) -> refac_syntax:type(E) == variable end, 
  					  refac_syntax:tuple_elements(LastExp));
  		       _  -> false
  		   end,
      
      Fun = fun(T, S) ->
  		  case refac_syntax:type(T) of 
  		      clause ->
  			  Exprs = refac_syntax:clause_body(T),
  			  SubExprs = sublists(Exprs, Len),
  			  CandidateExprs1 = 
  			      lists:map(fun(E) -> case ExpList =/= E of 
     						      true ->case expr_unification(ExpList, E) of 
     								 {true,Subst} -> {StartLoc1, _EndLoc1} = refac_util:get_range(hd(E)),
     										 {_StartLoc2, EndLoc2} = refac_util:get_range(lists:last(E)),
     										 {{StartLoc1, EndLoc2}, Subst};
     								 _ ->
  								     false
     							     end;
     						      _ -> false
     						  end							       
     					end, SubExprs),
   			  CandidateExprs2 = 
  			      case HasExportExp of 
  				  true ->
  				      SubExprs1 = sublists(Exprs, Len-1),
  				      lists:map(fun(E) -> case hd(ExpList) =/= hd(E) of 
  							      true -> VarsToExport = vars_to_export_1(Exprs, E),
  								      Res =  expr_unification(lists:sublist(ExpList, Len-1), E),
  								      case Res of 
  									  {true, Subst} ->
  									      case reorder_vars_to_export(LastExp, VarsToExport, Subst) of 
  										  {true, VarsToExport1} ->
  										      {StartLoc1, _EndLoc1} = refac_util:get_range(hd(E)),
  										      {_StartLoc2, EndLoc2} = refac_util:get_range(lists:last(E)),
  										      {{StartLoc1, EndLoc2}, Subst, VarsToExport1};
  										  _ -> false
  									      end;
  									  _ -> false
  								      end;
  							      _ -> false
  							  end
  						end, SubExprs1);
  				  _ -> []
  			      end,
  			  S ++ lists:filter(fun(C) -> C =/= false end, CandidateExprs1++CandidateExprs2);
  		      block_expr ->
  			  Exprs = refac_syntax:block_expr_body(T),
  			  SubExprs = sublists(Exprs, Len),
  			  CandidateExprs = lists:map(fun(E) -> case ExpList =/= E of 
  								   true ->case expr_unification(ExpList, E) of 
  									      {true, Subst} -> {StartLoc1, _EndLoc1} = refac_util:get_range(hd(E)),
  											       {_StartLoc2, EndLoc2} = refac_util:get_range(lists:last(E)),
  											       {{StartLoc1, EndLoc2}, Subst};
  									      _ -> false
  									  end;
  								   _ -> false
  							       end
  						     end, SubExprs),
  			  S ++ lists:filter(fun(C) -> C =/= false end, CandidateExprs);
  		      _  -> S
  		  end
  	  end,
       refac_syntax_lib:fold(Fun,[], AnnAST).
      
  
  -spec(expr_unification/2::(syntaxTree(), syntaxTree()) ->
  	     {true, [{atom(), syntaxTree()}]} | false).
  expr_unification(Exp1, Exp2) ->
      Res1 = case {is_list(Exp1), is_list(Exp2)} of 
  	       {true, true} ->   %% both are list of expressions
  		   case length(Exp1) == length(Exp2) of
  		       true -> Res = lists:map(fun({E1,E2}) ->			      
  						       expr_unification(E1,E2)						
  					       end, lists:zip(Exp1, Exp2)),
  			       Unifiable = lists:all(fun(E) -> case E of 
  								   {true, _} -> true;
  								   _ -> false
  							       end
  						     end, Res),
  			       Substs = lists:usort(lists:flatmap(fun(E) -> case E of 
  										{true,S} -> S;
  										_ -> []
  									    end
  								  end,Res)),
  			       case Unifiable of 
  				   true -> {true, Substs};
  				   _ -> false
  			       end;
  		       _ -> false
  		   end;
  	       {false, false} ->  %% both are single expressions.
  		   T1 = refac_syntax:type(Exp1),
  		   T2 = refac_syntax:type(Exp2),
  		   case T1 == T2 of 
  		       true -> 
  			   case T1 of 
  			       variable -> 
  				   case lists:keysearch(category, 1, refac_syntax:get_ann(Exp1)) of 
  				       {value, {category, macro_name}} ->
  					   case lists:keysearch(category, 1, refac_syntax:get_ann(Exp2)) of 
  					       {value, {category, macro_name}} ->
  						   case refac_syntax:variable_name(Exp1) == refac_syntax:variable_name(Exp2) of 
  						       true ->
  							   {true, []};
  					       _ ->false
  						   end;
  					       _ -> false
  					   end;
  				       _ -> {true, [{refac_syntax:variable_name(Exp1), set_default_ann(Exp2)}]} 
  				   end;
  			       atom -> case refac_syntax:atom_value(Exp1) == refac_syntax:atom_value(Exp2) of 
  					   true -> {true, []};
  					   _ -> false
  				       end;
  			       operator -> case refac_syntax:atom_value(Exp1) == refac_syntax:atom_value(Exp2) of
  					       true -> {true, []};
  					       _ -> false
  					   end;
  			       char -> case refac_syntax:char_value(Exp1) == refac_syntax:char_value(Exp2) of 
  					   true -> {true, []};
  					   _ -> false
  				       end;
  			       integer -> case refac_syntax:integer_value(Exp1) ==refac_syntax:integer_value(Exp2) of 
  					      true -> {true, []};
  					      _ -> false
  					  end;
  			       string -> case refac_syntax:string_value(Exp1) == refac_syntax:string_value(Exp2) of 
  					     true -> {true, []};
  					     _ -> false
  					 end;
  			       float -> case refac_syntax:float_value(Exp1) == refac_syntax:float_value(Exp2) of 
  					    true -> {true, []}
  					end;
  			       underscore -> {true, []};
  			       nil -> {true, []};
  			       _ -> 
  				   SubTrees1 = erl_syntax:subtrees(Exp1),
  				   SubTrees2 = erl_syntax:subtrees(Exp2),
  				   case length(SubTrees1) == length(SubTrees2) of 
  				       true -> 
  					   expr_unification(SubTrees1, SubTrees2);				    
  				       _ -> false
  				   end 
  			   end;
  		       _ -> case T1 of  
  				variable -> case variable_replaceable(Exp2) of 
  						false -> false;  
  						true -> 
  						    case lists:keysearch(category, 1, refac_syntax:get_ann(Exp2)) of 
  							{value, {category, application_op}} ->
  							    case lists:keysearch(fun_def, 1, refac_syntax:get_ann(Exp2)) of 
  								{value, {fun_def, {_M, _N, A, _P1, _P2}}} ->
  								    {true, [{refac_syntax:variable_name(Exp1), 
  									     set_default_ann(refac_syntax:implicit_fun(Exp2, refac_syntax:integer(A)))}]};
  								_ -> %% this is the function name part of a M:F. 
  								    {true, [{refac_syntax:variable_name(Exp1), set_default_ann(Exp2)}]}
  							    end;
  							_ -> {true, [{refac_syntax:variable_name(Exp1), set_default_ann(Exp2)}]}
  						    end
  					    end;
  				_ -> false
  			    end
  		   end;
  	       {true, false} -> %% Exp1 is a list, but Exp2 is not.
  		   false;
  	       {false, true} ->  %% Exp1 is a single expression, but Exp2 is not.
  		   false      %% an actual parameter cannot be a list of expressions.
  	   end,
      case Res1 of 
  	{true, Subst} ->
  	    Subst1 = lists:usort(lists:map(fun({E1,E2}) -> 
  						   {E1, refac_prettypr:format(E2)} end, Subst)),
  	    Len1 = length(lists:usort(lists:map(fun({E1,_E2}) -> E1 end, Subst1))),
  	    case Len1 == length(Subst1) of 
  		true -> {true, Subst};
  		_ -> false
  	    end;
  	_ -> false
      end.
  
  
  variable_replaceable(Exp) ->
  
negative.code: %% =============================================================================================   
  
  make_fun_call({FunDefMod, CurrentMod}, FunName, Pats, Subst) -> 
      Fun = fun(P) -> 
  		  case refac_syntax:type(P) of 
  		      variable -> 
  			  PName = refac_syntax:variable_name(P), 
  			  case lists:keysearch(PName, 1, Subst) of 
  			      {value, {PName, Par}} -> Par;
  			      _ -> refac_syntax:atom(undefined)
  			  end;
  		      underscore -> 
  			  refac_syntax:atom(undefined);
  		      _  -> P
  		  end
  	  end,
      Pars = lists:map(Fun, Pats),
      Op = case FunDefMod == CurrentMod of 
  	     true -> refac_syntax:atom(FunName);
  	     _ -> refac_syntax:module_qualifier(
  		    refac_syntax:atom(FunDefMod), refac_syntax:atom(FunName))
  	 end,
      refac_syntax:application(Op, Pars).
    
  
  make_match_expr({FunDefMod, CurrentMod}, FunName, Pats, Subst, VarsToExport) ->
      FunCall = make_fun_call({FunDefMod, CurrentMod},FunName, Pats, Subst),
  