* nnmairix.el: Remove old documentation in the commentary block.
[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 ;;   2005, 2006, 2007, 2008, 2009 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 3 of the License, or
14 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; There are three picon types relevant to Gnus:
27 ;;
28 ;; Persons: person@subdomain.dom
29 ;;          users/dom/subdomain/person/face.gif
30 ;;          usenix/dom/subdomain/person/face.gif
31 ;;          misc/MISC/person/face.gif
32 ;; Domains: subdomain.dom
33 ;;          domain/dom/subdomain/unknown/face.gif
34 ;; Groups:  comp.lang.lisp
35 ;;          news/comp/lang/lisp/unknown/face.gif
36 ;;
37 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
38 ;;
39 ;;; Code:
40
41 ;; For Emacs < 22.2.
42 (eval-and-compile
43   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
44
45 (eval-when-compile (require 'cl))
46
47 (require 'gnus)
48 (require 'gnus-art)
49
50 ;;; User variables:
51
52 (defcustom gnus-picon-news-directories '("news")
53   "*List of directories to search for newsgroups faces."
54   :type '(repeat string)
55   :group 'gnus-picon)
56
57 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
58   "*List of directories to search for user faces."
59   :type '(repeat string)
60   :group 'gnus-picon)
61
62 (defcustom gnus-picon-domain-directories '("domains")
63   "*List of directories to search for domain faces.
64 Some people may want to add \"unknown\" to this list."
65   :type '(repeat string)
66   :group 'gnus-picon)
67
68 (defcustom gnus-picon-file-types
69   (let ((types (list "xbm")))
70     (when (gnus-image-type-available-p 'gif)
71       (push "gif" types))
72     (when (gnus-image-type-available-p 'xpm)
73       (push "xpm" types))
74     types)
75   "*List of suffixes on picon file names to try."
76   :type '(repeat string)
77   :group 'gnus-picon)
78
79 (defcustom gnus-picon-style 'inline
80   "How should picons be displayed.
81 If `inline', the textual representation is replaced.  If `right', picons are
82 added right to the textual representation."
83   ;; FIXME: `right' needs improvement for XEmacs.
84   :type '(choice (const inline)
85                  (const right))
86   :group 'gnus-picon)
87
88 (defface gnus-picon-xbm '((t (:foreground "black" :background "white")))
89   "Face to show xbm picon in."
90   :group 'gnus-picon)
91 ;; backward-compatibility alias
92 (put 'gnus-picon-xbm-face 'face-alias 'gnus-picon-xbm)
93
94 (defface gnus-picon '((t (:foreground "black" :background "white")))
95   "Face to show picon in."
96   :group 'gnus-picon)
97 ;; backward-compatibility alias
98 (put 'gnus-picon-face 'face-alias 'gnus-picon)
99
100 ;;; Internal variables:
101
102 (defvar gnus-picon-setup-p nil)
103 (defvar gnus-picon-glyph-alist nil
104   "Picon glyphs cache.
105 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
106 (defvar gnus-picon-cache nil)
107
108 ;;; Functions:
109
110 (defsubst gnus-picon-split-address (address)
111   (setq address (split-string address "@"))
112   (if (stringp (cadr address))
113       (cons (car address) (split-string (cadr address) "\\."))
114     (if (stringp (car address))
115         (split-string (car address) "\\."))))
116
117 (defun gnus-picon-find-face (address directories &optional exact)
118   (let* ((address (gnus-picon-split-address address))
119          (user (pop address))
120          (faddress address)
121          database directory result instance base)
122     (catch 'found
123       (dolist (database gnus-picon-databases)
124         (dolist (directory directories)
125           (setq address faddress
126                 base (expand-file-name directory database))
127           (while address
128             (when (setq result (gnus-picon-find-image
129                                 (concat base "/" (mapconcat 'downcase
130                                                             (reverse address)
131                                                             "/")
132                                         "/" (downcase user) "/")))
133               (throw 'found result))
134             (if exact
135                 (setq address nil)
136               (pop address)))
137           ;; Kludge to search MISC as well.  But not in "news".
138           (unless (string= directory "news")
139             (when (setq result (gnus-picon-find-image
140                                 (concat base "/MISC/" user "/")))
141               (throw 'found result))))))))
142
143 (defun gnus-picon-find-image (directory)
144   (let ((types gnus-picon-file-types)
145         found type file)
146     (while (and (not found)
147                 (setq type (pop types)))
148       (setq found (file-exists-p (setq file (concat directory "face." type)))))
149     (if found
150         file
151       nil)))
152
153 (defun gnus-picon-insert-glyph (glyph category &optional nostring)
154   "Insert GLYPH into the buffer.
155 GLYPH can be either a glyph or a string.  When NOSTRING, no textual
156 replacement is added."
157   ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
158   ;; 'right.
159   (if (stringp glyph)
160       (insert glyph)
161     (gnus-add-wash-type category)
162     (gnus-add-image category (car glyph))
163     (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category)))
164
165 (defun gnus-picon-create-glyph (file)
166   (or (cdr (assoc file gnus-picon-glyph-alist))
167       (cdar (push (cons file (gnus-create-image file))
168                   gnus-picon-glyph-alist))))
169
170 ;;; Functions that does picon transformations:
171
172 (declare-function image-size "image.c" (spec &optional pixels frame))
173
174 (defun gnus-picon-transform-address (header category)
175   (gnus-with-article-headers
176    (let ((addresses
177           (mail-header-parse-addresses
178            ;; mail-header-parse-addresses does not work (reliably) on
179            ;; decoded headers.
180            (or
181             (ignore-errors
182              (mail-encode-encoded-word-string
183               (or (mail-fetch-field header) "")))
184             (mail-fetch-field header))))
185          spec file point cache len)
186      (dolist (address addresses)
187        (setq address (car address))
188        (when (and (stringp address)
189                   (setq spec (gnus-picon-split-address address)))
190          (if (setq cache (cdr (assoc address gnus-picon-cache)))
191              (setq spec cache)
192            (when (setq file (or (gnus-picon-find-face
193                                  address gnus-picon-user-directories)
194                                 (gnus-picon-find-face
195                                  (concat "unknown@"
196                                          (mapconcat
197                                           'identity (cdr spec) "."))
198                                  gnus-picon-user-directories)))
199              (setcar spec (cons (gnus-picon-create-glyph file)
200                                 (car spec))))
201
202            (dotimes (i (1- (length spec)))
203              (when (setq file (gnus-picon-find-face
204                                (concat "unknown@"
205                                        (mapconcat
206                                         'identity (nthcdr (1+ i) spec) "."))
207                                gnus-picon-domain-directories t))
208                (setcar (nthcdr (1+ i) spec)
209                        (cons (gnus-picon-create-glyph file)
210                              (nth (1+ i) spec)))))
211            (setq spec (nreverse spec))
212            (push (cons address spec) gnus-picon-cache))
213
214          (gnus-article-goto-header header)
215          (mail-header-narrow-to-field)
216          (case gnus-picon-style
217                (right
218                 (when (= (length addresses) 1)
219                   (setq len (apply '+ (mapcar (lambda (x)
220                                                 (condition-case nil
221                                                     (car (image-size (car x)))
222                                                   (error 0))) spec)))
223                   (when (> len 0)
224                     (goto-char (point-at-eol))
225                     (insert (propertize
226                              " " 'display
227                              (cons 'space
228                                    (list :align-to (- (window-width) 1 len))))))
229                   (goto-char (point-at-eol))
230                   (setq point (point-at-eol))
231                   (dolist (image spec)
232                     (unless (stringp image)
233                       (goto-char point)
234                       (gnus-picon-insert-glyph image category 'nostring)))))
235                (inline
236                  (when (search-forward address nil t)
237                    (delete-region (match-beginning 0) (match-end 0))
238                    (setq point (point))
239                    (while spec
240                      (goto-char point)
241                      (if (> (length spec) 2)
242                          (insert ".")
243                        (if (= (length spec) 2)
244                            (insert "@")))
245                      (gnus-picon-insert-glyph (pop spec) category))))))))))
246
247 (defun gnus-picon-transform-newsgroups (header)
248   (interactive)
249   (gnus-with-article-headers
250    (gnus-article-goto-header header)
251    (mail-header-narrow-to-field)
252    (let ((groups (message-tokenize-header (mail-fetch-field header)))
253          spec file point)
254      (dolist (group groups)
255        (unless (setq spec (cdr (assoc group gnus-picon-cache)))
256          (setq spec (nreverse (split-string group "[.]")))
257          (dotimes (i (length spec))
258            (when (setq file (gnus-picon-find-face
259                              (concat "unknown@"
260                                      (mapconcat
261                                       'identity (nthcdr i spec) "."))
262                              gnus-picon-news-directories t))
263              (setcar (nthcdr i spec)
264                      (cons (gnus-picon-create-glyph file)
265                            (nth i spec)))))
266          (push (cons group spec) gnus-picon-cache))
267        (when (search-forward group nil t)
268          (delete-region (match-beginning 0) (match-end 0))
269          (save-restriction
270            (narrow-to-region (point) (point))
271            (while spec
272              (goto-char (point-min))
273              (if (> (length spec) 1)
274                  (insert "."))
275              (gnus-picon-insert-glyph (pop spec) 'newsgroups-picon))
276            (goto-char (point-max))))))))
277
278 ;;; Commands:
279
280 ;; #### NOTE: the test for buffer-read-only is the same as in
281 ;; article-display-[x-]face. See the comment up there.
282
283 ;;;###autoload
284 (defun gnus-treat-from-picon ()
285   "Display picons in the From header.
286 If picons are already displayed, remove them."
287   (interactive)
288   (let ((wash-picon-p buffer-read-only))
289     (gnus-with-article-buffer
290      (if (and wash-picon-p (memq 'from-picon gnus-article-wash-types))
291          (gnus-delete-images 'from-picon)
292        (gnus-picon-transform-address "from" 'from-picon)))))
293
294 ;;;###autoload
295 (defun gnus-treat-mail-picon ()
296   "Display picons in the Cc and To headers.
297 If picons are already displayed, remove them."
298   (interactive)
299   (let ((wash-picon-p buffer-read-only))
300     (gnus-with-article-buffer
301      (if (and wash-picon-p (memq 'mail-picon gnus-article-wash-types))
302          (gnus-delete-images 'mail-picon)
303        (gnus-picon-transform-address "cc" 'mail-picon)
304        (gnus-picon-transform-address "to" 'mail-picon)))))
305
306 ;;;###autoload
307 (defun gnus-treat-newsgroups-picon ()
308   "Display picons in the Newsgroups and Followup-To headers.
309 If picons are already displayed, remove them."
310   (interactive)
311   (let ((wash-picon-p buffer-read-only))
312     (gnus-with-article-buffer
313      (if (and wash-picon-p (memq 'newsgroups-picon gnus-article-wash-types))
314          (gnus-delete-images 'newsgroups-picon)
315        (gnus-picon-transform-newsgroups "newsgroups")
316        (gnus-picon-transform-newsgroups "followup-to")))))
317
318 (provide 'gnus-picon)
319
320 ;; arch-tag: fe9aede0-1b1b-463a-b4ab-807f98bcb31f
321 ;;; gnus-picon.el ends here