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 (gnus-get-buffer-name gnus-picons-display-where))
156 (gnus-add-current-to-buffer-list)
157 (beginning-of-buffer)
158 (let ((iconpoint (point)))
159 (if (not (looking-at "^$"))
168 ;; append the annotation to gnus-article-annotations for deletion.
169 (setq gnus-x-face-annotations
171 (gnus-picons-try-to-find-face
172 gnus-picons-x-face-file-name iconpoint)
173 gnus-x-face-annotations)))
174 ;; delete the tmp file
175 (delete-file gnus-picons-x-face-file-name)))
177 (defun gnus-article-display-picons ()
178 "Display faces for an author and his/her domain in gnus-picons-display-where."
180 (if (and (featurep 'xpm)
181 (or (not (fboundp 'device-type)) (equal (device-type) 'x)))
183 (let* ((iconpoint (point)) (from (mail-fetch-field "from"))
186 (string-match "\\([-_a-zA-Z0-9]+\\)@" from)
187 (match-string 1 from)))
189 (concat (gnus-picons-reverse-domain-path
191 (replace-in-string from ".*@\\([_a-zA-Z0-9-.]+\\).*"
194 (switch-to-buffer (gnus-get-buffer-name gnus-picons-display-where))
195 (gnus-add-current-to-buffer-list)
196 (beginning-of-buffer)
197 (setq iconpoint (point))
198 (if (not (looking-at "^$"))
208 (gnus-picons-remove gnus-article-annotations)
209 (setq gnus-article-annotations 'nil)
210 (if (equal username from)
211 (setq username (progn
212 (string-match "<\\([_a-zA-Z0-9-.]+\\)>" from)
213 (match-string 1 from))))
214 (mapcar '(lambda (pathpart)
215 (setq gnus-article-annotations
217 (gnus-picons-insert-face-if-exists
219 (file-name-as-directory
220 gnus-picons-database) pathpart)
221 (concat hostpath username)
223 gnus-article-annotations)))
224 gnus-picons-user-directories)
225 (mapcar '(lambda (pathpart)
226 (setq gnus-article-annotations
228 (gnus-picons-insert-face-if-exists
229 (concat (file-name-as-directory
230 gnus-picons-database) pathpart)
231 (concat hostpath "unknown")
233 gnus-article-annotations)))
234 gnus-picons-domain-directories)
235 (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all)
238 (defun gnus-group-display-picons ()
239 "Display icons for the group in the gnus-picons-display-where buffer."
241 (if (and (featurep 'xpm)
242 (or (not (fboundp 'device-type)) (equal (device-type) 'x)))
245 ((iconpoint (point)))
246 (switch-to-buffer (gnus-get-buffer-name gnus-picons-display-where))
247 (gnus-add-current-to-buffer-list)
248 (beginning-of-buffer)
250 ((listp gnus-group-annotations)
251 (mapcar 'delete-annotation gnus-group-annotations)
252 (setq gnus-group-annotations nil))
253 ((annotationp gnus-group-annotations)
254 (delete-annotation gnus-group-annotations)
255 (setq gnus-group-annotations nil))
257 (setq iconpoint (point))
258 (if (not (looking-at "^$"))
260 (gnus-picons-remove gnus-group-annotations)
261 (setq gnus-group-annotations nil)
262 (setq gnus-group-annotations
263 (gnus-picons-insert-face-if-exists
264 (concat (file-name-as-directory gnus-picons-database)
265 gnus-picons-news-directory)
266 (concat (replace-in-string gnus-newsgroup-name "\\." "/")
269 (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all)))))
272 (defun gnus-picons-insert-face-if-exists (path filename ipoint &optional rev)
273 "Inserts a face at point if I can find one"
274 (let ((pathfile (concat path "/" filename "/face"))
276 (replace-in-string filename
277 "[_a-zA-Z0-9-]+/\\([_A-Za-z0-9-]+\\)$" "\\1"))
280 (not (equal filename newfilename)))
281 (setq annotations (append
282 (gnus-picons-insert-face-if-exists path newfilename ipoint rev)
284 (if (eq (length annotations) (length (setq annotations (append
285 (gnus-picons-try-to-find-face (concat pathfile ".xpm") ipoint)
287 (setq annotations (append
288 (gnus-picons-try-to-find-face
289 (concat pathfile ".xbm") ipoint)
292 (not (equal filename newfilename)))
293 (setq annotations (append
294 (gnus-picons-insert-face-if-exists path newfilename ipoint rev)
300 (defun gnus-picons-try-to-find-face (path ipoint)
301 "If PATH exists, display it as a bitmap. Returns t if succedded."
302 (when (file-exists-p path)
303 (let ((gl (make-glyph path)))
304 (set-glyph-face gl 'default)
305 (list (make-annotation gl ipoint 'text)))))
307 (defun gnus-picons-reverse-domain-path (str)
309 (if (equal (replace-in-string str "^[^/]*$" "") "")
311 (concat (replace-in-string str "^.*/\\([_a-zA-Z0-9-]+\\)$" "\\1") "/"
312 (gnus-picons-reverse-domain-path
313 (replace-in-string str "^\\(.*\\)/[_a-zA-Z0-9-]+$" "\\1")))))
315 (provide 'gnus-picon)
317 ;;; gnus-picon.el ends here