-(defun x-dnd-handle-one-url (window action arg)
- "Handle one dropped url by calling the appropriate handler.
-The handler is first localted by looking at `x-dnd-protocol-alist'.
-If no match is found here, and the value of `browse-url-browser-function'
-is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
-If no match is found, just call `x-dnd-insert-text'.
-WINDOW is where the drop happend, ACTION is the action for the drop,
-ARG is the URL that has been dropped.
-Returns ACTION."
- (require 'browse-url)
- (let* ((uri (replace-regexp-in-string
- "%[A-Z0-9][A-Z0-9]"
- (lambda (arg)
- (format "%c" (string-to-number (substring arg 1) 16)))
- arg))
- ret)
- (or
- (catch 'done
- (dolist (bf x-dnd-protocol-alist)
- (when (string-match (car bf) uri)
- (setq ret (funcall (cdr bf) uri action))
- (throw 'done t)))
- nil)
- (when (not (functionp browse-url-browser-function))
- (catch 'done
- (dolist (bf browse-url-browser-function)
- (when (string-match (car bf) uri)
- (setq ret 'private)
- (funcall (cdr bf) uri action)
- (throw 'done t)))
- nil))
- (progn
- (x-dnd-insert-text window action uri)
- (setq ret 'private)))
- ret))
-
-
-(defun x-dnd-get-local-file-uri (uri)
- "Return an uri converted to file:/// syntax if uri is a local file.
-Return nil if URI is not a local file."
-
- ;; The hostname may be our hostname, in that case, convert to a local
- ;; file. Otherwise return nil. TODO: How about an IP-address as hostname?
- (let ((hostname (when (string-match "^file://\\([^/]*\\)" uri)
- (downcase (match-string 1 uri))))
- (system-name-no-dot
- (downcase (if (string-match "^[^\\.]+" system-name)
- (match-string 0 system-name)
- system-name))))
- (when (and hostname
- (or (string-equal "localhost" hostname)
- (string-equal (downcase system-name) hostname)
- (string-equal system-name-no-dot hostname)))
- (concat "file://" (substring uri (+ 7 (length hostname)))))))
-
-(defun x-dnd-get-local-file-name (uri &optional must-exist)
- "Return file name converted from file:/// or file: syntax.
-URI is the uri for the file. If MUST-EXIST is given and non-nil,
-only return non-nil if the file exists.
-Return nil if URI is not a local file."
- (let ((f (cond ((string-match "^file:///" uri) ; XDND format.
- (substring uri (1- (match-end 0))))
- ((string-match "^file:" uri) ; Old KDE, Motif, Sun
- (substring uri (match-end 0))))))
- (when (and f must-exist)
- (let* ((decoded-f (decode-coding-string
- f
- (or file-name-coding-system
- default-file-name-coding-system)))
- (try-f (if (file-readable-p decoded-f) decoded-f f)))
- (when (file-readable-p try-f) try-f)))))
-
-
-(defun x-dnd-open-local-file (uri action)
- "Open a local file.
-The file is opened in the current window, or a new window if
-`x-dnd-open-file-other-window' is set. URI is the url for the file,
-and must have the format file:file-name or file:///file-name.
-The last / in file:/// is part of the file name. ACTION is ignored."
-
- (let* ((f (x-dnd-get-local-file-name uri t)))
- (if (and f (file-readable-p f))
- (progn
- (if x-dnd-open-file-other-window
- (find-file-other-window f)
- (find-file f))
- 'private)
- (error "Can not read %s" uri))))
-
-(defun x-dnd-open-file (uri action)
- "Open a local or remote file.
-The file is opened in the current window, or a new window if
-`x-dnd-open-file-other-window' is set. URI is the url for the file,
-and must have the format file://hostname/file-name. ACTION is ignored.
-The last / in file://hostname/ is part of the file name."
-
- ;; The hostname may be our hostname, in that case, convert to a local
- ;; file. Otherwise return nil.
- (let ((local-file (x-dnd-get-local-file-uri uri)))
- (if local-file (x-dnd-open-local-file local-file action)
- (error "Remote files not supported"))))
-
-