* texi/gnus-ja.texi (Article Date): Update Japanese translation.
[elisp/gnus.git-] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
7 ;; Keywords: news xpm annotation glyph faces
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; There are three picon types relevant to Gnus:
29 ;;
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
38
39 ;;; Code:
40
41 (eval-when-compile (require 'cl))
42
43 (require 'gnus)
44 (require 'custom)
45 (require 'gnus-art)
46
47 ;;; User variables:
48
49 (defgroup picon nil
50   "Show pictures of people, domains, and newsgroups."
51   :group 'gnus-visual)
52
53 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces")
54   "*Defines the location of the faces database.
55 For information on obtaining this database of pretty pictures, please
56 see http://www.cs.indiana.edu/picons/ftp/index.html"
57   :type 'directory
58   :group 'picon)
59
60 (defcustom gnus-picon-news-directories '("news")
61   "*List of directories to search for newsgroups faces."
62   :type '(repeat string)
63   :group 'picon)
64
65 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
66   "*List of directories to search for user faces."
67   :type '(repeat string)
68   :group 'picon)
69
70 (defcustom gnus-picon-domain-directories '("domains")
71   "*List of directories to search for domain faces.
72 Some people may want to add \"unknown\" to this list."
73   :type '(repeat string)
74   :group 'picon)
75
76 (defcustom gnus-picon-file-types
77   (let ((types (list "xbm")))
78     (if (gnus-image-type-available-p 'gif)
79         (setq types (cons "gif" types)))
80     (if (gnus-image-type-available-p 'xpm)
81         (setq types (cons "xpm" types)))
82     types)
83   "*List of suffixes on picon file names to try."
84   :type '(repeat string)
85   :group 'picon)
86
87 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
88   "Face to show xbm picon in."
89   :group 'picon)
90
91 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
92   "Face to show picon in."
93   :group 'picon)
94
95 ;;; Internal variables:
96
97 (defvar gnus-picon-setup-p nil)
98 (defvar gnus-picon-glyph-alist nil
99   "Picon glyphs cache.
100 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
101 (defvar gnus-picon-cache nil)
102
103 ;;; Functions:
104
105 (defsubst gnus-picon-split-address (address)
106   (setq address (split-string address "@"))
107   (if (stringp (cadr address))
108       (cons (car address) (split-string (cadr address) "\\."))
109     (if (stringp (car address))
110         (split-string (car address) "\\."))))
111
112 (defun gnus-picon-find-face (address directories &optional exact)
113   (let* ((address (gnus-picon-split-address address))
114          (user (pop address))
115          (faddress address)
116          database directory result instance base)
117     (catch 'found
118       (dolist (database gnus-picon-databases)
119         (dolist (directory directories)
120           (setq address faddress
121                 base (expand-file-name directory database))
122           (while address
123             (when (setq result (gnus-picon-find-image
124                                 (concat base "/" (mapconcat 'identity
125                                                             (reverse address)
126                                                             "/")
127                                         "/" user "/")))
128               (throw 'found result))
129             (if exact
130                 (setq address nil)
131               (pop address)))
132           ;; Kludge to search MISC as well.  But not in "news".
133           (unless (string= directory "news")
134             (when (setq result (gnus-picon-find-image
135                                 (concat base "/MISC/" user "/")))
136               (throw 'found result))))))))
137
138 (defun gnus-picon-find-image (directory)
139   (let ((types gnus-picon-file-types)
140         found type file)
141     (while (and (not found)
142                 (setq type (pop types)))
143       (setq found (file-exists-p (setq file (concat directory "face." type)))))
144     (if found
145         file
146       nil)))
147
148 (defun gnus-picon-insert-glyph (glyph category)
149   "Insert GLYPH into the buffer.
150 GLYPH can be either a glyph or a string."
151   (if (stringp glyph)
152       (insert glyph)
153     (gnus-add-wash-type category)
154     (gnus-add-image category (car glyph))
155     (gnus-put-image (car glyph) (cdr glyph))))
156
157 (defun gnus-picon-create-glyph (file)
158   (or (cdr (assoc file gnus-picon-glyph-alist))
159       (cdar (push (cons file (gnus-create-image file))
160                   gnus-picon-glyph-alist))))
161
162 ;;; Functions that does picon transformations:
163
164 (defun gnus-picon-transform-address (header category)
165   (gnus-with-article-headers
166     (let ((addresses
167            (mail-header-parse-addresses (mail-fetch-field header)))
168           spec file point cache)
169       (dolist (address addresses)
170         (setq address (car address))
171         (when (and (stringp address)
172                    (setq spec (gnus-picon-split-address address)))
173           (if (setq cache (cdr (assoc address gnus-picon-cache)))
174               (setq spec cache)
175             (when (setq file (or (gnus-picon-find-face
176                                   address gnus-picon-user-directories)
177                                  (gnus-picon-find-face
178                                   (concat "unknown@"
179                                           (mapconcat
180                                            'identity (cdr spec) "."))
181                                   gnus-picon-user-directories)))
182               (setcar spec (cons (gnus-picon-create-glyph file)
183                                  (car spec))))
184
185             (dotimes (i (1- (length spec)))
186               (when (setq file (gnus-picon-find-face
187                                 (concat "unknown@"
188                                         (mapconcat
189                                          'identity (nthcdr (1+ i) spec) "."))
190                                 gnus-picon-domain-directories t))
191                 (setcar (nthcdr (1+ i) spec)
192                         (cons (gnus-picon-create-glyph file)
193                               (nth (1+ i) spec)))))
194             (setq spec (nreverse spec))
195             (push (cons address spec) gnus-picon-cache))
196
197           (gnus-article-goto-header header)
198           (mail-header-narrow-to-field)
199           (when (search-forward address nil t)
200             (delete-region (match-beginning 0) (match-end 0))
201             (setq point (point))
202             (while spec
203               (goto-char point)
204               (if (> (length spec) 2)
205                   (insert ".")
206                 (if (= (length spec) 2)
207                   (insert "@")))
208               (gnus-picon-insert-glyph (pop spec) category))))))))
209
210 (defun gnus-picon-transform-newsgroups (header)
211   (interactive)
212   (gnus-with-article-headers
213     (gnus-article-goto-header header)
214     (mail-header-narrow-to-field)
215     (let ((groups (message-tokenize-header (mail-fetch-field header)))
216           spec file point)
217       (dolist (group groups)
218         (unless (setq spec (cdr (assoc group gnus-picon-cache)))
219           (setq spec (nreverse (split-string group "[.]")))
220           (dotimes (i (length spec))
221             (when (setq file (gnus-picon-find-face
222                               (concat "unknown@"
223                                       (mapconcat
224                                        'identity (nthcdr i spec) "."))
225                               gnus-picon-news-directories t))
226               (setcar (nthcdr i spec)
227                       (cons (gnus-picon-create-glyph file)
228                             (nth i spec)))))
229             (push (cons group spec) gnus-picon-cache))
230         (when (search-forward group nil t)
231           (delete-region (match-beginning 0) (match-end 0))
232           (save-restriction
233             (narrow-to-region (point) (point))
234             (while spec
235               (goto-char (point-min))
236               (if (> (length spec) 1)
237                   (insert "."))
238               (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
239             (goto-char (point-max))))))))
240
241 ;;; Commands:
242
243 ;;;###autoload
244 (defun gnus-treat-from-picon ()
245   "Display picons in the From header.
246 If picons are already displayed, remove them."
247   (interactive)
248   (gnus-with-article-buffer
249     (if (memq 'from-picon gnus-article-wash-types)
250         (gnus-delete-images 'from-picon)
251       (gnus-picon-transform-address "from" 'from-picon))))
252
253 ;;;###autoload
254 (defun gnus-treat-mail-picon ()
255   "Display picons in the Cc and To headers.
256 If picons are already displayed, remove them."
257   (interactive)
258   (gnus-with-article-buffer
259     (if (memq 'mail-picon gnus-article-wash-types)
260         (gnus-delete-images 'mail-picon)
261       (gnus-picon-transform-address "cc" 'mail-picon)
262       (gnus-picon-transform-address "to" 'mail-picon))))
263
264 ;;;###autoload
265 (defun gnus-treat-newsgroups-picon ()
266   "Display picons in the Newsgroups and Followup-To headers.
267 If picons are already displayed, remove them."
268   (interactive)
269   (gnus-with-article-buffer
270     (if (memq 'newsgroups-picon gnus-article-wash-types)
271         (gnus-delete-images 'newsgroups-picon)
272       (gnus-picon-transform-newsgroups "newsgroups")
273       (gnus-picon-transform-newsgroups "followup-to"))))
274
275 (provide 'gnus-picon)
276
277 ;;; gnus-picon.el ends here