gnus-fun.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ;;; gnus-fun.el --- various frivolous extension functions to Gnus
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; For Emacs <22.2 and XEmacs.
  19. (eval-and-compile
  20. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  21. (eval-when-compile
  22. (require 'cl))
  23. (require 'mm-util)
  24. (require 'gnus-ems)
  25. (require 'gnus-util)
  26. (require 'gnus)
  27. (defvar gnus-face-properties-alist)
  28. (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
  29. "*Directory where X-Face PBM files are stored."
  30. :version "22.1"
  31. :group 'gnus-fun
  32. :type 'directory)
  33. (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
  34. "Command for converting a PBM to an X-Face."
  35. :version "22.1"
  36. :group 'gnus-fun
  37. :type 'string)
  38. (defcustom gnus-convert-image-to-x-face-command
  39. "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
  40. "Command for converting an image to an X-Face.
  41. The command must take a image filename (use \"%s\") as input.
  42. The output must be the X-Face header data on stdout."
  43. :version "22.1"
  44. :group 'gnus-fun
  45. :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
  46. "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
  47. (const :tag "convert"
  48. "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
  49. (string)))
  50. (defcustom gnus-convert-image-to-face-command
  51. "convert -scale 48x48! %s -colors %d png:-"
  52. "Command for converting an image to a Face.
  53. The command must take an image filename (first format argument
  54. \"%s\") and the number of colors (second format argument: \"%d\")
  55. as input. The output must be the Face header data on stdout in
  56. PNG format."
  57. :version "22.1"
  58. :group 'gnus-fun
  59. :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
  60. "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
  61. (const :tag "convert"
  62. "convert -scale 48x48! %s -colors %d png:-")
  63. (string)))
  64. (defun gnus-shell-command-to-string (command)
  65. "Like `shell-command-to-string' except not mingling ERROR."
  66. (with-output-to-string
  67. (call-process shell-file-name nil (list standard-output nil)
  68. 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))
  75. ;;;###autoload
  76. (defun gnus-random-x-face ()
  77. "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
  78. (interactive)
  79. (when (file-exists-p gnus-x-face-directory)
  80. (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
  81. (file (nth (random (length files)) files)))
  82. (when file
  83. (gnus-shell-command-to-string
  84. (format gnus-convert-pbm-to-x-face-command
  85. (shell-quote-argument file)))))))
  86. (autoload 'message-goto-eoh "message" nil t)
  87. ;;;###autoload
  88. (defun gnus-insert-random-x-face-header ()
  89. "Insert a random X-Face header from `gnus-x-face-directory'."
  90. (interactive)
  91. (let ((data (gnus-random-x-face)))
  92. (save-excursion
  93. (message-goto-eoh)
  94. (if data
  95. (insert "X-Face: " data)
  96. (message
  97. "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?"
  98. gnus-x-face-directory)))))
  99. ;;;###autoload
  100. (defun gnus-x-face-from-file (file)
  101. "Insert an X-Face header based on an image file.
  102. Depending on `gnus-convert-image-to-x-face-command' it may accept
  103. different input formats."
  104. (interactive "fImage file name: ")
  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))))))
  109. ;;;###autoload
  110. (defun gnus-face-from-file (file)
  111. "Return a Face header based on an image file.
  112. Depending on `gnus-convert-image-to-face-command' it may accept
  113. different input formats."
  114. (interactive "fImage file name: ")
  115. (when (file-exists-p file)
  116. (let ((done nil)
  117. (attempt "")
  118. (quant 16))
  119. (while (and (not done)
  120. (> quant 1))
  121. (setq attempt
  122. (let ((coding-system-for-read 'binary))
  123. (gnus-shell-command-to-string
  124. (format gnus-convert-image-to-face-command
  125. (shell-quote-argument (expand-file-name file))
  126. quant))))
  127. (if (> (length attempt) 726)
  128. (progn
  129. (setq quant (- quant (if (< quant 10) 1 2)))
  130. (gnus-message 9 "Length %d; trying quant %d"
  131. (length attempt) quant))
  132. (setq done t)))
  133. (if done
  134. (mm-with-unibyte-buffer
  135. (insert attempt)
  136. (gnus-face-encode))
  137. nil))))
  138. (defun gnus-face-encode ()
  139. (let ((step 72))
  140. (base64-encode-region (point-min) (point-max))
  141. (goto-char (point-min))
  142. (while (search-forward "\n" nil t)
  143. (replace-match ""))
  144. (goto-char (point-min))
  145. (while (> (- (point-max) (point))
  146. step)
  147. (forward-char step)
  148. (insert "\n ")
  149. (setq step 76))
  150. (buffer-string)))
  151. ;;;###autoload
  152. (defun gnus-convert-face-to-png (face)
  153. "Convert FACE (which is base64-encoded) to a PNG.
  154. The PNG is returned as a string."
  155. (mm-with-unibyte-buffer
  156. (insert face)
  157. (ignore-errors
  158. (base64-decode-region (point-min) (point-max)))
  159. (buffer-string)))
  160. ;;;###autoload
  161. (defun gnus-convert-png-to-face (file)
  162. "Convert FILE to a Face.
  163. FILE should be a PNG file that's 48x48 and smaller than or equal to
  164. 726 bytes."
  165. (mm-with-unibyte-buffer
  166. (insert-file-contents file)
  167. (when (> (buffer-size) 726)
  168. (error "The file is %d bytes long, which is too long"
  169. (buffer-size)))
  170. (gnus-face-encode)))
  171. (defface gnus-x-face '((t (:foreground "black" :background "white")))
  172. "Face to show X-Face.
  173. The colors from this face are used as the foreground and background
  174. colors of the displayed X-Faces."
  175. :group 'gnus-article-headers)
  176. (declare-function article-narrow-to-head "gnus-art" ())
  177. (declare-function gnus-article-goto-header "gnus-art" (header))
  178. (declare-function gnus-add-image "gnus-art" (category image))
  179. (declare-function gnus-add-wash-type "gnus-art" (type))
  180. (defun gnus-display-x-face-in-from (data)
  181. "Display the X-Face DATA in the From header."
  182. (require 'gnus-art)
  183. (let (pbm)
  184. (when (or (gnus-image-type-available-p 'xface)
  185. (and (gnus-image-type-available-p 'pbm)
  186. (setq pbm (uncompface data))))
  187. (save-excursion
  188. (save-restriction
  189. (article-narrow-to-head)
  190. (gnus-article-goto-header "from")
  191. (when (bobp)
  192. (insert "From: [no `from' set]\n")
  193. (forward-char -17))
  194. (gnus-add-image
  195. 'xface
  196. (gnus-put-image
  197. (if (gnus-image-type-available-p 'xface)
  198. (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
  199. (cdr (assq 'xface gnus-face-properties-alist)))
  200. (apply 'gnus-create-image pbm 'pbm t
  201. (cdr (assq 'pbm gnus-face-properties-alist))))
  202. nil 'xface))
  203. (gnus-add-wash-type 'xface))))))
  204. (defun gnus-grab-cam-x-face ()
  205. "Grab a picture off the camera and make it into an X-Face."
  206. (interactive)
  207. (shell-command "xawtv-remote snap ppm")
  208. (let ((file nil))
  209. (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
  210. t "snap.*ppm")))
  211. (sleep-for 1))
  212. (setq file (car file))
  213. (with-temp-buffer
  214. (shell-command
  215. (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"
  216. file)
  217. (current-buffer))
  218. ;;(sleep-for 3)
  219. (delete-file file)
  220. (buffer-string))))
  221. (defun gnus-grab-cam-face ()
  222. "Grab a picture off the camera and make it into an X-Face."
  223. (interactive)
  224. (shell-command "xawtv-remote snap ppm")
  225. (let ((file nil)
  226. result)
  227. (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
  228. t "snap.*ppm")))
  229. (sleep-for 1))
  230. (setq file (car file))
  231. (shell-command
  232. (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
  233. file))
  234. (let ((gnus-convert-image-to-face-command
  235. (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
  236. (gnus-fun-ppm-change-string))))
  237. (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
  238. (delete-file file)
  239. ;;(delete-file "/tmp/gnus.face.ppm")
  240. result))
  241. (defun gnus-fun-ppm-change-string ()
  242. (let* ((possibilities '("%02x0000" "00%02x00" "0000%02x"
  243. "%02x%02x00" "00%02x%02x" "%02x00%02x"))
  244. (format (concat "'#%02x%02x%02x' '#"
  245. (nth (random 6) possibilities)
  246. "'"))
  247. (values nil))
  248. (dotimes (i 255)
  249. (push (format format i i i i i i)
  250. values))
  251. (mapconcat 'identity values " ")))
  252. (provide 'gnus-fun)
  253. ;;; gnus-fun.el ends here