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>
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/>.
30 (defgroup ecomplete nil
31 "Electric completion of email addresses and the like."
34 (defcustom ecomplete-database-file
"~/.ecompleterc"
35 "The name of the file to store the ecomplete data."
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")
44 ;;; Internal variables.
46 (defvar ecomplete-database nil
)
49 (defun ecomplete-setup ()
50 (when (file-exists-p ecomplete-database-file
)
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))))
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 ()
71 (let ((coding-system-for-write ecomplete-database-file-coding-system
))
73 (loop for
(type . elems
) in ecomplete-database
75 (insert (format "(%s\n" type
))
77 (prin1 entry
(current-buffer))
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
))
89 (loop for
(key count time text
) in elems
90 when
(string-match match text
)
91 collect
(list count time text
))
93 (> (car l1
) (car l2
))))))
94 (when (> (length candidates
) 10)
95 (setcdr (nthcdr 10 candidates
) nil
))
96 (unless (zerop (length candidates
))
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)
107 (defun ecomplete-display-matches (type word
&optional choose
)
108 (let* ((matches (ecomplete-get-matches type word
))
110 (max-lines (when matches
(- (length (split-string matches
"\n")) 2)))
111 (message-log-max nil
)
115 (message "No ecomplete matches")
119 (message "%s" matches
)
121 (setq highlight
(ecomplete-highlight-match-line matches line
))
122 (let ((local-map (make-sparse-keymap))
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
))))
141 (defun ecomplete-highlight-match-line (matches line
)
144 (goto-char (point-min))
147 (narrow-to-region (point) (point-at-eol))
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
))
158 ;;; ecomplete.el ends here