anchor.diff:           rename_var/4, 
            swap_args/4,
            fold_expr/4,
  -         gen_fun/5]).
  +         gen_fun/6,
  +         move_fun/4]).
   
   -include("../include/wrangler.hrl").
   
  -            
  -
  - %% wrangler_extended:gen_fun({test,fun(_Fun) -> true end,
  - %%                           fun(_Arity) -> true end},
  - %%                          fun(Expr) ->
  - %%                                  refac_api:splice(Expr)=="8"
  - %%                          end, 
  - %%                          {user_input, fun(_) ->"New parameter name: " end},
  - %%                          ["c:/cygwin/home/hl/test/test.erl"], callgraph_bottom_up).
  -
  -gen_fun({ModOrFile, FunName, Arity}, Expr, NewParName, SearchPaths, GenOrder) ->
  -    refac_io:format("ModOrFile:\n~p\n", [ModOrFile]),
  +move_fun({SrcModOrFile, FunName, Arity}, TgtModOrFile, SearchPaths, GenOrder) ->
  +    case locate_one_file(SrcModOrFile, SearchPaths) of
  +        {error, Reason} ->
  +            wrangler_io:format("Warning: ~p\n", [Reason]),
  +            [];
  +        SrcFile ->
  +            case locate_one_file(TgtModOrFile, SearchPaths) of
  +                {error, Reason} ->
  +                    wrangler_io:format("Warning: ~p\n", [Reason]),
  +                    [];
  +                TgtFile ->
  +                   move_fun_1({SrcFile, FunName, Arity}, TgtFile, SearchPaths, GenOrder)
  +            end
  +    end.
  +
  +move_fun_1({SrcFile, FunName, Arity}, TgtFile, SearchPaths, GenOrder) ->
  +    FAs= get_fun_arity(SrcFile, FunName, Arity, GenOrder),
  +    [{refactoring, {refac_move_fun, move_fun_by_name, [{SrcFile, F, A}, TgtFile, 
  +                                                       SearchPaths, composite_emacs]},
  +      gen_question(move_fun, {SrcFile, F, A, TgtFile})} 
  +     ||{F, A} <-FAs].
  +        
  +gen_fun(ModOrFile, FA, Expr, NewParName, SearchPaths, GenOrder) ->
       Files= get_files(ModOrFile, SearchPaths),
  -    refac_io:format("Files:\n~p\n", [Files]),
  -    CmdLists=[gen_fun_1(File, FunName, Arity, Expr, NewParName, SearchPaths, GenOrder)
  +    CmdLists=[gen_fun_1(File, FA, Expr, NewParName, SearchPaths, GenOrder, [])
                 ||File<-Files],
       lists:append(CmdLists).
   
  -gen_fun_1(ModOrFile, FunName, Arity, Expr, NewParName, SearchPaths, GenOrder) ->
  -    Files= get_files(ModOrFile, SearchPaths),
  -    Refacs=lists:append([gen_fun_2(File, FunName, Arity,Expr, NewParName, SearchPaths, GenOrder)
  -                         ||File<-Files]),
  +gen_fun_1(File, FA, Expr, NewParName, SearchPaths, GenOrder, Acc) ->
  +    Refacs=gen_fun_2(File, FA,Expr, NewParName, SearchPaths, GenOrder, Acc),
       case Refacs of 
           [] -> [];
  -        [R|_Rs] ->
  +        [R={refactoring, {refac_gen, generalise, [File, F, A |_]}, _Question}|_Rs] ->
               {R,{generator, fun()->
  -                                   gen_fun_1(ModOrFile, FunName, Arity, Expr, NewParName, SearchPaths, GenOrder)
  +                                   gen_fun_1(File, FA, Expr, NewParName, SearchPaths, GenOrder, [{File, F, A}|Acc])
                              end}}     
  -        end.
  +    end.
                                   
  -gen_fun_2(File, FunName, Arity,ExprFilter, NewParName, SearchPaths, GenOrder)->
  -    FAs= get_fa(File, FunName, Arity, GenOrder),
  +gen_fun_2(File, FA,ExprFilter, NewParName, SearchPaths, GenOrder, Acc)->
  +    FAs= get_fa(File, FA, GenOrder),
       [{refactoring, {refac_gen, generalise, 
                       [File, F, A, Exprs, new_name_gen(File, F, A, Exprs, NewParName),
                        SearchPaths, composite_emacs]}, gen_question(gen_fun,{File,F,A,Exprs})}
  -     ||{F, A}<-FAs, Exprs<-[get_exprs(File, F, A, ExprFilter)]].
  -
  -
  +     ||{F, A}<-FAs, not lists:member({File, F, A}, Acc),
  +       Ranges<-[get_exprs(File, F, A, ExprFilter)], Ranges/=[],
  +       Exprs<-[{range, {File, Ranges}}]].
  +
  +  
   get_exprs(File, FunName, Arity, ExprFilter) ->
       ModName=list_to_atom(filename:basename(File, ".erl")),
  -    FunDef=refac_api:mfa_to_fun_def({ModName, FunName, Arity}, File),
  -    Ranges=?FULL_TD_TU([?COLLECT(?T("E@"),
  -                                 refac_api:start_end_loc(E@),
  -                                 refac_api:is_expr(E@) andalso
  -                                 ExprFilter(E@))], FunDef),
  -    {range, {File, lists:sort(Ranges)}}.
  -    
  -
  -
  +    FunDef=api_refac:mfa_to_fun_def({ModName, FunName, Arity}, File),
  +    ?FULL_TD_TU([?COLLECT(?T("E@"),  
  +                          api_refac:start_end_loc(E@),
  +                          api_refac:is_expr(E@) andalso
  +                          ExprFilter(E@))], FunDef).
  + 
   fold_expr(CurModOrFile, {ModOrFile, FunName, Arity}, ClauseIndex, SearchPaths) ->
       Files= get_files(CurModOrFile, SearchPaths),
  -    refac_io:format("Files:\n~p\n", [Files]),
  -    refac_io:format("current files:~p\n", [get_files(ModOrFile, SearchPaths)]),
  -    [TargetFile] = get_files(ModOrFile, SearchPaths),
  -    ModName=list_to_atom(filename:basename(TargetFile, ".erl")),
  -    [{F,A}] = get_fa(TargetFile, FunName, Arity),
  -    CmdLists=[fold_expr_1(File,  ModName, F, A, ClauseIndex, SearchPaths)
  -              ||File<-Files],
  -    lists:append(CmdLists).
  -
  -
  +    case locate_one_file(ModOrFile, SearchPaths) of
  +        {error, Reason} ->
  +            wrangler_io:format("Warning: ~p\n", [Reason]); 
  +        TgtFile ->
  +            ModName=list_to_atom(filename:basename(TgtFile, ".erl")),
  +            [{F,A}] = get_fun_arity(TgtFile, FunName, Arity),
  +            CmdLists=[fold_expr_1(File,  ModName, F, A, ClauseIndex, SearchPaths)
  +                      ||File<-Files],
  +            lists:append(CmdLists)
  +    end.
  +        
   fold_expr_1(File, ModName, FunName, Arity, ClauseIndex, SearchPaths) ->
       [{refactoring, {refac_fold_expression, fold_expr_by_name, 
  -                    [File, ModName, FunName, Arity, ClauseIndex, SearchPaths, composite_emacs]},
  +                    [File, atom_to_list(ModName), atom_to_list(FunName), 
  +                     integer_to_list(Arity), integer_to_list(ClauseIndex), 
  +                     SearchPaths, composite_emacs]},
         lists:flatten(io_lib:format("Do you want to fold against function ~p:~p/~p?", 
                                     [ModName,FunName,Arity]))}].
   
  
positive.code: %%                                                              string())}|atom(),
  %%                  SearchPaths::[filename()|dir()]) ->
  %%                         [{refactoring, {refac_rename_var, rename_var, [any()]}, string()}]. 
  
                             
  
  rename_var({ModOrFile, FunName, Arity}, OldVarName, NewVarName, SearchPaths) ->
      rename_var({ModOrFile, FunName, Arity}, OldVarName, NewVarName, SearchPaths,0).
  
  
negative.code: 
  get_vars(File, FunName, Arity, VarFilter) ->
      ModName=list_to_atom(filename:basename(File, ".erl")),
      FunDef=api_refac:mfa_to_fun_def({ModName, FunName, Arity}, File),
      Vars=?FULL_TD_TU([?COLLECT(?T("V@"), 
                                 {range, {File, [api_refac:start_end_loc(V@)]},?SPLICE(V@)},
                                 api_refac:type(V@)==variable andalso
                                 api_refac:bound_vars(V@)/=[] andalso 
                                 case VarFilter of 
                                     atom ->
                                         atom_to_list(VarFilter)==?SPLICE(V@);
  