1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 3, 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.
37 (defvar gnus-face-properties-alist
)
39 (defcustom gnus-x-face-directory
(expand-file-name "x-faces" gnus-directory
)
40 "*Directory where X-Face PBM files are stored."
45 (defcustom gnus-convert-pbm-to-x-face-command
"pbmtoxbm %s | compface"
46 "Command for converting a PBM to an X-Face."
51 (defcustom gnus-convert-image-to-x-face-command
52 "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
53 "Command for converting an image to an X-Face.
54 The command must take a image filename (use \"%s\") as input.
55 The output must be the Face header data on stdout in PNG format.
57 By default it takes a GIF filename and output the X-Face header data
61 :type
'(choice (const :tag
"giftopnm, netpbm (GIF input only)"
62 "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
64 "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
67 (defcustom gnus-convert-image-to-face-command
68 "convert -scale 48x48! %s -colors %d png:-"
69 "Command for converting an image to a Face.
71 The command must take an image filename (first format argument
72 \"%s\") and the number of colors (second format argument: \"%d\")
73 as input. The output must be the Face header data on stdout in
77 :type
'(choice (const :tag
"djpeg, netpbm (JPG input only)"
78 "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
80 "convert -scale 48x48! %s -colors %d png:-")
83 (defun gnus-shell-command-to-string (command)
84 "Like `shell-command-to-string' except not mingling ERROR."
85 (with-output-to-string
86 (call-process shell-file-name nil
(list standard-output nil
)
87 nil shell-command-switch command
)))
89 (defun gnus-shell-command-on-region (start end command
)
90 "A simplified `shell-command-on-region'.
91 Output to the current buffer, replace text, and don't mingle error."
92 (call-process-region start end shell-file-name t
93 (list (current-buffer) nil
)
94 nil shell-command-switch command
))
97 (defun gnus-random-x-face ()
98 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
100 (when (file-exists-p gnus-x-face-directory
)
101 (let* ((files (directory-files gnus-x-face-directory t
"\\.pbm$"))
102 (file (nth (random (length files
)) files
)))
104 (gnus-shell-command-to-string
105 (format gnus-convert-pbm-to-x-face-command
106 (shell-quote-argument file
)))))))
109 (defun gnus-insert-random-x-face-header ()
110 "Insert a random X-Face header from `gnus-x-face-directory'."
112 (let ((data (gnus-random-x-face)))
116 (insert "X-Face: " data
)
118 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
119 gnus-x-face-directory
)))))
122 (defun gnus-x-face-from-file (file)
123 "Insert an X-Face header based on an image file.
125 Depending on `gnus-convert-image-to-x-face-command' it may accept
126 different input formats."
127 (interactive "fImage file name: ")
128 (when (file-exists-p file
)
129 (gnus-shell-command-to-string
130 (format gnus-convert-image-to-x-face-command
131 (shell-quote-argument (expand-file-name file
))))))
134 (defun gnus-face-from-file (file)
135 "Return a Face header based on an image file.
137 Depending on `gnus-convert-image-to-face-command' it may accept
138 different input formats."
139 (interactive "fImage file name: ")
140 (when (file-exists-p file
)
144 (while (and (not done
)
147 (let ((coding-system-for-read 'binary
))
148 (gnus-shell-command-to-string
149 (format gnus-convert-image-to-face-command
150 (shell-quote-argument (expand-file-name file
))
152 (if (> (length attempt
) 726)
154 (setq quant
(- quant
(if (< quant
10) 1 2)))
155 (gnus-message 9 "Length %d; trying quant %d"
156 (length attempt
) quant
))
159 (mm-with-unibyte-buffer
164 (defun gnus-face-encode ()
166 (base64-encode-region (point-min) (point-max))
167 (goto-char (point-min))
168 (while (search-forward "\n" nil t
)
170 (goto-char (point-min))
171 (while (> (- (point-max) (point))
179 (defun gnus-convert-face-to-png (face)
180 "Convert FACE (which is base64-encoded) to a PNG.
181 The PNG is returned as a string."
182 (mm-with-unibyte-buffer
185 (base64-decode-region (point-min) (point-max)))
189 (defun gnus-convert-png-to-face (file)
190 "Convert FILE to a Face.
191 FILE should be a PNG file that's 48x48 and smaller than or equal to
193 (mm-with-unibyte-buffer
194 (insert-file-contents file
)
195 (when (> (buffer-size) 726)
196 (error "The file is %d bytes long, which is too long"
200 (defface gnus-x-face
'((t (:foreground
"black" :background
"white")))
201 "Face to show X-Face.
202 The colors from this face are used as the foreground and background
203 colors of the displayed X-Faces."
204 :group
'gnus-article-headers
)
206 (defun gnus-display-x-face-in-from (data)
207 "Display the X-Face DATA in the From header."
208 (let ((default-enable-multibyte-characters nil
)
210 (when (or (gnus-image-type-available-p 'xface
)
211 (and (gnus-image-type-available-p 'pbm
)
212 (setq pbm
(uncompface data
))))
215 (article-narrow-to-head)
216 (gnus-article-goto-header "from")
218 (insert "From: [no `from' set]\n")
223 (if (gnus-image-type-available-p 'xface
)
224 (apply 'gnus-create-image
(concat "X-Face: " data
) 'xface t
225 (cdr (assq 'xface gnus-face-properties-alist
)))
226 (apply 'gnus-create-image pbm
'pbm t
227 (cdr (assq 'pbm gnus-face-properties-alist
))))
229 (gnus-add-wash-type 'xface
))))))
231 (defun gnus-grab-cam-x-face ()
232 "Grab a picture off the camera and make it into an X-Face."
234 (shell-command "xawtv-remote snap ppm")
236 (while (null (setq file
(directory-files "/tftpboot/sparky/tmp"
239 (setq file
(car file
))
242 (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"
249 (defun gnus-grab-cam-face ()
250 "Grab a picture off the camera and make it into an X-Face."
252 (shell-command "xawtv-remote snap ppm")
255 (while (null (setq file
(directory-files "/tftpboot/sparky/tmp"
258 (setq file
(car file
))
260 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
262 (let ((gnus-convert-image-to-face-command
263 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
264 (gnus-fun-ppm-change-string))))
265 (setq result
(gnus-face-from-file "/tmp/gnus.face.ppm")))
267 ;;(delete-file "/tmp/gnus.face.ppm")
270 (defun gnus-fun-ppm-change-string ()
271 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
272 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
273 (format (concat "'#%02x%02x%02x' '#"
274 (nth (random 6) possibilites
)
278 (push (format format i i i i i i
)
280 (mapconcat 'identity values
" ")))
284 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
285 ;;; gnus-fun.el ends here