eudc-bob.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. ;;; eudc-bob.el --- Binary Objects Support for EUDC
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Oscar Figueiredo <oscar@cpe.fr>
  4. ;; Maintainer: Pavel Janík <Pavel@Janik.cz>
  5. ;; Keywords: comm
  6. ;; Package: eudc
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Usage:
  20. ;; See the corresponding info file
  21. ;;; Code:
  22. (require 'eudc)
  23. (defvar eudc-bob-generic-keymap nil
  24. "Keymap for multimedia objects.")
  25. (defvar eudc-bob-image-keymap nil
  26. "Keymap for inline images.")
  27. (defvar eudc-bob-sound-keymap nil
  28. "Keymap for inline sounds.")
  29. (defvar eudc-bob-url-keymap nil
  30. "Keymap for inline urls.")
  31. (defvar eudc-bob-mail-keymap nil
  32. "Keymap for inline e-mail addresses.")
  33. (defvar eudc-bob-generic-menu
  34. '("EUDC Binary Object Menu"
  35. ["---" nil nil]
  36. ["Pipe to external program" eudc-bob-pipe-object-to-external-program t]
  37. ["Save object" eudc-bob-save-object t]))
  38. (defvar eudc-bob-image-menu
  39. `("EUDC Image Menu"
  40. ["---" nil nil]
  41. ["Toggle inline display" eudc-bob-toggle-inline-display
  42. (eudc-bob-can-display-inline-images)]
  43. ,@(cdr (cdr eudc-bob-generic-menu))))
  44. (defvar eudc-bob-sound-menu
  45. `("EUDC Sound Menu"
  46. ["---" nil nil]
  47. ["Play sound" eudc-bob-play-sound-at-point
  48. (fboundp 'play-sound)]
  49. ,@(cdr (cdr eudc-bob-generic-menu))))
  50. (defun eudc-jump-to-event (event)
  51. "Jump to the window and point where EVENT occurred."
  52. (if (fboundp 'event-closest-point)
  53. (goto-char (event-closest-point event))
  54. (set-buffer (window-buffer (posn-window (event-start event))))
  55. (goto-char (posn-point (event-start event)))))
  56. (defun eudc-bob-get-overlay-prop (prop)
  57. "Get property PROP from one of the overlays around."
  58. (let ((overlays (append (overlays-at (1- (point)))
  59. (overlays-at (point))))
  60. overlay value
  61. (notfound t))
  62. (while (and notfound
  63. (setq overlay (car overlays)))
  64. (if (setq value (overlay-get overlay prop))
  65. (setq notfound nil))
  66. (setq overlays (cdr overlays)))
  67. value))
  68. (defun eudc-bob-can-display-inline-images ()
  69. "Return non-nil if we can display images inline."
  70. (if (fboundp 'console-type)
  71. (and (memq (console-type) '(x mswindows))
  72. (fboundp 'make-glyph))
  73. (and (fboundp 'display-graphic-p)
  74. (display-graphic-p))))
  75. (defun eudc-bob-make-button (label keymap &optional menu plist)
  76. "Create a button with LABEL.
  77. Attach KEYMAP, MENU and properties from PLIST to a new overlay covering
  78. LABEL."
  79. (let (overlay
  80. (p (point))
  81. prop val)
  82. (insert label)
  83. (put-text-property p (point) 'face 'bold)
  84. (setq overlay (make-overlay p (point)))
  85. (overlay-put overlay 'mouse-face 'highlight)
  86. (overlay-put overlay 'keymap keymap)
  87. (overlay-put overlay 'local-map keymap)
  88. (overlay-put overlay 'menu menu)
  89. (while plist
  90. (setq prop (car plist)
  91. plist (cdr plist)
  92. val (car plist)
  93. plist (cdr plist))
  94. (overlay-put overlay prop val))))
  95. (defun eudc-bob-display-jpeg (data inline)
  96. "Display the JPEG DATA at point.
  97. If INLINE is non-nil, try to inline the image otherwise simply
  98. display a button."
  99. (cond ((fboundp 'make-glyph)
  100. (let ((glyph (if (eudc-bob-can-display-inline-images)
  101. (make-glyph (list (vector 'jpeg :data data)
  102. [string :data "[JPEG Picture]"])))))
  103. (eudc-bob-make-button "[JPEG Picture]"
  104. eudc-bob-image-keymap
  105. eudc-bob-image-menu
  106. (list 'glyph glyph
  107. 'end-glyph (if inline glyph)
  108. 'duplicable t
  109. 'invisible inline
  110. 'start-open t
  111. 'end-open t
  112. 'object-data data))))
  113. ((fboundp 'create-image)
  114. (let* ((image (create-image data nil t))
  115. (props (list 'object-data data 'eudc-image image)))
  116. (when (and inline (image-type-available-p 'jpeg))
  117. (setq props (nconc (list 'display image) props)))
  118. (eudc-bob-make-button "[Picture]"
  119. eudc-bob-image-keymap
  120. eudc-bob-image-menu
  121. props)))))
  122. (defun eudc-bob-toggle-inline-display ()
  123. "Toggle inline display of an image."
  124. (interactive)
  125. (when (eudc-bob-can-display-inline-images)
  126. (cond ((featurep 'xemacs)
  127. (let ((overlays (append (overlays-at (1- (point)))
  128. (overlays-at (point))))
  129. overlay glyph)
  130. (setq overlay (car overlays))
  131. (while (and overlay
  132. (not (setq glyph (overlay-get overlay 'glyph))))
  133. (setq overlays (cdr overlays))
  134. (setq overlay (car overlays)))
  135. (if overlay
  136. (if (overlay-get overlay 'end-glyph)
  137. (progn
  138. (overlay-put overlay 'end-glyph nil)
  139. (overlay-put overlay 'invisible nil))
  140. (overlay-put overlay 'end-glyph glyph)
  141. (overlay-put overlay 'invisible t)))))
  142. (t
  143. (let* ((overlays (append (overlays-at (1- (point)))
  144. (overlays-at (point))))
  145. image)
  146. ;; Search overlay with an image.
  147. (while (and overlays (null image))
  148. (let ((prop (overlay-get (car overlays) 'eudc-image)))
  149. (if (eq 'image (car-safe prop))
  150. (setq image prop)
  151. (setq overlays (cdr overlays)))))
  152. ;; Toggle that overlay's image display.
  153. (when overlays
  154. (let ((overlay (car overlays)))
  155. (overlay-put overlay 'display
  156. (if (overlay-get overlay 'display)
  157. nil image)))))))))
  158. (defun eudc-bob-display-audio (data)
  159. "Display a button for audio DATA."
  160. (eudc-bob-make-button "[Audio Sound]"
  161. eudc-bob-sound-keymap
  162. eudc-bob-sound-menu
  163. (list 'duplicable t
  164. 'start-open t
  165. 'end-open t
  166. 'object-data data)))
  167. (defun eudc-bob-display-generic-binary (data)
  168. "Display a button for unidentified binary DATA."
  169. (eudc-bob-make-button "[Binary Data]"
  170. eudc-bob-generic-keymap
  171. eudc-bob-generic-menu
  172. (list 'duplicable t
  173. 'start-open t
  174. 'end-open t
  175. 'object-data data)))
  176. (defun eudc-bob-play-sound-at-point ()
  177. "Play the sound data contained in the button at point."
  178. (interactive)
  179. (let (sound)
  180. (if (null (setq sound (eudc-bob-get-overlay-prop 'object-data)))
  181. (error "No sound data available here")
  182. (unless (fboundp 'play-sound)
  183. (error "Playing sounds not supported on this system"))
  184. (play-sound (list 'sound :data sound)))))
  185. (defun eudc-bob-play-sound-at-mouse (event)
  186. "Play the sound data contained in the button where EVENT occurred."
  187. (interactive "e")
  188. (save-excursion
  189. (eudc-jump-to-event event)
  190. (eudc-bob-play-sound-at-point)))
  191. (defun eudc-bob-save-object ()
  192. "Save the object data of the button at point."
  193. (interactive)
  194. (let ((data (eudc-bob-get-overlay-prop 'object-data))
  195. (buffer (generate-new-buffer "*eudc-tmp*")))
  196. (save-excursion
  197. (if (fboundp 'set-buffer-file-coding-system)
  198. (set-buffer-file-coding-system 'binary))
  199. (set-buffer buffer)
  200. (set-buffer-multibyte nil)
  201. (insert data)
  202. (save-buffer))
  203. (kill-buffer buffer)))
  204. (defun eudc-bob-pipe-object-to-external-program ()
  205. "Pipe the object data of the button at point to an external program."
  206. (interactive)
  207. (let ((data (eudc-bob-get-overlay-prop 'object-data))
  208. (buffer (generate-new-buffer "*eudc-tmp*"))
  209. program
  210. viewer)
  211. (condition-case nil
  212. (save-excursion
  213. (if (fboundp 'set-buffer-file-coding-system)
  214. (set-buffer-file-coding-system 'binary))
  215. (set-buffer buffer)
  216. (insert data)
  217. (setq program (completing-read "Viewer: " eudc-external-viewers))
  218. (if (setq viewer (assoc program eudc-external-viewers))
  219. (call-process-region (point-min) (point-max)
  220. (car (cdr viewer))
  221. (cdr (cdr viewer)))
  222. (call-process-region (point-min) (point-max) program)))
  223. (error
  224. (kill-buffer buffer)))))
  225. (defun eudc-bob-menu ()
  226. "Retrieve the menu attached to a binary object."
  227. (eudc-bob-get-overlay-prop 'menu))
  228. (defun eudc-bob-popup-menu (event)
  229. "Pop-up a menu of EUDC multimedia commands."
  230. (interactive "@e")
  231. (run-hooks 'activate-menubar-hook)
  232. (eudc-jump-to-event event)
  233. (if (featurep 'xemacs)
  234. (progn
  235. (run-hooks 'activate-popup-menu-hook)
  236. (popup-menu (eudc-bob-menu)))
  237. (let ((result (x-popup-menu t (eudc-bob-menu)))
  238. command)
  239. (if result
  240. (progn
  241. (setq command (lookup-key (eudc-bob-menu)
  242. (apply 'vector result)))
  243. (command-execute command))))))
  244. (setq eudc-bob-generic-keymap
  245. (let ((map (make-sparse-keymap)))
  246. (define-key map "s" 'eudc-bob-save-object)
  247. (define-key map "!" 'eudc-bob-pipe-object-to-external-program)
  248. (define-key map (if (featurep 'xemacs)
  249. [button3]
  250. [down-mouse-3]) 'eudc-bob-popup-menu)
  251. map))
  252. (setq eudc-bob-image-keymap
  253. (let ((map (make-sparse-keymap)))
  254. (define-key map "t" 'eudc-bob-toggle-inline-display)
  255. map))
  256. (setq eudc-bob-sound-keymap
  257. (let ((map (make-sparse-keymap)))
  258. (define-key map [return] 'eudc-bob-play-sound-at-point)
  259. (define-key map (if (featurep 'xemacs)
  260. [button2]
  261. [down-mouse-2]) 'eudc-bob-play-sound-at-mouse)
  262. map))
  263. (setq eudc-bob-url-keymap
  264. (let ((map (make-sparse-keymap)))
  265. (define-key map [return] 'browse-url-at-point)
  266. (define-key map (if (featurep 'xemacs)
  267. [button2]
  268. [down-mouse-2]) 'browse-url-at-mouse)
  269. map))
  270. (setq eudc-bob-mail-keymap
  271. (let ((map (make-sparse-keymap)))
  272. (define-key map [return] 'goto-address-at-point)
  273. (define-key map (if (featurep 'xemacs)
  274. [button2]
  275. [down-mouse-2]) 'goto-address-at-mouse)
  276. map))
  277. (set-keymap-parent eudc-bob-image-keymap eudc-bob-generic-keymap)
  278. (set-keymap-parent eudc-bob-sound-keymap eudc-bob-generic-keymap)
  279. ;; If the first arguments can be nil here, then these 3 can be
  280. ;; defconsts once more.
  281. (when (not (featurep 'xemacs))
  282. (easy-menu-define eudc-bob-generic-menu
  283. eudc-bob-generic-keymap
  284. ""
  285. eudc-bob-generic-menu)
  286. (easy-menu-define eudc-bob-image-menu
  287. eudc-bob-image-keymap
  288. ""
  289. eudc-bob-image-menu)
  290. (easy-menu-define eudc-bob-sound-menu
  291. eudc-bob-sound-keymap
  292. ""
  293. eudc-bob-sound-menu))
  294. ;;;###autoload
  295. (defun eudc-display-generic-binary (data)
  296. "Display a button for unidentified binary DATA."
  297. (eudc-bob-display-generic-binary data))
  298. ;;;###autoload
  299. (defun eudc-display-url (url)
  300. "Display URL and make it clickable."
  301. (require 'browse-url)
  302. (eudc-bob-make-button url eudc-bob-url-keymap))
  303. ;;;###autoload
  304. (defun eudc-display-mail (mail)
  305. "Display e-mail address and make it clickable."
  306. (require 'goto-addr)
  307. (eudc-bob-make-button mail eudc-bob-mail-keymap))
  308. ;;;###autoload
  309. (defun eudc-display-sound (data)
  310. "Display a button to play the sound DATA."
  311. (eudc-bob-display-audio data))
  312. ;;;###autoload
  313. (defun eudc-display-jpeg-inline (data)
  314. "Display the JPEG DATA inline at point if possible."
  315. (eudc-bob-display-jpeg data (eudc-bob-can-display-inline-images)))
  316. ;;;###autoload
  317. (defun eudc-display-jpeg-as-button (data)
  318. "Display a button for the JPEG DATA."
  319. (eudc-bob-display-jpeg data nil))
  320. ;;; eudc-bob.el ends here