# Main "replace" function
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Replace text in selection.
#'
#' @param pattern (character) A fixed pattern of text to be replaced
#' (not a regular expression).
#' @param replacement (character) The replacement text.
#' @inheritParams rs_get_ind
#' @export
rs_replace_in_selection <- function(pattern, replacement,
context = get_context()) {
sel <- context$selection[[1]]
old_text <- sel$text
Encoding(old_text) <- "UTF-8"
new_text <- gsub(pattern = pattern,
replacement = replacement,
x = old_text,
fixed = TRUE)
rstudioapi::modifyRange(location = sel$range,
text = as.character(new_text),
id = context$id)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.