![]() |
The URL system | ![]() |
TeXmacs uses a tree representation for urls. This allows us to generalize the concept of an url and allow paths and patterns to be regarded as urls too. There are three main types of urls:
rootless urls, like a/b/c. These urls are mainly used in computations. For example, they can be appended to another url.
Standard rooted urls, like file:///usr or https://www.texmacs.org. These are the same as those used on the web.
System urls, characterized by a “default” root. These urls are similar to standard rooted urls, but they behave in a slightly different way with respect to concatenation. For instance https://www.texmacs.org/Web * file:///tmp would yield file:///tmp, whereas https://www.texmacs.org/Web * /tmp yields https://www.texmacs.org/tmp.
There are several formats for parsing (and printing) urls:
System format: the usual format on your operating system. On unix systems “/usr/bin:/usr/local/bin” would be a valid url representing a path and on windows systems “c:\windows;c:\TeXmacs” would be OK.
Unix format: this format forces unix-like notation even for other systems like Windows. This is convenient for url's in the source code. The home directory ~ and environment variables like $TEXMACS_PATH can also be part of the url.
Standard format: the format which is used on the web. Notice that ftp://www.texmacs.org/pub and ftp://www.texmacs.org/pub/ represent different urls. The second one is represented by concating on the right with an empty name.
When an explicit operation on urls need to be performed, like reading a file, the url is first “resolved” into a single url with a unique name (modulo symbolic links) for the resource (first match). Next, the url is “concretized” as a system-specific file name which is understood by the operating system. Note that for remote urls this may involve downloading a file. Concretized urls should be used quickly and not memorized, since such names may be the names of temporary files, which can be destroyed afterwards.
From Scheme, urls are represented by a special data type; the routines below also accept strings, which are converted using the Unix format (the system format on Windows), so that ~, environment variables and the separators / and : may be used in them. The C++ implementation can be found in
System/Classes/url.cpp
and
System/Files/file.cpp
, and the glue definitions in
Scheme/Glue/build-glue-basic.scm
.
(string->url s)
(system->url s)
Convert a string into a url, using the standard, the system and the unix format respectively. The routine (root->url r) creates a url with root (protocol) r, and (url-unix dir name) the concatenation of two strings in the unix format. The routine url->url returns its argument (it converts a string into a url).
(url->string u)
(url->system u)
Convert a url into a string, using the standard, the system and the unix format respectively. The routine (url->stree u) returns the internal tree representation of u.
(url-append u1 u2)
Concatenate two urls (i.e. u2 relative to u1), resp. build the url which stands for either u1 or u2. The routine (url-ref u i) returns the child i (1 or 2) of a concatenation or of an alternative: since a:b:c is stored as a or (b or c), its second child is b:c. Use (url->list u) and (list->url l) to convert between alternatives and lists.
(url-none)
(url-any)
(url-wildcard pattern)
(url-parent)
(url-ancestor)
Respectively the url which matches nothing (tested by url-none?), the url which matches anything, a file name pattern with * wildcards (such as "*.tm"), the parent directory .., an arbitrary ancestor directory …, and the current working directory.
(url-temp)
(url-temp-dir)
Return a fresh temporary file name, the directory for temporary files, and the first url prefixjpostfix with j⩾i which does not exist in the TeXmacs scratch directory $TEXMACS_HOME_PATH/texts/scratch (created if needed) (this is used for the names of new unnamed buffers, which satisfy url-scratch?).
Opens a new buffer with the contents of the resource at u. This can be either a full URL or a file path, absolute or relative to the current buffer-master. Both types of argument accept parameters. The second, optional argument, is an optional path for the cursor history.
You can pass parameters in u in two ways: appending a hash # and some text, like in some/path/some-file.tm#blah will open the file and jump to the first label of name blah found, if any; this works for TeXmacs, generic and Html files. The other possibility is the usual way in the web: append a question mark ? followed by pairs parameter=value. The parameters line, column and select, which respectively jump to the chosen location and select the given text at that line, are supported for the source code formats (Scheme, C++, Java, Scala, Python and Julia; see source-file-post in
link-navigate.scm
). The parameters are ignored for tmfs urls.
(url-concat? "a/b")
Scheme]
(url-concat? "file.ext")
Scheme]
(url-or? "a/b:c")
Scheme]
(url-or? "a/b")
Scheme]
On unix systems, the colon separates alternatives in the system format; on Windows, the semicolon is used instead. See also url-atomic?, which tests whether a url consists of a single name, and url-expand below.
Return #t if the url is absolute. Absolute urls may be for instance full paths in the file system or internet URLs starting with a protocol specification like ftp or http. The tmfs urls are also understood to be rooted. See also url-rooted-tmfs?, url-rooted-web? and url-rooted-protocol?.
Return #t if u is an URL of the form tmfs://…. Such URLs designate documents generated by Scheme handlers; see the TeXmacs file system. The variant (url-rooted-tmfs-protocol? u class) also checks that the first component after tmfs:// is class.
Test whether u equals or descends from base, in a purely syntactic way (the urls are not resolved). If base is an alternative, it suffices that u descends from one of them; if u is an alternative, all of them must descend from base. The predicate (url-secure? u) checks whether u descends from the $TEXMACS_SECURE_PATH.
(url-descends? "/a/b/c.tm" "/a")
Scheme]
Applies only to filesystem urls. Returns #t if the url is a regular file, #f otherwise. See also url-directory? and url-link?.
(url-regular? "$TEXMACS_PATH/LICENSE")
Scheme]
Scheme]
Applies only to filesystem urls. Returns #t if the url is a directory and it exists, #f otherwise.
(url-directory? "/tmp")
Scheme]
(url-directory? "/tmp_not_exist")
Scheme]
(url-directory? "$TEXMACS_PATH/LICENSE")
Scheme]
Scheme]
Applies only to filesystem urls. Returns #t if the url is a symbolic link, #f otherwise.
The filter is a string of letters, all of which must be satisfied: f (regular file), d (directory), l (symbolic link), r, w, x (readable, writable, executable). Other letters are ignored, and the empty filter always succeeds.
(url-exists? u)
Test whether u resolves to an existing file, resp. whether an executable of name u exists in the $PATH.
(url-newer? u1 u2)
(url-last-modified u)
Test whether u1 was modified more recently than u2; return the time of the last modification of u (in seconds since the epoch); return the size of the file u in bytes.
(url-head "/tmp")
Scheme]
(url-head "/tmp/a.out")
Scheme]
(url-tail "/tmp")
Scheme]
(url-tail "/tmp/hello.tm")
Scheme]
Determine the file format of u (such as "texmacs", "latex" or "generic") from its suffix; for a tmfs url, the format is given by the handler of its class (see tmfs-format).
(url-suffix "/tmp/hello.tm")
Scheme]
(url-basename "/tmp")
Scheme]
(url-basename "/tmp/hello.tm")
Scheme]
(url-glue (url-basename (current-buffer)) ".new")
Scheme]
(url-unglue (current-buffer) 3) ;output edited
Scheme]
(url-relative "/a/b/c.tm" "d.tm")
Scheme]
(url-delta "/a/b/c/file.tm" "/a/b")
Scheme]
(url-root (current-buffer))
Scheme]
(url-root "https://www.texmacs.org")
Scheme]
(url-unroot "/a/b/c")
Scheme]
(url-unroot "https://www.texmacs.org")
Scheme]
(url-expand u)
The routine url-expand rewrites a/(b:c) into a/b:a/c, and also expands the ancestor pattern …. The routine url-factor is the inverse operation; it also sorts the alternatives.
(url-expand (url-append "a" (url-or "b"
"c")))
Scheme]
The C++ routines reroot (change the protocol of a url) and sort (order the alternatives of a url) are not exported to Scheme.
(string-load u)
(string-save s u)
Return the contents of the file u as a string, resp. write the string s into u, or append it at its end. Saving to a tmfs url goes through the handler of its class (see the TeXmacs file system), and appending to a tmfs url is a fatal error.
(system-copy from to)
(system-move from to)
(system-remove u)
(system-mkdir u)
(system-rmdir u)
Copy, move or remove files, create or remove a directory (which must be empty for system-rmdir), and remove a directory with all its contents. The routine (url-remove u) removes a file, through the handler of its class for a tmfs url.
(url-autosave u suffix)
(url-wrap u)
(url-backup u)
The routine url-autosave returns the url where u is autosaved, u followed by suffix, or #f when the file cannot be autosaved (for a tmfs url, the handler decides). The routine url-wrap returns the url of the file wrapped by a tmfs url, if any. The routine url-backup returns the name of the backup of u in $TEXMACS_HOME_PATH/texts/backup, and url-backup? tests whether a url is such a backup. These routines are defined in
tm-files.scm
and
file.cpp
.
Remove the remote url u from the cache of downloaded files, so that it is downloaded again the next time it is used.
The following routines take urls with alternatives and wildcards and look up the matching files. Here filter is a string as for url-test?; the default filter in C++ is "fr" (existing readable regular files).
Return the alternative of all files which match u and satisfy filter. For instance, (url-read-directory dir pattern) is implemented using url-complete and returns the list of files in dir matching the wildcard pattern:
(url-read-directory "$TEXMACS_PATH/styles" "*.ts") |
Like url-complete, but only return the first match, or (url-none) if there is none. The variant (url-resolve-in-path u) looks for an executable in the $PATH.
Look for a file called name in dir and its subdirectories; if there is none, look in the parent directory, and so on. The search goes up only while an ancestor directory has a name in the list of strings stops, and never beyond such a directory. The result is (url-none) if nothing is found.
Return the alternative of the files matching u which contain the string what (the results are cached).
(url-resolve-pattern u)
Find a background pattern, also in the directories of $TEXMACS_PATTERN_PATH, resp. test whether a file can be found by the TeX tools (kpsewhich).
(url-concretize u)
The routine url-concretize returns the system specific file name for a resolved url; for remote urls this may involve downloading the file into a temporary file. The routine url-materialize first resolves u using filter and then concretizes the result. The variant url-concretize* returns a url instead of a string, url-sys-concretize returns a string which is escaped for use in shell commands, and system-url->string is the same as url-sys-concretize.