File: Add_Prefix_and_Suffix.bsh

package info (click to toggle)
jedit 5.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,252 kB
  • ctags: 11,190
  • sloc: java: 98,480; xml: 94,070; makefile: 52; sh: 42; cpp: 6; python: 6
file content (165 lines) | stat: -rw-r--r-- 6,047 bytes parent folder | download | duplicates (4)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Add_Prefix_and_Suffix.bsh - a BeanShell macro script for the
 * jEdit text editor - obtains and processes input for prefix and
 * suffix text to be inserted in selected lines in the current
 * editing buffer
 * Copyright (C) 2001 John Gellene
 * jgellene@nyc.rr.com
 * http://community.jedit.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Add_Prefix_and_Suffix.bsh 23971 2015-08-08 19:37:35Z daleanson $
 *
 * Notes on use:
 *
 * If no text is selected, the macro will operate on the current line.
 *
 * The caret position is part of the selected text; if the caret is at
 * the beginning of a line, the macro will operate on that line.
 *
 * The use of HistoryTextField objects allows the macro to 'remember'
 * past entries for the prefix and suffix.
 */

// beginning of Add_Prefix_and_Suffix.bsh

// import statements
import javax.swing.border.*;

// Localization
final static String MainDialogTitle = jEdit.getProperty("macro.rs.AddPrefixAndSuffix.MainDialog.title", "Add prefix and suffix to selected lines");
final static String PrefixToAddLabel = jEdit.getProperty("macro.rs.AddPrefixAndSuffix.PrefixToAdd.label", "Prefix to add:");
final static String SuffixToAddLabel = jEdit.getProperty("macro.rs.AddPrefixAndSuffix.SuffixToAdd.label", "Suffix to add:");
final static String PROPERTY_COMMON_OK = jEdit.getProperty("common.ok");
final static String PROPERTY_COMMON_CANCEL = jEdit.getProperty("common.cancel");
final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");

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

    // create dialog object and set its features
    title = MainDialogTitle;
    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(4, 1, 0, 6));
    prefixField = new HistoryTextField("macro.add-prefix");
    prefixLabel = new JLabel(PrefixToAddLabel);
    suffixField = new HistoryTextField("macro.add-suffix");
    suffixLabel = new JLabel(SuffixToAddLabel);
    fieldPanel.add(prefixLabel);
    fieldPanel.add(prefixField);
    fieldPanel.add(suffixLabel);
    fieldPanel.add(suffixField);
    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(PROPERTY_COMMON_OK);
    cancel = new JButton(PROPERTY_COMMON_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);

    // 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);

    // 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;
        prefixField.addCurrentToHistory();
        suffixField.addCurrentToHistory();

        // text manipulation begins here using calls
        // to jEdit methods
        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);
        }
    }
}

// this single line of code is the script's main routine
// it calls the methods and exits
	prefixSuffixDialog(view);

/*
	Macro index data (in DocBook format)

<listitem>
    <para><filename>Add_Prefix_and_Suffix.bsh</filename></para>
    <abstract><para>
        Adds user-supplied <quote>prefix</quote> and <quote>suffix</quote>
        text to each line in a group of selected lines.
    </para></abstract>
    <para>
        Text is added after leading whitespace and before trailing whitespace.
        A dialog window receives input and <quote>remembers</quote> past entries.
    </para>
</listitem>

*/

// end Add_Prefix_and_Suffix.bsh