| 42333 |
ripley |
1 |
% File src/library/base/man/srcfile.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2011 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 39712 |
murdoch |
6 |
\name{srcfile}
|
| 56186 |
murdoch |
7 |
\alias{srcfile}
|
| 39739 |
murdoch |
8 |
\alias{srcfilecopy}
|
| 39712 |
murdoch |
9 |
\alias{getSrcLines}
|
|
|
10 |
\alias{srcref}
|
|
|
11 |
\alias{srcfile-class}
|
| 39739 |
murdoch |
12 |
\alias{srcfilecopy-class}
|
| 39712 |
murdoch |
13 |
\alias{srcref-class}
|
|
|
14 |
\alias{print.srcfile}
|
| 50981 |
murdoch |
15 |
\alias{summary.srcfile}
|
| 39712 |
murdoch |
16 |
\alias{open.srcfile}
|
| 39739 |
murdoch |
17 |
\alias{open.srcfilecopy}
|
| 39712 |
murdoch |
18 |
\alias{close.srcfile}
|
|
|
19 |
\alias{print.srcref}
|
| 50981 |
murdoch |
20 |
\alias{summary.srcref}
|
| 39712 |
murdoch |
21 |
\alias{as.character.srcref}
|
|
|
22 |
\alias{.isOpen}
|
| 56352 |
murdoch |
23 |
\alias{srcfilealias-class}
|
|
|
24 |
\alias{srcfilealias}
|
|
|
25 |
\alias{open.srcfilealias}
|
|
|
26 |
\alias{close.srcfilealias}
|
| 39712 |
murdoch |
27 |
|
|
|
28 |
\title{References to source files}
|
|
|
29 |
\description{
|
|
|
30 |
These functions are for working with source files.
|
|
|
31 |
}
|
|
|
32 |
\usage{
|
| 50829 |
ripley |
33 |
srcfile(filename, encoding = getOption("encoding"), Enc = "unknown")
|
| 58509 |
murdoch |
34 |
srcfilecopy(filename, lines, timestamp = Sys.time(), isFile = FALSE)
|
| 56352 |
murdoch |
35 |
srcfilealias(filename, srcfile)
|
| 39712 |
murdoch |
36 |
getSrcLines(srcfile, first, last)
|
|
|
37 |
srcref(srcfile, lloc)
|
| 40180 |
ripley |
38 |
\method{print}{srcfile}(x, \dots)
|
| 50981 |
murdoch |
39 |
\method{summary}{srcfile}(object, \dots)
|
| 40180 |
ripley |
40 |
\method{open}{srcfile}(con, line, \dots)
|
|
|
41 |
\method{close}{srcfile}(con, \dots)
|
|
|
42 |
\method{print}{srcref}(x, useSource = TRUE, \dots)
|
| 50981 |
murdoch |
43 |
\method{summary}{srcref}(object, useSource = FALSE, \dots)
|
| 40180 |
ripley |
44 |
\method{as.character}{srcref}(x, useSource = TRUE, \dots)
|
| 39712 |
murdoch |
45 |
.isOpen(srcfile)
|
|
|
46 |
}
|
|
|
47 |
\arguments{
|
| 57840 |
murdoch |
48 |
\item{filename}{ The name of a file. }
|
|
|
49 |
\item{encoding}{ The character encoding to assume for the file. }
|
| 50829 |
ripley |
50 |
\item{Enc}{ The encoding with which to make strings: see the
|
|
|
51 |
\code{encoding} argument of \code{\link{parse}}.}
|
|
|
52 |
\item{lines}{ A character vector of source lines. Other \R objects
|
|
|
53 |
will be coerced to character. }
|
| 57840 |
murdoch |
54 |
\item{timestamp}{ The timestamp to use on a copy of a file. }
|
| 58509 |
murdoch |
55 |
\item{isFile}{ Is this \code{srcfilecopy} known to come from a file system file? }
|
| 44184 |
ripley |
56 |
\item{srcfile}{ A \code{srcfile} object. }
|
|
|
57 |
\item{first, last, line}{ Line numbers. }
|
| 55161 |
murdoch |
58 |
\item{lloc}{ A vector of four, six or eight values giving a source location; see
|
| 44184 |
ripley |
59 |
\sQuote{Details}.}
|
| 50981 |
murdoch |
60 |
\item{x, object, con}{ An object of the appropriate class. }
|
| 50829 |
ripley |
61 |
\item{useSource}{ Whether to read the \code{srcfile} to obtain the
|
|
|
62 |
text of a \code{srcref}. }
|
| 44184 |
ripley |
63 |
\item{\dots}{ Additional arguments to the methods; these will be ignored. }
|
| 39712 |
murdoch |
64 |
}
|
|
|
65 |
\details{
|
| 44184 |
ripley |
66 |
These functions and classes handle source code references.
|
| 39712 |
murdoch |
67 |
|
| 44184 |
ripley |
68 |
The \code{srcfile} function produces an object of class
|
|
|
69 |
\code{srcfile}, which contains the name and directory of a source code
|
|
|
70 |
file, along with its timestamp, for use in source level debugging (not
|
|
|
71 |
yet implemented) and source echoing. The encoding of the file is
|
|
|
72 |
saved; see \code{\link{file}} for a discussion of encodings, and
|
|
|
73 |
\code{\link{iconvlist}} for a list of allowable encodings on your platform.
|
| 39712 |
murdoch |
74 |
|
| 44184 |
ripley |
75 |
The \code{srcfilecopy} function produces an object of the descendant
|
|
|
76 |
class \code{srcfilecopy}, which saves the source lines in a character
|
| 58509 |
murdoch |
77 |
vector. It copies the value of the \code{isFile} argument, to help
|
|
|
78 |
debuggers identify whether this text comes from a real file in the
|
|
|
79 |
file system.
|
| 39739 |
murdoch |
80 |
|
| 56352 |
murdoch |
81 |
The \code{srcfilealias} function produces an object of the descendant
|
| 61433 |
ripley |
82 |
class \code{srcfilealias}, which gives an alternate name to another
|
| 56352 |
murdoch |
83 |
srcfile. This is produced by the parser when a \code{#line} directive
|
|
|
84 |
is used.
|
| 61433 |
ripley |
85 |
|
| 44184 |
ripley |
86 |
The \code{getSrcLines} function reads the specified lines from
|
|
|
87 |
\code{srcfile}.
|
| 39712 |
murdoch |
88 |
|
| 50981 |
murdoch |
89 |
The \code{srcref} function produces an object of class
|
| 55161 |
murdoch |
90 |
\code{srcref}, which describes a range of characters in a \code{
|
| 61173 |
ripley |
91 |
srcfile}. The \code{lloc} value gives the following values:
|
|
|
92 |
\preformatted{c(first_line, first_byte, last_line, last_byte, first_column,
|
|
|
93 |
last_column, first_parsed, last_parsed)
|
|
|
94 |
}. Bytes (elements 2, 4) and
|
| 55161 |
murdoch |
95 |
columns (elements 5, 6) may be different due to multibyte
|
|
|
96 |
characters. If only four values are given, the columns and bytes
|
|
|
97 |
are assumed to match. Lines (elements 1, 3) and parsed lines (
|
|
|
98 |
elements 7, 8) may differ if a \code{#line} directive is used in
|
|
|
99 |
code: the former will respect the directive, the latter will just
|
|
|
100 |
count lines. If only 4 or 6 elements are given, the parsed lines
|
|
|
101 |
will be assumed to match the lines.
|
| 39712 |
murdoch |
102 |
|
| 50981 |
murdoch |
103 |
Methods are defined for \code{print}, \code{summary}, \code{open},
|
|
|
104 |
and \code{close} for classes \code{srcfile} and \code{srcfilecopy}.
|
| 54236 |
ripley |
105 |
The \code{open} method opens its internal \code{\link{file}} connection at
|
| 50981 |
murdoch |
106 |
a particular line; if it was already open, it will be repositioned
|
|
|
107 |
to that line.
|
| 39712 |
murdoch |
108 |
|
| 61433 |
ripley |
109 |
Methods are defined for \code{print}, \code{summary} and
|
| 55161 |
murdoch |
110 |
\code{as.character} for class \code{srcref}. The \code{as.character}
|
|
|
111 |
method will read the associated source file to obtain the text
|
|
|
112 |
corresponding to the reference. The exact behaviour depends on the
|
|
|
113 |
class of the source file. If the source file inherits from
|
| 61433 |
ripley |
114 |
class \code{"srcfilecopy"}, the lines are taken from the saved copy
|
| 55161 |
murdoch |
115 |
using the "parsed" line counts. If not, an attempt
|
| 61433 |
ripley |
116 |
is made to read the file, and the original line numbers of the
|
| 55161 |
murdoch |
117 |
\code{srcref} record (i.e. elements 1 and 3) are used. If an error
|
| 61433 |
ripley |
118 |
occurs (e.g. the file no longer exists), text like
|
| 55161 |
murdoch |
119 |
\code{<srcref: "file" chars 1:1 to 2:10>} will be returned instead,
|
|
|
120 |
indicating the \code{line:column} ranges of the first and last
|
|
|
121 |
character. The \code{summary} method defaults to this type of
|
| 61433 |
ripley |
122 |
display.
|
| 39712 |
murdoch |
123 |
|
| 48876 |
murdoch |
124 |
Lists of \code{srcref} objects may be attached to expressions as the
|
|
|
125 |
\code{"srcref"} attribute. (The list of \code{srcref} objects should be the same
|
| 44184 |
ripley |
126 |
length as the expression.) By default, expressions are printed by
|
|
|
127 |
\code{\link{print.default}} using the associated \code{srcref}. To
|
|
|
128 |
see deparsed code instead, call \code{\link{print}} with argument
|
|
|
129 |
\code{useSource = FALSE}. If a \code{srcref} object
|
|
|
130 |
is printed with \code{useSource = FALSE}, the \code{<srcref: \dots>}
|
|
|
131 |
record will be printed.
|
| 39712 |
murdoch |
132 |
|
| 44184 |
ripley |
133 |
\code{.isOpen} is intended for internal use: it checks whether the
|
|
|
134 |
connection associated with a \code{srcfile} object is open.
|
| 39712 |
murdoch |
135 |
}
|
|
|
136 |
\value{
|
|
|
137 |
\code{srcfile} returns a \code{srcfile} object.
|
|
|
138 |
|
| 39739 |
murdoch |
139 |
\code{srcfilecopy} returns a \code{srcfilecopy} object.
|
|
|
140 |
|
| 39712 |
murdoch |
141 |
\code{getSrcLines} returns a character vector of source code lines.
|
|
|
142 |
|
|
|
143 |
\code{srcref} returns a \code{srcref} object.
|
|
|
144 |
}
|
| 59309 |
murdoch |
145 |
\seealso{
|
|
|
146 |
\code{\link{getSrcFilename}} for extracting information from a source reference.
|
|
|
147 |
}
|
| 39712 |
murdoch |
148 |
\author{Duncan Murdoch}
|
| 51729 |
ripley |
149 |
\examples{\donttest{ # has timestamp
|
| 39712 |
murdoch |
150 |
src <- srcfile(system.file("DESCRIPTION", package = "base"))
|
| 50981 |
murdoch |
151 |
summary(src)
|
| 39712 |
murdoch |
152 |
getSrcLines(src, 1, 4)
|
|
|
153 |
ref <- srcref(src, c(1, 1, 2, 1000))
|
|
|
154 |
ref
|
| 39716 |
murdoch |
155 |
print(ref, useSource = FALSE)
|
| 51729 |
ripley |
156 |
}}
|
| 39716 |
murdoch |
157 |
|
| 39712 |
murdoch |
158 |
\keyword{ debugging }
|
|
|
159 |
\keyword{ utilities }
|