Written by moi, moi, moi.
[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             (mail-encode-encoded-word-string
160              (or (mail-fetch-field header) ""))))
161           spec file point cache)
162       (dolist (address addresses)
163         (setq address (car address))
164         (when (and (stringp address)
165                    (setq spec (gnus-picon-split-address address)))
166           (if (setq cache (cdr (assoc address gnus-picon-cache)))
167               (setq spec cache)
168             (when (setq file (or (gnus-picon-find-face
169                                   address gnus-picon-user-directories)
170                                  (gnus-picon-find-face
171                                   (concat "unknown@"
172                                           (mapconcat
173                                            'identity (cdr spec) "."))
174                                   gnus-picon-user-directories)))
175               (setcar spec (cons (gnus-picon-create-glyph file)
176                                  (car spec))))
177
178             (dotimes (i (1- (length spec)))
179               (when (setq file (gnus-picon-find-face
180                                 (concat "unknown@"
181                                         (mapconcat
182                                          'identity (nthcdr (1+ i) spec) "."))
183                                 gnus-picon-domain-directories t))
184                 (setcar (nthcdr (1+ i) spec)
185                         (cons (gnus-picon-create-glyph file)
186                               (nth (1+ i) spec)))))
187             (setq spec (nreverse spec))
188             (push (cons address spec) gnus-picon-cache))
189
190           (gnus-article-goto-header header)
191           (mail-header-narrow-to-field)
192           (when (search-forward address nil t)
193             (delete-region (match-beginning 0) (match-end 0))
194             (setq point (point))
195             (while spec
196               (goto-char point)
197               (if (> (length spec) 2)
198                   (insert ".")
199                 (if (= (length spec) 2)
200                   (insert "@")))
201               (gnus-picon-insert-glyph (pop spec) category))))))))
202
203 (defun gnus-picon-transform-newsgroups (header)
204   (interactive)
205   (gnus-with-article-headers
206     (gnus-article-goto-header header)
207     (mail-header-narrow-to-field)
208     (let ((groups (message-tokenize-header (mail-fetch-field header)))
209           spec file point)
210       (dolist (group groups)
211         (unless (setq spec (cdr (assoc group gnus-picon-cache)))
212           (setq spec (nreverse (split-string group "[.]")))
213           (dotimes (i (length spec))
214             (when (setq file (gnus-picon-find-face
215                               (concat "unknown@"
216                                       (mapconcat
217                                        'identity (nthcdr i spec) "."))
218                               gnus-picon-news-directories t))
219               (setcar (nthcdr i spec)
220                       (cons (gnus-picon-create-glyph file)
221                             (nth i spec)))))
222             (push (cons group spec) gnus-picon-cache))
223         (when (search-forward group nil t)
224           (delete-region (match-beginning 0) (match-end 0))
225           (save-restriction
226             (narrow-to-region (point) (point))
227             (while spec
228               (goto-char (point-min))
229               (if (> (length spec) 1)
230                   (insert "."))
231               (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
232             (goto-char (point-max))))))))
233
234 ;;; Commands:
235
236 ;; #### NOTE: the test for buffer-read-only is the same as in
237 ;; article-display-[x-]face. See the comment up there.
238
239 ;;;###autoload
240 (defun gnus-treat-from-picon ()
241   "Display picons in the From header.
242 If picons are already displayed, remove them."
243   (interactive)
244   (let ((wash-picon-p buffer-read-only))
245     (gnus-with-article-buffer
246       (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
247           (gnus-delete-images 'from-picon)
248         (gnus-picon-transform-address "from" 'from-picon)))
249     ))
250
251 ;;;###autoload
252 (defun gnus-treat-mail-picon ()
253   "Display picons in the Cc and To headers.
254 If picons are already displayed, remove them."
255   (interactive)
256   (let ((wash-picon-p buffer-read-only))
257     (gnus-with-article-buffer
258       (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
259           (gnus-delete-images 'mail-picon)
260         (gnus-picon-transform-address "cc" 'mail-picon)
261         (gnus-picon-transform-address "to" 'mail-picon)))
262     ))
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   (let ((wash-picon-p buffer-read-only))
270     (gnus-with-article-buffer
271       (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
272           (gnus-delete-images 'newsgroups-picon)
273         (gnus-picon-transform-newsgroups "newsgroups")
274         (gnus-picon-transform-newsgroups "followup-to")))
275     ))
276
277 (provide 'gnus-picon)
278
279 ;;; gnus-picon.el ends here