ide/src/wombat/gui/text/SchemeTextArea.java
public int tab() {
    	// Things that break tokens.
        String delimiters = "()[] ";

        // Get the text and current position.
        String text = getText();
        int pos = code.getCaretPosition();
        int len = text.length();
        
        // Fix tabs.
        if (text.indexOf('\t') != -1) {
        	text = text.replace('\t', ' ');
        	setText(text);
        	code.setCaretPosition(pos);
        }
        
        // If we're after the #!eof, don't format.
        if (text.lastIndexOf("#!eof", pos) >= 0) return 0;
        
        // Variables we are trying to determine.
        int indentNow = 0;
        int indentTo = 0;
        int insertAt = 0;
        int tokenStart = 0;
        int tokenEnd = pos;

        // Get the start of this line.
        int lineStart = text.lastIndexOf(NL, pos - 1);
        insertAt = (lineStart < 0 ? 0 : lineStart + NL.length());

        // Get the indentation on the current line.
        for (int i = Math.max(0, lineStart + NL.length()); i < len && text.charAt(i) == ' '; i++)
            indentNow++;

        // If we're on the first line, don't indent.
        if (lineStart == -1)
            indentTo = 0;

            // Otherwise, figure out how far we want to indent.
        else {
        	// Don't reallocate.
        	char c, cp;
        	boolean delimCP, delimC;
        	
            // Scan upwards until we find the first unmatched opening bracket.
            boolean unmatched = false;
            int index;
            Stack<Character> brackets = new Stack<Character>();
            for (int i = lineStart; i >= 0; i--) {
                c = text.charAt(i);
                
                index = text.lastIndexOf(';', i);
                if (index >= 0 && text.lastIndexOf('\n', i) < index) {
                	i = index;
                	continue;
                }
                
                index = text.lastIndexOf("|#", i);
                if (index >= 0 && text.lastIndexOf('\n', i) < index) {
                	i = text.lastIndexOf("#|", index);
                	continue;
                }

		// Ignore character literals
		if (i >= 2 && text.charAt(i - 1) == '\\' && text.charAt(i - 2) == '#')
		    continue;
                
                if (c == ')') brackets.push('(');
                if (c == ']') brackets.push('[');

                if (c == '(' || c == '[') {
                    if (brackets.isEmpty() || brackets.peek() != c) {
                        int thatLine = text.lastIndexOf(NL, i);
                        if (thatLine < 0)
                            thatLine = 0;
                        else
                            thatLine = thatLine + NL.length();

                        indentTo = i - thatLine;
                        unmatched = true;
                        break;
                    } else {
                        brackets.pop();
                    }
                }
                
                if (i > 0) {
                    cp = text.charAt(i - 1);

                    delimCP = (delimiters.indexOf(cp) != -1);
                    delimC = (delimiters.indexOf(c) != -1);
                    
                    if (delimCP && !delimC) tokenStart = i;
                    if (!delimCP && delimC) tokenEnd = i; 
                    if (delimCP && delimC) tokenStart = tokenEnd = i;
                }
            }
            
            // Get the token.
            String token = null;
            try {
                token = text.substring(tokenStart, tokenEnd).trim();
            } catch (StringIndexOutOfBoundsException sioobe) {
            }
            
            // If there aren't any unmatched brackets, start a line.
            if (!unmatched)
                indentTo = 0;

            // If there isn't a string, don't add anything.
            else if (token == null || token.isEmpty())
            	indentTo += 1;
            
            // Otherwise, if there's a valid keyword, indent based on that.
            else if (Options.Keywords.containsKey(token))
                indentTo += Options.Keywords.get(token);

            // Otherwise, fall back on the default indentation.
            else
                indentTo += 2;
        }
        
        // Add new indentation if we need to.
        if (indentNow < indentTo) {
            String toInsert = "";
            for (int i = indentNow; i < indentTo; i++)
                toInsert += " ";

            try {
				code.getDocument().insertString(insertAt, toInsert, null);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
            
            return (indentTo - indentNow);
        }

        // Or remove it, if we need to.
        else if (indentNow > indentTo) {
        	try {
				code.getDocument().remove(insertAt, indentNow - indentTo);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
        	
        	return -1 * (indentTo - indentNow);
        }
        
        // Shouldn't get this far.
        return 0;
    }
/**
     * Perform a tab at the current position.
     * @return How far the caret moved.
     */
Perform a tab at the current position.
@return How far the caret moved.
Perform a tab at the current position . @return How far the caret moved .
public int tab() {
        String delimiters = "()[] ";
        String text = getText();
        int pos = code.getCaretPosition();
        int len = text.length();
        if (text.indexOf('\t') != -1) {
        	text = text.replace('\t', ' ');
        	setText(text);
        	code.setCaretPosition(pos);
        }
        if (text.lastIndexOf("#!eof", pos) >= 0) return 0;
        int indentNow = 0;
        int indentTo = 0;
        int insertAt = 0;
        int tokenStart = 0;
        int tokenEnd = pos;
        int lineStart = text.lastIndexOf(NL, pos - 1);
        insertAt = (lineStart < 0 ? 0 : lineStart + NL.length());
        for (int i = Math.max(0, lineStart + NL.length()); i < len && text.charAt(i) == ' '; i++)
            indentNow++;
        if (lineStart == -1)
            indentTo = 0;
        else {
        	char c, cp;
        	boolean delimCP, delimC;
            boolean unmatched = false;
            int index;
            Stack<Character> brackets = new Stack<Character>();
            for (int i = lineStart; i >= 0; i--) {
                c = text.charAt(i);
                index = text.lastIndexOf(';', i);
                if (index >= 0 && text.lastIndexOf('\n', i) < index) {
                	i = index;
                	continue;
                }
                index = text.lastIndexOf("|#", i);
                if (index >= 0 && text.lastIndexOf('\n', i) < index) {
                	i = text.lastIndexOf("#|", index);
                	continue;
                }
		if (i >= 2 && text.charAt(i - 1) == '\\' && text.charAt(i - 2) == '#')
		    continue;
                if (c == ')') brackets.push('(');
                if (c == ']') brackets.push('[');
                if (c == '(' || c == '[') {
                    if (brackets.isEmpty() || brackets.peek() != c) {
                        int thatLine = text.lastIndexOf(NL, i);
                        if (thatLine < 0)
                            thatLine = 0;
                        else
                            thatLine = thatLine + NL.length();
                        indentTo = i - thatLine;
                        unmatched = true;
                        break;
                    } else {
                        brackets.pop();
                    }
                }
                if (i > 0) {
                    cp = text.charAt(i - 1);
                    delimCP = (delimiters.indexOf(cp) != -1);
                    delimC = (delimiters.indexOf(c) != -1);
                    if (delimCP && !delimC) tokenStart = i;
                    if (!delimCP && delimC) tokenEnd = i; 
                    if (delimCP && delimC) tokenStart = tokenEnd = i;
                }
            }
            String token = null;
            try {
                token = text.substring(tokenStart, tokenEnd).trim();
            } catch (StringIndexOutOfBoundsException sioobe) {
            }
            if (!unmatched)
                indentTo = 0;
            else if (token == null || token.isEmpty())
            	indentTo += 1;
            else if (Options.Keywords.containsKey(token))
                indentTo += Options.Keywords.get(token);
            else
                indentTo += 2;
        }
        if (indentNow < indentTo) {
            String toInsert = "";
            for (int i = indentNow; i < indentTo; i++)
                toInsert += " ";
            try {
				code.getDocument().insertString(insertAt, toInsert, null);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
            return (indentTo - indentNow);
        }
        else if (indentNow > indentTo) {
        	try {
				code.getDocument().remove(insertAt, indentNow - indentTo);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
        	return -1 * (indentTo - indentNow);
        }
        return 0;
    }
public int tab ( ) { String delimiters = "()[] " ; String text = getText ( ) ; int pos = code . getCaretPosition ( ) ; int len = text . length ( ) ; if ( text . indexOf ( '\t' ) != - 1 ) { text = text . replace ( '\t' , ' ' ) ; setText ( text ) ; code . setCaretPosition ( pos ) ; } if ( text . lastIndexOf ( "#!eof" , pos ) >= 0 ) return 0 ; int indentNow = 0 ; int indentTo = 0 ; int insertAt = 0 ; int tokenStart = 0 ; int tokenEnd = pos ; int lineStart = text . lastIndexOf ( NL , pos - 1 ) ; insertAt = ( lineStart < 0 ? 0 : lineStart + NL . length ( ) ) ; for ( int i = Math . max ( 0 , lineStart + NL . length ( ) ) ; i < len && text . charAt ( i ) == ' ' ; i ++ ) indentNow ++ ; if ( lineStart == - 1 ) indentTo = 0 ; else { char c , cp ; boolean delimCP , delimC ; boolean unmatched = false ; int index ; Stack < Character > brackets = new Stack < Character > ( ) ; for ( int i = lineStart ; i >= 0 ; i -- ) { c = text . charAt ( i ) ; index = text . lastIndexOf ( ';' , i ) ; if ( index >= 0 && text . lastIndexOf ( '\n' , i ) < index ) { i = index ; continue ; } index = text . lastIndexOf ( "|#" , i ) ; if ( index >= 0 && text . lastIndexOf ( '\n' , i ) < index ) { i = text . lastIndexOf ( "#|" , index ) ; continue ; } if ( i >= 2 && text . charAt ( i - 1 ) == '\\' && text . charAt ( i - 2 ) == '#' ) continue ; if ( c == ')' ) brackets . push ( '(' ) ; if ( c == ']' ) brackets . push ( '[' ) ; if ( c == '(' || c == '[' ) { if ( brackets . isEmpty ( ) || brackets . peek ( ) != c ) { int thatLine = text . lastIndexOf ( NL , i ) ; if ( thatLine < 0 ) thatLine = 0 ; else thatLine = thatLine + NL . length ( ) ; indentTo = i - thatLine ; unmatched = true ; break ; } else { brackets . pop ( ) ; } } if ( i > 0 ) { cp = text . charAt ( i - 1 ) ; delimCP = ( delimiters . indexOf ( cp ) != - 1 ) ; delimC = ( delimiters . indexOf ( c ) != - 1 ) ; if ( delimCP && ! delimC ) tokenStart = i ; if ( ! delimCP && delimC ) tokenEnd = i ; if ( delimCP && delimC ) tokenStart = tokenEnd = i ; } } String token = null ; try { token = text . substring ( tokenStart , tokenEnd ) . trim ( ) ; } catch ( StringIndexOutOfBoundsException sioobe ) { } if ( ! unmatched ) indentTo = 0 ; else if ( token == null || token . isEmpty ( ) ) indentTo += 1 ; else if ( Options . Keywords . containsKey ( token ) ) indentTo += Options . Keywords . get ( token ) ; else indentTo += 2 ; } if ( indentNow < indentTo ) { String toInsert = "" ; for ( int i = indentNow ; i < indentTo ; i ++ ) toInsert += " " ; try { code . getDocument ( ) . insertString ( insertAt , toInsert , null ) ; } catch ( BadLocationException e ) { e . printStackTrace ( ) ; } return ( indentTo - indentNow ) ; } else if ( indentNow > indentTo ) { try { code . getDocument ( ) . remove ( insertAt , indentNow - indentTo ) ; } catch ( BadLocationException e ) { e . printStackTrace ( ) ; } return - 1 * ( indentTo - indentNow ) ; } return 0 ; }
Perform a tab at the current position.
Perform a tab at the current position .
// Things that break tokens. // Get the text and current position. // Fix tabs. // If we're after the #!eof, don't format. // Variables we are trying to determine. // Get the start of this line. // Get the indentation on the current line. // If we're on the first line, don't indent. // Otherwise, figure out how far we want to indent. // Don't reallocate. // Scan upwards until we find the first unmatched opening bracket. // Ignore character literals // Get the token. // If there aren't any unmatched brackets, start a line. // If there isn't a string, don't add anything. // Otherwise, if there's a valid keyword, indent based on that. // Otherwise, fall back on the default indentation. // Add new indentation if we need to. // Or remove it, if we need to. // Shouldn't get this far.