anchor.diff:  -export([input_par_prompts/0, select_focus/1,
            check_pre_cond/1, selective/0,
            transform/1]).
  -
  --export([swap_args/6]).
   
   -include("../include/wrangler.hrl").
   
  
positive.code:           end,
            api_refac:fun_define_info(f@) == {M, F, A}).
  
  %% the following rules transform the different kinds of 
  %% application senarioes of the function.
  rule2({M,F,A}, I, J) ->
      ?RULE(?T("F@(Args@@)"), 
            begin NewArgs@@=swap(Args@@, I, J),
                  ?QUOTE("F@(NewArgs@@)")
            end,
            fun_define_info(F@) == {M, F, A}).
  
  rule3({M,F,A}, I, J)->
      ?RULE(?T("Fun@(N@@, M@, F@, [Args@@])"),
            begin
                NewArgs@@=swap(Args@@, I, J),
                ?QUOTE("Fun@(N@@, M@, F@,[NewArgs@@])")
            end,
            case fun_define_info(Fun@) of
                {erlang, apply, _} -> 
                    api_refac:fun_define_info(F@) == {M,F,A};
                _ -> false
            end).
  
  rule4({M,F,A}, I, J) ->
      ?RULE(?T("Fun@(N@@, M@, F@, Args@)"),
            begin
                AfterStr =lists:flatten(
                            io_lib:format(
                              "Fun@(N@@, M@, F@, 
                                      fun(List) ->
                                         Ith = lists:nth(~p, List),
                                         Jth = lists:nth(~p, List),
                                         T = list_to_tuple(List),
                                         T1=setelement(~p, setelement(~p, T, Jth), Ith),
                                         tuple_to_list(T1)
                                      end(Args@))", [I, J,J,I])),
                ?QUOTE(AfterStr)
            end,
            case fun_define_info(Fun@) of 
                {erlang,apply, _} -> 
                    api_refac:fun_define_info(F@) == {M,F,A};
                _ -> false
            end).
  
  rule5({M,F,A}, I, J) ->
      ?RULE(?T("Fun@(F@, [Args@@])"), 
            begin
                NewArgs@@= swap(Args@@, I,J),
                ?QUOTE("Fun@(F@, [NewArgs@@])")
            end, 
            fun_define_info(Fun@)=={erlang, apply, 2} andalso 
            fun_define_info(F@)=={M,F,A}).
  
  rule6({M,F,A}, I, J)->
      ?RULE(?T("Fun@(F@, Args@)"), 
            begin
                AfterStr =lists:flatten(
                            io_lib:format(
                              "Fun@(F@, 
                                      fun(List) ->
                                         Ith = lists:nth(~p, List),
                                         Jth = lists:nth(~p, List),
                                         T = list_to_tuple(List),
                                         T1=setelement(~p, setelement(~p, T, Jth), Ith),
                                         tuple_to_list(T1)
                                      end(Args@))", [I, J,J ,I])),
                ?QUOTE(AfterStr)
            end,
            fun_define_info(Fun@)=={erlang, apply, 2} andalso
            fun_define_info(F@)=={M,F,A}).
  
  
  %% utility functions.
  swap(List, I, J) ->
      Ith = lists:nth(I, List),
      Jth = lists:nth(J, List),
      T = list_to_tuple(List),
      T1=setelement(J, setelement(I, T, Jth), Ith),
      tuple_to_list(T1).
   
   
  
  swap_args({FileName, FunName, Arity}, Index1, Index2, SearchPaths, Editor, TabWidth) ->
      {ok, {AnnAST, _Info}} = wrangler_ast_server:parse_annotate_file(FileName, true, SearchPaths, TabWidth),
      ModName=list_to_atom(filename:basename(FileName, ".erl")),
      case wrangler_misc:funname_to_defpos(AnnAST, {ModName, FunName, Arity}) of
  	{ok, DefPos} ->
              {ok, FunDef} = api_interface:pos_to_fun_def(FileName, DefPos),
              Args=#args{current_file_name=FileName,
                         focus_sel=FunDef,
                         user_inputs=[Index1, Index2],
                         search_paths=SearchPaths,
                         tabwidth=TabWidth},
              case check_pre_cond(Args) of
                  ok -> 
                      case transform(Args) of
                          {ok, Res} ->
                              wrangler_write_file:write_refactored_files(Res,Editor,TabWidth,"");
                          {error, Reason}->
                              {error, Reason}
                      end;
                  {error, Reason} ->
                      {error, Reason}
              end;
          {error, Reason} ->
              {error, Reason}
      end.
  
negative.code: %% transform the current file.
  transform_in_cur_file(_Args=#args{current_file_name=File},MFA, I, J)->
      ?FULL_TD_TP([rule1(MFA, I, J),
                   rule2(MFA, I, J)],
                  [File]).
  
  %% transform the client files.
  transform_in_client_files(_Args=#args{current_file_name=File,
                                        search_paths=SearchPaths}, 
                            MFA, I, J) ->
      ?FULL_TD_TP([rule2(MFA, I, J)],
                  api_refac:client_files(File, SearchPaths)).
  
  
  %% transform the function definition itself.
  rule1({M,F,A}, I, J) ->
  