1 ;;; gnus-fun.el --- various frivoluos extension functions to Gnus
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
29 "*Directory where X-Face PBM files are stored."
33 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
34 "Command for converting a PBM to an X-Face."
38 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
39 "Command for converting an image to an X-Face.
40 By default it takes a GIF filename and output the X-Face header data
45 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
46 "Command for converting an image to an Face.
47 By default it takes a JPEG filename and output the Face header data
52 (defun gnus-shell-command-to-string (command)
53 "Like `shell-command-to-string' except not mingling ERROR."
54 (with-output-to-string
55 (call-process shell-file-name nil (list standard-output nil)
56 nil shell-command-switch command)))
58 (defun gnus-shell-command-on-region (start end command)
59 "A simplified `shell-command-on-region'.
60 Output to the current buffer, replace text, and don't mingle error."
61 (call-process-region start end shell-file-name t
62 (list (current-buffer) nil)
63 nil shell-command-switch command))
66 (defun gnus-random-x-face ()
67 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
69 (when (file-exists-p gnus-x-face-directory)
70 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
71 (file (nth (random (length files)) files)))
73 (gnus-shell-command-to-string
74 (format gnus-convert-pbm-to-x-face-command
75 (shell-quote-argument file)))))))
78 (defun gnus-insert-random-x-face-header ()
79 "Insert a random X-Face header from `gnus-x-face-directory'."
81 (let ((data (gnus-random-x-face)))
85 (insert "X-Face: " data)
87 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
88 gnus-x-face-directory)))))
91 (defun gnus-x-face-from-file (file)
92 "Insert an X-Face header based on an image file."
93 (interactive "fImage file name (by default GIF): ")
94 (when (file-exists-p file)
95 (gnus-shell-command-to-string
96 (format gnus-convert-image-to-x-face-command
97 (shell-quote-argument (expand-file-name file))))))
100 (defun gnus-face-from-file (file)
101 "Return an Face header based on an image file."
102 (interactive "fImage file name (by default JPEG): ")
103 (when (file-exists-p file)
107 (while (and (not done)
110 (gnus-shell-command-to-string
111 (format gnus-convert-image-to-face-command
112 (shell-quote-argument (expand-file-name file))
114 (if (> (length attempt) 740)
116 (setq quant (- quant 2))
117 (message "Length %d; trying quant %d"
118 (length attempt) quant))
121 (mm-with-unibyte-buffer
126 (defun gnus-face-encode ()
128 (base64-encode-region (point-min) (point-max))
129 (goto-char (point-min))
130 (while (search-forward "\n" nil t)
132 (goto-char (point-min))
133 (while (> (- (point-max) (point))
141 (defun gnus-convert-face-to-png (face)
142 "Convert FACE (which is base64-encoded) to a PNG.
143 The PNG is returned as a string."
144 (mm-with-unibyte-buffer
147 (base64-decode-region (point-min) (point-max)))
151 (defun gnus-convert-png-to-face (file)
152 "Convert FILE to a Face.
153 FILE should be a PNG file that's 48x48 and smaller than or equal to
155 (mm-with-unibyte-buffer
156 (insert-file-contents file)
157 (when (> (buffer-size) 740)
158 (error "The file is %d bytes long, which is too long"
162 (defface gnus-x-face '((t (:foreground "black" :background "white")))
163 "Face to show X-Face.
164 The colors from this face are used as the foreground and background
165 colors of the displayed X-Faces."
166 :group 'gnus-article-headers)
168 (defun gnus-display-x-face-in-from (data)
169 "Display the X-Face DATA in the From header."
170 (let ((default-enable-multibyte-characters nil)
172 (when (or (gnus-image-type-available-p 'xface)
173 (and (gnus-image-type-available-p 'pbm)
174 (setq pbm (uncompface data))))
177 (article-narrow-to-head)
178 (gnus-article-goto-header "from")
180 (insert "From: [no `from' set]\n")
185 (if (gnus-image-type-available-p 'xface)
187 (concat "X-Face: " data)
188 'xface t :ascent 'center :face 'gnus-x-face)
190 pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
191 (gnus-add-wash-type 'xface))))))
193 (defun gnus-grab-cam-x-face ()
194 "Grab a picture off the camera and make it into an X-Face."
196 (shell-command "xawtv-remote snap ppm")
198 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
201 (setq file (car file))
204 (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"
211 (defun gnus-grab-cam-face ()
212 "Grab a picture off the camera and make it into an X-Face."
214 (shell-command "xawtv-remote snap ppm")
217 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
220 (setq file (car file))
222 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmnorm 2>/dev/null | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
224 (let ((gnus-convert-image-to-face-command
225 (format "cat '%%s' | ppmchange %s | ppmquant %%d | pnmtopng"
226 (gnus-fun-ppm-change-string))))
227 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
229 ;;(delete-file "/tmp/gnus.face.ppm")
232 (defun gnus-fun-ppm-change-string ()
233 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
234 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
235 (format (concat "'#%02x%02x%02x' '#"
236 (nth (random 6) possibilites)
240 (push (format format i i i i i i)
242 (mapconcat 'identity values " ")))
246 ;;; gnus-fun.el ends here