* lisp/dgnushack.el: Make sure `dolist' is available.
[elisp/gnus.git-] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
7 ;; Keywords: news xpm annotation glyph faces
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; There are three picon types relevant to Gnus:
29 ;;
30 ;; Persons: person@subdomain.dom
31 ;;          users/dom/subdomain/person/face.gif
32 ;;          usenix/dom/subdomain/person/face.gif
33 ;;          misc/MISC/person/face.gif
34 ;; Domains: subdomain.dom
35 ;;          domain/dom/subdomain/unknown/face.gif
36 ;; Groups:  comp.lang.lisp
37 ;;          news/comp/lang/lisp/unknown/face.gif
38
39 ;;; Code:
40
41 (eval-when-compile (require 'cl))
42
43 (require 'gnus)
44 (require 'custom)
45 (require 'gnus-art)
46
47 ;;; User variables:
48
49 (defcustom gnus-picon-news-directories '("news")
50   "*List of directories to search for newsgroups faces."
51   :type '(repeat string)
52   :group 'gnus-picon)
53
54 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
55   "*List of directories to search for user faces."
56   :type '(repeat string)
57   :group 'gnus-picon)
58
59 (defcustom gnus-picon-domain-directories '("domains")
60   "*List of directories to search for domain faces.
61 Some people may want to add \"unknown\" to this list."
62   :type '(repeat string)
63   :group 'gnus-picon)
64
65 (defcustom gnus-picon-file-types
66   (let ((types (list "xbm")))
67     (if (gnus-image-type-available-p 'gif)
68         (setq types (cons "gif" types)))
69     (if (gnus-image-type-available-p 'xpm)
70         (setq types (cons "xpm" types)))
71     types)
72   "*List of suffixes on picon file names to try."
73   :type '(repeat string)
74   :group 'gnus-picon)
75
76 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
77   "Face to show xbm picon in."
78   :group 'gnus-picon)
79
80 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
81   "Face to show picon in."
82   :group 'gnus-picon)
83
84 ;;; Internal variables:
85
86 (defvar gnus-picon-setup-p nil)
87 (defvar gnus-picon-glyph-alist nil
88   "Picon glyphs cache.
89 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
90 (defvar gnus-picon-cache nil)
91
92 ;;; Functions:
93
94 (defsubst gnus-picon-split-address (address)
95   (setq address (split-string address "@"))
96   (if (stringp (cadr address))
97       (cons (car address) (split-string (cadr address) "\\."))
98     (if (stringp (car address))
99         (split-string (car address) "\\."))))
100
101 (defun gnus-picon-find-face (address directories &optional exact)
102   (let* ((address (gnus-picon-split-address address))
103          (user (pop address))
104          (faddress address)
105          database directory result instance base)
106     (catch 'found
107       (dolist (database gnus-picon-databases)
108         (dolist (directory directories)
109           (setq address faddress
110                 base (expand-file-name directory database))
111           (while address
112             (when (setq result (gnus-picon-find-image
113                                 (concat base "/" (mapconcat 'downcase
114                                                             (reverse address)
115                                                             "/")
116                                         "/" (downcase user) "/")))
117               (throw 'found result))
118             (if exact
119                 (setq address nil)
120               (pop address)))
121           ;; Kludge to search MISC as well.  But not in "news".
122           (unless (string= directory "news")
123             (when (setq result (gnus-picon-find-image
124                                 (concat base "/MISC/" user "/")))
125               (throw 'found result))))))))
126
127 (defun gnus-picon-find-image (directory)
128   (let ((types gnus-picon-file-types)
129         found type file)
130     (while (and (not found)
131                 (setq type (pop types)))
132       (setq found (file-exists-p (setq file (concat directory "face." type)))))
133     (if found
134         file
135       nil)))
136
137 (defun gnus-picon-insert-glyph (glyph category)
138   "Insert GLYPH into the buffer.
139 GLYPH can be either a glyph or a string."
140   (if (stringp glyph)
141       (insert glyph)
142     (gnus-add-wash-type category)
143     (gnus-add-image category (car glyph))
144     (gnus-put-image (car glyph) (cdr glyph))))
145
146 (defun gnus-picon-create-glyph (file)
147   (or (cdr (assoc file gnus-picon-glyph-alist))
148       (cdar (push (cons file (gnus-create-image file))
149                   gnus-picon-glyph-alist))))
150
151 ;;; Functions that does picon transformations:
152
153 (defun gnus-picon-transform-address (header category)
154   (gnus-with-article-headers
155     (let ((addresses
156            (mail-header-parse-addresses (mail-fetch-field header)))
157           spec file point cache)
158       (dolist (address addresses)
159         (setq address (car address))
160         (when (and (stringp address)
161                    (setq spec (gnus-picon-split-address address)))
162           (if (setq cache (cdr (assoc address gnus-picon-cache)))
163               (setq spec cache)
164             (when (setq file (or (gnus-picon-find-face
165                                   address gnus-picon-user-directories)
166                                  (gnus-picon-find-face
167                                   (concat "unknown@"
168                                           (mapconcat
169                                            'identity (cdr spec) "."))
170                                   gnus-picon-user-directories)))
171               (setcar spec (cons (gnus-picon-create-glyph file)
172                                  (car spec))))
173
174             (dotimes (i (1- (length spec)))
175               (when (setq file (gnus-picon-find-face
176                                 (concat "unknown@"
177                                         (mapconcat
178                                          'identity (nthcdr (1+ i) spec) "."))
179                                 gnus-picon-domain-directories t))
180                 (setcar (nthcdr (1+ i) spec)
181                         (cons (gnus-picon-create-glyph file)
182                               (nth (1+ i) spec)))))
183             (setq spec (nreverse spec))
184             (push (cons address spec) gnus-picon-cache))
185
186           (gnus-article-goto-header header)
187           (mail-header-narrow-to-field)
188           (when (search-forward address nil t)
189             (delete-region (match-beginning 0) (match-end 0))
190             (setq point (point))
191             (while spec
192               (goto-char point)
193               (if (> (length spec) 2)
194                   (insert ".")
195                 (if (= (length spec) 2)
196                   (insert "@")))
197               (gnus-picon-insert-glyph (pop spec) category))))))))
198
199 (defun gnus-picon-transform-newsgroups (header)
200   (interactive)
201   (gnus-with-article-headers
202     (gnus-article-goto-header header)
203     (mail-header-narrow-to-field)
204     (let ((groups (message-tokenize-header (mail-fetch-field header)))
205           spec file point)
206       (dolist (group groups)
207         (unless (setq spec (cdr (assoc group gnus-picon-cache)))
208           (setq spec (nreverse (split-string group "[.]")))
209           (dotimes (i (length spec))
210             (when (setq file (gnus-picon-find-face
211                               (concat "unknown@"
212                                       (mapconcat
213                                        'identity (nthcdr i spec) "."))
214                               gnus-picon-news-directories t))
215               (setcar (nthcdr i spec)
216                       (cons (gnus-picon-create-glyph file)
217                             (nth i spec)))))
218             (push (cons group spec) gnus-picon-cache))
219         (when (search-forward group nil t)
220           (delete-region (match-beginning 0) (match-end 0))
221           (save-restriction
222             (narrow-to-region (point) (point))
223             (while spec
224               (goto-char (point-min))
225               (if (> (length spec) 1)
226                   (insert "."))
227               (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
228             (goto-char (point-max))))))))
229
230 ;;; Commands:
231
232 ;;;###autoload
233 (defun gnus-treat-from-picon ()
234   "Display picons in the From header.
235 If picons are already displayed, remove them."
236   (interactive)
237   (gnus-with-article-buffer
238     (if (memq 'from-picon gnus-article-wash-types)
239         (gnus-delete-images 'from-picon)
240       (gnus-picon-transform-address "from" 'from-picon))))
241
242 ;;;###autoload
243 (defun gnus-treat-mail-picon ()
244   "Display picons in the Cc and To headers.
245 If picons are already displayed, remove them."
246   (interactive)
247   (gnus-with-article-buffer
248     (if (memq 'mail-picon gnus-article-wash-types)
249         (gnus-delete-images 'mail-picon)
250       (gnus-picon-transform-address "cc" 'mail-picon)
251       (gnus-picon-transform-address "to" 'mail-picon))))
252
253 ;;;###autoload
254 (defun gnus-treat-newsgroups-picon ()
255   "Display picons in the Newsgroups and Followup-To headers.
256 If picons are already displayed, remove them."
257   (interactive)
258   (gnus-with-article-buffer
259     (if (memq 'newsgroups-picon gnus-article-wash-types)
260         (gnus-delete-images 'newsgroups-picon)
261       (gnus-picon-transform-newsgroups "newsgroups")
262       (gnus-picon-transform-newsgroups "followup-to"))))
263
264 (provide 'gnus-picon)
265
266 ;;; gnus-picon.el ends here