PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Text/Add_Prefix_and_Suffix.bsh

#
Unknown | 162 lines | 145 code | 17 blank | 0 comment | 0 complexity | 84b831fafc36e8fd58c87f209380aea1 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Add_Prefix_and_Suffix.bsh - a BeanShell macro script for the
  3. * jEdit text editor - obtains and processes input for prefix and
  4. * suffix text to be inserted in selected lines in the current
  5. * editing buffer
  6. * Copyright (C) 2001 John Gellene
  7. * jgellene@nyc.rr.com
  8. * http://community.jedit.org
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. * $Id: Add_Prefix_and_Suffix.bsh 4988 2004-03-08 04:29:12Z spestov $
  25. *
  26. * Notes on use:
  27. *
  28. * If no text is selected, the macro will operate on the current line.
  29. *
  30. * The caret position is part of the selected text; if the caret is at
  31. * the beginning of a line, the macro will operate on that line.
  32. *
  33. * The use of HistoryTextField objects allows the macro to 'remember'
  34. * past entries for the prefix and suffix.
  35. */
  36. // beginning of Add_suffix_and_prefix.bsh
  37. // import statements
  38. import javax.swing.border.*;
  39. // main routine
  40. void prefixSuffixDialog(View view)
  41. {
  42. this.view = view;
  43. // create dialog object and set its features
  44. title = "Add prefix and suffix to selected lines";
  45. dialog = new JDialog(view, title, false);
  46. content = new JPanel(new BorderLayout());
  47. content.setBorder(new EmptyBorder(12, 12, 12, 12));
  48. dialog.setContentPane(content);
  49. // add to the dialog a panel containing the text fields for
  50. // entry of the prefix and suffix text
  51. fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
  52. prefixField = new HistoryTextField("macro.add-prefix");
  53. prefixLabel = new JLabel("Prefix to add:");
  54. suffixField = new HistoryTextField("macro.add-suffix");
  55. suffixLabel = new JLabel("Suffix to add:");
  56. fieldPanel.add(prefixLabel);
  57. fieldPanel.add(prefixField);
  58. fieldPanel.add(suffixLabel);
  59. fieldPanel.add(suffixField);
  60. content.add(fieldPanel, "Center");
  61. // add a panel containing the buttons
  62. buttonPanel = new JPanel();
  63. buttonPanel.setLayout(new BoxLayout(buttonPanel,
  64. BoxLayout.X_AXIS));
  65. buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
  66. buttonPanel.add(Box.createGlue());
  67. ok = new JButton("OK");
  68. cancel = new JButton("Cancel");
  69. ok.setPreferredSize(cancel.getPreferredSize());
  70. dialog.getRootPane().setDefaultButton(ok);
  71. buttonPanel.add(ok);
  72. buttonPanel.add(Box.createHorizontalStrut(6));
  73. buttonPanel.add(cancel);
  74. buttonPanel.add(Box.createGlue());
  75. content.add(buttonPanel, "South");
  76. // register this method as an ActionListener for
  77. // the buttons and text fields
  78. ok.addActionListener(this);
  79. cancel.addActionListener(this);
  80. prefixField.addActionListener(this);
  81. suffixField.addActionListener(this);
  82. // locate the dialog in the center of the
  83. // editing pane and make it visible
  84. dialog.pack();
  85. dialog.setLocationRelativeTo(view);
  86. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  87. dialog.setVisible(true);
  88. // this method will be called when a button is clicked
  89. // or when ENTER is pressed
  90. void actionPerformed(e)
  91. {
  92. if(e.getSource() != cancel)
  93. {
  94. processText();
  95. }
  96. dialog.dispose();
  97. }
  98. // this is where the work gets done to insert
  99. // the prefix and suffix
  100. void processText()
  101. {
  102. prefix = prefixField.getText();
  103. suffix = suffixField.getText();
  104. if(prefix.length() == 0 && suffix.length() == 0)
  105. return;
  106. prefixField.addCurrentToHistory();
  107. suffixField.addCurrentToHistory();
  108. // text manipulation begins here using calls
  109. // to jEdit methods
  110. buffer.beginCompoundEdit();
  111. selectedLines = textArea.getSelectedLines();
  112. for(i = 0; i < selectedLines.length; ++i)
  113. {
  114. offsetBOL = textArea.getLineStartOffset(selectedLines[i]);
  115. textArea.setCaretPosition(offsetBOL);
  116. textArea.goToStartOfWhiteSpace(false);
  117. textArea.goToEndOfWhiteSpace(true);
  118. text = textArea.getSelectedText();
  119. if(text == null) text = "";
  120. textArea.setSelectedText(prefix + text + suffix);
  121. }
  122. buffer.endCompoundEdit();
  123. }
  124. }
  125. // this single line of code is the script's main routine
  126. // it calls the methods and exits
  127. if(buffer.isReadOnly())
  128. Macros.error(view, "Buffer is read-only.");
  129. else
  130. prefixSuffixDialog(view);
  131. /*
  132. Macro index data (in DocBook format)
  133. <listitem>
  134. <para><filename>Add_Prefix_and_Suffix.bsh</filename></para>
  135. <abstract><para>
  136. Adds user-supplied <quote>prefix</quote> and <quote>suffix</quote>
  137. text to each line in a group of selected lines.
  138. </para></abstract>
  139. <para>
  140. Text is added after leading whitespace and before trailing whitespace.
  141. A dialog window receives input and <quote>remembers</quote> past entries.
  142. </para>
  143. </listitem>
  144. */
  145. // end Add_Prefix_and_Suffix.bsh