use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.
the class TextEditingTargetCompilePdfHelper method checkCompilers.
public void checkCompilers(final WarningBarDisplay display, TextFileType fileType) {
// for all tex files we need to parse magic comments and validate
// any explict latex proram directive
ArrayList<TexMagicComment> magicComments = null;
if (fileType.canCompilePDF()) {
magicComments = TexMagicComment.parseComments(docDisplay_.getCode());
String latexProgramDirective = detectLatexProgramDirective(magicComments);
if (latexProgramDirective != null) {
if (latexProgramRegistry_.findTypeIgnoreCase(latexProgramDirective) == null) {
// show warning and bail
display.showWarningBar("Unknown LaTeX program type '" + latexProgramDirective + "' specified (valid types are " + latexProgramRegistry_.getPrintableTypeNames() + ")");
return;
}
}
}
// for Rnw we first determine the RnwWeave type
RnwWeave rnwWeave = null;
RnwWeaveDirective rnwWeaveDirective = null;
if (fileType.isRnw()) {
rnwWeaveDirective = detectRnwWeaveDirective(magicComments);
if (rnwWeaveDirective != null) {
rnwWeave = rnwWeaveDirective.getRnwWeave();
if (rnwWeave == null) {
// show warning and bail
display.showWarningBar("Unknown Rnw weave method '" + rnwWeaveDirective.getName() + "' specified (valid types are " + rnwWeaveRegistry_.getPrintableTypeNames() + ")");
return;
}
} else {
rnwWeave = rnwWeaveRegistry_.findTypeIgnoreCase(prefs_.defaultSweaveEngine().getValue());
}
}
final SessionInfo sessionInfo = session_.getSessionInfo();
TexCapabilities texCap = sessionInfo.getTexCapabilities();
final boolean checkForTeX = fileType.canCompilePDF() && !texCap.isTexInstalled();
final boolean checkForRnwWeave = (rnwWeave != null) && !texCap.isRnwWeaveAvailable(rnwWeave);
if (checkForTeX || checkForRnwWeave) {
// alias variables to finals
final boolean hasRnwWeaveDirective = rnwWeaveDirective != null;
final RnwWeave fRnwWeave = rnwWeave;
server_.getTexCapabilities(new ServerRequestCallback<TexCapabilities>() {
@Override
public void onResponseReceived(TexCapabilities response) {
if (checkForTeX && !response.isTexInstalled()) {
String warning;
if (Desktop.isDesktop())
warning = "No TeX installation detected. Please install " + "TeX before compiling.";
else
warning = "This server does not have TeX installed. You " + "may not be able to compile.";
display.showWarningBar(warning);
} else if (checkForRnwWeave && !response.isRnwWeaveAvailable(fRnwWeave)) {
String forContext = "";
if (hasRnwWeaveDirective)
forContext = "this file";
else if (sessionInfo.getActiveProjectFile() != null)
forContext = "Rnw files for this project";
else
forContext = "Rnw files";
display.showWarningBar(fRnwWeave.getName() + " is configured to weave " + forContext + " " + "however the " + fRnwWeave.getPackageName() + " package is not installed.");
} else {
display.hideWarningBar();
}
}
@Override
public void onError(ServerError error) {
Debug.logError(error);
}
});
} else {
display.hideWarningBar();
}
}
use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.
the class Source method onNewSweaveDoc.
@Handler
public void onNewSweaveDoc() {
// set concordance value if we need to
String concordance = new String();
if (uiPrefs_.alwaysEnableRnwConcordance().getValue()) {
RnwWeave activeWeave = rnwWeaveRegistry_.findTypeIgnoreCase(uiPrefs_.defaultSweaveEngine().getValue());
if (activeWeave.getInjectConcordance())
concordance = "\\SweaveOpts{concordance=TRUE}\n";
}
final String concordanceValue = concordance;
// show progress
final ProgressIndicator indicator = new GlobalProgressDelayer(globalDisplay_, 500, "Creating new document...").getIndicator();
// get the template
server_.getSourceTemplate("", "sweave.Rnw", new ServerRequestCallback<String>() {
@Override
public void onResponseReceived(String templateContents) {
indicator.onCompleted();
// add in concordance if necessary
final boolean hasConcordance = concordanceValue.length() > 0;
if (hasConcordance) {
String beginDoc = "\\begin{document}\n";
templateContents = templateContents.replace(beginDoc, beginDoc + concordanceValue);
}
newDoc(FileTypeRegistry.SWEAVE, templateContents, new ResultCallback<EditingTarget, ServerError>() {
@Override
public void onSuccess(EditingTarget target) {
int startRow = 4 + (hasConcordance ? 1 : 0);
target.setCursorPosition(Position.create(startRow, 0));
}
});
}
@Override
public void onError(ServerError error) {
indicator.onError(error.getUserMessage());
}
});
}
use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.
the class TextEditingTarget method sourceActiveDocument.
private void sourceActiveDocument(final boolean echo) {
docDisplay_.focus();
// If the document being sourced is a Shiny file, run the app instead.
if (fileType_.isR() && extendedType_.startsWith(SourceDocument.XT_SHINY_PREFIX)) {
runShinyApp();
return;
}
// if the document is an R Markdown notebook, run all its chunks instead
if (fileType_.isRmd() && isRmdNotebook()) {
onExecuteAllCode();
return;
}
// If the document being sourced is a script then use that codepath
if (fileType_.isScript()) {
runScript();
return;
}
// If the document is previewable
if (fileType_.canPreviewFromR()) {
previewFromR();
return;
}
String code = docDisplay_.getCode();
if (code != null && code.trim().length() > 0) {
// R 2.14 prints a warning when sourcing a file with no trailing \n
if (!code.endsWith("\n"))
code = code + "\n";
boolean sweave = fileType_.canCompilePDF() || fileType_.canKnitToHTML() || fileType_.isRpres();
RnwWeave rnwWeave = compilePdfHelper_.getActiveRnwWeave();
final boolean forceEcho = sweave && (rnwWeave != null) ? rnwWeave.forceEchoOnExec() : false;
// NOTE: we always set echo to true for knitr because knitr doesn't
// require print statements so if you don't echo to the console
// then you don't see any of the output
boolean saveWhenSourcing = fileType_.isCpp() || docDisplay_.hasBreakpoints() || (prefs_.saveBeforeSourcing().getValue() && (getPath() != null) && !sweave);
if ((dirtyState_.getValue() || sweave) && !saveWhenSourcing) {
server_.saveActiveDocument(code, sweave, compilePdfHelper_.getActiveRnwWeaveName(), new SimpleRequestCallback<Void>() {
@Override
public void onResponseReceived(Void response) {
consoleDispatcher_.executeSourceCommand("~/.active-rstudio-document", fileType_, "UTF-8", activeCodeIsAscii(), forceEcho ? true : echo, prefs_.focusConsoleAfterExec().getValue(), docDisplay_.hasBreakpoints());
}
});
} else {
Command sourceCommand = new Command() {
@Override
public void execute() {
if (docDisplay_.hasBreakpoints()) {
hideBreakpointWarningBar();
}
consoleDispatcher_.executeSourceCommand(getPath(), fileType_, docUpdateSentinel_.getEncoding(), activeCodeIsAscii(), forceEcho ? true : echo, prefs_.focusConsoleAfterExec().getValue(), docDisplay_.hasBreakpoints());
}
};
if (saveWhenSourcing && (dirtyState_.getValue() || (getPath() == null)))
saveThenExecute(null, sourceCommand);
else
sourceCommand.execute();
}
}
// update pref if necessary
if (prefs_.sourceWithEcho().getValue() != echo) {
prefs_.sourceWithEcho().setGlobalValue(echo, true);
prefs_.writeUIPrefs();
}
}
use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.
the class TextEditingTargetCompilePdfHelper method getActiveRnwWeave.
// get the currently active rnw weave method -- note this can return
// null in the case that there is an embedded directive which is invalid
public RnwWeave getActiveRnwWeave() {
if (docDisplay_.getFileType().canKnitToHTML())
return rnwWeaveRegistry_.findTypeIgnoreCase("knitr");
RnwWeave weave = null;
ArrayList<TexMagicComment> magicComments = TexMagicComment.parseComments(docDisplay_.getCode());
RnwWeaveDirective rnwWeaveDirective = detectRnwWeaveDirective(magicComments);
if (rnwWeaveDirective != null) {
weave = rnwWeaveDirective.getRnwWeave();
} else {
weave = rnwWeaveRegistry_.findTypeIgnoreCase(prefs_.defaultSweaveEngine().getValue());
}
// look for the 'driver' directive and don't inject
// concocordance if there is a custom driver
String driver = detectRnwDriverDirective(magicComments);
if (driver != null)
return RnwWeave.withNoConcordance(weave);
else
return weave;
}
use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.
the class TextEditingTargetCompilePdfHelper method ensureRnwConcordance.
public void ensureRnwConcordance() {
RnwWeave rnwWeave = getActiveRnwWeave();
if ((rnwWeave != null) && rnwWeave.getInjectConcordance()) {
if (!hasConcordanceDirective(docDisplay_.getCode())) {
InputEditorSelection doc = docDisplay_.search("\\\\begin{document}", // backwards
false, // wrap
true, // case sensitive
false, // whole word
false, // from selection
null, // range (search all)
null, // regexp mode
true);
if (doc != null) {
InputEditorPosition pos = doc.getEnd().moveToNextLine();
docDisplay_.insertCode(pos, "\\SweaveOpts{concordance=TRUE}\n");
}
}
}
}
Aggregations