Merge from origin/emacs-25
[emacs.git] / lisp / ecomplete.el
blob70277facb0abd51ad0b46a6fd703cd33e0918cf6
1 ;;; ecomplete.el --- electric completion of addresses and the like
3 ;; Copyright (C) 2006-2017 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 <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile
28 (require 'cl))
30 (defgroup ecomplete nil
31 "Electric completion of email addresses and the like."
32 :group 'mail)
34 (defcustom ecomplete-database-file "~/.ecompleterc"
35 "The name of the file to store the ecomplete data."
36 :group 'ecomplete
37 :type 'file)
39 (defcustom ecomplete-database-file-coding-system 'iso-2022-7bit
40 "Coding system used for writing the ecomplete database file."
41 :type '(symbol :tag "Coding system")
42 :group 'ecomplete)
44 ;;; Internal variables.
46 (defvar ecomplete-database nil)
48 ;;;###autoload
49 (defun ecomplete-setup ()
50 (when (file-exists-p ecomplete-database-file)
51 (with-temp-buffer
52 (let ((coding-system-for-read ecomplete-database-file-coding-system))
53 (insert-file-contents ecomplete-database-file)
54 (setq ecomplete-database (read (current-buffer)))))))
56 (defun ecomplete-add-item (type key text)
57 (let ((elems (assq type ecomplete-database))
58 (now (string-to-number (format "%.0f" (float-time))))
59 entry)
60 (unless elems
61 (push (setq elems (list type)) ecomplete-database))
62 (if (setq entry (assoc key (cdr elems)))
63 (setcdr entry (list (1+ (cadr entry)) now text))
64 (nconc elems (list (list key 1 now text))))))
66 (defun ecomplete-get-item (type key)
67 (assoc key (cdr (assq type ecomplete-database))))
69 (defun ecomplete-save ()
70 (with-temp-buffer
71 (let ((coding-system-for-write ecomplete-database-file-coding-system))
72 (insert "(")
73 (loop for (type . elems) in ecomplete-database
75 (insert (format "(%s\n" type))
76 (dolist (entry elems)
77 (prin1 entry (current-buffer))
78 (insert "\n"))
79 (insert ")\n"))
80 (insert ")")
81 (write-region (point-min) (point-max)
82 ecomplete-database-file nil 'silent))))
84 (defun ecomplete-get-matches (type match)
85 (let* ((elems (cdr (assq type ecomplete-database)))
86 (match (regexp-quote match))
87 (candidates
88 (sort
89 (loop for (key count time text) in elems
90 when (string-match match text)
91 collect (list count time text))
92 (lambda (l1 l2)
93 (> (car l1) (car l2))))))
94 (when (> (length candidates) 10)
95 (setcdr (nthcdr 10 candidates) nil))
96 (unless (zerop (length candidates))
97 (with-temp-buffer
98 (dolist (candidate candidates)
99 (insert (caddr candidate) "\n"))
100 (goto-char (point-min))
101 (put-text-property (point) (1+ (point)) 'ecomplete t)
102 (while (re-search-forward match nil t)
103 (put-text-property (match-beginning 0) (match-end 0)
104 'face 'isearch))
105 (buffer-string)))))
107 (defun ecomplete-display-matches (type word &optional choose)
108 (let* ((matches (ecomplete-get-matches type word))
109 (line 0)
110 (max-lines (when matches (- (length (split-string matches "\n")) 2)))
111 (message-log-max nil)
112 command highlight)
113 (if (not matches)
114 (progn
115 (message "No ecomplete matches")
116 nil)
117 (if (not choose)
118 (progn
119 (message "%s" matches)
120 nil)
121 (setq highlight (ecomplete-highlight-match-line matches line))
122 (let ((local-map (make-sparse-keymap))
123 selected)
124 (define-key local-map (kbd "RET")
125 (lambda () (setq selected (nth line (split-string matches "\n")))))
126 (define-key local-map (kbd "M-n")
127 (lambda () (setq line (min (1+ line) max-lines))))
128 (define-key local-map (kbd "M-p")
129 (lambda () (setq line (max (1- line) 0))))
130 (let ((overriding-local-map local-map))
131 (while (and (null selected)
132 (setq command (read-key-sequence highlight))
133 (lookup-key local-map command))
134 (apply (key-binding command) nil)
135 (setq highlight (ecomplete-highlight-match-line matches line))))
136 (if selected
137 (message selected)
138 (message "Abort"))
139 selected)))))
141 (defun ecomplete-highlight-match-line (matches line)
142 (with-temp-buffer
143 (insert matches)
144 (goto-char (point-min))
145 (forward-line line)
146 (save-restriction
147 (narrow-to-region (point) (point-at-eol))
148 (while (not (eobp))
149 ;; Put the 'region face on any characters on this line that
150 ;; aren't already highlighted.
151 (unless (get-text-property (point) 'face)
152 (put-text-property (point) (1+ (point)) 'face 'highlight))
153 (forward-char 1)))
154 (buffer-string)))
156 (provide 'ecomplete)
158 ;;; ecomplete.el ends here