view lisp/url/url-expand.el @ 105799:3fe6da4a95a9

* cedet/srecode/srt-mode.el (semantic-analyze-possible-completions): * cedet/semantic/symref/list.el (semantic-symref-rb-toggle-expand-tag): * cedet/semantic/symref/grep.el (semantic-symref-perform-search): * cedet/semantic/bovine/gcc.el (semantic-gcc-query): * cedet/semantic/bovine/c.el (semantic-c-parse-lexical-token): * cedet/semantic/analyze/debug.el (semantic-analyzer-debug-add-buttons) (semantic-analyzer-debug-global-symbol) (semantic-analyzer-debug-missing-innertype) (semantic-analyzer-debug-insert-include-summary): * cedet/semantic/util.el (semantic-file-tag-table): (semantic-describe-buffer-var-helper, semantic-something-to-tag-table) (semantic-recursive-find-nonterminal-by-name): * cedet/semantic/tag-ls.el (semantic-tag-calculate-parent-default): * cedet/semantic/tag-file.el (semantic-prototype-file): * cedet/semantic/symref.el (semantic-symref-parse-tool-output): * cedet/semantic/sb.el (semantic-sb-fetch-tag-table): * cedet/semantic/lex-spp.el (semantic-lex-spp-lex-text-string): * cedet/semantic/idle.el (semantic-idle-work-for-one-buffer): (semantic-idle-summary-maybe-highlight): * cedet/semantic/ia-sb.el (semantic-ia-speedbar) (semantic-ia-sb-tag-info): * cedet/semantic/grammar.el (semantic-analyze-possible-completions): * cedet/semantic/find.el (semantic-brute-find-tag-by-position): * cedet/semantic/ede-grammar.el (project-compile-target): (ede-proj-makefile-insert-variables): * cedet/semantic/debug.el (semantic-debug-set-parser-location): (semantic-debug-set-source-location, semantic-debug-interface-layout) (semantic-debug-mode, semantic-debug): * cedet/semantic/db.el (semanticdb-needs-refresh-p): * cedet/semantic/db-typecache.el (semanticdb-typecache-refresh-for-buffer): * cedet/semantic/db-javascript.el (semanticdb-equivalent-mode): * cedet/semantic/db-find.el (semanticdb-find-log-new-search) (semanticdb-find-translate-path-includes--internal) (semanticdb-reset-log, semanticdb-find-log-activity): * cedet/semantic/db-file.el (object-write): * cedet/semantic/db-el.el (semanticdb-equivalent-mode): * cedet/semantic/db-ebrowse.el (semanticdb-ebrowse-C-file-p) (semanticdb-create-ebrowse-database): * cedet/semantic/db-debug.el (semanticdb-table-sanity-check): * cedet/semantic/complete.el (semantic-displayor-focus-request) (semantic-collector-calculate-completions-raw) (semantic-complete-read-tag-analyzer): * cedet/semantic/analyze.el (semantic-analyze-pulse): * cedet/ede/util.el (ede-update-version-in-source): * cedet/ede/proj.el (project-delete-target): * cedet/ede/proj-elisp.el (ede-update-version-in-source) (ede-proj-flush-autoconf): * cedet/ede/pconf.el (ede-proj-configure-synchronize) (ede-proj-configure-synchronize): * cedet/ede/locate.el (ede-locate-file-in-project-impl): * cedet/ede/linux.el (ede-linux-version): * cedet/ede/emacs.el (ede-emacs-version): * cedet/ede/dired.el (ede-dired-add-to-target): * cedet/ede.el (ede-buffer-header-file, ede-find-target) (ede-buffer-documentation-files, ede-project-buffers, ede-set) (ede-target-buffers, ede-buffers, ede-make-project-local-variable): * cedet/cedet-idutils.el (cedet-idutils-fnid-call): (cedet-idutils-lid-call, cedet-idutils-expand-filename) (cedet-idutils-version-check): * cedet/cedet-global.el (cedet-gnu-global-call): (cedet-gnu-global-expand-filename, cedet-gnu-global-root) (cedet-gnu-global-version-check, cedet-gnu-global-scan-hits): * cedet/cedet-cscope.el (cedet-cscope-call) (cedet-cscope-expand-filename, cedet-cscope-version-check): Use with-current-buffer. * cedet/ede.el (ede-make-project-local-variable) (ede-set-project-variables, ede-set): Use dolist.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Fri, 30 Oct 2009 02:16:41 +0000
parents fa76b8415858
children 1d1d5d9bd884
line wrap: on
line source

;;; url-expand.el --- expand-file-name for URLs

;; Copyright (C) 1999, 2004, 2005, 2006, 2007, 2008, 2009
;;   Free Software Foundation, Inc.

;; Keywords: comm, data, processes

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'url-methods)
(require 'url-util)
(require 'url-parse)
(eval-when-compile (require 'cl))

(defun url-expander-remove-relative-links (name)
  ;; Strip . and .. from pathnames
  (let ((new (if (not (string-match "^/" name))
		 (concat "/" name)
	       name)))

    ;; If it ends with a '/.' or '/..', tack on a trailing '/' sot hat
    ;; the tests that follow are not too complicated in terms of
    ;; looking for '..' or '../', etc.
    (if (string-match "/\\.+$" new)
	(setq new (concat new "/")))

    ;; Remove '/./' first
    (while (string-match "/\\(\\./\\)" new)
      (setq new (concat (substring new 0 (match-beginning 1))
			(substring new (match-end 1)))))

    ;; Then remove '/../'
    (while (string-match "/\\([^/]*/\\.\\./\\)" new)
      (setq new (concat (substring new 0 (match-beginning 1))
			(substring new (match-end 1)))))

    ;; Remove cruft at the beginning of the string, so people that put
    ;; in extraneous '..' because they are morons won't lose.
    (while (string-match "^/\\.\\.\\(/\\)" new)
      (setq new (substring new (match-beginning 1) nil)))
    new))

(defun url-expand-file-name (url &optional default)
  "Convert URL to a fully specified URL, and canonicalize it.
Second arg DEFAULT is a URL to start with if URL is relative.
If DEFAULT is nil or missing, the current buffer's URL is used.
Path components that are `.' are removed, and 
path components followed by `..' are removed, along with the `..' itself."
  (if (and url (not (string-match "^#" url)))
      ;; Need to nuke newlines and spaces in the URL, or we open
      ;; ourselves up to potential security holes.
      (setq url (mapconcat (function (lambda (x)
				       (if (memq x '(?  ?\n ?\r))
					   ""
					 (char-to-string x))))
			   url "")))

  ;; Need to figure out how/where to expand the fragment relative to
  (setq default (cond
		 ((vectorp default)
		  ;; Default URL has already been parsed
		  default)
		 (default
		   ;; They gave us a default URL in non-parsed format
		   (url-generic-parse-url default))
		 (url-current-object
		  ;; We are in a URL-based buffer, use the pre-parsed object
		  url-current-object)
		 ((string-match url-nonrelative-link url)
		  ;; The URL they gave us is absolute, go for it.
		  nil)
		 (t
		  ;; Hmmm - this shouldn't ever happen.
		  (error "url-expand-file-name confused - no default?"))))

  (cond
   ((= (length url) 0)			; nil or empty string
    (url-recreate-url default))
   ((string-match "^#" url)		; Offset link, use it raw
    url)
   ((string-match url-nonrelative-link url) ; Fully-qualified URL, return it immediately
    url)
   (t
    (let* ((urlobj (url-generic-parse-url url))
	   (inhibit-file-name-handlers t)
	   (expander (url-scheme-get-property (url-type default) 'expand-file-name)))
      (if (string-match "^//" url)
	  (setq urlobj (url-generic-parse-url (concat (url-type default) ":"
						      url))))
      (funcall expander urlobj default)
      (url-recreate-url urlobj)))))

(defun url-identity-expander (urlobj defobj)
  (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj))))

(defun url-default-expander (urlobj defobj)
  ;; The default expansion routine - urlobj is modified by side effect!
  (if (url-type urlobj)
      ;; Well, they told us the scheme, let's just go with it.
      nil
    (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj)))
    (setf (url-port urlobj) (or (url-port urlobj)
                                (and (string= (url-type urlobj)
                                              (url-type defobj))
                                     (url-port defobj))))
    (if (not (string= "file" (url-type urlobj)))
	(setf (url-host urlobj) (or (url-host urlobj) (url-host defobj))))
    (if (string= "ftp"  (url-type urlobj))
	(setf (url-user urlobj) (or (url-user urlobj) (url-user defobj))))
    (if (string= (url-filename urlobj) "")
	(setf (url-filename urlobj) "/"))
    (if (string-match "^/" (url-filename urlobj))
	nil
      (let ((query nil)
	    (file nil)
	    (sepchar nil))
	(if (string-match "[?#]" (url-filename urlobj))
	    (setq query (substring (url-filename urlobj) (match-end 0))
		  file (substring (url-filename urlobj) 0 (match-beginning 0))
		  sepchar (substring (url-filename urlobj) (match-beginning 0) (match-end 0)))
	  (setq file (url-filename urlobj)))
	;; We use concat rather than expand-file-name to combine
	;; directory and file name, since urls do not follow the same
	;; rules as local files on all platforms.
	(setq file (url-expander-remove-relative-links
		    (concat (url-file-directory (url-filename defobj)) file)))
	(setf (url-filename urlobj)
              (if query (concat file sepchar query) file))))))

(provide 'url-expand)

;; arch-tag: 7b5f744b-b721-49da-be47-484631680a5a
;;; url-expand.el ends here