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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/**
* Utility methods for Emacs emulation macros.
*/
import org.gjt.sp.jedit.Registers;
import org.gjt.sp.gui.HistoryModel;
boolean repeatingSameMacro (String macroName)
{
ih = textArea.getInputHandler();
lastAction = ih.getLastAction();
lastActionCount = ih.getLastActionCount();
// When called from within a macro, the last action will be that macro.
// But, if the last action count is greater than 1, then it's a repeat.
boolean repeat = false;
if ( (lastAction.getName().equals (macroName)) && (lastActionCount > 1) )
repeat = true;
return repeat;
}
String lineAt (int i)
{
StringBuffer buf = new StringBuffer();
while (! atEndOfBuffer (i))
{
char c = charAt (i);
buf.append (c);
if (c == '\n')
break;
}
return buf.toString();
}
char charAt (int i)
{
return buffer.getText (i, 1).charAt (0);
}
char charAtCaret()
{
caret = textArea.getCaretPosition();
return (atEndOfBuffer() ? '\0' : buffer.getText (caret, 1).charAt (0));
}
boolean atEndOfBuffer()
{
return atEndOfBuffer (textArea.getCaretPosition());
}
boolean atEndOfBuffer (int caret)
{
return (caret >= buffer.getLength());
}
int eatNonAlphanums()
{
boolean eat = true;
while (eat)
{
ch = charAtCaret();
if (ch == '\n')
{
textArea.goToNextLine (false);
textArea.goToStartOfLine (false);
}
else
{
if (Character.isLetterOrDigit (ch))
eat = false;
else
textArea.goToNextCharacter (false);
}
}
return textArea.getCaretPosition();
}
int eatWhitespace()
{
boolean eat = true;
while (eat)
{
ch = charAtCaret();
if (ch == '\n')
{
textArea.goToNextLine (false);
textArea.goToStartOfLine (false);
}
else if (Character.isWhitespace (ch))
{
textArea.goToNextCharacter (false);
}
else
{
eat = false;
}
}
return textArea.getCaretPosition();
}
int getCardinalProperty (String name, int defaultValue)
{
int result = jEdit.getIntegerProperty (name, defaultValue);
if (result <= 0)
result = defaultValue;
return result;
}
String makeBufferPropertyName (String prefix)
{
return makeBufferPropertyName (buffer, prefix);
}
String makeBufferPropertyName (Buffer theBuffer, String prefix)
{
propName = new StringBuffer (prefix);
// Convert any Windows-style file separators to Unix ones, since
// backslashes are special characters in properties files.
fileSep = System.getProperty ("file.separator");
if (! fileSep.equals ("/"))
{
// Backslash is also special in regular expressions. Since, in theory,
// the file separator could be *anything*, we check explicitly for
// backslash here.
if (fileSep.equals ("\\"))
fileSep = fileSep + "\\";
bufName = theBuffer.getPath().replaceAll (fileSep, "/");
}
else
{
bufName = theBuffer.getPath();
}
propName.append (bufName);
return propName.toString();
}
int getDefaultWrap()
{
return getCardinalProperty ("buffer.maxLineLen", 79);
}
int getMark (Buffer buffer)
{
propName = makeBufferPropertyName ("emacs.mark");
int mark = getCardinalProperty (propName, -1);
if (mark != -1)
{
if (mark >= buffer.getLength())
mark = buffer.getLength() - 1;
}
return mark;
}
void setMark (Buffer buffer, int pos)
{
propName = makeBufferPropertyName (buffer, "emacs.mark");
jEdit.setTemporaryProperty (propName, String.valueOf (pos));
}
void beep()
{
view.getToolkit().beep();
}
Selection getKillRegion()
{
// If there's a selection, use it instead.
int caret = textArea.getCaretPosition();
Selection selection = textArea.getSelectionAtOffset (caret);
if (selection == null)
{
int mark = getMark (buffer);
if (mark == -1)
{
beep();
return null;
}
selection = new Selection.Range (Math.min (caret, mark),
Math.max (caret, mark));
textArea.setSelection (selection);
}
return selection;
}
Registers.Register getClipboard()
{
return Registers.getRegister ('$');
}
void setClipboard (String string)
{
Registers.setRegister ('$', string);
}
void setClipboard (Selection selection)
{
setClipboard (textArea.getSelectedText (selection));
}
void addToClipboardAndHistory (String string)
{
// The special register '$' is the clipboard.
setClipboard (string);
// Save the text in the history, too.
HistoryModel.getModel ("clipboard").addItem (string);
}
void addToClipboardAndHistory (Selection selection)
{
addToClipboardAndHistory (textArea.getSelectedText (selection));
}
int findEndOfSentence()
{
caret = textArea.getCaretPosition();
for (;;)
{
if (atEndOfBuffer (caret))
break;
ch = charAt (caret);
if (ch == '.')
{
if (Character.isWhitespace (charAt (caret + 1)))
{
caret++;
break;
}
}
caret++;
}
return caret;
}
int findBeginningOfSentence()
{
caret = textArea.getCaretPosition() - 1;
if (charAt (caret) == '.')
caret--;
for (;;)
{
if (caret <= 0)
break;
ch = charAt (caret);
if (ch == '.')
{
if (Character.isWhitespace (charAt (caret + 1)))
{
caret++;
break;
}
}
else if (Character.isUpperCase (ch))
{
caret--;
if (caret <= 0)
break;
if (Character.isWhitespace (charAt (caret)))
break;
}
caret--;
}
return caret;
}
|