jEdit Community - Resources for users of the jEdit Text Editor
Placing selection in clipboard after replacing certain characters
Submitted by ghoetker on Thursday, 24 November, 2005 - 15:29
Hi all. I'm trying to write my first macro and am stuck at multiple points. The task at hand is this. I have text in jEdit of the following format

file write `output' _tab ///
`mformat' (`r(sd)') _tab `mformat' ///
(`r(max)') _tab

I would like to be able to select this text, have the "///" and new_line characters stripped, and the remaining text put in the clipboard, so I can paste it into another application. Thus, the line I would be pasting would be

file write `output' _tab `mformat' (`r(sd)') _tab `mformat' (`r(max)') _tab

I have the sense that the code to do this would be pretty simple, but I'm stuck since it's my first attempt. Could anyone provide me some help?

Thank you in advance.
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
A simple answer
by ghoetker on Wed, 07/12/2005 - 16:15
Replying to my own message, in case anyone else has a similar problem later. I came up with the strategy I would apply, then used the "record macro" option. It came up with something that seems sound. I'm sure it could be improved, but it does what I need.

The strategy is to copy the selected lines in register s, do the necessary search & replace and "join lines" on the original text in the buffer, copy that to the clipboard to be used in another program, then paste the original text from register s back into the buffer. Here is the macro.

Registers.copy(textArea,'s');
SearchAndReplace.setSearchString("///");
SearchAndReplace.setReplaceString(" ");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(false);
SearchAndReplace.replace(view);
textArea.joinLines();
Registers.copy(textArea,'$');
Registers.paste(textArea,'s',false);
one possible solution
by mobius on Wed, 30/11/2005 - 10:23
ghoetker, it seems like the simplest way would be to do a search-and-replace, where the replaced text ends up on the clipboard instead of in the source document, but I'm not sure if this is possible. Does anyone else know if it is?

I have come up with a solution, though. It just requires a bit more work. Basically, it involves the use of a temporary file (buffer) where we can process the text before placing it on the clipboard (register).

// Copy selected text
Registers.copy(textArea, '$');

// Paste copied text in temp buffer
jEdit.newFile(view);
Buffer temp = editPane.getBuffer();
temp.beginCompoundEdit();
Registers.paste(textArea, '$', false);

// Search-and-replace to remove "///" and newlines
textArea.setCaretPosition(0);
SearchAndReplace.setSearchString("///\\n"); // double backslash because need to escape it for beanshell
SearchAndReplace.setReplaceString("");
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
temp.endCompoundEdit();

// Copy result to clipboard
textArea.selectAll();
Registers.copy(textArea, '$');

// Close temp buffer
temp.undo(textArea); // Undo so we don't get "File Not Saved" dialog.
jEdit.closeBuffer(view, temp);

Hopefully that does what you're looking for. If anyone has suggestions on how to simplify or improve this, I'd be interested to know, as I'm working on something similar.
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