[R-SIG-Mac] enhance completion function to add () as well as $ on tab

Tim Bates timothy@c@b@te@ @ending from gm@il@com
Mon Oct 1 15:57:26 CEST 2018


Simon very kindly wrote this function a couple of years ago when I noted how nice it would be if pressing tab on an already-completed unique name would write a $ sign and give a drop-down of $ elements.

I'd like to improve the function so that if the completion is a function name, then pressing tab adds "()" (preferably leaving the cursor in the brackets)

  
umx_complete_dollar <- function ()  {
    RGUI = as.environment("tools:RGUI")
    RGUI$rcompgen.completion <- function(x) {
        comp <- function(x) {
            utils:::.assignLinebuffer(x)
            utils:::.assignEnd(nchar(x))
            utils:::.guessTokenFromLine()
            utils:::.completeToken()
            utils:::.CompletionEnv[["comps"]]
        }
        res <- unique(comp(x))
        if (nzchar(x) && identical(res, x) && !identical(substr(x, 
            nchar(x), nchar(x) + 1L), "$")) {
            rc <- comp(paste0(x, "$"))
            if (!identical(substr(rc, nchar(rc), nchar(rc) + 
                1L), "$")) 
                res <- rc
        }
        res
    }
}


Also, I added the function to my  ~/.Rprofile, but this function can't execute then  ( as.environment("tools:RGUI") fails with  "tools:RGUI is not in the search list"

Any clues to improving these? The functionality is worth its weight in gold, IMHO.


More information about the R-SIG-Mac mailing list