The macro script follows. You can find it in the jEdit
distribution in the Text
subdirectory of the
macros
directory. You can also try it out by
invoking
> > .
// beginning of Add_Prefix_and_Suffix.bsh // import statement (see the section called “Import Statements”) import javax.swing.border.*; // main routine void prefixSuffixDialog() { // create dialog object (see the section called “Create the Dialog”) title = “Add prefix and suffix to selected lines”; dialog = new JDialog(view, title, false); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(12, 12, 12, 12)); content.setPreferredSize(new Dimension(320, 160)); dialog.setContentPane(content); // add the text fields (see the section called “Create the Text Fields”) fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6)); prefixField = new HistoryTextField(“macro.add-prefix”); prefixLabel = new JLabel(“Prefix to add:”); suffixField = new HistoryTextField(“macro.add-suffix”); suffixLabel = new JLabel(“Suffix to add:”); fieldPanel.add(prefixLabel); fieldPanel.add(prefixField); fieldPanel.add(suffixLabel); fieldPanel.add(suffixField); content.add(fieldPanel, “Center”); // add a panel containing the buttons (see the section called “Create the Buttons”) buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50)); buttonPanel.add(Box.createGlue()); ok = new JButton(“OK”); cancel = new JButton(“Cancel”); ok.setPreferredSize(cancel.getPreferredSize()); dialog.getRootPane().setDefaultButton(ok); buttonPanel.add(ok); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(cancel); buttonPanel.add(Box.createGlue()); content.add(buttonPanel, “South”); // register this method as an ActionListener for // the buttons and text fields (see the section called “Register the Action Listeners”) ok.addActionListener(this); cancel.addActionListener(this); prefixField.addActionListener(this); suffixField.addActionListener(this); // locate the dialog in the center of the // editing pane and make it visible (see the section called “Make the Dialog Visible”) dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); // this method will be called when a button is clicked // or when ENTER is pressed (see the section called “The Action Listener”) void actionPerformed(e) { if(e.getSource() != cancel) { processText(); } dialog.dispose(); } // this is where the work gets done to insert // the prefix and suffix (see the section called “Get the User's Input”) void processText() { prefix = prefixField.getText(); suffix = suffixField.getText(); if(prefix.length() == 0 && suffix.length() == 0) return; prefixField.addCurrentToHistory(); suffixField.addCurrentToHistory(); // text manipulation begins here using calls // to jEdit methods (see the section called “Call jEdit Methods to Manipulate Text”) buffer.beginCompoundEdit(); selectedLines = textArea.getSelectedLines(); for(i = 0; i < selectedLines.length; ++i) { offsetBOL = textArea.getLineStartOffset( selectedLines[i]); textArea.setCaretPosition(offsetBOL); textArea.goToStartOfWhiteSpace(false); textArea.goToEndOfWhiteSpace(true); text = textArea.getSelectedText(); if(text == null) text = ""; textArea.setSelectedText(prefix + text + suffix); } buffer.endCompoundEdit(); } } // this single line of code is the script's main routine // (see the section called “The Main Routine”) prefixSuffixDialog(); // end of Add_Prefix_and_Suffix.bsh