22.14 Drag and Drop

In most graphical desktop environments, Emacs has basic support for drag and drop operations. For instance, dropping text onto an Emacs frame inserts the text where it is dropped. Dropping a file onto an Emacs frame visits that file. As a special case, dropping the file on a Dired buffer moves or copies the file (according to the conventions of the application it came from) into the directory displayed in that buffer.

Dropping a file normally visits it in the window you drop it on. If you prefer to visit the file in a new window in such cases, customize the variable dnd-open-file-other-window.

The XDND and Motif drag and drop protocols, and the old KDE 1.x protocol, are currently supported.

It can be difficult to scroll a window or determine where dropped text will be inserted while dragging text onto an Emacs window. Setting the option dnd-indicate-insertion-point to a non-nil value makes point move to the location any dropped text will be inserted when the mouse moves in a window during drag, and setting dnd-scroll-margin to an integer value causes a window to be scrolled if the mouse moves within that many lines of the top or bottom of the window during drag.

Emacs can also optionally drag the region with the mouse into another portion of this or another buffer. To enable that, customize the variable mouse-drag-and-drop-region to a non-nil value. Normally, the text is moved, i.e. cut and pasted, when the destination is the same buffer as the origin; dropping the region on another buffer copies the text instead. If the value of this variable names a modifier key, such as ‘shift’, ‘control’ or ‘alt’, then pressing that modifier key when dropping the text will copy it instead of cutting it, even if you drop on the same buffer as the one from which the text came.

In order to cut text even when source and destination buffers differ, set the option mouse-drag-and-drop-region-cut-when-buffers-differ to a non-nil value. By default, on a graphic display the selected text is shown in a tooltip and point moves together with the mouse cursor during dragging. To suppress such behavior, set the options mouse-drag-and-drop-region-show-tooltip and/or mouse-drag-and-drop-region-show-cursor to nil.

To drag text from Emacs to other programs, set the option mouse-drag-and-drop-region-cross-program to a non-nil value.

On the X window system, some programs can drop files on Emacs, expecting Emacs to save them. Normally, Emacs will prompt for a file name under which the file will be saved, and then open the file, but that behavior can be changed by changing the variable x-dnd-direct-save-function. See Drag and Drop in The Emacs Lisp Reference Manual.