* ecomplete.el: Implement more bits.
[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 (defvar ecomplete-current-matches nil)
44 (defvar ecomplete-match-length nil)
45 (defvar ecomplete-current-line nil)
46
47 (defun ecomplete-setup ()
48   (when (file-exists-p ecomplete-database-file)
49     (with-temp-buffer
50       (insert-file-contents ecomplete-database-file)
51       (setq ecomplete-database (read (current-buffer))))
52     (save-excursion
53       (loop for (type . elems) in ecomplete-database
54             do (let ((buffer (get-buffer-create
55                               (format " *ecomplete %s*" type))))
56                  (set-buffer buffer)
57                  (erase-buffer)
58                  (loop for (key count time text) in elems
59                        do (insert text "\n")))))))
60
61 (defun ecomplete-add-item (type key text)
62   (let ((elems (assq type ecomplete-database))
63         (now (string-to-number
64               (format "%.0f" (time-to-seconds (current-time)))))
65         entry)
66     (unless elems
67       (push (setq elems (list type)) ecomplete-database))
68     (if (setq entry (assoc key (cdr elems)))
69         (setcdr entry (list (1+ (cadr entry)) now text))
70       (nconc elems (list (list key 1 now text))))))
71
72 (defun ecomplete-get-item (type key)
73   (assoc key (cdr (assq type ecomplete-database))))
74
75 (defun ecomplete-save ()
76   (with-temp-buffer
77     (insert "(")
78     (loop for (type . elems) in ecomplete-database
79           do
80           (insert (format "(%s\n" type))
81           (dolist (entry elems)
82             (prin1 entry (current-buffer))
83             (insert "\n"))
84           (insert ")\n"))
85     (insert ")")
86     (write-region (point-min) (point-max) ecomplete-database-file nil 'silent)))
87
88 (defun ecomplete-show-matches (type match)
89   (let* ((elems (cdr (assq type ecomplete-database)))
90          (match (regexp-quote match))
91          (candidates
92           (sort 
93            (loop for (key count time text) in elems
94                  when (string-match match text)
95                  collect (list count time text))
96            (lambda (l1 l2)
97              (> (car l1) (car l2))))))
98     (when (> (length candidates) 10)
99       (setcdr (nthcdr 10 candidates) nil))
100     (unless (zerop (length candidates))
101       (with-temp-buffer
102         (dolist (candidate candidates)
103           (insert (caddr candidate) "\n"))
104         (goto-char (point-min))
105         (while (re-search-forward match nil t)
106           (put-text-property (match-beginning 0) (match-end 0)
107                              'face 'isearch))
108         (setq ecomplete-current-matches (buffer-string)
109               ecomplete-current-line 0
110               ecomplete-match-length (count-lines (point-min) (point-max)))
111         (ecomplete-highlight-match-line)))))
112
113 (defun ecomplete-up-list ()
114   "Go up the list of matches."
115   (interactive)
116   (when (and ecomplete-current-matches
117              (> ecomplete-current-line 0))
118     (decf ecomplete-current-line)
119     (ecomplete-highlight-match-line)))
120
121 (defun ecomplete-down-list ()
122   "Go down the list of matches."
123   (interactive)
124   (when (and ecomplete-current-matches
125              (< ecomplete-current-line ecomplete-match-length))
126     (incf ecomplete-current-line)
127     (ecomplete-highlight-match-line)))
128
129 (defun ecomplete-highlight-match-line ()
130   (with-temp-buffer
131     (insert ecomplete-current-matches)
132     (goto-char (point-min))
133     (forward-line ecomplete-current-line)
134     (save-restriction
135       (narrow-to-region (point) (line-end-position))
136       (while (not (eobp))
137         ;; Put the 'region face on any charactes on this line that
138         ;; aren't already highlighted.
139         (unless (get-text-property (point) 'face)
140           (put-text-property (point) (1+ (point)) 'face 'region))
141         (forward-char 1)))
142     (message "%s" (buffer-string))))
143
144 (provide 'ecomplete)
145
146 ;;; ecomplete.el ends here