All of SXEmacs' http URLs are now https. WooHoo!
[sxemacs] / lisp / dragdrop.el
1 ;;; dragdrop.el --- window system-independent Drag'n'Drop support.
2
3 ;; Copyright (C) 1998 Oliver Graf <ograf@fga.de>
4
5 ;; Maintainer: SXEmacs Development Team, Oliver Graf <ograf@fga.de>
6 ;; Keywords: mouse, gui, dumped
7
8 ;; This file is part of SXEmacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Synched up with: Not in FSF.
24
25 ;;; Commentary:
26
27 ;; This file is dumped with SXEmacs (when drag'n'drop support is compiled in).
28
29 ;;; Code:
30
31 ;; we need mouse-set-point
32 (require 'mouse)
33 (provide 'dragdrop)
34
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."
41   :group 'editing)
42
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
46 text is inserted."
47   :type 'boolean
48   :group 'drag-n-drop)
49
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."
53   :type 'boolean
54   :group 'drag-n-drop)
55
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)
71                                (checklist :greedy t
72                                           :format "Modifier Keys:\n%v"
73                                           :extra-offset 6
74                                           (const shift)
75                                           (const control)
76                                           (const meta)
77                                           (const alt)
78                                           (const hyper)
79                                           (const super)))
80           (repeat :inline t :value nil :tag "Extra Function Arguments"
81                   (sexp :tag "Arg" :value nil)))
82   :value '(nil t t))
83
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."
93   :group 'drag-n-drop
94   :type '(repeat dragdrop-function-widget))
95
96 (defgroup dnd-debug nil
97   "*{EXPERIMENTAL} Drag'n'Drop debugging options."
98   :group 'drag-n-drop)
99
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"
103   :group 'dnd-debug
104   :type 'boolean)
105
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."
109   :group 'dnd-debug
110   :type 'string)
111
112 (defvar dragdrop-drop-log-buffer nil
113   "*{EXPERIMENTAL} Buffer to log drops in debug mode.")
114
115 ;;
116 ;; Drop API
117 ;;
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)))
125
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))
137                  (buffer nil))
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)
146                   event
147                   object)
148                  (setq ext (extent-at pos buffer
149                                       'experimental-dragdrop-drop-functions
150                                       ext)))))))
151     ;; now look into the variable experimental-dragdrop-drop-functions
152     (dragdrop-drop-do-functions experimental-dragdrop-drop-functions event object)))
153
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 ()))
160                 (not (eq modb ())))
161       (setq modb (delete (car moda) modb))
162       (setq moda (delete (car moda) moda)))
163     (and (eq moda ())
164          (eq modb ()))))
165
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."
169   (let ((flist 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))))
182   nil)
183
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."
190   (save-excursion
191     (cond ((buffer-live-p buffer)
192            (set-buffer buffer))
193           ((stringp buffer)
194            (set-buffer (get-buffer-create buffer)))
195           ((buffer-live-p dragdrop-drop-log-buffer)
196            (set-buffer dragdrop-drop-log-buffer))
197           (t
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"
204                     (event-x event)
205                     (event-y event)
206                     (event-x-pixel event)
207                     (event-y-pixel event)
208                     (event-button 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")
213                    (t "UNKNOWN"))
214              (length (cdr object))
215              (if (= (length (cdr object)) 1) "element" "elements")))
216     (let ((i 1)
217           (data (cdr object)))
218       (while (not (eq data ()))
219         (insert (format "    Element %d: %S\n"
220                         i (car data)))
221         (setq i (1+ i))
222         (setq data (cdr data))))
223     (insert "----------\n"))
224   nil)
225
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))
232                (x pop-up-windows)
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
238                     (cond (window
239                            (select-window window)))
240                     (switch-to-buffer (find-file-noselect
241                                        (substring (car data) 5))))
242                    ;; to-do: open ftp URLs with efs...
243                    (t
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"))))
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
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))
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)
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)))
285                    (and window
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*")))
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                              (declare-fboundp (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
319                              (declare-fboundp (mime/viewer-mode buf (car data) (cadr data)))))
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 ;;
343 ;; Drag API
344 ;;
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
349 format."
350   (error "Not implemented"))
351
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)))
359
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"))
365
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"))
371
372 ;;
373 ;; The following ones come from frame.el but the better belong here
374 ;; until changed
375 ;;
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."
379   (interactive "_eXX")
380   (if (featurep 'cde)
381       ;; Avoid build-time doc string warning by calling the function
382       ;; in the following roundabout way:
383       (funcall (intern "cde-start-drag-internal")
384                event type data)
385     (error "CDE functionality not compiled in.")))
386
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>
392   (interactive "_er")
393   (if (featurep 'cde)
394       (funcall (intern "cde-start-drag-internal")
395                event nil (list (buffer-substring-no-properties begin end)))
396     (error "CDE functionality not compiled in.")))
397
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>
405   (interactive "esi")
406   (if (featurep 'offix)
407       (funcall (intern "offix-start-drag-internal") event data type)
408     (error "OffiX functionality not compiled in.")))
409
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>
415   (interactive "_er")
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.")))
420
421 ;;; dragdrop.el ends here