T-gnus 6.14.6; synch up with Gnus v5.8.8.
[elisp/gnus.git-] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
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 2, or (at your option)
14 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 ;; (require 'xpm)
34 (require 'annotations)
35 (require 'custom)
36 (require 'gnus-art)
37 (require 'gnus-win)
38
39 ;;; User variables:
40
41 (defgroup picons nil
42   "Show pictures of people, domains, and newsgroups (XEmacs).
43 For this to work, you must switch on the `gnus-treat-display-picons'
44 variable."
45   :group 'gnus-visual)
46
47 (defcustom gnus-picons-display-where 'picons
48   "Where to display the group and article icons.
49 Valid values are `article' and `picons'."
50   :type '(choice symbol string)
51   :group 'picons)
52
53 (defcustom gnus-picons-has-modeline-p t
54   "*Whether the picons window should have a modeline.
55 This is only useful if `gnus-picons-display-where' is `picons'."
56   :type 'boolean
57   :group 'picons)
58
59 (defcustom gnus-picons-database "/usr/local/faces"
60   "*Defines the location of the faces database.
61 For information on obtaining this database of pretty pictures, please
62 see http://www.cs.indiana.edu/picons/ftp/index.html"
63   :type 'directory
64   :group 'picons)
65
66 (defcustom gnus-picons-news-directories '("news")
67   "*List of directories to search for newsgroups faces."
68   :type '(repeat string)
69   :group 'picons)
70 (define-obsolete-variable-alias 'gnus-picons-news-directory
71   'gnus-picons-news-directories)
72
73 (defcustom gnus-picons-user-directories '("local" "users" "usenix" "misc")
74   "*List of directories to search for user faces."
75   :type '(repeat string)
76   :group 'picons)
77
78 (defcustom gnus-picons-domain-directories '("domains")
79   "*List of directories to search for domain faces.
80 Some people may want to add \"unknown\" to this list."
81   :type '(repeat string)
82   :group 'picons)
83
84 (defcustom gnus-picons-refresh-before-display nil
85   "*If non-nil, display the article buffer before computing the picons."
86   :type 'boolean
87   :group 'picons)
88
89 (defcustom gnus-picons-group-excluded-groups nil
90   "*If this regexp matches the group name, group picons will be disabled."
91   :type 'regexp
92   :group 'picons)
93
94 (defcustom gnus-picons-display-as-address t
95   "*If t display textual email addresses along with pictures."
96   :type 'boolean
97   :group 'picons)
98
99 (defcustom gnus-picons-file-suffixes
100   (when (featurep 'x)
101     (let ((types (list "xbm")))
102       (when (featurep 'gif)
103         (push "gif" types))
104       (when (featurep 'xpm)
105         (push "xpm" types))
106       types))
107   "*List of suffixes on picon file names to try."
108   :type '(repeat string)
109   :group 'picons)
110
111 (defcustom gnus-picons-display-article-move-p nil
112   "*Whether to move point to first empty line when displaying picons.
113 This has only an effect if `gnus-picons-display-where' has value `article'."
114   :type 'boolean
115   :group 'picons)
116
117 (defcustom gnus-picons-clear-cache-on-shutdown t
118   "*Whether to clear the picons cache when exiting gnus.
119 Gnus caches every picons it finds while it is running.  This saves
120 some time in the search process but eats some memory.  If this
121 variable is set to nil, Gnus will never clear the cache itself; you
122 will have to manually call `gnus-picons-clear-cache' to clear it.
123 Otherwise the cache will be cleared every time you exit Gnus."
124   :type 'boolean
125   :group 'picons)
126
127 (defcustom gnus-picons-piconsearch-url nil
128   "*The url to query for picons.  Setting this to nil will disable it.
129 The only publicly available address currently known is
130 http://www.cs.indiana.edu:800/piconsearch.  If you know of any other,
131 please tell me so that we can list it."
132   :type '(choice (const :tag "Disable" :value nil)
133                  (const :tag "www.cs.indiana.edu"
134                         :value "http://www.cs.indiana.edu:800/piconsearch")
135                  (string))
136   :group 'picons)
137
138 (defface gnus-picons-xbm-face '((t (:foreground "black" :background "white")))
139   "Face to show xbm picons in."
140   :group 'picons)
141
142 (defface gnus-picons-face '((t (:foreground "black" :background "white")))
143   "Face to show picons in."
144   :group 'picons)
145
146 (defcustom gnus-picons-setup-hook nil
147   "Hook run in Picons buffers."
148   :group 'picons
149   :type 'hook)
150
151 ;;; Internal variables:
152
153 (defvar gnus-picons-setup-p nil)
154 (defvar gnus-picons-processes-alist nil
155   "Picons processes currently running and their environment.")
156 (defvar gnus-picons-glyph-alist nil
157   "Picons glyphs cache.
158 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
159 (defvar gnus-picons-url-alist nil
160   "Picons file names cache.
161 List of pairs (KEY . NAME) where KEY is (USER HOST DBS) and NAME is an URL.")
162
163 (defvar gnus-picons-jobs-alist nil
164   "List of jobs that still need be done.
165 This is a list of (SYM-ANN TAG ARGS...) where SYM-ANN three annotations list,
166 TAG is one of `picon' or `search' indicating that the job should query a
167 picon or do a search for picons file names, and ARGS is some additionnal
168 arguments necessary for the job.")
169
170 (defvar gnus-picons-job-already-running nil
171   "Lock to ensure only one stream of http requests is running.")
172
173 ;;; Functions:
174
175 (defun gnus-picons-remove-all ()
176   "Removes all picons from the Gnus display(s)."
177   (interactive)
178   (map-extents (function (lambda (ext unused) (delete-annotation ext) nil))
179                nil nil nil nil nil 'gnus-picon)
180   (setq gnus-picons-jobs-alist '())
181   ;; notify running job that it may have been preempted
182   (if (and (listp gnus-picons-job-already-running)
183            gnus-picons-job-already-running)
184       (setq gnus-picons-job-already-running t)))
185
186 (defun gnus-get-buffer-name (variable)
187   "Returns the buffer name associated with the contents of a variable."
188   (let ((buf (gnus-get-buffer-create
189               (gnus-window-to-buffer-helper
190                (cdr (assq variable gnus-window-to-buffer))))))
191     (and buf
192          (buffer-name buf))))
193
194 (defun gnus-picons-buffer-name ()
195   (cond ((or (stringp gnus-picons-display-where)
196              (bufferp gnus-picons-display-where))
197          gnus-picons-display-where)
198         ((eq gnus-picons-display-where 'picons)
199          (if gnus-single-article-buffer
200              "*Picons*"
201            (concat "*Picons " gnus-newsgroup-name "*")))
202         (t
203          (gnus-get-buffer-name gnus-picons-display-where))))
204
205 (defun gnus-picons-kill-buffer ()
206   (let ((buf (get-buffer (gnus-picons-buffer-name))))
207     (when (and (buffer-live-p buf)
208                (string-match "Picons" (buffer-name buf)))
209       (kill-buffer buf))))
210
211 (defun gnus-picons-setup-buffer ()
212   (let ((name (gnus-picons-buffer-name)))
213     (save-excursion
214       (if (and (get-buffer name)
215                (with-current-buffer name
216                  gnus-picons-setup-p))
217           (set-buffer name)
218         (set-buffer (gnus-get-buffer-create name))
219         (buffer-disable-undo)
220         (setq buffer-read-only t)
221         (run-hooks 'gnus-picons-setup-hook)
222         (set (make-local-variable 'gnus-picons-setup-p) t)
223         (add-hook 'gnus-summary-prepare-exit-hook 'gnus-picons-kill-buffer))
224       (current-buffer))))
225
226 (defun gnus-picons-set-buffer ()
227   (set-buffer (gnus-picons-setup-buffer))
228   (goto-char (point-min))
229   (if (and (eq gnus-picons-display-where 'article)
230            gnus-picons-display-article-move-p)
231       (if (search-forward "\n\n" nil t)
232           (forward-line -1)
233         (goto-char (point-max)))
234     (setq buffer-read-only t)
235     (unless gnus-picons-has-modeline-p
236       (set-specifier has-modeline-p
237                      (list (list (current-buffer)
238                                  (cons nil gnus-picons-has-modeline-p)))))))
239
240 (defun gnus-picons-prepare-for-annotations ()
241   "Prepare picons buffer for putting annotations."
242   ;; let drawing catch up
243   (when gnus-picons-refresh-before-display
244     (sit-for 0))
245   (gnus-picons-set-buffer)
246   (gnus-picons-remove-all))
247
248 (defun gnus-picons-make-annotation (&rest args)
249   (let ((annot (apply 'make-annotation args)))
250     (set-extent-property annot 'gnus-picon t)
251     (set-extent-property annot 'duplicable t)
252     annot))
253
254 (defun gnus-article-display-picons ()
255   "Display faces for an author and her domain in gnus-picons-display-where."
256   (interactive)
257   (let (from at-idx)
258     (when (and (featurep 'xpm)
259                (or (not (fboundp 'device-type)) (equal (device-type) 'x))
260                (setq from (mail-fetch-field "from"))
261                (setq from (downcase (or (cadr (mail-extract-address-components
262                                                from))
263                                         "")))
264                (or (setq at-idx (string-match "@" from))
265                    (setq at-idx (length from))))
266       (save-excursion
267         (let ((username (downcase (substring from 0 at-idx)))
268               (addrs (if (eq at-idx (length from))
269                          (if gnus-local-domain
270                              (message-tokenize-header gnus-local-domain "."))
271                        (message-tokenize-header (substring from (1+ at-idx))
272                                                 "."))))
273           (gnus-picons-prepare-for-annotations)
274           (gnus-group-display-picons)
275           (unless gnus-picons-display-article-move-p
276             (let ((buffer-read-only nil)
277                   (case-fold-search t))
278               (when (re-search-forward "^From *: *" nil t)
279                 (when (search-forward from (gnus-point-at-eol) t)
280                   (gnus-put-text-property
281                    (match-beginning 0) (match-end 0)
282                    'invisible t)))))
283           (if (null gnus-picons-piconsearch-url)
284               (progn
285                 (gnus-picons-display-pairs (gnus-picons-lookup-pairs
286                                             addrs
287                                             gnus-picons-domain-directories)
288                                            gnus-picons-display-as-address
289                                            "." t)
290                 (if (and gnus-picons-display-as-address addrs)
291                     (gnus-picons-make-annotation
292                      [string :data "@"] nil 'text nil nil nil t))
293                 (gnus-picons-display-picon-or-name
294                  (gnus-picons-lookup-user username addrs)
295                  username t))
296             (push (list 'gnus-article-annotations 'search username addrs
297                         gnus-picons-domain-directories t (point-marker))
298                   gnus-picons-jobs-alist)
299             (gnus-picons-next-job)))))))
300
301 (defun gnus-group-display-picons ()
302   "Display icons for the group in the `gnus-picons-display-where' buffer."
303   (interactive)
304   (when (and (featurep 'xpm)
305              (or (not (fboundp 'device-type)) (equal (device-type) 'x))
306              (or (null gnus-picons-group-excluded-groups)
307                  (not (string-match gnus-picons-group-excluded-groups
308                                     gnus-newsgroup-name))))
309     (let* ((newsgroups (mail-fetch-field "newsgroups"))
310            (groups
311             (if (or gnus-picons-display-article-move-p
312                     (not newsgroups))
313                 (list (gnus-group-real-name gnus-newsgroup-name))
314               (split-string newsgroups ",")))
315            group)
316       (save-excursion
317         (gnus-picons-prepare-for-annotations)
318         (while (setq group (pop groups))
319           (unless gnus-picons-display-article-move-p
320             (let ((buffer-read-only nil)
321                   (case-fold-search t))
322               (goto-char (point-min))
323               (if (and (re-search-forward "^Newsgroups *: *" nil t)
324                        (search-forward group (gnus-point-at-eol) t))
325                   (gnus-put-text-property
326                    (match-beginning 0) (match-end 0)
327                    'invisible t)
328                 (let ((article-goto-body-goes-to-point-min-p nil))
329                   (article-goto-body))
330                 (unless (bobp)
331                   (backward-char 1)))))
332           (if (null gnus-picons-piconsearch-url)
333               (gnus-picons-display-pairs
334                (gnus-picons-lookup-pairs
335                 (reverse (split-string group "\\."))
336                 gnus-picons-news-directories)
337                t ".")
338             (push (list 'gnus-group-annotations 'search nil
339                         (split-string group "\\.")
340                         (if (listp gnus-picons-news-directories)
341                             gnus-picons-news-directories
342                           (list gnus-picons-news-directories))
343                         nil (point-marker))
344                   gnus-picons-jobs-alist)
345             (gnus-picons-next-job))
346
347           (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))))
348
349 (defun gnus-picons-lookup-internal (addrs dir)
350   (setq dir (expand-file-name dir gnus-picons-database))
351   (gnus-picons-try-face (dolist (part (reverse addrs) dir)
352                           (setq dir (expand-file-name part dir)))))
353
354 (defun gnus-picons-lookup (addrs dirs)
355   "Lookup the picon for ADDRS in databases DIRS.
356 Returns the picon filename or NIL if none found."
357   (let (result)
358     (while (and dirs (null result))
359       (setq result (gnus-picons-lookup-internal addrs (pop dirs))))
360     result))
361
362 (defun gnus-picons-lookup-user-internal (user domains)
363   (let ((dirs gnus-picons-user-directories)
364         domains-tmp dir picon)
365     (while (and dirs (null picon))
366       (setq domains-tmp domains
367             dir (pop dirs))
368       (while (and domains-tmp
369                   (null (setq picon (gnus-picons-lookup-internal
370                                      (cons user domains-tmp) dir))))
371         (pop domains-tmp))
372       ;; Also make a try in MISC subdir
373       (unless picon
374         (setq picon (gnus-picons-lookup-internal (list user "MISC") dir))))
375     picon))
376
377 (defun gnus-picons-lookup-user (user domains)
378   "Lookup the picon for USER at DOMAINS.
379 USER is a string containing a name.
380 DOMAINS is a list of strings from the fully qualified domain name."
381   (or (gnus-picons-lookup-user-internal user domains)
382       (gnus-picons-lookup-user-internal "unknown" domains)))
383
384 (defun gnus-picons-lookup-pairs (domains directories)
385   "Lookup picons for DOMAINS and all its parents in DIRECTORIES.
386 Returns a list of PAIRS whose CAR is the picon filename or NIL if
387 none, and whose CDR is the corresponding element of DOMAINS."
388   (let (picons)
389     (setq directories (if (listp directories)
390                           directories
391                         (list directories)))
392     (while domains
393       (push (list (gnus-picons-lookup (cons "unknown" domains) directories)
394                   (pop domains))
395             picons))
396     picons))
397
398 (defun gnus-picons-display-picon-or-name (picon name &optional right-p)
399   (cond (picon (gnus-picons-display-glyph picon name right-p))
400         (gnus-picons-display-as-address (list (gnus-picons-make-annotation
401                                                (vector 'string :data name)
402                                                nil 'text
403                                                nil nil nil right-p)))))
404
405 (defun gnus-picons-display-pairs (pairs &optional bar-p dot-p right-p)
406   "Display picons in list PAIRS."
407   (let ((domain-p (and gnus-picons-display-as-address dot-p))
408         pair picons)
409     (when (and bar-p domain-p right-p
410                gnus-picons-display-article-move-p)
411       (setq picons (gnus-picons-display-glyph
412                     (let ((gnus-picons-file-suffixes '("xbm")))
413                       (gnus-picons-try-face
414                        gnus-xmas-glyph-directory "bar."))
415                     nil right-p)))
416     (while (setq pair (pop pairs))
417       (setq picons (nconc picons
418                           (gnus-picons-display-picon-or-name
419                            (car pair) (cadr pair) right-p)
420                           (if (and domain-p pairs)
421                               (list (gnus-picons-make-annotation
422                                      (vector 'string :data dot-p)
423                                      nil 'text nil nil nil right-p))))))
424     picons))
425
426 (defun gnus-picons-try-face (dir &optional filebase)
427   (let* ((dir (file-name-as-directory dir))
428          (filebase (or filebase "face."))
429          (key (concat dir filebase))
430          (glyph (cdr (assoc key gnus-picons-glyph-alist)))
431          (suffixes gnus-picons-file-suffixes)
432          f suf)
433     (while (setq suf (pop suffixes))
434       (when (file-exists-p (setq f (expand-file-name
435                                     (concat filebase suf)
436                                     dir)))
437         (setq suffixes nil
438               glyph (make-glyph f))
439         (if (equal suf "xbm")
440             (set-glyph-face glyph 'gnus-picons-xbm-face)
441           (set-glyph-face glyph 'gnus-picons-face))
442         (push (cons key glyph) gnus-picons-glyph-alist)))
443     glyph))
444
445 (defun gnus-picons-display-glyph (glyph &optional part rightp)
446   (set-glyph-baseline glyph 70)
447   (let ((new (gnus-picons-make-annotation
448               glyph (point) 'text nil nil nil rightp)))
449     (when (and part gnus-picons-display-as-address)
450       (set-annotation-data
451        new (cons new (make-glyph (vector 'string :data part))))
452       (set-annotation-action new 'gnus-picons-action-toggle))
453     (nconc
454      (list new)
455      (if (and (eq major-mode 'gnus-article-mode)
456               (not gnus-picons-display-as-address)
457               (not part))
458          (list (gnus-picons-make-annotation [string :data " "] (point)
459                                             'text nil nil nil rightp))))))
460
461 (defun gnus-picons-action-toggle (data)
462   "Toggle annotation."
463   (interactive "e")
464   (let* ((annot (car data))
465          (glyph (annotation-glyph annot)))
466     (set-annotation-glyph annot (cdr data))
467     (set-annotation-data annot (cons annot glyph))))
468
469 (defun gnus-picons-clear-cache ()
470   "Clear the picons cache."
471   (interactive)
472   (setq gnus-picons-glyph-alist nil
473         gnus-picons-url-alist nil))
474
475 (gnus-add-shutdown 'gnus-picons-close 'gnus)
476
477 (defun gnus-picons-close ()
478   "Shut down the picons."
479   (if gnus-picons-clear-cache-on-shutdown
480       (gnus-picons-clear-cache)))
481
482 ;;; Query a remote DB.  This requires some stuff from w3 !
483
484 (eval-and-compile
485   (ignore-errors
486     (require 'url)
487     (require 'w3-forms)))
488
489 (defun gnus-picons-url-retrieve (url fn arg)
490   (let ((old-asynch (default-value 'url-be-asynchronous))
491         (url-working-buffer (generate-new-buffer " *picons*"))
492         (url-package-name "Gnus")
493         (url-package-version gnus-version-number)
494         url-request-method)
495     (setq-default url-be-asynchronous t)
496     (save-excursion
497       (set-buffer url-working-buffer)
498       (setq url-be-asynchronous t
499             url-current-callback-data arg
500             url-current-callback-func fn)
501       (url-retrieve url t))
502     (setq-default url-be-asynchronous old-asynch)))
503
504 (defun gnus-picons-make-glyph (type)
505   "Make a TYPE glyph using current buffer as data.  Handles xbm nicely."
506   (cond ((null type) nil)
507         ((eq type 'xbm) (let ((fname (make-temp-name "/tmp/picon")))
508                           (write-region (point-min) (point-max) fname
509                                         nil 'quiet)
510                           (prog1 (make-glyph (vector 'xbm :file fname))
511                             (delete-file fname))))
512         (t (make-glyph (vector type :data (buffer-string))))))
513
514 ;;; Parsing of piconsearch result page.
515
516 ;; Assumes:
517 ;; 1 - each value field has the form: "<strong>key</strong> = <kbd>value</kbd>"
518 ;; 2 - a "<p>" separates the keywords from the results
519 ;; 3 - every results begins by the path within the database at the beginning
520 ;;     of the line in raw text.
521 ;; 3b - and the href following it is the preferred image type.
522
523 ;; if 1 or 2 is not met, it will probably cause an error.  The other
524 ;; will go undetected
525
526 (defun gnus-picons-parse-value (name)
527   (goto-char (point-min))
528   (if (re-search-forward (concat "<strong>"
529                                  (regexp-quote name)
530                                  "</strong> *= *<kbd> *\\([^ <][^<]*\\) *</kbd>")
531                          nil t)
532       (buffer-substring (match-beginning 1) (match-end 1))))
533
534 (defun gnus-picons-parse-filenames ()
535   ;; returns an alist of ((USER ADDRS DB) . URL)
536   (let ((case-fold-search t)
537         (user (gnus-picons-parse-value "user"))
538         (host (gnus-picons-parse-value "host"))
539         (dbs (message-tokenize-header (gnus-picons-parse-value "db") " "))
540         start-re cur-db cur-host cur-user types res)
541     ;; now point will be somewhere in the header.  Find beginning of
542     ;; entries
543     (when (and user host dbs)
544       (setq start-re
545             (concat
546              ;; dbs
547              "^\\(" (mapconcat 'regexp-quote dbs "\\|") "\\)/"
548              ;; host
549              "\\(\\(" (mapconcat 'regexp-quote
550                                  (message-tokenize-header host ".") "/\\|")
551              "/\\|MISC/\\)*\\)"
552              ;; user
553              "\\(" (regexp-quote user) "\\|unknown\\)/"
554              "face\\."))
555       (re-search-forward "<p>[ \t\n]*")
556       (while (re-search-forward start-re nil t)
557         (setq cur-db (buffer-substring (match-beginning 1) (match-end 1))
558               cur-host (buffer-substring (match-beginning 2) (match-end 2))
559               cur-user (buffer-substring (match-beginning 4) (match-end 4))
560               cur-host (nreverse (message-tokenize-header cur-host "/")))
561         ;; XXX - KLUDGE: there is a blank picon in news/MISC/unknown
562         (unless (and (string-equal cur-db "news")
563                      (string-equal cur-user "unknown")
564                      (equal cur-host '("MISC")))
565           ;; ok now we have found an entry (USER HOST DB), find the
566           ;; corresponding picon URL
567           (save-restriction
568             ;; restrict region to this entry
569             (narrow-to-region (point) (search-forward "<br>"))
570             (goto-char (point-min))
571             (setq types gnus-picons-file-suffixes)
572             (while (and types
573                         (not (re-search-forward
574                               (concat "<a[ \t\n]+href=\"\\([^\"]*\\."
575                                       (regexp-quote (car types)) "\\)\"")
576                               nil t)))
577               (pop types))
578             (push (cons (list cur-user cur-host cur-db)
579                         (buffer-substring (match-beginning 1) (match-end 1)))
580                   res))))
581       (nreverse res))))
582
583 ;;; picon network display functions :
584
585 (defun gnus-picons-network-display-internal (sym-ann glyph part right-p marker)
586   (let ((buf (marker-buffer marker))
587         (pos (marker-position marker)))
588     (if (and buf pos)
589         (save-excursion
590           (set-buffer buf)
591           (goto-char pos)
592           (gnus-picons-display-picon-or-name glyph part right-p))))
593   (gnus-picons-next-job-internal))
594
595 (defun gnus-picons-network-display-callback (url part sym-ann right-p marker)
596   (let ((glyph (gnus-picons-make-glyph (cdr (assoc url-current-mime-type
597                                                    w3-image-mappings)))))
598     (kill-buffer (current-buffer))
599     (push (cons url glyph) gnus-picons-glyph-alist)
600     ;; only do the job if it has not been preempted.
601     (if (equal gnus-picons-job-already-running
602                (list sym-ann 'picon url part right-p marker))
603         (gnus-picons-network-display-internal
604          sym-ann glyph part right-p marker)
605       (gnus-picons-next-job-internal))))
606
607 (defun gnus-picons-network-display (url part sym-ann right-p marker)
608   (let ((cache (assoc url gnus-picons-glyph-alist)))
609     (if (or cache (null url))
610         (gnus-picons-network-display-internal
611          sym-ann (cdr cache) part right-p marker)
612       (gnus-picons-url-retrieve url 'gnus-picons-network-display-callback
613                                 (list url part sym-ann right-p marker)))))
614
615 ;;; search job functions
616
617 (defun gnus-picons-display-bar-p ()
618   (if (eq gnus-picons-display-where 'article)
619       gnus-picons-display-article-move-p
620     gnus-picons-display-as-address))
621
622 (defun gnus-picons-network-search-internal (user addrs dbs sym-ann right-p
623                                                  marker &optional fnames)
624   (let (curkey dom pfx url dbs-tmp cache new-jobs)
625     ;; First do the domain search
626     (dolist (part (if right-p
627                       (reverse addrs)
628                     addrs))
629       (setq pfx (nconc (list part) pfx)
630             dom (cond ((and dom right-p) (concat part "." dom))
631                       (dom (concat dom "." part))
632                       (t part))
633             curkey (list "unknown" dom dbs))
634       (when (null (setq cache (assoc curkey gnus-picons-url-alist)))
635         ;; This one is not yet in the cache, create a new entry
636         ;; Search for an entry
637         (setq dbs-tmp dbs
638               url nil)
639         (while (and dbs-tmp (null url))
640           (setq url (or (cdr (assoc (list "unknown" pfx (car dbs-tmp)) fnames))
641                         (and (eq dom part)
642                              ;; This is the first component.  Try the
643                              ;; catch-all MISC component
644                              (cdr (assoc (list "unknown"
645                                                '("MISC")
646                                                (car dbs-tmp))
647                                          fnames)))))
648           (pop dbs-tmp))
649         (push (setq cache (cons curkey url)) gnus-picons-url-alist))
650       ;; Put this glyph in the job list
651       (if (and (not (eq dom part)) gnus-picons-display-as-address)
652           (push (list sym-ann "." right-p marker) new-jobs))
653       (push (list sym-ann 'picon (cdr cache) part right-p marker) new-jobs))
654     ;; next, the user search
655     (when user
656       (setq curkey (list user dom gnus-picons-user-directories))
657       (if (null (setq cache (assoc curkey gnus-picons-url-alist)))
658           (let ((users (list user "unknown"))
659                 dirs usr domains-tmp dir picon)
660             (while (and users (null picon))
661               (setq dirs gnus-picons-user-directories
662                     usr (pop users))
663               (while (and dirs (null picon))
664                 (setq domains-tmp addrs
665                       dir (pop dirs))
666                 (while (and domains-tmp
667                             (null (setq picon (assoc (list usr domains-tmp dir)
668                                                      fnames))))
669                   (pop domains-tmp))
670                 (unless picon
671                   (setq picon (assoc (list usr '("MISC") dir) fnames)))))
672             (push (setq cache (cons curkey (cdr picon)))
673                   gnus-picons-url-alist)))
674       (if (and gnus-picons-display-as-address new-jobs)
675           (push (list sym-ann "@" right-p marker) new-jobs))
676       (push (list sym-ann 'picon (cdr cache) user right-p marker) new-jobs))
677     (if (and (gnus-picons-display-bar-p) (not right-p))
678         (push (list sym-ann 'bar right-p marker) new-jobs))
679     ;; only put the jobs in the queue if this job has not been preempted.
680     (if (equal gnus-picons-job-already-running
681                (list sym-ann 'search user addrs dbs right-p marker))
682         (setq gnus-picons-jobs-alist
683               (nconc (if (and (gnus-picons-display-bar-p) right-p)
684                          (list (list sym-ann 'bar right-p marker)))
685                      (nreverse new-jobs)
686                      gnus-picons-jobs-alist)))
687     (gnus-picons-next-job-internal)))
688
689 (defun gnus-picons-network-search-callback (user addrs dbs sym-ann right-p
690                                                  marker)
691   (gnus-picons-network-search-internal
692    user addrs dbs sym-ann right-p marker
693    (prog1
694        (gnus-picons-parse-filenames)
695      (kill-buffer (current-buffer)))))
696
697 ;; Initiate a query on the picon database
698 (defun gnus-picons-network-search (user addrs dbs sym-ann right-p marker)
699   (let* ((host (mapconcat 'identity addrs "."))
700          (key (list (or user "unknown") host (if user
701                                                  gnus-picons-user-directories
702                                                dbs)))
703          (cache (assoc key gnus-picons-url-alist)))
704     (if (null cache)
705         (gnus-picons-url-retrieve
706          (concat gnus-picons-piconsearch-url
707                  "?user=" (w3-form-encode-xwfu (or user "unknown"))
708                  "&host=" (w3-form-encode-xwfu host)
709                  "&db=" (mapconcat 'w3-form-encode-xwfu
710                                    (if user
711                                        (append dbs
712                                                gnus-picons-user-directories)
713                                      dbs)
714                                    "+"))
715          'gnus-picons-network-search-callback
716          (list user addrs dbs sym-ann right-p marker))
717       (gnus-picons-network-search-internal
718        user addrs dbs sym-ann right-p marker))))
719
720 ;;; Main jobs dispatcher function
721
722 (defun gnus-picons-next-job-internal ()
723   (when (setq gnus-picons-job-already-running (pop gnus-picons-jobs-alist))
724     (let* ((job gnus-picons-job-already-running)
725            (sym-ann (pop job))
726            (tag (pop job)))
727       (when tag
728         (cond
729          ((stringp tag);; (SYM-ANN "..." RIGHT-P MARKER)
730           (gnus-picons-network-display-internal
731            sym-ann nil tag (pop job) (pop job)))
732          ((eq 'bar tag);; (SYM-ANN 'bar RIGHT-P MARKER)
733           (gnus-picons-network-display-internal
734            sym-ann
735            (let ((gnus-picons-file-suffixes '("xbm")))
736              (gnus-picons-try-face
737               gnus-xmas-glyph-directory "bar."))
738            nil (pop job) (pop job)))
739          ((eq 'search tag);; (SYM-ANN 'search USER ADDRS DBS RIGHT-P MARKER)
740           (gnus-picons-network-search
741            (pop job) (pop job) (pop job) sym-ann (pop job) (pop job)))
742          ((eq 'picon tag);; (SYM-ANN 'picon URL PART RIGHT-P MARKER)
743           (gnus-picons-network-display
744            (pop job) (pop job) sym-ann (pop job) (pop job)))
745          (t
746           (setq gnus-picons-job-already-running nil)
747           (error "Unknown picon job tag %s" tag)))))))
748
749 (defun gnus-picons-next-job ()
750   "Start processing the job queue if it is not in progress."
751   (unless gnus-picons-job-already-running
752     (gnus-picons-next-job-internal)))
753
754 (provide 'gnus-picon)
755
756 ;;; gnus-picon.el ends here