Skip to content

Commit

Permalink
Add "Source Active Document" command
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed May 2, 2011
1 parent 8a00f11 commit 093930a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
Expand Up @@ -96,7 +96,10 @@ public HashSet<AppCommand> getSupportedCommands(Commands commands)
results.add(commands.commentUncomment());
}
if (canExecuteAllCode())
{
results.add(commands.executeAllCode());
results.add(commands.sourceActiveDocument());
}
if (canCompilePDF())
{
results.add(commands.compilePDF());
Expand Down
Expand Up @@ -77,6 +77,8 @@ well as menu structures (for main menu and popup menus).
<cmd refid="executeCode"/>
<cmd refid="executeAllCode"/>
<separator/>
<cmd refid="sourceActiveDocument"/>
<separator/>
<cmd refid="extractFunction"/>
<cmd refid="commentUncomment"/>
<separator/>
Expand Down Expand Up @@ -192,6 +194,7 @@ well as menu structures (for main menu and popup menus).
<shortcut refid="compilePDF" value="Cmd+Shift+P"/>
<shortcut refid="executeCode" value="Cmd+Enter"/>
<shortcut refid="executeAllCode" value="Cmd+Shift+Enter"/>
<shortcut refid="sourceActiveDocument" value="Cmd+Shift+S"/>
<shortcut refid="extractFunction" value="Cmd+Shift+F"/>
<shortcut refid="commentUncomment" value="Cmd+/"/>
<shortcut refid="consoleClear" value="Ctrl+L"/>
Expand Down Expand Up @@ -335,6 +338,9 @@ well as menu structures (for main menu and popup menus).
<cmd id="extractFunction"
menuLabel="E_xtract Function"
desc="Turn the current selection into a function"/>
<cmd id="sourceActiveDocument"
menuLabel="_Source Active Document"
desc="Sources the contents of the active document"/>
<cmd id="commentUncomment"
menuLabel="C_omment/Uncomment Lines"
desc="Comment or uncomment the current line/selection"/>
Expand Down
Expand Up @@ -33,6 +33,7 @@ public abstract class Commands extends CommandBundle
public abstract AppCommand closeSourceDoc();
public abstract AppCommand closeAllSourceDocs();
public abstract AppCommand executeAllCode();
public abstract AppCommand sourceActiveDocument();
public abstract AppCommand executeCode();
public abstract AppCommand compilePDF();
public abstract AppCommand publishPDF();
Expand Down
Expand Up @@ -150,6 +150,7 @@ public Source(Commands commands,
dynamicCommands_.add(commands.printSourceDoc());
dynamicCommands_.add(commands.executeCode());
dynamicCommands_.add(commands.executeAllCode());
dynamicCommands_.add(commands.sourceActiveDocument());
dynamicCommands_.add(commands.compilePDF());
dynamicCommands_.add(commands.publishPDF());
dynamicCommands_.add(commands.popoutDoc());
Expand Down
Expand Up @@ -999,6 +999,31 @@ private static String stangle(String sweaveStr)
return sweaveStr;
}

@Handler
void onSourceActiveDocument()
{
// Stops the console from thinking it has focus, and thus stealing it
Element activeEl = DomUtils.getActiveElement();
if (activeEl != null)
activeEl.blur();

String code = docDisplay_.getCode();
if (code != null && code.trim().length() > 0)
{
boolean sweave = fileType_.canCompilePDF();

server_.saveActiveDocument(code, sweave, new SimpleRequestCallback<Void>() {
@Override
public void onResponseReceived(Void response)
{
events_.fireEvent(new SendToConsoleEvent(
createSourceCommand("~/.active-rstudio-document", "UTF-8"),
true));
}
});
}
}

private String normalizeEncoding(String str)
{
return StringUtil.notNull(str).replaceAll("[- ]", "").toLowerCase();
Expand Down Expand Up @@ -1026,12 +1051,12 @@ private String createSourceCommand(String path, String encoding)
"'";

if (isAscii || isSystemEncoding)
return "source(" + escapedPath + ");";
return "source(" + escapedPath + ")";
else
{
return "source.with.encoding(" + escapedPath + ", encoding='" +
(!StringUtil.isNullOrEmpty(encoding) ? encoding : "UTF-8") +
"');";
"')";
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/gwt/www/docs/keyboard.htm
Expand Up @@ -131,10 +131,15 @@ <h2>Keyboard Shortcuts</h2>
<td>Command+Enter</td>
</tr>
<tr>
<td>Run current document</td>
<td>Run active document</td>
<td>Ctrl+Shift+Enter</td>
<td>Command+Shift+Enter</td>
</tr>
<tr>
<td>Source active document</td>
<td>Ctrl+Shift+S</td>
<td>Command+Shift+S</td>
</tr>
<tr>
<td>Switch to tab</td>
<td>Ctrl+Alt+Down</td>
Expand Down

0 comments on commit 093930a

Please sign in to comment.