* Makefile.in (epaths-force-w32): Use abs_srcdir
[emacs.git] / lisp / gnus / gnus-fun.el
blob1c9b4ceaf94ca5a1141556e86ac934eb43d93310
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
3 ;; Copyright (C) 2002-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32 (require 'cl))
34 (require 'mm-util)
35 (require 'gnus-ems)
36 (require 'gnus-util)
37 (require 'gnus)
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."
43 :version "22.1"
44 :group 'gnus-fun
45 :type 'directory)
47 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
48 "Command for converting a PBM to an X-Face."
49 :version "22.1"
50 :group 'gnus-fun
51 :type 'string)
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 X-Face header data on stdout."
58 :version "22.1"
59 :group 'gnus-fun
60 :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
61 "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
62 (const :tag "convert"
63 "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
64 (string)))
66 (defcustom gnus-convert-image-to-face-command
67 "convert -scale 48x48! %s -colors %d png:-"
68 "Command for converting an image to a Face.
70 The command must take an image filename (first format argument
71 \"%s\") and the number of colors (second format argument: \"%d\")
72 as input. The output must be the Face header data on stdout in
73 PNG format."
74 :version "22.1"
75 :group 'gnus-fun
76 :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
77 "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
78 (const :tag "convert"
79 "convert -scale 48x48! %s -colors %d png:-")
80 (string)))
82 (defun gnus-shell-command-to-string (command)
83 "Like `shell-command-to-string' except not mingling ERROR."
84 (with-output-to-string
85 (call-process shell-file-name nil (list standard-output nil)
86 nil shell-command-switch command)))
88 ;;;###autoload
89 (defun gnus-random-x-face ()
90 "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
91 (interactive)
92 (when (file-exists-p gnus-x-face-directory)
93 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
94 (file (nth (random (length files)) files)))
95 (when file
96 (gnus-shell-command-to-string
97 (format gnus-convert-pbm-to-x-face-command
98 (shell-quote-argument file)))))))
100 (autoload 'message-goto-eoh "message" nil t)
102 ;;;###autoload
103 (defun gnus-insert-random-x-face-header ()
104 "Insert a random X-Face header from `gnus-x-face-directory'."
105 (interactive)
106 (let ((data (gnus-random-x-face)))
107 (save-excursion
108 (message-goto-eoh)
109 (if data
110 (insert "X-Face: " data)
111 (message
112 "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
113 gnus-x-face-directory)))))
115 ;;;###autoload
116 (defun gnus-x-face-from-file (file)
117 "Insert an X-Face header based on an image file.
119 Depending on `gnus-convert-image-to-x-face-command' it may accept
120 different input formats."
121 (interactive "fImage file name: ")
122 (when (file-exists-p file)
123 (gnus-shell-command-to-string
124 (format gnus-convert-image-to-x-face-command
125 (shell-quote-argument (expand-file-name file))))))
127 ;;;###autoload
128 (defun gnus-face-from-file (file)
129 "Return a Face header based on an image file.
131 Depending on `gnus-convert-image-to-face-command' it may accept
132 different input formats."
133 (interactive "fImage file name: ")
134 (when (file-exists-p file)
135 (let ((done nil)
136 (attempt "")
137 (quant 16))
138 (while (and (not done)
139 (> quant 1))
140 (setq attempt
141 (let ((coding-system-for-read 'binary))
142 (gnus-shell-command-to-string
143 (format gnus-convert-image-to-face-command
144 (shell-quote-argument (expand-file-name file))
145 quant))))
146 (if (> (length attempt) 726)
147 (progn
148 (setq quant (- quant (if (< quant 10) 1 2)))
149 (gnus-message 9 "Length %d; trying quant %d"
150 (length attempt) quant))
151 (setq done t)))
152 (if done
153 (mm-with-unibyte-buffer
154 (insert attempt)
155 (gnus-face-encode))
156 nil))))
158 (defun gnus-face-encode ()
159 (let ((step 72))
160 (base64-encode-region (point-min) (point-max))
161 (goto-char (point-min))
162 (while (search-forward "\n" nil t)
163 (replace-match ""))
164 (goto-char (point-min))
165 (while (> (- (point-max) (point))
166 step)
167 (forward-char step)
168 (insert "\n ")
169 (setq step 76))
170 (buffer-string)))
172 ;;;###autoload
173 (defun gnus-convert-face-to-png (face)
174 "Convert FACE (which is base64-encoded) to a PNG.
175 The PNG is returned as a string."
176 (mm-with-unibyte-buffer
177 (insert face)
178 (ignore-errors
179 (base64-decode-region (point-min) (point-max)))
180 (buffer-string)))
182 ;;;###autoload
183 (defun gnus-convert-png-to-face (file)
184 "Convert FILE to a Face.
185 FILE should be a PNG file that's 48x48 and smaller than or equal to
186 726 bytes."
187 (mm-with-unibyte-buffer
188 (insert-file-contents file)
189 (when (> (buffer-size) 726)
190 (error "The file is %d bytes long, which is too long"
191 (buffer-size)))
192 (gnus-face-encode)))
194 (defface gnus-x-face '((t (:foreground "black" :background "white")))
195 "Face to show X-Face.
196 The colors from this face are used as the foreground and background
197 colors of the displayed X-Faces."
198 :group 'gnus-article-headers)
200 (declare-function article-narrow-to-head "gnus-art" ())
201 (declare-function gnus-article-goto-header "gnus-art" (header))
202 (declare-function gnus-add-image "gnus-art" (category image))
203 (declare-function gnus-add-wash-type "gnus-art" (type))
205 (defun gnus-display-x-face-in-from (data)
206 "Display the X-Face DATA in the From header."
207 (require 'gnus-art)
208 (let (pbm)
209 (when (or (gnus-image-type-available-p 'xface)
210 (and (gnus-image-type-available-p 'pbm)
211 (setq pbm (uncompface data))))
212 (save-excursion
213 (save-restriction
214 (article-narrow-to-head)
215 (gnus-article-goto-header "from")
216 (when (bobp)
217 (insert "From: [no `from' set]\n")
218 (forward-char -17))
219 (gnus-add-image
220 'xface
221 (gnus-put-image
222 (if (gnus-image-type-available-p 'xface)
223 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
224 (cdr (assq 'xface gnus-face-properties-alist)))
225 (apply 'gnus-create-image pbm 'pbm t
226 (cdr (assq 'pbm gnus-face-properties-alist))))
227 nil 'xface))
228 (gnus-add-wash-type 'xface))))))
230 (defun gnus-grab-cam-x-face ()
231 "Grab a picture off the camera and make it into an X-Face."
232 (interactive)
233 (shell-command "xawtv-remote snap ppm")
234 (let ((file nil))
235 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
236 t "snap.*ppm")))
237 (sleep-for 1))
238 (setq file (car file))
239 (with-temp-buffer
240 (shell-command
241 (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"
242 file)
243 (current-buffer))
244 ;;(sleep-for 3)
245 (delete-file file)
246 (buffer-string))))
248 (defun gnus-grab-cam-face ()
249 "Grab a picture off the camera and make it into an X-Face."
250 (interactive)
251 (shell-command "xawtv-remote snap ppm")
252 (let ((file nil)
253 result)
254 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
255 t "snap.*ppm")))
256 (sleep-for 1))
257 (setq file (car file))
258 (shell-command
259 (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
260 file))
261 (let ((gnus-convert-image-to-face-command
262 (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
263 (gnus-fun-ppm-change-string))))
264 (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
265 (delete-file file)
266 ;;(delete-file "/tmp/gnus.face.ppm")
267 result))
269 (defun gnus-fun-ppm-change-string ()
270 (let* ((possibilities '("%02x0000" "00%02x00" "0000%02x"
271 "%02x%02x00" "00%02x%02x" "%02x00%02x"))
272 (format (concat "'#%02x%02x%02x' '#"
273 (nth (random 6) possibilities)
274 "'"))
275 (values nil))
276 (dotimes (i 255)
277 (push (format format i i i i i i)
278 values))
279 (mapconcat 'identity values " ")))
281 (defun gnus-funcall-no-warning (function &rest args)
282 (when (fboundp function)
283 (apply function args)))
285 (provide 'gnus-fun)
287 ;;; gnus-fun.el ends here