From b79d2d62763dafc48ce669b89f4bff29242a6361 Mon Sep 17 00:00:00 2001 From: Christophe Rhodes Date: Tue, 13 Sep 2011 22:26:12 +0100 Subject: [PATCH] escape names when searching for completions otherwise extended regexp metacharacters, particularly ".", get interpreted as those metacharacters rather than literals. (bug #12) --- BUGS.org | 2 +- swank.R | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/BUGS.org b/BUGS.org index 24cd075..a91d7b8 100644 --- a/BUGS.org +++ b/BUGS.org @@ -42,7 +42,7 @@ Logged here so that I don't forget it if I sort out #9 first: we need to be able to indicate "no repl result please" if we open a new temporary buffer to display the result. -* OPEN #12 need to be more careful when searching for completions :NORMAL: +* RESOLVED #12 escaping when searching for completions :NORMAL:FIXED: Gets thoroughly confused on completing "read." because the "." is treated as a regexp "any character" pattern. * COMMENT: diff --git a/swank.R b/swank.R index b5c79c7..f0c0d15 100644 --- a/swank.R +++ b/swank.R @@ -430,14 +430,17 @@ computeRestartsForEmacs <- function (sldbState) { } `swank:simple-completions` <- function(slimeConnection, sldbState, prefix, package) { - ## fails multiply if prefix contains regexp metacharacters - matches <- apropos(sprintf("^%s", prefix), ignore.case=FALSE) + literal2rx <- function(string) { + ## list of ERE metacharacters from ?regexp + gsub("([.\\|()[{^$*+?])", "\\\\\\1", string) + } + matches <- apropos(sprintf("^%s", literal2rx(prefix)), ignore.case=FALSE) nmatches <- length(matches) if(nmatches == 0) { list(list(), "") } else { longest <- matches[order(nchar(matches))][1] - while(length(grep(sprintf("^%s", longest), matches)) < nmatches) { + while(length(grep(sprintf("^%s", literal2rx(longest)), matches)) < nmatches) { longest <- substr(longest, 1, nchar(longest)-1) } list(as.list(matches), longest) -- 2.30.2