]> git.eshelyaron.com Git - emacs.git/commitdiff
Handle /assets and /content file names in `android-browse-url'
authorPo Lu <luangruo@yahoo.com>
Thu, 15 Feb 2024 06:23:43 +0000 (14:23 +0800)
committerEshel Yaron <me@eshelyaron.com>
Thu, 15 Feb 2024 10:08:39 +0000 (11:08 +0100)
* lisp/net/browse-url.el (android-browse-url): New function.

* lisp/term/android-win.el (android-browse-url-internal): Update
function declaration.

* src/androidselect.c (Fandroid_browse_url): Rename to...
(Fandroid_browse_url_internal): ... this.
(syms_of_androidselect): Adjust to match.

(cherry picked from commit 783a511d1e31b5c9e5f9cb8ec27fd91d1b9078c9)

lisp/net/browse-url.el
lisp/term/android-win.el
src/androidselect.c

index bc2a7db9a8bb8c8a5bc47452dda7d3c8e5e55216..ddc5772434372485ed1bd00aaaabab7af7c2b46d 100644 (file)
@@ -1324,7 +1324,7 @@ and instant messengers instead of opening it in a web browser."
   :type 'boolean
   :version "30.1")
 
-(declare-function android-browse-url "androidselect.c")
+(declare-function android-browse-url "../term/android-win")
 
 ;;;###autoload
 (defun browse-url-default-android-browser (url &optional _new-window)
index e0d252f17e0aa43e53df44ee18a04617b59ecab5..b7b0920626e33f3fa785420e5c56484153b79de9 100644 (file)
@@ -479,6 +479,50 @@ the UTF-8 coding system."
     ;; Return the concatenation of both these values.
     (concat locale-base locale-modifier)))
 
+\f
+;; Miscellaneous functions.
+
+(declare-function android-browse-url-internal "androidselect.c")
+
+(defun android-browse-url (url &optional send)
+  "Open URL in an external application.
+
+URL should be a URL-encoded URL with a scheme specified unless
+SEND is non-nil.  Signal an error upon failure.
+
+If SEND is nil, start a program that is able to display the URL,
+such as a web browser.  Otherwise, try to share URL using
+programs such as email clients.
+
+If URL is a file URI, convert it into a `content' address
+accessible to other programs."
+  (when-let* ((uri (url-generic-parse-url url))
+              (filename (url-filename uri))
+              ;; If `uri' is a file URI and the file resides in /content
+              ;; or /assets, copy it to a temporary file before
+              ;; providing it to other programs.
+              (replacement-url (and (string-match-p
+                                     "/\\(content\\|assets\\)[/$]"
+                                     filename)
+                                    (prog1 t
+                                      (copy-file
+                                       filename
+                                       (setq filename
+                                             (make-temp-file
+                                              "local"
+                                              nil
+                                              (let ((extension
+                                                     (file-name-extension
+                                                      filename)))
+                                                (if extension
+                                                    (concat "."
+                                                            extension)
+                                                  nil))))
+                                       t))
+                                    (concat "file://" filename))))
+    (setq url replacement-url))
+  (android-browse-url-internal url send))
+
 \f
 (provide 'android-win)
 ;; android-win.el ends here.
index 5b23c559d2c267a6b60d80269409b96da58c1447..61f1c6045db416fe302c75c43f69b63f0d8c04da 100644 (file)
@@ -237,15 +237,21 @@ DEFUN ("android-clipboard-exists-p", Fandroid_clipboard_exists_p,
   return rc ? Qt : Qnil;
 }
 
-DEFUN ("android-browse-url", Fandroid_browse_url,
-       Sandroid_browse_url, 1, 2, 0,
-       doc: /* Open URL in an external application.  URL should be a
-URL-encoded URL with a scheme specified unless SEND is non-nil.
-Signal an error upon failure.
+DEFUN ("android-browse-url-internal", Fandroid_browse_url_internal,
+       Sandroid_browse_url_internal, 1, 2, 0,
+       doc: /* Open URL in an external application.
+
+URL should be a URL-encoded URL with a scheme specified unless SEND is
+non-nil.  Signal an error upon failure.
 
 If SEND is nil, start a program that is able to display the URL, such
 as a web browser.  Otherwise, try to share URL using programs such as
-email clients.  */)
+email clients.
+
+If URL is a file URI, convert it into a `content' address accessible to
+other programs.  Files inside the /content or /assets directories cannot
+be opened through such addresses, which this function does not provide
+for.  Use `android-browse-url' instead.  */)
   (Lisp_Object url, Lisp_Object send)
 {
   Lisp_Object value;
@@ -803,7 +809,7 @@ syms_of_androidselect (void)
   defsubr (&Sandroid_set_clipboard);
   defsubr (&Sandroid_get_clipboard);
   defsubr (&Sandroid_clipboard_exists_p);
-  defsubr (&Sandroid_browse_url);
+  defsubr (&Sandroid_browse_url_internal);
   defsubr (&Sandroid_get_clipboard_targets);
   defsubr (&Sandroid_get_clipboard_data);