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 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
38 "*Directory where X-Face PBM files are stored."
43 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
44 "Command for converting a PBM to an X-Face."
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.
55 By default it takes a GIF filename and output the X-Face header data
59 :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
60 "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
62 "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
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.
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
75 :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
76 "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
78 "convert -scale 48x48! %s -colors %d png:-")
81 (defun gnus-shell-command-to-string (command)
82 "Like `shell-command-to-string' except not mingling ERROR."
83 (with-output-to-string
84 (call-process shell-file-name nil (list standard-output nil)
85 nil shell-command-switch command)))
87 (defun gnus-shell-command-on-region (start end command)
88 "A simplified `shell-command-on-region'.
89 Output to the current buffer, replace text, and don't mingle error."
90 (call-process-region start end shell-file-name t
91 (list (current-buffer) nil)
92 nil shell-command-switch command))
95 (defun gnus-random-x-face ()
96 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
98 (when (file-exists-p gnus-x-face-directory)
99 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
100 (file (nth (random (length files)) files)))
102 (gnus-shell-command-to-string
103 (format gnus-convert-pbm-to-x-face-command
104 (shell-quote-argument file)))))))
107 (defun gnus-insert-random-x-face-header ()
108 "Insert a random X-Face header from `gnus-x-face-directory'."
110 (let ((data (gnus-random-x-face)))
114 (insert "X-Face: " data)
116 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
117 gnus-x-face-directory)))))
120 (defun gnus-x-face-from-file (file)
121 "Insert an X-Face header based on an image file.
123 Depending on `gnus-convert-image-to-x-face-command' it may accept
124 different input formats."
125 (interactive "fImage file name: ")
126 (when (file-exists-p file)
127 (gnus-shell-command-to-string
128 (format gnus-convert-image-to-x-face-command
129 (shell-quote-argument (expand-file-name file))))))
132 (defun gnus-face-from-file (file)
133 "Return a Face header based on an image file.
135 Depending on `gnus-convert-image-to-face-command' it may accept
136 different input formats."
137 (interactive "fImage file name: ")
138 (when (file-exists-p file)
142 (while (and (not done)
145 (let ((coding-system-for-read 'binary))
146 (gnus-shell-command-to-string
147 (format gnus-convert-image-to-face-command
148 (shell-quote-argument (expand-file-name file))
150 (if (> (length attempt) 726)
152 (setq quant (- quant (if (< quant 10) 1 2)))
153 (gnus-message 9 "Length %d; trying quant %d"
154 (length attempt) quant))
157 (mm-with-unibyte-buffer
162 (defun gnus-face-encode ()
164 (base64-encode-region (point-min) (point-max))
165 (goto-char (point-min))
166 (while (search-forward "\n" nil t)
168 (goto-char (point-min))
169 (while (> (- (point-max) (point))
177 (defun gnus-convert-face-to-png (face)
178 "Convert FACE (which is base64-encoded) to a PNG.
179 The PNG is returned as a string."
180 (mm-with-unibyte-buffer
183 (base64-decode-region (point-min) (point-max)))
187 (defun gnus-convert-png-to-face (file)
188 "Convert FILE to a Face.
189 FILE should be a PNG file that's 48x48 and smaller than or equal to
191 (mm-with-unibyte-buffer
192 (insert-file-contents file)
193 (when (> (buffer-size) 726)
194 (error "The file is %d bytes long, which is too long"
198 (defface gnus-x-face '((t (:foreground "black" :background "white")))
199 "Face to show X-Face.
200 The colors from this face are used as the foreground and background
201 colors of the displayed X-Faces."
202 :group 'gnus-article-headers)
204 (defun gnus-display-x-face-in-from (data)
205 "Display the X-Face DATA in the From header."
206 (let ((default-enable-multibyte-characters nil)
208 (when (or (gnus-image-type-available-p 'xface)
209 (and (gnus-image-type-available-p 'pbm)
210 (setq pbm (uncompface data))))
213 (article-narrow-to-head)
214 (gnus-article-goto-header "from")
216 (insert "From: [no `from' set]\n")
221 (if (gnus-image-type-available-p 'xface)
222 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
223 (cdr (assq 'xface gnus-face-properties-alist)))
224 (apply 'gnus-create-image pbm 'pbm t
225 (cdr (assq 'pbm gnus-face-properties-alist))))
227 (gnus-add-wash-type 'xface))))))
229 (defun gnus-grab-cam-x-face ()
230 "Grab a picture off the camera and make it into an X-Face."
232 (shell-command "xawtv-remote snap ppm")
234 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
237 (setq file (car file))
240 (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"
247 (defun gnus-grab-cam-face ()
248 "Grab a picture off the camera and make it into an X-Face."
250 (shell-command "xawtv-remote snap ppm")
253 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
256 (setq file (car file))
258 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
260 (let ((gnus-convert-image-to-face-command
261 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
262 (gnus-fun-ppm-change-string))))
263 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
265 ;;(delete-file "/tmp/gnus.face.ppm")
268 (defun gnus-fun-ppm-change-string ()
269 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
270 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
271 (format (concat "'#%02x%02x%02x' '#"
272 (nth (random 6) possibilites)
276 (push (format format i i i i i i)
278 (mapconcat 'identity values " ")))
282 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
283 ;;; gnus-fun.el ends here