Bloquear movimento para qualquer tipo de suporte?

8

Se eu quiser excluir um bloco, posso usar movimentos de objeto de texto.

Ou seja, se meu texto se parece com isso:

(let [a 1 b {:x 3  :y 4}]
      a)

E o cursor está, por exemplo, no caractere 3 .

Se eu digitar diB , então :x 3 :y 4 será excluído Se for daB , o bloco e os parênteses circundantes serão excluídos: {:x 3 :y 4}

Então, o padrão é:

operation inclusion block-motion

Onde a operação pode ser:

  • d - excluir

  • c - alterar

  • y - copie ...

a inclusão é:

  • i - inner (sem colchetes) ou

  • a - all

e bloco de movimento:

  • b , ( ou ) para () parens

  • B , { ou } para {} curlies

  • [ ou ] e < ou > para seus respectivos colchetes, etc.

Agora, a pergunta é: Existe uma movimentação em bloco para o bloco mais interno com colchetes de qualquer um desses tipos?

Eu gostaria de poder fazer da? com ? sendo a moção que estou procurando. E se o cursor no meu exemplo acima estiver dentro de {} em 3 , eu excluiria apenas o {} , mas se meu cursor estivesse em b , eu excluiria o bloco [] , etc.

    
por Goran Jovic 16.12.2010 / 02:04

3 respostas

3

Eis porque a pergunta deveria estar em SO: é necessário um script não trivial ...

" Public Mappings {{{1
onoremap <silent> i% :<c-u>call <sid>SelectFirstPair(1,0)<cr>
xnoremap <silent> i% :<c-u>call <sid>SelectFirstPair(1,1)<cr><esc>gv
onoremap <silent> a% :<c-u>call <sid>SelectFirstPair(0,0)<cr>
xnoremap <silent> a% :<c-u>call <sid>SelectFirstPair(0,1)<cr><esc>gv
" Public Mappings }}}1
"------------------------------------------------------------------------
" Private Functions {{{1
" Note: most functions are best placed into
" autoload/«your-initials»/«omap_any_bracket».vim
" Keep here only the functions are are required when the plugin is loaded,
" like functions that help building a vim-menu for this plugin.
let s:k_pairs = {
      \ '(': ')',
      \ '[': ']',
      \ '{': '}',
      \ '<': '>'
      \ }

let s:k_begin = '[([{<]'
let s:k_end   = '[)\]}>]'

function! s:SelectFirstPair(inner, visual)
  " In case we already are in visual mode, we may have to extend the current
  " zone if it selects a pair of brackets
  if a:visual
    let char_b = lh#position#char_at_mark("'<")
    if char_b =~ s:k_begin
      \ && s:k_pairs[char_b] == lh#position#char_at_mark("'>")
      call search('.', 'bW') " previous char
    elseif a:inner
      " handle case the case "vi%i%i%"
      let current_pos = getpos('.')
      call setpos('.', getpos("'<"))
      call search('.', 'bW') " previous char
      let pos_b = getpos('.')
      call setpos('.', getpos("'>"))
      call search('.', 'W') " next char
      let pos_e = getpos('.')
      let char_b = lh#position#char_at_pos(pos_b)
      let char_e = lh#position#char_at_pos(pos_e)
      echomsg "chars = ".char_b.char_e
      if char_b =~ s:k_begin
        \ && s:k_pairs[char_b] == char_e
    call setpos('.', pos_b) " restore start_pos
    call search('.', 'bW') " previous char
      else
    call setpos('.', current_pos) " restore init_pos
      endif
    endif
  endif

  " Searching the n outer blocks requested
  let cnt = v:count <= 0 ? 1 : v:count
  while cnt > 0
    let cnt -= 1
    let char_c = lh#position#char_at_pos(getpos('.'))
    let accept_at_current = char_c =~ s:k_begin ? 'c' : ''

    " Begin of the current outer block
    if 0 ==searchpair(s:k_begin, '', s:k_end, 'bW'.accept_at_current, 'lh#syntax#skip()')
      throw "No outer bloc"
    endif
    if cnt > 0
      call search('.', 'bW') " previous char
    endif
  endwhile

  let char_b = lh#position#char_at_pos(getpos('.'))

  normal! v

  " End of the outer block
  let pos_e = searchpair(s:k_begin, '', s:k_end, 'W', 'lh#syntax#skip()')
  let char_e = lh#position#char_at_pos(getpos('.'))
  if pos_e == 0
    throw "pos_e == 0"
  elseif s:k_pairs[char_b] != char_e
    echomsg "unbalanced blocks"
  endif

  " Adjusting the extremities
  if a:inner
    call search('.', 'b')
    normal! o
    call search('.')
    normal! o
  endif
endfunction
" Private Functions }}}1

NB: reutilizei a função de lh-vim-lib - BTW, há um pequeno bug na versão de lh#position#char_at_pos() em conf: col() não deve ser usado.

    
por 21.12.2010 / 12:42
1

Não por padrão, mas pode haver algum mecanismo para adicionar essa funcionalidade. No visual.txt, a seção sobre como operar na área visual, tem isto:

The objects that can be used are:
    aw      a word (with white space)                       |v_aw|
    iw      inner word                                      |v_iw|
    aW      a WORD (with white space)                       |v_aW|
    iW      inner WORD                                      |v_iW|
    as      a sentence (with white space)                   |v_as|
    is      inner sentence                                  |v_is|
    ap      a paragraph (with white space)                  |v_ap|
    ip      inner paragraph                                 |v_ip|
    ab      a () block (with parenthesis)                   |v_ab|
    ib      inner () block                                  |v_ib|
    aB      a {} block (with braces)                        |v_aB|
    iB      inner {} block                                  |v_iB|
    at      a <tag> </tag> block (with tags)                |v_at|
    it      inner <tag> </tag> block                        |v_it|
    a<      a <> block (with <>)                            |v_a<|
    i<      inner <> block                                  |v_i<|
    a[      a [] block (with [])                            |v_a[|
    i[      inner [] block                                  |v_i[|
    a"      a double quoted string (with quotes)            |v_aquote|
    a'      a single quoted string (with quotes)            |v_a'|
    i'      inner simple quoted string                      |v_i'|
    a'      a string in backticks (with backticks)          |v_a'|
    i'      inner string in backticks                       |v_i'|
    
por 16.12.2010 / 03:06
0

Há um addon vim chamado textobj-user que suporta ... hum, algo assim . Na verdade, não tenho certeza se entendi o que você está procurando, mas acho que o addon foi feito para tornar mais conveniente escrever um addon para implementar o que você deseja.

    
por 04.04.2011 / 09:35

Tags