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