anchor.diff:      Pat = refac_syntax:variable(NewVarName),
       refac_syntax:match_expr(Pat, Expr).
   
  +
  +get_inmost_enclosing_clause(Form, Expr) ->
  +    ExprPos = refac_syntax:get_pos(Expr),
  +    Fun = fun (Node, S) ->
  +		  Type =  refac_syntax:type(Node),
  +		  case lists:member(Type, [clause, block_expr, try_expr]) of
  +		      true ->
  +			  Body = case Type of 
  +				     clause ->
  +					 refac_syntax:clause_body(Node);
  +				     block_expr ->
  +					 refac_syntax:block_expr_body(Node);
  +				     try_expr ->
  +					 refac_syntax:try_expr_body(Node)
  +				 end,
  +			  {Start, _End} = refac_misc:get_range(hd(Body)),
  +			  {_, End} = refac_misc:get_range(lists:last(Body)),
  +			  case Start =< ExprPos andalso ExprPos =< End of
  +			      true ->
  +				 [{Node, End}|S];
  +			      _ -> S
  +			  end;
  +		      _ ->
  +			  S
  +		  end
  +	  end,		 
  +    Res = lists:keysort(2, refac_syntax_lib:fold(Fun, [], Form)),
  +    case Res of
  +	[{Node, _}| _] ->
  +	  Node;
  +	_ -> throw({error, "Wrangler internal error."})
  +    end.
  +
  +get_inmost_enclosing_body_expr(Form, Expr) ->
  +    ExprPos = refac_syntax:get_pos(Expr),
  +    Fun = fun (Node, S) ->
  +		  Type =  refac_syntax:type(Node),
  +		  case lists:member(Type, [clause, block_expr, try_expr]) of
  +		      true ->
  +			  Body = case Type of 
  +				     clause ->
  +					 refac_syntax:clause_body(Node);
  +				     block_expr ->
  +					 refac_syntax:block_expr_body(Node);
  +				     try_expr ->
  +					 refac_syntax:try_expr_body(Node)
  +				 end,
  +			  {Start, _End} = refac_misc:get_range(hd(Body)),
  +			  {_, End} = refac_misc:get_range(lists:last(Body)),
  +			  case Start =< ExprPos andalso ExprPos =< End of
  +			      true ->
  +				  EnclosingExpr = get_enclosing_expr(Body, Expr),
  +				  [{Body, End, EnclosingExpr}| S];
  +			      _ -> S
  +			  end;
  +		      _ ->
  +			  S
  +		  end
  +	  end,
  +    Res = lists:keysort(2, refac_syntax_lib:fold(Fun, [], Form)),
  +    case Res of
  +	[{Body, _, E}| _] ->
  +	    {Body, E};
  +	_ -> 
  +	    throw({error, "Wrangler failed to perform this refactoring."})
  +    end.
  +
  +get_enclosing_expr(Body, Expr) ->
  +    ExprPos = refac_syntax:get_pos(Expr),
  +    Fun = fun(ExprStatement) ->
  +		  {Start, End} = refac_misc:get_start_end_loc(ExprStatement),
  +		  case Start =< ExprPos andalso
  +		      ExprPos =< End of 
  +		      true ->
  +			  [ExprStatement];
  +		      false ->
  +			  []
  +		  end
  +	  end,
  +    hd(lists:append([Fun(B)||B<-Body])).
  +
  +
  
positive.code: 			  Form
  		  end
  	  end,
      refac_syntax:form_list([Fun(Form)|| Form <- Forms]).
  
  do_intro_new_var_in_fun(Fun, Expr, NewVarName) ->
      EnclosingExpr =get_enclosing_body(Fun, Expr),
      {NewFun1,_} = ast_traverse_api:stop_tdTP(
  		    fun do_insert_and_replace/2, Fun, {EnclosingExpr, Expr, NewVarName}),
      NewFun1.
  
  do_insert_and_replace(Node, {ExprList, Expr, NewVarName}) ->
      case Node of 
  	ExprList ->
  	    {insert_new_expr(Node, Expr, NewVarName), true};
  	_ -> {Node, false}
      end.
  
  
  	    
  insert_new_expr(Node, Expr,NewVarName) ->
      MatchExpr = make_match_expr(Expr, NewVarName),
      ExprPos = refac_syntax:get_pos(Expr),
      Body = case refac_syntax:type(Node) of 
  	       clause ->
  		   refac_syntax:clause_body(Node);
  	       block_expr ->
  		   refac_syntax:block_expr_body(Node);
  	       try_expr ->
  		   refac_syntax:try_expr_body(Node)
  	   end,
      Fun = fun(ExprStatement) ->
  		  {Start, End} = refac_misc:get_start_end_loc(ExprStatement),
  		  case Start =< ExprPos andalso
  		      ExprPos =< End of 
  		      true ->
  			  {ExprStatement1,_} =ast_traverse_api:stop_tdTP(
  					       fun do_replace_expr_with_var/2, 
  					       ExprStatement, {Expr, NewVarName}),
  			  [MatchExpr, ExprStatement1];
  		      false ->
  			  [ExprStatement]
  		  end
  	  end,
      NewBody=[E1||E<-Body, E1<-Fun(E)],
      case refac_syntax:type(Node) of 
  	clause ->
  	    Pat = refac_syntax:clause_patterns(Node),
  	    Guard = refac_syntax:clause_guard(Node),
  	    refac_syntax:clause(Pat, Guard, NewBody);
  	block_expr ->
  	    refac_syntax:block_expr(NewBody);
  	try_expr ->
  	    C = refac_syntax:try_expr_clauses(Node),
  	    H = refac_syntax:try_expr_handlers(Node),
  	    A = refac_syntax:try_expr_after(Node),
  	    refac_syntax:try_expr(NewBody, C, H, A)	      
      end.
  
  %% get_enclosing_body_and_expr(Form, Expr) ->	 
  %%       ExprPos = refac_syntax:get_pos(Expr),
  %%     Fun = fun(Node, S) ->
  %% 		  case refac_syntax:type(Node) of
  %% 		      clause ->
  %% 			  Body = refac_syntax:clause_body(Node),
  %% 			  {Start,_End}= refac_misc:get_range(hd(Body)),	
  %% 			  {_, End} = refac_misc:get_range(lists:last(Body)),
  %% 			  case Start =< ExprPos  andalso 
  %% 			      ExprPos =< End of
  %% 			      true ->
  %% 				  [{Node, End}|S];
  %% 			      _ -> S
  %% 			  end;
  %% 		      _ ->
  %% 			  S
  %% 		  end
  %% 	  end,
  %%     Res =lists:keysort(2, refac_syntax_lib:fold(Fun, [], Form)),
  %%     case Res of
  %% 	[{Node,_}|_] ->
  %% 	    Node;
  %% 	_ -> throw({error, "Wrangler internal error."})
  %%     end.
  
  get_enclosing_body(Form, Expr) ->
      ExprPos = refac_syntax:get_pos(Expr),
      Fun = fun(Node, S) ->
  		  case refac_syntax:type(Node) of
  		      clause ->
  			  Body = refac_syntax:clause_body(Node),
  			  {Start,_End}= refac_misc:get_range(hd(Body)),	
  			  {_, End} = refac_misc:get_range(lists:last(Body)),
  			  case Start =< ExprPos  andalso 
  			      ExprPos =< End of
  			      true ->
  				  [{Node, End}|S];
  			      _ -> S
  			  end;
  		      _ ->
  			  S
  		  end
  	  end,
      Res =lists:keysort(2, refac_syntax_lib:fold(Fun, [], Form)),
      case Res of
  	[{Node,_}|_] ->
  	    Node;
  	_ -> throw({error, "Wrangler internal error."})
      end.
  
  do_replace_expr_with_var(Node, {Expr, NewVarName}) ->
      case Node of 
  
negative.code: %% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  %% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  %% ============================================================================================
  %% Refactoring: Introduce a new variable to represent an expression selected.
  %%
  %% Author contact: hl@kent.ac.uk, sjt@kent.ac.uk
  %% 
  