Sync up with Pteroductyl Gnus 0.62
[elisp/gnus.git-] / lisp / gnus-picon.el
index 7f04650..4f50461 100644 (file)
@@ -117,7 +117,7 @@ Some people may want to add \"unknown\" to this list."
   :type '(repeat string)
   :group 'picons)
 
-(defcustom gnus-picons-display-article-move-p t
+(defcustom gnus-picons-display-article-move-p nil
   "*Whether to move point to first empty line when displaying picons.
 This has only an effect if `gnus-picons-display-where' has value `article'."
   :type 'boolean
@@ -145,7 +145,7 @@ please tell me so that we can list it."
   :group 'picons)
 
 (defface gnus-picons-xbm-face '((t (:foreground "black" :background "white")))
-  "Face to show X face"
+  "Face to show xbm picons in."
   :group 'picons)
 
 ;;; Internal variables:
@@ -184,17 +184,41 @@ arguments necessary for the job.")
 
 (defun gnus-get-buffer-name (variable)
   "Returns the buffer name associated with the contents of a variable."
-  (cond ((symbolp variable) (let ((newvar (cdr (assq variable
-                                                    gnus-window-to-buffer))))
-                             (cond ((symbolp newvar)
-                                    (symbol-value newvar))
-                                   ((stringp newvar) newvar))))
-        ((stringp variable) variable)))
+  (let ((buf (gnus-get-buffer-create (gnus-window-to-buffer-helper
+                                (cdr 
+                                 (assq variable gnus-window-to-buffer))))))
+    (and buf
+        (buffer-name buf))))
+
+(defun gnus-picons-buffer-name ()
+  (cond ((or (stringp gnus-picons-display-where)
+            (bufferp gnus-picons-display-where))
+        gnus-picons-display-where)
+       ((eq gnus-picons-display-where 'picons)
+        (if gnus-single-article-buffer
+            "*Picons*"
+          (concat "*Picons " gnus-newsgroup-name "*")))
+       (t
+        (gnus-get-buffer-name gnus-picons-display-where))))
+
+(defun gnus-picons-kill-buffer ()
+  (let ((buf (get-buffer (gnus-picons-buffer-name))))
+    (if (buffer-live-p buf)
+       (kill-buffer buf))))
+
+(defun gnus-picons-setup-buffer ()
+  (let ((name (gnus-picons-buffer-name)))
+    (save-excursion
+      (if (get-buffer name)
+         (set-buffer name)
+       (set-buffer (gnus-get-buffer-create name))
+       (buffer-disable-undo)
+       (setq buffer-read-only t)
+       (add-hook 'gnus-summary-prepare-exit-hook 'gnus-picons-kill-buffer))
+      (current-buffer))))
 
 (defun gnus-picons-set-buffer ()
-  (set-buffer
-   (get-buffer-create (gnus-get-buffer-name gnus-picons-display-where)))
-  (gnus-add-current-to-buffer-list)
+  (set-buffer (gnus-picons-setup-buffer))
   (goto-char (point-min))
   (if (and (eq gnus-picons-display-where 'article)
           gnus-picons-display-article-move-p)
@@ -233,7 +257,8 @@ arguments necessary for the job.")
     (gnus-picons-set-buffer)
     (gnus-picons-make-annotation (make-glyph gnus-picons-x-face-file-name)
                                 nil 'text)
-    (delete-file gnus-picons-x-face-file-name)))
+    (when (file-exists-p gnus-picons-x-face-file-name)
+      (delete-file gnus-picons-x-face-file-name))))
 
 (defun gnus-picons-display-x-face (beg end)
   "Function to display the x-face header in the picons window.
@@ -242,11 +267,16 @@ To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
   (if (featurep 'xface)
       ;; Use builtin support
       (save-excursion
-       (gnus-picons-set-buffer)
-       (gnus-picons-make-annotation
-        (vector 'xface
-                :data (concat "X-Face: " (buffer-substring beg end)))
-        nil 'text))
+       ;; Don't remove this binding, it is really needed: when
+       ;; `gnus-picons-set-buffer' changes buffer (like when it is
+       ;; set to display picons outside of the article buffer), BEG
+       ;; and END still refer the buffer current now !
+       (let ((buf (current-buffer)))
+         (gnus-picons-set-buffer)
+         (gnus-picons-make-annotation
+          (vector 'xface
+                  :data (concat "X-Face: " (buffer-substring beg end buf)))
+          nil 'text nil nil nil t)))
     ;; convert the x-face header to a .xbm file
     (let* ((process-connection-type nil)
           (process (start-process-shell-command "gnus-x-face" nil 
@@ -264,8 +294,9 @@ To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
     (when (and (featurep 'xpm)
               (or (not (fboundp 'device-type)) (equal (device-type) 'x))
               (setq from (mail-fetch-field "from"))
-              (setq from (downcase (or (cadr (mail-extract-address-components
-                                              from))
+              (setq from (downcase (or (cadr
+                                        (funcall gnus-extract-address-components
+                                                 from))
                                        "")))
               (or (setq at-idx (string-match "@" from))
                   (setq at-idx (length from))))
@@ -278,6 +309,15 @@ To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
                                                "."))))
          (gnus-picons-prepare-for-annotations)
          (gnus-group-display-picons)
+         (unless gnus-picons-display-article-move-p
+           (save-restriction
+             (let ((buffer-read-only nil))
+               (when (re-search-forward "^From: " nil t)
+                 (narrow-to-region (point) (gnus-point-at-eol))
+                 (when (search-forward from nil t)
+                   (gnus-put-text-property
+                    (match-beginning 0) (match-end 0)
+                    'invisible t))))))
          (if (null gnus-picons-piconsearch-url)
              (progn
                (gnus-picons-display-pairs (gnus-picons-lookup-pairs
@@ -304,27 +344,42 @@ To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
             (or (null gnus-picons-group-excluded-groups)
                 (not (string-match gnus-picons-group-excluded-groups
                                    gnus-newsgroup-name))))
-    (save-excursion
-      (gnus-picons-prepare-for-annotations)
-      (if (null gnus-picons-piconsearch-url)
-         (gnus-picons-display-pairs
-                (gnus-picons-lookup-pairs
-                 (reverse (message-tokenize-header
-                           (gnus-group-real-name gnus-newsgroup-name) 
-                           "."))
-                 gnus-picons-news-directories)
-                t ".")
-       (push (list 'gnus-group-annotations 'search nil
-                   (message-tokenize-header 
-                    (gnus-group-real-name gnus-newsgroup-name) ".")
-                   (if (listp gnus-picons-news-directories)
-                       gnus-picons-news-directories
-                     (list gnus-picons-news-directories))
-                   nil)
-             gnus-picons-jobs-alist)
-       (gnus-picons-next-job))
-
-      (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))
+    (let* ((newsgroups (mail-fetch-field "newsgroups"))
+          (groups
+           (if (or gnus-picons-display-article-move-p
+                   (not newsgroups))(mail-fetch-field "newsgroups")
+               (list (gnus-group-real-name gnus-newsgroup-name))
+             (split-string newsgroups ",")))
+          group)
+      (save-excursion
+       (gnus-picons-prepare-for-annotations)
+       (while (setq group (pop groups))
+         (unless gnus-picons-display-article-move-p
+           (save-restriction
+             (let ((buffer-read-only nil))
+               (goto-char (point-min))
+               (when (re-search-forward "^Newsgroups:" nil t)
+                 (narrow-to-region (point) (gnus-point-at-eol))
+                 (when (search-forward group nil t)
+                   (gnus-put-text-property
+                    (match-beginning 0) (match-end 0)
+                    'invisible t))))))
+         (if (null gnus-picons-piconsearch-url)
+             (gnus-picons-display-pairs
+              (gnus-picons-lookup-pairs
+               (reverse (split-string group "\\."))
+               gnus-picons-news-directories)
+              t ".")
+           (push (list 'gnus-group-annotations 'search nil
+                       (split-string group "\\.")
+                       (if (listp gnus-picons-news-directories)
+                           gnus-picons-news-directories
+                         (list gnus-picons-news-directories))
+                       nil)
+                 gnus-picons-jobs-alist)
+           (gnus-picons-next-job))
+
+         (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))))
 
 (defun gnus-picons-lookup-internal (addrs dir)
   (setq dir (expand-file-name dir gnus-picons-database))
@@ -386,7 +441,8 @@ none, and whose CDR is the corresponding element of DOMAINS."
   "Display picons in list PAIRS."
   (let ((domain-p (and gnus-picons-display-as-address dot-p))
        pair picons)
-    (when (and bar-p domain-p right-p)
+    (when (and bar-p domain-p right-p
+              gnus-picons-display-article-move-p)
       (setq picons (gnus-picons-display-glyph
                    (let ((gnus-picons-file-suffixes '("xbm")))
                      (gnus-picons-try-face
@@ -421,6 +477,7 @@ none, and whose CDR is the corresponding element of DOMAINS."
     glyph))
 
 (defun gnus-picons-display-glyph (glyph &optional part rightp)
+  (set-glyph-baseline glyph 70)
   (let ((new (gnus-picons-make-annotation
              glyph (point) 'text nil nil nil rightp)))
     (when (and part gnus-picons-display-as-address)
@@ -436,7 +493,7 @@ none, and whose CDR is the corresponding element of DOMAINS."
                                            'text nil nil nil rightp))))))
 
 (defun gnus-picons-action-toggle (data)
-  "Toggle annotation"
+  "Toggle annotation."
   (interactive "e")
   (let* ((annot (car data))
         (glyph (annotation-glyph annot)))
@@ -444,7 +501,7 @@ none, and whose CDR is the corresponding element of DOMAINS."
     (set-annotation-data annot (cons annot glyph))))
 
 (defun gnus-picons-clear-cache ()
-  "Clear the picons cache"
+  "Clear the picons cache."
   (interactive)
   (setq gnus-picons-glyph-alist nil
        gnus-picons-url-alist nil))
@@ -683,7 +740,8 @@ none, and whose CDR is the corresponding element of DOMAINS."
            (cond ((stringp tag);; (SYM-ANN "..." RIGHT-P)
                   (gnus-picons-network-display-internal sym-ann nil tag
                                                         (pop job)))
-                 ((eq 'bar tag)
+                 ((and (eq 'bar tag)
+                       gnus-picons-display-article-move-p)
                   (gnus-picons-network-display-internal
                    sym-ann
                    (let ((gnus-picons-file-suffixes '("xbm")))
@@ -700,7 +758,7 @@ none, and whose CDR is the corresponding element of DOMAINS."
                     (error "Unknown picon job tag %s" tag)))))))
 
 (defun gnus-picons-next-job ()
-  "Start processing the job queue if it is not in progress"
+  "Start processing the job queue if it is not in progress."
   (unless gnus-picons-job-already-running
     (gnus-picons-next-job-internal)))