インデックス挿入@マクロ

C:\Program Files\jEdit\macros\Test
などに保存すると、メニューのマクロから使える。

ショートカットキーを割り当てると、さらに便利になる。

使い方:

12.2.3

と入力すれば、

12.2.1
12.2.2
12.2.3

と書くことができる。


// beginning of test.bsh( インデックス挿入 )
// Ref: add prefix and suffix

// import statements
import javax.swing.border.*;
import java.lang.Integer;

// main routine
void prefixSuffixDialog(View view)
{
this.view = view;

// create dialog object and set its features
title = "test : xx.xx.xx / xx-xx-xx Make Index Number To";
dialog = new JDialog(view, title, false);
content = new JPanel(new BorderLayout());
content.setBorder(new EmptyBorder(12, 12, 12, 12));
dialog.setContentPane(content);

// add to the dialog a panel containing the text fields for
// entry of the prefix and suffix text
fieldPanel = new JPanel(new GridLayout(6, 1, 0, 6));
prefixField = new HistoryTextField("macro.add-prefix");
prefixLabel = new JLabel("Index To:");
suffixField = new HistoryTextField("macro.add-suffix");
suffixLabel = new JLabel("Index From:");
spaceCheckBox = new JCheckBox("全角", false);
fieldPanel.add(prefixLabel);
fieldPanel.add(prefixField);
fieldPanel.add(suffixLabel);
fieldPanel.add(suffixField);
fieldPanel.add(spaceCheckBox);
content.add(fieldPanel, "Center");

// add a panel containing 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
ok.addActionListener(this);
cancel.addActionListener(this);
prefixField.addActionListener(this);
suffixField.addActionListener(this);
spaceCheckBox.addKeyListener(this);

// locate the dialog in the center of the
// editing pane and make it visible
dialog.pack();
dialog.setLocationRelativeTo(view);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);

//初期値を設定
suffixField.setText("1");

// this method will be called when a button is clicked
// or when ENTER is pressed
void actionPerformed(e)
{
if(e.getSource() != cancel)
{
processText();
}
dialog.dispose();
}

// this is where the work gets done to insert
// the prefix and suffix
void processText()
{
prefix = prefixField.getText();
suffix = suffixField.getText();
if(prefix.length() == 0 && suffix.length() == 0)
return;

//開始数字を指定可能にする
if(suffix.length()==0)
{
startNum = 0;
}
else
{
startNum = Integer.valueOf( suffix );
}

space=new String(" ");
boolean a= spaceCheckBox.isSelected();
if(a){
space=new String(" ");
}

//xx.xx.x の最後の x を数字として取得。
searchTo = new String();
searchTo = prefix;
searchPos1 = searchTo.lastIndexOf(".");
searchPos2 = searchTo.lastIndexOf("-");
// "." が無い場合
if(-1 == searchPos1 && -1 == searchPos2)
{
endNum = Integer.valueOf( prefix );
for(i = startNum; i <= endNum; ++i )
{
textArea.setSelectedText( i + "." + space + "\n");
}
return;
}
searchPos = searchPos1;
if(-1 == searchPos1)
{
searchPos = searchPos2;
}

//目的の数
int targetNumber;
targetLetter = new String();
targetLetter = searchTo.substring(searchPos + 1);
targetNumber = Integer.valueOf( targetLetter );

//prefix 修正( xx.xx.x の xx.xx. まで )
prefix = searchTo.substring(0, searchPos);

if( startNum == 0 )
{
//採取の数字指定なし
if(-1 != searchPos1)
{
textArea.setSelectedText(prefix + space);
}
else
{
textArea.setSelectedText(prefix + "." +space);
}
}
else
{
//最初の数字が指定されていた場合
suffix = startNum;
if(-1 != searchPos1)
{
textArea.setSelectedText(prefix + "." + suffix + space);
}
else
{
textArea.setSelectedText(prefix + "-" + suffix + "." + space);
}
}

// set caret position
int caretPos = textArea.getCaretPosition();
//fix little(2)
textArea.setSelectedText("\n");

for(i = startNum; i < targetNumber; ++i )
{
//suffix 設定( xx.xx.x の最後の x )
suffix = i + 1;
if(-1 != searchPos1)
{
textArea.setSelectedText(prefix + "." + suffix + space + "\n");
}
else
{
textArea.setSelectedText(prefix + "-" + suffix + "." + space +"\n");
}
}

// Move the cursor into the set caret position
textArea.setCaretPosition(caretPos);
}
}

// this single line of code is the script's main routine
// it calls the methods and exits
if(buffer.isReadOnly())
Macros.error(view, "Buffer is read-only.");
else
prefixSuffixDialog(view);

// end Test.bsh