1 ;;; gnus-picons.el: Icon hacks for displaying pretty icons in Gnus.
2 ;; Copyright (C) 1996 Wes Hardaker
4 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
5 ;; Keywords: gnus xpm annotation glyph faces
10 ;; - You must have XEmacs (19.12 or above I think) to use this.
11 ;; - Read the variable descriptions below.
15 ;; 1) display the icons in its own buffer:
17 ;; (add-hook 'gnus-article-display-hook 'gnus-article-display-picons t)
18 ;; (add-hook 'gnus-summary-display-hook 'gnus-group-display-picons t)
19 ;; (setq gnus-picons-display-where 'picons)
21 ;; Then add the picons buffer to your display configuration:
22 ;; The picons buffer needs to be at least 48 pixels high,
23 ;; which for me is 5 lines:
25 ;; (gnus-add-configuration
26 ;; '(article (vertical 1.0
29 ;; (summary .25 point)
32 ;; (gnus-add-configuration
33 ;; '(summary (vertical 1.0 (group 6)
35 ;; (summary 1.0 point))))
37 ;; 2) display the icons in the summary buffer
39 ;; (add-hook 'gnus-article-display-hook 'gnus-article-display-picons t)
40 ;; (add-hook 'gnus-summary-display-hook 'gnus-group-display-picons t)
41 ;; (setq gnus-picons-display-where 'summary)
43 ;; 3) display the icons in the article buffer
45 ;; (add-hook 'gnus-article-display-hook 'gnus-article-display-picons t)
46 ;; (add-hook 'gnus-article-display-hook 'gnus-group-display-picons t)
47 ;; (setq gnus-picons-display-where 'article)
51 ;; - I'm not even close to being a lisp expert.
52 ;; - The 't' (append) flag MUST be in the add-hook line
55 ;; - Remove the TODO section in the headers.
61 (require 'annotations)
62 (eval-when-compile (require 'cl))
64 (defvar gnus-picons-buffer "*Icon Buffer*"
65 "Buffer name to display the icons in if gnus-picons-display-where is 'picons.")
67 (defvar gnus-picons-display-where 'picons
68 "Where to display the group and article icons.")
70 (defvar gnus-picons-database "/usr/local/faces"
71 "Defines the location of the faces database.
72 For information on obtaining this database of pretty pictures, please
73 see http://www.cs.indiana.edu/picons/ftp/index.html" )
75 (defvar gnus-picons-news-directory "news"
76 "Sub-directory of the faces database containing the icons for newsgroups."
79 (defvar gnus-picons-user-directories '("local" "users" "usenix" "misc/MISC")
80 "List of directories to search for user faces."
83 (defvar gnus-picons-domain-directories '("domains")
84 "List of directories to search for domain faces.
85 Some people may want to add \"unknown\" to this list."
88 (defvar gnus-group-annotations nil)
89 (defvar gnus-article-annotations nil)
90 (defvar gnus-x-face-annotations nil)
92 (defun gnus-picons-remove (plist)
93 (let ((listitem (car plist)))
94 (while (setq listitem (car plist))
95 (if (annotationp listitem)
96 (delete-annotation listitem))
97 (setq plist (cdr plist))))
100 (defun gnus-picons-remove-all ()
101 "Removes all picons from the Gnus display(s)."
103 (gnus-picons-remove gnus-article-annotations)
104 (gnus-picons-remove gnus-group-annotations)
105 (setq gnus-article-annotations nil
106 gnus-group-annotations nil)
107 (if (bufferp gnus-picons-buffer)
108 (kill-buffer gnus-picons-buffer))
111 (defun gnus-get-buffer-name (variable)
112 "Returns the buffer name associated with the contents of a variable."
113 (cond ((symbolp variable)
114 (let ((newvar (cdr (assq variable gnus-window-to-buffer))))
115 (cond ((symbolp newvar)
116 (symbol-value newvar))
117 ((stringp newvar) newvar))))
121 (defvar gnus-picons-x-face-file-name
122 (format "/tmp/picon-xface.%s.xbm" (user-login-name))
123 "The name of the file in which to store the converted X-face header.")
125 (defvar gnus-picons-convert-x-face (format "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pbmtoxbm > %s" gnus-picons-x-face-file-name)
126 "Command to convert the x-face header into a xbm file."
129 (defun gnus-picons-article-display-x-face ()
130 "Display the x-face header bitmap in the 'gnus-picons-display-where buffer."
131 ;; delete any old ones.
132 (gnus-picons-remove gnus-x-face-annotations)
133 (setq gnus-x-face-annotations nil)
134 ;; display the new one.
135 (let ((gnus-article-x-face-command 'gnus-picons-display-x-face))
136 (gnus-article-display-x-face)))
138 (defun gnus-picons-display-x-face (beg end)
139 "Function to display the x-face header in the picons window.
140 To use: (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
142 ;; convert the x-face header to a .xbm file
143 (let ((process-connection-type nil)
145 (process-kill-without-query
146 (setq process (start-process
147 "gnus-x-face" nil "sh" "-c" gnus-picons-convert-x-face)))
148 (process-send-region "gnus-x-face" beg end)
149 (process-send-eof "gnus-x-face")
151 (while (not (equal (process-status process) 'exit))
155 (set-buffer (get-buffer-create (gnus-get-buffer-name
156 gnus-picons-display-where)))
157 (gnus-add-current-to-buffer-list)
158 (beginning-of-buffer)
159 (let ((iconpoint (point)))
160 (if (not (looking-at "^$"))
169 ;; append the annotation to gnus-article-annotations for deletion.
170 (setq gnus-x-face-annotations
172 (gnus-picons-try-to-find-face
173 gnus-picons-x-face-file-name iconpoint)
174 gnus-x-face-annotations)))
175 ;; delete the tmp file
176 (delete-file gnus-picons-x-face-file-name)))
178 (defun gnus-article-display-picons ()
179 "Display faces for an author and his/her domain in gnus-picons-display-where."
181 (if (and (featurep 'xpm)
182 (or (not (fboundp 'device-type)) (equal (device-type) 'x)))
184 (let* ((iconpoint (point)) (from (mail-fetch-field "from"))
187 (string-match "\\([-_a-zA-Z0-9]+\\)@" from)
188 (match-string 1 from)))
190 (concat (gnus-picons-reverse-domain-path
192 (replace-in-string from ".*@\\([_a-zA-Z0-9-.]+\\).*"
195 (switch-to-buffer (gnus-get-buffer-name gnus-picons-display-where))
196 (gnus-add-current-to-buffer-list)
197 (beginning-of-buffer)
198 (setq iconpoint (point))
199 (if (not (looking-at "^$"))
209 (gnus-picons-remove gnus-article-annotations)
210 (setq gnus-article-annotations 'nil)
211 (if (equal username from)
212 (setq username (progn
213 (string-match "<\\([_a-zA-Z0-9-.]+\\)>" from)
214 (match-string 1 from))))
215 (mapcar '(lambda (pathpart)
216 (setq gnus-article-annotations
218 (gnus-picons-insert-face-if-exists
220 (file-name-as-directory
221 gnus-picons-database) pathpart)
222 (concat hostpath username)
224 gnus-article-annotations)))
225 gnus-picons-user-directories)
226 (mapcar '(lambda (pathpart)
227 (setq gnus-article-annotations
229 (gnus-picons-insert-face-if-exists
230 (concat (file-name-as-directory
231 gnus-picons-database) pathpart)
232 (concat hostpath "unknown")
234 gnus-article-annotations)))
235 gnus-picons-domain-directories)
236 (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all)
239 (defun gnus-group-display-picons ()
240 "Display icons for the group in the gnus-picons-display-where buffer."
242 (if (and (featurep 'xpm)
243 (or (not (fboundp 'device-type)) (equal (device-type) 'x)))
246 ((iconpoint (point)))
247 (switch-to-buffer (gnus-get-buffer-name gnus-picons-display-where))
248 (gnus-add-current-to-buffer-list)
249 (beginning-of-buffer)
251 ((listp gnus-group-annotations)
252 (mapcar 'delete-annotation gnus-group-annotations)
253 (setq gnus-group-annotations nil))
254 ((annotationp gnus-group-annotations)
255 (delete-annotation gnus-group-annotations)
256 (setq gnus-group-annotations nil))
258 (setq iconpoint (point))
259 (if (not (looking-at "^$"))
261 (gnus-picons-remove gnus-group-annotations)
262 (setq gnus-group-annotations nil)
263 (setq gnus-group-annotations
264 (gnus-picons-insert-face-if-exists
265 (concat (file-name-as-directory gnus-picons-database)
266 gnus-picons-news-directory)
267 (concat (replace-in-string gnus-newsgroup-name "\\." "/")
270 (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all)))))
273 (defun gnus-picons-insert-face-if-exists (path filename ipoint &optional rev)
274 "Inserts a face at point if I can find one"
275 (let ((pathfile (concat path "/" filename "/face"))
277 (replace-in-string filename
278 "[_a-zA-Z0-9-]+/\\([_A-Za-z0-9-]+\\)$" "\\1"))
281 (not (equal filename newfilename)))
282 (setq annotations (append
283 (gnus-picons-insert-face-if-exists path newfilename ipoint rev)
285 (if (eq (length annotations) (length (setq annotations (append
286 (gnus-picons-try-to-find-face (concat pathfile ".xpm") ipoint)
288 (setq annotations (append
289 (gnus-picons-try-to-find-face
290 (concat pathfile ".xbm") ipoint)
293 (not (equal filename newfilename)))
294 (setq annotations (append
295 (gnus-picons-insert-face-if-exists path newfilename ipoint rev)
301 (defun gnus-picons-try-to-find-face (path ipoint)
302 "If PATH exists, display it as a bitmap. Returns t if succedded."
303 (when (file-exists-p path)
304 (let ((gl (make-glyph path)))
305 (set-glyph-face gl 'default)
306 (list (make-annotation gl ipoint 'text)))))
308 (defun gnus-picons-reverse-domain-path (str)
310 (if (equal (replace-in-string str "^[^/]*$" "") "")
312 (concat (replace-in-string str "^.*/\\([_a-zA-Z0-9-]+\\)$" "\\1") "/"
313 (gnus-picons-reverse-domain-path
314 (replace-in-string str "^\\(.*\\)/[_a-zA-Z0-9-]+$" "\\1")))))
316 (provide 'gnus-picon)
318 ;;; gnus-picon.el ends here