PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/macros/Text/Add_Prefix_and_Suffix.bsh

#
Unknown | 158 lines | 142 code | 16 blank | 0 comment | 0 complexity | 38f83ef0830d97913ce7c3bfff6ea2d9 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 3866 2001-11-06 15:04:21Z jgellene $
  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. * Checked for jEdit 4.0 API
  37. *
  38. */
  39. // beginning of Add_suffix_and_prefix.bsh
  40. // import statements
  41. import javax.swing.border.*;
  42. // main routine
  43. void prefixSuffixDialog()
  44. {
  45. // create dialog object and set its features
  46. title = "Add prefix and suffix to selected lines";
  47. dialog = new JDialog(view, title, false);
  48. content = new JPanel(new BorderLayout());
  49. content.setBorder(new EmptyBorder(12, 12, 12, 12));
  50. dialog.setContentPane(content);
  51. // add to the dialog a panel containing the text fields for
  52. // entry of the prefix and suffix text
  53. fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
  54. prefixField = new HistoryTextField("macro.add-prefix");
  55. prefixLabel = new JLabel("Prefix to add:");
  56. suffixField = new HistoryTextField("macro.add-suffix");
  57. suffixLabel = new JLabel("Suffix to add:");
  58. fieldPanel.add(prefixLabel);
  59. fieldPanel.add(prefixField);
  60. fieldPanel.add(suffixLabel);
  61. fieldPanel.add(suffixField);
  62. content.add(fieldPanel, "Center");
  63. // add a panel containing the buttons
  64. buttonPanel = new JPanel();
  65. buttonPanel.setLayout(new BoxLayout(buttonPanel,
  66. BoxLayout.X_AXIS));
  67. buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
  68. buttonPanel.add(Box.createGlue());
  69. ok = new JButton("OK");
  70. cancel = new JButton("Cancel");
  71. ok.setPreferredSize(cancel.getPreferredSize());
  72. dialog.getRootPane().setDefaultButton(ok);
  73. buttonPanel.add(ok);
  74. buttonPanel.add(Box.createHorizontalStrut(6));
  75. buttonPanel.add(cancel);
  76. buttonPanel.add(Box.createGlue());
  77. content.add(buttonPanel, "South");
  78. // register this method as an ActionListener for
  79. // the buttons and text fields
  80. ok.addActionListener(this);
  81. cancel.addActionListener(this);
  82. prefixField.addActionListener(this);
  83. suffixField.addActionListener(this);
  84. // locate the dialog in the center of the
  85. // editing pane and make it visible
  86. dialog.pack();
  87. dialog.setLocationRelativeTo(view);
  88. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  89. dialog.setVisible(true);
  90. // this method will be called when a button is clicked
  91. // or when ENTER is pressed
  92. void actionPerformed(e)
  93. {
  94. if(e.getSource() != cancel)
  95. {
  96. processText();
  97. }
  98. dialog.dispose();
  99. }
  100. // this is where the work gets done to insert
  101. // the prefix and suffix
  102. void processText()
  103. {
  104. prefix = prefixField.getText();
  105. suffix = suffixField.getText();
  106. if(prefix.length() == 0 && suffix.length() == 0)
  107. return;
  108. prefixField.addCurrentToHistory();
  109. suffixField.addCurrentToHistory();
  110. // text manipulation begins here using calls
  111. // to jEdit methods
  112. selectedLines = textArea.getSelectedLines();
  113. for(i = 0; i < selectedLines.length; ++i)
  114. {
  115. offsetBOL = textArea.getLineStartOffset(selectedLines[i]);
  116. textArea.setCaretPosition(offsetBOL);
  117. textArea.goToStartOfWhiteSpace(false);
  118. textArea.goToEndOfWhiteSpace(true);
  119. text = textArea.getSelectedText();
  120. if(text == null) text = "";
  121. textArea.setSelectedText(prefix + text + suffix);
  122. }
  123. }
  124. }
  125. // this single line of code is the script's main routine
  126. // it calls the methods and exits
  127. prefixSuffixDialog();
  128. /*
  129. Macro index data (in DocBook format)
  130. <listitem>
  131. <para><filename>Add_Prefix_and_Suffix.bsh</filename></para>
  132. <abstract><para>
  133. Adds user-supplied <quote>prefix</quote> and <quote>suffix</quote>
  134. text to each line in a group of selected lines.
  135. </para></abstract>
  136. <para>
  137. Text is added after leading whitespace and before trailing whitespace.
  138. A dialog window receives input and <quote>remembers</quote> past entries.
  139. </para>
  140. </listitem>
  141. */
  142. // end Add_Prefix_and_Suffix.bsh