anchor.diff:  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]),
  -    SimiScore1 = try
  -		   case SimiScore0 of
  -		     [] -> ?DefaultSimiScore;
  -		     _ -> list_to_float(SimiScore0)
  -		   end
  -		 catch
  -		   V -> V;
  -		   _:_ -> throw({error, "Parameter input is invalid."})
  -		 end,
  -    SimiScore = case SimiScore1 >= 0.1 andalso SimiScore1 =< 1.0 of
  -		  true -> SimiScore1;
  -		  _ -> ?DefaultSimiScore
  -		end,
  -    {ok, {AnnAST, _Info}} = refac_util:parse_annotate_file(FName, true, [], TabWidth),
  +    SimiScore = get_simi_score(SimiScore0),
  +    {Ranges, AntiUnifier} = search_and_gen_anti_unifier(FName, Start, End, SimiScore, SearchPaths, TabWidth),
  +    display_search_results(Ranges, AntiUnifier).
  +
  +
  +get_simi_score(SimiScore0) ->
  +    try  case SimiScore0 of
  +	     [] -> ?DefaultSimiScore;
  +	     _ -> list_to_float(SimiScore0)
  +	 end
  +    catch
  +	V -> case V>=0.1 andalso V=<1.0 of 
  +		 true -> V;
  +		 _ ->?DefaultSimiScore
  +	     end;			      
  +	_:_ -> throw({error, "Parameter input is invalid."})
  +    end.
  +    
  +
  +get_fundef_and_expr(Start, End, AnnAST) ->
       case refac_util:pos_to_fun_def(AnnAST, Start) of
  -      {ok, FunDef} -> FunDef;
  -      {error, _} -> throw({error, "You have not selected an expression!"}),
  -		    FunDef = []
  -    end,
  -    Exprs = refac_util:pos_to_expr_list(FunDef, Start, End),
  -    case Exprs of
  -      [] -> throw({error, "You have not selected an expression!"});
  -      _ -> ok
  -    end,
  +	{ok, FunDef} -> 
  +	    Exprs = refac_util:pos_to_expr_list(FunDef, Start, End),
  +	    case Exprs of
  +		[] -> throw({error, "You have not selected an expression!"});
  +		_ -> {FunDef, Exprs}
  +	    end;
  +	{error, _} -> throw({error, "You have not selected an expression!"})
  +    end.
  +   
  +
  +search_and_gen_anti_unifier(FName, Start, End, SimiScore, SearchPaths, TabWidth) ->
  +    {ok, {AnnAST, _Info}} = refac_util:parse_annotate_file(FName, true, SearchPaths, TabWidth),
  +    {FunDef, Exprs} = get_fundef_and_expr(Start, End, AnnAST),
       RecordInfo = get_module_record_info(FName, SearchPaths, TabWidth),
  -    Exprs1 = case Exprs of
  -	       [E] -> normalise_expr(E, RecordInfo);
  -	       [_| _] ->
  -		   normalise_expr(Exprs, RecordInfo)
  -	     end,
  -    Res = do_search_similar_expr(AnnAST, RecordInfo, Exprs1, SimiScore),
  +    Exprs1 = normalise_expr(Exprs, RecordInfo),
  +    Res = do_search_similar_expr(FName, AnnAST, RecordInfo, Exprs1, SimiScore),
       {Ranges, ExportVars, SubSt} = lists:unzip3(Res),
       SE = get_start_end_loc(Exprs),
  -    Ranges1 = [SE| Ranges -- [SE]],
  -    ExportVars1 = {element(1, lists:unzip(vars_to_export(FunDef, End, Exprs1))), lists:usort(lists:append(ExportVars))},
  +    ExportVars1 = {element(1, lists:unzip(vars_to_export(FunDef, End, Exprs1))),
  +		   lists:usort(lists:append(ExportVars))},
       AntiUnifier = generalise_expr(Exprs1, SubSt, ExportVars1),
  -    %% AntiUnifier = denormalise_expr(generalise_expr(Exprs1,SubSt, ExportVars1),RecordInfo),
  -    Num = length(Ranges1),
  -    case Num =< 1 of
  -      true -> ?wrangler_io("No similar expression has been found.\n", []);
  -      false -> ?wrangler_io("~p expressions (including the expression selected)"
  -			    " which are similar to the expression selected have been found. \n", [Num]),
  -	       ?wrangler_io(compose_search_result_info(FName, Ranges1), []),
  -	       ?wrangler_io("\nThe generalised expression would be:\n\n~s\n\n", [refac_prettypr:format(AntiUnifier)]),
  -	       ?wrangler_io("\nUse 'C-c C-e' to remove highlights!\n", []),
  -	       {ok, Ranges1}
  -    end.
  -   
  -
  -
  -do_search_similar_expr(AnnAST, RecordInfo, Exprs, SimiScore) when is_list(Exprs) ->
  +    {[{FName, SE}| Ranges -- [{FName, SE}]], AntiUnifier}.
  +
  +display_search_results(Ranges, AntiUnifier) ->
  +    case Ranges of
  +	[_] -> 
  +	    ?wrangler_io("No similar expression has been found.\n", []);
  +	_ -> 
  +	    ?wrangler_io("~p expressions (including the expression selected)"
  +			 " which are similar to the expression selected have been found. \n", [length(Ranges)]),
  +	    ?wrangler_io(compose_search_result_info(Ranges), []),
  +	    ?wrangler_io("\nThe generalised expression would be:\n\n~s\n\n", [refac_prettypr:format(AntiUnifier)]),
  +	    ?wrangler_io("\n\nNOTE: Use 'M-x compilation-minor-mode' to make the result "
  +			 "mouse clickable if this mode is not already enabled.\n",[]),
  +	    ?wrangler_io("      Use 'C-c C-e' to remove highlights!\n", []),
  +	    {ok, Ranges}
  +    end.
  +
  +do_search_similar_expr(FileName, AnnAST, RecordInfo, Exprs, SimiScore) when is_list(Exprs) ->
       F0 = fun (FunNode, Acc) ->
   		 F = fun (T, Acc1) ->
  -			     case refac_syntax:type(T) of
  -			       clause -> Exprs1 = refac_syntax:clause_body(T),
  -					 do_search_similar_expr_1(Exprs, Exprs1, RecordInfo, SimiScore, FunNode) ++ Acc1;
  -			       block_expr -> Exprs1 = refac_syntax:block_expr_body(T),
  -					     do_search_similar_expr_1(Exprs, Exprs1, RecordInfo, SimiScore, FunNode) ++ Acc1;
  -			       try_expr -> Exprs1 = refac_syntax:try_expr_body(T),
  -					   do_search_similar_expr_1(Exprs, Exprs1, RecordInfo, SimiScore, FunNode) ++ Acc1;
  -			       _ -> Acc1
  -			     end
  +			     Exprs1 = get_expr_seqs(T),
  +			     do_search_similar_expr_1(FileName, Exprs, Exprs1, RecordInfo, SimiScore, FunNode) ++ Acc1
   		     end,
   		 refac_syntax_lib:fold(F, Acc, FunNode)
   	 end,
  -    F1 = fun (Node, Acc) ->
  -		 case refac_syntax:type(Node) of
  -		   function -> F0(Node, Acc);
  -		   _ -> Acc
  -		 end
  -	 end,
  -    lists:reverse(refac_syntax_lib:fold(F1, [], AnnAST));
  -
  -do_search_similar_expr(AnnAST, RecordInfo, Expr, SimiScore) ->
  +    do_search_similar_expr_1(AnnAST, F0);
  +
  +do_search_similar_expr(FileName, AnnAST, RecordInfo, Expr, SimiScore) ->
       {EStart, EEnd} = get_start_end_loc(Expr),
       F0 = fun (FunNode, Acc) ->
   		 F = fun (Node, Acc1) ->
   			     case refac_util:is_expr(Node) of
   			       true ->
   				   {NStart, NEnd} = get_start_end_loc(Node),
  -				   case EStart =< NStart andalso NEnd =< EEnd orelse
  -					  NStart =< EStart andalso EEnd =< NEnd
  -				       of
  -				     true -> Acc1;
  -				     _ ->
  +				   OverLapped = overlapped_locs({EStart, EEnd}, {NStart, NEnd}),
  +				   case OverLapped of
  +				     true ->
  +					 Acc1;
  +				     false ->
   					 Node1 = normalise_expr(Node, RecordInfo),
   					 ExportVars = vars_to_export(FunNode, NEnd, Node),
  -					 find_anti_unifier(Expr, Node1, SimiScore, ExportVars) ++ Acc1
  +					 find_anti_unifier(FileName, Expr, Node1, SimiScore, ExportVars) ++ Acc1
   				   end;
   			       _ -> Acc1
   			     end
   		     end,
   		 refac_syntax_lib:fold(F, Acc, FunNode)
   	 end,
  +    do_search_similar_expr_1(AnnAST, F0).
  +
  +do_search_similar_expr_1(AnnAST, Fun) ->
       F1 = fun (Node, Acc) ->
   		 case refac_syntax:type(Node) of
  -		   function -> F0(Node, Acc);
  +		   function -> Fun(Node, Acc);
   		   _ -> Acc
   		 end
   	 end,
       lists:reverse(refac_syntax_lib:fold(F1, [], AnnAST)).
  -   
  -
  - 
  -do_search_similar_expr_1(Exprs1, Exprs2, RecordInfo, SimiScore, FunNode) ->
  +
  +
  +get_expr_seqs(T) ->
  +    case refac_syntax:type(T) of
  +	clause ->
  +	    refac_syntax:clause_body(T);
  +	block_expr ->
  +	    refac_syntax:block_expr_body(T);
  +	try_expr ->
  +	    refac_syntax:try_expr_body(T);
  +	_ -> []
  +    end.
  +
  +overlapped_locs({Start1, End1}, {Start2, End2}) ->
  +    Start1 =< Start2 andalso End2 =< End1 orelse
  +      Start2 =< Start1 andalso End1 =< End2.
  +
  +
  +do_search_similar_expr_1(FileName, Exprs1, Exprs2, RecordInfo, SimiScore, FunNode) ->
       Len1 = length(Exprs1),
       Len2 = length(Exprs2),
       case Len1 =< Len2 of
  -      true -> Exprs21 = lists:sublist(Exprs2, Len1),
  -	      {S1, E1} = get_start_end_loc(Exprs1),
  -	      {S2, E2} = get_start_end_loc(Exprs21),
  -	      case S1 =< S2 andalso E2 =< E1 orelse S2 =< S1 andalso E1 =< E2 of
  -		true -> [];
  -		_ ->
  -		    ExportVars = vars_to_export(FunNode, E2, Exprs21),
  -		    find_anti_unifier(Exprs1, normalise_expr(Exprs21, RecordInfo), SimiScore, ExportVars)
  -		      ++ do_search_similar_expr_1(Exprs1, tl(Exprs2), RecordInfo, SimiScore, FunNode)
  -	      end;
  -      _ -> []
  -    end.
  -
  -
  -find_anti_unifier(Expr1, Expr2, SimiScore, Expr2ExportVars) ->
  +	true -> Exprs21 = lists:sublist(Exprs2, Len1),
  +		{S1, E1} = get_start_end_loc(Exprs1),
  +		{S2, E2} = get_start_end_loc(Exprs21),
  +		case overlapped_locs({S1,E1}, {S2,E2}) of
  +		    true -> [];
  +		    _ ->
  +			ExportVars = vars_to_export(FunNode, E2, Exprs21),
  +			find_anti_unifier(FileName, Exprs1, normalise_expr(Exprs21, RecordInfo), SimiScore, ExportVars)
  +			    ++ do_search_similar_expr_1(FileName, Exprs1, tl(Exprs2), RecordInfo, SimiScore, FunNode)
  +		end;
  +	_ -> []
  +    end.
  +
  +
  +find_anti_unifier(FileName, Expr1, Expr2, SimiScore, Expr2ExportVars) ->
       try
  -      do_find_anti_unifier(Expr1, Expr2)
  +	do_find_anti_unifier(Expr1, Expr2)
       of
         SubSt ->
   	    {SubExprs1, SubExprs2} = lists:unzip(SubSt),
  
positive.code: -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.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>
  
  
negative.code: 
  -export([get_start_end_loc/1, start_counter_process/0, 
  	 stop_counter_process/1, gen_new_var_name/1,
  	 variable_replaceable/1, find_anti_unifier/5,
  	 generalise_expr/3, vars_to_export/3]).
  
  -import(refac_duplicated_code, [collect_vars/1]).
  -import(refac_expr_search, [compose_search_result_info/1]).
  
  -include("../include/wrangler.hrl").
  
  