Import No Gnus v0.4.
[elisp/gnus.git-] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl))
31
32 (require 'mm-util)
33 (require 'gnus-ems)
34 (require 'gnus-util)
35 (require 'gnus)
36
37 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
38   "*Directory where X-Face PBM files are stored."
39   :version "22.1"
40   :group 'gnus-fun
41   :type 'directory)
42
43 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
44   "Command for converting a PBM to an X-Face."
45   :version "22.1"
46   :group 'gnus-fun
47   :type 'string)
48
49 (defcustom gnus-convert-image-to-x-face-command
50   "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
51   "Command for converting an image to an X-Face.
52 The command must take a image filename (use \"%s\") as input.
53 The output must be the Face header data on stdout in PNG format.
54
55 By default it takes a GIF filename and output the X-Face header data
56 on stdout."
57   :version "22.1"
58   :group 'gnus-fun
59   :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
60                         "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
61                  (const :tag "convert"
62                         "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
63                  (string)))
64
65 (defcustom gnus-convert-image-to-face-command
66   "convert -scale 48x48! %s -colors %d png:-"
67   "Command for converting an image to a Face.
68
69 The command must take an image filename (first format argument
70 \"%s\") and the number of colors (second format argument: \"%d\")
71 as input.  The output must be the Face header data on stdout in
72 PNG format."
73   :version "22.1"
74   :group 'gnus-fun
75   :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
76                         "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
77                  (const :tag "convert"
78                         "convert -scale 48x48! %s -colors %d png:-")
79                  (string)))
80
81 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
82                                           '((xface . (:face gnus-x-face)))
83                                         '((pbm . (:face gnus-x-face))
84                                           (png . nil)))
85   "Alist of image types and properties applied to Face and X-Face images.
86 Here are examples:
87
88 ;; Specify the altitude of Face images in the From header.
89 \(setq gnus-face-properties-alist
90       '((pbm . (:face gnus-x-face :ascent 80))
91         (png . (:ascent 80))))
92
93 ;; Show Face images as pressed buttons.
94 \(setq gnus-face-properties-alist
95       '((pbm . (:face gnus-x-face :relief -2))
96         (png . (:relief -2))))
97
98 See the manual for the valid properties for various image types.
99 Currently, `pbm' is used for X-Face images and `png' is used for Face
100 images in Emacs.  Only the `:face' property is effective on the `xface'
101 image type in XEmacs if it is built with the libcompface library."
102   :version "23.0" ;; No Gnus
103   :group 'gnus-fun
104   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
105
106 (defun gnus-shell-command-to-string (command)
107   "Like `shell-command-to-string' except not mingling ERROR."
108   (with-output-to-string
109     (call-process shell-file-name nil (list standard-output nil)
110                   nil shell-command-switch command)))
111
112 (defun gnus-shell-command-on-region (start end command)
113   "A simplified `shell-command-on-region'.
114 Output to the current buffer, replace text, and don't mingle error."
115   (call-process-region start end shell-file-name t
116                        (list (current-buffer) nil)
117                        nil shell-command-switch command))
118
119 ;;;###autoload
120 (defun gnus-random-x-face ()
121   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
122   (interactive)
123   (when (file-exists-p gnus-x-face-directory)
124     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
125            (file (nth (random (length files)) files)))
126       (when file
127         (gnus-shell-command-to-string
128          (format gnus-convert-pbm-to-x-face-command
129                  (shell-quote-argument file)))))))
130
131 ;;;###autoload
132 (defun gnus-insert-random-x-face-header ()
133   "Insert a random X-Face header from `gnus-x-face-directory'."
134   (interactive)
135   (let ((data (gnus-random-x-face)))
136     (save-excursion
137       (message-goto-eoh)
138       (if data
139           (insert "X-Face: " data)
140         (message
141          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
142          gnus-x-face-directory)))))
143
144 ;;;###autoload
145 (defun gnus-x-face-from-file (file)
146   "Insert an X-Face header based on an image file.
147
148 Depending on `gnus-convert-image-to-x-face-command' it may accept
149 different input formats."
150   (interactive "fImage file name: ")
151   (when (file-exists-p file)
152     (gnus-shell-command-to-string
153      (format gnus-convert-image-to-x-face-command
154              (shell-quote-argument (expand-file-name file))))))
155
156 ;;;###autoload
157 (defun gnus-face-from-file (file)
158   "Return a Face header based on an image file.
159
160 Depending on `gnus-convert-image-to-face-command' it may accept
161 different input formats."
162   (interactive "fImage file name: ")
163   (when (file-exists-p file)
164     (let ((done nil)
165           (attempt "")
166           (quant 16))
167       (while (and (not done)
168                   (> quant 1))
169         (setq attempt
170               (let ((coding-system-for-read 'binary))
171                 (gnus-shell-command-to-string
172                  (format gnus-convert-image-to-face-command
173                          (shell-quote-argument (expand-file-name file))
174                          quant))))
175         (if (> (length attempt) 726)
176             (progn
177               (setq quant (- quant (if (< quant 10) 1 2)))
178               (gnus-message 9 "Length %d; trying quant %d"
179                             (length attempt) quant))
180           (setq done t)))
181       (if done
182           (mm-with-unibyte-buffer
183             (insert attempt)
184             (gnus-face-encode))
185         nil))))
186
187 (defun gnus-face-encode ()
188   (let ((step 72))
189     (base64-encode-region (point-min) (point-max))
190     (goto-char (point-min))
191     (while (search-forward "\n" nil t)
192       (replace-match ""))
193     (goto-char (point-min))
194     (while (> (- (point-max) (point))
195               step)
196       (forward-char step)
197       (insert "\n ")
198       (setq step 76))
199     (buffer-string)))
200
201 ;;;###autoload
202 (defun gnus-convert-face-to-png (face)
203   "Convert FACE (which is base64-encoded) to a PNG.
204 The PNG is returned as a string."
205   (mm-with-unibyte-buffer
206     (insert face)
207     (ignore-errors
208       (base64-decode-region (point-min) (point-max)))
209     (buffer-string)))
210
211 ;;;###autoload
212 (defun gnus-convert-png-to-face (file)
213   "Convert FILE to a Face.
214 FILE should be a PNG file that's 48x48 and smaller than or equal to
215 726 bytes."
216   (mm-with-unibyte-buffer
217     (insert-file-contents file)
218     (when (> (buffer-size) 726)
219       (error "The file is %d bytes long, which is too long"
220              (buffer-size)))
221     (gnus-face-encode)))
222
223 (defface gnus-x-face '((t (:foreground "black" :background "white")))
224   "Face to show X-Face.
225 The colors from this face are used as the foreground and background
226 colors of the displayed X-Faces."
227   :group 'gnus-article-headers)
228
229 (defun gnus-display-x-face-in-from (data)
230   "Display the X-Face DATA in the From header."
231   (let ((default-enable-multibyte-characters nil)
232         pbm)
233     (when (or (gnus-image-type-available-p 'xface)
234               (and (gnus-image-type-available-p 'pbm)
235                    (setq pbm (uncompface data))))
236       (save-excursion
237         (save-restriction
238           (article-narrow-to-head)
239           (gnus-article-goto-header "from")
240           (when (bobp)
241             (insert "From: [no `from' set]\n")
242             (forward-char -17))
243           (gnus-add-image
244            'xface
245            (gnus-put-image
246             (if (gnus-image-type-available-p 'xface)
247                 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
248                        (cdr (assq 'xface gnus-face-properties-alist)))
249               (apply 'gnus-create-image pbm 'pbm t
250                      (cdr (assq 'pbm gnus-face-properties-alist))))
251             nil 'xface))
252           (gnus-add-wash-type 'xface))))))
253
254 (defun gnus-grab-cam-x-face ()
255   "Grab a picture off the camera and make it into an X-Face."
256   (interactive)
257   (shell-command "xawtv-remote snap ppm")
258   (let ((file nil))
259     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
260                                              t "snap.*ppm")))
261       (sleep-for 1))
262     (setq file (car file))
263     (with-temp-buffer
264       (shell-command
265        (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmnorm 2>/dev/null | pnmscale -width 48 | ppmtopgm | pgmtopbm -threshold -value 0.92 | pbmtoxbm | compface"
266                file)
267        (current-buffer))
268       ;;(sleep-for 3)
269       (delete-file file)
270       (buffer-string))))
271
272 (defun gnus-grab-cam-face ()
273   "Grab a picture off the camera and make it into an X-Face."
274   (interactive)
275   (shell-command "xawtv-remote snap ppm")
276   (let ((file nil)
277         result)
278     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
279                                              t "snap.*ppm")))
280       (sleep-for 1))
281     (setq file (car file))
282     (shell-command
283      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
284              file))
285     (let ((gnus-convert-image-to-face-command
286            (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
287                    (gnus-fun-ppm-change-string))))
288       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
289     (delete-file file)
290     ;;(delete-file "/tmp/gnus.face.ppm")
291     result))
292
293 (defun gnus-fun-ppm-change-string ()
294   (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
295                         "%02x%02x00" "00%02x%02x" "%02x00%02x"))
296          (format (concat "'#%02x%02x%02x' '#"
297                          (nth (random 6) possibilites)
298                          "'"))
299          (values nil))
300   (dotimes (i 255)
301     (push (format format i i i i i i)
302           values))
303   (mapconcat 'identity values " ")))
304
305 (provide 'gnus-fun)
306
307 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
308 ;;; gnus-fun.el ends here