anchor.diff:  -include("../include/wrangler.hrl").
   
   -define(DefaultSimiScore, 0.8).
  +
  +%%-define(DEBUG, true).
  +
  +-ifdef(DEBUG).
  +-define(debug(__String, __Args), ?wrangler_io(__String, __Args)).
  +-else.
  +-define(debug(__String, __Args), ok).
  +-endif.
  +
   %% ================================================================================================
   %% @doc Search expressions that are 'similar' to a user-selected expression or a sequence of expressions in the current buffer.
   %%
   %% <p> Given expression selected by the user, A say, expression B is similar to A if there exist a least general common abstation, C, 
  -%% such that, substitution C(Args1) = A, and C(Arg2) == B, and Size(C) /Size(A) > the threshold specified (0.6 by default).
  +%% such that, substitution C(Args1) = A, and C(Arg2) == B, and Size(C) /Size(A) > the threshold specified (0.8 by default).
   %% </p>
   %% <p> When the selected code contains multiple, but non-continuous sequence of, expressions, the first
   %% continuous sequence of expressions is taken as the user-selected expression. A continuous sequence of
   %% expressions is a sequence of expressions separated by ','.
   %% <p>
   
  --spec(sim_expr_search/6::(filename(), pos(), pos(), string(),[dir()],integer()) -> {ok, [{integer(), integer(), integer(), integer()}]} | {error, string()}).    
  +-spec(sim_expr_search/6::(filename(), pos(), pos(), string(),[dir()],integer()) 
  +      -> {ok, [{integer(), integer(), integer(), integer()}]} | {error, string()}).    
   sim_expr_search(FName, Start = {Line, Col}, End = {Line1, Col1}, SimiScore0, SearchPaths, TabWidth) ->
  -    ?wrangler_io("\nCMD: ~p:sim_expr_search(~p, {~p,~p},{~p,~p},~p, ~p, ~p).\n", [?MODULE, FName, Line, Col, Line1, Col1, SimiScore0, SearchPaths, TabWidth]),
  +    ?wrangler_io("\nCMD: ~p:sim_expr_search(~p, {~p,~p},{~p,~p},~p, ~p, ~p).\n", 
  +		 [?MODULE, FName, Line, Col, Line1, Col1, SimiScore0, SearchPaths, TabWidth]),
       SimiScore1 = case SimiScore0 of 
   		     [] -> ?DefaultSimiScore;
   		     _ -> list_to_float(SimiScore0)
  
positive.code: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  
  -spec(normalise_record_expr/4::(filename(), pos(), [dir()], integer()) -> {error, string()} | {ok, [filename()]}).
  normalise_record_expr(FName, Pos={Line, Col}, SearchPaths, TabWidth) ->
      ?wrangler_io("\nCMD: ~p:normalise_record_expr(~p, {~p,~p},~p, ~p).\n", [?MODULE, FName, Line, Col, SearchPaths,TabWidth]),
      {ok, {AnnAST, _Info}} =refac_util:parse_annotate_file(FName,true, [], TabWidth),
      RecordExpr =pos_to_record_expr(AnnAST, Pos),
      {AnnAST1, _Changed} = normalise_record_expr_1(FName, AnnAST,Pos, RecordExpr,  SearchPaths, TabWidth),
      refac_util:write_refactored_files_for_preview([{{FName, FName}, AnnAST1}]),
      {ok, [FName]}.
  
  
  normalise_record_expr_1(FName, AnnAST,Pos, RecordExpr, SearchPaths, TabWidth) ->
      RecordTypes1 = collect_record_types(RecordExpr),
      RecordTypes = lists:flatmap(fun(T) -> case refac_syntax:type(T) of 
  					      atom-> [refac_syntax:concrete(T)];
  					      _ -> []
  					  end
  				end, RecordTypes1),
      case length(RecordTypes) < length(RecordTypes1) of 
  	true -> ?wrangler_io("Warning: record expressions with a non-atom record type are not normalised by this refactoring.\n",[]);
  	_ -> ok
      end,
      case RecordTypes of 
  	[] -> throw({error, "No record expression to normalise."});
  	_ ->
  	    RecordInfo = get_module_record_info(FName, SearchPaths, TabWidth),
  	    refac_util:stop_tdTP(fun do_normalise_record_expr/2, AnnAST, {Pos,RecordInfo})
      end.
  
  do_normalise_record_expr(Node, {Pos, RecordInfo}) ->
      case refac_syntax:type(Node) of 
  	record_expr ->
  	    {S, E} = refac_util:get_range(Node), 
  	    case (S =<Pos) andalso (Pos =< E) of 
  		true ->  Node1 =refac_util:full_buTP(fun do_normalise_record_expr_1/2, Node, RecordInfo),
  			 {Node1, true};
  		_ -> {Node, false}
  	    end;
  	_ -> {Node, false}
      end.
  
  do_normalise_record_expr_1(Node, RecordInfo) ->
      Fun = fun({{FName, FVal}, Fields}) ->
  		  case lists:filter(fun(F1) -> T1 = refac_syntax:record_field_name(F1), 
  					       case refac_syntax:type(T1) of 
  						   atom -> refac_syntax:concrete(refac_syntax:record_field_name(F1))== FName;
  						   _ -> false
  					       end
  				    end, Fields) of 
  		      [F2|_] -> F2;
  		      _ ->
  			  case lists:filter(fun(F1) ->
  					  refac_syntax:type(refac_syntax:record_field_name(F1))== underscore end, Fields) of 
  			      [F2|_] ->
  				  refac_syntax:record_field(refac_syntax:atom(FName), refac_syntax:record_field_value(F2));
  			      _ ->
  				  case FVal of 
  				      none -> refac_syntax:record_field(refac_syntax:atom(FName),refac_syntax:atom(undefined));
  				      _ ->
  					  refac_syntax:record_field(refac_syntax:atom(FName), FVal)
  				  end
  			  end
  		  end	  			 
  	  end,	  
      case refac_syntax:type(Node) of 
  	record_expr ->
  	    Arg = refac_syntax:record_expr_argument(Node),
  
negative.code: 		    As = refac_syntax:get_ann(Op),
  		    case lists:keysearch(fun_def, 1, As) of
  			{value, {fun_def, {M, _FunName, _Arity, _UsePos, _DefinePos}}} ->
  			    Op1 =refac_util:rewrite(
  				   Op, refac_syntax:module_qualifier(set_random_pos(refac_syntax:atom(M)), Op)),
  				refac_util:rewrite(Node, refac_syntax:application(Op1,Args));
  			_ -> Node
  		    end;
  		_ -> Node
  	    end;
  	_ -> Node
      end. 					    
  
  
  denormalise_expr(Exprs, _RecordInfo) ->
     do_denormalise_fun_calls(Exprs).
    
  do_denormalise_fun_calls(Exprs)  when is_list(Exprs)->
      [refac_util:full_buTP(fun do_denormalise_fun_calls_1/2, E, [])||E<-Exprs];
  
  do_denormalise_fun_calls(Expr) ->
      refac_util:full_buTP(fun do_denormalise_fun_calls_1/2, Expr, []).
  
  do_denormalise_fun_calls_1(Node, _Others) ->
      case refac_syntax:type(Node) of
  	application ->
  	    Op = refac_syntax:application_operator(Node),
  	    Args = refac_syntax:application_arguments(Node),
  	    case refac_syntax:type(Op) of 
  		module_qualifier ->
  		    M = refac_syntax:module_qualifier_argument(Op),
  		    F = refac_syntax:module_qualifier_body(Op),
  		    case refac_syntax:type(M)==atom andalso (refac_syntax:get_pos(M) < {0, 0}) of 
  			true ->
  			    refac_util:rewrite(Node, refac_syntax:application(F, Args));
  			_ -> Node
  		    end;
  		_ -> Node
  