1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
36 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
37 "*Directory where X-Face PBM files are stored."
42 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
43 "Command for converting a PBM to an X-Face."
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.
54 By default it takes a GIF filename and output the X-Face header data
58 :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
59 "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
61 "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
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.
68 The command must take an image filename (first format
69 argument\"%s\") and the number of colors (second format argument:
70 \"%d\") as input. The output must be the Face header data on
71 stdout in PNG format."
74 :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
75 "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
77 "convert -scale 48x48! %s -colors %d png:-")
80 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
81 '((xface . (:face gnus-x-face)))
82 '((pbm . (:face gnus-x-face))
84 "Alist of image types and properties applied to Face and X-Face images.
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))))
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))))
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."
102 :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
104 (defun gnus-shell-command-to-string (command)
105 "Like `shell-command-to-string' except not mingling ERROR."
106 (with-output-to-string
107 (call-process shell-file-name nil (list standard-output nil)
108 nil shell-command-switch command)))
110 (defun gnus-shell-command-on-region (start end command)
111 "A simplified `shell-command-on-region'.
112 Output to the current buffer, replace text, and don't mingle error."
113 (call-process-region start end shell-file-name t
114 (list (current-buffer) nil)
115 nil shell-command-switch command))
118 (defun gnus-random-x-face ()
119 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
121 (when (file-exists-p gnus-x-face-directory)
122 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
123 (file (nth (random (length files)) files)))
125 (gnus-shell-command-to-string
126 (format gnus-convert-pbm-to-x-face-command
127 (shell-quote-argument file)))))))
130 (defun gnus-insert-random-x-face-header ()
131 "Insert a random X-Face header from `gnus-x-face-directory'."
133 (let ((data (gnus-random-x-face)))
137 (insert "X-Face: " data)
139 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
140 gnus-x-face-directory)))))
143 (defun gnus-x-face-from-file (file)
144 "Insert an X-Face header based on an image file.
146 Depending on `gnus-convert-image-to-x-face-command' it may accept
147 different input formats."
148 (interactive "fImage file name: ")
149 (when (file-exists-p file)
150 (gnus-shell-command-to-string
151 (format gnus-convert-image-to-x-face-command
152 (shell-quote-argument (expand-file-name file))))))
155 (defun gnus-face-from-file (file)
156 "Return a Face header based on an image file.
158 Depending on `gnus-convert-image-to-face-command' it may accept
159 different input formats."
160 (interactive "fImage file name: ")
161 (when (file-exists-p file)
165 (while (and (not done)
168 (let ((coding-system-for-read 'binary))
169 (gnus-shell-command-to-string
170 (format gnus-convert-image-to-face-command
171 (shell-quote-argument (expand-file-name file))
173 (if (> (length attempt) 726)
175 (setq quant (- quant (if (< quant 10) 1 2)))
176 (gnus-message 9 "Length %d; trying quant %d"
177 (length attempt) quant))
180 (mm-with-unibyte-buffer
185 (defun gnus-face-encode ()
187 (base64-encode-region (point-min) (point-max))
188 (goto-char (point-min))
189 (while (search-forward "\n" nil t)
191 (goto-char (point-min))
192 (while (> (- (point-max) (point))
200 (defun gnus-convert-face-to-png (face)
201 "Convert FACE (which is base64-encoded) to a PNG.
202 The PNG is returned as a string."
203 (mm-with-unibyte-buffer
206 (base64-decode-region (point-min) (point-max)))
210 (defun gnus-convert-png-to-face (file)
211 "Convert FILE to a Face.
212 FILE should be a PNG file that's 48x48 and smaller than or equal to
214 (mm-with-unibyte-buffer
215 (insert-file-contents file)
216 (when (> (buffer-size) 726)
217 (error "The file is %d bytes long, which is too long"
221 (defface gnus-x-face '((t (:foreground "black" :background "white")))
222 "Face to show X-Face.
223 The colors from this face are used as the foreground and background
224 colors of the displayed X-Faces."
225 :group 'gnus-article-headers)
227 (defun gnus-display-x-face-in-from (data)
228 "Display the X-Face DATA in the From header."
229 (let ((default-enable-multibyte-characters nil)
231 (when (or (gnus-image-type-available-p 'xface)
232 (and (gnus-image-type-available-p 'pbm)
233 (setq pbm (uncompface data))))
236 (article-narrow-to-head)
237 (gnus-article-goto-header "from")
239 (insert "From: [no `from' set]\n")
244 (if (gnus-image-type-available-p 'xface)
245 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
246 (cdr (assq 'xface gnus-face-properties-alist)))
247 (apply 'gnus-create-image pbm 'pbm t
248 (cdr (assq 'pbm gnus-face-properties-alist))))
250 (gnus-add-wash-type 'xface))))))
252 (defun gnus-grab-cam-x-face ()
253 "Grab a picture off the camera and make it into an X-Face."
255 (shell-command "xawtv-remote snap ppm")
257 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
260 (setq file (car file))
263 (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"
270 (defun gnus-grab-cam-face ()
271 "Grab a picture off the camera and make it into an X-Face."
273 (shell-command "xawtv-remote snap ppm")
276 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
279 (setq file (car file))
281 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
283 (let ((gnus-convert-image-to-face-command
284 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
285 (gnus-fun-ppm-change-string))))
286 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
288 ;;(delete-file "/tmp/gnus.face.ppm")
291 (defun gnus-fun-ppm-change-string ()
292 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
293 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
294 (format (concat "'#%02x%02x%02x' '#"
295 (nth (random 6) possibilites)
299 (push (format format i i i i i i)
301 (mapconcat 'identity values " ")))
305 ;;; gnus-fun.el ends here