]> code.delx.au - gnu-emacs/blobdiff - lisp/dnd.el
; Fix breakage from previous commit
[gnu-emacs] / lisp / dnd.el
index f98d6112fba51ed29da34ec754c9dada0e328ae2..b715eecad9f903a8d9d4028a318117a66f7856f2 100644 (file)
@@ -1,11 +1,11 @@
-;;; dnd.el --- drag and drop support.  -*- coding: utf-8 -*-
+;;; dnd.el --- drag and drop support
 
-;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 2005-2016 Free Software Foundation, Inc.
 
 ;; Author: Jan Djärv <jan.h.d@swipnet.se>
-;; Maintainer: FSF
+;; Maintainer: emacs-devel@gnu.org
 ;; Keywords: window, drag, drop
+;; Package: emacs
 
 ;; This file is part of GNU Emacs.
 
@@ -122,18 +122,29 @@ 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)
+  (let ((sysname (system-name)))
+    (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)))))))
-
+         (sysname-no-dot
+          (downcase (if (string-match "^[^\\.]+" sysname)
+                        (match-string 0 sysname)
+                      sysname))))
+      (when (and hostname
+                (or (string-equal "localhost" hostname)
+                    (string-equal (downcase sysname) hostname)
+                    (string-equal sysname-no-dot hostname)))
+       (concat "file://" (substring uri (+ 7 (length hostname))))))))
+
+(defsubst dnd-unescape-uri (uri)
+  (replace-regexp-in-string
+   "%[A-Fa-f0-9][A-Fa-f0-9]"
+   (lambda (arg)
+     (let ((str (make-string 1 0)))
+       (aset str 0 (string-to-number (substring arg 1) 16))
+       str))
+   uri t t))
+
+;; http://lists.gnu.org/archive/html/emacs-devel/2006-05/msg01060.html
 (defun 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,
@@ -142,25 +153,18 @@ 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)
-      (setq f (replace-regexp-in-string
-              "%[A-Fa-f0-9][A-Fa-f0-9]"
-              (lambda (arg)
-                (let ((str (make-string 1 0)))
-                  (aset str 0 (string-to-number (substring arg 1) 16))
-                  str))
-              f t t))
-      (let* ((decoded-f (decode-coding-string
-                        f
-                        (or file-name-coding-system
-                            default-file-name-coding-system))))
-       (setq f (cond ((file-readable-p decoded-f) decoded-f)
-                     ((file-readable-p f) f)
-                     (t nil)))))
+                 (substring uri (match-end 0)))))
+       (coding (if (equal system-type 'windows-nt)
+                   ;; W32 pretends that file names are UTF-8 encoded.
+                   'utf-8
+                 (or file-name-coding-system
+                     default-file-name-coding-system))))
+    (and f (setq f (decode-coding-string (dnd-unescape-uri f) coding)))
+    (when (and f must-exist (not (file-readable-p f)))
+      (setq f nil))
     f))
 
-(defun dnd-open-local-file (uri action)
+(defun dnd-open-local-file (uri _action)
   "Open a local file.
 The file is opened in the current window, or a new window if
 `dnd-open-file-other-window' is set.  URI is the url for the file,
@@ -180,7 +184,7 @@ An alternative for systems that do not support unc file names is
          'private)
       (error "Can not read %s" uri))))
 
-(defun dnd-open-remote-url (uri action)
+(defun dnd-open-remote-url (uri _action)
   "Open a remote file with `find-file' and `url-handler-mode'.
 Turns `url-handler-mode' on if not on before.  The file is opened in the
 current window, or a new window if `dnd-open-file-other-window' is set.
@@ -226,5 +230,4 @@ TEXT is the text as a string, WINDOW is the window where the drop happened."
 
 (provide 'dnd)
 
-;; arch-tag: 0472f6a5-2e8f-4304-9e44-1a0877c771b7
 ;;; dnd.el ends here