<commit_before>package jline.console;

import java.util.HashMap;
import java.util.Map;

public class KeyMap {

    private static final int KEYMAP_LENGTH = 256;

    private static final Object NULL_FUNCTION = new Object();

    private Object[] mapping = new Object[KEYMAP_LENGTH];
    private Object anotherKey = null;
//    private boolean convertMetaCharsToAscii = false;


    public KeyMap() {
        this(new Object[KEYMAP_LENGTH]);
    }

    protected KeyMap(Object[] mapping) {
        this.mapping = mapping;
    }

    public Object getAnotherKey() {
        return anotherKey;
    }

    public void from(KeyMap other) {
        this.mapping = other.mapping;
        this.anotherKey = other.anotherKey;
    }

    public Object getBound( CharSequence keySeq ) {
        if (keySeq != null && keySeq.length() > 0) {
            KeyMap map = this;
            for (int i = 0; i < keySeq.length(); i++) {
                char c = keySeq.charAt(i);
//                if (isMeta(c) && isConvertMetaCharsToAscii()) {
//                    if (map.mapping[ESCAPE] instanceof KeyMap) {
//                        map = (KeyMap) map.mapping[ESCAPE];
//                        c = unMeta(c);
//                    } else {
//                        return map.mapping[c];
//                    }
//                }
                if (c > 255) {
                    return Operation.SELF_INSERT;
                }
                if (map.mapping[c] instanceof KeyMap) {
                    if (i == keySeq.length() - 1) {
                        return map.mapping[c];
                    } else {
                        map = (KeyMap) map.mapping[c];
                    }
                } else {
                    return map.mapping[c];
                }
            }
        }
        return null;
    }

    public void bindIfNotBound( CharSequence keySeq, Object function ) {
        if (keySeq != null && keySeq.length() > 0) {
            KeyMap map = this;
            for (int i = 0; i < keySeq.length(); i++) {
                char c = keySeq.charAt(i);
                if (c >= map.mapping.length) {
                    return;
                }
//                if (isMeta(c) && isConvertMetaCharsToAscii()) {
//                    c = unMeta( c );
//                    if (map.mapping[ESCAPE] instanceof KeyMap) {
//                        map = (KeyMap) map.mapping[ESCAPE];
//                    }
//                }
                if (i < keySeq.length() - 1) {
                    if (!(map.mapping[c] instanceof KeyMap)) {
                        KeyMap m = new KeyMap();
                        if (map.mapping[c] != Operation.DO_LOWERCASE_VERSION) {
                            m.anotherKey = map.mapping[c];
                        }
                        map.mapping[c] = m;
                    }
                    map = (KeyMap) map.mapping[c];
                } else {
                    if (function == null) {
                        function = NULL_FUNCTION;
                    }
                    if (map.mapping[c] instanceof KeyMap) {
                        map.anotherKey = function;
                    } else {
                        Object op = map.mapping[c];
                        if ( op == null || op == Operation.DO_LOWERCASE_VERSION || op == Operation.VI_MOVEMENT_MODE ) {
                            map.mapping[c] = function;
                        }
                    }
                }
            }
        }
    }

    public void bind( CharSequence keySeq, Object function ) {
        if (keySeq != null && keySeq.length() > 0) {
            KeyMap map = this;
            for (int i = 0; i < keySeq.length(); i++) {
                char c = keySeq.charAt(i);
                if (c >= map.mapping.length) {
                    return;
                }
//                if (isMeta(c) && isConvertMetaCharsToAscii()) {
//                    c = unMeta( c );
//                    if (map.mapping[ESCAPE] instanceof KeyMap) {
//                        map = (KeyMap) map.mapping[ESCAPE];
//                    }
//                }
                if (i < keySeq.length() - 1) {
                    if (!(map.mapping[c] instanceof KeyMap)) {
                        KeyMap m = new KeyMap();
                        if (map.mapping[c] != Operation.DO_LOWERCASE_VERSION) {
                            m.anotherKey = map.mapping[c];
                        }
                        map.mapping[c] = m;
                    }
                    map = (KeyMap) map.mapping[c];
                } else {
                    if (function == null) {
                        function = NULL_FUNCTION;
                    }
                    if (map.mapping[c] instanceof KeyMap) {
                        map.anotherKey = function;
                    } else {
                        map.mapping[c] = function;
                    }
                }
            }
        }
    }

    public void bindArrowKeys() {
        // MS-DOS
        bindIfNotBound( "\033[0A", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\033[0B", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\033[0C", Operation.FORWARD_CHAR );
        bindIfNotBound( "\033[0D", Operation.NEXT_HISTORY );

        // Windows
        bindIfNotBound( "\340\000", Operation.KILL_WHOLE_LINE );
        bindIfNotBound( "\340\107", Operation.BEGINNING_OF_LINE );
        bindIfNotBound( "\340\110", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\340\111", Operation.BEGINNING_OF_HISTORY );
        bindIfNotBound( "\340\113", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\340\115", Operation.FORWARD_CHAR );
        bindIfNotBound( "\340\117", Operation.END_OF_LINE );
        bindIfNotBound( "\340\120", Operation.NEXT_HISTORY );
        bindIfNotBound( "\340\121", Operation.END_OF_HISTORY );
        bindIfNotBound( "\340\122", Operation.OVERWRITE_MODE );
        bindIfNotBound( "\340\123", Operation.DELETE_CHAR );
        bindIfNotBound( "\000\110", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\000\113", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\000\115", Operation.FORWARD_CHAR );
        bindIfNotBound( "\000\120", Operation.NEXT_HISTORY );
        bindIfNotBound( "\000\123", Operation.DELETE_CHAR );

        bindIfNotBound( "\033[A", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\033[B", Operation.NEXT_HISTORY );
        bindIfNotBound( "\033[C", Operation.FORWARD_CHAR );
        bindIfNotBound( "\033[D", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\033[H", Operation.BEGINNING_OF_LINE );
        bindIfNotBound( "\033[F", Operation.END_OF_LINE );

        bindIfNotBound( "\033[OA", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\033[OB", Operation.NEXT_HISTORY );
        bindIfNotBound( "\033[OC", Operation.FORWARD_CHAR );
        bindIfNotBound( "\033[OD", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\033[OH", Operation.BEGINNING_OF_LINE );
        bindIfNotBound( "\033[OF", Operation.END_OF_LINE );

        // MINGW32
        bindIfNotBound( "\0340H", Operation.PREVIOUS_HISTORY );<commit_msg>fixed binding of Delete key on OS X / Linux<commit_after>package jline.console;

import java.util.HashMap;
import java.util.Map;

public class KeyMap {

    private static final int KEYMAP_LENGTH = 256;

    private static final Object NULL_FUNCTION = new Object();

    private Object[] mapping = new Object[KEYMAP_LENGTH];
    private Object anotherKey = null;
//    private boolean convertMetaCharsToAscii = false;


    public KeyMap() {
        this(new Object[KEYMAP_LENGTH]);
    }

    protected KeyMap(Object[] mapping) {
        this.mapping = mapping;
    }

    public Object getAnotherKey() {
        return anotherKey;
    }

    public void from(KeyMap other) {
        this.mapping = other.mapping;
        this.anotherKey = other.anotherKey;
    }

    public Object getBound( CharSequence keySeq ) {
        if (keySeq != null && keySeq.length() > 0) {
            KeyMap map = this;
            for (int i = 0; i < keySeq.length(); i++) {
                char c = keySeq.charAt(i);
//                if (isMeta(c) && isConvertMetaCharsToAscii()) {
//                    if (map.mapping[ESCAPE] instanceof KeyMap) {
//                        map = (KeyMap) map.mapping[ESCAPE];
//                        c = unMeta(c);
//                    } else {
//                        return map.mapping[c];
//                    }
//                }
                if (c > 255) {
                    return Operation.SELF_INSERT;
                }
                if (map.mapping[c] instanceof KeyMap) {
                    if (i == keySeq.length() - 1) {
                        return map.mapping[c];
                    } else {
                        map = (KeyMap) map.mapping[c];
                    }
                } else {
                    return map.mapping[c];
                }
            }
        }
        return null;
    }

    public void bindIfNotBound( CharSequence keySeq, Object function ) {
        if (keySeq != null && keySeq.length() > 0) {
            KeyMap map = this;
            for (int i = 0; i < keySeq.length(); i++) {
                char c = keySeq.charAt(i);
                if (c >= map.mapping.length) {
                    return;
                }
//                if (isMeta(c) && isConvertMetaCharsToAscii()) {
//                    c = unMeta( c );
//                    if (map.mapping[ESCAPE] instanceof KeyMap) {
//                        map = (KeyMap) map.mapping[ESCAPE];
//                    }
//                }
                if (i < keySeq.length() - 1) {
                    if (!(map.mapping[c] instanceof KeyMap)) {
                        KeyMap m = new KeyMap();
                        if (map.mapping[c] != Operation.DO_LOWERCASE_VERSION) {
                            m.anotherKey = map.mapping[c];
                        }
                        map.mapping[c] = m;
                    }
                    map = (KeyMap) map.mapping[c];
                } else {
                    if (function == null) {
                        function = NULL_FUNCTION;
                    }
                    if (map.mapping[c] instanceof KeyMap) {
                        map.anotherKey = function;
                    } else {
                        Object op = map.mapping[c];
                        if ( op == null || op == Operation.DO_LOWERCASE_VERSION || op == Operation.VI_MOVEMENT_MODE ) {
                            map.mapping[c] = function;
                        }
                    }
                }
            }
        }
    }

    public void bind( CharSequence keySeq, Object function ) {
        if (keySeq != null && keySeq.length() > 0) {
            KeyMap map = this;
            for (int i = 0; i < keySeq.length(); i++) {
                char c = keySeq.charAt(i);
                if (c >= map.mapping.length) {
                    return;
                }
//                if (isMeta(c) && isConvertMetaCharsToAscii()) {
//                    c = unMeta( c );
//                    if (map.mapping[ESCAPE] instanceof KeyMap) {
//                        map = (KeyMap) map.mapping[ESCAPE];
//                    }
//                }
                if (i < keySeq.length() - 1) {
                    if (!(map.mapping[c] instanceof KeyMap)) {
                        KeyMap m = new KeyMap();
                        if (map.mapping[c] != Operation.DO_LOWERCASE_VERSION) {
                            m.anotherKey = map.mapping[c];
                        }
                        map.mapping[c] = m;
                    }
                    map = (KeyMap) map.mapping[c];
                } else {
                    if (function == null) {
                        function = NULL_FUNCTION;
                    }
                    if (map.mapping[c] instanceof KeyMap) {
                        map.anotherKey = function;
                    } else {
                        map.mapping[c] = function;
                    }
                }
            }
        }
    }

    public void bindArrowKeys() {
        // MS-DOS
        bindIfNotBound( "\033[0A", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\033[0B", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\033[0C", Operation.FORWARD_CHAR );
        bindIfNotBound( "\033[0D", Operation.NEXT_HISTORY );

        // Windows
        bindIfNotBound( "\340\000", Operation.KILL_WHOLE_LINE );
        bindIfNotBound( "\340\107", Operation.BEGINNING_OF_LINE );
        bindIfNotBound( "\340\110", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\340\111", Operation.BEGINNING_OF_HISTORY );
        bindIfNotBound( "\340\113", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\340\115", Operation.FORWARD_CHAR );
        bindIfNotBound( "\340\117", Operation.END_OF_LINE );
        bindIfNotBound( "\340\120", Operation.NEXT_HISTORY );
        bindIfNotBound( "\340\121", Operation.END_OF_HISTORY );
        bindIfNotBound( "\340\122", Operation.OVERWRITE_MODE );
        bindIfNotBound( "\340\123", Operation.DELETE_CHAR );
        bindIfNotBound( "\000\110", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\000\113", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\000\115", Operation.FORWARD_CHAR );
        bindIfNotBound( "\000\120", Operation.NEXT_HISTORY );
        bindIfNotBound( "\000\123", Operation.DELETE_CHAR );

        bindIfNotBound( "\033[A", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\033[B", Operation.NEXT_HISTORY );
        bindIfNotBound( "\033[C", Operation.FORWARD_CHAR );
        bindIfNotBound( "\033[D", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\033[H", Operation.BEGINNING_OF_LINE );
        bindIfNotBound( "\033[F", Operation.END_OF_LINE );

        bindIfNotBound( "\033[OA", Operation.PREVIOUS_HISTORY );
        bindIfNotBound( "\033[OB", Operation.NEXT_HISTORY );
        bindIfNotBound( "\033[OC", Operation.FORWARD_CHAR );
        bindIfNotBound( "\033[OD", Operation.BACKWARD_CHAR );
        bindIfNotBound( "\033[OH", Operation.BEGINNING_OF_LINE );
        bindIfNotBound( "\033[OF", Operation.END_OF_LINE );

        bindIfNotBound( "\033[3~", Operation.DELETE_CHAR);

        // MINGW32
        bindIfNotBound( "\0340H", Operation.PREVIOUS_HISTORY );
{'fraction_non_alphanumeric': 0.1192002229343737, 'fraction_numerical': 0.023826111188518878, 'mean_word_length': 1.3536645351696999, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1444803', 'n_tokens_mistral': 4384, 'n_tokens_neox': 3896, 'n_words': 795}