-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.R
More file actions
76 lines (67 loc) · 2.15 KB
/
browser.R
File metadata and controls
76 lines (67 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## For now, you need a recent enough svn checkout
## and have to compile main.c with -DUSE_BROWSER_HOOK
stopifnot(as.integer(R.version$'svn rev') >= 85116)
bhook <- function(hook, condition, envir) {
## cat("Entering Browser Hook. ")
op <- options(browser.hook = bhook)
repeat {
## cat("Starting Browser REPL\n")
success <- FALSE
tryCatch({
val <- browser("", condition, envir, 0L, ignoreHook = TRUE)
success <- TRUE
return(val)
}, finally = if (! askBrowserExit(success)) next)
}
}
askBrowserExit <- function(success) {
if (success)
TRUE
else {
mtitle <- "Enter a selection number:"
options <- c("Leave the browser with a jump.",
"Stay in the browser session.")
choice <- menu(options, title = mtitle)
if (choice == 2) FALSE else TRUE
}
}
options(browser.hook = bhook)
options(browser.error.handler = function(e) NULL)
options(error = NULL)
restartName <- function(r) r[[1]] ## **** need to add this to base
## These top-level handlers should deal with
##
## - warnings
## - traceback
##
## Ideally we would factor out and make available the current C code
## for that
errhandler <- function(e) {
msg <- conditionMessage(e)
nf <- sys.nframe()
call <- if (nf < 3) NULL else sys.call(-3)
env <- if (nf < 3) .GlobalEnv else sys.frame(-3)
if (is.null(call))
cat(sprintf("Error: %s\n", msg))
else
cat(sprintf("Error in %s: %s\n", deparse(call, nlines = 1), msg))
withCallingHandlers({
browser(condition = e, expr = env)
invokeRestart(findAbortOrBrowserRestart())
}, error = errhandler2)
}
errhandler2 <- function(e) {
msg <- conditionMessage(e)
cat(sprintf("Error: %s\n", msg))
r <- findAbortOrBrowserRestart()
rn <- restartName(r)
cat(sprintf("Recursive error? Invoking '%s' restart.\n", rn))
invokeRestart(r)
}
findAbortOrBrowserRestart <- function() {
goodRestart <- function(x)
restartName(x) %in% c("abort", "browser")
Find(goodRestart, computeRestarts())
}
globalCallingHandlers(NULL)
globalCallingHandlers(error = errhandler)