jEdit Community - Resources for users of the jEdit Text Editor
Run shell command; return to text area
Submitted by grantciv on Monday, 29 November, 2010 - 00:28
I'm having trouble writing a macro that runs a shell command and returns to the text area. The ultimate goal is to have a macro that saves all the buffers (without confirmation), runs the current buffer in an appropriate interpreter (python, e.g.), and returns to the text area. Really, if focus could simply remain in the text area, that would be great.

The trouble I'm having is that the view.prevTextArea() call is either being executed too early or it's simply not working the way I understood. The focus ends up in the System shell area. My code is below. I think the only important lines are the three last ones, but I'm including it all, just in case.

// This is a recorded macro. First, check over the
// commands to make sure this is what you intended. Then,
// save this buffer, and the macro should appear in the
// Macros menu.
filetypeToCompilers = new java.util.HashMap();
filetypeToCompilers.put(".py", "python -tt");
filetypeToCompilers.put(".java", "javac");

fileExtensionPattern = java.util.regex.Pattern.compile("(\\.[^.]*)$");
matcher = fileExtensionPattern.matcher(buffer.getName());
if (matcher.find())
{
	extension = matcher.group(1);
}
else
{
	Macros.error(view, "Unable to determine file type (missing extension?).");
	return;
}

compiler = filetypeToCompilers.get(extension);
if (compiler == null)
{
	Macros.message(view, "No compiler/interpreter defined for extension '" + 
	extension + "'");
	return;
}

runCommandInConsole(view,"System",compiler + ' ' + buffer.getPath());
waitForConsole(view);
view.prevTextArea();

Any thoughts?
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
tricks
by Robert Schwenn on Mon, 29/11/2010 - 19:59
1. save buffer:
if (buffer.isNewFile()) {
buffer.saveAs(view, true);
} else {
if (buffer.isDirty()) {
buffer.save(view, null);
}
}
// Wait for I/O operations
VFSManager.waitForRequests();

2. You could bypass the Console, if You wouldn't need it's error parsing:
wdir = buffer.getDirectory();
String[] environment = null;
Runtime.getRuntime().exec(command, environment, new File(wdir));

3. Close Console's dockable, if it's open. So, the text area gets the focus:
jEdit.getAction("console-toggle").invoke(view);

Robert
 
thanks
by grantciv on Tue, 30/11/2010 - 00:13

Robert, thanks for your reply. The "save" logic looks promising.

I do want the Console error parsing, so option 2 wouldn't be ideal, and similarly for option 3--the Console might well have information I wish to see.

Thanks again for your help. If you have some more ideas, I'd be happy to learn more of your tricks. I'm curious about why view.prevTextArea() works when I enter it manually, but it fails to work as expected during macro execution.

 
For me, view.prevTextArea() w
by Robert Schwenn on Tue, 30/11/2010 - 17:56
For me, view.prevTextArea() works from within a macro (mostly). Maybe it's a timing problem in the way that the focus is set properly BEFORE the console is completely ready. What happens when You invoke the macro from the action bar?

If You aren't using jEdit 4.3.2, please update. There has been fixed an issue with macro invoking from the menu. Maybe it's related somehow.
 
Very interesting: works from action bar
by grantciv on Thu, 02/12/2010 - 12:32

Robert, thanks for suggesting the action bar. The macro seems to work properly when executed from there. Any idea why that would be?

I'm actually about one week into using jEdit, so I wasn't familiar with the action bar. It looks like it'll be very useful. The only downside is that the macro name would need to be very short and probably non-descriptive if this were to replace my keyboard-shortcut.

I have a few questions in this area:

  1. Is there a way to incorporate execution via the action bar from within the macro? This would be a workaround to the inconsistent behavior from directly executing the macro.
  2. Is there some way to alias a macro so it's more convenient to execute via the action bar?
  3. If aliases aren't directly supported, I suppose I could create a "shorcut macro" with a convenient name that simply calls the real, longer-named macro. Would there be any downside to this sort of pattern?

By the way, I'm at version 4.3.2.

Thanks again.

 
I'm curious: are You saying t
by Robert Schwenn on Thu, 02/12/2010 - 20:42
I'm curious: are You saying that the macro works well when invoked via action bar, but not when invoked via keyboard shortcut?

To Your questions:
1. If You want macro 1 to invoke macro 2, this could be done directly via source(path) - see the run_script macro.
2. No, I don't think so.
3. see point 1. If it works well, it's ok Smiling

If You can tell steps to reproduce the undesired behavior, please file a bug report at the SourceForge tracker ( http://sourceforge.net/tracker/?atid=100588&group_id=588&func=browse ).
 
Working
by grantciv on Fri, 03/12/2010 - 21:58

When I run my macro via the action bar, the focus returns to the text area after execution the system shell. This is the intended behavior.

If I run the same macro directly (via menu selection or via keyboard shortcut), the focus remains in the Console area. This is NOT the desired behavior.

I successfully created a shortcut macro named 'run' that calls other macros to accomplish what I wanted (save all buffers, run current buffer in its associated interpreter, and return to the text area). Here's what that script looks like:

Macros.getMacro("Save_All_(no_confirm)").invoke(view);
Macros.getMacro("Development/Run_Current_Buffer").invoke(view);
editPane.focusOnTextArea();

As you can see, I changed the focusing method to editPane.focusOnTextArea instead of view.prevTextArea(). For one, it's probably more reliable since it's no longer a relative action. More importantly, with that change, I can now run the macro directly and the focus returns as I intended.

User login
Browse archives
« September 2024  
MoTuWeThFrSaSu
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   96500
Context Free Art (*.cfdg)   0.31   46061
BBEdit scheme   1.0   18600
JBuilder scheme   .001   18501
ColdFusion scheme   1.0   18030
R Edit Mode - extensive version   0.1   17480
Advanced HTML edit mode   1.0   16212
Matlab Edit Mode   1.0   16074
jEdit XP icons   1.0   15235
XP icons for jEdit   1.1   14299