1 ;;; ecomplete.el --- electric completion of addresses and the like
2 ;; Copyright (C) 2006 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
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" (time-to-seconds (current-time)))))
63 (push (setq elems (list type)) ecomplete-database))
64 (if (setq entry (assoc key (cdr elems)))
65 (setcdr entry (list (1+ (cadr entry)) now text))
66 (nconc elems (list (list key 1 now text))))))
68 (defun ecomplete-get-item (type key)
69 (assoc key (cdr (assq type ecomplete-database))))
71 (defun ecomplete-save ()
73 (let ((coding-system-for-write ecomplete-database-file-coding-system))
75 (loop for (type . elems) in ecomplete-database
77 (insert (format "(%s\n" type))
79 (prin1 entry (current-buffer))
83 (write-region (point-min) (point-max)
84 ecomplete-database-file nil 'silent))))
86 (defun ecomplete-get-matches (type match)
87 (let* ((elems (cdr (assq type ecomplete-database)))
88 (match (regexp-quote match))