- (let ((prefix (try-completion "" (mapcar 'list strings)))
- (letters (let ((completion-regexp-list '("^.$")))
- (all-completions "" (mapcar 'list strings)))))
- (cond
- ;;
- ;; If there is a common prefix, remove it and recurse on the suffixes.
- ((> (length prefix) 0)
- (let* ((length (length prefix))
- (suffixes (mapcar (lambda (s) (substring s length)) strings)))
- (concat open-group
- (regexp-quote prefix) (regexp-opt-group suffixes t t)
- close-group)))
- ;;
- ;; If there are several one-character strings, remove them and recurse
- ;; on the rest (first so the final regexp finds the longest match).
- ((> (length letters) 1)
- (let ((rest (let ((completion-regexp-list '("^..+$")))
- (all-completions "" (mapcar 'list strings)))))
- (concat open-group
- (regexp-opt-group rest) "\\|" (regexp-opt-charset letters)
- close-group)))
- ;;
- ;; Otherwise, divide the list into those that start with a particular
- ;; letter and those that do not, and recurse on them.
- (t
- (let* ((char (substring (car strings) 0 1))
- (half1 (all-completions char (mapcar 'list strings)))
- (half2 (nthcdr (length half1) strings)))
- (concat open-group
- (regexp-opt-group half1) "\\|" (regexp-opt-group half2)
- close-group)))))))))
+ (let ((prefix (try-completion "" strings)))
+ (if (> (length prefix) 0)
+ ;; common prefix: take it and recurse on the suffixes.
+ (let* ((n (length prefix))
+ (suffixes (mapcar (lambda (s) (substring s n)) strings)))
+ (concat open-group
+ (regexp-quote prefix)
+ (regexp-opt-group suffixes t t)
+ close-group))
+
+ (let* ((sgnirts (mapcar (lambda (s)
+ (concat (nreverse (string-to-list s))))
+ strings))
+ (xiffus (try-completion "" sgnirts)))
+ (if (> (length xiffus) 0)
+ ;; common suffix: take it and recurse on the prefixes.
+ (let* ((n (- (length xiffus)))
+ (prefixes
+ ;; Sorting is necessary in cases such as ("ad" "d").
+ (sort (mapcar (lambda (s) (substring s 0 n)) strings)
+ 'string-lessp)))
+ (concat open-group
+ (regexp-opt-group prefixes t t)
+ (regexp-quote
+ (concat (nreverse (string-to-list xiffus))))
+ close-group))
+
+ ;; Otherwise, divide the list into those that start with a
+ ;; particular letter and those that do not, and recurse on them.
+ (let* ((char (char-to-string (string-to-char (car strings))))
+ (half1 (all-completions char strings))
+ (half2 (nthcdr (length half1) strings)))
+ (concat open-group
+ (regexp-opt-group half1)
+ "\\|" (regexp-opt-group half2)
+ close-group))))))))))
+