Merge from emacs--devo--0
[gnus] / lisp / ecomplete.el
1 ;;; ecomplete.el --- electric completion of addresses and the like
2
3 ;; Copyright (C) 2006, 2007, 2008  Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile
28   (require 'cl))
29
30 (defgroup ecomplete nil
31   "Electric completion of email addresses and the like."
32   :group 'mail)
33
34 (defcustom ecomplete-database-file "~/.ecompleterc"
35   "*The name of the file to store the ecomplete data."
36   :group 'ecomplete
37   :type 'file)
38
39 (defcustom ecomplete-database-file-coding-system 'iso-2022-7bit
40   "Coding system used for writing the ecomplete database file."
41   :type '(symbol :tag "Coding system")
42   :group 'ecomplete)
43
44 ;;; Internal variables.
45
46 (defvar ecomplete-database nil)
47
48 ;;;###autoload
49 (defun ecomplete-setup ()
50   (when (file-exists-p ecomplete-database-file)
51     (with-temp-buffer
52       (let ((coding-system-for-read ecomplete-database-file-coding-system))
53         (insert-file-contents ecomplete-database-file)
54         (setq ecomplete-database (read (current-buffer)))))))
55
56 (defun ecomplete-add-item (type key text)
57   (let ((elems (assq type ecomplete-database))
58         (now (string-to-number
59               (format "%.0f" (time-to-seconds (current-time)))))
60         entry)
61     (unless elems
62       (push (setq elems (list type)) ecomplete-database))
63     (if (setq entry (assoc key (cdr elems)))
64         (setcdr entry (list (1+ (cadr entry)) now text))
65       (nconc elems (list (list key 1 now text))))))
66
67 (defun ecomplete-get-item (type key)
68   (assoc key (cdr (assq type ecomplete-database))))
69
70 (defun ecomplete-save ()
71   (with-temp-buffer
72     (let ((coding-system-for-write ecomplete-database-file-coding-system))
73       (insert "(")
74       (loop for (type . elems) in ecomplete-database
75             do
76             (insert (format "(%s\n" type))
77             (dolist (entry elems)
78               (prin1 entry (current-buffer))
79               (insert "\n"))
80             (insert ")\n"))
81       (insert ")")
82       (write-region (point-min) (point-max)
83                     ecomplete-database-file nil 'silent))))
84
85 (defun ecomplete-get-matches (type match)
86   (let* ((elems (cdr (assq type ecomplete-database)))
87          (match (regexp-quote match))
88          (candidates
89           (sort 
90            (loop for (key count time text) in elems
91                  when (string-match match text)
92                  collect (list count time text))
93            (lambda (l1 l2)
94              (> (car l1) (car l2))))))
95     (when (> (length candidates) 10)
96       (setcdr (nthcdr 10 candidates) nil))
97     (unless (zerop (length candidates))
98       (with-temp-buffer
99         (dolist (candidate candidates)
100           (insert (caddr candidate) "\n"))
101         (goto-char (point-min))
102         (put-text-property (point) (1+ (point)) 'ecomplete t)
103         (while (re-search-forward match nil t)
104           (put-text-property (match-beginning 0) (match-end 0)
105                              'face 'isearch))
106         (buffer-string)))))
107
108 (defun ecomplete-display-matches (type word &optional choose)
109   (let* ((matches (ecomplete-get-matches type word))
110          (line 0)
111          (max-lines (when matches (- (length (split-string matches "\n")) 2)))
112          (message-log-max nil)
113          command highlight)
114     (if (not matches)
115         (progn
116           (message "No ecomplete matches")
117           nil)
118       (if (not choose)
119           (progn
120             (message "%s" matches)
121             nil)
122         (setq highlight (ecomplete-highlight-match-line matches line))
123         (while (not (memq (setq command (read-event highlight)) '(? return)))
124           (cond
125            ((eq command ?\M-n)
126             (setq line (min (1+ line) max-lines)))
127            ((eq command ?\M-p)
128             (setq line (max (1- line) 0))))
129           (setq highlight (ecomplete-highlight-match-line matches line)))
130         (when (eq command 'return)
131           (nth line (split-string matches "\n")))))))
132
133 (defun ecomplete-highlight-match-line (matches line)
134   (with-temp-buffer
135     (insert matches)
136     (goto-char (point-min))
137     (forward-line line)
138     (save-restriction
139       (narrow-to-region (point) (point-at-eol))
140       (while (not (eobp))
141         ;; Put the 'region face on any charactes on this line that
142         ;; aren't already highlighted.
143         (unless (get-text-property (point) 'face)
144           (put-text-property (point) (1+ (point)) 'face 'highlight))
145         (forward-char 1)))
146     (buffer-string)))
147
148 (provide 'ecomplete)
149
150 ;; arch-tag: 34622935-bb81-4711-a600-57b89c2ece72
151 ;;; ecomplete.el ends here