Skip to main content
complete :endif
Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=, or using <expr> when defining the abbreviation) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

When you find that "a" word, then you can expand it to a <C-w>, that will delete that word, followed by the abbreviation you desire.

You can use this function to implement all the "à" (named crase) abbreviations in Portuguese:

function Crase()
    " Get the list of words until this point,
    " including the one being abbreviated.
    let words = split(getline('.')[0:col('.')-2])
    " By default, return the word itself.
    let replacement = words[-1]
    " Check if a word 'a' precedes it.
    if len(words) >=# 2 && words[-2] ==? 'a'
        " Handle capitalization.
        if words[-2] ==# 'A' || words[-1] =~# '^A'
            let replacement = substitute(replacement, '^\ca', 'À', '')
        else
            let replacement = substitute(replacement, '^\ca', 'à', '')
        endendif
        " Remove the 'a' word and
        " contract the 'crase'.
        return "\<C-w>".replacement
    endendif
    " Otherwise, no change.
    return replacement
endfunction

It looks at the sentence so far, then takes the two last words in it (when using iab <expr>, you'll get the word being abbreviated as well.) Then it checks whether the second last word was an "a" or an "A". If that's the case, it will then remove that word (using the "\<C-w>" at the beginning of the expansion), then return the word being abbreviated, just replacing the first character (which must be an "a", lower or uppercase!) with an "à" or an "À".

The function does proper capitalization, so "A aquele" will be expanded to "Àquele", as expected.

Use the function as follows:

inoreabbrev <expr> aquele Crase()
inoreabbrev <expr> Aquele Crase()
inoreabbrev <expr> AQUELE Crase()
inoreabbrev <expr> aquela Crase()
inoreabbrev <expr> Aquela Crase()
inoreabbrev <expr> AQUELA Crase()
inoreabbrev <expr> aquilo Crase()
inoreabbrev <expr> Aquilo Crase()
inoreabbrev <expr> AQUILO Crase()

You can even use it for "a a", turn it into "à", with:

inoreabbrev <expr> a Crase()
inoreabbrev <expr> A Crase()

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=, or using <expr> when defining the abbreviation) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

When you find that "a" word, then you can expand it to a <C-w>, that will delete that word, followed by the abbreviation you desire.

You can use this function to implement all the "à" (named crase) abbreviations in Portuguese:

function Crase()
    " Get the list of words until this point,
    " including the one being abbreviated.
    let words = split(getline('.')[0:col('.')-2])
    " By default, return the word itself.
    let replacement = words[-1]
    " Check if a word 'a' precedes it.
    if len(words) >=# 2 && words[-2] ==? 'a'
        " Handle capitalization.
        if words[-2] ==# 'A' || words[-1] =~# '^A'
            let replacement = substitute(replacement, '^\ca', 'À', '')
        else
            let replacement = substitute(replacement, '^\ca', 'à', '')
        end
        " Remove the 'a' word and
        " contract the 'crase'.
        return "\<C-w>".replacement
    end
    " Otherwise, no change.
    return replacement
endfunction

It looks at the sentence so far, then takes the two last words in it (when using iab <expr>, you'll get the word being abbreviated as well.) Then it checks whether the second last word was an "a" or an "A". If that's the case, it will then remove that word (using the "\<C-w>" at the beginning of the expansion), then return the word being abbreviated, just replacing the first character (which must be an "a", lower or uppercase!) with an "à" or an "À".

The function does proper capitalization, so "A aquele" will be expanded to "Àquele", as expected.

Use the function as follows:

inoreabbrev <expr> aquele Crase()
inoreabbrev <expr> Aquele Crase()
inoreabbrev <expr> AQUELE Crase()
inoreabbrev <expr> aquela Crase()
inoreabbrev <expr> Aquela Crase()
inoreabbrev <expr> AQUELA Crase()
inoreabbrev <expr> aquilo Crase()
inoreabbrev <expr> Aquilo Crase()
inoreabbrev <expr> AQUILO Crase()

You can even use it for "a a", turn it into "à", with:

inoreabbrev <expr> a Crase()
inoreabbrev <expr> A Crase()

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=, or using <expr> when defining the abbreviation) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

When you find that "a" word, then you can expand it to a <C-w>, that will delete that word, followed by the abbreviation you desire.

You can use this function to implement all the "à" (named crase) abbreviations in Portuguese:

function Crase()
    " Get the list of words until this point,
    " including the one being abbreviated.
    let words = split(getline('.')[0:col('.')-2])
    " By default, return the word itself.
    let replacement = words[-1]
    " Check if a word 'a' precedes it.
    if len(words) >=# 2 && words[-2] ==? 'a'
        " Handle capitalization.
        if words[-2] ==# 'A' || words[-1] =~# '^A'
            let replacement = substitute(replacement, '^\ca', 'À', '')
        else
            let replacement = substitute(replacement, '^\ca', 'à', '')
        endif
        " Remove the 'a' word and
        " contract the 'crase'.
        return "\<C-w>".replacement
    endif
    " Otherwise, no change.
    return replacement
endfunction

It looks at the sentence so far, then takes the two last words in it (when using iab <expr>, you'll get the word being abbreviated as well.) Then it checks whether the second last word was an "a" or an "A". If that's the case, it will then remove that word (using the "\<C-w>" at the beginning of the expansion), then return the word being abbreviated, just replacing the first character (which must be an "a", lower or uppercase!) with an "à" or an "À".

The function does proper capitalization, so "A aquele" will be expanded to "Àquele", as expected.

Use the function as follows:

inoreabbrev <expr> aquele Crase()
inoreabbrev <expr> Aquele Crase()
inoreabbrev <expr> AQUELE Crase()
inoreabbrev <expr> aquela Crase()
inoreabbrev <expr> Aquela Crase()
inoreabbrev <expr> AQUELA Crase()
inoreabbrev <expr> aquilo Crase()
inoreabbrev <expr> Aquilo Crase()
inoreabbrev <expr> AQUILO Crase()

You can even use it for "a a", turn it into "à", with:

inoreabbrev <expr> a Crase()
inoreabbrev <expr> A Crase()
added 1694 characters in body; added 70 characters in body
Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=, or using <expr> when defining the abbreviation) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

This command-line does what you want for "àquilo"You can use this function to implement all the "à" (named crase) abbreviations in Portuguese:

:iabfunction aquiloCrase()
 <C-r>=match   " Get the list of words until this point,
    " including the one being abbreviated.
    let words = split(getline('.')[0:col('.')-2])
    " By default, '\<lt>areturn $'the word itself.
    let replacement = words[-1]
    " Check if a word 'a' precedes it.
    if len(words) >=>=# 02 && words[-2] ==? "\<lt>C'a'
        " Handle capitalization.
        if words[-w>àquilo"2] :==# 'aquilo'<CR>'A' || words[-1] =~# '^A'
            let replacement = substitute(replacement, '^\ca', 'À', '')
        else
            let replacement = substitute(replacement, '^\ca', 'à', '')
        end
        " Remove the 'a' word and
        " contract the 'crase'.
        return "\<C-w>".replacement
    end
    " Otherwise, no change.
    return replacement
endfunction

But it's pretty awkward.It looks at the sentence so far, then takes the two last words in it TODO: Let me replace(when using iab <expr>, you'll get the word being abbreviated as well.) Then it checks whether the second last word was an "a" or an "A". If that's the case, it will then remove that word (using the "\<C-w>" at the beginning of the expansion), then return the word being abbreviated, just replacing the first character (which must be an "a", lower or uppercase!) with aan "à" or an "À".

The function does proper capitalization, whichso "A aquele" will make it cleanerbe expanded to "Àquele", as expected.

Use the function as follows:

inoreabbrev <expr> aquele Crase()
inoreabbrev <expr> Aquele Crase()
inoreabbrev <expr> AQUELE Crase()
inoreabbrev <expr> aquela Crase()
inoreabbrev <expr> Aquela Crase()
inoreabbrev <expr> AQUELA Crase()
inoreabbrev <expr> aquilo Crase()
inoreabbrev <expr> Aquilo Crase()
inoreabbrev <expr> AQUILO Crase()

You can even use it for "a a", turn it into "à", with:

inoreabbrev <expr> a Crase()
inoreabbrev <expr> A Crase()

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

This command-line does what you want for "àquilo":

:iab aquilo <C-r>=match(getline('.')[0:col('.')-2], '\<lt>a $') >= 0 ? "\<lt>C-w>àquilo" : 'aquilo'<CR>

But it's pretty awkward. TODO: Let me replace that with a function, which will make it cleaner.

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=, or using <expr> when defining the abbreviation) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

You can use this function to implement all the "à" (named crase) abbreviations in Portuguese:

function Crase()
    " Get the list of words until this point,
    " including the one being abbreviated.
    let words = split(getline('.')[0:col('.')-2])
    " By default, return the word itself.
    let replacement = words[-1]
    " Check if a word 'a' precedes it.
    if len(words) >=# 2 && words[-2] ==? 'a'
        " Handle capitalization.
        if words[-2] ==# 'A' || words[-1] =~# '^A'
            let replacement = substitute(replacement, '^\ca', 'À', '')
        else
            let replacement = substitute(replacement, '^\ca', 'à', '')
        end
        " Remove the 'a' word and
        " contract the 'crase'.
        return "\<C-w>".replacement
    end
    " Otherwise, no change.
    return replacement
endfunction

It looks at the sentence so far, then takes the two last words in it (when using iab <expr>, you'll get the word being abbreviated as well.) Then it checks whether the second last word was an "a" or an "A". If that's the case, it will then remove that word (using the "\<C-w>" at the beginning of the expansion), then return the word being abbreviated, just replacing the first character (which must be an "a", lower or uppercase!) with an "à" or an "À".

The function does proper capitalization, so "A aquele" will be expanded to "Àquele", as expected.

Use the function as follows:

inoreabbrev <expr> aquele Crase()
inoreabbrev <expr> Aquele Crase()
inoreabbrev <expr> AQUELE Crase()
inoreabbrev <expr> aquela Crase()
inoreabbrev <expr> Aquela Crase()
inoreabbrev <expr> AQUELA Crase()
inoreabbrev <expr> aquilo Crase()
inoreabbrev <expr> Aquilo Crase()
inoreabbrev <expr> AQUILO Crase()

You can even use it for "a a", turn it into "à", with:

inoreabbrev <expr> a Crase()
inoreabbrev <expr> A Crase()
Source Link
filbranden
  • 29.9k
  • 5
  • 28
  • 73

You can accomplish what you're trying to do by using an expression (introduced using <C-r>=) and then matching a single "a" word at the end of the text that precedes the word being abbreviated.

When you find that "a" word, then you can expand it to a <C-w>, that will delete that word, followed by the abbreviation you desire.

This command-line does what you want for "àquilo":

:iab aquilo <C-r>=match(getline('.')[0:col('.')-2], '\<lt>a $') >= 0 ? "\<lt>C-w>àquilo" : 'aquilo'<CR>

But it's pretty awkward. TODO: Let me replace that with a function, which will make it cleaner.