Add arch taglines
[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, 2004
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 (defcustom gnus-picon-style 'inline
78   "How should picons be displayed.
79 If `inline', the textual representation is replaced.  If `right', picons are
80 added right to the textual representation."
81   ;; FIXME: `right' needs improvement for XEmacs.
82   :type '(choice (const inline)
83                  (const right))
84   :group 'gnus-picon)
85
86 (defface gnus-picon-xbm-face '((t (:foreground "black" :background "white")))
87   "Face to show xbm picon in."
88   :group 'gnus-picon)
89
90 (defface gnus-picon-face '((t (:foreground "black" :background "white")))
91   "Face to show picon in."
92   :group 'gnus-picon)
93
94 ;;; Internal variables:
95
96 (defvar gnus-picon-setup-p nil)
97 (defvar gnus-picon-glyph-alist nil
98   "Picon glyphs cache.
99 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
100 (defvar gnus-picon-cache nil)
101
102 ;;; Functions:
103
104 (defsubst gnus-picon-split-address (address)
105   (setq address (split-string address "@"))
106   (if (stringp (cadr address))
107       (cons (car address) (split-string (cadr address) "\\."))
108     (if (stringp (car address))
109         (split-string (car address) "\\."))))
110
111 (defun gnus-picon-find-face (address directories &optional exact)
112   (let* ((address (gnus-picon-split-address address))
113          (user (pop address))
114          (faddress address)
115          database directory result instance base)
116     (catch 'found
117       (dolist (database gnus-picon-databases)
118         (dolist (directory directories)
119           (setq address faddress
120                 base (expand-file-name directory database))
121           (while address
122             (when (setq result (gnus-picon-find-image
123                                 (concat base "/" (mapconcat 'downcase
124                                                             (reverse address)
125                                                             "/")
126                                         "/" (downcase user) "/")))
127               (throw 'found result))
128             (if exact
129                 (setq address nil)
130               (pop address)))
131           ;; Kludge to search MISC as well.  But not in "news".
132           (unless (string= directory "news")
133             (when (setq result (gnus-picon-find-image
134                                 (concat base "/MISC/" user "/")))
135               (throw 'found result))))))))
136
137 (defun gnus-picon-find-image (directory)
138   (let ((types gnus-picon-file-types)
139         found type file)
140     (while (and (not found)
141                 (setq type (pop types)))
142       (setq found (file-exists-p (setq file (concat directory "face." type)))))
143     (if found
144         file
145       nil)))
146
147 (defun gnus-picon-insert-glyph (glyph category &optional nostring)
148   "Insert GLYPH into the buffer.
149 GLYPH can be either a glyph or a string.  When NOSTRING, no textual
150 replacement is added."
151   ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
152   ;; 'right.
153   (if (stringp glyph)
154       (insert glyph)
155     (gnus-add-wash-type category)
156     (gnus-add-image category (car glyph))
157     (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category)))
158
159 (defun gnus-picon-create-glyph (file)
160   (or (cdr (assoc file gnus-picon-glyph-alist))
161       (cdar (push (cons file (gnus-create-image file))
162                   gnus-picon-glyph-alist))))
163
164 ;;; Functions that does picon transformations:
165
166 (defun gnus-picon-transform-address (header category)
167   (gnus-with-article-headers
168     (let ((addresses
169            (mail-header-parse-addresses
170             ;; mail-header-parse-addresses does not work (reliably) on
171             ;; decoded headers.
172             (or
173              (ignore-errors
174                (mail-encode-encoded-word-string
175                 (or (mail-fetch-field header) "")))
176              (mail-fetch-field header))))
177           spec file point cache len)
178       (dolist (address addresses)
179         (setq address (car address))
180         (when (and (stringp address)
181                    (setq spec (gnus-picon-split-address address)))
182           (if (setq cache (cdr (assoc address gnus-picon-cache)))
183               (setq spec cache)
184             (when (setq file (or (gnus-picon-find-face
185                                   address gnus-picon-user-directories)
186                                  (gnus-picon-find-face
187                                   (concat "unknown@"
188                                           (mapconcat
189                                            'identity (cdr spec) "."))
190                                   gnus-picon-user-directories)))
191               (setcar spec (cons (gnus-picon-create-glyph file)
192                                  (car spec))))
193
194             (dotimes (i (1- (length spec)))
195               (when (setq file (gnus-picon-find-face
196                                 (concat "unknown@"
197                                         (mapconcat
198                                          'identity (nthcdr (1+ i) spec) "."))
199                                 gnus-picon-domain-directories t))
200                 (setcar (nthcdr (1+ i) spec)
201                         (cons (gnus-picon-create-glyph file)
202                               (nth (1+ i) spec)))))
203             (setq spec (nreverse spec))
204             (push (cons address spec) gnus-picon-cache))
205
206           (gnus-article-goto-header header)
207           (mail-header-narrow-to-field)
208           (case gnus-picon-style
209             (right
210              (when (= (length addresses) 1)
211                (setq len (apply '+ (mapcar (lambda (x)
212                                              (condition-case nil
213                                                  (car (image-size (car x)))
214                                                (error 0))) spec)))
215                (when (> len 0)
216                  (goto-char (point-at-eol))
217                  (insert (propertize
218                           " " 'display
219                           (cons 'space
220                                 (list :align-to (- (window-width) 1 len))))))
221                (goto-char (point-at-eol))
222                (setq point (point-at-eol))
223                (dolist (image spec)
224                  (unless (stringp image)
225                    (goto-char point)
226                    (gnus-picon-insert-glyph image category 'nostring)))))
227             (inline
228               (when (search-forward address nil t)
229                 (delete-region (match-beginning 0) (match-end 0))
230                 (setq point (point))
231                 (while spec
232                   (goto-char point)
233                   (if (> (length spec) 2)
234                       (insert ".")
235                     (if (= (length spec) 2)
236                         (insert "@")))
237                   (gnus-picon-insert-glyph (pop spec) category)))))
238               )))))
239
240 (defun gnus-picon-transform-newsgroups (header)
241   (interactive)
242   (gnus-with-article-headers
243     (gnus-article-goto-header header)
244     (mail-header-narrow-to-field)
245     (let ((groups (message-tokenize-header (mail-fetch-field header)))
246           spec file point)
247       (dolist (group groups)
248         (unless (setq spec (cdr (assoc group gnus-picon-cache)))
249           (setq spec (nreverse (split-string group "[.]")))
250           (dotimes (i (length spec))
251             (when (setq file (gnus-picon-find-face
252                               (concat "unknown@"
253                                       (mapconcat
254                                        'identity (nthcdr i spec) "."))
255                               gnus-picon-news-directories t))
256               (setcar (nthcdr i spec)
257                       (cons (gnus-picon-create-glyph file)
258                             (nth i spec)))))
259             (push (cons group spec) gnus-picon-cache))
260         (when (search-forward group nil t)
261           (delete-region (match-beginning 0) (match-end 0))
262           (save-restriction
263             (narrow-to-region (point) (point))
264             (while spec
265               (goto-char (point-min))
266               (if (> (length spec) 1)
267                   (insert "."))
268               (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
269             (goto-char (point-max))))))))
270
271 ;;; Commands:
272
273 ;; #### NOTE: the test for buffer-read-only is the same as in
274 ;; article-display-[x-]face. See the comment up there.
275
276 ;;;###autoload
277 (defun gnus-treat-from-picon ()
278   "Display picons in the From header.
279 If picons are already displayed, remove them."
280   (interactive)
281   (let ((wash-picon-p buffer-read-only))
282     (gnus-with-article-buffer
283       (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
284           (gnus-delete-images 'from-picon)
285         (gnus-picon-transform-address "from" 'from-picon)))
286     ))
287
288 ;;;###autoload
289 (defun gnus-treat-mail-picon ()
290   "Display picons in the Cc and To headers.
291 If picons are already displayed, remove them."
292   (interactive)
293   (let ((wash-picon-p buffer-read-only))
294     (gnus-with-article-buffer
295       (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
296           (gnus-delete-images 'mail-picon)
297         (gnus-picon-transform-address "cc" 'mail-picon)
298         (gnus-picon-transform-address "to" 'mail-picon)))
299     ))
300
301 ;;;###autoload
302 (defun gnus-treat-newsgroups-picon ()
303   "Display picons in the Newsgroups and Followup-To headers.
304 If picons are already displayed, remove them."
305   (interactive)
306   (let ((wash-picon-p buffer-read-only))
307     (gnus-with-article-buffer
308       (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
309           (gnus-delete-images 'newsgroups-picon)
310         (gnus-picon-transform-newsgroups "newsgroups")
311         (gnus-picon-transform-newsgroups "followup-to")))
312     ))
313
314 (provide 'gnus-picon)
315
316 ;;; arch-tag: fe9aede0-1b1b-463a-b4ab-807f98bcb31f
317 ;;; gnus-picon.el ends here