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