Function: browse-url-file-url

browse-url-file-url is a byte-compiled function defined in browse-url.el.gz.

Signature

(browse-url-file-url FILE)

Documentation

Return the URL corresponding to FILE.

Use variable browse-url-filename-alist to map filenames to URLs.

Source Code

;; Defined in /usr/src/emacs/lisp/net/browse-url.el.gz
(defun browse-url-file-url (file)
  "Return the URL corresponding to FILE.
Use variable `browse-url-filename-alist' to map filenames to URLs."
  (when-let* ((coding (browse-url--file-name-coding-system)))
    (setq file (encode-coding-string file coding)))
  (if (and (file-remote-p file)
           ;; We're applying special rules for FTP URLs for historical
           ;; reasons.
           (seq-find (lambda (match)
                       (and (string-match-p (car match) file)
                            (not (string-match "\\`file:" (cdr match)))))
                     browse-url-filename-alist))
      (setq file (browse-url-url-encode-chars file "[*\"()',=;?% ]"))
    ;; Encode all other file names properly.
    (let ((bits (file-name-split file)))
      (setq file
            (string-join
             ;; On Windows, the first bit here might be "c:" or the
             ;; like, so don't encode the ":" in the first bit.
             (cons (let ((url-unreserved-chars
                          (if (file-name-absolute-p file)
                              (cons ?: url-unreserved-chars)
                            url-unreserved-chars)))
                     (url-hexify-string (car bits)))
                   (mapcar #'url-hexify-string (cdr bits)))
             "/"))))
  (dolist (map browse-url-filename-alist)
    (when (and map (string-match (car map) file))
      (setq file (replace-match (cdr map) t nil file))))
  file)