1 ;;; gnus-fun.el --- various frivolous 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.
35 (defcustom gnus-x-face-directory
(expand-file-name "x-faces" gnus-directory
)
36 "*Directory where X-Face PBM files are stored."
41 (defcustom gnus-convert-pbm-to-x-face-command
"pbmtoxbm %s | compface"
42 "Command for converting a PBM to an X-Face."
47 (defcustom gnus-convert-image-to-x-face-command
"giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
48 "Command for converting an image to an X-Face.
49 By default it takes a GIF filename and output the X-Face header data
55 (defcustom gnus-convert-image-to-face-command
"djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
56 "Command for converting an image to an Face.
57 By default it takes a JPEG filename and output the Face header data
63 (defun gnus-shell-command-to-string (command)
64 "Like `shell-command-to-string' except not mingling ERROR."
65 (with-output-to-string
66 (call-process shell-file-name nil
(list standard-output nil
)
67 nil shell-command-switch command
)))
69 (defun gnus-shell-command-on-region (start end command
)
70 "A simplified `shell-command-on-region'.
71 Output to the current buffer, replace text, and don't mingle error."
72 (call-process-region start end shell-file-name t
73 (list (current-buffer) nil
)
74 nil shell-command-switch command
))
77 (defun gnus-random-x-face ()
78 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
80 (when (file-exists-p gnus-x-face-directory
)
81 (let* ((files (directory-files gnus-x-face-directory t
"\\.pbm$"))
82 (file (nth (random (length files
)) files
)))
84 (gnus-shell-command-to-string
85 (format gnus-convert-pbm-to-x-face-command
86 (shell-quote-argument file
)))))))
89 (defun gnus-insert-random-x-face-header ()
90 "Insert a random X-Face header from `gnus-x-face-directory'."
92 (let ((data (gnus-random-x-face)))
96 (insert "X-Face: " data
)
98 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
99 gnus-x-face-directory
)))))
102 (defun gnus-x-face-from-file (file)
103 "Insert an X-Face header based on an image file."
104 (interactive "fImage file name (by default GIF): ")
105 (when (file-exists-p file
)
106 (gnus-shell-command-to-string
107 (format gnus-convert-image-to-x-face-command
108 (shell-quote-argument (expand-file-name file
))))))
111 (defun gnus-face-from-file (file)
112 "Return an Face header based on an image file."
113 (interactive "fImage file name (by default JPEG): ")
114 (when (file-exists-p file
)
118 (while (and (not done
)
121 (let ((coding-system-for-read 'binary
))
122 (gnus-shell-command-to-string
123 (format gnus-convert-image-to-face-command
124 (shell-quote-argument (expand-file-name file
))
126 (if (> (length attempt
) 726)
128 (setq quant
(- quant
2))
129 (gnus-message 9 "Length %d; trying quant %d"
130 (length attempt
) quant
))
133 (mm-with-unibyte-buffer
138 (defun gnus-face-encode ()
140 (base64-encode-region (point-min) (point-max))
141 (goto-char (point-min))
142 (while (search-forward "\n" nil t
)
144 (goto-char (point-min))
145 (while (> (- (point-max) (point))
153 (defun gnus-convert-face-to-png (face)
154 "Convert FACE (which is base64-encoded) to a PNG.
155 The PNG is returned as a string."
156 (mm-with-unibyte-buffer
159 (base64-decode-region (point-min) (point-max)))
163 (defun gnus-convert-png-to-face (file)
164 "Convert FILE to a Face.
165 FILE should be a PNG file that's 48x48 and smaller than or equal to
167 (mm-with-unibyte-buffer
168 (insert-file-contents file
)
169 (when (> (buffer-size) 726)
170 (error "The file is %d bytes long, which is too long"
174 (defface gnus-x-face
'((t (:foreground
"black" :background
"white")))
175 "Face to show X-Face.
176 The colors from this face are used as the foreground and background
177 colors of the displayed X-Faces."
178 :group
'gnus-article-headers
)
180 (defun gnus-display-x-face-in-from (data)
181 "Display the X-Face DATA in the From header."
182 (let ((default-enable-multibyte-characters nil
)
184 (when (or (gnus-image-type-available-p 'xface
)
185 (and (gnus-image-type-available-p 'pbm
)
186 (setq pbm
(uncompface data
))))
189 (article-narrow-to-head)
190 (gnus-article-goto-header "from")
192 (insert "From: [no `from' set]\n")
197 (if (gnus-image-type-available-p 'xface
)
199 (concat "X-Face: " data
)
200 'xface t
:face
'gnus-x-face
)
202 pbm
'pbm t
:face
'gnus-x-face
)) nil
'xface
))
203 (gnus-add-wash-type 'xface
))))))
205 (defun gnus-grab-cam-x-face ()
206 "Grab a picture off the camera and make it into an X-Face."
208 (shell-command "xawtv-remote snap ppm")
210 (while (null (setq file
(directory-files "/tftpboot/sparky/tmp"
213 (setq file
(car file
))
216 (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"
223 (defun gnus-grab-cam-face ()
224 "Grab a picture off the camera and make it into an X-Face."
226 (shell-command "xawtv-remote snap ppm")
229 (while (null (setq file
(directory-files "/tftpboot/sparky/tmp"
232 (setq file
(car file
))
234 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
236 (let ((gnus-convert-image-to-face-command
237 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
238 (gnus-fun-ppm-change-string))))
239 (setq result
(gnus-face-from-file "/tmp/gnus.face.ppm")))
241 ;;(delete-file "/tmp/gnus.face.ppm")
244 (defun gnus-fun-ppm-change-string ()
245 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
246 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
247 (format (concat "'#%02x%02x%02x' '#"
248 (nth (random 6) possibilites
)
252 (push (format format i i i i i i
)
254 (mapconcat 'identity values
" ")))
258 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
259 ;;; gnus-fun.el ends here