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