1 ;;; ecomplete.el --- electric completion of addresses and the like
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (defgroup ecomplete nil
32 "Electric completion of email addresses and the like."
35 (defcustom ecomplete-database-file
"~/.ecompleterc"
36 "*The name of the file to store the ecomplete data."
40 (defcustom ecomplete-database-file-coding-system
'iso-2022-7bit
41 "Coding system used for writing the ecomplete database file."
42 :type
'(symbol :tag
"Coding system")
45 ;;; Internal variables.
47 (defvar ecomplete-database nil
)
50 (defun ecomplete-setup ()
51 (when (file-exists-p ecomplete-database-file
)
53 (let ((coding-system-for-read ecomplete-database-file-coding-system
))
54 (insert-file-contents ecomplete-database-file
)
55 (setq ecomplete-database
(read (current-buffer)))))))
57 (defun ecomplete-add-item (type key text
)
58 (let ((elems (assq type ecomplete-database
))
59 (now (string-to-number
60 (format "%.0f" (if (featurep 'emacs
)
66 (push (setq elems
(list type
)) ecomplete-database
))
67 (if (setq entry
(assoc key
(cdr elems
)))
68 (setcdr entry
(list (1+ (cadr entry
)) now text
))
69 (nconc elems
(list (list key
1 now text
))))))
71 (defun ecomplete-get-item (type key
)
72 (assoc key
(cdr (assq type ecomplete-database
))))
74 (defun ecomplete-save ()
76 (let ((coding-system-for-write ecomplete-database-file-coding-system
))
78 (loop for
(type . elems
) in ecomplete-database
80 (insert (format "(%s\n" type
))
82 (prin1 entry
(current-buffer))
86 (write-region (point-min) (point-max)
87 ecomplete-database-file nil
'silent
))))
89 (defun ecomplete-get-matches (type match
)
90 (let* ((elems (cdr (assq type ecomplete-database
)))
91 (match (regexp-quote match
))
94 (loop for
(key count time text
) in elems
95 when
(string-match match text
)
96 collect
(list count time text
))
98 (> (car l1
) (car l2
))))))
99 (when (> (length candidates
) 10)
100 (setcdr (nthcdr 10 candidates
) nil
))
101 (unless (zerop (length candidates
))
103 (dolist (candidate candidates
)
104 (insert (caddr candidate
) "\n"))
105 (goto-char (point-min))
106 (put-text-property (point) (1+ (point)) 'ecomplete t
)
107 (while (re-search-forward match nil t
)
108 (put-text-property (match-beginning 0) (match-end 0)
112 (defun ecomplete-display-matches (type word
&optional choose
)
113 (let* ((matches (ecomplete-get-matches type word
))
115 (max-lines (when matches
(- (length (split-string matches
"\n")) 2)))
116 (message-log-max nil
)
120 (message "No ecomplete matches")
124 (message "%s" matches
)
126 (setq highlight
(ecomplete-highlight-match-line matches line
))
127 (while (not (memq (setq command
(read-event highlight
)) '(? return
)))
130 (setq line
(min (1+ line
) max-lines
)))
132 (setq line
(max (1- line
) 0))))
133 (setq highlight
(ecomplete-highlight-match-line matches line
)))
134 (when (eq command
'return
)
135 (nth line
(split-string matches
"\n")))))))
137 (defun ecomplete-highlight-match-line (matches line
)
140 (goto-char (point-min))
143 (narrow-to-region (point) (point-at-eol))
145 ;; Put the 'region face on any characters on this line that
146 ;; aren't already highlighted.
147 (unless (get-text-property (point) 'face
)
148 (put-text-property (point) (1+ (point)) 'face
'highlight
))
154 ;;; ecomplete.el ends here