(gnus-picon-transform-address): Protect against
[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: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
40 ;;
41 ;;; Code:
42
43 (require 'gnus)
44 (require 'custom)
45 (require 'gnus-art)
46
47 ;;; User variables:
48
49 (defcustom gnus-picon-news-directories '("news")
50   "*List of directories to search for newsgroups faces."
51   :type '(repeat string)
52   :group 'gnus-picon)
53
54 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
55   "*List of directories to search for user faces."
56   :type '(repeat string)
57   :group 'gnus-picon)
58
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)
63   :group 'gnus-picon)
64
65 (defcustom gnus-picon-file-types
66   (let ((types (list "xbm")))
67     (when (gnus-image-type-available-p 'gif)
68       (push "gif" types))
69     (when (gnus-image-type-available-p 'xpm)
70       (push "xpm" types))
71     types)
72   "*List of suffixes on picon file names to try."
73   :type '(repeat string)
74   :group 'gnus-picon)
75
76 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
77   "Face to show xbm picon in."
78   :group 'gnus-picon)
79
80 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
81   "Face to show picon in."
82   :group 'gnus-picon)
83
84 ;;; Internal variables:
85
86 (defvar gnus-picon-setup-p nil)
87 (defvar gnus-picon-glyph-alist nil
88   "Picon glyphs cache.
89 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
90 (defvar gnus-picon-cache nil)
91
92 ;;; Functions:
93
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) "\\."))))
100
101 (defun gnus-picon-find-face (address directories &optional exact)
102   (let* ((address (gnus-picon-split-address address))
103          (user (pop address))
104          (faddress address)
105          database directory result instance base)
106     (catch 'found
107       (dolist (database gnus-picon-databases)
108         (dolist (directory directories)
109           (setq address faddress
110                 base (expand-file-name directory database))
111           (while address
112             (when (setq result (gnus-picon-find-image
113                                 (concat base "/" (mapconcat 'downcase
114                                                             (reverse address)
115                                                             "/")
116                                         "/" (downcase user) "/")))
117               (throw 'found result))
118             (if exact
119                 (setq address nil)
120               (pop address)))
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))))))))
126
127 (defun gnus-picon-find-image (directory)
128   (let ((types gnus-picon-file-types)
129         found type file)
130     (while (and (not found)
131                 (setq type (pop types)))
132       (setq found (file-exists-p (setq file (concat directory "face." type)))))
133     (if found
134         file
135       nil)))
136
137 (defun gnus-picon-insert-glyph (glyph category)
138   "Insert GLYPH into the buffer.
139 GLYPH can be either a glyph or a string."
140   (if (stringp glyph)
141       (insert glyph)
142     (gnus-add-wash-type category)
143     (gnus-add-image category (car glyph))
144     (gnus-put-image (car glyph) (cdr glyph) category)))
145
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))))
150
151 ;;; Functions that does picon transformations:
152
153 (defun gnus-picon-transform-address (header category)
154   (gnus-with-article-headers
155     (let ((addresses
156            (mail-header-parse-addresses
157             ;; mail-header-parse-addresses does not work (reliably) on
158             ;; decoded headers.
159             (or
160              (ignore-errors
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)))
170               (setq spec cache)
171             (when (setq file (or (gnus-picon-find-face
172                                   address gnus-picon-user-directories)
173                                  (gnus-picon-find-face
174                                   (concat "unknown@"
175                                           (mapconcat
176                                            'identity (cdr spec) "."))
177                                   gnus-picon-user-directories)))
178               (setcar spec (cons (gnus-picon-create-glyph file)
179                                  (car spec))))
180
181             (dotimes (i (1- (length spec)))
182               (when (setq file (gnus-picon-find-face
183                                 (concat "unknown@"
184                                         (mapconcat
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))
192
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))
197             (setq point (point))
198             (while spec
199               (goto-char point)
200               (if (> (length spec) 2)
201                   (insert ".")
202                 (if (= (length spec) 2)
203                   (insert "@")))
204               (gnus-picon-insert-glyph (pop spec) category))))))))
205
206 (defun gnus-picon-transform-newsgroups (header)
207   (interactive)
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)))
212           spec file point)
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
218                               (concat "unknown@"
219                                       (mapconcat
220                                        'identity (nthcdr i spec) "."))
221                               gnus-picon-news-directories t))
222               (setcar (nthcdr i spec)
223                       (cons (gnus-picon-create-glyph file)
224                             (nth i spec)))))
225             (push (cons group spec) gnus-picon-cache))
226         (when (search-forward group nil t)
227           (delete-region (match-beginning 0) (match-end 0))
228           (save-restriction
229             (narrow-to-region (point) (point))
230             (while spec
231               (goto-char (point-min))
232               (if (> (length spec) 1)
233                   (insert "."))
234               (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
235             (goto-char (point-max))))))))
236
237 ;;; Commands:
238
239 ;; #### NOTE: the test for buffer-read-only is the same as in
240 ;; article-display-[x-]face. See the comment up there.
241
242 ;;;###autoload
243 (defun gnus-treat-from-picon ()
244   "Display picons in the From header.
245 If picons are already displayed, remove them."
246   (interactive)
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)))
252     ))
253
254 ;;;###autoload
255 (defun gnus-treat-mail-picon ()
256   "Display picons in the Cc and To headers.
257 If picons are already displayed, remove them."
258   (interactive)
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)))
265     ))
266
267 ;;;###autoload
268 (defun gnus-treat-newsgroups-picon ()
269   "Display picons in the Newsgroups and Followup-To headers.
270 If picons are already displayed, remove them."
271   (interactive)
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")))
278     ))
279
280 (provide 'gnus-picon)
281
282 ;;; gnus-picon.el ends here