#' Find Calling Functions
#'
#' @param func A function  object (no quote) or
#' a list/vector of the function object names to be searched.
#' @param resff The result of find_functions from which func will be searched.
#' @param from A name of a function or a list of functions from which
#' func will be searched. (resff has priority over from.)
#' @param nonative : argument of find_functions
#' @param noglobal : argument of find_functions
#' @param packages : argument of find_functions
#' @param print = 0 not to print the result.
#'
#' @details
#' \cr
#' When resff is not given, this function  requires "codetools" package.
#' \cr
#'
#' This function matches each element of func to the elements of resff.
#' \cr
#' resff can be created as \cr
#' \code{ resff <- find_functions( from, nonative=nonative, unlist=0
#' , noglobal=noglobal, packages=packages ) }
#' \cr
#'
#'
#' @examples
#' \dontrun{
#' library(codetools)
#' library(lazy.tools)
#'
#' res=find_calling_functions( vartype, from=ls("package:lazy.tools") )
#' res2=find_calling_functions( c("vartype","varlist"), resff=res$resff )$res
#' # res2 can be obtained by
#' #  find_calling_functions( c("vartype","varlist")
#' #                        , from=ls("package:lazy.tools") )$res
#' # but find_function takes long time.
#' }
#'
#' @return
#' A list of: \cr
#' res The list of function names which contains each of func. \cr
#' resff The reslt of find_functions.
#'
#'
#'
#' @export
#'



find_calling_functions <- function( func=NULL, resff=NULL, from=NULL
                        , nonative=1, noglobal=0, packages=NULL, print=1 ){
 # find functions which call func.
 # Shin-ichi Mayekawa
 # 20180203
 # roxygen2: 20220927dnc

 if( is.null(func) ){
  return(NULL)
 }

 # result of find functions: find functions in from
 if( is.null(resff) ){
  resff=find_functions( from, nonative=nonative, unlist=0
                        , noglobal=noglobal, packages=packages )
 }

 if( mode(func) == "function" ) func=as.character(substitute(func))
 # Here, func contains function names

 # Print( resff, func)

 # first func
 res2=unlist(
  lapply( resff, function(x){ if( func[1]  %in% x ) TRUE else FALSE  } )
 )
 res=list(names(resff)[res2])

 if( length(func) > 1 ){
  for( i in seq(along=(1:length(func)))[-1] ){
   res2=unlist(
    lapply( resff, function(x){ if( func[i]  %in% x ) TRUE else FALSE  } )
   )
   res=append( res, list(names(resff)[res2]) )
  }
 }

 names(res)=func

 if( print ){
  Print(res)
 }

 return( named_list( res, resff ) )

} # end of find_calling_functions


