* lisp/vc/diff-mode.el (diff-wiggle): New command.
[emacs.git] / lisp / ecomplete.el
blob3bfab4743cb8980e27767a9828b126c5d1df4447
1 ;;; ecomplete.el --- electric completion of addresses and the like -*- lexical-binding:t -*-
3 ;; Copyright (C) 2006-2018 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; ecomplete stores matches in a file that looks like this:
27 ;; ((mail
28 ;; ("larsi@gnus.org" 38154 1516109510 "Lars Ingebrigtsen <larsi@gnus.org>")
29 ;; ("kfogel@red-bean.com" 10 1516065455 "Karl Fogel <kfogel@red-bean.com>")
30 ;; ...
31 ;; ))
33 ;; That is, it's an alist map where the key is the "type" of match (so
34 ;; that you can have one list of things for `mail' and one for, say,
35 ;; `twitter'). In each of these sections you then have a list where
36 ;; each item is on the form
38 ;; (KEY TIMES-USED LAST-TIME-USED STRING)
40 ;; If you call `ecomplete-display-matches', it will then display all
41 ;; items that match STRING. KEY is unique and is used to identify the
42 ;; item, and is used for updates. For instance, if given the above
43 ;; data, you call
45 ;; (ecomplete-add-item "larsi@gnus.org" 'mail "Lars Magne Ingebrigtsen <larsi@gnus.org>")
47 ;; the "larsi@gnus.org" entry will then be updated with that new STRING.
49 ;; The interface functions are `ecomplete-add-item' and
50 ;; `ecomplete-display-matches', while `ecomplete-setup' should be
51 ;; called to read the .ecompleterc file, and `ecomplete-save' are
52 ;; called to save the file.
54 ;;; Code:
56 (eval-when-compile (require 'cl-lib))
58 (defgroup ecomplete nil
59 "Electric completion of email addresses and the like."
60 :group 'mail)
62 (defcustom ecomplete-database-file
63 (locate-user-emacs-file "ecompleterc" "~/.ecompleterc")
64 "The name of the file to store the ecomplete data."
65 :type 'file)
67 (defcustom ecomplete-database-file-coding-system 'iso-2022-7bit
68 "Coding system used for writing the ecomplete database file."
69 :type '(symbol :tag "Coding system"))
71 (defcustom ecomplete-sort-predicate 'ecomplete-decay
72 "Predicate to use when sorting matched.
73 The predicate is called with two parameters that represent the
74 completion. Each parameter is a list where the first element is
75 the times the completion has been used, the second is the
76 timestamp of the most recent usage, and the third item is the
77 string that was matched."
78 :type '(radio (function-item :tag "Sort by usage and newness" ecomplete-decay)
79 (function-item :tag "Sort by times used" ecomplete-usage)
80 (function-item :tag "Sort by newness" ecomplete-newness)
81 (function :tag "Other")))
83 ;;; Internal variables.
85 (defvar ecomplete-database nil)
87 ;;;###autoload
88 (defun ecomplete-setup ()
89 "Read the .ecompleterc file."
90 (when (file-exists-p ecomplete-database-file)
91 (with-temp-buffer
92 (let ((coding-system-for-read ecomplete-database-file-coding-system))
93 (insert-file-contents ecomplete-database-file)
94 (setq ecomplete-database (read (current-buffer)))))))
96 (defun ecomplete-add-item (type key text)
97 "Add item TEXT of TYPE to the database, using KEY as the identifier."
98 (let ((elems (assq type ecomplete-database))
99 (now (string-to-number (format-time-string "%s")))
100 entry)
101 (unless elems
102 (push (setq elems (list type)) ecomplete-database))
103 (if (setq entry (assoc key (cdr elems)))
104 (setcdr entry (list (1+ (cadr entry)) now text))
105 (nconc elems (list (list key 1 now text))))))
107 (defun ecomplete-get-item (type key)
108 "Return the text for the item identified by KEY of the required TYPE."
109 (assoc key (cdr (assq type ecomplete-database))))
111 (defun ecomplete-save ()
112 "Write the .ecompleterc file."
113 (with-temp-buffer
114 (let ((coding-system-for-write ecomplete-database-file-coding-system))
115 (insert "(")
116 (cl-loop for (type . elems) in ecomplete-database
118 (insert (format "(%s\n" type))
119 (dolist (entry elems)
120 (prin1 entry (current-buffer))
121 (insert "\n"))
122 (insert ")\n"))
123 (insert ")")
124 (write-region (point-min) (point-max)
125 ecomplete-database-file nil 'silent))))
127 (defun ecomplete-get-matches (type match)
128 (let* ((elems (cdr (assq type ecomplete-database)))
129 (match (regexp-quote match))
130 (candidates
131 (sort
132 (cl-loop for (_key count time text) in elems
133 when (string-match match text)
134 collect (list count time text))
135 ecomplete-sort-predicate)))
136 (when (> (length candidates) 10)
137 (setcdr (nthcdr 10 candidates) nil))
138 (unless (zerop (length candidates))
139 (with-temp-buffer
140 (dolist (candidate candidates)
141 (insert (caddr candidate) "\n"))
142 (goto-char (point-min))
143 (put-text-property (point) (1+ (point)) 'ecomplete t)
144 (while (re-search-forward match nil t)
145 (put-text-property (match-beginning 0) (match-end 0)
146 'face 'isearch))
147 (buffer-string)))))
149 (defun ecomplete-display-matches (type word &optional choose)
150 "Display the top-rated elements TYPE that match WORD.
151 If CHOOSE, allow the user to choose interactively between the
152 matches."
153 (let* ((matches (ecomplete-get-matches type word))
154 (line 0)
155 (max-lines (when matches (- (length (split-string matches "\n")) 2)))
156 (message-log-max nil)
157 command highlight)
158 (if (not matches)
159 (progn
160 (message "No ecomplete matches")
161 nil)
162 (if (not choose)
163 (progn
164 (message "%s" matches)
165 nil)
166 (setq highlight (ecomplete-highlight-match-line matches line))
167 (let ((local-map (make-sparse-keymap))
168 (prev-func (lambda () (setq line (max (1- line) 0))))
169 (next-func (lambda () (setq line (min (1+ line) max-lines))))
170 selected)
171 (define-key local-map (kbd "RET")
172 (lambda () (setq selected (nth line (split-string matches "\n")))))
173 (define-key local-map (kbd "M-n") next-func)
174 (define-key local-map (kbd "<down>") next-func)
175 (define-key local-map (kbd "M-p") prev-func)
176 (define-key local-map (kbd "<up>") prev-func)
177 (let ((overriding-local-map local-map))
178 (while (and (null selected)
179 (setq command (read-key-sequence highlight))
180 (lookup-key local-map command))
181 (apply (key-binding command) nil)
182 (setq highlight (ecomplete-highlight-match-line matches line))))
183 (message (or selected "Abort"))
184 selected)))))
186 (defun ecomplete-highlight-match-line (matches line)
187 (with-temp-buffer
188 (insert matches)
189 (goto-char (point-min))
190 (forward-line line)
191 (save-restriction
192 (narrow-to-region (point) (point-at-eol))
193 (while (not (eobp))
194 ;; Put the 'region face on any characters on this line that
195 ;; aren't already highlighted.
196 (unless (get-text-property (point) 'face)
197 (put-text-property (point) (1+ (point)) 'face 'highlight))
198 (forward-char 1)))
199 (buffer-string)))
201 (defun ecomplete-usage (l1 l2)
202 (> (car l1) (car l2)))
204 (defun ecomplete-newness (l1 l2)
205 (> (cadr l1) (cadr l2)))
207 (defun ecomplete-decay (l1 l2)
208 (> (ecomplete-decay-1 l1) (ecomplete-decay-1 l2)))
210 (defun ecomplete-decay-1 (elem)
211 ;; We subtract 5% from the item for each week it hasn't been used.
212 (/ (car elem)
213 (expt 1.05 (/ (- (float-time) (cadr elem))
214 (* 7 24 60 60)))))
216 ;; `ecomplete-get-matches' uses substring matching, so also use the `substring'
217 ;; style by default.
218 (add-to-list 'completion-category-defaults
219 '(ecomplete (styles basic substring)))
221 (defun ecomplete-completion-table (type)
222 "Return a completion-table suitable for TYPE."
223 (lambda (string pred action)
224 (pcase action
225 (`(boundaries . ,_) nil)
226 ('metadata `(metadata (category . ecomplete)
227 (display-sort-function . ,#'identity)
228 (cycle-sort-function . ,#'identity)))
230 (let* ((elems (cdr (assq type ecomplete-database)))
231 (candidates
232 (mapcar (lambda (x) (nth 2 x))
233 (sort
234 (cl-loop for x in elems
235 when (string-prefix-p string (nth 3 x)
236 completion-ignore-case)
237 collect (cdr x))
238 ecomplete-sort-predicate))))
239 (complete-with-action action candidates string pred))))))
241 (provide 'ecomplete)
243 ;;; ecomplete.el ends here