1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news xpm annotation glyph faces
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; There are three picon types relevant to Gnus:
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
36 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
40 ;; For Emacs <22.2 and XEmacs.
42 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
44 (eval-when-compile (require 'cl
))
51 (defcustom gnus-picon-news-directories
'("news")
52 "*List of directories to search for newsgroups faces."
53 :type
'(repeat string
)
56 (defcustom gnus-picon-user-directories
'("users" "usenix" "local" "misc")
57 "*List of directories to search for user faces."
58 :type
'(repeat string
)
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
)
67 (defcustom gnus-picon-file-types
68 (let ((types (list "xbm")))
69 (when (gnus-image-type-available-p 'gif
)
71 (when (gnus-image-type-available-p 'xpm
)
74 "*List of suffixes on picon file names to try."
75 :type
'(repeat string
)
78 (defcustom gnus-picon-properties
'(:color-symbols
(("None" .
"white")))
79 "List of image properties applied to picons."
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
)
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."
100 ;;; Internal variables:
102 (defvar gnus-picon-glyph-alist nil
104 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
105 (defvar gnus-picon-cache nil
)
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
) "\\."))))
116 (defun gnus-picon-find-face (address directories
&optional exact
)
117 (let* ((address (gnus-picon-split-address address
))
120 database directory result instance base
)
122 (dolist (database gnus-picon-databases
)
123 (dolist (directory directories
)
124 (setq address faddress
125 base
(expand-file-name directory database
))
127 (when (setq result
(gnus-picon-find-image
128 (concat base
"/" (mapconcat 'downcase
131 "/" (downcase user
) "/")))
132 (throw 'found result
))
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
))))))))
142 (defun gnus-picon-find-image (directory)
143 (let ((types gnus-picon-file-types
)
145 (while (and (not found
)
146 (setq type
(pop types
)))
147 (setq found
(file-exists-p (setq file
(concat directory
"face." type
)))))
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
160 (gnus-add-wash-type category
)
161 (gnus-add-image category
(car glyph
))
162 (gnus-put-image (car glyph
) (unless nostring
(cdr glyph
)) category
)))
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
168 gnus-picon-properties
))
169 gnus-picon-glyph-alist
))))
171 ;;; Functions that does picon transformations:
173 (declare-function image-size
"image.c" (spec &optional pixels frame
))
175 (defun gnus-picon-transform-address (header category
)
176 (gnus-with-article-headers
178 (mail-header-parse-addresses
179 ;; mail-header-parse-addresses does not work (reliably) on
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
)))
193 (when (setq file
(or (gnus-picon-find-face
194 address gnus-picon-user-directories
)
195 (gnus-picon-find-face
198 'identity
(cdr spec
) "."))
199 gnus-picon-user-directories
)))
200 (setcar spec
(cons (gnus-picon-create-glyph file
)
203 (dotimes (i (- (length spec
)
204 (if gnus-picon-inhibit-top-level-domains
206 (when (setq file
(gnus-picon-find-face
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
))
217 (gnus-article-goto-header header
)
218 (mail-header-narrow-to-field)
219 (case gnus-picon-style
221 (when (= (length addresses
) 1)
222 (setq len
(apply '+ (mapcar (lambda (x)
224 (car (image-size (car x
)))
227 (goto-char (point-at-eol))
231 (list :align-to
(- (window-width) 1 len
))))))
232 (goto-char (point-at-eol))
233 (setq point
(point-at-eol))
235 (unless (stringp image
)
237 (gnus-picon-insert-glyph image category
'nostring
)))))
239 (when (search-forward address nil t
)
240 (delete-region (match-beginning 0) (match-end 0))
244 (if (> (length spec
) 2)
246 (if (= (length spec
) 2)
248 (gnus-picon-insert-glyph (pop spec
) category
))))))))))
250 (defun gnus-picon-transform-newsgroups (header)
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
)))
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
264 'identity
(nthcdr i spec
) "."))
265 gnus-picon-news-directories t
))
266 (setcar (nthcdr i spec
)
267 (cons (gnus-picon-create-glyph file
)
269 (push (cons group spec
) gnus-picon-cache
))
270 (when (search-forward group nil t
)
271 (delete-region (match-beginning 0) (match-end 0))
273 (narrow-to-region (point) (point))
275 (goto-char (point-min))
276 (if (> (length spec
) 1)
278 (gnus-picon-insert-glyph (pop spec
) 'newsgroups-picon
))
279 (goto-char (point-max))))))))
283 ;; #### NOTE: the test for buffer-read-only is the same as in
284 ;; article-display-[x-]face. See the comment up there.
287 (defun gnus-treat-from-picon ()
288 "Display picons in the From header.
289 If picons are already displayed, remove them."
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
)))))
298 (defun gnus-treat-mail-picon ()
299 "Display picons in the Cc and To headers.
300 If picons are already displayed, remove them."
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
)))))
310 (defun gnus-treat-newsgroups-picon ()
311 "Display picons in the Newsgroups and Followup-To headers.
312 If picons are already displayed, remove them."
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")))))
321 (provide 'gnus-picon
)
323 ;;; gnus-picon.el ends here