jEdit Community - Resources for users of the jEdit Text Editor
Extending the action bar - passing arguments to actions
Submitted by irumat on Wednesday, 31 March, 2010 - 22:46
Hi all,

Long time reader, first time poster. I was thinking of adding a (probably awful) hack to the ActionBar to see if I could have the action bar do something like:

<ctrl+enter>action_name my_arg<enter>

For example, it could be "my.save.as filename.bak". Ofcourse, it would be a big effort to have this work for the built-in jEdit actions. But one could, perhaps, write a macro that took an argument like that. It would replace having to show a dialog box to collect simple input.

Any thoughts? Do you think it is possible and worth pursuing?

Cheers,
Irumat
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
Okay...something that I can build on. Just sharing!
by irumat on Thu, 01/04/2010 - 13:41
Hi all,

I had a go at hacking something that let me do the above, just for my own personal BeanShell macro. Someone might find it useful.

I modified org/gjt/sp/jedit/gui/ActionBar.java, the invoke() method. Instead of "final String finalCmd = cmd;" we have:

final String finalCmd;

// See if finalCmd is my custom action
if (cmd.startsWith("mycmd")) {
	// then the second part of the command is what to pass
	// to the 'mycmd' beanshell script
	int index = cmd.indexOf(' ');
	String cmdValue = cmd.substring(index + 1).trim();
	try {
	    BeanShell.getNameSpace().setVariable("mycmd_command", cmdValue);
	} catch (Exception e) {
		System.out.println("Couldn't set second part of command!");
	}
	finalCmd = "mycmd";
} else {
	finalCmd = cmd;
}


Code before and after this line in invoke() method is as before. I then have a BeanShell macro "mycmd.bsh":

RunMyCommand()
{
String command = BeanShell.getNameSpace().getVariable("mycmd_command");
BeanShell.getNameSpace().unsetVariable("mycmd_command");
	
// Fix action history
HistoryModel.getModel("action").removeElement("mycmd"); // by default, jEdit only saves the action name
HistoryModel.getModel("action").addItem("mycmd " + command); // but we want the whole thing
	    
// cd - changes VFS file browser path
if (command.startsWith("cd")) {
	int index = command.indexOf(' ');
	String dir = command.substring(index + 1).trim();
	cd(dir);
}
	
// s/ or s# - substitutions aka search and replace
if (command.startsWith("s/") || command.startsWith("s#")) {
	String delimiter = command.substring(1,2); // delimeter is either '/' or '#'
	String[] parts = command.split(delimiter);
	s(parts[1], parts[2], parts[3]);
}
}

void s(find, replace, options) {
   // search and replace code here
}

void cd(path) {
  // change vfs file browser path code here
}

 
RunMyCommand();


So now I can <ctrl+enter>mycmd cd ~/Docs<enter> in the action bar and it will open the vfs file browser to that directory. Or <ctrl+enter>mycmd s/replace_this/with_that/g<enter> and it will do a search & replace in the current buffer! I've implemented the last part ("/g") to set the options (g = entire buffer, r = regex, i = ignore-case) etc. Very fast!

As one adds functions to the mycmd.bsh beanshell macro, I can see that this could become very useful.

Hopefully someone will find this useful!

Cheers,
Irumat.
User login
Browse archives
« April 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   82348
Context Free Art (*.cfdg)   0.31   46055
JBuilder scheme   .001   18495
BBEdit scheme   1.0   18116
ColdFusion scheme   1.0   18024
R Edit Mode - extensive version   0.1   17473
Advanced HTML edit mode   1.0   16206
Matlab Edit Mode   1.0   16068
jEdit XP icons   1.0   15229
XP icons for jEdit   1.1   14293