diff --git a/R/addin.R b/R/addin.R index 1acfd18..612053a 100644 --- a/R/addin.R +++ b/R/addin.R @@ -18,13 +18,6 @@ addin_insertReturn <- function() { rstudioapi::insertText("%<>% ") } -#' @export -addin_selectWord <- function() { - r = rstudioapi::getActiveDocumentContext() - browser() - print(r) -} - # fix new line ending in windows system # If content is empty, not write #' @import clipr @@ -55,7 +48,6 @@ addin_cutLines <- function() { #' @rdname addin_cutLines #' @export addin_copyLines <- function(output = FALSE){ - # info <- getSourceEditorContext() info <- getActiveDocumentContext() # set ranges @@ -75,6 +67,33 @@ addin_copyLines <- function(output = FALSE){ if (output) return(info) } +#' @export +addin_selectWord <- function() { + info = rstudioapi::getActiveDocumentContext() + rng <- info$selection[[1]]$range + + row = rng$start[1] + col = rng$start[2] + nline <- rng$end[1] - rng$start[1] + 1 + nchar <- rng$end[2] - rng$start[2] + 1 + + # str_post = str_extract(row, glue("(?<=.{{col}})\\w+")) + tryCatch({ + str = info$contents[row] + pos = str_locate_all(str, "[\\w\\.]+") %>% + .[start <= col & end >= col, ] + + rng$start[2] = pos$start + rng$end[2] = pos$end + 1 + info$selection[[1]]$range <- rng + info$selection[[1]]$text <- info$contents[rng$start[1]] + + rstudioapi::setSelectionRanges(rng, info$id) + }, error = function(e) { + message(sprintf('%s', e$message)) + }) +} + #' blind shortcuts to rstudio addin #' @export key_blind <- function(){ @@ -85,12 +104,12 @@ key_blind <- function(){ if (!dir.exists(indir)) dir.create(indir, recursive = TRUE) if (!file.exists(file_addin)) writeLines("{}", file_addin) - + options_addin <- list( "Ipaper::addin_copyLines" = "Alt+C", - "Ipaper::addin_cutLines" = "Alt+X", + "Ipaper::addin_cutLines" = "Ctrl+X", "Ipaper::addin_insertDo" = "Ctrl+Alt+D", - "Ipaper::addin_selectWord" = "Alt+D", + "Ipaper::addin_selectWord" = "Ctrl+D", "Ipaper::addin_insertIn" = "Ctrl+Shift+I", "Ipaper::addin_insertReturn" = "Ctrl+Shift+,", "Ipaper::smerge" = "Ctrl+Shift+G", diff --git a/R/file_name.R b/R/file_name.R index 070cddd..0dc0f9b 100644 --- a/R/file_name.R +++ b/R/file_name.R @@ -3,11 +3,16 @@ str_extract <- function(x, pattern) { substr(x, ans, ans + attr(ans, "match.length")) } -str_locate_all <- function(x, pattern) { +str_locate_all.vec <- function(x, pattern) { ans <- gregexpr(pattern, x, perl = TRUE)[[1]] ans } +str_locate_all <- function(x, pattern) { + ans <- gregexpr(pattern, x, perl = TRUE)[[1]] + data.table(I = seq_along(ans), start = ans, end = attr(ans, "match.length") + ans - 1) +} + #' @export #' @rdname file_name file_ext <- function(file) { @@ -26,7 +31,7 @@ file_ext <- function(file) { #' @export file_name <- function(file) { file <- basename(file) - pos <- str_locate_all(file, "\\.") + pos <- str_locate_all.vec(file, "\\.") pos <- pos[[length(pos)]] # I_dot <- pos[nrow(pos), 1] # no dot diff --git a/R/tools_plot.R b/R/tools_plot.R new file mode 100644 index 0000000..1aa8aec --- /dev/null +++ b/R/tools_plot.R @@ -0,0 +1,19 @@ + +add_gridLine <- function(dates, col = "grey60", lty = 3, ...) { + years <- year(dates) + date_beg <- ymd( min(years) *1e4 + 0101 ) + date_end <- ymd( max(years) *1e4 + 0101 ) + + t_grids <- seq.Date(date_beg, date_end, by = "year") + abline(v = t_grids, col = col, lty = lty, ...) +} + +make_dt <- function(..., ncol = 3) { + x <- list(...) + n <- length(x) + nrow <- floor(n / ncol) + lapply(1:nrow, function(i) { + ind <- seq((i - 1) * ncol + 1, i * ncol) + x[ind] %>% as.data.table() + }) %>% do.call(rbind, .) +} diff --git a/README.md b/README.md index 886c362..06fd8c5 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,19 @@ install.packages("../Ipaper_0.1.5.9000.tar.gz", repos = NULL, type = "source", d | Description | Shortcut | |---------------------------------------------------|--------------| -| copy lines at the cursor position (sublime style) | Alt+c | -| clip lines at the cursor position | Alt+x | +| copy lines at the cursor position (sublime style) | Alt+C | +| clip lines at the cursor position | Ctrl+X | +| Select word | Ctrl+D | | Insert `%<>%` | Ctrl+Shift+, | | Insert `%do% {}` | Ctrl+Shift+D | | Insert `%in% {}` | Ctrl+Shift+I | +| Open in VSCode | Alt+Shift+V | +| Open in smerge | Ctrl+Shift+G | +| Open in subl | Alt+Shift+L | + +Note that `VSCode`, `smerge` or `subl` should be in the system path, if you want to use it. + +After install, run `Ipaper::key_blind()` to make those shortcuts work. ### Visualization diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index 8a4535f..c3f0c32 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -1,24 +1,24 @@ -Name: Insert %in% +Name: Insert %in% (Ctrl+Shift+I) Description: Inserts `%in%` at the cursor position. Binding: addin_insertIn Interactive: false -Name: Insert %do% {} +Name: Insert %do% {} (Ctrl+Alt+D) Description: Inserts `%do% {}` at the cursor position. Binding: addin_insertDo Interactive: false -Name: Insert %<>% +Name: Insert %<>% (Ctrl+Shift+,) Description: Inserts `%<>%` at the cursor position. Binding: addin_insertReturn Interactive: false -Name: Copy lines +Name: Copy lines (Alt+C) Description: Copy lines at the cursor position. Binding: addin_copyLines Interactive: false -Name: Cut lines +Name: Cut lines (Ctrl+X) Description: Cut lines at the cursor position. Binding: addin_cutLines Interactive: false @@ -28,17 +28,17 @@ Description: Open current working project in smerge Binding: smerge Interactive: false -Name: Open in subl +Name: Open in subl (Alt+Shift+L) Description: Open current working directory in sublime Binding: subl Interactive: false -Name: Open in VSCode +Name: Open in VSCode (Alt+Shift+V) Description: Open current working directory in VSCode Binding: code Interactive: false -Name: Select word +Name: Select word (Ctrl+D) Description: Select word Binding: addin_selectWord Interactive: false