Importing Oort Gnus v0.05.
[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 (require 'gnus)
42 (require 'custom)
43 (require 'gnus-art)
44
45 ;;; User variables:
46
47 (defcustom gnus-picon-news-directories '("news")
48   "*List of directories to search for newsgroups faces."
49   :type '(repeat string)
50   :group 'gnus-picon)
51
52 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
53   "*List of directories to search for user faces."
54   :type '(repeat string)
55   :group 'gnus-picon)
56
57 (defcustom gnus-picon-domain-directories '("domains")
58   "*List of directories to search for domain faces.
59 Some people may want to add \"unknown\" to this list."
60   :type '(repeat string)
61   :group 'gnus-picon)
62
63 (defcustom gnus-picon-file-types
64   (let ((types (list "xbm")))
65     (when (gnus-image-type-available-p 'gif)
66       (push "gif" types))
67     (when (gnus-image-type-available-p 'xpm)
68       (push "xpm" types))
69     types)
70   "*List of suffixes on picon file names to try."
71   :type '(repeat string)
72   :group 'gnus-picon)
73
74 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
75   "Face to show xbm picon in."
76   :group 'gnus-picon)
77
78 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
79   "Face to show picon in."
80   :group 'gnus-picon)
81
82 ;;; Internal variables:
83
84 (defvar gnus-picon-setup-p nil)
85 (defvar gnus-picon-glyph-alist nil
86   "Picon glyphs cache.
87 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
88 (defvar gnus-picon-cache nil)
89
90 ;;; Functions:
91
92 (defsubst gnus-picon-split-address (address)
93   (setq address (split-string address "@"))
94   (if (stringp (cadr address))
95       (cons (car address) (split-string (cadr address) "\\."))
96     (if (stringp (car address))
97         (split-string (car address) "\\."))))
98
99 (defun gnus-picon-find-face (address directories &optional exact)
100   (let* ((address (gnus-picon-split-address address))
101          (user (pop address))
102          (faddress address)
103          database directory result instance base)
104     (catch 'found
105       (dolist (database gnus-picon-databases)
106         (dolist (directory directories)
107           (setq address faddress
108                 base (expand-file-name directory database))
109           (while address
110             (when (setq result (gnus-picon-find-image
111                                 (concat base "/" (mapconcat 'downcase
112                                                             (reverse address)
113                                                             "/")
114                                         "/" (downcase user) "/")))
115               (throw 'found result))
116             (if exact
117                 (setq address nil)
118               (pop address)))
119           ;; Kludge to search MISC as well.  But not in "news".
120           (unless (string= directory "news")
121             (when (setq result (gnus-picon-find-image
122                                 (concat base "/MISC/" user "/")))
123               (throw 'found result))))))))
124
125 (defun gnus-picon-find-image (directory)
126   (let ((types gnus-picon-file-types)
127         found type file)
128     (while (and (not found)
129                 (setq type (pop types)))
130       (setq found (file-exists-p (setq file (concat directory "face." type)))))
131     (if found
132         file
133       nil)))
134
135 (defun gnus-picon-insert-glyph (glyph category)
136   "Insert GLYPH into the buffer.
137 GLYPH can be either a glyph or a string."
138   (if (stringp glyph)
139       (insert glyph)
140     (gnus-add-wash-type category)
141     (gnus-add-image category (car glyph))
142     (gnus-put-image (car glyph) (cdr glyph))))
143
144 (defun gnus-picon-create-glyph (file)
145   (or (cdr (assoc file gnus-picon-glyph-alist))
146       (cdar (push (cons file (gnus-create-image file))
147                   gnus-picon-glyph-alist))))
148
149 ;;; Functions that does picon transformations:
150
151 (defun gnus-picon-transform-address (header category)
152   (gnus-with-article-headers
153     (let ((addresses
154            (mail-header-parse-addresses (mail-fetch-field header)))
155           spec file point cache)
156       (dolist (address addresses)
157         (setq address (car address))
158         (when (and (stringp address)
159                    (setq spec (gnus-picon-split-address address)))
160           (if (setq cache (cdr (assoc address gnus-picon-cache)))
161               (setq spec cache)
162             (when (setq file (or (gnus-picon-find-face
163                                   address gnus-picon-user-directories)
164                                  (gnus-picon-find-face
165                                   (concat "unknown@"
166                                           (mapconcat
167                                            'identity (cdr spec) "."))
168                                   gnus-picon-user-directories)))
169               (setcar spec (cons (gnus-picon-create-glyph file)
170                                  (car spec))))
171               
172             (dotimes (i (1- (length spec)))
173               (when (setq file (gnus-picon-find-face
174                                 (concat "unknown@"
175                                         (mapconcat
176                                          'identity (nthcdr (1+ i) spec) "."))
177                                 gnus-picon-domain-directories t))
178                 (setcar (nthcdr (1+ i) spec)
179                         (cons (gnus-picon-create-glyph file)
180                               (nth (1+ i) spec)))))
181             (setq spec (nreverse spec))
182             (push (cons address spec) gnus-picon-cache))
183           
184           (gnus-article-goto-header header)
185           (mail-header-narrow-to-field)
186           (when (search-forward address nil t)
187             (delete-region (match-beginning 0) (match-end 0))
188             (setq point (point))
189             (while spec
190               (goto-char point)
191               (if (> (length spec) 2)
192                   (insert ".")
193                 (if (= (length spec) 2)
194                   (insert "@")))
195               (gnus-picon-insert-glyph (pop spec) category))))))))
196
197 (defun gnus-picon-transform-newsgroups (header)
198   (interactive)
199   (gnus-with-article-headers
200     (gnus-article-goto-header header)
201     (mail-header-narrow-to-field)
202     (let ((groups (message-tokenize-header (mail-fetch-field header)))
203           spec file point)
204       (dolist (group groups)
205         (unless (setq spec (cdr (assoc group gnus-picon-cache)))
206           (setq spec (nreverse (split-string group "[.]")))
207           (dotimes (i (length spec))
208             (when (setq file (gnus-picon-find-face
209                               (concat "unknown@"
210                                       (mapconcat
211                                        'identity (nthcdr i spec) "."))
212                               gnus-picon-news-directories t))
213               (setcar (nthcdr i spec)
214                       (cons (gnus-picon-create-glyph file)
215                             (nth i spec)))))
216             (push (cons group spec) gnus-picon-cache))
217         (when (search-forward group nil t)
218           (delete-region (match-beginning 0) (match-end 0))
219           (save-restriction
220             (narrow-to-region (point) (point))
221             (while spec
222               (goto-char (point-min))
223               (if (> (length spec) 1)
224                   (insert "."))
225               (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
226             (goto-char (point-max))))))))
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