The R Project SVN R

Rev

Rev 86027 | Details | Compare with Previous | Last modification | View Log | RSS feed

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