PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Editing/Mode_Switcher.bsh

#
Unknown | 187 lines | 178 code | 9 blank | 0 comment | 0 complexity | 931a1f05811a08fe981e7ac31c0bc89b 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. * ModeSwitcher.bsh - a BeanShell macro script for changing the current
  3. * buffer's edit mode.
  4. *
  5. * Copyright (C) 2004 Nicholas O'Leary nol@deferential.net
  6. *
  7. * :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=false:
  8. * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  9. *
  10. * {{{ License
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with the jEdit program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. * }}}
  25. *
  26. * Notes:
  27. * There are two other ways to change the buffers mode:
  28. * - enter 'buffer.mode=[mode]' in the action bar
  29. * - change it in the Buffer Options dialog
  30. * Whilst both of these do the job, I wanted a way to achieve it with minimum
  31. * effort, and keypresses.
  32. * It also has the benefit of auto-completion of mode names.
  33. *
  34. * $Id: Mode_Switcher.bsh 5028 2004-04-28 21:15:29Z spestov $
  35. */
  36. import javax.swing.border.EmptyBorder;
  37. Mode[] modes = jEdit.getModes();
  38. JDialog dialog = new JDialog(view, "Buffer Mode Switcher", true);
  39. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  40. JPanel content = new JPanel(new BorderLayout());
  41. content.setBorder(new EmptyBorder(12,12,12,12));
  42. Mode[] modes = jEdit.getModes();
  43. String[] names = new String[modes.length];
  44. for(int i=0;i<modes.length;i++) {
  45. names[i] = modes[i].getName();
  46. }
  47. Arrays.sort(names);
  48. JTextField textfield = new JTextField() {
  49. protected String[] names;
  50. protected boolean shifted = false;
  51. //{{{ setNames
  52. public void setNames(String[] a){
  53. names = a;
  54. } //}}}
  55. public boolean getFocusTraversalKeysEnabled(){return false;}
  56. //{{{ processKeyEvent
  57. protected void processKeyEvent(KeyEvent evt)
  58. {
  59. if (evt.getID() == KeyEvent.KEY_RELEASED) {
  60. if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
  61. shifted = false;
  62. }
  63. } else if(evt.getID() == KeyEvent.KEY_PRESSED) {
  64. if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
  65. shifted = true;
  66. } else if(evt.getKeyCode() == KeyEvent.VK_TAB) {
  67. // Get the current text
  68. String txt = getText();
  69. String original = txt;
  70. // See if some text is selected
  71. if (getSelectedText() != null)
  72. txt = txt.substring(0,txt.length()-getSelectedText().length());
  73. // txt represents the unhighlighted text in the box. This is used
  74. // to find further matches.
  75. // See if the current text is a known mode
  76. int index = Arrays.binarySearch(names,original);
  77. if (index < 0) index = 0;
  78. int indexStep = 1;
  79. if (shifted) indexStep = -1;
  80. index+=indexStep;
  81. if (index == names.length) index = 0;
  82. if (index < 0) index = names.length-1;
  83. int match = -1;
  84. boolean foundExact = false;
  85. boolean keepLooping = true;
  86. // Loop through modes, starting at current+1
  87. int i = index;
  88. while(keepLooping) {
  89. // Skip if the mode name is shorter than the current text
  90. if (names[i].length()>=txt.length())
  91. {
  92. // If the mode matches, escape
  93. if (names[i].substring(0,txt.length()).equals(txt)) {
  94. match = i;
  95. break;
  96. }
  97. }
  98. // Loop the loop
  99. i+=indexStep;
  100. if (i == names.length) i = 0;
  101. if (i < 0) i = names.length-1;
  102. if (i==index) break;
  103. }
  104. // If a match has been found...
  105. if (match >= 0) {
  106. setText(names[match]);
  107. setSelectionStart(txt.length());
  108. setSelectionEnd(names[match].length());
  109. }
  110. return;
  111. }
  112. }
  113. super.processKeyEvent(evt);
  114. } //}}}
  115. };
  116. textfield.setColumns(20);
  117. textfield.setNames(names);
  118. Mode m = buffer.getMode();
  119. // Set the inital text to the current mode, and highlight it, so a key
  120. // press will clear the entry.
  121. if (m != null) {
  122. textfield.setText(m.getName());
  123. textfield.setSelectionStart(0);
  124. textfield.setSelectionEnd(m.getName().length());
  125. }
  126. content.add(new JLabel("Enter buffer mode:"), BorderLayout.NORTH);
  127. content.add(textfield, BorderLayout.CENTER);
  128. Vector v = new Vector();
  129. // KeyListener Interface
  130. //{{{ keyPressed
  131. void keyPressed(evt)
  132. {
  133. if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  134. dialog.dispose();
  135. else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  136. {
  137. Mode m = jEdit.getMode(textfield.getText());
  138. if (m!=null)
  139. {
  140. buffer.setMode(m);
  141. Log.log(Log.NOTICE,
  142. BeanShell.class,
  143. "Changing mode of buffer ["+
  144. buffer.getName()+"] to ["+
  145. m.getName()+"]");
  146. } else {
  147. Log.log(Log.WARNING,
  148. BeanShell.class,
  149. "Mode ["+textfield.getText()+"] not found");
  150. }
  151. evt.consume();
  152. dialog.dispose();
  153. }
  154. }//}}}
  155. void keyReleased(evt) {}
  156. void keyTyped(evt) {}
  157. dialog.addKeyListener(this);
  158. textfield.addKeyListener(this);
  159. dialog.setContentPane(content);
  160. dialog.pack();
  161. dialog.setLocationRelativeTo(view);
  162. dialog.setVisible(true);
  163. /*
  164. Macro index data (in DocBook format)
  165. <listitem>
  166. <para><filename>Mode_Switcher.bsh</filename></para>
  167. Displays a modal dialog with the current buffer's mode in a text field,
  168. allowing one to change the mode by typing in its name.
  169. <keycap>ENTER</keycap> selects the current mode; if the text is not a
  170. valid mode, the dialog still dismisses, but a warning is logged to the
  171. activity log.
  172. <keycap>ESACPE</keycap> closes the dialog with no further action.
  173. <keycap>TAB</keycap> attempts to auto-complete the mode name. Pressing
  174. <keycap>TAB</keycap> repeatedly cycles through the possible completions.
  175. <keycap>SHIFT-TAB</keycap> cycles through the completions in reverse.
  176. </para></abstract>
  177. </listitem>
  178. */