f6d5b4fdff4db6c02565e90704ea38fd83a8bf77
[gnus] / lisp / ecomplete.el
1 ;;; ecomplete.el --- electric completion of addresses and the like
2 ;; Copyright (C) 2006 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: mail
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29   (require 'cl))
30
31 (defgroup ecomplete nil
32   "Suppression of duplicate articles."
33   :group 'mail)
34
35 (defcustom ecomplete-database-file "~/.ecompleterc"
36   "*The name of the file to store the ecomplete data."
37   :group 'ecomplete
38   :type 'file)
39
40 ;;; Internal variables.
41
42 (defvar ecomplete-database nil)
43
44 (defun ecomplete-setup ()
45   (when (file-exists-p ecomplete-database-file)
46     (with-temp-buffer
47       (insert-file-contents ecomplete-database-file)
48       (setq ecomplete-database (read (current-buffer))))
49     (save-excursion
50       (loop for (type . elems) in ecomplete-database
51             do (let ((buffer (get-buffer-create
52                               (format " *ecomplete %s*" type))))
53                  (set-buffer buffer)
54                  (erase-buffer)
55                  (loop for (key count time text) in elems
56                        do (insert text "\n")))))))
57
58 (defun ecomplete-add-item (type key text)
59   (let ((elems (assq type ecomplete-database))
60         (now (string-to-number
61               (format "%.0f" (time-to-seconds (current-time)))))
62         entry)
63     (unless elems
64       (push (setq elems (list type)) ecomplete-database))
65     (if (setq entry (assoc key (cdr elems)))
66         (setcdr entry (list (1+ (cadr entry)) now text))
67       (nconc elems (list (list key 1 now text))))))
68
69 (defun ecomplete-get-item (type key)
70   (assoc key (cdr (assq type ecomplete-database))))
71
72 (defun ecomplete-save ()
73   (with-temp-buffer
74     (insert "(")
75     (loop for (type . elems) in ecomplete-database
76           do
77           (insert (format "(%s\n" type))
78           (dolist (entry elems)
79             (prin1 entry (current-buffer))
80             (insert "\n"))
81           (insert ")\n"))
82     (insert ")")
83     (write-region (point-min) (point-max) ecomplete-database-file nil 'silent)))
84
85 (provide 'ecomplete)
86
87 ;;; ecomplete.el ends here