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))
51 (defcustom gnus-picon-news-directories '("news")
52 "*List of directories to search for newsgroups faces."
53 :type '(repeat string)
56 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
57 "*List of directories to search for user faces."
58 :type '(repeat string)
61 (defcustom gnus-picon-domain-directories '("domains")
62 "*List of directories to search for domain faces.
63 Some people may want to add \"unknown\" to this list."
64 :type '(repeat string)
67 (defcustom gnus-picon-file-types
68 (let ((types (list "xbm")))
69 (if (gnus-image-type-available-p 'gif)
70 (setq types (cons "gif" types)))
71 (if (gnus-image-type-available-p 'xpm)
72 (setq types (cons "xpm" types)))
74 "*List of suffixes on picon file names to try."
75 :type '(repeat string)
78 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
79 "Face to show xbm picon in."
82 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
83 "Face to show picon in."
86 ;;; Internal variables:
88 (defvar gnus-picon-setup-p nil)
89 (defvar gnus-picon-glyph-alist nil
91 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
92 (defvar gnus-picon-cache nil)
96 (defsubst gnus-picon-split-address (address)
97 (setq address (split-string address "@"))
98 (if (stringp (cadr address))
99 (cons (car address) (split-string (cadr address) "\\."))
100 (if (stringp (car address))
101 (split-string (car address) "\\."))))
103 (defun gnus-picon-find-face (address directories &optional exact)
104 (let* ((address (gnus-picon-split-address address))
107 database directory result instance base)
109 (dolist (database gnus-picon-databases)
110 (dolist (directory directories)
111 (setq address faddress
112 base (expand-file-name directory database))
114 (when (setq result (gnus-picon-find-image
115 (concat base "/" (mapconcat 'downcase
118 "/" (downcase user) "/")))
119 (throw 'found result))
123 ;; Kludge to search MISC as well. But not in "news".
124 (unless (string= directory "news")
125 (when (setq result (gnus-picon-find-image
126 (concat base "/MISC/" user "/")))
127 (throw 'found result))))))))
129 (defun gnus-picon-find-image (directory)
130 (let ((types gnus-picon-file-types)
132 (while (and (not found)
133 (setq type (pop types)))
134 (setq found (file-exists-p (setq file (concat directory "face." type)))))
139 (defun gnus-picon-insert-glyph (glyph category)
140 "Insert GLYPH into the buffer.
141 GLYPH can be either a glyph or a string."
144 (gnus-add-wash-type category)
145 (gnus-add-image category (car glyph))
146 (gnus-put-image (car glyph) (cdr glyph) category)))
148 (defun gnus-picon-create-glyph (file)
149 (or (cdr (assoc file gnus-picon-glyph-alist))
150 (cdar (push (cons file (gnus-create-image file))
151 gnus-picon-glyph-alist))))
153 ;;; Functions that does picon transformations:
155 (defun gnus-picon-transform-address (header category)
156 (gnus-with-article-headers
158 (mail-header-parse-addresses
159 ;; mail-header-parse-addresses does not work (reliably) on
161 (mail-encode-encoded-word-string
162 (or (mail-fetch-field header) ""))))
163 spec file point cache)
164 (dolist (address addresses)
165 (setq address (car address))
166 (when (and (stringp address)
167 (setq spec (gnus-picon-split-address address)))
168 (if (setq cache (cdr (assoc address gnus-picon-cache)))
170 (when (setq file (or (gnus-picon-find-face
171 address gnus-picon-user-directories)
172 (gnus-picon-find-face
175 'identity (cdr spec) "."))
176 gnus-picon-user-directories)))
177 (setcar spec (cons (gnus-picon-create-glyph file)
180 (dotimes (i (1- (length spec)))
181 (when (setq file (gnus-picon-find-face
184 'identity (nthcdr (1+ i) spec) "."))
185 gnus-picon-domain-directories t))
186 (setcar (nthcdr (1+ i) spec)
187 (cons (gnus-picon-create-glyph file)
188 (nth (1+ i) spec)))))
189 (setq spec (nreverse spec))
190 (push (cons address spec) gnus-picon-cache))
192 (gnus-article-goto-header header)
193 (mail-header-narrow-to-field)
194 (when (search-forward address nil t)
195 (delete-region (match-beginning 0) (match-end 0))
199 (if (> (length spec) 2)
201 (if (= (length spec) 2)
203 (gnus-picon-insert-glyph (pop spec) category))))))))
205 (defun gnus-picon-transform-newsgroups (header)
207 (gnus-with-article-headers
208 (gnus-article-goto-header header)
209 (mail-header-narrow-to-field)
210 (let ((groups (message-tokenize-header (mail-fetch-field header)))
212 (dolist (group groups)
213 (unless (setq spec (cdr (assoc group gnus-picon-cache)))
214 (setq spec (nreverse (split-string group "[.]")))
215 (dotimes (i (length spec))
216 (when (setq file (gnus-picon-find-face
219 'identity (nthcdr i spec) "."))
220 gnus-picon-news-directories t))
221 (setcar (nthcdr i spec)
222 (cons (gnus-picon-create-glyph file)
224 (push (cons group spec) gnus-picon-cache))
225 (when (search-forward group nil t)
226 (delete-region (match-beginning 0) (match-end 0))
228 (narrow-to-region (point) (point))
230 (goto-char (point-min))
231 (if (> (length spec) 1)
233 (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
234 (goto-char (point-max))))))))
238 ;; #### NOTE: the test for buffer-read-only is the same as in
239 ;; article-display-[x-]face. See the comment up there.
242 (defun gnus-treat-from-picon ()
243 "Display picons in the From header.
244 If picons are already displayed, remove them."
246 (let ((wash-picon-p buffer-read-only))
247 (gnus-with-article-buffer
248 (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
249 (gnus-delete-images 'from-picon)
250 (gnus-picon-transform-address "from" 'from-picon)))
254 (defun gnus-treat-mail-picon ()
255 "Display picons in the Cc and To headers.
256 If picons are already displayed, remove them."
258 (let ((wash-picon-p buffer-read-only))
259 (gnus-with-article-buffer
260 (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
261 (gnus-delete-images 'mail-picon)
262 (gnus-picon-transform-address "cc" 'mail-picon)
263 (gnus-picon-transform-address "to" 'mail-picon)))
267 (defun gnus-treat-newsgroups-picon ()
268 "Display picons in the Newsgroups and Followup-To headers.
269 If picons are already displayed, remove them."
271 (let ((wash-picon-p buffer-read-only))
272 (gnus-with-article-buffer
273 (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
274 (gnus-delete-images 'newsgroups-picon)
275 (gnus-picon-transform-newsgroups "newsgroups")
276 (gnus-picon-transform-newsgroups "followup-to")))
279 (provide 'gnus-picon)
281 ;;; gnus-picon.el ends here