File: Mode_Switcher.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 (201 lines) | stat: -rw-r--r-- 6,937 bytes parent folder | download | duplicates (5)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * ModeSwitcher.bsh - a BeanShell macro script for changing the current
 * buffer's edit mode.
 *
 * Copyright (C) 2004-6 Nicholas O'Leary nick.oleary@gmail.com
 * 
 * :mode=beanshell:tabSize=3:indentSize=3:maxLineLen=0:noTabs=false:
 * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
 *
 * {{{ License
 * 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 the jEdit program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * }}}
 *
 * Notes:
 *  There are two other ways to change the buffers mode:
 *    - enter 'buffer.mode=[mode]' in the action bar
 *    - change it in the Buffer Options dialog
 *  Whilst both of these do the job, I wanted a way to achieve it with minimum
 *  effort, and keypresses.
 *  It also has the benefit of auto-completion of mode names.
 * 
 * $Id: Mode_Switcher.bsh 21353 2012-03-14 09:46:51Z jojaba_67 $
 */

import javax.swing.border.EmptyBorder;

//Localization
final static String MainDialogTitle = jEdit.getProperty("macro.rs.BufferModeSwitcher.MainDialog.title", "Buffer Mode Switcher");
final static String EnterBufferModeLabel = jEdit.getProperty("macro.rs.BufferModeSwitcher.EnterBufferMode.label", "Enter buffer mode:");
final static String ChangingBufferModeLabel = jEdit.getProperty("macro.rs.BufferModeSwitcher.ChangingBufferMode.label", "Changing mode of buffer");
final static String ToLabel = jEdit.getProperty("macro.rs.BufferModeSwitcher.To.label", "to");
final static String ModeLabel = jEdit.getProperty("macro.rs.BufferModeSwitcher.Mode.label", "Mode");
final static String NotFoundLabel = jEdit.getProperty("macro.rs.BufferModeSwitcher.NotFound.label", "Not found");

//class ModeSwitcherTextField extends JTextField
void modeSwitcher() {
	JDialog dialog = new JDialog(view, MainDialogTitle, true);
	dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	JPanel content = new JPanel(new BorderLayout());
	content.setBorder(new EmptyBorder(12,12,12,12));
	Mode[] modes = jEdit.getModes();
	String[] names = new String[modes.length];
	for(int i=0;i<modes.length;i++) {
		names[i] = modes[i].getName();
	}
	Arrays.sort(names);
	JTextField textfield = new JTextField() {
		protected String[] names;
		protected boolean shifted = false;
		//{{{ setNames
		public void setNames(String[] a){
			names = a;
		} //}}}
		public boolean getFocusTraversalKeysEnabled(){return false;}
		//{{{ processKeyEvent
		protected void processKeyEvent(KeyEvent evt)
		{
			if (evt.getID() == KeyEvent.KEY_RELEASED) {
				if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
					shifted = false;
				}
			} else if(evt.getID() == KeyEvent.KEY_PRESSED) {
				if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
					shifted = true;
				} else if(evt.getKeyCode() == KeyEvent.VK_TAB) {
					// Get the current text
					String txt = getText();
					String original = txt;
					// See if some text is selected
					if (getSelectedText() != null)
						txt = txt.substring(0,txt.length()-getSelectedText().length());
					// txt represents the unhighlighted text in the box. This is used
					// to find further matches.
					
					// See if the current text is a known mode
					int index = Arrays.binarySearch(names,original);
					if (index < 0) index = 0;
					int indexStep = 1;
					if (shifted) indexStep = -1;
					index+=indexStep;
					if (index == names.length) index = 0;
					if (index < 0) index = names.length-1;
					int match = -1;
					boolean foundExact = false;
					boolean keepLooping = true;
					// Loop through modes, starting at current+1
					int i = index;
					while(keepLooping) {
						// Skip if the mode name is shorter than the current text
						if (names[i].length()>=txt.length())
						{
							// If the mode matches, escape
							if (names[i].substring(0,txt.length()).equals(txt)) {
								match = i;
								break;
							}
						}
						// Loop the loop
						i+=indexStep;
						if (i == names.length) i = 0;
						if (i < 0) i = names.length-1;
						if (i==index) break;
					}
					// If a match has been found...
					if (match >= 0) {
						setText(names[match]);
						setSelectionStart(txt.length());
						setSelectionEnd(names[match].length());
					}
					return;
				}
			}
			super.processKeyEvent(evt);
		} //}}}
	};
	
	textfield.setColumns(20);
	textfield.setNames(names);
	Mode m = buffer.getMode();
	// Set the inital text to the current mode, and highlight it, so a key
	// press will clear the entry.
	if (m != null) {
		textfield.setText(m.getName());
		textfield.setSelectionStart(0);
		textfield.setSelectionEnd(m.getName().length());
	}
	content.add(new JLabel(EnterBufferModeLabel), BorderLayout.NORTH);
	content.add(textfield, BorderLayout.CENTER);
	Vector v = new Vector();
	
	// KeyListener Interface
	//{{{ keyPressed
	void keyPressed(evt)
	{
		if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
			dialog.dispose();
		else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			Mode m = jEdit.getMode(textfield.getText());
			if (m!=null)
			{
				buffer.setMode(m);
				Log.log(Log.NOTICE,
						  BeanShell.class,
						  ChangingBufferModeLabel+" ["+
							  buffer.getName()+"] "+ToLabel+" ["+
							  m.getName()+"]");
			} else {
				Log.log(Log.WARNING,
						  BeanShell.class,
						  ModeLabel+" ["+textfield.getText()+"] "+NotFoundLabel);
			}
			evt.consume();
			dialog.dispose();
		}
	}//}}}
	void keyReleased(evt) {}
	void keyTyped(evt) {}
	
	dialog.addKeyListener(this);
	textfield.addKeyListener(this);
	dialog.setContentPane(content);
	dialog.pack();
	dialog.setLocationRelativeTo(view);
	textfield.grabFocus();
	dialog.setVisible(true);
}

modeSwitcher();

/*

Macro index data (in DocBook format)

   <listitem>
      <para><filename>Mode_Switcher.bsh</filename></para>
      Displays a modal dialog with the current buffer's mode in a text field,
      allowing one to change the mode by typing in its name.
      <keycap>ENTER</keycap> selects the current mode; if the text is not a 
      valid mode, the dialog still dismisses, but a warning is logged to the
      activity log.
      <keycap>ESACPE</keycap> closes the dialog with no further action.
      <keycap>TAB</keycap> attempts to auto-complete the mode name. Pressing
      <keycap>TAB</keycap> repeatedly cycles through the possible completions. 
		<keycap>SHIFT-TAB</keycap> cycles through the completions in reverse.
      </para></abstract>
   </listitem>

*/