1 ;;; gnus-fun.el --- various frivoluos extension functions to Gnus
2 ;; Copyright (C) 2002 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 a GIF to an X-Face."
43 (defun gnus-shell-command-to-string (command)
44 "Like `shell-command-to-string' except not mingling ERROR."
45 (with-output-to-string
46 (call-process shell-file-name nil (list standard-output nil)
47 nil shell-command-switch command)))
49 (defun gnus-shell-command-on-region (start end command)
50 "A simplified `shell-command-on-region'.
51 Output to the current buffer, replace text, and don't mingle error."
52 (call-process-region start end shell-file-name t
53 (list (current-buffer) nil)
54 nil shell-command-switch command))
57 (defun gnus-random-x-face ()
58 "Insert a random X-Face header from `gnus-x-face-directory'."
60 (when (file-exists-p gnus-x-face-directory)
61 (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
62 (file (nth (random (length files)) files)))
64 (gnus-shell-command-to-string
65 (format gnus-convert-pbm-to-x-face-command
66 (shell-quote-argument file)))))))
69 (defun gnus-x-face-from-file (file)
70 "Insert an X-Face header based on an image file."
71 (interactive "fImage file name:" )
72 (when (file-exists-p file)
73 (gnus-shell-command-to-string
74 (format gnus-convert-image-to-x-face-command
75 (shell-quote-argument file)))))
77 (defun gnus-convert-image-to-gray-x-face (file depth)
78 (let* ((mapfile (mm-make-temp-file (expand-file-name "gnus."
80 (levels (expt 2 depth))
81 (step (/ 255 (1- levels)))
82 color-alist bits bits-list mask pixel x-faces)
83 (with-temp-file mapfile
85 (insert (format "%d 1\n" levels))
88 (insert (format "%d %d %d\n"
89 (* step i) (* step i) (* step i)))
90 (push (cons (* step i) i) color-alist)))
91 (when (file-exists-p file)
93 (insert (gnus-shell-command-to-string
94 (format "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant -fs -map %s | ppmtopgm | pnmnoraw"
95 (shell-quote-argument file)
97 (goto-char (point-min))
99 (while (setq pixel (ignore-errors (read (current-buffer))))
100 (push (cdr (assq pixel color-alist)) bits-list))
101 (setq bits-list (nreverse bits-list))
102 (dotimes (bit-number depth)
103 (setq mask (expt 2 bit-number))
105 (insert "P1\n48 48\n")
106 (dolist (bits bits-list)
107 (insert (if (zerop (logand bits mask)) "0 " "1 ")))
108 (gnus-shell-command-on-region
109 (point-min) (point-max)
110 ;; the following is taken from xbmtoikon:
111 "pbmtoicon | sed '/^[ ]*[*\\\\/]/d; s/[ ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface")
112 (push (buffer-string) x-faces))))
113 (dotimes (i (length x-faces))
114 (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
116 (delete-file mapfile)))
119 (defun gnus-convert-gray-x-face-to-xpm (faces)
120 (let* ((depth (length faces))
121 (scale (/ 255 (1- (expt 2 depth))))
123 (coding-system-for-read 'binary)
124 (coding-system-for-write 'binary)
125 default-enable-multibyte-characters
126 start bit-array bit-arrays pixel)
130 (insert (uncompface face))
131 (gnus-shell-command-on-region
132 (point-min) (point-max)
134 (goto-char (point-min))
143 (setq bit-array (read (current-buffer)))
144 (unless (= (length bit-array) (* 48 48))
146 (push bit-array bit-arrays))
149 (insert "P2\n48 48\n255\n")
150 (dotimes (i (* 48 48))
152 (dotimes (plane depth)
153 (setq pixel (+ (* pixel 2) (aref (nth plane bit-arrays) i))))
154 (insert (number-to-string (* scale pixel)) " "))
155 (gnus-shell-command-on-region
156 (point-min) (point-max)
161 (defun gnus-convert-gray-x-face-region (beg end)
162 "Convert the X-Faces in region to a PPM file."
164 (let ((input (buffer-substring beg end))
168 (goto-char (point-min))
171 (mail-header-narrow-to-field)
172 (push (mail-header-field-value) faces)
173 (goto-char (point-max)))))
174 (gnus-convert-gray-x-face-to-xpm faces)))
176 (defface gnus-x-face '((t (:foreground "black" :background "white")))
177 "Face to show X-Face.
178 The colors from this face are used as the foreground and background
179 colors of the displayed X-Faces."
180 :group 'gnus-article-headers)
182 (defun gnus-display-x-face-in-from (data)
183 "Display the X-Face DATA in the From header."
184 (let ((default-enable-multibyte-characters nil)
186 (when (or (gnus-image-type-available-p 'xface)
187 (and (gnus-image-type-available-p 'pbm)
188 (setq pbm (uncompface data))))
191 (article-narrow-to-head)
192 (gnus-article-goto-header "from")
194 (insert "From: [no `from' set]\n")
199 (if (gnus-image-type-available-p 'xface)
201 (concat "X-Face: " data)
202 'xface t :ascent 'center :face 'gnus-x-face)
204 pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
205 (gnus-add-wash-type 'xface))))))
207 (defun gnus-grab-cam-x-face ()
208 "Grab a picture off the camera and make it into an X-Face."
210 (shell-command "xawtv-remote snap ppm")
212 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
215 (setq file (car file))
218 (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"
225 (defun gnus-grab-gray-x-face ()
226 "Grab a picture off the camera and make it into an X-Face."
228 (shell-command "xawtv-remote snap ppm")
230 (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
233 (setq file (car file))
236 (format "pnmcut -left 70 -top 100 -width 144 -height 144 '%s' | ppmquant 256 2>/dev/null | ppmtogif > '%s.gif'"
240 (gnus-convert-image-to-gray-x-face (concat file ".gif") 3)
241 (delete-file (concat file ".gif"))))
245 ;;; gnus-fun.el ends here