jEdit Macro: Line_Enumerator

This Macro insert a sequenced number at the beginning of each line of text.

 

How to use

Save the file below as …/jEdit/macros/Custom/Line_Enumerator.bsh

Run: Macros > Custom > Line Enumerator

Input the initial number and press ENTER at the dialog box:

 

 

Before:
aaaa
bbbb
cccc
….

After:
100,aaaa
101,bbbb
102,cccc
….

 

/*
* Insert a sequencial number at the beginning of each line
* Marcelo Gennari - 200216_130427
*
*/

import javax.swing.border.*;

void showDialog() {
	title = "Line Enumerator";
	dialog = new JDialog(view, title, false);
	content = new JPanel(new BorderLayout());
	content.setBorder(new EmptyBorder(5, 5, 5, 5));
	dialog.setContentPane(content);
	pwPanel = new JPanel(new GridLayout(1, 2));
	pwLabel = new JLabel("Input the initial value:");
	txtField = new JTextField();
	pwPanel.add(pwLabel);
	pwPanel.add(txtField);
	content.add(pwPanel, BorderLayout.CENTER);
	buttonPanel = new JPanel(new GridLayout(1, 2));
	buttonPanel.setBorder( new EmptyBorder(5, 0, 0, 0));
	okButton = new JButton("OK");
	cancelButton = new JButton("Cancel");
	buttonPanel.add(okButton);
	buttonPanel.add(cancelButton);
	content.add(buttonPanel, BorderLayout.SOUTH);
	okButton.addActionListener(this);
	cancelButton.addActionListener(this);
	dialog.getRootPane().setDefaultButton(okButton);
	actionPerformed(e) {
		this.dialog.dispose();
		cmd = e.getActionCommand();
		if(cmd.equals("OK")) {
			lineEnumerator(this.txtField.getText().trim());
		}
		return;
	}
	dialog.pack();
	dialog.setLocationRelativeTo(view);
	dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	dialog.setVisible(true);
}

void lineEnumerator(String str) {
	int initialValue = 0;
	try {
		initialValue = Integer.parseInt(str);
	} catch (NumberFormatException nfe) {
		view.getStatus().setMessageAndClear("Please, insert a number. Digits only.");
		return;
	}
	// use line separator from current buffer
	String ls = buffer.getStringProperty("lineSeparator");
	if (ls == null) {
		ls = jEdit.getProperty("buffer.lineSeparator");
	}

	StringBuffer sbf = new StringBuffer();
	int endL = textArea.getLineCount();
	for (int i = 0; i < endL; i++) {
		inLine = textArea.getLineText(i);
		sbf.append(String.valueOf(initialValue + i));
		sbf.append(",");
		sbf.append(inLine);
		sbf.append(ls);
	}
	textArea.setText(sbf.toString());
}

showDialog();

Fale conosco

Utilize o formulário ao lado

Gren 2024