summaryrefslogtreecommitdiff
path: root/lisp/ecomplete.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2018-01-23 12:14:48 -0500
committerStefan Monnier <monnier@iro.umontreal.ca>2018-01-23 12:14:48 -0500
commitf2918640bf35d6bb0130f854b2ea8ed4b4fd89d4 (patch)
tree7e3cccebbefe0c919536b43f0d8d600c0bec1ab0 /lisp/ecomplete.el
parent6d836771da7e9a6a67fcd18e52dd16de1cdc154e (diff)
downloademacs-f2918640bf35d6bb0130f854b2ea8ed4b4fd89d4.tar.gz
* lisp/ecomplete.el: Add completion-table; use lexical-binding and cl-lib
Also remove redundant :group args. (ecomplete-database-file): Use locate-user-emacs-file. (ecomplete-completion-table): New completion table. (completion-category-defaults): Set default behavior for that table.
Diffstat (limited to 'lisp/ecomplete.el')
-rw-r--r--lisp/ecomplete.el64
1 files changed, 42 insertions, 22 deletions
diff --git a/lisp/ecomplete.el b/lisp/ecomplete.el
index 3f0d21c2305..3bfab4743cb 100644
--- a/lisp/ecomplete.el
+++ b/lisp/ecomplete.el
@@ -1,4 +1,4 @@
-;;; ecomplete.el --- electric completion of addresses and the like
+;;; ecomplete.el --- electric completion of addresses and the like -*- lexical-binding:t -*-
;; Copyright (C) 2006-2018 Free Software Foundation, Inc.
@@ -53,22 +53,20 @@
;;; Code:
-(eval-when-compile
- (require 'cl))
+(eval-when-compile (require 'cl-lib))
(defgroup ecomplete nil
"Electric completion of email addresses and the like."
:group 'mail)
-(defcustom ecomplete-database-file "~/.ecompleterc"
+(defcustom ecomplete-database-file
+ (locate-user-emacs-file "ecompleterc" "~/.ecompleterc")
"The name of the file to store the ecomplete data."
- :group 'ecomplete
:type 'file)
(defcustom ecomplete-database-file-coding-system 'iso-2022-7bit
"Coding system used for writing the ecomplete database file."
- :type '(symbol :tag "Coding system")
- :group 'ecomplete)
+ :type '(symbol :tag "Coding system"))
(defcustom ecomplete-sort-predicate 'ecomplete-decay
"Predicate to use when sorting matched.
@@ -80,8 +78,7 @@ string that was matched."
:type '(radio (function-item :tag "Sort by usage and newness" ecomplete-decay)
(function-item :tag "Sort by times used" ecomplete-usage)
(function-item :tag "Sort by newness" ecomplete-newness)
- (function :tag "Other"))
- :group 'ecomplete)
+ (function :tag "Other")))
;;; Internal variables.
@@ -116,13 +113,13 @@ string that was matched."
(with-temp-buffer
(let ((coding-system-for-write ecomplete-database-file-coding-system))
(insert "(")
- (loop for (type . elems) in ecomplete-database
- do
- (insert (format "(%s\n" type))
- (dolist (entry elems)
- (prin1 entry (current-buffer))
- (insert "\n"))
- (insert ")\n"))
+ (cl-loop for (type . elems) in ecomplete-database
+ do
+ (insert (format "(%s\n" type))
+ (dolist (entry elems)
+ (prin1 entry (current-buffer))
+ (insert "\n"))
+ (insert ")\n"))
(insert ")")
(write-region (point-min) (point-max)
ecomplete-database-file nil 'silent))))
@@ -132,9 +129,9 @@ string that was matched."
(match (regexp-quote match))
(candidates
(sort
- (loop for (key count time text) in elems
- when (string-match match text)
- collect (list count time text))
+ (cl-loop for (_key count time text) in elems
+ when (string-match match text)
+ collect (list count time text))
ecomplete-sort-predicate)))
(when (> (length candidates) 10)
(setcdr (nthcdr 10 candidates) nil))
@@ -183,9 +180,7 @@ matches."
(lookup-key local-map command))
(apply (key-binding command) nil)
(setq highlight (ecomplete-highlight-match-line matches line))))
- (if selected
- (message selected)
- (message "Abort"))
+ (message (or selected "Abort"))
selected)))))
(defun ecomplete-highlight-match-line (matches line)
@@ -218,6 +213,31 @@ matches."
(expt 1.05 (/ (- (float-time) (cadr elem))
(* 7 24 60 60)))))
+;; `ecomplete-get-matches' uses substring matching, so also use the `substring'
+;; style by default.
+(add-to-list 'completion-category-defaults
+ '(ecomplete (styles basic substring)))
+
+(defun ecomplete-completion-table (type)
+ "Return a completion-table suitable for TYPE."
+ (lambda (string pred action)
+ (pcase action
+ (`(boundaries . ,_) nil)
+ ('metadata `(metadata (category . ecomplete)
+ (display-sort-function . ,#'identity)
+ (cycle-sort-function . ,#'identity)))
+ (_
+ (let* ((elems (cdr (assq type ecomplete-database)))
+ (candidates
+ (mapcar (lambda (x) (nth 2 x))
+ (sort
+ (cl-loop for x in elems
+ when (string-prefix-p string (nth 3 x)
+ completion-ignore-case)
+ collect (cdr x))
+ ecomplete-sort-predicate))))
+ (complete-with-action action candidates string pred))))))
+
(provide 'ecomplete)
;;; ecomplete.el ends here