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))
49 (defcustom gnus-picon-news-directories '("news")
50 "*List of directories to search for newsgroups faces."
51 :type '(repeat string)
54 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
55 "*List of directories to search for user faces."
56 :type '(repeat string)
59 (defcustom gnus-picon-domain-directories '("domains")
60 "*List of directories to search for domain faces.
61 Some people may want to add \"unknown\" to this list."
62 :type '(repeat string)
65 (defcustom gnus-picon-file-types
66 (let ((types (list "xbm")))
67 (if (gnus-image-type-available-p 'gif)
68 (setq types (cons "gif" types)))
69 (if (gnus-image-type-available-p 'xpm)
70 (setq types (cons "xpm" types)))
72 "*List of suffixes on picon file names to try."
73 :type '(repeat string)
76 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
77 "Face to show xbm picon in."
80 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
81 "Face to show picon in."
84 ;;; Internal variables:
86 (defvar gnus-picon-setup-p nil)
87 (defvar gnus-picon-glyph-alist nil
89 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
90 (defvar gnus-picon-cache nil)
94 (defsubst gnus-picon-split-address (address)
95 (setq address (split-string address "@"))
96 (if (stringp (cadr address))
97 (cons (car address) (split-string (cadr address) "\\."))
98 (if (stringp (car address))
99 (split-string (car address) "\\."))))
101 (defun gnus-picon-find-face (address directories &optional exact)
102 (let* ((address (gnus-picon-split-address address))
105 database directory result instance base)
107 (dolist (database gnus-picon-databases)
108 (dolist (directory directories)
109 (setq address faddress
110 base (expand-file-name directory database))
112 (when (setq result (gnus-picon-find-image
113 (concat base "/" (mapconcat 'downcase
116 "/" (downcase user) "/")))
117 (throw 'found result))
121 ;; Kludge to search MISC as well. But not in "news".
122 (unless (string= directory "news")
123 (when (setq result (gnus-picon-find-image
124 (concat base "/MISC/" user "/")))
125 (throw 'found result))))))))
127 (defun gnus-picon-find-image (directory)
128 (let ((types gnus-picon-file-types)
130 (while (and (not found)
131 (setq type (pop types)))
132 (setq found (file-exists-p (setq file (concat directory "face." type)))))
137 (defun gnus-picon-insert-glyph (glyph category)
138 "Insert GLYPH into the buffer.
139 GLYPH can be either a glyph or a string."
142 (gnus-add-wash-type category)
143 (gnus-add-image category (car glyph))
144 (gnus-put-image (car glyph) (cdr glyph))))
146 (defun gnus-picon-create-glyph (file)
147 (or (cdr (assoc file gnus-picon-glyph-alist))
148 (cdar (push (cons file (gnus-create-image file))
149 gnus-picon-glyph-alist))))
151 ;;; Functions that does picon transformations:
153 (defun gnus-picon-transform-address (header category)
154 (gnus-with-article-headers
156 (mail-header-parse-addresses (mail-fetch-field header)))
157 spec file point cache)
158 (dolist (address addresses)
159 (setq address (car address))
160 (when (and (stringp address)
161 (setq spec (gnus-picon-split-address address)))
162 (if (setq cache (cdr (assoc address gnus-picon-cache)))
164 (when (setq file (or (gnus-picon-find-face
165 address gnus-picon-user-directories)
166 (gnus-picon-find-face
169 'identity (cdr spec) "."))
170 gnus-picon-user-directories)))
171 (setcar spec (cons (gnus-picon-create-glyph file)
174 (dotimes (i (1- (length spec)))
175 (when (setq file (gnus-picon-find-face
178 'identity (nthcdr (1+ i) spec) "."))
179 gnus-picon-domain-directories t))
180 (setcar (nthcdr (1+ i) spec)
181 (cons (gnus-picon-create-glyph file)
182 (nth (1+ i) spec)))))
183 (setq spec (nreverse spec))
184 (push (cons address spec) gnus-picon-cache))
186 (gnus-article-goto-header header)
187 (mail-header-narrow-to-field)
188 (when (search-forward address nil t)
189 (delete-region (match-beginning 0) (match-end 0))
193 (if (> (length spec) 2)
195 (if (= (length spec) 2)
197 (gnus-picon-insert-glyph (pop spec) category))))))))
199 (defun gnus-picon-transform-newsgroups (header)
201 (gnus-with-article-headers
202 (gnus-article-goto-header header)
203 (mail-header-narrow-to-field)
204 (let ((groups (message-tokenize-header (mail-fetch-field header)))
206 (dolist (group groups)
207 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
208 (setq spec (nreverse (split-string group "[.]")))
209 (dotimes (i (length spec))
210 (when (setq file (gnus-picon-find-face
213 'identity (nthcdr i spec) "."))
214 gnus-picon-news-directories t))
215 (setcar (nthcdr i spec)
216 (cons (gnus-picon-create-glyph file)
218 (push (cons group spec) gnus-picon-cache))
219 (when (search-forward group nil t)
220 (delete-region (match-beginning 0) (match-end 0))
222 (narrow-to-region (point) (point))
224 (goto-char (point-min))
225 (if (> (length spec) 1)
227 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
228 (goto-char (point-max))))))))
233 (defun gnus-treat-from-picon ()
234 "Display picons in the From header.
235 If picons are already displayed, remove them."
237 (gnus-with-article-buffer
238 (if (memq 'from-picon gnus-article-wash-types)
239 (gnus-delete-images 'from-picon)
240 (gnus-picon-transform-address "from" 'from-picon))))
243 (defun gnus-treat-mail-picon ()
244 "Display picons in the Cc and To headers.
245 If picons are already displayed, remove them."
247 (gnus-with-article-buffer
248 (if (memq 'mail-picon gnus-article-wash-types)
249 (gnus-delete-images 'mail-picon)
250 (gnus-picon-transform-address "cc" 'mail-picon)
251 (gnus-picon-transform-address "to" 'mail-picon))))
254 (defun gnus-treat-newsgroups-picon ()
255 "Display picons in the Newsgroups and Followup-To headers.
256 If picons are already displayed, remove them."
258 (gnus-with-article-buffer
259 (if (memq 'newsgroups-picon gnus-article-wash-types)
260 (gnus-delete-images 'newsgroups-picon)
261 (gnus-picon-transform-newsgroups "newsgroups")
262 (gnus-picon-transform-newsgroups "followup-to"))))
264 (provide 'gnus-picon)
266 ;;; gnus-picon.el ends here