PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Editing/Mode_Switcher.bsh

#
Unknown | 193 lines | 182 code | 11 blank | 0 comment | 0 complexity | 00f1e764607dba4d0bd63cbce9eabf52 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-6 Nicholas O'Leary nick.oleary@gmail.com
  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 5465 2006-06-21 13:04:43Z kpouer $
  35. */
  36. import javax.swing.border.EmptyBorder;
  37. //class ModeSwitcherTextField extends JTextField
  38. void modeSwitcher() {
  39. JDialog dialog = new JDialog(view, "Buffer Mode Switcher", true);
  40. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  41. JPanel content = new JPanel(new BorderLayout());
  42. content.setBorder(new EmptyBorder(12,12,12,12));
  43. Mode[] modes = jEdit.getModes();
  44. String[] names = new String[modes.length];
  45. for(int i=0;i<modes.length;i++) {
  46. names[i] = modes[i].getName();
  47. }
  48. Arrays.sort(names);
  49. JTextField textfield = new JTextField() {
  50. protected String[] names;
  51. protected boolean shifted = false;
  52. //{{{ setNames
  53. public void setNames(String[] a){
  54. names = a;
  55. } //}}}
  56. public boolean getFocusTraversalKeysEnabled(){return false;}
  57. //{{{ processKeyEvent
  58. protected void processKeyEvent(KeyEvent evt)
  59. {
  60. if (evt.getID() == KeyEvent.KEY_RELEASED) {
  61. if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
  62. shifted = false;
  63. }
  64. } else if(evt.getID() == KeyEvent.KEY_PRESSED) {
  65. if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
  66. shifted = true;
  67. } else if(evt.getKeyCode() == KeyEvent.VK_TAB) {
  68. // Get the current text
  69. String txt = getText();
  70. String original = txt;
  71. // See if some text is selected
  72. if (getSelectedText() != null)
  73. txt = txt.substring(0,txt.length()-getSelectedText().length());
  74. // txt represents the unhighlighted text in the box. This is used
  75. // to find further matches.
  76. // See if the current text is a known mode
  77. int index = Arrays.binarySearch(names,original);
  78. if (index < 0) index = 0;
  79. int indexStep = 1;
  80. if (shifted) indexStep = -1;
  81. index+=indexStep;
  82. if (index == names.length) index = 0;
  83. if (index < 0) index = names.length-1;
  84. int match = -1;
  85. boolean foundExact = false;
  86. boolean keepLooping = true;
  87. // Loop through modes, starting at current+1
  88. int i = index;
  89. while(keepLooping) {
  90. // Skip if the mode name is shorter than the current text
  91. if (names[i].length()>=txt.length())
  92. {
  93. // If the mode matches, escape
  94. if (names[i].substring(0,txt.length()).equals(txt)) {
  95. match = i;
  96. break;
  97. }
  98. }
  99. // Loop the loop
  100. i+=indexStep;
  101. if (i == names.length) i = 0;
  102. if (i < 0) i = names.length-1;
  103. if (i==index) break;
  104. }
  105. // If a match has been found...
  106. if (match >= 0) {
  107. setText(names[match]);
  108. setSelectionStart(txt.length());
  109. setSelectionEnd(names[match].length());
  110. }
  111. return;
  112. }
  113. }
  114. super.processKeyEvent(evt);
  115. } //}}}
  116. };
  117. textfield.setColumns(20);
  118. textfield.setNames(names);
  119. Mode m = buffer.getMode();
  120. // Set the inital text to the current mode, and highlight it, so a key
  121. // press will clear the entry.
  122. if (m != null) {
  123. textfield.setText(m.getName());
  124. textfield.setSelectionStart(0);
  125. textfield.setSelectionEnd(m.getName().length());
  126. }
  127. content.add(new JLabel("Enter buffer mode:"), BorderLayout.NORTH);
  128. content.add(textfield, BorderLayout.CENTER);
  129. Vector v = new Vector();
  130. // KeyListener Interface
  131. //{{{ keyPressed
  132. void keyPressed(evt)
  133. {
  134. if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  135. dialog.dispose();
  136. else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  137. {
  138. Mode m = jEdit.getMode(textfield.getText());
  139. if (m!=null)
  140. {
  141. buffer.setMode(m);
  142. Log.log(Log.NOTICE,
  143. BeanShell.class,
  144. "Changing mode of buffer ["+
  145. buffer.getName()+"] to ["+
  146. m.getName()+"]");
  147. } else {
  148. Log.log(Log.WARNING,
  149. BeanShell.class,
  150. "Mode ["+textfield.getText()+"] not found");
  151. }
  152. evt.consume();
  153. dialog.dispose();
  154. }
  155. }//}}}
  156. void keyReleased(evt) {}
  157. void keyTyped(evt) {}
  158. dialog.addKeyListener(this);
  159. textfield.addKeyListener(this);
  160. dialog.setContentPane(content);
  161. dialog.pack();
  162. dialog.setLocationRelativeTo(view);
  163. textfield.grabFocus();
  164. dialog.setVisible(true);
  165. }
  166. modeSwitcher();
  167. /*
  168. Macro index data (in DocBook format)
  169. <listitem>
  170. <para><filename>Mode_Switcher.bsh</filename></para>
  171. Displays a modal dialog with the current buffer's mode in a text field,
  172. allowing one to change the mode by typing in its name.
  173. <keycap>ENTER</keycap> selects the current mode; if the text is not a
  174. valid mode, the dialog still dismisses, but a warning is logged to the
  175. activity log.
  176. <keycap>ESACPE</keycap> closes the dialog with no further action.
  177. <keycap>TAB</keycap> attempts to auto-complete the mode name. Pressing
  178. <keycap>TAB</keycap> repeatedly cycles through the possible completions.
  179. <keycap>SHIFT-TAB</keycap> cycles through the completions in reverse.
  180. </para></abstract>
  181. </listitem>
  182. */