mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Added preference editor.keys.home_and_end_travel_smart to make home and end keys travel to the first/last non-whitespace character before travelling to the actual start/end of the line
This commit is contained in:
@@ -11,6 +11,9 @@ package processing.app.syntax;
|
||||
|
||||
import javax.swing.text.*;
|
||||
import javax.swing.JPopupMenu;
|
||||
|
||||
import processing.app.Preferences;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.awt.Component;
|
||||
import java.util.*;
|
||||
@@ -36,6 +39,16 @@ public abstract class InputHandler extends KeyAdapter
|
||||
* this property is not set.
|
||||
*/
|
||||
public static final String SMART_HOME_END_PROPERTY = "InputHandler.homeEnd";
|
||||
|
||||
/**
|
||||
* If this PDE property is set to Boolean.TRUE, the home/end keys will
|
||||
* go to the first/last non-whitespace character of the line. If already at
|
||||
* the that character, the keypress will move the cursor to the actual
|
||||
* start/end of the line.
|
||||
*
|
||||
* SMART_HOME_END_PROPERTY takes precedence over this property.
|
||||
*/
|
||||
public static final String CONTEXT_AWARE_HOME_END = "editor.keys.home_and_end_travel_smart";
|
||||
|
||||
public static final ActionListener BACKSPACE = new backspace();
|
||||
public static final ActionListener BACKSPACE_WORD = new backspace_word();
|
||||
@@ -580,8 +593,10 @@ public abstract class InputHandler extends KeyAdapter
|
||||
|
||||
int caret = textArea.getCaretPosition();
|
||||
|
||||
int lastOfLine = textArea.getLineStopOffset(
|
||||
textArea.getCaretLine()) - 1;
|
||||
int caretLine = textArea.getCaretLine();
|
||||
|
||||
int lastOfLine = textArea.getLineStopOffset(caretLine) - 1;
|
||||
int lastNonWhiteSpaceOfLine = textArea.getLineStopNonWhiteSpaceOffset(caretLine) - 1;
|
||||
int lastVisibleLine = textArea.getFirstLine()
|
||||
+ textArea.getVisibleLines();
|
||||
if(lastVisibleLine >= textArea.getLineCount())
|
||||
@@ -595,14 +610,20 @@ public abstract class InputHandler extends KeyAdapter
|
||||
int lastVisible = textArea.getLineStopOffset(lastVisibleLine) - 1;
|
||||
int lastDocument = textArea.getDocumentLength();
|
||||
|
||||
if(caret == lastDocument)
|
||||
if(caret == lastDocument && !Preferences.getBoolean(CONTEXT_AWARE_HOME_END))
|
||||
{
|
||||
textArea.getToolkit().beep();
|
||||
return;
|
||||
}
|
||||
else if(!Boolean.TRUE.equals(textArea.getClientProperty(
|
||||
SMART_HOME_END_PROPERTY)))
|
||||
caret = lastOfLine;
|
||||
{
|
||||
if (!Preferences.getBoolean(CONTEXT_AWARE_HOME_END)
|
||||
|| caret == lastNonWhiteSpaceOfLine)
|
||||
caret = lastOfLine;
|
||||
else
|
||||
caret = lastNonWhiteSpaceOfLine;
|
||||
}
|
||||
else if(caret == lastVisible)
|
||||
caret = lastDocument;
|
||||
else if(caret == lastOfLine)
|
||||
@@ -655,21 +676,29 @@ public abstract class InputHandler extends KeyAdapter
|
||||
|
||||
int firstLine = textArea.getFirstLine();
|
||||
|
||||
int firstOfLine = textArea.getLineStartOffset(
|
||||
textArea.getCaretLine());
|
||||
int caretLine = textArea.getCaretLine();
|
||||
|
||||
int firstOfLine = textArea.getLineStartOffset(caretLine);
|
||||
int firstNonWhiteSpaceOfLine = textArea.getLineStartNonWhiteSpaceOffset(caretLine);
|
||||
int firstVisibleLine = (firstLine == 0 ? 0 :
|
||||
firstLine + textArea.getElectricScroll());
|
||||
int firstVisible = textArea.getLineStartOffset(
|
||||
firstVisibleLine);
|
||||
|
||||
if(caret == 0)
|
||||
if(caret == 0 && !Preferences.getBoolean(CONTEXT_AWARE_HOME_END))
|
||||
{
|
||||
textArea.getToolkit().beep();
|
||||
return;
|
||||
}
|
||||
else if(!Boolean.TRUE.equals(textArea.getClientProperty(
|
||||
SMART_HOME_END_PROPERTY)))
|
||||
caret = firstOfLine;
|
||||
{
|
||||
if (!Preferences.getBoolean(CONTEXT_AWARE_HOME_END)
|
||||
|| caret == firstNonWhiteSpaceOfLine)
|
||||
caret = firstOfLine;
|
||||
else
|
||||
caret = firstNonWhiteSpaceOfLine;
|
||||
}
|
||||
else if(caret == firstVisible)
|
||||
caret = 0;
|
||||
else if(caret == firstOfLine)
|
||||
|
||||
@@ -819,6 +819,20 @@ public class JEditTextArea extends JComponent
|
||||
return lineElement.getStartOffset();
|
||||
}
|
||||
|
||||
public int getLineStartNonWhiteSpaceOffset(int line)
|
||||
{
|
||||
int offset = getLineStartOffset(line);
|
||||
int length = getLineLength(line);
|
||||
String str = getText(offset, length);
|
||||
|
||||
for(int i = 0; i < str.length(); i++) {
|
||||
if(!Character.isWhitespace(str.charAt(i))) {
|
||||
return offset + i;
|
||||
}
|
||||
}
|
||||
return offset + length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the end offset of the specified line.
|
||||
* @param line The line
|
||||
@@ -834,6 +848,21 @@ public class JEditTextArea extends JComponent
|
||||
else
|
||||
return lineElement.getEndOffset();
|
||||
}
|
||||
|
||||
public int getLineStopNonWhiteSpaceOffset(int line)
|
||||
{
|
||||
int offset = getLineStopOffset(line);
|
||||
int length = getLineLength(line);
|
||||
String str = getText(offset - length - 1, length);
|
||||
|
||||
for(int i = 0; i < length; i++) {
|
||||
if(!Character.isWhitespace(str.charAt(length - i - 1))) {
|
||||
return offset - i;
|
||||
}
|
||||
}
|
||||
|
||||
return offset - length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the start offset of the line after this line, or the end of
|
||||
|
||||
@@ -104,6 +104,10 @@ editor.keys.shift_backspace_is_delete = true
|
||||
|
||||
# home and end keys should only travel to the start/end of the current line
|
||||
editor.keys.home_and_end_travel_far = false
|
||||
# home and end keys move to the first/last non-whitespace character, and move
|
||||
# to the actual start/end when pressed a second time. Only works if
|
||||
# editor.keys.home_and_end_travel_far is false
|
||||
editor.keys.home_and_end_travel_smart = true
|
||||
# the OS X HI Guidelines say that home/end are relative to the document
|
||||
# if you don't like it, this is the preference to change
|
||||
editor.keys.home_and_end_travel_far.macosx = true
|
||||
|
||||
Reference in New Issue
Block a user