(gnus-picon-find-face): Don't search "news" for MISC.
[gnus] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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 (require 'gnus)
42 (require 'custom)
43 (require 'gnus-art)
44
45 ;;; User variables:
46
47 (defgroup picon nil
48   "Show pictures of people, domains, and newsgroups."
49   :group 'gnus-visual)
50
51 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces")
52   "*Defines the location of the faces database.
53 For information on obtaining this database of pretty pictures, please
54 see http://www.cs.indiana.edu/picons/ftp/index.html"
55   :type 'directory
56   :group 'picon)
57
58 (defcustom gnus-picon-news-directories '("news")
59   "*List of directories to search for newsgroups faces."
60   :type '(repeat string)
61   :group 'picon)
62
63 (defcustom gnus-picon-user-directories '("users" "usenix" "local"
64                                          "misc" "unknown")
65   "*List of directories to search for user faces."
66   :type '(repeat string)
67   :group 'picon)
68
69 (defcustom gnus-picon-domain-directories '("domains")
70   "*List of directories to search for domain faces.
71 Some people may want to add \"unknown\" to this list."
72   :type '(repeat string)
73   :group 'picon)
74
75 (defcustom gnus-picon-file-types
76   (let ((types (list "xbm")))
77     (when (gnus-image-type-available-p 'gif)
78       (push "gif" types))
79     (when (gnus-image-type-available-p 'xpm)
80       (push "xpm" types))
81     types)
82   "*List of suffixes on picon file names to try."
83   :type '(repeat string)
84   :group 'picon)
85
86 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
87   "Face to show xbm picon in."
88   :group 'picon)
89
90 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
91   "Face to show picon in."
92   :group 'picon)
93
94 ;;; Internal variables:
95
96 (defvar gnus-picon-setup-p nil)
97 (defvar gnus-picon-glyph-alist nil
98   "Picon glyphs cache.
99 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
100
101 ;;; Functions:
102
103 (defsubst gnus-picon-split-address (address)
104   (setq address (split-string address "@"))
105   (if (stringp (cadr address))
106       (cons (car address) (split-string (cadr address) "\\."))
107     (if (stringp (car address))
108         (split-string (car address) "\\."))))
109
110 (defun gnus-picon-find-face (address directories &optional exact)
111   (let* ((address (gnus-picon-split-address address))
112          (user (pop address))
113          (faddress address)
114          database directory result instance base)
115     (catch 'found
116       (dolist (database gnus-picon-databases)
117         (dolist (directory directories)
118           (setq address faddress
119                 base (expand-file-name directory database))
120           (while address
121             (when (setq result (gnus-picon-find-image
122                                 (concat base "/" (mapconcat 'identity
123                                                             (reverse address)
124                                                             "/")
125                                         "/" user "/")))
126               (throw 'found result))
127             (if exact
128                 (setq address nil)
129               (pop address)))
130           ;; Kludge to search MISC as well.  But not in "news".
131           (unless (string= directory "news")
132             (when (setq result (gnus-picon-find-image
133                                 (concat base "/MISC/" user "/")))
134               (throw 'found result))))))))
135
136 (defun gnus-picon-find-image (directory)
137   (let ((types gnus-picon-file-types)
138         found type file)
139     (while (and (not found)
140                 (setq type (pop types)))
141       (setq found (file-exists-p (setq file (concat directory "face." type)))))
142     (if found
143         file
144       nil)))
145
146 (defun gnus-picon-insert-glyph (glyph category)
147   "Insert GLYPH into the buffer.
148 GLYPH can be either a glyph or a string."
149   (if (stringp glyph)
150       (insert glyph)
151     (gnus-add-wash-type category)
152     (gnus-add-image category (car glyph))
153     (gnus-put-image (car glyph) (cdr glyph))))
154
155 (defun gnus-picon-create-glyph (file)
156   (or (cdr (assoc file gnus-picon-glyph-alist))
157       (cdar (push (cons file (gnus-create-image file))
158                   gnus-picon-glyph-alist))))
159
160 ;;; Functions that does picon transformations:
161
162 (defun gnus-picon-transform-address (header category)
163   (gnus-with-article-headers
164     (let ((addresses
165            (mail-header-parse-addresses (mail-fetch-field header)))
166           first spec file)
167       (dolist (address addresses)
168         (setq address (car address)
169               first t)
170         (when (and (stringp address)
171                    (setq spec (gnus-picon-split-address address)))
172           (when (setq file (or (gnus-picon-find-face
173                                 address gnus-picon-user-directories)
174                                (gnus-picon-find-face
175                                 (concat "unknown@"
176                                         (mapconcat
177                                          'identity (cdr spec) "."))
178                                 gnus-picon-user-directories)))
179             (setcar spec (cons (gnus-picon-create-glyph file)
180                                (car spec))))
181               
182           (dotimes (i (1- (length spec)))
183             (when (setq file (gnus-picon-find-face
184                               (concat "unknown@"
185                                       (mapconcat
186                                        'identity (nthcdr (1+ i) spec) "."))
187                               gnus-picon-domain-directories t))
188               (setcar (nthcdr (1+ i) spec)
189                       (cons (gnus-picon-create-glyph file)
190                             (nth (1+ i) spec)))))
191           
192           (gnus-article-goto-header header)
193           (mail-header-narrow-to-field)
194           (when (search-forward address nil t)
195             (delete-region (match-beginning 0) (match-end 0))
196             (while spec
197               (gnus-picon-insert-glyph (pop spec) category)
198               (when spec
199                 (if (not first)
200                     (insert ".")
201                   (insert "@")
202                   (setq first nil))))))))))
203
204 (defun gnus-picon-transform-newsgroups (header)
205   (interactive)
206   (gnus-with-article-headers
207     (let ((groups
208            (sort
209             (message-tokenize-header (mail-fetch-field header))
210             (lambda (g1 g2) (> (length g1) (length g2)))))
211           spec file)
212       (dolist (group groups)
213         (setq spec (nreverse (split-string group "[.]")))
214         (dotimes (i (length spec))
215           (when (setq file (gnus-picon-find-face
216                             (concat "unknown@"
217                                     (mapconcat
218                                      'identity (nthcdr i spec) "."))
219                             gnus-picon-news-directories t))
220             (setcar (nthcdr i spec)
221                     (cons (gnus-picon-create-glyph file)
222                           (nth i spec)))))
223         
224         (gnus-article-goto-header header)
225         (mail-header-narrow-to-field)
226         (when (search-forward group nil t)
227           (delete-region (match-beginning 0) (match-end 0))
228           (setq spec (nreverse spec))
229           (while spec
230             (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon)
231             (when spec
232               (insert "."))))))))
233
234 ;;; Commands:
235
236 ;;;###autoload
237 (defun gnus-treat-from-picon ()
238   "Display picons in the From header.
239 If picons are already displayed, remove them."
240   (interactive)
241   (gnus-with-article-buffer
242     (if (memq 'from-picon gnus-article-wash-types)
243         (gnus-delete-images 'from-picon)
244       (gnus-picon-transform-address "from" 'from-picon))))
245
246 ;;;###autoload
247 (defun gnus-treat-mail-picon ()
248   "Display picons in the Cc and To headers.
249 If picons are already displayed, remove them."
250   (interactive)
251   (gnus-with-article-buffer
252     (if (memq 'mail-picon gnus-article-wash-types)
253         (gnus-delete-images 'mail-picon)
254       (gnus-picon-transform-address "cc" 'mail-picon)
255       (gnus-picon-transform-address "to" 'mail-picon))))
256
257 ;;;###autoload
258 (defun gnus-treat-newsgroups-picon ()
259   "Display picons in the Newsgroups and Followup-To headers.
260 If picons are already displayed, remove them."
261   (interactive)
262   (gnus-with-article-buffer
263     (if (memq 'newsgroups-picon gnus-article-wash-types)
264         (gnus-delete-images 'newsgroups-picon)
265       (gnus-picon-transform-newsgroups "newsgroups")
266       (gnus-picon-transform-newsgroups "followup-to"))))
267
268 (provide 'gnus-picon)
269
270 ;;; gnus-picon.el ends here