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