2020-08-15 21:30:11 +00:00
|
|
|
;;; eudc-bob.el --- Binary Objects Support for EUDC -*- lexical-binding: t; -*-
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2022-01-01 07:45:51 +00:00
|
|
|
;; Copyright (C) 1999-2022 Free Software Foundation, Inc.
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2002-01-16 08:05:19 +00:00
|
|
|
;; Author: Oscar Figueiredo <oscar@cpe.fr>
|
2015-01-25 22:54:41 +00:00
|
|
|
;; Pavel Janík <Pavel@Janik.cz>
|
|
|
|
;; Maintainer: Thomas Fitzsimmons <fitzsim@fitzsim.org>
|
2002-01-06 16:37:36 +00:00
|
|
|
;; Keywords: comm
|
2010-08-29 16:17:13 +00:00
|
|
|
;; Package: eudc
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 07:31:51 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2000-01-12 20:50:20 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 07:31:51 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
;; 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
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2001-07-16 07:46:48 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
2018-06-12 18:50:48 +00:00
|
|
|
;; 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.
|
|
|
|
|
2000-01-12 20:50:20 +00:00
|
|
|
;;; Usage:
|
2018-06-12 18:50:48 +00:00
|
|
|
|
|
|
|
;; The eudc-bob interactive functions are invoked when the user
|
|
|
|
;; interacts with an `eudc-query-form' results buffer.
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'eudc)
|
|
|
|
|
2020-08-15 21:30:11 +00:00
|
|
|
(defvar eudc-bob-generic-keymap
|
|
|
|
(let ((map (make-sparse-keymap)))
|
2021-03-08 15:11:22 +00:00
|
|
|
(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)
|
2020-08-15 21:30:11 +00:00
|
|
|
map)
|
2000-01-12 20:50:20 +00:00
|
|
|
"Keymap for multimedia objects.")
|
|
|
|
|
2020-08-15 21:30:11 +00:00
|
|
|
(defvar eudc-bob-image-keymap
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(set-keymap-parent map eudc-bob-generic-keymap)
|
2021-03-08 15:11:22 +00:00
|
|
|
(define-key map "t" #'eudc-bob-toggle-inline-display)
|
2020-08-15 21:30:11 +00:00
|
|
|
map)
|
2000-01-12 20:50:20 +00:00
|
|
|
"Keymap for inline images.")
|
|
|
|
|
2020-08-15 21:30:11 +00:00
|
|
|
(defvar eudc-bob-sound-keymap
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(set-keymap-parent map eudc-bob-generic-keymap)
|
2021-03-08 15:11:22 +00:00
|
|
|
(define-key map (kbd "RET") #'eudc-bob-play-sound-at-point)
|
|
|
|
(define-key map [down-mouse-2] #'eudc-bob-play-sound-at-mouse)
|
2020-08-15 21:30:11 +00:00
|
|
|
map)
|
2000-01-13 13:55:49 +00:00
|
|
|
"Keymap for inline sounds.")
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2020-08-15 21:30:11 +00:00
|
|
|
(defvar eudc-bob-url-keymap
|
|
|
|
(let ((map (make-sparse-keymap)))
|
2021-03-08 15:11:22 +00:00
|
|
|
(define-key map (kbd "RET") #'browse-url-at-point)
|
|
|
|
(define-key map [down-mouse-2] #'browse-url-at-mouse)
|
2020-08-15 21:30:11 +00:00
|
|
|
map)
|
2002-01-04 07:59:46 +00:00
|
|
|
"Keymap for inline urls.")
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2020-08-15 21:30:11 +00:00
|
|
|
(defvar eudc-bob-mail-keymap
|
|
|
|
(let ((map (make-sparse-keymap)))
|
2021-03-08 15:11:22 +00:00
|
|
|
(define-key map (kbd "RET") #'goto-address-at-point)
|
|
|
|
(define-key map [down-mouse-2] #'goto-address-at-point)
|
2020-08-15 21:30:11 +00:00
|
|
|
map)
|
2002-01-16 08:05:19 +00:00
|
|
|
"Keymap for inline e-mail addresses.")
|
|
|
|
|
2009-09-01 03:16:07 +00:00
|
|
|
(defvar eudc-bob-generic-menu
|
2000-01-12 20:50:20 +00:00
|
|
|
'("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]))
|
|
|
|
|
2009-09-01 03:16:07 +00:00
|
|
|
(defvar eudc-bob-image-menu
|
2000-01-12 20:50:20 +00:00
|
|
|
`("EUDC Image Menu"
|
|
|
|
["---" nil nil]
|
|
|
|
["Toggle inline display" eudc-bob-toggle-inline-display
|
2022-05-13 19:30:05 +00:00
|
|
|
(display-graphic-p)]
|
2000-01-12 20:50:20 +00:00
|
|
|
,@(cdr (cdr eudc-bob-generic-menu))))
|
2002-01-06 15:08:06 +00:00
|
|
|
|
2009-09-01 03:16:07 +00:00
|
|
|
(defvar eudc-bob-sound-menu
|
2000-01-12 20:50:20 +00:00
|
|
|
`("EUDC Sound Menu"
|
|
|
|
["---" nil nil]
|
2000-11-15 20:54:15 +00:00
|
|
|
["Play sound" eudc-bob-play-sound-at-point
|
2020-08-12 16:37:18 +00:00
|
|
|
(fboundp 'play-sound-internal)]
|
2000-01-12 20:50:20 +00:00
|
|
|
,@(cdr (cdr eudc-bob-generic-menu))))
|
2002-01-06 15:08:06 +00:00
|
|
|
|
2000-01-12 20:50:20 +00:00
|
|
|
(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-make-button (label keymap &optional menu plist)
|
|
|
|
"Create a button with LABEL.
|
2000-11-15 20:54:15 +00:00
|
|
|
Attach KEYMAP, MENU and properties from PLIST to a new overlay covering
|
2000-01-12 20:50:20 +00:00
|
|
|
LABEL."
|
|
|
|
(let (overlay
|
|
|
|
(p (point))
|
|
|
|
prop val)
|
2022-04-21 21:24:29 +00:00
|
|
|
(insert (or label ""))
|
2000-11-15 20:54:15 +00:00
|
|
|
(put-text-property p (point) 'face 'bold)
|
2000-01-12 20:50:20 +00:00
|
|
|
(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.
|
2000-11-15 20:54:15 +00:00
|
|
|
If INLINE is non-nil, try to inline the image otherwise simply
|
2000-01-12 20:50:20 +00:00
|
|
|
display a button."
|
2022-05-13 19:30:05 +00:00
|
|
|
(cond ((fboundp 'create-image)
|
2000-01-13 13:55:49 +00:00
|
|
|
(let* ((image (create-image data nil t))
|
|
|
|
(props (list 'object-data data 'eudc-image image)))
|
2000-11-15 20:54:15 +00:00
|
|
|
(when (and inline (image-type-available-p 'jpeg))
|
2000-01-13 13:55:49 +00:00
|
|
|
(setq props (nconc (list 'display image) props)))
|
|
|
|
(eudc-bob-make-button "[Picture]"
|
|
|
|
eudc-bob-image-keymap
|
|
|
|
eudc-bob-image-menu
|
|
|
|
props)))))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
(defun eudc-bob-toggle-inline-display ()
|
|
|
|
"Toggle inline display of an image."
|
|
|
|
(interactive)
|
2022-05-13 19:30:05 +00:00
|
|
|
(when (display-graphic-p)
|
2018-08-15 17:16:25 +00:00
|
|
|
(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)))))))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
(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
|
2020-11-20 15:43:57 +00:00
|
|
|
'object-data data)))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
(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
|
2020-11-20 15:43:57 +00:00
|
|
|
'object-data data)))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
(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")
|
2020-08-12 16:37:18 +00:00
|
|
|
(unless (fboundp 'play-sound-internal)
|
2002-01-16 08:05:19 +00:00
|
|
|
(error "Playing sounds not supported on this system"))
|
|
|
|
(play-sound (list 'sound :data sound)))))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
(defun eudc-bob-play-sound-at-mouse (event)
|
|
|
|
"Play the sound data contained in the button where EVENT occurred."
|
|
|
|
(interactive "e")
|
|
|
|
(save-excursion
|
2020-08-15 21:30:11 +00:00
|
|
|
(mouse-set-point event)
|
2000-01-12 20:50:20 +00:00
|
|
|
(eudc-bob-play-sound-at-point)))
|
2002-01-05 18:54:53 +00:00
|
|
|
|
2020-08-15 21:30:11 +00:00
|
|
|
(defun eudc-bob-save-object (filename)
|
2000-01-12 20:50:20 +00:00
|
|
|
"Save the object data of the button at point."
|
2020-08-15 21:30:11 +00:00
|
|
|
(interactive "fWrite file: ")
|
2000-01-12 20:50:20 +00:00
|
|
|
(let ((data (eudc-bob-get-overlay-prop 'object-data))
|
2020-08-15 21:30:11 +00:00
|
|
|
(coding-system-for-write 'binary)) ;Inhibit EOL conversion.
|
|
|
|
(write-region data nil filename)))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2020-08-16 02:44:16 +00:00
|
|
|
(defun eudc-bob-pipe-object-to-external-program (program)
|
2000-01-12 20:50:20 +00:00
|
|
|
"Pipe the object data of the button at point to an external program."
|
2020-08-16 02:44:16 +00:00
|
|
|
(interactive (list (completing-read "Viewer: " eudc-external-viewers)))
|
2000-01-12 20:50:20 +00:00
|
|
|
(let ((data (eudc-bob-get-overlay-prop 'object-data))
|
2020-08-16 02:44:16 +00:00
|
|
|
(viewer (assoc program eudc-external-viewers)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(set-buffer-multibyte nil)
|
|
|
|
(insert data)
|
|
|
|
(let ((coding-system-for-write 'binary)) ;Inhibit EOL conversion
|
|
|
|
(if viewer
|
|
|
|
(call-process-region (point-min) (point-max)
|
|
|
|
(car (cdr viewer))
|
|
|
|
(cdr (cdr viewer)))
|
|
|
|
(call-process-region (point-min) (point-max) program))))))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
(defun eudc-bob-menu ()
|
|
|
|
"Retrieve the menu attached to a binary object."
|
|
|
|
(eudc-bob-get-overlay-prop 'menu))
|
2002-01-06 15:08:06 +00:00
|
|
|
|
2000-01-12 20:50:20 +00:00
|
|
|
(defun eudc-bob-popup-menu (event)
|
|
|
|
"Pop-up a menu of EUDC multimedia commands."
|
|
|
|
(interactive "@e")
|
|
|
|
(run-hooks 'activate-menubar-hook)
|
2020-08-15 21:30:11 +00:00
|
|
|
(mouse-set-point event)
|
|
|
|
(popup-menu (eudc-bob-menu) event))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
2009-09-01 03:16:07 +00:00
|
|
|
;; If the first arguments can be nil here, then these 3 can be
|
|
|
|
;; defconsts once more.
|
2021-09-19 14:48:35 +00:00
|
|
|
(easy-menu-define eudc-bob-generic-menu eudc-bob-generic-keymap
|
|
|
|
"EUDC Binary Object Menu."
|
2018-08-15 17:16:25 +00:00
|
|
|
eudc-bob-generic-menu)
|
2021-09-19 14:48:35 +00:00
|
|
|
(easy-menu-define eudc-bob-image-menu eudc-bob-image-keymap
|
|
|
|
"EUDC Image Menu."
|
2018-08-15 17:16:25 +00:00
|
|
|
eudc-bob-image-menu)
|
2021-09-19 14:48:35 +00:00
|
|
|
(easy-menu-define eudc-bob-sound-menu eudc-bob-sound-keymap
|
|
|
|
"EUDC Sound Menu."
|
2018-08-15 17:16:25 +00:00
|
|
|
eudc-bob-sound-menu)
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
;;;###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))
|
|
|
|
|
2002-01-16 08:05:19 +00:00
|
|
|
;;;###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))
|
|
|
|
|
2000-01-12 20:50:20 +00:00
|
|
|
;;;###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."
|
2022-05-13 19:30:05 +00:00
|
|
|
(eudc-bob-display-jpeg data (display-graphic-p)))
|
2000-01-12 20:50:20 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun eudc-display-jpeg-as-button (data)
|
|
|
|
"Display a button for the JPEG DATA."
|
|
|
|
(eudc-bob-display-jpeg data nil))
|
2002-01-06 15:08:06 +00:00
|
|
|
|
2022-05-13 19:30:05 +00:00
|
|
|
(define-obsolete-function-alias 'eudc-bob-can-display-inline-images #'display-graphic-p "29.1")
|
|
|
|
|
2000-01-12 20:50:20 +00:00
|
|
|
;;; eudc-bob.el ends here
|