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