1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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
48 "Show pictures of people, domains, and newsgroups."
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"
58 (defcustom gnus-picon-news-directories '("news")
59 "*List of directories to search for newsgroups faces."
60 :type '(repeat string)
63 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
64 "*List of directories to search for user faces."
65 :type '(repeat string)
68 (defcustom gnus-picon-domain-directories '("domains")
69 "*List of directories to search for domain faces.
70 Some people may want to add \"unknown\" to this list."
71 :type '(repeat string)
74 (defcustom gnus-picon-file-types
75 (let ((types (list "xbm")))
76 (when (gnus-image-type-available-p 'gif)
78 (when (gnus-image-type-available-p 'xpm)
81 "*List of suffixes on picon file names to try."
82 :type '(repeat string)
85 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
86 "Face to show xbm picon in."
89 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
90 "Face to show picon in."
93 ;;; Internal variables:
95 (defvar gnus-picon-setup-p nil)
96 (defvar gnus-picon-glyph-alist nil
98 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
102 (defsubst gnus-picon-split-address (address)
103 (setq address (split-string address "@"))
104 (if (stringp (cadr address))
105 (cons (car address) (split-string (cadr address) "\\."))
106 (if (stringp (car address))
107 (split-string (car address) "\\."))))
109 (defun gnus-picon-find-face (address directories &optional exact)
110 (let* ((address (gnus-picon-split-address address))
113 database directory result instance base)
115 (dolist (database gnus-picon-databases)
116 (dolist (directory directories)
117 (setq address faddress
118 base (expand-file-name directory database))
119 ;; Kludge to search misc/MISC for users.
120 (when (string= directory "misc")
121 (setq address '("MISC")))
123 (when (setq result (gnus-picon-find-image
124 (concat base "/" (mapconcat 'identity
128 (throw 'found result))
133 (defun gnus-picon-find-image (directory)
134 (let ((types gnus-picon-file-types)
136 (while (and (not found)
137 (setq type (pop types)))
138 (setq found (file-exists-p (setq file (concat directory "face." type)))))
143 (defun gnus-picon-insert-glyph (glyph category)
144 "Insert GLYPH into the buffer.
145 GLYPH can be either a glyph or a string."
148 (gnus-add-wash-type category)
149 (gnus-add-image category (car glyph))
150 (gnus-put-image (car glyph) (cdr glyph))))
152 (defun gnus-picon-create-glyph (file)
153 (or (cdr (assoc file gnus-picon-glyph-alist))
154 (cdar (push (cons file (gnus-create-image file))
155 gnus-picon-glyph-alist))))
157 ;;; Functions that does picon transformations:
159 (defun gnus-picon-transform-address (header category)
160 (gnus-with-article-headers
162 (mail-header-parse-addresses (mail-fetch-field header)))
164 (dolist (address addresses)
165 (setq address (car address)
167 (when (and (stringp address)
168 (setq spec (gnus-picon-split-address address)))
169 (when (setq file (gnus-picon-find-face
170 address gnus-picon-user-directories))
171 (setcar spec (cons (gnus-picon-create-glyph file)
173 (dotimes (i (1- (length spec)))
174 (when (setq file (gnus-picon-find-face
177 'identity (nthcdr (1+ i) spec) "."))
178 gnus-picon-domain-directories t))
179 (setcar (nthcdr (1+ i) spec)
180 (cons (gnus-picon-create-glyph file)
181 (nth (1+ i) spec)))))
183 (gnus-article-goto-header header)
184 (mail-header-narrow-to-field)
185 (when (search-forward address nil t)
186 (delete-region (match-beginning 0) (match-end 0))
188 (gnus-picon-insert-glyph (pop spec) category)
193 (setq first nil))))))))))
195 (defun gnus-picon-transform-newsgroups (header)
197 (gnus-with-article-headers
200 (message-tokenize-header (mail-fetch-field header))
201 (lambda (g1 g2) (> (length g1) (length g2)))))
203 (dolist (group groups)
204 (setq spec (nreverse (split-string group "[.]")))
205 (dotimes (i (length spec))
206 (when (setq file (gnus-picon-find-face
209 'identity (nthcdr i spec) "."))
210 gnus-picon-news-directories t))
211 (setcar (nthcdr i spec)
212 (cons (gnus-picon-create-glyph file)
215 (gnus-article-goto-header header)
216 (mail-header-narrow-to-field)
217 (when (search-forward group nil t)
218 (delete-region (match-beginning 0) (match-end 0))
219 (setq spec (nreverse spec))
221 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon)
228 (defun gnus-treat-from-picon ()
229 "Display picons in the From header.
230 If picons are already displayed, remove them."
232 (gnus-with-article-buffer
233 (if (memq 'from-picon gnus-article-wash-types)
234 (gnus-delete-images 'from-picon)
235 (gnus-picon-transform-address "from" 'from-picon))))
238 (defun gnus-treat-mail-picon ()
239 "Display picons in the Cc and To headers.
240 If picons are already displayed, remove them."
242 (gnus-with-article-buffer
243 (if (memq 'mail-picon gnus-article-wash-types)
244 (gnus-delete-images 'mail-picon)
245 (gnus-picon-transform-address "cc" 'mail-picon)
246 (gnus-picon-transform-address "to" 'mail-picon))))
249 (defun gnus-treat-newsgroups-picon ()
250 "Display picons in the Newsgroups and Followup-To headers.
251 If picons are already displayed, remove them."
253 (gnus-with-article-buffer
254 (if (memq 'newsgroups-picon gnus-article-wash-types)
255 (gnus-delete-images 'newsgroups-picon)
256 (gnus-picon-transform-newsgroups "newsgroups")
257 (gnus-picon-transform-newsgroups "followup-to"))))
259 (provide 'gnus-picon)
261 ;;; gnus-picon.el ends here