dnd.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ;;; dnd.el --- drag and drop support. -*- coding: utf-8 -*-
  2. ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
  3. ;; Author: Jan Djärv <jan.h.d@swipnet.se>
  4. ;; Maintainer: FSF
  5. ;; Keywords: window, drag, drop
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file provides the generic handling of the drop part only.
  20. ;; Different DND backends (X11, W32, etc.) that handle the platform
  21. ;; specific DND parts call the functions here to do final delivery of
  22. ;; a drop.
  23. ;;; Code:
  24. ;;; Customizable variables
  25. ;;;###autoload
  26. (defcustom dnd-protocol-alist
  27. `((,(purecopy "^file:///") . dnd-open-local-file) ; XDND format.
  28. (,(purecopy "^file://") . dnd-open-file) ; URL with host
  29. (,(purecopy "^file:") . dnd-open-local-file) ; Old KDE, Motif, Sun
  30. (,(purecopy "^\\(https?\\|ftp\\|file\\|nfs\\)://") . dnd-open-file)
  31. )
  32. "The functions to call for different protocols when a drop is made.
  33. This variable is used by `dnd-handle-one-url' and `dnd-handle-file-name'.
  34. The list contains of (REGEXP . FUNCTION) pairs.
  35. The functions shall take two arguments, URL, which is the URL dropped and
  36. ACTION which is the action to be performed for the drop (move, copy, link,
  37. private or ask).
  38. If no match is found here, and the value of `browse-url-browser-function'
  39. is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
  40. If no match is found, the URL is inserted as text by calling `dnd-insert-text'.
  41. The function shall return the action done (move, copy, link or private)
  42. if some action was made, or nil if the URL is ignored."
  43. :version "22.1"
  44. :type '(repeat (cons (regexp) (function)))
  45. :group 'dnd)
  46. (defcustom dnd-open-remote-file-function
  47. (if (eq system-type 'windows-nt)
  48. 'dnd-open-local-file
  49. 'dnd-open-remote-url)
  50. "The function to call when opening a file on a remote machine.
  51. The function will be called with two arguments; URI and ACTION. See
  52. `dnd-open-file' for details.
  53. If nil, then dragging remote files into Emacs will result in an error.
  54. Predefined functions are `dnd-open-local-file' and `dnd-open-remote-url'.
  55. `dnd-open-local-file' attempts to open a remote file using its UNC name and
  56. is the default on MS-Windows. `dnd-open-remote-url' uses `url-handler-mode'
  57. and is the default except for MS-Windows."
  58. :version "22.1"
  59. :type 'function
  60. :group 'dnd)
  61. (defcustom dnd-open-file-other-window nil
  62. "If non-nil, always use find-file-other-window to open dropped files."
  63. :version "22.1"
  64. :type 'boolean
  65. :group 'dnd)
  66. ;; Functions
  67. (defun dnd-handle-one-url (window action url)
  68. "Handle one dropped url by calling the appropriate handler.
  69. The handler is first located by looking at `dnd-protocol-alist'.
  70. If no match is found here, and the value of `browse-url-browser-function'
  71. is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
  72. If no match is found, just call `dnd-insert-text'.
  73. WINDOW is where the drop happened, ACTION is the action for the drop,
  74. URL is what has been dropped.
  75. Returns ACTION."
  76. (require 'browse-url)
  77. (let (ret)
  78. (or
  79. (catch 'done
  80. (dolist (bf dnd-protocol-alist)
  81. (when (string-match (car bf) url)
  82. (setq ret (funcall (cdr bf) url action))
  83. (throw 'done t)))
  84. nil)
  85. (when (not (functionp browse-url-browser-function))
  86. (catch 'done
  87. (dolist (bf browse-url-browser-function)
  88. (when (string-match (car bf) url)
  89. (setq ret 'private)
  90. (funcall (cdr bf) url action)
  91. (throw 'done t)))
  92. nil))
  93. (progn
  94. (dnd-insert-text window action url)
  95. (setq ret 'private)))
  96. ret))
  97. (defun dnd-get-local-file-uri (uri)
  98. "Return an uri converted to file:/// syntax if uri is a local file.
  99. Return nil if URI is not a local file."
  100. ;; The hostname may be our hostname, in that case, convert to a local
  101. ;; file. Otherwise return nil. TODO: How about an IP-address as hostname?
  102. (let ((hostname (when (string-match "^file://\\([^/]*\\)" uri)
  103. (downcase (match-string 1 uri))))
  104. (system-name-no-dot
  105. (downcase (if (string-match "^[^\\.]+" system-name)
  106. (match-string 0 system-name)
  107. system-name))))
  108. (when (and hostname
  109. (or (string-equal "localhost" hostname)
  110. (string-equal (downcase system-name) hostname)
  111. (string-equal system-name-no-dot hostname)))
  112. (concat "file://" (substring uri (+ 7 (length hostname)))))))
  113. (defsubst dnd-unescape-uri (uri)
  114. (replace-regexp-in-string
  115. "%[A-Fa-f0-9][A-Fa-f0-9]"
  116. (lambda (arg)
  117. (let ((str (make-string 1 0)))
  118. (aset str 0 (string-to-number (substring arg 1) 16))
  119. str))
  120. uri t t))
  121. ;; http://lists.gnu.org/archive/html/emacs-devel/2006-05/msg01060.html
  122. (defun dnd-get-local-file-name (uri &optional must-exist)
  123. "Return file name converted from file:/// or file: syntax.
  124. URI is the uri for the file. If MUST-EXIST is given and non-nil,
  125. only return non-nil if the file exists.
  126. Return nil if URI is not a local file."
  127. (let ((f (cond ((string-match "^file:///" uri) ; XDND format.
  128. (substring uri (1- (match-end 0))))
  129. ((string-match "^file:" uri) ; Old KDE, Motif, Sun
  130. (substring uri (match-end 0))))))
  131. (and f (setq f (decode-coding-string (dnd-unescape-uri f)
  132. (or file-name-coding-system
  133. default-file-name-coding-system))))
  134. (when (and f must-exist (not (file-readable-p f)))
  135. (setq f nil))
  136. f))
  137. (defun dnd-open-local-file (uri _action)
  138. "Open a local file.
  139. The file is opened in the current window, or a new window if
  140. `dnd-open-file-other-window' is set. URI is the url for the file,
  141. and must have the format file:file-name or file:///file-name.
  142. The last / in file:/// is part of the file name. If the system
  143. natively supports unc file names, then remote urls of the form
  144. file://server-name/file-name will also be handled by this function.
  145. An alternative for systems that do not support unc file names is
  146. `dnd-open-remote-url'. ACTION is ignored."
  147. (let* ((f (dnd-get-local-file-name uri t)))
  148. (if (and f (file-readable-p f))
  149. (progn
  150. (if dnd-open-file-other-window
  151. (find-file-other-window f)
  152. (find-file f))
  153. 'private)
  154. (error "Can not read %s" uri))))
  155. (defun dnd-open-remote-url (uri _action)
  156. "Open a remote file with `find-file' and `url-handler-mode'.
  157. Turns `url-handler-mode' on if not on before. The file is opened in the
  158. current window, or a new window if `dnd-open-file-other-window' is set.
  159. URI is the url for the file. ACTION is ignored."
  160. (progn
  161. (require 'url-handlers)
  162. (or url-handler-mode (url-handler-mode))
  163. (if dnd-open-file-other-window
  164. (find-file-other-window uri)
  165. (find-file uri))
  166. 'private))
  167. (defun dnd-open-file (uri action)
  168. "Open a local or remote file.
  169. The file is opened in the current window, or a new window if
  170. `dnd-open-file-other-window' is set. URI is the url for the file,
  171. and must have the format file://hostname/file-name. ACTION is ignored.
  172. The last / in file://hostname/ is part of the file name."
  173. ;; The hostname may be our hostname, in that case, convert to a local
  174. ;; file. Otherwise return nil.
  175. (let ((local-file (dnd-get-local-file-uri uri)))
  176. (if local-file (dnd-open-local-file local-file action)
  177. (if dnd-open-remote-file-function
  178. (funcall dnd-open-remote-file-function uri action)
  179. (error "Remote files not supported")))))
  180. (defun dnd-insert-text (window action text)
  181. "Insert text at point or push to the kill ring if buffer is read only.
  182. TEXT is the text as a string, WINDOW is the window where the drop happened."
  183. (if (or buffer-read-only
  184. (not (windowp window)))
  185. (progn
  186. (kill-new text)
  187. (message "%s"
  188. (substitute-command-keys
  189. "The dropped text can be accessed with \\[yank]")))
  190. (insert text))
  191. action)
  192. (provide 'dnd)
  193. ;;; dnd.el ends here