Indent.
[gnus] / lisp / gnus-fun.el
index eef1c88..b2cf411 100644 (file)
   :type 'string)
 
 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
-  "Command for converting a GIF to an X-Face."
+  "Command for converting an image to an X-Face.
+By default it takes a GIF filename and output the X-Face header data
+on stdout."
   :group 'gnus-fun
   :type 'string)
 
 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
-  "Command for converting a GIF to an X-Face."
+  "Command for converting an image to an Face.
+By default it takes a JPEG filename and output the Face header data
+on stdout."
   :group 'gnus-fun
   :type 'string)
 
@@ -60,7 +64,7 @@ Output to the current buffer, replace text, and don't mingle error."
 
 ;;;###autoload
 (defun gnus-random-x-face ()
-  "Insert a random X-Face header from `gnus-x-face-directory'."
+  "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
   (interactive)
   (when (file-exists-p gnus-x-face-directory)
     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
@@ -70,41 +74,63 @@ Output to the current buffer, replace text, and don't mingle error."
         (format gnus-convert-pbm-to-x-face-command
                 (shell-quote-argument file)))))))
 
+;;;###autoload
+(defun gnus-insert-random-x-face-header ()
+  "Insert a random X-Face header from `gnus-x-face-directory'."
+  (interactive)
+  (let ((data (gnus-random-x-face)))
+    (save-excursion
+      (message-goto-eoh)
+      (if data
+         (insert "X-Face: " data)
+       (message
+        "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
+        gnus-x-face-directory)))))
+
 ;;;###autoload
 (defun gnus-x-face-from-file (file)
   "Insert an X-Face header based on an image file."
-  (interactive "fImage file name:" )
+  (interactive "fImage file name (by default GIF): ")
   (when (file-exists-p file)
     (gnus-shell-command-to-string
      (format gnus-convert-image-to-x-face-command
-            (shell-quote-argument file)))))
+            (shell-quote-argument (expand-file-name file))))))
 
+;;;###autoload
 (defun gnus-face-from-file (file)
   "Return an Face header based on an image file."
-  (interactive "fImage file name:" )
+  (interactive "fImage file name (by default JPEG): ")
   (when (file-exists-p file)
     (let ((done nil)
          (attempt "")
-         (quant 4))
+         (step 72)
+         (quant 16))
       (while (and (not done)
                  (> quant 1))
        (setq attempt
              (gnus-shell-command-to-string
               (format gnus-convert-image-to-face-command
-                      (shell-quote-argument file)
+                      (shell-quote-argument (expand-file-name file))
                       quant)))
        (if (> (length attempt) 740)
-           (setq quant (/ quant 2))
+           (progn
+             (setq quant (- quant 2))
+             (message "Length %d; trying quant %d"
+                      (length attempt) quant))
          (setq done t)))
       (if done
          (mm-with-unibyte-buffer       
            (insert attempt)
            (base64-encode-region (point-min) (point-max))
            (goto-char (point-min))
-           (forward-line 1)
-           (while (not (eobp))
-             (insert " ")
-             (forward-line 1))
+           (while (search-forward "\n" nil t)
+             (replace-match ""))
+           (goto-char (point-min))
+           (while (> (- (point-max) (point))
+                     step)
+             (forward-char step)
+             (insert "\n ")
+             (setq step 76))
            (buffer-string))
        nil))))
 
@@ -112,7 +138,8 @@ Output to the current buffer, replace text, and don't mingle error."
 (defun gnus-convert-face-to-png (face)
   (mm-with-unibyte-buffer
     (insert face)
-    (base64-decode-region (point-min) (point-max))
+    (ignore-errors
+      (base64-decode-region (point-min) (point-max)))
     (buffer-string)))
 
 (defun gnus-convert-image-to-gray-x-face (file depth)