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>.
43 (eval-when-compile (require 'cl))
50 (defcustom gnus-picon-news-directories '("news")
51 "*List of directories to search for newsgroups faces."
52 :type '(repeat string)
55 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
56 "*List of directories to search for user faces."
57 :type '(repeat string)
60 (defcustom gnus-picon-domain-directories '("domains")
61 "*List of directories to search for domain faces.
62 Some people may want to add \"unknown\" to this list."
63 :type '(repeat string)
66 (defcustom gnus-picon-file-types
67 (let ((types (list "xbm")))
68 (if (gnus-image-type-available-p 'gif)
69 (setq types (cons "gif" types)))
70 (if (gnus-image-type-available-p 'xpm)
71 (setq types (cons "xpm" types)))
73 "*List of suffixes on picon file names to try."
74 :type '(repeat string)
77 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
78 "Face to show xbm picon in."
81 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
82 "Face to show picon in."
85 ;;; Internal variables:
87 (defvar gnus-picon-setup-p nil)
88 (defvar gnus-picon-glyph-alist nil
90 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
91 (defvar gnus-picon-cache nil)
95 (defsubst gnus-picon-split-address (address)
96 (setq address (split-string address "@"))
97 (if (stringp (cadr address))
98 (cons (car address) (split-string (cadr address) "\\."))
99 (if (stringp (car address))
100 (split-string (car address) "\\."))))
102 (defun gnus-picon-find-face (address directories &optional exact)
103 (let* ((address (gnus-picon-split-address address))
106 database directory result instance base)
108 (dolist (database gnus-picon-databases)
109 (dolist (directory directories)
110 (setq address faddress
111 base (expand-file-name directory database))
113 (when (setq result (gnus-picon-find-image
114 (concat base "/" (mapconcat 'downcase
117 "/" (downcase user) "/")))
118 (throw 'found result))
122 ;; Kludge to search MISC as well. But not in "news".
123 (unless (string= directory "news")
124 (when (setq result (gnus-picon-find-image
125 (concat base "/MISC/" user "/")))
126 (throw 'found result))))))))
128 (defun gnus-picon-find-image (directory)
129 (let ((types gnus-picon-file-types)
131 (while (and (not found)
132 (setq type (pop types)))
133 (setq found (file-exists-p (setq file (concat directory "face." type)))))
138 (defun gnus-picon-insert-glyph (glyph category)
139 "Insert GLYPH into the buffer.
140 GLYPH can be either a glyph or a string."
143 (gnus-add-wash-type category)
144 (gnus-add-image category (car glyph))
145 (gnus-put-image (car glyph) (cdr glyph) category)))
147 (defun gnus-picon-create-glyph (file)
148 (or (cdr (assoc file gnus-picon-glyph-alist))
149 (cdar (push (cons file (gnus-create-image file))
150 gnus-picon-glyph-alist))))
152 ;;; Functions that does picon transformations:
154 (defun gnus-picon-transform-address (header category)
155 (gnus-with-article-headers
157 (mail-header-parse-addresses
158 ;; mail-header-parse-addresses does not work (reliably) on
162 (mail-encode-encoded-word-string
163 (or (mail-fetch-field header) "")))
164 (mail-fetch-field header))))
165 spec file point cache)
166 (dolist (address addresses)
167 (setq address (car address))
168 (when (and (stringp address)
169 (setq spec (gnus-picon-split-address address)))
170 (if (setq cache (cdr (assoc address gnus-picon-cache)))
172 (when (setq file (or (gnus-picon-find-face
173 address gnus-picon-user-directories)
174 (gnus-picon-find-face
177 'identity (cdr spec) "."))
178 gnus-picon-user-directories)))
179 (setcar spec (cons (gnus-picon-create-glyph file)
182 (dotimes (i (1- (length spec)))
183 (when (setq file (gnus-picon-find-face
186 'identity (nthcdr (1+ i) spec) "."))
187 gnus-picon-domain-directories t))
188 (setcar (nthcdr (1+ i) spec)
189 (cons (gnus-picon-create-glyph file)
190 (nth (1+ i) spec)))))
191 (setq spec (nreverse spec))
192 (push (cons address spec) gnus-picon-cache))
194 (gnus-article-goto-header header)
195 (mail-header-narrow-to-field)
196 (when (search-forward address nil t)
197 (delete-region (match-beginning 0) (match-end 0))
201 (if (> (length spec) 2)
203 (if (= (length spec) 2)
205 (gnus-picon-insert-glyph (pop spec) category))))))))
207 (defun gnus-picon-transform-newsgroups (header)
209 (gnus-with-article-headers
210 (gnus-article-goto-header header)
211 (mail-header-narrow-to-field)
212 (let ((groups (message-tokenize-header (mail-fetch-field header)))
214 (dolist (group groups)
215 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
216 (setq spec (nreverse (split-string group "[.]")))
217 (dotimes (i (length spec))
218 (when (setq file (gnus-picon-find-face
221 'identity (nthcdr i spec) "."))
222 gnus-picon-news-directories t))
223 (setcar (nthcdr i spec)
224 (cons (gnus-picon-create-glyph file)
226 (push (cons group spec) gnus-picon-cache))
227 (when (search-forward group nil t)
228 (delete-region (match-beginning 0) (match-end 0))
230 (narrow-to-region (point) (point))
232 (goto-char (point-min))
233 (if (> (length spec) 1)
235 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
236 (goto-char (point-max))))))))
240 ;; #### NOTE: the test for buffer-read-only is the same as in
241 ;; article-display-[x-]face. See the comment up there.
244 (defun gnus-treat-from-picon ()
245 "Display picons in the From header.
246 If picons are already displayed, remove them."
248 (let ((wash-picon-p buffer-read-only))
249 (gnus-with-article-buffer
250 (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
251 (gnus-delete-images 'from-picon)
252 (gnus-picon-transform-address "from" 'from-picon)))
256 (defun gnus-treat-mail-picon ()
257 "Display picons in the Cc and To headers.
258 If picons are already displayed, remove them."
260 (let ((wash-picon-p buffer-read-only))
261 (gnus-with-article-buffer
262 (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
263 (gnus-delete-images 'mail-picon)
264 (gnus-picon-transform-address "cc" 'mail-picon)
265 (gnus-picon-transform-address "to" 'mail-picon)))
269 (defun gnus-treat-newsgroups-picon ()
270 "Display picons in the Newsgroups and Followup-To headers.
271 If picons are already displayed, remove them."
273 (let ((wash-picon-p buffer-read-only))
274 (gnus-with-article-buffer
275 (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
276 (gnus-delete-images 'newsgroups-picon)
277 (gnus-picon-transform-newsgroups "newsgroups")
278 (gnus-picon-transform-newsgroups "followup-to")))
281 (provide 'gnus-picon)
283 ;;; gnus-picon.el ends here