177
|
1 ;;; internal-drag-and-drop.el --- Internal drag and drop interface
|
|
2
|
|
3 ;; Copyright (C) 1996, 1997 Heiko Muenkel
|
|
4
|
|
5 ;; Author: Heiko Muenkel <muenkel@tnt.uni-hannover.de>
|
|
6 ;; Keywords: mouse
|
|
7
|
|
8 ;; $Id: internal-drag-and-drop.el,v 1.5 1997/07/26 22:09:46 steve Exp $
|
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your
|
|
15 ;; option) any later version.
|
|
16
|
|
17 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with XEmacs; See the file COPYING. if not, write to the Free
|
|
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
25 ;; 02111-1307, USA.
|
|
26
|
|
27 ;;; Synched up with: Not part of Emacs.
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
|
31 ;; Description:
|
|
32
|
|
33 ;; This package provides functions to define and call internal
|
|
34 ;; drag and drop actions in the emacs. One could start such an
|
|
35 ;; action by clicking with the mouse in the source buffer and
|
|
36 ;; then in the destination buffer. The action could depend on
|
|
37 ;; the points where you've clicked with the mouse, on the state
|
|
38 ;; of the region, the point, the mark and any other properties
|
|
39 ;; of the source and the destination buffers. The actions are
|
|
40 ;; defined by the variable `idd-actions', which is a buffer local
|
|
41 ;; variable. The following is an example for the hm--html-mode:
|
|
42 ;; (defvar hm--html-idd-actions
|
|
43 ;; '((nil (((idd-if-major-mode-p . dired-mode)
|
|
44 ;; (idd-if-dired-file-on-line-p
|
|
45 ;; . ".*\\.\\(gif\\)\\|\\(jpq\\)"))
|
|
46 ;; hm--html-idd-add-include-image-from-dired-line)
|
|
47 ;; (((idd-if-major-mode-p . dired-mode)
|
|
48 ;; (idd-if-dired-no-file-on-line-p . nil))
|
|
49 ;; hm--html-idd-add-file-link-to-file-on-dired-line)
|
|
50 ;; (((idd-if-major-mode-p . dired-mode)
|
|
51 ;; (idd-if-dired-no-file-on-line-p . t))
|
|
52 ;; hm--html-idd-add-file-link-to-directory-of-buffer)
|
|
53 ;; (((idd-if-major-mode-p . w3-mode)
|
|
54 ;; (idd-if-url-at-point-p . t))
|
|
55 ;; hm--html-idd-add-html-link-from-w3-buffer-point)
|
|
56 ;; (((idd-if-major-mode-p . w3-mode))
|
|
57 ;; hm--html-idd-add-html-link-to-w3-buffer)
|
|
58 ;; (((idd-if-local-file-p . t))
|
|
59 ;; hm--html-idd-add-file-link-to-buffer)))
|
|
60 ;; Look at the variable `idd-actions' for further descriptions.
|
|
61
|
|
62
|
|
63 ;; Installation:
|
|
64
|
|
65 ;; Put this file in one of your load path directories.
|
|
66
|
|
67 ;; Put the following in your .emacs:
|
|
68 ;; (autoload 'idd-mouse-drag-and-drop "internal-drag-and-drop"
|
|
69 ;; "Performs a drag and drop action.
|
|
70 ;; At first you must click on the source and
|
|
71 ;; after that on the destination."
|
|
72 ;; t)
|
|
73
|
|
74 ;; Define actions in the variable `idd-actions'.
|
|
75
|
|
76 ;; The variable `idd-global-mouse-keys' defines the mouse keys,
|
|
77 ;; which are bound to the drag and drop command.
|
|
78
|
|
79 ;; The variable `idd-global-help-mouse-keys' defines the mouse keys,
|
|
80 ;; which are bound to the drag and drop help command.
|
|
81
|
|
82 ;; The variable `idd-drag-and-drop-mouse-binding-type' determines
|
|
83 ;; if you've to hold a mouse button down during moving the mouse
|
|
84 ;; from the source to the destination or not.
|
|
85
|
|
86 ;; Emacs 19 users should read carefully the whole comments of
|
|
87 ;; `idd-drag-and-drop-mouse-binding-type', `idd-global-mouse-keys'
|
|
88 ;; and `idd-global-help-mouse-keys', if they would like to change
|
|
89 ;; any of these variables or the mouse bindings!
|
|
90
|
|
91 ;;; Code:
|
2
|
92
|
98
|
93 (require 'adapt)
|
|
94 (require 'cl)
|
|
95
|
177
|
96 (defgroup idd-drag-and-drop nil
|
|
97 "This package provides functions to define and call internal
|
|
98 drag and drop actions in the emacs. One could start such an
|
|
99 action by clicking with the mouse in the source buffer and
|
|
100 then in the destination buffer. The action could depend on
|
|
101 the points where you've clicked with the mouse, on the state
|
|
102 of the region, the point, the mark and any other properties
|
|
103 of the source and the destination buffers. The actions are
|
|
104 defined by the variable `idd-actions', which is a buffer local
|
|
105 variable."
|
|
106 :group 'mouse)
|
|
107
|
|
108 (defcustom idd-drag-and-drop-mouse-binding-type 'click
|
153
|
109 "*The type of the drag and drop mouse binding.
|
|
110 The value maybe `click' or `press-button-during-move'.
|
|
111 A value of `click' means, that you've to click over the source, leave
|
|
112 the button and click it again over the destination.
|
|
113 A value of `press-button-during-move' means, that you've to press
|
|
114 the button down over the source and hold it until the mouse pointer
|
|
115 is over the destination.
|
|
116
|
|
117 The disadvantage of the `press-button-during-move' type compared with
|
|
118 the `click' type is, that you can't select a destination region and
|
|
119 therefore a drag and drop action depending on a selected region can't
|
|
120 be started with that type of mouse binding.
|
|
121
|
|
122 Note: In the Emacs 19 you'll have to change also the keybindings of
|
|
123 the drag and drop commands, if you change this variable. Look at the
|
|
124 variables `idd-global-mouse-keys' and `idd-global-help-mouse-keys' for
|
177
|
125 this."
|
|
126 :group 'idd-drag-and-drop
|
|
127 :type '(choice (const :tag "Click on source and destination"
|
|
128 :value click)
|
|
129 (const :tag "Press button during mouse move"
|
|
130 :value press-button-during-move)))
|
153
|
131
|
98
|
132 (defvar idd-global-mouse-keys (if (adapt-emacs19p)
|
153
|
133 (if (eq idd-drag-and-drop-mouse-binding-type
|
|
134 'click)
|
|
135 [(meta control mouse-1)]
|
|
136 [(meta control down-mouse-1)])
|
98
|
137 [(meta control button1)])
|
|
138 "The mouse keys for the command `idd-mouse-drag-and-drop'.
|
|
139 The command `idd-mouse-drag-and-drop' is bound during the loading
|
|
140 of the package internal-drag-and-drop to this keys in the global
|
|
141 key map.
|
|
142
|
|
143 Set it to nil, if you don't want to bind this function during loading.
|
|
144
|
|
145 If the command is already bound in the global keymap during loading,
|
153
|
146 then this key sequence will not be bind.
|
98
|
147
|
153
|
148 Note: In the Emacs 19 the mouse keys must contain the modifier
|
|
149 `down', if `idd-drag-and-drop-mouse-binding-type' is set to
|
|
150 `press-button-during-move' and must not contain the modifier, if it
|
|
151 is set to `click'. If you set `idd-drag-and-drop-mouse-binding-type'
|
|
152 before loading the package internal-drag-and-drop, the mouse will
|
|
153 be bind in the right way.")
|
|
154
|
|
155 (defvar idd-global-help-mouse-keys
|
|
156 (if (adapt-emacs19p)
|
|
157 (if (eq idd-drag-and-drop-mouse-binding-type 'click)
|
|
158 [(meta control mouse-3)]
|
|
159 [(meta control down-mouse-3)])
|
|
160 [(meta control button3)])
|
98
|
161 "The mouse keys for the command `idd-help-mouse-drag-and-drop'.
|
116
|
162 The command `idd-help-mouse-drag-and-drop' is bound during the loading
|
98
|
163 of the package internal-drag-and-drop to this keys in the global
|
|
164 key map.
|
|
165
|
|
166 Set it to nil, if you don't want to bind this function during loading.
|
|
167
|
|
168 If the command is already bound in the global keymap during loading,
|
153
|
169 then this key sequence will not be bind.
|
98
|
170
|
153
|
171 Note: In the Emacs 19 the mouse keys must contain the modifier
|
|
172 `down', if `idd-drag-and-drop-mouse-binding-type' is set to
|
|
173 `press-button-during-move' and must not contain the modifier, if it
|
|
174 is set to `click'. If you set `idd-drag-and-drop-mouse-binding-type'
|
|
175 before loading the package internal-drag-and-drop, the mouse will
|
|
176 be bind in the right way.")
|
98
|
177
|
177
|
178 (defcustom idd-actions '((((idd-if-region-active-p . nil))
|
|
179 (((idd-if-region-active-p . t))
|
|
180 idd-action-copy-region))
|
|
181
|
|
182 (((idd-if-region-active-p . t))
|
|
183 (((idd-if-region-active-p . t))
|
|
184 idd-action-copy-replace-region))
|
|
185
|
|
186 (((idd-if-region-active-p . nil)
|
|
187 (idd-if-modifiers-p . nil))
|
|
188 (((idd-if-region-active-p . t))
|
|
189 idd-action-move-region))
|
|
190
|
|
191 (((idd-if-region-active-p . t)
|
|
192 (idd-if-modifiers-p . nil))
|
|
193 (((idd-if-region-active-p . t))
|
|
194 idd-action-move-replace-region))
|
|
195 )
|
|
196 "*The list with actions, depending on the source and the destination.
|
2
|
197 The list looks like:
|
98
|
198 '((<destination-specification-1> (<source-specification-1> <action-1-1>)
|
|
199 (<source-specification-2> <action-1-2>)
|
|
200 :
|
2
|
201 )
|
98
|
202 (<destination-specification-2> (<source-specification-1> <action-2-1>)
|
|
203 (<source-specification-2> <action-2-2>)
|
|
204 :
|
2
|
205 )
|
|
206 :
|
|
207 )
|
|
208 The <source-specification> looks like the following:
|
|
209 '([(<specification-type> <value>)])
|
98
|
210 with <specification-type> :== idd-if-minor-mode-p | idd-if-buffer-name-p
|
|
211 | idd-if-region-active-p | idd-if-url-at-point-p
|
|
212 | idd-if-major-mode-p | idd-if-variable-non-nil-p
|
|
213 | idd-if-dired-file-on-line-p
|
|
214 | idd-if-dired-no-file-on-line-p
|
|
215 | idd-if-local-file-p | idd-if-buffer-name-p
|
|
216 | idd-if-modifiers-p | ...
|
2
|
217
|
98
|
218 The <specification-type> - functions must have two arguments, the first one
|
|
219 is the source or destination and the second is the <value>. It must return
|
|
220 nil, if the test wasn't successfull and a number (in general 1), which
|
|
221 specifies the weight of the test function. The weights of all single tests
|
|
222 are added to a summary weight and assigned to the action. The action
|
|
223 with the highest weight is called from the action handler. Look at
|
|
224 the definition of `idd-if-major-mode-p', `idd-if-minor-mode-p' and so on for
|
|
225 examples. Look at the function `idd-get-source-or-destination-alist', if
|
|
226 you wan't to know the structure of the 'source-or-destination' argument
|
|
227 of these functions.
|
|
228
|
|
229 The <destination-specification> looks like <source-specification>,
|
|
230 but in general it could be set to nil in mode specific idd-action
|
|
231 lists.
|
|
232
|
|
233 If <destination-specification-1> or <source-specification-1> is set to
|
|
234 nil, then every source or source matches. `idd-actions' is a
|
70
|
235 buffer local variable, which should be at least mode depended. So if
|
98
|
236 the <destination-specification-1> is set to nil it says, that the destination
|
70
|
237 buffer must only have a specific mode. But however, it's also possible
|
98
|
238 to define a general `idd-actions' list, where the destination mode is
|
|
239 specified by `idd-if-major-mode-p'.
|
22
|
240
|
98
|
241 <action> ist a function, which has two arguments, the first specifies the
|
|
242 source and the second the destination. Look at the function definition
|
|
243 of `idd-action-copy-region' and `idd-action-copy-replace-region'. They are
|
177
|
244 examples for such actions."
|
|
245 :group 'idd-drag-and-drop
|
|
246 :type 'list)
|
2
|
247
|
|
248 (make-variable-buffer-local 'idd-actions)
|
|
249
|
177
|
250 (defcustom idd-help-instead-of-action nil
|
98
|
251 "*If this variable is t, then a help buffer is displayed.
|
177
|
252 No action will be performed if this variable is t.
|
|
253
|
|
254 Note: You can also use the help mouse key instead of setting
|
|
255 this to t."
|
|
256 :group 'idd-drag-and-drop
|
|
257 :type 'boolean)
|
|
258
|
|
259 (defcustom idd-mouse-pointer-image "drop"
|
|
260 "*The name of the image used as mouse pointer during drag and drop.
|
|
261 The image must be in the directory `idd-data-directory'.
|
|
262 Run the command `idd-make-drag-and-drop-pointer-glyph' after changing
|
|
263 this variable."
|
|
264 :group 'idd-drag-and-drop
|
|
265 :type 'file)
|
|
266
|
|
267 (defcustom idd-data-directory (file-name-as-directory
|
|
268 (expand-file-name "idd" data-directory))
|
|
269 "Data directory for the file `idd-mouse-pointer-image'.
|
|
270 Run the command `idd-make-drag-and-drop-pointer-glyph' after changing
|
|
271 this variable."
|
|
272 :group 'idd-drag-and-drop
|
|
273 :type 'directory)
|
|
274
|
|
275 (defcustom idd-overwrite-mouse-pointers
|
|
276 (if (adapt-xemacsp)
|
|
277 '(text-pointer-glyph
|
|
278 nontext-pointer-glyph
|
|
279 selection-pointer-glyph)
|
|
280 nil)
|
|
281 "*A list with pointer glyph variables, which should be overwritten
|
|
282 by the idd-drag-and-drop-pointer-glyph. If it is nil, the pointer
|
|
283 wont be changed. Currently it must be nil in the Emacs."
|
|
284 :group 'idd-drag-and-drop
|
|
285 :type '(repeat lisp))
|
|
286
|
|
287 (defvar idd-drag-and-drop-pointer-glyph nil
|
|
288 ; (if idd-overwrite-mouse-pointers
|
|
289 ; (make-pointer-glyph
|
|
290 ; (vector 'autodetect :data idd-mouse-pointer-image))
|
|
291 ; nil)
|
|
292 "The shape of the mouse-pointer when internal drag and drop is active.")
|
|
293
|
|
294 (defvar idd-original-pointer-image-instances nil
|
|
295 "Internal variable. Alist with the saved images instances of the pointers.
|
|
296 This list is used to restore the old mouse pointers.")
|
98
|
297
|
|
298 (defvar idd-help-start-action-keymap nil
|
|
299 "Keymap used in an extent in the help buffer to start the action.")
|
|
300
|
|
301 (defvar idd-help-source nil
|
|
302 "Contains the source of an action. Used only in the help buffer.")
|
|
303
|
|
304 (defvar idd-help-destination nil
|
|
305 "Contains the destination of an action. Used only in the help buffer.")
|
|
306
|
|
307 (defvar idd-help-start-extent nil
|
|
308 "The start extent in the help buffer.")
|
|
309
|
2
|
310 (defun idd-compare-a-specification (source-or-destination
|
|
311 specification)
|
|
312 "Tests if SOURCE-OR-DESTINATION matches the SPECIFICATION.
|
|
313 It returns a value (1 in general) if both are matching or nil."
|
|
314 (eval (list (car specification)
|
|
315 'source-or-destination
|
|
316 '(cdr specification))))
|
|
317
|
177
|
318 (defun idd-get-old-pointer-image-instances (mouse-pointers)
|
|
319 "Returns an alist with the pointer variables and there image instances."
|
|
320 (cond ((not mouse-pointers) nil)
|
|
321 (t (cons (cons (car mouse-pointers)
|
|
322 (glyph-image-instance (eval (car mouse-pointers))))
|
|
323 (idd-get-old-pointer-image-instances (cdr mouse-pointers))))))
|
|
324
|
|
325 (defun idd-set-drag-and-drop-pointer-glyphs-1 (mouse-pointers
|
|
326 drag-and-drop-pointer-glyph)
|
|
327 "Internal function."
|
|
328 (cond ((not mouse-pointers))
|
|
329 (t (set-glyph-image (eval (car mouse-pointers))
|
|
330 (glyph-image-instance drag-and-drop-pointer-glyph))
|
|
331 (idd-set-drag-and-drop-pointer-glyphs-1 (cdr mouse-pointers)
|
|
332 drag-and-drop-pointer-glyph)
|
|
333 )))
|
|
334
|
|
335 (defun idd-set-drag-and-drop-pointer-glyphs ()
|
|
336 "Set the shape of some pointers to the drag and drop shape.
|
|
337 Only the pointers in the list `idd-overwrite-mouse-pointers' are
|
|
338 used."
|
|
339 (unless (or idd-original-pointer-image-instances
|
|
340 (not idd-overwrite-mouse-pointers))
|
|
341 (setq idd-original-pointer-image-instances
|
|
342 (idd-get-old-pointer-image-instances idd-overwrite-mouse-pointers))
|
|
343 (idd-set-drag-and-drop-pointer-glyphs-1 idd-overwrite-mouse-pointers
|
|
344 idd-drag-and-drop-pointer-glyph)))
|
|
345
|
|
346 (defun idd-restore-original-pointer-glyphs-1 (pointer-alist)
|
|
347 "Internal function."
|
|
348 (cond ((not pointer-alist))
|
|
349 (t (set-glyph-image (eval (car (car pointer-alist)))
|
|
350 (cdr (car pointer-alist)))
|
|
351 (idd-restore-original-pointer-glyphs-1 (cdr pointer-alist)))))
|
|
352
|
|
353 (defun idd-restore-original-pointer-glyphs ()
|
|
354 "Restores the original pointer shapes."
|
|
355 (interactive)
|
|
356 (when idd-overwrite-mouse-pointers
|
|
357 (idd-restore-original-pointer-glyphs-1
|
|
358 idd-original-pointer-image-instances)
|
|
359 (setq idd-original-pointer-image-instances nil)))
|
|
360
|
2
|
361 (defun idd-compare-specifications-1 (source-or-destination
|
98
|
362 specifications
|
|
363 value)
|
2
|
364 "Internal function of `idd-compare-specifications'.
|
|
365 VALUE is the value of the last matches."
|
|
366 (cond ((not specifications) value)
|
|
367 (t (let ((match (idd-compare-a-specification source-or-destination
|
|
368 (car specifications))))
|
|
369 (cond ((not match) 0)
|
|
370 (t (idd-compare-specifications-1 source-or-destination
|
|
371 (cdr specifications)
|
|
372 (+ value match))))))))
|
|
373
|
|
374 (defun idd-compare-specifications (source-or-destination
|
98
|
375 specifications)
|
2
|
376 "Determines how good SOURCE-OR-DESTINATION and SPECIFICATIONS are matching.
|
|
377 A return value of zero means, that they don't match. The higher the
|
|
378 return value the better is the matching."
|
|
379 (cond ((not specifications) 1)
|
|
380 (t (idd-compare-specifications-1 source-or-destination
|
98
|
381 specifications
|
|
382 0))))
|
2
|
383
|
98
|
384 (defun idd-get-action-depending-on-source (source
|
|
385 actions-depending-on-source
|
|
386 destination-value
|
|
387 value-action-pair)
|
2
|
388 "Internal function of `idd-get-action-depending-on-source-and-destination'."
|
98
|
389 (let ((source-value (idd-compare-specifications
|
|
390 source
|
|
391 (car (car actions-depending-on-source)))))
|
|
392 (cond ((not actions-depending-on-source) value-action-pair)
|
|
393 ((or (= source-value 0)
|
|
394 (<= (+ destination-value source-value) (car value-action-pair)))
|
|
395 (idd-get-action-depending-on-source
|
|
396 source
|
|
397 (cdr actions-depending-on-source)
|
|
398 destination-value
|
2
|
399 value-action-pair))
|
98
|
400 (t (idd-get-action-depending-on-source
|
|
401 source
|
|
402 (cdr actions-depending-on-source)
|
|
403 destination-value
|
|
404 (cons (+ destination-value source-value)
|
|
405 (second (car actions-depending-on-source))))))))
|
2
|
406
|
|
407 (defun idd-get-action-depending-on-source-and-destination (source
|
|
408 destination
|
|
409 actions
|
|
410 value-action-pair)
|
|
411 "Internal function of `idd-get-action'.
|
|
412 VALUE-ACTION-PAIR is a list like (<value> <action>).
|
|
413 It returns VALUE-ACTION-PAIR, if no other action is found, which has a
|
|
414 value higher than (car VALUE-ACTION-PAIR)."
|
98
|
415 (let ((destination-value
|
|
416 (idd-compare-specifications destination (car (car actions)))))
|
2
|
417 (cond ((not actions) value-action-pair)
|
98
|
418 ((= destination-value 0)
|
2
|
419 (idd-get-action-depending-on-source-and-destination
|
|
420 source
|
|
421 destination
|
|
422 (cdr actions)
|
|
423 value-action-pair))
|
|
424 (t (idd-get-action-depending-on-source-and-destination
|
|
425 source
|
|
426 destination
|
|
427 (cdr actions)
|
98
|
428 (idd-get-action-depending-on-source
|
|
429 source
|
2
|
430 (cdr (car actions))
|
98
|
431 destination-value
|
2
|
432 value-action-pair))))))
|
|
433
|
|
434 (defun idd-get-action (source destination actions)
|
|
435 "Returns the action, which depends on the SOURCE and the DESTINATION.
|
|
436 The list ACTIONS contains all possible actions. Look at the variable
|
|
437 `idd-actions' for a description of the format of this list."
|
|
438 (idd-get-action-depending-on-source-and-destination source
|
|
439 destination
|
|
440 actions
|
|
441 '(0 . nil)))
|
|
442
|
116
|
443 ;(autoload 'ange-ftp-ftp-path "ange-ftp"
|
|
444 ; "Parse PATH according to ange-ftp-path-format (which see).
|
|
445 ;Returns a list (HOST USER PATH), or nil if PATH does not match the format.")
|
98
|
446
|
153
|
447 (defun idd-get-buffer (source-or-destination)
|
|
448 "Returns the buffer of the SOURCE-OR-DESTINATION."
|
|
449 (cdr (assoc ':buffer source-or-destination)))
|
|
450
|
98
|
451 (defun idd-set-point (source-or-destination)
|
|
452 "Sets the point and buffer to SOURCE-OR-DESTINATION."
|
153
|
453 (set-buffer (idd-get-buffer source-or-destination))
|
98
|
454 (goto-char (cdr (assoc ':drag-or-drop-point source-or-destination))))
|
|
455
|
|
456 (defun idd-set-region (source-or-destination)
|
|
457 "Sets the point, mark and buffer to SOURCE-OR-DESTINATION.
|
|
458 The region is active after this function is called."
|
153
|
459 (set-buffer (idd-get-buffer source-or-destination))
|
98
|
460 (goto-char (car (cdr (assoc ':region-active source-or-destination))))
|
|
461 (set-mark (cdr (cdr (assoc ':region-active source-or-destination))))
|
153
|
462 (if (adapt-xemacsp)
|
|
463 (activate-region))
|
|
464 )
|
98
|
465
|
|
466
|
|
467 ;;; Specification type functions for the list `idd-actions'
|
|
468
|
|
469 (defun idd-if-region-active-p (source-or-destination value)
|
|
470 "Checks if the region in the SOURCE-OR-DESTINATION was active.
|
|
471 It returns 1, if the region was active and VALUE is t, or if
|
|
472 the region was not active and VALUE is nil. Otherwise it returns
|
|
473 nil."
|
|
474 (if (cdr (assoc ':region-active source-or-destination))
|
|
475 (if value 1 nil)
|
|
476 (if value nil 1)))
|
|
477
|
2
|
478 (defun idd-get-buffer-url (source-or-destination)
|
|
479 "Returns the URL of the buffer specified by SOURCE-OR-DESTINATION."
|
|
480 (save-excursion
|
|
481 (idd-set-point source-or-destination)
|
|
482 (url-view-url t)))
|
|
483
|
|
484 (defun idd-get-url-at-point (source-or-destination)
|
|
485 "Returns the URL at the point specified by SOURCE-OR-DESTINATION.
|
|
486 It returns nil, if there is no URL."
|
|
487 (save-excursion
|
|
488 (idd-set-point source-or-destination)
|
|
489 (w3-view-this-url t)))
|
|
490
|
98
|
491 (defun idd-if-url-at-point-p (source-or-destination value)
|
2
|
492 "Checks if there is an URL at the point of SOURCE-OR-DESTINATION.
|
|
493 If that is t and VALUE is t, or that is nil and VALUE is nil, then 1
|
|
494 is returned. Otherwise nil is returned."
|
|
495 (if value
|
|
496 (if (idd-get-url-at-point source-or-destination)
|
|
497 1
|
|
498 nil)
|
|
499 (if (idd-get-url-at-point source-or-destination)
|
|
500 nil
|
|
501 1)))
|
|
502
|
98
|
503 (defun idd-if-major-mode-p (source-or-destination mode)
|
2
|
504 "Checks, if the major mode of SOURCE-OR-DESTINATION is MODE.
|
|
505 It returns 1, if that is t and nil otherwise."
|
|
506 (save-excursion
|
153
|
507 (set-buffer (idd-get-buffer source-or-destination))
|
2
|
508 (if (eq major-mode mode)
|
|
509 1
|
|
510 nil)))
|
|
511
|
98
|
512 (defun idd-if-variable-non-nil-p (source-or-destination variable)
|
|
513 "Checks, if the variable named VARIABLE isn't t in SOURCE-OR-DESTINATION.
|
|
514 It returns 1, if this is t."
|
|
515 (save-excursion
|
153
|
516 (set-buffer (idd-get-buffer source-or-destination))
|
98
|
517 (if (eval variable)
|
|
518 1
|
|
519 nil)))
|
2
|
520
|
98
|
521 (defun idd-if-minor-mode-p (source-or-destination minor-mode-variable)
|
|
522 "Checks, if the variable MINOR-MODE-VARIABLE is t in SOURCE-OR-DESTINATION.
|
|
523 MINOR-MODE-VARIABLE is the name of the variable!."
|
116
|
524 (idd-if-variable-non-nil-p source-or-destination minor-mode-variable))
|
2
|
525
|
|
526 (defun idd-get-dired-filename-from-line (source-or-destination)
|
|
527 "Returns the filename form the line in a dired buffer.
|
|
528 The position and the buffer is specified by SOURCE-OR-DESTINATION."
|
|
529 (save-excursion
|
|
530 (idd-set-point source-or-destination)
|
|
531 (dired-get-filename nil t)))
|
|
532
|
98
|
533 (defun idd-if-dired-file-on-line-p (source-or-destination filename-regexp)
|
2
|
534 "Checks, if the filename on the line match FILENAME-REGEXP.
|
|
535 The function `dired-get-filename' is used, to get the filename from
|
|
536 the SOURCE-OR-DESTINATION. It returns 1, if it matchs or nil."
|
|
537 (let ((case-fold-search t))
|
|
538 (if (and (idd-get-dired-filename-from-line source-or-destination)
|
|
539 (string-match filename-regexp
|
|
540 (idd-get-dired-filename-from-line
|
|
541 source-or-destination)))
|
|
542 1
|
|
543 nil)))
|
|
544
|
98
|
545 (defun idd-if-dired-no-file-on-line-p (source-or-destination value)
|
2
|
546 "Checks, if a filename is in the dired buffer of SOURCE-OR-DESTINATION.
|
|
547 It returns 1, if a filename is on the line and if VALUE is t, or if
|
|
548 no filename is on the line and VALUE is nil, otherwise it returns
|
|
549 nil. For the test the function `dired-get-filename' is used."
|
|
550 (if (idd-get-dired-filename-from-line source-or-destination)
|
|
551 (if value nil 1)
|
|
552 (if value 1 nil)))
|
|
553
|
|
554 (defun idd-get-local-filename (source-or-destination)
|
|
555 "Returns the filename of a local file specified by SOURCE-OR-DESTINATION."
|
153
|
556 (buffer-file-name (idd-get-buffer source-or-destination)))
|
2
|
557
|
|
558 (defun idd-get-directory-of-buffer (source-or-destination)
|
|
559 "Returns the directory name assigned to the SOURCE-OR-DESTINATION buffer."
|
|
560 (save-excursion
|
|
561 (idd-set-point source-or-destination)
|
|
562 default-directory))
|
|
563
|
98
|
564 (defun idd-if-local-file-p (source-or-destination value)
|
2
|
565 "Checks, if SOURCE-OR-DESTINATION has a file on the local filesystem.
|
|
566 If that is t and VALUE is t, or that is nil and VALUE is nil, then 1
|
|
567 is returned. Otherwise nil is returned."
|
|
568 (let ((filename (idd-get-local-filename source-or-destination)))
|
|
569 (if (and filename
|
116
|
570 ; (not (ange-ftp-ftp-path filename)))
|
|
571 (not (file-remote-p filename)))
|
2
|
572 (if value 1 nil)
|
|
573 (if value nil 1))))
|
|
574
|
98
|
575 (defun idd-if-buffer-name-p (source-or-destination buffer-name)
|
|
576 "Checks, if SOURCE-OR-DESTINATION has a buffer called BUFFER-NAME.
|
|
577 It returns 1 if this is the case or nil otherwise."
|
|
578 (if (string= buffer-name
|
153
|
579 (buffer-name (idd-get-buffer source-or-destination)))
|
98
|
580 1
|
|
581 nil))
|
|
582
|
|
583 (defun idd-list-1-subset-of-list-2 (list-1 list-2)
|
|
584 "Returns t, if LIST-1 is a subset of LIST-2."
|
|
585 (cond ((not list-1))
|
116
|
586 ((member (car list-1) list-2)
|
98
|
587 (idd-list-1-subset-of-list-2 (cdr list-1) list-2))
|
|
588 (t nil)))
|
|
589
|
116
|
590 (defun idd-same-elements-p (list-1 list-2)
|
98
|
591 "Returns t, if both list have the same modifiers."
|
116
|
592 (and (= (length list-1) (length list-2))
|
|
593 (idd-list-1-subset-of-list-2 list-1 list-2)))
|
98
|
594
|
|
595 (defun idd-if-modifiers-p (source-or-destination modifiers)
|
|
596 "Checks, if the MODIFIERS hold during selecting the SOURCE-OR-DESTINATION.
|
|
597 Returns 1, if the list MODIFIERS contains the same modifiers,
|
|
598 or if any modyfiers are hold and MODIFIERS is t,
|
|
599 or if no modyfiers are hold and MODIFIERS is nil.
|
|
600 Otherwise nil is returned."
|
|
601 (let ((event-modifiers (event-modifiers
|
|
602 (cdr (assoc ':event source-or-destination)))))
|
|
603 (cond ((not modifiers)
|
|
604 (if event-modifiers nil 1))
|
|
605 ((listp modifiers)
|
116
|
606 (if (idd-same-elements-p modifiers event-modifiers)
|
98
|
607 1
|
|
608 nil))
|
|
609 (t (if event-modifiers 1 nil)))))
|
|
610
|
|
611 ;;; action functions
|
|
612
|
|
613 (defun idd-action-copy-region (source destination)
|
|
614 "Copy the region from DESTINATION to SOURCE."
|
|
615 (idd-set-region source)
|
|
616 (let ((region-contents (buffer-substring (point) (mark))))
|
|
617 (idd-set-point destination)
|
|
618 (insert region-contents)))
|
|
619
|
|
620 (defun idd-action-copy-replace-region (source destination)
|
|
621 "Copy the region from SOURCE and replace the DESTINATION region with it."
|
|
622 (idd-set-region source)
|
|
623 (let ((region-contents (buffer-substring (point) (mark))))
|
|
624 (idd-set-region destination)
|
|
625 (delete-region (point) (mark))
|
|
626 (insert region-contents)))
|
|
627
|
|
628 (defmacro* idd-with-source-and-destination (source
|
|
629 destination
|
|
630 &key
|
|
631 do-in-source
|
|
632 do-in-destination)
|
|
633 "Macro, usefull for the definition of action functions.
|
|
634 Look at the example `idd-action-move-region'."
|
|
635 `(progn
|
|
636 (if (idd-if-region-active-p ,source t)
|
|
637 (idd-set-region ,source)
|
|
638 (idd-set-point ,source))
|
|
639 ,(when do-in-source
|
|
640 (cons 'progn do-in-source))
|
|
641 (if (idd-if-region-active-p ,destination t)
|
|
642 (idd-set-region ,destination)
|
|
643 (idd-set-point ,destination))
|
|
644 ,(when do-in-destination
|
|
645 (cons 'progn do-in-destination))))
|
|
646
|
|
647 (defun idd-action-move-region (source destination)
|
|
648 "Move the region from SOURCE to DESTINATION."
|
|
649 (let ((region))
|
|
650 (idd-with-source-and-destination
|
|
651 source destination
|
|
652 :do-in-source ((setq region (buffer-substring (point) (mark)))
|
|
653 (delete-region (point) (mark)))
|
|
654 :do-in-destination ((insert region)))))
|
|
655
|
|
656
|
|
657 (defun idd-action-move-replace-region (source destination)
|
|
658 "Delete the region at SOURCE and overwrite the DESTINATION region with it."
|
|
659 (let ((region))
|
|
660 (idd-with-source-and-destination
|
|
661 source destination
|
|
662 :do-in-source ((setq region (buffer-substring (point) (mark)))
|
|
663 (delete-region (point) (mark)))
|
|
664 :do-in-destination ((delete-region (point) (mark))
|
|
665 (insert region)))))
|
|
666
|
|
667
|
|
668 ;;; Performing the drag and drop
|
|
669
|
|
670 (defun idd-display-help-about-action (action source destination)
|
|
671 "Display a help buffer with information about the action."
|
|
672 (if (> (car action) 0)
|
|
673 (if (symbol-function (cdr action))
|
|
674 (progn
|
|
675 (with-displaying-help-buffer
|
|
676 '(lambda ()
|
|
677 (set-buffer "*Help*")
|
|
678 (setq idd-help-source source)
|
|
679 (setq idd-help-destination destination)
|
|
680 (insert "Drag and drop action: `")
|
|
681 (let ((start (point)))
|
|
682 (insert (format "%s" (cdr action)))
|
|
683 (setq idd-help-start-extent (make-extent start (point)))
|
|
684 (set-extent-mouse-face idd-help-start-extent 'highlight)
|
|
685 (set-extent-face idd-help-start-extent 'bold)
|
153
|
686 (if (adapt-xemacsp)
|
|
687 (set-extent-keymap idd-help-start-extent
|
|
688 idd-help-start-action-keymap)
|
|
689 )
|
98
|
690 )
|
|
691 (insert "'\n")
|
|
692 (insert (format "Source buffer : `%s'\n"
|
|
693 (buffer-name (cdr (assoc ':buffer source)))))
|
|
694 (insert (format "Destination buffer : `%s'\n"
|
|
695 (buffer-name (cdr (assoc ':buffer destination))
|
|
696 )))
|
|
697 (insert "=================================================="
|
|
698 "====================\n")
|
|
699 (insert "Look at `idd-actions' in the "
|
|
700 "destination buffer for other actions!\n")
|
|
701 (insert (format "The documentation of `%s':\n\n"
|
|
702 (cdr action)))
|
|
703 (insert (documentation (cdr action)))))
|
|
704 )
|
|
705 (error "Error: Action %s isn't a valid function!" (cdr action)))
|
|
706 (message "No valid action defined for this source and this destination!")))
|
|
707
|
2
|
708 (defun idd-call-action (action source destination)
|
|
709 "Calls the drag and drop ACTION with its arguments SOURCE and DESTINATION."
|
|
710 (if (> (car action) 0)
|
|
711 (if (symbol-function (cdr action))
|
|
712 (eval (list (cdr action) 'source 'destination))
|
|
713 (error "Error: Action %s isn't a valid function!" (cdr action)))
|
|
714 (message "No valid action defined for this source and this destination!")))
|
|
715
|
98
|
716 (defun idd-start-help-mouse-drag-and-drop ()
|
|
717 "Starts help on `idd-start-mouse-drag-and-drop'."
|
|
718 (interactive)
|
|
719 (let ((idd-help-instead-of-action t))
|
|
720 (idd-start-mouse-drag-and-drop)))
|
|
721
|
|
722 (defun idd-start-mouse-drag-and-drop ()
|
|
723 "Starts a drag and drop command.
|
|
724 This command could be used to start a drag and drop command without a
|
|
725 button event. Therefore this should not be bind direct to a mouse button."
|
|
726 (interactive)
|
116
|
727 (let ((source-event)
|
98
|
728 (drag-and-drop-message "Drag&Drop: Click on the source!"))
|
|
729 (message drag-and-drop-message)
|
|
730 (setq source-event
|
|
731 (next-command-event nil drag-and-drop-message))
|
|
732 (if (button-press-event-p source-event)
|
153
|
733 (progn
|
|
734 (when (and (adapt-emacs19p)
|
|
735 (mouse-event-p source-event)
|
|
736 (eq idd-drag-and-drop-mouse-binding-type 'click))
|
|
737 (while (not (button-release-event-p (next-command-event)))))
|
|
738 (idd-mouse-drag-and-drop source-event))
|
98
|
739 (message "Wrong event! Exit drag and drop."))))
|
|
740
|
|
741 (defun idd-help-mouse-drag-and-drop (source-event)
|
|
742 "Displays help about the drag and drop action."
|
|
743 (interactive "@e")
|
|
744 (let ((idd-help-instead-of-action t))
|
|
745 (idd-mouse-drag-and-drop source-event)))
|
|
746
|
2
|
747 (defun idd-mouse-drag-and-drop (source-event)
|
|
748 "Performs a drag and drop action.
|
98
|
749 It calls the command `idd-mouse-drag-and-drop-click' or
|
|
750 `idd-mouse-drag-and-drop-press-button-during-move' depending on
|
|
751 the value of `idd-drag-and-drop-mouse-binding-type'."
|
2
|
752 (interactive "@e")
|
98
|
753 (if (eq idd-drag-and-drop-mouse-binding-type 'click)
|
|
754 (idd-mouse-drag-and-drop-click source-event)
|
|
755 (idd-mouse-drag-and-drop-press-button-during-move source-event)))
|
|
756
|
|
757 (defun idd-get-source-or-destination-alist (event)
|
|
758 "Returns an alist with the description of a source or destination point.
|
|
759 The EVENT must be the button event, which has selected the source or
|
|
760 destination of the drag and drop command.
|
|
761
|
|
762 The alist has the following structure:
|
|
763 '((:buffer . <buffer-of-the-event>)
|
|
764 (:drag-or-drop-point . <closest-point-to-the-event>)
|
|
765 (:region-active . <t-or-nil>)
|
|
766 (:event . EVENT))
|
|
767
|
|
768 Note: <closest-point-to-the-event> is (event-closest-point EVENT),
|
|
769 if the EVENT is a mouse event and if it isn't nil. Otherwise the
|
|
770 point is used."
|
|
771 ; (set-buffer (event-buffer event))
|
|
772 (list (cons ':buffer (event-buffer event))
|
|
773 (cons ':drag-or-drop-point (set-marker
|
|
774 (make-marker)
|
|
775 (if (mouse-event-p event)
|
|
776 (or (event-closest-point event)
|
|
777 (point))
|
|
778 (point))))
|
|
779 (cons ':region-active (if (region-active-p)
|
|
780 (cons (set-marker (make-marker) (point))
|
|
781 (set-marker (make-marker) (mark)))))
|
|
782 (cons ':event event))
|
|
783 )
|
|
784
|
|
785 (defun idd-mouse-drag-and-drop-press-button-during-move (source-event)
|
|
786 "Performs a drag and drop action.
|
|
787 At first you must press the button down over the source and then
|
|
788 move with the pressed button to the destination, where you must leave
|
|
789 the button up.
|
|
790 This must be bind to a mouse button. The SOURCE-EVENT must be a
|
|
791 button-press-event.
|
|
792
|
|
793 The disadvantage of this command compared with the command
|
|
794 `idd-mouse-drag-and-drop-click' is, that you can't select a
|
|
795 destination region."
|
|
796 (interactive "@e")
|
177
|
797 (idd-set-drag-and-drop-pointer-glyphs)
|
98
|
798 (let ((drag-and-drop-message
|
|
799 "Drag&Drop: Leave the button over the destination!")
|
|
800 (source (idd-get-source-or-destination-alist source-event))
|
2
|
801 (destination nil)
|
|
802 (destination-event))
|
98
|
803 (message drag-and-drop-message)
|
|
804 (setq destination-event
|
|
805 (next-command-event nil drag-and-drop-message))
|
|
806 (message "")
|
153
|
807 (cond ((or (button-release-event-p destination-event)
|
|
808 (and (adapt-emacs19p)
|
|
809 (button-drag-event-p destination-event)))
|
98
|
810 (setq destination (idd-get-source-or-destination-alist
|
|
811 destination-event))
|
|
812 (idd-set-point destination)
|
|
813 (if idd-help-instead-of-action
|
|
814 (idd-display-help-about-action (idd-get-action source
|
|
815 destination
|
|
816 idd-actions)
|
|
817 source
|
|
818 destination)
|
|
819 (idd-call-action (idd-get-action source destination idd-actions)
|
|
820 source
|
|
821 destination)))
|
177
|
822 (t (message "Wrong event! Exit drag and drop.") nil)))
|
|
823 (idd-restore-original-pointer-glyphs))
|
98
|
824
|
|
825 (defun idd-mouse-drag-and-drop-click (source-event)
|
|
826 "Performs a drag and drop action.
|
|
827 At first you must click on the source and after that on the destination.
|
|
828 This must be bind to a mouse button. The SOURCE-EVENT must be a
|
|
829 button-press-event."
|
|
830 (interactive "@e")
|
177
|
831 (idd-set-drag-and-drop-pointer-glyphs)
|
98
|
832 (let ((drag-and-drop-message "Drag&Drop: Click on the destination!")
|
|
833 (source (idd-get-source-or-destination-alist source-event))
|
|
834 (destination nil)
|
|
835 (destination-event))
|
|
836 (message drag-and-drop-message)
|
153
|
837 (when (and (adapt-xemacsp) (mouse-event-p source-event))
|
|
838 (dispatch-event (next-command-event)))
|
2
|
839 (setq destination-event
|
98
|
840 (next-command-event nil drag-and-drop-message))
|
|
841 (message "")
|
2
|
842 (cond ((button-press-event-p destination-event)
|
98
|
843 (mouse-track destination-event)
|
|
844 (setq destination (idd-get-source-or-destination-alist
|
|
845 destination-event))
|
|
846 (idd-set-point destination)
|
153
|
847 ; (when (adapt-emacs19p)
|
|
848 ; (while (not (button-release-event-p (next-command-event)))))
|
98
|
849 (if idd-help-instead-of-action
|
|
850 (idd-display-help-about-action (idd-get-action source
|
|
851 destination
|
|
852 idd-actions)
|
|
853 source
|
|
854 destination)
|
|
855 (idd-call-action (idd-get-action source destination idd-actions)
|
|
856 source
|
|
857 destination)))
|
116
|
858 ((and (adapt-emacs19p)
|
|
859 (button-click-event-p destination-event))
|
|
860 (setq destination (idd-get-source-or-destination-alist
|
|
861 destination-event))
|
|
862 (idd-set-point destination)
|
|
863 (if idd-help-instead-of-action
|
|
864 (idd-display-help-about-action (idd-get-action source
|
|
865 destination
|
|
866 idd-actions)
|
|
867 source
|
|
868 destination)
|
|
869 (idd-call-action (idd-get-action source destination idd-actions)
|
|
870 source
|
|
871 destination)))
|
153
|
872 (t (message "Wrong event! Exit drag and drop.") nil))
|
|
873
|
|
874 ;; Useful for debugging
|
|
875 ;; (setq idd-last-source source)
|
|
876 ;; (setq idd-last-destination destination)
|
|
877
|
177
|
878 )
|
|
879 (idd-restore-original-pointer-glyphs))
|
98
|
880
|
|
881 (defun idd-help-start-action (event)
|
|
882 "Used to start the action from the help buffer."
|
|
883 (interactive "@e")
|
|
884 (idd-set-point idd-help-destination)
|
|
885 (idd-call-action (idd-get-action idd-help-source
|
|
886 idd-help-destination
|
|
887 idd-actions)
|
|
888 idd-help-source
|
|
889 idd-help-destination)
|
|
890 (delete-extent idd-help-start-extent))
|
|
891
|
177
|
892 (if (adapt-xemacsp)
|
|
893 (progn
|
|
894
|
|
895 (defun idd-make-drag-and-drop-pointer-glyph ()
|
|
896 "Creates the drag and drop pointer glyph.
|
|
897 You've to rerun this, if you change either the variable
|
|
898 `idd-data-directory' or `idd-mouse-pointer-image'."
|
|
899 (interactive)
|
|
900 (let ((mouse-pointer-image (if (and idd-data-directory
|
|
901 idd-mouse-pointer-image)
|
|
902 (expand-file-name
|
|
903 (file-name-nondirectory
|
|
904 idd-mouse-pointer-image)
|
|
905 idd-data-directory)
|
|
906 idd-mouse-pointer-image)))
|
|
907 (if (and mouse-pointer-image
|
|
908 (file-exists-p mouse-pointer-image))
|
|
909 (setq idd-drag-and-drop-pointer-glyph
|
|
910 (make-pointer-glyph
|
|
911 (vector 'autodetect :data mouse-pointer-image)))
|
|
912 (setq idd-drag-and-drop-pointer-glyph (make-pointer-glyph))
|
|
913 (message
|
|
914 "Warning: Can't find drag and drop mouse pointer image!"))))
|
|
915
|
|
916 (idd-make-drag-and-drop-pointer-glyph)
|
|
917
|
|
918 ))
|
|
919
|
98
|
920 ;; keymap for help buffer extents
|
|
921 (if (not idd-help-start-action-keymap)
|
|
922 (progn
|
|
923 (setq idd-help-start-action-keymap
|
|
924 (make-sparse-keymap 'idd-help-start-action-keymap))
|
|
925 (if (adapt-emacs19p)
|
|
926 (define-key idd-help-start-action-keymap [(mouse-2)]
|
|
927 'idd-help-start-action)
|
|
928 (define-key idd-help-start-action-keymap "[(button2)]"
|
|
929 'idd-help-start-action))))
|
|
930
|
|
931 ;; global key bindings
|
|
932 (when idd-global-mouse-keys
|
|
933 (unless (where-is-internal 'idd-mouse-drag-and-drop global-map t)
|
|
934 (define-key global-map idd-global-mouse-keys 'idd-mouse-drag-and-drop))
|
|
935 (unless (where-is-internal 'idd-help-mouse-drag-and-drop global-map t)
|
|
936 (define-key global-map
|
|
937 idd-global-help-mouse-keys 'idd-help-mouse-drag-and-drop)))
|
2
|
938
|
|
939
|
|
940 (provide 'internal-drag-and-drop)
|
177
|
941
|
|
942 ;;; internal-drag-and-drop ends here
|