1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
39 (defvar gnus-face-properties-alist
)
41 (defcustom gnus-x-face-directory
(expand-file-name "x-faces" gnus-directory
)
42 "*Directory where X-Face PBM files are stored."
47 (defcustom gnus-convert-pbm-to-x-face-command
"pbmtoxbm %s | compface"
48 "Command for converting a PBM to an X-Face."
53 (defcustom gnus-convert-image-to-x-face-command
54 "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
55 "Command for converting an image to an X-Face.
56 The command must take a image filename (use \"%s\") as input.
57 The output must be the Face header data on stdout in PNG format.
59 By default it takes a GIF filename and output the X-Face header data
63 :type
'(choice (const :tag
"giftopnm, netpbm (GIF input only)"
64 "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
66 "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
69 (defcustom gnus-convert-image-to-face-command
70 "convert -scale 48x48! %s -colors %d png:-"
71 "Command for converting an image to a Face.
73 The command must take an image filename (first format argument
74 \"%s\") and the number of colors (second format argument: \"%d\")
75 as input. The output must be the Face header data on stdout in
79 :type
'(choice (const :tag
"djpeg, netpbm (JPG input only)"
80 "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
82 "convert -scale 48x48! %s -colors %d png:-")
85 (defun gnus-shell-command-to-string (command)
86 "Like `shell-command-to-string' except not mingling ERROR."
87 (with-output-to-string
88 (call-process shell-file-name nil
(list standard-output nil
)
89 nil shell-command-switch command
)))
91 (defun gnus-shell-command-on-region (start end command
)
92 "A simplified `shell-command-on-region'.
93 Output to the current buffer, replace text, and don't mingle error."
94 (call-process-region start end shell-file-name t
95 (list (current-buffer) nil
)
96 nil shell-command-switch command
))
99 (defun gnus-random-x-face ()
100 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
102 (when (file-exists-p gnus-x-face-directory
)
103 (let* ((files (directory-files gnus-x-face-directory t
"\\.pbm$"))
104 (file (nth (random (length files
)) files
)))
106 (gnus-shell-command-to-string
107 (format gnus-convert-pbm-to-x-face-command
108 (shell-quote-argument file
)))))))
110 (autoload 'message-goto-eoh
"message" nil t
)
113 (defun gnus-insert-random-x-face-header ()
114 "Insert a random X-Face header from `gnus-x-face-directory'."
116 (let ((data (gnus-random-x-face)))
120 (insert "X-Face: " data
)
122 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
123 gnus-x-face-directory
)))))
126 (defun gnus-x-face-from-file (file)
127 "Insert an X-Face header based on an image file.
129 Depending on `gnus-convert-image-to-x-face-command' it may accept
130 different input formats."
131 (interactive "fImage file name: ")
132 (when (file-exists-p file
)
133 (gnus-shell-command-to-string
134 (format gnus-convert-image-to-x-face-command
135 (shell-quote-argument (expand-file-name file
))))))
138 (defun gnus-face-from-file (file)
139 "Return a Face header based on an image file.
141 Depending on `gnus-convert-image-to-face-command' it may accept
142 different input formats."
143 (interactive "fImage file name: ")
144 (when (file-exists-p file
)
148 (while (and (not done
)
151 (let ((coding-system-for-read 'binary
))
152 (gnus-shell-command-to-string
153 (format gnus-convert-image-to-face-command
154 (shell-quote-argument (expand-file-name file
))
156 (if (> (length attempt
) 726)
158 (setq quant
(- quant
(if (< quant
10) 1 2)))
159 (gnus-message 9 "Length %d; trying quant %d"
160 (length attempt
) quant
))
163 (mm-with-unibyte-buffer
168 (defun gnus-face-encode ()
170 (base64-encode-region (point-min) (point-max))
171 (goto-char (point-min))
172 (while (search-forward "\n" nil t
)
174 (goto-char (point-min))
175 (while (> (- (point-max) (point))
183 (defun gnus-convert-face-to-png (face)
184 "Convert FACE (which is base64-encoded) to a PNG.
185 The PNG is returned as a string."
186 (mm-with-unibyte-buffer
189 (base64-decode-region (point-min) (point-max)))
193 (defun gnus-convert-png-to-face (file)
194 "Convert FILE to a Face.
195 FILE should be a PNG file that's 48x48 and smaller than or equal to
197 (mm-with-unibyte-buffer
198 (insert-file-contents file
)
199 (when (> (buffer-size) 726)
200 (error "The file is %d bytes long, which is too long"
204 (defface gnus-x-face
'((t (:foreground
"black" :background
"white")))
205 "Face to show X-Face.
206 The colors from this face are used as the foreground and background
207 colors of the displayed X-Faces."
208 :group
'gnus-article-headers
)
210 (declare-function article-narrow-to-head
"gnus-art" ())
211 (declare-function gnus-article-goto-header
"gnus-art" (header))
212 (declare-function gnus-add-image
"gnus-art" (category image
))
213 (declare-function gnus-add-wash-type
"gnus-art" (type))
215 (defun gnus-display-x-face-in-from (data)
216 "Display the X-Face DATA in the From header."
219 (when (or (gnus-image-type-available-p 'xface
)
220 (and (gnus-image-type-available-p 'pbm
)
221 (setq pbm
(uncompface data
))))
224 (article-narrow-to-head)
225 (gnus-article-goto-header "from")
227 (insert "From: [no `from' set]\n")
232 (if (gnus-image-type-available-p 'xface
)
233 (apply 'gnus-create-image
(concat "X-Face: " data
) 'xface t
234 (cdr (assq 'xface gnus-face-properties-alist
)))
235 (apply 'gnus-create-image pbm
'pbm t
236 (cdr (assq 'pbm gnus-face-properties-alist
))))
238 (gnus-add-wash-type 'xface
))))))
240 (defun gnus-grab-cam-x-face ()
241 "Grab a picture off the camera and make it into an X-Face."
243 (shell-command "xawtv-remote snap ppm")
245 (while (null (setq file
(directory-files "/tftpboot/sparky/tmp"
248 (setq file
(car file
))
251 (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"
258 (defun gnus-grab-cam-face ()
259 "Grab a picture off the camera and make it into an X-Face."
261 (shell-command "xawtv-remote snap ppm")
264 (while (null (setq file
(directory-files "/tftpboot/sparky/tmp"
267 (setq file
(car file
))
269 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
271 (let ((gnus-convert-image-to-face-command
272 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
273 (gnus-fun-ppm-change-string))))
274 (setq result
(gnus-face-from-file "/tmp/gnus.face.ppm")))
276 ;;(delete-file "/tmp/gnus.face.ppm")
279 (defun gnus-fun-ppm-change-string ()
280 (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
281 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
282 (format (concat "'#%02x%02x%02x' '#"
283 (nth (random 6) possibilites
)
287 (push (format format i i i i i i
)
289 (mapconcat 'identity values
" ")))
293 ;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
294 ;;; gnus-fun.el ends here