282
|
1 ;;; dragdrop.el --- window system-independent Drag'n'Drop support.
|
|
2
|
|
3 ;; Copyright (C) 1998 Oliver Graf <ograf@fga.de>
|
|
4
|
|
5 ;; Maintainer: XEmacs Development Team, Oliver Graf <ograf@fga.de>
|
|
6 ;; Keywords: drag, drop, dumped
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
|
25 ;;; Synched up with: Not in FSF.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
284
|
29 ;; This file is dumped with XEmacs (when drag'n'drop support is compiled in).
|
282
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33 ;; we need mouse-set-point
|
|
34 (require 'mouse)
|
284
|
35 (provide 'dragdrop)
|
282
|
36
|
|
37 ;; I think this is a better name for the custom group
|
|
38 ;; looks better in the menu and the group display as dragdrop
|
284
|
39 ;; Anyway: is dragdrop- a good prefix for all this?
|
|
40 ;; What if someone type drop<TAB> into the minibuffer?
|
282
|
41 (defgroup drag-n-drop nil
|
|
42 "Window system-independent drag'n'drop support."
|
|
43 :group 'editing)
|
|
44
|
|
45 (defcustom dragdrop-drop-at-point nil
|
|
46 "*If non-nil, the drop handler functions will drop text at the cursor location.
|
|
47 Otherwise, the cursor will be moved to the location of the pointer drop before
|
|
48 text is inserted."
|
|
49 :type 'boolean
|
|
50 :group 'drag-n-drop)
|
|
51
|
|
52 (defcustom dragdrop-autoload-tm-view nil
|
|
53 "*If non-nil, autoload tm-view if a MIME buffer needs to be decoded.
|
|
54 Otherwise, the buffer is only decoded if tm-view is already avaiable."
|
|
55 :type 'boolean
|
|
56 :group 'drag-n-drop)
|
|
57
|
284
|
58 ;; the widget for editing the drop-functions
|
|
59 (define-widget 'dragdrop-function-widget 'list
|
|
60 "Widget for editing drop dispatch functions."
|
|
61 :args `((choice :tag "Function"
|
|
62 (function-item dragdrop-drop-url-default)
|
|
63 (function-item dragdrop-drop-mime-default)
|
|
64 (function-item dragdrop-drop-log-function)
|
|
65 (function :tag "Other" nil))
|
|
66 (choice :tag "Button" :value t
|
|
67 (choice-item :tag "Ignore" t)
|
|
68 (choice-item 0) (choice-item 1) (choice-item 2)
|
|
69 (choice-item 3) (choice-item 4) (choice-item 5)
|
|
70 (choice-item 6) (choice-item 7))
|
|
71 (radio-button-choice :tag "Modifiers"
|
|
72 (const :tag "Ignore Modifier Keys" t)
|
|
73 (checklist :greedy t
|
|
74 :format "Modifier Keys:\n%v"
|
|
75 :extra-offset 6
|
|
76 (const shift)
|
|
77 (const control)
|
|
78 (const meta)
|
|
79 (const alt)
|
|
80 (const hyper)
|
|
81 (const super)))
|
|
82 (repeat :inline t :value nil :tag "Extra Function Arguments"
|
|
83 (sexp :tag "Arg" :value nil)))
|
|
84 :value '(nil t t))
|
|
85
|
|
86 ;; button and widget selectors are still "shaky":
|
|
87 ;; button may be a number or t (or nil?), t means "Ignore"
|
|
88 ;; mods may be t or nil or a list of mod-syms, t means "Ignore"
|
|
89 ;; but this seems to be a porblem for the widget, well perhaps I find
|
|
90 ;; a solution...
|
|
91 (defcustom dragdrop-drop-functions '((dragdrop-drop-url-default t t)
|
|
92 (dragdrop-drop-mime-default t t))
|
282
|
93 "This is the standart drop function search list.
|
284
|
94 Each element is a list of a function, a button selector, a modifier
|
|
95 selector and optional argumets to the function call.
|
|
96 The function must accept at least two arguments: first is the event
|
|
97 of the drop, second the object data, followed by any of the optional
|
|
98 arguments provided in this list.
|
|
99 The functions are called in order, until one returns t."
|
282
|
100 :group 'drag-n-drop
|
284
|
101 :type '(repeat dragdrop-function-widget))
|
|
102
|
|
103 (defgroup dnd-debug nil
|
|
104 "Drag'n'Drop debugging options."
|
|
105 :group 'drag-n-drop)
|
|
106
|
|
107 (defcustom dragdrop-drop-log nil
|
|
108 "If non-nil, every drop is logged.
|
|
109 The name of the buffer is set in the custom 'dragdrop-drop-log-name"
|
|
110 :group 'dnd-debug
|
|
111 :type 'boolean)
|
|
112
|
|
113 (defcustom dragdrop-drop-log-name "*drop log buffer*"
|
|
114 "The name of the buffer used to log drops.
|
|
115 Set dragdrop-drop-log to non-nil to enable this feature."
|
|
116 :group 'dnd-debug
|
|
117 :type 'string)
|
|
118
|
|
119 (defvar dragdrop-drop-log-buffer nil
|
|
120 "Buffer to log drops in debug mode.")
|
282
|
121
|
|
122 (defun dragdrop-drop-dispatch (object)
|
|
123 "This function identifies DROP type misc-user-events.
|
284
|
124 It calls functions which will handle the drag."
|
|
125 (let ((event current-mouse-event))
|
|
126 (and dragdrop-drop-log
|
|
127 (dragdrop-drop-log-function event object))
|
|
128 (dragdrop-drop-find-functions event object)))
|
|
129
|
|
130 (defun dragdrop-drop-find-functions (event object)
|
|
131 "Finds valid drop-handle functions and executes them to dispose the drop.
|
|
132 It does this by looking for extent-properties called 'dragdrop-drop-functions
|
|
133 and for variables named like this."
|
282
|
134 (catch 'dragdrop-drop-is-done
|
284
|
135 (and (event-over-text-area-p event)
|
282
|
136 ;; let's search the extents
|
|
137 (catch 'dragdrop-extents-done
|
284
|
138 (let ((window (event-window event))
|
|
139 (pos (event-point event))
|
|
140 (cpos (event-closest-point event))
|
282
|
141 (buffer nil))
|
|
142 (or window (throw 'dragdrop-extents-done nil))
|
|
143 (or pos (setq pos cpos))
|
|
144 (select-window window)
|
|
145 (setq buffer (window-buffer))
|
|
146 (let ((ext (extent-at pos buffer 'dragdrop-drop-functions)))
|
|
147 (while (not (eq ext nil))
|
|
148 (dragdrop-drop-do-functions
|
|
149 (extent-property ext 'dragdrop-drop-functions)
|
284
|
150 event
|
282
|
151 object)
|
|
152 (setq ext (extent-at pos buffer 'dragdrop-drop-functions ext)))))))
|
|
153 ;; now look into the variable dragdrop-drop-functions
|
284
|
154 (dragdrop-drop-do-functions dragdrop-drop-functions event object)))
|
282
|
155
|
284
|
156 (defun dragdrop-compare-mods (first-mods second-mods)
|
|
157 "Returns t if both first-mods and second-mods contain the same elements.
|
|
158 Order is not important."
|
|
159 (let ((moda (copy-sequence first-mods))
|
|
160 (modb (copy-sequence second-mods)))
|
|
161 (while (and (not (eq moda ()))
|
|
162 (not (eq modb ())))
|
|
163 (setq modb (delete (car moda) modb))
|
|
164 (setq moda (delete (car moda) moda)))
|
|
165 (and (eq moda ())
|
|
166 (eq modb ()))))
|
|
167
|
|
168 (defun dragdrop-drop-do-functions (drop-funs event object)
|
282
|
169 "Calls all functions in drop-funs with object until one returns t.
|
|
170 Returns t if one of drop-funs returns t. Otherwise returns nil."
|
284
|
171 (let ((flist nil)
|
|
172 (button (event-button event))
|
|
173 (mods (event-modifiers event)))
|
|
174 (while (not (eq drop-funs ()))
|
|
175 (setq flist (car drop-funs))
|
|
176 (and (or (eq (cadr flist) t)
|
|
177 (= (cadr flist) button))
|
|
178 (or (eq (caddr flist) t)
|
|
179 (dragdrop-compare-mods (caddr flist) modifiers))
|
|
180 (apply (car flist) `(,event ,object ,@(cdddr flist)))
|
|
181 ;; (funcall (car flist) event object)
|
|
182 (throw 'dragdrop-drop-is-done t))
|
|
183 (setq drop-funs (cdr drop-funs))))
|
282
|
184 nil)
|
|
185
|
284
|
186 (defun dragdrop-drop-log-function (event object &optional message buffer)
|
|
187 "Logs any drops into a buffer.
|
|
188 If buffer is nil, it inserts the data into a buffer called after
|
|
189 dragdrop-drop-log-name.
|
|
190 If dragdrop-drop-log is non-nil, this is done automatically for each drop.
|
|
191 The function always returns nil."
|
|
192 (save-excursion
|
|
193 (cond ((buffer-live-p buffer)
|
|
194 (set-buffer buffer))
|
|
195 ((stringp buffer)
|
|
196 (set-buffer (get-buffer-create buffer)))
|
|
197 ((buffer-live-p dragdrop-drop-log-buffer)
|
|
198 (set-buffer dragdrop-drop-log-buffer))
|
|
199 (t
|
|
200 (setq dragdrop-drop-log-buffer (get-buffer-create dragdrop-drop-log-name))
|
|
201 (set-buffer dragdrop-drop-log-buffer)))
|
|
202 (insert (format "* %s: %s\n"
|
|
203 (current-time-string)
|
|
204 (if message message "received a drop")))
|
|
205 (insert (format " at %d,%d (%d,%d) with button %d and mods %s\n"
|
|
206 (event-x event)
|
|
207 (event-y event)
|
|
208 (event-x-pixel event)
|
|
209 (event-y-pixel event)
|
|
210 (event-button event)
|
|
211 (event-modifiers event)))
|
|
212 (insert (format " data is of type %s (%d %s)\n"
|
|
213 (cond ((eq (car object) 'dragdrop-URL) "URL")
|
|
214 ((eq (car object) 'dragdrop-MIME) "MIME")
|
|
215 (t "UNKNOWN"))
|
|
216 (length (cdr object))
|
|
217 (if (= (length (cdr object)) 1) "element" "elements")))
|
|
218 (let ((i 1)
|
|
219 (data (cdr object)))
|
|
220 (while (not (eq data ()))
|
|
221 (insert (format " Element %d: %S\n"
|
|
222 i (car data)))
|
|
223 (setq i (1+ i))
|
|
224 (setq data (cdr data))))
|
|
225 (insert "----------\n"))
|
|
226 nil)
|
|
227
|
|
228 (defun dragdrop-drop-url-default (event object)
|
282
|
229 "Default handler for dropped URL data.
|
|
230 Finds files and URLs. Returns nil if object does not contain URL data."
|
|
231 (cond ((eq (car object) 'dragdrop-URL)
|
|
232 (let ((data (cdr object))
|
284
|
233 (frame (event-channel event))
|
282
|
234 (x pop-up-windows))
|
|
235 (setq pop-up-windows nil)
|
|
236 (while (not (eq data ()))
|
|
237 (cond ((dragdrop-is-some-url "file" (car data))
|
|
238 ;; if it is some file, pop it to a buffer
|
|
239 (pop-to-buffer (find-file-noselect
|
|
240 (substring (car data) 5))
|
|
241 nil frame))
|
|
242 ;; to-do: open ftp URLs with efs...
|
|
243 (t
|
|
244 ;; some other URL, try to fire up some browser for it
|
|
245 (if (boundp 'browse-url-browser-function)
|
|
246 (funcall browse-url-browser-function (car data))
|
|
247 (display-message 'error
|
|
248 "Can't show URL, no browser selected"))))
|
|
249 (undo-boundary)
|
|
250 (setq data (cdr data)))
|
|
251 (make-frame-visible frame)
|
|
252 (setq pop-up-windows x)
|
|
253 t))
|
|
254 (t nil)))
|
|
255
|
284
|
256 (defun dragdrop-drop-mime-default (event object)
|
282
|
257 "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))
|
284
|
262 (frame (event-channel event))
|
282
|
263 (x pop-up-windows)
|
|
264 (data nil))
|
|
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...
|
|
269 ;;
|
|
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)
|
|
273 ;;
|
|
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)
|
|
279 (= (length data) 3)
|
284
|
280 (listp (car data))
|
|
281 (stringp (caar data))
|
|
282 (string= (caar data) "text/plain")
|
|
283 (event-over-text-area-p event))
|
|
284 (let ((window (event-window event)))
|
282
|
285 (and window
|
|
286 (select-window window))
|
|
287 (and (not dragdrop-drop-at-point)
|
284
|
288 (mouse-set-point event))
|
282
|
289 (insert (caddr data)))
|
|
290 (let ((buf (get-buffer-create "*MIME-Drop data*")))
|
|
291 (set-buffer buf)
|
|
292 (pop-to-buffer buf nil frame)
|
|
293 (or (featurep 'tm-view)
|
|
294 (and dragdrop-autoload-tm-view
|
|
295 (require 'tm-view)))
|
|
296 (cond ((stringp data)
|
|
297 ;; this is some raw MIME stuff
|
|
298 ;; create some buffer and let tm do the job
|
|
299 ;;
|
|
300 ;; this is always the same buffer!!!
|
|
301 ;; change?
|
|
302 (erase-buffer)
|
|
303 (insert data)
|
|
304 (and (featurep 'tm-view)
|
|
305 (mime/viewer-mode buf)))
|
|
306 ((and (listp data)
|
|
307 (= (length data) 3))
|
|
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
|
|
311 ;;
|
|
312 ;; this is always the same buffer!!!
|
|
313 ;; change?
|
|
314 (erase-buffer)
|
|
315 (insert (caddr data))
|
|
316 (and (featurep 'tm-view)
|
|
317 ;; this list of (car data) should be done before
|
|
318 ;; enqueing the event
|
284
|
319 (mime/viewer-mode buf (car data) (cadr data))))
|
282
|
320 (t
|
|
321 (display-message 'error "Wrong drop data")))))
|
|
322 (undo-boundary)
|
|
323 (setq ldata (cdr ldata)))
|
|
324 (make-frame-visible frame)
|
|
325 (setq pop-up-windows x))
|
|
326 t)
|
|
327 (t nil)))
|
|
328
|
|
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
|
|
332 compare."
|
|
333 (cond ((and (stringp url)
|
|
334 (stringp method)
|
|
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))))
|
|
340 (t nil)))
|
|
341
|
|
342 ;;; dragdrop.el ends here
|