Fix cursor display when several display strings follow each other.
[emacs.git] / lisp / url / url-expand.el
blob4bf13f4abe3f2c945f6fc12e45fc79f27442981c
1 ;;; url-expand.el --- expand-file-name for URLs
3 ;; Copyright (C) 1999, 2004-2012 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'url-methods)
25 (require 'url-util)
26 (require 'url-parse)
28 (defun url-expander-remove-relative-links (name)
29 ;; Strip . and .. from pathnames
30 (let ((new (if (not (string-match "^/" name))
31 (concat "/" name)
32 name)))
34 ;; If it ends with a '/.' or '/..', tack on a trailing '/' sot hat
35 ;; the tests that follow are not too complicated in terms of
36 ;; looking for '..' or '../', etc.
37 (if (string-match "/\\.+$" new)
38 (setq new (concat new "/")))
40 ;; Remove '/./' first
41 (while (string-match "/\\(\\./\\)" new)
42 (setq new (concat (substring new 0 (match-beginning 1))
43 (substring new (match-end 1)))))
45 ;; Then remove '/../'
46 (while (string-match "/\\([^/]*/\\.\\./\\)" new)
47 (setq new (concat (substring new 0 (match-beginning 1))
48 (substring new (match-end 1)))))
50 ;; Remove cruft at the beginning of the string, so people that put
51 ;; in extraneous '..' because they are morons won't lose.
52 (while (string-match "^/\\.\\.\\(/\\)" new)
53 (setq new (substring new (match-beginning 1) nil)))
54 new))
56 (defun url-expand-file-name (url &optional default)
57 "Convert URL to a fully specified URL, and canonicalize it.
58 Second arg DEFAULT is a URL to start with if URL is relative.
59 If DEFAULT is nil or missing, the current buffer's URL is used.
60 Path components that are `.' are removed, and
61 path components followed by `..' are removed, along with the `..' itself."
62 (if (and url (not (string-match "^#" url)))
63 ;; Need to nuke newlines and spaces in the URL, or we open
64 ;; ourselves up to potential security holes.
65 (setq url (mapconcat (function (lambda (x)
66 (if (memq x '(? ?\n ?\r))
68 (char-to-string x))))
69 url "")))
71 ;; Need to figure out how/where to expand the fragment relative to
72 (setq default (cond
73 ((vectorp default)
74 ;; Default URL has already been parsed
75 default)
76 (default
77 ;; They gave us a default URL in non-parsed format
78 (url-generic-parse-url default))
79 (url-current-object
80 ;; We are in a URL-based buffer, use the pre-parsed object
81 url-current-object)
82 ((string-match url-nonrelative-link url)
83 ;; The URL they gave us is absolute, go for it.
84 nil)
86 ;; Hmmm - this shouldn't ever happen.
87 (error "url-expand-file-name confused - no default?"))))
89 (cond
90 ((= (length url) 0) ; nil or empty string
91 (url-recreate-url default))
92 ((string-match "^#" url) ; Offset link, use it raw
93 url)
94 ((string-match url-nonrelative-link url) ; Fully-qualified URL, return it immediately
95 url)
97 (let* ((urlobj (url-generic-parse-url url))
98 (inhibit-file-name-handlers t)
99 (expander (url-scheme-get-property (url-type default) 'expand-file-name)))
100 (if (string-match "^//" url)
101 (setq urlobj (url-generic-parse-url (concat (url-type default) ":"
102 url))))
103 (funcall expander urlobj default)
104 (url-recreate-url urlobj)))))
106 (defun url-identity-expander (urlobj defobj)
107 (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj))))
109 (defun url-default-expander (urlobj defobj)
110 ;; The default expansion routine - urlobj is modified by side effect!
111 (if (url-type urlobj)
112 ;; Well, they told us the scheme, let's just go with it.
114 (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj)))
115 (setf (url-port urlobj) (or (url-port urlobj)
116 (and (string= (url-type urlobj)
117 (url-type defobj))
118 (url-port defobj))))
119 (if (not (string= "file" (url-type urlobj)))
120 (setf (url-host urlobj) (or (url-host urlobj) (url-host defobj))))
121 (if (string= "ftp" (url-type urlobj))
122 (setf (url-user urlobj) (or (url-user urlobj) (url-user defobj))))
123 (if (string= (url-filename urlobj) "")
124 (setf (url-filename urlobj) "/"))
125 ;; If the object we're expanding from is full, then we are now
126 ;; full.
127 (unless (url-fullness urlobj)
128 (setf (url-fullness urlobj) (url-fullness defobj)))
129 (if (string-match "^/" (url-filename urlobj))
131 (let ((query nil)
132 (file nil)
133 (sepchar nil))
134 (if (string-match "[?#]" (url-filename urlobj))
135 (setq query (substring (url-filename urlobj) (match-end 0))
136 file (substring (url-filename urlobj) 0 (match-beginning 0))
137 sepchar (substring (url-filename urlobj) (match-beginning 0) (match-end 0)))
138 (setq file (url-filename urlobj)))
139 ;; We use concat rather than expand-file-name to combine
140 ;; directory and file name, since urls do not follow the same
141 ;; rules as local files on all platforms.
142 (setq file (url-expander-remove-relative-links
143 (concat (url-file-directory (url-filename defobj)) file)))
144 (setf (url-filename urlobj)
145 (if query (concat file sepchar query) file))))))
147 (provide 'url-expand)
149 ;;; url-expand.el ends here