1 ;;; dragdrop.el --- window system-independent Drag'n'Drop support.
3 ;; Copyright (C) 1998 Oliver Graf <ograf@fga.de>
5 ;; Maintainer: SXEmacs Development Team, Oliver Graf <ograf@fga.de>
6 ;; Keywords: mouse, gui, dumped
8 ;; This file is part of SXEmacs.
10 ;; SXEmacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; SXEmacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Synched up with: Not in FSF.
27 ;; This file is dumped with SXEmacs (when drag'n'drop support is compiled in).
31 ;; we need mouse-set-point
35 ;; I think this is a better name for the custom group
36 ;; looks better in the menu and the group display as dragdrop
37 ;; Anyway: is dragdrop- a good prefix for all this?
38 ;; What if someone trys drop<TAB> in the minibuffer?
39 (defgroup drag-n-drop nil
40 "*{EXPERIMENTAL} Window system-independent drag'n'drop support."
43 (defcustom dragdrop-drop-at-point nil
44 "*{EXPERIMENTAL} If non-nil, drop text at the cursor location.
45 Otherwise, the cursor will be moved to the location of the pointer drop before
50 (defcustom dragdrop-autoload-tm-view nil
51 "*{EXPERIMENTAL} If non-nil, autoload tm-view to decode MIME data.
52 Otherwise, the buffer is only decoded if tm-view is already available."
56 ;; the widget for editing the drop-functions
57 (define-widget 'dragdrop-function-widget 'list
58 "*{EXPERIMENTAL} Widget for editing drop dispatch functions."
59 :args `((choice :tag "Function"
60 (function-item experimental-dragdrop-drop-url-default)
61 (function-item experimental-dragdrop-drop-mime-default)
62 (function-item experimental-dragdrop-drop-log-function)
63 (function :tag "Other" nil))
64 (choice :tag "Button" :value t
65 (choice-item :tag "Ignore" t)
66 (choice-item 0) (choice-item 1) (choice-item 2)
67 (choice-item 3) (choice-item 4) (choice-item 5)
68 (choice-item 6) (choice-item 7))
69 (radio-button-choice :tag "Modifiers"
70 (const :tag "Ignore Modifier Keys" t)
72 :format "Modifier Keys:\n%v"
80 (repeat :inline t :value nil :tag "Extra Function Arguments"
81 (sexp :tag "Arg" :value nil)))
84 (defcustom experimental-dragdrop-drop-functions '((experimental-dragdrop-drop-url-default t t)
85 (experimental-dragdrop-drop-mime-default t t))
86 "*{EXPERIMENTAL} This is the standart drop function search list.
87 Each element is a list of a function, a button selector, a modifier
88 selector and optional argumets to the function call.
89 The function must accept at least two arguments: first is the event
90 of the drop, second the object data, followed by any of the optional
91 arguments provided in this list.
92 The functions are called in order, until one returns t."
94 :type '(repeat dragdrop-function-widget))
96 (defgroup dnd-debug nil
97 "*{EXPERIMENTAL} Drag'n'Drop debugging options."
100 (defcustom dragdrop-drop-log nil
101 "*{EXPERIMENTAL} If non-nil, every drop is logged.
102 The name of the buffer is set in the custom 'dragdrop-drop-log-name"
106 (defcustom dragdrop-drop-log-name "*drop log buffer*"
107 "*{EXPERIMENTAL} The name of the buffer used to log drops.
108 Set dragdrop-drop-log to non-nil to enable this feature."
112 (defvar dragdrop-drop-log-buffer nil
113 "*{EXPERIMENTAL} Buffer to log drops in debug mode.")
118 (defun dragdrop-drop-dispatch (object)
119 "*{EXPERIMENTAL} This function identifies DROP type misc-user-events.
120 It calls functions which will handle the drag."
121 (let ((event current-mouse-event))
122 (and dragdrop-drop-log
123 (experimental-dragdrop-drop-log-function event object))
124 (dragdrop-drop-find-functions event object)))
126 (defun dragdrop-drop-find-functions (event object)
127 "Finds valid drop-handle functions and executes them to dispose the drop.
128 It does this by looking for extent-properties called
129 'experimental-dragdrop-drop-functions and for variables named like this."
130 (catch 'dragdrop-drop-is-done
131 (and (event-over-text-area-p event)
132 ;; let's search the extents
133 (catch 'dragdrop-extents-done
134 (let ((window (event-window event))
135 (pos (event-point event))
136 (cpos (event-closest-point event))
138 (or window (throw 'dragdrop-extents-done nil))
139 (or pos (setq pos cpos))
140 (select-window window)
141 (setq buffer (window-buffer))
142 (let ((ext (extent-at pos buffer 'experimental-dragdrop-drop-functions)))
143 (while (not (eq ext nil))
144 (dragdrop-drop-do-functions
145 (extent-property ext 'experimental-dragdrop-drop-functions)
148 (setq ext (extent-at pos buffer
149 'experimental-dragdrop-drop-functions
151 ;; now look into the variable experimental-dragdrop-drop-functions
152 (dragdrop-drop-do-functions experimental-dragdrop-drop-functions event object)))
154 (defun dragdrop-compare-mods (first-mods second-mods)
155 "Returns t if both first-mods and second-mods contain the same elements.
156 Order is not important."
157 (let ((moda (copy-sequence first-mods))
158 (modb (copy-sequence second-mods)))
159 (while (and (not (eq moda ()))
161 (setq modb (delete (car moda) modb))
162 (setq moda (delete (car moda) moda)))
166 (defun dragdrop-drop-do-functions (drop-funs event object)
167 "Calls all functions in drop-funs with object until one returns t.
168 Returns t if one of drop-funs returns t. Otherwise returns nil."
170 (button (event-button event))
171 (mods (event-modifiers event)))
172 (while (not (eq drop-funs ()))
173 (setq flist (car drop-funs))
174 (and (or (eq (cadr flist) t)
175 (= (cadr flist) button))
176 (or (eq (caddr flist) t)
177 (dragdrop-compare-mods (caddr flist) mods))
178 (apply (car flist) `(,event ,object ,@(cdddr flist)))
179 ;; (funcall (car flist) event object)
180 (throw 'dragdrop-drop-is-done t))
181 (setq drop-funs (cdr drop-funs))))
184 (defun experimental-dragdrop-drop-log-function (event object &optional message buffer)
185 "*{EXPERIMENTAL} Logs any drops into a buffer.
186 If buffer is nil, it inserts the data into a buffer called after
187 dragdrop-drop-log-name.
188 If dragdrop-drop-log is non-nil, this is done automatically for each drop.
189 The function always returns nil."
191 (cond ((buffer-live-p buffer)
194 (set-buffer (get-buffer-create buffer)))
195 ((buffer-live-p dragdrop-drop-log-buffer)
196 (set-buffer dragdrop-drop-log-buffer))
198 (setq dragdrop-drop-log-buffer (get-buffer-create dragdrop-drop-log-name))
199 (set-buffer dragdrop-drop-log-buffer)))
200 (insert (format "* %s: %s\n"
201 (current-time-string)
202 (if message message "received a drop")))
203 (insert (format " at %d,%d (%d,%d) with button %d and mods %s\n"
206 (event-x-pixel event)
207 (event-y-pixel event)
209 (event-modifiers event)))
210 (insert (format " data is of type %s (%d %s)\n"
211 (cond ((eq (car object) 'dragdrop-URL) "URL")
212 ((eq (car object) 'dragdrop-MIME) "MIME")
214 (length (cdr object))
215 (if (= (length (cdr object)) 1) "element" "elements")))
218 (while (not (eq data ()))
219 (insert (format " Element %d: %S\n"
222 (setq data (cdr data))))
223 (insert "----------\n"))
226 (defun experimental-dragdrop-drop-url-default (event object)
227 "*{EXPERIMENTAL} Default handler for dropped URL data.
228 Finds files and URLs. Returns nil if object does not contain URL data."
229 (cond ((eq (car object) 'dragdrop-URL)
230 (let ((data (cdr object))
231 (frame (event-channel event))
233 (window (event-window event)))
234 (setq pop-up-windows nil)
235 (while (not (eq data ()))
236 (cond ((dragdrop-is-some-url "file" (car data))
237 ;; if it is some file, pop it to a buffer
239 (select-window window)))
240 (switch-to-buffer (find-file-noselect
241 (substring (car data) 5))))
242 ;; to-do: open ftp URLs with efs...
244 ;; some other URL, try to fire up some browser for it
245 (if-fboundp 'browse-url
246 (browse-url (car data))
247 (display-message 'error
248 "Can't show URL, no browser selected"))))
250 (setq data (cdr data)))
251 (make-frame-visible frame)
252 (setq pop-up-windows x)
256 (defun experimental-dragdrop-drop-mime-default (event object)
257 "*{EXPERIMENTAL} Default handler for dropped MIME data.
258 Inserts text into buffer, creates MIME buffers for other types.
259 Returns nil if object does not contain MIME data."
260 (cond ((eq (car object) 'dragdrop-MIME)
261 (let ((ldata (cdr object))
262 (frame (event-channel event))
265 ;; how should this be handled???
266 ;; insert drops of text/* into buffer
267 ;; create new buffer if pointer is outside buffer...
268 ;; but there are many other ways...
270 ;; first thing: check if it's only text/plain and if the
271 ;; drop happened inside some buffer. if yes insert it into
272 ;; this buffer (hope it is not encoded in some MIME way)
274 ;; Remember: ("text/plain" "dosnotmatter" "somedata")
275 ;; drops are inserted at mouse-point, if inside a buffer
276 (while (not (eq ldata ()))
277 (setq data (car ldata))
278 (if (and (listp data)
281 (stringp (caar data))
282 (string= (caar data) "text/plain")
283 (event-over-text-area-p event))
284 (let ((window (event-window event)))
286 (select-window window))
287 (and (not dragdrop-drop-at-point)
288 (mouse-set-point event))
289 (insert (caddr data)))
290 (let ((buf (get-buffer-create "*MIME-Drop data*")))
292 (pop-to-buffer buf nil frame)
293 (or (featurep 'tm-view)
294 (and dragdrop-autoload-tm-view
296 (cond ((stringp data)
297 ;; this is some raw MIME stuff
298 ;; create some buffer and let tm do the job
300 ;; this is always the same buffer!!!
304 (and (featurep 'tm-view)
305 (declare-fboundp (mime/viewer-mode buf))))
308 ;; change the internal content-type representation to the
309 ;; way tm does it ("content/type" (key . value)*)
310 ;; but for now list will do the job
312 ;; this is always the same buffer!!!
315 (insert (caddr data))
316 (and (featurep 'tm-view)
317 ;; this list of (car data) should be done before
318 ;; enqueing the event
319 (declare-fboundp (mime/viewer-mode buf (car data) (cadr data)))))
321 (display-message 'error "Wrong drop data")))))
323 (setq ldata (cdr ldata)))
324 (make-frame-visible frame)
325 (setq pop-up-windows x))
329 (defun dragdrop-is-some-url (method url)
330 "Returns true if method equals the start of url.
331 If method does not end into ':' this is appended before the
333 (cond ((and (stringp url)
335 (> (length url) (length method)))
336 ;; is this ?: check efficient enough?
337 (if (not (string= (substring method -1) ":"))
338 (setq method (concat method ":")))
339 (string= method (substring url 0 (length method))))
345 (defun experimental-dragdrop-drag (event object)
346 "*{EXPERIMENTAL} The generic drag function.
347 Tries to do the best with object in the selected protocol.
348 Object must comply to the standart drag'n'drop object
350 (error "Not implemented"))
352 (defun experimental-dragdrop-drag-region (event begin end)
353 "*{EXPERIMENTAL} Drag a region.
354 This function uses special data types if the low-level
355 protocol requires it. It does so by calling
356 dragdrop-drag-pure-text."
357 (experimental-dragdrop-drag-pure-text event
358 (buffer-substring-no-properties begin end)))
360 (defun experimental-dragdrop-drag-pure-text (event text)
361 "*{EXPERIMENTAL} Drag text-only data.
362 Takes care of special low-level protocol data types.
363 Text must be a list of strings."
364 (error "Not implemented"))
366 (defun experimental-dragdrop-drag-pure-file (event file)
367 "*{EXPERIMENTAL} Drag filepath-only data.
368 Takes care of special low-level protocol data types.
369 file must be a list of strings."
370 (error "Not implemented"))
373 ;; The following ones come from frame.el but the better belong here
376 (defun cde-start-drag (event type data)
377 "Implement the CDE drag operation.
378 Calls the internal function cde-start-drag-internal to do the actual work."
381 ;; Avoid build-time doc string warning by calling the function
382 ;; in the following roundabout way:
383 (funcall (intern "cde-start-drag-internal")
385 (error "CDE functionality not compiled in.")))
387 (defun cde-start-drag-region (event begin end)
388 "Implement the CDE drag operation for a region.
389 Calls the internal function CDE-start-drag-internal to do the actual work.
390 This always does buffer transfers."
391 ;; Oliver Graf <ograf@fga.de>
394 (funcall (intern "cde-start-drag-internal")
395 event nil (list (buffer-substring-no-properties begin end)))
396 (error "CDE functionality not compiled in.")))
398 ;; the OffiX drag stuff will soon move also (perhaps mouse.el)
399 ;; if the drag event is done
400 (defun offix-start-drag (event data &optional type)
401 "Implement the OffiX drag operation.
402 Calls the internal function offix-start-drag-internal to do the actual work.
403 If type is not given, DndText is assumed."
404 ;; Oliver Graf <ograf@fga.de>
406 (if (featurep 'offix)
407 (funcall (intern "offix-start-drag-internal") event data type)
408 (error "OffiX functionality not compiled in.")))
410 (defun offix-start-drag-region (event begin end)
411 "Implement the OffiX drag operation for a region.
412 Calls the internal function offix-start-drag-internal to do the actual work.
413 This always assumes DndText as type."
414 ;; Oliver Graf <ograf@fga.de>
416 (if (featurep 'offix)
417 (funcall (intern "offix-start-drag-internal")
418 event (buffer-substring-no-properties begin end))
419 (error "OffiX functionality not compiled in.")))
421 (defun gtk-start-drag (event data &optional type)
424 (declare-fboundp (gtk-start-drag-internal event data type))
425 (error "GTK functionality not compiled in.")))
427 (defun gtk-start-drag-region (event begin end)
431 (gtk-start-drag-internal
432 event (buffer-substring-no-properties begin end) "text/plain"))
433 (error "GTK functionality not compiled in.")))
435 ;;; dragdrop.el ends here