<commit_before># A widget that auto-inserts matching pairs in ZSH.

# Stops zsh-autopair from auto-binding keymaps
AUTOPAIR_INHIBIT_INIT=
AUTOPAIR_BETWEEN_WHITESPACE=

typeset -A AUTOPAIR_PAIRS
AUTOPAIR_PAIRS=('`' '`' "'" "'" '"' '"' '{' '}' '[' ']' '(' ')' '<' '>')

typeset -A AUTOPAIR_LBOUNDS
AUTOPAIR_LBOUNDS=(\
    all '[.:/\!]' \
    quotes '[]})a-zA-Z0-9]' \
    braces '' \
    '"' '"' \
    "'" "'" \
    '`' '`')

typeset -A AUTOPAIR_RBOUNDS
AUTOPAIR_RBOUNDS=(\
    all '[[{(<,.:?/%$!a-zA-Z0-9]' \
    quotes '[a-zA-Z0-9]' \
    braces '')

####

ap-boundary-p() {
    [[ -n "$1" && "$LBUFFER" =~ "${1}$" || -n "$2" && "$RBUFFER" =~ "^${2}" ]]
}
ap-next-to-boundary-p() {
    local groups=(all)
    case "$1" in
        \'|\"|\`)    groups+=quotes ;;
        \{|\[|\(|\<) groups+=braces ;;
    esac
    groups+="$1"
    for group in $groups; do
        ap-boundary-p "$AUTOPAIR_LBOUNDS[$group]" "$AUTOPAIR_RBOUNDS[$group]" && return 0
    done
    return 1
}

# If provided pair is balanced in the buffer
ap-balanced-p() {
    local lbuf="${LBUFFER//\\$1}"
    local rbuf="${RBUFFER//\\$2}"
    local llen="${#lbuf//[^$1]}"
    local rlen="${#rbuf//[^$2]}"
    (( $rlen == 0 && $llen == 0 )) && return 0
    if [[ "$1" == "$2" ]]; then
        (( $llen == $rlen || ($llen + $rlen) % 2 == 0 )) && return 0
    else
        local l2len="${#lbuf//[^$2]}"
        local r2len="${#rbuf//[^$1]}"
        local ltotal=$(( $llen - $l2len ))
        local rtotal=$(( $rlen - $r2len ))

        (( $ltotal < 0 )) && ltotal=0
        (( $ltotal < $rtotal )) && return 1
        return 0
    fi
    return 1
}

ap-can-pair-p() {
    local rchar="$AUTOPAIR_PAIRS[$KEYS]"

    # Don't pair if pair doesn't exist
    [[ -z "$rchar" ]] && return 1

    # Force pair if surrounded by space/[BE]OL, regardless of boundaries/balance
    [[ -n "$AUTOPAIR_BETWEEN_WHITESPACE" && \
        "$LBUFFER" =~ "(^|[ 	])$" && \
        "$RBUFFER" =~ "^($|[ 	])" ]] && return 0

    # Don't pair quotes if the delimiters are unbalanced
    ! ap-balanced-p $KEYS $rchar && return 1

    # Don't pair when in front of characters that likely signify the start of a string
    # or path (i.e. boundary characters)
    ap-next-to-boundary-p "$KEYS" "$rchar" && return 1

    return 0
}

ap-can-skip-p() {
    [[ -n "$2" && "$RBUFFER[1]" == "$2" ]] # && ap-balanced-p "$1" "$2"
}

ap-can-delete-p() {
    local lchar="$LBUFFER[-1]"
    local rchar="$AUTOPAIR_PAIRS[$lchar]"
    [[ -n "$rchar" && "$RBUFFER[1]" == "$rchar" ]] && ap-balanced-p "$lchar" "$rchar"
}

typeset -A AUTOPAIR_REVERSE_PAIRS
for i in ${(@k)AUTOPAIR_PAIRS}; do AUTOPAIR_REVERSE_PAIRS["$AUTOPAIR_PAIRS[$i]"]="$i"; done
typeset -r AUTOPAIR_REVERSE_PAIRS

autopair-self-insert() {
    LBUFFER+="$1$2"
    zle .backward-char
}

autopair-insert() {
    local rchar="$AUTOPAIR_PAIRS[$KEYS]"
    if [[ $KEYS == (\'|\"|\`) ]] && ap-can-skip-p "$KEYS" "$rchar"; then
        zle .forward-char
    elif ap-can-pair-p; then
        autopair-self-insert "$KEYS" "$rchar"
    else
        zle .self-insert
    fi
}
<commit_msg>Do balance checks when skipping/deleting quotes<commit_after># A widget that auto-inserts matching pairs in ZSH.

# Stops zsh-autopair from auto-binding keymaps
AUTOPAIR_INHIBIT_INIT=
AUTOPAIR_BETWEEN_WHITESPACE=

typeset -A AUTOPAIR_PAIRS
AUTOPAIR_PAIRS=('`' '`' "'" "'" '"' '"' '{' '}' '[' ']' '(' ')' '<' '>')

typeset -A AUTOPAIR_LBOUNDS
AUTOPAIR_LBOUNDS=(\
    all '[.:/\!]' \
    quotes '[]})a-zA-Z0-9]' \
    braces '' \
    '"' '"' \
    "'" "'" \
    '`' '`')

typeset -A AUTOPAIR_RBOUNDS
AUTOPAIR_RBOUNDS=(\
    all '[[{(<,.:?/%$!a-zA-Z0-9]' \
    quotes '[a-zA-Z0-9]' \
    braces '')

####

ap-boundary-p() {
    [[ -n "$1" && "$LBUFFER" =~ "${1}$" || -n "$2" && "$RBUFFER" =~ "^${2}" ]]
}
ap-next-to-boundary-p() {
    local groups=(all)
    case "$1" in
        \'|\"|\`)    groups+=quotes ;;
        \{|\[|\(|\<) groups+=braces ;;
    esac
    groups+="$1"
    for group in $groups; do
        ap-boundary-p "$AUTOPAIR_LBOUNDS[$group]" "$AUTOPAIR_RBOUNDS[$group]" && return 0
    done
    return 1
}

# If provided pair is balanced in the buffer
ap-balanced-p() {
    local lbuf="${LBUFFER//\\$1}"
    local rbuf="${RBUFFER//\\$2}"
    local llen="${#lbuf//[^$1]}"
    local rlen="${#rbuf//[^$2]}"
    (( $rlen == 0 && $llen == 0 )) && return 0
    if [[ "$1" == "$2" ]]; then
        (( $llen == $rlen || ($llen + $rlen) % 2 == 0 )) && return 0
    else
        local l2len="${#lbuf//[^$2]}"
        local r2len="${#rbuf//[^$1]}"
        local ltotal=$(( $llen - $l2len ))
        local rtotal=$(( $rlen - $r2len ))

        (( $ltotal < 0 )) && ltotal=0
        (( $ltotal < $rtotal )) && return 1
        return 0
    fi
    return 1
}

ap-can-pair-p() {
    local rchar="$AUTOPAIR_PAIRS[$KEYS]"

    # Don't pair if pair doesn't exist
    [[ -z "$rchar" ]] && return 1

    # Force pair if surrounded by space/[BE]OL, regardless of boundaries/balance
    [[ -n "$AUTOPAIR_BETWEEN_WHITESPACE" && \
        "$LBUFFER" =~ "(^|[ 	])$" && \
        "$RBUFFER" =~ "^($|[ 	])" ]] && return 0

    # Don't pair quotes if the delimiters are unbalanced
    ! ap-balanced-p $KEYS $rchar && return 1

    # Don't pair when in front of characters that likely signify the start of a string
    # or path (i.e. boundary characters)
    ap-next-to-boundary-p "$KEYS" "$rchar" && return 1

    return 0
}

ap-can-skip-p() {
    ! [[ -n "$2" && "$RBUFFER[1]" == "$2" && "$LBUFFER[-1]" != '\' ]] && return 1
    [[ "$1" == "$2" ]] && ! ap-balanced-p "$1" "$2" && return 1
    return 0
}

ap-can-delete-p() {
    local lchar="$LBUFFER[-1]"
    local rchar="$AUTOPAIR_PAIRS[$lchar]"
    ! [[ -n "$rchar" && "$RBUFFER[1]" == "$rchar" ]] && return 1
    [[ "$lchar" == "$rchar" ]] && ! ap-balanced-p "$lchar" "$rchar" && return 1
    return 0
}

typeset -A AUTOPAIR_REVERSE_PAIRS
for i in ${(@k)AUTOPAIR_PAIRS}; do AUTOPAIR_REVERSE_PAIRS["$AUTOPAIR_PAIRS[$i]"]="$i"; done
typeset -r AUTOPAIR_REVERSE_PAIRS

autopair-self-insert() {
    LBUFFER+="$1$2"
    zle .backward-char
}

autopair-insert() {
    local rchar="$AUTOPAIR_PAIRS[$KEYS]"
    if [[ $KEYS == (\'|\"|\`) ]] && ap-can-skip-p "$KEYS" "$rchar"; then
        zle .forward-char
    elif ap-can-pair-p; then
        autopair-self-insert "$KEYS" "$rchar"
    else
        zle .self-insert
    fi
}

{'fraction_non_alphanumeric': 0.23213436856282682, 'fraction_numerical': 0.017588337822848993, 'mean_word_length': 2.6027397260273974, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '47611', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2580, 'n_words': 532}