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: Lars Magne Ingebrigtsen <larsi@gnus.org>
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
39 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
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 (when (gnus-image-type-available-p 'gif)
69 (when (gnus-image-type-available-p 'xpm)
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) category)))
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
157 ;; mail-header-parse-addresses does not work (reliably) on
161 (mail-encode-encoded-word-string
162 (or (mail-fetch-field header) "")))
163 (mail-fetch-field header))))
164 spec file point cache)
165 (dolist (address addresses)
166 (setq address (car address))
167 (when (and (stringp address)
168 (setq spec (gnus-picon-split-address address)))
169 (if (setq cache (cdr (assoc address gnus-picon-cache)))
171 (when (setq file (or (gnus-picon-find-face
172 address gnus-picon-user-directories)
173 (gnus-picon-find-face
176 'identity (cdr spec) "."))
177 gnus-picon-user-directories)))
178 (setcar spec (cons (gnus-picon-create-glyph file)
181 (dotimes (i (1- (length spec)))
182 (when (setq file (gnus-picon-find-face
185 'identity (nthcdr (1+ i) spec) "."))
186 gnus-picon-domain-directories t))
187 (setcar (nthcdr (1+ i) spec)
188 (cons (gnus-picon-create-glyph file)
189 (nth (1+ i) spec)))))
190 (setq spec (nreverse spec))
191 (push (cons address spec) gnus-picon-cache))
193 (gnus-article-goto-header header)
194 (mail-header-narrow-to-field)
195 (when (search-forward address nil t)
196 (delete-region (match-beginning 0) (match-end 0))
200 (if (> (length spec) 2)
202 (if (= (length spec) 2)
204 (gnus-picon-insert-glyph (pop spec) category))))))))
206 (defun gnus-picon-transform-newsgroups (header)
208 (gnus-with-article-headers
209 (gnus-article-goto-header header)
210 (mail-header-narrow-to-field)
211 (let ((groups (message-tokenize-header (mail-fetch-field header)))
213 (dolist (group groups)
214 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
215 (setq spec (nreverse (split-string group "[.]")))
216 (dotimes (i (length spec))
217 (when (setq file (gnus-picon-find-face
220 'identity (nthcdr i spec) "."))
221 gnus-picon-news-directories t))
222 (setcar (nthcdr i spec)
223 (cons (gnus-picon-create-glyph file)
225 (push (cons group spec) gnus-picon-cache))
226 (when (search-forward group nil t)
227 (delete-region (match-beginning 0) (match-end 0))
229 (narrow-to-region (point) (point))
231 (goto-char (point-min))
232 (if (> (length spec) 1)
234 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
235 (goto-char (point-max))))))))
239 ;; #### NOTE: the test for buffer-read-only is the same as in
240 ;; article-display-[x-]face. See the comment up there.
243 (defun gnus-treat-from-picon ()
244 "Display picons in the From header.
245 If picons are already displayed, remove them."
247 (let ((wash-picon-p buffer-read-only))
248 (gnus-with-article-buffer
249 (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
250 (gnus-delete-images 'from-picon)
251 (gnus-picon-transform-address "from" 'from-picon)))
255 (defun gnus-treat-mail-picon ()
256 "Display picons in the Cc and To headers.
257 If picons are already displayed, remove them."
259 (let ((wash-picon-p buffer-read-only))
260 (gnus-with-article-buffer
261 (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
262 (gnus-delete-images 'mail-picon)
263 (gnus-picon-transform-address "cc" 'mail-picon)
264 (gnus-picon-transform-address "to" 'mail-picon)))
268 (defun gnus-treat-newsgroups-picon ()
269 "Display picons in the Newsgroups and Followup-To headers.
270 If picons are already displayed, remove them."
272 (let ((wash-picon-p buffer-read-only))
273 (gnus-with-article-buffer
274 (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
275 (gnus-delete-images 'newsgroups-picon)
276 (gnus-picon-transform-newsgroups "newsgroups")
277 (gnus-picon-transform-newsgroups "followup-to")))
280 (provide 'gnus-picon)
282 ;;; gnus-picon.el ends here