Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.0 KiB

5 years ago
  1. ## Do *NOT* use 1L -- it gives parse errors in historical versions of R
  2. ## Try a setup working in as old R as possible.
  3. ## ===>
  4. ## 1) do not use "_" in names! --- seems impossible for the Millenials ..
  5. ## 2) use our own simplified definition of '::' and ':::' ?
  6. ##
  7. if(!exists("local"))
  8. local <- function(expr, envir = environment()) { invisible(eval(expr, envir=envir)) }
  9. ##' Robust version of
  10. ##' utils:::.addFunctionInfo(c = c("recursive", "use.names"))
  11. local({
  12. U <- asNamespace("utils"); fn <- ".addFunctionInfo"
  13. EX <- exists(fn, envir=U)
  14. if(EX && is.function(FN <- get(fn, envir=U))) {
  15. FN(c = c("recursive", "use.names")); ##dbg: cat("Calling utils:::",fn,"(c = ...)\n")
  16. }
  17. })
  18. .ess_eval <- function(str, env = globalenv()) {
  19. ## don't remove; really need eval(parse( here!!
  20. tryCatch(base::eval(base::parse(text=str), envir = env),
  21. error=function(e) NULL) ## also works for special objects containing @:$ etc
  22. }
  23. .ess_nonull <- function(x, default = "") {
  24. if (is.null(x)) default
  25. else x
  26. }
  27. .ess_srcref <- function(name, pkg) {
  28. if (!is.null(pkg) && requireNamespace(pkg)) {
  29. env <- asNamespace(pkg)
  30. } else {
  31. env <- globalenv()
  32. }
  33. fn <- .ess_eval(name, env)
  34. out <- "()\n"
  35. if (is.function(fn) && !is.null(utils::getSrcref(fn))) {
  36. file <- utils::getSrcFilename(fn, full.names = TRUE)
  37. if (file != "") {
  38. line <- .ess_nonull(utils::getSrcLocation(fn, "line"), 1)
  39. col <- .ess_nonull(utils::getSrcLocation(fn, "column"), 1)
  40. out <- sprintf("(\"%s\" %d %d)\n", file, line, col - 1)
  41. }
  42. }
  43. cat(out)
  44. }
  45. .ess_fn_pkg <- function(fn_name) {
  46. fn <- .ess_eval(fn_name)
  47. env_name <- base::environmentName(base::environment(fn))
  48. out <- if (base::is.primitive(fn)) { # environment() does not work on primitives.
  49. "base"
  50. } else if (base::is.function(fn) && env_name != "R_GlobalEnv") {
  51. env_name
  52. } else {
  53. ""
  54. }
  55. base::cat(base::sprintf("%s\n", out))
  56. }
  57. .ess_funargs <- function(funname) {
  58. if(.ess.Rversion > '2.14.1') {
  59. ## temporarily disable JIT compilation and errors
  60. comp <- compiler::enableJIT(0)
  61. op <- options(error=NULL)
  62. on.exit({ options(op); compiler::enableJIT(comp) })
  63. }
  64. fun <- .ess_eval(funname)
  65. if(is.function(fun)) {
  66. special <- grepl('[:$@[]', funname)
  67. args <- if(!special){
  68. fundef <- paste(funname, '.default',sep='')
  69. do.call('argsAnywhere', list(fundef))
  70. }
  71. if(is.null(args))
  72. args <- args(fun)
  73. if(is.null(args))
  74. args <- do.call('argsAnywhere', list(funname))
  75. fmls <- formals(args)
  76. fmls_names <- names(fmls)
  77. fmls <- gsub('\"', '\\\"',
  78. gsub("\\", "\\\\", as.character(fmls), fixed = TRUE),
  79. fixed=TRUE)
  80. args_alist <-
  81. sprintf("'(%s)",
  82. paste("(\"", fmls_names, "\" . \"", fmls, "\")",
  83. sep = '', collapse = ' '))
  84. allargs <-
  85. if (special) fmls_names
  86. else tryCatch(gsub(' ?= ?', '', utils:::functionArgs(funname, ''), fixed = FALSE),
  87. error=function(e) NULL)
  88. allargs <- sprintf("'(\"%s\")",
  89. paste(allargs, collapse = '\" "'))
  90. envname <-
  91. if (is.primitive(fun)) "base"
  92. else environmentName(environment(fun))
  93. if (envname == "R_GlobalEnv") envname <- ""
  94. cat(sprintf('(list \"%s\" %s %s)\n',
  95. envname, args_alist, allargs))
  96. }
  97. }
  98. .ess_get_completions <- function(string, end, suffix = " = ") {
  99. oldopts <- utils::rc.options(funarg.suffix = suffix)
  100. on.exit(utils::rc.options(oldopts))
  101. if(.ess.Rversion > '2.14.1'){
  102. comp <- compiler::enableJIT(0)
  103. op <- options(error=NULL)
  104. on.exit({ options(op); compiler::enableJIT(comp)}, add = TRUE)
  105. }
  106. utils:::.assignLinebuffer(string)
  107. utils:::.assignEnd(end)
  108. utils:::.guessTokenFromLine()
  109. utils:::.completeToken()
  110. c(get('token', envir=utils:::.CompletionEnv),
  111. utils:::.retrieveCompletions())
  112. }
  113. .ess_arg_help <- function(arg, func){
  114. op <- options(error=NULL)
  115. on.exit(options(op))
  116. fguess <-
  117. if(is.null(func)) get('fguess', envir=utils:::.CompletionEnv)
  118. else func
  119. findArgHelp <- function(fun, arg){
  120. file <- help(fun, try.all.packages=FALSE)[[1]]
  121. hlp <- utils:::.getHelpFile(file)
  122. id <- grep('arguments', tools:::RdTags(hlp), fixed=TRUE)
  123. if(length(id)){
  124. arg_section <- hlp[[id[[1]]]]
  125. items <- grep('item', tools:::RdTags(arg_section), fixed=TRUE)
  126. ## cat('items:', items, fill=TRUE)
  127. if(length(items)){
  128. arg_section <- arg_section[items]
  129. args <- unlist(lapply(arg_section,
  130. function(el) paste(unlist(el[[1]][[1]], TRUE, FALSE), collapse='')))
  131. fits <- grep(arg, args, fixed=TRUE)
  132. ## cat('args', args, 'fits', fill=TRUE)
  133. if(length(fits))
  134. paste(unlist(arg_section[[fits[1]]][[2]], TRUE, FALSE), collapse='')
  135. }
  136. }
  137. }
  138. funcs <- c(fguess, tryCatch(methods(fguess),
  139. warning=function(w) {NULL},
  140. error=function(e) {NULL}))
  141. if(length(funcs) > 1 && length(pos <- grep('default', funcs))){
  142. funcs <- c(funcs[[pos[[1]]]], funcs[-pos[[1]]])
  143. }
  144. i <- 1; found <- FALSE
  145. out <- 'No help found'
  146. while(i <= length(funcs) && is.null(out <-
  147. tryCatch(findArgHelp(funcs[[i]], arg),
  148. warning=function(w) {NULL},
  149. error=function(e) {NULL})
  150. ))
  151. i <- i + 1
  152. cat('\n\n', as.character(out), '\n')
  153. }