about summary refs log tree commit diff stats
path: root/completer
diff options
context:
space:
mode:
Diffstat (limited to 'completer')
-rw-r--r--completer/completer.go43
1 files changed, 28 insertions, 15 deletions
diff --git a/completer/completer.go b/completer/completer.go
index bc6c96f..251658e 100644
--- a/completer/completer.go
+++ b/completer/completer.go
@@ -8,6 +8,7 @@ import (
 	"mime"
 	"net/mail"
 	"os/exec"
+	"regexp"
 	"strings"
 
 	"github.com/google/shlex"
@@ -28,8 +29,8 @@ type Completer struct {
 }
 
 // A CompleteFunc accepts a string to be completed and returns a slice of
-// possible completions.
-type CompleteFunc func(string) []string
+// completions candidates with a prefix to prepend to the chosen candidate
+type CompleteFunc func(string) ([]string, string)
 
 // New creates a new Completer with the specified address book command.
 func New(addressBookCmd string, errHandler func(error), logger *log.Logger) *Completer {
@@ -50,13 +51,13 @@ func (c *Completer) ForHeader(h string) CompleteFunc {
 			return nil
 		}
 		// wrap completeAddress in an error handler
-		return func(s string) []string {
-			completions, err := c.completeAddress(s)
+		return func(s string) ([]string, string) {
+			completions, prefix, err := c.completeAddress(s)
 			if err != nil {
 				c.handleErr(err)
-				return []string{}
+				return []string{}, ""
 			}
-			return completions
+			return completions, prefix
 		}
 	}
 	return nil
@@ -73,23 +74,24 @@ func isAddressHeader(h string) bool {
 }
 
 // completeAddress uses the configured address book completion command to fetch
-// completions for the specified string, returning a slice of completions or an
-// error.
-func (c *Completer) completeAddress(s string) ([]string, error) {
-	cmd, err := c.getAddressCmd(s)
+// completions for the specified string, returning a slice of completions and
+// a prefix to be prepended to the selected completion, or an error.
+func (c *Completer) completeAddress(s string) ([]string, string, error) {
+	prefix, candidate := c.parseAddress(s)
+	cmd, err := c.getAddressCmd(candidate)
 	if err != nil {
-		return nil, err
+		return nil, "", err
 	}
 	stdout, err := cmd.StdoutPipe()
 	if err != nil {
-		return nil, fmt.Errorf("stdout: %v", err)
+		return nil, "", fmt.Errorf("stdout: %v", err)
 	}
 	if err := cmd.Start(); err != nil {
-		return nil, fmt.Errorf("cmd start: %v", err)
+		return nil, "", fmt.Errorf("cmd start: %v", err)
 	}
 	completions, err := readCompletions(stdout)
 	if err != nil {
-		return nil, fmt.Errorf("read completions: %v", err)
+		return nil, "", fmt.Errorf("read completions: %v", err)
 	}
 
 	// Wait returns an error if the exit status != 0, which some completion
@@ -100,7 +102,18 @@ func (c *Completer) completeAddress(s string) ([]string, error) {
 		c.logger.Printf("completion error: %v", err)
 	}
 
-	return completions, nil
+	return completions, prefix, nil
+}
+
+// parseAddress will break an address header into a prefix (containing
+// the already valid addresses) and an input for completion
+func (c *Completer) parseAddress(s string) (string, string) {
+	pattern := regexp.MustCompile(`^(.*),\s+([^,]*)$`)
+	matches := pattern.FindStringSubmatch(s)
+	if matches == nil {
+		return "", s
+	}
+	return matches[1] + ", ", matches[2]
 }
 
 // getAddressCmd constructs an exec.Cmd based on the configured command and
a> 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360