Rev 50012 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/utils/man/rcompgen.Rd% Part of the R package, http://www.R-project.org% Copyright 2008-2009 R Core Development Team% Distributed under GPL 2 or later\name{rcompgen}\alias{rcompgen}\alias{rc.settings}\alias{rc.status}\alias{rc.getOption}\alias{rc.options}\alias{.DollarNames}\alias{.DollarNames.default}\alias{.DollarNames.list}\alias{.DollarNames.environment}\title{A Completion Generator for R}\description{This package provides a mechanism to generate relevant completionsfrom a partially completed command line. It is not intended to beuseful by itself, but rather in conjunction with other mechanisms thatuse it as a backend. The functions listed in the usage sectionprovide a simple control and query mechanism. The actual interfaceconsists of a few unexported functions described further down.}\usage{rc.settings(ops, ns, args, func, ipck, S3, data, help,argdb, files)rc.status()rc.getOption(name)rc.options(\dots).DollarNames(x, pattern)\S3method{.DollarNames}{default}(x, pattern = "")\S3method{.DollarNames}{list}(x, pattern = "")\S3method{.DollarNames}{environment}(x, pattern = "")}\arguments{\item{ops, ns, args, func, ipck, S3, data, help, argdb, files}{logical, turning some optional completion features on and off.\describe{\item{\code{ops}:}{activates completion after the \code{$} and\code{@} operators}\item{\code{ns}:}{controls name space related completions}\item{\code{args}:}{enables completion of function arguments}\item{\code{func}:}{enables detection of functions. If enabled,a customizable extension (\code{"("} by default) is appended tofunction names. The process of determining whether a potentialcompletion is a function requires evaluation, including for lazyloaded symbols. This is extremely undesirable for largeobjects, because of potentially wasteful use of memory inaddition to the time overhead associated with loading. For thisreason, this feature is disabled by default. }\item{\code{S3}:}{ when \code{args=TRUE}, activates completion onarguments of all S3 methods (otherwise just the generic, whichusually has very few arguments) }\item{\code{ipck}:}{ enables completion of installed package namesinside \code{\link{library}} and \code{\link{require}} }\item{\code{data}:}{ enables completion of data sets (includingthose already visible) inside \code{\link{data}} }\item{\code{help}:}{ enables completion of help requests startingwith a question mark, by looking inside help index files }\item{\code{argdb}:}{ when \code{args=TRUE}, completion isattempted on function arguments. Generally, the list of validarguments is determined by dynamic calls to \code{\link{args}}.While this gives results that are technically correct, the useof the \code{\dots} argument often hides some useful arguments.To give more flexibility in this regard, an optional table ofvalid arguments names for specific functions is retainedinternally. Setting \code{argdb=TRUE} enables preferentiallookup in this internal data base for functions with an entry init. Of course, this is useful only when the data base containsinformation about the function of interest. Some functions areincluded in the package (the maintainer is happy to add moreupon request), and more can be added by the user through theunexported function \code{.addFunctionInfo} (see below).}\item{\code{files}:}{ enables filename completion in R code. Thisis initially set to \code{FALSE}, in which case the underlyingcompletion front-end can take over (and hopefully do a betterjob than we would have done). For systems where no suchfacilities exist, this can be set to \code{TRUE} if file namecompletion is desired. This is currently experimental and maynot work very well. }}All settings are turned on by default except \code{ipck},\code{func} and \code{files}. Turn more off if your CPU cycles arevaluable; you will still retain basic completion on names of objectsin the search list. See below for additional details.}\item{name, \dots}{ user-settable options. Currently valid names are\describe{\item{\code{function.suffix}:}{ default \code{"("} }\item{\code{funarg.suffix}:}{ default \code{" = "} }\item{\code{package.suffix}}{ default \code{"::"} }}See \code{\link{options}} for detailed usage description}\item{x}{ An R object for which valid names after \code{"$"}are computed and returned.}\item{pattern}{ A regular expression. Only matching names arereturned.}}\details{There are several types of completion, some of which can be disabledusing \code{rc.settings}. The most basic level, which can not beturned off once the package is loaded, provides completion on namesvisible on the search path, along with a few special keywords(e.g. \code{TRUE}). This type of completion is not attempted if thepartial \sQuote{word} (a.k.a. token) being completed is empty (sincethere would be too many completions). The more advanced types ofcompletion are described below.\describe{\item{\bold{Completion after extractors \code{$} and \code{@}}:}{When the \code{ops} setting is turned on, completion after\code{$} and \code{@} is attempted. This requires the prefix tobe evaluated, which is attempted unless it involves an explicitfunction call (implicit function calls involving the use of\code{[}, \code{$}, etc \emph{do not} inhibit evaluation).Valid completions after the \code{$} extractor are determined bythe generic function \code{.DollarNames}. Some basic methods areprovided, and more can be written for custom classes.}\item{\bold{Completion inside name spaces}:}{When the \code{ns} setting is turned on, completion insidename spaces is attempted when a token is preceded by the \code{::}or \code{:::} operators. Additionally, the basic completionmechanism is extended to include attached name spaces, or moreprecisely, \code{foopkg::} becomes a valid completion of\code{foo} if the return value of \code{\link{search}()} includesthe string \code{"package:foopkg"}.The completion of package name spaces applies only to attachedpackages, i.e. if \code{MASS} is not attached (whether or not itis loaded), \code{MAS} will not complete to \code{MASS::}.However, attempted completion \emph{inside} an apparent name spacewill attempt to load the name space if it is not already loaded,e.g. trying to complete on \code{MASS::fr} will load \code{MASS}(but not necessarily attach it) even if it is not already loaded.}\item{\bold{Completion of function arguments}:}{When the \code{args} setting is turned on, completion on functionarguments is attempted whenever deemed appropriate. The mechanismused will currently fail if the relevant function (at the pointwhere completion is requested) was entered on a previous prompt(which implies in particular that the current line is being typedin response to a continuation prompt, usually \code{+}). Notethat separation by newlines is fine.The list of possible argument completions that is generated can bemisleading. There is no problem for non-generic functions (exceptthat \code{\dots} is listed as a completion; this is intentionalas it signals the fact that the function can accept furtherarguments). However, for generic functions, it is practicallyimpossible to give a reliable argument list without evaluatingarguments (and not even then, in some cases), which is risky (inaddition to being difficult to code, which is the real reason ithasn't even been tried), especially when that argument is itselfan inline function call. Our compromise is to consider argumentsof \emph{all} currently available methods of that generic. Thishas two drawbacks. First, not all listed completions may beappropriate in the call currently being constructed. Second, forgenerics with many methods (like \code{print} and \code{plot}),many matches will need to be considered, which may take anoticeable amount of time. Despite these drawbacks, we believethis behaviour to be more useful than the only other practicalalternative, which is to list arguments of the generic only.Only S3 methods are currently supported in this fashion, and thatcan be turned off using the \code{S3} setting.Since arguments can be unnamed in \R function calls, other typesof completion are also appropriate whenever argument completionis. Since there are usually many many more visible objects thanformal arguments of any particular function, possible argumentcompletions are often buried in a bunch of other possibilities.However, recall that basic completion is suppressed for blanktokens. This can be useful to list possible arguments of afunction. For example, trying to complete \code{seq([TAB]} and\code{seq(from = 1, [TAB])} will both list only the arguments of\code{seq} (or any of its methods), whereas trying to complete\code{seq(length[TAB]} will list both the \code{length.out}argument and the \code{length(} function as possible completions.Note that no attempt is made to remove arguments already supplied,as that would incur a further speed penalty.}\item{\bold{Special functions}:}{For a few special functions (\code{\link{library}},\code{\link{data}}, etc), the first argument is treated specially,in the sense that normal completion is suppressed, and somefunction specific completions are enabled if so requested by thesettings. The \code{ipck} setting, which controls whether\code{\link{library}} and \code{\link{require}} will complete on\emph{installed packages}, is disabled by default because thefirst call to \code{\link{installed.packages}} is potentially timeconsuming (e.g. when packages are installed on a remote networkfile server). Note, however, that the results of a call to\code{\link{installed.packages}} is cached, so subsequent callsare usually fast, so turning this option on is not particularlyonerous even in such situations.}}}\value{\code{rc.status} returns, as a list, the contents of an internal(unexported) environment that is used to record the results of thelast completion attempt. This can be useful for debugging. For suchuse, one must resist the temptation to use completion when typing thecall to \code{rc.status} itself, as that then becomes the last attemptby the time the call is executed.The items of primary interest in the returned list are:\item{comps}{ the possible completions generated by the lastcall to \code{.completeToken}, as a character vector }\item{token}{ the token that was (or, is to be) completed, asset by the last call to \code{.assignToken} (possibly inside a callto \code{.guessTokenFromLine}) }\item{linebuffer}{ the full line, as set by the last call to\code{.assignLinebuffer}}\item{start}{ the start position of the token in the linebuffer, as set by the last call to \code{.assignStart} }\item{end}{ the end position of the token in the linebuffer, as set by the last call to \code{.assignEnd} }\item{fileName}{ logical, indicating whether the cursor iscurrently inside quotes. If so, no completion is attempted. Areasonable default behaviour for the backend in that case is to fallback to filename completion. }\item{fguess}{ the name of the function \code{rcompgen} thinksthe cursor is currently inside }\item{isFirstArg}{ logical. If cursor is inside a function, is it thefirst argument? }In addition, the components \code{settings} and \code{options} givethe current values of settings and options respectively.\code{rc.getOption} and \code{rc.options} behave much like\code{\link{getOption}} and \code{\link{options}} respectively.}\section{Unexported API}{There are several unexported functions in the package. Of these,a few are special because they provide the API through which othermechanisms can make use of the facilities provided by this package(they are unexported because they are not meant to be called directlyby users). The usage of these functions are:\preformatted{.assignToken(text).assignLinebuffer(line).assignStart(start).assignEnd(end).completeToken().retrieveCompletions().getFileComp().guessTokenFromLine().win32consoleCompletion(linebuffer, cursorPosition,check.repeat = TRUE,minlength = -1).addFunctionInfo(...)}The first four functions set up a completion attempt by specifying thetoken to be completed (\code{text}), and indicating where(\code{start} and \code{end}, which should be integers) the token isplaced within the complete line typed so far (\code{line}).Potential completions of the token are generated by\code{.completeToken}, and the completions can be retrieved as an \Rcharacter vector using \code{.retrieveCompletions}.If the cursor is inside quotes, no completion is attempted. Thefunction \code{.getFileComp} can be used after a call to\code{.completeToken} to determine if this is the case (returns\code{TRUE}), and alternative completions generated as deemed useful.In most cases, filename completion is a reasonable fallback.The \code{.guessTokenFromLine} function is provided for use withbackends that do not already break a line into tokens. It requiresthe linebuffer and endpoint (cursor position) to be already set, anditself sets the token and the start position. It returns the token asa character string. (This is used by the ESS completion hook examplegiven in the \code{examples/altesscomp.el} file.)The \code{.win32consoleCompletion} is similar in spirit, but is moregeared towards the Windows GUI (or rather, any front-end that has nocompletion facilities of its own). It requires the linebufferand cursor position as arguments, and returns a list with threecomponents, \code{addition}, \code{possible} and \code{comps}. Ifthere is an unambiguous extension at the current position,\code{addition} contains the additional text that should be insertedat the cursor. If there is more than one possibility, these areavailable either as a character vector of preformatted strings in\code{possible}, or as a single string in \code{comps}.\code{possible} consists of lines formatted using the current\code{width} option, so that printing them on the console one line ata time will be a reasonable way to list them. \code{comps} is a spaceseparated (collapsed) list of the same completions, in case thefront-end wishes to display it in some other fashion.The \code{minlength} argument can be used to suppress completion whenthe token is too short (which can be useful if the front-end is set upto try completion on every keypress). If \code{check.repeat} is\code{TRUE}, it is detected if the same completion is being requestedmore than once in a row, and ambiguous completions are returned onlyin that case. This is an attempt to emulate GNU Readline behaviour,where a single TAB completes up to any unambiguous part, and multiplepossibilities are reported only on two consecutive TABs.As the various front-end interfaces evolve, the details of thesefunctions are likely to change as well.The function \code{.addFunctionInfo} can be used to add informationabout the permitted argument names for specific functions. Multiplenamed arguments are allowed in calls to it, where the tags are namesof functions and values are character vectors representing validarguments. When the \code{argdb} setting is \code{TRUE}, these areused as a source of valid argument names for the relevant functions.}\note{If you are uncomfortable with unsolicited evaluation of pieces ofcode, you should set \code{ops = FALSE}. Otherwise, trying tocomplete \code{foo@ba} will evaluate \code{foo}, trying to complete\code{foo[i,1:10]$ba} will evaluate \code{foo[i,1:10]}, etc. Thisshould not be too bad, as explicit function calls (involvingparentheses) are not evaluated in this manner. However, this\emph{will} affect lazy loaded symbols (and presumably other promisetype thingies).}\author{ Deepayan Sarkar, \email{deepayan.sarkar@r-project.org} }\keyword{IO}\keyword{utilities}