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