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
47 (defcustom gnus-picon-news-directories '("news")
48 "*List of directories to search for newsgroups faces."
49 :type '(repeat string)
52 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
53 "*List of directories to search for user faces."
54 :type '(repeat string)
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)
63 (defcustom gnus-picon-file-types
64 (let ((types (list "xbm")))
65 (when (gnus-image-type-available-p 'gif)
67 (when (gnus-image-type-available-p 'xpm)
70 "*List of suffixes on picon file names to try."
71 :type '(repeat string)
74 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
75 "Face to show xbm picon in."
78 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
79 "Face to show picon in."
82 ;;; Internal variables:
84 (defvar gnus-picon-setup-p nil)
85 (defvar gnus-picon-glyph-alist nil
87 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
88 (defvar gnus-picon-cache nil)
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) "\\."))))
99 (defun gnus-picon-find-face (address directories &optional exact)
100 (let* ((address (gnus-picon-split-address address))
103 database directory result instance base)
105 (dolist (database gnus-picon-databases)
106 (dolist (directory directories)
107 (setq address faddress
108 base (expand-file-name directory database))
110 (when (setq result (gnus-picon-find-image
111 (concat base "/" (mapconcat 'downcase
114 "/" (downcase user) "/")))
115 (throw 'found result))
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))))))))
125 (defun gnus-picon-find-image (directory)
126 (let ((types gnus-picon-file-types)
128 (while (and (not found)
129 (setq type (pop types)))
130 (setq found (file-exists-p (setq file (concat directory "face." type)))))
135 (defun gnus-picon-insert-glyph (glyph category)
136 "Insert GLYPH into the buffer.
137 GLYPH can be either a glyph or a string."
140 (gnus-add-wash-type category)
141 (gnus-add-image category (car glyph))
142 (gnus-put-image (car glyph) (cdr glyph))))
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))))
149 ;;; Functions that does picon transformations:
151 (defun gnus-picon-transform-address (header category)
152 (gnus-with-article-headers
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)))
162 (when (setq file (or (gnus-picon-find-face
163 address gnus-picon-user-directories)
164 (gnus-picon-find-face
167 'identity (cdr spec) "."))
168 gnus-picon-user-directories)))
169 (setcar spec (cons (gnus-picon-create-glyph file)
172 (dotimes (i (1- (length spec)))
173 (when (setq file (gnus-picon-find-face
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))
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))
191 (if (> (length spec) 2)
193 (if (= (length spec) 2)
195 (gnus-picon-insert-glyph (pop spec) category))))))))
197 (defun gnus-picon-transform-newsgroups (header)
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)))
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
211 'identity (nthcdr i spec) "."))
212 gnus-picon-news-directories t))
213 (setcar (nthcdr i spec)
214 (cons (gnus-picon-create-glyph file)
216 (push (cons group spec) gnus-picon-cache))
217 (when (search-forward group nil t)
218 (delete-region (match-beginning 0) (match-end 0))
220 (narrow-to-region (point) (point))
222 (goto-char (point-min))
223 (if (> (length spec) 1)
225 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
226 (goto-char (point-max))))))))
231 (defun gnus-treat-from-picon ()
232 "Display picons in the From header.
233 If picons are already displayed, remove them."
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))))
241 (defun gnus-treat-mail-picon ()
242 "Display picons in the Cc and To headers.
243 If picons are already displayed, remove them."
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))))
252 (defun gnus-treat-newsgroups-picon ()
253 "Display picons in the Newsgroups and Followup-To headers.
254 If picons are already displayed, remove them."
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"))))
262 (provide 'gnus-picon)
264 ;;; gnus-picon.el ends here