1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
7 ;; Keywords: news xpm annotation glyph faces
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; There are three picon types relevant to Gnus:
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
41 (eval-when-compile (require 'cl))
50 "Show pictures of people, domains, and newsgroups."
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"
60 (defcustom gnus-picon-news-directories '("news")
61 "*List of directories to search for newsgroups faces."
62 :type '(repeat string)
65 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
66 "*List of directories to search for user faces."
67 :type '(repeat string)
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)
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)))
83 "*List of suffixes on picon file names to try."
84 :type '(repeat string)
87 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
88 "Face to show xbm picon in."
91 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
92 "Face to show picon in."
95 ;;; Internal variables:
97 (defvar gnus-picon-setup-p nil)
98 (defvar gnus-picon-glyph-alist nil
100 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
101 (defvar gnus-picon-cache nil)
105 (defsubst gnus-picon-split-address (address)
106 (setq address (split-string address "@"))
107 (if (stringp (cadr address))
108 (cons (car address) (split-string (cadr address) "\\."))
109 (if (stringp (car address))
110 (split-string (car address) "\\."))))
112 (defun gnus-picon-find-face (address directories &optional exact)
113 (let* ((address (gnus-picon-split-address address))
116 database directory result instance base)
118 (dolist (database gnus-picon-databases)
119 (dolist (directory directories)
120 (setq address faddress
121 base (expand-file-name directory database))
123 (when (setq result (gnus-picon-find-image
124 (concat base "/" (mapconcat 'identity
128 (throw 'found result))
132 ;; Kludge to search MISC as well. But not in "news".
133 (unless (string= directory "news")
134 (when (setq result (gnus-picon-find-image
135 (concat base "/MISC/" user "/")))
136 (throw 'found result))))))))
138 (defun gnus-picon-find-image (directory)
139 (let ((types gnus-picon-file-types)
141 (while (and (not found)
142 (setq type (pop types)))
143 (setq found (file-exists-p (setq file (concat directory "face." type)))))
148 (defun gnus-picon-insert-glyph (glyph category)
149 "Insert GLYPH into the buffer.
150 GLYPH can be either a glyph or a string."
153 (gnus-add-wash-type category)
154 (gnus-add-image category (car glyph))
155 (gnus-put-image (car glyph) (cdr glyph))))
157 (defun gnus-picon-create-glyph (file)
158 (or (cdr (assoc file gnus-picon-glyph-alist))
159 (cdar (push (cons file (gnus-create-image file))
160 gnus-picon-glyph-alist))))
162 ;;; Functions that does picon transformations:
164 (defun gnus-picon-transform-address (header category)
165 (gnus-with-article-headers
167 (mail-header-parse-addresses (mail-fetch-field header)))
168 spec file point cache)
169 (dolist (address addresses)
170 (setq address (car address))
171 (when (and (stringp address)
172 (setq spec (gnus-picon-split-address address)))
173 (if (setq cache (cdr (assoc address gnus-picon-cache)))
175 (when (setq file (or (gnus-picon-find-face
176 address gnus-picon-user-directories)
177 (gnus-picon-find-face
180 'identity (cdr spec) "."))
181 gnus-picon-user-directories)))
182 (setcar spec (cons (gnus-picon-create-glyph file)
185 (dotimes (i (1- (length spec)))
186 (when (setq file (gnus-picon-find-face
189 'identity (nthcdr (1+ i) spec) "."))
190 gnus-picon-domain-directories t))
191 (setcar (nthcdr (1+ i) spec)
192 (cons (gnus-picon-create-glyph file)
193 (nth (1+ i) spec)))))
194 (setq spec (nreverse spec))
195 (push (cons address spec) gnus-picon-cache))
197 (gnus-article-goto-header header)
198 (mail-header-narrow-to-field)
199 (when (search-forward address nil t)
200 (delete-region (match-beginning 0) (match-end 0))
204 (if (> (length spec) 2)
206 (if (= (length spec) 2)
208 (gnus-picon-insert-glyph (pop spec) category))))))))
210 (defun gnus-picon-transform-newsgroups (header)
212 (gnus-with-article-headers
213 (gnus-article-goto-header header)
214 (mail-header-narrow-to-field)
215 (let ((groups (message-tokenize-header (mail-fetch-field header)))
217 (dolist (group groups)
218 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
219 (setq spec (nreverse (split-string group "[.]")))
220 (dotimes (i (length spec))
221 (when (setq file (gnus-picon-find-face
224 'identity (nthcdr i spec) "."))
225 gnus-picon-news-directories t))
226 (setcar (nthcdr i spec)
227 (cons (gnus-picon-create-glyph file)
229 (push (cons group spec) gnus-picon-cache))
230 (when (search-forward group nil t)
231 (delete-region (match-beginning 0) (match-end 0))
233 (narrow-to-region (point) (point))
235 (goto-char (point-min))
236 (if (> (length spec) 1)
238 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
239 (goto-char (point-max))))))))
244 (defun gnus-treat-from-picon ()
245 "Display picons in the From header.
246 If picons are already displayed, remove them."
248 (gnus-with-article-buffer
249 (if (memq 'from-picon gnus-article-wash-types)
250 (gnus-delete-images 'from-picon)
251 (gnus-picon-transform-address "from" 'from-picon))))
254 (defun gnus-treat-mail-picon ()
255 "Display picons in the Cc and To headers.
256 If picons are already displayed, remove them."
258 (gnus-with-article-buffer
259 (if (memq 'mail-picon gnus-article-wash-types)
260 (gnus-delete-images 'mail-picon)
261 (gnus-picon-transform-address "cc" 'mail-picon)
262 (gnus-picon-transform-address "to" 'mail-picon))))
265 (defun gnus-treat-newsgroups-picon ()
266 "Display picons in the Newsgroups and Followup-To headers.
267 If picons are already displayed, remove them."
269 (gnus-with-article-buffer
270 (if (memq 'newsgroups-picon gnus-article-wash-types)
271 (gnus-delete-images 'newsgroups-picon)
272 (gnus-picon-transform-newsgroups "newsgroups")
273 (gnus-picon-transform-newsgroups "followup-to"))))
275 (provide 'gnus-picon)
277 ;;; gnus-picon.el ends here