1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
155 ;; mail-header-parse-addresses does not work (reliably) on
157 (mail-encode-encoded-word-string
158 (or (mail-fetch-field header) ""))))
159 spec file point cache)
160 (dolist (address addresses)
161 (setq address (car address))
162 (when (and (stringp address)
163 (setq spec (gnus-picon-split-address address)))
164 (if (setq cache (cdr (assoc address gnus-picon-cache)))
166 (when (setq file (or (gnus-picon-find-face
167 address gnus-picon-user-directories)
168 (gnus-picon-find-face
171 'identity (cdr spec) "."))
172 gnus-picon-user-directories)))
173 (setcar spec (cons (gnus-picon-create-glyph file)
176 (dotimes (i (1- (length spec)))
177 (when (setq file (gnus-picon-find-face
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 (setq spec (nreverse spec))
186 (push (cons address spec) gnus-picon-cache))
188 (gnus-article-goto-header header)
189 (mail-header-narrow-to-field)
190 (when (search-forward address nil t)
191 (delete-region (match-beginning 0) (match-end 0))
195 (if (> (length spec) 2)
197 (if (= (length spec) 2)
199 (gnus-picon-insert-glyph (pop spec) category))))))))
201 (defun gnus-picon-transform-newsgroups (header)
203 (gnus-with-article-headers
204 (gnus-article-goto-header header)
205 (mail-header-narrow-to-field)
206 (let ((groups (message-tokenize-header (mail-fetch-field header)))
208 (dolist (group groups)
209 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
210 (setq spec (nreverse (split-string group "[.]")))
211 (dotimes (i (length spec))
212 (when (setq file (gnus-picon-find-face
215 'identity (nthcdr i spec) "."))
216 gnus-picon-news-directories t))
217 (setcar (nthcdr i spec)
218 (cons (gnus-picon-create-glyph file)
220 (push (cons group spec) gnus-picon-cache))
221 (when (search-forward group nil t)
222 (delete-region (match-beginning 0) (match-end 0))
224 (narrow-to-region (point) (point))
226 (goto-char (point-min))
227 (if (> (length spec) 1)
229 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
230 (goto-char (point-max))))))))
235 (defun gnus-treat-from-picon ()
236 "Display picons in the From header.
237 If picons are already displayed, remove them."
239 (gnus-with-article-buffer
240 (if (memq 'from-picon gnus-article-wash-types)
241 (gnus-delete-images 'from-picon)
242 (gnus-picon-transform-address "from" 'from-picon))))
245 (defun gnus-treat-mail-picon ()
246 "Display picons in the Cc and To headers.
247 If picons are already displayed, remove them."
249 (gnus-with-article-buffer
250 (if (memq 'mail-picon gnus-article-wash-types)
251 (gnus-delete-images 'mail-picon)
252 (gnus-picon-transform-address "cc" 'mail-picon)
253 (gnus-picon-transform-address "to" 'mail-picon))))
256 (defun gnus-treat-newsgroups-picon ()
257 "Display picons in the Newsgroups and Followup-To headers.
258 If picons are already displayed, remove them."
260 (gnus-with-article-buffer
261 (if (memq 'newsgroups-picon gnus-article-wash-types)
262 (gnus-delete-images 'newsgroups-picon)
263 (gnus-picon-transform-newsgroups "newsgroups")
264 (gnus-picon-transform-newsgroups "followup-to"))))
266 (provide 'gnus-picon)
268 ;;; gnus-picon.el ends here