Christophe Weblog Wiki Code Publications Music
escape names when searching for completions
[swankr.git] / swank.R
diff --git a/swank.R b/swank.R
index b5c79c78204695667f421bbceb3b3e83664971c1..f0c0d15373ff150a29f291f56d89b23f9050c276 100644 (file)
--- 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)