Feedback from `t-gnus-6_15-quimby' branch.
[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
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 (defgroup picon nil
50   "Show pictures of people, domains, and newsgroups."
51   :group 'gnus-visual)
52
53 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces")
54   "*Defines the location of the faces database.
55 For information on obtaining this database of pretty pictures, please
56 see http://www.cs.indiana.edu/picons/ftp/index.html"
57   :type 'directory
58   :group 'picon)
59
60 (defcustom gnus-picon-news-directories '("news")
61   "*List of directories to search for newsgroups faces."
62   :type '(repeat string)
63   :group 'picon)
64
65 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
66   "*List of directories to search for user faces."
67   :type '(repeat string)
68   :group 'picon)
69
70 (defcustom gnus-picon-domain-directories '("domains")
71   "*List of directories to search for domain faces.
72 Some people may want to add \"unknown\" to this list."
73   :type '(repeat string)
74   :group 'picon)
75
76 (defcustom gnus-picon-file-types
77   (let ((types (list "xbm")))
78     (if (gnus-image-type-available-p 'gif)
79         (setq types (cons "gif" types)))
80     (if (gnus-image-type-available-p 'xpm)
81         (setq types (cons "xpm" types)))
82     types)
83   "*List of suffixes on picon file names to try."
84   :type '(repeat string)
85   :group 'picon)
86
87 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
88   "Face to show xbm picon in."
89   :group 'picon)
90
91 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
92   "Face to show picon in."
93   :group 'picon)
94
95 ;;; Internal variables:
96
97 (defvar gnus-picon-setup-p nil)
98 (defvar gnus-picon-glyph-alist nil
99   "Picon glyphs cache.
100 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
101
102 ;;; Functions:
103
104 (defsubst gnus-picon-split-address (address)
105   (setq address (split-string address "@"))
106   (if (stringp (cadr address))
107       (cons (car address) (split-string (cadr address) "\\."))
108     (if (stringp (car address))
109         (split-string (car address) "\\."))))
110
111 (defun gnus-picon-find-face (address directories &optional exact)
112   (let* ((databases gnus-picon-databases)
113          (address (gnus-picon-split-address address))
114          (user (pop address))
115          database directory found instance base)
116     (while (and (not found)
117                 (setq database (pop databases)))
118       (while (and (not found)
119                   (setq directory (pop directories)))
120         (setq base (expand-file-name directory database))
121         ;; Kludge to search misc/MISC for users.
122         (when (string= directory "misc")
123           (setq address '("MISC")))
124         (while (and (not found)
125                     address)
126           (setq found (gnus-picon-find-image
127                        (concat base "/" (mapconcat 'identity
128                                                    (reverse address)
129                                                    "/")
130                                "/" user "/")))
131           (if exact
132               (setq address nil)
133             (pop address)))))
134     found))
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 (gnus-picon-find-face
173                             address gnus-picon-user-directories))
174             (setcar spec (cons (gnus-picon-create-glyph file)
175                                (car spec))))
176           (dotimes (i (1- (length spec)))
177             (when (setq file (gnus-picon-find-face
178                               (concat "unknown@"
179                                       (mapconcat
180                                        'identity (nthcdr (1+ i) spec) "."))
181                               gnus-picon-domain-directories t))
182               (setcar (nthcdr (1+ i) spec)
183                       (cons (gnus-picon-create-glyph file)
184                             (nth (1+ i) spec)))))
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             (while spec
191               (gnus-picon-insert-glyph (pop spec) category)
192               (when spec
193                 (if (not first)
194                     (insert ".")
195                   (insert "@")
196                   (setq first nil))))))))))
197
198 (defun gnus-picon-transform-newsgroups (header)
199   (interactive)
200   (gnus-with-article-headers
201     (let ((groups
202            (sort
203             (message-tokenize-header (mail-fetch-field header))
204             (lambda (g1 g2) (> (length g1) (length g2)))))
205           spec file)
206       (dolist (group groups)
207         (setq spec (nreverse (split-string group "[.]")))
208         (dotimes (i (length spec))
209           (when (setq file (gnus-picon-find-face
210                             (concat "unknown@"
211                                     (mapconcat
212                                      'identity (nthcdr i spec) "."))
213                             gnus-picon-news-directories t))
214             (setcar (nthcdr i spec)
215                     (cons (gnus-picon-create-glyph file)
216                           (nth i spec)))))
217
218         (gnus-article-goto-header header)
219         (mail-header-narrow-to-field)
220         (when (search-forward group nil t)
221           (delete-region (match-beginning 0) (match-end 0))
222           (setq spec (nreverse spec))
223           (while spec
224             (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon)
225             (when spec
226               (insert "."))))))))
227
228 ;;; Commands:
229
230 ;;;###autoload
231 (defun gnus-treat-from-picon ()
232   "Display picons in the From header.
233 If picons are already displayed, remove them."
234   (interactive)
235   (gnus-with-article-buffer
236     (if (memq 'from-picon gnus-article-wash-types)
237         (gnus-delete-images 'from-picon)
238       (gnus-picon-transform-address "from" 'from-picon))))
239
240 ;;;###autoload
241 (defun gnus-treat-mail-picon ()
242   "Display picons in the Cc and To headers.
243 If picons are already displayed, remove them."
244   (interactive)
245   (gnus-with-article-buffer
246     (if (memq 'mail-picon gnus-article-wash-types)
247         (gnus-delete-images 'mail-picon)
248       (gnus-picon-transform-address "cc" 'mail-picon)
249       (gnus-picon-transform-address "to" 'mail-picon))))
250
251 ;;;###autoload
252 (defun gnus-treat-newsgroups-picon ()
253   "Display picons in the Newsgroups and Followup-To headers.
254 If picons are already displayed, remove them."
255   (interactive)
256   (gnus-with-article-buffer
257     (if (memq 'newsgroups-picon gnus-article-wash-types)
258         (gnus-delete-images 'newsgroups-picon)
259       (gnus-picon-transform-newsgroups "newsgroups")
260       (gnus-picon-transform-newsgroups "followup-to"))))
261
262 (provide 'gnus-picon)
263
264 ;;; gnus-picon.el ends here