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.

134 lines
4.3 KiB

4 years ago
  1. #### Essential functionality needed by ESS
  2. ## Should work on *all* vesions of R.
  3. ## Do not use _ in names, nor :: , nor 1L etc, as they
  4. ## cannot be parsed in old R versions
  5. .ess.getRversion <- function() {
  6. if(exists("getRversion", mode="function")) getRversion()
  7. else paste(R.version$major, R.version$minor, sep=".")
  8. }
  9. ## loading ESSR.rda might fail, so re-assign here:
  10. .ess.Rversion <- .ess.getRversion()
  11. .ess.R.has.utils <- (.ess.Rversion >= "1.9.0")
  12. .ess.utils.name <- paste("package",
  13. if(.ess.Rversion >= "1.9.0") "utils" else "base",
  14. sep = ":")
  15. ## Instead of modern utils::help use one that works in R 1.0.0:
  16. .ess.findFUN <- get("find", .ess.utils.name)
  17. ### HELP
  18. .ess.help <- function(..., help.type = getOption("help_type")) {
  19. if (is.null(help.type)) {
  20. help.type <- "text"
  21. }
  22. ## - get("help", ..) searching in global env works with devtools redefines
  23. ## - Redefining to .ess.help this way is necessary because
  24. ## utils:::print.help_files_with_topic (used internally when there's
  25. ## more than one a package) uses the quoted call
  26. ## MM: don't understand; more specifically?
  27. .ess.help <- function(...) {
  28. do.call(get("help", envir = .GlobalEnv), list(...))
  29. }
  30. if (.ess.Rversion > "2.10") {
  31. ## Abbreviating help_type to avoid underscore
  32. .ess.help(..., help = help.type)
  33. } else {
  34. .ess.help(..., htmlhelp = help.type == "html")
  35. }
  36. }
  37. .ess.getHelpAliases <- function(){
  38. readrds <-
  39. if(.ess.Rversion >= '2.13.0') readRDS
  40. else .readRDS
  41. rds.files <- paste(searchpaths(), "/help/aliases.rds", sep = "")
  42. unlist(lapply(rds.files,
  43. function(f){
  44. if( file.exists(f) )
  45. try(names(readrds(f)))
  46. }),
  47. use.names = FALSE)
  48. }
  49. ### SOURCING
  50. .ess.eval <- function(string, visibly = TRUE, output = FALSE,
  51. max.deparse.length = 300,
  52. file = tempfile("ESS"), local = NULL)
  53. {
  54. if (is.null(local)) {
  55. local <- if (.ess.Rversion > '2.13') parent.frame() else FALSE
  56. }
  57. ## create FILE, put string into it. Then source.
  58. ## arguments are like in source and .ess.source
  59. cat(string, file = file)
  60. ## The following on.exit infloops in R 3.3.0
  61. ## https://github.com/emacs-ess/ESS/issues/334
  62. ## https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16971
  63. ## So we are cleanning it in .ess.source instead.
  64. ## on.exit(file.remove(file))
  65. .ess.source(file, visibly = visibly, output = output,
  66. max.deparse.length = max.deparse.length,
  67. local = local, fake.source = TRUE)
  68. }
  69. .ess.strip.error <- function(msg, srcfile) {
  70. pattern <- paste0(srcfile, ":[0-9]+:[0-9]+: ")
  71. sub(pattern, "", msg)
  72. }
  73. .ess.file.remove <- function(file){
  74. if (base::file.exists(file)) base::file.remove(file)
  75. else FALSE
  76. }
  77. .ess.source <- function(file, visibly = TRUE, output = FALSE,
  78. max.deparse.length = 300, local = NULL,
  79. fake.source = FALSE, keep.source = TRUE,
  80. message.prefix = "") {
  81. if (is.null(local)) {
  82. local <- if (.ess.Rversion > "2.13")
  83. parent.frame()
  84. else FALSE
  85. }
  86. ss <-
  87. if (.ess.Rversion >= "3.4")
  88. base::source
  89. else if (.ess.Rversion >= "2.8")
  90. function(..., spaced) base::source(...)
  91. else function(..., spaced, keep.source) base::source(...)
  92. on.exit({
  93. if (fake.source)
  94. .ess.file.remove(file)
  95. })
  96. out <- ss(file, echo = visibly, local = local, print.eval = output, spaced = FALSE,
  97. max.deparse.length = max.deparse.length, keep.source = keep.source)
  98. if(!fake.source)
  99. cat(sprintf("%sSourced file %s\n", message.prefix, file))
  100. ## Return value for org-babel
  101. invisible(out$value)
  102. }
  103. if(.ess.Rversion < "1.8")
  104. ## (works for "1.7.2"): bquote() was new in 1.8.0
  105. bquote <- function(expr, where=parent.frame()){
  106. unquote <- function(e)
  107. if (is.pairlist(e)) as.pairlist(lapply(e, unquote))
  108. else if (length(e) <= 1) e
  109. else if (e[[1]] == as.name(".")) eval(e[[2]], where)
  110. else as.call(lapply(e, unquote))
  111. unquote(substitute(expr))
  112. }